libMesh
Loading...
Searching...
No Matches
diff_context.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
19#include "libmesh/diff_context.h"
20#include "libmesh/diff_system.h"
21#include "libmesh/diff_system.h"
22#include "libmesh/unsteady_solver.h"
23
24// C++ includes
25#include <memory>
26
27namespace libMesh
28{
29
30
31
33 bool allocate_local_matrices) :
34 time(sys.time),
35 system_time(sys.time),
36 elem_solution_derivative(1.),
37 elem_solution_rate_derivative(1.),
38 elem_solution_accel_derivative(1.),
39 fixed_solution_derivative(0.),
40 _have_local_matrices(allocate_local_matrices),
41 _dof_indices_var(sys.n_vars()),
42 _deltat(nullptr),
43 _system(sys),
44 _is_adjoint(false)
45{
46 // Finally initialize solution/residual/jacobian data structures
47 unsigned int nv = sys.n_vars();
48
49 _elem_subsolutions.reserve(nv);
50 _elem_subresiduals.reserve(nv);
51 _elem_subsolution_rates.reserve(nv);
52 _elem_subsolution_accels.reserve(nv);
53
54 if (sys.use_fixed_solution)
55 _elem_fixed_subsolutions.reserve(nv);
56
57 if (allocate_local_matrices)
58 _elem_subjacobians.resize(nv);
59
60 // If the user resizes sys.qoi, it will invalidate us
61 std::size_t n_qoi = sys.n_qois();
62 _elem_qoi.resize(n_qoi);
63 _elem_qoi_derivative.resize(n_qoi);
64 _elem_qoi_subderivatives.resize(n_qoi);
65 for (std::size_t q=0; q != n_qoi; ++q)
66 _elem_qoi_subderivatives[q].reserve(nv);
67
68 for (unsigned int i=0; i != nv; ++i)
69 {
72 for (std::size_t q=0; q != n_qoi; ++q)
74 if (allocate_local_matrices)
75 _elem_subjacobians[i].reserve(nv);
76
77 // Only make space for these if we're using DiffSystem
78 // This is assuming *only* DiffSystem is using elem_solution_rate/accel
79 const DifferentiableSystem * diff_system = dynamic_cast<const DifferentiableSystem *>(&sys);
80 if (diff_system)
81 {
82 // Now, we only need these if the solver is unsteady
83 if (!diff_system->get_time_solver().is_steady())
84 {
86
87 // We only need accel space if the TimeSolver is second order
88 const UnsteadySolver & time_solver = cast_ref<const UnsteadySolver &>(diff_system->get_time_solver());
89
90 if (time_solver.time_order() >= 2 || !diff_system->get_second_order_vars().empty())
92 }
93 }
94
95 if (sys.use_fixed_solution)
97
98 if (allocate_local_matrices)
99 for (unsigned int j=0; j != nv; ++j)
100 _elem_subjacobians[i].emplace_back(_elem_jacobian);
101 }
102}
103
104
105
106DiffContext::~DiffContext () = default;
107
108
110{
111 // We may actually want to be able to set this pointer to nullptr, so
112 // don't report an error for that.
113 _deltat = dt;
114}
115
116
123
124
126{
127 // Make an empty pair keyed with a reference to this _localized_vector
128 _localized_vectors[&localized_vector] = std::make_pair(DenseVector<Number>(), std::vector<DenseSubVector<Number>>());
129
130 unsigned int nv = sys.n_vars();
131
132 _localized_vectors[&localized_vector].second.reserve(nv);
133
134 // Fill the DenseSubVector with nv copies of DenseVector
135 for (unsigned int i=0; i != nv; ++i)
136 _localized_vectors[&localized_vector].second.emplace_back(_localized_vectors[&localized_vector].first);
137}
138
139
141{
142 return _localized_vectors[&localized_vector].first;
143}
144
145
147{
148 auto localized_vectors_it = _localized_vectors.find(&localized_vector);
149 libmesh_assert(localized_vectors_it != _localized_vectors.end());
150 return localized_vectors_it->second.first;
151}
152
153
155{
156 return _localized_vectors[&localized_vector].second[var];
157}
158
159
160const DenseSubVector<Number> & DiffContext::get_localized_subvector (const NumericVector<Number> & localized_vector, unsigned int var) const
161{
162 auto localized_vectors_it = _localized_vectors.find(&localized_vector);
163 libmesh_assert(localized_vectors_it != _localized_vectors.end());
164 return localized_vectors_it->second.second[var];
165}
166
167} // namespace libMesh
unsigned int n_vars
Defines a dense subvector for use in finite element computations.
Defines a dense vector for use in Finite Element-type computations.
DenseVector< Number > _elem_solution_rate
Element by element components of du/dt as adjusted by a time_solver.
std::map< const NumericVector< Number > *, std::pair< DenseVector< Number >, std::vector< DenseSubVector< Number > > > > _localized_vectors
Contains pointers to vectors the user has asked to be localized, keyed with pairs of element localize...
std::vector< Number > _elem_qoi
Element quantity of interest contributions.
std::vector< DenseSubVector< Number > > _elem_fixed_subsolutions
std::vector< DenseSubVector< Number > > _elem_subsolutions
std::vector< std::vector< DenseSubMatrix< Number > > > _elem_subjacobians
DenseVector< Number > & get_localized_vector(const NumericVector< Number > &localized_vector)
Return a reference to DenseVector localization of localized_vector contained in the _localized_vector...
DenseVector< Number > _elem_fixed_solution
Element by element components of nonlinear_solution at a fixed point in a timestep,...
DenseVector< Number > _elem_solution
Element by element components of nonlinear_solution as adjusted by a time_solver.
std::vector< DenseSubVector< Number > > _elem_subsolution_rates
Real * _deltat
Defaults to nullptr, can optionally be used to point to a timestep value in the System-derived class ...
std::vector< std::vector< DenseSubVector< Number > > > _elem_qoi_subderivatives
std::vector< DenseSubVector< Number > > _elem_subresiduals
Element residual subvectors and (if _have_local_matrices) Jacobian submatrices.
DenseMatrix< Number > _elem_jacobian
Element jacobian: derivatives of elem_residual with respect to elem_solution.
DenseVector< Number > _elem_residual
Element residual vector.
virtual ~DiffContext()
Destructor.
void set_deltat_pointer(Real *dt)
Points the _deltat member of this class at a timestep value stored in the creating System,...
DenseSubVector< Number > & get_localized_subvector(const NumericVector< Number > &localized_vector, unsigned int var)
Return a reference to DenseSubVector localization of localized_vector at variable var contained in th...
void add_localized_vector(NumericVector< Number > &localized_vector, const System &sys)
Adds a vector to the map of localized vectors.
std::vector< DenseSubVector< Number > > _elem_subsolution_accels
DiffContext(const System &, bool allocate_local_matrices=true)
Constructor.
DenseVector< Number > _elem_solution_accel
Element by element components of du/dt as adjusted by a time_solver.
std::vector< DenseVector< Number > > _elem_qoi_derivative
Element quantity of interest derivative contributions.
const std::set< unsigned int > & get_second_order_vars() const
This class provides a specific system class.
Definition diff_system.h:57
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition system.h:100
bool use_fixed_solution
A boolean to be set to true by systems using elem_fixed_solution, for optional use by e....
Definition system.h:1625
unsigned int n_qois() const
Number of currently active quantities of interest.
Definition system.h:2562
unsigned int n_vars() const
Definition system.C:2674
virtual bool is_steady() const =0
Is this effectively a steady-state solver?
This is a generic class that defines a solver to handle time integration of DifferentiableSystems.
virtual unsigned int time_order() const =0
The libMesh namespace provides an interface to certain functionality in the library.
libmesh_assert(ctx)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real