libMesh
time_solver.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 #include "libmesh/time_solver.h"
19 
20 #include "libmesh/diff_solver.h"
21 #include "libmesh/diff_system.h"
22 #include "libmesh/linear_solver.h"
23 #include "libmesh/no_solution_history.h"
24 
25 #include "libmesh/adjoint_refinement_estimator.h"
26 #include "libmesh/error_vector.h"
27 
28 
29 // C++ includes
30 #include <memory>
31 
32 
33 namespace libMesh
34 {
35 
37  : quiet (true),
38  reduce_deltat_on_diffsolver_failure (0),
39  _diff_solver (),
40  _linear_solver (),
41  _system (s),
42  solution_history(std::make_unique<NoSolutionHistory>()),
43  last_deltat (s.deltat),
44  _is_adjoint (false)
45 {
46 }
47 
48 
49 
50 TimeSolver::~TimeSolver () = default;
51 
52 
53 
55 {
56  libmesh_assert(this->diff_solver().get());
57  libmesh_assert_equal_to (&(this->diff_solver()->system()), &(this->system()));
58  this->diff_solver()->reinit();
59 
60  libmesh_assert(this->linear_solver().get());
61  this->linear_solver()->clear();
62  if (libMesh::on_command_line("--solver-system-names"))
63  this->linear_solver()->init((_system.name()+"_").c_str());
64  else
65  this->linear_solver()->init();
66 
67  this->_linear_solver->init_names(_system);
68 }
69 
70 
71 
73 {
74  // If the user hasn't given us a solver to use,
75  // just build a default solver
76  if (this->diff_solver().get() == nullptr)
78 
79  if (this->linear_solver().get() == nullptr)
81 }
82 
84 {
85  libmesh_assert_msg(_system.n_qois() != 0, "System qois have to be initialized before initializing adjoints.");
86 
87  // Add adjoint vectors
88  for(auto i : make_range(_system.n_qois()))
89  {
90  std::string adjoint_solution_name = "adjoint_solution";
91  adjoint_solution_name+= std::to_string(i);
92  _system.add_vector(adjoint_solution_name, false, GHOSTED);
93  }
94 
95 }
96 
98 {
99  this->diff_solver()->init();
100 
101  if (libMesh::on_command_line("--solver-system-names"))
102  this->linear_solver()->init((_system.name()+"_").c_str());
103  else
104  this->linear_solver()->init();
105 
106  this->linear_solver()->init_names(_system);
107 }
108 
109 
110 
112 {
113  libmesh_assert(this->diff_solver().get());
114  libmesh_assert_equal_to (&(this->diff_solver()->system()), &(this->system()));
115  this->diff_solver()->solve();
116 }
117 
118 
119 void TimeSolver::set_solution_history (const SolutionHistory & _solution_history)
120 {
121  solution_history = _solution_history.clone();
122 }
123 
125 {
126  return *solution_history;
127 }
128 
130 {
131 }
132 
133 std::pair<unsigned int, Real> TimeSolver::adjoint_solve (const QoISet & qoi_indices)
134 {
135  libmesh_assert(this->diff_solver().get());
136  libmesh_assert_equal_to (&(this->diff_solver()->system()), &(this->system()));
137 
138  return this->_system.ImplicitSystem::adjoint_solve(qoi_indices);
139 }
140 
142 {
143  libmesh_not_implemented();
144 }
145 
146 void TimeSolver::integrate_adjoint_sensitivity(const QoISet & /* qois */, const ParameterVector & /* parameter_vector */, SensitivityData & /* sensitivities */)
147 {
148  libmesh_not_implemented();
149 }
150 
151 #ifdef LIBMESH_ENABLE_AMR
153  (AdjointRefinementEstimator & /* adjoint_refinement_error_estimator */,
154  ErrorVector & /* QoI_elementwise_error */)
155 {
156  libmesh_not_implemented();
157 }
158 #endif // LIBMESH_ENABLE_AMR
159 
161 {
162  return last_deltat;
163 }
164 
166 {
167 }
168 
170 {
171 }
172 
173 } // namespace libMesh
virtual Real last_completed_timestep_size()
Returns system.deltat if fixed timestep solver is used, the complete timestep size (sum of all subste...
Definition: time_solver.C:160
virtual void advance_timestep()
This method advances the solution to the next timestep, after a solve() has been performed.
Definition: time_solver.C:129
Data structure for specifying which Parameters should be independent variables in a parameter sensiti...
static std::unique_ptr< LinearSolver< T > > build(const libMesh::Parallel::Communicator &comm_in, const SolverPackage solver_package=libMesh::default_solver_package())
Builds a LinearSolver using the linear solver package specified by solver_package.
Definition: linear_solver.C:59
This class implements a "brute force" goal-oriented error estimator which computes an estimate of err...
virtual void init_data()
The data initialization function.
Definition: time_solver.C:97
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
Definition: qoi_set.h:45
unsigned int n_qois() const
Number of currently active quantities of interest.
Definition: system.h:2621
The ErrorVector is a specialization of the StatisticsVector for error data computed on a finite eleme...
Definition: error_vector.h:50
std::unique_ptr< SolutionHistory > solution_history
A std::unique_ptr to a SolutionHistory object.
Definition: time_solver.h:319
virtual void reinit()
The reinitialization function.
Definition: time_solver.C:54
const Parallel::Communicator & comm() const
NumericVector< Number > & add_vector(std::string_view vec_name, const bool projections=true, const ParallelType type=PARALLEL)
Adds the additional vector vec_name to this system.
Definition: system.C:768
virtual void integrate_adjoint_refinement_error_estimate(AdjointRefinementEstimator &adjoint_refinement_error_estimator, ErrorVector &QoI_elementwise_error)
A method to compute the adjoint refinement error estimate at the current timestep.
Definition: time_solver.C:153
The libMesh namespace provides an interface to certain functionality in the library.
static std::unique_ptr< DiffSolver > build(sys_type &s)
Factory method.
Definition: diff_solver.C:56
virtual std::unique_ptr< SolutionHistory > clone() const =0
Cloning function for a std::unique_ptr, pure virtual, used in the setter function in time_solver...
virtual std::unique_ptr< DiffSolver > & diff_solver()
An implicit linear or nonlinear solver to use at each timestep.
Definition: time_solver.h:220
A SolutionHistory class that enables the storage and retrieval of timesteps and (in the future) adapt...
const sys_type & system() const
Definition: time_solver.h:210
This class provides a specific system class.
Definition: diff_system.h:54
virtual void integrate_adjoint_sensitivity(const QoISet &qois, const ParameterVector &parameter_vector, SensitivityData &sensitivities)
A method to integrate the adjoint sensitivity w.r.t a given parameter vector.
Definition: time_solver.C:146
sys_type & _system
A reference to the system we are solving.
Definition: time_solver.h:312
TimeSolver(sys_type &s)
Constructor.
Definition: time_solver.C:36
&#39;Save nothing&#39; subclass of Solution History, this is the default.
Data structure for holding completed parameter sensitivity calculations.
virtual void init()
The initialization function.
Definition: time_solver.C:72
virtual std::pair< unsigned int, Real > adjoint_solve(const QoISet &qoi_indices)
This method solves for the adjoint solution at the next adjoint timestep (or a steady state adjoint s...
Definition: time_solver.C:133
libmesh_assert(ctx)
virtual void retrieve_timestep()
This method retrieves all the stored solutions at the current system.time.
Definition: time_solver.C:169
std::unique_ptr< LinearSolver< Number > > _linear_solver
An implicit linear solver to use for adjoint problems.
Definition: time_solver.h:307
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void set_solution_history(const SolutionHistory &_solution_history)
A setter function users will employ if they need to do something other than save no solution history...
Definition: time_solver.C:119
virtual std::unique_ptr< LinearSolver< Number > > & linear_solver()
An implicit linear solver to use for adjoint and sensitivity problems.
Definition: time_solver.h:225
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
Definition: int_range.h:140
virtual ~TimeSolver()
Destructor.
virtual void integrate_qoi_timestep()
A method to integrate the system::QoI functionals.
Definition: time_solver.C:141
Real last_deltat
The deltat for the last completed timestep before the current one.
Definition: time_solver.h:332
bool on_command_line(std::string arg)
Definition: libmesh.C:987
virtual void adjoint_advance_timestep()
This method advances the adjoint solution to the previous timestep, after an adjoint_solve() has been...
Definition: time_solver.C:165
const std::string & name() const
Definition: system.h:2342
virtual void solve()
This method solves for the solution at the next timestep (or solves for a steady-state solution)...
Definition: time_solver.C:111
virtual void init_adjoints()
Initialize any adjoint related data structures, based on the number of qois.
Definition: time_solver.C:83
SolutionHistory & get_solution_history()
A getter function that returns a reference to the solution history object owned by TimeSolver...
Definition: time_solver.C:124