PSCF v1.4.0
cpu/RField.tpp
1#ifndef PRDC_CPU_R_FIELD_TPP
2#define PRDC_CPU_R_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 "RField.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<double>(),
26 meshDimensions_(0)
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 *\param other the Field to be copied.
42 */
43 template <int D>
45 : FftwDArray<double>(),
46 meshDimensions_(0)
47 {
48 if (other.isAllocated()) {
49 allocate(other.meshDimensions_);
50 for (int i = 0; i < capacity_; ++i) {
51 data_[i] = other.data_[i];
52 }
53 }
54 }
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>
68 {
69 // Check for self assignment
70 if (this == &other) return *this;
71
72 // Precondition
73 if (!other.isAllocated()) {
74 UTIL_THROW("Other Field must be allocated.");
75 }
76
77 if (!isAllocated()) {
78 allocate(other.meshDimensions_);
79 }
81 UTIL_CHECK(meshDimensions_ == other.meshDimensions_);
82
83 // Copy elements
84 for (int i = 0; i < capacity_; ++i) {
85 data_[i] = other[i];
86 }
87
88 return *this;
89 }
90
91 /*
92 * Allocate the underlying C array for an FFT grid.
93 */
94 template <int D>
96 {
97 int size = 1;
98 for (int i = 0; i < D; ++i) {
100 meshDimensions_[i] = meshDimensions[i];
101 size *= meshDimensions[i];
102 }
104 }
105
106 /*
107 * Dellocate the underlying C array and clear meshDimensions.
108 */
109 template <int D>
111 {
113 for (int i = 0; i < D; ++i) {
114 meshDimensions_[i] = 0;
115 }
116 }
117
118}
119}
120}
121#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
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
void allocate(IntVec< D > const &meshDimensions)
Allocate the underlying C array for an FFT grid.
virtual ~RField()
Destructor.
virtual void deallocate()
Deallocate memory and return to empty state.
RField()
Default constructor.
const IntVec< D > & meshDimensions() const
Return mesh dimensions by constant reference.
Definition cpu/RField.h:111
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.