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

Refs idaholab#17512
  • Loading branch information
loganharbour committed Apr 7, 2021
1 parent 8b7394a commit e8f1494
Show file tree
Hide file tree
Showing 21 changed files with 312 additions and 215 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
69 changes: 38 additions & 31 deletions framework/include/interfaces/PostprocessorInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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 @@ -51,12 +51,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 @@ -79,24 +79,26 @@ 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) 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, unsigned int index = 0) const;

/**
* Determine if the Postprocessor data exists
Expand All @@ -107,14 +109,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 @@ -124,26 +118,32 @@ 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;

/// Extract the value using parameter name
const PostprocessorValue & getPostprocessorValueHelper(const std::string & name,
Expand All @@ -153,4 +153,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;
};
16 changes: 16 additions & 0 deletions framework/include/problems/FEProblemBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,12 @@ class FEProblemBase : public SubProblem, public Restartable
*/
ReporterData & getReporterData(ReporterData::WriteKey /*key*/) { return _reporter_data; }

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 @@ -2414,3 +2420,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);
}
11 changes: 5 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,12 @@ class ReporterData

/// Names of objects that have been declared
std::set<ReporterName> _declare_names;
std::set<ReporterName> _get_names;
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 +310,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
61 changes: 33 additions & 28 deletions framework/include/utils/InputParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,13 +649,11 @@ class InputParameters : public Parameters
/**
* Get the default value for a postprocessor added with addPostprocessor
* @param name The name of the postprocessor
* @param suppress_error If true, the error check is suppressed
* @param index The index in the default postprocessor vector
* @return The default value for the postprocessor
*/
const PostprocessorValue & getDefaultPostprocessorValue(const std::string & name,
bool suppress_error = false,
unsigned int index = 0) const;
const unsigned int index = 0) const;

/**
* Set the default value for a postprocessor added with addPostprocessor
Expand All @@ -668,12 +666,20 @@ class InputParameters : public Parameters
unsigned int index = 0);

/**
* Returns true if a default PostprocessorValue is defined
* Remove the default value for a postprocessor added with addPostprocessor. This is used when the
* set value is a PostprocessorName and not a user-set value.
* @param name The name of the postprocessor
* @param index The index in the default postprocessor vector
* @return True if a default value exists
*/
bool hasDefaultPostprocessorValue(const std::string & name, unsigned int index = 0) const;
void removeDefaultPostprocessorValue(const std::string & name, unsigned int index = 0);

/**
* Returns true if a default PostprocessorValue is a default value (not a name)
* @param name The name of the postprocessor
* @param index The index in the default postprocessor vector
* @return True if the postprocessor is a default value
*/
bool isDefaultPostprocessorValue(const std::string & name, unsigned int index = 0) const;

// BEGIN APPLY PARAMETER METHODS
/**
Expand Down Expand Up @@ -856,14 +862,10 @@ class InputParameters : public Parameters
bool shouldIgnore(const std::string & name);

/**
* Getter for the _vector_of_postprocessors flag in parameters
*
* @param pp_name The name of the postprocessor parameter
* @returns True if the parameter with name \p name is of type T.
*/
bool isSinglePostprocessor(const std::string & pp_name) const
{
return !_params.find(pp_name)->second._vector_of_postprocessors;
}
template <typename T>
bool isType(const std::string & name) const;

private:
// Private constructor so that InputParameters can only be created in certain places.
Expand Down Expand Up @@ -904,10 +906,10 @@ class InputParameters : public Parameters
bool _have_coupled_default = false;
/// The default value for optionally coupled variables
std::vector<Real> _coupled_default = {0};
/// are pps provided as single pp or as vector of pps
bool _vector_of_postprocessors = false;
std::vector<bool> _have_default_postprocessor_val = {false};
std::vector<PostprocessorValue> _default_postprocessor_val = {0};
/// Default values for postprocessors
std::vector<bool> _is_default_postprocessor_val = {false};
std::vector<PostprocessorValue> _default_postprocessor_val = {
-std::numeric_limits<Real>::max()};
/// True if a parameters value was set by addParam, and not set again.
bool _set_by_add_param = false;
/// The reserved option names for a parameter
Expand Down Expand Up @@ -972,15 +974,9 @@ class InputParameters : public Parameters
void reserveDefaultPostprocessorValueStorage(const std::string & name, unsigned int size);

/**
* Setter for the _vector_of_postprocessors flag in parameters
*
* @param pp_name The name of the postprocessor parameter
* @param b value that _vector_of_postprocessors is set to
* Internal helper for throwing errors when accessing postprocessor parameters
*/
void setVectorOfPostprocessors(const std::string & pp_name, bool b)
{
_params[pp_name]._vector_of_postprocessors = b;
}
void checkPostprocessorParam(const std::string & name, const unsigned int index) const;

/// original location of input block (i.e. filename,linenum) - used for nice error messages.
std::string _block_location;
Expand Down Expand Up @@ -1581,9 +1577,6 @@ void InputParameters::setParamHelper<MaterialPropertyName, int>(const std::strin
MaterialPropertyName & l_value,
const int & r_value);

template <>
void InputParameters::setHelper<std::vector<PostprocessorName>>(const std::string & name);

template <typename T>
const T &
InputParameters::getParamHelper(const std::string & name, const InputParameters & pars, const T *)
Expand Down Expand Up @@ -1631,3 +1624,15 @@ validParams()
// has *only* the new style validParams.
return T::validParams();
}

template <typename T>
bool
InputParameters::isType(const std::string & name) const
{
#ifndef LIBMESH_HAVE_RTTI
static_assert("Run time type info is required and is not available.");
#endif
if (!isParamValid(name))
mooseError("Parameter \"", name, "\" is not valid.");
return have_parameter<T>(name);
}

0 comments on commit e8f1494

Please sign in to comment.