Skip to content

Commit

Permalink
Simplify current value, add docstrings, rename method
Browse files Browse the repository at this point in the history
- Adds lots of documentation as to getValue vs getCurrentValue
- Removes unnecessary headers
- Stores the value at construction time instead of doing a map lookup
- Moves extraneous methods to source

refs idaholab#25012
  • Loading branch information
loganharbour authored and oanaoana committed Oct 19, 2023
1 parent 8f89725 commit 53cc592
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 48 deletions.
57 changes: 36 additions & 21 deletions framework/include/postprocessors/Postprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
#pragma once

#include "OutputInterface.h"
#include "FEProblemBase.h"
#include "NonADFunctorInterface.h"
#include "libmesh/parallel.h"

class MooseObject;

/**
* Base class for all Postprocessors. Defines a name and sets up the
* virtual getValue() interface which must be overridden by derived
Expand All @@ -29,43 +30,64 @@ class Postprocessor : public OutputInterface,
Postprocessor(const MooseObject * moose_object);

/**
* This will get called to actually grab the final value the postprocessor has calculated.
* This will get called to actually grab the final value the postprocessor has calculated
*
* Note that this should only be called by internal methods, namely the problem that
* actually sets the value globally for other things to use. If you want the value
* outside of one of these external methods, you should use getCurrentValue().
*
* This method will be removed in favor of the const version.
*/
virtual PostprocessorValue getValue();

/**
* This will get called to actually grab the final value the postprocessor has calculated.
*
* Note that this should only be called by internal methods, namely the problem that
* actually sets the value globally for other things to use. If you want the value
* outside of one of these external methods, you should use getCurrentValue().
*/
virtual PostprocessorValue getValue() const;

/**
* Gets the current value stored in the ReporterData.
* @return The "current" value of this Postprocessor.
*
* Your sanity would tell you... why not just call getValue()? Well - the intention
* of getValue() is to be called by the problem when the UserObjects are executed,
* and not by other things. This enables the control of _when_ this Postprocessor
* is updated, which could be very important. If the implementation of getValue() is
* such that it actually computes a new value (instead of one that is called in
* finalize()), you could potentially call getValue() and not get the value as it
* was at the last time this PP was executed.
*
* In most (but not all) cases, this should return the same as \c getValue().
* What this does instead is gives you the value that was last set as this PP was
* executed by the problem. That is, the value that every object that uses the
* PostprocessorInterface will get you.
*/
PostprocessorValue getCurrentReporterValue() const;
const PostprocessorValue & getCurrentValue() const { return _current_value; }

/**
* Returns the name of the Postprocessor.
*/
std::string PPName() const { return _pp_name; }
const std::string & PPName() const { return _pp_name; }

protected:
/// MOOSE object
const MooseObject & _pp_moose_object;

/// Post-processor name
const std::string _pp_name;
const std::string & _pp_name;

/// FE problem
FEProblemBase & _pp_fe_problem;

/// Reporter data
ReporterData & _reporter_data;
/// The current value, which is the Reporter value that changes when we execute UOs in the problem)
const PostprocessorValue & _current_value;

private:
/**
* Internal method to be used to declare the value and store it within _current_value in the
* constructor.
*/
const PostprocessorValue & declareValue();

using ElemArg = Moose::ElemArg;
using ElemQpArg = Moose::ElemQpArg;
using ElemSideQpArg = Moose::ElemSideQpArg;
Expand Down Expand Up @@ -100,14 +122,7 @@ class Postprocessor : public OutputInterface,
const Moose::StateArg & state) const override final;

