LCOV - code coverage report
Current view: top level - src/postprocessors - ChangeOverTimePostprocessor.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 55 59 93.2 %
Date: 2026-05-29 20:35:17 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       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             : #include "ChangeOverTimePostprocessor.h"
      11             : #include "MooseUtils.h"
      12             : 
      13             : registerMooseObject("MooseApp", ChangeOverTimePostprocessor);
      14             : 
      15             : InputParameters
      16        6320 : ChangeOverTimePostprocessor::validParams()
      17             : {
      18        6320 :   InputParameters params = GeneralPostprocessor::validParams();
      19             : 
      20       25280 :   params.addRequiredParam<PostprocessorName>("postprocessor", "The name of the postprocessor");
      21       18960 :   params.addParam<bool>("change_with_respect_to_initial",
      22       12640 :                         false,
      23             :                         "Compute change with respect to initial value instead of previous value");
      24       18960 :   params.addParam<bool>(
      25       12640 :       "compute_relative_change", false, "Compute magnitude of relative change instead of change");
      26       25280 :   params.addParam<bool>("take_absolute_value", false, "Option to take absolute value of change");
      27       18960 :   params.addParam<bool>("divide_by_dt",
      28       12640 :                         false,
      29             :                         "If true, divide by the time step size. This may only be enabled if "
      30             :                         "'change_with_respect_to_initial' is 'false'.");
      31             : 
      32        6320 :   params.addClassDescription("Computes the change or relative change in a post-processor value "
      33             :                              "over a timestep or the entire transient");
      34             : 
      35        6320 :   return params;
      36           0 : }
      37             : 
      38         102 : ChangeOverTimePostprocessor::ChangeOverTimePostprocessor(const InputParameters & parameters)
      39             :   : GeneralPostprocessor(parameters),
      40         102 :     _change_with_respect_to_initial(getParam<bool>("change_with_respect_to_initial")),
      41         204 :     _compute_relative_change(getParam<bool>("compute_relative_change")),
      42         204 :     _take_absolute_value(getParam<bool>("take_absolute_value")),
      43         204 :     _divide_by_dt(getParam<bool>("divide_by_dt")),
      44         204 :     _pps_value(getPostprocessorValue("postprocessor")),
      45         204 :     _pps_value_old(getPostprocessorValueOld("postprocessor")),
      46         204 :     _pps_value_initial(declareRestartableData<Real>("pps_value_initial")),
      47         102 :     _value(0.0)
      48             : {
      49         102 :   if (_change_with_respect_to_initial)
      50             :   {
      51             :     // ensure dependent post-processor is executed on initial
      52         132 :     const PostprocessorName & pp_name = getParam<PostprocessorName>("postprocessor");
      53          66 :     const UserObject & pp = _fe_problem.getUserObject<UserObject>(pp_name);
      54          66 :     if (!pp.getExecuteOnEnum().isValueSet(EXEC_INITIAL))
      55           3 :       mooseError("When 'change_with_respect_to_initial' is specified to be true, 'execute_on' for "
      56           3 :                  "the dependent post-processor ('" +
      57           3 :                  pp_name + "') must include 'initial'");
      58             : 
      59             :     // ensure THIS post-processor is executed on initial
      60          63 :     if (!_execute_enum.isValueSet(EXEC_INITIAL))
      61           3 :       mooseError("When 'change_with_respect_to_initial' is specified to be true, 'execute_on' for "
      62           3 :                  "the ChangeOverTimePostprocessor ('" +
      63           6 :                  name() + "') must include 'initial'");
      64             : 
      65             :     // The quantity (y_n - y_{n-1})/dt_n has obvious physical relevance, but the
      66             :     // quantity (y_n - y_0)/dt_n has no obvious physical relevance, so we assume
      67             :     // that this is a mistake and do not allow it. This could be a warning instead if later needed.
      68          60 :     if (_divide_by_dt)
      69           0 :       mooseError(
      70             :           "The parameter 'divide_by_dt' may only be set to 'true' if "
      71             :           "'change_with_respect_to_initial' is 'false', since this is assumed to be unintended.");
      72             :   }
      73          96 : }
      74             : 
      75             : void
      76         633 : ChangeOverTimePostprocessor::initialize()
      77             : {
      78         633 : }
      79             : 
      80             : void
      81         633 : ChangeOverTimePostprocessor::execute()
      82             : {
      83         633 : }
      84             : 
      85             : void
      86         633 : ChangeOverTimePostprocessor::finalize()
      87             : {
      88             :   // copy initial value in case difference is measured against initial value
      89         633 :   if (_t_step == 0)
      90          88 :     _pps_value_initial = _pps_value;
      91             : 
      92             :   // determine value which change is measured against
      93             :   Real base_value;
      94         633 :   if (_change_with_respect_to_initial)
      95         522 :     base_value = _pps_value_initial;
      96             :   else
      97             :   {
      98             :     // Currently post-processors do not copy the values to old and older;
      99             :     // _pps_value_old will therefore always be zero for _t_step = 0.
     100         111 :     if (_t_step == 0)
     101          33 :       base_value = _pps_value;
     102             :     else
     103          78 :       base_value = _pps_value_old;
     104             :   }
     105             : 
     106             :   Real change;
     107         633 :   if (_compute_relative_change)
     108           0 :     change = (_pps_value - base_value) / base_value;
     109             :   else
     110         633 :     change = _pps_value - base_value;
     111             : 
     112         633 :   if (_take_absolute_value)
     113           0 :     _value = std::fabs(change);
     114             :   else
     115         633 :     _value = change;
     116             : 
     117             :   // Guard against division by zero; _dt=0 on INITIAL
     118         633 :   if (_divide_by_dt && !MooseUtils::absoluteFuzzyEqual(_dt, 0.0))
     119          55 :     _value /= _dt;
     120         633 : }
     121             : 
     122             : Real
     123         633 : ChangeOverTimePostprocessor::getValue() const
     124             : {
     125         633 :   return _value;
     126             : }

Generated by: LCOV version 1.14