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