LCOV - code coverage report
Current view: top level - src/functions - PiecewiseConstant.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 419b9d Lines: 47 63 74.6 %
Date: 2025-08-08 20:01:16 Functions: 3 8 37.5 %
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 "PiecewiseConstant.h"
      11             : #include "MathUtils.h"
      12             : 
      13             : registerMooseObject("MooseApp", PiecewiseConstant);
      14             : 
      15             : InputParameters
      16       14968 : PiecewiseConstant::validParams()
      17             : {
      18       14968 :   InputParameters params = PiecewiseTabularBase::validParams();
      19       14968 :   MooseEnum direction("LEFT RIGHT LEFT_INCLUSIVE RIGHT_INCLUSIVE", "LEFT");
      20       14968 :   params.addParam<MooseEnum>(
      21       29936 :       "direction", direction, "Direction to look to find value: " + direction.getRawNames());
      22       14968 :   params.addClassDescription("Defines data using a set of x-y data pairs");
      23       29936 :   return params;
      24       14968 : }
      25             : 
      26         356 : PiecewiseConstant::PiecewiseConstant(const InputParameters & parameters)
      27             :   : PiecewiseTabularBase(parameters),
      28         356 :     _direction(getParam<MooseEnum>("direction").getEnum<Direction>())
      29             : {
      30         348 : }
      31             : 
      32             : Real
      33       15037 : PiecewiseConstant::value(Real t, const Point & p) const
      34             : {
      35       15037 :   const Real x = _has_axis ? p(_axis) : t;
      36             : 
      37       15037 :   unsigned i = 1;
      38       15037 :   const unsigned len = functionSize();
      39       15037 :   const Real tolerance = 1.0e-14;
      40             : 
      41             :   // endpoint cases
      42       38295 :   if ((_direction == Direction::LEFT &&
      43        8221 :        x < (1 + tolerance * MathUtils::sign(domain(0))) * domain(0)) ||
      44       14377 :       (_direction == Direction::RIGHT &&
      45        6312 :        x < (1 - tolerance * MathUtils::sign(domain(0))) * domain(0)) ||
      46       13617 :       (_direction == Direction::LEFT_INCLUSIVE &&
      47       30302 :        x < (1 - tolerance * MathUtils::sign(domain(0))) * domain(0)) ||
      48       13593 :       (_direction == Direction::RIGHT_INCLUSIVE &&
      49         252 :        x < (1 + tolerance * MathUtils::sign(domain(0))) * domain(0)))
      50        1480 :     return _scale_factor * range(0);
      51       34675 :   else if ((_direction == Direction::LEFT &&
      52        7561 :             x > (1 + tolerance * MathUtils::sign(domain(len - 1))) * domain(len - 1)) ||
      53       10151 :            (_direction == Direction::RIGHT &&
      54        5552 :             x > (1 - tolerance * MathUtils::sign(domain(len - 1))) * domain(len - 1)) ||
      55        7779 :            (_direction == Direction::LEFT_INCLUSIVE &&
      56       27306 :             x > (1 - tolerance * MathUtils::sign(domain(len - 1))) * domain(len - 1)) ||
      57        7743 :            (_direction == Direction::RIGHT_INCLUSIVE &&
      58         216 :             x > (1 + tolerance * MathUtils::sign(domain(len - 1))) * domain(len - 1)))
      59        5838 :     return _scale_factor * range(len - 1);
      60             : 
      61       13283 :   for (; i < len; ++i)
      62             :   {
      63       19698 :     if (_direction == Direction::LEFT &&
      64        6415 :         x < (1 + tolerance * MathUtils::sign(domain(i))) * domain(i))
      65        4155 :       return _scale_factor * range(i - 1);
      66        9884 :     else if (_direction == Direction::LEFT_INCLUSIVE &&
      67         756 :              x < (1 - tolerance * MathUtils::sign(domain(i))) * domain(i))
      68         192 :       return _scale_factor * range(i - 1);
      69       14268 :     else if ((_direction == Direction::RIGHT &&
      70        5332 :               x < (1 - tolerance * MathUtils::sign(domain(i))) * domain(i)))
      71        3180 :       return _scale_factor * range(i);
      72        6536 :     else if ((_direction == Direction::RIGHT_INCLUSIVE &&
      73         780 :               x < (1 + tolerance * MathUtils::sign(domain(i))) * domain(i)))
      74         192 :       return _scale_factor * range(i);
      75             :   }
      76             : 
      77           0 :   return 0.0;
      78             : }
      79             : 
      80             : ADReal
      81           0 : PiecewiseConstant::value(const ADReal & t, const ADPoint & p) const
      82             : {
      83             :   // piecewise constant has all zero derivatives (ignoring discontinuities)
      84           0 :   return value(MetaPhysicL::raw_value(t), MetaPhysicL::raw_value(p));
      85             : }
      86             : 
      87             : Real
      88           0 : PiecewiseConstant::timeDerivative(Real /*t*/, const Point & /*p*/) const
      89             : {
      90           0 :   return 0;
      91             : }
      92             : 
      93             : Real
      94           0 : PiecewiseConstant::integral() const
      95             : {
      96           0 :   const unsigned len = functionSize();
      97           0 :   Real sum = 0;
      98           0 :   unsigned offset = 0;
      99             : 
     100           0 :   if (_direction == Direction::RIGHT || _direction == Direction::RIGHT_INCLUSIVE)
     101           0 :     offset = 1;
     102             : 
     103           0 :   for (unsigned i = 0; i < len - 1; ++i)
     104           0 :     sum += range(i + offset) * (domain(i + 1) - domain(i));
     105             : 
     106           0 :   return _scale_factor * sum;
     107             : }
     108             : 
     109             : Real
     110           0 : PiecewiseConstant::average() const
     111             : {
     112           0 :   return integral() / (domain(functionSize() - 1) - domain(0));
     113             : }

Generated by: LCOV version 1.14