PSCF v1.3
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
13namespace Pscf {
14namespace Prdc {
15namespace Cpu {
16
17 using namespace Util;
18
22 template <int D>
24 : FftwDArray<fftw_complex>(),
25 meshDimensions_()
26 {}
27
28 /*
29 * Destructor.
30 */
31 template <int D>
34
35 /*
36 * Copy constructor.
37 *
38 * Allocates new memory and copies all elements by value.
39 */
40 template <int D>
42 : FftwDArray<fftw_complex>(),
43 meshDimensions_()
44 {
45 if (other.isAllocated() && other.capacity_ > 0) {
46 FftwDArray<fftw_complex>::allocate(other.capacity_);
47 meshDimensions_ = other.meshDimensions_;
48 for (int i = 0; i < capacity_; ++i) {
49 data_[i][0] = other.data_[i][0];
50 data_[i][1] = other.data_[i][1];
51 }
52 }
53 }
54
55 /*
56 * Assignment, element-by-element.
57 *
58 * This operator will allocate memory if not allocated previously.
59 */
60 template <int D>
62 {
63 // Check for self assignment
64 if (this == &other) return *this;
65
66 // Precondition
67 if (!other.isAllocated()) {
68 UTIL_THROW("Other CField must be allocated in assignment.");
69 }
70
71 if (!isAllocated()) {
72 allocate(other.meshDimensions_);
73 }
75 UTIL_CHECK(meshDimensions_ == other.meshDimensions_);
76
77 // Copy elements
78 for (int i = 0; i < capacity_; ++i) {
79 data_[i][0] = other.data_[i][0];
80 data_[i][1] = other.data_[i][1];
81 }
82
83 return *this;
84 }
85
86 /*
87 * Allocate the underlying C array for an FFT grid.
88 */
89 template <int D>
91 {
92 int size = 1;
93 for (int i = 0; i < D; ++i) {
95 meshDimensions_[i] = meshDimensions[i];
96 size *= meshDimensions[i];
97 }
99 }
100
101 /*
102 * Allocate the underlying C array for an FFT grid.
103 */
104 template <int D>
106 {
108 for (int i = 0; i < D; ++i) {
109 meshDimensions_[i] = 0;
110 }
111 }
112
113}
114}
115}
116#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
const IntVec< D > & meshDimensions() const
Return mesh dimensions by constant reference.
Definition cpu/CField.h:127
CField()
Default constructor.
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 & operator=(const CField &other)
Assignment operator.
virtual ~CField()
Destructor.
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:102
fftw_complex * data_
Definition Array.h:109
int capacity_
Allocated size of the data_ array.
Definition Array.h:112
#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 CField.cpp:12
Periodic fields and crystallography.
Definition CField.cpp:11
PSCF package top-level namespace.
Definition param_pc.dox:1