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