PSCF v1.5.0
FilmFieldGenExtBase.tpp
1#ifndef RP_FILM_FIELD_GEN_EXT_BASE_TPP
2#define RP_FILM_FIELD_GEN_EXT_BASE_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "FilmFieldGenExtBase.h"
12#include <pscf/crystal/SpaceGroup.h>
13#include <pscf/sweep/ParameterType.h>
14#include <util/param/ParamComponent.h>
15#include <util/containers/GArray.h>
16
17namespace Pscf {
18namespace Rp {
19
20 using namespace Util;
21
22 /*
23 * Constructor
24 */
25 template <int D>
28 normalVecId_(-1),
29 interfaceThickness_(-1.0),
30 excludedThickness_(-1.0)
31 {
32 type_ = External;
33 isDependent_ = true;
34 }
35
36 /*
37 * Destructor
38 */
39 template <int D>
42
43 /*
44 * Read and initialize.
45 */
46 template <int D>
48 {
49 // First, read data defining the mask (quietly, with echo = false)
52 read(in, "normalVecId", normalVecId_);
53 read(in, "interfaceThickness", interfaceThickness_);
54 read(in, "excludedThickness", excludedThickness_);
55 double tmp;
56 readOptional(in, "fBulk", tmp); // will not be used
58
59 // Remove all of these parameters from this ParamComposite, since
60 // they are already managed by the FilmFieldGenMask ParamComposite
62
63 // Make sure inputs are valid
64 if (normalVecId_ >= D || normalVecId_ < 0) {
65 UTIL_THROW("bad value for normalVecId, must be in [0,D)");
66 }
67 if (interfaceThickness_ > excludedThickness_) {
68 UTIL_THROW("excludedThickness must be larger than interfaceThickness");
69 }
70 if ((excludedThickness_ <= 0) || (interfaceThickness_ <= 0)) {
71 UTIL_THROW("excludedThickness and interfaceThickness must be >0");
72 }
73
74 // Allocate chiBottom_ and chiTop_
75 int nm = systemNMonomer();
76 chiBottom_.allocate(nm);
77 chiTop_.allocate(nm);
78
79 // Read chiBottom_ and chiTop_ arrays
80 readDArray(in, "chiBottom", chiBottom_, nm);
81 readDArray(in, "chiTop", chiTop_, nm);
82 }
83
84 /*
85 * Check whether system has changed such that the fields need updating
86 */
87 template <int D>
89 {
90 UTIL_CHECK(normalVecId_ >= 0); // Check that readParameters was called
91
92 // If chiBottomCurrent_ and chiTopCurrent_ are unset, generate() has
93 // not been called. Therefore, needsUpdate is true
94 if (!chiBottomCurrent_.isAllocated()) {
95 UTIL_CHECK(!chiTopCurrent_.isAllocated());
96 return true;
97 }
98
99 // Check if chiBottom or chiTop have been changed
100 for (int i = 0; i < chiBottom_.capacity(); i++) {
101 if ((chiBottom_[i] != chiBottomCurrent_[i]) ||
102 (chiTop_[i] != chiTopCurrent_[i])) {
103 return true;
104 }
105 }
106
107 // If chiTop and chiBottom are unchanged and all zeros, no update needed
108 if (isAthermal()) return false;
109
110 // Check if system normalVec differ from normalVecCurrent_
111 if (normalVecCurrent_ == systemLatticeVector(normalVecId_)) {
112 return false;
113 } else {
114 return true;
115 }
116 }
117
118 /*
119 * Check that the system is compatible with this field
120 */
121 template <int D>
123 {
124 // Ensure that space group symmetry is compatible with the fields
125
126 // If chiBottom == chiTop, do nothing. All necessary checks have
127 // already been performed by the mask generator.
128 if (hasSymmetricWalls()) return;
129
130 // Otherwise, walls are asymmetric, so the space group must not
131 // have any operations that swap the two walls
132 std::string groupName = systemSpaceGroup();
133 if (groupName.empty()) return; // no space group, nothing to check
134 SpaceGroup<D> group;
135 std::ifstream in;
136
137 // Open and read file containing space group's symmetry operations
138 readGroup(groupName, group);
139
140 // Make sure all symmetry operations are allowed
141 std::string msg = "Space group contains forbidden symmetry operations";
142 for (int i = 0; i < group.size(); i++) {
143 for (int j = 0; j < D; j++) {
144 int r = group[i].R(normalVecId_,j);
145 if (j == normalVecId_) {
146 if (r != 1) {
147 UTIL_THROW(msg.c_str());
148 }
149 } else { // j != normalVecId_
150 if (r != 0) {
151 UTIL_THROW(msg.c_str());
152 }
153 }
154 }
155 if (group[i].t(normalVecId_) != 0) {
156 UTIL_THROW(msg.c_str());
157 }
158 }
159 }
160
161 /*
162 * Check whether or not the two walls are chemically identical using
163 * the chi array.
164 */
165 template <int D>
167 {
168 int nm = systemNMonomer();
169
170 UTIL_CHECK(nm > 0);
171 UTIL_CHECK(chiBottom_.capacity() == nm);
172 UTIL_CHECK(chiTop_.capacity() == nm);
173
174 for (int i = 0; i < nm; i++) {
175 if (fabs(chiBottom_[i]-chiTop_[i]) > 1e-7) {
176 return false;
177 }
178 }
179 return true;
180 }
181
182 /*
183 * Check whether or not the walls are athermal (only true if all values
184 * in chi array are zero)
185 */
186 template <int D>
188 {
189 int nm = systemNMonomer();
190
191 UTIL_CHECK(nm > 0);
192 UTIL_CHECK(chiBottom_.capacity() == nm);
193 UTIL_CHECK(chiTop_.capacity() == nm);
194
195 for (int i = 0; i < nm; i++) {
196 if ((fabs(chiBottom_[i]) >= 1e-7) || (fabs(chiTop_[i]) >= 1e-7)) {
197 return false;
198 }
199 }
200 return true;
201 }
202
203 /*
204 * Return specialized sweep parameter types to add to the Sweep object.
205 */
206 template <int D>
209 {
211 pTypes.append(ParameterType("chi_top", 1, *this));
212 pTypes.append(ParameterType("chi_bottom", 1, *this));
213 return pTypes;
214 }
215
216 /*
217 * Set the value of a specialized sweep parameter (chi_top or chi_bottom).
218 */
219 template <int D>
221 DArray<int> ids,
222 double value,
223 bool& success)
224 {
225 success = true;
226 if (name == "chi_top") {
227 chiTop_[ids[0]] = value;
228 } else if (name == "chi_bottom") {
229 chiBottom_[ids[0]] = value;
230 } else {
231 success = false;
232 }
233 }
234
235 /*
236 * Get the value of a specialized sweep parameter (chi_top or chi_bottom).
237 */
238 template <int D>
239 double FilmFieldGenExtBase<D>::getParameter(std::string name,
240 DArray<int> ids,
241 bool& success)
242 const
243 {
244 success = true;
245 if (name == "chi_top") {
246 return chiTop_[ids[0]];
247 } else if (name == "chi_bottom") {
248 return chiBottom_[ids[0]];
249 } else {
250 success = false;
251 return 0.0;
252 }
253 }
254
255}
256}
257#endif
Type type_
Type of field (Mask, External, or None).
bool isDependent_
Is this object dependent on parameters of another FieldGeneratorBase?
void readParameters(std::istream &in)
Read parameters and initialize.
bool isAthermal() const
Are the walls athermal?
virtual int systemNMonomer() const =0
Get the number of monomer species for this system.
virtual std::string systemSpaceGroup() const =0
Get the space group name for this system (empty if none).
RealVec< D > normalVecCurrent_
The lattice vector normal to the film used to generate these fields.
double getParameter(std::string name, DArray< int > ids, bool &success) const
Get the value of a specialized sweep parameter.
DArray< double > chiBottomCurrent_
The chiBottom array used to generate the current external fields.
GArray< ParameterType > getParameterTypes()
Return specialized sweep parameter types to add to the Sweep object.
void checkCompatibility()
Check that the system is compatible with these fields.
bool hasSymmetricWalls() const
Are the walls chemically identical?
void setParameter(std::string name, DArray< int > ids, double value, bool &success)
Set the value of a specialized sweep parameter.
DArray< double > chiTopCurrent_
The chiTop array used to generate the current external fields.
bool needsUpdate() const
Check whether system has changed such that the fields need updating.
virtual RealVec< D > systemLatticeVector(int id) const =0
Get one of the lattice vectors for this system.
Crystallographic space group.
Definition SpaceGroup.h:31
int size() const
Return number of elements in group (i.e., the order of the group).
Dynamically allocatable contiguous array template.
Definition DArray.h:32
An automatically growable array, analogous to a std::vector.
Definition GArray.h:34
void append(Data const &data)
Append an element to the end of the sequence.
Definition GArray.h:303
static bool echo()
Get echo parameter.
static void setEcho(bool echo=true)
Enable or disable echoing for all subclasses of ParamComponent.
DArrayParam< Type > & readDArray(std::istream &in, const char *label, DArray< Type > &array, int n)
Add and read a required DArray < Type > parameter.
ScalarParam< Type > & read(std::istream &in, const char *label, Type &value)
Add and read a new required ScalarParam < Type > object.
void resetParam()
Resets ParamComposite to its empty state.
ScalarParam< Type > & readOptional(std::istream &in, const char *label, Type &value)
Add and read a new optional ScalarParam < Type > object.
#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
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.
ParameterType()
Constructor.