Line data Source code
1 : /**********************************************************************/ 2 : /* DO NOT MODIFY THIS HEADER */ 3 : /* Swift, a Fourier spectral solver for MOOSE */ 4 : /* */ 5 : /* Copyright 2024 Battelle Energy Alliance, LLC */ 6 : /* ALL RIGHTS RESERVED */ 7 : /**********************************************************************/ 8 : 9 : #include "MooseFunctionTensor.h" 10 : #include "Function.h" 11 : #include "SwiftUtils.h" 12 : #include "TensorProblem.h" 13 : 14 : registerMooseObject("SwiftApp", MooseFunctionTensor); 15 : 16 : InputParameters 17 8 : MooseFunctionTensor::validParams() 18 : { 19 8 : InputParameters params = TensorOperator::validParams(); 20 8 : params.addClassDescription("Map a MooseFunction to a tensor."); 21 16 : params.addRequiredParam<FunctionName>("function", "Function to map."); 22 : // params.addParam<bool>("reciprocal", false, "Construct a reciprocal buffer"); 23 8 : return params; 24 0 : } 25 : 26 4 : MooseFunctionTensor::MooseFunctionTensor(const InputParameters & parameters) 27 4 : : TensorOperator(parameters), FunctionInterface(this), _func(getFunction("function")) 28 : { 29 4 : } 30 : 31 : void 32 4 : MooseFunctionTensor::computeBuffer() 33 : { 34 4 : auto buffer = torch::zeros(_tensor_problem.getShape(), torch::kDouble); 35 : 36 4 : const auto & n = _domain.getGridSize(); 37 : const auto & dx = _domain.getGridSpacing(); 38 : 39 4 : switch (_domain.getDim()) 40 : { 41 : { 42 0 : case 1: 43 0 : auto b = buffer.accessor<double, 1>(); 44 0 : for (const auto i : make_range(n[0])) 45 0 : b[i] = _func.value(0, Point(i * dx(0) + dx(0) / 2.0, 0.0, 0.0)); 46 : break; 47 : } 48 4 : case 2: 49 : { 50 4 : auto b = buffer.accessor<double, 2>(); 51 84 : for (const auto j : make_range(n[1])) 52 1680 : for (const auto i : make_range(n[0])) 53 1600 : b[i][j] = _func.value(0, Point(i * dx(0) + dx(0) / 2.0, j * dx(1) + dx(1) / 2.0, 0.0)); 54 : break; 55 : } 56 0 : case 3: 57 : { 58 0 : auto b = buffer.accessor<double, 3>(); 59 0 : for (const auto k : make_range(n[2])) 60 0 : for (const auto j : make_range(n[1])) 61 0 : for (const auto i : make_range(n[0])) 62 0 : b[i][j][k] = _func.value( 63 : 0, 64 0 : Point(i * dx(0) + dx(0) / 2.0, j * dx(1) + dx(1) / 2.0, k * dx(2) + dx(2) / 2.0)); 65 : break; 66 : } 67 0 : default: 68 0 : mooseError("Unsupported dimension"); 69 : } 70 : 71 8 : _u = buffer.to(MooseTensor::floatTensorOptions()); 72 4 : }