PSCF v1.1
pspg/sweep/SweepParameter.tpp
1#ifndef PSPG_SWEEP_PARAMETER_TPP
2#define PSPG_SWEEP_PARAMETER_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field Theory
6*
7* Copyright 2016 - 2022, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <pspg/System.h>
12#include <pspg/solvers/Mixture.h>
13#include <pspg/solvers/Polymer.h>
14#include <pspg/solvers/Block.h>
15
16#include <pscf/crystal/UnitCell.h>
17#include <pscf/inter/Interaction.h>
18#include <util/containers/FSArray.h>
19#include <util/global.h>
20
21#include <algorithm>
22#include <iomanip>
23
24namespace Pscf {
25namespace Pspg {
26
27 using namespace Util;
28
29 /*
30 * Default constructor.
31 */
32 template <int D>
34 : type_(SweepParameter<D>::Null),
35 nID_(0),
36 id_(),
37 initial_(0.0),
38 change_(0.0),
39 systemPtr_(0)
40 {}
41
42 /*
43 * Constructor, creates association with system.
44 */
45 template <int D>
47 : type_(SweepParameter<D>::Null),
48 nID_(0),
49 id_(),
50 initial_(0.0),
51 change_(0.0),
52 systemPtr_(&system)
53 {}
54
55 /*
56 * Read type, set nId and allocate id_ array.
57 */
58 template <int D>
59 void SweepParameter<D>::readParamType(std::istream& in)
60 {
61 std::string buffer;
62 in >> buffer;
63 std::transform(buffer.begin(), buffer.end(),
64 buffer.begin(), ::tolower);
65
66 if (buffer == "block" || buffer == "block_length") {
67 type_ = Block;
68 nID_ = 2; // polymer and block identifiers
69 } else if (buffer == "chi") {
70 type_ = Chi;
71 nID_ = 2; // two monomer type identifiers
72 } else if (buffer == "kuhn") {
73 type_ = Kuhn;
74 nID_ = 1; // monomer type identifier
75 } else if (buffer == "phi_polymer") {
76 type_ = Phi_Polymer;
77 nID_ = 1; //species identifier.
78 } else if (buffer == "phi_solvent") {
79 type_ = Phi_Solvent;
80 nID_ = 1; //species identifier.
81 } else if (buffer == "mu_polymer") {
82 type_ = Mu_Polymer;
83 nID_ = 1; //species identifier.
84 } else if (buffer == "mu_solvent") {
85 type_ = Mu_Solvent;
86 nID_ = 1; //species identifier.
87 } else if (buffer == "solvent" || buffer == "solvent_size") {
88 type_ = Solvent;
89 nID_ = 1; //species identifier.
90 } else if (buffer == "cell_param") {
91 type_ = Cell_Param;
92 nID_ = 1; //lattice parameter identifier.
93 } else {
94 std::string msg = "Invalid SweepParameter::ParamType value: ";
95 msg += buffer;
96 //UTIL_THROW("Invalid SweepParameter::ParamType value");
97 UTIL_THROW(msg.c_str());
98 }
99
100 if (id_.isAllocated()) id_.deallocate();
101 id_.allocate(nID_);
102
103 }
104
105 /*
106 * Write type enum value
107 */
108 template <int D>
109 void SweepParameter<D>::writeParamType(std::ostream& out) const
110 {
111 out << type();
112 }
113
114 /*
115 * Get the current value from the parent system.
116 */
117 template <int D>
119 {
120 initial_ = get_();
121 }
122
123 /*
124 * Set a new value in the parent system.
125 */
126 template <int D>
127 void SweepParameter<D>::update(double newVal)
128 {
129 set_(newVal);
130 }
131
132 /*
133 * Get string representation of type enum value.
134 */
135 template <int D>
136 std::string SweepParameter<D>::type() const
137 {
138 if (type_ == Block) {
139 return "block";
140 } else if (type_ == Chi) {
141 return "chi";
142 } else if (type_ == Kuhn) {
143 return "kuhn";
144 } else if (type_ == Phi_Polymer) {
145 return "phi_polymer";
146 } else if (type_ == Phi_Solvent) {
147 return "phi_solvent";
148 } else if (type_ == Mu_Polymer) {
149 return "mu_polymer";
150 } else if (type_ == Mu_Solvent) {
151 return "mu_solvent";
152 } else if (type_ == Solvent) {
153 return "solvent_size";
154 } else if (type_ == Cell_Param) {
155 return "cell_param";
156 } else {
157 UTIL_THROW("This should never happen.");
158 }
159 }
160
161 template <int D>
163 {
164 if (type_ == Block) {
165 return systemPtr_->mixture().polymer(id(0)).block(id(1)).length();
166 } else if (type_ == Chi) {
167 return systemPtr_->interaction().chi(id(0),id(1));
168 } else if (type_ == Kuhn) {
169 return systemPtr_->mixture().monomer(id(0)).kuhn();
170 } else if (type_ == Phi_Polymer) {
171 return systemPtr_->mixture().polymer(id(0)).phi();
172 } else if (type_ == Phi_Solvent) {
173 return systemPtr_->mixture().solvent(id(0)).phi();
174 } else if (type_ == Mu_Polymer) {
175 return systemPtr_->mixture().polymer(id(0)).mu();
176 } else if (type_ == Mu_Solvent) {
177 return systemPtr_->mixture().solvent(id(0)).mu();
178 } else if (type_ == Solvent) {
179 return systemPtr_->mixture().solvent(id(0)).size();
180 } else if (type_ == Cell_Param) {
181 return systemPtr_->unitCell().parameter(id(0));
182 } else {
183 UTIL_THROW("This should never happen.");
184 }
185 }
186
187 template <int D>
188 void SweepParameter<D>::set_(double newVal)
189 {
190 if (type_ == Block) {
191 systemPtr_->mixture().polymer(id(0)).block(id(1)).setLength(newVal);
192 } else if (type_ == Chi) {
193 systemPtr_->interaction().setChi(id(0), id(1), newVal);
194 } else if (type_ == Kuhn) {
195 systemPtr_->mixture().setKuhn(id(0), newVal);
196 } else if (type_ == Phi_Polymer) {
197 systemPtr_->mixture().polymer(id(0)).setPhi(newVal);
198 } else if (type_ == Phi_Solvent) {
199 systemPtr_->mixture().solvent(id(0)).setPhi(newVal);
200 } else if (type_ == Mu_Polymer) {
201 systemPtr_->mixture().polymer(id(0)).setMu(newVal);
202 } else if (type_ == Mu_Solvent) {
203 systemPtr_->mixture().solvent(id(0)).setMu(newVal);
204 } else if (type_ == Solvent) {
205 systemPtr_->mixture().solvent(id(0)).setSize(newVal);
206 } else if (type_ == Cell_Param) {
207 FSArray<double,6> params = systemPtr_->unitCell().parameters();
208 params[id(0)] = newVal;
209 systemPtr_->setUnitCell(params);
210 } else {
211 UTIL_THROW("This should never happen.");
212 }
213 }
214
215 template <int D>
216 template <class Archive>
217 void SweepParameter<D>::serialize(Archive ar, const unsigned int version)
218 {
219 serializeEnum(ar, type_, version);
220 ar & nID_;
221 for (int i = 0; i < nID_; ++i) {
222 ar & id_[i];
223 }
224 ar & initial_;
225 ar & change_;
226 }
227
228 // Definitions of operators, with no explicit instantiations.
229
236 template <int D>
237 std::istream& operator >> (std::istream& in,
238 SweepParameter<D>& param)
239 {
240 // Read the parameter type.
241 param.readParamType(in);
242 // Read the identifiers associated with this parameter type.
243 for (int i = 0; i < param.nID_; ++i) {
244 in >> param.id_[i];
245 }
246 // Read in the range in the parameter to sweep over
247 in >> param.change_;
248
249 return in;
250 }
251
258 template <int D>
259 std::ostream& operator << (std::ostream& out,
260 SweepParameter<D> const & param)
261 {
262 param.writeParamType(out);
263 out << " ";
264 for (int i = 0; i < param.nID_; ++i) {
265 out << param.id(i);
266 out << " ";
267 }
268 out << param.change_;
269
270 return out;
271 }
272
273}
274}
275
276#endif
Block within a branched polymer.
Solver and descriptor for a solvent species.
Class for storing data about an individual sweep parameter.
std::string type() const
Return a string representation of the parameter type.
void serialize(Archive ar, const unsigned int version)
Serialize to or from an archive.
int id(int i) const
Get a id for a sub-object or element to which this is applied.
void getInitial()
Store the pre-sweep value of the corresponding parameter.
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.
Main class in SCFT simulation of one system.
Definition: pspg/System.h:71
A fixed capacity (static) contiguous array with a variable logical size.
Definition: FSArray.h:38
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
void serializeEnum(Archive &ar, T &data, const unsigned int version=0)
Serialize an enumeration value.
Definition: serialize.h:42
C++ namespace for polymer self-consistent field theory (PSCF).
Utility classes for scientific computation.
Definition: accumulators.mod: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