PSCF v1.5.0
FilmFieldGenExt.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 "FilmFieldGenExt_u.h"
9
10#include <rp/system/System.h>
11#include <rp/scft/iterator/Iterator.h>
12#include <rp/solvers/Mixture.h>
13#include <rp/field/Domain.h>
14#include <rp/field/CFields.cu>
15#include <rp/field/WFields.cu>
16#include <rp/field/Mask.cu>
17
18#include <pscf/field/cpp/RField.h>
19#include <pscf/field/cuda/resources.h>
20
21#include <pscf/crystal/Basis.h>
22#include <pscf/crystal/UnitCell.h>
23#include <pscf/crystal/paramIdConversions.h>
24
25#include <cmath>
26
27namespace Pscf {
28namespace Rp {
29
30 using namespace Util;
31
32 // CUDA kernels:
33 // (defined in anonymous namespace, used only in this file)
34
35 namespace {
36
37 /*
38 * Compute the derivative of h fields 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 hDerivs array of fields containing the h derivatives (output)
47 * \param mask field containing the mask
48 * \param chiBottom array of chiBottom values for each species
49 * \param chiTop array of chiTop values for each species
50 * \param interfaceThickness thickness of the polymer/wall interface
51 * \param normalVecId index of the lattice vector normal to the film
52 * \param nMonomer number of monomer species in the system
53 * \param meshDims grid dimensions of the field in real space
54 */
55 template <int D>
56 __global__ void _hFieldDerivatives(cudaReal ** hDerivs,
57 cudaReal const * mask,
58 cudaReal const * chiBottom,
59 cudaReal const * chiTop,
60 cudaReal const interfaceThickness,
61 int const normalVecId,
62 int const nMonomer,
63 dim3 const meshDims)
64 {
65 // Get position in 3-dimensional grid of threads
66 int ix = blockIdx.x * blockDim.x + threadIdx.x;
67 int iy = blockIdx.y * blockDim.y + threadIdx.y;
68 int iz = blockIdx.z * blockDim.z + threadIdx.z;
69
70 // Get thread index in linearized array
71 int id = ix;
72 if (D > 1) id += meshDims.x * iy;
73 if (D > 2) id += meshDims.x * meshDims.y * iz;
74
75 // Get position d along normalVecId in reduced coordinates
76 cudaReal d;
77 if (D == 3) {
78 if (normalVecId == 2) d = (cudaReal)ix / (cudaReal)meshDims.x;
79 if (normalVecId == 1) d = (cudaReal)iy / (cudaReal)meshDims.y;
80 if (normalVecId == 0) d = (cudaReal)iz / (cudaReal)meshDims.z;
81 } else if (D == 2) {
82 if (normalVecId == 1) d = (cudaReal)ix / (cudaReal)meshDims.x;
83 if (normalVecId == 0) d = (cudaReal)iy / (cudaReal)meshDims.y;
84 } else { // D == 1
85 d = (cudaReal)ix / (cudaReal)meshDims.x;
86 }
87
88 if (ix < meshDims.x && iy < meshDims.y && iz < meshDims.z) {
89 // Get mask value at this gridpoint from global memory
90 cudaReal dPhidL = mask[id];
91
92 // Calculate d\phi_m / dL at this gridpoint
93 dPhidL = dPhidL * (dPhidL - 1) * 8.0 * ( std::abs(d - 0.5) - 0.5 )
94 / interfaceThickness;
95
96 // Calculate dh_i / dL at this gridpoint for each species i
97 for (int in = 0; in < nMonomer; in++) {
98 if (d < 0.5) {
99 hDerivs[in][id] = -1.0 * dPhidL * chiBottom[in];
100 } else {
101 hDerivs[in][id] = -1.0 * dPhidL * chiTop[in];
102 }
103 }
104 }
105 }
106
107 /*
108 * Generate the h fields.
109 *
110 * \param hFields array of h fields (output)
111 * \param mask field containing the mask
112 * \param chiBottom array of chiBottom values for each species
113 * \param chiTop array of chiTop values for each species
114 * \param normalVecId index of the lattice vector normal to the film
115 * \param nMonomer number of monomer species in the system
116 * \param meshDims grid dimensions of the field in real space
117 */
118 template <int D>
119 __global__ void _generateHFields(cudaReal ** hFields,
120 cudaReal const * mask,
121 cudaReal const * chiBottom,
122 cudaReal const * chiTop,
123 int const normalVecId,
124 int const nMonomer,
125 dim3 const meshDims)
126 {
127 // Get position in 3-dimensional grid of threads
128 int ix = blockIdx.x * blockDim.x + threadIdx.x;
129 int iy = blockIdx.y * blockDim.y + threadIdx.y;
130 int iz = blockIdx.z * blockDim.z + threadIdx.z;
131
132 // Get thread index in linearized array
133 int id = ix;
134 if (D > 1) id += meshDims.x * iy;
135 if (D > 2) id += meshDims.x * meshDims.y * iz;
136
137 // Determine if this grid point is in the "top" half, in which
138 // the reduced coordinate along normalVecId is d >= 1/2
139 bool topHalf;
140 if (D == 3) {
141 if (normalVecId == 2) topHalf = (2*ix >= meshDims.x);
142 if (normalVecId == 1) topHalf = (2*iy >= meshDims.y);
143 if (normalVecId == 0) topHalf = (2*iz >= meshDims.z);
144 } else if (D == 2) {
145 if (normalVecId == 1) topHalf = (2*ix >= meshDims.x);
146 if (normalVecId == 0) topHalf = (2*iy >= meshDims.y);
147 } else { // D == 1
148 topHalf = (2*ix >= meshDims.x);
149 }
150
151 if (ix < meshDims.x && iy < meshDims.y && iz < meshDims.z) {
152 // Get mask value at this gridpoint from global memory
153 cudaReal maskVal = mask[id];
154
155 // Calculate h field value at this gridpoint for each species i
156 for (int in = 0; in < nMonomer; in++) {
157 if (topHalf) {
158 hFields[in][id] = (1.0 - maskVal) * chiTop[in];
159 } else {
160 hFields[in][id] = (1.0 - maskVal) * chiBottom[in];
161 }
162 }
163 }
164 }
165
166 }
167
168 /*
169 * Default constructor
170 */
171 template <int D>
176
177 /*
178 * Constructor
179 */
180 template <int D>
186
187 /*
188 * Destructor
189 */
190 template <int D>
193
194 /*
195 * Get contribution to the stress from these external fields
196 */
197 template <int D>
198 double FilmFieldGenExt<D,CUT>::stress(int paramId) const
199 {
200 // Precondition
201 UTIL_CHECK(sysPtr_);
202 UTIL_CHECK(system().mixture().isCanonical());
203
204 // If walls are athermal then there is no external field, so no
205 // contribution to the stress.
206 if (isAthermal()) return 0.0;
207
208 // If paramId is not normalVecId, there is no stress contribution
209 UTIL_CHECK(normalVecId() >= 0); // normalVecId has been set
211 system().domain().lattice());
212 if (nvParamId != paramId) return 0.0;
213
214 // If this point is reached, calculate the stress contribution
215 // from the external fields.
217 UTIL_CHECK(system().mask().hasData());
218 UTIL_CHECK(system().h().hasData());
219
220 // Setup
221 int nMonomer = system().mixture().nMonomer();
222
223 DArray< RField<D,CUT> > hDerivatives;
224 HostSendArray<cudaReal*,CUT> hDerivPtrs_h(nMonomer);
225 BackendArray<cudaReal*,CUT> hDerivPtrs(nMonomer);
226
227 hDerivatives.allocate(nMonomer);
228 for (int i = 0; i < nMonomer; i++) {
229 hDerivatives[i].allocate(system().domain().mesh().dimensions());
230 hDerivPtrs_h[i] = hDerivatives[i].cArray();
231 }
232 hDerivPtrs = hDerivPtrs_h;
233
234 HostSendArray<cudaReal,CUT> chiBottom_h(nMonomer), chiTop_h(nMonomer);
235 BackendArray<cudaReal,CUT> chiBottom_d(nMonomer), chiTop_d(nMonomer);
236 for (int i = 0; i < nMonomer; i++) {
237 chiBottom_h[i] = chiBottom(i);
238 chiTop_h[i] = chiTop(i);
239 }
240 chiBottom_d = chiBottom_h;
241 chiTop_d = chiTop_h;
242
243 // Set D-dimensional GPU configuration
244 ThreadMesh::setConfig(system().domain().mesh().dimensions(), true);
245 dim3 gridDims = ThreadMesh::gridDims();
246 dim3 blockDims = ThreadMesh::blockDims();
247
248 // Compute the derivative of h fields with respect to film thickness
249 _hFieldDerivatives<D><<<gridDims, blockDims>>>
250 (hDerivPtrs.cArray(), system().mask().rgrid().cArray(),
251 chiBottom_d.cArray(), chiTop_d.cArray(), interfaceThickness(),
252 normalVecId(), nMonomer, ThreadMesh::meshDims());
253
254 // Integrate resulting fields to get the stress
255 double stress = 0.0;
256 for (int i = 0; i < nMonomer; i++) {
257 stress += Reduce::innerProduct(hDerivatives[i], system().c().rgrid(i));
258 }
259 stress /= system().mask().phiTot() * hDerivatives[0].capacity();
260 return stress;
261 }
262
266 template <int D>
268 {
269 // Set chiBottomCurrent_, chiTopCurrent_, and parametersCurrent_
273
274 // If walls are athermal then there is no external field needed.
275 // If external fields already exist in System, clear them, then return.
276 if (isAthermal()) {
277 system().h().clear();
278 return;
279 }
280
281 // If this point is reached, external field must be generated
282 UTIL_CHECK(normalVecId() >= 0);
283 UTIL_CHECK(system().domain().unitCell().isInitialized());
284 UTIL_CHECK(system().mask().hasData());
285
286 // Setup
287 int nMonomer = system().mixture().nMonomer();
288
289 DArray< RField<D,CUT> > hFields;
290 HostSendArray<cudaReal*,CUT> hPtrs_h(nMonomer);
291 BackendArray<cudaReal*,CUT> hPtrs(nMonomer);
292
293 hFields.allocate(nMonomer);
294 for (int i = 0; i < nMonomer; i++) {
295 hFields[i].allocate(system().domain().mesh().dimensions());
296 hPtrs_h[i] = hFields[i].cArray();
297 }
298 hPtrs = hPtrs_h;
299
300 HostSendArray<cudaReal,CUT> chiBottom_h(nMonomer), chiTop_h(nMonomer);
301 BackendArray<cudaReal,CUT> chiBottom_d(nMonomer), chiTop_d(nMonomer);
302 for (int i = 0; i < nMonomer; i++) {
303 chiBottom_h[i] = chiBottom(i);
304 chiTop_h[i] = chiTop(i);
305 }
306 chiBottom_d = chiBottom_h;
307 chiTop_d = chiTop_h;
308
309 // Set D-dimensional GPU configuration
310 ThreadMesh::setConfig(system().domain().mesh().dimensions(), true);
311 dim3 gridDims = ThreadMesh::gridDims();
312 dim3 blockDims = ThreadMesh::blockDims();
313
314 // Generate the h fields
315 _generateHFields<D><<<gridDims, blockDims>>>
316 (hPtrs.cArray(), system().mask().rgrid().cArray(),
317 chiBottom_d.cArray(), chiTop_d.cArray(), normalVecId(),
318 nMonomer, ThreadMesh::meshDims());
319
320 // Check if a space group and basis exist
321 bool isSymmetric = system().domain().hasGroup();
322 if (isSymmetric) {
323 UTIL_CHECK(system().domain().basis().isInitialized());
324 }
325
326 // Pass h into the System
327 system().h().setRGrid(hFields, isSymmetric);
328 }
329
330}
331}
332
333// Explicit instantiation definitions
334namespace Pscf {
335 namespace Rp {
336 template class FilmFieldGenExt<1,CUT>;
337 template class FilmFieldGenExt<2,CUT>;
338 template class FilmFieldGenExt<3,CUT>;
339 }
340}
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.
double stress(int paramId) const
Get contribution to the stress from the external fields.
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.
Data * cArray()
Return a pointer to the underlying C array (non-const data).
Definition Array.h:222
int capacity() const
Return allocated size.
Definition Array.h:250
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.
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
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