PSCF v1.1
RDFieldDft.h
1#ifndef PSPG_R_DFIELD_DFT_H
2#define PSPG_R_DFIELD_DFT_H
3
4/*
5* PSCF Package
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 "DField.h"
12#include <pspg/math/GpuResources.h>
13#include <pscf/math/IntVec.h>
14#include <util/global.h>
15#include <cufft.h>
16
17namespace Pscf {
18namespace Pspg
19{
20
21 using namespace Util;
22 using namespace Pscf;
23
33 template <int D>
34 class RDFieldDft : public DField<cudaComplex>
35 {
36
37 public:
38
42 RDFieldDft();
43
51 RDFieldDft(const RDFieldDft<D>& other);
52
58 virtual ~RDFieldDft();
59
71
80
84 const IntVec<D>& meshDimensions() const;
85
93 const IntVec<D>& dftDimensions() const;
94
101 template <class Archive>
102 void serialize(Archive& ar, const unsigned int version);
103
104 using DField<cudaComplex>::allocate;
105 using DField<cudaComplex>::operator =;
106
107 private:
108
109 // Vector containing number of grid points in each direction.
110 IntVec<D> meshDimensions_;
111
112 // Vector containing dimensions of dft (Fourier) grid.
113 IntVec<D> dftDimensions_;
114
115 };
116
117 /*
118 * Allocate the underlying C array for an FFT grid.
119 */
120 template <int D>
121 void RDFieldDft<D>::allocate(const IntVec<D>& meshDimensions)
122 {
123 int size = 1;
124 for (int i = 0; i < D; ++i) {
125 UTIL_CHECK(meshDimensions[i] > 0);
126 meshDimensions_[i] = meshDimensions[i];
127 if (i < D - 1) {
128 dftDimensions_[i] = meshDimensions[i];
129 size *= meshDimensions[i];
130 } else {
131 dftDimensions_[i] = (meshDimensions[i]/2 + 1);
132 size *= (meshDimensions[i]/2 + 1);
133 }
134 }
136 }
137
138 /*
139 * Return mesh dimensions by constant reference.
140 */
141 template <int D>
143 { return meshDimensions_; }
144
145 /*
146 * Return dimensions of dft grid by constant reference.
147 */
148 template <int D>
150 { return dftDimensions_; }
151
152 /*
153 * Serialize a Field to/from an Archive.
154 */
155 template <int D>
156 template <class Archive>
157 void RDFieldDft<D>::serialize(Archive& ar, const unsigned int version)
158 {
159 int capacity;
160 if (Archive::is_saving()) {
161 capacity = capacity_;
162 }
163 ar & capacity;
164 if (Archive::is_loading()) {
165 if (!isAllocated()) {
166 if (capacity > 0) {
167 allocate(capacity);
168 }
169 } else {
170 if (capacity != capacity_) {
171 UTIL_THROW("Inconsistent Field capacities");
172 }
173 }
174 }
175
176 if (isAllocated()) {
177 cudaComplex* tempData = new cudaComplex[capacity];
178 cudaMemcpy(tempData, data_, capacity * sizeof(cudaComplex),
179 cudaMemcpyDeviceToHost);
180 for (int i = 0; i < capacity_; ++i) {
181 ar & tempData[i].x;
182 ar & tempData[i].y;
183 }
184 delete[] tempData;
185 }
186 ar & meshDimensions_;
187 }
188
189}
190}
191#include "RDFieldDft.tpp"
192#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition: IntVec.h:27
Dynamic array on the GPU with alligned data.
Definition: DField.h:30
void allocate(int capacity)
Allocate the underlying C array on the device.
Definition: DField.tpp:52
Discrete Fourier Transform (DFT) of a real field on an FFT mesh.
Definition: RDFieldDft.h:35
virtual ~RDFieldDft()
Destructor.
Definition: RDFieldDft.tpp:30
void allocate(const IntVec< D > &meshDimensions)
Allocate the underlying C array for an FFT grid.
Definition: RDFieldDft.h:121
const IntVec< D > & meshDimensions() const
Return vector of mesh dimensions by constant reference.
Definition: RDFieldDft.h:142
RDFieldDft< D > & operator=(const RDFieldDft< D > &other)
Assignment operator.
Definition: RDFieldDft.tpp:59
RDFieldDft()
Default constructor.
Definition: RDFieldDft.tpp:22
const IntVec< D > & dftDimensions() const
Return vector of dft (Fourier) grid dimensions by const reference.
Definition: RDFieldDft.h:149
void serialize(Archive &ar, const unsigned int version)
Serialize a Field to/from an Archive.
Definition: RDFieldDft.h:157
File containing preprocessor macros for error handling.
#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:51
C++ namespace for polymer self-consistent field theory (PSCF).
Utility classes for scientific computation.
Definition: accumulators.mod:1