PSCF v1.3.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/Polymer.h>
17#include <rpg/solvers/Solvent.h>
18#include <rpg/solvers/Block.h>
19#include <rpg/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 Rpg {
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") {
85 type_ = Block;
86 nId_ = 2; // polymer and block identifiers
87 } else if (buffer == "chi") {
88 type_ = Chi;
89 nId_ = 2; // two monomer type identifiers
90 } else if (buffer == "kuhn") {
91 type_ = Kuhn;
92 nId_ = 1; // monomer type identifier
93 } else if (buffer == "phi_polymer") {
94 type_ = Phi_Polymer;
95 nId_ = 1; // species identifier.
96 } else if (buffer == "phi_solvent") {
97 type_ = Phi_Solvent;
98 nId_ = 1; // species identifier.
99 } else if (buffer == "mu_polymer") {
100 type_ = Mu_Polymer;
101 nId_ = 1; // species identifier.
102 } else if (buffer == "mu_solvent") {
103 type_ = Mu_Solvent;
104 nId_ = 1; // species identifier.
105 } else if (buffer == "solvent" || buffer == "solvent_size") {
106 type_ = Solvent;
107 nId_ = 1; // species identifier.
108 } else if (buffer == "cell_param") {
109 type_ = Cell_Param;
110 nId_ = 1; // lattice parameter identifier.
111 } else if (buffer == "lambda_pert") {
112 type_ = Lambda_Pert;
113 nId_ = 0; // No associated index
114 } else if (buffer == "v_monomer") {
115 type_ = Vmonomer;
116 nId_ = 0; // No associated index
117 } else {
118 UTIL_THROW("Invalid RampParameter::ParamType value");
119 }
120
121 if (id_.isAllocated()) id_.deallocate();
122 if (nId_ > 0) {
123 id_.allocate(nId_);
124 }
125
126 }
127
128 /*
129 * Write type enum value
130 */
131 template <int D>
132 void RampParameter<D>::writeParamType(std::ostream& out) const
133 { out << type(); }
134
135 /*
136 * Get initial (current) values of swept parameters from parent system.
137 */
138 template <int D>
140 { initial_ = get_(); }
141
142 /*
143 * Set new values of swept parameters in the parent system.
144 */
145 template <int D>
146 void RampParameter<D>::update(double newVal)
147 { set_(newVal); }
148
149 /*
150 * Get string representation of type enum value.
151 */
152 template <int D>
153 std::string RampParameter<D>::type() const
154 {
155 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) {
186 return systemPtr_->mixture().polymer(id(0)).block(id(1)).length();
187 } else if (type_ == Chi) {
188 return systemPtr_->interaction().chi(id(0), id(1));
189 } else if (type_ == Kuhn) {
190 return systemPtr_->mixture().monomer(id(0)).kuhn();
191 } else if (type_ == Phi_Polymer) {
192 return systemPtr_->mixture().polymer(id(0)).phi();
193 } else if (type_ == Phi_Solvent) {
194 return systemPtr_->mixture().solvent(id(0)).phi();
195 } else if (type_ == Mu_Polymer) {
196 return systemPtr_->mixture().polymer(id(0)).mu();
197 } else if (type_ == Mu_Solvent) {
198 return systemPtr_->mixture().solvent(id(0)).mu();
199 } else if (type_ == Solvent) {
200 return systemPtr_->mixture().solvent(id(0)).size();
201 } else if (type_ == Cell_Param) {
202 return systemPtr_->domain().unitCell().parameter(id(0));
203 } else if (type_ == Lambda_Pert) {
204 UTIL_CHECK(simulatorPtr_->hasPerturbation());
205 return simulatorPtr_->perturbation().lambda();
206 } else if (type_ == Vmonomer) {
207 return systemPtr_->mixture().vMonomer();
208 }else {
209 UTIL_THROW("This should never happen.");
210 }
211 }
212
213 template <int D>
214 void RampParameter<D>::set_(double newVal)
215 {
216 if (type_ == Chi) {
217 systemPtr_->interaction().setChi(id(0), id(1), newVal);
218 } else if (type_ == Kuhn) {
219 systemPtr_->mixtureModifier().setKuhn(id(0), newVal);
220 } else if (type_ == Phi_Polymer) {
221 systemPtr_->mixtureModifier().setPhiPolymer(id(0), newVal);
222 } else if (type_ == Mu_Polymer) {
223 systemPtr_->mixtureModifier().setMuPolymer(id(0), newVal);
224 } else if (type_ == Block) {
225 systemPtr_->mixtureModifier().setBlockLength(id(0), id(1), newVal);
226 } else if (type_ == Phi_Solvent) {
227 systemPtr_->mixtureModifier().setPhiSolvent(id(0), newVal);
228 } else if (type_ == Mu_Solvent) {
229 systemPtr_->mixtureModifier().setMuSolvent(id(0), newVal);
230 } else if (type_ == Solvent) {
231 systemPtr_->mixtureModifier().setSolventSize(id(0), newVal);
232 } else if (type_ == Vmonomer) {
233 systemPtr_->mixtureModifier().setVMonomer(newVal);
234 #if 0
235 } else if (type_ == Kuhn) {
236 systemPtr_->mixture().setKuhn(id(0), newVal);
237 } else if (type_ == Phi_Polymer) {
238 systemPtr_->mixture().polymer(id(0)).setPhi(newVal);
239 } else if (type_ == Phi_Solvent) {
240 systemPtr_->mixture().solvent(id(0)).setPhi(newVal);
241 } else if (type_ == Mu_Polymer) {
242 systemPtr_->mixture().polymer(id(0)).setMu(newVal);
243 } else if (type_ == Mu_Solvent) {
244 systemPtr_->mixture().solvent(id(0)).setMu(newVal);
245 } else if (type_ == Block) {
246 systemPtr_->mixture().polymer(id(0)).block(id(1)).setLength(newVal);
247 } else if (type_ == Solvent) {
248 systemPtr_->mixture().solvent(id(0)).setSize(newVal);
249 #endif
250 } else if (type_ == Cell_Param) {
251 FSArray<double,6> params;
252 params = systemPtr_->domain().unitCell().parameters();
253 params[id(0)] = newVal;
254 systemPtr_->setUnitCell(params);
255 } else if (type_ == Lambda_Pert) {
256 UTIL_CHECK(simulatorPtr_->hasPerturbation());
257 return simulatorPtr_->perturbation().setLambda(newVal);
258 } else {
259 UTIL_THROW("This should never happen.");
260 }
261 }
262
263 template <int D>
264 template <class Archive>
265 void RampParameter<D>::serialize(Archive ar, const unsigned int version)
266 {
267 serializeEnum(ar, type_, version);
268 ar & nId_;
269 if (nId_ > 0) {
270 for (int i = 0; i < nId_; ++i) {
271 ar & id_[i];
272 }
273 }
274 ar & initial_;
275 ar & change_;
276 }
277
278 // Definitions of operators, with no explicit instantiations.
279
280 /*
281 * Inserter for reading a RampParameter from an istream.
282 */
283 template <int D>
284 std::istream& operator >> (std::istream& in,
285 RampParameter<D>& param)
286 {
287 // Read the parameter type identifier string
288 param.readParamType(in);
289
290 // Read the identifiers associated with this parameter type.
291 if (param.nId_ > 0) {
292 for (int i = 0; i < param.nId_; ++i) {
293 in >> param.id_[i];
294 }
295 }
296
297 // Read in the range in the parameter to sweep over
298 in >> param.change_;
299
300 return in;
301 }
302
303 /*
304 * Extractor for writing a RampParameter to ostream.
305 */
306 template <int D>
307 std::ostream& operator << (std::ostream& out,
308 RampParameter<D> const & param)
309 {
310 param.writeParamType(out);
311 out << " ";
312 if (param.nId_ > 0) {
313 for (int i = 0; i < param.nId_; ++i) {
314 out << param.id(i);
315 out << " ";
316 }
317 }
318 out << param.change_;
319
320 return out;
321 }
322
323}
324}
325#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.
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