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