libMesh
Loading...
Searching...
No Matches
time_solver.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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
33namespace 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
50TimeSolver::~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_systems(_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_systems(_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
119void TimeSolver::set_solution_history (const SolutionHistory & _solution_history)
120{
121 solution_history = _solution_history.clone();
122}
123
128
132
133std::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
146void 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
164
168
172
173} // namespace libMesh
This class implements a "brute force" goal-oriented error estimator which computes an estimate of err...
static std::unique_ptr< DiffSolver > build(sys_type &s)
Factory method.
Definition diff_solver.C:56
This class provides a specific system class.
Definition diff_system.h:57
The ErrorVector is a specialization of the StatisticsVector for error data computed on a finite eleme...
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.
'Save nothing' subclass of Solution History, this is the default.
virtual void init(const numeric_index_type n, const numeric_index_type n_local, const bool fast=false, const ParallelType ptype=AUTOMATIC)=0
Change the dimension of the vector to n.
const Parallel::Communicator & comm() const
Data structure for specifying which Parameters should be independent variables in a parameter sensiti...
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
Definition qoi_set.h:46
Data structure for holding completed parameter sensitivity calculations.
A SolutionHistory class that enables the storage and retrieval of timesteps and (in the future) adapt...
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....
const std::string & name() const
Definition system.h:2385
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:756
unsigned int n_qois() const
Number of currently active quantities of interest.
Definition system.h:2562
TimeSolver(sys_type &s)
Constructor.
Definition time_solver.C:36
virtual ~TimeSolver()
Destructor.
virtual void init_adjoints()
Initialize any adjoint related data structures, based on the number of qois.
Definition time_solver.C:83
virtual void init()
The initialization function.
Definition time_solver.C:72
sys_type & _system
A reference to the system we are solving.
virtual void integrate_qoi_timestep()
A method to integrate the system::QoI functionals.
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.
const sys_type & system() const
virtual std::unique_ptr< DiffSolver > & diff_solver()
An implicit linear or nonlinear solver to use at each timestep.
virtual std::unique_ptr< LinearSolver< Number > > & linear_solver()
An implicit linear solver to use for adjoint and sensitivity problems.
SolutionHistory & get_solution_history()
A getter function that returns a reference to the solution history object owned by TimeSolver.
Real last_deltat
The deltat for the last completed timestep before the current one.
std::unique_ptr< LinearSolver< Number > > _linear_solver
An implicit linear solver to use for adjoint problems.
virtual void solve()
This method solves for the solution at the next timestep (or solves for a steady-state solution).
virtual void init_data()
The data initialization function.
Definition time_solver.C:97
virtual void retrieve_timestep()
This method retrieves all the stored solutions at the current system.time.
virtual void reinit()
The reinitialization function.
Definition time_solver.C:54
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...
virtual Real last_completed_timestep_size()
Returns system.deltat if fixed timestep solver is used, the complete timestep size (sum of all subste...
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.
std::unique_ptr< SolutionHistory > solution_history
A std::unique_ptr to a SolutionHistory object.
virtual void adjoint_advance_timestep()
This method advances the adjoint solution to the previous timestep, after an adjoint_solve() has been...
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.
virtual void advance_timestep()
This method advances the solution to the next timestep, after a solve() has been performed.
The libMesh namespace provides an interface to certain functionality in the library.
libmesh_assert(ctx)
bool on_command_line(std::string arg)
Definition libmesh.C:934
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
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:176