https://mooseframework.inl.gov
UserObjectInterface.h
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 #pragma once
11 
12 // MOOSE includes
13 #include "MooseTypes.h"
14 #include "MooseUtils.h"
15 
16 // Forward declarations
17 class UserObject;
18 class FEProblemBase;
19 class MooseObject;
20 
25 {
26 public:
28 
29  UserObjectInterface(const MooseObject * moose_object);
30 
31 #ifdef MOOSE_KOKKOS_ENABLED
32 
36 #endif
37 
41  UserObjectName getUserObjectName(const std::string & param_name) const;
42 
46  bool hasUserObject(const std::string & param_name) const;
48  template <class T>
49  bool hasUserObject(const std::string & param_name) const;
51 
52  /*
53  * @return Whether or not a UserObject exists with the name \p object_name.
54  */
56  bool hasUserObjectByName(const UserObjectName & object_name) const;
57  template <class T>
58  bool hasUserObjectByName(const UserObjectName & object_name) const;
60 
69  template <class T>
70  const T & getUserObject(const std::string & param_name, bool is_dependency = true) const;
71 
80  template <class T>
81  const T & getUserObjectByName(const UserObjectName & object_name,
82  bool is_dependency = true) const;
83 
92  const UserObject & getUserObjectBase(const std::string & param_name,
93  bool is_dependency = true) const;
94 
103  const UserObject & getUserObjectBaseByName(const UserObjectName & object_name,
104  bool is_dependency = true) const;
105 
106 protected:
111  virtual void addUserObjectDependencyHelper(const UserObject & /* uo */) const {}
112 
113 private:
117  const UserObject & getUserObjectFromFEProblem(const UserObjectName & object_name) const;
118 
124  template <class T>
125  const T & castUserObject(const UserObject & uo_base, const std::string & param_name = "") const;
126 
130  void mooseObjectError(const std::string & param_name, std::stringstream & oss) const;
131 
133  const std::string & userObjectType(const UserObject & uo) const;
135  const std::string & userObjectName(const UserObject & uo) const;
136 
139 
142 
145 };
146 
147 template <class T>
148 const T &
150  const std::string & param_name /* = "" */) const
151 {
152  const T * uo = dynamic_cast<const T *>(&uo_base);
153 
154  if (!uo)
155  {
156  std::stringstream oss;
157  oss << "The provided UserObject \"" << userObjectName(uo_base) << "\" of type "
158  << userObjectType(uo_base)
159  << " is not derived from the required type.\n\nThe UserObject must derive from "
160  << MooseUtils::prettyCppType<T>() << ".";
161 
162  mooseObjectError(param_name, oss);
163  }
164 
165  return *uo;
166 }
167 
168 template <class T>
169 const T &
170 UserObjectInterface::getUserObject(const std::string & param_name, const bool is_dependency) const
171 {
172  return castUserObject<T>(getUserObjectBase(param_name, is_dependency), param_name);
173 }
174 
175 template <class T>
176 const T &
177 UserObjectInterface::getUserObjectByName(const UserObjectName & object_name,
178  const bool is_dependency) const
179 {
180  return castUserObject<T>(getUserObjectBaseByName(object_name, is_dependency));
181 }
182 
183 template <class T>
184 bool
185 UserObjectInterface::hasUserObject(const std::string & param_name) const
186 {
187  return hasUserObjectByName<T>(getUserObjectName(param_name));
188 }
189 
190 template <class T>
191 bool
192 UserObjectInterface::hasUserObjectByName(const UserObjectName & object_name) const
193 {
194  if (!hasUserObjectByName(object_name))
195  return false;
196  return dynamic_cast<const T *>(&getUserObjectFromFEProblem(object_name));
197 }
UserObjectName getUserObjectName(const std::string &param_name) const
const MooseObject & _uoi_moose_object
Moose object using the interface.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const T & getUserObjectByName(const UserObjectName &object_name, bool is_dependency=true) const
Get an user object with the name object_name.
static InputParameters validParams()
const T & castUserObject(const UserObject &uo_base, const std::string &param_name="") const
Internal helper that casts the UserObject uo_base to the requested type.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
const FEProblemBase & _uoi_feproblem
Reference to the FEProblemBase instance.
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:27
const THREAD_ID _uoi_tid
Thread ID.
Interface for objects that need to use UserObjects.
const T & getUserObject(const std::string &param_name, bool is_dependency=true) const
Get an user object with a given parameter param_name.
void mooseObjectError(const std::string &param_name, std::stringstream &oss) const
emit an error for the given parameter
const UserObject & getUserObjectBase(const std::string &param_name, bool is_dependency=true) const
Get an user object with a given parameter param_name.
const std::string & userObjectType(const UserObject &uo) const
Gets a UserObject&#39;s type; avoids including UserObject.h in the UserObjectInterface.
const UserObject & getUserObjectFromFEProblem(const UserObjectName &object_name) const
Go directly to the FEProblem for the requested UserObject.
const UserObject & getUserObjectBaseByName(const UserObjectName &object_name, bool is_dependency=true) const
Get an user object with the name object_name.
virtual void addUserObjectDependencyHelper(const UserObject &) const
Helper for deriving classes to override to add dependencies when a UserObject is requested.
bool hasUserObjectByName(const UserObjectName &object_name) const
bool hasUserObject(const std::string &param_name) const
Base class for user-specific data.
Definition: UserObject.h:40
const std::string & userObjectName(const UserObject &uo) const
Gets a UserObject&#39;s name; avoids including UserObject.h in the UserObjectInterface.
unsigned int THREAD_ID
Definition: MooseTypes.h:209
UserObjectInterface(const MooseObject *moose_object)