Skip to content

Commit 0aca399

Browse files
committed
Initial conditions (idaholab#401)
1 parent 51f374f commit 0aca399

File tree

2 files changed

+93
-24
lines changed

2 files changed

+93
-24
lines changed

include/userobjects/FFTBufferBase.h

+18-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#pragma once
1010

11-
#include "GeneralUserObject.h"
11+
#include "ElementUserObject.h"
1212

1313
template <typename T>
1414
class FFTBufferBase;
@@ -17,7 +17,7 @@ class FFTBufferBase;
1717
* Generic FFT interleaved data buffer base class
1818
*/
1919
template <typename T>
20-
class FFTBufferBase : public GeneralUserObject
20+
class FFTBufferBase : public ElementUserObject
2121
{
2222
public:
2323
static InputParameters validParams();
@@ -27,14 +27,22 @@ class FFTBufferBase : public GeneralUserObject
2727
virtual void initialize() {}
2828
virtual void execute() {}
2929
virtual void finalize() {}
30+
virtual void threadJoin(const UserObject &) {}
3031

31-
// transforms
32+
///@{ transforms
3233
virtual void forward() = 0;
3334
virtual void backward() = 0;
35+
///@}
3436

35-
// data access
37+
///@{ data access by index
3638
const T & operator[](std::size_t i) const { return _buffer[i]; }
3739
T & operator[](std::size_t i) { return _buffer[i]; }
40+
///@}
41+
42+
///@{ data access by location
43+
const T & operator()(const Point & p) const;
44+
T & operator()(const Point & p);
45+
///@}
3846

3947
protected:
4048
/// get the addres of the first data element of the ith object in the bufefr
@@ -70,4 +78,10 @@ class FFTBufferBase : public GeneralUserObject
7078

7179
/// stride in units of double size
7280
std::ptrdiff_t _stride;
81+
82+
/// optional moose sister variabe (to obtain IC from)
83+
std::vector<const VariableValue *> _moose_variable;
84+
85+
/// cache the howMany value
86+
const std::size_t _how_many;
7387
};

src/userobjects/FFTBufferBase.C

+75-20
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,48 @@
55
/* Copyright 2017 Battelle Energy Alliance, LLC */
66
/* ALL RIGHTS RESERVED */
77
/**********************************************************************/
8+
89
#include "FFTBufferBase.h"
9-
#include "MooseTypes.h"
1010
#include "MyTRIMMesh.h"
11+
12+
#include "MooseTypes.h"
13+
#include "AuxiliarySystem.h"
14+
#include "AllLocalDofIndicesThread.h"
1115
#include "RankTwoTensor.h"
1216
#include "RankThreeTensor.h"
1317
#include "RankFourTensor.h"
1418

1519
#include <type_traits>
1620

17-
// template <>
18-
// InputParameters
19-
// validParams<FFTBufferBase>()
20-
// {
21-
// InputParameters params = validParams<GeneralUserObject>();
22-
// params.addClassDescription("");
23-
// return params;
24-
// }
25-
2621
template <typename T>
2722
InputParameters
2823
FFTBufferBase<T>::validParams()
2924
{
30-
InputParameters params = GeneralUserObject::validParams();
25+
InputParameters params = ElementUserObject::validParams();
3126
params.addClassDescription("Fourier transform data buffer object.");
3227
params.addRangeCheckedParam<std::vector<int>>(
3328
"grid",
3429
"grid > 0",
3530
"Number of grid cells in each dimension to compute "
3631
"the FFT on (can be omitted when using MyTRIMMesh)");
37-
params.addParam<unsigned int>(
38-
"max_h_level", 0, "Further grid refinement to apply when using MyTRIMMesh with adaptivity");
32+
params.addCoupledVar("moose_variable",
33+
"Optional AuxVariable to take the initial condition of the buffer from.");
34+
35+
// make sure we run the object on initial to apply the initial conditions
36+
params.set<ExecFlagEnum>("execute_on") = EXEC_INITIAL;
37+
3938
return params;
4039
}
4140

4241
template <typename T>
4342
FFTBufferBase<T>::FFTBufferBase(const InputParameters & parameters)
44-
: GeneralUserObject(parameters),
43+
: ElementUserObject(parameters),
4544
_mesh(_subproblem.mesh()),
4645
_dim(_mesh.dimension()),
4746
_cell_volume(1.0),
48-
_buffer_size(1)
49-
47+
_buffer_size(1),
48+
_moose_variable(coupledComponents("moose_variable")),
49+
_how_many(howMany())
5050
{
5151
// make sure Real is double
5252
static_assert(
@@ -56,6 +56,12 @@ FFTBufferBase<T>::FFTBufferBase(const InputParameters & parameters)
5656
// FFT needs a regular grid of values to operate on
5757
if (isParamValid("grid"))
5858
{
59+
// cannot specify a corresponding MOOSE Variable when running mesh-less
60+
if (!_moose_variable.empty())
61+
paramError("moose_variable",
62+
"You cannot specify a corresponding MOOSE Variable when running mesh-less, i.e. "
63+
"using the 'grid' parameter.");
64+
5965
// if valid use the user-supplied sampling grid
6066
_grid = getParam<std::vector<int>>("grid");
6167
if (_grid.size() != _dim)
@@ -72,10 +78,24 @@ FFTBufferBase<T>::FFTBufferBase(const InputParameters & parameters)
7278
_grid.push_back(mytrim_mesh->getCellCountInDimension(i));
7379
}
7480

75-
// refine grid by max_h_level powers of two
76-
auto max_h_level = getParam<unsigned int>("max_h_level");
77-
for (auto & count : _grid)
78-
count = count << max_h_level;
81+
// check optional coupled MOOSE variable (only for Real valued buffers)
82+
if (!_moose_variable.empty())
83+
{
84+
if (_moose_variable.size() != _how_many)
85+
paramError("moose_variable", "Variable needs to have ", _how_many, " components.");
86+
87+
for (unsigned i = 0; i < _moose_variable.size(); ++i)
88+
{
89+
// check
90+
auto var = getVar("moose_variable", i);
91+
if (var->order() != 0)
92+
paramError("moose_variable", "Variable needs to have CONSTANT order.");
93+
if (var->isNodal())
94+
paramError("moose_variable", "Variable must be elemental.");
95+
96+
_moose_variable[i] = &coupledValue("moose_variable");
97+
}
98+
}
7999

80100
// get mesh extents and calculate space required and estimate spectrum bins
81101
for (unsigned int i = 0; i < _dim; ++i)
@@ -98,6 +118,41 @@ FFTBufferBase<T>::FFTBufferBase(const InputParameters & parameters)
98118
istride /= sizeof(Real);
99119
}
100120

121+
template <>
122+
void
123+
FFTBufferBase<Real>::execute()
124+
{
125+
// get grid / buffer location
126+
Point centroid = _current_elem->centroid();
127+
std::size_t a = 0;
128+
for (unsigned int i = 0; i < _dim; ++i)
129+
a = a * _grid[i] + std::floor(((centroid(i) - _min_corner(i)) * _grid[i]) / _box_size(i));
130+
131+
// copy solution value from the variables into the buffer
132+
for (unsigned i = 0; i < _moose_variable.size(); ++i)
133+
_start[a * _how_many + i] = (*_moose_variable[i])[0];
134+
}
135+
136+
template <typename T>
137+
const T &
138+
FFTBufferBase<T>::operator()(const Point & p) const
139+
{
140+
std::size_t a = 0;
141+
for (unsigned int i = 0; i < _dim; ++i)
142+
a = a * _grid[i] + std::floor(((p(i) - _min_corner(i)) * _grid[i]) / _box_size(i));
143+
return _buffer[a];
144+
}
145+
146+
template <typename T>
147+
T &
148+
FFTBufferBase<T>::operator()(const Point & p)
149+
{
150+
std::size_t a = 0;
151+
for (unsigned int i = 0; i < _dim; ++i)
152+
a = a * _grid[i] + std::floor(((p(i) - _min_corner(i)) * _grid[i]) / _box_size(i));
153+
return _buffer[a];
154+
}
155+
101156
template <>
102157
Real *
103158
FFTBufferBase<Real>::start(std::size_t i)

0 commit comments

Comments
 (0)