Cantera  4.0.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(span<const 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(span<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, span<const 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, span<const double> x, span<double> r,
158 double rdt=-1.0, int count=1);
159
160 void eval(span<const double> x, span<double> r,
161 double rdt=-1.0, int count=1) override
162 {
163 return eval(npos, x, r, rdt, count);
164 }
165
166 void evalJacobian(span<const double> x0) override;
167
168 //! Return a pointer to the domain global point *i* belongs to.
169 /*!
170 * The domains are scanned right-to-left, and the first one with starting
171 * location less or equal to i is returned.
172 */
173 Domain1D* pointDomain(size_t i);
174
175 void resize() override;
176 void resetBadValues(span<double> x) override;
177
178 //! Write statistics about the number of iterations and Jacobians at each
179 //! grid level
180 /*!
181 * @param printTime Boolean that indicates whether time should be printed
182 * out The default is true. It's turned off for test
183 * problems where we don't want to print any times
184 */
185 void writeStats(int printTime = 1);
186
187 /**
188 * Save statistics on function and Jacobian evaluation, and reset the
189 * counters. Statistics are saved only if the number of Jacobian
190 * evaluations is greater than zero. The statistics saved are:
191 *
192 * - number of grid points
193 * - number of Jacobian evaluations
194 * - CPU time spent evaluating Jacobians
195 * - number of non-Jacobian function evaluations
196 * - CPU time spent evaluating functions
197 * - number of time steps
198 */
199 void saveStats();
200
201 //! Clear saved statistics
202 void clearStats();
203
204 //! Return total grid size in each call to solve()
205 const vector<size_t>& gridSizeStats() {
206 saveStats();
207 return m_gridpts;
208 }
209
210 //! Return CPU time spent evaluating Jacobians in each call to solve()
211 const vector<double>& jacobianTimeStats() {
212 saveStats();
213 return m_jacElapsed;
214 }
215
216 //! Return CPU time spent on non-Jacobian function evaluations in each call
217 //! to solve()
218 const vector<double>& evalTimeStats() {
219 saveStats();
220 return m_funcElapsed;
221 }
222
223 //! Return number of Jacobian evaluations made in each call to solve()
224 const vector<int>& jacobianCountStats() {
225 saveStats();
226 return m_jacEvals;
227 }
228
229 //! Return number of non-Jacobian function evaluations made in each call to
230 //! solve()
231 const vector<int>& evalCountStats() {
232 saveStats();
233 return m_funcEvals;
234 }
235
236 //! Return number of time steps taken in each call to solve()
237 const vector<int>& timeStepStats() {
238 saveStats();
239 return m_timeSteps;
240 }
241
242 //! Access internal work array.
243 const vector<double>& _workVector() const {
244 return m_xnew;
245 }
246
247protected:
248 //! All domains comprising the system
249 vector<shared_ptr<Domain1D>> m_dom;
250
251 //! All connector and boundary domains
252 vector<shared_ptr<Domain1D>> m_connect;
253
254 //! All bulk/flow domains
255 vector<shared_ptr<Domain1D>> m_bulk;
256
257 //! Indicates whether one-time initialization for each domain has been completed.
258 bool m_init = false;
259
260 //! Number of variables at each point, across all domains. Length points().
261 //! Accessed with nVars().
262 vector<size_t> m_nvars;
263
264 //! Location in the state vector of the first component of each point, across all
265 //! domains. Accessed with loc().
266 vector<size_t> m_loc;
267
268 //! Domain, grid point, and component indices for each element of the global state
269 //! vector. Length size()
270 vector<std::tuple<size_t, size_t, size_t>>m_componentInfo;
271
272 //! Total number of points.
273 size_t m_pts = 0;
274
275private:
276 //! @name Statistics
277 //! Solver stats are collected after successfully solving on a particular grid.
278 //! @{
279 int m_nevals = 0; //!< Number of calls to eval()
280 double m_evaltime = 0; //!< Total time [s] spent in eval()
281
282 vector<size_t> m_gridpts; //!< Number of grid points in this grid
283 vector<int> m_jacEvals; //!< Number of Jacobian evaluations on this grid
284 vector<double> m_jacElapsed; //!< Time [s] spent evaluating Jacobians on this grid
285
286 //! Number of residual function evaluations on this grid (not counting evaluations
287 //! used to construct Jacobians).
288 vector<int> m_funcEvals;
289
290 //! Time [s] spent on residual function evaluations on this grid (not counting
291 //! evaluations used to construct Jacobians).
292 vector<double> m_funcElapsed;
293
294 //! Number of time steps taken in each call to solve() (for example, for each
295 //! successive grid refinement)
296 vector<int> m_timeSteps;
297
298 //! @}
299};
300
301}
302
303#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:350
void checkDomainIndex(size_t n) const
Check that the specified domain index is in range.
Definition OneDim.h:74
double weightedNorm(span< 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
void initTimeInteg(double dt, span< const double > x) override
Prepare for time stepping beginning with solution x and timestep dt.
Definition OneDim.cpp:325
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 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:284
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:243
OneDim()=default
Default constructor.
void setSteadyMode() override
Prepare to solve the steady-state problem.
Definition OneDim.cpp:336
vector< double > m_funcElapsed
Time [s] spent on residual function evaluations on this grid (not counting evaluations used to constr...
Definition OneDim.h:292
const vector< int > & evalCountStats()
Return number of non-Jacobian function evaluations made in each call to solve()
Definition OneDim.h:231
vector< shared_ptr< Domain1D > > m_connect
All connector and boundary domains.
Definition OneDim.h:252
const vector< int > & jacobianCountStats()
Return number of Jacobian evaluations made in each call to solve()
Definition OneDim.h:224
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:270
const vector< size_t > & gridSizeStats()
Return total grid size in each call to solve()
Definition OneDim.h:205
const vector< int > & timeStepStats()
Return number of time steps taken in each call to solve()
Definition OneDim.h:237
vector< shared_ptr< Domain1D > > m_bulk
All bulk/flow domains.
Definition OneDim.h:255
vector< int > m_funcEvals
Number of residual function evaluations on this grid (not counting evaluations used to construct Jaco...
Definition OneDim.h:288
vector< size_t > m_loc
Location in the state vector of the first component of each point, across all domains.
Definition OneDim.h:266
double m_evaltime
Total time [s] spent in eval()
Definition OneDim.h:280
size_t nVars(size_t jg)
Number of solution components at global point jg.
Definition OneDim.h:112
void evalJacobian(span< const double > x0) override
Evaluates the Jacobian at x0 using finite differences.
Definition OneDim.cpp:278
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:234
vector< size_t > m_gridpts
Number of grid points in this grid.
Definition OneDim.h:282
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:262
int m_nevals
Number of calls to eval()
Definition OneDim.h:279
bool m_init
Indicates whether one-time initialization for each domain has been completed.
Definition OneDim.h:258
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
size_t m_pts
Total number of points.
Definition OneDim.h:273
void resetBadValues(span< double > x) override
Reset values such as negative species concentrations.
Definition OneDim.cpp:362
vector< int > m_jacEvals
Number of Jacobian evaluations on this grid.
Definition OneDim.h:283
const vector< double > & jacobianTimeStats()
Return CPU time spent evaluating Jacobians in each call to solve()
Definition OneDim.h:211
vector< int > m_timeSteps
Number of time steps taken in each call to solve() (for example, for each successive grid refinement)
Definition OneDim.h:296
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:249
const vector< double > & evalTimeStats()
Return CPU time spent on non-Jacobian function evaluations in each call to solve()
Definition OneDim.h:218
Domain1D * left()
Pointer to left-most domain (first added).
Definition OneDim.h:102
void eval(span< const double > x, span< double > r, double rdt=-1.0, int count=1) override
Evaluate the residual function.
Definition OneDim.h:160
double lowerBound(size_t i) const override
Get the lower bound for global component i in the state vector.
Definition OneDim.cpp:71
void eval(size_t j, span< const double > x, span< double > r, double rdt=-1.0, int count=1)
Evaluate the multi-domain residual function.
Definition OneDim.cpp:246
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:183