Skip to content

Commit

Permalink
Improve PostprocessorInterface
Browse files Browse the repository at this point in the history
- Make const-correct where possible
- Use the related object's paramError and mooseError where possible
- Make Reporter more const-correct without the need for a write key
  for the PostprocessorInterface
- Improve the checking of the existance of a postprocessor
- Remove metadata store for postprocessors in InputParameters/Parser
  and instead check on the fly

Refs idaholab#17512
  • Loading branch information
loganharbour authored and aeslaughter committed Jun 2, 2021
1 parent abe740f commit e421eed
Show file tree
Hide file tree
Showing 21 changed files with 319 additions and 285 deletions.
8 changes: 8 additions & 0 deletions framework/include/actions/ActionFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ class ActionFactory

std::set<std::string> getTasksByAction(const std::string & action) const;

/**
* Whether or not a task with the name \p task is registered.
*/
bool isRegisteredTask(const std::string & task) const { return _tasks.count(task); }

protected:
MooseApp & _app;

Expand All @@ -135,4 +140,7 @@ class ActionFactory

/// set<objectname, task> used to track if an object previously added is being added again
std::set<std::pair<std::string, std::string>> _current_objs;

/// The registered tasks
std::set<std::string> _tasks;
};
7 changes: 7 additions & 0 deletions framework/include/actions/ActionWarehouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ class ActionWarehouse : public ConsoleStreamInterface

std::string getCurrentActionName() const;

/**
* @returns True if the registered task with the name \p task is complete.
*/
bool isTaskComplete(const std::string & task) const;

protected:
/**
* This method auto-builds all Actions that needs to be built and adds them to ActionWarehouse.
Expand All @@ -250,6 +255,8 @@ class ActionWarehouse : public ConsoleStreamInterface
std::map<std::string, std::list<Action *>> _action_blocks;
/// The container that holds the sorted action names from the DependencyResolver
std::vector<std::string> _ordered_names;
/// The completed tasks
std::set<std::string> _completed_tasks;
/// Use to store the current list of unsatisfied dependencies
std::set<std::string> _unsatisfied_dependencies;

Expand Down
4 changes: 4 additions & 0 deletions framework/include/base/MooseApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ class MooseApp : public ConsoleStreamInterface, public libMesh::ParallelObject
* Return a writable reference to the ActionWarehouse associated with this app
*/
ActionWarehouse & actionWarehouse() { return _action_warehouse; }
/**
* Return a const reference to the ActionWarehouse associated with this app
*/
const ActionWarehouse & actionWarehouse() const { return _action_warehouse; }

/**
* Returns a writable reference to the parser
Expand Down
74 changes: 43 additions & 31 deletions framework/include/interfaces/PostprocessorInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PostprocessorInterface
/**
* doco-normal-methods-begin
* Retrieve the value of a Postprocessor or one of it's old or older values
* @param name The name of the Postprocessor parameter (see below)
* @param param_name The name of the Postprocessor parameter (see below)
* @param index The index of the Postprocessor
* @return A reference to the desired value
*
Expand All @@ -53,12 +53,12 @@ class PostprocessorInterface
*
* see getPostprocessorValueByName getPostprocessorValueOldByName getPostprocessorValueOlderByName
*/
const PostprocessorValue & getPostprocessorValue(const std::string & name,
unsigned int index = 0) const;
const PostprocessorValue & getPostprocessorValueOld(const std::string & name,
unsigned int index = 0) const;
const PostprocessorValue & getPostprocessorValueOlder(const std::string & name,
unsigned int index = 0) const;
const PostprocessorValue & getPostprocessorValue(const std::string & param_name,
const unsigned int index = 0) const;
const PostprocessorValue & getPostprocessorValueOld(const std::string & param_name,
const unsigned int index = 0) const;
const PostprocessorValue & getPostprocessorValueOlder(const std::string & param_name,
const unsigned int index = 0) const;
// doco-normal-methods-end

///@}
Expand All @@ -81,24 +81,27 @@ class PostprocessorInterface
const PostprocessorValue & getPostprocessorValueOlderByName(const PostprocessorName & name) const;
///@}

