www.mooseframework.org
ShapeUserObject.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 "Assembly.h"
13 #include "Coupleable.h"
14 #include "InputParameters.h"
15 #include "MooseVariableFE.h"
16 #include "MooseObject.h"
17 
26 enum class ShapeType
27 {
28  Element,
29  Side
30 };
31 
40 template <typename T>
41 class ShapeUserObject : public T
42 {
43 public:
45 
46  ShapeUserObject(const InputParameters & parameters, ShapeType type);
47 
49  const bool & computeJacobianFlag() const { return _compute_jacobians; }
50 
54  const std::set<const MooseVariableFEBase *> & jacobianMooseVariables() const
55  {
57  }
58 
63  virtual void executeJacobianWrapper(unsigned int jvar,
64  const std::vector<dof_id_type> & dof_indices);
65 
66 protected:
72  virtual void executeJacobian(unsigned int /*jvar*/) = 0;
73 
79  virtual unsigned int coupled(const std::string & var_name, unsigned int comp = 0) const override;
80 
83 
86 
88  unsigned int _j;
89 
92 
93 private:
94  const bool _compute_jacobians;
95  mutable std::set<const MooseVariableFEBase *> _jacobian_moose_variables;
96 };
97 
98 template <typename T>
100  : T(parameters),
101  _phi(type == ShapeType::Element ? this->_assembly.phi() : this->_assembly.phiFace()),
102  _grad_phi(type == ShapeType::Element ? this->_assembly.gradPhi()
103  : this->_assembly.gradPhiFace()),
104  _compute_jacobians(MooseObject::getParam<bool>("compute_jacobians"))
105 {
106  mooseWarning("Jacobian calculation in UserObjects is an experimental capability with a "
107  "potentially unstable interface.");
108 }
109 
110 template <typename T>
113 {
115  params.addParam<bool>("compute_jacobians", true, "Compute Jacobians for coupled variables");
116  params.addParamNamesToGroup("compute_jacobians", "Advanced");
117  return params;
118 }
119 
120 template <typename T>
121 unsigned int
122 ShapeUserObject<T>::coupled(const std::string & var_name, unsigned int comp) const
123 {
124  const auto * var = this->template getVarHelper<MooseVariable>(var_name, comp);
125 
126  // add to the set of variables for which executeJacobian will be called
127  if (_compute_jacobians && var->kind() == Moose::VAR_SOLVER)
128  _jacobian_moose_variables.insert(var);
129 
130  // return the variable number
131  return T::coupled(var_name, comp);
132 }
133 
134 template <typename T>
135 void
137  const std::vector<dof_id_type> & dof_indices)
138 {
139  for (_j = 0; _j < _phi.size(); ++_j)
140  {
141  _j_global = dof_indices[_j];
142  executeJacobian(jvar);
143  }
144 }
const bool _compute_jacobians
static InputParameters validParams()
dof_id_type _j_global
global DOF ID corresponding to _j
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:333
virtual unsigned int coupled(const std::string &var_name, unsigned int comp=0) const override
Returns the index for a coupled variable by name and requests the computation of a Jacobian w...
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
OutputTools< Real >::VariablePhiValue VariablePhiValue
Definition: MooseTypes.h:307
InputParameters emptyInputParameters()
unsigned int _j
j-th index for enumerating the shape functions
std::set< const MooseVariableFEBase * > _jacobian_moose_variables
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:33
ShapeType
Users of this template class must specify the type of shape functions that will be used in the Jacobi...
UserObject template class in which the _phi and _grad_phi shape function data is available and correc...
virtual void executeJacobian(unsigned int)=0
Implement this function to compute Jacobian terms for this UserObject.
virtual void executeJacobianWrapper(unsigned int jvar, const std::vector< dof_id_type > &dof_indices)
This function will be called with the shape functions for jvar initialized.
OutputTools< Real >::VariablePhiGradient VariablePhiGradient
Definition: MooseTypes.h:308
const std::set< const MooseVariableFEBase * > & jacobianMooseVariables() const
Returns the set of variables a Jacobian has been requested for.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
const VariablePhiGradient & _grad_phi
shape function gradients
const VariablePhiValue & _phi
shape function values
const bool & computeJacobianFlag() const
check if jacobian is to be computed in user objects
ShapeUserObject(const InputParameters &parameters, ShapeType type)
uint8_t dof_id_type
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...