Cantera  3.1.0b1
Loading...
Searching...
No Matches
MultiJac.cpp
Go to the documentation of this file.
1//! @file MultiJac.cpp Implementation file for class MultiJac
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
7#include <ctime>
8
9namespace Cantera
10{
11
13 : BandMatrix(r.size(),r.bandwidth(),r.bandwidth())
14{
15 m_resid = &r;
16 m_r1.resize(m_n);
17 m_ssdiag.resize(m_n);
18 m_mask.resize(m_n);
19}
20
21void MultiJac::updateTransient(double rdt, integer* mask)
22{
23 for (size_t n = 0; n < m_n; n++) {
24 value(n,n) = m_ssdiag[n] - mask[n]*rdt;
25 }
26}
27
28void MultiJac::incrementDiagonal(int j, double d)
29{
30 warn_deprecated("MultiJac::incrementDiagonal", "To be removed after Cantera 3.1.");
31 m_ssdiag[j] += d;
32 value(j,j) = m_ssdiag[j];
33}
34
35void MultiJac::eval(double* x0, double* resid0, double rdt)
36{
37 m_nevals++;
38 clock_t t0 = clock();
39 bfill(0.0);
40 size_t ipt=0;
41
42 for (size_t j = 0; j < m_resid->points(); j++) {
43 size_t nv = m_resid->nVars(j);
44 for (size_t n = 0; n < nv; n++) {
45 // perturb x(n); preserve sign(x(n))
46 double xsave = x0[ipt];
47 double dx;
48 if (xsave >= 0) {
49 dx = xsave*m_rtol + m_atol;
50 } else {
51 dx = xsave*m_rtol - m_atol;
52 }
53 x0[ipt] = xsave + dx;
54 dx = x0[ipt] - xsave;
55 double rdx = 1.0/dx;
56
57 // calculate perturbed residual
58 m_resid->eval(j, x0, m_r1.data(), rdt, 0);
59
60 // compute nth column of Jacobian
61 for (size_t i = j - 1; i != j+2; i++) {
62 if (i != npos && i < m_resid->points()) {
63 size_t mv = m_resid->nVars(i);
64 size_t iloc = m_resid->loc(i);
65 for (size_t m = 0; m < mv; m++) {
66 value(m+iloc,ipt) = (m_r1[m+iloc] - resid0[m+iloc])*rdx;
67 }
68 }
69 }
70 x0[ipt] = xsave;
71 ipt++;
72 }
73 }
74
75 for (size_t n = 0; n < m_n; n++) {
76 m_ssdiag[n] = value(n,n);
77 }
78
79 m_elapsed += double(clock() - t0)/CLOCKS_PER_SEC;
80 m_age = 0;
81}
82
83} // namespace
A class for banded matrices, involving matrix inversion processes.
Definition BandMatrix.h:37
void bfill(double v=0.0)
Fill or zero the matrix.
size_t m_n
Number of rows and columns of the matrix.
Definition BandMatrix.h:249
double & value(size_t i, size_t j)
Return a changeable reference to element (i,j).
double m_rtol
Relative tolerance for perturbing solution components.
Definition MultiJac.h:92
double m_elapsed
Elapsed CPU time taken to compute the Jacobian.
Definition MultiJac.h:97
void incrementDiagonal(int j, double d)
Definition MultiJac.cpp:28
vector< int > m_mask
Transient mask for transient terms, 1 if transient, 0 if steady-state.
Definition MultiJac.h:101
MultiJac(OneDim &r)
Constructor.
Definition MultiJac.cpp:12
void eval(double *x0, double *resid0, double rdt)
Evaluates the Jacobian at x0 using finite differences.
Definition MultiJac.cpp:35
void updateTransient(double rdt, integer *mask)
Update the transient terms in the Jacobian by using the transient mask.
Definition MultiJac.cpp:21
vector< double > m_ssdiag
Diagonal of the steady-state Jacobian.
Definition MultiJac.h:98
int m_nevals
Number of Jacobian evaluations.
Definition MultiJac.h:103
int m_age
Age of the Jacobian (times incrementAge() has been called)
Definition MultiJac.h:104
OneDim * m_resid
Residual evaluator for this Jacobian.
Definition MultiJac.h:89
vector< double > m_r1
Perturbed residual vector.
Definition MultiJac.h:91
double m_atol
Absolute tolerance for perturbing solution components.
Definition MultiJac.h:95
Container class for multiple-domain 1D problems.
Definition OneDim.h:27
size_t loc(size_t jg)
Location in the solution vector of the first component of global point jg.
Definition OneDim.h:127
void eval(size_t j, double *x, double *r, double rdt=-1.0, int count=1)
Evaluate the multi-domain residual function.
Definition OneDim.cpp:241
size_t nVars(size_t jg)
Number of solution components at global point jg.
Definition OneDim.h:122
size_t points()
Total number of points.
Definition OneDim.h:148
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
const size_t npos
index returned by functions to indicate "no position"
Definition ct_defs.h:180
void warn_deprecated(const string &source, const AnyBase &node, const string &message)
A deprecation warning for syntax in an input file.
Definition AnyMap.cpp:1997