https://mooseframework.inl.gov
UserObject.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 "DistributionInterface.h"
14 #include "FunctionInterface.h"
15 #include "UserObjectInterface.h"
16 #include "PostprocessorInterface.h"
18 #include "ReporterInterface.h"
19 #include "MeshChangedInterface.h"
20 #include "MeshDisplacedInterface.h"
21 #include "MooseObject.h"
22 #include "MooseTypes.h"
23 #include "Restartable.h"
24 #include "MeshMetaDataInterface.h"
25 #include "ScalarCoupleable.h"
26 #include "SetupInterface.h"
27 #include "PerfGraphInterface.h"
28 #include "SamplerInterface.h"
29 
30 #include "libmesh/parallel.h"
31 
32 class FEProblemBase;
33 class SubProblem;
34 class Assembly;
35 class SystemBase;
36 
40 class UserObject : public MooseObject,
41  public SetupInterface,
42  protected FunctionInterface,
43  public UserObjectInterface,
44  protected PostprocessorInterface,
46  protected ReporterInterface,
47  protected DistributionInterface,
48  protected SamplerInterface,
49  protected Restartable,
50  protected MeshMetaDataInterface,
51  protected MeshChangedInterface,
52  protected MeshDisplacedInterface,
53  protected PerfGraphInterface,
55 {
56 public:
58 
59  UserObject(const InputParameters & params);
60  virtual ~UserObject() = default;
61 
65  virtual void execute() = 0;
66 
70  virtual void initialize() = 0;
71 
76  virtual void finalize() = 0;
77 
82  SubProblem & getSubProblem() const { return _subproblem; }
83 
89 
95  virtual Real spatialValue(const Point & /*p*/) const
96  {
97  mooseError(name(), " does not satisfy the Spatial UserObject interface!");
98  }
99 
106  virtual const std::vector<Point> spatialPoints() const
107  {
108  mooseError("Spatial UserObject interface is not satisfied; spatialPoints() must be overridden");
109  }
110 
117  virtual void threadJoin(const UserObject & uo) = 0;
118 
125  template <typename T>
126  void gatherSum(T & value)
127  {
129  }
130 
137  template <typename T>
138  void gatherMax(T & value)
139  {
141  }
142 
149  template <typename T>
150  void gatherMin(T & value)
151  {
153  }
154 
161  template <typename T1, typename T2>
162  void gatherProxyValueMax(T1 & proxy, T2 & value);
163 
170  template <typename T1, typename T2>
171  void gatherProxyValueMin(T1 & proxy, T2 & value);
172 
173  void setPrimaryThreadCopy(UserObject * primary);
174 
176 
181  std::set<UserObjectName> getDependObjects() const;
182 
189  virtual bool needThreadedCopy() const { return false; }
190 
191  const std::set<std::string> & getRequestedItems() override { return _depend_uo; }
192 
193  const std::set<std::string> & getSuppliedItems() override { return _supplied_uo; }
194 
198  unsigned int systemNumber() const;
199 
200 protected:
201  virtual void addPostprocessorDependencyHelper(const PostprocessorName & name) const override;
202  virtual void
203  addVectorPostprocessorDependencyHelper(const VectorPostprocessorName & name) const override;
204  virtual void addUserObjectDependencyHelper(const UserObject & uo) const override;
205  void addReporterDependencyHelper(const ReporterName & reporter_name) override;
206 
209 
212 
216 
220 
223 
225 
228  mutable std::set<std::string> _depend_uo;
229 
230 private:
232 
234  std::set<std::string> _supplied_uo;
235 };
236 
237 template <typename T1, typename T2>
238 void
239 UserObject::gatherProxyValueMax(T1 & proxy, T2 & value)
240 {
241  // Get all proxy, value pairs. _communicator.maxloc would be faster but leads to
242  // partitioning dependent results if the maximum proxy is not unique.
243  std::vector<std::pair<T1, T2>> all(n_processors());
244  const auto pair = std::make_pair(proxy, value);
245  _communicator.allgather(pair, all);
246 
247  // find maximum, disambiguated by the value
248  const auto it = std::max_element(all.begin(), all.end());
249  proxy = it->first;
250  value = it->second;
251 }
252 
253 template <typename T1, typename T2>
254 void
255 UserObject::gatherProxyValueMin(T1 & proxy, T2 & value)
256 {
257  // get all proxy, value pairs
258  std::vector<std::pair<T1, T2>> all(n_processors());
259  const auto pair = std::make_pair(proxy, value);
260  _communicator.allgather(pair, all);
261 
262  // find minimum, disambiguated by the value
263  const auto it = std::min_element(all.begin(), all.end());
264  proxy = it->first;
265  value = it->second;
266 }
const std::set< std::string > & getRequestedItems() override
Return a set containing the names of items requested by the object.
Definition: UserObject.h:191
const bool _duplicate_initial_execution
Definition: UserObject.h:224
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:219
void setPrimaryThreadCopy(UserObject *primary)
Definition: UserObject.C:146
virtual ~UserObject()=default
Keeps track of stuff related to assembling.
Definition: Assembly.h:101
Interface for objects acting when the mesh has been displaced.
virtual const std::vector< Point > spatialPoints() const
Optional interface function for providing the points at which a UserObject attains spatial values...
Definition: UserObject.h:106
UserObject * primaryThreadCopy()
Definition: UserObject.h:175
std::set< std::string > _supplied_uo
A name of the "supplied" user objects, which is just this object.
Definition: UserObject.h:234
UserObject * _primary_thread_copy
Definition: UserObject.h:231
void gatherMax(T &value)
Gather the parallel max of the variable passed in.
Definition: UserObject.h:138
void gatherProxyValueMin(T1 &proxy, T2 &value)
Determine the value of a variable according to which process has the parallel minimum of the provided...
Definition: UserObject.h:255
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:193
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:150
bool shouldDuplicateInitialExecution() const
Returns whether or not this user object should be executed twice during the initial condition when de...
Definition: UserObject.h:88
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:134
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:57
SubProblem & _subproblem
Reference to the Subproblem for this user object.
Definition: UserObject.h:208
virtual void execute()=0
Execute method.
virtual bool needThreadedCopy() const
Whether or not a threaded copy of this object is needed when obtaining it in another object...
Definition: UserObject.h:189
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:126
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:28
SubProblem & getSubProblem() const
Returns a reference to the subproblem that this postprocessor is tied to.
Definition: UserObject.h:82
void gatherProxyValueMax(T1 &proxy, T2 &value)
Deteremine the value of a variable according to the parallel maximum of the provided proxy...
Definition: UserObject.h:239
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:215
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:98
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:228
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:122
virtual Real spatialValue(const Point &) const
Optional interface function for "evaluating" a UserObject at a spatial position.
Definition: UserObject.h:95
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const Moose::CoordinateSystemType & _coord_sys
Coordinate system.
Definition: UserObject.h:222
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:78
CoordinateSystemType
Definition: MooseTypes.h:809
void max(const T &r, T &o, Request &req) const
FEProblemBase & _fe_problem
Reference to the FEProblemBase for this user object.
Definition: UserObject.h:211
UserObject(const InputParameters &params)
Definition: UserObject.C:65
const THREAD_ID _tid
Thread ID of this postprocessor.
Definition: UserObject.h:218
Interface for sorting dependent vectors of objects.
virtual void finalize()=0
Finalize.
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:140
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:128
unsigned int systemNumber() const
Definition: UserObject.C:153
Interface for objects that need to use functions.
Base class for user-specific data.
Definition: UserObject.h:40
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:209
virtual void threadJoin(const UserObject &uo)=0
Must override.
static InputParameters validParams()
Definition: UserObject.C:18