https://mooseframework.inl.gov
LazyCoupleable.C
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 #include "LazyCoupleable.h"
11 #include "FEProblem.h"
12 #include "MooseVariable.h"
13 #include "InputParameters.h"
14 #include "MooseObject.h"
15 #include "MooseApp.h"
16 #include "Executioner.h"
17 
18 #include "libmesh/simple_range.h"
19 
21  : _l_parameters(moose_object->parameters()),
22  _l_name(_l_parameters.get<std::string>("_object_name")),
23  _l_fe_problem(nullptr),
24  _l_app(moose_object->getMooseApp())
25 {
26  for (const auto & var_name :
28  _coupled_var_numbers[var_name] = std::make_unique<unsigned int>(0);
29 }
30 
31 void
33 {
34  _l_fe_problem = fe_problem;
35  init();
36 }
37 
38 void
40 {
41  for (const auto & var_pair : _coupled_var_numbers)
42  {
43  if (!_l_fe_problem->hasVariable(var_pair.first))
44  mooseError("Unable to find variable ", var_pair.first);
45 
46  auto & moose_var = _l_fe_problem->getVariable(
48  if (moose_var.kind() == Moose::VAR_SOLVER)
49  *(var_pair.second) = moose_var.number();
50  else
51  *(var_pair.second) = std::numeric_limits<unsigned int>::max() - moose_var.number();
52  }
53 }
54 
55 unsigned int &
56 LazyCoupleable::coupled(const std::string & var_name, unsigned int /*comp*/)
57 {
58  if (!_l_fe_problem)
59  {
60  auto executioner_ptr = _l_app.getExecutioner();
61  if (!executioner_ptr)
62  mooseError("Executioner is nullptr in LazyCoupleable. You cannot call the \"coupled\" method "
63  "until the add_algebraic_rm task");
64  setFEProblemPtr(&executioner_ptr->feProblem());
65  }
66 
67  const auto & var_pair = _coupled_var_numbers.find(var_name);
68  mooseAssert(var_pair != _coupled_var_numbers.end(), "Internal error in LazyCoupleable");
69 
70  return *(var_pair->second);
71 }
virtual bool hasVariable(const std::string &var_name) const override
Whether or not this problem has the variable.
std::map< std::string, std::unique_ptr< unsigned int > > _coupled_var_numbers
Coupled vars whose values we provide.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
unsigned int number() const
Get variable number coming from libMesh.
T * get(const std::unique_ptr< T > &u)
The MooseUtils::get() specializations are used to support making forwards-compatible code changes fro...
Definition: MooseUtils.h:1155
std::set< std::string >::const_iterator coupledVarsBegin() const
Methods returning iterators to the coupled variables names stored in this InputParameters object...
const InputParameters & _l_parameters
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
virtual const MooseVariableFieldBase & getVariable(const THREAD_ID tid, const std::string &var_name, Moose::VarKindType expected_var_type=Moose::VarKindType::VAR_ANY, Moose::VarFieldType expected_var_field_type=Moose::VarFieldType::VAR_FIELD_ANY) const override
Returns the variable reference for requested variable which must be of the expected_var_type (Nonline...
auto max(const L &left, const R &right)
unsigned int & coupled(const std::string &var_name, unsigned int comp=0)
Returns the index for a coupled variable by name.
std::set< std::string >::const_iterator coupledVarsEnd() const
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
MooseApp & _l_app
Reference to the MooseApp.
FEProblemBase * _l_fe_problem
Reference to FEProblemBase.
Executioner * getExecutioner() const
Retrieve the Executioner for this App.
Definition: MooseApp.C:2085
LazyCoupleable(const MooseObject *moose_object)
Constructing the object.
void setFEProblemPtr(FEProblemBase *fe_problem)
Sets the FEProblem pointer which can (and is expected) to happen long after this interface is constru...