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 "TimeIntegrator.h" 11 : #include "FEProblem.h" 12 : #include "NonlinearSystemBase.h" 13 : 14 : #include "libmesh/nonlinear_implicit_system.h" 15 : #include "libmesh/nonlinear_solver.h" 16 : #include "libmesh/dof_map.h" 17 : 18 : using namespace libMesh; 19 : 20 : InputParameters 21 334765 : TimeIntegrator::validParams() 22 : { 23 334765 : InputParameters params = MooseObject::validParams(); 24 334765 : params.addParam<std::vector<VariableName>>( 25 : "variables", {}, "A subset of the variables that this time integrator should be applied to"); 26 334765 : params.registerBase("TimeIntegrator"); 27 334765 : return params; 28 0 : } 29 : 30 61486 : TimeIntegrator::TimeIntegrator(const InputParameters & parameters) 31 : : MooseObject(parameters), 32 : Restartable(this, "TimeIntegrators"), 33 122972 : NonlinearTimeIntegratorInterface(*getCheckedPointerParam<FEProblemBase *>("_fe_problem_base"), 34 122972 : *getCheckedPointerParam<SystemBase *>("_sys")), 35 122972 : LinearTimeIntegratorInterface(*getCheckedPointerParam<SystemBase *>("_sys")), 36 61486 : _fe_problem(*getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")), 37 61486 : _sys(*getCheckedPointerParam<SystemBase *>("_sys")), 38 61486 : _du_dot_du(_sys.duDotDus()), 39 61486 : _solution(_sys.currentSolution()), 40 61486 : _solution_old(_sys.solutionState(1)), 41 61486 : _solution_sub(declareRestartableDataWithContext<std::unique_ptr<NumericVector<Number>>>( 42 61486 : "solution_sub", &const_cast<libMesh::Parallel::Communicator &>(this->comm()))), 43 61486 : _solution_old_sub(declareRestartableDataWithContext<std::unique_ptr<NumericVector<Number>>>( 44 61486 : "solution_old_sub", &const_cast<libMesh::Parallel::Communicator &>(this->comm()))), 45 61486 : _t_step(_fe_problem.timeStep()), 46 61486 : _dt(_fe_problem.dt()), 47 61486 : _dt_old(_fe_problem.dtOld()), 48 61486 : _n_nonlinear_iterations(0), 49 61486 : _n_linear_iterations(0), 50 61486 : _is_lumped(false), 51 61486 : _var_restriction(declareRestartableData<bool>( 52 122972 : "var_restriction", !getParam<std::vector<VariableName>>("variables").empty())), 53 61486 : _local_indices(declareRestartableData<std::vector<dof_id_type>>("local_indices")), 54 61486 : _vars(declareRestartableData<std::unordered_set<unsigned int>>("vars")), 55 368916 : _from_subvector(NumericVector<Number>::build(this->comm())) 56 : { 57 61486 : _fe_problem.setUDotRequested(true); 58 61486 : } 59 : 60 : void 61 27759 : TimeIntegrator::init() 62 : { 63 27759 : if (!_var_restriction) 64 27645 : return; 65 : 66 114 : const auto & var_names = getParam<std::vector<VariableName>>("variables"); 67 114 : std::vector<unsigned int> var_num_vec; 68 114 : auto & lm_sys = _sys.system(); 69 114 : lm_sys.get_all_variable_numbers(var_num_vec); 70 114 : std::unordered_set<unsigned int> var_nums(var_num_vec.begin(), var_num_vec.end()); 71 228 : for (const auto & var_name : var_names) 72 114 : if (lm_sys.has_variable(var_name)) 73 : { 74 114 : const auto var_num = lm_sys.variable_number(var_name); 75 114 : _vars.insert(var_num); 76 114 : var_nums.erase(var_num); 77 : } 78 : 79 : // If var_nums is empty then that means the user has specified all the variables in this system 80 114 : if (var_nums.empty()) 81 : { 82 0 : _var_restriction = false; 83 0 : return; 84 : } 85 : 86 114 : std::vector<dof_id_type> var_dof_indices, work_vec; 87 228 : for (const auto var_num : _vars) 88 : { 89 114 : work_vec = _local_indices; 90 114 : _local_indices.clear(); 91 114 : lm_sys.get_dof_map().local_variable_indices(var_dof_indices, lm_sys.get_mesh(), var_num); 92 114 : std::merge(work_vec.begin(), 93 : work_vec.end(), 94 : var_dof_indices.begin(), 95 : var_dof_indices.end(), 96 : std::back_inserter(_local_indices)); 97 : } 98 : 99 114 : _solution_sub = NumericVector<Number>::build(_solution->comm()); 100 114 : _solution_old_sub = NumericVector<Number>::build(_solution_old.comm()); 101 114 : } 102 : 103 : void 104 0 : TimeIntegrator::solve() 105 : { 106 0 : mooseError("Calling TimeIntegrator::solve() is no longer supported"); 107 : } 108 : 109 : void 110 267907 : TimeIntegrator::setNumIterationsLastSolve() 111 : { 112 267907 : _n_nonlinear_iterations = getNumNonlinearIterationsLastSolve(); 113 267907 : _n_linear_iterations = getNumLinearIterationsLastSolve(); 114 267907 : } 115 : 116 : unsigned int 117 288042 : TimeIntegrator::getNumNonlinearIterationsLastSolve() const 118 : { 119 288042 : return _nonlinear_implicit_system->n_nonlinear_iterations(); 120 : } 121 : 122 : unsigned int 123 288042 : TimeIntegrator::getNumLinearIterationsLastSolve() const 124 : { 125 288042 : auto & nonlinear_solver = _nonlinear_implicit_system->nonlinear_solver; 126 : libmesh_assert(nonlinear_solver); 127 : 128 288042 : return nonlinear_solver->get_total_linear_iterations(); 129 : } 130 : 131 : void 132 10233 : TimeIntegrator::copyVector(const NumericVector<Number> & from, NumericVector<Number> & to) 133 : { 134 10233 : if (!_var_restriction) 135 9459 : to = from; 136 : else 137 : { 138 774 : auto to_sub = to.get_subvector(_local_indices); 139 774 : from.create_subvector(*_from_subvector, _local_indices, false); 140 774 : *to_sub = *_from_subvector; 141 774 : to.restore_subvector(std::move(to_sub), _local_indices); 142 774 : } 143 10233 : } 144 : 145 : bool 146 10171754 : TimeIntegrator::integratesVar(const unsigned int var_num) const 147 : { 148 10171754 : if (!_var_restriction) 149 10103504 : return true; 150 : 151 68250 : return _vars.count(var_num); 152 : } 153 : 154 : void 155 4777035 : TimeIntegrator::computeDuDotDu() 156 : { 157 4777035 : const auto coeff = duDotDuCoeff(); 158 14404014 : for (const auto i : index_range(_du_dot_du)) 159 9626979 : if (integratesVar(i)) 160 9610128 : _du_dot_du[i] = coeff / _dt; 161 4777035 : }