Cantera  3.2.0
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 warn_deprecated("Domain1D::setSolution",
38 "After Cantera 3.2, a change of domain contents after instantiation "
39 "will be disabled.");
40 if (m_solution) {
41 m_solution->thermo()->removeSpeciesLock();
42 }
43 m_solution = sol;
44 m_solution->thermo()->addSpeciesLock();
45}
46
47string Domain1D::info(const vector<string>& keys, int rows, int width)
48{
49 return toArray()->info(keys, rows, width);
50}
51
52void Domain1D::resize(size_t nv, size_t np)
53{
54 // if the number of components is being changed, then a
55 // new grid refiner is required.
56 if (nv != m_nv || !m_refiner) {
57 m_nv = nv;
58 m_refiner = make_unique<Refiner>(*this);
59 }
60 m_nv = nv;
61 m_name.resize(m_nv,"");
62 m_max.resize(m_nv, 0.0);
63 m_min.resize(m_nv, 0.0);
64 // Default error tolerances for all domains
65 m_rtol_ss.resize(m_nv, 1.0e-4);
66 m_atol_ss.resize(m_nv, 1.0e-9);
67 m_rtol_ts.resize(m_nv, 1.0e-4);
68 m_atol_ts.resize(m_nv, 1.0e-11);
69 m_points = np;
70 m_z.resize(np, 0.0);
71 m_slast.resize(m_nv * m_points, 0.0);
72 locate();
73}
74
75string Domain1D::componentName(size_t n) const
76{
77 if (n < m_nv) {
78 if (m_name[n] != "") {
79 return m_name[n];
80 } else {
81 return fmt::format("component {}", n);
82 }
83 }
84 throw IndexError("Domain1D::componentName", "component", n, m_nv);
85}
86
87size_t Domain1D::componentIndex(const string& name, bool checkAlias) const
88{
89 for (size_t n = 0; n < m_nv; n++) {
90 if (name == componentName(n)) {
91 return n;
92 }
93 }
94 throw CanteraError("Domain1D::componentIndex",
95 "Component '{}' not found", name);
96}
97
98bool Domain1D::hasComponent(const string& name, bool checkAlias) const
99{
100 for (size_t n = 0; n < m_nv; n++) {
101 if (name == componentName(n)) {
102 return true;
103 }
104 }
105 throw CanteraError("Domain1D::hasComponent",
106 "Component '{}' not found", name);
107}
108
109void Domain1D::setTransientTolerances(double rtol, double atol, size_t n)
110{
111 if (n == npos) {
112 for (n = 0; n < m_nv; n++) {
113 m_rtol_ts[n] = rtol;
114 m_atol_ts[n] = atol;
115 }
116 } else {
117 m_rtol_ts[n] = rtol;
118 m_atol_ts[n] = atol;
119 }
120}
121
122void Domain1D::setSteadyTolerances(double rtol, double atol, size_t n)
123{
124 if (n == npos) {
125 for (n = 0; n < m_nv; n++) {
126 m_rtol_ss[n] = rtol;
127 m_atol_ss[n] = atol;
128 }
129 } else {
130 m_rtol_ss[n] = rtol;
131 m_atol_ss[n] = atol;
132 }
133}
134
136{
137 if (m_container) {
138 m_container->linearSolver()->setAge(10000);
139 m_container->saveStats();
140 }
141}
142
144{
145 auto wrap_tols = [this](const vector<double>& tols) {
146 // If all tolerances are the same, just store the scalar value.
147 // Otherwise, store them by component name
148 set<double> unique_tols(tols.begin(), tols.end());
149 if (unique_tols.size() == 1) {
150 return AnyValue(tols[0]);
151 } else {
152 AnyMap out;
153 for (size_t i = 0; i < tols.size(); i++) {
154 out[componentName(i)] = tols[i];
155 }
156 return AnyValue(out);
157 }
158 };
159 AnyMap state;
160 state["type"] = type();
161 state["points"] = static_cast<long int>(nPoints());
162 if (nComponents() && nPoints()) {
163 state["tolerances"]["transient-abstol"] = wrap_tols(m_atol_ts);
164 state["tolerances"]["steady-abstol"] = wrap_tols(m_atol_ss);
165 state["tolerances"]["transient-reltol"] = wrap_tols(m_rtol_ts);
166 state["tolerances"]["steady-reltol"] = wrap_tols(m_rtol_ss);
167 }
168 return state;
169}
170
171void Domain1D::setMeta(const AnyMap& meta)
172{
173 auto set_tols = [&](const AnyValue& tols, const string& which, vector<double>& out)
174 {
175 if (!tols.hasKey(which)) {
176 return;
177 }
178 const auto& tol = tols[which];
179 if (tol.isScalar()) {
180 out.assign(nComponents(), tol.asDouble());
181 } else {
182 for (size_t i = 0; i < nComponents(); i++) {
183 string name = componentName(i);
184 if (tol.hasKey(name)) {
185 out[i] = tol[name].asDouble();
186 } else {
187 warn_user("Domain1D::setMeta", "No {} found for component '{}'",
188 which, name);
189 }
190 }
191 }
192 };
193
194 if (meta.hasKey("tolerances")) {
195 const auto& tols = meta["tolerances"];
196 set_tols(tols, "transient-abstol", m_atol_ts);
197 set_tols(tols, "transient-reltol", m_rtol_ts);
198 set_tols(tols, "steady-abstol", m_atol_ss);
199 set_tols(tols, "steady-reltol", m_rtol_ss);
200 }
201}
202
204{
205 if (m_left) {
206 // there is a domain on the left, so the first grid point in this domain
207 // is one more than the last one on the left
208 m_jstart = m_left->lastPoint() + 1;
209
210 // the starting location in the solution vector
211 m_iloc = m_left->loc() + m_left->size();
212 } else {
213 // this is the left-most domain
214 m_jstart = 0;
215 m_iloc = 0;
216 }
217 // if there is a domain to the right of this one, then repeat this for it
218 if (m_right) {
219 m_right->locate();
220 }
221}
222
223void Domain1D::setupGrid(const vector<double>& grid)
224{
225 setupGrid(grid.size(), grid.data());
226}
227
228void Domain1D::setupGrid(size_t n, const double* z)
229{
230 if (n > 1) {
231 resize(m_nv, n);
232 for (size_t j = 0; j < m_points; j++) {
233 m_z[j] = z[j];
234 }
235 }
236}
237
238void Domain1D::setupUniformGrid(size_t points, double length, double start)
239{
240 vector<double> grid(points);
241 double dz = length / static_cast<double>(points - 1);
242 for (size_t iz = 0; iz < points; iz++) {
243 grid[iz] = start + iz * dz;
244 }
246}
247
248void Domain1D::show(const double* x)
249{
250 size_t nn = m_nv/5;
251 for (size_t i = 0; i < nn; i++) {
252 writeline('-', 79, false, true);
253 writelog("\n z ");
254 for (size_t n = 0; n < 5; n++) {
255 writelog(" {:>10s} ", componentName(i*5 + n));
256 }
257 writeline('-', 79, false, true);
258 for (size_t j = 0; j < m_points; j++) {
259 writelog("\n {:10.4g} ", m_z[j]);
260 for (size_t n = 0; n < 5; n++) {
261 writelog(" {:10.4g} ", x[index(i*5 + n, j)]);
262 }
263 }
264 writelog("\n");
265 }
266 size_t nrem = m_nv - 5*nn;
267 writeline('-', 79, false, true);
268 writelog("\n z ");
269 for (size_t n = 0; n < nrem; n++) {
270 writelog(" {:>10s} ", componentName(nn*5 + n));
271 }
272 writeline('-', 79, false, true);
273 for (size_t j = 0; j < m_points; j++) {
274 writelog("\n {:10.4g} ", m_z[j]);
275 for (size_t n = 0; n < nrem; n++) {
276 writelog(" {:10.4g} ", x[index(nn*5 + n, j)]);
277 }
278 }
279 writelog("\n");
280}
281
282void Domain1D::setProfile(const string& name, double* values, double* soln)
283{
285 "Domain1D::setProfile", "To be removed after Cantera 3.2. Replaceable by "
286 "version using vector arguments.");
287 for (size_t n = 0; n < m_nv; n++) {
288 if (name == componentName(n)) {
289 for (size_t j = 0; j < m_points; j++) {
290 soln[index(n, j) + m_iloc] = values[j];
291 }
292 return;
293 }
294 }
295 throw CanteraError("Domain1D::setProfile", "unknown component: "+name);
296}
297
298void Domain1D::setRefineCriteria(double ratio, double slope, double curve, double prune)
299{
300 m_refiner->setCriteria(ratio, slope, curve, prune);
301}
302
304{
305 return m_refiner->getCriteria();
306}
307
309{
310 for (size_t j = 0; j < m_points; j++) {
311 for (size_t n = 0; n < m_nv; n++) {
312 x[index(n,j)] = initialValue(n,j);
313 }
314 }
315}
316
317double Domain1D::initialValue(size_t n, size_t j)
318{
319 throw NotImplementedError("Domain1D::initialValue");
320}
321
322} // 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:109
size_t m_iloc
Starting location within the solution vector for unknowns that correspond to this domain.
Definition Domain1D.h:847
shared_ptr< Solution > m_solution
Composite thermo/kinetics/transport handler.
Definition Domain1D.h:864
Domain1D * m_left
Pointer to the domain to the left.
Definition Domain1D.h:853
OneDim * m_container
Parent OneDim simulation containing this and adjacent domains.
Definition Domain1D.h:838
size_t nComponents() const
Number of components at each grid point.
Definition Domain1D.h:174
double rtol(size_t n)
Relative tolerance of the nth component.
Definition Domain1D.h:294
vector< double > m_atol_ss
Absolute tolerances for steady mode.
Definition Domain1D.h:833
void setupUniformGrid(size_t points, double length, double start=0.)
Set up uniform grid.
Definition Domain1D.cpp:238
vector< double > m_rtol_ts
Relative tolerances for transient mode.
Definition Domain1D.h:832
vector< double > m_atol_ts
Absolute tolerances for transient mode.
Definition Domain1D.h:834
virtual void setMeta(const AnyMap &meta)
Retrieve meta data.
Definition Domain1D.cpp:171
void setupGrid(const vector< double > &grid)
Set up initial grid.
Definition Domain1D.cpp:223
vector< double > & grid()
Access the array of grid coordinates [m].
Definition Domain1D.h:754
size_t m_jstart
Index of the first point in this domain in the global point list.
Definition Domain1D.h:851
size_t m_nv
Number of solution components.
Definition Domain1D.h:826
size_t nPoints() const
Number of grid points in this domain.
Definition Domain1D.h:202
vector< string > m_name
Names of solution components.
Definition Domain1D.h:859
string info(const vector< string > &keys, int rows=10, int width=80)
Return a concise summary of a Domain.
Definition Domain1D.cpp:47
virtual size_t componentIndex(const string &name, bool checkAlias=true) const
Index of component with name name.
Definition Domain1D.cpp:87
vector< double > values(const string &component) const
Retrieve component values.
Definition Domain1D.h:459
virtual void resize(size_t nv, size_t np)
Resize the domain to have nv components and np grid points.
Definition Domain1D.cpp:52
double z(size_t jlocal) const
Get the coordinate [m] of the point with local index jlocal
Definition Domain1D.h:731
virtual void show(const double *x)
Print the solution.
Definition Domain1D.cpp:248
vector< double > m_rtol_ss
Relative tolerances for steady mode.
Definition Domain1D.h:831
vector< double > m_slast
Solution vector at the last time step.
Definition Domain1D.h:828
Domain1D * m_right
Pointer to the domain to the right.
Definition Domain1D.h:854
void setSteadyTolerances(double rtol, double atol, size_t n=npos)
Set tolerances for steady-state mode.
Definition Domain1D.cpp:122
virtual string componentName(size_t n) const
Name of component n. May be overloaded.
Definition Domain1D.cpp:75
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:299
vector< double > m_z
1D spatial grid coordinates
Definition Domain1D.h:835
size_t m_points
Number of grid points.
Definition Domain1D.h:827
string type() const
String indicating the domain implemented.
Definition Domain1D.h:51
vector< double > m_max
Upper bounds on solution components.
Definition Domain1D.h:829
unique_ptr< Refiner > m_refiner
Refiner object used for placing grid points.
Definition Domain1D.h:858
vector< double > m_min
Lower bounds on solution components.
Definition Domain1D.h:830
virtual void setProfile(const string &component, const vector< double > &pos, const vector< double > &values)
Specify a profile for a component.
Definition Domain1D.h:534
virtual double initialValue(size_t n, size_t j)
Initial value of solution component n at grid point j.
Definition Domain1D.cpp:317
virtual shared_ptr< SolutionArray > toArray(bool normalize=false)
Save the state of this domain to a SolutionArray.
Definition Domain1D.h:575
void needJacUpdate()
Set this if something has changed in the governing equations (for example, the value of a constant ha...
Definition Domain1D.cpp:135
virtual void _getInitialSoln(double *x)
Writes some or all initial solution values into the global solution array, beginning at the location ...
Definition Domain1D.cpp:308
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:408
vector< double > getRefineCriteria()
Get the grid refinement criteria.
Definition Domain1D.cpp:303
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:203
virtual AnyMap getMeta() const
Retrieve meta data.
Definition Domain1D.cpp:143
virtual bool hasComponent(const string &name, bool checkAlias=true) const
Check whether the Domain contains a component.
Definition Domain1D.cpp:98
void setRefineCriteria(double ratio=10.0, double slope=0.8, double curve=0.8, double prune=-0.1)
Set grid refinement criteria.
Definition Domain1D.cpp:298
An array index is out of range.
An error indicating that an unimplemented function has been called.
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