Skip to content

Commit

Permalink
Clean up PP interface for default values and remove deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
loganharbour committed Apr 19, 2021
1 parent 73b5c75 commit 0fe9f60
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 292 deletions.
53 changes: 43 additions & 10 deletions framework/include/interfaces/PostprocessorInterface.h
Expand Up @@ -94,6 +94,17 @@ class PostprocessorInterface
bool isDefaultPostprocessorValue(const std::string & param_name,
const unsigned int index = 0) const;

/**
* Get the default value associated with a Postprocessor parameter.
* You should check isDefaultPostprocessorValue to verify that the parameter
* is a default value before calling this method.
* @param param_name The name of the Postprocessor parameter
* @param index The index of the postprocessor
* @return The default value
*/
PostprocessorValue getDefaultPostprocessorValue(const std::string & param_name,
const unsigned int index = 0) const;

/**
* Determine if the Postprocessor data exists
* @param param_name The name of the Postprocessor parameter
Expand Down Expand Up @@ -148,18 +159,40 @@ class PostprocessorInterface
/// Reference the the FEProblemBase class
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;
/// Holds the default postprocessor values that are requested (key is PostprocessorName)
mutable std::map<PostprocessorName, std::unique_ptr<PostprocessorValue>> _default_values;

/// Extract the value using parameter name
const PostprocessorValue & getPostprocessorValueHelper(const std::string & name,
unsigned int index,
std::size_t t_index) const;
/**
* Internal method for getting the PostprocessorName associated with a paremeter.
* Needed in order to allow the return of a name that is a default value.
*/
const PostprocessorName &
getPostprocessorNameInternal(const std::string & param_name,
const unsigned int index,
const bool allow_default_value = true) const;

/// Extract the value using stored name
const PostprocessorValue & getPostprocessorValueByNameHelper(const PostprocessorName & name,
std::size_t t_index) const;
/**
* Internal methods for getting Postprocessor values.
*/
///@{
const PostprocessorValue & getPostprocessorValueInternal(const std::string & param_name,
unsigned int index,
std::size_t t_index) const;
const PostprocessorValue & getPostprocessorValueByNameInternal(const PostprocessorName & name,
std::size_t t_index) const;
///@}

/**
* @returns True if the PostprocessorName \p name repesents a default value: the name
* converts to a value (set by addParam or set via input), and a Postprocessor does not
* exist with the same name (we do allow Postprocessors with numbered names...)
*/
bool isDefaultPostprocessorValueByName(const PostprocessorName & name) const;

/**
* @returns The default value stored in the PostprocessorName \p name.
*/
PostprocessorValue getDefaultPostprocessorValueByName(const PostprocessorName & name) const;

/**
* Checks the parameters relating to a Postprocessor. If \p index is not set, index
Expand Down
40 changes: 11 additions & 29 deletions framework/include/problems/FEProblemBase.h
Expand Up @@ -904,10 +904,20 @@ class FEProblemBase : public SubProblem, public Restartable
*/
void initPostprocessorData(const std::string &) {}

/**
* Whether or not a Postprocessor value exists by a given name.
* @param name The name of the Postprocessor
* @return True if a Postprocessor value exists
*
* Note: You should prioritize the use of PostprocessorInterface::hasPostprocessor
* and PostprocessorInterface::hasPostprocessorByName over this method when possible.
*/
bool hasPostprocessorValueByName(const PostprocessorName & name) const;

/**
* Get a read-only reference to the value associated with a Postprocessor that exists.
* @param name The name of the post-processor
* @partm t_index Flag for getting current (0), old (1), or older (2) values
* @param t_index Flag for getting current (0), old (1), or older (2) values
* @return The reference to the value at the given time index
*
* Note: This method is only for retrieving values that already exist, the Postprocessor and
Expand Down Expand Up @@ -936,16 +946,6 @@ class FEProblemBase : public SubProblem, public Restartable
std::size_t t_index = 0);
///@}

///@{
/**
* Deprecated
*/
bool hasPostprocessor(const std::string & name) const;
PostprocessorValue & getPostprocessorValue(const PostprocessorName & name);
PostprocessorValue & getPostprocessorValueOld(const std::string & name);
PostprocessorValue & getPostprocessorValueOlder(const std::string & name);
///@}

