<commit_before>/****************************************************************/
/*               DO NOT MODIFY THIS HEADER                      */
/* MOOSE - Multiphysics Object Oriented Simulation Environment  */
/*                                                              */
/*           (c) 2010 Battelle Energy Alliance, LLC             */
/*                   ALL RIGHTS RESERVED                        */
/*                                                              */
/*          Prepared by Battelle Energy Alliance, LLC           */
/*            Under Contract No. DE-AC07-05ID14517              */
/*            With the U. S. Department of Energy               */
/*                                                              */
/*            See COPYRIGHT for full restrictions               */
/****************************************************************/

// MOOSE includes
#include "PetscOutput.h"
#include "FEProblem.h"

// libMesh includes
#include "libmesh/libmesh_common.h"
#include "libmesh/petsc_nonlinear_solver.h"

template<>
InputParameters validParams<PetscOutput>()
{
  InputParameters params = validParams<Output>();

  // Toggled for outputting nonlinear and linear residuals, only if we have PETSc
#ifdef LIBMESH_HAVE_PETSC

  // **** DEPRECATED PARAMETERS ****
  params.addDeprecatedParam<bool>("linear_residuals", false, "Specifies whether output occurs on each linear residual evaluation",
                                  "Please include 'linear' in the 'output_on' execution list to get this behavior.");
  params.addDeprecatedParam<bool>("nonlinear_residuals", false, "Specifies whether output occurs on each nonlinear residual evaluation",
                                  "Please include 'nonlinear' in the 'output_on' execution list to get this behavior.");
  // Psuedo time step divisors
  params.addParam<Real>("nonlinear_residual_dt_divisor", 1000, "Number of divisions applied to time step when outputting non-linear residuals");
  params.addParam<Real>("linear_residual_dt_divisor", 1000, "Number of divisions applied to time step when outputting linear residuals");

  // Start times for residual output
  params.addParam<Real>("linear_residual_start_time", "Specifies a start time to begin output on each linear residual evaluation");
  params.addParam<Real>("nonlinear_residual_start_time", "Specifies a start time to begin output on each nonlinear residual evaluation");

  // End time for residual output
  /* Note, No default is given here so that in Peacock giant numbers do not show up by default, the defaults are set in the initialization list */
  params.addParam<Real>("linear_residual_end_time", "Specifies an end time to begin output on each linear residual evaluation");
  params.addParam<Real>("nonlinear_residual_end_time", "Specifies an end time to begin output on each nonlinear residual evaluation");

  params.addParamNamesToGroup("linear_residuals nonlinear_residuals linear_residual_start_time nonlinear_residual_start_time linear_residual_end_time nonlinear_residual_end_time  nonlinear_residual_dt_divisor linear_residual_dt_divisor", "PETSc");
#endif

  return params;
}

PetscOutput::PetscOutput(const std::string & name, InputParameters & parameters) :
    Output(name, parameters),
    _nonlinear_iter(0),
    _linear_iter(0),
    _on_linear_residual(false),
    _on_nonlinear_residual(false),
    _nonlinear_dt_divisor(getParam<Real>("nonlinear_residual_dt_divisor")),
    _linear_dt_divisor(getParam<Real>("linear_residual_dt_divisor")),
    _nonlinear_start_time(-std::numeric_limits<Real>::max()),
    _linear_start_time(-std::numeric_limits<Real>::max()),
    _nonlinear_end_time(std::numeric_limits<Real>::max()),
    _linear_end_time(std::numeric_limits<Real>::max())
{
  // **** DEPRECATED PARAMETER SUPPORT ****
  if (getParam<bool>("linear_residuals"))
    _output_on.push_back("linear");
  if (getParam<bool>("nonlinear_residuals"))
    _output_on.push_back("nonlinear");

  // Nonlinear residual start-time supplied by user
  if (isParamValid("nonlinear_residual_start_time"))
  {
    _nonlinear_start_time = getParam<Real>("nonlinear_residual_start_time");
    _output_on.push_back("nonlinear");
  }

  // Nonlinear residual end-time supplied by user
  if (isParamValid("nonlinear_residual_end_time"))
    _nonlinear_end_time = getParam<Real>("nonlinear_residual_end_time");

  // Linear residual start-time supplied by user
  if (isParamValid("linear_residual_start_time"))
  {
    _linear_start_time = getParam<Real>("linear_residual_start_time");
    _output_on.push_back("linear");
  }

  // Linear residual end-time supplied by user
  if (isParamValid("linear_residual_end_time"))
    _linear_end_time = getParam<Real>("linear_residual_end_time");
}

