LCOV - code coverage report
Current view: top level - src/timeintegrators - ExplicitTimeIntegrator.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #31730 (e8b711) with base e0c998 Lines: 117 138 84.8 %
Date: 2025-10-29 16:49:47 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             : // MOOSE includes
      11             : #include "ExplicitTimeIntegrator.h"
      12             : #include "NonlinearSystem.h"
      13             : #include "FEProblem.h"
      14             : #include "PetscSupport.h"
      15             : #include "KernelBase.h"
      16             : #include "DGKernelBase.h"
      17             : #include "ScalarKernelBase.h"
      18             : #include "FVElementalKernel.h"
      19             : #include "FVFluxKernel.h"
      20             : #include "NodalKernelBase.h"
      21             : 
      22             : // libMesh includes
      23             : #include "libmesh/enum_convergence_flags.h"
      24             : 
      25             : using namespace libMesh;
      26             : 
      27             : InputParameters
      28       45633 : ExplicitTimeIntegrator::validParams()
      29             : {
      30       45633 :   InputParameters params = TimeIntegrator::validParams();
      31             : 
      32      182532 :   MooseEnum solve_type("consistent lumped lump_preconditioned", "consistent");
      33             : 
      34      136899 :   params.addParam<MooseEnum>(
      35             :       "solve_type",
      36             :       solve_type,
      37             :       "The way to solve the system.  A 'consistent' solve uses the full mass matrix and actually "
      38             :       "needs to use a linear solver to solve the problem.  'lumped' uses a lumped mass matrix with "
      39             :       "a simple inversion - incredibly fast but may be less accurate.  'lump_preconditioned' uses "
      40             :       "the lumped mass matrix as a preconditioner for the 'consistent' solve");
      41             : 
      42       91266 :   return params;
      43       45633 : }
      44             : 
      45         688 : ExplicitTimeIntegrator::ExplicitTimeIntegrator(const InputParameters & parameters)
      46             :   : TimeIntegrator(parameters),
      47             :     MeshChangedInterface(parameters),
      48             : 
      49         688 :     _solve_type(getParam<MooseEnum>("solve_type")),
      50        1376 :     _explicit_residual(addVector("explicit_residual", false, PARALLEL)),
      51        1376 :     _solution_update(addVector("solution_update", true, PARALLEL)),
      52        2752 :     _mass_matrix_diag_inverted(addVector("mass_matrix_diag_inverted", true, GHOSTED))
      53             : {
      54         688 :   _Ke_time_tag = _fe_problem.getMatrixTagID("TIME");
      55             : 
      56             :   // This effectively changes the default solve_type to LINEAR instead of PJFNK,
      57             :   // so that it is valid to not supply solve_type in the Executioner block:
      58         688 :   if (_nl)
      59         344 :     _fe_problem.solverParams(_nl->number())._type = Moose::ST_LINEAR;
      60             : 
      61         688 :   if (_solve_type == LUMPED || _solve_type == LUMP_PRECONDITIONED)
      62         564 :     _ones = addVector("ones", true, PARALLEL);
      63             :   // don't set any of the common SNES-related petsc options to prevent unused option warnings
      64         688 :   Moose::PetscSupport::dontAddCommonSNESOptions(_fe_problem);
      65         688 : }
      66             : 
      67             : void
      68         336 : ExplicitTimeIntegrator::initialSetup()
      69             : {
      70         336 :   meshChanged();
      71             : 
      72         336 :   if (_nl)
      73             :   {
      74         336 :     std::unordered_set<unsigned int> vars_to_check;
      75         336 :     if (!_vars.empty())
      76           0 :       vars_to_check = _vars;
      77             :     else
      78         685 :       for (const auto i : make_range(_nl->nVariables()))
      79         349 :         vars_to_check.insert(i);
      80             : 
      81         336 :     const auto mass_matrix_tag_id = massMatrixTagID();
      82         672 :     std::set<TagID> matrix_tags = {mass_matrix_tag_id};
      83         336 :     auto fv_object_starting_query = _fe_problem.theWarehouse()
      84         336 :                                         .query()
      85         336 :                                         .template condition<AttribMatrixTags>(matrix_tags)
      86         336 :                                         .template condition<AttribSysNum>(_nl->number());
      87             : 
      88         681 :     for (const auto var_id : vars_to_check)
      89             :     {
      90         349 :       const auto & var_name = _nl->system().variable_name(var_id);
      91         349 :       const bool field_var = _nl->hasVariable(var_name);
      92         349 :       if (!field_var)
      93             :         mooseAssert(_nl->hasScalarVariable(var_name),
      94             :                     var_name << " should be either a field or scalar variable");
      95         349 :       if (field_var)
      96             :       {
      97         293 :         const auto & field_var = _nl->getVariable(/*tid=*/0, var_name);
      98         293 :         if (field_var.isFV())
      99             :         {
     100           0 :           std::vector<FVElementalKernel *> fv_elemental_kernels;
     101           0 :           auto var_query = fv_object_starting_query.clone().template condition<AttribVar>(var_id);
     102           0 :           auto var_query_clone = var_query.clone();
     103           0 :           var_query.template condition<AttribSystem>("FVElementalKernel")
     104           0 :               .queryInto(fv_elemental_kernels);
     105           0 :           if (fv_elemental_kernels.size())
     106           0 :             continue;
     107             : 
     108           0 :           std::vector<FVFluxKernel *> fv_flux_kernels;
     109           0 :           var_query_clone.template condition<AttribSystem>("FVFluxKernel")
     110           0 :               .queryInto(fv_flux_kernels);
     111           0 :           if (fv_flux_kernels.size())
     112           0 :             continue;
     113           0 :         }
     114             :         else
     115             :         {
     116             :           // We are a finite element variable
     117         293 :           if (_nl->getKernelWarehouse()
     118         293 :                   .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
     119         293 :                   .hasVariableObjects(var_id))
     120         277 :             continue;
     121          16 :           if (_nl->getDGKernelWarehouse()
     122          16 :                   .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
     123          16 :                   .hasVariableObjects(var_id))
     124           0 :             continue;
     125          16 :           if (_nl->getNodalKernelWarehouse()
     126          16 :                   .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
     127          16 :                   .hasVariableObjects(var_id))
     128           0 :             continue;
     129             : #ifdef MOOSE_KOKKOS_ENABLED
     130          14 :           if (_nl->getKokkosKernelWarehouse()
     131          14 :                   .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
     132          14 :                   .hasVariableObjects(var_id))
     133          12 :             continue;
     134           2 :           if (_nl->getKokkosNodalKernelWarehouse()
     135           2 :                   .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
     136           2 :                   .hasVariableObjects(var_id))
     137           0 :             continue;
     138             : #endif
     139             :         }
     140             :       }
     141          56 :       else if (_nl->getScalarKernelWarehouse()
     142          56 :                    .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
     143          56 :                    .hasVariableObjects(var_id))
     144          56 :         continue;
     145             : 
     146           4 :       mooseError("No objects contributing to the mass matrix were found for variable '",
     147             :                  var_name,
     148             :                  "'. Did you, e.g., forget a time derivative term?");
     149             :     }
     150         332 :   }
     151         332 : }
     152             : 
     153             : void
     154         308 : ExplicitTimeIntegrator::init()
     155             : {
     156         308 :   if (_nl && _fe_problem.solverParams(_nl->number())._type != Moose::ST_LINEAR)
     157           4 :     mooseError(
     158             :         "The chosen time integrator requires 'solve_type = LINEAR' in the Executioner block.");
     159         304 : }
     160             : 
     161             : void
     162        4747 : ExplicitTimeIntegrator::preSolve()
     163             : {
     164        4747 : }
     165             : 
     166             : void
     167         384 : ExplicitTimeIntegrator::meshChanged()
     168             : {
     169             :   // Can only be done after the system is initialized
     170         384 :   if (_solve_type == LUMPED || _solve_type == LUMP_PRECONDITIONED)
     171          94 :     *_ones = 1.;
     172             : 
     173         384 :   if (_solve_type == CONSISTENT || _solve_type == LUMP_PRECONDITIONED)
     174         320 :     _linear_solver = LinearSolver<Number>::build(comm());
     175             : 
     176         384 :   if (_solve_type == LUMP_PRECONDITIONED)
     177             :   {
     178          30 :     _preconditioner = std::make_unique<LumpedPreconditioner>(*_mass_matrix_diag_inverted);
     179          30 :     _linear_solver->attach_preconditioner(_preconditioner.get());
     180          30 :     _linear_solver->init();
     181             :   }
     182             : 
     183         384 :   if (_solve_type == CONSISTENT || _solve_type == LUMP_PRECONDITIONED)
     184         320 :     Moose::PetscSupport::setLinearSolverDefaults(_fe_problem, *_linear_solver);
     185         384 : }
     186             : 
     187             : bool
     188        4894 : ExplicitTimeIntegrator::performExplicitSolve(SparseMatrix<Number> & mass_matrix)
     189             : {
     190        4894 :   bool converged = false;
     191             : 
     192        4894 :   switch (_solve_type)
     193             :   {
     194        1899 :     case CONSISTENT:
     195             :     {
     196        1899 :       converged = solveLinearSystem(mass_matrix);
     197             : 
     198        1899 :       break;
     199             :     }
     200        2690 :     case LUMPED:
     201             :     {
     202             :       // Computes the sum of each row (lumping)
     203             :       // Note: This is actually how PETSc does it
     204             :       // It's not "perfectly optimal" - but it will be fast (and universal)
     205        2690 :       mass_matrix.vector_mult(*_mass_matrix_diag_inverted, *_ones);
     206             : 
     207             :       // "Invert" the diagonal mass matrix
     208        2690 :       _mass_matrix_diag_inverted->reciprocal();
     209             : 
     210             :       // Multiply the inversion by the RHS
     211        2690 :       _solution_update->pointwise_mult(*_mass_matrix_diag_inverted, *_explicit_residual);
     212             : 
     213             :       // Check for convergence by seeing if there is a nan or inf
     214        2690 :       auto sum = _solution_update->sum();
     215        2690 :       converged = std::isfinite(sum);
     216             : 
     217             :       // The linear iteration count remains zero
     218        2690 :       _n_linear_iterations = 0;
     219             : 
     220        2690 :       break;
     221             :     }
     222         305 :     case LUMP_PRECONDITIONED:
     223             :     {
     224         305 :       mass_matrix.vector_mult(*_mass_matrix_diag_inverted, *_ones);
     225         305 :       _mass_matrix_diag_inverted->reciprocal();
     226             : 
     227         305 :       converged = solveLinearSystem(mass_matrix);
     228             : 
     229         305 :       break;
     230             :     }
     231           0 :     default:
     232           0 :       mooseError("Unknown solve_type in ExplicitTimeIntegrator.");
     233             :   }
     234             : 
     235        4894 :   return converged;
     236             : }
     237             : 
     238             : bool
     239        2204 : ExplicitTimeIntegrator::solveLinearSystem(SparseMatrix<Number> & mass_matrix)
     240             : {
     241        2204 :   auto & es = _fe_problem.es();
     242             : 
     243             :   const auto num_its_and_final_tol =
     244        5092 :       _linear_solver->solve(mass_matrix,
     245        2204 :                             *_solution_update,
     246        2204 :                             *_explicit_residual,
     247        1444 :                             es.parameters.get<Real>("linear solver tolerance"),
     248        1444 :                             es.parameters.get<unsigned int>("linear solver maximum iterations"));
     249             : 
     250        2204 :   _n_linear_iterations += num_its_and_final_tol.first;
     251             : 
     252        2204 :   const bool converged = checkLinearConvergence();
     253             : 
     254        2204 :   return converged;
     255             : }
     256             : 
     257             : bool
     258        2204 : ExplicitTimeIntegrator::checkLinearConvergence()
     259             : {
     260        2204 :   auto reason = _linear_solver->get_converged_reason();
     261             : 
     262        2204 :   switch (reason)
     263             :   {
     264        2060 :     case CONVERGED_RTOL_NORMAL:
     265             :     case CONVERGED_ATOL_NORMAL:
     266             :     case CONVERGED_RTOL:
     267             :     case CONVERGED_ATOL:
     268             :     case CONVERGED_ITS:
     269             :     case CONVERGED_CG_NEG_CURVE:
     270             :     case CONVERGED_CG_CONSTRAINED:
     271             :     case CONVERGED_STEP_LENGTH:
     272             :     case CONVERGED_HAPPY_BREAKDOWN:
     273        2060 :       return true;
     274         144 :     case DIVERGED_NULL:
     275             :     case DIVERGED_ITS:
     276             :     case DIVERGED_DTOL:
     277             :     case DIVERGED_BREAKDOWN:
     278             :     case DIVERGED_BREAKDOWN_BICG:
     279             :     case DIVERGED_NONSYMMETRIC:
     280             :     case DIVERGED_INDEFINITE_PC:
     281             :     case DIVERGED_NAN:
     282             :     case DIVERGED_INDEFINITE_MAT:
     283             :     case CONVERGED_ITERATING:
     284             :     case DIVERGED_PCSETUP_FAILED:
     285         144 :       return false;
     286           0 :     default:
     287           0 :       mooseError("Unknown convergence reason in ExplicitTimeIntegrator.");
     288             :   }
     289             : }

Generated by: LCOV version 1.14