www.mooseframework.org
Restartable.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 "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<T>() { _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 protected:
119  template <typename T, typename... Args>
120  T & declareRestartableData(const std::string & data_name, Args &&... args);
121 
136  template <typename T, typename... Args>
137  ManagedValue<T> declareManagedRestartableDataWithContext(const std::string & data_name,
138  void * context,
139  Args &&... args);
140 
151  template <typename T, typename... Args>
152  const T & getRestartableData(const std::string & data_name) const;
153 
165  template <typename T, typename... Args>
166  T &
167  declareRestartableDataWithContext(const std::string & data_name, void * context, Args &&... args);
168 
181  template <typename T, typename... Args>
182  T & declareRecoverableData(const std::string & data_name, Args &&... args);
183 
195  template <typename T, typename... Args>
196  T & declareRestartableDataWithObjectName(const std::string & data_name,
197  const std::string & object_name,
198  Args &&... args);
199 
212  template <typename T, typename... Args>
213  T & declareRestartableDataWithObjectNameWithContext(const std::string & data_name,
214  const std::string & object_name,
215  void * context,
216  Args &&... args);
217 
224  std::string restartableName(const std::string & data_name) const;
225 
228 
230  const std::string _restartable_system_name;
231 
234 
237 
238 private:
241 
243  std::string _restartable_name;
244 
246  RestartableDataValue & registerRestartableDataOnApp(std::unique_ptr<RestartableDataValue> data,
247  THREAD_ID tid) const;
248 
250  void registerRestartableNameWithFilterOnApp(const std::string & name,
252 
261  template <typename T, typename... Args>
262  RestartableData<T> & declareRestartableDataHelper(const std::string & data_name,
263  void * context,
264  Args &&... args) const;
265 };
266 
267 template <typename T, typename... Args>
268 T &
269 Restartable::declareRestartableData(const std::string & data_name, Args &&... args)
270 {
271  return declareRestartableDataWithContext<T>(data_name, nullptr, std::forward<Args>(args)...);
272 }
273 
274 template <typename T, typename... Args>
277  void * context,
278  Args &&... args)
279 {
280  auto & data_ptr =
281  declareRestartableDataHelper<T>(data_name, context, std::forward<Args>(args)...);
282  return Restartable::ManagedValue<T>(data_ptr);
283 }
284 
285 template <typename T, typename... Args>
286 const T &
287 Restartable::getRestartableData(const std::string & data_name) const
288 {
289  return declareRestartableDataHelper<T>(data_name, nullptr).get();
290 }
291 
292 template <typename T, typename... Args>
293 T &
295  void * context,
296  Args &&... args)
297 {
298  return declareRestartableDataHelper<T>(data_name, context, std::forward<Args>(args)...).set();
299 }
300 
301 template <typename T, typename... Args>
303 Restartable::declareRestartableDataHelper(const std::string & data_name,
304  void * context,
305  Args &&... args) const
306 {
307  const auto full_name = restartableName(data_name);
308 
309  // Here we will create the RestartableData even though we may not use this instance.
310  // If it's already in use, the App will return a reference to the existing instance and we'll
311  // return that one instead. We might refactor this to have the app create the RestartableData
312  // at a later date.
313  auto data_ptr =
314  std::make_unique<RestartableData<T>>(full_name, context, std::forward<Args>(args)...);
315  auto & restartable_data_ref = static_cast<RestartableData<T> &>(
316  registerRestartableDataOnApp(std::move(data_ptr), _restartable_tid));
317 
318  return restartable_data_ref;
319 }
320 
321 template <typename T, typename... Args>
322 T &
324  const std::string & object_name,
325  Args &&... args)
326 {
327  return declareRestartableDataWithObjectNameWithContext<T>(
328  data_name, object_name, nullptr, std::forward<Args>(args)...);
329 }
330 
331 template <typename T, typename... Args>
332 T &
334  const std::string & object_name,
335  void * context,
336  Args &&... args)
337 {
338  std::string old_name = _restartable_name;
339 
340  _restartable_name = object_name;
341 
342  T & value = declareRestartableDataWithContext<T>(data_name, context, std::forward<Args>(args)...);
343 
344  _restartable_name = old_name;
345 
346  return value;
347 }
348 
349 template <typename T, typename... Args>
350 T &
351 Restartable::declareRecoverableData(const std::string & data_name, Args &&... args)
352 {
353  const auto full_name = restartableName(data_name);
354 
356 
357  return declareRestartableDataWithContext<T>(data_name, nullptr, std::forward<Args>(args)...);
358 }
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
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:66
T & declareRestartableDataWithContext(const std::string &data_name, void *context, Args &&... args)
Declare a piece of data as "restartable" and initialize it.
Definition: Restartable.h:294
Base class for MOOSE-based applications.
Definition: MooseApp.h:69
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:269
std::string _restartable_name
The name of the object.
Definition: Restartable.h:243
RestartableData< T > & declareRestartableDataHelper(const std::string &data_name, void *context, Args &&... args) const
Helper function for declaring restartable data.
Definition: Restartable.h:303
const std::string _restartable_system_name
The system name this object is in.
Definition: Restartable.h:230
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:704
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:33
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:88
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:287
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:276
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:75
std::string RestartableDataMapName
Definition: MooseTypes.h:203
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:333
void registerRestartableNameWithFilterOnApp(const std::string &name, Moose::RESTARTABLE_FILTER filter)
Helper function for actually registering the restartable data.
Definition: Restartable.C:59
const THREAD_ID _restartable_tid
The thread ID for this object.
Definition: Restartable.h:233
T & declareRecoverableData(const std::string &data_name, Args &&... args)
Declare a piece of data as "recoverable" and initialize it.
Definition: Restartable.h:351
const bool _restartable_read_only
Flag for toggling read only status (see ReporterData)
Definition: Restartable.h:236
const RestartableDataMapName _metaname
Restartable metadata name.
Definition: Restartable.h:240
MooseApp & _restartable_app
Reference to the application.
Definition: Restartable.h:227
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:51
T & declareRestartableDataWithObjectName(const std::string &data_name, const std::string &object_name, Args &&... args)
Declare a piece of data as "restartable".
Definition: Restartable.h:323
unsigned int THREAD_ID
Definition: MooseTypes.h:198