PSCF v1.4.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 - 2025, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "RampParameter.h"
12#include <prdc/crystal/UnitCell.h>
13#include <pscf/chem/Monomer.h>
14#include <pscf/chem/PolymerModel.h>
15#include <pscf/interaction/Interaction.h>
16#include <util/containers/FSArray.h>
17
18#include <algorithm>
19
20namespace Pscf {
21namespace Rp {
22
23 using namespace Util;
24 using namespace Pscf::Prdc;
25
26 /*
27 * Default constructor.
28 */
29 template <int D, class T>
31 : type_(RampParameter<D,T>::Null),
32 nId_(0),
33 id_(),
34 initial_(0.0),
35 change_(0.0),
36 simulatorPtr_(0),
37 systemPtr_(0)
38 {}
39
40 /*
41 * Constructor, creates association with simulator and system.
42 */
43 template <int D, class T>
44 RampParameter<D,T>::RampParameter(typename T::Simulator& simulator)
45 : type_(RampParameter<D,T>::Null),
46 nId_(0),
47 id_(),
48 initial_(0.0),
49 change_(0.0),
50 simulatorPtr_(&simulator),
51 systemPtr_(&(simulator.system()))
52 {}
53
54 /*
55 * Set the simulator and system associated with this object.
56 */
57 template <int D, class T>
58 void RampParameter<D,T>::setSimulator(typename T::Simulator& simulator)
59 {
60 simulatorPtr_ = &simulator;
61 systemPtr_ = &(simulator.system());
62 }
63
64 /*
65 * Read type, set nId and allocate id_ array.
66 */
67 template <int D, class T>
68 void RampParameter<D,T>::readParamType(std::istream& in)
69 {
70 std::string buffer;
71 in >> buffer;
72 std::transform(buffer.begin(), buffer.end(),
73 buffer.begin(), ::tolower);
74
75 if (buffer == "block" || buffer == "block_length") {
77 type_ = Block;
78 nId_ = 2; // polymer and block identifiers
79 } else if (buffer == "chi") {
80 type_ = Chi;
81 nId_ = 2; // two monomer type identifiers
82 } else if (buffer == "kuhn") {
83 type_ = Kuhn;
84 nId_ = 1; // monomer type identifier
85 } else if (buffer == "phi_polymer") {
86 type_ = Phi_Polymer;
87 nId_ = 1; // species identifier.
88 } else if (buffer == "phi_solvent") {
89 type_ = Phi_Solvent;
90 nId_ = 1; // species identifier.
91 } else if (buffer == "mu_polymer") {
92 type_ = Mu_Polymer;
93 nId_ = 1; // species identifier.
94 } else if (buffer == "mu_solvent") {
95 type_ = Mu_Solvent;
96 nId_ = 1; // species identifier.
97 } else if (buffer == "solvent" || buffer == "solvent_size") {
98 type_ = Solvent;
99 nId_ = 1; // species identifier.
100 } else if (buffer == "cell_param") {
101 type_ = Cell_Param;
102 nId_ = 1; // lattice parameter identifier.
103 } else if (buffer == "lambda_pert") {
104 type_ = Lambda_Pert;
105 nId_ = 0; // No associated index
106 } else if (buffer == "v_monomer") {
107 type_ = Vmonomer;
108 nId_ = 0; // No associated index
109 } else {
110 UTIL_THROW("Invalid RampParameter::ParamType value");
111 }
112
113 if (id_.isAllocated()) id_.deallocate();
114 if (nId_ > 0) {
115 id_.allocate(nId_);
117
118 }
119
120 /*
121 * Write type enum value.
122 */
123 template <int D, class T>
124 void RampParameter<D,T>::writeParamType(std::ostream& out) const
125 { out << type(); }
126
127 /*
128 * Get initial (current) values of swept parameters from parent system.
129 */
130 template <int D, class T>
132 { initial_ = get_(); }
133
134 /*
135 * Set new values of swept parameters in the parent system.
136 */
137 template <int D, class T>
138 void RampParameter<D,T>::update(double newVal)
139 { set_(newVal); }
140
141 /*
142 * Get string representation of type enum value.
143 */
144 template <int D, class T>
145 std::string RampParameter<D,T>::type() const
146 {
147 if (type_ == Block) {
149 return "block";
150 } else if (type_ == Chi) {
151 return "chi";
152 } else if (type_ == Kuhn) {
153 return "kuhn";
154 } else if (type_ == Phi_Polymer) {
155 return "phi_polymer";
156 } else if (type_ == Phi_Solvent) {
157 return "phi_solvent";
158 } else if (type_ == Mu_Polymer) {
159 return "mu_polymer";
160 } else if (type_ == Mu_Solvent) {
161 return "mu_solvent";
162 } else if (type_ == Solvent) {
163 return "solvent_size";
164 } else if (type_ == Cell_Param) {
165 return "cell_param";
166 } else if (type_ == Lambda_Pert) {
167 return "lambda_pert";
168 } else if (type_ == Vmonomer) {
169 return "vMonomer";
170 } else {
171 UTIL_THROW("This should never happen.");
172 }
173 }
174
175 /*
176 * Return current value of this parameter.
177 */
178 template <int D, class T>
179 double RampParameter<D,T>::get_()
180 {
181 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_ == Vmonomer) {
199 return systemPtr_->mixture().vMonomer();
200 } else if (type_ == Cell_Param) {
201 return systemPtr_->domain().unitCell().parameter(id(0));
202 } else if (type_ == Lambda_Pert) {
203 UTIL_CHECK(simulatorPtr_->hasPerturbation());
204 return simulatorPtr_->perturbation().lambda();
205 } else {
206 UTIL_THROW("This should never happen.");
207 }
208 }
209
210 /*
211 * Modify system to set the value of this parameter.
212 */
213 template <int D, class T>
214 void RampParameter<D,T>::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) {
226 systemPtr_->mixtureModifier().setBlockLength(id(0), id(1), newVal);
227 } else if (type_ == Phi_Solvent) {
228 systemPtr_->mixtureModifier().setPhiSolvent(id(0), newVal);
229 } else if (type_ == Mu_Solvent) {
230 systemPtr_->mixtureModifier().setMuSolvent(id(0), newVal);
231 } else if (type_ == Solvent) {
232 systemPtr_->mixtureModifier().setSolventSize(id(0), newVal);
233 } else if (type_ == Vmonomer) {
234 systemPtr_->mixtureModifier().setVMonomer(newVal);
235 } else if (type_ == Cell_Param) {
236 FSArray<double,6> params;
237 params = systemPtr_->domain().unitCell().parameters();
238 UTIL_CHECK(id(0) < params.size());
239 params[id(0)] = newVal;
240 systemPtr_->setUnitCell(params);
241 } else if (type_ == Lambda_Pert) {
242 UTIL_CHECK(simulatorPtr_->hasPerturbation());
243 return simulatorPtr_->perturbation().setLambda(newVal);
244 } else {
245 UTIL_THROW("This should never happen.");
246 }
247 }
248
249}
250}
251#endif
RampParameter()
Default constructor.
void writeParamType(std::ostream &out) const
Write the parameter type to an output stream.
void getInitial()
Get and store initial value this parameters.
void setSimulator(typename T::Simulator &simulator)
Set the simulator and system associated with this object.
void update(double newVal)
Update the corresponding parameter value in the System.
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
#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
bool isThread()
Is the thread model in use ?
Periodic fields and crystallography.
Definition complex.cpp:11
Class templates for real-valued periodic fields.
PSCF package top-level namespace.