/**
* Get a read-only reference to the vector value associated with the VectorPostprocessor.
* @param object_name The name of the VPP object.
Expand Down Expand Up @@ -984,24 +984,6 @@ class FEProblemBase : public SubProblem, public Restartable
const VectorPostprocessor & getVectorPostprocessorObjectByName(const std::string & object_name,
THREAD_ID tid = 0) const;

///@{
/**
* DEPRECATED
*/
bool hasVectorPostprocessor(const std::string & name);
VectorPostprocessorValue & getVectorPostprocessorValue(const VectorPostprocessorName & name,
const std::string & vector_name);
VectorPostprocessorValue & getVectorPostprocessorValueOld(const std::string & name,
const std::string & vector_name);
VectorPostprocessorValue & getVectorPostprocessorValue(const VectorPostprocessorName & name,
const std::string & vector_name,
bool needs_broadcast);
VectorPostprocessorValue & getVectorPostprocessorValueOld(const std::string & name,
const std::string & vector_name,
bool needs_broadcast);
bool vectorPostprocessorHasVectors(const std::string & vpp_name);
///@}

///@{
/**
* Returns whether or not the current simulation has any multiapps
Expand Down
65 changes: 48 additions & 17 deletions framework/include/reporters/ReporterData.h
Expand Up @@ -18,6 +18,7 @@
#include "libmesh/auto_ptr.h"

class MooseApp;
class Receiver;

/**
* This is a helper class for managing the storage of declared Reporter object values. This design
Expand Down Expand Up @@ -60,6 +61,7 @@ class ReporterData
WriteKey() {} // private constructor
friend class Reporter;
friend class Postprocessor;
friend class Receiver;
friend class VectorPostprocessor;
friend class ReporterTransferInterface;
};
Expand Down Expand Up @@ -144,13 +146,23 @@ class ReporterData
const T & value,
const std::size_t time_index = 0);

/**
* Method for setting that a specific time index is requested for a Reporter value.
* @tparam T The Reporter value C++ type.
* @param reporter_name The name of the reporter value, which includes the object name and the
* data name.
* @param time_index The time index that is needed
*/
template <typename T>
void needReporterTimeIndex(const ReporterName & reporter_name, const std::size_t time_index);

///@{
/**
* Method for returning a writable reference to the current Reporter value. This method is
* used by the Reporter class to produce values.
* @tparam T The Reporter value C++ type.
* @tparam S (optional) The ReporterContext for performing specialized actions after the values
* have been computed. For example, ReporterBroadcastContext automatically broadcasts
* @tparam S (optional) The ReporterContext for performing specialized actions after the
* values have been computed. For example, ReporterBroadcastContext automatically broadcasts
* the computed value. See ReporterState.C/h for more information.
* @param reporter_name The name of the reporter value, which includes the object name and the
* data name.
Expand All @@ -160,12 +172,13 @@ class ReporterData
* by the S template parameter. If S = ReporterContext then the first argument
* can be used as the default value (see ReporterContext.h).
*
* The ReporterContext objects allow for custom handling of data (e.g., broadcasting the value).
* The get/declare methods can be called in any order thus an the underlying RestartableData
* object is often created by the get method before it is declared. Therefore the custom
* functionality cannot be handled by specializing the RestartableData/ReporterState object
* directly because the state is often created prior to the declaration that dictates how the
* produced value shall be computed. Thus, the reason for the separate ReporterContext objects.
* The ReporterContext objects allow for custom handling of data (e.g., broadcasting the
* value). The get/declare methods can be called in any order thus an the underlying
* RestartableData object is often created by the get method before it is declared. Therefore
* the custom functionality cannot be handled by specializing the
* RestartableData/ReporterState object directly because the state is often created prior to
* the declaration that dictates how the produced value shall be computed. Thus, the reason
* for the separate ReporterContext objects.
*/
template <typename T, typename S, typename... Args>
T & declareReporterValue(const ReporterName & reporter_name,
Expand Down Expand Up @@ -258,7 +271,7 @@ class ReporterData
template <typename T>
ReporterState<T> & getReporterStateHelper(const ReporterName & reporter_name,
bool declare,
const MooseObject & moose_object) const;
const MooseObject * moose_object = nullptr) const;

