PSCF v1.5.0
cpp/RField.tpp
1#ifndef PSCF_R_FIELD_CP_TPP
2#define PSCF_R_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 "RField.h"
12#include <pscf/backend/cpp/HostSendArray.h>
13#include <util/global.h>
14
15namespace Pscf {
16
17 using namespace Util;
18
22 template <int D>
24 : BackendArray<double,CPT>(),
25 meshDimensions_(0)
26 {}
27
28 /*
29 * Copy constructor.
30 *
31 * Allocates new memory and copies all elements by value.
32 *
33 *\param other the Field to be copied.
34 */
35 template <int D>
37 : BackendArray<double,CPT>(),
38 meshDimensions_(0)
39 {
40 if (other.isAllocated()) {
41 allocate(other.meshDimensions_);
42 for (int i = 0; i < capacity_; ++i) {
43 data_[i] = other.data_[i];
44 }
45 }
46 }
47
48 /*
49 * Allocate the underlying C array for an FFT grid.
50 */
51 template <int D>
53 {
54 int size = 1;
55 for (int i = 0; i < D; ++i) {
57 meshDimensions_[i] = meshDimensions[i];
58 size *= meshDimensions[i];
59 }
61 }
62
63 /*
64 * Dellocate the underlying C array and clear meshDimensions.
65 */
66 template <int D>
68 {
70 for (int i = 0; i < D; ++i) {
71 meshDimensions_[i] = 0;
72 }
73 }
74
75 /*
76 * Associate this object with a slice of another BackendArray.
77 */
78 template <int D>
80 int beginId,
82 {
83 int size = 1;
84 for (int i = 0; i < D; ++i) {
86 meshDimensions_[i] = meshDimensions[i];
87 size *= meshDimensions[i];
88 }
89 BackendArray<double,CPT>::associate(arr, beginId, size);
90 }
91
92 /*
93 * Assignment, element-by-element.
94 *
95 * This operator will allocate memory if not allocated previously.
96 *
97 * \throw Exception if other Field is not allocated.
98 * \throw Exception if both Fields are allocated with unequal capacities.
99 *
100 * \param other the rhs Field
101 */
102 template <int D>
105 {
106 // Check for self assignment
107 if (this == &other) return *this;
108
109 // Preconditions
110 UTIL_CHECK (other.isAllocated());
111 if (!isAllocated()) {
112 allocate(other.meshDimensions_);
113 }
114 UTIL_CHECK(capacity_ == other.capacity_);
115 UTIL_CHECK(meshDimensions_ == other.meshDimensions_);
116
117 // Copy elements
118 for (int i = 0; i < capacity_; ++i) {
119 data_[i] = other[i];
120 }
121
122 return *this;
123 }
124
125 /*
126 * Pseudo-assignment from an associated HostSendArray.
127 */
128 template <int D>
135
136}
137#endif
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
void allocate(IntVec< D > const &meshDimensions)
Allocate the underlying C array for values on a regular mesh.
RField()
Default constructor.
IntVec< D > const & meshDimensions() const
Return mesh dimensions by constant reference.
Definition cpp/RField.h:152
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
PSCF package top-level namespace.