PSCF v1.5.0
cpp/CField.tpp
1#ifndef PSCF_C_FIELD_CP_TPP
2#define PSCF_C_FIELD_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 "CField.h"
12#include <util/global.h>
13
14namespace Pscf {
15
16 using namespace Util;
17
21 template <int D>
23 : BackendArray<fftw_complex,CPT>(),
24 meshDimensions_()
25 {}
26
27 /*
28 * Destructor.
29 */
30 template <int D>
33
34 /*
35 * Copy constructor.
36 *
37 * Allocates new memory and copies all elements by value.
38 */
39 template <int D>
41 : BackendArray<fftw_complex,CPT>(),
42 meshDimensions_()
43 {
44 if (other.isAllocated() && other.capacity_ > 0) {
45 FftwDRArray<fftw_complex>::allocate(other.capacity_);
46 meshDimensions_ = other.meshDimensions_;
47 for (int i = 0; i < capacity_; ++i) {
48 data_[i][0] = other.data_[i][0];
49 data_[i][1] = other.data_[i][1];
50 }
51 }
52 }
53
54 /*
55 * Assignment, element-by-element.
56 *
57 * This operator will allocate memory if not allocated previously.
58 */
59 template <int D>
61 {
62 // Check for self assignment
63 if (this == &other) return *this;
64
65 // Precondition
66 if (!other.isAllocated()) {
67 UTIL_THROW("Other CField must be allocated in assignment.");
68 }
69
70 if (!isAllocated()) {
71 allocate(other.meshDimensions_);
72 }
73 UTIL_CHECK(capacity_ == other.capacity_);
74 UTIL_CHECK(meshDimensions_ == other.meshDimensions_);
75
76 // Copy elements
77 for (int i = 0; i < capacity_; ++i) {
78 data_[i][0] = other.data_[i][0];
79 data_[i][1] = other.data_[i][1];
80 }
81
82 return *this;
83 }
84
85 /*
86 * Allocate the underlying C array for an FFT grid.
87 */
88 template <int D>
90 {
91 int size = 1;
92 for (int i = 0; i < D; ++i) {
94 meshDimensions_[i] = meshDimensions[i];
95 size *= meshDimensions[i];
96 }
98 }
99
100 /*
101 * Allocate the underlying C array for an FFT grid.
102 */
103 template <int D>
105 {
107 for (int i = 0; i < D; ++i) {
108 meshDimensions_[i] = 0;
109 }
110 }
111
112}
113#endif
CField()
Default constructor.
const IntVec< D > & meshDimensions() const
Return mesh dimensions by constant reference.
Definition cpp/CField.h:122
void allocate(const IntVec< D > &meshDimensions)
Allocate the underlying C array for an FFT grid.
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
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
PSCF package top-level namespace.