PSCF v1.5.0
Block.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 "Block_u.h"
9#include "Propagator_u.h"
10
11#include <pscf/field/cuda/WaveList.h>
12#include <pscf/field/cuda/FFT.h>
13#include <pscf/crystal/UnitCell.h>
14
15#include <pscf/backend/cuda/Reduce.h>
16#include <pscf/backend/cuda/VecOp.h>
17#include <pscf/backend/cuda/ThreadArray.h>
18#include <pscf/backend/cuda/cudaTypes.h>
19#include <pscf/mesh/Mesh.h>
20#include <pscf/mesh/MeshIterator.h>
21
22#include <pscf/solvers/BlockTmpl.tpp>
23
24namespace Pscf {
25namespace Rp {
26
27 using namespace Util;
28
29 // CUDA kernels (anonymous namespace - only used in this file)
30 namespace {
31
32 /*
33 * Element-wise calculation of a = real(b * conj(c) * d), CUDA kernel
34 */
35 __global__ void _realMulVConjVV(cudaReal* a,
36 cudaComplex const * b,
37 cudaComplex const * c,
38 cudaReal const * d, const int n)
39 {
40 int nThreads = blockDim.x * gridDim.x;
41 int startID = blockIdx.x * blockDim.x + threadIdx.x;
42 cudaComplex bt, ct;
43 for (int i = startID; i < n; i += nThreads) {
44 // Load complex numbers from global memory to local
45 // (this way the accessed memory is contiguous, rather than
46 // accessing the x or y elements individually, which are not
47 // contiguous)
48 bt = b[i];
49 ct = c[i];
50
51 // Perform calculation
52 a[i] = ((bt.x * ct.x) + (bt.y * ct.y)) * d[i];
53 }
54 }
55
56 /*
57 * Performs qNew = (4*(qr2 * expW2)-qr)/3 elementwise, CUDA kernel
58 */
59 __global__ void _richardsonEx(cudaReal* qNew,
60 cudaReal const * qr,
61 cudaReal const * qr2,
62 cudaReal const * expW2, const int n)
63 {
64 int nThreads = blockDim.x * gridDim.x;
65 int startID = blockIdx.x * blockDim.x + threadIdx.x;
66 cudaReal q2;
67 for (int i = startID; i < n; i += nThreads) {
68 q2 = qr2[i] * expW2[i];
69 qNew[i] = (4.0 * q2 - qr[i]) / 3.0;
70 }
71 }
72
73 /*
74 * Performs a[i] += b[i] * c[i] * d. CUDA kernel
75 */
76 __global__ void _addEqMulVVc(cudaReal* a,
77 cudaReal const * b,
78 cudaReal const * c,
79 const cudaReal d,
80 const int n)
81 {
82 int nThreads = blockDim.x * gridDim.x;
83 int startID = blockIdx.x * blockDim.x + threadIdx.x;
84 for (int i = startID; i < n; i += nThreads) {
85 a[i] += b[i] * c[i] * d;
86 }
87 }
88
89 /*
90 * Element-wise calculation of a[i] += b[i]*c[i]* d[i], CUDA kernel
91 */
92 __global__ void _addEqMulVVV(cudaReal* a,
93 cudaReal const * b,
94 cudaReal const * c,
95 cudaReal const * d,
96 const int n)
97 {
98 int nThreads = blockDim.x * gridDim.x;
99 int startID = blockIdx.x * blockDim.x + threadIdx.x;
100 for (int i = startID; i < n; i += nThreads) {
101 a[i] += b[i]*c[i]*d[i];
102 }
103 }
104
105 }
106
107 // CUDA kernel wrappers:
108
109 /*
110 * Element-wise calculation of a = real(b * conj(c) * d), kernel wrapper
111 *
112 * \param a output array (real)
113 * \param b input array 1 (complex)
114 * \param c input array 2 (complex)
115 * \param d input array 3 (real)
116 */
117 void realMulVConjVV(BackendArray<cudaReal,CUT>& a,
118 BackendArray<cudaComplex,CUT> const & b,
119 BackendArray<cudaComplex,CUT> const & c,
120 BackendArray<cudaReal,CUT> const & d)
121 {
122 int n = a.capacity();
123 UTIL_CHECK(b.capacity() >= n);
124 UTIL_CHECK(c.capacity() >= n);
125 UTIL_CHECK(d.capacity() >= n);
126
127 // GPU resources
128 int nBlocks, nThreads;
129 ThreadArray::setThreadsLogical(n, nBlocks, nThreads);
130
131 // Launch kernel
132 _realMulVConjVV<<<nBlocks, nThreads>>>(a.cArray(), b.cArray(),
133 c.cArray(), d.cArray(), n);
134 }
135
136 /*
137 * Performs qNew = (4 * (qr2 * expW2) - qr) / 3 elementwise, kernel wrapper
138 *
139 * \param qNew output array (a propagator slice)
140 * \param qr input array 1 (a propagator slice)
141 * \param qr2 input array 2 (a propagator slice)
142 * \param expW2 input array 3 (exp(-W[i]*ds/4) array)
143 */
144 void richardsonEx(BackendArray<cudaReal,CUT>& qNew,
145 BackendArray<cudaReal,CUT> const & qr,
146 BackendArray<cudaReal,CUT> const & qr2,
147 BackendArray<cudaReal,CUT> const & expW2)
148 {
149 int n = qNew.capacity();
150 UTIL_CHECK(qr.capacity() == n);
151 UTIL_CHECK(qr2.capacity() == n);
152 UTIL_CHECK(expW2.capacity() == n);
153
154 // GPU resources
155 int nBlocks, nThreads;
156 ThreadArray::setThreadsLogical(n, nBlocks, nThreads);
157
158 // Launch kernel
159 _richardsonEx<<<nBlocks, nThreads>>>(qNew.cArray(), qr.cArray(),
160 qr2.cArray(), expW2.cArray(),
161 n);
162 }
163
164 /*
165 * Performs a[i] += b[i] * c[i] * d, kernel wrapper
166 *
167 * \param a output array
168 * \param b input array 1
169 * \param c input array 2
170 * \param d input scalar
171 */
172 void addEqMulVVc(BackendArray<cudaReal,CUT>& a,
173 BackendArray<cudaReal,CUT> const & b,
174 BackendArray<cudaReal,CUT> const & c,
175 cudaReal const d)
176 {
177 int n = a.capacity();
178 UTIL_CHECK(b.capacity() >= n);
179 UTIL_CHECK(c.capacity() >= n);
180
181 // GPU resources
182 int nBlocks, nThreads;
183 ThreadArray::setThreadsLogical(n, nBlocks, nThreads);
184
185 // Launch kernel
186 _addEqMulVVc<<<nBlocks, nThreads>>>(a.cArray(), b.cArray(),
187 c.cArray(), d, n);
188 }
189
190 /*
191 * Element-wise calculation of a[i] = b[i]*c[i]*d[i], kernel wrapper
192 */
193 void addEqMulVVV(BackendArray<cudaReal,CUT>& a,
194 BackendArray<cudaReal,CUT> const & b,
195 BackendArray<cudaReal,CUT> const & c,
196 BackendArray<cudaReal,CUT> const & d)
197 {
198 int n = a.capacity();
199 UTIL_CHECK(b.capacity() >= n);
200 UTIL_CHECK(c.capacity() >= n);
201 UTIL_CHECK(d.capacity() >= n);
202
203 // GPU resources
204 int nBlocks, nThreads;
205 ThreadArray::setThreadsLogical(n, nBlocks, nThreads);
206
207 // Launch kernel
208 _addEqMulVVV<<<nBlocks, nThreads>>>(a.cArray(), b.cArray(),
209 c.cArray(), d.cArray(), n);
210 }
211
212 // Block class template member functions:
213
214 /*
215 * Constructor.
216 */
217 template <int D>
219 : meshPtr_(nullptr),
220 fftPtr_(nullptr),
221 unitCellPtr_(nullptr),
222 waveListPtr_(nullptr),
223 kMeshDimensions_(0),
224 kSize_(0),
225 ds_(0.0),
226 dsTarget_(0.0),
227 ns_(0),
228 nParams_(0),
229 isAllocated_(false),
230 hasExpKsq_(false),
231 useBatchedFFT_(true)
232 {
233 propagator(0).setBlock(*this);
234 propagator(1).setBlock(*this);
235 }
236
237 /*
238 * Destructor.
239 */
240 template <int D>
243
244 template <int D>
246 FFT<D,CUT> const & fft,
247 UnitCell<D> const & cell,
248 WaveList<D,CUT>& waveList)
249 {
250 UTIL_CHECK(!isAllocated_);
251 UTIL_CHECK(mesh.size() > 1);
252 UTIL_CHECK(fft.isSetup());
253 UTIL_CHECK(mesh.dimensions() == fft.meshDimensions());
254
255 nParams_ = cell.nParameter();
256 UTIL_CHECK(nParams_ > 0);
257
258 // Store pointers to associated objects
259 meshPtr_ = &mesh;
260 fftPtr_ = &fft;
261 unitCellPtr_ = &cell;
262 waveListPtr_ = &waveList;
263
264 hasExpKsq_ = false;
265 }
266
267 template <int D>
268 void Block<D,CUT>::allocate(double ds, bool useBatchedFFT)
269 {
270 UTIL_CHECK(meshPtr_);
271 UTIL_CHECK(unitCellPtr_);
272 UTIL_CHECK(ds > 0.0);
273 UTIL_CHECK(!isAllocated_);
274
275 // Store useBatchedFFT
276 useBatchedFFT_ = useBatchedFFT;
277
278 // Compute k-space grid dimensions (kMeshDimensions_) and size_
279 FFT<D,CUT>::computeKMesh(mesh().dimensions(), kMeshDimensions_, kSize_);
280
281 // Allocate work arrays
282 expW_.allocate(mesh().dimensions());
283 expKsq_.allocate(kMeshDimensions_);
284 expKsq2_.allocate(kMeshDimensions_);
286 expW2_.allocate(mesh().dimensions());
287 qrPair_.allocate(2 * mesh().size());
288 qkPair_.allocate(2 * kSize_);
289 } else
290 if (PolymerModel::isBead()) {
291 expWInv_.allocate(mesh().dimensions());
292 qk_.allocate(mesh().dimensions());
293 }
294
295 // Allocate space for block monomer concentration
296 cField().allocate(mesh().dimensions());
297
298 // Compute ns_
299 dsTarget_ = ds;
301
302 // Set contour length discretization for this block
303 const double length = Edge::length();
304 UTIL_CHECK(length > 0.0);
305 int tempNs;
306 tempNs = floor(length / (2.0 * ds) + 0.5);
307 if (tempNs == 0) {
308 tempNs = 1; // ensure ns_ >= 3
309 }
310 ns_ = 2*tempNs + 1;
311 ds_ = length/double(ns_ - 1);
312
313 } else
314 if (PolymerModel::isBead()) {
315
316 ds_ = 1.0;
317 ns_ = Edge::nBead() + 2;
318
319 }
320
321 // Allocate memory for solutions of MDE (requires ns_)
322 propagator(0).allocate(ns_, mesh());
323 propagator(1).allocate(ns_, mesh());
324
325 // Setup fftBatchedPair_
326 UTIL_CHECK(!fftBatchedPair_.isSetup());
327 fftBatchedPair_.setup(mesh().dimensions(), 2);
328
329 // Setup batched data used for stress calculation
330 if (useBatchedFFT_) {
331 UTIL_CHECK(!fftBatchedAll_.isSetup());
332 fftBatchedAll_.setup(mesh().dimensions(), ns_);
333 q0kBatched_.allocate(ns_ * kSize_);
334 q1kBatched_.allocate(ns_ * kSize_);
335 }
336
337 isAllocated_ = true;
338 hasExpKsq_ = false;
339 }
340
341 /*
342 * Set or reset the the block length.
343 */
344 template <int D>
345 void Block<D,CUT>::setLength(double newLength)
346 {
347 // Precondition
349
350 Edge::setLength(newLength);
351 const double length = Edge::length();
352
353 if (isAllocated_) { // if allocate() has already been called
354 // Reset contour length discretization
355 UTIL_CHECK(dsTarget_ > 0);
356 int oldNs = ns_;
357 int tempNs;
358 tempNs = floor(length / (2.0 * dsTarget_) + 0.5);
359 if (tempNs == 0) {
360 tempNs = 1; // ensure at least 3 contour steps per chain
361 }
362 ns_ = 2*tempNs + 1;
363 ds_ = length/double(ns_-1);
364
365 if (oldNs != ns_) {
366 // If propagators are already allocated and ns_ has changed,
367 // reallocate memory for solutions to MDE
368 propagator(0).reallocate(ns_);
369 propagator(1).reallocate(ns_);
370
371 // If using batched FFTs, resize arrays and change batch size
372 if (useBatchedFFT_) {
373 UTIL_CHECK(fftBatchedAll_.isSetup());
374 q0kBatched_.deallocate();
375 q1kBatched_.deallocate();
376 q0kBatched_.allocate(ns_ * kSize_);
377 q1kBatched_.allocate(ns_ * kSize_);
378 fftBatchedAll_.resetBatchSize(ns_);
379 }
380 }
381 }
382
383 hasExpKsq_ = false;
384 }
385
386 /*
387 * Set or reset the number of beads in this block.
388 */
389 template <int D>
391 {
393
394 if (isAllocated_ && ns_ != nBead + 2) {
395 // Reallocate memory for solutions to MDE
396 ns_ = nBead + 2;
397 propagator(0).reallocate(ns_);
398 propagator(1).reallocate(ns_);
399
400 // If using batched FFTs, resize arrays and change batch size
401 if (useBatchedFFT_) {
402 UTIL_CHECK(fftBatchedAll_.isSetup());
403 q0kBatched_.deallocate();
404 q1kBatched_.deallocate();
405 q0kBatched_.allocate(ns_ * kSize_);
406 q1kBatched_.allocate(ns_ * kSize_);
407 fftBatchedAll_.resetBatchSize(ns_);
408 }
409 }
410 }
411
412 /*
413 * Set or reset monomer statistical segment length.
414 */
415 template <int D>
417 {
419 hasExpKsq_ = false;
420 }
421
422 /*
423 * Clear all internal data that depends on lattice parameters.
424 */
425 template <int D>
427 {
428 UTIL_CHECK(unitCellPtr_);
429 UTIL_CHECK(nParams_ == unitCell().nParameter());
430 hasExpKsq_ = false;
431 stress_.clear();
432 }
433
434 /*
435 * Compute all elements of expKsq_ and expKsq2_ arrays
436 */
437 template <int D>
439 {
440 UTIL_CHECK(isAllocated_);
441 UTIL_CHECK(waveListPtr_);
442
443 // Calculate kSq if necessary
444 if (!waveList().hasKSq()) {
445 waveList().computeKSq();
446 }
447
448 // Compute bSqFactor
449 const double b = BlockTmplT::kuhn();
450 double bSqFactor = -1.0 * b * b / 6.0;
452 bSqFactor *= ds_;
453 }
454
455 // Calculate expKsq values on device
456 VecOp::expVc(expKsq_, waveList().kSq(), bSqFactor);
457 VecOp::expVc(expKsq2_, waveList().kSq(), bSqFactor / 2.0);
458
459 hasExpKsq_ = true;
460 }
461
462 /*
463 * Setup the contour length step algorithm.
464 */
465 template <int D>
466 void
468 {
469 // Preconditions
470 int nx = mesh().size();
471 UTIL_CHECK(nx > 0);
472 UTIL_CHECK(isAllocated_);
473
474 // Populate expW_
476 VecOp::expVc(expW_, w, -0.5 * ds_);
477 VecOp::expVc(expW2_, w, -0.25 * ds_);
478 } else {
479 VecOp::expVc(expW_, w, -1.0);
480 VecOp::divSV(expWInv_, 1.0, expW_);
481 }
482
483 // Compute expKsq arrays if necessary
484 if (!hasExpKsq_) {
485 computeExpKsq();
486 }
487
488 }
489
490 /*
491 * Propagate solution by one step.
492 */
493 template <int D>
495 {
496 // Preconditions
497 UTIL_CHECK(isAllocated_);
498 UTIL_CHECK(hasExpKsq_);
499 UTIL_CHECK(meshPtr_);
500 int nx = mesh().size();
501 UTIL_CHECK(nx > 0);
502 UTIL_CHECK(fft().isSetup());
503 UTIL_CHECK(fft().meshDimensions() == mesh().dimensions());
504 UTIL_CHECK(qrPair_.capacity() == nx * 2);
505 UTIL_CHECK(qkPair_.capacity() == kSize_ * 2);
506 UTIL_CHECK(expW_.capacity() == nx);
507 UTIL_CHECK(expKsq_.capacity() == kSize_);
508 UTIL_CHECK(fftBatchedPair_.isSetup());
509
510 // Set up associated workspace fields slices
511 RField<D,CUT> qr, qr2;
512 RFieldDft<D,CUT> qk, qk2;
513 qr.associate(qrPair_, 0, mesh().dimensions());
514 qr2.associate(qrPair_, nx, mesh().dimensions());
515 qk.associate(qkPair_, 0, mesh().dimensions());
516 qk2.associate(qkPair_, kSize_, mesh().dimensions());
517
518 // Apply pseudo-spectral algorithm
519 // qr = expW*q, qr2 = expW2*q
520 VecOp::mulVVPair(qr, qr2, expW_, expW2_, qin);
521 fftBatchedPair_.forwardTransform(qrPair_, qkPair_);
522 VecOp::mulEqV(qk, expKsq_);
523 VecOp::mulEqV(qk2, expKsq2_);
524 fftBatchedPair_.inverseTransformUnsafe(qkPair_, qrPair_);
525 VecOp::mulEqVPair(qr, qr2, expW_); // qr *= expW, qr2 *= expW
526 fft().forwardTransform(qr2, qk2);
527 VecOp::mulEqV(qk2, expKsq2_); // qk2 *= expKsq2
528 fft().inverseTransformUnsafe(qk2, qr2);
529 richardsonEx(qout, qr, qr2, expW2_); // qout=(4*(qr2*expW2)-qr)/3
530
531 // Explcitly dissociate workspace field slices
532 qr.dissociate();
533 qr2.dissociate();
534 qk.dissociate();
535 qk2.dissociate();
536 }
537
538 /*
539 * Apply one step of the MDE solution for the bead model.
540 */
541 template <int D>
543 {
544 stepBondBead(qin, qout);
545 stepFieldBead(qout);
546 }
547
548 /*
549 * Apply the field operator for the bead model.
550 */
551 template <int D>
553 {
554 // Preconditions
555 int nx = mesh().size();
556 UTIL_CHECK(expW_.capacity() == nx);
557 UTIL_CHECK(q.capacity() == nx);
558
559 VecOp::mulEqV(q, expW_); // q *= expW
560 }
561
562 /*
563 * Apply the bond operator for the bead model.
564 */
565 template <int D>
567 {
568 // Preconditions
569 UTIL_CHECK(isAllocated_);
570 UTIL_CHECK(hasExpKsq_);
571 UTIL_CHECK(meshPtr_);
572 int nx = mesh().size();
573 UTIL_CHECK(fft().isSetup());
574 UTIL_CHECK(fft().meshDimensions() == mesh().dimensions());
575 UTIL_CHECK(nx > 0);
576 UTIL_CHECK(qin.capacity() == nx);
577 UTIL_CHECK(qout.capacity() == nx);
578 UTIL_CHECK(qk_.isAllocated());
579 UTIL_CHECK(qk_.capacity() == kSize_);
580 UTIL_CHECK(expKsq_.capacity() == kSize_);
581
582 // Apply full bond operator
583 fft().forwardTransform(qin, qk_);
584 VecOp::mulEqV(qk_, expKsq_); // qk *= expKsq
585 fft().inverseTransformUnsafe(qk_, qout); // overwrites qk_
586 }
587
588 /*
589 * Apply the half-bond operator for the bead model.
590 */
591 template <int D>
593 {
594 // Preconditions
595 UTIL_CHECK(isAllocated_);
596 UTIL_CHECK(hasExpKsq_);
597 int nx = mesh().size();
598 UTIL_CHECK(nx > 0);
599 UTIL_CHECK(qin.capacity() == nx);
600 UTIL_CHECK(qout.capacity() == nx);
601 UTIL_CHECK(qk_.isAllocated());
602 UTIL_CHECK(qk_.capacity() == kSize_);
603 UTIL_CHECK(expKsq_.capacity() == kSize_);
604
605 // Apply half-bond operator
606 fft().forwardTransform(qin, qk_);
607 VecOp::mulEqV(qk_, expKsq2_); // qk *= expKsq2
608 fft().inverseTransformUnsafe(qk_, qout); // overwrites qk_
609 }
610
611 /*
612 * Integrate to calculate monomer concentration for this block
613 */
614 template <int D>
616 {
617 // Preconditions
618 int nx = mesh().size();
619 UTIL_CHECK(nx > 0);
620 UTIL_CHECK(ns_ > 0);
621 UTIL_CHECK(propagator(0).isSolved());
622 UTIL_CHECK(propagator(1).isSolved());
623 UTIL_CHECK(cField().capacity() == nx);
624
625 // Initialize cField to zero at all points
626 VecOp::eqS(cField(), 0.0);
627
628 // References to forward and reverse propagators
629 Propagator<D,CUT> const & p0 = propagator(0);
630 Propagator<D,CUT> const & p1 = propagator(1);
631
632 addEqMulVVc(cField(), p0.q(0), p1.q(ns_ - 1), 1.0);
633 addEqMulVVc(cField(), p0.q(ns_ - 1), p1.q(0), 1.0);
634
635 for (int j = 1; j < ns_ - 1; j += 2) {
636 // Odd indices
637 addEqMulVVc(cField(), p0.q(j), p1.q(ns_ - 1 - j), 4.0);
638 }
639 for (int j = 2; j < ns_ - 2; j += 2) {
640 // Even indices
641 addEqMulVVc(cField(), p0.q(j), p1.q(ns_ - 1 - j), 2.0);
642 }
643
644 VecOp::mulEqS(cField(), (prefactor * ds_/3.0));
645 }
646
647 /*
648 * Integrate to calculate monomer concentration for this block
649 */
650 template <int D>
652 {
653 // Preconditions
654 int nx = mesh().size();
655 UTIL_CHECK(nx > 0);
656 UTIL_CHECK(ns_ > 0);
657 UTIL_CHECK(propagator(0).isSolved());
658 UTIL_CHECK(propagator(1).isSolved());
659 UTIL_CHECK(cField().capacity() == nx);
660
661 // Initialize cField to zero at all points
662 VecOp::eqS(cField(), 0.0);
663
664 // References to forward and reverse propagators
665 Propagator<D,CUT> const & p0 = propagator(0);
666 Propagator<D,CUT> const & p1 = propagator(1);
667
668 // Internal beads (j = 1, ..., nbead, with nbead = ns_ -2)
669 for (int j = 1; j < ns_ - 1; ++j) {
670 addEqMulVVV(cField(), p0.q(j), p1.q(ns_ - 1 - j), expWInv_);
671 }
672
673 // Scale cField() by prefactor
674 VecOp::mulEqS(cField(), prefactor);
675 }
676
677 /*
678 * Compute stress contribution from this block.
679 */
680 template <int D>
682 {
684 UTIL_CHECK(meshPtr_);
685 int nx = mesh().size();
686
687 // Preconditions
688 UTIL_CHECK(isAllocated_);
689 UTIL_CHECK(nx > 0);
690 UTIL_CHECK(kSize_ > 0);
691 UTIL_CHECK(ns_ > 0);
692 UTIL_CHECK(ds_ > 0);
693 UTIL_CHECK(nParams_ > 0);
694 UTIL_CHECK(mesh().dimensions() == fft().meshDimensions());
695
696 // References to forward and reverse propagators
699 UTIL_CHECK(p0.isSolved());
700 UTIL_CHECK(p1.isSolved());
701
702 // Calculate dKSq if necessary
703 if (!waveList().hasdKSq()) {
704 waveList().computedKSq();
705 }
706
707 // Workspace variables
708 double dels, normal, increment;
709 normal = 3.0*6.0;
711 int i, j, n;
712 RField<D,CUT> rTmp(kMeshDimensions_); // array of real values on kgrid
713
714 // Initialize dQ and stress to 0
715 stress_.clear();
716 for (i = 0; i < nParams_; ++i) {
717 dQ.append(0.0);
718 stress_.append(0.0);
719 }
720
721 if (useBatchedFFT_) {
722
723 UTIL_CHECK(fftBatchedAll_.isSetup());
724 UTIL_CHECK(mesh().dimensions() == fftBatchedAll_.meshDimensions());
725 UTIL_CHECK(!q0k_.isAllocated());
726 UTIL_CHECK(!q1k_.isAllocated());
727 // In this case, containers q0k_ and q1k_ will be associated
728 // with slices of q0kBatched_, q1kBatched_
729
730 // Computed batched FFT for propagators in both directions
731 fftBatchedAll_.forwardTransform(p0.qAll(), q0kBatched_);
732 fftBatchedAll_.forwardTransform(p1.qAll(), q1kBatched_);
733
734 } else {
735
736 if (!q0k_.isAllocated()) {
737 q0k_.allocate(mesh().dimensions());
738 q1k_.allocate(mesh().dimensions());
739 }
740
741 }
742
743 // Main loop over contour points
744 for (j = 0; j < ns_ ; ++j) {
745
746 if (useBatchedFFT_) {
747 // Batched FFTs have already been computed
748 // Associate q0k_, q1k_ with slices of q0kBatched_, q1kBatched_
749 q0k_.associate(q0kBatched_, j * kSize_, mesh().dimensions());
750 q1k_.associate(q1kBatched_, (ns_-1-j) * kSize_,
751 mesh().dimensions());
752 } else {
753 // Compute Fourier transforms at contour grid point j
754 UTIL_CHECK(fft().isSetup());
755 fft().forwardTransform(p0.q(j), q0k_);
756 fft().forwardTransform(p1.q(ns_-1-j), q1k_);
757 }
758
759 // Compute prefactor for Simpson's rule
760 dels = ds_;
761 if (j != 0 && j != ns_ - 1) {
762 if (j % 2 == 0) {
763 dels = dels*2.0;
764 } else {
765 dels = dels*4.0;
766 }
767 }
768
769 // Increment stress contributions for all unit cell parameters
770 const double b = BlockTmplT::kuhn();
771 for (n = 0; n < nParams_ ; ++n) {
772
773 // Launch kernel to evaluate dQ at all wavevectors
774 realMulVConjVV(rTmp, q0k_, q1k_, waveList().dKSq(n));
775
776 // Get the sum of all elements
777 increment = Reduce::sum(rTmp);
778 increment *= b * b * dels / normal;
779 dQ[n] -= increment;
780 }
781
782 if (useBatchedFFT_) {
783 q0k_.dissociate();
784 q1k_.dissociate();
785 }
786
787 } // end loop over contour points
788
789 // Normalize total stress values
790 for (i = 0; i < nParams_; ++i) {
791 stress_[i] -= (dQ[i] * prefactor);
792 }
793
794 }
795
796 /*
797 * Compute stress contribution from this block, in bead model.
798 */
799 template <int D>
800 void Block<D,CUT>::computeStressBead(double prefactor)
801 {
802 // Preconditions
804 UTIL_CHECK(isAllocated_);
805 int nx = mesh().size();
806 IntVec<D> const & meshDim = mesh().dimensions();
807 UTIL_CHECK(nx > 0);
808 UTIL_CHECK(kSize_ > 0);
809 UTIL_CHECK(nParams_ > 0);
810 UTIL_CHECK(meshDim == fft().meshDimensions());
811 UTIL_CHECK(ns_ > 0);
812
813 // References to forward and reverse propagators
816 UTIL_CHECK(p0.isSolved());
817 UTIL_CHECK(p1.isSolved());
818
819 // Compute batched FFTs, or check allocation of work space
820 if (useBatchedFFT_) {
821 UTIL_CHECK(fftBatchedAll_.isSetup());
822 UTIL_CHECK(meshDim == fftBatchedAll_.meshDimensions());
823 UTIL_CHECK(!q0k_.isAllocated());
824 UTIL_CHECK(!q1k_.isAllocated());
825 // In this case, containers q0k_ and q1k_ will be associated
826 // with slices of q0kBatched_, q1kBatched_
827
828 // Computed batched FFT for propagators in both directions
829 fftBatchedAll_.forwardTransform(p0.qAll(), q0kBatched_);
830 fftBatchedAll_.forwardTransform(p1.qAll(), q1kBatched_);
831 } else {
832 if (!q0k_.isAllocated()) {
833 q0k_.allocate(meshDim);
834 q1k_.allocate(meshDim);
835 }
836 }
837
838 // Calculate dKSq if necessary
839 if (!waveList().hasdKSq()) {
840 waveList().computedKSq();
841 }
842
843 // Initialize dQ to zero
845 dQ.clear();
846 for (int i = 0; i < nParams_; ++i) {
847 dQ.append(0.0);
848 }
849
850 RField<D,CUT> rTmp(kMeshDimensions_); // k-space work space
851 const double b = BlockTmplT::kuhn();
852 const double bSq = b * b / 6.0;
853 double increment;
854
855 // Half bond from head junction to first bead, if not a chain end
856 if (!p0.isHeadEnd()) {
857 if (useBatchedFFT_) {
858 // Batched FFTs have already been computed
859 // Associate q0k_, q1k_ with slices of q0kBatched_, q1kBatched_
860 q0k_.associate(q0kBatched_, 0, meshDim);
861 q1k_.associate(q1kBatched_, (ns_- 2) * kSize_, meshDim);
862 } else {
863 // Compute Fourier transforms at contour grid point j
864 UTIL_CHECK(fft().isSetup());
865 fft().forwardTransform(p0.q(0), q0k_);
866 fft().forwardTransform(p1.q(ns_ - 2), q1k_);
867 }
868 for (int n = 0; n < nParams_ ; ++n) {
869 realMulVConjVV(rTmp, q0k_, q1k_, waveList().dKSq(n));
870 VecOp::mulEqV(rTmp, expKsq2_);
871 increment = Reduce::sum(rTmp);
872 increment *= 0.5*bSq;
873 dQ[n] -= increment;
874 }
875 if (useBatchedFFT_) {
876 q0k_.dissociate();
877 q1k_.dissociate();
878 }
879 }
880
881 // Loop over all nBead - 1 full bonds within this block
882 for (int j = 1; j < ns_ - 2; ++j) {
883
884 // Retrieve or compute FFTs at two neighboring slices
885 if (useBatchedFFT_) {
886 // Batched FFTs have already been computed
887 // Associate q0k_, q1k_ with slices of q0kBatched_, q1kBatched_
888 q0k_.associate(q0kBatched_, j * kSize_, meshDim);
889 q1k_.associate(q1kBatched_, (ns_- 2 -j) * kSize_, meshDim);
890 } else {
891 // Compute Fourier transforms of p0, p1 at neighboring beads
892 UTIL_CHECK(fft().isSetup());
893 fft().forwardTransform(p0.q(j), q0k_);
894 fft().forwardTransform(p1.q(ns_ - 2 - j), q1k_);
895 }
896
897 // Loop over unit cell parameters
898 for (int n = 0; n < nParams_ ; ++n) {
899
900 // Compute contributions to dQ at all wavevectors, in rTmp
901 realMulVConjVV(rTmp, q0k_, q1k_, waveList().dKSq(n));
902 VecOp::mulEqV(rTmp, expKsq_);
903
904 // Evaluate sum of over wavevectors
905 increment = Reduce::sum(rTmp);
906 increment *= bSq;
907 dQ[n] -= increment;
908
909 }
910
911 if (useBatchedFFT_) {
912 // Dissociate to allow re-association
913 q0k_.dissociate();
914 q1k_.dissociate();
915 }
916
917 } // end loop over contour points
918
919 // Half bond from last bead to tail, if not a chain end
920 if (!p0.isTailEnd()) {
921 if (useBatchedFFT_) {
922 q0k_.associate(q0kBatched_, (ns_-2)*kSize_, meshDim);
923 q1k_.associate(q1kBatched_, 0, meshDim);
924 } else {
925 UTIL_CHECK(fft().isSetup());
926 fft().forwardTransform(p0.q(ns_-2), q0k_);
927 fft().forwardTransform(p1.q(0), q1k_);
928 }
929 for (int n = 0; n < nParams_ ; ++n) {
930 realMulVConjVV(rTmp, q0k_, q1k_, waveList().dKSq(n));
931 VecOp::mulEqV(rTmp, expKsq2_);
932 increment = Reduce::sum(rTmp);
933 increment *= 0.5*bSq;
934 dQ[n] -= increment;
935 }
936 if (useBatchedFFT_) {
937 q0k_.dissociate();
938 q1k_.dissociate();
939 }
940 }
941
942 // Normalize total stress values
943 stress_.clear();
944 for (int i = 0; i < nParams_; ++i) {
945 stress_.append(-1.0*prefactor*dQ[i]);
946 }
947
948 }
949
950} // namespace Rp
951} // namespace Pscf
952
953namespace Pscf {
954
955 template class
956 BlockTmpl< Rp::Propagator<1,CUT>, RField<1,CUT> >;
957 template class
958 BlockTmpl< Rp::Propagator<2,CUT>, RField<2,CUT> >;
959 template class
960 BlockTmpl< Rp::Propagator<3,CUT>, RField<3,CUT> >;
961
962 namespace Rp {
963 template class Block<1,CUT>;
964 template class Block<2,CUT>;
965 template class Block<3,CUT>;
966 }
967
968}
virtual void setNBead(int nBead)
Set the number of beads in this block (only valid for bead model).
Definition Edge.cpp:55
int nBead() const
Get the number of beads in this block, in the bead model.
Definition Edge.h:305
virtual void setLength(double length)
Set the length of this block (only valid for thread model).
Definition Edge.cpp:65
double length() const
Get the length of this block, in the thread model.
Definition Edge.h:314
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
void stepBondBead(RField< D, CUT > const &qin, RField< D, CUT > &qout) const
Compute a bond operator for the bead model.
Definition Block.cu:566
Block()
Constructor.
Definition Block.cu:218
Rp::Propagator< D, CUT > & propagator(int directionId)
Get a Propagator for a specified direction (non-const reference).
double ds() const
Contour length step size.
Definition Block_u.h:458
void stepFieldBead(RField< D, CUT > &q) const
Apply the exponential field operator for the bead model.
Definition Block.cu:552
RField< D, CUT > const & cField() const
Get the associated monomer concentration field (const reference).
int nParameter() const
Get the number of parameters in the unit cell.
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:55
A fixed capacity (static) contiguous array with a variable logical size.
Definition FSArray.h:38
void clear()
Set logical size to zero.
Definition FSArray.h:274
void append(Data const &data)
Append data to the end of the array.
Definition FSArray.h:261
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
void setThreadsLogical(int nThreadsLogical)
Given total number of threads, set 1D execution configuration.
int nThreads()
Get the number of threads per block for execution.
int nBlocks()
Get the current number of blocks for execution.
double sum(Array< double > const &in)
Compute sum of array elements (real).
Definition Reduce.cpp:19
void mulVVPair(Array< double > &a1, Array< double > &a2, Array< double > const &b1, Array< double > const &b2, Array< double > const &c)
Vector multiplication in pairs, ax[i] = bx[i] * s[i], x=1,2.
Definition VecOp.cpp:468
void mulEqV(Array< double > &a, Array< double > const &b)
Vector-vector in-place multiplication, a[i] *= b[i] (real).
Definition VecOp.cpp:257
void mulEqS(Array< double > &a, double b)
Vector-scalar in-place multiplication, a[i] *= b (real).
Definition VecOp.cpp:270
void expVc(Array< double > &a, Array< double > const &b, const double c)
Exponentiation a scaled vector, a[i] = exp(b[i]*c) (real).
Definition VecOp.cpp:324
void eqS(Array< double > &a, double b)
Vector assignment, a[i] = b (real).
Definition VecOp.cpp:55
void mulEqVPair(Array< double > &a1, Array< double > &a2, Array< double > const &b)
In-place vector multiplication in pairs, ax[i] *= b[i], x=1,2.
Definition VecOp.cpp:489
void divSV(Array< double > &a, double b, Array< double > const &c)
Vector division, a[i] = b / c[i].
Definition VecOp.cpp:188
bool isThread()
Is the thread model in use ?
bool isBead()
Is the bead model in use ?
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.
cufftDoubleComplex cudaComplex
Complex number type used in CPU code that uses FFTW.
Definition cudaTypes.h:22
cufftDoubleReal cudaReal
Real number type used in CPU code that uses FFTW.
Definition cudaTypes.h:35