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