/**
* Gives a one-time warning for calling an \c evaluateDot() method.
* Internal method for giving a one-time warning for calling an \c evaluateDot() method.
*/
void evaluateDotWarning() const
{
mooseDoOnce(
mooseWarning("The time derivative functor operator was called on the post-processor '",
_pp_name,
"'. A zero value will always be returned, even if the post-processor value "
"changes with time."));
}
void evaluateDotWarning() const;
};
63 changes: 36 additions & 27 deletions framework/src/postprocessors/Postprocessor.C
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,33 @@ Postprocessor::Postprocessor(const MooseObject * moose_object)
NonADFunctorInterface(moose_object),
Moose::FunctorBase<Real>(moose_object->name()),
_pp_moose_object(*moose_object),
_pp_name(moose_object->name()),
_pp_fe_problem(
*moose_object->parameters().getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")),
_reporter_data(_pp_fe_problem.getReporterData(ReporterData::WriteKey()))
_pp_name(_pp_moose_object.name()),
_current_value(declareValue())
{
}

const PostprocessorValue &
Postprocessor::declareValue()
{
auto & fe_problem =
*_pp_moose_object.parameters().getCheckedPointerParam<FEProblemBase *>("_fe_problem_base");

const PostprocessorReporterName r_name(_pp_name);

const bool is_thread_0 = _pp_moose_object.parameters().get<THREAD_ID>("_tid") == 0;
mooseAssert(is_thread_0 ==
!fe_problem.getReporterData().hasReporterValue<PostprocessorValue>(r_name),
"Postprocessor Reporter threaded value declaration mismatch");

// Declare the Reporter value on thread 0 only; this lets us add error checking to
// make sure that it really is added only once
if (moose_object->parameters().get<THREAD_ID>("_tid") == 0)
{
mooseAssert(!_reporter_data.hasReporterValue<PostprocessorValue>(r_name),
"Postprocessor Reporter value is already declared");

_reporter_data
if (is_thread_0)
fe_problem.getReporterData(ReporterData::WriteKey())
.declareReporterValue<PostprocessorValue, ReporterGeneralContext<PostprocessorValue>>(
r_name, REPORTER_MODE_UNSET, *moose_object);
}
else
mooseAssert(_reporter_data.hasReporterValue<PostprocessorValue>(r_name),
"Postprocessor Reporter value is not declared");
r_name, REPORTER_MODE_UNSET, _pp_moose_object);

// At this point, thread 0 should have declared the value and getting it should be valid
return fe_problem.getReporterData().getReporterValue<PostprocessorValue>(r_name);
}

PostprocessorValue
Expand All @@ -67,43 +73,36 @@ Postprocessor::getValue() const
mooseError("getValue() (const or non-const) must be implemented.");
}

PostprocessorValue
Postprocessor::getCurrentReporterValue() const
{
return _reporter_data.getReporterValue<PostprocessorValue>(
PostprocessorReporterName(_pp_name), _pp_moose_object, REPORTER_MODE_ROOT);
}

typename Postprocessor::ValueType
Postprocessor::evaluate(const ElemArg & /*elem_arg*/, const Moose::StateArg & /*state*/) const
{
return getCurrentReporterValue();
return getCurrentValue();
}

typename Postprocessor::ValueType
Postprocessor::evaluate(const FaceArg & /*face*/, const Moose::StateArg & /*state*/) const
{
return getCurrentReporterValue();
return getCurrentValue();
}

typename Postprocessor::ValueType
Postprocessor::evaluate(const ElemQpArg & /*elem_qp*/, const Moose::StateArg & /*state*/) const
{
return getCurrentReporterValue();
return getCurrentValue();
}

typename Postprocessor::ValueType
Postprocessor::evaluate(const ElemSideQpArg & /*elem_side_qp*/,
const Moose::StateArg & /*state*/) const
{
return getCurrentReporterValue();
return getCurrentValue();
}

typename Postprocessor::ValueType
Postprocessor::evaluate(const ElemPointArg & /*elem_point_arg*/,
const Moose::StateArg & /*state*/) const
{
return getCurrentReporterValue();
return getCurrentValue();
}

typename Postprocessor::GradientType
Expand Down Expand Up @@ -176,3 +175,13 @@ Postprocessor::evaluateDot(const ElemPointArg & /*elem_point_arg*/,
evaluateDotWarning();
return 0;
}

void
Postprocessor::evaluateDotWarning() const
{
mooseDoOnce(
mooseWarning("The time derivative functor operator was called on the post-processor '",
_pp_name,
"'. A zero value will always be returned, even if the post-processor value "
"changes with time."));
}

0 comments on commit 53cc592

Please sign in to comment.