Skip to content

Commit

Permalink
Demonstrate working transform (idaholab#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen committed Jan 22, 2020
1 parent 1ae87ec commit 39b29e6
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 11 deletions.
64 changes: 64 additions & 0 deletions examples/spectral.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[Mesh]
type = MyTRIMMesh
dim = 2
xmax = 100
ymax = 100
nx = 100
ny = 100
[]

[Problem]
type = FFTProblem
[]

[AuxVariables]
[./c_aux]
order = CONSTANT
family = MONOMIAL
[./InitialCondition]
type = FunctionIC
function = 'cos(x/100*2*pi*4)*cos(y/100*2*pi*3)'
[../]
[../]
[]

[Materials]
[./test]
type = ParsedMaterial
args = c
function = c*c
[../]
[]

[UserObjects]
# Buffers
[./c]
type = RealFFTWBuffer
moose_variable = c_aux
[../]
[./R]
type = RankTwoTensorFFTWBuffer
[../]

# Solver
# ...
[]

[AuxKernels]
[./c_aux]
type = FFTBufferAux
variable = c_aux
fft_buffer = c
execute_on = FINAL
[../]
[]

[Executioner]
type = SpectralExecutionerBase
[]

[Outputs]
exodus = true
execute_on = 'INITIAL FINAL'
perf_graph = true
[]
53 changes: 53 additions & 0 deletions include/executioners/SpectralExecutionerBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**********************************************************************/
/* 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 "Executioner.h"
#include "FFTWBufferBase.h"
#include "FFTProblem.h"

// System includes
#include <string>

// Forward declarations
class InputParameters;

/**
* FFT Executioner base class.
*/
class SpectralExecutionerBase : public Executioner
{
public:
static InputParameters validParams();

SpectralExecutionerBase(const InputParameters & parameters);

virtual void init() override;
virtual void execute() override;
virtual bool lastSolveConverged() const override { return true; }

protected:
template <typename T>
FFTBufferBase<T> & getFFTBuffer(const std::string & name);

Real _system_time;
int & _time_step;
Real & _time;

PerfID _final_timer;

FFTProblem * _fft_problem;
};

template <typename T>
FFTBufferBase<T> &
SpectralExecutionerBase::getFFTBuffer(const std::string & name)
{
return _fft_problem->getFFTBuffer<Real>("c");
}
19 changes: 18 additions & 1 deletion include/problems/FFTProblem.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "FEProblem.h"
#include "AuxiliarySystem.h"
#include "FFTBufferBase.h"

/**
* Enhanced FEProblem that supports FFT buffers as variables
Expand All @@ -29,7 +30,8 @@ class FFTProblem : public FEProblem
Moose::VarKindType expected_var_type = Moose::VarKindType::VAR_ANY,
Moose::VarFieldType expected_var_field_type = Moose::VarFieldType::VAR_FIELD_ANY) override;

// getMaterialData
template <typename T>
FFTBufferBase<T> & getFFTBuffer(const std::string & name);

protected:
/// map from variable name to list of variable objects (one per thread)
Expand All @@ -40,3 +42,18 @@ class FFTProblem : public FEProblem

unsigned int _fft_var_number;
};

template <typename T>
FFTBufferBase<T> &
FFTProblem::getFFTBuffer(const std::string & name)
{
std::vector<UserObject *> objs;
theWarehouse().query().condition<AttribThread>(0).condition<AttribName>(name).queryInto(objs);
if (objs.empty())
mooseError("Unable to find FFT buffer with name '" + name + "'");
auto fft_buffer = dynamic_cast<FFTBufferBase<T> *>(objs[0]);
if (!fft_buffer)
mooseError(name, " is not an FFT buffer of the requested type");

return *fft_buffer;
}
9 changes: 9 additions & 0 deletions include/userobjects/FFTBufferBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
template <typename T>
class FFTBufferBase;

#define usingFFTBufferBaseMembers \
using ElementUserObject::_perf_graph; \
using FFTBufferBase<T>::_dim; \
using FFTBufferBase<T>::_grid; \
using FFTBufferBase<T>::_buffer; \
using FFTBufferBase<T>::_start; \
using FFTBufferBase<T>::_stride; \
using FFTBufferBase<T>::_how_many

/**
* Generic FFT interleaved data buffer base class
*/
Expand Down
11 changes: 10 additions & 1 deletion include/userobjects/FFTWBufferBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once

#include "FFTBufferBase.h"
#include "PerfGraphInterface.h"

#include "fftw3.h"

Expand All @@ -31,9 +32,17 @@ class FFTWBufferBase : public FFTBufferBase<T>
void backward() override;

protected:
/// FFTW plans
///@{ FFTW plans
fftw_plan _forward_plan;
fftw_plan _backward_plan;
///@}

///@{ timers
PerfID _perf_plan;
PerfID _perf_fft;
///@}

usingFFTBufferBaseMembers;
};

#endif
1 change: 1 addition & 0 deletions include/userobjects/FourierTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once

#include "ElementUserObject.h"
#include "PerfGraphInterface.h"
#include "fftw3.h"

#include <memory>
Expand Down
11 changes: 7 additions & 4 deletions include/variables/MooseFFTVariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class MooseFFTVariable : public MooseVariable
* @param subdomain The subdomain id in question
* @return true if active on subdomain, false otherwise
*/
virtual bool activeOnSubdomain(SubdomainID subdomain) const { return true; }
virtual bool activeOnSubdomain(SubdomainID /*subdomain*/) const { return true; }

/**
* Prepare the initial condition
Expand Down Expand Up @@ -140,12 +140,15 @@ class MooseFFTVariable : public MooseVariable
*/
virtual void computeNodalValues() {}

