Cantera 2.6.0
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 onedim
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 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(std::vector<Domain1D*>& domains);
39
40 /**
41 * @name Setting initial values
42 *
43 * These methods are used to set the initial values of solution components.
44 */
45 //! @{
46
47 /// Set initial guess for one component for all domains
48 /**
49 * @param component component name
50 * @param locs A vector of relative positions, beginning with 0.0 at the
51 * left of the domain, and ending with 1.0 at the right of the domain.
52 * @param vals A vector of values corresponding to the relative position
53 * locations.
54 */
55 void setInitialGuess(const std::string& component, vector_fp& locs,
56 vector_fp& vals);
57
58 /**
59 * Set a single value in the solution vector.
60 * @param dom domain number, beginning with 0 for the leftmost domain.
61 * @param comp component number
62 * @param localPoint grid point within the domain, beginning with 0 for
63 * the leftmost grid point in the domain.
64 * @param value the value.
65 */
66 void setValue(size_t dom, size_t comp, size_t localPoint, doublereal value);
67
68 /**
69 * Get one entry in the solution vector.
70 * @param dom domain number, beginning with 0 for the leftmost domain.
71 * @param comp component number
72 * @param localPoint grid point within the domain, beginning with 0 for
73 * the leftmost grid point in the domain.
74 */
75 doublereal value(size_t dom, size_t comp, size_t localPoint) const;
76
77 doublereal workValue(size_t dom, size_t comp, size_t localPoint) const;
78
79 /**
80 * Specify a profile for one component of one domain.
81 * @param dom domain number, beginning with 0 for the leftmost domain.
82 * @param comp component number
83 * @param pos A vector of relative positions, beginning with 0.0 at the
84 * left of the domain, and ending with 1.0 at the right of the domain.
85 * @param values A vector of values corresponding to the relative position
86 * locations.
87 *
88 * Note that the vector pos and values can have lengths different than the
89 * number of grid points, but their lengths must be equal. The values at
90 * the grid points will be linearly interpolated based on the (pos,
91 * values) specification.
92 */
93 void setProfile(size_t dom, size_t comp, const vector_fp& pos,
94 const vector_fp& values);
95
96 /// Set component 'comp' of domain 'dom' to value 'v' at all points.
97 void setFlatProfile(size_t dom, size_t comp, doublereal v);
98
99 //! @}
100
101 void save(const std::string& fname, const std::string& id,
102 const std::string& desc, int loglevel=1);
103
104 void saveResidual(const std::string& fname, const std::string& id,
105 const std::string& desc, int loglevel=1);
106
107 /// Print to stream s the current solution for all domains.
108 void showSolution(std::ostream& s);
109 void showSolution();
110
111 const doublereal* solution() {
112 return m_x.data();
113 }
114
115 void setTimeStep(double stepsize, size_t n, const int* tsteps);
116
117 void solve(int loglevel = 0, bool refine_grid = true);
118
119 void eval(doublereal rdt=-1.0, int count = 1) {
120 OneDim::eval(npos, m_x.data(), m_xnew.data(), rdt, count);
121 }
122
123 // Evaluate the governing equations and return the vector of residuals
124 void getResidual(double rdt, double* resid) {
125 OneDim::eval(npos, m_x.data(), resid, rdt, 0);
126 }
127
128 /// Refine the grid in all domains.
129 int refine(int loglevel=0);
130
131 //! Add node for fixed temperature point of freely propagating flame
132 int setFixedTemperature(double t);
133
134 //! Return temperature at the point used to fix the flame location
135 double fixedTemperature();
136
137 //! Return location of the point where temperature is fixed
139
140 /**
141 * Set grid refinement criteria. If dom >= 0, then the settings
142 * apply only to the specified domain. If dom < 0, the settings
143 * are applied to each domain. @see Refiner::setCriteria.
144 */
145 void setRefineCriteria(int dom = -1, double ratio = 10.0,
146 double slope = 0.8, double curve = 0.8,
147 double prune = -0.1);
148
149 /**
150 * Get the grid refinement criteria. dom must be greater than
151 * or equal to zero (that is, the domain must be specified).
152 * @see Refiner::getCriteria
153 */
155
156 /**
157 * Set the maximum number of grid points in the domain. If dom >= 0,
158 * then the settings apply only to the specified domain. If dom < 0,
159 * the settings are applied to each domain. @see Refiner::setMaxPoints.
160 */
161 void setMaxGridPoints(int dom, int npoints);
162
163 /**
164 * Get the maximum number of grid points in this domain. @see Refiner::maxPoints
165 *
166 * @param dom domain number, beginning with 0 for the leftmost domain.
167 */
168 size_t maxGridPoints(size_t dom);
169
170 //! Set the minimum grid spacing in the specified domain(s).
171 /*!
172 * @param dom Domain index. If dom == -1, the specified spacing is applied
173 * to all domains.
174 * @param gridmin The minimum allowable grid spacing [m]
175 */
176 void setGridMin(int dom, double gridmin);
177
178 //! Initialize the solution with a previously-saved solution.
179 void restore(const std::string& fname, const std::string& id, int loglevel=2);
180
181 //! Set the current solution vector to the last successful time-stepping
182 //! solution. This can be used to examine the solver progress after a failed
183 //! integration.
185
186 //! Set the current solution vector and grid to the last successful steady-
187 //! state solution. This can be used to examine the solver progress after a
188 //! failure during grid refinement.
190
191 void getInitialSoln();
192
193 void setSolution(const doublereal* soln) {
194 std::copy(soln, soln + m_x.size(), m_x.data());
195 }
196
197 const doublereal* solution() const {
198 return m_x.data();
199 }
200
201 doublereal jacobian(int i, int j);
202
203 void evalSSJacobian();
204
205 //! Solve the equation \f$ J^T \lambda = b \f$.
206 /**
207 * Here, \f$ J = \partial f/\partial x \f$ is the Jacobian matrix of the
208 * system of equations \f$ f(x,p)=0 \f$. This can be used to efficiently
209 * solve for the sensitivities of a scalar objective function \f$ g(x,p) \f$
210 * to a vector of parameters \f$ p \f$ by solving:
211 * \f[ J^T \lambda = \left( \frac{\partial g}{\partial x} \right)^T \f]
212 * for \f$ \lambda \f$ and then computing:
213 * \f[
214 * \left.\frac{dg}{dp}\right|_{f=0} = \frac{\partial g}{\partial p}
215 * - \lambda^T \frac{\partial f}{\partial p}
216 * \f]
217 */
218 void solveAdjoint(const double* b, double* lambda);
219
220 virtual void resize();
221
222 //! Set a function that will be called after each successful steady-state
223 //! solve, before regridding. Intended to be used for observing solver
224 //! progress for debugging purposes.
225 void setSteadyCallback(Func1* callback) {
226 m_steady_callback = callback;
227 }
228
229protected:
230 //! the solution vector
232
233 //! the solution vector after the last successful timestepping
235
236 //! the solution vector after the last successful steady-state solve (stored
237 //! before grid refinement)
239
240 //! the grids for each domain after the last successful steady-state solve
241 //! (stored before grid refinement)
242 std::vector<vector_fp> m_grid_last_ss;
243
244 //! a work array used to hold the residual or the new solution
246
247 //! timestep
248 doublereal m_tstep;
249
250 //! array of number of steps to take before re-attempting the steady-state
251 //! solution
253
254 //! User-supplied function called after a successful steady-state solve.
256
257private:
258 /// Calls method _finalize in each domain.
259 void finalize();
260
261 //! Wrapper around the Newton solver
262 /*!
263 * @return 0 if successful, -1 on failure
264 */
265 int newtonSolve(int loglevel);
266};
267
268}
269#endif
Base class for 'functor' classes that evaluate a function of one variable.
Definition: Func1.h:44
Container class for multiple-domain 1D problems.
Definition: OneDim.h:27
std::tuple< std::string, size_t, std::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
void eval(size_t j, double *x, double *r, doublereal rdt=-1.0, int count=1)
Evaluate the multi-domain residual function.
Definition: OneDim.cpp:259
MultiJac & jacobian()
Return a reference to the Jacobian evaluator.
Definition: OneDim.cpp:102
doublereal rdt() const
Reciprocal of the time step.
Definition: OneDim.h:151
One-dimensional simulations.
Definition: Sim1D.h:22
vector_fp m_xlast_ss
the solution vector after the last successful steady-state solve (stored before grid refinement)
Definition: Sim1D.h:238
virtual void resize()
Call after one or more grids has changed size, for example after being refined.
Definition: Sim1D.cpp:714
void restoreTimeSteppingSolution()
Set the current solution vector to the last successful time-stepping solution.
Definition: Sim1D.cpp:248
vector_fp m_x
the solution vector
Definition: Sim1D.h:231
double fixedTemperatureLocation()
Return location of the point where temperature is fixed.
Definition: Sim1D.cpp:610
void finalize()
Calls method _finalize in each domain.
Definition: Sim1D.cpp:277
vector_int m_steps
array of number of steps to take before re-attempting the steady-state solution
Definition: Sim1D.h:252
void setSteadyCallback(Func1 *callback)
Set a function that will be called after each successful steady-state solve, before regridding.
Definition: Sim1D.h:225
int refine(int loglevel=0)
Refine the grid in all domains.
Definition: Sim1D.cpp:421
double fixedTemperature()
Return temperature at the point used to fix the flame location.
Definition: Sim1D.cpp:597
vector_fp m_xlast_ts
the solution vector after the last successful timestepping
Definition: Sim1D.h:234
vector_fp m_xnew
a work array used to hold the residual or the new solution
Definition: Sim1D.h:245
void setProfile(size_t dom, size_t comp, const vector_fp &pos, const vector_fp &values)
Specify a profile for one component of one domain.
Definition: Sim1D.cpp:78
vector_fp getRefineCriteria(int dom)
Get the grid refinement criteria.
Definition: Sim1D.cpp:637
void setMaxGridPoints(int dom, int npoints)
Set the maximum number of grid points in the domain.
Definition: Sim1D.cpp:661
int setFixedTemperature(double t)
Add node for fixed temperature point of freely propagating flame.
Definition: Sim1D.cpp:507
int newtonSolve(int loglevel)
Wrapper around the Newton solver.
Definition: Sim1D.cpp:293
std::vector< vector_fp > m_grid_last_ss
the grids for each domain after the last successful steady-state solve (stored before grid refinement...
Definition: Sim1D.h:242
void restore(const std::string &fname, const std::string &id, int loglevel=2)
Initialize the solution with a previously-saved solution.
Definition: Sim1D.cpp:162
void solveAdjoint(const double *b, double *lambda)
Solve the equation .
Definition: Sim1D.cpp:690
Func1 * m_steady_callback
User-supplied function called after a successful steady-state solve.
Definition: Sim1D.h:255
void restoreSteadySolution()
Set the current solution vector and grid to the last successful steady- state solution.
Definition: Sim1D.cpp:257
size_t maxGridPoints(size_t dom)
Get the maximum number of grid points in this domain.
Definition: Sim1D.cpp:674
doublereal value(size_t dom, size_t comp, size_t localPoint) const
Get one entry in the solution vector.
Definition: Sim1D.cpp:62
void setValue(size_t dom, size_t comp, size_t localPoint, doublereal value)
Set a single value in the solution vector.
Definition: Sim1D.cpp:54
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:648
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:623
void setFlatProfile(size_t dom, size_t comp, doublereal v)
Set component 'comp' of domain 'dom' to value 'v' at all points.
Definition: Sim1D.cpp:220
doublereal m_tstep
timestep
Definition: Sim1D.h:248
void setInitialGuess(const std::string &component, vector_fp &locs, vector_fp &vals)
Set initial guess for one component for all domains.
Definition: Sim1D.cpp:41
Namespace for the Cantera kernel.
Definition: AnyMap.h:29
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:192
std::vector< int > vector_int
Vector of ints.
Definition: ct_defs.h:186
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Definition: ct_defs.h:184