/**
* Helper for registering data with the MooseApp to avoid cyclic includes
Expand All @@ -283,7 +296,7 @@ template <typename T>
ReporterState<T> &
ReporterData::getReporterStateHelper(const ReporterName & reporter_name,
bool declare,
const MooseObject & moose_object) const
const MooseObject * moose_object /* = nullptr */) const
{
if (hasReporterState(reporter_name))
{
Expand All @@ -298,7 +311,10 @@ ReporterData::getReporterStateHelper(const ReporterName & reporter_name,
oss << ",\na Reporter with the same name " << suffix << ".\n\n";
oss << getReporterInfo(reporter_name);

moose_object.mooseError(oss.str());
if (moose_object)
moose_object->mooseError(oss.str());
else
mooseError(oss.str());
};

if (declare && hasReporterValue(reporter_name))
Expand Down Expand Up @@ -330,6 +346,12 @@ ReporterData::getReporterStateHelper(const ReporterName & reporter_name,
// different special types are not unique so they'll be the same entry
_states.emplace(reporter_name, state);

if (declare)
{
mooseAssert(moose_object, "Declaring a Reporter requires a MooseObject as a producer");
state->setProducer(*moose_object);
}

return *state;
}

Expand All @@ -340,7 +362,7 @@ ReporterData::getReporterValue(const ReporterName & reporter_name,
const ReporterMode & mode,
const std::size_t time_index /* = 0 */) const
{
auto & state = getReporterStateHelper<T>(reporter_name, /* declare = */ false, consumer);
auto & state = getReporterStateHelper<T>(reporter_name, /* declare = */ false, &consumer);
state.addConsumer(mode, consumer);
return state.value(time_index);
}
Expand All @@ -353,8 +375,7 @@ ReporterData::declareReporterValue(const ReporterName & reporter_name,
Args &&... args)
{
// Get/create the ReporterState
auto & state = getReporterStateHelper<T>(reporter_name, /* declare = */ true, producer);
state.setProducer(producer);
auto & state = getReporterStateHelper<T>(reporter_name, /* declare = */ true, &producer);

// They key in _states (ReporterName) is not unique. This is done on purpose
// because we want to store reporter names a single name regardless of type.
Expand Down Expand Up @@ -419,10 +440,11 @@ ReporterData::getReporterValue(const ReporterName & reporter_name,
reporter_name,
"\" with type \"",
MooseUtils::prettyCppType<T>(),
" is not declared.");
"\" is not declared.");

return static_cast<const ReporterContext<T> *>(&getReporterContextBase(reporter_name))
->state()
// Force the const version of value, which does not allow for increasing time index
return static_cast<const ReporterState<T> &>(
getReporterStateHelper<T>(reporter_name, /* declare = */ false))
.value(time_index);
}

Expand All @@ -437,6 +459,15 @@ ReporterData::setReporterValue(const ReporterName & reporter_name,
const_cast<T &>(me.getReporterValue<T>(reporter_name, time_index)) = value;
}

template <typename T>
void
ReporterData::needReporterTimeIndex(const ReporterName & reporter_name,
const std::size_t time_index)
{
getReporterValue<T>(reporter_name, 0); // for error checking that it is declared
getReporterStateHelper<T>(reporter_name, /* declare = */ false).value(time_index);
}

// This is defined here to avoid cyclic includes, see ReporterContext.h
template <typename T>
void
Expand Down
21 changes: 0 additions & 21 deletions framework/include/utils/InputParameters.h
Expand Up @@ -646,27 +646,6 @@ class InputParameters : public Parameters
*/
std::map<std::string, std::pair<std::string, std::string>> getAutoBuildVectors() const;

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

/**
* Get the default value for a postprocessor added with addPostprocessor
*
* This does _not_ return a reference. If you want to hand it out as a reference, you need
* to store it on your own.
*
* @param param_name The name of the postprocessor parameter
* @param index The index in the default postprocessor vector
* @return The default value for the postprocessor
*/
Real getDefaultPostprocessorValue(const std::string & param_name,
const unsigned int index = 0) const;

// BEGIN APPLY PARAMETER METHODS
/**
* Method for applying common parameters
Expand Down

0 comments on commit 0fe9f60

Please sign in to comment.