Cantera  3.1.0a1
AdaptivePreconditioner.h
Go to the documentation of this file.
1 /**
2  * @file AdaptivePreconditioner.h Declarations for the class
3  * AdaptivePreconditioner which is a child class of PreconditionerBase
4  * for preconditioners used by sundials
5  */
6 
7 // This file is part of Cantera. See License.txt in the top-level directory or
8 // at https://cantera.org/license.txt for license and copyright information.
9 
10 #ifndef ADAPTIVEPRECONDITIONER_H
11 #define ADAPTIVEPRECONDITIONER_H
12 
14 #include "cantera/numerics/eigen_sparse.h"
15 #include "cantera/base/global.h"
16 #include <iostream>
17 
18 namespace Cantera
19 {
20 
21 //! AdaptivePreconditioner a preconditioner designed for use with large
22 //! mechanisms that leverages sparse solvers. It does this by pruning
23 //! the preconditioner by a threshold value. It also neglects pressure
24 //! dependence and third body contributions in its formation and has a
25 //! finite difference approximation for temperature.
27 {
28 public:
30 
31  void initialize(size_t networkSize) override;
32 
33  void reset() override {
34  m_precon_matrix.setZero();
35  m_jac_trips.clear();
36  };
37 
38  void setup() override;
39 
40  void solve(const size_t stateSize, double* rhs_vector, double* output) override;
41 
42  void setValue(size_t row, size_t col, double value) override;
43 
44  void stateAdjustment(vector<double>& state) override;
45 
46  void updatePreconditioner() override;
47 
48  //! Prune preconditioner elements
49  void prunePreconditioner();
50 
51  //! Return semi-analytical Jacobian of an AdaptivePreconditioner object.
52  //! @ingroup derivGroup
53  Eigen::SparseMatrix<double> jacobian() {
54  Eigen::SparseMatrix<double> jacobian_mat(m_dim, m_dim);
55  jacobian_mat.setFromTriplets(m_jac_trips.begin(), m_jac_trips.end());
56  return jacobian_mat;
57  }
58 
59  //! Return the internal preconditioner matrix
60  Eigen::SparseMatrix<double> matrix() {
62  return m_precon_matrix;
63  }
64 
65  //! Get the threshold value for setting elements
66  double threshold() { return m_threshold; }
67 
68  //! Get ILUT fill factor
69  double ilutFillFactor() { return m_fill_factor; }
70 
71  //! Get ILUT drop tolerance
72  double ilutDropTol() { return m_drop_tol; }
73 
74  //! Set the threshold value to compare elements against
75  //! @param threshold double value used in setting by threshold
76  void setThreshold(double threshold) {
78  m_prune_precon = (threshold <= 0) ? false : true;
79  }
80 
81  //! Set drop tolerance for ILUT
82  //! @param droptol double value used in setting solver drop tolerance
83  void setIlutDropTol(double droptol) {
84  m_drop_tol = droptol;
85  m_solver.setDroptol(droptol);
86  }
87 
88  //! Set the fill factor for ILUT solver
89  //! @param fillFactor fill in factor for ILUT solver
90  void setIlutFillFactor(int fillFactor) {
91  m_fill_factor = fillFactor;
92  m_solver.setFillfactor(fillFactor);
93  }
94 
95  //! Print preconditioner contents
96  void printPreconditioner() override;
97 
98  //! Print jacobian contents
99  void printJacobian();
100 
101 protected:
102  //! ILUT fill factor
103  double m_fill_factor = 0;
104 
105  //! ILUT drop tolerance
106  double m_drop_tol = 0;
107 
108  //! Vector of triples representing the jacobian used in preconditioning
109  vector<Eigen::Triplet<double>> m_jac_trips;
110 
111  //! Storage of appropriately sized identity matrix for making the preconditioner
112  Eigen::SparseMatrix<double> m_identity;
113 
114  //! Container that is the sparse preconditioner
115  Eigen::SparseMatrix<double> m_precon_matrix;
116 
117  //! Solver used in solving the linear system
118  Eigen::IncompleteLUT<double> m_solver;
119 
120  //! Minimum value a non-diagonal element must be to be included in
121  //! the preconditioner
122  double m_threshold = 0.0;
123 
124  //! Bool set whether to prune the matrix or not
125  double m_prune_precon = true;
126 };
127 
128 }
129 
130 #endif
Declarations for the class PreconditionerBase which is a virtual base class for preconditioning syste...
AdaptivePreconditioner a preconditioner designed for use with large mechanisms that leverages sparse ...
double threshold()
Get the threshold value for setting elements.
Eigen::IncompleteLUT< double > m_solver
Solver used in solving the linear system.
double ilutDropTol()
Get ILUT drop tolerance.
Eigen::SparseMatrix< double > matrix()
Return the internal preconditioner matrix.
void setIlutDropTol(double droptol)
Set drop tolerance for ILUT.
double m_drop_tol
ILUT drop tolerance.
void solve(const size_t stateSize, double *rhs_vector, double *output) override
Solve a linear system Ax=b where A is the preconditioner.
void setValue(size_t row, size_t col, double value) override
Set a value at the specified row and column of the jacobian triplet vector.
double m_prune_precon
Bool set whether to prune the matrix or not.
double m_threshold
Minimum value a non-diagonal element must be to be included in the preconditioner.
void initialize(size_t networkSize) override
Called during setup for any processes that need to be completed prior to setup functions used in sund...
vector< Eigen::Triplet< double > > m_jac_trips
Vector of triples representing the jacobian used in preconditioning.
void setIlutFillFactor(int fillFactor)
Set the fill factor for ILUT solver.
Eigen::SparseMatrix< double > m_identity
Storage of appropriately sized identity matrix for making the preconditioner.
void printPreconditioner() override
Print preconditioner contents.
void setup() override
Perform preconditioner specific post-reactor setup operations such as factorize.
void stateAdjustment(vector< double > &state) override
Adjust the state vector based on the preconditioner, e.g., Adaptive preconditioning uses a strictly p...
void updatePreconditioner() override
Transform Jacobian vector and write into preconditioner, P = (I - gamma * J)
void prunePreconditioner()
Prune preconditioner elements.
void printJacobian()
Print jacobian contents.
Eigen::SparseMatrix< double > m_precon_matrix
Container that is the sparse preconditioner.
void setThreshold(double threshold)
Set the threshold value to compare elements against.
void reset() override
Reset preconditioner parameters as needed.
double ilutFillFactor()
Get ILUT fill factor.
PreconditionerBase serves as an abstract type to extend different preconditioners.
size_t m_dim
Dimension of the preconditioner.
This file contains definitions for utility functions and text for modules, inputfiles and logging,...
Eigen::SparseMatrix< double > jacobian()
Return semi-analytical Jacobian of an AdaptivePreconditioner object.
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:564