PSCF v1.5.0
RampParameter.tpp
1#ifndef RP_RAMP_PARAMETER_TPP
2#define RP_RAMP_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 "RampParameter.h"
12
13#include <rp/fts/simulator/Simulator.h>
14#include <rp/system/System.h>
15#include <rp/fts/perturbation/Perturbation.h>
16#include <rp/solvers/Mixture.h>
17#include <rp/solvers/MixtureModifier.h>
18#include <rp/solvers/Polymer.h>
19#include <rp/solvers/Solvent.h>
20#include <rp/solvers/Block.h>
21#include <rp/field/Domain.h>
22
23#include <pscf/crystal/UnitCell.h>
24#include <pscf/chem/Monomer.h>
25#include <pscf/chem/PolymerModel.h>
26#include <pscf/interaction/Interaction.h>
27#include <util/containers/FSArray.h>
28#include <util/misc/ioUtil.h>
29
30#include <algorithm>
31
32namespace Pscf {
33namespace Rp {
34
35 using namespace Util;
36
37 /*
38 * Default constructor.
39 */
40 template <int D, class T>
42 : type_(RampParameter<D,T>::Null),
43 nId_(0),
44 id_(),
45 initial_(0.0),
46 change_(0.0),
47 simulatorPtr_(0),
48 systemPtr_(0)
49 {}
50
51 /*
52 * Constructor, creates association with simulator and system.
53 */
54 template <int D, class T>
56 : type_(RampParameter<D,T>::Null),
57 nId_(0),
58 id_(),
59 initial_(0.0),
60 change_(0.0),
61 simulatorPtr_(&simulator),
62 systemPtr_(&(simulator.system()))
63 {}
64
65 /*
66 * Set the simulator and system associated with this object.
67 */
68 template <int D, class T>
70 {
71 simulatorPtr_ = &simulator;
72 systemPtr_ = &(simulator.system());
73 }
74
75 /*
76 * Read type, set nId and allocate id_ array.
77 */
78 template <int D, class T>
79 void RampParameter<D,T>::readParamType(std::istream& in)
80 {
81 std::string buffer;
82 in >> buffer;
83
84 // Transform buffer string to all lower case
85 std::transform(buffer.begin(), buffer.end(),
86 buffer.begin(), ::tolower);
87
88 // Attempt to match buffer to any allowed label
89 if (buffer == "block" || buffer == "block_length") {
91 type_ = Block;
92 nId_ = 2; // polymer and block identifiers
93 } else if (buffer == "chi") {
94 type_ = Chi;
95 nId_ = 2; // two monomer type identifiers
96 } else if (buffer == "kuhn") {
97 type_ = Kuhn;
98 nId_ = 1; // monomer type identifier
99 } else if (buffer == "phi_polymer") {
100 type_ = Phi_Polymer;
101 nId_ = 1; // species identifier.
102 } else if (buffer == "phi_solvent") {
103 type_ = Phi_Solvent;
104 nId_ = 1; // species identifier.
105 } else if (buffer == "mu_polymer") {
106 type_ = Mu_Polymer;
107 nId_ = 1; // species identifier.
108 } else if (buffer == "mu_solvent") {
109 type_ = Mu_Solvent;
110 nId_ = 1; // species identifier.
111 } else if (buffer == "solvent" || buffer == "solvent_size") {
112 type_ = Solvent;
113 nId_ = 1; // species identifier.
114 } else if (buffer == "cell_param") {
115 type_ = Cell_Param;
116 nId_ = 1; // lattice parameter identifier.
117 } else if (buffer == "lambda_pert") {
118 type_ = Lambda_Pert;
119 nId_ = 0; // No associated index
120 } else if (buffer == "v_monomer") {
121 type_ = V_monomer;
122 nId_ = 0; // No associated index
123 } else {
124 UTIL_THROW("Invalid RampParameter::ParamType value");
125 }
126
127 // Allocate array id_ with capacity nId_
128 if (id_.isAllocated()) id_.deallocate();
129 if (nId_ > 0) {
130 id_.allocate(nId_);
131 }
132
133 }
134
135 /*
136 * Write type enum value.
137 */
138 template <int D, class T>
139 void RampParameter<D,T>::writeParamType(std::ostream& out) const
140 { out << type(); }
141
142 /*
143 * Check the indices, get initial (current) values of swept parameters
144 * from parent system.
145 */
146 template <int D, class T>
148 {
149 checkIds();
150 initial_ = get_();
151 }
152
153 /*
154 * Check that the indices of this parameter are in range.
155 *
156 * Throw an Exception if any index is out of range.
157 */
158 template <int D, class T>
160 {
161 UTIL_CHECK(systemPtr_);
162 Mixture<D,T> const & mixture = systemPtr_->mixture();
163 if (type_ == Block) {
164 checkId(0, mixture.nPolymer(), "polymer");
165 checkId(1, mixture.polymer(id(0)).nBlock(), "block");
166 } else if (type_ == Chi) {
167 checkId(0, mixture.nMonomer(), "monomer");
168 checkId(1, mixture.nMonomer(), "monomer");
169 } else if (type_ == Kuhn) {
170 checkId(0, mixture.nMonomer(), "monomer");
171 } else if (type_ == Phi_Polymer || type_ == Mu_Polymer) {
172 checkId(0, mixture.nPolymer(), "polymer");
173 } else if (type_ == Phi_Solvent || type_ == Mu_Solvent
174 || type_ == Solvent) {
175 checkId(0, mixture.nSolvent(), "solvent");
176 } else if (type_ == Cell_Param) {
177 checkId(0, systemPtr_->domain().unitCell().nParameter(),
178 "lattice parameter");
179 }
180 }
181
182 /*
183 * Throw an Exception if index id(i) is not in the range [0, n).
184 */
185 template <int D, class T>
186 void RampParameter<D,T>::checkId(int i, int n,
187 std::string const & name) const
188 {
189 if (id(i) < 0 || id(i) >= n) {
190 std::string msg = "Ramp parameter " + type() + ": " + name
191 + " index " + toString(id(i))
192 + " out of range [0, " + toString(n) + ")";
193 UTIL_THROW(msg.c_str());
194 }
195 }
196
197 /*
198 * Set new values of swept parameters in the parent system.
199 */
200 template <int D, class T>
201 void RampParameter<D,T>::update(double newVal)
202 { set_(newVal); }
203
204 /*
205 * Get string representation of type enum value.
206 */
207 template <int D, class T>
208 std::string RampParameter<D,T>::type() const
209 {
210 if (type_ == Block) {
212 return "block";
213 } else if (type_ == Chi) {
214 return "chi";
215 } else if (type_ == Kuhn) {
216 return "kuhn";
217 } else if (type_ == Phi_Polymer) {
218 return "phi_polymer";
219 } else if (type_ == Phi_Solvent) {
220 return "phi_solvent";
221 } else if (type_ == Mu_Polymer) {
222 return "mu_polymer";
223 } else if (type_ == Mu_Solvent) {
224 return "mu_solvent";
225 } else if (type_ == Solvent) {
226 return "solvent_size";
227 } else if (type_ == Cell_Param) {
228 return "cell_param";
229 } else if (type_ == Lambda_Pert) {
230 return "lambda_pert";
231 } else if (type_ == V_monomer) {
232 return "v_monomer";
233 } else if (type_ == Null) {
234 std::string msg
235 = "Attempt to convert a Null RampParameter type ";
236 msg += "enum value to a string";
237 UTIL_THROW(msg.c_str());
238 } else {
239 UTIL_THROW("This should have been impossible.");
240 }
241 }
242
243 /*
244 * Return current value of this parameter.
245 */
246 template <int D, class T>
247 double RampParameter<D,T>::get_()
248 {
249 if (type_ == Block) {
251 return systemPtr_->mixture().polymer(id(0)).block(id(1)).length();
252 } else if (type_ == Chi) {
253 return systemPtr_->interaction().chi(id(0), id(1));
254 } else if (type_ == Kuhn) {
255 return systemPtr_->mixture().monomer(id(0)).kuhn();
256 } else if (type_ == Phi_Polymer) {
257 return systemPtr_->mixture().polymer(id(0)).phi();
258 } else if (type_ == Phi_Solvent) {
259 return systemPtr_->mixture().solvent(id(0)).phi();
260 } else if (type_ == Mu_Polymer) {
261 return systemPtr_->mixture().polymer(id(0)).mu();
262 } else if (type_ == Mu_Solvent) {
263 return systemPtr_->mixture().solvent(id(0)).mu();
264 } else if (type_ == Solvent) {
265 return systemPtr_->mixture().solvent(id(0)).size();
266 } else if (type_ == V_monomer) {
267 return systemPtr_->mixture().vMonomer();
268 } else if (type_ == Cell_Param) {
269 return systemPtr_->domain().unitCell().parameter(id(0));
270 } else if (type_ == Lambda_Pert) {
271 UTIL_CHECK(simulatorPtr_->hasPerturbation());
272 return simulatorPtr_->perturbation().lambda();
273 } else {
274 UTIL_THROW("This should never happen.");
275 }
276 }
277
278 /*
279 * Modify system to set the value of this parameter.
280 */
281 template <int D, class T>
282 void RampParameter<D,T>::set_(double newVal)
283 {
284 if (type_ == Chi) {
285 systemPtr_->interaction().setChi(id(0), id(1), newVal);
286 } else if (type_ == Kuhn) {
287 systemPtr_->mixtureModifier().setKuhn(id(0), newVal);
288 } else if (type_ == Phi_Polymer) {
289 systemPtr_->mixtureModifier().setPhiPolymer(id(0), newVal);
290 } else if (type_ == Mu_Polymer) {
291 systemPtr_->mixtureModifier().setMuPolymer(id(0), newVal);
292 } else if (type_ == Block) {
294 systemPtr_->mixtureModifier().setBlockLength(id(0), id(1), newVal);
295 } else if (type_ == Phi_Solvent) {
296 systemPtr_->mixtureModifier().setPhiSolvent(id(0), newVal);
297 } else if (type_ == Mu_Solvent) {
298 systemPtr_->mixtureModifier().setMuSolvent(id(0), newVal);
299 } else if (type_ == Solvent) {
300 systemPtr_->mixtureModifier().setSolventSize(id(0), newVal);
301 } else if (type_ == V_monomer) {
302 systemPtr_->mixtureModifier().setVMonomer(newVal);
303 } else if (type_ == Cell_Param) {
304 FSArray<double,6> params;
305 params = systemPtr_->domain().unitCell().parameters();
306 UTIL_CHECK(id(0) < params.size());
307 params[id(0)] = newVal;
308 systemPtr_->setUnitCell(params);
309 } else if (type_ == Lambda_Pert) {
310 UTIL_CHECK(simulatorPtr_->hasPerturbation());
311 return simulatorPtr_->perturbation().setLambda(newVal);
312 } else {
313 UTIL_THROW("This should never happen.");
314 }
315 }
316
317}
318}
319#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.
Solver and descriptor for a mixture of polymers and solvents.
PolymerT & polymer(int id)
Get a polymer solver object by non-const reference.
RampParameter()
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.
void setSimulator(Simulator< D, T > &simulator)
Set the simulator and system associated with this object.
void getInitial()
Get and store initial value this parameters.
void update(double newVal)
Update the corresponding parameter value in the System.
void checkIds() const
Check that any indices of this parameter are in range.
Base class for field theoretic PS-FTS simulator.
Definition Simulator.h:93
System< D, T > & system()
Get the parent system by reference.
Definition Simulator.h:944
Solver and descriptor for a solvent species.
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
#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
bool isThread()
Is the thread model in use ?
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.