| Cantera 3.0.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
13 namespace 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 */
21 class Sim1D : public OneDim
22{
23 public:
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 //! @deprecated To be removed after %Cantera 3.0;
41 //! superseded by Sim1D() using shared_ptr
42 Sim1D(vector<Domain1D*>& domains);
43
44 //! @name Setting initial values
45 //!
46 //! These methods are used to set the initial values of solution components.
47 //! @{
48
49 //! Set initial guess for one component for all domains
50 /**
51 * @param component component name
52 * @param locs A vector of relative positions, beginning with 0.0 at the
53 * left of the domain, and ending with 1.0 at the right of the domain.
54 * @param vals A vector of values corresponding to the relative position
55 * locations.
56 */
57 void setInitialGuess(const string& component, vector<double>& locs,
58 vector<double>& vals);
59
60 /**
61 * Set a single value in the solution vector.
62 * @param dom domain number, beginning with 0 for the leftmost domain.
63 * @param comp component number
64 * @param localPoint grid point within the domain, beginning with 0 for
65 * the leftmost grid point in the domain.
66 * @param value the value.
67 */
68 void setValue(size_t dom, size_t comp, size_t localPoint, double value);
69
70 /**
71 * Get one entry in the solution vector.
72 * @param dom domain number, beginning with 0 for the leftmost domain.
73 * @param comp component number
74 * @param localPoint grid point within the domain, beginning with 0 for
75 * the leftmost grid point in the domain.
76 */
77 double value(size_t dom, size_t comp, size_t localPoint) const;
78
79 double workValue(size_t dom, size_t comp, size_t localPoint) const;
80
81 /**
82 * Specify a profile for one component of one domain.
83 * @param dom domain number, beginning with 0 for the leftmost domain.
84 * @param comp component number
85 * @param pos A vector of relative positions, beginning with 0.0 at the
86 * left of the domain, and ending with 1.0 at the right of the domain.
87 * @param values A vector of values corresponding to the relative position
88 * locations.
89 *
90 * Note that the vector pos and values can have lengths different than the
91 * number of grid points, but their lengths must be equal. The values at
92 * the grid points will be linearly interpolated based on the (pos,
93 * values) specification.
94 */
95 void setProfile(size_t dom, size_t comp, const vector<double>& pos,
96 const vector<double>& values);
97
98 //! Set component 'comp' of domain 'dom' to value 'v' at all points.
99 void setFlatProfile(size_t dom, size_t comp, double v);
100
101 //! @}
102
103 //! @name Logging, saving and restoring of solutions
104 //!
105 //! @{
106
107 /**
108 * Output information on current solution for all domains to stream.
109 * @param s Output stream
110 * @deprecated To be removed after %Cantera 3.0; replaced by show()
111 */
112 void showSolution(std::ostream& s);
113
114 /**
115 * Show logging information on current solution for all domains.
116 * @deprecated To be removed after %Cantera 3.0; replaced by show()
117 */
118 void showSolution();
119
120 /**
121 * Output information on current solution for all domains to stream.
122 * @param s Output stream
123 * @since New in %Cantera 3.0.
124 */
125 void show(std::ostream& s);
126
127 /**
128 * Show logging information on current solution for all domains.
129 * @since New in %Cantera 3.0.
130 */
131 void show();
132
133 /**
134 * Save the current solution to a container file.
135 * @param fname Name of output container file
136 * @param id Identifier of solution within the container file
137 * @param desc Description of the solution
138 * @param loglevel Level of diagnostic output
139 * @deprecated To be removed after %Cantera 3.0; loglevel is deprecated.
140 */
141 void save(const string& fname, const string& id, const string& desc, int loglevel);
142
143 /**
144 * Save current simulation data to a container file or CSV format.
145 *
146 * In order to save the content of a Sim1D object, individual domains are
147 * converted to SolutionArray objects and saved using the SolutionArray::save()
148 * method. For HDF and YAML output, all domains are written to a single container
149 * file with shared header information. Simulation settings of individual domains
150 * are preserved as meta data of the corresponding SolutionArray objects.
151 * For CSV files, only state and auxiliary data of the main 1D domain are saved.
152 *
153 * The complete state of the current object can be restored from HDF and YAML
154 * container files using the restore() method, while individual domains can be
155 * loaded using SolutionArray::restore() for further analysis. While CSV do not
156 * contain complete information, they can still be used for setting initial states
157 * of individual simulation objects for some %Cantera API's.
158 *
159 * @param fname Name of output file (CSV, YAML or HDF)
160 * @param name Identifier of storage location within the container file; this
161 * node/group contains header information and multiple subgroups holding
162 * domain-specific SolutionArray data (YAML/HDF only)
163 * @param desc Custom comment describing the dataset to be stored (YAML/HDF only)
164 * @param overwrite Force overwrite if file/name exists; optional (default=false)
165 * @param compression Compression level (0-9); optional (default=0; HDF only)
166 * @param basis Output mass ("Y"/"mass") or mole ("X"/"mole") fractions;
167 * if not specified (default=""), the native basis of the underlying
168 * ThermoPhase manager is used - @see nativeState (CSV only)
169 */
170 void save(const string& fname, const string& name, const string& desc,
171 bool overwrite=false, int compression=0, const string& basis="");
172
173 /**
174 * Save the residual of the current solution to a container file.
175 * @param fname Name of output container file
176 * @param id Identifier of solution within the container file
177 * @param desc Description of the solution
178 * @param loglevel Level of diagnostic output
179 * @deprecated To be removed after %Cantera 3.0; loglevel is deprecated.
180 */
181 void saveResidual(const string& fname, const string& id,
182 const string& desc, int loglevel);
183
184 /**
185 * Save the residual of the current solution to a container file.
186 * @param fname Name of output container file
187 * @param name Identifier of solution within the container file
188 * @param desc Description of the solution
189 * @param overwrite Force overwrite if name exists; optional (default=false)
190 * @param compression Compression level (optional; HDF only)
191 */
192 void saveResidual(const string& fname, const string& name,
193 const string& desc, bool overwrite=false, int compression=0);
194
195 /**
196 * Initialize the solution with a previously-saved solution.
197 * @param fname Name of container file
198 * @param id Identifier of solution within the container file
199 * @param loglevel Level of diagnostic output
200 * @deprecated To be removed after %Cantera 3.0; loglevel is deprecated.
201 * @return AnyMap containing header information
202 */
203 AnyMap restore(const string& fname, const string& id, int loglevel);
204
205 /**
206 * Retrieve data and settings from a previously saved simulation.
207 *
208 * This method restores a simulation object from YAML or HDF data previously saved
209 * using the save() method.
210 *
211 * @param fname Name of container file (YAML or HDF)
212 * @param name Identifier of location within the container file; this node/group
213 * contains header information and subgroups with domain-specific SolutionArray
214 * data
215 * @return AnyMap containing header information
216 */
217 AnyMap restore(const string& fname, const string& name);
218
219 //! @}
220
221 //! @deprecated To be removed after %Cantera 3.0 (unused)
222 const double* solution() {
223 warn_deprecated("Sim1D::solution",
224 "This method is unused and will be removed after Cantera 3.0.");
225 return m_state->data();
226 }
227
228 void setTimeStep(double stepsize, size_t n, const int* tsteps);
229
230 void solve(int loglevel = 0, bool refine_grid = true);
231
232 void eval(double rdt=-1.0, int count = 1) {
233 OneDim::eval(npos, m_state->data(), m_xnew.data(), rdt, count);
234 }
235
236 // Evaluate the governing equations and return the vector of residuals
237 void getResidual(double rdt, double* resid) {
238 OneDim::eval(npos, m_state->data(), resid, rdt, 0);
239 }
240
241 //! Refine the grid in all domains.
242 int refine(int loglevel=0);
243
244 //! Add node for fixed temperature point of freely propagating flame
245 int setFixedTemperature(double t);
246
247 //! Return temperature at the point used to fix the flame location
248 double fixedTemperature();
249
250 //! Return location of the point where temperature is fixed
251 double fixedTemperatureLocation();
252
253 /**
254 * Set grid refinement criteria. If dom >= 0, then the settings
255 * apply only to the specified domain. If dom < 0, the settings
256 * are applied to each domain. @see Refiner::setCriteria.
257 */
258 void setRefineCriteria(int dom = -1, double ratio = 10.0,
259 double slope = 0.8, double curve = 0.8,
260 double prune = -0.1);
261
262 /**
263 * Get the grid refinement criteria. dom must be greater than
264 * or equal to zero (that is, the domain must be specified).
265 * @see Refiner::getCriteria
266 */
267 vector<double> getRefineCriteria(int dom);
268
269 /**
270 * Set the maximum number of grid points in the domain. If dom >= 0,
271 * then the settings apply only to the specified domain. If dom < 0,
272 * the settings are applied to each domain. @see Refiner::setMaxPoints.
273 */
274 void setMaxGridPoints(int dom, int npoints);
275
276 /**
277 * Get the maximum number of grid points in this domain. @see Refiner::maxPoints
278 *
279 * @param dom domain number, beginning with 0 for the leftmost domain.
280 */
281 size_t maxGridPoints(size_t dom);
282
283 //! Set the minimum grid spacing in the specified domain(s).
284 /*!
285 * @param dom Domain index. If dom == -1, the specified spacing is applied
286 * to all domains.
287 * @param gridmin The minimum allowable grid spacing [m]
288 */
289 void setGridMin(int dom, double gridmin);
290
291 //! Set the current solution vector to the last successful time-stepping
292 //! solution. This can be used to examine the solver progress after a failed
293 //! integration.
294 void restoreTimeSteppingSolution();
295
296 //! Set the current solution vector and grid to the last successful steady-
297 //! state solution. This can be used to examine the solver progress after a
298 //! failure during grid refinement.
299 void restoreSteadySolution();
300
301 void getInitialSoln();
302
303 //! @deprecated To be removed after %Cantera 3.0 (unused)
304 void setSolution(const double* soln) {
305 warn_deprecated("Sim1D::setSolution",
306 "This method is unused and will be removed after Cantera 3.0.");
307 std::copy(soln, soln + m_state->size(), m_state->data());
308 }
309
310 //! @deprecated To be removed after %Cantera 3.0 (unused)
311 const double* solution() const {
312 warn_deprecated("Sim1D::solution",
313 "This method is unused and will be removed after Cantera 3.0.");
314 return m_state->data();
315 }
316
317 double jacobian(int i, int j);
318
319 void evalSSJacobian();
320
321 //! Solve the equation @f$ J^T \lambda = b @f$.
322 /**
323 * Here, @f$ J = \partial f/\partial x @f$ is the Jacobian matrix of the
324 * system of equations @f$ f(x,p)=0 @f$. This can be used to efficiently
325 * solve for the sensitivities of a scalar objective function @f$ g(x,p) @f$
326 * to a vector of parameters @f$ p @f$ by solving:
327 * @f[ J^T \lambda = \left( \frac{\partial g}{\partial x} \right)^T @f]
328 * for @f$ \lambda @f$ and then computing:
329 * @f[
330 * \left.\frac{dg}{dp}\right|_{f=0} = \frac{\partial g}{\partial p}
331 * - \lambda^T \frac{\partial f}{\partial p}
332 * @f]
333 */
334 void solveAdjoint(const double* b, double* lambda);
335
336 void resize() override;
337
338 //! Set a function that will be called after each successful steady-state
339 //! solve, before regridding. Intended to be used for observing solver
340 //! progress for debugging purposes.
341 void setSteadyCallback(Func1* callback) {
342 m_steady_callback = callback;
343 }
344
345 protected:
346 //! the solution vector after the last successful timestepping
347 vector<double> m_xlast_ts;
348
349 //! the solution vector after the last successful steady-state solve (stored
350 //! before grid refinement)
351 vector<double> m_xlast_ss;
352
353 //! the grids for each domain after the last successful steady-state solve
354 //! (stored before grid refinement)
355 vector<vector<double>> m_grid_last_ss;
356
357 //! a work array used to hold the residual or the new solution
358 vector<double> m_xnew;
359
360 //! timestep
361 double m_tstep;
362
363 //! array of number of steps to take before re-attempting the steady-state
364 //! solution
365 vector<int> m_steps;
366
367 //! User-supplied function called after a successful steady-state solve.
368 Func1* m_steady_callback;
369
370 private:
371 //! Calls method _finalize in each domain.
372 void finalize();
373
374 //! Wrapper around the Newton solver
375 /*!
376 * @return 0 if successful, -1 on failure
377 */
378 int newtonSolve(int loglevel);
379};
380
381}
382#endif
OneDim.h
Cantera::AnyMap
A map of string keys to values whose type can vary at runtime.
Definition AnyMap.h:427
Cantera::Func1
Base class for 'functor' classes that evaluate a function of one variable.
Definition Func1.h:96
Cantera::OneDim
Container class for multiple-domain 1D problems.
Definition OneDim.h:27
Cantera::OneDim::eval
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:292
Cantera::OneDim::rdt
double rdt() const
Reciprocal of the time step.
Definition OneDim.h:160
Cantera::OneDim::component
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:66
Cantera::OneDim::m_state
shared_ptr< vector< double > > m_state
Solution vector.
Definition OneDim.h:342
Cantera::Sim1D
One-dimensional simulations.
Definition Sim1D.h:22
Cantera::Sim1D::restoreTimeSteppingSolution
void restoreTimeSteppingSolution()
Set the current solution vector to the last successful time-stepping solution.
Definition Sim1D.cpp:415
Cantera::Sim1D::resize
void resize() override
Call after one or more grids has changed size, for example after being refined.
Definition Sim1D.cpp:881
Cantera::Sim1D::m_xnew
vector< double > m_xnew
a work array used to hold the residual or the new solution
Definition Sim1D.h:358
Cantera::Sim1D::saveResidual
void saveResidual(const string &fname, const string &id, const string &desc, int loglevel)
Save the residual of the current solution to a container file.
Definition Sim1D.cpp:178
Cantera::Sim1D::setProfile
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:99
Cantera::Sim1D::fixedTemperatureLocation
double fixedTemperatureLocation()
Return location of the point where temperature is fixed.
Definition Sim1D.cpp:777
Cantera::Sim1D::m_grid_last_ss
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:355
Cantera::Sim1D::finalize
void finalize()
Calls method _finalize in each domain.
Definition Sim1D.cpp:444
Cantera::Sim1D::setValue
void setValue(size_t dom, size_t comp, size_t localPoint, double value)
Set a single value in the solution vector.
Definition Sim1D.cpp:75
Cantera::Sim1D::setSteadyCallback
void setSteadyCallback(Func1 *callback)
Set a function that will be called after each successful steady-state solve, before regridding.
Definition Sim1D.h:341
Cantera::Sim1D::refine
int refine(int loglevel=0)
Refine the grid in all domains.
Definition Sim1D.cpp:585
Cantera::Sim1D::show
void show()
Show logging information on current solution for all domains.
Definition Sim1D.cpp:404
Cantera::Sim1D::fixedTemperature
double fixedTemperature()
Return temperature at the point used to fix the flame location.
Definition Sim1D.cpp:764
Cantera::Sim1D::m_xlast_ss
vector< double > m_xlast_ss
the solution vector after the last successful steady-state solve (stored before grid refinement)
Definition Sim1D.h:351
Cantera::Sim1D::save
void save(const string &fname, const string &id, const string &desc, int loglevel)
Save the current solution to a container file.
Definition Sim1D.cpp:118
Cantera::Sim1D::solution
const double * solution() const
Definition Sim1D.h:311
Cantera::Sim1D::setMaxGridPoints
void setMaxGridPoints(int dom, int npoints)
Set the maximum number of grid points in the domain.
Definition Sim1D.cpp:828
Cantera::Sim1D::setFixedTemperature
int setFixedTemperature(double t)
Add node for fixed temperature point of freely propagating flame.
Definition Sim1D.cpp:674
Cantera::Sim1D::restore
AnyMap restore(const string &fname, const string &id, int loglevel)
Initialize the solution with a previously-saved solution.
Definition Sim1D.cpp:289
Cantera::Sim1D::setInitialGuess
void setInitialGuess(const string &component, vector< double > &locs, vector< double > &vals)
Set initial guess for one component for all domains.
Definition Sim1D.cpp:61
Cantera::Sim1D::showSolution
void showSolution()
Show logging information on current solution for all domains.
Definition Sim1D.cpp:388
Cantera::Sim1D::newtonSolve
int newtonSolve(int loglevel)
Wrapper around the Newton solver.
Definition Sim1D.cpp:460
Cantera::Sim1D::m_steps
vector< int > m_steps
array of number of steps to take before re-attempting the steady-state solution
Definition Sim1D.h:365
Cantera::Sim1D::m_tstep
double m_tstep
timestep
Definition Sim1D.h:361
Cantera::Sim1D::solveAdjoint
void solveAdjoint(const double *b, double *lambda)
Solve the equation .
Definition Sim1D.cpp:857
Cantera::Sim1D::m_steady_callback
Func1 * m_steady_callback
User-supplied function called after a successful steady-state solve.
Definition Sim1D.h:368
Cantera::Sim1D::restoreSteadySolution
void restoreSteadySolution()
Set the current solution vector and grid to the last successful steady- state solution.
Definition Sim1D.cpp:424
Cantera::Sim1D::maxGridPoints
size_t maxGridPoints(size_t dom)
Get the maximum number of grid points in this domain.
Definition Sim1D.cpp:841
Cantera::Sim1D::setFlatProfile
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:373
Cantera::Sim1D::value
double value(size_t dom, size_t comp, size_t localPoint) const
Get one entry in the solution vector.
Definition Sim1D.cpp:83
Cantera::Sim1D::setSolution
void setSolution(const double *soln)
Definition Sim1D.h:304
Cantera::Sim1D::getRefineCriteria
vector< double > getRefineCriteria(int dom)
Get the grid refinement criteria.
Definition Sim1D.cpp:804
Cantera::Sim1D::Sim1D
Sim1D()
Default constructor.
Definition Sim1D.h:29
Cantera::Sim1D::setGridMin
void setGridMin(int dom, double gridmin)
Set the minimum grid spacing in the specified domain(s).
Definition Sim1D.cpp:815
Cantera::Sim1D::setRefineCriteria
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:790
Cantera::Sim1D::m_xlast_ts
vector< double > m_xlast_ts
the solution vector after the last successful timestepping
Definition Sim1D.h:347
Cantera::Sim1D::solution
const double * solution()
Definition Sim1D.h:222
Cantera::OneDim::jacobian
MultiJac & jacobian()
Return a reference to the Jacobian evaluator of an OneDim object.
Definition OneDim.cpp:133
Cantera
Namespace for the Cantera kernel.
Definition AnyMap.cpp:564
Cantera::npos
const size_t npos
index returned by functions to indicate "no position"
Definition ct_defs.h:195
Cantera::warn_deprecated
void warn_deprecated(const string &source, const AnyBase &node, const string &message)
A deprecation warning for syntax in an input file.
Definition AnyMap.cpp:1926