PSCF v1.5.0
FilmFieldGenMask.cpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "FilmFieldGenMask_c.h"
9
10#include <rp/solvers/Mixture.h>
11#include <rp/scft/iterator/Iterator.h>
12#include <rp/scft/ScftThermo.h>
13#include <rp/field/Domain.h>
14#include <rp/field/FieldIo.h>
15#include <rp/field/WFields.h>
16#include <rp/field/CFields.h>
17#include <rp/field/Mask.h>
18
19#include <pscf/field/cpp/RField.h>
20#include <pscf/crystal/UnitCell.h>
21#include <pscf/crystal/Basis.h>
22#include <pscf/crystal/paramIdConversions.h>
23
24#include <pscf/interaction/Interaction.h>
25#include <pscf/math/IntVec.h>
26
27#include <cmath>
28
29namespace Pscf {
30namespace Rp {
31
32 using namespace Util;
33
34 /*
35 * Default constructor
36 */
37 template <int D>
42
43 /*
44 * Constructor
45 */
46 template <int D>
52
53 /*
54 * Destructor
55 */
56 template <int D>
59
60 /*
61 * Compute contribution to the stress from this mask
62 */
63 template <int D>
64 double FilmFieldGenMask<D,CPT>::stress(int paramId) const
65 {
66 // Preconditions
67 UTIL_CHECK(sysPtr_);
68 UTIL_CHECK(system().mixture().isCanonical());
69
70 Rp::Domain<D,CPT> const & domain = system().domain();
71 Mesh<D> const & mesh = domain.mesh();
72
73 int normalVecParamId = convertFullParamIdToReduced<D>(normalVecId(),
74 domain.lattice());
75
76 // If paramId is not normalVecId, there is no stress contribution
77 if (normalVecParamId != paramId) return 0.0;
78
79 // If this point is reached, stress contribution must be calculated
80 UTIL_CHECK(system().mask().hasData());
81
82 // Get the volume fraction of the unit cell occupied by polymers
83 double phiTot = system().mask().phiTot();
84
85 // Create a 3 element vector 'dim' that contains the grid dimensions.
86 // If system is 2D (1D), then the z (y & z) dimensions are set to 1.
87 IntVec<3> dim;
88 for (int ind = 0; ind < 3; ind++) {
89 if (ind < D) {
90 dim[ind] = mesh.dimensions()[ind];
91 } else {
92 dim[ind] = 1;
93 }
94 }
95
96 // Create the derivative field in rgrid format
97 RField<D,CPT> deriv;
98 IntVec<3> coords;
99 int x, y, z;
100 int counter = 0;
101 double d, maskVal;
102
103 deriv.allocate(mesh.dimensions());
104 RField<D,CPT> const & maskRGrid = system().mask().rgrid();
105
106 for (x = 0; x < dim[0]; x++) {
107 coords[0] = x;
108 for (y = 0; y < dim[1]; y++) {
109 coords[1] = y;
110 for (z = 0; z < dim[2]; z++) {
111 coords[2] = z;
112 maskVal = maskRGrid[counter];
113
114 // Get the distance 'd' traveled along the lattice basis
115 // vector normal to the walls, in reduced coordinates
116 d = (double)coords[normalVecId()] /
117 (double)dim[normalVecId()];
118
119 deriv[counter] = maskVal * (maskVal - 1) * 8.0
120 * (std::abs(d - 0.5) - 0.5)
122 counter++;
123 }
124 }
125 }
126
127 // Get xi, the Lagrange multiplier field, in rgrid format
128 int nMonomer = system().mixture().nMonomer();
129 int nx = mesh.size();
130 double chi;
131 RField<D,CPT> xi;
132 xi.allocate(mesh.dimensions());
133
134 for (int i = 0; i < nx; i++) {
135 xi[i] = system().w().rgrid(0)[i];
136 }
137
138 for (int in = 0; in < nMonomer; in++) {
139 chi = system().interaction().chi(0,in);
140 if (fabs(chi) > 1e-6) { // if chi is nonzero
141 RField<D,CPT> const & cfield = system().c().rgrid(in);
142 for (int i = 0; i < nx; i++) {
143 //xi[i] -= system().c().rgrid(in)[i] * chi;
144 xi[i] -= cfield[i] * chi;
145 }
146 }
147 }
148
149 if (system().h().hasData()) {
150 RField<D,CPT> const & hfield = system().h().rgrid(0);
151 for (int i = 0; i < nx; i++) {
152 //xi[i] -= system().h().rgrid(0)[i];
153 xi[i] -= hfield[i];
154 }
155 }
156
157 // Get the integral term in the stress
158 double intTerm = 0.0;
159 for (int i = 0; i < nx; i++) {
160 intTerm += xi[i] * deriv[i];
161 }
162 intTerm /= (phiTot * nx);
163
164 // Get the pressure term in the stress
165 if (!system().scft().hasData()) {
166 sysPtr_->scft().compute();
167 }
168 double pSys = system().scft().pressure();
169
170 // d(phiTot)/dL is the spatial average of deriv. (The ratio
171 // excludedThickness()/L^2 is exact only for a sharp interface.)
172 double dPhiTot = 0.0;
173 for (int i = 0; i < nx; i++) {
174 dPhiTot += deriv[i];
175 }
176 dPhiTot /= double(nx);
177 double pTerm = pSys * dPhiTot / phiTot;
178
179 return pTerm - intTerm;
180 }
181
182 template <int D>
184 int paramId,
185 double stress) const
186 {
187 // Preconditions
188 UTIL_CHECK(sysPtr_);
189 UTIL_CHECK(system().mixture().isCanonical());
190
192 system().domain().lattice());
193 if (nvParamId == paramId) {
194 UTIL_CHECK(system().mask().hasData());
195
196 if (!hasFBulk()) {
197 UTIL_THROW("fBulk must be set before calculating this stress.");
198 }
199
200 // Get system free energy
201 if (!system().scft().hasData()) {
202 sysPtr_->scft().compute();
203 }
204 double fSys = system().scft().fHelmholtz();
205
206 // Get the length L of the basis vector normal to the walls
207 double L = system().domain().unitCell().parameter(paramId);
208
209 double modifiedStress = (stress * (L - excludedThickness()))
210 + (fSys - fBulk_);
211
212 return modifiedStress;
213 } else {
214 return stress;
215 }
216 }
217
218 /*
219 * Compute the field and store where the System can access
220 */
221 template <int D>
223 {
224 UTIL_CHECK(sysPtr_);
227 UTIL_CHECK(normalVecId() >= 0);
228
229 // Get the length L of the lattice basis vector normal to the walls
230 int paramId
232 system().domain().lattice());
233 double L = system().domain().unitCell().parameter(paramId);
234
235 // Create a 3 element vector 'dim' that contains the grid dimensions.
236 // If system is 2D (1D), then the z (y & z) dimensions are set to 1.
237 IntVec<3> dim;
238 for (int ind = 0; ind < 3; ind++) {
239 if (ind < D) {
240 dim[ind] = system().domain().mesh().dimensions()[ind];
241 } else {
242 dim[ind] = 1;
243 }
244 }
245
246 // Generate an r-grid representation of the walls
247 RField<D,CPT> rGrid;
248 rGrid.allocate(system().domain().mesh().dimensions());
249 int x, y, z;
250 int counter = 0;
251 IntVec<3> coords;
252 double d, rhoW;
253
254 for (x = 0; x < dim[0]; x++) {
255 coords[0] = x;
256 for (y = 0; y < dim[1]; y++) {
257 coords[1] = y;
258 for (z = 0; z < dim[2]; z++) {
259 coords[2] = z;
260
261 // Get the distance 'd' traveled along the lattice basis
262 // vector that is normal to the walls
263 d = coords[normalVecId()] * L / dim[normalVecId()];
264
265 // Calculate wall volume fraction (rhoW) at gridpoint (x,y,z)
266 rhoW = 0.5 * (1 + tanh(4 * (((.5 * (excludedThickness()-L)) +
267 fabs(d - (L/2))) / interfaceThickness())));
268 rGrid[counter++] = 1-rhoW;
269 }
270 }
271 }
272
273 // Check if a space group and basis exist
274 bool isSymmetric = system().domain().hasGroup();
275 if (isSymmetric) {
276 UTIL_CHECK(system().domain().basis().isInitialized());
277 }
278
279 // Store this mask in System
280 system().mask().setRGrid(rGrid, isSymmetric);
281
282 // Store lattice vector normal to film used to construct this mask
284
285 }
286
287 /*
288 * Sets flexible lattice parameters to be compatible with the mask.
289 */
290 template <int D>
292 {
293 UTIL_CHECK(sysPtr_);
294 if (system().hasIterator() && system().iterator().isFlexible()) {
295 FSArray<bool,6> updated;
296 updated = modifyFlexibleParams(system().iterator().flexibleParams(),
297 system().domain().unitCell());
298 sysPtr_->iterator().setFlexibleParams(updated);
299 }
300 }
301
302} // namespace Rp
303} // namespace Pscf
304
305// Explicit instantiation definitions
306namespace Pscf {
307 namespace Rp {
308 template class FilmFieldGenMask<1,CPT>;
309 template class FilmFieldGenMask<2,CPT>;
310 template class FilmFieldGenMask<3,CPT>;
311 }
312}
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
Description of a regular grid of points in a periodic domain.
Definition Mesh.h:61
IntVec< D > dimensions() const
Get an IntVec<D> of the grid dimensions.
Definition Mesh.h:217
int size() const
Get total number of grid points.
Definition Mesh.h:229
Spatial domain for a periodic structure with real fields.
Mesh< D > & mesh()
Get the Mesh by non-const reference.
UnitCell< D >::LatticeSystem lattice() const
Get the lattice type (enumeration value).
double excludedThickness() const
Get value of excludedThickness.
double fBulk_
Reference free energy used to calculate stress normal to the film.
int normalVecId() const
Get value of normalVecId.
double interfaceThickness() const
Get value of interfaceThickness.
bool hasFBulk() const
Check whether a value of fBulk was provided.
FSArray< bool, 6 > modifyFlexibleParams(FSArray< bool, 6 > current, UnitCell< D > const &cell) const
Modifies a flexibleParams array to be compatible with this mask.
RealVec< D > normalVecCurrent_
The lattice vector normal to the film used to generate these fields.
RealVec< D > systemLatticeVector(int id) const
Get one of the lattice vectors for this system.
Rp::System< D, CPT > & system()
Get the parent System by non-const reference.
double stress(int paramId) const
Get contribution to the stress from this mask.
Base class template for classes that represent a complete system.
A fixed capacity (static) contiguous array with a variable logical size.
Definition FSArray.h:38
void setClassName(const char *className)
Set class name string.
#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
int convertFullParamIdToReduced(const int fullId, const typename UnitCell< D >::LatticeSystem lattice)
Convert full lattice parameter index to a reduced index.
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.