PSCF v1.5.0
fieldCheck.tpp
1#ifndef PSCF_FIELD_CHECK_TPP
2#define PSCF_FIELD_CHECK_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <pscf/math/IntVec.h>
12#include <util/containers/DArray.h>
13#include <util/global.h>
14
15namespace Pscf {
16
17 // Arrays of fields
18
19 /*
20 * Allocate an array of n fields of type FT.
21 */
22 template <int D, class FT>
24 int n,
25 IntVec<D> const& dimension)
26 {
27 UTIL_CHECK(!fields.isAllocated());
28 fields.allocate(n);
29 for (int i = 0; i < n; ++i) {
30 fields[i].allocate(dimension);
31 }
32 }
33
34 /*
35 * Check allocation of a single field of type FT, allocate if needed.
36 */
37 template <int D, class FT>
38 void checkAllocateField(FT& field,
39 IntVec<D> const& dimensions)
40 {
41 if (field.isAllocated()) {
42 UTIL_CHECK(field.meshDimensions() == dimensions);
43 } else {
44 field.allocate(dimensions);
45 }
46 }
47
48 /*
49 * Check an array of n fields of type FT, allocate if needed.
50 */
51 template <int D, class FT>
53 int nMonomer,
54 IntVec<D> const& dimensions)
55 {
56 if (fields.isAllocated()) {
57 int nMonomerFields = fields.capacity();
58 UTIL_CHECK(nMonomerFields > 0)
59 UTIL_CHECK(nMonomerFields == nMonomer)
60 for (int i = 0; i < nMonomer; ++i) {
61 UTIL_CHECK(fields[i].isAllocated());
62 UTIL_CHECK(fields[i].meshDimensions() == dimensions);
63 }
64 } else {
65 fields.allocate(nMonomer);
66 for (int i = 0; i < nMonomer; ++i) {
67 fields[i].allocate(dimensions);
68 }
69 }
70 }
71
72 /*
73 * Inspect dimensions of a DArray of n fields of type FT.
74 */
75 template <int D, class FT>
76 void inspectFields(DArray<FT> const& fields,
77 int & nMonomer,
78 IntVec<D> & dimensions)
79 {
80 UTIL_CHECK(fields.isAllocated());
81 nMonomer = fields.capacity();
82 UTIL_CHECK(nMonomer > 0);
83
84 dimensions = fields[0].meshDimensions();
85 for (int i = 0; i < nMonomer; ++i) {
86 UTIL_CHECK(fields[i].isAllocated());
87 UTIL_CHECK(fields[i].meshDimensions() == dimensions);
88 }
89 }
90
91 // Array of arrays
92
93 template <class AT>
94 void allocateArrays(DArray<AT>& arrays, int n, int capacity)
95 {
96 UTIL_CHECK(!arrays.isAllocated());
97 arrays.allocate(n);
98 for (int i = 0; i < n; ++i) {
99 arrays[i].allocate(capacity);
100 }
101 }
102
103 /*
104 * Check allocation of an array of arrays of type AT, allocate if needed.
105 */
106 template <class AT>
107 void checkAllocateArrays(DArray<AT>& arrays,
108 int nMonomer,
109 int capacity)
110 {
111 if (arrays.isAllocated()) {
112 int nMonomerArrays = arrays.capacity();
113 UTIL_CHECK(nMonomerArrays > 0)
114 UTIL_CHECK(nMonomerArrays == nMonomer)
115 for (int i = 0; i < nMonomer; ++i) {
116 UTIL_CHECK(arrays[i].isAllocated());
117 UTIL_CHECK(arrays[i].capacity() == capacity);
118 }
119 } else {
120 arrays.allocate(nMonomer);
121 for (int i = 0; i < nMonomer; ++i) {
122 arrays[i].allocate(capacity);
123 }
124 }
125 }
126
127 /*
128 * Inspect dimensions of an array of arrays of type AT.
129 */
130 template <class AT>
131 void inspectArrays(DArray<AT> const& arrays,
132 int & nMonomer,
133 int & capacity)
134 {
135 UTIL_CHECK(arrays.isAllocated());
136 nMonomer = arrays.capacity();
137 UTIL_CHECK(nMonomer > 0);
138
139 capacity = arrays[0].capacity();
140 UTIL_CHECK(capacity > 0);
141 for (int i = 0; i < nMonomer; ++i) {
142 UTIL_CHECK(arrays[i].isAllocated());
143 UTIL_CHECK(arrays[i].capacity() == capacity);
144 }
145 }
146
147 template <class OAT, class IAT>
148 void copyArrays(DArray<OAT>& out, DArray<IAT> const& in)
149 {
151 int n = in.capacity();
152 if (!out.isAllocated()) {
153 out.allocate(n);
154 }
155 UTIL_CHECK(out.capacity() == n);
156 for (int i = 0; i < n; ++i) {
157 out[i] = in[i];
158 }
159 }
160
161 template <class HAT, class DAT>
163 {
164 UTIL_CHECK(device.isAllocated());
165 int n = device.capacity();
166 if (!host.isAllocated()) {
167 host.allocate(n);
168 }
169 UTIL_CHECK(host.capacity() == n);
170 for (int i = 0; i < n; ++i) {
171 UTIL_CHECK(device[i].isAllocated());
172 host[i].associate(device[i]);
173 }
174 }
175
176 template <class HAT>
178 {
179 UTIL_CHECK(host.isAllocated());
180 int n = host.capacity();
181 for (int i = 0; i < n; ++i) {
182 host[i].dissociate();
183 }
184 }
185
186} // namespace Pscf
187#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
int capacity() const
Return allocated size.
Definition Array.h:250
bool isAllocated() const
Does this array have associated data?
Definition Array.h:236
Dynamically allocatable contiguous array template.
Definition DArray.h:32
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:262
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
void checkAllocateArrays(DArray< AT > &arrays, int nMonomer, int capacity)
Check allocation of a DArray of 1D arrays, allocate if necessary.
void inspectArrays(DArray< AT > const &arrays, int &nMonomer, int &capacity)
Inspect dimensions of a DArray of 1D arrays, each of type AT.
void checkAllocateField(FT &field, IntVec< D > const &dimensions)
Check allocation of a single field, allocate if necessary.
void checkAllocateFields(DArray< FT > &fields, int nMonomer, IntVec< D > const &dimensions)
Check allocation of an array of fields, allocate if necessary.
void copyArrays(DArray< OAT > &out, DArray< IAT > const &in)
Copy a DArray of 1D arrays.
void associateArrays(DArray< HAT > &host, DArray< DAT > &device)
Associate host arrays with backend arrays.
void dissociateArrays(DArray< HAT > &host)
Dissociate an array of host arrays.
void inspectFields(DArray< FT > const &fields, int &nMonomer, IntVec< D > &dimensions)
Inspect dimensions of a DArray of fields, each of type FT.
PSCF package top-level namespace.
void allocateArrays(DArray< AT > &arrays, int n, int capacity)
Allocate an array of arrays.
void allocateFields(DArray< FT > &fields, int n, IntVec< D > const &dimension)
Allocate a DArray of fields.