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