LCOV - code coverage report
Current view: top level - src/functions - PiecewiseConstant.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 47 63 74.6 %
Date: 2025-07-17 01:28:37 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       14922 : PiecewiseConstant::validParams()
      17             : {
      18       14922 :   InputParameters params = PiecewiseTabularBase::validParams();
      19       14922 :   MooseEnum direction("LEFT RIGHT LEFT_INCLUSIVE RIGHT_INCLUSIVE", "LEFT");
      20       14922 :   params.addParam<MooseEnum>(
      21       29844 :       "direction", direction, "Direction to look to find value: " + direction.getRawNames());
      22       14922 :   params.addClassDescription("Defines data using a set of x-y data pairs");
      23       29844 :   return params;
      24       14922 : }
      25             : 
      26         333 : PiecewiseConstant::PiecewiseConstant(const InputParameters & parameters)
      27             :   : PiecewiseTabularBase(parameters),
      28         333 :     _direction(getParam<MooseEnum>("direction").getEnum<Direction>())
      29             : {
      30         325 : }
      31             : 
      32             : Real
      33       13603 : PiecewiseConstant::value(Real t, const Point & p) const
      34             : {
      35       13603 :   const Real x = _has_axis ? p(_axis) : t;
      36             : 
      37       13603 :   unsigned i = 1;
      38       13603 :   const unsigned len = functionSize();
      39       13603 :   const Real tolerance = 1.0e-14;
      40             : 
      41             :   // endpoint cases
      42       34577 :   if ((_direction == Direction::LEFT &&
      43        7371 :        x < (1 + tolerance * MathUtils::sign(domain(0))) * domain(0)) ||
      44       13014 :       (_direction == Direction::RIGHT &&
      45        5770 :        x < (1 - tolerance * MathUtils::sign(domain(0))) * domain(0)) ||
      46       12320 :       (_direction == Direction::LEFT_INCLUSIVE &&
      47       27415 :        x < (1 - tolerance * MathUtils::sign(domain(0))) * domain(0)) ||
      48       12298 :       (_direction == Direction::RIGHT_INCLUSIVE &&
      49         231 :        x < (1 + tolerance * MathUtils::sign(domain(0))) * domain(0)))
      50        1338 :     return _scale_factor * range(0);
      51       31312 :   else if ((_direction == Direction::LEFT &&
      52        6782 :             x > (1 + tolerance * MathUtils::sign(domain(len - 1))) * domain(len - 1)) ||
      53        9247 :            (_direction == Direction::RIGHT &&
      54        5076 :             x > (1 - tolerance * MathUtils::sign(domain(len - 1))) * domain(len - 1)) ||
      55        7166 :            (_direction == Direction::LEFT_INCLUSIVE &&
      56       24706 :             x > (1 - tolerance * MathUtils::sign(domain(len - 1))) * domain(len - 1)) ||
      57        7133 :            (_direction == Direction::RIGHT_INCLUSIVE &&
      58         198 :             x > (1 + tolerance * MathUtils::sign(domain(len - 1))) * domain(len - 1)))
      59        5154 :     return _scale_factor * range(len - 1);
      60             : 
      61       12212 :   for (; i < len; ++i)
      62             :   {
      63       18019 :     if (_direction == Direction::LEFT &&
      64        5807 :         x < (1 + tolerance * MathUtils::sign(domain(i))) * domain(i))
      65        3764 :       return _scale_factor * range(i - 1);
      66        9141 :     else if (_direction == Direction::LEFT_INCLUSIVE &&
      67         693 :              x < (1 - tolerance * MathUtils::sign(domain(i))) * domain(i))
      68         176 :       return _scale_factor * range(i - 1);
      69       13269 :     else if ((_direction == Direction::RIGHT &&
      70        4997 :               x < (1 - tolerance * MathUtils::sign(domain(i))) * domain(i)))
      71        2995 :       return _scale_factor * range(i);
      72        5992 :     else if ((_direction == Direction::RIGHT_INCLUSIVE &&
      73         715 :               x < (1 + tolerance * MathUtils::sign(domain(i))) * domain(i)))
      74         176 :       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