virtual void getDofIndices(const Elem * elem, std::vector<dof_id_type> & dof_indices) const {}
virtual void getDofIndices(const Elem * /*elem*/,
std::vector<dof_id_type> & /*dof_indices*/) const
{
}

virtual unsigned int numberOfDofsNeighbor() { return 0; }

virtual void insert(NumericVector<Number> & residual) {}
virtual void add(NumericVector<Number> & residual) {}
virtual void insert(NumericVector<Number> & /*residual*/) {}
virtual void add(NumericVector<Number> & /*residual*/) {}

protected:
std::vector<dof_id_type> _no_dofs;
Expand Down
1 change: 1 addition & 0 deletions src/auxkernels/FFTBufferAux.C
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ FFTBufferAux::FFTBufferAux(const InputParameters & parameters)
Real
FFTBufferAux::computeValue()
{
std::cout << 'B';
return _buffer(_current_elem->centroid());
}
68 changes: 68 additions & 0 deletions src/executioners/SpectralExecutionerBase.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**********************************************************************/
/* DO NOT MODIFY THIS HEADER */
/* MAGPIE - Mesoscale Atomistic Glue Program for Integrated Execution */
/* */
/* Copyright 2017 Battelle Energy Alliance, LLC */
/* ALL RIGHTS RESERVED */
/**********************************************************************/

#include "SpectralExecutionerBase.h"
#include "InputParameters.h"

// testing
registerMooseObject("MagpieApp", SpectralExecutionerBase);

InputParameters
SpectralExecutionerBase::validParams()
{
InputParameters params = Executioner::validParams();
params.addClassDescription("Executioner for FFT simulations.");
params.addParam<Real>("time", 0.0, "System time");
return params;
}

SpectralExecutionerBase::SpectralExecutionerBase(const InputParameters & parameters)
: Executioner(parameters),
_system_time(getParam<Real>("time")),
_time_step(_fe_problem.timeStep()),
_time(_fe_problem.time()),
_final_timer(registerTimedSection("final", 1)),
_fft_problem(dynamic_cast<FFTProblem *>(&_fe_problem))
{

if (!_fft_problem)
mooseError("Use Problem/type=FFTProblem with a spectral executioner");
}

void
SpectralExecutionerBase::init()
{
if (_app.isRecovering())
{
_console << "\nCannot recover FFT solves!\nExiting...\n" << std::endl;
return;
}

// checkIntegrity();
_fe_problem.execute(EXEC_PRE_MULTIAPP_SETUP);
_fe_problem.initialSetup();
}

void
SpectralExecutionerBase::execute()
{
_time_step = 0;
_time = _time_step;
_fe_problem.outputStep(EXEC_INITIAL);
_fe_problem.advanceState();

mooseInfo("SpectralExecutionerBase::execute()");

auto & c = getFFTBuffer<Real>("c");
c.forward();

_time_step = 1;
_fe_problem.execute(EXEC_FINAL);
_time = _time_step;
_fe_problem.outputStep(EXEC_FINAL);
}
5 changes: 3 additions & 2 deletions src/userobjects/FFTBufferBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ FFTBufferBase<T>::FFTBufferBase(const InputParameters & parameters)
_buffer.resize(_buffer_size);

// compute stride and start pointer
auto istart = start(0);
std::ptrdiff_t istride = reinterpret_cast<char *>(start(1)) - reinterpret_cast<char *>(istart);
_start = reinterpret_cast<Real *>(start(0));
std::ptrdiff_t istride = reinterpret_cast<char *>(start(1)) - reinterpret_cast<char *>(_start);
if (istride % sizeof(Real) != 0)
mooseError("Invalid data alignment");
istride /= sizeof(Real);
Expand All @@ -122,6 +122,7 @@ template <>
void
FFTBufferBase<Real>::execute()
{
std::cout << 'A';
// get grid / buffer location
Point centroid = _current_elem->centroid();
std::size_t a = 0;
Expand Down
22 changes: 19 additions & 3 deletions src/userobjects/FFTWBufferBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@

template <typename T>
FFTWBufferBase<T>::FFTWBufferBase(const InputParameters & parameters)
: FFTBufferBase<T>(parameters), _forward_plan(nullptr), _backward_plan(nullptr)
: FFTBufferBase<T>(parameters),
_perf_plan(this->registerTimedSection("fftw_plan_r2r", 2)),
_perf_fft(this->registerTimedSection("fftw_execute", 2))
{
// create plans
std::vector<fftw_r2r_kind> kind(_dim, FFTW_R2HC);
{
TIME_SECTION(_perf_plan);
_forward_plan = fftw_plan_r2r(_dim, _grid.data(), _start, _start, kind.data(), FFTW_ESTIMATE);
_backward_plan = fftw_plan_r2r(_dim, _grid.data(), _start, _start, kind.data(), FFTW_ESTIMATE);
}
}

template <typename T>
Expand All @@ -33,16 +41,24 @@ template <typename T>
void
FFTWBufferBase<T>::forward()
{
mooseInfo("FFTWBufferBase<T>::forward()");

// execute plan
fftw_execute(_forward_plan);
{
TIME_SECTION(_perf_fft);
fftw_execute(_forward_plan);
}
}

template <typename T>
void
FFTWBufferBase<T>::backward()
{
// execute plan
fftw_execute(_backward_plan);
{
TIME_SECTION(_perf_fft);
fftw_execute(_backward_plan);
}
}

// explicit instantiation and registration
Expand Down

0 comments on commit 39b29e6

Please sign in to comment.