LCOV - code coverage report
Current view: top level - src/userobjects - StepUserObject.C (source / functions) Hit Total Coverage
Test: idaholab/moose tensor_mechanics: d6b47a Lines: 55 61 90.2 %
Date: 2024-02-27 11:53:14 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://www.mooseframework.org
       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 "StepUserObject.h"
      11             : #include <limits>
      12             : #include <algorithm>
      13             : 
      14             : registerMooseObject("TensorMechanicsApp", StepUserObject);
      15             : 
      16             : InputParameters
      17          92 : StepUserObject::validParams()
      18             : {
      19          92 :   InputParameters params = GeneralUserObject::validParams();
      20         184 :   params.addParam<std::vector<Real>>(
      21             :       "step_start_times",
      22             :       "The beginning of step times. The number of steps is inferred from the number of times. One "
      23             :       "step is defined by its start time; and its end time is taken from the start time of the "
      24             :       "next step (if it exists). This list needs to be in ascending value order.");
      25             : 
      26         184 :   params.addParam<Real>("total_time_interval",
      27             :                         "The total time interval in which the steps take place. This option needs "
      28             :                         "to be used together with the 'number_steps'.");
      29         184 :   params.addParam<unsigned int>(
      30             :       "number_steps",
      31             :       "Total number of steps in the total time inteval (provided as total_time_interval).");
      32             : 
      33         184 :   params.addParam<std::vector<Real>>(
      34             :       "step_durations",
      35             :       "The durations of the steps. 'n' of step time intervals define 'n+1' steps "
      36             :       "starting at time equals zero.");
      37             : 
      38          92 :   return params;
      39           0 : }
      40             : 
      41          46 : StepUserObject::StepUserObject(const InputParameters & parameters)
      42             :   : GeneralUserObject(parameters),
      43          92 :     _times(0),
      44          46 :     _step_durations(0),
      45          46 :     _total_time_interval(0),
      46          46 :     _number_steps(0)
      47             : {
      48          46 :   const bool is_step_times = isParamSetByUser("step_start_times");
      49             :   const bool is_interval_and_steps =
      50          98 :       isParamSetByUser("total_time_interval") && isParamSetByUser("number_steps");
      51          46 :   const bool is_step_durations = isParamSetByUser("step_durations");
      52             : 
      53          46 :   if (is_step_times)
      54             :   {
      55         120 :     _times = getParam<std::vector<Real>>("step_start_times");
      56          40 :     if (!std::is_sorted(_times.begin(), _times.end()))
      57           0 :       paramError(
      58             :           "step_start_times",
      59             :           "start times for StepUserObject are not provided in ascending order. Please revise "
      60             :           "your input.");
      61             : 
      62             :     mooseInfo("Step start times are used to define simulation steps in ", name(), ".");
      63             :   }
      64           6 :   else if (is_interval_and_steps)
      65             :   {
      66           6 :     _total_time_interval = getParam<Real>("total_time_interval");
      67           6 :     _number_steps = getParam<unsigned int>("number_steps");
      68           3 :     _times.resize(_number_steps + 1);
      69             : 
      70          12 :     for (const auto i : index_range(_times))
      71             :     {
      72           9 :       if (i == 0)
      73           3 :         _times[0] = 0.0;
      74             :       else
      75           6 :         _times[i] = _total_time_interval / _number_steps + _times[i - 1];
      76             :     }
      77             : 
      78             :     mooseInfo("The total time interval and the provided number of steps are used to define "
      79             :               "simulation steps in ",
      80             :               name(),
      81             :               ".");
      82             :   }
      83           3 :   else if (is_step_durations)
      84             :   {
      85           9 :     _step_durations = getParam<std::vector<Real>>("step_durations");
      86           3 :     _times.resize(_step_durations.size() + 1);
      87           9 :     for (const auto i : index_range(_times))
      88             :     {
      89           6 :       if (i == 0)
      90           3 :         _times[i] = 0.0;
      91             :       else
      92           3 :         _times[i] = _step_durations[i - 1] + _times[i - 1];
      93             :     }
      94             :     mooseInfo("Step durations are used to define simulation steps in ", name(), ".");
      95             :   }
      96             :   else
      97           0 :     paramError("step_start_times",
      98             :                "Plese provide 'step_start_times' or 'step_durations' or 'total_time_interval' and "
      99             :                "'number_steps' to define simulation loading steps.");
     100          46 : }
     101             : 
     102             : Real
     103         780 : StepUserObject::getStartTime(const unsigned int & step) const
     104             : {
     105         780 :   if (_times.size() <= step)
     106           0 :     mooseError("StepUserObject was called with a wrong step number");
     107             : 
     108         780 :   return _times[step];
     109             : }
     110             : 
     111             : Real
     112          30 : StepUserObject::getEndTime(const unsigned int & step) const
     113             : {
     114             :   Real end_time(0);
     115             : 
     116          30 :   if (_times.size() > step + 1)
     117          18 :     end_time = _times[step + 1];
     118          12 :   else if (_times.size() == step + 1)
     119             :     end_time = std::numeric_limits<double>::max();
     120             :   else
     121           0 :     mooseError("StepUserObject was called with a wrong step number");
     122             : 
     123          30 :   return end_time;
     124             : }
     125             : 
     126             : unsigned int
     127         809 : StepUserObject::getStep(const Real & time) const
     128             : {
     129             :   int which_step = 0;
     130             : 
     131        1482 :   for (const auto i : index_range(_times))
     132             :   {
     133        1482 :     if (i + 1 == _times.size())
     134         505 :       return i;
     135             : 
     136         977 :     which_step = i;
     137         977 :     if (time >= _times[i] && time < _times[i + 1])
     138         304 :       return which_step;
     139             :   }
     140             : 
     141           0 :   return which_step;
     142             : }
     143             : 
     144             : void
     145         211 : StepUserObject::initialize()
     146             : {
     147         211 : }
     148             : void
     149         211 : StepUserObject::execute()
     150             : {
     151         211 : }
     152             : void
     153         211 : StepUserObject::finalize()
     154             : {
     155         211 : }

Generated by: LCOV version 1.14