PSCF v1.1
pspc/field/CFieldContainer.tpp
1#ifndef PSPC_C_FIELD_CONTAINER_TPP
2#define PSPC_C_FIELD_CONTAINER_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field Theory
6*
7* Copyright 2016 - 2022, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "CFieldContainer.h"
12
13namespace Pscf {
14namespace Pspc
15{
16
17 using namespace Util;
18
19 /*
20 * Constructor.
21 */
22 template <int D>
24 : basis_(),
25 rgrid_(),
26 nMonomer_(0),
27 isAllocatedRGrid_(false),
28 isAllocatedBasis_(false)
29 {}
30
31 /*
32 * Destructor.
33 */
34 template <int D>
36 {}
37
38 /*
39 * Set the stored value of nMonomer (this may only be called once).
40 */
41 template <int D>
43 {
44 UTIL_CHECK(nMonomer_ == 0);
45 UTIL_CHECK(nMonomer > 0);
46 nMonomer_ = nMonomer;
47 }
48
49 /*
50 * Allocate memory for fields in r-grid format.
51 */
52 template <int D>
53 void
55 {
56 UTIL_CHECK(nMonomer_ > 0);
57
58 // If already allocated, deallocate.
59 if (isAllocatedRGrid_) {
60 deallocateRGrid();
61 }
62
63 // Allocate arrays
64 rgrid_.allocate(nMonomer_);
65 for (int i = 0; i < nMonomer_; ++i) {
66 rgrid_[i].allocate(meshDimensions);
67 }
68 isAllocatedRGrid_ = true;
69 }
70
71 /*
72 * De-allocate memory for fields in r-grid format
73 */
74 template <int D>
76 {
77 UTIL_CHECK(isAllocatedRGrid_);
78 UTIL_CHECK(nMonomer_ > 0);
79 for (int i = 0; i < nMonomer_ ; ++i) {
80 rgrid_[i].deallocate();
81 }
82 rgrid_.deallocate();
83 isAllocatedRGrid_ = false;
84 }
85
86 /*
87 * Allocate memory for fields in basis format.
88 */
89 template <int D>
91 {
92 UTIL_CHECK(nMonomer_ > 0);
93
94 // If already allocated, deallocate.
95 if (isAllocatedBasis_) {
96 deallocateBasis();
97 }
98
99 // Allocate
100 basis_.allocate(nMonomer_);
101 for (int i = 0; i < nMonomer_; ++i) {
102 basis_[i].allocate(nBasis);
103 }
104 isAllocatedBasis_ = true;
105 }
106
107 /*
108 * De-allocate memory for fields in basis format.
109 */
110 template <int D>
112 {
113 UTIL_CHECK(nMonomer_ > 0);
114 UTIL_CHECK(isAllocatedBasis_);
115 for (int i = 0; i < nMonomer_; ++i) {
116 basis_[i].deallocate();
117 }
118 basis_.deallocate();
119 isAllocatedBasis_ = false;
120 }
121
122 /*
123 * Allocate memory for all fields.
124 */
125 template <int D>
126 void CFieldContainer<D>::allocate(int nMonomer, int nBasis,
127 IntVec<D> const & meshDimensions)
128 {
129 setNMonomer(nMonomer);
130 allocateRGrid(meshDimensions);
131 allocateBasis(nBasis);
132 }
133
134} // namespace Pspc
135} // namespace Pscf
136#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition: IntVec.h:27
void deallocateBasis()
De-allocate fields in basis format.
void setNMonomer(int nMonomer)
Set stored value of nMonomer.
void allocate(int nMonomer, int nBasis, IntVec< D > const &dimensions)
Allocate memory for both r-grid and basis field formats.
void allocateRGrid(IntVec< D > const &dimensions)
Allocate or re-allocate memory for fields in rgrid format.
void deallocateRGrid()
De-allocate fields in rgrid format.
void allocateBasis(int nBasis)
Allocate or re-allocate memory for fields in basis format.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
C++ namespace for polymer self-consistent field theory (PSCF).
Utility classes for scientific computation.
Definition: accumulators.mod:1