Skip to content

Commit

Permalink
Add tests (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen committed Feb 5, 2025
1 parent c8e5a26 commit c6c435c
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/tensor_computes/ParsedCompute.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ class ParsedCompute : public TensorOperator
ParsedJITTensor _jit;
ParsedTensor _no_jit;

torch::Tensor _time_tensor;
std::vector<const torch::Tensor *> _params;
};
3 changes: 3 additions & 0 deletions include/tensor_computes/TensorOperatorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@ class TensorOperatorBase : public MooseObject, public DependencyResolverInterfac

/// reciprocal axes
const torch::Tensor &_i, &_j, &_k;

/// substep time
const Real & _time;
};
16 changes: 11 additions & 5 deletions src/tensor_computes/ParsedCompute.C
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ParsedCompute::validParams()
false,
"Provide i (imaginary unit), kx,ky,kz (reciprocal space frequency), k2 "
"(square of the k-vector), x,y,z "
"(real space coordinates), and pi,e.");
"(real space coordinates), time t, pi, and e.");
// Constants and their values
params.addParam<std::vector<std::string>>(
"constant_names",
Expand Down Expand Up @@ -69,7 +69,7 @@ ParsedCompute::ParsedCompute(const InputParameters & parameters)
_params.push_back(&getInputBufferByName(name));

static const std::vector<std::string> reserved_symbols = {
"i", "x", "kx", "y", "ky", "z", "kz", "k2"};
"i", "x", "kx", "y", "ky", "z", "kz", "k2", "t"};

// helper function to check if the name given is one of the reserved_names
auto isReservedName = [this](const auto & name)
Expand Down Expand Up @@ -108,7 +108,8 @@ ParsedCompute::ParsedCompute(const InputParameters & parameters)
// append extra symbols
variables_vec.insert(variables_vec.end(), reserved_symbols.begin(), reserved_symbols.end());

_constant_tensors.push_back(torch::tensor(c10::complex<double>(0.0, 1.0)));
_constant_tensors.push_back(
torch::tensor(c10::complex<double>(0.0, 1.0), MooseTensor::complexFloatTensorOptions()));
_params.push_back(&_constant_tensors[0]);

for (const auto dim : make_range(3u))
Expand All @@ -118,6 +119,7 @@ ParsedCompute::ParsedCompute(const InputParameters & parameters)
}

_params.push_back(&_domain.getKSquare());
_params.push_back(&_time_tensor);

fp.AddConstant("pi", libMesh::pi);
fp.AddConstant("e", std::exp(Real(1.0)));
Expand Down Expand Up @@ -185,8 +187,12 @@ ParsedCompute::ParsedCompute(const InputParameters & parameters)
void
ParsedCompute::computeBuffer()
{
if (_extra_symbols)
_time_tensor = torch::tensor(_time, MooseTensor::floatTensorOptions());

// use local shape if we add parallel support, and add option for reciprocal shape
if (_use_jit)
_u = _jit.Eval(_params);
_u = _jit.Eval(_params).expand(_domain.getShape());
else
_u = _no_jit.Eval(_params);
_u = _no_jit.Eval(_params).expand(_domain.getShape());
}
3 changes: 2 additions & 1 deletion src/tensor_computes/TensorOperatorBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ TensorOperatorBase::TensorOperatorBase(const InputParameters & parameters)
_z(_domain.getAxis(2)),
_i(_domain.getReciprocalAxis(0)),
_j(_domain.getReciprocalAxis(1)),
_k(_domain.getReciprocalAxis(2))
_k(_domain.getReciprocalAxis(2)),
_time(_tensor_problem.subTime())
{
}

Expand Down
12 changes: 12 additions & 0 deletions test/tests/postprocessors/gold/interface_velocity_out.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
time,v
0,0
0.01,0
0.02,0.2006196179713
0.03,0.2006238853419
0.04,0.20062820630563
0.05,0.20063258191329
0.06,0.20063701324401
0.07,0.200641501405
0.08,0.20064604753323
0.09,0.20065065279606
0.1,0.20065531839251
45 changes: 45 additions & 0 deletions test/tests/postprocessors/interface_velocity.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[Domain]
dim = 2
nx = 10
ny = 2
xmax = ${fparse pi*4}
mesh_mode = DUMMY
device_names = cpu
[]

[TensorBuffers]
[c]
[]
[]

[TensorComputes]
[Solve]
[c]
type = ParsedCompute
buffer = c
extra_symbols = true
expression = sin(x+0.2*t)
[]
[]
[]

[Postprocessors]
[v]
type = TensorInterfaceVelocityPostprocessor
buffer = c
[]
[]

[Problem]
type = TensorProblem
[]

[Executioner]
type = Transient
num_steps = 10
dt = 0.01
[]

[Outputs]
csv = true
[]
9 changes: 9 additions & 0 deletions test/tests/postprocessors/tests
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@
issues = '#6'
design = 'ReciprocalIntegral.md'
[]

[interface_velocity]
type = CSVDiff
input = interface_velocity.i
csvdiff = interface_velocity_out.csv
requirement = 'The system shall be able to measure the maximum interfacial velocity for a given tensor.'
issues = '#6'
design = 'TensorInterfaceVelocityPostprocessor.md'
[]
[]

0 comments on commit c6c435c

Please sign in to comment.