https://mooseframework.inl.gov
Loading...
Searching...
No Matches
TorchFEInterpolation.h
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#pragma once
11
12#ifdef MOOSE_LIBTORCH_ENABLED
13
14#include <torch/torch.h>
15
16// system includes
17#include <vector>
18#include <unordered_map>
19
20// libmesh includes
21#include "MooseTypes.h"
22#include "libmesh/fe_type.h"
23#include "libmesh/id_types.h"
24
25// MOOSE includes
26#include "ElementUserObject.h"
27#include "TorchAssembly.h"
28#include "MooseVariableFE.h"
29
37{
38public:
40
42
51 const at::Tensor & getValue(const std::string & var_name);
52
62 const at::Tensor & getGradient(const std::string & var_name);
63
73 const at::Tensor & getPhi(const std::string & var_name);
74
84 const at::Tensor & getPhiGradient(const std::string & var_name);
85
95 const at::Tensor & getDofMap(const std::string & var_name);
96
98 const std::vector<dof_id_type> & getGlobalDofMap(const std::string & var_name);
99
100 int64_t local_ndof() const;
101
104
107
110
111 void initialSetup() override;
112 void meshChanged() override;
113
114 void initialize() override;
115 void execute() override;
116 void finalize() override;
117 void threadJoin(const UserObject &) override;
118
119protected:
120 virtual void syncWithMainThread();
121 virtual void updateFEMContext();
122 virtual void updateDofMap();
123 virtual void updatePhi();
124 virtual void updateGradPhi();
125 virtual void updateInterpolations();
126
129
132
134 bool _interp_up_to_date = false;
135
137 std::unordered_map<std::string, at::Tensor> _vars;
138
140 std::unordered_map<std::string, at::Tensor> _grad_vars;
141
143 std::unordered_map<std::string, const MooseVariableFE<Real> *> _moose_vars;
144
147 std::unordered_map<FEType, int64_t> _ndofe;
148 std::unordered_map<FEType, const VariablePhiValue *> _phis;
149 std::unordered_map<FEType, const VariablePhiGradient *> _grad_phis;
150
151 std::unordered_map<std::string, std::vector<int64_t>> _moose_dof_map;
152 std::unordered_map<std::string, std::vector<dof_id_type>> _moose_dof_map_global;
153 std::unordered_map<FEType, std::vector<Real>> _moose_phi;
154 std::unordered_map<FEType, std::vector<Real>> _moose_grad_phi;
155
156 std::unordered_map<std::string, at::Tensor> _torch_dof_map;
157 std::unordered_map<FEType, at::Tensor> _torch_phi;
158 std::unordered_map<FEType, at::Tensor> _torch_grad_phi;
160
163
164private:
166 const MooseVariableFE<Real> * getMOOSEVariable(const std::string & var_name) const;
167
169 std::vector<dof_id_type> _dof_indices;
170
172 int64_t _local_ndof;
173};
174
175#endif // MOOSE_LIBTORCH_ENABLED
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
const InputParameters & parameters() const
Get the parameters of the object.
Definition MooseBase.h:131
Class for stuff related to variables.
This user object caches assembly information (JxWxT) from MOOSE as a batched at::Tensor for the Torch...
This user object serves as the "interface" for interpolating MOOSE variable values and gradients from...
void invalidateInterpolations()
Invalidate the cached interpolations.
void invalidateFEMContext()
Invalidate the cached FEM context such as dof map, shape functions, etc.
std::unordered_map< FEType, std::vector< Real > > _moose_phi
void initialize() override
Called before execute() is ever called so that data can be cleared.
std::unordered_map< FEType, at::Tensor > _torch_grad_phi
const std::vector< dof_id_type > & getGlobalDofMap(const std::string &var_name)
Similar to getDofMap, but returns the global dof map (as a flattened vector of dof_id_type)
std::unordered_map< std::string, at::Tensor > _vars
coupled variables (by value) requested by other objects
bool _fem_context_up_to_date
Whether the current FEM context is up to date.
int64_t _local_ndof
Number of local dofs (including ghost dofs)
const at::Tensor & getDofMap(const std::string &var_name)
Get the local dof map associated with a MOOSE variable.
static InputParameters validParams()
std::unordered_map< std::string, at::Tensor > _grad_vars
coupled variables (by gradient) requested by other objects
bool contextUpToDate() const
Whether the current FEM context is up to date.
const at::Tensor & getPhiGradient(const std::string &var_name)
Get the shape function gradient associated with a MOOSE variable.
virtual void updateInterpolations()
std::unordered_map< std::string, const MooseVariableFE< Real > * > _moose_vars
moose variables that have been coupled
std::unordered_map< FEType, const VariablePhiGradient * > _grad_phis
std::vector< dof_id_type > _dof_indices
Helper vector to store local dof indices.
void meshChanged() override
Called on this object when the mesh changes.
const at::Tensor & getGradient(const std::string &var_name)
Get the variable gradient of a MOOSE nonlinear variable converted to a libtorch tensor.
const TorchAssembly & _assembly
Assembly.
void threadJoin(const UserObject &) override
Must override.
std::unordered_map< FEType, int64_t > _ndofe
cached information on the requested function spaces
void execute() override
Execute method.
void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job.
std::unordered_map< std::string, std::vector< int64_t > > _moose_dof_map
void finalize() override
Finalize.
std::unordered_map< FEType, std::vector< Real > > _moose_grad_phi
std::unordered_map< std::string, std::vector< dof_id_type > > _moose_dof_map_global
bool _interp_up_to_date
Whether the current interpolations are up to date.
std::unordered_map< std::string, at::Tensor > _torch_dof_map
const libMesh::PetscVector< Real > * _petsc_solution
PETSc solution vector.
std::unordered_map< FEType, at::Tensor > _torch_phi
const at::Tensor & getValue(const std::string &var_name)
Get the variable value of a MOOSE nonlinear variable converted to a libtorch tensor.
const at::Tensor & getPhi(const std::string &var_name)
Get the shape function associated with a MOOSE variable.
const MooseVariableFE< Real > * getMOOSEVariable(const std::string &var_name) const
Helper to get the MOOSE variable and check for common restrictions.
virtual void syncWithMainThread()
std::unordered_map< FEType, const VariablePhiValue * > _phis
Base class for user-specific data.
Definition UserObject.h:20