Cantera  3.2.0a2
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() override;
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) override;
198
199 //! @}
200
201 /**
202 * Performs the hybrid Newton steady/time-stepping solution.
203 *
204 * The solver attempts to solve the steady-state problem first. If the steady-state
205 * solver fails, the time-stepping solver is used to take multiple time steps to
206 * move the solution closer to the steady-state solution. The steady-state solver is
207 * called again after the timesteps to make further progress towards the steady-state
208 * solution. This process is repeated until the steady-state solver converges or the
209 * maximum number of timesteps is reached.
210 *
211 * At the end of a successful solve, if the `refine_grid` flag is set, the grid will be
212 * analyzed and refined if necessary. If the grid is refined, the solution process
213 * described above is repeated with the new grid. This process is repeated until the
214 * grid no longer needs refinement based on the refine criteria.
215 *
216 * @param loglevel Controls the amount of diagnostic output.
217 * @param refine_grid If `true`, the grid will be refined
218 */
219 void solve(int loglevel = 0, bool refine_grid = true);
220
221 void eval(double rdt=-1.0, int count = 1) {
222 OneDim::eval(npos, m_state->data(), m_xnew.data(), rdt, count);
223 }
224 using OneDim::eval;
225
226 //! Evaluate the governing equations and return the vector of residuals
227 void getResidual(double rdt, double* resid) {
228 OneDim::eval(npos, m_state->data(), resid, rdt, 0);
229 }
230
231 //! Refine the grid in all domains.
232 //!
233 //! @returns If positive, the number of new grid points added. If negative, the
234 //! number of grid points removed. If zero, the grid is unchanged.
235 //!
236 //! @since Changed in %Cantera 3.1. Previously, the return value was zero if points
237 //! were removed but not added.
238 int refine(int loglevel=0);
239
240 //! Add node for fixed temperature point of freely propagating flame
241 int setFixedTemperature(double t);
242
243 //! Return temperature at the point used to fix the flame location
244 double fixedTemperature();
245
246 //! Return location of the point where temperature is fixed
248
249 /**
250 * Set the left control point location using the specified temperature.
251 * This is used when two-point flame control is active.
252 *
253 * The provided temperature will be used to locate the closest grid point to
254 * that temperature, which will serve to locate the left control point's
255 * coordinate. Starting from the left boundary, the first grid point that is
256 * equal to or exceeds the specified temperature will be used to locate the
257 * left control point's coordinate.
258 */
259 void setLeftControlPoint(double temperature);
260
261 /**
262 * Set the right control point location using the specified temperature.
263 * This is used when two-point flame control is active.
264 *
265 * The provided temperature will be used to locate the closest grid point to
266 * that temperature, which will serve to locate the right control point's
267 * coordinate. Starting from the right boundary, the first grid point that is
268 * equal to or exceeds the specified temperature will be used to locate the
269 * right control point's coordinate.
270 */
271 void setRightControlPoint(double temperature);
272
273 /**
274 * Set grid refinement criteria. If dom >= 0, then the settings
275 * apply only to the specified domain. If dom < 0, the settings
276 * are applied to each domain. @see Refiner::setCriteria.
277 */
278 void setRefineCriteria(int dom = -1, double ratio = 10.0,
279 double slope = 0.8, double curve = 0.8,
280 double prune = -0.1);
281
282 /**
283 * Get the grid refinement criteria. dom must be greater than
284 * or equal to zero (that is, the domain must be specified).
285 * @see Refiner::getCriteria
286 */
287 vector<double> getRefineCriteria(int dom);
288
289 /**
290 * Set the maximum number of grid points in the domain. If dom >= 0,
291 * then the settings apply only to the specified domain. If dom < 0,
292 * the settings are applied to each domain. @see Refiner::setMaxPoints.
293 */
294 void setMaxGridPoints(int dom, int npoints);
295
296 /**
297 * Get the maximum number of grid points in this domain. @see Refiner::maxPoints
298 *
299 * @param dom domain number, beginning with 0 for the leftmost domain.
300 */
301 size_t maxGridPoints(size_t dom);
302
303 //! Set the minimum grid spacing in the specified domain(s).
304 /*!
305 * @param dom Domain index. If dom == -1, the specified spacing is applied
306 * to all domains.
307 * @param gridmin The minimum allowable grid spacing [m]
308 */
309 void setGridMin(int dom, double gridmin);
310
311 //! Set the current solution vector to the last successful time-stepping
312 //! solution. This can be used to examine the solver progress after a failed
313 //! integration.
315
316 //! Set the current solution vector and grid to the last successful steady-
317 //! state solution. This can be used to examine the solver progress after a
318 //! failure during grid refinement.
320
321 //! Get the initial value of the system state from each domain in the simulation.
322 void getInitialSoln();
323
324 //! Get the Jacobian element @f$ J_{ij} = \partial f_i / \partial x_j \f$
325 //! @deprecated To be removed after %Cantera 3.2.
326 double jacobian(int i, int j);
327
328 //! Evaluate the Jacobian in steady-state mode.
329 void evalSSJacobian();
330
331 //! Solve the equation @f$ J^T \lambda = b @f$.
332 /**
333 * Here, @f$ J = \partial f/\partial x @f$ is the Jacobian matrix of the
334 * system of equations @f$ f(x,p)=0 @f$. This can be used to efficiently
335 * solve for the sensitivities of a scalar objective function @f$ g(x,p) @f$
336 * to a vector of parameters @f$ p @f$ by solving:
337 * @f[ J^T \lambda = \left( \frac{\partial g}{\partial x} \right)^T @f]
338 * for @f$ \lambda @f$ and then computing:
339 * @f[
340 * \left.\frac{dg}{dp}\right|_{f=0} = \frac{\partial g}{\partial p}
341 * - \lambda^T \frac{\partial f}{\partial p}
342 * @f]
343 */
344 void solveAdjoint(const double* b, double* lambda);
345
346 void resize() override;
347
348 //! Set a function that will be called after each successful steady-state
349 //! solve, before regridding. Intended to be used for observing solver
350 //! progress for debugging purposes.
351 void setSteadyCallback(Func1* callback) {
352 m_steady_callback = callback;
353 }
354
355protected:
356 //! the solution vector after the last successful steady-state solve (stored
357 //! before grid refinement)
358 vector<double> m_xlast_ss;
359
360 //! the grids for each domain after the last successful steady-state solve
361 //! (stored before grid refinement)
362 vector<vector<double>> m_grid_last_ss;
363
364 //! User-supplied function called after a successful steady-state solve.
366
367private:
368 //! Calls method _finalize in each domain.
369 void finalize();
370};
371
372/**
373 * Create a Sim1D object with a list of domains.
374 * @param domains A vector of shared pointers to the domains to be linked together.
375 * The domain pointers must be entered in left-to-right order --- that is,
376 * the pointer to the leftmost domain is domain[0], the pointer to the
377 * domain to its right is domain[1], etc.
378 * @since New in %Cantera 3.2.
379 */
380shared_ptr<Sim1D> newSim1D(vector<shared_ptr<Domain1D>>& domains);
381
382}
383#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:360
void restoreTimeSteppingSolution()
Set the current solution vector to the last successful time-stepping solution.
Definition Sim1D.cpp:338
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:843
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:143
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:75
double fixedTemperatureLocation()
Return location of the point where temperature is fixed.
Definition Sim1D.cpp:631
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:362
void finalize()
Calls method _finalize in each domain.
Definition Sim1D.cpp:367
void setValue(size_t dom, size_t comp, size_t localPoint, double value)
Set a single value in the solution vector.
Definition Sim1D.cpp:51
void setSteadyCallback(Func1 *callback)
Set a function that will be called after each successful steady-state solve, before regridding.
Definition Sim1D.h:351
int refine(int loglevel=0)
Refine the grid in all domains.
Definition Sim1D.cpp:422
void show()
Show logging information on current solution for all domains.
Definition Sim1D.cpp:327
double fixedTemperature()
Return temperature at the point used to fix the flame location.
Definition Sim1D.cpp:618
void clearDebugFile() override
Deletes a debug_sim1d.yaml file if it exists.
Definition Sim1D.cpp:509
vector< double > m_xlast_ss
the solution vector after the last successful steady-state solve (stored before grid refinement)
Definition Sim1D.h:358
void setMaxGridPoints(int dom, int npoints)
Set the maximum number of grid points in the domain.
Definition Sim1D.cpp:785
int setFixedTemperature(double t)
Add node for fixed temperature point of freely propagating flame.
Definition Sim1D.cpp:528
void getResidual(double rdt, double *resid)
Evaluate the governing equations and return the vector of residuals.
Definition Sim1D.h:227
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:374
void evalSSJacobian()
Evaluate the Jacobian in steady-state mode.
Definition Sim1D.cpp:810
void solveAdjoint(const double *b, double *lambda)
Solve the equation .
Definition Sim1D.cpp:815
AnyMap restore(const string &fname, const string &name)
Retrieve data and settings from a previously saved simulation.
Definition Sim1D.cpp:242
Func1 * m_steady_callback
User-supplied function called after a successful steady-state solve.
Definition Sim1D.h:365
void restoreSteadySolution()
Set the current solution vector and grid to the last successful steady- state solution.
Definition Sim1D.cpp:347
size_t maxGridPoints(size_t dom)
Get the maximum number of grid points in this domain.
Definition Sim1D.cpp:798
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:319
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:514
void setRightControlPoint(double temperature)
Set the right control point location using the specified temperature.
Definition Sim1D.cpp:695
double value(size_t dom, size_t comp, size_t localPoint) const
Get one entry in the solution vector.
Definition Sim1D.cpp:59
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:67
vector< double > getRefineCriteria(int dom)
Get the grid refinement criteria.
Definition Sim1D.cpp:761
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:772
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:747
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:94
void setLeftControlPoint(double temperature)
Set the left control point location using the specified temperature.
Definition Sim1D.cpp:644
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:849