PSCF v1.4.0
FftwDArray.tpp
1#ifndef PRDC_CPU_FFTW_D_ARRAY_TPP
2#define PRDC_CPU_FFTW_D_ARRAY_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 "FftwDArray.h"
12#include <fftw3.h>
13
14namespace Pscf {
15namespace Prdc {
16namespace Cpu {
17
18 using namespace Util;
19
20 /*
21 * Default constructor.
22 */
23 template <typename Data>
25 : Array<Data>()
26 {}
27
28 /*
29 * Destructor.
30 */
31 template <typename Data>
33 {
34 if (isAllocated()) {
35 fftw_free(data_);
36 }
37 data_ = nullptr;
38 capacity_ = 0;
39 }
40
41 /*
42 * Allocate the underlying C array.
43 *
44 * Throw an Exception if the FftwDArray has already been allocated.
45 *
46 * \param capacity number of elements to allocate.
47 */
48 template <typename Data>
50 {
51 if (isAllocated()) {
52 UTIL_THROW("Attempt to re-allocate a FftwDArray");
53 }
54 if (capacity <= 0) {
55 UTIL_THROW("Attempt to allocate FftwDArray with capacity <= 0");
56 }
57 data_ = (Data*) fftw_malloc(sizeof(Data)*capacity);
58 capacity_ = capacity;
59 }
60
61 /*
62 * Deallocate the underlying C array.
63 *
64 * Throw an Exception if this FftwDArray is not allocated.
65 */
66 template <typename Data>
68 {
69 if (!isAllocated()) {
70 UTIL_THROW("Array is not allocated");
71 }
72 fftw_free(data_);
73 data_ = nullptr;
74 capacity_ = 0;
75 }
76
77}
78}
79}
80#endif
void allocate(int capacity)
Allocate the underlying C array.
virtual ~FftwDArray()
Destructor.
FftwDArray()
Default constructor.
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
Data * data_
Pointer to an array of Data elements.
Definition Array.h:107
int capacity() const
Return allocated size.
Definition Array.h:144
int capacity_
Allocated size of the data_ array.
Definition Array.h:110
#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.