https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
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
44UserObjectName
45UserObjectInterface::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
77bool
78UserObjectInterface::hasUserObject(const std::string & param_name) const
79{
80 return hasUserObjectByName(getUserObjectName(param_name));
81}
82
83bool
84UserObjectInterface::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
95const UserObjectBase &
96UserObjectInterface::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
114const UserObjectBase &
115UserObjectInterface::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
126const UserObjectBase &
127UserObjectInterface::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
142const std::string &
144{
145 return uo.type();
146}
147
148const std::string &
150{
151 return uo.name();
152}
153
154void
155UserObjectInterface::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}
InputParameters emptyInputParameters()
unsigned int THREAD_ID
Definition MooseTypes.h:237
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
bool hasKokkosUserObject(const std::string &name) const
Check if there if a Kokkos user object of given name.
const T & getKokkosUserObject(const std::string &name) const
Get the Kokkos user object by its name.
bool hasUserObject(const std::string &name) const
Check if there if a user object of given name.
const UserObject & getUserObjectBase(const std::string &name, const THREAD_ID tid=0) const
Get the user object by its name.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another,...
const InputParameters & parameters() const
Get the parameters of the object.
Definition MooseBase.h:131
const std::string & type() const
Get the type of this class.
Definition MooseBase.h:93
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
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:457
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:271
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
Interface for objects that need to use UserObjects.
virtual void addUserObjectDependencyHelper(const UserObjectBase &) const
Helper for deriving classes to override to add dependencies when a UserObject is requested.
UserObjectInterface(const MooseObject *moose_object)
const std::string & userObjectName(const UserObjectBase &uo) const
Gets a UserObject's name; avoids including UserObject.h in the UserObjectInterface.
UserObjectName getUserObjectName(const std::string &param_name) const
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.
static InputParameters validParams()
bool hasUserObject(const std::string &param_name) const
const FEProblemBase & _uoi_feproblem
Reference to the FEProblemBase instance.
const UserObjectBase & getUserObjectBase(const std::string &param_name, bool is_dependency=true) const
Get an user object with a given parameter param_name.
const THREAD_ID _uoi_tid
Thread ID.
const std::string & userObjectType(const UserObjectBase &uo) const
Gets a UserObject's type; avoids including UserObject.h in the UserObjectInterface.
bool hasUserObjectByName(const UserObjectName &object_name) const
const UserObjectBase & getUserObjectBaseByName(const UserObjectName &object_name, bool is_dependency=true) const
Get an user object with the name object_name.
void mooseObjectError(const std::string &param_name, std::stringstream &oss) const
emit an error for the given parameter
const MooseObject & _uoi_moose_object
Moose object using the interface.