Cantera  3.2.0a4
Loading...
Searching...
No Matches
Sim1D.h
Go to the documentation of this file.
1/**
2 * @file Sim1D.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_SIM1D_H
9#define CT_SIM1D_H
10
11#include "OneDim.h"
12
13namespace Cantera
14{
15
16/**
17 * One-dimensional simulations. Class Sim1D extends class OneDim by storing
18 * the solution vector, and by adding a hybrid Newton/time-stepping solver.
19 * @ingroup onedGroup
20 */
21class Sim1D : public OneDim
22{
23public:
24 //! Default constructor.
25 /*!
26 * This constructor is provided to make the class default-constructible, but
27 * is not meant to be used in most applications. Use the next constructor
28 */
29 Sim1D() {}
30
31 /**
32 * Standard constructor.
33 * @param domains A vector of shared pointers to the domains to be linked together.
34 * The domain pointers must be entered in left-to-right order --- that is,
35 * the pointer to the leftmost domain is domain[0], the pointer to the
36 * domain to its right is domain[1], etc.
37 */
38 Sim1D(vector<shared_ptr<Domain1D>>& domains);
39
40 //! @name Setting initial values
41 //!
42 //! These methods are used to set the initial values of solution components.
43 //! @{
44
45 //! Set initial guess for one component for all domains
46 /**
47 * @param component component name
48 * @param locs A vector of relative positions, beginning with 0.0 at the
49 * left of the domain, and ending with 1.0 at the right of the domain.
50 * @param vals A vector of values corresponding to the relative position
51 * locations.
52 *
53 * @deprecated To be removed after %Cantera 3.2.
54 * Replaceable by Domain1D::setProfile.
55 */
56 void setInitialGuess(const string& component, vector<double>& locs,
57 vector<double>& vals);
58
59 /**
60 * Set a single value in the solution vector.
61 * @param dom domain number, beginning with 0 for the leftmost domain.
62 * @param comp component number
63 * @param localPoint grid point within the domain, beginning with 0 for
64 * the leftmost grid point in the domain.
65 * @param value the value.
66 * @deprecated To be removed after %Cantera 3.2. Replaceable by Domain1D::setValue()
67 */
68 void setValue(size_t dom, size_t comp, size_t localPoint, double value) {
69 warn_deprecated("Sim1D::setValue",
70 "To be removed after Cantera 3.2. Access from Domain1D object instead.");
71 _setValue(dom, comp, localPoint, value);
72 }
73
74 /**
75 * Get one entry in the solution vector.
76 * @param dom domain number, beginning with 0 for the leftmost domain.
77 * @param comp component number
78 * @param localPoint grid point within the domain, beginning with 0 for
79 * the leftmost grid point in the domain.
80 * @deprecated To be removed after %Cantera 3.2. Replaceable by Domain1D::value()
81 */
82 double value(size_t dom, size_t comp, size_t localPoint) const {
83 warn_deprecated("Sim1D::setValue",
84 "To be removed after Cantera 3.2. Access from Domain1D object instead.");
85 return _value(dom, comp, localPoint);
86 }
87
88 //! Get an entry in the work vector, which may contain either a new system state
89 //! or the current residual of the system.
90 //! @param dom domain index
91 //! @param comp component index
92 //! @param localPoint grid point within the domain
93 //! @deprecated To be removed after %Cantera 3.2. Replaceable by
94 //! Domain1D::getResiduals()
95 double workValue(size_t dom, size_t comp, size_t localPoint) const {
96 warn_deprecated("Sim1D::setValue",
97 "To be removed after Cantera 3.2. Access residuals via Domain1D instead.");
98 return _workValue(dom, comp, localPoint);
99 }
100
101protected:
102 /**
103 * Set a single value in the solution vector.
104 * @param dom domain number, beginning with 0 for the leftmost domain.
105 * @param comp component number
106 * @param localPoint grid point within the domain, beginning with 0 for
107 * the leftmost grid point in the domain.
108 * @param value the value.
109 * @since New in %Cantera 3.2. Previously part of public interface.
110 */
111 void _setValue(size_t dom, size_t comp, size_t localPoint, double value);
112
113 /**
114 * Get one entry in the solution vector.
115 * @param dom domain number, beginning with 0 for the leftmost domain.
116 * @param comp component number
117 * @param localPoint grid point within the domain, beginning with 0 for
118 * the leftmost grid point in the domain.
119 * @since New in %Cantera 3.2. Previously part of public interface.
120 */
121 double _value(size_t dom, size_t comp, size_t localPoint) const;
122
123 /**
124 * Get an entry in the work vector, which may contain either a new system state
125 * or the current residual of the system.
126 * @param dom domain index
127 * @param comp component index
128 * @param localPoint grid point within the domain
129 * @since New in %Cantera 3.2. Previously part of public interface.
130 */
131 double _workValue(size_t dom, size_t comp, size_t localPoint) const;
132
133public:
134 /**
135 * Specify a profile for one component of one domain.
136 * @param dom domain number, beginning with 0 for the leftmost domain.
137 * @param comp component number
138 * @param pos A vector of relative positions, beginning with 0.0 at the
139 * left of the domain, and ending with 1.0 at the right of the domain.
140 * @param values A vector of values corresponding to the relative position
141 * locations.
142 *
143 * Note that the vector pos and values can have lengths different than the
144 * number of grid points, but their lengths must be equal. The values at
145 * the grid points will be linearly interpolated based on the (pos,
146 * values) specification.
147 *
148 * @deprecated To be removed after %Cantera 3.2.
149 * Replaceable by Domain1D::setProfile.
150 */
151 void setProfile(size_t dom, size_t comp, const vector<double>& pos,
152 const vector<double>& values);
153
154 //! Set component 'comp' of domain 'dom' to value 'v' at all points.
155 //! @deprecated To be removed after %Cantera 3.2.
156 //! Replaceable by Domain1D::setProfile.
157 void setFlatProfile(size_t dom, size_t comp, double v);
158
159 //! @}
160
161 //! @name Logging, saving and restoring of solutions
162 //!
163 //! @{
164
165 /**
166 * Show logging information on current solution for all domains.
167 * @since New in %Cantera 3.0.
168 */
169 void show();
170
171 /**
172 * Save current simulation data to a container file or CSV format.
173 *
174 * In order to save the content of a Sim1D object, individual domains are
175 * converted to SolutionArray objects and saved using the SolutionArray::save()
176 * method. For HDF and YAML output, all domains are written to a single container
177 * file with shared header information. Simulation settings of individual domains
178 * are preserved as meta data of the corresponding SolutionArray objects.
179 * For CSV files, only state and auxiliary data of the main 1D domain are saved.
180 *
181 * The complete state of the current object can be restored from HDF and YAML
182 * container files using the restore() method, while individual domains can be
183 * loaded using SolutionArray::restore() for further analysis. While CSV do not
184 * contain complete information, they can still be used for setting initial states
185 * of individual simulation objects for some %Cantera API's.
186 *
187 * @param fname Name of output file (CSV, YAML or HDF)
188 * @param name Identifier of storage location within the container file; this
189 * node/group contains header information and multiple subgroups holding
190 * domain-specific SolutionArray data (YAML/HDF only)
191 * @param desc Custom comment describing the dataset to be stored (YAML/HDF only)
192 * @param overwrite Force overwrite if file/name exists; optional (default=false)
193 * @param compression Compression level (0-9); optional (default=0; HDF only)
194 * @param basis Output mass ("Y"/"mass") or mole ("X"/"mole") fractions;
195 * if not specified (default=""), the native basis of the underlying
196 * ThermoPhase manager is used - @see nativeState (CSV only)
197 */
198 void save(const string& fname, const string& name, const string& desc,
199 bool overwrite=false, int compression=0, const string& basis="");
200
201 /**
202 * Save the residual of the current solution to a container file.
203 * @param fname Name of output container file
204 * @param name Identifier of solution within the container file
205 * @param desc Description of the solution
206 * @param overwrite Force overwrite if name exists; optional (default=false)
207 * @param compression Compression level (optional; HDF only)
208 */
209 void saveResidual(const string& fname, const string& name,
210 const string& desc, bool overwrite=false, int compression=0);
211
212 /**
213 * Retrieve data and settings from a previously saved simulation.
214 *
215 * This method restores a simulation object from YAML or HDF data previously saved
216 * using the save() method.
217 *
218 * @param fname Name of container file (YAML or HDF)
219 * @param name Identifier of location within the container file; this node/group
220 * contains header information and subgroups with domain-specific SolutionArray
221 * data
222 * @return AnyMap containing header information
223 */
224 AnyMap restore(const string& fname, const string& name);
225
226 /**
227 * Retrieve data from a previously saved simulation.
228 *
229 * This method is almost identical to restore() but avoids the return of an AnyMap,
230 * which is not implemented in CLib.
231 *
232 * @param fname Name of container file (YAML or HDF)
233 * @param name Identifier of location within the container file; this node/group
234 * contains header information and subgroups with domain-specific SolutionArray
235 * data
236 */
237 void _restore(const string& fname, const string& name);
238
239 /**
240 * Deletes a `debug_sim1d.yaml` file if it exists. Used to clear the file for
241 * successive calls to the solve() method.
242 */
243 void clearDebugFile() override;
244
245 /**
246 * Write solver debugging information to a YAML file based on the specified log
247 * level.
248 *
249 * This method writes solver debug information to a specified YAML file
250 * (`debug_sim1d.yaml`). The section headers are formatted according to the provided
251 * `header_suffix` and `attempt_counter` arguments. Depending on the log level, the
252 * method will save either the solution information or the residual information
253 * for each attempted solution.
254 *
255 * @param header_suffix Header used to construct a unique section in the YAML file
256 * where the information will be written to.
257 * @param message A string that is written to the `description` tag in the YAML
258 * file.
259 * @param loglevel Controls the type of output that will be written. A `loglevel`
260 * greater than 6 saves the solution, and a `loglevel` greater
261 * than 7 saves the residual additionally.
262 * @param attempt_counter An integer counter used to uniquely identify the attempt
263 * which is included in the file header to differentiate
264 * between multiple solution attempts.
265 */
266 void writeDebugInfo(const string& header_suffix, const string& message, int loglevel,
267 int attempt_counter) override;
268
269 //! @}
270
271 /**
272 * Performs the hybrid Newton steady/time-stepping solution.
273 *
274 * The solver attempts to solve the steady-state problem first. If the steady-state
275 * solver fails, the time-stepping solver is used to take multiple time steps to
276 * move the solution closer to the steady-state solution. The steady-state solver is
277 * called again after the timesteps to make further progress towards the steady-state
278 * solution. This process is repeated until the steady-state solver converges or the
279 * maximum number of timesteps is reached.
280 *
281 * At the end of a successful solve, if the `refine_grid` flag is set, the grid will be
282 * analyzed and refined if necessary. If the grid is refined, the solution process
283 * described above is repeated with the new grid. This process is repeated until the
284 * grid no longer needs refinement based on the refine criteria.
285 *
286 * @param loglevel Controls the amount of diagnostic output.
287 * @param refine_grid If `true`, the grid will be refined
288 */
289 void solve(int loglevel = 0, bool refine_grid = true);
290
291 void eval(double rdt=-1.0, int count = 1) {
292 OneDim::eval(npos, m_state->data(), m_xnew.data(), rdt, count);
293 }
294 using OneDim::eval;
295
296 //! Evaluate the governing equations and return the vector of residuals
297 void getResidual(double rdt, double* resid) {
298 OneDim::eval(npos, m_state->data(), resid, rdt, 0);
299 }
300
301 //! Refine the grid in all domains.
302 //!
303 //! @returns If positive, the number of new grid points added. If negative, the
304 //! number of grid points removed. If zero, the grid is unchanged.
305 //!
306 //! @since Changed in %Cantera 3.1. Previously, the return value was zero if points
307 //! were removed but not added.
308 int refine(int loglevel=0);
309
310 //! Add node for fixed temperature point of freely propagating flame
311 int setFixedTemperature(double t);
312
313 //! Return temperature at the point used to fix the flame location
314 double fixedTemperature();
315
316 //! Return location of the point where temperature is fixed
318
319 /**
320 * Set the left control point location using the specified temperature.
321 * This is used when two-point flame control is active.
322 *
323 * The provided temperature will be used to locate the closest grid point to
324 * that temperature, which will serve to locate the left control point's
325 * coordinate. Starting from the left boundary, the first grid point that is
326 * equal to or exceeds the specified temperature will be used to locate the
327 * left control point's coordinate.
328 */
329 void setLeftControlPoint(double temperature);
330
331 /**
332 * Set the right control point location using the specified temperature.
333 * This is used when two-point flame control is active.
334 *
335 * The provided temperature will be used to locate the closest grid point to
336 * that temperature, which will serve to locate the right control point's
337 * coordinate. Starting from the right boundary, the first grid point that is
338 * equal to or exceeds the specified temperature will be used to locate the
339 * right control point's coordinate.
340 */
341 void setRightControlPoint(double temperature);
342
343 /**
344 * Set grid refinement criteria. If dom >= 0, then the settings
345 * apply only to the specified domain. If dom < 0, the settings
346 * are applied to each domain. @see Refiner::setCriteria.
347 */
348 void setRefineCriteria(int dom = -1, double ratio = 10.0,
349 double slope = 0.8, double curve = 0.8,
350 double prune = -0.1);
351
352 /**
353 * Get the grid refinement criteria. dom must be greater than
354 * or equal to zero (that is, the domain must be specified).
355 * @see Refiner::getCriteria
356 */
357 vector<double> getRefineCriteria(int dom);
358
359 /**
360 * Set the maximum number of grid points in the domain. If dom >= 0,
361 * then the settings apply only to the specified domain. If dom < 0,
362 * the settings are applied to each domain. @see Refiner::setMaxPoints.
363 */
364 void setMaxGridPoints(int dom, int npoints);
365
366 /**
367 * Get the maximum number of grid points in this domain. @see Refiner::maxPoints
368 *
369 * @param dom domain number, beginning with 0 for the leftmost domain.
370 */
371 size_t maxGridPoints(size_t dom);
372
373 //! Set the minimum grid spacing in the specified domain(s).
374 /*!
375 * @param dom Domain index. If dom == -1, the specified spacing is applied
376 * to all domains.
377 * @param gridmin The minimum allowable grid spacing [m]
378 */
379 void setGridMin(int dom, double gridmin);
380
381 //! Set the current solution vector to the last successful time-stepping
382 //! solution. This can be used to examine the solver progress after a failed
383 //! integration.
385
386 //! Set the current solution vector and grid to the last successful steady-
387 //! state solution. This can be used to examine the solver progress after a
388 //! failure during grid refinement.
390
391 //! Get the initial value of the system state from each domain in the simulation.
392 void getInitialSoln();
393
394 //! Get the Jacobian element @f$ J_{ij} = \partial f_i / \partial x_j \f$
395 //! @deprecated To be removed after %Cantera 3.2.
396 double jacobian(int i, int j);
397
398 //! Evaluate the Jacobian in steady-state mode.
399 void evalSSJacobian();
400
401 //! Solve the equation @f$ J^T \lambda = b @f$.
402 /**
403 * Here, @f$ J = \partial f/\partial x @f$ is the Jacobian matrix of the
404 * system of equations @f$ f(x,p)=0 @f$. This can be used to efficiently
405 * solve for the sensitivities of a scalar objective function @f$ g(x,p) @f$
406 * to a vector of parameters @f$ p @f$ by solving:
407 * @f[ J^T \lambda = \left( \frac{\partial g}{\partial x} \right)^T @f]
408 * for @f$ \lambda @f$ and then computing:
409 * @f[
410 * \left.\frac{dg}{dp}\right|_{f=0} = \frac{\partial g}{\partial p}
411 * - \lambda^T \frac{\partial f}{\partial p}
412 * @f]
413 */
414 void solveAdjoint(const double* b, double* lambda);
415
416 void resize() override;
417
418 //! Set a function that will be called after each successful steady-state
419 //! solve, before regridding. Intended to be used for observing solver
420 //! progress for debugging purposes.
421 void setSteadyCallback(Func1* callback) {
422 m_steady_callback = callback;
423 }
424
425protected:
426 //! the solution vector after the last successful steady-state solve (stored
427 //! before grid refinement)
428 vector<double> m_xlast_ss;
429
430 //! the grids for each domain after the last successful steady-state solve
431 //! (stored before grid refinement)
432 vector<vector<double>> m_grid_last_ss;
433
434 //! User-supplied function called after a successful steady-state solve.
436
437private:
438 //! Calls method _finalize in each domain.
439 void finalize();
440};
441
442/**
443 * Create a Sim1D object with a list of domains.
444 * @param domains A vector of shared pointers to the domains to be linked together.
445 * The domain pointers must be entered in left-to-right order --- that is,
446 * the pointer to the leftmost domain is domain[0], the pointer to the
447 * domain to its right is domain[1], etc.
448 * @since New in %Cantera 3.2.
449 */
450shared_ptr<Sim1D> newSim1D(vector<shared_ptr<Domain1D>>& domains);
451
452}
453#endif
A map of string keys to values whose type can vary at runtime.
Definition AnyMap.h:431
Base class for 'functor' classes that evaluate a function of one variable.
Definition Func1.h:75
Container class for multiple-domain 1D problems.
Definition OneDim.h:27
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
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
One-dimensional simulations.
Definition Sim1D.h:22
void getInitialSoln()
Get the initial value of the system state from each domain in the simulation.
Definition Sim1D.cpp:371
void restoreTimeSteppingSolution()
Set the current solution vector to the last successful time-stepping solution.
Definition Sim1D.cpp:349
void resize() override
Call to set the size of internal data structures after first defining the system or if the problem si...
Definition Sim1D.cpp:855
void saveResidual(const string &fname, const string &name, const string &desc, bool overwrite=false, int compression=0)
Save the residual of the current solution to a container file.
Definition Sim1D.cpp:147
void setProfile(size_t dom, size_t comp, const vector< double > &pos, const vector< double > &values)
Specify a profile for one component of one domain.
Definition Sim1D.cpp:77
double fixedTemperatureLocation()
Return location of the point where temperature is fixed.
Definition Sim1D.cpp:643
vector< vector< double > > m_grid_last_ss
the grids for each domain after the last successful steady-state solve (stored before grid refinement...
Definition Sim1D.h:432
double _value(size_t dom, size_t comp, size_t localPoint) const
Get one entry in the solution vector.
Definition Sim1D.cpp:61
void finalize()
Calls method _finalize in each domain.
Definition Sim1D.cpp:378
void setValue(size_t dom, size_t comp, size_t localPoint, double value)
Set a single value in the solution vector.
Definition Sim1D.h:68
void setSteadyCallback(Func1 *callback)
Set a function that will be called after each successful steady-state solve, before regridding.
Definition Sim1D.h:421
int refine(int loglevel=0)
Refine the grid in all domains.
Definition Sim1D.cpp:433
void show()
Show logging information on current solution for all domains.
Definition Sim1D.cpp:338
double fixedTemperature()
Return temperature at the point used to fix the flame location.
Definition Sim1D.cpp:630
void clearDebugFile() override
Deletes a debug_sim1d.yaml file if it exists.
Definition Sim1D.cpp:520
double _workValue(size_t dom, size_t comp, size_t localPoint) const
Get an entry in the work vector, which may contain either a new system state or the current residual ...
Definition Sim1D.cpp:69
vector< double > m_xlast_ss
the solution vector after the last successful steady-state solve (stored before grid refinement)
Definition Sim1D.h:428
void _restore(const string &fname, const string &name)
Retrieve data from a previously saved simulation.
Definition Sim1D.cpp:323
void setMaxGridPoints(int dom, int npoints)
Set the maximum number of grid points in the domain.
Definition Sim1D.cpp:797
int setFixedTemperature(double t)
Add node for fixed temperature point of freely propagating flame.
Definition Sim1D.cpp:539
void getResidual(double rdt, double *resid)
Evaluate the governing equations and return the vector of residuals.
Definition Sim1D.h:297
void setInitialGuess(const string &component, vector< double > &locs, vector< double > &vals)
Set initial guess for one component for all domains.
Definition Sim1D.cpp:37
void solve(int loglevel=0, bool refine_grid=true)
Performs the hybrid Newton steady/time-stepping solution.
Definition Sim1D.cpp:385
void evalSSJacobian()
Evaluate the Jacobian in steady-state mode.
Definition Sim1D.cpp:822
void solveAdjoint(const double *b, double *lambda)
Solve the equation .
Definition Sim1D.cpp:827
AnyMap restore(const string &fname, const string &name)
Retrieve data and settings from a previously saved simulation.
Definition Sim1D.cpp:246
Func1 * m_steady_callback
User-supplied function called after a successful steady-state solve.
Definition Sim1D.h:435
void restoreSteadySolution()
Set the current solution vector and grid to the last successful steady- state solution.
Definition Sim1D.cpp:358
size_t maxGridPoints(size_t dom)
Get the maximum number of grid points in this domain.
Definition Sim1D.cpp:810
void setFlatProfile(size_t dom, size_t comp, double v)
Set component 'comp' of domain 'dom' to value 'v' at all points.
Definition Sim1D.cpp:328
void writeDebugInfo(const string &header_suffix, const string &message, int loglevel, int attempt_counter) override
Write solver debugging information to a YAML file based on the specified log level.
Definition Sim1D.cpp:525
void setRightControlPoint(double temperature)
Set the right control point location using the specified temperature.
Definition Sim1D.cpp:707
double value(size_t dom, size_t comp, size_t localPoint) const
Get one entry in the solution vector.
Definition Sim1D.h:82
double workValue(size_t dom, size_t comp, size_t localPoint) const
Get an entry in the work vector, which may contain either a new system state or the current residual ...
Definition Sim1D.h:95
vector< double > getRefineCriteria(int dom)
Get the grid refinement criteria.
Definition Sim1D.cpp:773
Sim1D()
Default constructor.
Definition Sim1D.h:29
void setGridMin(int dom, double gridmin)
Set the minimum grid spacing in the specified domain(s).
Definition Sim1D.cpp:784
void setRefineCriteria(int dom=-1, double ratio=10.0, double slope=0.8, double curve=0.8, double prune=-0.1)
Set grid refinement criteria.
Definition Sim1D.cpp:759
void _setValue(size_t dom, size_t comp, size_t localPoint, double value)
Set a single value in the solution vector.
Definition Sim1D.cpp:53
void save(const string &fname, const string &name, const string &desc, bool overwrite=false, int compression=0, const string &basis="")
Save current simulation data to a container file or CSV format.
Definition Sim1D.cpp:98
void setLeftControlPoint(double temperature)
Set the left control point location using the specified temperature.
Definition Sim1D.cpp:656
vector< double > m_xnew
Work array used to hold the residual or the new solution.
double rdt() const
Reciprocal of the time step.
shared_ptr< vector< double > > m_state
Solution vector.
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
shared_ptr< Sim1D > newSim1D(vector< shared_ptr< Domain1D > > &domains)
Create a Sim1D object with a list of domains.
Definition Sim1D.cpp:861
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