PSCF v1.1
Field.tpp
1#ifndef PSPC_FIELD_TPP
2#define PSPC_FIELD_TPP
3
4/*
5* PSCF Package
6*
7* Copyright 2016 - 2022, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "Field.h"
12#include <util/misc/Memory.h>
13
14#include <fftw3.h>
15
16namespace Pscf {
17namespace Pspc
18{
19
20 using namespace Util;
21
22 /*
23 * Default constructor.
24 */
25 template <typename Data>
27 : data_(0),
28 capacity_(0)
29 {}
30
31 /*
32 * Destructor.
33 */
34 template <typename Data>
36 {
37 if (isAllocated()) {
38 fftw_free(data_);
39 capacity_ = 0;
40 }
41 }
42
43 /*
44 * Allocate the underlying C array.
45 *
46 * Throw an Exception if the Field has already allocated.
47 *
48 * \param capacity number of elements to allocate.
49 */
50 template <typename Data>
51 void Field<Data>::allocate(int capacity)
52 {
53 if (isAllocated()) {
54 UTIL_THROW("Attempt to re-allocate a Field");
55 }
56 if (capacity <= 0) {
57 UTIL_THROW("Attempt to allocate with capacity <= 0");
58 }
59 data_ = (Data*) fftw_malloc(sizeof(Data)*capacity);
60 capacity_ = capacity;
61 }
62
63 /*
64 * Deallocate the underlying C array.
65 *
66 * Throw an Exception if this Field is not allocated.
67 */
68 template <typename Data>
70 {
71 if (!isAllocated()) {
72 UTIL_THROW("Array is not allocated");
73 }
74 fftw_free(data_);
75 capacity_ = 0;
76 }
77
78}
79}
80#endif
Base class template for a field defined on a spatial grid.
Field()
Default constructor.
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:199
void deallocate()
Dellocate the underlying C array.
Definition: DArray.h:217
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
C++ namespace for polymer self-consistent field theory (PSCF).
Utility classes for scientific computation.
Definition: accumulators.mod:1