PSCF v1.4.0
fieldCheck.tpp
1#ifndef PRDC_FIELD_CHECK_TPP
2#define PRDC_FIELD_CHECK_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2025, The Regents of the University of Minnesota
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 {
16namespace Prdc {
17
18 // Arrays of fields
19
20 /*
21 * Allocate an array of n fields of type FT.
22 */
23 template <int D, class FT>
25 int n,
26 IntVec<D> const& dimension)
27 {
28 UTIL_CHECK(!fields.isAllocated());
29 fields.allocate(n);
30 for (int i = 0; i < n; ++i) {
31 fields[i].allocate(dimension);
32 }
33 }
34
35 /*
36 * Check allocation of a single field of type FT, allocate if needed.
37 */
38 template <int D, class FT>
39 void checkAllocateField(FT& field,
40 IntVec<D> const& dimensions)
41 {
42 if (field.isAllocated()) {
43 UTIL_CHECK(field.meshDimensions() == dimensions);
44 } else {
45 field.allocate(dimensions);
46 }
47 }
48
49 /*
50 * Check an array of n fields of type FT, allocate if needed.
51 */
52 template <int D, class FT>
54 int nMonomer,
55 IntVec<D> const& dimensions)
56 {
57 if (fields.isAllocated()) {
58 int nMonomerFields = fields.capacity();
59 UTIL_CHECK(nMonomerFields > 0)
60 UTIL_CHECK(nMonomerFields == nMonomer)
61 for (int i = 0; i < nMonomer; ++i) {
62 UTIL_CHECK(fields[i].isAllocated());
63 UTIL_CHECK(fields[i].meshDimensions() == dimensions);
64 }
65 } else {
66 fields.allocate(nMonomer);
67 for (int i = 0; i < nMonomer; ++i) {
68 fields[i].allocate(dimensions);
69 }
70 }
71 }
72
73 /*
74 * Inspect dimensions of a DArray of n fields of type FT.
75 */
76 template <int D, class FT>
77 void inspectFields(DArray<FT> const& fields,
78 int & nMonomer,
79 IntVec<D> & dimensions)
80 {
81 UTIL_CHECK(fields.isAllocated());
82 nMonomer = fields.capacity();
83 UTIL_CHECK(nMonomer > 0);
84
85 dimensions = fields[0].meshDimensions();
86 for (int i = 0; i < nMonomer; ++i) {
87 UTIL_CHECK(fields[i].isAllocated());
88 UTIL_CHECK(fields[i].meshDimensions() == dimensions);
89 }
90 }
91
92 // Array of arrays
93
94 template <class AT>
95 void allocateArrays(DArray<AT>& arrays, int n, int capacity)
96 {
97 UTIL_CHECK(!arrays.isAllocated());
98 arrays.allocate(n);
99 for (int i = 0; i < n; ++i) {
100 arrays[i].allocate(capacity);
101 }
102 }
103
104 /*
105 * Check allocation of an array of arrays of type AT, allocate if needed.
106 */
107 template <class AT>
108 void checkAllocateArrays(DArray<AT>& arrays,
109 int nMonomer,
110 int capacity)
111 {
112 if (arrays.isAllocated()) {
113 int nMonomerArrays = arrays.capacity();
114 UTIL_CHECK(nMonomerArrays > 0)
115 UTIL_CHECK(nMonomerArrays == nMonomer)
116 for (int i = 0; i < nMonomer; ++i) {
117 UTIL_CHECK(arrays[i].isAllocated());
118 UTIL_CHECK(arrays[i].capacity() == capacity);
119 }
120 } else {
121 arrays.allocate(nMonomer);
122 for (int i = 0; i < nMonomer; ++i) {
123 arrays[i].allocate(capacity);
124 }
125 }
126 }
127
128 /*
129 * Inspect dimensions of an array of arrays of type AT.
130 */
131 template <class AT>
132 void inspectArrays(DArray<AT> const& arrays,
133 int & nMonomer,
134 int & capacity)
135 {
136 UTIL_CHECK(arrays.isAllocated());
137 nMonomer = arrays.capacity();
138 UTIL_CHECK(nMonomer > 0);
139
140 capacity = arrays[0].capacity();
141 UTIL_CHECK(capacity > 0);
142 for (int i = 0; i < nMonomer; ++i) {
143 UTIL_CHECK(arrays[i].isAllocated());
144 UTIL_CHECK(arrays[i].capacity() == capacity);
145 }
146 }
147
148 template <class OAT, class IAT>
149 void copyArrays(DArray<OAT>& out, DArray<IAT> const& in)
150 {
151 int n = in.capacity();
152 UTIL_CHECK(out.capacity() == n);
153 for (int i = 0; i < n; ++i) {
154 UTIL_CHECK(in[i].isAllocated());
155 UTIL_CHECK(out[i].isAllocated());
156 UTIL_CHECK(in[i].capacity() == out[i].capacity());
157 out[i] = in[i];
158 }
159 }
160
161} // namespace Prdc
162} // namespace Pscf
163#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:144
Dynamically allocatable contiguous array template.
Definition DArray.h:32
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:269
bool isAllocated() const
Return true if this DArray has been allocated, false otherwise.
Definition DArray.h:151
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 copyArrays(DArray< OAT > &out, DArray< IAT > const &in)
Copy a DArray of 1D arrays.
void inspectArrays(DArray< AT > const &arrays, int &nMonomer, int &capacity)
Inspect dimensions of a DArray of 1D arrays, each of type AT.
void inspectFields(DArray< FT > const &fields, int &nMonomer, IntVec< D > &dimensions)
Inspect dimensions of a DArray of fields, each of type FT.
void checkAllocateArrays(DArray< AT > &arrays, int nMonomer, int capacity)
Check allocation of a DArray of 1D arrays, 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 checkAllocateField(FT &field, IntVec< D > const &dimensions)
Check allocation of a single field, allocate if necessary.
Periodic fields and crystallography.
Definition complex.cpp:11
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.
PSCF package top-level namespace.