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