Cantera  3.2.0a4
Loading...
Searching...
No Matches
Domain1D.cpp
Go to the documentation of this file.
1/**
2 * @file Domain1D.cpp
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
10#include "cantera/oneD/refine.h"
11#include "cantera/base/AnyMap.h"
15
16namespace Cantera
17{
18
19Domain1D::Domain1D(size_t nv, size_t points, double time)
20{
21 resize(nv, points);
22}
23
24Domain1D::~Domain1D()
25{
26 if (m_solution) {
27 m_solution->thermo()->removeSpeciesLock();
28 }
29}
30
31void Domain1D::setSolution(shared_ptr<Solution> sol)
32{
33 if (!sol || !(sol->thermo())) {
34 throw CanteraError("Domain1D::setSolution",
35 "Missing or incomplete Solution object.");
36 }
37 if (m_solution) {
38 m_solution->thermo()->removeSpeciesLock();
39 }
40 m_solution = sol;
41 m_solution->thermo()->addSpeciesLock();
42}
43
44void Domain1D::resize(size_t nv, size_t np)
45{
46 // if the number of components is being changed, then a
47 // new grid refiner is required.
48 if (nv != m_nv || !m_refiner) {
49 m_nv = nv;
50 m_refiner = make_unique<Refiner>(*this);
51 }
52 m_nv = nv;
53 m_name.resize(m_nv,"");
54 m_max.resize(m_nv, 0.0);
55 m_min.resize(m_nv, 0.0);
56 // Default error tolerances for all domains
57 m_rtol_ss.resize(m_nv, 1.0e-4);
58 m_atol_ss.resize(m_nv, 1.0e-9);
59 m_rtol_ts.resize(m_nv, 1.0e-4);
60 m_atol_ts.resize(m_nv, 1.0e-11);
61 m_points = np;
62 m_z.resize(np, 0.0);
63 m_slast.resize(m_nv * m_points, 0.0);
64 locate();
65}
66
67string Domain1D::componentName(size_t n) const
68{
69 if (m_name[n] != "") {
70 return m_name[n];
71 } else {
72 return fmt::format("component {}", n);
73 }
74}
75
76size_t Domain1D::componentIndex(const string& name, bool checkAlias) const
77{
78 for (size_t n = 0; n < nComponents(); n++) {
79 if (name == componentName(n)) {
80 return n;
81 }
82 }
83 throw CanteraError("Domain1D::componentIndex",
84 "no component named '{}'", name);
85}
86
87bool Domain1D::hasComponent(const string& name, bool checkAlias) const
88{
89 for (size_t n = 0; n < nComponents(); n++) {
90 if (name == componentName(n)) {
91 return true;
92 }
93 }
94 throw CanteraError("Domain1D::hasComponent",
95 "no component named '{}'", name);
96}
97
98void Domain1D::setTransientTolerances(double rtol, double atol, size_t n)
99{
100 if (n == npos) {
101 for (n = 0; n < m_nv; n++) {
102 m_rtol_ts[n] = rtol;
103 m_atol_ts[n] = atol;
104 }
105 } else {
106 m_rtol_ts[n] = rtol;
107 m_atol_ts[n] = atol;
108 }
109}
110
111void Domain1D::setSteadyTolerances(double rtol, double atol, size_t n)
112{
113 if (n == npos) {
114 for (n = 0; n < m_nv; n++) {
115 m_rtol_ss[n] = rtol;
116 m_atol_ss[n] = atol;
117 }
118 } else {
119 m_rtol_ss[n] = rtol;
120 m_atol_ss[n] = atol;
121 }
122}
123
125{
126 if (m_container) {
127 m_container->linearSolver()->setAge(10000);
129 }
130}
131
133{
134 auto wrap_tols = [this](const vector<double>& tols) {
135 // If all tolerances are the same, just store the scalar value.
136 // Otherwise, store them by component name
137 set<double> unique_tols(tols.begin(), tols.end());
138 if (unique_tols.size() == 1) {
139 return AnyValue(tols[0]);
140 } else {
141 AnyMap out;
142 for (size_t i = 0; i < tols.size(); i++) {
143 out[componentName(i)] = tols[i];
144 }
145 return AnyValue(out);
146 }
147 };
148 AnyMap state;
149 state["type"] = type();
150 state["points"] = static_cast<long int>(nPoints());
151 if (nComponents() && nPoints()) {
152 state["tolerances"]["transient-abstol"] = wrap_tols(m_atol_ts);
153 state["tolerances"]["steady-abstol"] = wrap_tols(m_atol_ss);
154 state["tolerances"]["transient-reltol"] = wrap_tols(m_rtol_ts);
155 state["tolerances"]["steady-reltol"] = wrap_tols(m_rtol_ss);
156 }
157 return state;
158}
159
160void Domain1D::setMeta(const AnyMap& meta)
161{
162 auto set_tols = [&](const AnyValue& tols, const string& which, vector<double>& out)
163 {
164 if (!tols.hasKey(which)) {
165 return;
166 }
167 const auto& tol = tols[which];
168 if (tol.isScalar()) {
169 out.assign(nComponents(), tol.asDouble());
170 } else {
171 for (size_t i = 0; i < nComponents(); i++) {
172 string name = componentName(i);
173 if (tol.hasKey(name)) {
174 out[i] = tol[name].asDouble();
175 } else {
176 warn_user("Domain1D::setMeta", "No {} found for component '{}'",
177 which, name);
178 }
179 }
180 }
181 };
182
183 if (meta.hasKey("tolerances")) {
184 const auto& tols = meta["tolerances"];
185 set_tols(tols, "transient-abstol", m_atol_ts);
186 set_tols(tols, "transient-reltol", m_rtol_ts);
187 set_tols(tols, "steady-abstol", m_atol_ss);
188 set_tols(tols, "steady-reltol", m_rtol_ss);
189 }
190}
191
193{
194 if (m_left) {
195 // there is a domain on the left, so the first grid point in this domain
196 // is one more than the last one on the left
197 m_jstart = m_left->lastPoint() + 1;
198
199 // the starting location in the solution vector
200 m_iloc = m_left->loc() + m_left->size();
201 } else {
202 // this is the left-most domain
203 m_jstart = 0;
204 m_iloc = 0;
205 }
206 // if there is a domain to the right of this one, then repeat this for it
207 if (m_right) {
208 m_right->locate();
209 }
210}
211
212void Domain1D::setupGrid(const vector<double>& grid)
213{
214 setupGrid(grid.size(), grid.data());
215}
216
217void Domain1D::setupGrid(size_t n, const double* z)
218{
219 if (n > 1) {
220 resize(m_nv, n);
221 for (size_t j = 0; j < m_points; j++) {
222 m_z[j] = z[j];
223 }
224 }
225}
226
227void Domain1D::setupUniformGrid(size_t points, double length, double start)
228{
229 vector<double> grid(points);
230 double dz = length / (double)(points - 1);
231 for (int iz = 0; iz < points; iz++) {
232 grid[iz] = start + iz * dz;
233 }
235}
236
237void Domain1D::show(const double* x)
238{
239 size_t nn = m_nv/5;
240 for (size_t i = 0; i < nn; i++) {
241 writeline('-', 79, false, true);
242 writelog("\n z ");
243 for (size_t n = 0; n < 5; n++) {
244 writelog(" {:>10s} ", componentName(i*5 + n));
245 }
246 writeline('-', 79, false, true);
247 for (size_t j = 0; j < m_points; j++) {
248 writelog("\n {:10.4g} ", m_z[j]);
249 for (size_t n = 0; n < 5; n++) {
250 writelog(" {:10.4g} ", x[index(i*5 + n, j)]);
251 }
252 }
253 writelog("\n");
254 }
255 size_t nrem = m_nv - 5*nn;
256 writeline('-', 79, false, true);
257 writelog("\n z ");
258 for (size_t n = 0; n < nrem; n++) {
259 writelog(" {:>10s} ", componentName(nn*5 + n));
260 }
261 writeline('-', 79, false, true);
262 for (size_t j = 0; j < m_points; j++) {
263 writelog("\n {:10.4g} ", m_z[j]);
264 for (size_t n = 0; n < nrem; n++) {
265 writelog(" {:10.4g} ", x[index(nn*5 + n, j)]);
266 }
267 }
268 writelog("\n");
269}
270
271void Domain1D::setProfile(const string& name, double* values, double* soln)
272{
274 "Domain1D::setProfile", "To be removed after Cantera 3.2. Replaceable by "
275 "version using vector arguments.");
276 for (size_t n = 0; n < m_nv; n++) {
277 if (name == componentName(n)) {
278 for (size_t j = 0; j < m_points; j++) {
279 soln[index(n, j) + m_iloc] = values[j];
280 }
281 return;
282 }
283 }
284 throw CanteraError("Domain1D::setProfile", "unknown component: "+name);
285}
286
288{
289 for (size_t j = 0; j < m_points; j++) {
290 for (size_t n = 0; n < m_nv; n++) {
291 x[index(n,j)] = initialValue(n,j);
292 }
293 }
294}
295
296double Domain1D::initialValue(size_t n, size_t j)
297{
298 throw NotImplementedError("Domain1D::initialValue");
299}
300
301} // namespace
Header file for class ThermoPhase, the base class for phases with thermodynamic properties,...
A map of string keys to values whose type can vary at runtime.
Definition AnyMap.h:431
bool hasKey(const string &key) const
Returns true if the map contains an item named key.
Definition AnyMap.cpp:1477
A wrapper for a variable whose type is determined at runtime.
Definition AnyMap.h:88
bool hasKey(const string &key) const
Returns true if this AnyValue is an AnyMap and that map contains a key with the given name.
Definition AnyMap.cpp:660
Base class for exceptions thrown by Cantera classes.
void setTransientTolerances(double rtol, double atol, size_t n=npos)
Set tolerances for time-stepping mode.
Definition Domain1D.cpp:98
size_t lastPoint() const
The index of the last (that is, right-most) grid point belonging to this domain.
Definition Domain1D.h:595
size_t m_iloc
Starting location within the solution vector for unknowns that correspond to this domain.
Definition Domain1D.h:773
shared_ptr< Solution > m_solution
Composite thermo/kinetics/transport handler.
Definition Domain1D.h:790
Domain1D * m_left
Pointer to the domain to the left.
Definition Domain1D.h:779
OneDim * m_container
Parent OneDim simulation containing this and adjacent domains.
Definition Domain1D.h:764
size_t nComponents() const
Number of components at each grid point.
Definition Domain1D.h:148
double rtol(size_t n)
Relative tolerance of the nth component.
Definition Domain1D.h:268
vector< double > m_atol_ss
Absolute tolerances for steady mode.
Definition Domain1D.h:759
void setupUniformGrid(size_t points, double length, double start=0.)
Set up uniform grid.
Definition Domain1D.cpp:227
vector< double > m_rtol_ts
Relative tolerances for transient mode.
Definition Domain1D.h:758
size_t size() const
Return the size of the solution vector (the product of m_nv and m_points).
Definition Domain1D.h:574
vector< double > m_atol_ts
Absolute tolerances for transient mode.
Definition Domain1D.h:760
virtual void setMeta(const AnyMap &meta)
Retrieve meta data.
Definition Domain1D.cpp:160
void setupGrid(const vector< double > &grid)
Set up initial grid.
Definition Domain1D.cpp:212
vector< double > & grid()
Access the array of grid coordinates [m].
Definition Domain1D.h:682
size_t m_jstart
Index of the first point in this domain in the global point list.
Definition Domain1D.h:777
size_t m_nv
Number of solution components.
Definition Domain1D.h:752
size_t nPoints() const
Number of grid points in this domain.
Definition Domain1D.h:176
vector< string > m_name
Names of solution components.
Definition Domain1D.h:785
virtual size_t componentIndex(const string &name, bool checkAlias=true) const
Index of component with name name.
Definition Domain1D.cpp:76
vector< double > values(const string &component) const
Retrieve component values.
Definition Domain1D.h:419
virtual void resize(size_t nv, size_t np)
Resize the domain to have nv components and np grid points.
Definition Domain1D.cpp:44
double z(size_t jlocal) const
Get the coordinate [m] of the point with local index jlocal
Definition Domain1D.h:659
virtual void show(const double *x)
Print the solution.
Definition Domain1D.cpp:237
vector< double > m_rtol_ss
Relative tolerances for steady mode.
Definition Domain1D.h:757
vector< double > m_slast
Solution vector at the last time step.
Definition Domain1D.h:754
Domain1D * m_right
Pointer to the domain to the right.
Definition Domain1D.h:780
void setSteadyTolerances(double rtol, double atol, size_t n=npos)
Set tolerances for steady-state mode.
Definition Domain1D.cpp:111
virtual string componentName(size_t n) const
Name of component n. May be overloaded.
Definition Domain1D.cpp:67
void setSolution(shared_ptr< Solution > sol)
Set the solution manager.
Definition Domain1D.cpp:31
double atol(size_t n)
Absolute tolerance of the nth component.
Definition Domain1D.h:273
vector< double > m_z
1D spatial grid coordinates
Definition Domain1D.h:761
size_t m_points
Number of grid points.
Definition Domain1D.h:753
string type() const
String indicating the domain implemented.
Definition Domain1D.h:50
vector< double > m_max
Upper bounds on solution components.
Definition Domain1D.h:755
unique_ptr< Refiner > m_refiner
Refiner object used for placing grid points.
Definition Domain1D.h:784
vector< double > m_min
Lower bounds on solution components.
Definition Domain1D.h:756
virtual void setProfile(const string &component, const vector< double > &pos, const vector< double > &values)
Specify a profile for a component.
Definition Domain1D.h:494
virtual double initialValue(size_t n, size_t j)
Initial value of solution component n at grid point j.
Definition Domain1D.cpp:296
void needJacUpdate()
Set this if something has changed in the governing equations (for example, the value of a constant ha...
Definition Domain1D.cpp:124
virtual void _getInitialSoln(double *x)
Writes some or all initial solution values into the global solution array, beginning at the location ...
Definition Domain1D.cpp:287
Domain1D(size_t nv=1, size_t points=1, double time=0.0)
Constructor.
Definition Domain1D.cpp:19
size_t index(size_t n, size_t j) const
Returns the index of the solution vector, which corresponds to component n at grid point j.
Definition Domain1D.h:368
virtual size_t loc(size_t j=0) const
Location of the start of the local solution vector in the global solution vector.
Definition Domain1D.h:585
void locate()
Find the index of the first grid point in this domain, and the start of its variables in the global s...
Definition Domain1D.cpp:192
virtual AnyMap getMeta() const
Retrieve meta data.
Definition Domain1D.cpp:132
virtual bool hasComponent(const string &name, bool checkAlias=true) const
Check whether the Domain contains a component.
Definition Domain1D.cpp:87
An error indicating that an unimplemented function has been called.
void saveStats()
Save statistics on function and Jacobian evaluation, and reset the counters.
Definition OneDim.cpp:155
shared_ptr< SystemJacobian > linearSolver() const
Get the the linear solver being used to hold the Jacobian matrix and solve linear systems as part of ...
void writelog(const string &fmt, const Args &... args)
Write a formatted message to the screen.
Definition global.h:176
void warn_user(const string &method, const string &msg, const Args &... args)
Print a user warning raised from method as CanteraWarning.
Definition global.h:268
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
void warn_deprecated(const string &source, const AnyBase &node, const string &message)
A deprecation warning for syntax in an input file.
Definition AnyMap.cpp:1997