www.mooseframework.org
UserObject.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "DistributionInterface.h"
14 #include "FunctionInterface.h"
15 #include "UserObjectInterface.h"
16 #include "PostprocessorInterface.h"
18 #include "ReporterInterface.h"
19 #include "MeshChangedInterface.h"
20 #include "MooseObject.h"
21 #include "MooseTypes.h"
22 #include "Restartable.h"
23 #include "MeshMetaDataInterface.h"
24 #include "ScalarCoupleable.h"
25 #include "SetupInterface.h"
26 #include "PerfGraphInterface.h"
27 #include "SamplerInterface.h"
28 
29 #include "libmesh/parallel.h"
30 
31 class FEProblemBase;
32 class SubProblem;
33 class Assembly;
34 class SystemBase;
35 
39 class UserObject : public MooseObject,
40  public SetupInterface,
41  protected FunctionInterface,
42  public UserObjectInterface,
43  protected PostprocessorInterface,
45  protected ReporterInterface,
46  protected DistributionInterface,
47  protected SamplerInterface,
48  protected Restartable,
49  protected MeshMetaDataInterface,
50  protected MeshChangedInterface,
51  protected ScalarCoupleable,
52  protected PerfGraphInterface,
54 {
55 public:
57 
58  UserObject(const InputParameters & params);
59  virtual ~UserObject() = default;
60 
64  virtual void execute() = 0;
65 
69  virtual void initialize() = 0;
70 
75  virtual void finalize() = 0;
76 
81  SubProblem & getSubProblem() const { return _subproblem; }
82 
88 
94  virtual Real spatialValue(const Point & /*p*/) const
95  {
96  mooseError(name(), " does not satisfy the Spatial UserObject interface!");
97  }
98 
105  virtual const std::vector<Point> spatialPoints() const
106  {
107  mooseError("Spatial UserObject interface is not satisfied; spatialPoints() must be overridden");
108  }
109 
116  virtual void threadJoin(const UserObject & uo) = 0;
117 
124  template <typename T>
125  void gatherSum(T & value)
126  {
128  }
129 
136  template <typename T>
137  void gatherMax(T & value)
138  {
140  }
141 
148  template <typename T>
149  void gatherMin(T & value)
150  {
152  }
153 
160  template <typename T1, typename T2>
161  void gatherProxyValueMax(T1 & value, T2 & proxy);
162 
169  template <typename T1, typename T2>
170  void gatherProxyValueMin(T1 & value, T2 & proxy);
171 
172  void setPrimaryThreadCopy(UserObject * primary);
173 
175 
180  std::set<UserObjectName> getDependObjects() const;
181 
188  virtual bool needThreadedCopy() const { return false; }
189 
190  const std::set<std::string> & getRequestedItems() override { return _depend_uo; }
191 
192  const std::set<std::string> & getSuppliedItems() override { return _supplied_uo; }
193 
197  unsigned int systemNumber() const;
198 
199 protected:
200  virtual void addPostprocessorDependencyHelper(const PostprocessorName & name) const override;
201  virtual void
202  addVectorPostprocessorDependencyHelper(const VectorPostprocessorName & name) const override;
203  virtual void addUserObjectDependencyHelper(const UserObject & uo) const override;
204  void addReporterDependencyHelper(const ReporterName & reporter_name) override;
205 
208 
211 
215 
219 
222 
224 
227  mutable std::set<std::string> _depend_uo;
228 
229 private:
231 
233  std::set<std::string> _supplied_uo;
234 };
235 
236 template <typename T1, typename T2>
237 void
238 UserObject::gatherProxyValueMax(T1 & value, T2 & proxy)
239 {
240  // get all value, proxy pairs, _communicator.maxloc would be faster but leads to
241  // partitioning dependent results if the maximum value is not unique.
242  std::vector<std::pair<T1, T2>> all(n_processors());
243  auto pair = std::make_pair(value, proxy);
244  _communicator.allgather(pair, all);
245 
246  // find maximum, disambiguated by the proxy value
247  const auto it = std::max_element(all.begin(), all.end());
248  value = it->first;
249  proxy = it->second;
250 }
251 
252 template <typename T1, typename T2>
253 void
254 UserObject::gatherProxyValueMin(T1 & value, T2 & proxy)
255 {
256  // get all value, proxy pairs
257  std::vector<std::pair<T1, T2>> all(n_processors());
258  auto pair = std::make_pair(value, proxy);
259  _communicator.allgather(pair, all);
260 
261  // find minimum, disambiguated by the proxy value
262  const auto it = std::min_element(all.begin(), all.end());
263  value = it->first;
264  proxy = it->second;
265 }
const std::set< std::string > & getRequestedItems() override
Return a set containing the names of items requested by the object.
Definition: UserObject.h:190
const bool _duplicate_initial_execution
Definition: UserObject.h:223
void gatherProxyValueMax(T1 &value, T2 &proxy)
Gather the parallel value of a variable according to which process has the parallel maximum of the pr...
Definition: UserObject.h:238
void allgather(const T &send_data, std::vector< T, A > &recv_data) const
A class for creating restricted objects.
Definition: Restartable.h:28
Assembly & _assembly
Definition: UserObject.h:218
void setPrimaryThreadCopy(UserObject *primary)
Definition: UserObject.C:148
virtual ~UserObject()=default
Keeps track of stuff related to assembling.
Definition: Assembly.h:93
virtual const std::vector< Point > spatialPoints() const
Optional interface function for providing the points at which a UserObject attains spatial values...
Definition: UserObject.h:105
UserObject * primaryThreadCopy()
Definition: UserObject.h:174
std::set< std::string > _supplied_uo
A name of the "supplied" user objects, which is just this object.
Definition: UserObject.h:233
UserObject * _primary_thread_copy
Definition: UserObject.h:230
void gatherMax(T &value)
Gather the parallel max of the variable passed in.
Definition: UserObject.h:137
Interface for objects that need to use samplers.
const std::set< std::string > & getSuppliedItems() override
Return a set containing the names of items owned by the object.
Definition: UserObject.h:192
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void gatherMin(T &value)
Gather the parallel min of the variable passed in.
Definition: UserObject.h:149
bool shouldDuplicateInitialExecution() const
Returns whether or not this user object should be executed twice during the initial condition when de...
Definition: UserObject.h:87
const Parallel::Communicator & _communicator
Base class for a system (of equations)
Definition: SystemBase.h:84
virtual void addVectorPostprocessorDependencyHelper(const VectorPostprocessorName &name) const override
Helper for deriving classes to override to add dependencies when a VectorPostprocessor is requested...
Definition: UserObject.C:136
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:56
SubProblem & _subproblem
Reference to the Subproblem for this user object.
Definition: UserObject.h:207
virtual void execute()=0
Execute method.
void gatherProxyValueMin(T1 &value, T2 &proxy)
Gather the parallel value of a variable according to which process has the parallel minimum of the pr...
Definition: UserObject.h:254
virtual bool needThreadedCopy() const
Whether or not a threaded copy of this object is needed when obtaining it in another object...
Definition: UserObject.h:188
processor_id_type n_processors() const
Interface for notifications that the mesh has changed.
void gatherSum(T &value)
Gather the parallel sum of the variable passed in.
Definition: UserObject.h:125
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
void min(const T &r, T &o, Request &req) const
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:33
SubProblem & getSubProblem() const
Returns a reference to the subproblem that this postprocessor is tied to.
Definition: UserObject.h:81
Interface for objects that need to use distributions.
Interface to allow object to consume Reporter values.
SystemBase & _sys
Reference to the system object for this user object.
Definition: UserObject.h:214
Interface for objects that need to use UserObjects.
virtual void initialize()=0
Called before execute() is ever called so that data can be cleared.
std::set< UserObjectName > getDependObjects() const
Recursively return a set of user objects this user object depends on Note: this can be called only af...
Definition: UserObject.C:100
Interface for objects interacting with the PerfGraph.
std::set< std::string > _depend_uo
Depend UserObjects that to be used both for determining user object sorting and by AuxKernel for find...
Definition: UserObject.h:227
virtual void addUserObjectDependencyHelper(const UserObject &uo) const override
Helper for deriving classes to override to add dependencies when a UserObject is requested.
Definition: UserObject.C:124
virtual Real spatialValue(const Point &) const
Optional interface function for "evaluating" a UserObject at a spatial position.
Definition: UserObject.h:94
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const Moose::CoordinateSystemType & _coord_sys
Coordinate system.
Definition: UserObject.h:221
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:75
CoordinateSystemType
Definition: MooseTypes.h:722
void max(const T &r, T &o, Request &req) const
FEProblemBase & _fe_problem
Reference to the FEProblemBase for this user object.
Definition: UserObject.h:210
UserObject(const InputParameters &params)
Definition: UserObject.C:64
const THREAD_ID _tid
Thread ID of this postprocessor.
Definition: UserObject.h:217
Interface for sorting dependent vectors of objects.
virtual void finalize()=0
Finalize.
Interface for objects that needs scalar coupling capabilities.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
void addReporterDependencyHelper(const ReporterName &reporter_name) override
A method that can be overridden to update the UO dependencies.
Definition: UserObject.C:142
The Interface used to retrieve mesh meta data (attributes) set by the MeshGenerator system...
virtual void addPostprocessorDependencyHelper(const PostprocessorName &name) const override
Helper for deriving classes to override to add dependencies when a Postprocessor is requested...
Definition: UserObject.C:130
unsigned int systemNumber() const
Definition: UserObject.C:155
Interface for objects that need to use functions.
Base class for user-specific data.
Definition: UserObject.h:39
The Reporter system is comprised of objects that can contain any number of data values.
Definition: ReporterName.h:30
Interface class for classes which interact with Postprocessors.
unsigned int THREAD_ID
Definition: MooseTypes.h:198
virtual void threadJoin(const UserObject &uo)=0
Must override.
static InputParameters validParams()
Definition: UserObject.C:18