forked from idaholab/magpie
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FFT buffer base class template (idaholab#401)
- Loading branch information
Showing
4 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/**********************************************************************/ | ||
/* DO NOT MODIFY THIS HEADER */ | ||
/* MAGPIE - Mesoscale Atomistic Glue Program for Integrated Execution */ | ||
/* */ | ||
/* Copyright 2017 Battelle Energy Alliance, LLC */ | ||
/* ALL RIGHTS RESERVED */ | ||
/**********************************************************************/ | ||
|
||
#pragma once | ||
|
||
#include "GeneralUserObject.h" | ||
|
||
template <typename T> | ||
class FFTBufferBase; | ||
|
||
/** | ||
* Generic FFT interleaved data buffer base class | ||
*/ | ||
template <typename T> | ||
class FFTBufferBase : public GeneralUserObject | ||
{ | ||
public: | ||
FFTBufferBase(const InputParameters & parameters); | ||
|
||
virtual void initialize() {} | ||
virtual void execute() {} | ||
virtual void finalize() {} | ||
|
||
// transforms | ||
virtual void forward() = 0; | ||
virtual void backward() = 0; | ||
|
||
// data access | ||
const T & operator[](std::size_t i) const { return _buffer[i]; } | ||
T & operator[](std::size_t i) { return _buffer[i]; } | ||
|
||
// size buffer | ||
virtual void resize(); | ||
void resize(std::size_t ni); | ||
void resize(std::size_t ni, std::size_t nj); | ||
void resize(std::size_t ni, std::size_t nj, std::size_t nk); | ||
|
||
protected: | ||
/// get the addres of the first data element of the ith object in the bufefr | ||
Real * start(std::size_t i); | ||
|
||
/// get the number of transforms required for type T | ||
std::size_t howMany() const; | ||
|
||
std::size_t _dim; | ||
|
||
std::size_t _ni; | ||
std::size_t _nj; | ||
std::size_t _nk; | ||
|
||
std::vector<T> _buffer; | ||
|
||
/// pointer to the start of the data | ||
Real * _start; | ||
|
||
/// stride in units of double size | ||
std::ptrdiff_t _stride; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/**********************************************************************/ | ||
/* DO NOT MODIFY THIS HEADER */ | ||
/* MAGPIE - Mesoscale Atomistic Glue Program for Integrated Execution */ | ||
/* */ | ||
/* Copyright 2017 Battelle Energy Alliance, LLC */ | ||
/* ALL RIGHTS RESERVED */ | ||
/**********************************************************************/ | ||
#ifdef FFTW3_ENABLED | ||
|
||
#pragma once | ||
|
||
#include "FFTBufferBase.h" | ||
|
||
#include "fftw3.h" | ||
|
||
template <typename T> | ||
class FFTWBufferBase; | ||
|
||
/** | ||
* FFTW specific interleaved data buffer base class | ||
*/ | ||
template <typename T> | ||
class FFTWBufferBase : public FFTBufferBase<T> | ||
{ | ||
public: | ||
FFTWBufferBase(const InputParameters & parameters); | ||
~FFTWBufferBase(); | ||
|
||
// transforms | ||
virtual void forward(); | ||
virtual void backward(); | ||
|
||
// resize buffer | ||
void resize() override; | ||
|
||
protected: | ||
/// FFTW plans | ||
fftw_plan _forward_plan; | ||
fftw_plan _backward_plan; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/**********************************************************************/ | ||
/* DO NOT MODIFY THIS HEADER */ | ||
/* MAGPIE - Mesoscale Atomistic Glue Program for Integrated Execution */ | ||
/* */ | ||
/* Copyright 2017 Battelle Energy Alliance, LLC */ | ||
/* ALL RIGHTS RESERVED */ | ||
/**********************************************************************/ | ||
#include "FFTBufferBase.h" | ||
#include "MooseTypes.h" | ||
#include "RankTwoTensor.h" | ||
#include "RankThreeTensor.h" | ||
#include "RankFourTensor.h" | ||
|
||
// template <> | ||
// InputParameters | ||
// validParams<FFTBufferBase>() | ||
// { | ||
// InputParameters params = validParams<GeneralUserObject>(); | ||
// params.addClassDescription(""); | ||
// return params; | ||
// } | ||
|
||
template <typename T> | ||
FFTBufferBase<T>::FFTBufferBase(const InputParameters & parameters) : GeneralUserObject(parameters) | ||
{ | ||
} | ||
|
||
template <typename T> | ||
void | ||
FFTBufferBase<T>::resize() | ||
{ | ||
if (sizeof(Real) != sizeof(double)) | ||
mooseError("Invalid Real type size"); | ||
_buffer.resize(_ni * _nj * _nk); | ||
|
||
// redo plan / pointers | ||
auto istart = start(0); | ||
std::ptrdiff_t istride = reinterpret_cast<char *>(start(1)) - reinterpret_cast<char *>(istart); | ||
if (istride % sizeof(Real) != 0) | ||
mooseError("Invalid data alignment"); | ||
istride /= sizeof(Real); | ||
} | ||
|
||
template <typename T> | ||
void | ||
FFTBufferBase<T>::resize(std::size_t ni) | ||
{ | ||
_ni = ni; | ||
_nj = 1; | ||
_nk = 1; | ||
_dim = 1; | ||
resize(); | ||
} | ||
|
||
template <typename T> | ||
void | ||
FFTBufferBase<T>::resize(std::size_t ni, std::size_t nj) | ||
{ | ||
_ni = ni; | ||
_nj = nj; | ||
_nk = 1; | ||
_dim = 2; | ||
resize(); | ||
} | ||
|
||
template <typename T> | ||
void | ||
FFTBufferBase<T>::resize(std::size_t ni, std::size_t nj, std::size_t nk) | ||
{ | ||
_ni = ni; | ||
_nj = nj; | ||
_nk = nk; | ||
_dim = 3; | ||
resize(); | ||
} | ||
|
||
template <> | ||
Real * | ||
FFTBufferBase<Real>::start(std::size_t i) | ||
{ | ||
return &_buffer[i]; | ||
} | ||
|
||
template <> | ||
Real * | ||
FFTBufferBase<RealVectorValue>::start(std::size_t i) | ||
{ | ||
return &_buffer[i](0); | ||
} | ||
|
||
template <> | ||
Real * | ||
FFTBufferBase<RankTwoTensor>::start(std::size_t i) | ||
{ | ||
return &_buffer[i](0, 0); | ||
} | ||
|
||
template <> | ||
Real * | ||
FFTBufferBase<RankThreeTensor>::start(std::size_t i) | ||
{ | ||
return &_buffer[i](0, 0, 0); | ||
} | ||
|
||
template <> | ||
Real * | ||
FFTBufferBase<RankFourTensor>::start(std::size_t i) | ||
{ | ||
return &_buffer[i](0, 0, 0, 0); | ||
} | ||
|
||
template <> | ||
std::size_t | ||
FFTBufferBase<Real>::howMany() const | ||
{ | ||
return 1; | ||
} | ||
|
||
template <> | ||
std::size_t | ||
FFTBufferBase<RealVectorValue>::howMany() const | ||
{ | ||
return LIBMESH_DIM; | ||
} | ||
|
||
template <> | ||
std::size_t | ||
FFTBufferBase<RankTwoTensor>::howMany() const | ||
{ | ||
return LIBMESH_DIM * LIBMESH_DIM; | ||
} | ||
|
||
template <> | ||
std::size_t | ||
FFTBufferBase<RankThreeTensor>::howMany() const | ||
{ | ||
return LIBMESH_DIM * LIBMESH_DIM * LIBMESH_DIM; | ||
} | ||
|
||
template <> | ||
std::size_t | ||
FFTBufferBase<RankFourTensor>::howMany() const | ||
{ | ||
return LIBMESH_DIM * LIBMESH_DIM * LIBMESH_DIM * LIBMESH_DIM; | ||
} | ||
|
||
// explicit instantiation | ||
template class FFTBufferBase<Real>; | ||
template class FFTBufferBase<RealVectorValue>; | ||
template class FFTBufferBase<RankTwoTensor>; | ||
template class FFTBufferBase<RankThreeTensor>; | ||
template class FFTBufferBase<RankFourTensor>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/**********************************************************************/ | ||
/* DO NOT MODIFY THIS HEADER */ | ||
/* MAGPIE - Mesoscale Atomistic Glue Program for Integrated Execution */ | ||
/* */ | ||
/* Copyright 2017 Battelle Energy Alliance, LLC */ | ||
/* ALL RIGHTS RESERVED */ | ||
/**********************************************************************/ | ||
#ifdef FFTW3_ENABLED | ||
|
||
#include "FFTWBufferBase.h" | ||
#include "MooseTypes.h" | ||
#include "RankTwoTensor.h" | ||
#include "RankThreeTensor.h" | ||
#include "RankFourTensor.h" | ||
|
||
template <typename T> | ||
FFTWBufferBase<T>::FFTWBufferBase(const InputParameters & parameters) | ||
: FFTBufferBase<T>(parameters), _forward_plan(nullptr), _backward_plan(nullptr) | ||
{ | ||
} | ||
|
||
template <typename T> | ||
FFTWBufferBase<T>::~FFTWBufferBase() | ||
{ | ||
// destroy FFTW plans | ||
if (_forward_plan) | ||
fftw_destroy_plan(_forward_plan); | ||
if (_backward_plan) | ||
fftw_destroy_plan(_backward_plan); | ||
} | ||
|
||
template <typename T> | ||
void | ||
FFTWBufferBase<T>::forward() | ||
{ | ||
// execute plan | ||
if (_forward_plan) | ||
fftw_execute(_forward_plan); | ||
else | ||
mooseError("No forward plan created"); | ||
} | ||
|
||
template <typename T> | ||
void | ||
FFTWBufferBase<T>::backward() | ||
{ | ||
// execute plan | ||
if (_backward_plan) | ||
fftw_execute(_backward_plan); | ||
else | ||
mooseError("No backward plan created"); | ||
} | ||
|
||
template <typename T> | ||
void | ||
FFTWBufferBase<T>::resize() | ||
{ | ||
// call parent class to rezize data bufefr | ||
FFTBufferBase<T>::resize(); | ||
|
||
// destroy FFTW plans | ||
if (_forward_plan) | ||
fftw_destroy_plan(_forward_plan); | ||
if (_backward_plan) | ||
fftw_destroy_plan(_backward_plan); | ||
|
||
// create new plans using _start, _stride, and howMany() | ||
} | ||
|
||
// explicit instantiation | ||
template class FFTWBufferBase<Real>; | ||
template class FFTWBufferBase<RealVectorValue>; | ||
template class FFTWBufferBase<RankTwoTensor>; | ||
template class FFTWBufferBase<RankThreeTensor>; | ||
template class FFTWBufferBase<RankFourTensor>; | ||
|
||
#endif |