libMesh
diff_context.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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 
35 namespace libMesh
36 {
37 
38 // Forward declarations
39 template <typename T> class NumericVector;
40 class System;
41 
55 {
56 public:
57 
62  explicit
63  DiffContext (const System &);
64 
68  virtual ~DiffContext ();
69 
75  virtual void elem_reinit(Real /* theta */) {}
76 
81  virtual void elem_side_reinit(Real /* theta */) {}
82 
87  virtual void elem_edge_reinit(Real /* theta */) {}
88 
93  virtual void nonlocal_reinit(Real /* theta */) {}
94 
98  unsigned int n_vars() const
99  { return cast_int<unsigned int>(_dof_indices_var.size()); }
100 
104  const System & get_system() const
105  { return _system; }
106 
111  { return _elem_solution; }
112 
117  { return _elem_solution; }
118 
123  const DenseSubVector<Number> & get_elem_solution( unsigned int var ) const
124  {
125  libmesh_assert_greater(_elem_subsolutions.size(), var);
127  return *(_elem_subsolutions[var]);
128  }
129 
135  {
136  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);
162  return *(_elem_subsolution_rates[var]);
163  }
164 
170  {
171  libmesh_assert_greater(_elem_subsolution_rates.size(), var);
173  return *(_elem_subsolution_rates[var]);
174  }
175 
180  { return _elem_solution_accel; }
181 
187  { return _elem_solution_accel; }
188 
193  const DenseSubVector<Number> & get_elem_solution_accel( unsigned int var ) const
194  {
195  libmesh_assert_greater(_elem_subsolution_accels.size(), var);
197  return *(_elem_subsolution_accels[var]);
198  }
199 
205  {
206  libmesh_assert_greater(_elem_subsolution_accels.size(), var);
208  return *(_elem_subsolution_accels[var]);
209  }
210 
215  { return _elem_fixed_solution; }
216 
221  { return _elem_fixed_solution; }
222 
227  const DenseSubVector<Number> & get_elem_fixed_solution( unsigned int var ) const
228  {
229  libmesh_assert_greater(_elem_fixed_subsolutions.size(), var);
231  return *(_elem_fixed_subsolutions[var]);
232  }
233 
239  {
240  libmesh_assert_greater(_elem_fixed_subsolutions.size(), var);
242  return *(_elem_fixed_subsolutions[var]);
243  }
244 
249  { return _elem_residual; }
250 
255  { return _elem_residual; }
256 
261  const DenseSubVector<Number> & get_elem_residual( unsigned int var ) const
262  {
263  libmesh_assert_greater(_elem_subresiduals.size(), var);
265  return *(_elem_subresiduals[var]);
266  }
267 
273  {
274  libmesh_assert_greater(_elem_subresiduals.size(), var);
276  return *(_elem_subresiduals[var]);
277  }
278 
283  { return _elem_jacobian; }
284 
289  { return _elem_jacobian; }
290 
295  const DenseSubMatrix<Number> & get_elem_jacobian( unsigned int var1, unsigned int var2 ) const
296  {
297  libmesh_assert_greater(_elem_subjacobians.size(), var1);
298  libmesh_assert_greater(_elem_subjacobians[var1].size(), var2);
299  libmesh_assert(_elem_subjacobians[var1][var2]);
300  return *(_elem_subjacobians[var1][var2]);
301  }
302 
307  DenseSubMatrix<Number> & get_elem_jacobian( unsigned int var1, unsigned int var2 )
308  {
309  libmesh_assert_greater(_elem_subjacobians.size(), var1);
310  libmesh_assert_greater(_elem_subjacobians[var1].size(), var2);
311  libmesh_assert(_elem_subjacobians[var1][var2]);
312  return *(_elem_subjacobians[var1][var2]);
313  }
314 
318  const std::vector<Number> & get_qois() const
319  { return _elem_qoi; }
320 
324  std::vector<Number> & get_qois()
325  { return _elem_qoi; }
326 
330  const std::vector<DenseVector<Number>> & get_qoi_derivatives() const
331  { return _elem_qoi_derivative; }
332 
336  std::vector<DenseVector<Number>> & get_qoi_derivatives()
337  { return _elem_qoi_derivative; }
338 
343  const DenseSubVector<Number> & get_qoi_derivatives( unsigned int qoi, unsigned int var ) const
344  {
345  libmesh_assert_greater(_elem_qoi_subderivatives.size(), qoi);
346  libmesh_assert_greater(_elem_qoi_subderivatives[qoi].size(), var);
348  return *(_elem_qoi_subderivatives[qoi][var]);
349  }
350 
355  DenseSubVector<Number> & get_qoi_derivatives( unsigned int qoi, unsigned int var )
356  {
357  libmesh_assert_greater(_elem_qoi_subderivatives.size(), qoi);
358  libmesh_assert_greater(_elem_qoi_subderivatives[qoi].size(), var);
360  return *(_elem_qoi_subderivatives[qoi][var]);
361  }
362 
366  const std::vector<dof_id_type> & get_dof_indices() const
367  { return _dof_indices; }
368 
372  std::vector<dof_id_type> & get_dof_indices()
373  { return _dof_indices; }
374 
379  const std::vector<dof_id_type> & get_dof_indices( unsigned int var ) const
380  {
381  libmesh_assert_greater(_dof_indices_var.size(), var);
382  return _dof_indices_var[var];
383  }
384 
389  std::vector<dof_id_type> & get_dof_indices( unsigned int var )
390  {
391  libmesh_assert_greater(_dof_indices_var.size(), var);
392  return _dof_indices_var[var];
393  }
394 
399  { return system_time; }
400 
404  Real get_time() const
405  { return time; }
406 
410  void set_time( Real time_in )
411  { time = time_in; }
412 
420  { return elem_solution_derivative; }
421 
430 
439 
447  { return fixed_solution_derivative; }
448 
453  bool is_adjoint() const
454  { return _is_adjoint; }
455 
460  bool & is_adjoint()
461  { return _is_adjoint; }
462 
469 
478 
484 
491 
498 
505 
510  void set_deltat_pointer(Real * dt);
511 
517 
522  void add_localized_vector (NumericVector<Number> & localized_vector, const System & sys);
523 
527  typedef std::map<const NumericVector<Number> *, std::pair<DenseVector<Number>, std::vector<DenseSubVector<Number> *>>>::iterator localized_vectors_iterator;
528 
534 
538  const DenseVector<Number> & get_localized_vector (const NumericVector<Number> & localized_vector) const;
539 
544  DenseSubVector<Number> & get_localized_subvector (const NumericVector<Number> & localized_vector, unsigned int var);
545 
549  const DenseSubVector<Number> & get_localized_subvector (const NumericVector<Number> & localized_vector, unsigned int var) const;
550 
551 protected:
552 
558  std::map<const NumericVector<Number> *, std::pair<DenseVector<Number>, std::vector<DenseSubVector<Number> *>>> _localized_vectors;
559 
565  std::vector<DenseSubVector<Number> *> _elem_subsolutions;
566 
572  std::vector<DenseSubVector<Number> *> _elem_subsolution_rates;
573 
579  std::vector<DenseSubVector<Number> *> _elem_subsolution_accels;
580 
587  std::vector<DenseSubVector<Number> *> _elem_fixed_subsolutions;
588 
593 
599 
603  std::vector<Number> _elem_qoi;
604 
608  std::vector<DenseVector<Number>> _elem_qoi_derivative;
609  std::vector<std::vector<DenseSubVector<Number> *>> _elem_qoi_subderivatives;
610 
614  std::vector<DenseSubVector<Number> *> _elem_subresiduals;
615  std::vector<std::vector<DenseSubMatrix<Number> *>> _elem_subjacobians;
616 
620  std::vector<dof_id_type> _dof_indices;
621  std::vector<std::vector<dof_id_type>> _dof_indices_var;
622 
623 private:
624 
637 
641  const System & _system;
642 
647 
648 };
649 
650 } // namespace libMesh
651 
652 
653 #endif // LIBMESH_DIFF_CONTEXT_H
std::vector< std::vector< dof_id_type > > _dof_indices_var
Definition: diff_context.h:621
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:307
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:204
std::vector< DenseSubVector< Number > * > _elem_subsolution_rates
Definition: diff_context.h:572
DenseVector< Number > _elem_solution
Element by element components of nonlinear_solution as adjusted by a time_solver. ...
Definition: diff_context.h:564
Real get_fixed_solution_derivative() const
The derivative of the current fixed_elem_solution w.r.t.
Definition: diff_context.h:446
std::vector< Number > _elem_qoi
Element quantity of interest contributions.
Definition: diff_context.h:603
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:504
const DenseVector< Number > & get_elem_solution() const
Accessor for element solution.
Definition: diff_context.h:110
This class provides all data required for a physics package (e.g.
Definition: diff_context.h:54
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:558
DenseVector< Number > & get_elem_residual()
Non-const accessor for element residual.
Definition: diff_context.h:254
DenseSubVector< Number > & get_qoi_derivatives(unsigned int 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:355
std::vector< std::vector< DenseSubMatrix< Number > * > > _elem_subjacobians
Definition: diff_context.h:615
bool is_adjoint() const
Accessor for querying whether we need to do a primal or adjoint solve.
Definition: diff_context.h:453
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:586
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:295
ImplicitSystem & sys
std::vector< DenseSubVector< Number > * > _elem_subsolution_accels
Definition: diff_context.h:579
const DenseSubVector< Number > & get_qoi_derivatives(unsigned int 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:343
std::vector< std::vector< DenseSubVector< Number > * > > _elem_qoi_subderivatives
Definition: diff_context.h:609
const DenseVector< Number > & get_elem_solution_accel() const
Accessor for element solution accel of change w.r.t.
Definition: diff_context.h:179
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:169
std::vector< DenseVector< Number > > _elem_qoi_derivative
Element quantity of interest derivative contributions.
Definition: diff_context.h:608
const std::vector< dof_id_type > & get_dof_indices() const
Accessor for element dof indices.
Definition: diff_context.h:366
DenseVector< Number > & get_elem_solution_accel()
Non-const accessor for element solution accel of change w.r.t.
Definition: diff_context.h:186
const DenseVector< Number > & get_elem_fixed_solution() const
Accessor for element fixed solution.
Definition: diff_context.h:214
DenseMatrix< Number > _elem_jacobian
Element jacobian: derivatives of elem_residual with respect to elem_solution.
Definition: diff_context.h:598
Real * _deltat
Default NULL, can optionally be used to point to a timestep value in the System-derived class respons...
Definition: diff_context.h:636
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:123
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:227
The libMesh namespace provides an interface to certain functionality in the library.
const DenseVector< Number > & get_elem_residual() const
Const accessor for element residual.
Definition: diff_context.h:248
DenseMatrix< Number > & get_elem_jacobian()
Non-const accessor for element Jacobian.
Definition: diff_context.h:288
Real get_elem_solution_accel_derivative() const
The derivative of the current elem_solution_accel w.r.t.
Definition: diff_context.h:437
std::vector< DenseSubVector< Number > * > _elem_subresiduals
Element residual subvectors and Jacobian submatrices.
Definition: diff_context.h:614
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:75
std::vector< dof_id_type > _dof_indices
Global Degree of freedom index lists.
Definition: diff_context.h:620
DiffContext(const System &)
Constructor.
Definition: diff_context.C:29
libmesh_assert(j)
Defines a dense subvector for use in finite element computations.
virtual ~DiffContext()
Destructor.
Definition: diff_context.C:101
std::vector< DenseVector< Number > > & get_qoi_derivatives()
Non-const accessor for QoI derivatives.
Definition: diff_context.h:336
void add_localized_vector(NumericVector< Number > &localized_vector, const System &sys)
Adds a vector to the map of localized vectors.
Definition: diff_context.C:154
const DenseMatrix< Number > & get_elem_jacobian() const
Const accessor for element Jacobian.
Definition: diff_context.h:282
DenseVector< Number > & get_elem_fixed_solution()
Non-const accessor for element fixed solution.
Definition: diff_context.h:220
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:490
const std::vector< DenseVector< Number > > & get_qoi_derivatives() const
Const accessor for QoI derivatives.
Definition: diff_context.h:330
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:527
Real get_elem_solution_rate_derivative() const
The derivative of the current elem_solution_rate w.r.t.
Definition: diff_context.h:428
std::vector< DenseSubVector< Number > * > _elem_fixed_subsolutions
Definition: diff_context.h:587
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:272
std::vector< Number > & get_qois()
Non-const accessor for QoI vector.
Definition: diff_context.h:324
const std::vector< Number > & get_qois() const
Const accessor for QoI vector.
Definition: diff_context.h:318
This is the base class for classes which contain information related to any physical process that mig...
Definition: system.h:76
bool & is_adjoint()
Accessor for setting whether we need to do a primal or adjoint solve.
Definition: diff_context.h:460
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:238
Defines a dense submatrix for use in Finite Element-type computations.
Real elem_solution_derivative
The derivative of elem_solution with respect to the current nonlinear solution.
Definition: diff_context.h:483
const System & _system
A reference to the system this context is constructed with.
Definition: diff_context.h:641
DenseVector< Number > & get_elem_solution_rate()
Non-const accessor for element solution rate of change w.r.t.
Definition: diff_context.h:151
void set_time(Real time_in)
Set the time for which the current nonlinear_solution is defined.
Definition: diff_context.h:410
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 Real system_time
This is the time stored in the System class at the time this context was created, i...
Definition: diff_context.h:477
const System & get_system() const
Accessor for associated system.
Definition: diff_context.h:104
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:184
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:193
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_residual(unsigned int var) const
Const accessor for element residual of a particular variable corresponding to the variable index argu...
Definition: diff_context.h:261
Real get_time() const
Accessor for the time for which the current nonlinear_solution is defined.
Definition: diff_context.h:404
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:372
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:389
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:87
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:379
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:81
Real get_elem_solution_derivative() const
The derivative of the current elem_solution w.r.t.
Definition: diff_context.h:419
DenseVector< Number > _elem_residual
Element residual vector.
Definition: diff_context.h:592
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:169
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:138
bool _is_adjoint
Is this context to be used for a primal or adjoint solve?
Definition: diff_context.h:646
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:134
std::vector< DenseSubVector< Number > * > _elem_subsolutions
Definition: diff_context.h:565
DenseVector< Number > _elem_solution_accel
Element by element components of du/dt as adjusted by a time_solver.
Definition: diff_context.h:578
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:497
DenseVector< Number > & get_elem_solution()
Non-const accessor for element solution.
Definition: diff_context.h:116
Real time
For time-dependent problems, this is the time t for which the current nonlinear_solution is defined...
Definition: diff_context.h:468
Real get_system_time() const
Accessor for the time variable stored in the system class.
Definition: diff_context.h:398
virtual void nonlocal_reinit(Real)
Gives derived classes the opportunity to reinitialize data needed for nonlocal calculations at a new ...
Definition: diff_context.h:93
unsigned int n_vars() const
Number of variables in solution.
Definition: diff_context.h:98
DenseVector< Number > _elem_solution_rate
Element by element components of du/dt as adjusted by a time_solver.
Definition: diff_context.h:571