libMesh
Loading...
Searching...
No Matches
diff_context.h
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
20#ifndef LIBMESH_DIFF_CONTEXT_H
21#define LIBMESH_DIFF_CONTEXT_H
22
23// Local Includes
24#include "libmesh/dense_matrix.h"
25#include "libmesh/dense_submatrix.h"
26#include "libmesh/dense_subvector.h"
27#include "libmesh/dense_vector.h"
28#include "libmesh/id_types.h"
29
30// C++ includes
31#include <cstddef>
32#include <map>
33#include <vector>
34#include <memory>
35
36namespace libMesh
37{
38
39// Forward declarations
40template <typename T> class NumericVector;
41class System;
42
56{
57public:
58
63 explicit
64 DiffContext (const System &,
65 bool allocate_local_matrices = true);
66
70 virtual ~DiffContext ();
71
77 virtual void elem_reinit(Real /* theta */) {}
78
83 virtual void elem_side_reinit(Real /* theta */) {}
84
89 virtual void elem_edge_reinit(Real /* theta */) {}
90
95 virtual void nonlocal_reinit(Real /* theta */) {}
96
100 unsigned int n_vars() const
101 { return cast_int<unsigned int>(_dof_indices_var.size()); }
102
106 const System & get_system() const
107 { return _system; }
108
113 { return _elem_solution; }
114
120
125 const DenseSubVector<Number> & get_elem_solution( unsigned int var ) const
126 {
127 libmesh_assert_greater(_elem_subsolutions.size(), var);
128 return _elem_subsolutions[var];
129 }
130
136 {
137 libmesh_assert_greater(_elem_subsolutions.size(), var);
138 return _elem_subsolutions[var];
139 }
140
146
153
158 const DenseSubVector<Number> & get_elem_solution_rate( unsigned int var ) const
159 {
160 libmesh_assert_greater(_elem_subsolution_rates.size(), var);
161 return _elem_subsolution_rates[var];
162 }
163
169 {
170 libmesh_assert_greater(_elem_subsolution_rates.size(), var);
171 return _elem_subsolution_rates[var];
172 }
173
179
186
191 const DenseSubVector<Number> & get_elem_solution_accel( unsigned int var ) const
192 {
193 libmesh_assert_greater(_elem_subsolution_accels.size(), var);
194 return _elem_subsolution_accels[var];
195 }
196
202 {
203 libmesh_assert_greater(_elem_subsolution_accels.size(), var);
204 return _elem_subsolution_accels[var];
205 }
206
212
218
223 const DenseSubVector<Number> & get_elem_fixed_solution( unsigned int var ) const
224 {
225 libmesh_assert_greater(_elem_fixed_subsolutions.size(), var);
226 return _elem_fixed_subsolutions[var];
227 }
228
234 {
235 libmesh_assert_greater(_elem_fixed_subsolutions.size(), var);
236 return _elem_fixed_subsolutions[var];
237 }
238
243 { return _elem_residual; }
244
250
255 const DenseSubVector<Number> & get_elem_residual( unsigned int var ) const
256 {
257 libmesh_assert_greater(_elem_subresiduals.size(), var);
258 return _elem_subresiduals[var];
259 }
260
266 {
267 libmesh_assert_greater(_elem_subresiduals.size(), var);
268 return _elem_subresiduals[var];
269 }
270
279
288
293 const DenseSubMatrix<Number> & get_elem_jacobian( unsigned int var1, unsigned int var2 ) const
294 {
296 libmesh_assert_greater(_elem_subjacobians.size(), var1);
297 libmesh_assert_greater(_elem_subjacobians[var1].size(), var2);
298 return _elem_subjacobians[var1][var2];
299 }
300
306 DenseSubMatrix<Number> & get_elem_jacobian( unsigned int var1, unsigned int var2 )
307 {
309 libmesh_assert_greater(_elem_subjacobians.size(), var1);
310 libmesh_assert_greater(_elem_subjacobians[var1].size(), var2);
311 return _elem_subjacobians[var1][var2];
312 }
313
317 const std::vector<Number> & get_qois() const
318 { return _elem_qoi; }
319
323 std::vector<Number> & get_qois()
324 { return _elem_qoi; }
325
329 const std::vector<DenseVector<Number>> & get_qoi_derivatives() const
330 { return _elem_qoi_derivative; }
331
335 std::vector<DenseVector<Number>> & get_qoi_derivatives()
336 { return _elem_qoi_derivative; }
337
342 const DenseSubVector<Number> & get_qoi_derivatives( std::size_t qoi, unsigned int var ) const
343 {
344 libmesh_assert_greater(_elem_qoi_subderivatives.size(), qoi);
345 libmesh_assert_greater(_elem_qoi_subderivatives[qoi].size(), var);
346 return _elem_qoi_subderivatives[qoi][var];
347 }
348
353 DenseSubVector<Number> & get_qoi_derivatives( std::size_t qoi, unsigned int var )
354 {
355 libmesh_assert_greater(_elem_qoi_subderivatives.size(), qoi);
356 libmesh_assert_greater(_elem_qoi_subderivatives[qoi].size(), var);
357 return _elem_qoi_subderivatives[qoi][var];
358 }
359
363 const std::vector<dof_id_type> & get_dof_indices() const
364 { return _dof_indices; }
365
369 std::vector<dof_id_type> & get_dof_indices()
370 { return _dof_indices; }
371
376 const std::vector<dof_id_type> & get_dof_indices( unsigned int var ) const
377 {
378 libmesh_assert_greater(_dof_indices_var.size(), var);
379 return _dof_indices_var[var];
380 }
381
386 std::vector<dof_id_type> & get_dof_indices( unsigned int var )
387 {
388 libmesh_assert_greater(_dof_indices_var.size(), var);
389 return _dof_indices_var[var];
390 }
391
395 unsigned int n_dof_indices() const
396 { return cast_int<unsigned int>(_dof_indices.size()); }
397
402 unsigned int n_dof_indices( unsigned int var ) const
403 {
404 libmesh_assert_greater(_dof_indices_var.size(), var);
405 return cast_int<unsigned int>(_dof_indices_var[var].size());
406 }
407
412 { return system_time; }
413
418 { return time; }
419
423 void set_time( Real time_in )
424 { time = time_in; }
425
434
443
452
461
466 bool is_adjoint() const
467 { return _is_adjoint; }
468
473 bool & is_adjoint()
474 { return _is_adjoint; }
475
482
491
497
504
511
518
523 void set_deltat_pointer(Real * dt);
524
530
535 void add_localized_vector (NumericVector<Number> & localized_vector, const System & sys);
536
540 typedef std::map<const NumericVector<Number> *, std::pair<DenseVector<Number>, std::vector<DenseSubVector<Number>>>>::iterator localized_vectors_iterator;
541
547
551 const DenseVector<Number> & get_localized_vector (const NumericVector<Number> & localized_vector) const;
552
557 DenseSubVector<Number> & get_localized_subvector (const NumericVector<Number> & localized_vector, unsigned int var);
558
562 const DenseSubVector<Number> & get_localized_subvector (const NumericVector<Number> & localized_vector, unsigned int var) const;
563
564protected:
565
571 std::map<const NumericVector<Number> *, std::pair<DenseVector<Number>, std::vector<DenseSubVector<Number>>>> _localized_vectors;
572
577
583 std::vector<DenseSubVector<Number>> _elem_subsolutions;
584
590 std::vector<DenseSubVector<Number>> _elem_subsolution_rates;
591
597 std::vector<DenseSubVector<Number>> _elem_subsolution_accels;
598
605 std::vector<DenseSubVector<Number>> _elem_fixed_subsolutions;
606
611
617
621 std::vector<Number> _elem_qoi;
622
626 std::vector<DenseVector<Number>> _elem_qoi_derivative;
627 std::vector<std::vector<DenseSubVector<Number>>> _elem_qoi_subderivatives;
628
633 std::vector<DenseSubVector<Number>> _elem_subresiduals;
634 std::vector<std::vector<DenseSubMatrix<Number>>> _elem_subjacobians;
635
639 std::vector<dof_id_type> _dof_indices;
640 std::vector<std::vector<dof_id_type>> _dof_indices_var;
641
642private:
643
656
661
666
667};
668
669} // namespace libMesh
670
671
672#endif // LIBMESH_DIFF_CONTEXT_H
Defines a dense matrix for use in Finite Element-type computations.
Defines a dense submatrix for use in Finite Element-type computations.
Defines a dense subvector for use in finite element computations.
Defines a dense vector for use in Finite Element-type computations.
This class provides all data required for a physics package (e.g.
DenseVector< Number > _elem_solution_rate
Element by element components of du/dt as adjusted by a time_solver.
Real elem_solution_derivative
The derivative of elem_solution with respect to the current nonlinear solution.
const DenseSubVector< Number > & get_elem_fixed_solution(unsigned int var) const
Accessor for element fixed solution of a particular variable corresponding to the variable index argu...
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...
const DenseSubVector< Number > & get_elem_solution(unsigned int var) const
Accessor for element solution of a particular variable corresponding to the variable index argument.
DenseMatrix< Number > & get_elem_jacobian()
Non-const accessor for element Jacobian.
const DenseVector< Number > & get_elem_solution_rate() const
Accessor for element solution rate of change w.r.t.
Real time
For time-dependent problems, this is the time t for which the current nonlinear_solution is defined.
std::vector< Number > _elem_qoi
Element quantity of interest contributions.
std::vector< DenseSubVector< Number > > _elem_fixed_subsolutions
const std::vector< Number > & get_qois() const
Const accessor for QoI vector.
Real elem_solution_accel_derivative
The derivative of elem_solution_accel with respect to the current nonlinear solution,...
std::vector< DenseSubVector< Number > > _elem_subsolutions
DenseSubVector< Number > & get_elem_solution(unsigned int var)
Accessor for element solution of a particular variable corresponding to the variable index argument.
std::vector< std::vector< DenseSubMatrix< Number > > > _elem_subjacobians
DenseVector< Number > & get_elem_solution_rate()
Non-const accessor for element solution rate of change w.r.t.
DenseVector< Number > & get_localized_vector(const NumericVector< Number > &localized_vector)
Return a reference to DenseVector localization of localized_vector contained in the _localized_vector...
DenseSubMatrix< Number > & get_elem_jacobian(unsigned int var1, unsigned int var2)
Non-const accessor for element Jacobian of particular variables corresponding to the variable index a...
const Real system_time
This is the time stored in the System class at the time this context was created, i....
unsigned int n_vars() const
Number of variables in solution.
DenseVector< Number > _elem_fixed_solution
Element by element components of nonlinear_solution at a fixed point in a timestep,...
std::vector< dof_id_type > & get_dof_indices()
Non-const accessor for element dof indices.
virtual void elem_edge_reinit(Real)
Gives derived classes the opportunity to reinitialize data needed for an edge integration at a new po...
const DenseSubVector< Number > & get_elem_solution_accel(unsigned int var) const
Accessor for element solution accel for a particular variable corresponding to the variable index arg...
DenseVector< Number > _elem_solution
Element by element components of nonlinear_solution as adjusted by a time_solver.
virtual void elem_reinit(Real)
Gives derived classes the opportunity to reinitialize data (FE objects in FEMSystem,...
DenseSubVector< Number > & get_elem_solution_accel(unsigned int var)
Accessor for element solution accel for a particular variable corresponding to the variable index arg...
Real get_elem_solution_derivative() const
The derivative of the current elem_solution w.r.t.
std::vector< DenseSubVector< Number > > _elem_subsolution_rates
virtual void elem_side_reinit(Real)
Gives derived classes the opportunity to reinitialize data needed for a side integration at a new poi...
Real * _deltat
Defaults to nullptr, can optionally be used to point to a timestep value in the System-derived class ...
std::map< constNumericVector< Number > *, std::pair< DenseVector< Number >, std::vector< DenseSubVector< Number > > > >::iterator localized_vectors_iterator
Typedef for the localized_vectors iterator.
std::vector< DenseVector< Number > > & get_qoi_derivatives()
Non-const accessor for QoI derivatives.
const std::vector< dof_id_type > & get_dof_indices() const
Accessor for element dof indices.
Real elem_solution_rate_derivative
The derivative of elem_solution_rate with respect to the current nonlinear solution,...
std::vector< Number > & get_qois()
Non-const accessor for QoI vector.
const DenseSubVector< Number > & get_elem_residual(unsigned int var) const
Const accessor for element residual of a particular variable corresponding to the variable index argu...
Real get_system_time() const
Accessor for the time variable stored in the system class.
const DenseSubVector< Number > & get_qoi_derivatives(std::size_t qoi, unsigned int var) const
Const accessor for QoI derivative of a particular qoi and variable corresponding to the index argumen...
bool is_adjoint() const
Accessor for querying whether we need to do a primal or adjoint solve.
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.
DenseVector< Number > & get_elem_fixed_solution()
Non-const accessor for element fixed solution.
const System & _system
A reference to the system this context is constructed with.
std::vector< std::vector< dof_id_type > > _dof_indices_var
DenseVector< Number > & get_elem_solution()
Non-const accessor for element solution.
DenseVector< Number > & get_elem_solution_accel()
Non-const accessor for element solution accel of change w.r.t.
const DenseVector< Number > & get_elem_solution() const
Accessor for element solution.
bool _is_adjoint
Is this context to be used for a primal or adjoint solve?
const DenseVector< Number > & get_elem_solution_accel() const
Accessor for element solution accel of change w.r.t.
DenseMatrix< Number > _elem_jacobian
Element jacobian: derivatives of elem_residual with respect to elem_solution.
Real get_elem_solution_accel_derivative() const
The derivative of the current elem_solution_accel w.r.t.
DenseSubVector< Number > & get_elem_fixed_solution(unsigned int var)
Accessor for element fixed solution of a particular variable corresponding to the variable index argu...
DenseSubVector< Number > & get_elem_solution_rate(unsigned int var)
Accessor for element solution rate for a particular variable corresponding to the variable index argu...
unsigned int n_dof_indices() const
Total number of dof indices on the element.
const DenseSubMatrix< Number > & get_elem_jacobian(unsigned int var1, unsigned int var2) const
Const accessor for element Jacobian of particular variables corresponding to the variable index argum...
DenseVector< Number > _elem_residual
Element residual vector.
Real fixed_solution_derivative
The derivative of elem_fixed_solution with respect to the nonlinear solution, for use by systems cons...
unsigned int n_dof_indices(unsigned int var) const
Total number of dof indices of the particular variable corresponding to the index argument.
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,...
std::vector< dof_id_type > & get_dof_indices(unsigned int var)
Accessor for element dof indices of a particular variable corresponding to the index argument.
void set_time(Real time_in)
Set the time for which the current nonlinear_solution is defined.
bool & is_adjoint()
Accessor for setting whether we need to do a primal or adjoint solve.
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...
std::vector< dof_id_type > _dof_indices
Global Degree of freedom index lists.
const System & get_system() const
Accessor for associated system.
const DenseVector< Number > & get_elem_fixed_solution() const
Accessor for element fixed solution.
void add_localized_vector(NumericVector< Number > &localized_vector, const System &sys)
Adds a vector to the map of localized vectors.
DenseSubVector< Number > & get_elem_residual(unsigned int var)
Non-const accessor for element residual of a particular variable corresponding to the variable index ...
std::vector< DenseSubVector< Number > > _elem_subsolution_accels
const std::vector< dof_id_type > & get_dof_indices(unsigned int var) const
Accessor for element dof indices of a particular variable corresponding to the index argument.
Real get_time() const
Accessor for the time for which the current nonlinear_solution is defined.
DenseVector< Number > _elem_solution_accel
Element by element components of du/dt as adjusted by a time_solver.
const DenseSubVector< Number > & get_elem_solution_rate(unsigned int var) const
Accessor for element solution rate for a particular variable corresponding to the variable index argu...
virtual void nonlocal_reinit(Real)
Gives derived classes the opportunity to reinitialize data needed for nonlocal calculations at a new ...
const std::vector< DenseVector< Number > > & get_qoi_derivatives() const
Const accessor for QoI derivatives.
DenseSubVector< Number > & get_qoi_derivatives(std::size_t qoi, unsigned int var)
Non-const accessor for QoI derivative of a particular qoi and variable corresponding to the index arg...
std::vector< DenseVector< Number > > _elem_qoi_derivative
Element quantity of interest derivative contributions.
DenseVector< Number > & get_elem_residual()
Non-const accessor for element residual.
Real get_elem_solution_rate_derivative() const
The derivative of the current elem_solution_rate w.r.t.
const bool _have_local_matrices
Whether we have local matrices allocated/initialized.
const DenseVector< Number > & get_elem_residual() const
Const accessor for element residual.
const DenseMatrix< Number > & get_elem_jacobian() const
Const accessor for element Jacobian.
Real get_fixed_solution_derivative() const
The derivative of the current fixed_elem_solution w.r.t.
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
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