libMesh
diff_context.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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 
36 namespace libMesh
37 {
38 
39 // Forward declarations
40 template <typename T> class NumericVector;
41 class System;
42 
56 {
57 public:
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 
119  { return _elem_solution; }
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 
145  { return _elem_solution_rate; }
146 
152  { return _elem_solution_rate; }
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 
178  { return _elem_solution_accel; }
179 
185  { return _elem_solution_accel; }
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 
211  { return _elem_fixed_solution; }
212 
217  { return _elem_fixed_solution; }
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 
249  { return _elem_residual; }
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 
275  {
277  return _elem_jacobian;
278  }
279 
284  {
286  return _elem_jacobian;
287  }
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 
417  Real get_time() const
418  { return time; }
419 
423  void set_time( Real time_in )
424  { time = time_in; }
425 
433  { return elem_solution_derivative; }
434 
443 
452 
460  { return fixed_solution_derivative; }
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 
564 protected:
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 
642 private:
643 
656 
660  const System & _system;
661 
666 
667 };
668 
669 } // namespace libMesh
670 
671 
672 #endif // LIBMESH_DIFF_CONTEXT_H
std::vector< std::vector< dof_id_type > > _dof_indices_var
Definition: diff_context.h:640
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...
Definition: diff_context.h:306
DenseSubVector< Number > & get_elem_solution_accel(unsigned int var)
Accessor for element solution accel for a particular variable corresponding to the variable index arg...
Definition: diff_context.h:201
Real get_elem_solution_derivative() const
The derivative of the current elem_solution w.r.t.
Definition: diff_context.h:432
const std::vector< Number > & get_qois() const
Const accessor for QoI vector.
Definition: diff_context.h:317
DenseVector< Number > _elem_solution
Element by element components of nonlinear_solution as adjusted by a time_solver. ...
Definition: diff_context.h:582
const DenseMatrix< Number > & get_elem_jacobian() const
Const accessor for element Jacobian.
Definition: diff_context.h:274
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...
Definition: diff_context.h:353
std::vector< Number > _elem_qoi
Element quantity of interest contributions.
Definition: diff_context.h:621
Real fixed_solution_derivative
The derivative of elem_fixed_solution with respect to the nonlinear solution, for use by systems cons...
Definition: diff_context.h:517
std::vector< DenseSubVector< Number > > _elem_fixed_subsolutions
Definition: diff_context.h:605
This class provides all data required for a physics package (e.g.
Definition: diff_context.h:55
DenseVector< Number > & get_elem_residual()
Non-const accessor for element residual.
Definition: diff_context.h:248
std::vector< DenseSubVector< Number > > _elem_subsolution_accels
Definition: diff_context.h:597
const DenseVector< Number > & get_elem_fixed_solution() const
Accessor for element fixed solution.
Definition: diff_context.h:210
DenseVector< Number > _elem_fixed_solution
Element by element components of nonlinear_solution at a fixed point in a timestep, for optional use by e.g.
Definition: diff_context.h:604
const DenseVector< Number > & get_elem_solution_rate() const
Accessor for element solution rate of change w.r.t.
Definition: diff_context.h:144
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...
Definition: diff_context.h:223
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...
Definition: diff_context.h:191
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...
Definition: diff_context.h:158
const DenseSubVector< Number > & get_elem_solution(unsigned int var) const
Accessor for element solution of a particular variable corresponding to the variable index argument...
Definition: diff_context.h:125
unsigned int n_dof_indices() const
Total number of dof indices on the element.
Definition: diff_context.h:395
DenseSubVector< Number > & get_elem_solution_rate(unsigned int var)
Accessor for element solution rate for a particular variable corresponding to the variable index argu...
Definition: diff_context.h:168
std::vector< DenseVector< Number > > _elem_qoi_derivative
Element quantity of interest derivative contributions.
Definition: diff_context.h:626
DenseVector< Number > & get_elem_solution_accel()
Non-const accessor for element solution accel of change w.r.t.
Definition: diff_context.h:184
DenseMatrix< Number > _elem_jacobian
Element jacobian: derivatives of elem_residual with respect to elem_solution.
Definition: diff_context.h:616
Real * _deltat
Defaults to nullptr, can optionally be used to point to a timestep value in the System-derived class ...
Definition: diff_context.h:655
std::vector< std::vector< DenseSubVector< Number > > > _elem_qoi_subderivatives
Definition: diff_context.h:627
The libMesh namespace provides an interface to certain functionality in the library.
bool is_adjoint() const
Accessor for querying whether we need to do a primal or adjoint solve.
Definition: diff_context.h:466
DenseMatrix< Number > & get_elem_jacobian()
Non-const accessor for element Jacobian.
Definition: diff_context.h:283
virtual void elem_reinit(Real)
Gives derived classes the opportunity to reinitialize data (FE objects in FEMSystem, for example) needed for an interior integration at a new point within a timestep.
Definition: diff_context.h:77
std::vector< dof_id_type > _dof_indices
Global Degree of freedom index lists.
Definition: diff_context.h:639
const bool _have_local_matrices
Whether we have local matrices allocated/initialized.
Definition: diff_context.h:576
Defines a dense subvector for use in finite element computations.
virtual ~DiffContext()
Destructor.
std::vector< DenseSubVector< Number > > _elem_subsolution_rates
Definition: diff_context.h:590
std::vector< DenseVector< Number > > & get_qoi_derivatives()
Non-const accessor for QoI derivatives.
Definition: diff_context.h:335
void add_localized_vector(NumericVector< Number > &localized_vector, const System &sys)
Adds a vector to the map of localized vectors.
Definition: diff_context.C:125
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...
Definition: diff_context.h:376
DenseVector< Number > & get_elem_fixed_solution()
Non-const accessor for element fixed solution.
Definition: diff_context.h:216
Real get_fixed_solution_derivative() const
The derivative of the current fixed_elem_solution w.r.t.
Definition: diff_context.h:459
Real elem_solution_rate_derivative
The derivative of elem_solution_rate with respect to the current nonlinear solution, for use by systems with non default mass_residual terms.
Definition: diff_context.h:503
const DenseVector< Number > & get_elem_solution() const
Accessor for element solution.
Definition: diff_context.h:112
DenseSubVector< Number > & get_elem_residual(unsigned int var)
Non-const accessor for element residual of a particular variable corresponding to the variable index ...
Definition: diff_context.h:265
const System & get_system() const
Accessor for associated system.
Definition: diff_context.h:106
std::vector< Number > & get_qois()
Non-const accessor for QoI vector.
Definition: diff_context.h:323
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:96
bool & is_adjoint()
Accessor for setting whether we need to do a primal or adjoint solve.
Definition: diff_context.h:473
DenseSubVector< Number > & get_elem_fixed_solution(unsigned int var)
Accessor for element fixed solution of a particular variable corresponding to the variable index argu...
Definition: diff_context.h:233
Defines a dense submatrix for use in Finite Element-type computations.
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...
Definition: diff_context.h:255
Real elem_solution_derivative
The derivative of elem_solution with respect to the current nonlinear solution.
Definition: diff_context.h:496
libmesh_assert(ctx)
const System & _system
A reference to the system this context is constructed with.
Definition: diff_context.h:660
DenseVector< Number > & get_elem_solution_rate()
Non-const accessor for element solution rate of change w.r.t.
Definition: diff_context.h:151
const std::vector< dof_id_type > & get_dof_indices() const
Accessor for element dof indices.
Definition: diff_context.h:363
void set_time(Real time_in)
Set the time for which the current nonlinear_solution is defined.
Definition: diff_context.h:423
std::map< const NumericVector< Number > *, std::pair< DenseVector< Number >, std::vector< DenseSubVector< Number > > > >::iterator localized_vectors_iterator
Typedef for the localized_vectors iterator.
Definition: diff_context.h:540
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...
Definition: diff_context.h:571
unsigned int n_dof_indices(unsigned int var) const
Total number of dof indices of the particular variable corresponding to the index argument...
Definition: diff_context.h:402
const Real system_time
This is the time stored in the System class at the time this context was created, i...
Definition: diff_context.h:490
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...
Definition: diff_context.C:154
Real get_system_time() const
Accessor for the time variable stored in the system class.
Definition: diff_context.h:411
std::vector< DenseSubVector< Number > > _elem_subsolutions
Definition: diff_context.h:583
Real get_elem_solution_accel_derivative() const
The derivative of the current elem_solution_accel w.r.t.
Definition: diff_context.h:450
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...
Definition: diff_context.h:342
const DenseVector< Number > & get_elem_residual() const
Const accessor for element residual.
Definition: diff_context.h:242
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::vector< dof_id_type > & get_dof_indices()
Non-const accessor for element dof indices.
Definition: diff_context.h:369
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...
Definition: diff_context.h:386
virtual void elem_edge_reinit(Real)
Gives derived classes the opportunity to reinitialize data needed for an edge integration at a new po...
Definition: diff_context.h:89
virtual void elem_side_reinit(Real)
Gives derived classes the opportunity to reinitialize data needed for a side integration at a new poi...
Definition: diff_context.h:83
std::vector< std::vector< DenseSubMatrix< Number > > > _elem_subjacobians
Definition: diff_context.h:634
const DenseVector< Number > & get_elem_solution_accel() const
Accessor for element solution accel of change w.r.t.
Definition: diff_context.h:177
Real get_elem_solution_rate_derivative() const
The derivative of the current elem_solution_rate w.r.t.
Definition: diff_context.h:441
DenseVector< Number > _elem_residual
Element residual vector.
Definition: diff_context.h:610
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...
Definition: diff_context.h:293
DenseVector< Number > & get_localized_vector(const NumericVector< Number > &localized_vector)
Return a reference to DenseVector localization of localized_vector contained in the _localized_vector...
Definition: diff_context.C:140
void set_deltat_pointer(Real *dt)
Points the _deltat member of this class at a timestep value stored in the creating System...
Definition: diff_context.C:109
DiffContext(const System &, bool allocate_local_matrices=true)
Constructor.
Definition: diff_context.C:32
Real get_time() const
Accessor for the time for which the current nonlinear_solution is defined.
Definition: diff_context.h:417
bool _is_adjoint
Is this context to be used for a primal or adjoint solve?
Definition: diff_context.h:665
DenseSubVector< Number > & get_elem_solution(unsigned int var)
Accessor for element solution of a particular variable corresponding to the variable index argument...
Definition: diff_context.h:135
DenseVector< Number > _elem_solution_accel
Element by element components of du/dt as adjusted by a time_solver.
Definition: diff_context.h:596
unsigned int n_vars() const
Number of variables in solution.
Definition: diff_context.h:100
const std::vector< DenseVector< Number > > & get_qoi_derivatives() const
Const accessor for QoI derivatives.
Definition: diff_context.h:329
Real elem_solution_accel_derivative
The derivative of elem_solution_accel with respect to the current nonlinear solution, for use by systems with non default mass_residual terms.
Definition: diff_context.h:510
DenseVector< Number > & get_elem_solution()
Non-const accessor for element solution.
Definition: diff_context.h:118
std::vector< DenseSubVector< Number > > _elem_subresiduals
Element residual subvectors and (if _have_local_matrices) Jacobian submatrices.
Definition: diff_context.h:633
Real time
For time-dependent problems, this is the time t for which the current nonlinear_solution is defined...
Definition: diff_context.h:481
virtual void nonlocal_reinit(Real)
Gives derived classes the opportunity to reinitialize data needed for nonlocal calculations at a new ...
Definition: diff_context.h:95
DenseVector< Number > _elem_solution_rate
Element by element components of du/dt as adjusted by a time_solver.
Definition: diff_context.h:589