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 "LatticeBoltzmannProblem.h"
10 : #include "LatticeBoltzmannStencilBase.h"
11 :
12 : #include "TensorSolver.h"
13 : #include "TensorOperatorBase.h"
14 : #include "TensorOutput.h"
15 : #include "DomainAction.h"
16 :
17 : #include "SwiftUtils.h"
18 : #include "DependencyResolverInterface.h"
19 :
20 : registerMooseObject("SwiftApp", LatticeBoltzmannProblem);
21 :
22 : InputParameters
23 0 : LatticeBoltzmannProblem::validParams()
24 : {
25 0 : InputParameters params = TensorProblem::validParams();
26 0 : params.addParam<TensorInputBufferName>("binary_media",
27 : "The tensor buffer object containing binary media/mesh");
28 0 : params.addParam<bool>("enable_slip", false, "Enable slip model");
29 : // params.addParam<Real>("mfp", 0.0, "Mean free path of the system, (meters)");
30 : // params.addParam<Real>("dx", 0.0, "Domain resolution, (meters)");
31 0 : params.addParam<unsigned int>("substeps", 1, "Number of LBM iterations for every MOOSE timestep");
32 0 : params.addParam<Real>("tolerance", 1.0e-15, "LBM convergence tolerance");
33 0 : params.addClassDescription("Problem object to enable solving lattice Boltzmann problems");
34 :
35 0 : return params;
36 0 : }
37 :
38 0 : LatticeBoltzmannProblem::LatticeBoltzmannProblem(const InputParameters & parameters)
39 : : TensorProblem(parameters),
40 0 : _is_binary_media(isParamValid("binary_media")),
41 0 : _enable_slip(getParam<bool>("enable_slip")),
42 : /*_mfp(getParam<Real>("mfp")),
43 : _dx(getParam<Real>("dx")),*/
44 0 : _lbm_substeps(getParam<unsigned int>("substeps")),
45 0 : _tolerance(getParam<Real>("tolerance"))
46 : {
47 : // fix sizes
48 0 : std::vector<int64_t> shape(_domain.getShape().begin(), _domain.getShape().end());
49 0 : if (_domain.getDim() < 3)
50 0 : shape.push_back(1);
51 :
52 0 : for (const auto i : index_range(shape))
53 : {
54 0 : _shape_extended.push_back(shape[i]);
55 0 : _shape_extended_to_q.push_back(shape[i]);
56 : }
57 0 : }
58 :
59 : void
60 0 : LatticeBoltzmannProblem::init()
61 : {
62 0 : TensorProblem::init();
63 :
64 : // dependency resolution of boundary conditions
65 0 : DependencyResolverInterface::sort(_bcs);
66 :
67 : // binary mesh if provided
68 0 : if (_is_binary_media)
69 0 : _binary_media = getBuffer(getParam<TensorInputBufferName>("binary_media"));
70 : else
71 0 : _binary_media = torch::ones(_shape, MooseTensor::intTensorOptions());
72 0 : }
73 :
74 : void
75 0 : LatticeBoltzmannProblem::execute(const ExecFlagType & exec_type)
76 : {
77 0 : if (_convergence_residual < _tolerance)
78 : return;
79 :
80 0 : if (exec_type == EXEC_INITIAL)
81 : {
82 : // check for constants
83 0 : if (_fetched_constants.size() == 1)
84 0 : mooseError(
85 0 : "Constant ", Moose::stringify(_fetched_constants), " was requested but never declared.");
86 0 : if (_fetched_constants.size() > 1)
87 0 : mooseError("Constants ",
88 0 : Moose::stringify(_fetched_constants),
89 : " were requested but never declared.");
90 0 : _can_fetch_constants = false;
91 :
92 : // update time
93 0 : _sub_time = FEProblem::time();
94 :
95 0 : executeTensorInitialConditions();
96 :
97 0 : executeTensorOutputs(EXEC_INITIAL);
98 : }
99 :
100 0 : if (exec_type == EXEC_TIMESTEP_BEGIN && timeStep() > 1)
101 0 : for (unsigned substep = 0; substep < _lbm_substeps; substep++)
102 : {
103 : // create old state buffers
104 0 : advanceState();
105 :
106 : // run solver for streaming
107 0 : if (_solver)
108 0 : _solver->computeBuffer();
109 :
110 : // run bcs
111 0 : for (auto & bc : _bcs)
112 0 : bc->computeBuffer();
113 :
114 : // run computes
115 0 : for (auto & cmp : _computes)
116 0 : cmp->computeBuffer();
117 0 : _console << COLOR_WHITE << "Lattice Boltzmann Substep " << substep << ", Residual "
118 0 : << _convergence_residual << COLOR_DEFAULT << std::endl;
119 :
120 0 : _t_total++;
121 : }
122 :
123 0 : if (exec_type == EXEC_TIMESTEP_END)
124 0 : executeTensorOutputs(EXEC_TIMESTEP_END);
125 :
126 : // mapBuffersToAux();
127 0 : FEProblem::execute(exec_type);
128 : }
129 :
130 : void
131 0 : LatticeBoltzmannProblem::addTensorBoundaryCondition(const std::string & compute_type,
132 : const std::string & name,
133 : InputParameters & parameters)
134 : {
135 0 : addTensorCompute(compute_type, name, parameters, _bcs);
136 0 : }
137 :
138 : void
139 0 : LatticeBoltzmannProblem::addStencil(const std::string & stencil_name,
140 : const std::string & name,
141 : InputParameters & parameters)
142 : {
143 0 : if (_stencil_counter > 0)
144 0 : mooseError("Problem object LatticeBoltzmannProblem can only have one stencil");
145 : // Create the object
146 0 : _stencil = _factory.create<LatticeBoltzmannStencilBase>(stencil_name, name, parameters, 0);
147 0 : _stencil_counter++;
148 0 : logAdd("LatticeBoltzmannStencilBase", name, stencil_name, parameters);
149 :
150 0 : _shape_extended_to_q.push_back(_stencil->_q);
151 0 : }
152 :
153 : void
154 0 : LatticeBoltzmannProblem::maskedFillSolids(torch::Tensor & t, const Real & value)
155 : {
156 : const auto tensor_shape = t.sizes();
157 0 : if (_is_binary_media)
158 : {
159 0 : if (t.dim() == _binary_media.dim())
160 : {
161 : // 3D
162 0 : const auto solid_mask = (_binary_media == value);
163 0 : t.masked_fill_(solid_mask, value);
164 : }
165 : else
166 : {
167 : // 2D and 1D
168 0 : const auto solid_mask = (_binary_media == value).unsqueeze(-1).expand(tensor_shape);
169 0 : t.masked_fill_(solid_mask, value);
170 : }
171 : }
172 0 : }
|