https://mooseframework.inl.gov
Restartable.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 "RestartableData.h"
15 
16 // Forward declarations
17 class PostprocessorData;
18 class SubProblem;
19 class InputParameters;
20 class MooseObject;
21 class MooseApp;
22 class MooseMesh;
23 
29 {
30 public:
41  template <typename T>
43  {
44  public:
46 
52  ~ManagedValue() { _value.reset(); }
53 
57  const T & get() const { return _value.get(); }
59  T & set() { return _value.set(); }
61 
62  private:
65  };
66 
77  Restartable(const MooseObject * moose_object, const std::string & system_name);
78 
86  Restartable(const MooseObject * moose_object, const std::string & system_name, THREAD_ID tid);
87 
101  Restartable(MooseApp & moose_app,
102  const std::string & name,
103  const std::string & system_name,
104  THREAD_ID tid,
105  const bool read_only = false,
106  const RestartableDataMapName & metaname = "");
107 
108 #ifdef MOOSE_KOKKOS_ENABLED
109 
112  Restartable(const Restartable & object, const Moose::Kokkos::FunctorCopy & key);
113 #endif
114 
115 protected:
126  template <typename T, typename... Args>
127  T & declareRestartableData(const std::string & data_name, Args &&... args);
128 
143  template <typename T, typename... Args>
144  ManagedValue<T> declareManagedRestartableDataWithContext(const std::string & data_name,
145  void * context,
146  Args &&... args);
147 
158  template <typename T, typename... Args>
159  const T & getRestartableData(const std::string & data_name) const;
160 
172  template <typename T, typename... Args>
173  T &
174  declareRestartableDataWithContext(const std::string & data_name, void * context, Args &&... args);
175 
188  template <typename T, typename... Args>
189  T & declareRecoverableData(const std::string & data_name, Args &&... args);
190 
202  template <typename T, typename... Args>
203  T & declareRestartableDataWithObjectName(const std::string & data_name,
204  const std::string & object_name,
205  Args &&... args);
206 
219  template <typename T, typename... Args>
220  T & declareRestartableDataWithObjectNameWithContext(const std::string & data_name,
221  const std::string & object_name,
222  void * context,
223  Args &&... args);
224 
231  std::string restartableName(const std::string & data_name) const;
232 
235 
237  const std::string _restartable_system_name;
238 
241 
244 
245 private:
248 
250  std::string _restartable_name;
251 
253  RestartableDataValue & registerRestartableDataOnApp(std::unique_ptr<RestartableDataValue> data,
254  THREAD_ID tid) const;
255 
257  void registerRestartableNameWithFilterOnApp(const std::string & name,
259 
268  template <typename T, typename... Args>
269  RestartableData<T> & declareRestartableDataHelper(const std::string & data_name,
270  void * context,
271  Args &&... args) const;
272 };
273 
274 template <typename T, typename... Args>
275 T &
276 Restartable::declareRestartableData(const std::string & data_name, Args &&... args)
277 {
278  return declareRestartableDataWithContext<T>(data_name, nullptr, std::forward<Args>(args)...);
279 }
280 
281 template <typename T, typename... Args>
284  void * context,
285  Args &&... args)
286 {
287  auto & data_ptr =
288  declareRestartableDataHelper<T>(data_name, context, std::forward<Args>(args)...);
289  return Restartable::ManagedValue<T>(data_ptr);
290 }
291 
292 template <typename T, typename... Args>
293 const T &
294 Restartable::getRestartableData(const std::string & data_name) const
295 {
296  return declareRestartableDataHelper<T>(data_name, nullptr).get();
297 }
298 
299 template <typename T, typename... Args>
300 T &
302  void * context,
303  Args &&... args)
304 {
305  return declareRestartableDataHelper<T>(data_name, context, std::forward<Args>(args)...).set();
306 }
307 
308 template <typename T, typename... Args>
310 Restartable::declareRestartableDataHelper(const std::string & data_name,
311  void * context,
312  Args &&... args) const
313 {
314  const auto full_name = restartableName(data_name);
315 
316  // Here we will create the RestartableData even though we may not use this instance.
317  // If it's already in use, the App will return a reference to the existing instance and we'll
318  // return that one instead. We might refactor this to have the app create the RestartableData
319  // at a later date.
320  auto data_ptr =
321  std::make_unique<RestartableData<T>>(full_name, context, std::forward<Args>(args)...);
322  auto & restartable_data_ref = static_cast<RestartableData<T> &>(
323  registerRestartableDataOnApp(std::move(data_ptr), _restartable_tid));
324 
325  return restartable_data_ref;
326 }
327 
328 template <typename T, typename... Args>
329 T &
331  const std::string & object_name,
332  Args &&... args)
333 {
334  return declareRestartableDataWithObjectNameWithContext<T>(
335  data_name, object_name, nullptr, std::forward<Args>(args)...);
336 }
337 
338 template <typename T, typename... Args>
339 T &
341  const std::string & object_name,
342  void * context,
343  Args &&... args)
344 {
345  std::string old_name = _restartable_name;
346 
347  _restartable_name = object_name;
348 
349  T & value = declareRestartableDataWithContext<T>(data_name, context, std::forward<Args>(args)...);
350 
351  _restartable_name = old_name;
352 
353  return value;
354 }
355 
356 template <typename T, typename... Args>
357 T &
358 Restartable::declareRecoverableData(const std::string & data_name, Args &&... args)
359 {
360  const auto full_name = restartableName(data_name);
361 
363 
364  return declareRestartableDataWithContext<T>(data_name, nullptr, std::forward<Args>(args)...);
365 }
A class for creating restricted objects.
Definition: Restartable.h:28
RestartableData< T > & _value
The underlying data.
Definition: Restartable.h:64
Wrapper class for restartable data that is "managed.
Definition: Restartable.h:42
ManagedValue(RestartableData< T > &value)
Definition: Restartable.h:45
std::string restartableName(const std::string &data_name) const
Gets the name of a piece of restartable data given a data name, adding the system name and object nam...
Definition: Restartable.C:78
T & declareRestartableDataWithContext(const std::string &data_name, void *context, Args &&... args)
Declare a piece of data as "restartable" and initialize it.
Definition: Restartable.h:301
Base class for MOOSE-based applications.
Definition: MooseApp.h:103
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
T & declareRestartableData(const std::string &data_name, Args &&... args)
Declare a piece of data as "restartable" and initialize it.
Definition: Restartable.h:276
std::string _restartable_name
The name of the object.
Definition: Restartable.h:250
~ManagedValue()
Destructor.
Definition: Restartable.h:52
RestartableData< T > & declareRestartableDataHelper(const std::string &data_name, void *context, Args &&... args) const
Helper function for declaring restartable data.
Definition: Restartable.h:310
const std::string _restartable_system_name
The system name this object is in.
Definition: Restartable.h:237
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
RESTARTABLE_FILTER
The filter type applied to a particular piece of "restartable" data.
Definition: MooseTypes.h:792
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:27
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:92
const T & getRestartableData(const std::string &data_name) const
Declare a piece of data as "restartable" and initialize it Similar to declareRestartableData but retu...
Definition: Restartable.h:294
ManagedValue< T > declareManagedRestartableDataWithContext(const std::string &data_name, void *context, Args &&... args)
Declares a piece of "managed" restartable data and initialize it.
Definition: Restartable.h:283
Restartable(const MooseObject *moose_object, const std::string &system_name)
Class constructor.
Definition: Restartable.C:18
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:78
std::string RestartableDataMapName
Definition: MooseTypes.h:214
Concrete definition of a parameter value for a specified type.
T & declareRestartableDataWithObjectNameWithContext(const std::string &data_name, const std::string &object_name, void *context, Args &&... args)
Declare a piece of data as "restartable".
Definition: Restartable.h:340
void registerRestartableNameWithFilterOnApp(const std::string &name, Moose::RESTARTABLE_FILTER filter)
Helper function for actually registering the restartable data.
Definition: Restartable.C:71
const THREAD_ID _restartable_tid
The thread ID for this object.
Definition: Restartable.h:240
T & declareRecoverableData(const std::string &data_name, Args &&... args)
Declare a piece of data as "recoverable" and initialize it.
Definition: Restartable.h:358
const bool _restartable_read_only
Flag for toggling read only status (see ReporterData)
Definition: Restartable.h:243
const RestartableDataMapName _metaname
Restartable metadata name.
Definition: Restartable.h:247
MooseApp & _restartable_app
Reference to the application.
Definition: Restartable.h:234
Abstract definition of a RestartableData value.
RestartableDataValue & registerRestartableDataOnApp(std::unique_ptr< RestartableDataValue > data, THREAD_ID tid) const
Helper function for actually registering the restartable data.
Definition: Restartable.C:63
T & declareRestartableDataWithObjectName(const std::string &data_name, const std::string &object_name, Args &&... args)
Declare a piece of data as "restartable".
Definition: Restartable.h:330
unsigned int THREAD_ID
Definition: MooseTypes.h:209