PSCF v1.5.0
FilmFieldGenExt.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 "FilmFieldGenExt_c.h"
9#include <rp/solvers/Mixture.h>
10#include <rp/field/Domain.h>
11#include <rp/field/FieldIo.h>
12#include <rp/field/CFields.h>
13#include <rp/field/WFields.h>
14#include <rp/field/Mask.h>
15#include <rp/scft/iterator/Iterator.h>
16
17#include <pscf/field/cpp/RField.h>
18#include <pscf/crystal/Basis.h>
19#include <pscf/crystal/paramIdConversions.h>
20#include <pscf/math/IntVec.h>
21
22#include <cmath>
23
24namespace Pscf {
25namespace Rp {
26
27 using namespace Util;
28
29 /*
30 * Default constructor
31 */
32 template <int D>
37
38 /*
39 * Constructor
40 */
41 template <int D>
47
48 /*
49 * Destructor
50 */
51 template <int D>
54
55 /*
56 * Get contribution to the stress from these external fields.
57 */
58 template <int D>
59 double FilmFieldGenExt<D,CPT>::stress(int paramId) const
60 {
61 // Precondition
62 UTIL_CHECK(sysPtr_);
63 UTIL_CHECK(system().mixture().isCanonical());
64
65 // If walls are athermal then there is no external field, so no
66 // contribution to the stress.
67 if (isAthermal()) return 0.0;
68
69 // If paramId is not normalVecId, there is no stress contribution
70 UTIL_CHECK(normalVecId() >= 0); // normalVecId has been set
71 int nvParamId
73 system().domain().lattice());
74 if (nvParamId != paramId) return 0.0;
75
76 // If this point is reached, calculate the stress contribution
77 // from the external fields.
79 UTIL_CHECK(system().mask().hasData());
80 UTIL_CHECK(system().h().hasData());
81
82 // Setup
83 int nMonomer = system().mixture().nMonomer();
84 int nx = system().domain().mesh().size();
85 RField<D,CPT> const & maskRGrid = system().mask().rgrid();
86 RField<D,CPT> maskDeriv, hDeriv;
87 maskDeriv.allocate(system().domain().mesh().dimensions());
88 hDeriv.allocate(system().domain().mesh().dimensions());
89 IntVec<3> coords;
90 int x, y, z;
91 int counter = 0;
92 double d, maskVal;
93 double term = 0.0;
94
95 // Create a 3 element vector 'dim' that contains the grid dimensions.
96 // If system is 2D (1D), then the z (y & z) dimensions are set to 1.
97 IntVec<3> dim;
98 for (int ind = 0; ind < 3; ind++) {
99 if (ind < D) {
100 dim[ind] = system().domain().mesh().dimensions()[ind];
101 } else {
102 dim[ind] = 1;
103 }
104 }
105
106 // Compute the derivative of the mask with respect to film thickness
107 for (x = 0; x < dim[0]; x++) {
108 coords[0] = x;
109 for (y = 0; y < dim[1]; y++) {
110 coords[1] = y;
111 for (z = 0; z < dim[2]; z++) {
112 coords[2] = z;
113 maskVal = maskRGrid[counter];
114
115 // Get the distance 'd' traveled along the lattice basis
116 // vector normal to the walls, in reduced coordinates
117 d = (double)coords[normalVecId()] /
118 (double)dim[normalVecId()];
119
120 maskDeriv[counter] = maskVal * (maskVal - 1) * 8.0
121 * (std::abs(d - 0.5) - 0.5)
123 counter++;
124 }
125 }
126 }
127
128 for (int i = 0; i < nMonomer; i++) {
129 // Compute the h field derivative with respect to film thickness
130 counter = 0;
131 for (x = 0; x < dim[0]; x++) {
132 coords[0] = x;
133 for (y = 0; y < dim[1]; y++) {
134 coords[1] = y;
135 for (z = 0; z < dim[2]; z++) {
136 coords[2] = z;
137 // Bottom half: reduced coordinate d < 1/2
138 if (2*coords[normalVecId()] < dim[normalVecId()]) {
139 hDeriv[counter] = -1.0 * maskDeriv[counter] * chiBottom(i);
140 } else {
141 hDeriv[counter] = -1.0 * maskDeriv[counter] * chiTop(i);
142 }
143 counter++;
144 }
145 }
146 }
147
148 // Get the integral term in the stress
149 RField<D,CPT> const & c = system().c().rgrid(i);
150 for (int i = 0; i < nx; i++) {
151 term += c[i] * hDeriv[i];
152 }
153 }
154 term /= (system().mask().phiTot() * nx);
155 return term;
156 }
157
161 template <int D>
163 {
164 // Set chiBottomCurrent_, chiTopCurrent_, and parametersCurrent_
168
169 // If walls are athermal then there is no external field needed.
170 // If external fields already exist in System, clear them, then return.
171 if (isAthermal()) {
172 system().h().clear();
173 return;
174 }
175
176 // If this point is reached, external field must be computed
177 UTIL_CHECK(normalVecId() >= 0);
178 UTIL_CHECK(system().domain().unitCell().isInitialized());
179 UTIL_CHECK(system().mask().hasData());
180
181 int nm = systemNMonomer();
182
183 // Create a 3 element vector 'dim' that contains the grid dimensions.
184 // If system is 2D (1D), then the z (y & z) dimensions are set to 1.
185 IntVec<3> dim;
186 for (int ind = 0; ind < 3; ind++) {
187 if (ind < D) {
188 dim[ind] = system().domain().mesh().dimension(ind);
189 } else {
190 dim[ind] = 1;
191 }
192 }
193
194 // Get pointer to mask RField
195 RField<D,CPT> const & maskPtr = system().mask().rgrid();
196
197 // Generate an r-grid representation of the external fields
198 DArray< RField<D,CPT> > hRGrid;
199 hRGrid.allocate(nm);
200 for (int i = 0; i < nm; i++) {
201 hRGrid[i].allocate(system().domain().mesh().dimensions());
202 }
203
204 int i, x, y, z;
205 int counter = 0;
206 IntVec<3> coords;
207 double rhoW;
208
209 for (i = 0; i < nm; i++) {
210 for (x = 0; x < dim[0]; x++) {
211 coords[0] = x;
212 for (y = 0; y < dim[1]; y++) {
213 coords[1] = y;
214 for (z = 0; z < dim[2]; z++) {
215 coords[2] = z;
216
217 // Calculate wall volume fraction (rho_w) at gridpoint
218 // (x,y,z)
219 rhoW = maskPtr[counter];
220 // Bottom half: reduced coordinate d < 1/2
221 if (2*coords[normalVecId()] < dim[normalVecId()]) {
222 hRGrid[i][counter++] = (1.0-rhoW) * chiBottom(i);
223 } else {
224 hRGrid[i][counter++] = (1.0-rhoW) * chiTop(i);
225 }
226 }
227 }
228 }
229 counter = 0;
230 }
231
232 // Check if a space group and basis exist
233 bool isSymmetric = system().domain().hasGroup();
234 if (isSymmetric) {
235 UTIL_CHECK(system().domain().basis().isInitialized());
236 }
237
238 // Pass h into the System
239 system().h().setRGrid(hRGrid, isSymmetric);
240 }
241
242 /*
243 * Get space group name for this system.
244 */
245 template <int D>
246 std::string
248 { return system().domain().groupName(); }
249
250 /*
251 * Get one of the lattice vectors for this system.
252 */
253 template <int D>
256 { return system().domain().unitCell().rBasis(id); }
257
258 /*
259 * Get the number of monomer species for this system.
260 */
261 template <int D>
263 { return system().mixture().nMonomer(); }
264
265}
266}
267
268// Explicit instantiation definitions
269namespace Pscf {
270 namespace Rp {
271 template class FilmFieldGenExt<1,CPT>;
272 template class FilmFieldGenExt<2,CPT>;
273 template class FilmFieldGenExt<3,CPT>;
274 }
275}
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
A RealVec<D, T> is D-component vector with elements of floating type T.
Definition RealVec.h:28
int normalVecId() const
Get value of normalVecId.
bool isAthermal() const
Are the walls athermal?
RealVec< D > normalVecCurrent_
The lattice vector normal to the film used to generate these fields.
DArray< double > chiBottomCurrent_
The chiBottom array used to generate the current external fields.
double interfaceThickness() const
Get value of interfaceThickness.
DArray< double > const & chiBottom() const
Get const chiBottom matrix by reference.
DArray< double > chiTopCurrent_
The chiTop array used to generate the current external fields.
DArray< double > const & chiTop() const
Get const chiTop array by reference.
int systemNMonomer() const
Get the number of monomer species for this system.
RealVec< D > systemLatticeVector(int id) const
Get one of the lattice vectors for this system.
System< D, CPT > & system()
Get the parent System by non-const reference.
Base class template for classes that represent a complete system.
Dynamically allocatable contiguous array template.
Definition DArray.h:32
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:262
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
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.