Cantera  3.2.0a2
Loading...
Searching...
No Matches
OneDim.h
Go to the documentation of this file.
1/**
2 * @file OneDim.h
3 */
4
5// This file is part of Cantera. See License.txt in the top-level directory or
6// at https://cantera.org/license.txt for license and copyright information.
7
8#ifndef CT_ONEDIM_H
9#define CT_ONEDIM_H
10
11#include "Domain1D.h"
12#include "MultiJac.h"
15
16namespace Cantera
17{
18
19class AnyMap;
20
21/**
22 * Container class for multiple-domain 1D problems. Each domain is
23 * represented by an instance of Domain1D.
24 * @ingroup onedGroup
25 */
27{
28public:
29 //! Default constructor
30 OneDim() = default;
31
32 //! Construct a OneDim container for the domains in the list *domains*.
33 OneDim(vector<shared_ptr<Domain1D>>& domains);
34
35 //! Add a domain. Domains are added left-to-right.
36 void addDomain(shared_ptr<Domain1D> d);
37
38 //! @deprecated To be removed after Cantera 3.2. Use linearSolver() instead.
39 shared_ptr<SystemJacobian> getJacobian() {
40 warn_deprecated("OneDim::getJacobian",
41 "To be removed after Cantera 3.2. Use linearSolver() instead.");
42 return m_jac;
43 }
44
45 //! Compute the weighted norm of a step vector
46 //!
47 //! The weighted norm of a step vector @f$ \Delta x @f$ is defined as
48 //! @f[
49 //! ||\Delta x|| = \sqrt{ \frac{1}{N}
50 //! \sum_{d,n,j} \left( \frac{\Delta x_{d,n,j}}{w_{d,n}} \right)^2 }
51 //! @f]
52 //! where the error weight for solution component @f$ n @f$ in domain @f$ d @f$ is
53 //! given by
54 //! @f[
55 //! w_{d,n} = \frac{\epsilon_{{\rm r};d,n}}{J_d} \sum_j |x_{d,n,j}|
56 //! + \epsilon_{{\rm a};d,n}
57 //! @f]
58 //! Here, @f$ \epsilon_{{\rm r};d,n} @f$ is the relative error tolerance for
59 //! component @f$ n @f$ in domain @f$ d @f$, and multiplies the average magnitude of
60 //! solution component @f$ n @f$ in the domain. The second term,
61 //! @f$ \epsilon_{{\rm a};d,n} @f$, is the absolute error tolerance for component
62 //! @f$ n @f$ in domain @f$ d @f$. @f$ N @f$ is the total number of state variables
63 //! across all domains and @f$ J_d @f$ is the number of grid points in domain
64 //! @f$ d @f$.
65 double weightedNorm(const double* step) const override;
66
67 //! Return a reference to the Jacobian evaluator of an OneDim object.
68 //! @deprecated To be removed after Cantera 3.2. Superseded by linearSolver()
69 //! @ingroup derivGroup
71
72 //! Number of domains.
73 size_t nDomains() const {
74 return m_dom.size();
75 }
76
77 //! Return a reference to domain i.
78 Domain1D& domain(size_t i) const {
79 return *m_dom[i];
80 }
81
82 //! Get the index of the domain named `name`.
83 size_t domainIndex(const string& name);
84
85 //! Check that the specified domain index is in range.
86 //! Throws an exception if n is greater than nDomains()-1
87 void checkDomainIndex(size_t n) const {
88 if (n >= m_dom.size()) {
89 throw IndexError("OneDim::checkDomainIndex", "domains", n, m_dom.size());
90 }
91 }
92
93 //! Check that an array size is at least nDomains().
94 //! Throws an exception if nn is less than nDomains(). Used before calls
95 //! which take an array pointer.
96 void checkDomainArraySize(size_t nn) const {
97 if (m_dom.size() > nn) {
98 throw ArraySizeError("OneDim::checkDomainArraySize", nn,
99 m_dom.size());
100 }
101 }
102
103 //! The index of the start of domain i in the solution vector.
104 size_t start(size_t i) const {
105 if (m_dom[i]->nComponents()) {
106 return m_dom[i]->loc();
107 } else {
108 // Special case for domains with no solution components to avoid
109 // spurious out-of-bounds memory access
110 return 0;
111 }
112 }
113
114 //! Pointer to left-most domain (first added).
116 return m_dom[0].get();
117 }
118
119 //! Pointer to right-most domain (last added).
121 return m_dom.back().get();
122 }
123
124 //! Number of solution components at global point `jg`.
125 size_t nVars(size_t jg) {
126 return m_nvars[jg];
127 }
128
129 //! Location in the solution vector of the first component of global point `jg`.
130 size_t loc(size_t jg) {
131 return m_loc[jg];
132 }
133
134 //! Return the domain, local point index, and component name for the i-th
135 //! component of the global solution vector
136 std::tuple<string, size_t, string> component(size_t i) const;
137
138 string componentName(size_t i) const override;
139 pair<string, string> componentTableHeader() const override;
140 string componentTableLabel(size_t i) const override;
141 double upperBound(size_t i) const override;
142 double lowerBound(size_t i) const override;
143
144 /**
145 * Initialize all domains. On the first call, this methods calls the init
146 * method of each domain, proceeding from left to right. Subsequent calls
147 * do nothing.
148 */
149 void init();
150
151 //! Total number of points.
152 size_t points() {
153 return m_pts;
154 }
155
156 void initTimeInteg(double dt, double* x) override;
157 void setSteadyMode() override;
158
159 /**
160 * Evaluate the multi-domain residual function
161 *
162 * @param j if j != npos, only evaluate residual for points j-1, j,
163 * and j + 1; otherwise, evaluate at all grid points.
164 * @param x solution vector
165 * @param r on return, contains the residual vector
166 * @param rdt Reciprocal of the time step. if omitted, then
167 * the default value is used.
168 * @param count Set to zero to omit this call from the statistics
169 */
170 void eval(size_t j, double* x, double* r, double rdt=-1.0, int count=1);
171
172 void eval(double* x, double* r, double rdt=-1.0, int count=1) override {
173 return eval(npos, x, r, rdt, count);
174 }
175
176 void evalJacobian(double* x0) override;
177
178 //! Return a pointer to the domain global point *i* belongs to.
179 /*!
180 * The domains are scanned right-to-left, and the first one with starting
181 * location less or equal to i is returned.
182 */
183 Domain1D* pointDomain(size_t i);
184
185 void resize() override;
186 void resetBadValues(double* x) override;
187
188 //! Write statistics about the number of iterations and Jacobians at each
189 //! grid level
190 /*!
191 * @param printTime Boolean that indicates whether time should be printed
192 * out The default is true. It's turned off for test
193 * problems where we don't want to print any times
194 */
195 void writeStats(int printTime = 1);
196
197 /**
198 * Save statistics on function and Jacobian evaluation, and reset the
199 * counters. Statistics are saved only if the number of Jacobian
200 * evaluations is greater than zero. The statistics saved are:
201 *
202 * - number of grid points
203 * - number of Jacobian evaluations
204 * - CPU time spent evaluating Jacobians
205 * - number of non-Jacobian function evaluations
206 * - CPU time spent evaluating functions
207 * - number of time steps
208 */
209 void saveStats();
210
211 //! Clear saved statistics
212 void clearStats();
213
214 //! Return total grid size in each call to solve()
215 const vector<size_t>& gridSizeStats() {
216 saveStats();
217 return m_gridpts;
218 }
219
220 //! Return CPU time spent evaluating Jacobians in each call to solve()
221 const vector<double>& jacobianTimeStats() {
222 saveStats();
223 return m_jacElapsed;
224 }
225
226 //! Return CPU time spent on non-Jacobian function evaluations in each call
227 //! to solve()
228 const vector<double>& evalTimeStats() {
229 saveStats();
230 return m_funcElapsed;
231 }
232
233 //! Return number of Jacobian evaluations made in each call to solve()
234 const vector<int>& jacobianCountStats() {
235 saveStats();
236 return m_jacEvals;
237 }
238
239 //! Return number of non-Jacobian function evaluations made in each call to
240 //! solve()
241 const vector<int>& evalCountStats() {
242 saveStats();
243 return m_funcEvals;
244 }
245
246 //! Return number of time steps taken in each call to solve()
247 const vector<int>& timeStepStats() {
248 saveStats();
249 return m_timeSteps;
250 }
251
252protected:
253 //! All domains comprising the system
254 vector<shared_ptr<Domain1D>> m_dom;
255
256 //! All connector and boundary domains
257 vector<shared_ptr<Domain1D>> m_connect;
258
259 //! All bulk/flow domains
260 vector<shared_ptr<Domain1D>> m_bulk;
261
262 //! Indicates whether one-time initialization for each domain has been completed.
263 bool m_init = false;
264
265 //! Number of variables at each point, across all domains. Length points().
266 //! Accessed with nVars().
267 vector<size_t> m_nvars;
268
269 //! Location in the state vector of the first component of each point, across all
270 //! domains. Accessed with loc().
271 vector<size_t> m_loc;
272
273 //! Domain, grid point, and component indices for each element of the global state
274 //! vector. Length size()
275 vector<std::tuple<size_t, size_t, size_t>>m_componentInfo;
276
277 //! Total number of points.
278 size_t m_pts = 0;
279
280private:
281 //! @name Statistics
282 //! Solver stats are collected after successfully solving on a particular grid.
283 //! @{
284 int m_nevals = 0; //!< Number of calls to eval()
285 double m_evaltime = 0; //!< Total time [s] spent in eval()
286
287 vector<size_t> m_gridpts; //!< Number of grid points in this grid
288 vector<int> m_jacEvals; //!< Number of Jacobian evaluations on this grid
289 vector<double> m_jacElapsed; //!< Time [s] spent evaluating Jacobians on this grid
290
291 //! Number of residual function evaluations on this grid (not counting evaluations
292 //! used to construct Jacobians).
293 vector<int> m_funcEvals;
294
295 //! Time [s] spent on residual function evaluations on this grid (not counting
296 //! evaluations used to construct Jacobians).
297 vector<double> m_funcElapsed;
298
299 //! Number of time steps taken in each call to solve() (for example, for each
300 //! successive grid refinement)
301 vector<int> m_timeSteps;
302
303 //! @}
304};
305
306}
307
308#endif
Declarations for class SystemJacobian.
Array size error.
Base class for one-dimensional domains.
Definition Domain1D.h:29
An array index is out of range.
Class MultiJac evaluates the Jacobian of a system of equations defined by a residual function supplie...
Definition MultiJac.h:25
Container class for multiple-domain 1D problems.
Definition OneDim.h:27
size_t start(size_t i) const
The index of the start of domain i in the solution vector.
Definition OneDim.h:104
void init()
Initialize all domains.
Definition OneDim.cpp:354
void checkDomainIndex(size_t n) const
Check that the specified domain index is in range.
Definition OneDim.h:87
double weightedNorm(const double *step) const override
Compute the weighted norm of a step vector.
Definition OneDim.cpp:98
void resize() override
Call to set the size of internal data structures after first defining the system or if the problem si...
Definition OneDim.cpp:186
string componentName(size_t i) const override
Get the name of the i-th component of the state vector.
Definition OneDim.cpp:45
void saveStats()
Save statistics on function and Jacobian evaluation, and reset the counters.
Definition OneDim.cpp:155
pair< string, string > componentTableHeader() const override
Get header lines describing the column names included in a component label.
Definition OneDim.cpp:50
size_t loc(size_t jg)
Location in the solution vector of the first component of global point jg.
Definition OneDim.h:130
double upperBound(size_t i) const override
Get the upper bound for global component i in the state vector.
Definition OneDim.cpp:61
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:251
void addDomain(shared_ptr< Domain1D > d)
Add a domain. Domains are added left-to-right.
Definition OneDim.cpp:75
string componentTableLabel(size_t i) const override
Get elements of the component name, aligned with the column headings given by componentTableHeader().
Definition OneDim.cpp:55
size_t nDomains() const
Number of domains.
Definition OneDim.h:73
vector< double > m_jacElapsed
Time [s] spent evaluating Jacobians on this grid.
Definition OneDim.h:289
Domain1D * right()
Pointer to right-most domain (last added).
Definition OneDim.h:120
OneDim()=default
Default constructor.
void setSteadyMode() override
Prepare to solve the steady-state problem.
Definition OneDim.cpp:340
vector< double > m_funcElapsed
Time [s] spent on residual function evaluations on this grid (not counting evaluations used to constr...
Definition OneDim.h:297
const vector< int > & evalCountStats()
Return number of non-Jacobian function evaluations made in each call to solve()
Definition OneDim.h:241
vector< shared_ptr< Domain1D > > m_connect
All connector and boundary domains.
Definition OneDim.h:257
const vector< int > & jacobianCountStats()
Return number of Jacobian evaluations made in each call to solve()
Definition OneDim.h:234
void checkDomainArraySize(size_t nn) const
Check that an array size is at least nDomains().
Definition OneDim.h:96
vector< std::tuple< size_t, size_t, size_t > > m_componentInfo
Domain, grid point, and component indices for each element of the global state vector.
Definition OneDim.h:275
const vector< size_t > & gridSizeStats()
Return total grid size in each call to solve()
Definition OneDim.h:215
const vector< int > & timeStepStats()
Return number of time steps taken in each call to solve()
Definition OneDim.h:247
vector< shared_ptr< Domain1D > > m_bulk
All bulk/flow domains.
Definition OneDim.h:260
vector< int > m_funcEvals
Number of residual function evaluations on this grid (not counting evaluations used to construct Jaco...
Definition OneDim.h:293
vector< size_t > m_loc
Location in the state vector of the first component of each point, across all domains.
Definition OneDim.h:271
void evalJacobian(double *x0) override
Evaluates the Jacobian at x0 using finite differences.
Definition OneDim.cpp:283
double m_evaltime
Total time [s] spent in eval()
Definition OneDim.h:285
size_t nVars(size_t jg)
Number of solution components at global point jg.
Definition OneDim.h:125
std::tuple< string, size_t, string > component(size_t i) const
Return the domain, local point index, and component name for the i-th component of the global solutio...
Definition OneDim.cpp:39
size_t domainIndex(const string &name)
Get the index of the domain named name.
Definition OneDim.cpp:29
Domain1D * pointDomain(size_t i)
Return a pointer to the domain global point i belongs to.
Definition OneDim.cpp:239
void resetBadValues(double *x) override
Reset values such as negative species concentrations.
Definition OneDim.cpp:366
vector< size_t > m_gridpts
Number of grid points in this grid.
Definition OneDim.h:287
size_t points()
Total number of points.
Definition OneDim.h:152
vector< size_t > m_nvars
Number of variables at each point, across all domains.
Definition OneDim.h:267
int m_nevals
Number of calls to eval()
Definition OneDim.h:284
bool m_init
Indicates whether one-time initialization for each domain has been completed.
Definition OneDim.h:263
void writeStats(int printTime=1)
Write statistics about the number of iterations and Jacobians at each grid level.
Definition OneDim.cpp:138
void clearStats()
Clear saved statistics.
Definition OneDim.cpp:173
void eval(double *x, double *r, double rdt=-1.0, int count=1) override
Evaluate the residual function.
Definition OneDim.h:172
size_t m_pts
Total number of points.
Definition OneDim.h:278
vector< int > m_jacEvals
Number of Jacobian evaluations on this grid.
Definition OneDim.h:288
const vector< double > & jacobianTimeStats()
Return CPU time spent evaluating Jacobians in each call to solve()
Definition OneDim.h:221
void initTimeInteg(double dt, double *x) override
Prepare for time stepping beginning with solution x and timestep dt.
Definition OneDim.cpp:329
vector< int > m_timeSteps
Number of time steps taken in each call to solve() (for example, for each successive grid refinement)
Definition OneDim.h:301
Domain1D & domain(size_t i) const
Return a reference to domain i.
Definition OneDim.h:78
vector< shared_ptr< Domain1D > > m_dom
All domains comprising the system.
Definition OneDim.h:254
const vector< double > & evalTimeStats()
Return CPU time spent on non-Jacobian function evaluations in each call to solve()
Definition OneDim.h:228
Domain1D * left()
Pointer to left-most domain (first added).
Definition OneDim.h:115
double lowerBound(size_t i) const override
Get the lower bound for global component i in the state vector.
Definition OneDim.cpp:68
shared_ptr< SystemJacobian > getJacobian()
Definition OneDim.h:39
Base class for representing a system of differential-algebraic equations and solving for its steady-s...
double rdt() const
Reciprocal of the time step.
shared_ptr< SystemJacobian > m_jac
Jacobian evaluator.
MultiJac & jacobian()
Return a reference to the Jacobian evaluator of an OneDim object.
Definition OneDim.cpp:126
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