///@{
/**
* Return the default postprocessor value
* @param name The name of the postprocessor parameter
* @return A const reference to the default value
* Determine whether or not the Postprocessor is a default value. A default value is when
* the value is either the value set by addParam, or is a user-set value in input instead of
* a name to a postprocessor.
* @param param_name The name of the Postprocessor parameter
* @param index The index of the postprocessor
* @return True if the Postprocessor is a default value, false if the Postprocessor
* is the name of a Postprocessor
*/
const PostprocessorValue & getDefaultPostprocessorValue(const std::string & name) const;
///@}
bool isDefaultPostprocessorValue(const std::string & param_name,
const unsigned int index = 0) const;

/**
* Determine if the Postprocessor data exists
* @param name The name of the Postprocessor parameter
* @param param_name The name of the Postprocessor parameter
* @param index The index of the Postprocessor
* @return True if the Postprocessor exists
*
* @see hasPostprocessorByName getPostprocessorValue
*/
bool hasPostprocessor(const std::string & name, unsigned int index = 0) const;
bool hasPostprocessor(const std::string & param_name, const unsigned int index = 0) const;

/**
* Determine if the Postprocessor data exists
Expand All @@ -109,14 +112,6 @@ class PostprocessorInterface
*/
bool hasPostprocessorByName(const PostprocessorName & name) const;

/**
* Determine if the Postprocessor object exists
* @param name The name of the Postprocessor parameter
* @param index The index of the Postprocessor
* @return True if the Postprocessor exists
*/
bool hasPostprocessorObject(const std::string & name, unsigned int index = 0) const;

/**
* Determine if the Postprocessor object exists
* @param name The name of the Postprocessor
Expand All @@ -126,26 +121,36 @@ class PostprocessorInterface

/**
* Returns number of Postprocessors coupled under parameter name
* @param name The name of the Postprocessor parameter
* @param param_name The name of the Postprocessor parameter
* @return Number of coupled post-processors, 1 if it's a single
*
*/
unsigned int coupledPostprocessors(const std::string & name) const;
std::size_t coupledPostprocessors(const std::string & param_name) const;

/**
* Checks if there is a single postprocessor coupled by parameter name
* @param name The name of the Postprocessor parameter
* @return Number of coupled post-processors, 1 if it's a single
*
* Get the name of a postprocessor. This can only be used if the postprocessor
* parameter does _not_ have a default value set (see isDefaultPostprocessorValue()),
* in which case the "name" is actually the default value.
* @param param_name The name of the Postprocessor parameter
* @param index The index of the Postprocessor
* @return The name of the given Postprocessor
*/
bool singlePostprocessor(const std::string & name) const;
const PostprocessorName & getPostprocessorName(const std::string & param_name,
const unsigned int index = 0) const;

private:
/// The MooseObject that uses this interface
const MooseObject & _ppi_moose_object;

/// PostprocessorInterface Parameters
const InputParameters & _ppi_params;

/// Reference the the FEProblemBase class
FEProblemBase & _pi_feproblem;
const FEProblemBase & _ppi_feproblem;

/// Holds the default postprocessor values that are requested (key is param name, index)
mutable std::map<std::pair<std::string, unsigned int>, std::unique_ptr<PostprocessorValue>>
_default_values;

/// Extract the value using parameter name
const PostprocessorValue & getPostprocessorValueHelper(const std::string & name,
Expand All @@ -155,4 +160,11 @@ class PostprocessorInterface
/// Extract the value using stored name
const PostprocessorValue & getPostprocessorValueByNameHelper(const PostprocessorName & name,
std::size_t t_index) const;

/**
* Checks the parameters relating to a Postprocessor. If \p index is not set, index
* checking is not performed.
*/
void checkParam(const std::string & param_name,
const unsigned int index = std::numeric_limits<unsigned int>::max()) const;
};
26 changes: 26 additions & 0 deletions framework/include/problems/FEProblemBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,22 @@ class FEProblemBase : public SubProblem, public Restartable
*/
ReporterData & getReporterData(ReporterData::WriteKey /*key*/) { return _reporter_data; }

