Cantera  3.0.0
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"
13
14namespace Cantera
15{
16
17class Func1;
18class MultiNewton;
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 */
26class OneDim
27{
28public:
29 OneDim();
30
31 //! Construct a OneDim container for the domains in the list *domains*.
32 OneDim(vector<shared_ptr<Domain1D>>& domains);
33
34 //! @deprecated To be removed after %Cantera 3.0;
35 //! superseded by OneDim() using vector<shared_ptr<Domain1D>>
36 OneDim(vector<Domain1D*> domains);
37 virtual ~OneDim();
38 OneDim(const OneDim&) = delete;
39 OneDim& operator=(const OneDim&) = delete;
40
41 //! Add a domain. Domains are added left-to-right.
42 void addDomain(shared_ptr<Domain1D> d);
43
44 //! @deprecated To be removed after %Cantera 3.0;
45 //! superseded by addDomain() using shared_ptr<Domain1D>
46 void addDomain(Domain1D* d);
47
48 //! Return a reference to the Jacobian evaluator of an OneDim object.
49 //! @ingroup derivGroup
51
52 //! Return a reference to the Newton iterator.
54
55 /**
56 * Solve F(x) = 0, where F(x) is the multi-domain residual function.
57 * @param x0 Starting estimate of solution.
58 * @param x1 Final solution satisfying F(x1) = 0.
59 * @param loglevel Controls amount of diagnostic output.
60 */
61 int solve(double* x0, double* x1, int loglevel);
62
63 //! Number of domains.
64 size_t nDomains() const {
65 return m_dom.size();
66 }
67
68 //! Return a reference to domain i.
69 Domain1D& domain(size_t i) const {
70 return *m_dom[i];
71 }
72
73 size_t domainIndex(const string& name);
74
75 //! Check that the specified domain index is in range.
76 //! Throws an exception if n is greater than nDomains()-1
77 void checkDomainIndex(size_t n) const {
78 if (n >= m_dom.size()) {
79 throw IndexError("OneDim::checkDomainIndex", "domains", n,
80 m_dom.size()-1);
81 }
82 }
83
84 //! Check that an array size is at least nDomains().
85 //! Throws an exception if nn is less than nDomains(). Used before calls
86 //! which take an array pointer.
87 void checkDomainArraySize(size_t nn) const {
88 if (m_dom.size() > nn) {
89 throw ArraySizeError("OneDim::checkDomainArraySize", nn,
90 m_dom.size());
91 }
92 }
93
94 //! The index of the start of domain i in the solution vector.
95 size_t start(size_t i) const {
96 if (m_dom[i]->nComponents()) {
97 return m_dom[i]->loc();
98 } else {
99 // Special case for domains with no solution components to avoid
100 // spurious out-of-bounds memory access
101 return 0;
102 }
103 }
104
105 //! Total solution vector length;
106 size_t size() const {
107 return m_size;
108 }
109
110 //! Pointer to left-most domain (first added).
112 return m_dom[0];
113 }
114
115 //! Pointer to right-most domain (last added).
117 return m_dom.back();
118 }
119
120 //! Number of solution components at global point jg.
121 size_t nVars(size_t jg) {
122 return m_nvars[jg];
123 }
124
125 //! Location in the solution vector of the first component of global point
126 //! jg.
127 size_t loc(size_t jg) {
128 return m_loc[jg];
129 }
130
131 //! Return the domain, local point index, and component name for the i-th
132 //! component of the global solution vector
133 std::tuple<string, size_t, string> component(size_t i);
134
135 //! Jacobian bandwidth.
136 size_t bandwidth() const {
137 return m_bw;
138 }
139
140 /**
141 * Initialize all domains. On the first call, this methods calls the init
142 * method of each domain, proceeding from left to right. Subsequent calls
143 * do nothing.
144 */
145 void init();
146
147 //! Total number of points.
148 size_t points() {
149 return m_pts;
150 }
151
152 /**
153 * Steady-state max norm (infinity norm) of the residual evaluated using
154 * solution x. On return, array r contains the steady-state residual
155 * values. Used only for diagnostic output.
156 */
157 double ssnorm(double* x, double* r);
158
159 //! Reciprocal of the time step.
160 double rdt() const {
161 return m_rdt;
162 }
163
164 //! Prepare for time stepping beginning with solution *x* and timestep *dt*.
165 void initTimeInteg(double dt, double* x);
166
167 //! True if transient mode.
168 bool transient() const {
169 return (m_rdt != 0.0);
170 }
171
172 //! True if steady mode.
173 bool steady() const {
174 return (m_rdt == 0.0);
175 }
176
177 /**
178 * Prepare to solve the steady-state problem. After invoking this method,
179 * subsequent calls to solve() will solve the steady-state problem. Sets
180 * the reciprocal of the time step to zero, and, if it was previously non-
181 * zero, signals that a new Jacobian will be needed.
182 */
183 void setSteadyMode();
184
185 /**
186 * Evaluate the multi-domain residual function
187 *
188 * @param j if j != npos, only evaluate residual for points j-1, j,
189 * and j + 1; otherwise, evaluate at all grid points.
190 * @param x solution vector
191 * @param r on return, contains the residual vector
192 * @param rdt Reciprocal of the time step. if omitted, then
193 * the default value is used.
194 * @param count Set to zero to omit this call from the statistics
195 */
196 void eval(size_t j, double* x, double* r, double rdt=-1.0, int count = 1);
197
198 //! Return a pointer to the domain global point *i* belongs to.
199 /*!
200 * The domains are scanned right-to-left, and the first one with starting
201 * location less or equal to i is returned.
202 */
203 Domain1D* pointDomain(size_t i);
204
205 //! Call after one or more grids has changed size, for example after being refined.
206 virtual void resize();
207
208 vector<int>& transientMask() {
209 return m_mask;
210 }
211
212 /**
213 * Take time steps using Backward Euler.
214 *
215 * @param nsteps number of steps
216 * @param dt initial step size
217 * @param x current solution vector
218 * @param r solution vector after time stepping
219 * @param loglevel controls amount of printed diagnostics
220 * @returns size of last timestep taken
221 */
222 double timeStep(int nsteps, double dt, double* x, double* r, int loglevel);
223
224 void resetBadValues(double* x);
225
226 //! Write statistics about the number of iterations and Jacobians at each
227 //! grid level
228 /*!
229 * @param printTime Boolean that indicates whether time should be printed
230 * out The default is true. It's turned off for test
231 * problems where we don't want to print any times
232 */
233 void writeStats(int printTime = 1);
234
235 //! @deprecated To be removed after %Cantera 3.0; unused.
236 AnyMap serialize(const double* soln) const;
237
238 // options
239 void setMinTimeStep(double tmin) {
240 m_tmin = tmin;
241 }
242 void setMaxTimeStep(double tmax) {
243 m_tmax = tmax;
244 }
245 void setTimeStepFactor(double tfactor) {
246 m_tfactor = tfactor;
247 }
248
249 //! Set the maximum number of timeteps allowed before successful
250 //! steady-state solve
251 void setMaxTimeStepCount(int nmax) {
252 m_nsteps_max = nmax;
253 }
254
255 //! Return the maximum number of timeteps allowed before successful
256 //! steady-state solve
257 int maxTimeStepCount() const {
258 return m_nsteps_max;
259 }
260
261 void setJacAge(int ss_age, int ts_age=-1);
262
263 /**
264 * Save statistics on function and Jacobian evaluation, and reset the
265 * counters. Statistics are saved only if the number of Jacobian
266 * evaluations is greater than zero. The statistics saved are:
267 *
268 * - number of grid points
269 * - number of Jacobian evaluations
270 * - CPU time spent evaluating Jacobians
271 * - number of non-Jacobian function evaluations
272 * - CPU time spent evaluating functions
273 * - number of time steps
274 */
275 void saveStats();
276
277 //! Clear saved statistics
278 void clearStats();
279
280 //! Return total grid size in each call to solve()
281 const vector<size_t>& gridSizeStats() {
282 saveStats();
283 return m_gridpts;
284 }
285
286 //! Return CPU time spent evaluating Jacobians in each call to solve()
287 const vector<double>& jacobianTimeStats() {
288 saveStats();
289 return m_jacElapsed;
290 }
291
292 //! Return CPU time spent on non-Jacobian function evaluations in each call
293 //! to solve()
294 const vector<double>& evalTimeStats() {
295 saveStats();
296 return m_funcElapsed;
297 }
298
299 //! Return number of Jacobian evaluations made in each call to solve()
300 const vector<int>& jacobianCountStats() {
301 saveStats();
302 return m_jacEvals;
303 }
304
305 //! Return number of non-Jacobian function evaluations made in each call to
306 //! solve()
307 const vector<int>& evalCountStats() {
308 saveStats();
309 return m_funcEvals;
310 }
311
312 //! Return number of time steps taken in each call to solve()
313 const vector<int>& timeStepStats() {
314 saveStats();
315 return m_timeSteps;
316 }
317
318 //! Set a function that will be called every time #eval is called.
319 //! Can be used to provide keyboard interrupt support in the high-level
320 //! language interfaces.
321 void setInterrupt(Func1* interrupt) {
322 m_interrupt = interrupt;
323 }
324
325 //! Set a function that will be called after each successful timestep. The
326 //! function will be called with the size of the timestep as the argument.
327 //! Intended to be used for observing solver progress for debugging
328 //! purposes.
329 void setTimeStepCallback(Func1* callback) {
330 m_time_step_callback = callback;
331 }
332
333protected:
334 void evalSSJacobian(double* x, double* xnew);
335
336 double m_tmin = 1e-16; //!< minimum timestep size
337 double m_tmax = 1e+08; //!< maximum timestep size
338
339 //! factor time step is multiplied by if time stepping fails ( < 1 )
340 double m_tfactor = 0.5;
341
342 shared_ptr<vector<double>> m_state; //!< Solution vector
343
344 unique_ptr<MultiJac> m_jac; //!< Jacobian evaluator
345 unique_ptr<MultiNewton> m_newt; //!< Newton iterator
346 double m_rdt = 0.0; //!< reciprocal of time step
347 bool m_jac_ok = false; //!< if true, Jacobian is current
348
349 size_t m_bw = 0; //!< Jacobian bandwidth
350 size_t m_size = 0; //!< solution vector size
351
352 vector<shared_ptr<Domain1D>> m_sharedDom;
353 vector<shared_ptr<Domain1D>> m_sharedConnect;
354 vector<shared_ptr<Domain1D>> m_sharedBulk;
355
356 vector<Domain1D*> m_dom; //!< @todo remove raw pointers after %Cantera 3.0
357 vector<Domain1D*> m_connect; //!< @todo remove raw pointers after %Cantera 3.0
358 vector<Domain1D*> m_bulk; //!< @todo remove raw pointers after %Cantera 3.0
359
360 bool m_init = false;
361 vector<size_t> m_nvars;
362 vector<size_t> m_loc;
363 vector<int> m_mask;
364 size_t m_pts = 0;
365
366 // options
367 int m_ss_jac_age = 20;
368 int m_ts_jac_age = 20;
369
370 //! Function called at the start of every call to #eval.
371 Func1* m_interrupt = nullptr;
372
373 //! User-supplied function called after each successful timestep.
375
376 //! Number of time steps taken in the current call to solve()
377 int m_nsteps = 0;
378
379 //! Maximum number of timesteps allowed per call to solve()
380 int m_nsteps_max = 500;
381
382private:
383 // statistics
384 int m_nevals = 0;
385 double m_evaltime = 0;
386 vector<size_t> m_gridpts;
387 vector<int> m_jacEvals;
388 vector<double> m_jacElapsed;
389 vector<int> m_funcEvals;
390 vector<double> m_funcElapsed;
391
392 //! Number of time steps taken in each call to solve() (for example, for each
393 //! successive grid refinement)
394 vector<int> m_timeSteps;
395};
396
397}
398
399#endif
Array size error.
Base class for one-dimensional domains.
Definition Domain1D.h:41
Base class for 'functor' classes that evaluate a function of one variable.
Definition Func1.h:96
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:24
Newton iterator for multi-domain, one-dimensional problems.
Definition MultiNewton.h:24
Container class for multiple-domain 1D problems.
Definition OneDim.h:27
int solve(double *x0, double *x1, int loglevel)
Solve F(x) = 0, where F(x) is the multi-domain residual function.
Definition OneDim.cpp:258
size_t start(size_t i) const
The index of the start of domain i in the solution vector.
Definition OneDim.h:95
int m_nsteps
Number of time steps taken in the current call to solve()
Definition OneDim.h:377
void init()
Initialize all domains.
Definition OneDim.cpp:370
vector< Domain1D * > m_bulk
Definition OneDim.h:358
void checkDomainIndex(size_t n) const
Check that the specified domain index is in range.
Definition OneDim.h:77
size_t m_size
solution vector size
Definition OneDim.h:350
int m_nsteps_max
Maximum number of timesteps allowed per call to solve()
Definition OneDim.h:380
virtual void resize()
Call after one or more grids has changed size, for example after being refined.
Definition OneDim.cpp:200
AnyMap serialize(const double *soln) const
Definition OneDim.cpp:456
void saveStats()
Save statistics on function and Jacobian evaluation, and reset the counters.
Definition OneDim.cpp:169
unique_ptr< MultiNewton > m_newt
Newton iterator.
Definition OneDim.h:345
size_t size() const
Total solution vector length;.
Definition OneDim.h:106
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:292
double ssnorm(double *x, double *r)
Steady-state max norm (infinity norm) of the residual evaluated using solution x.
Definition OneDim.cpp:324
void addDomain(shared_ptr< Domain1D > d)
Add a domain. Domains are added left-to-right.
Definition OneDim.cpp:80
double rdt() const
Reciprocal of the time step.
Definition OneDim.h:160
void initTimeInteg(double dt, double *x)
Prepare for time stepping beginning with solution x and timestep dt.
Definition OneDim.cpp:334
size_t nDomains() const
Number of domains.
Definition OneDim.h:64
Domain1D * right()
Pointer to right-most domain (last added).
Definition OneDim.h:116
std::tuple< string, size_t, string > component(size_t i)
Return the domain, local point index, and component name for the i-th component of the global solutio...
Definition OneDim.cpp:66
size_t bandwidth() const
Jacobian bandwidth.
Definition OneDim.h:136
double m_rdt
reciprocal of time step
Definition OneDim.h:346
shared_ptr< vector< double > > m_state
Solution vector.
Definition OneDim.h:342
const vector< int > & evalCountStats()
Return number of non-Jacobian function evaluations made in each call to solve()
Definition OneDim.h:307
void setTimeStepCallback(Func1 *callback)
Set a function that will be called after each successful timestep.
Definition OneDim.h:329
Func1 * m_interrupt
Function called at the start of every call to eval.
Definition OneDim.h:371
const vector< int > & jacobianCountStats()
Return number of Jacobian evaluations made in each call to solve()
Definition OneDim.h:300
vector< Domain1D * > m_connect
Definition OneDim.h:357
bool transient() const
True if transient mode.
Definition OneDim.h:168
void checkDomainArraySize(size_t nn) const
Check that an array size is at least nDomains().
Definition OneDim.h:87
const vector< size_t > & gridSizeStats()
Return total grid size in each call to solve()
Definition OneDim.h:281
void setMaxTimeStepCount(int nmax)
Set the maximum number of timeteps allowed before successful steady-state solve.
Definition OneDim.h:251
const vector< int > & timeStepStats()
Return number of time steps taken in each call to solve()
Definition OneDim.h:313
unique_ptr< MultiJac > m_jac
Jacobian evaluator.
Definition OneDim.h:344
size_t nVars(size_t jg)
Number of solution components at global point jg.
Definition OneDim.h:121
size_t m_bw
Jacobian bandwidth.
Definition OneDim.h:349
double m_tfactor
factor time step is multiplied by if time stepping fails ( < 1 )
Definition OneDim.h:340
vector< Domain1D * > m_dom
Definition OneDim.h:356
bool m_jac_ok
if true, Jacobian is current
Definition OneDim.h:347
int maxTimeStepCount() const
Return the maximum number of timeteps allowed before successful steady-state solve.
Definition OneDim.h:257
double timeStep(int nsteps, double dt, double *x, double *r, int loglevel)
Take time steps using Backward Euler.
Definition OneDim.cpp:382
Domain1D * pointDomain(size_t i)
Return a pointer to the domain global point i belongs to.
Definition OneDim.cpp:280
double m_tmin
minimum timestep size
Definition OneDim.h:336
size_t points()
Total number of points.
Definition OneDim.h:148
double m_tmax
maximum timestep size
Definition OneDim.h:337
void writeStats(int printTime=1)
Write statistics about the number of iterations and Jacobians at each grid level.
Definition OneDim.cpp:152
void clearStats()
Clear saved statistics.
Definition OneDim.cpp:187
void setSteadyMode()
Prepare to solve the steady-state problem.
Definition OneDim.cpp:353
const vector< double > & jacobianTimeStats()
Return CPU time spent evaluating Jacobians in each call to solve()
Definition OneDim.h:287
Func1 * m_time_step_callback
User-supplied function called after each successful timestep.
Definition OneDim.h:374
vector< int > m_timeSteps
Number of time steps taken in each call to solve() (for example, for each successive grid refinement)
Definition OneDim.h:394
Domain1D & domain(size_t i) const
Return a reference to domain i.
Definition OneDim.h:69
const vector< double > & evalTimeStats()
Return CPU time spent on non-Jacobian function evaluations in each call to solve()
Definition OneDim.h:294
Domain1D * left()
Pointer to left-most domain (first added).
Definition OneDim.h:111
bool steady() const
True if steady mode.
Definition OneDim.h:173
MultiNewton & newton()
Return a reference to the Newton iterator.
Definition OneDim.cpp:137
void setInterrupt(Func1 *interrupt)
Set a function that will be called every time eval is called.
Definition OneDim.h:321
MultiJac & jacobian()
Return a reference to the Jacobian evaluator of an OneDim object.
Definition OneDim.cpp:133
Namespace for the Cantera kernel.
Definition AnyMap.cpp:564