PSCF v1.5.0
r1/sweep/SweepParameter.cpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include <r1/sweep/SweepParameter.h>
9#include <r1/system/System.h>
10#include <r1/solvers/Block.h>
11#include <r1/solvers/Mixture.h>
12#include <r1/solvers/Polymer.h>
13#include <r1/solvers/Solvent.h>
14#include <pscf/interaction/Interaction.h>
15#include <pscf/sweep/ParameterModifier.h>
16#include <util/containers/FSArray.h>
17#include <util/misc/ioUtil.h>
18#include <util/global.h>
19
20#include <algorithm>
21#include <iomanip>
22
23namespace Pscf {
24namespace R1 {
25
26 using namespace Util;
27
28 /*
29 * Default constructor.
30 */
32 : type_(SweepParameter::Null),
33 nId_(0),
34 id_(),
35 initial_(0.0),
36 change_(0.0),
37 systemPtr_(0),
38 parameterTypesPtr_(0),
39 parameterTypeId_(-1)
40 {}
41
42 /*
43 * Constructor, creates association with system.
44 */
46 : type_(SweepParameter::Null),
47 nId_(0),
48 id_(),
49 initial_(0.0),
50 change_(0.0),
51 systemPtr_(&system),
52 parameterTypesPtr_(0),
53 parameterTypeId_(-1)
54 {}
55
56 /*
57 * Read type, set nId and allocate id_ array.
58 */
59 void SweepParameter::readParamType(std::istream& in)
60 {
61 std::string buffer;
62 in >> buffer;
63 std::transform(buffer.begin(), buffer.end(),
64 buffer.begin(), ::tolower);
65
66 if (buffer == "block" || buffer == "block_length") {
67 type_ = Block;
68 nId_ = 2; // polymer and block identifiers
69 } else if (buffer == "chi") {
70 type_ = Chi;
71 nId_ = 2; // two monomer type identifiers
72 } else if (buffer == "kuhn") {
73 type_ = Kuhn;
74 nId_ = 1; // monomer type identifier
75 } else if (buffer == "phi_polymer") {
76 type_ = Phi_Polymer;
77 nId_ = 1; //species identifier.
78 } else if (buffer == "phi_solvent") {
79 type_ = Phi_Solvent;
80 nId_ = 1; //species identifier.
81 } else if (buffer == "mu_polymer") {
82 type_ = Mu_Polymer;
83 nId_ = 1; //species identifier.
84 } else if (buffer == "mu_solvent") {
85 type_ = Mu_Solvent;
86 nId_ = 1; //species identifier.
87 } else if (buffer == "solvent" || buffer == "solvent_size") {
88 type_ = Solvent;
89 nId_ = 1; //species identifier.
90 } else {
91 // Search in parameterTypes array for this sweep parameter
92 bool found = false;
93 for (int i = 0; i < parameterTypesPtr_->size(); i++) {
94 ParameterType& pType = (*parameterTypesPtr_)[i];
95 if (buffer == pType.name) {
96 type_ = Special;
97 nId_ = pType.nId;
98 parameterTypeId_ = i;
99 found = true;
100 break;
101 }
102 }
103 if (!found) {
104 std::string msg;
105 msg = "Invalid SweepParameter::ParamType value: " + buffer;
106 UTIL_THROW(msg.c_str());
107 }
108 }
109
110 if (id_.isAllocated()) id_.deallocate();
111 id_.allocate(nId_);
112 }
113
114 /*
115 * Write type enum value
116 */
117 void SweepParameter::writeParamType(std::ostream& out) const
118 {
119 out << type();
120 }
121
122 /*
123 * Get the ParameterType object for a specialized sweep parameter
124 */
126 {
128 return (*parameterTypesPtr_)[parameterTypeId_];
129 }
130
131 /*
132 * Check the indices, get the current value from the parent system.
133 */
135 {
136 checkIds();
137 initial_ = get_();
138 }
139
140 /*
141 * Check that the indices of this parameter are in range.
142 */
144 {
145 UTIL_CHECK(systemPtr_);
146 Mixture const & mixture = systemPtr_->mixture();
147 if (type_ == Block) {
148 checkId(0, mixture.nPolymer(), "polymer");
149 checkId(1, mixture.polymer(id(0)).nBlock(), "block");
150 } else if (type_ == Chi) {
151 checkId(0, mixture.nMonomer(), "monomer");
152 checkId(1, mixture.nMonomer(), "monomer");
153 } else if (type_ == Kuhn) {
154 checkId(0, mixture.nMonomer(), "monomer");
155 } else if (type_ == Phi_Polymer || type_ == Mu_Polymer) {
156 checkId(0, mixture.nPolymer(), "polymer");
157 } else if (type_ == Phi_Solvent || type_ == Mu_Solvent
158 || type_ == Solvent) {
159 checkId(0, mixture.nSolvent(), "solvent");
160 }
161 }
162
163 /*
164 * Throw an Exception if index id(i) is not in the range [0, n).
165 */
166 void SweepParameter::checkId(int i, int n,
167 std::string const & name) const
168 {
169 if (id(i) < 0 || id(i) >= n) {
170 std::string msg = "Sweep parameter " + type() + ": " + name
171 + " index " + toString(id(i))
172 + " out of range [0, " + toString(n) + ")";
173 UTIL_THROW(msg.c_str());
174 }
175 }
176
177 /*
178 * Set a new value in the parent system.
179 */
180 void SweepParameter::update(double newVal)
181 {
182 set_(newVal);
183 }
184
185 /*
186 * Get string representation of type enum value.
187 */
188 std::string SweepParameter::type() const
189 {
190 if (type_ == Block) {
191 return "block";
192 } else if (type_ == Chi) {
193 return "chi";
194 } else if (type_ == Kuhn) {
195 return "kuhn";
196 } else if (type_ == Phi_Polymer) {
197 return "phi_polymer";
198 } else if (type_ == Phi_Solvent) {
199 return "phi_solvent";
200 } else if (type_ == Mu_Polymer) {
201 return "mu_polymer";
202 } else if (type_ == Mu_Solvent) {
203 return "mu_solvent";
204 } else if (type_ == Solvent) {
205 return "solvent_size";
206 } else if (type_ == Special) {
207 return parameterType().name;
208 } else {
209 UTIL_THROW("Invalid type_ in accessor SweepParameter::type().");
210 }
211 }
212
213 double SweepParameter::get_()
214 {
215 if (type_ == Block) {
216 return systemPtr_->mixture().polymer(id(0)).block(id(1)).length();
217 } else if (type_ == Chi) {
218 return systemPtr_->interaction().chi(id(0), id(1));
219 } else if (type_ == Kuhn) {
220 return systemPtr_->mixture().monomer(id(0)).kuhn();
221 } else if (type_ == Phi_Polymer) {
222 return systemPtr_->mixture().polymer(id(0)).phi();
223 } else if (type_ == Phi_Solvent) {
224 return systemPtr_->mixture().solvent(id(0)).phi();
225 } else if (type_ == Mu_Polymer) {
226 return systemPtr_->mixture().polymer(id(0)).mu();
227 } else if (type_ == Mu_Solvent) {
228 return systemPtr_->mixture().solvent(id(0)).mu();
229 } else if (type_ == Solvent) {
230 return systemPtr_->mixture().solvent(id(0)).size();
231 } else if (type_ == Special) {
232 ParameterModifier* modifier = parameterType().modifierPtr;
233 std::string name = parameterType().name;
234 return modifier->getParameter(name,id_);
235 } else {
236 UTIL_THROW("Invalid type_ in SweepParameter::get_.");
237 }
238 }
239
240 void SweepParameter::set_(double newVal)
241 {
242 if (type_ == Block) {
243 systemPtr_->mixture().polymer(id(0)).block(id(1)).setLength(newVal);
244 } else if (type_ == Chi) {
245 systemPtr_->interaction().setChi(id(0), id(1), newVal);
246 } else if (type_ == Kuhn) {
247 systemPtr_->mixture().setKuhn(id(0), newVal);
248 } else if (type_ == Phi_Polymer) {
249 systemPtr_->mixture().polymer(id(0)).setPhi(newVal);
250 } else if (type_ == Phi_Solvent) {
251 systemPtr_->mixture().solvent(id(0)).setPhi(newVal);
252 } else if (type_ == Mu_Polymer) {
253 systemPtr_->mixture().polymer(id(0)).setMu(newVal);
254 } else if (type_ == Mu_Solvent) {
255 systemPtr_->mixture().solvent(id(0)).setMu(newVal);
256 } else if (type_ == Solvent) {
257 systemPtr_->mixture().solvent(id(0)).setSize(newVal);
258 } else if (type_ == Special) {
259 ParameterModifier* modifier = parameterType().modifierPtr;
260 std::string name = parameterType().name;
261 return modifier->setParameter(name,id_,newVal);
262 } else {
263 UTIL_THROW("Invalid type_ in SweepParameter::set_.");
264 }
265 }
266
267 // Definitions of operators, with no explicit specializations.
268
275 std::istream& operator >> (std::istream& in,
276 SweepParameter& param)
277 {
278 // Read the parameter type.
279 param.readParamType(in);
280 // Read the identifiers associated with this parameter type.
281 for (int i = 0; i < param.nId_; ++i) {
282 in >> param.id_[i];
283 }
284 // Read in the range in the parameter to sweep over
285 in >> param.change_;
286
287 return in;
288 }
289
296 std::ostream& operator << (std::ostream& out,
297 SweepParameter const & param)
298 {
299 param.writeParamType(out);
300 out << " ";
301 for (int i = 0; i < param.nId_; ++i) {
302 out << param.id(i);
303 out << " ";
304 }
305 out << param.change_;
306
307 return out;
308 }
309
310}
311}
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.
Monomer const & monomer(int id) const
Get a Monomer type descriptor by const reference.
DMatrix< double > const & chi() const
Return the chi matrix by const reference.
PolymerT & polymer(int id)
Get a polymer solver object by non-const reference.
double kuhn() const
Statistical segment length (random walk step size).
Definition Monomer.h:131
Block within a block polymer.
Solver and descriptor for a mixture of polymers and solvents.
std::string type() const
Return a string representation of the parameter type.
int id(int i) const
Get a id for a sub-object or element to which this is applied.
SweepParameter()
Default constructor.
void checkIds() const
Check that the indices of this parameter are in range.
bool isSpecialized() const
Is this SweepParameter a specialized parameter type?
void update(double newVal)
Update the corresponding parameter value in the system.
void writeParamType(std::ostream &out) const
Write the parameter type to an output stream.
void getInitial()
Store the pre-sweep value of the corresponding parameter.
ParameterType & parameterType() const
Get the ParameterType object for a specialized sweep parameter.
Main class in SCFT simulation of one system.
Mixture & mixture()
Get Mixture by reference.
Interaction & interaction()
Get interaction (i.e., excess free energy) by reference.
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
SCFT with real 1D fields.
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.