LCOV - code coverage report
Current view: top level - src/userobjects - AnalysisStepUserObject.C (source / functions) Hit Total Coverage
Test: idaholab/moose solid_mechanics: f45d79 Lines: 76 85 89.4 %
Date: 2025-07-25 05:00:39 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://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 "AnalysisStepUserObject.h"
      11             : #include "MooseUtils.h"
      12             : #include <limits>
      13             : #include <algorithm>
      14             : 
      15             : registerMooseObject("SolidMechanicsApp", AnalysisStepUserObject);
      16             : 
      17             : InputParameters
      18         232 : AnalysisStepUserObject::validParams()
      19             : {
      20         232 :   InputParameters params = GeneralUserObject::validParams();
      21         232 :   params.addClassDescription(
      22             :       "Maps the time steps and the load step simulation times. It can be "
      23             :       "used in either direction depending on the simulation setup. It "
      24             :       "generates steps to be used in StepPeriod to control the enabled/disabled state of objects "
      25             :       "with user-provided simulation steps.");
      26         464 :   params.addParam<std::vector<Real>>(
      27             :       "step_start_times",
      28             :       "The beginning of step times. The number of steps is inferred from the number of times. One "
      29             :       "step is defined by its start time; and its end time is taken from the start time of the "
      30             :       "next step (if it exists). This list needs to be in ascending value order.");
      31         464 :   params.addParam<std::vector<Real>>(
      32             :       "step_end_times",
      33             :       "The end of step times. The number of steps is inferred from the number of times. One "
      34             :       "step is defined by the interval between previous start time and the next. The first step "
      35             :       "is assumed to start at time zero. This list needs to be in ascending value order.");
      36         464 :   params.addParam<Real>("total_time_interval",
      37             :                         "The total time interval in which the steps take place. This option needs "
      38             :                         "to be used together with the 'number_steps'.");
      39         464 :   params.addParam<unsigned int>(
      40             :       "number_steps",
      41             :       "Total number of steps in the total time inteval (provided as total_time_interval).");
      42             : 
      43         464 :   params.addParam<std::vector<Real>>(
      44             :       "step_durations",
      45             :       "The durations of the steps. 'n' of step time intervals define 'n+1' steps "
      46             :       "starting at time equals zero.");
      47             : 
      48         464 :   params.addParam<bool>(
      49         464 :       "set_sync_times", false, "Whether to make the output times include the step times.");
      50             : 
      51         232 :   return params;
      52           0 : }
      53             : 
      54         116 : AnalysisStepUserObject::AnalysisStepUserObject(const InputParameters & parameters)
      55             :   : GeneralUserObject(parameters),
      56         232 :     _times(0),
      57         116 :     _step_durations(0),
      58         116 :     _total_time_interval(0),
      59         116 :     _number_steps(0)
      60             : {
      61         116 :   const bool is_step_start_times = isParamSetByUser("step_start_times");
      62         116 :   const bool is_step_end_times = isParamSetByUser("step_end_times");
      63             :   const bool is_interval_and_steps =
      64         244 :       isParamSetByUser("total_time_interval") && isParamSetByUser("number_steps");
      65         116 :   const bool is_step_durations = isParamSetByUser("step_durations");
      66             : 
      67             :   // check for valid user input
      68         116 :   if (int(is_step_start_times) + int(is_step_end_times) + int(is_interval_and_steps) +
      69         116 :           int(is_step_durations) >
      70             :       1)
      71           0 :     mooseError("In AnalysisStepUserObject, only one of 'step_start_times', 'step_end_times', "
      72             :                "'total_time_interval', and 'step_durations' can be set");
      73         470 :   if ((isParamSetByUser("total_time_interval") && !isParamSetByUser("number_steps")) ||
      74         568 :       (!isParamSetByUser("total_time_interval") && isParamSetByUser("number_steps")))
      75           0 :     mooseError("In AnalysisStepUserObject, both 'total_time_interval' and 'number_steps' need both "
      76             :                "be set.");
      77             : 
      78             :   // define step times
      79         116 :   if (is_step_start_times)
      80             :   {
      81         294 :     _times = getParam<std::vector<Real>>("step_start_times");
      82          98 :     if (!std::is_sorted(_times.begin(), _times.end()))
      83           0 :       paramError("step_start_times",
      84             :                  "start times for AnalysisStepUserObject are not provided in ascending order. "
      85             :                  "Please revise "
      86             :                  "your input.");
      87             : 
      88          98 :     mooseInfo("Step start times are used to define simulation steps in ", name(), ".");
      89             :   }
      90          18 :   else if (is_step_end_times)
      91             :   {
      92          18 :     _times = getParam<std::vector<Real>>("step_end_times");
      93           6 :     if (!std::is_sorted(_times.begin(), _times.end()))
      94           0 :       paramError(
      95             :           "step_end_times",
      96             :           "end times for AnalysisStepUserObject are not provided in ascending order. Please revise "
      97             :           "your input.");
      98           6 :     _times.insert(_times.begin(), 0.0);
      99             : 
     100           6 :     mooseInfo("Step start times are used to define simulation steps in ", name(), ".");
     101             :   }
     102          12 :   else if (is_interval_and_steps)
     103             :   {
     104          12 :     _total_time_interval = getParam<Real>("total_time_interval");
     105          12 :     _number_steps = getParam<unsigned int>("number_steps");
     106           6 :     _times.resize(_number_steps + 1);
     107             : 
     108          24 :     for (const auto i : index_range(_times))
     109             :     {
     110          18 :       if (i == 0)
     111           6 :         _times[0] = 0.0;
     112             :       else
     113          12 :         _times[i] = _total_time_interval / _number_steps + _times[i - 1];
     114             :     }
     115             : 
     116           6 :     mooseInfo("The total time interval and the provided number of steps are used to define "
     117             :               "simulation steps in ",
     118             :               name(),
     119             :               ".");
     120             :   }
     121           6 :   else if (is_step_durations)
     122             :   {
     123          18 :     _step_durations = getParam<std::vector<Real>>("step_durations");
     124           6 :     _times.resize(_step_durations.size() + 1);
     125          18 :     for (const auto i : index_range(_times))
     126             :     {
     127          12 :       if (i == 0)
     128           6 :         _times[i] = 0.0;
     129             :       else
     130           6 :         _times[i] = _step_durations[i - 1] + _times[i - 1];
     131             :     }
     132           6 :     mooseInfo("Step durations are used to define simulation steps in ", name(), ".");
     133             :   }
     134             :   else
     135           0 :     paramError("step_start_times",
     136             :                "Plese provide 'step_start_times' or 'step_durations' or 'total_time_interval' and "
     137             :                "'number_steps' to define simulation loading steps.");
     138             : 
     139             :   // set sync times
     140         232 :   if (getParam<bool>("set_sync_times"))
     141             :   {
     142           6 :     std::set<Real> & sync_times = _app.getOutputWarehouse().getSyncTimes();
     143          24 :     for (const auto t : _times)
     144          18 :       sync_times.insert(t);
     145             :   }
     146         116 : }
     147             : 
     148             : Real
     149        1632 : AnalysisStepUserObject::getStartTime(const unsigned int & step) const
     150             : {
     151        1632 :   if (_times.size() <= step)
     152           0 :     mooseError("AnalysisStepUserObject was called with a wrong step number");
     153             : 
     154        1632 :   return _times[step];
     155             : }
     156             : 
     157             : Real
     158          72 : AnalysisStepUserObject::getEndTime(const unsigned int & step) const
     159             : {
     160             :   Real end_time(0);
     161             : 
     162          72 :   if (_times.size() > step + 1)
     163          48 :     end_time = _times[step + 1];
     164          24 :   else if (_times.size() == step + 1)
     165             :     end_time = std::numeric_limits<double>::max();
     166             :   else
     167           0 :     mooseError("AnalysisStepUserObject was called with a wrong step number");
     168             : 
     169          72 :   return end_time;
     170             : }
     171             : 
     172             : unsigned int
     173        1888 : AnalysisStepUserObject::getStep(const Real & time) const
     174             : {
     175             :   int which_step = 0;
     176             : 
     177        3504 :   for (const auto i : index_range(_times))
     178             :   {
     179        3504 :     if (i + 1 == _times.size())
     180        1026 :       return i;
     181             : 
     182        2478 :     which_step = i;
     183        2478 :     if (MooseUtils::absoluteFuzzyGreaterEqual(time, _times[i]) &&
     184             :         MooseUtils::absoluteFuzzyLessThan(time, _times[i + 1]))
     185         862 :       return which_step;
     186             :   }
     187             : 
     188           0 :   return which_step;
     189             : }
     190             : 
     191             : void
     192         674 : AnalysisStepUserObject::initialize()
     193             : {
     194         674 : }
     195             : void
     196         674 : AnalysisStepUserObject::execute()
     197             : {
     198         674 : }
     199             : void
     200         674 : AnalysisStepUserObject::finalize()
     201             : {
     202         674 : }

Generated by: LCOV version 1.14