https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
17class MooseObject;
18class UserObjectBase;
19class FEProblemBase;
20
25{
26public:
28
29 UserObjectInterface(const MooseObject * moose_object);
30
31#ifdef MOOSE_KOKKOS_ENABLED
36#endif
37
41 UserObjectName getUserObjectName(const std::string & param_name) const;
42
47 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 UserObjectBase & getUserObjectBase(const std::string & param_name,
93 bool is_dependency = true) const;
94
103 const UserObjectBase & getUserObjectBaseByName(const UserObjectName & object_name,
104 bool is_dependency = true) const;
105
106protected:
111 virtual void addUserObjectDependencyHelper(const UserObjectBase & /* uo */) const {}
112
113private:
117 const UserObjectBase & getUserObjectFromFEProblem(const UserObjectName & object_name,
118 const THREAD_ID tid = 0) const;
119
125 template <class T>
126 const T & castUserObject(const UserObjectBase & uo_base,
127 const std::string & param_name = "") const;
128
132 void mooseObjectError(const std::string & param_name, std::stringstream & oss) const;
133
135 const std::string & userObjectType(const UserObjectBase & uo) const;
137 const std::string & userObjectName(const UserObjectBase & uo) const;
138
141
144
147};
148
149template <class T>
150const T &
152 const std::string & param_name /* = "" */) const
153{
154 const T * uo = dynamic_cast<const T *>(&uo_base);
155
156 if (!uo)
157 {
158 std::stringstream oss;
159 oss << "The provided UserObject \"" << userObjectName(uo_base) << "\" of type "
160 << userObjectType(uo_base)
161 << " is not derived from the required type.\n\nThe UserObject must derive from "
162 << MooseUtils::prettyCppType<T>() << ".";
163
164 mooseObjectError(param_name, oss);
165 }
166
167 return *uo;
168}
169
170template <class T>
171const T &
172UserObjectInterface::getUserObject(const std::string & param_name, const bool is_dependency) const
173{
174 return castUserObject<T>(getUserObjectBase(param_name, is_dependency), param_name);
175}
176
177template <class T>
178const T &
179UserObjectInterface::getUserObjectByName(const UserObjectName & object_name,
180 const bool is_dependency) const
181{
182 return castUserObject<T>(getUserObjectBaseByName(object_name, is_dependency));
183}
184
185template <class T>
186bool
187UserObjectInterface::hasUserObject(const std::string & param_name) const
188{
189 return hasUserObjectByName<T>(getUserObjectName(param_name));
190}
191
192template <class T>
193bool
194UserObjectInterface::hasUserObjectByName(const UserObjectName & object_name) const
195{
196 if (!hasUserObjectByName(object_name))
197 return false;
198 return dynamic_cast<const T *>(&getUserObjectFromFEProblem(object_name));
199}
unsigned int THREAD_ID
Definition MooseTypes.h:237
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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.
const T & castUserObject(const UserObjectBase &uo_base, const std::string &param_name="") const
Internal helper that casts the UserObject uo_base to the requested type.
virtual void addUserObjectDependencyHelper(const UserObjectBase &) const
Helper for deriving classes to override to add dependencies when a UserObject is requested.
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 T & getUserObjectByName(const UserObjectName &object_name, bool is_dependency=true) const
Get an user object with the name object_name.
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.
const T & getUserObject(const std::string &param_name, bool is_dependency=true) const
Get an user object with a given parameter param_name.