www.mooseframework.org
JvarMapInterface.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 #include "MooseVariableFE.h"
13 #include "NonlinearSystemBase.h"
14 #include "Enumerate.h"
15 
16 template <class T>
18 
29 template <class T>
31 {
32 public:
33  JvarMapKernelInterface(const InputParameters & parameters);
34  virtual void computeOffDiagJacobian(unsigned int jvar) override;
35 };
36 
47 template <class T>
49 {
50 public:
52  virtual void computeOffDiagJacobian(unsigned int jvar) override;
53 };
54 
59 template <class T>
60 class JvarMapInterfaceBase : public T
61 {
62 public:
63  typedef std::vector<int> JvarMap;
64 
66 
67  JvarMapInterfaceBase(const InputParameters & parameters);
68 
70  unsigned int mapJvarToCvar(unsigned int jvar);
71 
77  int mapJvarToCvar(unsigned int jvar, const JvarMap & jvar_map);
78 
80  const JvarMap & getJvarMap() { return _jvar_map; }
81 
83  const JvarMap & getParameterJvarMap(std::string parameter_name);
84 
93  bool mapJvarToCvar(unsigned int jvar, unsigned int & cvar);
94 
95 protected:
97  const unsigned int _n_args;
98 
99 private:
101  const std::size_t _jvar_max_size;
102 
105 
107  std::map<std::string, JvarMap> _jvar_local_map;
108 
109  friend class JvarMapKernelInterface<T>;
111 };
112 
113 template <class T>
116 {
117  auto params = T::validParams();
118  params.addCoupledVar("args", "Vector of nonlinear variable arguments this object depends on");
119  params.deprecateCoupledVar("args", "coupled_variables", "02/07/2024");
120 
121  return params;
122 }
123 
124 template <class T>
126  : T(parameters),
127  _n_args(this->_coupled_standard_moose_vars.size()),
128  _jvar_max_size(this->_sys.nVariables()),
130 {
131  // populate map
132  for (auto it : Moose::enumerate(this->_coupled_moose_vars))
133  {
134  auto number = it.value()->number();
135 
136  // skip AuxVars as off-diagonal jacobian entries are not calculated for them
137  if (number < _jvar_max_size)
138  _jvar_map[number] = it.index();
139  }
140 
141  // mark the kernel variable for the check in computeOffDiagJacobian
142  _jvar_map[this->_var.number()] = 0;
143 }
144 
145 template <class T>
146 unsigned int
148 {
149  mooseAssert(jvar < _jvar_max_size,
150  "Calling mapJvarToCvar for an invalid Moose variable number. Maybe an AuxVariable?");
151  int cit = _jvar_map[jvar];
152 
153  mooseAssert(cit >= 0, "Calling mapJvarToCvar for a variable not coupled to this kernel.");
154  return cit;
155 }
156 
157 template <class T>
158 int
159 JvarMapInterfaceBase<T>::mapJvarToCvar(unsigned int jvar, const JvarMap & jvar_map)
160 {
161  mooseAssert(jvar < _jvar_max_size,
162  "Calling mapJvarToCvar for an invalid Moose variable number. Maybe an AuxVariable?");
163  return jvar_map[jvar];
164 }
165 
166 template <class T>
167 const typename JvarMapInterfaceBase<T>::JvarMap &
169 {
170  auto & jvar_map = _jvar_local_map[parameter_name];
171  jvar_map.assign(_jvar_max_size, -1);
172 
173  // populate local map
174  const auto num = this->coupledComponents(parameter_name);
175  for (std::size_t i = 0; i < num; ++i)
176  {
177  const auto number = this->getVar(parameter_name, i)->number();
178 
179  // skip AuxVars as off-diagonal jacobian entries are not calculated for them
180  if (number < _jvar_max_size)
181  jvar_map[number] = i;
182  }
183 
184  return jvar_map;
185 }
186 
187 template <class T>
189  : JvarMapInterfaceBase<T>(parameters)
190 {
191 }
192 
193 template <class T>
195  : JvarMapInterfaceBase<T>(parameters)
196 {
197 }
198 
199 template <class T>
200 void
202 {
203  // the Kernel is not coupled to the variable; no need to loop over QPs
204  if (this->_jvar_map[jvar] < 0)
205  return;
206 
207  // call the underlying class' off-diagonal Jacobian
208  T::computeOffDiagJacobian(jvar);
209 }
210 
211 template <class T>
212 void
214 {
215  // the Kernel is not coupled to the variable; no need to loop over QPs
216  if (this->_jvar_map[jvar] < 0)
217  return;
218 
219  // call the underlying class' off-diagonal Jacobian
220  T::computeOffDiagJacobian(jvar);
221 }
JvarMapIntegratedBCInterface(const InputParameters &parameters)
virtual void computeOffDiagJacobian(unsigned int jvar) override
JvarMapInterfaceBase(const InputParameters &parameters)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
_enumerate_range< Iterator > enumerate(Iterator first, Iterator last, typename std::iterator_traits< Iterator >::difference_type initial)
Enumerate function for iterating over a range and obtaining both a reference to the underlying type a...
Definition: Enumerate.h:52
Interface class ("Veneer") for Kernel to provide a mapping from &#39;jvar&#39; in computeQpOffDiagJacobian in...
InputParameters validParams()
virtual void computeOffDiagJacobian(unsigned int jvar) override
const std::size_t _jvar_max_size
number of nonlinear variables in the system
static InputParameters validParams()
const JvarMap & getJvarMap()
Obtain the map connecting libmesh variable ID number to its position in the _coupled_moose_vars vecto...
std::map< std::string, JvarMap > _jvar_local_map
map of local look-up tables for specific parameters
unsigned int mapJvarToCvar(unsigned int jvar)
Return index into the _coupled_moose_vars array for a given jvar.
Base class ("Veneer") that implements the actual mapping from &#39;jvar&#39; in into the _coupled_moose_vars ...
std::vector< int > JvarMap
const JvarMap & getParameterJvarMap(std::string parameter_name)
Make a specific map for a given parameter name representing a couple variable (vector) ...
Interface class ("Veneer") for IntegratedBC to provide a mapping from &#39;jvar&#39; in computeJacobianBlock ...
JvarMapKernelInterface(const InputParameters &parameters)
const unsigned int _n_args
number of coupled moose variables
JvarMap _jvar_map
look-up table to determine the _coupled_moose_vars index for the jvar parameter