libMesh
implicit_system.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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_IMPLICIT_SYSTEM_H
21 #define LIBMESH_IMPLICIT_SYSTEM_H
22 
23 // Local Includes
24 #include "libmesh/explicit_system.h"
25 #include "libmesh/auto_ptr.h"
26 
27 // C++ includes
28 #include <cstddef>
29 
30 namespace libMesh
31 {
32 
33 // Forward declarations
34 template <typename T> class LinearSolver;
35 template <typename T> class SparseMatrix;
36 
58 {
59 public:
60 
66  const std::string & name,
67  const unsigned int number);
68 
72  virtual ~ImplicitSystem ();
73 
78 
82  sys_type & system () { return *this; }
83 
88 
93  virtual void clear () override;
94 
99  virtual void reinit () override;
100 
106  virtual void assemble () override;
107 
112  virtual void disable_cache () override;
113 
118  virtual std::string system_type () const override { return "Implicit"; }
119 
134  virtual LinearSolver<Number> * get_linear_solver() const;
135 
141  virtual std::pair<unsigned int, Real>
143 
152  virtual void release_linear_solver(LinearSolver<Number> *) const;
153 
161  virtual void assembly (bool /* get_residual */,
162  bool /* get_jacobian */,
163  bool /* apply_heterogeneous_constraints */ = false,
164  bool /* apply_no_constraints */ = false)
165  { libmesh_not_implemented(); }
166 
178  virtual void assemble_residual_derivatives (const ParameterVector & parameters) override;
179 
185  virtual void solve () override
186  { libmesh_not_implemented(); }
187 
195  virtual std::pair<unsigned int, Real>
196  sensitivity_solve (const ParameterVector & parameters) override;
197 
206  virtual std::pair<unsigned int, Real>
207  weighted_sensitivity_solve (const ParameterVector & parameters,
208  const ParameterVector & weights) override;
209 
219  virtual std::pair<unsigned int, Real>
220  adjoint_solve (const QoISet & qoi_indices = QoISet()) override;
221 
234  virtual std::pair<unsigned int, Real>
236  const ParameterVector & weights,
237  const QoISet & qoi_indices = QoISet()) override;
238 
250  virtual void adjoint_qoi_parameter_sensitivity (const QoISet & qoi_indices,
251  const ParameterVector & parameters,
252  SensitivityData & sensitivities) override;
253 
265  virtual void forward_qoi_parameter_sensitivity (const QoISet & qoi_indices,
266  const ParameterVector & parameters,
267  SensitivityData & sensitivities) override;
268 
284  virtual void qoi_parameter_hessian(const QoISet & qoi_indices,
285  const ParameterVector & parameters,
286  SensitivityData & hessian) override;
287 
298  virtual void qoi_parameter_hessian_vector_product(const QoISet & qoi_indices,
299  const ParameterVector & parameters,
300  const ParameterVector & vector,
301  SensitivityData & product) override;
302 
306  typedef std::map<std::string, SparseMatrix<Number> *>::iterator matrices_iterator;
307  typedef std::map<std::string, SparseMatrix<Number> *>::const_iterator const_matrices_iterator;
308 
321  SparseMatrix<Number> & add_matrix (const std::string & mat_name);
322 
335  template <template <typename> class>
336  SparseMatrix<Number> & add_matrix (const std::string & mat_name);
337 
341  void remove_matrix(const std::string & mat_name);
342 
347  bool have_matrix (const std::string & mat_name) const;
348 
354  const SparseMatrix<Number> * request_matrix (const std::string & mat_name) const;
355 
361  SparseMatrix<Number> * request_matrix (const std::string & mat_name);
362 
371  const SparseMatrix<Number> & get_matrix (const std::string & mat_name) const;
372 
381  SparseMatrix<Number> & get_matrix (const std::string & mat_name);
382 
386  virtual unsigned int n_matrices () const override;
387 
394 
401 
402 protected:
403 
408  virtual void init_data () override;
409 
413  virtual void init_matrices ();
414 
415 private:
416 
421  void add_system_matrix ();
422 
426  std::map<std::string, SparseMatrix<Number> *> _matrices;
427 
432 };
433 
434 
435 
436 // ------------------------------------------------------------
437 // ImplicitSystem inline methods
438 inline
439 bool ImplicitSystem::have_matrix (const std::string & mat_name) const
440 {
441  return (_matrices.count(mat_name));
442 }
443 
444 
445 inline
446 unsigned int ImplicitSystem::n_matrices () const
447 {
448  return cast_int<unsigned int>(_matrices.size());
449 }
450 
451 template <template <typename> class MatrixType>
452 inline
454 ImplicitSystem::add_matrix (const std::string & mat_name)
455 {
456  // only add matrices before initializing...
457  if (!_can_add_matrices)
458  libmesh_error_msg("ERROR: Too late. Cannot add matrices to the system after initialization"
459  << "\n any more. You should have done this earlier.");
460 
461  // Return the matrix if it is already there.
462  if (this->have_matrix(mat_name))
463  return *(_matrices[mat_name]);
464 
465  // Otherwise build the matrix and return it.
466  SparseMatrix<Number> * buf = libmesh_make_unique<MatrixType<Number>>(this->comm()).release();
467  _matrices.insert (std::make_pair (mat_name, buf));
468 
469  return *buf;
470 }
471 
472 } // namespace libMesh
473 
474 #endif // LIBMESH_IMPLICIT_SYSTEM_H
libMesh::ImplicitSystem::assemble_residual_derivatives
virtual void assemble_residual_derivatives(const ParameterVector &parameters) override
Residual parameter derivative function.
Definition: implicit_system.C:643
libMesh::ImplicitSystem::request_matrix
const SparseMatrix< Number > * request_matrix(const std::string &mat_name) const
Definition: implicit_system.C:236
libMesh::ImplicitSystem
Manages consistently variables, degrees of freedom, coefficient vectors, and matrices for implicit sy...
Definition: implicit_system.h:57
libMesh::ImplicitSystem::n_matrices
virtual unsigned int n_matrices() const override
Definition: implicit_system.h:446
libMesh::ImplicitSystem::get_linear_solve_parameters
virtual std::pair< unsigned int, Real > get_linear_solve_parameters() const
Definition: implicit_system.C:1410
libMesh::ImplicitSystem::adjoint_solve
virtual std::pair< unsigned int, Real > adjoint_solve(const QoISet &qoi_indices=QoISet()) override
Assembles & solves the linear system (dR/du)^T*z = dq/du, for those quantities of interest q specifie...
Definition: implicit_system.C:359
libMesh::ImplicitSystem::reinit
virtual void reinit() override
Reinitializes the member data fields associated with the system, so that, e.g., assemble() may be use...
Definition: implicit_system.C:149
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::ImplicitSystem::add_matrix
SparseMatrix< Number > & add_matrix(const std::string &mat_name)
Adds the additional matrix mat_name to this system.
Definition: implicit_system.C:202
libMesh::ImplicitSystem::assemble
virtual void assemble() override
Prepares matrix and rhs for system assembly, then calls user assembly function.
Definition: implicit_system.C:183
libMesh::ImplicitSystem::system_type
virtual std::string system_type() const override
Definition: implicit_system.h:118
libMesh::ImplicitSystem::_matrices
std::map< std::string, SparseMatrix< Number > * > _matrices
Some systems need an arbitrary number of matrices.
Definition: implicit_system.h:426
libMesh::ParallelObject::comm
const Parallel::Communicator & comm() const
Definition: parallel_object.h:94
libMesh::ImplicitSystem::qoi_parameter_hessian_vector_product
virtual void qoi_parameter_hessian_vector_product(const QoISet &qoi_indices, const ParameterVector &parameters, const ParameterVector &vector, SensitivityData &product) override
For each of the system's quantities of interest q in qoi[qoi_indices], and for a vector of parameters...
Definition: implicit_system.C:886
libMesh::SensitivityData
Data structure for holding completed parameter sensitivity calculations.
Definition: sensitivity_data.h:46
libMesh::System::number
unsigned int number() const
Definition: system.h:2075
libMesh::ImplicitSystem::sys_type
ImplicitSystem sys_type
The type of system.
Definition: implicit_system.h:77
libMesh::ImplicitSystem::get_linear_solver
virtual LinearSolver< Number > * get_linear_solver() const
Definition: implicit_system.C:1384
libMesh::SparseMatrix< Number >
libMesh::ImplicitSystem::Parent
ExplicitSystem Parent
The type of the parent.
Definition: implicit_system.h:87
libMesh::ImplicitSystem::const_matrices_iterator
std::map< std::string, SparseMatrix< Number > * >::const_iterator const_matrices_iterator
Definition: implicit_system.h:307
libMesh::ImplicitSystem::weighted_sensitivity_solve
virtual std::pair< unsigned int, Real > weighted_sensitivity_solve(const ParameterVector &parameters, const ParameterVector &weights) override
Assembles & solves the linear system(s) (dR/du)*u_w = sum(w_p*-dR/dp), for those parameters p contain...
Definition: implicit_system.C:560
libMesh::ImplicitSystem::~ImplicitSystem
virtual ~ImplicitSystem()
Destructor.
Definition: implicit_system.C:56
libMesh::QoISet
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
Definition: qoi_set.h:45
libMesh::ImplicitSystem::clear
virtual void clear() override
Clear all the data structures associated with the system.
Definition: implicit_system.C:66
libMesh::ImplicitSystem::system
sys_type & system()
Definition: implicit_system.h:82
libMesh::ImplicitSystem::_can_add_matrices
bool _can_add_matrices
true when additional matrices may still be added, false otherwise.
Definition: implicit_system.h:431
libMesh::ImplicitSystem::assembly
virtual void assembly(bool, bool, bool=false, bool=false)
Assembles a residual in rhs and/or a jacobian in matrix, as requested.
Definition: implicit_system.h:161
libMesh::ImplicitSystem::adjoint_qoi_parameter_sensitivity
virtual void adjoint_qoi_parameter_sensitivity(const QoISet &qoi_indices, const ParameterVector &parameters, SensitivityData &sensitivities) override
Solves for the derivative of each of the system's quantities of interest q in qoi[qoi_indices] with r...
Definition: implicit_system.C:686
libMesh::ImplicitSystem::init_matrices
virtual void init_matrices()
Initializes the matrices associated with this system.
Definition: implicit_system.C:105
libMesh::ImplicitSystem::matrix
SparseMatrix< Number > * matrix
The system matrix.
Definition: implicit_system.h:393
libMesh::ParameterVector
Data structure for specifying which Parameters should be independent variables in a parameter sensiti...
Definition: parameter_vector.h:44
libMesh::ImplicitSystem::sensitivity_solve
virtual std::pair< unsigned int, Real > sensitivity_solve(const ParameterVector &parameters) override
Assembles & solves the linear system(s) (dR/du)*u_p = -dR/dp, for those parameters contained within p...
Definition: implicit_system.C:301
libMesh::ImplicitSystem::have_matrix
bool have_matrix(const std::string &mat_name) const
Definition: implicit_system.h:439
libMesh::ImplicitSystem::matrices_iterator
std::map< std::string, SparseMatrix< Number > * >::iterator matrices_iterator
Matrix iterator typedefs.
Definition: implicit_system.h:306
libMesh::ImplicitSystem::release_linear_solver
virtual void release_linear_solver(LinearSolver< Number > *) const
Releases a pointer to a linear solver acquired by this->get_linear_solver()
Definition: implicit_system.C:1418
libMesh::EquationSystems
This is the EquationSystems class.
Definition: equation_systems.h:74
libMesh::ImplicitSystem::solve
virtual void solve() override
For explicit systems, just assemble and solve the system A*x=b.
Definition: implicit_system.h:185
libMesh::ExplicitSystem
Manages consistently variables, degrees of freedom, and coefficient vectors for explicit systems.
Definition: explicit_system.h:48
libMesh::ImplicitSystem::ImplicitSystem
ImplicitSystem(EquationSystems &es, const std::string &name, const unsigned int number)
Constructor.
Definition: implicit_system.C:41
libMesh::System::name
const std::string & name() const
Definition: system.h:2067
libMesh::LinearSolver< Number >
libMesh::ImplicitSystem::remove_matrix
void remove_matrix(const std::string &mat_name)
Removes the additional matrix mat_name from this system.
Definition: implicit_system.C:221
libMesh::ImplicitSystem::forward_qoi_parameter_sensitivity
virtual void forward_qoi_parameter_sensitivity(const QoISet &qoi_indices, const ParameterVector &parameters, SensitivityData &sensitivities) override
Solves for the derivative of each of the system's quantities of interest q in qoi[qoi_indices] with r...
Definition: implicit_system.C:796
libMesh::ImplicitSystem::weighted_sensitivity_adjoint_solve
virtual std::pair< unsigned int, Real > weighted_sensitivity_adjoint_solve(const ParameterVector &parameters, const ParameterVector &weights, const QoISet &qoi_indices=QoISet()) override
Assembles & solves the linear system(s) (dR/du)^T*z_w = sum(w_p*(d^2q/dudp - d^2R/dudp*z)),...
Definition: implicit_system.C:412
libMesh::ImplicitSystem::zero_out_matrix_and_rhs
bool zero_out_matrix_and_rhs
By default, the system will zero out the matrix and the right hand side.
Definition: implicit_system.h:400
libMesh::ImplicitSystem::init_data
virtual void init_data() override
Initializes the member data fields associated with the system, so that, e.g., assemble() may be used.
Definition: implicit_system.C:90
libMesh::ImplicitSystem::get_matrix
const SparseMatrix< Number > & get_matrix(const std::string &mat_name) const
Definition: implicit_system.C:262
libMesh::ImplicitSystem::add_system_matrix
void add_system_matrix()
Add the system matrix to the _matrices data structure.
Definition: implicit_system.C:276
libMesh::ImplicitSystem::qoi_parameter_hessian
virtual void qoi_parameter_hessian(const QoISet &qoi_indices, const ParameterVector &parameters, SensitivityData &hessian) override
For each of the system's quantities of interest q in qoi[qoi_indices], and for a vector of parameters...
Definition: implicit_system.C:1091
libMesh::ImplicitSystem::disable_cache
virtual void disable_cache() override
Avoids use of any cached data that might affect any solve result.
Definition: implicit_system.C:293