PSCF v1.5.0
FilmFieldGenMask.cu
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_u.h"
9
10#include <rp/system/System.h>
11#include <rp/scft/ScftThermo.h>
12#include <rp/scft/iterator/Iterator.h>
13#include <rp/solvers/Mixture.h>
14#include <rp/field/Domain.h>
15#include <rp/field/CFields.h>
16#include <rp/field/WFields.h>
17#include <rp/field/Mask.h>
18
19#include <pscf/field/RField.h>
20#include <pscf/crystal/Basis.h>
21#include <pscf/crystal/UnitCell.h>
22#include <pscf/crystal/paramIdConversions.h>
23#include <pscf/field/cuda/resources.h>
24
25#include <pscf/interaction/Interaction.h>
26
27#include <cmath>
28
29namespace Pscf {
30namespace Rp {
31
32 using namespace Util;
33
34 // CUDA kernels (anonymous namespace - used only in this file)
35 namespace {
36
37 /*
38 * Compute the derivative of mask field with respect to film thickness.
39 *
40 * (note: indices are inverted from their RField representation. So,
41 * for a 3D system, x, y, and z in an RField become z, y, and x in the
42 * CUDA thread grid, respectively. In 2D, x, y becomes y, x. This
43 * allows for memory coalescing; see ThreadMesh::setConfig
44 * documentation for more information.)
45 *
46 * \param deriv field containing the mask derivative (output)
47 * \param mask field containing the mask
48 * \param interfaceThickness thickness of the polymer/wall interface
49 * \param normalVecId index of the lattice vector normal to the film
50 * \param meshDims grid dimensions of the field in real space
51 */
52 template <int D>
53 __global__ void _maskDerivative(cudaReal * deriv,
54 cudaReal const * mask,
55 cudaReal const interfaceThickness,
56 int const normalVecId,
57 dim3 const meshDims)
58 {
59 // Get position in 3-dimensional grid of threads
60 int ix = blockIdx.x * blockDim.x + threadIdx.x;
61 int iy = blockIdx.y * blockDim.y + threadIdx.y;
62 int iz = blockIdx.z * blockDim.z + threadIdx.z;
63
64 // Get thread index in linearized array
65 int id = ix;
66 if (D > 1) id += meshDims.x * iy;
67 if (D > 2) id += meshDims.x * meshDims.y * iz;
68
69 // Get position d along normalVecId in reduced coordinates
70 cudaReal d;
71 if (D == 3) {
72 if (normalVecId == 2) d = (cudaReal)ix / (cudaReal)meshDims.x;
73 if (normalVecId == 1) d = (cudaReal)iy / (cudaReal)meshDims.y;
74 if (normalVecId == 0) d = (cudaReal)iz / (cudaReal)meshDims.z;
75 } else if (D == 2) {
76 if (normalVecId == 1) d = (cudaReal)ix / (cudaReal)meshDims.x;
77 if (normalVecId == 0) d = (cudaReal)iy / (cudaReal)meshDims.y;
78 } else { // D == 1
79 d = (cudaReal)ix / (cudaReal)meshDims.x;
80 }
81
82 if (ix < meshDims.x && iy < meshDims.y && iz < meshDims.z) {
83 // Get mask value at this gridpoint from global memory
84 cudaReal maskVal = mask[id];
85
86 // Calculate d\phi_m / dL at this gridpoint
87 deriv[id] = maskVal * (maskVal - 1) * 8.0 * (abs(d - 0.5) - 0.5)
88 / interfaceThickness;
89 }
90 }
91
92 /*
93 * Generate the mask.
94 *
95 * \param mask field containing the mask (output)
96 * \param nvLength length of the lattice vector normal to the film
97 * \param excludedThickness thickness of region occupied by the wall
98 * \param interfaceThickness thickness of the polymer/wall interface
99 * \param normalVecId index of the lattice vector normal to the film
100 * \param meshDims grid dimensions of the field in real space
101 */
102 template <int D>
103 __global__ void _generateMask(cudaReal * mask, cudaReal const nvLength,
104 cudaReal const interfaceThickness,
105 cudaReal const excludedThickness,
106 int const normalVecId, dim3 const meshDims)
107 {
108 // Get position in 3-dimensional grid of threads
109 int ix = blockIdx.x * blockDim.x + threadIdx.x;
110 int iy = blockIdx.y * blockDim.y + threadIdx.y;
111 int iz = blockIdx.z * blockDim.z + threadIdx.z;
112
113 // Get thread index in linearized array
114 int id = ix;
115 if (D > 1) id += meshDims.x * iy;
116 if (D > 2) id += meshDims.x * meshDims.y * iz;
117
118 // Get position d along normalVecId in reduced coordinates
119 cudaReal d;
120 if (D == 3) {
121 if (normalVecId == 2) d = (cudaReal)ix / (cudaReal)meshDims.x;
122 if (normalVecId == 1) d = (cudaReal)iy / (cudaReal)meshDims.y;
123 if (normalVecId == 0) d = (cudaReal)iz / (cudaReal)meshDims.z;
124 } else if (D == 2) {
125 if (normalVecId == 1) d = (cudaReal)ix / (cudaReal)meshDims.x;
126 if (normalVecId == 0) d = (cudaReal)iy / (cudaReal)meshDims.y;
127 } else { // D == 1
128 d = (cudaReal)ix / (cudaReal)meshDims.x;
129 }
130
131 // Convert d to unreduced coordinates
132 d *= nvLength;
133
134 if (ix < meshDims.x && iy < meshDims.y && iz < meshDims.z) {
135 // Calculate wall volume fraction at this gridpoint
136 mask[id] =
137 (0.5 * (1.0 - tanh(
138 (
139 0.5 * (excludedThickness - nvLength) +
140 abs(d - (nvLength / 2.0))
141 )
142 * 4.0 / interfaceThickness
143 )));
144 }
145 }
146
147 }
148
149 /*
150 * Default constructor
151 */
152 template <int D>
157
158 /*
159 * Constructor
160 */
161 template <int D>
167
168 /*
169 * Destructor
170 */
171 template <int D>
174
175 /*
176 * Get contribution to the stress from this mask
177 */
178 template <int D>
179 double FilmFieldGenMask<D,CUT>::stress(int paramId) const
180 {
181 // Preconditions
182 UTIL_CHECK(sysPtr_);
183 UTIL_CHECK(system().mixture().isCanonical());
184
185 // If paramId is not normalVecId, there is no stress contribution
186 UTIL_CHECK(normalVecId() >= 0);
187 int normalVecParamId
189 system().domain().lattice());
190 if (normalVecParamId != paramId) return 0.0;
191
192 // If this point is reached, stress contribution must be calculated
193 UTIL_CHECK(system().mask().hasData());
194
195 // Get the volume fraction of the unit cell occupied by polymers
196 double phiTot = system().mask().phiTot();
197
198 // Create the derivative field in rgrid format
199 RField<D,CUT> deriv;
200 deriv.allocate(system().domain().mesh().dimensions());
201
202 // Set D-dimensional GPU configuration
203 ThreadMesh::setConfig(system().domain().mesh().dimensions(), true);
204 dim3 gridDims = ThreadMesh::gridDims();
205 dim3 blockDims = ThreadMesh::blockDims();
206
207 // Compute derivative of the mask with respect to film thickness
208 _maskDerivative<D><<<gridDims, blockDims>>>
209 (deriv.cArray(), system().mask().rgrid().cArray(),
211
212 // Get xi, the Lagrange multiplier field, in rgrid format
213 RField<D,CUT> xi;
214 xi.allocate(system().domain().mesh().dimensions());
215
216 if (system().h().hasData()) {
217 VecOp::subVV(xi, system().w().rgrid(0), system().h().rgrid(0));
218 } else {
219 VecOp::eqV(xi, system().w().rgrid(0));
220 }
221
222 int nMonomer = system().mixture().nMonomer();
223 for (int i = 0; i < nMonomer; i++) {
224 double chi = system().interaction().chi(0, i);
225 if (fabs(chi) > 1e-6) { // if chi is nonzero
226 VecOp::addEqVc(xi, system().c().rgrid(i), -1.0 * chi);
227 }
228 }
229
230 // Integrate deriv * xi to get the integral term in the stress
231 double intTerm = Reduce::innerProduct(deriv, xi);
232 intTerm /= phiTot * deriv.capacity();
233
234 // Get the pressure term in the stress
235 if (!sysPtr_->scft().hasData()) {
236 sysPtr_->scft().compute();
237 }
238 double pSys = sysPtr_->scft().pressure();
239
240 // d(phiTot)/dL is the spatial average of deriv. (The ratio
241 // excludedThickness()/L^2 is exact only for a sharp interface.)
242 double dPhiTot = Reduce::sum(deriv) / double(deriv.capacity());
243 double pTerm = pSys * dPhiTot / phiTot;
244
245 return pTerm - intTerm;
246 }
247
248 template <int D>
249 double
251 int paramId,
252 double stress) const
253 {
254 // Preconditions
255 UTIL_CHECK(sysPtr_);
256 UTIL_CHECK(system().mixture().isCanonical());
257
259 system().domain().lattice());
260
261 // If paramId is not normalVecId, there is no stress modification
262 if (nvParamId != paramId) return stress;
263
264 // If this point is reached, stress must be modified
265 UTIL_CHECK(system().mask().hasData());
266
267 if (!hasFBulk()) {
268 UTIL_THROW("fBulk must be set before calling modifyStress.");
269 }
270
271 // Get system free energy
272 if (!sysPtr_->scft().hasData()) {
273 sysPtr_->scft().compute();
274 }
275 double fSys = sysPtr_->scft().fHelmholtz();
276
277 // Get the length L of the basis vector normal to the walls
278 double L = system().domain().unitCell().parameter(paramId);
279
280 double modifiedStress = (stress * (L - excludedThickness()))
281 + (fSys - fBulk_);
282
283 return modifiedStress;
284 }
285
286 /*
287 * Compute the field and store where the System can access
288 */
289 template <int D>
291 {
292 UTIL_CHECK(normalVecId() >= 0);
295
296 // Get the length of the lattice basis vector normal to the walls
297 int paramId;
299 system().domain().lattice());
300 double nvLength = system().domain().unitCell().parameter(paramId);
301
302 // Setup
303 RField<D,CUT> rGrid;
304 rGrid.allocate(system().domain().mesh().dimensions());
305
306 // Set D-dimensional GPU configuration
307 ThreadMesh::setConfig(system().domain().mesh().dimensions(), true);
308 dim3 gridDims = ThreadMesh::gridDims();
309 dim3 blockDims = ThreadMesh::blockDims();
310
311 // Generate the mask in r-grid format
312 _generateMask<D><<<gridDims, blockDims>>>
313 (rGrid.cArray(), nvLength, interfaceThickness(),
315
316 // Check if a space group is declared and basis is initialized
317 bool isSymmetric = system().domain().hasGroup();
318 if (isSymmetric) {
319 UTIL_CHECK(system().domain().basis().isInitialized());
320 }
321
322 // Store this mask in System
323 system().mask().setRGrid(rGrid, isSymmetric);
324
325 // Store lattice vector normal to film used to construct this mask
327 }
328
329 /*
330 * Sets flexible lattice parameters to be compatible with the mask.
331 */
332 template <int D>
334 {
335 if (system().hasIterator() && system().iterator().isFlexible()) {
336 FSArray<bool,6> updated;
337 updated = modifyFlexibleParams(system().iterator().flexibleParams(),
338 system().domain().unitCell());
339 sysPtr_->iterator().setFlexibleParams(updated);
340 }
341 }
342
343} // namespace Rp
344} // namespace Pscf
345
346// Explicit instantiation definitions
347namespace Pscf {
348 namespace Rp {
349 template class FilmFieldGenMask<1,CUT>;
350 template class FilmFieldGenMask<2,CUT>;
351 template class FilmFieldGenMask<3,CUT>;
352 }
353}
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.
double stress(int paramId) const
Get contribution to the stress from this mask.
RealVec< D > systemLatticeVector(int id) const
Get one of the lattice vectors for this system.
System< D, CUT > & system()
Get the System associated with this object by reference.
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
double abs(fftw_complex const &a)
Return absolute magnitude of an fftw_complex number.
Definition cpp/complex.h:62
int convertFullParamIdToReduced(const int fullId, const typename UnitCell< D >::LatticeSystem lattice)
Convert full lattice parameter index to a reduced index.
void setConfig(IntVec< D > const &meshDims, bool invert, int blockSize)
Given a multidimensional grid of threads, set execution configuration.
Definition ThreadMesh.cu:88
dim3 gridDims()
Get the multidimensional grid of blocks determined by setConfig.
dim3 meshDims()
Return last requested multidimensional grid of threads.
dim3 blockDims()
Get the dimensions of a single block determined by setConfig.
double innerProduct(Array< double > const &a, Array< double > const &b)
Compute Euclidean inner product of two real arrays .
Definition Reduce.cpp:176
double sum(Array< double > const &in)
Compute sum of array elements (real).
Definition Reduce.cpp:19
void eqV(Array< double > &a, Array< double > const &b, const int beginIdA, const int beginIdB, const int n)
Vector assignment, a[i] = b[i] (real, slice).
Definition VecOp.cpp:26
void subVV(Array< double > &a, Array< double > const &b, Array< double > const &c)
Vector-vector subtraction, a[i] = b[i] - c[i] (real).
Definition VecOp.cpp:100
void addEqVc(Array< double > &a, Array< double > const &b, const double c)
Add scaled vector in-place, a[i] += b[i]*c (real).
Definition VecOp.cpp:400
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.
cufftDoubleReal cudaReal
Real number type used in CPU code that uses FFTW.
Definition cudaTypes.h:35