PSCF v1.3
MixAndMatchEnvTmpl.tpp
1#ifndef PSCF_MIX_AND_MATCH_ENV_TMPL_TPP
2#define PSCF_MIX_AND_MATCH_ENV_TMPL_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2025, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/param/Label.h>
12
13namespace Pscf {
14
15 /*
16 * Constructor.
17 */
18 template <class Env, class FG>
20 : fieldGenPtr1_(nullptr),
21 fieldGenPtr2_(nullptr)
22 { setClassName("MixAndMatchEnvTmpl"); }
23
24 /*
25 * Destructor.
26 */
27 template <class Env, class FG>
29 {
30 if (fieldGenPtr1_ != nullptr) {
31 delete fieldGenPtr1_;
32 }
33 if (fieldGenPtr2_ != nullptr) {
34 delete fieldGenPtr2_;
35 }
36 }
37
38 /*
39 * Read parameters from input stream.
40 *
41 * Parameters for field generators are read at the same indentation level
42 * as parameters of the parent MaxAndMatchEnv object, without enclosed curly
43 * bracket delimiters around blocks for each generator.
44 *
45 * If the second generator is "dependent", it backs up and re-reads the
46 * block of the first generator.
47 */
48 template <class Env, class FG>
50 {
51 bool generatesMask(false), generatesExt(false);
52
53 // Before reading parameters, create FieldGenerator objects
55
56 // Save current istream position (allows for possible rewind to here)
57 std::streampos pos = in.tellg();
58
59 // Read first FieldGenerator
60 if (fieldGenPtr1_ != nullptr) {
61
62 UTIL_CHECK(!fieldGenPtr1_->isDependent());
63
64 // Make fieldGenPtr1_ a child ParamComponent of this object
65 bool next = false; // Set indentation to be same as that of parent
68 // Read parameters for this FieldGenerator.
69 // Reads body without indentation or curly bracket delimiters
70 fieldGenPtr1_->readParameters(in);
71
72 if (fieldGenPtr1_->type() == FG::Mask) {
73 generatesMask = true;
74 } else if (fieldGenPtr1_->type() == FG::Mask) {
75 generatesExt = true;
76 } else {
77 UTIL_THROW("fieldGenPtr1_ must have type Mask or External.");
78 }
80 } else {
81
82 UTIL_THROW("Object must contain at least one FieldGenerator.");
83
84 }
85
86 // Read second FieldGenerator (optional)
87 if (fieldGenPtr2_ != nullptr) {
88
89 // Check that one FieldGenerator is a Mask and other is External
90 if (fieldGenPtr2_->type() == FG::External) {
91 generatesExt = true;
92 UTIL_CHECK(fieldGenPtr1_->type() == FG::Mask);
93 } else if (fieldGenPtr2_->type() == FG::Mask) {
94 generatesMask = true;
95 UTIL_CHECK(fieldGenPtr1_->type() == FG::External);
96 } else {
97 UTIL_THROW("fieldGenPtr2_ must have type Mask or External.");
98 }
100 // If this FieldGenerator is dependent, rewind the istream
101 if (fieldGenPtr2_->isDependent()) {
102 in.seekg(pos);
103 Label::clear();
105
106 // Make this FieldGenerator a child ParamComponent of this object
107 bool next = false; // Set indentation to be same as that of parent
108 addParamComposite(*fieldGenPtr2_, next);
109
110 // Read parameters for this FieldGenerator.
111 // Reads body without indentation or curly bracket delimiters
112 fieldGenPtr2_->readParameters(in);
113 }
114
115 setGenerateBools(generatesMask, generatesExt);
116 }
117
118 /*
119 * Check if fields need to be (re)generated. If so, generates them.
120 */
121 template <class Env, class FG>
123 {
124 if (!needsUpdate()) return;
125
126 if (fieldGenPtr1_ != nullptr) fieldGenPtr1_->generate();
127 if (fieldGenPtr2_ != nullptr) fieldGenPtr2_->generate();
128
129 setNeedsUpdateFalse();
130 }
131
132 /*
133 * Return specialized sweep parameter types to add to the Sweep object.
134 */
135 template <class Env, class FG>
137 {
139
140 if (fieldGenPtr1_ != nullptr) a1 = fieldGenPtr1_->getParameterTypes();
141 if (fieldGenPtr2_ != nullptr) a2 = fieldGenPtr2_->getParameterTypes();
142
143 for (int i = 0; i < a2.size(); i++) {
144 a1.append(a2[i]);
145 }
146
147 return a1;
148 }
149
150 /*
151 * Set the value of a specialized sweep parameter.
152 */
153 template <class Env, class FG>
155 DArray<int> ids,
156 double value,
157 bool& success)
158 {
159 success = false;
160 if (fieldGenPtr1_ != nullptr) {
161 fieldGenPtr1_->setParameter(name, ids, value, success);
162 }
163 if ((!success) && (fieldGenPtr2_)) {
164 fieldGenPtr2_->setParameter(name, ids, value, success);
165 }
166 if (success) reset();
167 }
168
169 /*
170 * Get the value of a specialized sweep parameter.
171 */
172 template <class Env, class FG>
174 DArray<int> ids,
175 bool& success) const
176 {
177 double val(0.0);
178 success = false;
179 if (fieldGenPtr1_ != nullptr) {
180 val = fieldGenPtr1_->getParameter(name, ids, success);
181 }
182 if ((!success) && (fieldGenPtr2_)) {
183 val = fieldGenPtr2_->getParameter(name, ids, success);
184 }
185 return val;
186 }
187
188 /*
189 * Get the first field generator by const reference.
190 */
191 template <class Env, class FG>
193 const
194 {
196 return *fieldGenPtr1_;
197 }
198
199 /*
200 * Get the second field generator (if any).
201 */
202 template <class Env, class FG>
204 const
205 {
207 return *fieldGenPtr2_;
208 }
209
210 /*
211 * Does a second FieldGenerator child object exist?
212 */
213 template <class Env, class FG>
216
217}
218#endif
void setParameter(std::string name, DArray< int > ids, double value, bool &success)
Set the value of a specialized sweep parameter.
void addParamComposite(ParamComposite &child, bool next=true)
Add a child ParamComposite object to the format array.
void setClassName(const char *className)
Set class name string.
FG const & fieldGenerator2() const
Get the second FieldGenerator (if any) by const reference.
FG * fieldGenPtr1_
Pointer to the first FieldGenerator object (required).
GArray< ParameterType > getParameterTypes()
Get specialized sweep parameter types to add to a Sweep object.
double getParameter(std::string name, DArray< int > ids, bool &success) const
Get the value of a specialized sweep parameter.
FG * fieldGenPtr2_
Pointer to the second FieldGenerator object (optional).
void generate()
Checks if fields need to be (re)generated.
void readParameters(std::istream &in)
Read parameters from input stream.
FG const & fieldGenerator1() const
Get the first FieldGenerator by const reference.
virtual void createGenerators()=0
Create FieldGenerator objects for mask and/or external field.
bool hasFieldGenerator2() const
Does a second FieldGenerator exist?
Dynamically allocatable contiguous array template.
Definition DArray.h:32
An automatically growable array, analogous to a std::vector.
Definition GArray.h:34
int size() const
Return logical size of this array (i.e., current number of elements).
Definition GArray.h:455
void append(Data const &data)
Append an element to the end of the sequence.
Definition GArray.h:306
static void clear()
Clear the input buffer.
Definition Label.cpp:25
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
PSCF package top-level namespace.
Definition param_pc.dox:1