PSCF v1.5.0
cpp/RFieldDft.tpp
1#ifndef PSCF_R_FIELD_DFT_CP_TPP
2#define PSCF_R_FIELD_DFT_CP_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 "RFieldDft.h"
12#include <pscf/field/cpp/FFT.h>
13
14namespace Pscf {
15
16 using namespace Util;
17
18 /*
19 * Default constructor.
20 */
21 template <int D>
23 : BackendArray<fftw_complex,CPT>(),
24 meshDimensions_(0),
25 dftDimensions_(0)
26 {}
27
28 /*
29 * Copy constructor.
30 *
31 * Allocates new memory and copies all elements by value.
32 */
33 template <int D>
35 : BackendArray<fftw_complex,CPT>(),
36 meshDimensions_(0),
37 dftDimensions_(0)
38 {
39 if (!other.isAllocated()) {
40 UTIL_THROW("Other Field must be allocated.");
41 }
42 allocate(other.meshDimensions_);
43 for (int i = 0; i < capacity_; ++i) {
44 data_[i][0] = other.data_[i][0];
45 data_[i][1] = other.data_[i][1];
46 }
47 }
48
49 /*
50 * Destructor.
51 */
52 template <int D>
55
56 /*
57 * Assignment, element-by-element.
58 *
59 * This operator will allocate memory if not allocated previously.
60 *
61 * \throw Exception if other Field is not allocated.
62 * \throw Exception if both Fields are allocated with unequal capacities.
63 *
64 * \param other the rhs Field
65 */
66 template <int D>
69 {
70 // Check for self assignment
71 if (this == &other) return *this;
72
73 // Precondition
74 if (!other.isAllocated()) {
75 UTIL_THROW("Other RFieldDft must be allocated.");
76 }
77
78 // Allocate if necessary, check dimensions
79 if (!isAllocated()) {
80 allocate(other.meshDimensions_);
81 }
82 UTIL_CHECK(capacity_ == other.capacity_);
83 UTIL_CHECK(meshDimensions_ == other.meshDimensions_);
84 UTIL_CHECK(dftDimensions_ == other.dftDimensions_);
85
86 // Copy elements
87 for (int i = 0; i < capacity_; ++i) {
88 data_[i][0] = other.data_[i][0];
89 data_[i][1] = other.data_[i][1];
90 }
91
92 return *this;
93 }
94
95 /*
96 * Pseudo-assignment from an associated HostSendArray.
97 */
98 template <int D>
105
106 /*
107 * Allocate the underlying array for an FFT grid.
108 */
109 template <int D>
111 {
112 // Copy real space grid dimensions
113 for (int i = 0; i < D; ++i) {
115 meshDimensions_[i] = meshDimensions[i];
116 }
117
118 // Compute dimensions and size for k-space grid
119 int size;
120 FFT<D,CPT>::computeKMesh(meshDimensions, dftDimensions_, size);
121
122 // Allocate memory
124 }
125
126 /*
127 * Deallocate the underlying C array and clear dimensions.
128 */
129 template <int D>
131 {
133 for (int i = 0; i < D; ++i) {
134 meshDimensions_[i] = 0;
135 dftDimensions_[i] = 0;
136 }
137 }
138
139}
140#endif
Backend identifier class for the C++ serial CPU backend.
Definition CPT.h:32
virtual void deallocate()
Dellocate an underlying C array that is owned by this container.
void allocate(int capacity)
Allocate an underlying C array, which this container then owns.
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
RFieldDft()
Default constructor.
IntVec< D > const & meshDimensions() const
Return vector of spatial mesh dimensions by const reference.
void allocate(IntVec< D > const &meshDimensions)
Allocate the underlying C array and set mesh dimensions.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
PSCF package top-level namespace.