PSCF v1.5.0
SweepParameter.tpp
1#ifndef RP_SWEEP_PARAMETER_TPP
2#define RP_SWEEP_PARAMETER_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <rp/scft/sweep/SweepParameter.h>
12
13#include <rp/system/System.h>
14#include <rp/solvers/Mixture.h>
15#include <rp/solvers/MixtureModifier.h>
16#include <rp/solvers/Polymer.h>
17#include <rp/solvers/Solvent.h>
18#include <rp/solvers/Block.h>
19#include <rp/field/Domain.h>
20#include <rp/scft/iterator/Iterator.h>
21
22#include <pscf/crystal/UnitCell.h>
23
24#include <pscf/interaction/Interaction.h>
25#include <pscf/sweep/ParameterModifier.h>
26
27#include <util/containers/FSArray.h>
28#include <util/misc/ioUtil.h>
29#include <util/global.h>
30
31#include <algorithm>
32#include <iomanip>
33
34namespace Pscf {
35namespace Rp {
36
37 using namespace Util;
38
39 /*
40 * Default constructor.
41 */
42 template <int D, class T>
44 : type_(SweepParameter<D,T>::Null),
45 nId_(0),
46 id_(),
47 initial_(0.0),
48 change_(0.0),
49 systemPtr_(nullptr),
50 parameterTypesPtr_(nullptr),
51 parameterTypeId_(-1)
52 {}
53
54 /*
55 * Constructor, creates association with system.
56 */
57 template <int D, class T>
59 : type_(SweepParameter<D,T>::Null),
60 nId_(0),
61 id_(),
62 initial_(0.0),
63 change_(0.0),
64 systemPtr_(&system),
65 parameterTypesPtr_(nullptr),
66 parameterTypeId_(-1)
67 {}
68
69 /*
70 * Read type, set nId and allocate id_ array.
71 */
72 template <int D, class T>
73 void SweepParameter<D,T>::readParamType(std::istream& in)
74 {
75 std::string buffer;
76 in >> buffer;
77 std::transform(buffer.begin(), buffer.end(),
78 buffer.begin(), ::tolower);
79
80 if (buffer == "block" || buffer == "block_length") {
81 type_ = Block;
82 nId_ = 2; // polymer and block identifiers
83 } else if (buffer == "chi") {
84 type_ = Chi;
85 nId_ = 2; // two monomer type identifiers
86 } else if (buffer == "kuhn") {
87 type_ = Kuhn;
88 nId_ = 1; // monomer type identifier
89 } else if (buffer == "phi_polymer") {
90 type_ = Phi_Polymer;
91 nId_ = 1; // species identifier
92 } else if (buffer == "phi_solvent") {
93 type_ = Phi_Solvent;
94 nId_ = 1; // species identifier
95 } else if (buffer == "mu_polymer") {
96 type_ = Mu_Polymer;
97 nId_ = 1; // species identifier
98 } else if (buffer == "mu_solvent") {
99 type_ = Mu_Solvent;
100 nId_ = 1; // species identifier
101 } else if (buffer == "solvent" || buffer == "solvent_size") {
102 type_ = Solvent;
103 nId_ = 1; // species identifier
104 } else if (buffer == "cell_param") {
105 type_ = Cell_Param;
106 nId_ = 1; // lattice parameter identifier
107 } else {
108 // Search in parameterTypes array for this sweep parameter
109 bool found = false;
110 if (parameterTypesPtr_) {
111 for (int i = 0; i < parameterTypesPtr_->size(); i++) {
112 ParameterType& pType = (*parameterTypesPtr_)[i];
113 if (buffer == pType.name) {
114 type_ = Special;
115 nId_ = pType.nId;
116 parameterTypeId_ = i;
117 found = true;
118 break;
119 }
120 }
121 }
122 if (!found) {
123 std::string msg;
124 msg = "Invalid SweepParameter::ParamType value: " + buffer;
125 UTIL_THROW(msg.c_str());
126 }
127 }
128
129 if (id_.isAllocated()) id_.deallocate();
130 id_.allocate(nId_);
131
132 }
133
134 /*
135 * Write type enum value.
136 */
137 template <int D, class T>
138 void SweepParameter<D,T>::writeParamType(std::ostream& out) const
139 { out << type(); }
140
141 /*
142 * Get the ParameterType object for a specialized sweep parameter.
143 */
144 template <int D, class T>
146 {
147 UTIL_CHECK(parameterTypesPtr_);
149 return (*parameterTypesPtr_)[parameterTypeId_];
150 }
151
152 /*
153 * Check the indices, get and store initial (current) value from the
154 * parent system.
155 */
156 template <int D, class T>
158 {
159 checkIds();
160 initial_ = get_();
161 }
162
163 /*
164 * Check that the indices of this parameter are in range.
165 */
166 template <int D, class T>
168 {
169 UTIL_CHECK(systemPtr_);
170 Mixture<D,T> const & mixture = systemPtr_->mixture();
171 if (type_ == Block) {
172 checkId(0, mixture.nPolymer(), "polymer");
173 checkId(1, mixture.polymer(id(0)).nBlock(), "block");
174 } else if (type_ == Chi) {
175 checkId(0, mixture.nMonomer(), "monomer");
176 checkId(1, mixture.nMonomer(), "monomer");
177 } else if (type_ == Kuhn) {
178 checkId(0, mixture.nMonomer(), "monomer");
179 } else if (type_ == Phi_Polymer || type_ == Mu_Polymer) {
180 checkId(0, mixture.nPolymer(), "polymer");
181 } else if (type_ == Phi_Solvent || type_ == Mu_Solvent
182 || type_ == Solvent) {
183 checkId(0, mixture.nSolvent(), "solvent");
184 } else if (type_ == Cell_Param) {
185 checkId(0, systemPtr_->domain().unitCell().nParameter(),
186 "lattice parameter");
187 }
188 }
189
190 /*
191 * Throw an Exception if index id(i) is not in the range [0, n).
192 */
193 template <int D, class T>
194 void SweepParameter<D,T>::checkId(int i, int n,
195 std::string const & name) const
196 {
197 if (id(i) < 0 || id(i) >= n) {
198 std::string msg = "Sweep parameter " + type() + ": " + name
199 + " index " + toString(id(i))
200 + " out of range [0, " + toString(n) + ")";
201 UTIL_THROW(msg.c_str());
202 }
203 }
204
205 /*
206 * Set a new parameter value in the parent system.
207 */
208 template <int D, class T>
209 void SweepParameter<D,T>::update(double newVal)
210 { set_(newVal); }
211
212 /*
213 * Get string representation of type enum value.
214 */
215 template <int D, class T>
216 std::string SweepParameter<D,T>::type() const
217 {
218 if (type_ == Block) {
219 return "block";
220 } else if (type_ == Chi) {
221 return "chi";
222 } else if (type_ == Kuhn) {
223 return "kuhn";
224 } else if (type_ == Phi_Polymer) {
225 return "phi_polymer";
226 } else if (type_ == Phi_Solvent) {
227 return "phi_solvent";
228 } else if (type_ == Mu_Polymer) {
229 return "mu_polymer";
230 } else if (type_ == Mu_Solvent) {
231 return "mu_solvent";
232 } else if (type_ == Solvent) {
233 return "solvent_size";
234 } else if (type_ == Cell_Param) {
235 return "cell_param";
236 } else if (type_ == Special) {
237 return parameterType().name;
238 } else {
239 UTIL_THROW("This should never happen.");
240 }
241 }
242
243 template <int D, class T>
244 double SweepParameter<D,T>::get_()
245 {
246 UTIL_CHECK(systemPtr_);
247 if (type_ == Chi) {
248 return systemPtr_->interaction().chi(id(0), id(1));
249 } else if (type_ == Kuhn) {
250 return systemPtr_->mixture().monomer(id(0)).kuhn();
251 } else if (type_ == Phi_Polymer) {
252 return systemPtr_->mixture().polymer(id(0)).phi();
253 } else if (type_ == Mu_Polymer) {
254 return systemPtr_->mixture().polymer(id(0)).mu();
255 } else if (type_ == Block) {
256 return systemPtr_->mixture().polymer(id(0)).block(id(1)).length();
257 } else if (type_ == Phi_Solvent) {
258 return systemPtr_->mixture().solvent(id(0)).phi();
259 } else if (type_ == Mu_Solvent) {
260 return systemPtr_->mixture().solvent(id(0)).mu();
261 } else if (type_ == Solvent) {
262 return systemPtr_->mixture().solvent(id(0)).size();
263 } else if (type_ == Cell_Param) {
264 return systemPtr_->domain().unitCell().parameter(id(0));
265 } else if (type_ == Special) {
266 ParameterModifier* modifier = parameterType().modifierPtr;
267 std::string name = parameterType().name;
268 return modifier->getParameter(name,id_);
269 } else {
270 UTIL_THROW("This should never happen.");
271 }
272 }
273
274 template <int D, class T>
275 void SweepParameter<D,T>::set_(double newVal)
276 {
277 UTIL_CHECK(systemPtr_);
278 if (type_ == Chi) {
279 systemPtr_->interaction().setChi(id(0), id(1), newVal);
280 } else if (type_ == Kuhn) {
281 systemPtr_->mixtureModifier().setKuhn(id(0), newVal);
282 } else if (type_ == Phi_Polymer) {
283 systemPtr_->mixtureModifier().setPhiPolymer(id(0), newVal);
284 } else if (type_ == Mu_Polymer) {
285 systemPtr_->mixtureModifier().setMuPolymer(id(0), newVal);
286 } else if (type_ == Block) {
287 systemPtr_->mixtureModifier().setBlockLength(id(0), id(1), newVal);
288 } else if (type_ == Phi_Solvent) {
289 systemPtr_->mixtureModifier().setPhiSolvent(id(0), newVal);
290 } else if (type_ == Mu_Solvent) {
291 systemPtr_->mixtureModifier().setMuSolvent(id(0), newVal);
292 } else if (type_ == Solvent) {
293 systemPtr_->mixtureModifier().setSolventSize(id(0), newVal);
294 } else if (type_ == Cell_Param) {
295 // The iterator optimizes a flexible lattice parameter, and the
296 // sweep extrapolates it from previous states, which would undo
297 // the change. (Check here, not in setup: a thin film generator
298 // may change the flexibleParams array of the iterator when it
299 // generates the mask, before the first state is solved.)
300 if (systemPtr_->hasIterator()) {
301 Iterator<D,T> const & iterator = systemPtr_->iterator();
302 FSArray<bool,6> const flexParams = iterator.flexibleParams();
303 if (iterator.isFlexible() && id(0) < flexParams.size()
304 && flexParams[id(0)]) {
305 std::string msg = "Sweep parameter cell_param "
306 + toString(id(0)) + ": lattice parameter"
307 + " is flexible, so the iterator would"
308 + " optimize it and undo the sweep; make"
309 + " it rigid (isFlexible 0, or 0 in the"
310 + " flexibleParams array)";
311 UTIL_THROW(msg.c_str());
312 }
313 }
314 FSArray<double,6> params
315 = systemPtr_->domain().unitCell().parameters();
316 params[id(0)] = newVal;
317 systemPtr_->setUnitCell(params);
318 } else if (type_ == Special) {
319 ParameterModifier* modifier = parameterType().modifierPtr;
320 std::string name = parameterType().name;
321 return modifier->setParameter(name,id_,newVal);
322 } else {
323 UTIL_THROW("This should never happen.");
324 }
325 }
326
327 #if 0
328 template <int D, class T>
329 template <class Archive>
330 void SweepParameter<D,T>::serialize(Archive ar, const unsigned int version)
331 {
332 serializeEnum(ar, type_, version);
333 ar & nId_;
334 for (int i = 0; i < nId_; ++i) {
335 ar & id_[i];
336 }
337 ar & initial_;
338 ar & change_;
339 }
340
341 // Definitions of IO operator templates
342
343 /*
344 * Inserter for reading a SweepParameter from an istream.
345 */
346 template <int D, class T>
347 std::istream& operator >> (std::istream& in,
348 SweepParameter<D,T>& param)
349 {
350 // Read the parameter type identifier string
351 param.readParamType(in);
352
353 // Read the identifiers associated with this parameter type.
354 for (int i = 0; i < param.nId_; ++i) {
355 in >> param.id_[i];
356 }
357 // Read in the range in the parameter to sweep over
358 in >> param.change_;
359
360 return in;
361 }
362
363 /*
364 * Extractor for writing a SweepParameter to ostream.
365 */
366 template <int D, class T>
367 std::ostream& operator << (std::ostream& out,
368 SweepParameter<D,T> const & param)
369 {
370 param.writeParamType(out);
371 out << " ";
372 for (int i = 0; i < param.nId_; ++i) {
373 out << param.id(i);
374 out << " ";
375 }
376 out << param.change_;
377
378 return out;
379 }
380 #endif
381
382}
383}
384#endif
int nMonomer() const
Get number of monomer types.
int nPolymer() const
Get number of polymer species.
int nSolvent() const
Get number of solvent (point particle) species.
int nBlock() const
Number of blocks.
Base class template for iterative solvers for SCF equations.
Solver and descriptor for a mixture of polymers and solvents.
PolymerT & polymer(int id)
Get a polymer solver object by non-const reference.
Solver and descriptor for a solvent species.
Class template for storing data about an individual sweep parameter.
void getInitial()
Get initial value of this parameter.
SweepParameter()
Default constructor.
std::string type() const
Return a string representation of the parameter type.
void writeParamType(std::ostream &out) const
Write the parameter type to an output stream.
ParameterType & parameterType() const
Get the ParameterType object for a specialized sweep parameter.
void serialize(Archive &ar, const unsigned int version)
Serialize to or from an archive.
void update(double newVal)
Update the corresponding parameter value in the system.
bool isSpecialized() const
Is this SweepParameter a specialized parameter type?
void checkIds() const
Check that the indices of this parameter are in range.
Base class template for classes that represent a complete system.
A fixed capacity (static) contiguous array with a variable logical size.
Definition FSArray.h:38
int size() const
Return logical size of this array (i.e., number of elements).
Definition FSArray.h:210
File containing preprocessor macros for error handling.
#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
std::string toString(int n)
Return string representation of an integer.
Definition ioUtil.cpp:52
void serializeEnum(Archive &ar, T &data, const unsigned int version=0)
Serialize an enumeration value.
Definition serialize.h:59
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.
std::istream & operator>>(std::istream &in, Pair< Data > &pair)
Input a Pair from an istream.
Definition Pair.h:44
std::ostream & operator<<(std::ostream &out, const Pair< Data > &pair)
Output a Pair to an ostream, without line breaks.
Definition Pair.h:57
Declaration of a specialized sweep parameter type.