PSCF v1.3
rpc/fts/ramp/RampParameter.tpp
1#ifndef RPC_RAMP_PARAMETER_TPP
2#define RPC_RAMP_PARAMETER_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 <rpc/system/System.h>
12#include <rpc/fts/simulator/Simulator.h>
13#include <rpc/fts/perturbation/Perturbation.h>
14#include <rpc/solvers/Mixture.h>
15#include <rpc/solvers/MixtureModifier.h>
16#include <rpc/solvers/Block.h>
17#include <rpc/solvers/Mixture.h>
18#include <rpc/solvers/Polymer.h>
19#include <prdc/crystal/UnitCell.h>
20#include <pscf/inter/Interaction.h>
21#include <util/containers/FSArray.h>
22#include <util/global.h>
23#include <algorithm>
24#include <iomanip>
25
26namespace Pscf {
27namespace Rpc {
28
29 using namespace Util;
30 using namespace Pscf::Prdc;
31
32 /*
33 * Default constructor.
34 */
35 template <int D>
37 : type_(RampParameter<D>::Null),
38 nId_(0),
39 id_(),
40 initial_(0.0),
41 change_(0.0),
42 simulatorPtr_(0),
43 systemPtr_(0)
44 {}
45
46 /*
47 * Constructor, creates association with simulator and system.
48 */
49 template <int D>
51 : type_(RampParameter<D>::Null),
52 nId_(0),
53 id_(),
54 initial_(0.0),
55 change_(0.0),
56 simulatorPtr_(&simulator),
57 systemPtr_(&(simulator.system()))
58 {}
59
60
61 /*
62 * Set the simulator and system associated with this object.
63 */
64 template <int D>
66 {
67 simulatorPtr_ = &simulator;
68 systemPtr_ = &(simulator.system());
69 }
70
71 /*
72 * Read type, set nId and allocate id_ array.
73 */
74 template <int D>
75 void RampParameter<D>::readParamType(std::istream& in)
76 {
77 std::string buffer;
78 in >> buffer;
79 std::transform(buffer.begin(), buffer.end(),
80 buffer.begin(), ::tolower);
81
82 if (buffer == "block" || buffer == "block_length") {
84 type_ = Block;
85 nId_ = 2; // polymer and block identifiers
86 } else if (buffer == "chi") {
87 type_ = Chi;
88 nId_ = 2; // two monomer type identifiers
89 } else if (buffer == "kuhn") {
90 type_ = Kuhn;
91 nId_ = 1; // monomer type identifier
92 } else if (buffer == "phi_polymer") {
93 type_ = Phi_Polymer;
94 nId_ = 1; // species identifier.
95 } else if (buffer == "phi_solvent") {
96 type_ = Phi_Solvent;
97 nId_ = 1; // species identifier.
98 } else if (buffer == "mu_polymer") {
99 type_ = Mu_Polymer;
100 nId_ = 1; // species identifier.
101 } else if (buffer == "mu_solvent") {
102 type_ = Mu_Solvent;
103 nId_ = 1; // species identifier.
104 } else if (buffer == "solvent" || buffer == "solvent_size") {
105 type_ = Solvent;
106 nId_ = 1; // species identifier.
107 } else if (buffer == "cell_param") {
108 type_ = Cell_Param;
109 nId_ = 1; // lattice parameter identifier.
110 } else if (buffer == "lambda_pert") {
111 type_ = Lambda_Pert;
112 nId_ = 0; // No associated index
113 } else if (buffer == "v_monomer") {
114 type_ = Vmonomer;
115 nId_ = 0; // No associated index
116 } else {
117 UTIL_THROW("Invalid RampParameter::ParamType value");
118 }
119
120 if (id_.isAllocated()) id_.deallocate();
121 if (nId_ > 0) {
122 id_.allocate(nId_);
123 }
124
125 }
126
127 /*
128 * Write type enum value
129 */
130 template <int D>
131 void RampParameter<D>::writeParamType(std::ostream& out) const
132 { out << type(); }
133
134 /*
135 * Get initial (current) values of swept parameters from parent system.
136 */
137 template <int D>
139 { initial_ = get_(); }
140
141 /*
142 * Set new values of swept parameters in the parent system.
143 */
144 template <int D>
145 void RampParameter<D>::update(double newVal)
146 { set_(newVal); }
147
148 /*
149 * Get string representation of type enum value.
150 */
151 template <int D>
152 std::string RampParameter<D>::type() const
153 {
154 if (type_ == Block) {
156 return "block";
157 } else if (type_ == Chi) {
158 return "chi";
159 } else if (type_ == Kuhn) {
160 return "kuhn";
161 } else if (type_ == Phi_Polymer) {
162 return "phi_polymer";
163 } else if (type_ == Phi_Solvent) {
164 return "phi_solvent";
165 } else if (type_ == Mu_Polymer) {
166 return "mu_polymer";
167 } else if (type_ == Mu_Solvent) {
168 return "mu_solvent";
169 } else if (type_ == Solvent) {
170 return "solvent_size";
171 } else if (type_ == Cell_Param) {
172 return "cell_param";
173 } else if (type_ == Lambda_Pert) {
174 return "lambda_pert";
175 } else if (type_ == Vmonomer) {
176 return "vMonomer";
177 } else {
178 UTIL_THROW("This should never happen.");
179 }
180 }
181
182 template <int D>
183 double RampParameter<D>::get_()
184 {
185 if (type_ == Block) {
187 return systemPtr_->mixture().polymer(id(0)).block(id(1)).length();
188 } else if (type_ == Chi) {
189 return systemPtr_->interaction().chi(id(0), id(1));
190 } else if (type_ == Kuhn) {
191 return systemPtr_->mixture().monomer(id(0)).kuhn();
192 } else if (type_ == Phi_Polymer) {
193 return systemPtr_->mixture().polymer(id(0)).phi();
194 } else if (type_ == Phi_Solvent) {
195 return systemPtr_->mixture().solvent(id(0)).phi();
196 } else if (type_ == Mu_Polymer) {
197 return systemPtr_->mixture().polymer(id(0)).mu();
198 } else if (type_ == Mu_Solvent) {
199 return systemPtr_->mixture().solvent(id(0)).mu();
200 } else if (type_ == Solvent) {
201 return systemPtr_->mixture().solvent(id(0)).size();
202 } else if (type_ == Cell_Param) {
203 return systemPtr_->domain().unitCell().parameter(id(0));
204 } else if (type_ == Lambda_Pert) {
205 UTIL_CHECK(simulatorPtr_->hasPerturbation());
206 return simulatorPtr_->perturbation().lambda();
207 } else if (type_ == Vmonomer) {
208 return systemPtr_->mixture().vMonomer();
209 }else {
210 UTIL_THROW("This should never happen.");
211 }
212 }
213
214 template <int D>
215 void RampParameter<D>::set_(double newVal)
216 {
217 if (type_ == Chi) {
218 systemPtr_->interaction().setChi(id(0), id(1), newVal);
219 } else if (type_ == Kuhn) {
220 //systemPtr_->mixture().setKuhn(id(0), newVal);
221 systemPtr_->mixtureModifier().setKuhn(id(0), newVal);
222 } else if (type_ == Phi_Polymer) {
223 //systemPtr_->mixture().polymer(id(0)).setPhi(newVal);
224 systemPtr_->mixtureModifier().setPhiPolymer(id(0), newVal);
225 } else if (type_ == Mu_Polymer) {
226 //systemPtr_->mixture().polymer(id(0)).setMu(newVal);
227 systemPtr_->mixtureModifier().setMuPolymer(id(0), newVal);
228 } else if (type_ == Block) {
230 //systemPtr_->mixture().polymer(id(0)).block(id(1)).setLength(newVal);
231 systemPtr_->mixtureModifier().setBlockLength(id(0), id(1), newVal);
232 } else if (type_ == Phi_Solvent) {
233 //systemPtr_->mixture().solvent(id(0)).setPhi(newVal);
234 systemPtr_->mixtureModifier().setPhiSolvent(id(0), newVal);
235 } else if (type_ == Mu_Solvent) {
236 //systemPtr_->mixture().solvent(id(0)).setMu(newVal);
237 systemPtr_->mixtureModifier().setMuSolvent(id(0), newVal);
238 } else if (type_ == Solvent) {
239 //systemPtr_->mixture().solvent(id(0)).setSize(newVal);
240 systemPtr_->mixtureModifier().setSolventSize(id(0), newVal);
241 } else if (type_ == Cell_Param) {
242 FSArray<double,6> params
243 = systemPtr_->domain().unitCell().parameters();
244 UTIL_CHECK(id(0) < params.size());
245 params[id(0)] = newVal;
246 systemPtr_->setUnitCell(params);
247 } else if (type_ == Lambda_Pert) {
248 UTIL_CHECK(simulatorPtr_->hasPerturbation());
249 return simulatorPtr_->perturbation().setLambda(newVal);
250 } else if (type_ == Vmonomer) {
251 systemPtr_->mixtureModifier().setVMonomer(newVal);
252 } else {
253 UTIL_THROW("This should never happen.");
254 }
255 }
256
257 template <int D>
258 template <class Archive>
259 void RampParameter<D>::serialize(Archive ar, const unsigned int version)
260 {
261 serializeEnum(ar, type_, version);
262 ar & nId_;
263 if (nId_ > 0) {
264 for (int i = 0; i < nId_; ++i) {
265 ar & id_[i];
266 }
267 }
268 ar & initial_;
269 ar & change_;
270 }
271
272 // Definitions of operators, with no explicit instantiations.
273
274 /*
275 * Inserter for reading a RampParameter from an istream.
276 */
277 template <int D>
278 std::istream& operator >> (std::istream& in,
279 RampParameter<D>& param)
280 {
281 // Read the parameter type identifier string
282 param.readParamType(in);
283
284 // Read the identifiers associated with this parameter type.
285 if (param.nId_ > 0) {
286 for (int i = 0; i < param.nId_; ++i) {
287 in >> param.id_[i];
288 }
289 }
290
291 // Read in the range in the parameter to sweep over
292 in >> param.change_;
293
294 return in;
295 }
296
297 /*
298 * Extractor for writing a RampParameter to ostream.
299 */
300 template <int D>
301 std::ostream& operator << (std::ostream& out,
302 RampParameter<D> const & param)
303 {
304 param.writeParamType(out);
305 out << " ";
306 if (param.nId_ > 0) {
307 for (int i = 0; i < param.nId_; ++i) {
308 out << param.id(i);
309 out << " ";
310 }
311 }
312 out << param.change_;
313
314 return out;
315 }
316
317}
318}
319#endif
Block within a linear or branched block polymer.
Class for storing data about an individual ramp parameter.
void getInitial()
Get and store initial value this parameters.
int id(int i) const
Get id for a sub-object or element to which this is applied.
void serialize(Archive ar, const unsigned int version)
Serialize to or from an archive.
std::string type() const
Return a string representation of the 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 setSimulator(Simulator< D > &simulator)
Set the simulator and system associated with this object.
Field theoretic simulator (base class).
System< D > & system()
Get parent system by reference.
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:207
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
void serializeEnum(Archive &ar, T &data, const unsigned int version=0)
Serialize an enumeration value.
Definition serialize.h:59
bool isThread()
Is the thread model in use ?
Periodic fields and crystallography.
Definition CField.cpp:11
Real periodic fields, SCFT and PS-FTS (CPU).
Definition param_pc.dox:2
PSCF package top-level namespace.
Definition param_pc.dox:1
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