/**
* Requests a reporter value when it has not necessarily been delcared yet.
* @param reporter_name The name of the reporter
* @param object_name The name of the object; in the case of Postprocessors and
* VectorPostprocessors, this is not necessarily the same name as the \p reporter_name. It is used
* in error handling when reporters that were requested were not delcared.
* @param mode The reporter mode
* @param time_index The time index
* @returns A const reference to the Reporter value
*/
template <typename T>
const T & getReporterValue(const ReporterName & reporter_name,
const std::string & object_name,
const ReporterMode & mode,
const std::size_t time_index = 0) const;

// UserObjects /////
virtual void addUserObject(const std::string & user_object_name,
const std::string & name,
Expand Down Expand Up @@ -2416,3 +2432,13 @@ FEProblemBase::addObject(const std::string & type,

return objects;
}

template <typename T>
const T &
FEProblemBase::getReporterValue(const ReporterName & reporter_name,
const std::string & object_name,
const ReporterMode & mode,
const std::size_t time_index /* = 0 */) const
{
return _reporter_data.getReporterValue<T>(reporter_name, object_name, mode, time_index);
}
13 changes: 7 additions & 6 deletions framework/include/reporters/ReporterData.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class ReporterData
friend class ReporterInterface;
friend class VectorPostprocessor;
friend class VectorPostprocessorInterface;
friend class PostprocessorInterface;
friend class ReporterTransferInterface;
};

Expand Down Expand Up @@ -97,7 +96,7 @@ class ReporterData
const T & getReporterValue(const ReporterName & reporter_name,
const std::string & object_name,
const ReporterMode & mode,
const std::size_t time_index = 0);
const std::size_t time_index = 0) const;

/**
* Method for returning a read-only reference to Reporter values that already exist.
Expand Down Expand Up @@ -269,7 +268,7 @@ class ReporterData
* value is declared multiple times.
*/
template <typename T>
ReporterState<T> & getReporterStateHelper(const ReporterName & reporter_name, bool declare);
ReporterState<T> & getReporterStateHelper(const ReporterName & reporter_name, bool declare) const;
friend class VectorPostprocessorInterface;

/**
Expand All @@ -292,12 +291,14 @@ class ReporterData

/// Names of objects that have been declared
std::set<ReporterName> _declare_names;
std::set<ReporterName> _get_names;
/// Names of objects that have been requested. This is mutable so that names can be requested early
/// in const methods before they are declared
mutable std::set<ReporterName> _get_names;
};

template <typename T>
ReporterState<T> &
ReporterData::getReporterStateHelper(const ReporterName & reporter_name, bool declare)
ReporterData::getReporterStateHelper(const ReporterName & reporter_name, bool declare) const
{
// Creates the RestartableData object for storage in the MooseApp restart/recover system
auto data_ptr = libmesh_make_unique<ReporterState<T>>(reporter_name);
Expand All @@ -311,7 +312,7 @@ const T &
ReporterData::getReporterValue(const ReporterName & reporter_name,
const std::string & object_name,
const ReporterMode & mode,
const std::size_t time_index)
const std::size_t time_index) const
{
_get_names.insert(reporter_name);
ReporterState<T> & state_ref = getReporterStateHelper<T>(reporter_name, false);
Expand Down
12 changes: 2 additions & 10 deletions framework/include/userobject/UserObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,8 @@ class UserObject : public MooseObject,
virtual const PostprocessorValue & getPostprocessorValue(const std::string & name,
unsigned int index = 0) const
{
if (hasPostprocessor(name, index))
{
UserObjectName nm;
if (_pars.isSinglePostprocessor(name))
nm = _pars.get<PostprocessorName>(name);
else
nm = _pars.get<std::vector<PostprocessorName>>(name)[index];

_depend_uo.insert(nm);
}
if (!isDefaultPostprocessorValue(name, index)) // if default, no dependencies to add
_depend_uo.insert(getPostprocessorName(name, index));
return PostprocessorInterface::getPostprocessorValue(name, index);
}

Expand Down

0 comments on commit e421eed

Please sign in to comment.