PetscOutput::~PetscOutput()
{
}

void
PetscOutput::timestepSetupInternal()
{
// Only execute if PETSc exists
#ifdef LIBMESH_HAVE_PETSC

  // Extract the non-linear and linear solvers from PETSc
  NonlinearSystem & nl = _problem_ptr->getNonlinearSystem();
  PetscNonlinearSolver<Number> * petsc_solver = dynamic_cast<PetscNonlinearSolver<Number> *>(nl.sys().nonlinear_solver.get());
  SNES snes = petsc_solver->snes();
  KSP ksp;
  SNESGetKSP(snes, &ksp);

  // Update the pseudo times
  _nonlinear_time = _time_old;                   // non-linear time starts with the previous time step
  _nonlinear_dt = _dt/_nonlinear_dt_divisor;     // set the pseudo non-linear timestep
  _linear_dt = _nonlinear_dt/_linear_dt_divisor; // set the pseudo linear timestep

  // Set the PETSc monitor functions
  if (_output_on.contains(EXEC_NONLINEAR) && (_time >= _nonlinear_start_time - _t_tol && _time <= _nonlinear_end_time + _t_tol) )
  {
    PetscErrorCode ierr = SNESMonitorSet(snes, petscNonlinearOutput, this, PETSC_NULL);
    CHKERRABORT(_communicator.get(),ierr);
  }

  if (_output_on.contains(EXEC_LINEAR) && (_time >= _linear_start_time - _t_tol && _time <= _linear_end_time + _t_tol) )
  {
    PetscErrorCode ierr = KSPMonitorSet(ksp, petscLinearOutput, this, PETSC_NULL);<commit_msg>Correctly setting sub-timestep number for output_on=nonlinear and steady executioners, fixes #4573<commit_after>/****************************************************************/
/*               DO NOT MODIFY THIS HEADER                      */
/* MOOSE - Multiphysics Object Oriented Simulation Environment  */
/*                                                              */
/*           (c) 2010 Battelle Energy Alliance, LLC             */
/*                   ALL RIGHTS RESERVED                        */
/*                                                              */
/*          Prepared by Battelle Energy Alliance, LLC           */
/*            Under Contract No. DE-AC07-05ID14517              */
/*            With the U. S. Department of Energy               */
/*                                                              */
/*            See COPYRIGHT for full restrictions               */
/****************************************************************/

// MOOSE includes
#include "PetscOutput.h"
#include "FEProblem.h"

// libMesh includes
#include "libmesh/libmesh_common.h"
#include "libmesh/petsc_nonlinear_solver.h"

template<>
InputParameters validParams<PetscOutput>()
{
  InputParameters params = validParams<Output>();

  // Toggled for outputting nonlinear and linear residuals, only if we have PETSc
#ifdef LIBMESH_HAVE_PETSC

  // **** DEPRECATED PARAMETERS ****
  params.addDeprecatedParam<bool>("linear_residuals", false, "Specifies whether output occurs on each linear residual evaluation",
                                  "Please include 'linear' in the 'output_on' execution list to get this behavior.");
  params.addDeprecatedParam<bool>("nonlinear_residuals", false, "Specifies whether output occurs on each nonlinear residual evaluation",
                                  "Please include 'nonlinear' in the 'output_on' execution list to get this behavior.");
  // Psuedo time step divisors
  params.addParam<Real>("nonlinear_residual_dt_divisor", 1000, "Number of divisions applied to time step when outputting non-linear residuals");
  params.addParam<Real>("linear_residual_dt_divisor", 1000, "Number of divisions applied to time step when outputting linear residuals");

  // Start times for residual output
  params.addParam<Real>("linear_residual_start_time", "Specifies a start time to begin output on each linear residual evaluation");
  params.addParam<Real>("nonlinear_residual_start_time", "Specifies a start time to begin output on each nonlinear residual evaluation");

  // End time for residual output
  /* Note, No default is given here so that in Peacock giant numbers do not show up by default, the defaults are set in the initialization list */
  params.addParam<Real>("linear_residual_end_time", "Specifies an end time to begin output on each linear residual evaluation");
  params.addParam<Real>("nonlinear_residual_end_time", "Specifies an end time to begin output on each nonlinear residual evaluation");

  params.addParamNamesToGroup("linear_residuals nonlinear_residuals linear_residual_start_time nonlinear_residual_start_time linear_residual_end_time nonlinear_residual_end_time  nonlinear_residual_dt_divisor linear_residual_dt_divisor", "PETSc");
#endif

  return params;
}

PetscOutput::PetscOutput(const std::string & name, InputParameters & parameters) :
    Output(name, parameters),
    _nonlinear_iter(0),
    _linear_iter(0),
    _on_linear_residual(false),
    _on_nonlinear_residual(false),
    _nonlinear_dt_divisor(getParam<Real>("nonlinear_residual_dt_divisor")),
    _linear_dt_divisor(getParam<Real>("linear_residual_dt_divisor")),
    _nonlinear_start_time(-std::numeric_limits<Real>::max()),
    _linear_start_time(-std::numeric_limits<Real>::max()),
    _nonlinear_end_time(std::numeric_limits<Real>::max()),
    _linear_end_time(std::numeric_limits<Real>::max())
{
  // **** DEPRECATED PARAMETER SUPPORT ****
  if (getParam<bool>("linear_residuals"))
    _output_on.push_back("linear");
  if (getParam<bool>("nonlinear_residuals"))
    _output_on.push_back("nonlinear");

  // Nonlinear residual start-time supplied by user
  if (isParamValid("nonlinear_residual_start_time"))
  {
    _nonlinear_start_time = getParam<Real>("nonlinear_residual_start_time");
    _output_on.push_back("nonlinear");
  }

  // Nonlinear residual end-time supplied by user
  if (isParamValid("nonlinear_residual_end_time"))
    _nonlinear_end_time = getParam<Real>("nonlinear_residual_end_time");

  // Linear residual start-time supplied by user
  if (isParamValid("linear_residual_start_time"))
  {
    _linear_start_time = getParam<Real>("linear_residual_start_time");
    _output_on.push_back("linear");
  }

  // Linear residual end-time supplied by user
  if (isParamValid("linear_residual_end_time"))
    _linear_end_time = getParam<Real>("linear_residual_end_time");
}

PetscOutput::~PetscOutput()
{
}

void
PetscOutput::timestepSetupInternal()
{
// Only execute if PETSc exists
#ifdef LIBMESH_HAVE_PETSC

  // Extract the non-linear and linear solvers from PETSc
  NonlinearSystem & nl = _problem_ptr->getNonlinearSystem();
  PetscNonlinearSolver<Number> * petsc_solver = dynamic_cast<PetscNonlinearSolver<Number> *>(nl.sys().nonlinear_solver.get());
  SNES snes = petsc_solver->snes();
  KSP ksp;
  SNESGetKSP(snes, &ksp);

  // Update the pseudo times
  _nonlinear_time = _time_old;                   // non-linear time starts with the previous time step
  if (_dt != 0)
  {
    _nonlinear_dt = _dt/_nonlinear_dt_divisor;     // set the pseudo non-linear timestep as fraction of real timestep for transient executioners
  }
  else
  {
    _nonlinear_dt = 1./_nonlinear_dt_divisor;     // set the pseudo non-linear timestep for steady executioners (here _dt==0)
  }
  _linear_dt = _nonlinear_dt/_linear_dt_divisor; // set the pseudo linear timestep

  // Set the PETSc monitor functions
  if (_output_on.contains(EXEC_NONLINEAR) && (_time >= _nonlinear_start_time - _t_tol && _time <= _nonlinear_end_time + _t_tol) )
  {
    PetscErrorCode ierr = SNESMonitorSet(snes, petscNonlinearOutput, this, PETSC_NULL);
    CHKERRABORT(_communicator.get(),ierr);
  }

  if (_output_on.contains(EXEC_LINEAR) && (_time >= _linear_start_time - _t_tol && _time <= _linear_end_time + _t_tol) )
  {
    PetscErrorCode ierr = KSPMonitorSet(ksp, petscLinearOutput, this, PETSC_NULL);
{'fraction_non_alphanumeric': 0.11768027101145347, 'fraction_numerical': 0.0042748830456525245, 'mean_word_length': 3.664785553047404, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4422037', 'n_tokens_mistral': 3776, 'n_tokens_neox': 3559, 'n_words': 863}