https://mooseframework.inl.gov
UserObjectInterface.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 "UserObjectInterface.h"
11 
12 // MOOSE includes
13 #include "FEProblemBase.h"
16 #include "MooseObject.h"
17 
20 {
21  return emptyInputParameters();
22 }
23 
25  : _uoi_moose_object(*moose_object),
26  _uoi_feproblem(*_uoi_moose_object.parameters().getCheckedPointerParam<FEProblemBase *>(
27  "_fe_problem_base")),
28  _uoi_tid(_uoi_moose_object.parameters().have_parameter<THREAD_ID>("_tid")
29  ? _uoi_moose_object.parameters().get<THREAD_ID>("_tid")
30  : 0)
31 {
32 }
33 
34 #ifdef MOOSE_KOKKOS_ENABLED
37  : _uoi_moose_object(object._uoi_moose_object),
38  _uoi_feproblem(object._uoi_feproblem),
39  _uoi_tid(object._uoi_tid)
40 {
41 }
42 #endif
43 
44 UserObjectName
45 UserObjectInterface::getUserObjectName(const std::string & param_name) const
46 {
47  const auto & params = _uoi_moose_object.parameters();
48 
49  if (!params.isParamValid(param_name))
50  _uoi_moose_object.mooseError("Failed to get a parameter with the name \"",
51  param_name,
52  "\" when getting a UserObjectName.",
53  "\n\nKnown parameters:\n",
55 
56  // Other interfaces will use this interface (PostprocessorInterface, VectorPostprocessorInterface)
57  // to grab UOs with a specialized name, so we need to check them all
58  UserObjectName name;
59  if (params.isType<UserObjectName>(param_name))
60  name = params.get<UserObjectName>(param_name);
61  else if (params.isType<PostprocessorName>(param_name))
62  name = params.get<PostprocessorName>(param_name);
63  else if (params.isType<VectorPostprocessorName>(param_name))
64  name = params.get<VectorPostprocessorName>(param_name);
65  else if (params.isType<std::string>(param_name))
66  name = params.get<std::string>(param_name);
67  else
69  param_name,
70  "Parameter of type \"",
71  params.type(param_name),
72  "\" is not an expected type for getting the name of a UserObject.");
73 
74  return name;
75 }
76 
77 bool
78 UserObjectInterface::hasUserObject(const std::string & param_name) const
79 {
80  return hasUserObjectByName(getUserObjectName(param_name));
81 }
82 
83 bool
84 UserObjectInterface::hasUserObjectByName(const UserObjectName & object_name) const
85 {
86  bool flag = _uoi_feproblem.hasUserObject(object_name);
87 
88 #ifdef MOOSE_KOKKOS_ENABLED
89  flag = flag || _uoi_feproblem.hasKokkosUserObject(object_name);
90 #endif
91 
92  return flag;
93 }
94 
95 const UserObjectBase &
96 UserObjectInterface::getUserObjectFromFEProblem(const UserObjectName & object_name,
97  const THREAD_ID tid) const
98 {
99  const UserObjectBase * uo = nullptr;
100 
101  if (_uoi_feproblem.hasUserObject(object_name))
102  uo = &_uoi_feproblem.getUserObjectBase(object_name, tid);
103 
104 #ifdef MOOSE_KOKKOS_ENABLED
105  if (_uoi_feproblem.hasKokkosUserObject(object_name))
107 #endif
108 
109  mooseAssert(uo, "getUserObjectFromFEProblem() could not find user object");
110 
111  return *uo;
112 }
113 
114 const UserObjectBase &
115 UserObjectInterface::getUserObjectBase(const std::string & param_name,
116  const bool is_dependency) const
117 {
118  const auto object_name = getUserObjectName(param_name);
119  if (!hasUserObjectByName(object_name))
121  param_name, "The requested UserObject with the name \"", object_name, "\" was not found.");
122 
123  return getUserObjectBaseByName(object_name, is_dependency);
124 }
125 
126 const UserObjectBase &
127 UserObjectInterface::getUserObjectBaseByName(const UserObjectName & object_name,
128  const bool is_dependency) const
129 {
130  if (!hasUserObjectByName(object_name))
132  "The requested UserObject with the name \"", object_name, "\" was not found.");
133 
134  const auto & uo_base_tid0 = getUserObjectFromFEProblem(object_name);
135  if (is_dependency)
136  addUserObjectDependencyHelper(uo_base_tid0);
137 
138  const THREAD_ID tid = uo_base_tid0.needThreadedCopy() ? _uoi_tid : 0;
139  return getUserObjectFromFEProblem(object_name, tid);
140 }
141 
142 const std::string &
144 {
145  return uo.type();
146 }
147 
148 const std::string &
150 {
151  return uo.name();
152 }
153 
154 void
155 UserObjectInterface::mooseObjectError(const std::string & param_name, std::stringstream & oss) const
156 {
157  if (_uoi_moose_object.parameters().isParamValid(param_name))
158  _uoi_moose_object.paramError(param_name, oss.str());
159  else
160  _uoi_moose_object.mooseError(oss.str());
161 }
std::string name(const ElemQuality q)
const std::string & userObjectName(const UserObjectBase &uo) const
Gets a UserObject&#39;s name; avoids including UserObject.h in the UserObjectInterface.
UserObjectName getUserObjectName(const std::string &param_name) const
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition: MooseBase.h:467
const MooseObject & _uoi_moose_object
Moose object using the interface.
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
virtual void addUserObjectDependencyHelper(const UserObjectBase &) const
Helper for deriving classes to override to add dependencies when a UserObject is requested.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
bool hasKokkosUserObject(const std::string &name) const
Check if there if a Kokkos user object of given name.
bool hasUserObject(const std::string &name) const
Check if there if a user object of given name.
static InputParameters validParams()
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
InputParameters emptyInputParameters()
const FEProblemBase & _uoi_feproblem
Reference to the FEProblemBase instance.
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
const T & getKokkosUserObject(const std::string &name) const
Get the Kokkos user object by its name.
const UserObjectBase & getUserObjectBaseByName(const UserObjectName &object_name, bool is_dependency=true) const
Get an user object with the name object_name.
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:93
const THREAD_ID _uoi_tid
Thread ID.
Interface for objects that need to use UserObjects.
void mooseObjectError(const std::string &param_name, std::stringstream &oss) const
emit an error for the given parameter
const UserObjectBase & getUserObjectFromFEProblem(const UserObjectName &object_name, const THREAD_ID tid=0) const
Go directly to the FEProblem for the requested object_name for thread ID tid.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:281
const UserObject & getUserObjectBase(const std::string &name, const THREAD_ID tid=0) const
Get the user object by its name.
bool hasUserObjectByName(const UserObjectName &object_name) const
bool hasUserObject(const std::string &param_name) const
const std::string & userObjectType(const UserObjectBase &uo) const
Gets a UserObject&#39;s type; avoids including UserObject.h in the UserObjectInterface.
const UserObjectBase & getUserObjectBase(const std::string &param_name, bool is_dependency=true) const
Get an user object with a given parameter param_name.
const Elem & get(const ElemType type_in)
unsigned int THREAD_ID
Definition: MooseTypes.h:237
UserObjectInterface(const MooseObject *moose_object)
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.