LCOV - code coverage report
Current view: top level - src/times - Times.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 62 69 89.9 %
Date: 2026-05-29 20:35:17 Functions: 9 9 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 "Times.h"
      11             : #include "libmesh/parallel_algebra.h"
      12             : 
      13             : InputParameters
      14       24979 : Times::validParams()
      15             : {
      16       24979 :   InputParameters params = GeneralReporter::validParams();
      17       74937 :   params.addParam<bool>(
      18       49958 :       "unique_times", true, "Whether duplicate values should be removed from the Times vector");
      19       74937 :   params.addParam<Real>(
      20             :       "unique_tolerance",
      21       49958 :       1e-12,
      22             :       "Absolute tolerance for removing duplicated times when getting a set of unique times");
      23       99916 :   params.addParam<bool>("auto_sort", true, "Whether Times should be sorted");
      24             :   // This parameter should be set by each derived class depending on whether the generation of
      25             :   // times is replicated or distributed. We want times to be replicated across all ranks
      26       99916 :   params.addRequiredParam<bool>("auto_broadcast",
      27             :                                 "Wether Times should be broadcasted across all ranks");
      28       99916 :   params.addParamNamesToGroup("auto_broadcast auto_sort unique_times unique_tolerance", "Advanced");
      29             : 
      30             :   // Unlikely to ever be used as Times do not loop over the mesh and use material properties,
      31             :   // let alone stateful
      32       49958 :   params.suppressParameter<MaterialPropertyName>("prop_getter_suffix");
      33       49958 :   params.suppressParameter<bool>("use_interpolated_state");
      34             : 
      35       74937 :   params.addParam<bool>("dynamic_time_sequence",
      36       49958 :                         true,
      37             :                         "Whether the time sequence is dynamic and thus needs to be updated");
      38             : 
      39       24979 :   params.registerBase("Times");
      40       24979 :   return params;
      41           0 : }
      42             : 
      43         247 : Times::Times(const InputParameters & parameters)
      44             :   : GeneralReporter(parameters),
      45             :     // Times will be replicated on every rank
      46         247 :     _times(declareValueByName<std::vector<Real>, ReporterVectorContext<Real>>(
      47             :         "times", REPORTER_MODE_REPLICATED)),
      48         494 :     _need_broadcast(getParam<bool>("auto_broadcast")),
      49         494 :     _need_sort(getParam<bool>("auto_sort")),
      50         494 :     _need_unique(getParam<bool>("unique_times")),
      51         494 :     _unique_tol(getParam<Real>("unique_tolerance")),
      52         741 :     _dynamic_time_sequence(getParam<bool>("dynamic_time_sequence"))
      53             : {
      54         247 : }
      55             : 
      56             : Real
      57          48 : Times::getTimeAtIndex(unsigned int index) const
      58             : {
      59          48 :   if (_times.size() < index)
      60           0 :     mooseError("Times retrieved with an out-of-bound index");
      61             : 
      62          48 :   if (_times.size())
      63          48 :     return _times[index];
      64             :   else
      65           0 :     mooseError("Times vector has not been initialized.");
      66             : }
      67             : 
      68             : Real
      69         490 : Times::getPreviousTime(const Real current_time) const
      70             : {
      71         490 :   if (!_times.size())
      72           0 :     mooseError("Times vector has not been initialized.");
      73             : 
      74         490 :   Real previous_time = _times[0];
      75        1358 :   for (const auto i : make_range(std::size_t(1), _times.size()))
      76             :   {
      77        1314 :     const auto & time = _times[i];
      78        1314 :     if (MooseUtils::absoluteFuzzyGreaterThan(time, current_time))
      79         446 :       return previous_time;
      80         868 :     previous_time = time;
      81             :   }
      82             :   // entire Times vector is prior to current_time
      83          44 :   return previous_time;
      84             : }
      85             : 
      86             : Real
      87         512 : Times::getNextTime(const Real current_time, const bool error_if_no_next) const
      88             : {
      89        1958 :   for (const auto i : index_range(_times))
      90             :   {
      91        1914 :     const auto & time = _times[i];
      92        1914 :     if (MooseUtils::absoluteFuzzyGreaterThan(time, current_time))
      93         468 :       return time;
      94             :   }
      95          44 :   if (_times.size() && error_if_no_next)
      96           0 :     mooseError("No next time in Times vector for time ",
      97             :                current_time,
      98             :                ". Maximum time in vector is ",
      99           0 :                *_times.rbegin());
     100          44 :   else if (!error_if_no_next)
     101          44 :     return std::numeric_limits<Real>::max();
     102             :   else
     103           0 :     mooseError("Times vector has not been initialized.");
     104             : }
     105             : 
     106             : const std::vector<Real> &
     107         328 : Times::getTimes() const
     108             : {
     109         328 :   if (_times.size())
     110         326 :     return _times;
     111             :   else
     112             :   {
     113           2 :     mooseError("Times vector has not been initialized.");
     114             :   }
     115             : }
     116             : 
     117             : void
     118          34 : Times::clearTimes()
     119             : {
     120          34 :   _times.clear();
     121          34 : }
     122             : 
     123             : void
     124         789 : Times::finalize()
     125             : {
     126         789 :   if (_need_broadcast)
     127             :     // The consumer/producer reporter interface can keep track of whether a reduction is needed
     128             :     // (for example if a consumer needs replicated data, but the producer is distributed) however,
     129             :     // we have currently made the decision that times should ALWAYS be replicated
     130          78 :     _communicator.allgather(_times, /* identical buffer lengths = */ false);
     131             : 
     132         789 :   if (_need_sort)
     133         687 :     std::sort(_times.begin(), _times.end());
     134         789 :   if (_need_unique)
     135        1578 :     _times.erase(unique(_times.begin(),
     136         789 :                         _times.end(),
     137        4402 :                         [this](Real l, Real r) { return std::abs(l - r) < _unique_tol; }),
     138        1578 :                  _times.end());
     139         789 : }

Generated by: LCOV version 1.14