Cantera  3.3.0a1
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
45protected:
46 /**
47 * Set a single value in the solution vector.
48 * @param dom domain number, beginning with 0 for the leftmost domain.
49 * @param comp component number
50 * @param localPoint grid point within the domain, beginning with 0 for
51 * the leftmost grid point in the domain.
52 * @param value the value.
53 * @since New in %Cantera 3.2. Previously part of public interface.
54 */
55 void _setValue(size_t dom, size_t comp, size_t localPoint, double value);
56
57 /**
58 * Get one entry in the solution vector.
59 * @param dom domain number, beginning with 0 for the leftmost domain.
60 * @param comp component number
61 * @param localPoint grid point within the domain, beginning with 0 for
62 * the leftmost grid point in the domain.
63 * @since New in %Cantera 3.2. Previously part of public interface.
64 */
65 double _value(size_t dom, size_t comp, size_t localPoint) const;
66
67 /**
68 * Get an entry in the work vector, which may contain either a new system state
69 * or the current residual of the system.
70 * @param dom domain index
71 * @param comp component index
72 * @param localPoint grid point within the domain
73 * @since New in %Cantera 3.2. Previously part of public interface.
74 */
75 double _workValue(size_t dom, size_t comp, size_t localPoint) const;
76 //! @}
77
78public:
79
80 //! @name Logging, saving and restoring of solutions
81 //!
82 //! @{
83
84 /**
85 * Show logging information on current solution for all domains.
86 * @since New in %Cantera 3.0.
87 */
88 void show();
89
90 /**
91 * Save current simulation data to a container file or CSV format.
92 *
93 * In order to save the content of a Sim1D object, individual domains are
94 * converted to SolutionArray objects and saved using the SolutionArray::save()
95 * method. For HDF and YAML output, all domains are written to a single container
96 * file with shared header information. Simulation settings of individual domains
97 * are preserved as meta data of the corresponding SolutionArray objects.
98 * For CSV files, only state and auxiliary data of the main 1D domain are saved.
99 *
100 * The complete state of the current object can be restored from HDF and YAML
101 * container files using the restore() method, while individual domains can be
102 * loaded using SolutionArray::restore() for further analysis. While CSV do not
103 * contain complete information, they can still be used for setting initial states
104 * of individual simulation objects for some %Cantera API's.
105 *
106 * @param fname Name of output file (CSV, YAML or HDF)
107 * @param name Identifier of storage location within the container file; this
108 * node/group contains header information and multiple subgroups holding
109 * domain-specific SolutionArray data (YAML/HDF only)
110 * @param desc Custom comment describing the dataset to be stored (YAML/HDF only)
111 * @param overwrite Force overwrite if file/name exists; optional (default=false)
112 * @param compression Compression level (0-9); optional (default=0; HDF only)
113 * @param basis Output mass ("Y"/"mass") or mole ("X"/"mole") fractions;
114 * if not specified (default=""), the native basis of the underlying
115 * ThermoPhase manager is used - @see nativeState (CSV only)
116 */
117 void save(const string& fname, const string& name, const string& desc,
118 bool overwrite=false, int compression=0, const string& basis="");
119
120 /**
121 * Save the residual of the current solution to a container file.
122 * @param fname Name of output container file
123 * @param name Identifier of solution within the container file
124 * @param desc Description of the solution
125 * @param overwrite Force overwrite if name exists; optional (default=false)
126 * @param compression Compression level (optional; HDF only)
127 */
128 void saveResidual(const string& fname, const string& name,
129 const string& desc, bool overwrite=false, int compression=0);
130
131 /**
132 * Retrieve data and settings from a previously saved simulation.
133 *
134 * This method restores a simulation object from YAML or HDF data previously saved
135 * using the save() method.
136 *
137 * @param fname Name of container file (YAML or HDF)
138 * @param name Identifier of location within the container file; this node/group
139 * contains header information and subgroups with domain-specific SolutionArray
140 * data
141 * @return AnyMap containing header information
142 */
143 AnyMap restore(const string& fname, const string& name);
144
145 /**
146 * Retrieve data from a previously saved simulation.
147 *
148 * This method is almost identical to restore() but avoids the return of an AnyMap,
149 * which is not implemented in CLib.
150 *
151 * @param fname Name of container file (YAML or HDF)
152 * @param name Identifier of location within the container file; this node/group
153 * contains header information and subgroups with domain-specific SolutionArray
154 * data
155 */
156 void _restore(const string& fname, const string& name);
157
158 /**
159 * Deletes a `debug_sim1d.yaml` file if it exists. Used to clear the file for
160 * successive calls to the solve() method.
161 */
162 void clearDebugFile() override;
163
164 /**
165 * Write solver debugging information to a YAML file based on the specified log
166 * level.
167 *
168 * This method writes solver debug information to a specified YAML file
169 * (`debug_sim1d.yaml`). The section headers are formatted according to the provided
170 * `header_suffix` and `attempt_counter` arguments. Depending on the log level, the
171 * method will save either the solution information or the residual information
172 * for each attempted solution.
173 *
174 * @param header_suffix Header used to construct a unique section in the YAML file
175 * where the information will be written to.
176 * @param message A string that is written to the `description` tag in the YAML
177 * file.
178 * @param loglevel Controls the type of output that will be written. A `loglevel`
179 * greater than 6 saves the solution, and a `loglevel` greater
180 * than 7 saves the residual additionally.
181 * @param attempt_counter An integer counter used to uniquely identify the attempt
182 * which is included in the file header to differentiate
183 * between multiple solution attempts.
184 */
185 void writeDebugInfo(const string& header_suffix, const string& message, int loglevel,
186 int attempt_counter) override;
187
188 //! @}
189
190 /**
191 * Performs the hybrid Newton steady/time-stepping solution.
192 *
193 * The solver attempts to solve the steady-state problem first. If the steady-state
194 * solver fails, the time-stepping solver is used to take multiple time steps to
195 * move the solution closer to the steady-state solution. The steady-state solver is
196 * called again after the timesteps to make further progress towards the steady-state
197 * solution. This process is repeated until the steady-state solver converges or the
198 * maximum number of timesteps is reached.
199 *
200 * At the end of a successful solve, if the `refine_grid` flag is set, the grid will be
201 * analyzed and refined if necessary. If the grid is refined, the solution process
202 * described above is repeated with the new grid. This process is repeated until the
203 * grid no longer needs refinement based on the refine criteria.
204 *
205 * @param loglevel Controls the amount of diagnostic output.
206 * @param refine_grid If `true`, the grid will be refined
207 */
208 void solve(int loglevel = 0, bool refine_grid = true);
209
210 void eval(double rdt=-1.0, int count = 1) {
211 OneDim::eval(npos, m_state->data(), m_xnew.data(), rdt, count);
212 }
213 using OneDim::eval;
214
215 //! Evaluate the governing equations and return the vector of residuals
216 void getResidual(double rdt, double* resid) {
217 OneDim::eval(npos, m_state->data(), resid, rdt, 0);
218 }
219
220 //! Refine the grid in all domains.
221 //!
222 //! @returns If positive, the number of new grid points added. If negative, the
223 //! number of grid points removed. If zero, the grid is unchanged.
224 //!
225 //! @since Changed in %Cantera 3.1. Previously, the return value was zero if points
226 //! were removed but not added.
227 int refine(int loglevel=0);
228
229 //! Add node for fixed temperature point of freely propagating flame
230 int setFixedTemperature(double t);
231
232 //! Return temperature at the point used to fix the flame location
233 double fixedTemperature();
234
235 //! Return location of the point where temperature is fixed
237
238 /**
239 * Set the left control point location using the specified temperature.
240 * This is used when two-point flame control is active.
241 *
242 * The provided temperature will be used to locate the closest grid point to
243 * that temperature, which will serve to locate the left control point's
244 * coordinate. Starting from the left boundary, the first grid point that is
245 * equal to or exceeds the specified temperature will be used to locate the
246 * left control point's coordinate.
247 */
248 void setLeftControlPoint(double temperature);
249
250 /**
251 * Set the right control point location using the specified temperature.
252 * This is used when two-point flame control is active.
253 *
254 * The provided temperature will be used to locate the closest grid point to
255 * that temperature, which will serve to locate the right control point's
256 * coordinate. Starting from the right boundary, the first grid point that is
257 * equal to or exceeds the specified temperature will be used to locate the
258 * right control point's coordinate.
259 */
260 void setRightControlPoint(double temperature);
261
262 /**
263 * Set grid refinement criteria. If dom >= 0, then the settings
264 * apply only to the specified domain. If dom < 0, the settings
265 * are applied to each domain. @see Refiner::setCriteria.
266 */
267 void setRefineCriteria(int dom = -1, double ratio = 10.0,
268 double slope = 0.8, double curve = 0.8,
269 double prune = -0.1);
270
271 /**
272 * Get the grid refinement criteria. dom must be greater than
273 * or equal to zero (that is, the domain must be specified).
274 * @see Refiner::getCriteria
275 */
276 vector<double> getRefineCriteria(int dom);
277
278 /**
279 * Set the maximum number of grid points in the domain. If dom >= 0,
280 * then the settings apply only to the specified domain. If dom < 0,
281 * the settings are applied to each domain. @see Refiner::setMaxPoints.
282 */
283 void setMaxGridPoints(int dom, int npoints);
284
285 /**
286 * Get the maximum number of grid points in this domain. @see Refiner::maxPoints
287 *
288 * @param dom domain number, beginning with 0 for the leftmost domain.
289 */
290 size_t maxGridPoints(size_t dom);
291
292 //! Set the minimum grid spacing in the specified domain(s).
293 /*!
294 * @param dom Domain index. If dom == -1, the specified spacing is applied
295 * to all domains.
296 * @param gridmin The minimum allowable grid spacing [m]
297 */
298 void setGridMin(int dom, double gridmin);
299
300 //! Set the current solution vector to the last successful time-stepping
301 //! solution. This can be used to examine the solver progress after a failed
302 //! integration.
304
305 //! Set the current solution vector and grid to the last successful steady-
306 //! state solution. This can be used to examine the solver progress after a
307 //! failure during grid refinement.
309
310 //! Get the initial value of the system state from each domain in the simulation.
311 void getInitialSoln();
312
313 //! Evaluate the Jacobian in steady-state mode.
314 void evalSSJacobian();
315
316 //! Solve the equation @f$ J^T \lambda = b @f$.
317 /**
318 * Here, @f$ J = \partial f/\partial x @f$ is the Jacobian matrix of the
319 * system of equations @f$ f(x,p)=0 @f$. This can be used to efficiently
320 * solve for the sensitivities of a scalar objective function @f$ g(x,p) @f$
321 * to a vector of parameters @f$ p @f$ by solving:
322 * @f[ J^T \lambda = \left( \frac{\partial g}{\partial x} \right)^T @f]
323 * for @f$ \lambda @f$ and then computing:
324 * @f[
325 * \left.\frac{dg}{dp}\right|_{f=0} = \frac{\partial g}{\partial p}
326 * - \lambda^T \frac{\partial f}{\partial p}
327 * @f]
328 */
329 void solveAdjoint(const double* b, double* lambda);
330
331 void resize() override;
332
333 //! Set a function that will be called after each successful steady-state
334 //! solve, before regridding. Intended to be used for observing solver
335 //! progress for debugging purposes.
336 void setSteadyCallback(Func1* callback) {
337 m_steady_callback = callback;
338 }
339
340protected:
341 //! the solution vector after the last successful steady-state solve (stored
342 //! before grid refinement)
343 vector<double> m_xlast_ss;
344
345 //! the grids for each domain after the last successful steady-state solve
346 //! (stored before grid refinement)
347 vector<vector<double>> m_grid_last_ss;
348
349 //! User-supplied function called after a successful steady-state solve.
351
352private:
353 //! Calls method _finalize in each domain.
354 void finalize();
355};
356
357/**
358 * Create a Sim1D object with a list of domains.
359 * @param[in] domains A vector of shared pointers to the domains to be linked together.
360 * The domain pointers must be entered in left-to-right order --- that is,
361 * the pointer to the leftmost domain is domain[0], the pointer to the
362 * domain to its right is domain[1], etc.
363 * @since New in %Cantera 3.2.
364 */
365shared_ptr<Sim1D> newSim1D(vector<shared_ptr<Domain1D>>& domains);
366
367}
368#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:26
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
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:324
void restoreTimeSteppingSolution()
Set the current solution vector to the last successful time-stepping solution.
Definition Sim1D.cpp:302
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:799
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:110
double fixedTemperatureLocation()
Return location of the point where temperature is fixed.
Definition Sim1D.cpp:596
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:347
double _value(size_t dom, size_t comp, size_t localPoint) const
Get one entry in the solution vector.
Definition Sim1D.cpp:45
void finalize()
Calls method _finalize in each domain.
Definition Sim1D.cpp:331
void setSteadyCallback(Func1 *callback)
Set a function that will be called after each successful steady-state solve, before regridding.
Definition Sim1D.h:336
int refine(int loglevel=0)
Refine the grid in all domains.
Definition Sim1D.cpp:386
void show()
Show logging information on current solution for all domains.
Definition Sim1D.cpp:291
double fixedTemperature()
Return temperature at the point used to fix the flame location.
Definition Sim1D.cpp:583
void clearDebugFile() override
Deletes a debug_sim1d.yaml file if it exists.
Definition Sim1D.cpp:473
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:53
vector< double > m_xlast_ss
the solution vector after the last successful steady-state solve (stored before grid refinement)
Definition Sim1D.h:343
void _restore(const string &fname, const string &name)
Retrieve data from a previously saved simulation.
Definition Sim1D.cpp:286
void setMaxGridPoints(int dom, int npoints)
Set the maximum number of grid points in the domain.
Definition Sim1D.cpp:747
int setFixedTemperature(double t)
Add node for fixed temperature point of freely propagating flame.
Definition Sim1D.cpp:492
void getResidual(double rdt, double *resid)
Evaluate the governing equations and return the vector of residuals.
Definition Sim1D.h:216
void solve(int loglevel=0, bool refine_grid=true)
Performs the hybrid Newton steady/time-stepping solution.
Definition Sim1D.cpp:338
void evalSSJacobian()
Evaluate the Jacobian in steady-state mode.
Definition Sim1D.cpp:766
void solveAdjoint(const double *b, double *lambda)
Solve the equation .
Definition Sim1D.cpp:771
AnyMap restore(const string &fname, const string &name)
Retrieve data and settings from a previously saved simulation.
Definition Sim1D.cpp:209
Func1 * m_steady_callback
User-supplied function called after a successful steady-state solve.
Definition Sim1D.h:350
void restoreSteadySolution()
Set the current solution vector and grid to the last successful steady- state solution.
Definition Sim1D.cpp:311
size_t maxGridPoints(size_t dom)
Get the maximum number of grid points in this domain.
Definition Sim1D.cpp:760
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:478
void setRightControlPoint(double temperature)
Set the right control point location using the specified temperature.
Definition Sim1D.cpp:660
vector< double > getRefineCriteria(int dom)
Get the grid refinement criteria.
Definition Sim1D.cpp:724
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:734
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:712
void _setValue(size_t dom, size_t comp, size_t localPoint, double value)
Set a single value in the solution vector.
Definition Sim1D.cpp:37
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:61
void setLeftControlPoint(double temperature)
Set the left control point location using the specified temperature.
Definition Sim1D.cpp:609
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.
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:805