PSCF v1.5.0
Propagator.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 "Propagator_u.h"
9#include "Block_u.h"
10#include <rp/solvers/PropagatorBase.tpp>
11
12namespace Pscf {
13namespace Rp {
14
15 using namespace Util;
16
17 // Public member functions
18
19 /*
20 * Constructor.
21 */
22 template <int D>
26
27 /*
28 * Destructor.
29 */
30 template <int D>
32 {
33 dissociateQFields();
34
35 /*
36 * The above function dissociates elements of qFields_ from memory
37 * owned by qFieldsAll_. Because this destructor body is called
38 * before the destructors for members, disassociation will occur
39 * before either qFieldsAll_ or qFields_ is destroyed.
40 */
41
42 }
43
44 /*
45 * Allocate memory used by this propagator.
46 */
47 template <int D>
49 {
51
52 const int meshSize = mesh.size();
53 IntVec<D> const & meshDimensions = mesh.dimensions();
54
55 // Allocate memory in qFieldsAll_ using value of ns
56 qFieldsAll_.allocate(ns * meshSize);
57
58 // Set up array of associated RField<D,CUT> arrays
59 qFields_.allocate(ns);
60 for (int i = 0; i < ns; ++i) {
61 qFields_[i].associate(qFieldsAll_, i*meshSize, meshDimensions);
62 }
63 isAllocated_ = true;
64
66 }
67
68 /*
69 * Reallocate memory used by this propagator using new ns value.
70 */
71 template <int D>
73 {
75
76 // Deallocate memory previously used by this propagator.
77 dissociateQFields(); // dissociate, nulliify data pointers
78 qFields_.deallocate(); // destroy RField<D,CUT> objects for slices
79 qFieldsAll_.deallocate(); // destroy contiguous data block
80
81 // Store mesh properties
83 const int meshSize = mesh.size();
84 IntVec<D> const & meshDimensions = mesh.dimensions();
85
86 // Allocate memory in qFieldsAll_ using new value of ns
87 qFieldsAll_.allocate(ns * meshSize);
88
89 // Recreate associations between qFields_ and qFieldsAll_
90 qFields_.allocate(ns);
91 for (int i = 0; i < ns; ++i) {
92 qFields_[i].associate(qFieldsAll_, i*meshSize, meshDimensions);
93 }
94
96 }
97
98 // Private member function
99
100 /*
101 * Dissociate qFields_ from associated memory blocks in qFieldsAll_.
102 *
103 * These associations must be destroyed before qFieldsAll_ is
104 * de-allocated or destroyed, because it is an error to deallocate
105 * a BackendArray<T> container that is still referred to by one or
106 * more other such containers. Associations are kept track of via
107 * a private ReferenceCounter owned by qFieldsAll_ (which stores
108 * the number of remaining references), and via a data pointer and
109 * CountedReference object owned by each element of qFields_ that
110 * has a pointer to the associated ReferenceCounter. For each
111 * element of qFields_, invoking the dissociate() member function
112 * nullifies the array data pointer, sets the array capacity to
113 * zero, decrements the number of references in the associated
114 * ReferenceCounter owned by qFieldsAll_, and nullifies the pointer
115 * to this ReferenceCounter.
116 */
117 template <int D>
119 {
120 if (qFields_.isAllocated()) {
121 int ns = qFields_.capacity();
122 for (int i = 0; i < ns; ++i) {
123 if (qFields_[i].isAssociated()) {
124 qFields_[i].dissociate();
125 }
126 }
127 }
128 }
129
130}
131}
132
133// Explicit Instantiation definitions
134namespace Pscf {
135 namespace Rp {
136 template class PropagatorBase<1,CUT>;
137 template class PropagatorBase<2,CUT>;
138 template class PropagatorBase<3,CUT>;
139 template class Propagator<1,CUT>;
140 template class Propagator<2,CUT>;
141 template class Propagator<3,CUT>;
142 }
143}
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
void setIsSolved(bool isSolved)
Set the isSolved flag to true or false.
MDE solver for one direction of one block.
virtual void reallocate(int ns)
Mesh< D > const & mesh() const
virtual void allocate(int ns, const Mesh< D > &mesh)
PropagatorBase< D, CUT > PropagatorBaseT
Direct base class.
bool isAllocated_
Is this propagator allocated?
DArray< RField< D, CUT > > qFields_
Array of propagator slices at different contour variable values.
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.