Skip to content

Commit

Permalink
Adds postprocessor to track time step number for transient problems
Browse files Browse the repository at this point in the history
Minor quality-of-life improvement for users who want to track
time step counts during their MOOSE simulations.

closes #29127
  • Loading branch information
anshchaube committed Nov 23, 2024
1 parent 8a5faa9 commit c915948
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 0 deletions.
17 changes: 17 additions & 0 deletions framework/doc/content/source/postprocessors/NumTimeSteps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# TimestepSize

!syntax description /Postprocessors/NumTimeSteps

## Description

`NumTimeSteps` reports the timestep size.

## Example Input Syntax

!listing /Users/achaube/tools/moose/test/tests/postprocessors/num_time_steps/moose.i block=Postprocessors

!syntax parameters /Postprocessors/NumTimeSteps

!syntax inputs /Postprocessors/NumTimeSteps

!syntax children /Postprocessors/NumTimeSteps
31 changes: 31 additions & 0 deletions framework/include/postprocessors/NumTimeSteps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#pragma once

#include "GeneralPostprocessor.h"

class NumTimeSteps : public GeneralPostprocessor
{
public:
static InputParameters validParams();

NumTimeSteps(const InputParameters & parameters);

virtual void initialize() override {}
virtual void execute() override {}

/**
* This will return the current time step number.
*/
virtual Real getValue() const override;

protected:
FEProblemBase & _feproblem;
};
32 changes: 32 additions & 0 deletions framework/src/postprocessors/NumTimeSteps.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "NumTimeSteps.h"
#include "FEProblem.h"

registerMooseObject("MooseApp", NumTimeSteps);

InputParameters
NumTimeSteps::validParams()
{
InputParameters params = GeneralPostprocessor::validParams();
params.addClassDescription("Reports the timestep number");
return params;
}

NumTimeSteps::NumTimeSteps(const InputParameters & parameters)
: GeneralPostprocessor(parameters), _feproblem(dynamic_cast<FEProblemBase &>(_subproblem))
{
}

Real
NumTimeSteps::getValue() const
{
return _feproblem.timeStep();
}
12 changes: 12 additions & 0 deletions test/tests/postprocessors/num_time_steps/gold/moose_out.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
time,timestep_ctr
0,0
1,1
2,2
3,3
4,4
5,5
6,6
7,7
8,8
9,9
10,10
98 changes: 98 additions & 0 deletions test/tests/postprocessors/num_time_steps/moose.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
[Mesh]
type = GeneratedMesh
dim = 3
nx = 30
ny = 30
nz = 30
[]

[Variables]
[temperature]
initial_condition = 500.0
[]
[]

[AuxVariables]
[source]
[]
[]

[Kernels]
[conduction]
type = HeatConduction
variable = temperature
[]
[source]
type = CoupledForce
variable = temperature
v = source
[]
[]

[AuxKernels]
[source]
type = ParsedAux
variable = source
function = 'temperature*7*t'
coupled_variables = 'temperature'
execute_on = 'linear'
use_xyzt = true
[]
[]

[BCs]
[left]
type = DirichletBC
variable = temperature
boundary = 'left'
value = 500.0
[]
[right]
type = DirichletBC
variable = temperature
boundary = 'right'
value = 600.0
[]
[top]
type = DirichletBC
variable = temperature
boundary = 'top'
value = 650.0
[]
[front]
type = DirichletBC
variable = temperature
boundary = 'front'
value = 650.0
[]
[]

[Materials]
[k]
type = GenericConstantMaterial
prop_names = 'thermal_conductivity'
prop_values = '1.5'
[]
[]

[Postprocessors]
[timestep_ctr]
type = NumTimeSteps
[]
[]


[Executioner]
type = Transient
nl_abs_tol = 1e-10
nl_rel_tol = 1e-4
dt = 1
num_steps = 10
[]

[Outputs]
exodus = false
csv = true
hide = 'source temperature'
[]

9 changes: 9 additions & 0 deletions test/tests/postprocessors/num_time_steps/tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Tests]
[num_time_steps]
type = 'CSVDiff'
input = 'numtimesteps.i'
csvdiff = 'numtimesteps_out.csv'
requirement = 'The postprocessor `NumTimeSteps` shall count the number of time-steps accurately'
'over the course of a transient solve'
[]
[]

0 comments on commit c915948

Please sign in to comment.