1#ifndef RP_SWEEP_PARAMETER_TPP
2#define RP_SWEEP_PARAMETER_TPP
11#include <rp/scft/sweep/SweepParameter.h>
13#include <rp/system/System.h>
14#include <rp/solvers/Mixture.h>
15#include <rp/solvers/MixtureModifier.h>
16#include <rp/solvers/Polymer.h>
17#include <rp/solvers/Solvent.h>
18#include <rp/solvers/Block.h>
19#include <rp/field/Domain.h>
20#include <rp/scft/iterator/Iterator.h>
22#include <pscf/crystal/UnitCell.h>
24#include <pscf/interaction/Interaction.h>
25#include <pscf/sweep/ParameterModifier.h>
27#include <util/containers/FSArray.h>
28#include <util/misc/ioUtil.h>
42 template <
int D,
class T>
50 parameterTypesPtr_(nullptr),
57 template <
int D,
class T>
65 parameterTypesPtr_(nullptr),
72 template <
int D,
class T>
73 void SweepParameter<D,T>::readParamType(std::istream& in)
77 std::transform(buffer.begin(), buffer.end(),
78 buffer.begin(), ::tolower);
80 if (buffer ==
"block" || buffer ==
"block_length") {
83 }
else if (buffer ==
"chi") {
86 }
else if (buffer ==
"kuhn") {
89 }
else if (buffer ==
"phi_polymer") {
92 }
else if (buffer ==
"phi_solvent") {
95 }
else if (buffer ==
"mu_polymer") {
98 }
else if (buffer ==
"mu_solvent") {
101 }
else if (buffer ==
"solvent" || buffer ==
"solvent_size") {
104 }
else if (buffer ==
"cell_param") {
110 if (parameterTypesPtr_) {
111 for (
int i = 0; i < parameterTypesPtr_->size(); i++) {
112 ParameterType& pType = (*parameterTypesPtr_)[i];
113 if (buffer == pType.name) {
116 parameterTypeId_ = i;
124 msg =
"Invalid SweepParameter::ParamType value: " + buffer;
129 if (id_.isAllocated()) id_.deallocate();
137 template <
int D,
class T>
144 template <
int D,
class T>
149 return (*parameterTypesPtr_)[parameterTypeId_];
156 template <
int D,
class T>
166 template <
int D,
class T>
171 if (type_ == Block) {
172 checkId(0, mixture.
nPolymer(),
"polymer");
174 }
else if (type_ == Chi) {
175 checkId(0, mixture.
nMonomer(),
"monomer");
176 checkId(1, mixture.
nMonomer(),
"monomer");
177 }
else if (type_ == Kuhn) {
178 checkId(0, mixture.
nMonomer(),
"monomer");
179 }
else if (type_ == Phi_Polymer || type_ == Mu_Polymer) {
180 checkId(0, mixture.
nPolymer(),
"polymer");
181 }
else if (type_ == Phi_Solvent || type_ == Mu_Solvent
182 || type_ == Solvent) {
183 checkId(0, mixture.
nSolvent(),
"solvent");
184 }
else if (type_ == Cell_Param) {
185 checkId(0, systemPtr_->domain().unitCell().nParameter(),
186 "lattice parameter");
193 template <
int D,
class T>
194 void SweepParameter<D,T>::checkId(
int i,
int n,
195 std::string
const & name)
const
197 if (
id(i) < 0 ||
id(i) >= n) {
198 std::string msg =
"Sweep parameter " + type() +
": " + name
200 +
" out of range [0, " +
toString(n) +
")";
208 template <
int D,
class T>
215 template <
int D,
class T>
218 if (type_ == Block) {
220 }
else if (type_ == Chi) {
222 }
else if (type_ == Kuhn) {
224 }
else if (type_ == Phi_Polymer) {
225 return "phi_polymer";
226 }
else if (type_ == Phi_Solvent) {
227 return "phi_solvent";
228 }
else if (type_ == Mu_Polymer) {
230 }
else if (type_ == Mu_Solvent) {
232 }
else if (type_ == Solvent) {
233 return "solvent_size";
234 }
else if (type_ == Cell_Param) {
236 }
else if (type_ == Special) {
243 template <
int D,
class T>
244 double SweepParameter<D,T>::get_()
248 return systemPtr_->interaction().chi(
id(0),
id(1));
249 }
else if (type_ == Kuhn) {
250 return systemPtr_->mixture().monomer(
id(0)).kuhn();
251 }
else if (type_ == Phi_Polymer) {
252 return systemPtr_->mixture().polymer(
id(0)).phi();
253 }
else if (type_ == Mu_Polymer) {
254 return systemPtr_->mixture().polymer(
id(0)).mu();
255 }
else if (type_ ==
Block) {
256 return systemPtr_->mixture().polymer(
id(0)).block(
id(1)).length();
257 }
else if (type_ == Phi_Solvent) {
258 return systemPtr_->mixture().solvent(
id(0)).phi();
259 }
else if (type_ == Mu_Solvent) {
260 return systemPtr_->mixture().solvent(
id(0)).mu();
262 return systemPtr_->mixture().solvent(
id(0)).size();
263 }
else if (type_ == Cell_Param) {
264 return systemPtr_->domain().unitCell().parameter(
id(0));
265 }
else if (type_ == Special) {
266 ParameterModifier* modifier = parameterType().modifierPtr;
267 std::string name = parameterType().name;
268 return modifier->getParameter(name,id_);
274 template <
int D,
class T>
275 void SweepParameter<D,T>::set_(
double newVal)
279 systemPtr_->interaction().setChi(
id(0),
id(1), newVal);
280 }
else if (type_ == Kuhn) {
281 systemPtr_->mixtureModifier().setKuhn(
id(0), newVal);
282 }
else if (type_ == Phi_Polymer) {
283 systemPtr_->mixtureModifier().setPhiPolymer(
id(0), newVal);
284 }
else if (type_ == Mu_Polymer) {
285 systemPtr_->mixtureModifier().setMuPolymer(
id(0), newVal);
286 }
else if (type_ ==
Block) {
287 systemPtr_->mixtureModifier().setBlockLength(
id(0),
id(1), newVal);
288 }
else if (type_ == Phi_Solvent) {
289 systemPtr_->mixtureModifier().setPhiSolvent(
id(0), newVal);
290 }
else if (type_ == Mu_Solvent) {
291 systemPtr_->mixtureModifier().setMuSolvent(
id(0), newVal);
293 systemPtr_->mixtureModifier().setSolventSize(
id(0), newVal);
294 }
else if (type_ == Cell_Param) {
300 if (systemPtr_->hasIterator()) {
303 if (iterator.isFlexible() &&
id(0) < flexParams.
size()
304 && flexParams[
id(0)]) {
305 std::string msg =
"Sweep parameter cell_param "
306 +
toString(
id(0)) +
": lattice parameter"
307 +
" is flexible, so the iterator would"
308 +
" optimize it and undo the sweep; make"
309 +
" it rigid (isFlexible 0, or 0 in the"
310 +
" flexibleParams array)";
315 = systemPtr_->domain().unitCell().parameters();
316 params[id(0)] = newVal;
317 systemPtr_->setUnitCell(params);
318 }
else if (type_ == Special) {
319 ParameterModifier* modifier = parameterType().modifierPtr;
320 std::string name = parameterType().name;
321 return modifier->setParameter(name,id_,newVal);
328 template <
int D,
class T>
329 template <
class Archive>
334 for (
int i = 0; i < nId_; ++i) {
346 template <
int D,
class T>
351 param.readParamType(in);
354 for (
int i = 0; i < param.nId_; ++i) {
366 template <
int D,
class T>
370 param.writeParamType(out);
372 for (
int i = 0; i < param.nId_; ++i) {
376 out << param.change_;
int nMonomer() const
Get number of monomer types.
int nPolymer() const
Get number of polymer species.
int nSolvent() const
Get number of solvent (point particle) species.
int nBlock() const
Number of blocks.
Base class template for iterative solvers for SCF equations.
Solver and descriptor for a mixture of polymers and solvents.
PolymerT & polymer(int id)
Get a polymer solver object by non-const reference.
Solver and descriptor for a solvent species.
Class template for storing data about an individual sweep parameter.
void getInitial()
Get initial value of this parameter.
SweepParameter()
Default constructor.
std::string type() const
Return a string representation of the parameter type.
void writeParamType(std::ostream &out) const
Write the parameter type to an output stream.
ParameterType & parameterType() const
Get the ParameterType object for a specialized sweep parameter.
void serialize(Archive &ar, const unsigned int version)
Serialize to or from an archive.
void update(double newVal)
Update the corresponding parameter value in the system.
bool isSpecialized() const
Is this SweepParameter a specialized parameter type?
void checkIds() const
Check that the indices of this parameter are in range.
Base class template for classes that represent a complete system.
A fixed capacity (static) contiguous array with a variable logical size.
int size() const
Return logical size of this array (i.e., number of elements).
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
std::string toString(int n)
Return string representation of an integer.
void serializeEnum(Archive &ar, T &data, const unsigned int version=0)
Serialize an enumeration value.
Code for programs that simulate real-valued periodic fields.
PSCF package top-level namespace.
std::istream & operator>>(std::istream &in, Pair< Data > &pair)
Input a Pair from an istream.
std::ostream & operator<<(std::ostream &out, const Pair< Data > &pair)
Output a Pair to an ostream, without line breaks.
Declaration of a specialized sweep parameter type.