PSCF v1.4.0
cpu/CField.tpp
1#ifndef PRDC_CPU_C_FIELD_TPP
2#define PRDC_CPU_C_FIELD_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 "CField.h"
12#include <util/global.h>
13
14namespace Pscf {
15namespace Prdc {
16namespace Cpu {
17
18 using namespace Util;
19
23 template <int D>
25 : FftwDArray<fftw_complex>(),
26 meshDimensions_()
27 {}
28
29 /*
30 * Destructor.
31 */
32 template <int D>
35
36 /*
37 * Copy constructor.
38 *
39 * Allocates new memory and copies all elements by value.
40 */
41 template <int D>
43 : FftwDArray<fftw_complex>(),
44 meshDimensions_()
45 {
46 if (other.isAllocated() && other.capacity_ > 0) {
47 FftwDArray<fftw_complex>::allocate(other.capacity_);
48 meshDimensions_ = other.meshDimensions_;
49 for (int i = 0; i < capacity_; ++i) {
50 data_[i][0] = other.data_[i][0];
51 data_[i][1] = other.data_[i][1];
52 }
53 }
54 }
55
56 /*
57 * Assignment, element-by-element.
58 *
59 * This operator will allocate memory if not allocated previously.
60 */
61 template <int D>
63 {
64 // Check for self assignment
65 if (this == &other) return *this;
66
67 // Precondition
68 if (!other.isAllocated()) {
69 UTIL_THROW("Other CField must be allocated in assignment.");
70 }
71
72 if (!isAllocated()) {
73 allocate(other.meshDimensions_);
74 }
76 UTIL_CHECK(meshDimensions_ == other.meshDimensions_);
77
78 // Copy elements
79 for (int i = 0; i < capacity_; ++i) {
80 data_[i][0] = other.data_[i][0];
81 data_[i][1] = other.data_[i][1];
82 }
83
84 return *this;
85 }
86
87 /*
88 * Allocate the underlying C array for an FFT grid.
89 */
90 template <int D>
92 {
93 int size = 1;
94 for (int i = 0; i < D; ++i) {
96 meshDimensions_[i] = meshDimensions[i];
97 size *= meshDimensions[i];
98 }
100 }
101
102 /*
103 * Allocate the underlying C array for an FFT grid.
104 */
105 template <int D>
107 {
109 for (int i = 0; i < D; ++i) {
110 meshDimensions_[i] = 0;
111 }
112 }
113
114}
115}
116}
117#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
virtual ~CField()
Destructor.
const IntVec< D > & meshDimensions() const
Return mesh dimensions by constant reference.
Definition cpu/CField.h:119
void allocate(const IntVec< D > &meshDimensions)
Allocate the underlying C array for an FFT grid.
virtual void deallocate()
Deallocate underlying C array and clear mesh dimensions.
CField()
Default constructor.
void allocate(int capacity)
Allocate the underlying C array.
virtual void deallocate()
Dellocate the underlying C array.
bool isAllocated() const
Return true if the FftwDArray has been allocated, false otherwise.
Definition FftwDArray.h:107
fftw_complex * data_
Definition Array.h:107
int capacity_
Allocated size of the data_ array.
Definition Array.h:110
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:49
Fields and FFTs for periodic boundary conditions (CPU)
Definition complex.cpp:12
Periodic fields and crystallography.
Definition complex.cpp:11
PSCF package top-level namespace.