Cantera  3.3.0a1
Loading...
Searching...
No Matches
SystemJacobian.h
Go to the documentation of this file.
1//! @file SystemJacobian.h Declarations for class SystemJacobian
2
3// This file is part of Cantera. See License.txt in the top-level directory or
4// at https://cantera.org/license.txt for license and copyright information.
5
6#ifndef SYSTEMJACOBIAN_H
7#define SYSTEMJACOBIAN_H
8
10#include "cantera/base/global.h"
11
12namespace Cantera
13{
14
15/**
16 * Specifies the side of the system on which the preconditioner is applied. Not all
17 * methods are supported by all integrators.
18 */
20 NO_PRECONDITION, //! No preconditioning
21 LEFT_PRECONDITION, //! Left side preconditioning
22 RIGHT_PRECONDITION, //! Right side preconditioning
23 BOTH_PRECONDITION //! Left and right side preconditioning
24};
25
26//! Abstract base class representing Jacobian matrices and preconditioners used in
27//! nonlinear solvers.
28//!
29//! The matrix is initially populated with elements of the (steady-state) Jacobian
30//! using the setValue() method.
31//!
32//! If the object is being used for preconditioning, the preconditioner matrix
33//! @f$ M = I - \gamma J @f$ is computed and factorized by calling
34//! updatePreconditioner(). If the object is being used for solving a (pseudo-)transient
35//! problem, the transient Jacobian @f$ M = J - \alpha \Delta t^{-1} @f$ is computed and
36//! factorized by calling the updateTransient() method.
37//!
38//! Once the system is factorized, the solve() method can be used for preconditioning
39//! or solving Newton steps as appropriate.
40//!
41//! Implementations of this class provide different options for how to store the
42//! matrix elements and different algorithms for factorizing the matrix.
44{
45public:
47
48 virtual ~SystemJacobian() {}
49
50 //! Derived type, corresponding to names registered with SystemJacobianFactory
51 virtual const string type() const = 0;
52
53 //! Set a value at the specified row and column of the jacobian triplet vector
54 //! @param row row in the jacobian matrix
55 //! @param col column in the jacobian matrix
56 //! @param value value of the element at row and col
57 virtual void setValue(size_t row, size_t col, double value) {
58 throw NotImplementedError("SystemJacobian::setValue");
59 }
60
61 //! Adjust the state vector based on the preconditioner, e.g., Adaptive
62 //! preconditioning uses a strictly positive composition when preconditioning which
63 //! is handled by this function
64 //! @param state a vector containing the state to be updated
65 virtual void stateAdjustment(vector<double>& state) {
66 throw NotImplementedError("SystemJacobian::stateAdjustment");
67 }
68
69 //! Get preconditioner application side for CVODES
70 string preconditionerSide() const {
71 return m_precon_side;
72 }
73
74 //! For iterative solvers, set the side where the preconditioner is applied
75 virtual void setPreconditionerSide(const string& preconSide) {
76 m_precon_side = preconSide;
77 }
78
79 //! Transform Jacobian vector and write into
80 //! preconditioner, P = (I - gamma * J)
81 virtual void updatePreconditioner() {
82 throw NotImplementedError("SystemJacobian::updatePreconditioner");
83 }
84
85 //! Update the diagonal terms in the Jacobian by using the transient mask
86 //! @f$ \alpha @f$.
87 //! @param rdt Reciprocal of the time step [1/s]
88 //! @param mask Mask for transient terms: 1 if transient, 0 if algebraic.
89 virtual void updateTransient(double rdt, int* mask) {
90 throw NotImplementedError("SystemJacobian::updateTransient");
91 }
92
93 //! Solve a linear system using the system matrix *M*
94 //!
95 //! The matrix *M* can either be the preconditioner or the transient Jacobian
96 //! matrix, depending on whether updateTransient() or updatePreconditioner() was
97 //! called last.
98 //!
99 //! @param[in] stateSize length of the rhs and output vectors
100 //! @param[in] rhs_vector right hand side vector used in linear system
101 //! @param[out] output output vector for solution
102 virtual void solve(const size_t stateSize, double* rhs_vector, double* output) {
103 throw NotImplementedError("SystemJacobian::solve");
104 };
105
106 //! Reset parameters as needed
107 virtual void reset() {
108 throw NotImplementedError("SystemJacobian::reset");
109 };
110
111 //! Called during setup for any processes that need
112 //! to be completed prior to setup functions used in sundials.
113 //! @param networkSize the number of variables in the associated reactor network
114 virtual void initialize(size_t networkSize) {
115 throw NotImplementedError("SystemJacobian::initialize");
116 };
117
118 //! Used to provide system bandwidth for implementations that use banded matrix
119 //! storage. Ignored if not needed.
120 virtual void setBandwidth(size_t bw) {}
121
122 //! Print preconditioner contents
123 virtual void printPreconditioner() {
124 throw NotImplementedError("SystemJacobian::printPreconditioner");
125 };
126
127 //! Set gamma used in preconditioning
128 //! @param gamma used in M = I - gamma*J
129 virtual void setGamma(double gamma) {
130 m_gamma = gamma;
131 };
132
133 //! Get gamma used in preconditioning
134 virtual double gamma() {
135 return m_gamma;
136 };
137
138 //! Set the absolute tolerance in the solver outside of the network initialization
139 //! @param atol the specified tolerance
140 virtual void setAbsoluteTolerance(double atol) {
141 m_atol = atol;
142 }
143
144 //! Get latest status of linear solver. Zero indicates success. Meaning of non-zero
145 //! values is solver dependent.
146 virtual int info() const {
147 throw NotImplementedError("SystemJacobian::info");
148 }
149
150 //! Elapsed CPU time spent computing the Jacobian elements.
151 double elapsedTime() const {
152 return m_elapsed;
153 }
154
155 //! Increase the elapsed time by the specified time.
156 void updateElapsed(double evalTime) {
157 m_elapsed += evalTime;
158 }
159
160 //! Number of Jacobian evaluations.
161 int nEvals() const {
162 return m_nevals;
163 }
164 //! Increment the number of times the Jacobian has been evaluated
166 m_nevals++;
167 }
168
169 //! Number of times 'incrementAge' has been called since the last evaluation
170 int age() const {
171 return m_age;
172 }
173
174 //! Increment the Jacobian age.
176 m_age++;
177 }
178
179 //! Set the Jacobian age.
180 void setAge(int age) {
181 m_age = age;
182 }
183
184 //! Clear collected stats about number of Jacobian evaluations, CPU time spent on
185 //! Jacobian updates, and the number of Newton steps that have been taken since
186 //! updating the Jacobian.
187 void clearStats() {
188 m_elapsed = 0.0;
189 m_nevals = 0;
190 m_age = 100000;
191 }
192
193protected:
194 //! Factorize the system matrix. This method should be called at the end of
195 //! implementations of updatePreconditioner() and updateTransient().
196 virtual void factorize() {
197 throw NotImplementedError("SystemJacobian::factorize");
198 }
199
200 //! Dimension of the system
201 size_t m_dim;
202
203 //! gamma value used in M = I - gamma*J
204 double m_gamma = 1.0;
205
206 //! bool saying whether or not the system is initialized
207 bool m_init = false;
208
209 //! Absolute tolerance of the ODE solver
210 double m_atol = 0;
211
212 double m_elapsed = 0.0; //!< Elapsed CPU time taken to compute the Jacobian
213 int m_nevals = 0; //!< Number of Jacobian evaluations.
214
215 int m_age = 100000; //!< Age of the Jacobian (times incrementAge() has been called)
216
217 //! For iterative solvers, side of the system to apply the preconditioner on
218 string m_precon_side = "none";
219};
220
221}
222#endif
An error indicating that an unimplemented function has been called.
Abstract base class representing Jacobian matrices and preconditioners used in nonlinear solvers.
virtual double gamma()
Get gamma used in preconditioning.
int nEvals() const
Number of Jacobian evaluations.
void setAge(int age)
Set the Jacobian age.
virtual void setGamma(double gamma)
Set gamma used in preconditioning.
size_t m_dim
Dimension of the system.
void incrementEvals()
Increment the number of times the Jacobian has been evaluated.
string preconditionerSide() const
Get preconditioner application side for CVODES.
double m_elapsed
Elapsed CPU time taken to compute the Jacobian.
double m_gamma
gamma value used in M = I - gamma*J
virtual int info() const
Get latest status of linear solver.
virtual void setPreconditionerSide(const string &preconSide)
For iterative solvers, set the side where the preconditioner is applied.
virtual void printPreconditioner()
Print preconditioner contents.
virtual void factorize()
Factorize the system matrix.
virtual void solve(const size_t stateSize, double *rhs_vector, double *output)
Solve a linear system using the system matrix M
virtual void reset()
Reset parameters as needed.
string m_precon_side
For iterative solvers, side of the system to apply the preconditioner on.
virtual void setBandwidth(size_t bw)
Used to provide system bandwidth for implementations that use banded matrix storage.
void incrementAge()
Increment the Jacobian age.
virtual void stateAdjustment(vector< double > &state)
Adjust the state vector based on the preconditioner, e.g., Adaptive preconditioning uses a strictly p...
void updateElapsed(double evalTime)
Increase the elapsed time by the specified time.
int age() const
Number of times 'incrementAge' has been called since the last evaluation.
virtual const string type() const =0
Derived type, corresponding to names registered with SystemJacobianFactory.
int m_nevals
Number of Jacobian evaluations.
bool m_init
bool saying whether or not the system is initialized
int m_age
Age of the Jacobian (times incrementAge() has been called)
void clearStats()
Clear collected stats about number of Jacobian evaluations, CPU time spent on Jacobian updates,...
virtual void initialize(size_t networkSize)
Called during setup for any processes that need to be completed prior to setup functions used in sund...
double m_atol
Absolute tolerance of the ODE solver.
virtual void setAbsoluteTolerance(double atol)
Set the absolute tolerance in the solver outside of the network initialization.
virtual void setValue(size_t row, size_t col, double value)
Set a value at the specified row and column of the jacobian triplet vector.
double elapsedTime() const
Elapsed CPU time spent computing the Jacobian elements.
virtual void updatePreconditioner()
Transform Jacobian vector and write into preconditioner, P = (I - gamma * J)
virtual void updateTransient(double rdt, int *mask)
Update the diagonal terms in the Jacobian by using the transient mask .
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
This file contains definitions for utility functions and text for modules, inputfiles and logging,...
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
PreconditionerSide
Specifies the side of the system on which the preconditioner is applied.
@ LEFT_PRECONDITION
No preconditioning.
@ BOTH_PRECONDITION
Right side preconditioning.
@ RIGHT_PRECONDITION
Left side preconditioning.