PSCF v1.1
SweepParameter.cpp
1/*
2* PSCF - Polymer Self-Consistent Field Theory
3*
4* Copyright 2016 - 2022, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include <fd1d/sweep/SweepParameter.h>
9#include <fd1d/solvers/Block.h>
10#include <fd1d/solvers/Mixture.h>
11#include <fd1d/solvers/Polymer.h>
12#include <fd1d/System.h>
13#include <pscf/inter/Interaction.h>
14#include <util/containers/FSArray.h>
15#include <util/global.h>
16
17#include <algorithm>
18#include <iomanip>
19
20namespace Pscf {
21namespace Fd1d {
22
23 using namespace Util;
24
25 /*
26 * Default constructor.
27 */
29 : type_(SweepParameter::Null),
30 nID_(0),
31 id_(),
32 initial_(0.0),
33 change_(0.0),
34 systemPtr_(0)
35 {}
36
37 /*
38 * Constructor, creates association with system.
39 */
41 : type_(SweepParameter::Null),
42 nID_(0),
43 id_(),
44 initial_(0.0),
45 change_(0.0),
46 systemPtr_(&system)
47 {}
48
49 /*
50 * Read type, set nId and allocate id_ array.
51 */
52 void SweepParameter::readParamType(std::istream& in)
53 {
54 std::string buffer;
55 in >> buffer;
56 std::transform(buffer.begin(), buffer.end(),
57 buffer.begin(), ::tolower);
58
59 if (buffer == "block" || buffer == "block_length") {
60 type_ = Block;
61 nID_ = 2; // polymer and block identifiers
62 } else if (buffer == "chi") {
63 type_ = Chi;
64 nID_ = 2; // two monomer type identifiers
65 } else if (buffer == "kuhn") {
66 type_ = Kuhn;
67 nID_ = 1; // monomer type identifier
68 } else if (buffer == "phi_polymer") {
69 type_ = Phi_Polymer;
70 nID_ = 1; //species identifier.
71 } else if (buffer == "phi_solvent") {
72 type_ = Phi_Solvent;
73 nID_ = 1; //species identifier.
74 } else if (buffer == "mu_polymer") {
75 type_ = Mu_Polymer;
76 nID_ = 1; //species identifier.
77 } else if (buffer == "mu_solvent") {
78 type_ = Mu_Solvent;
79 nID_ = 1; //species identifier.
80 } else if (buffer == "solvent" || buffer == "solvent_size") {
81 type_ = Solvent;
82 nID_ = 1; //species identifier.
83 } else if (buffer == "cell_param") {
84 type_ = Cell_Param;
85 nID_ = 1; //lattice parameter identifier.
86 } else {
87 UTIL_THROW("Invalid SweepParameter::ParamType value");
88 }
89
90 if (id_.isAllocated()) id_.deallocate();
91 id_.allocate(nID_);
92 }
93
94 /*
95 * Write type enum value
96 */
97 void SweepParameter::writeParamType(std::ostream& out) const
98 {
99 out << type();
100 }
101
102 /*
103 * Get the current value from the parent system.
104 */
106 {
107 initial_ = get_();
108 }
109
110 /*
111 * Set a new value in the parent system.
112 */
113 void SweepParameter::update(double newVal)
114 {
115 set_(newVal);
116 }
117
118 /*
119 * Get string representation of type enum value.
120 */
121 std::string SweepParameter::type() const
122 {
123 if (type_ == Block) {
124 return "block";
125 } else if (type_ == Chi) {
126 return "chi";
127 } else if (type_ == Kuhn) {
128 return "kuhn";
129 } else if (type_ == Phi_Polymer) {
130 return "phi_polymer";
131 } else if (type_ == Phi_Solvent) {
132 return "phi_solvent";
133 } else if (type_ == Mu_Polymer) {
134 return "mu_polymer";
135 } else if (type_ == Mu_Solvent) {
136 return "mu_solvent";
137 } else if (type_ == Solvent) {
138 return "solvent_size";
139 } else {
140 UTIL_THROW("Invalid type_ in accessor SweepParameter::type().");
141 }
142 }
143
144 double SweepParameter::get_()
145 {
146 if (type_ == Block) {
147 return systemPtr_->mixture().polymer(id(0)).block(id(1)).length();
148 } else if (type_ == Chi) {
149 return systemPtr_->interaction().chi(id(0), id(1));
150 } else if (type_ == Kuhn) {
151 return systemPtr_->mixture().monomer(id(0)).kuhn();
152 } else if (type_ == Phi_Polymer) {
153 return systemPtr_->mixture().polymer(id(0)).phi();
154 } else if (type_ == Phi_Solvent) {
155 return systemPtr_->mixture().solvent(id(0)).phi();
156 } else if (type_ == Mu_Polymer) {
157 return systemPtr_->mixture().polymer(id(0)).mu();
158 } else if (type_ == Mu_Solvent) {
159 return systemPtr_->mixture().solvent(id(0)).mu();
160 } else if (type_ == Solvent) {
161 return systemPtr_->mixture().solvent(id(0)).size();
162 } else {
163 UTIL_THROW("Invalid type_ in SweepParameter::get_.");
164 }
165 }
166
167 void SweepParameter::set_(double newVal)
168 {
169 if (type_ == Block) {
170 systemPtr_->mixture().polymer(id(0)).block(id(1)).setLength(newVal);
171 } else if (type_ == Chi) {
172 systemPtr_->interaction().setChi(id(0), id(1), newVal);
173 } else if (type_ == Kuhn) {
174 systemPtr_->mixture().setKuhn(id(0), newVal);
175 } else if (type_ == Phi_Polymer) {
176 systemPtr_->mixture().polymer(id(0)).setPhi(newVal);
177 } else if (type_ == Phi_Solvent) {
178 systemPtr_->mixture().solvent(id(0)).setPhi(newVal);
179 } else if (type_ == Mu_Polymer) {
180 systemPtr_->mixture().polymer(id(0)).setMu(newVal);
181 } else if (type_ == Mu_Solvent) {
182 systemPtr_->mixture().solvent(id(0)).setMu(newVal);
183 } else if (type_ == Solvent) {
184 systemPtr_->mixture().solvent(id(0)).setSize(newVal);
185 } else {
186 UTIL_THROW("Invalid type_ in SweepParameter::set_.");
187 }
188 }
189
190 // Definitions of operators, with no explicit instantiations.
191
198 std::istream& operator >> (std::istream& in,
199 SweepParameter& param)
200 {
201 // Read the parameter type.
202 param.readParamType(in);
203 // Read the identifiers associated with this parameter type.
204 for (int i = 0; i < param.nID_; ++i) {
205 in >> param.id_[i];
206 }
207 // Read in the range in the parameter to sweep over
208 in >> param.change_;
209
210 return in;
211 }
212
219 std::ostream& operator << (std::ostream& out,
220 SweepParameter const & param)
221 {
222 param.writeParamType(out);
223 out << " ";
224 for (int i = 0; i < param.nID_; ++i) {
225 out << param.id(i);
226 out << " ";
227 }
228 out << param.change_;
229
230 return out;
231 }
232
233}
234}
Block within a branched polymer.
void setKuhn(int monomerId, double kuhn)
Reset statistical segment length for one monomer type.
Solver and descriptor for a solvent species.
Class for storing data about an individual sweep parameter.
void getInitial()
Store the pre-sweep value of the corresponding parameter.
std::string type() const
Return a string representation of the parameter type.
int id(int i) const
Get a id for a sub-object or element to which this is applied.
void writeParamType(std::ostream &out) const
Write the parameter type to an output stream.
SweepParameter()
Default constructor.
void update(double newVal)
Update the corresponding parameter value in the system.
Main class in SCFT simulation of one system.
Definition: fd1d/System.h:63
Mixture & mixture()
Get Mixture by reference.
Definition: fd1d/System.h:537
Interaction & interaction()
Get interaction (i.e., excess free energy) by reference.
Definition: fd1d/System.h:549
double chi(int i, int j) const
Return one element of the chi matrix.
Definition: Interaction.h:163
void setChi(int i, int j, double chi)
Change one element of the chi matrix.
Definition: Interaction.cpp:78
Polymer & polymer(int id)
Get a polymer object.
Definition: MixtureTmpl.h:220
Monomer const & monomer(int id) const
Get a Monomer type descriptor (const reference).
Definition: MixtureTmpl.h:206
Solvent & solvent(int id)
Set a solvent solver object.
Definition: MixtureTmpl.h:234
double kuhn() const
Statistical segment length (random walk step size).
Definition: Monomer.h:118
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:199
void deallocate()
Dellocate the underlying C array.
Definition: DArray.h:217
bool isAllocated() const
Return true if this DArray has been allocated, false otherwise.
Definition: DArray.h:247
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
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