Cantera
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
11#include "cantera/oneD/refine.h"
12#include "cantera/base/AnyMap.h"
16
17namespace Cantera
18{
19
20Domain1D::Domain1D(size_t nv, size_t points, double time)
21{
22 resize(nv, points);
23}
24
25Domain1D::~Domain1D()
26{
27 if (m_solution) {
28 m_solution->thermo()->removeSpeciesLock();
29 }
30}
31
32string Domain1D::info(const vector<string>& keys, int rows, int width)
33{
34 return toArray()->info(keys, rows, width);
35}
36
37void Domain1D::resize(size_t nv, size_t np)
38{
39 // if the number of components is being changed, then a
40 // new grid refiner is required.
41 if (nv != m_nv || !m_refiner) {
42 m_nv = nv;
43 m_refiner = make_unique<Refiner>(*this);
44 }
45 m_nv = nv;
46 m_name.resize(m_nv,"");
47 m_max.resize(m_nv, 0.0);
48 m_min.resize(m_nv, 0.0);
49 // Default error tolerances for all domains
50 m_rtol_ss.resize(m_nv, 1.0e-4);
51 m_atol_ss.resize(m_nv, 1.0e-9);
52 m_rtol_ts.resize(m_nv, 1.0e-4);
53 m_atol_ts.resize(m_nv, 1.0e-11);
54 m_points = np;
55 m_z.resize(np, 0.0);
56 m_slast.resize(m_nv * m_points, 0.0);
57 locate();
58}
59
60string Domain1D::componentName(size_t n) const
61{
62 if (n < m_nv) {
63 if (m_name[n] != "") {
64 return m_name[n];
65 } else {
66 return fmt::format("component {}", n);
67 }
68 }
69 throw IndexError("Domain1D::componentName", "component", n, m_nv);
70}
71
72size_t Domain1D::componentIndex(const string& name, bool checkAlias) const
73{
74 for (size_t n = 0; n < m_nv; n++) {
75 if (name == componentName(n)) {
76 return n;
77 }
78 }
79 throw CanteraError("Domain1D::componentIndex",
80 "Component '{}' not found", name);
81}
82
83bool Domain1D::hasComponent(const string& name, bool checkAlias) const
84{
85 for (size_t n = 0; n < m_nv; n++) {
86 if (name == componentName(n)) {
87 return true;
88 }
89 }
90 throw CanteraError("Domain1D::hasComponent",
91 "Component '{}' not found", name);
92}
93
94void Domain1D::setTransientTolerances(double rtol, double atol, size_t n)
95{
96 if (n == npos) {
97 for (n = 0; n < m_nv; n++) {
98 m_rtol_ts[n] = rtol;
99 m_atol_ts[n] = atol;
100 }
101 } else {
102 m_rtol_ts[n] = rtol;
103 m_atol_ts[n] = atol;
104 }
105}
106
107void Domain1D::setSteadyTolerances(double rtol, double atol, size_t n)
108{
109 if (n == npos) {
110 for (n = 0; n < m_nv; n++) {
111 m_rtol_ss[n] = rtol;
112 m_atol_ss[n] = atol;
113 }
114 } else {
115 m_rtol_ss[n] = rtol;
116 m_atol_ss[n] = atol;
117 }
118}
119
121{
122 if (m_container) {
123 m_container->linearSolver()->setAge(10000);
124 m_container->saveStats();
125 }
126}
127
129{
130 auto wrap_tols = [this](const vector<double>& tols) {
131 // If all tolerances are the same, just store the scalar value.
132 // Otherwise, store them by component name
133 set<double> unique_tols(tols.begin(), tols.end());
134 if (unique_tols.size() == 1) {
135 return AnyValue(tols[0]);
136 } else {
137 AnyMap out;
138 for (size_t i = 0; i < tols.size(); i++) {
139 out[componentName(i)] = tols[i];
140 }
141 return AnyValue(out);
142 }
143 };
144 AnyMap state;
145 state["type"] = domainType();
146 state["points"] = static_cast<long int>(nPoints());
147 if (nComponents() && nPoints()) {
148 state["tolerances"]["transient-abstol"] = wrap_tols(m_atol_ts);
149 state["tolerances"]["steady-abstol"] = wrap_tols(m_atol_ss);
150 state["tolerances"]["transient-reltol"] = wrap_tols(m_rtol_ts);
151 state["tolerances"]["steady-reltol"] = wrap_tols(m_rtol_ss);
152 }
153 return state;
154}
155
156void Domain1D::setMeta(const AnyMap& meta)
157{
158 auto set_tols = [&](const AnyValue& tols, const string& which, vector<double>& out)
159 {
160 if (!tols.hasKey(which)) {
161 return;
162 }
163 const auto& tol = tols[which];
164 if (tol.isScalar()) {
165 out.assign(nComponents(), tol.asDouble());
166 } else {
167 for (size_t i = 0; i < nComponents(); i++) {
168 string name = componentName(i);
169 if (tol.hasKey(name)) {
170 out[i] = tol[name].asDouble();
171 } else {
172 warn_user("Domain1D::setMeta", "No {} found for component '{}'",
173 which, name);
174 }
175 }
176 }
177 };
178
179 if (meta.hasKey("tolerances")) {
180 const auto& tols = meta["tolerances"];
181 set_tols(tols, "transient-abstol", m_atol_ts);
182 set_tols(tols, "transient-reltol", m_rtol_ts);
183 set_tols(tols, "steady-abstol", m_atol_ss);
184 set_tols(tols, "steady-reltol", m_rtol_ss);
185 }
186}
187
189{
190 if (m_left) {
191 // there is a domain on the left, so the first grid point in this domain
192 // is one more than the last one on the left
193 m_jstart = m_left->lastPoint() + 1;
194
195 // the starting location in the solution vector
196 m_iloc = m_left->loc() + m_left->size();
197 } else {
198 // this is the left-most domain
199 m_jstart = 0;
200 m_iloc = 0;
201 }
202 // if there is a domain to the right of this one, then repeat this for it
203 if (m_right) {
204 m_right->locate();
205 }
206}
207
208void Domain1D::setupGrid(span<const double> z)
209{
210 m_z.assign(z.begin(), z.end());
211 m_points = z.size();
212 m_slast.resize(m_nv * m_points, 0.0);
213}
214
215void Domain1D::setupUniformGrid(size_t points, double length, double start)
216{
217 vector<double> grid(points);
218 double dz = length / static_cast<double>(points - 1);
219 for (size_t iz = 0; iz < points; iz++) {
220 grid[iz] = start + iz * dz;
221 }
223}
224
225void Domain1D::show(span<const double> x)
226{
227 size_t nn = m_nv/5;
228 for (size_t i = 0; i < nn; i++) {
229 writeline('-', 79, false, true);
230 writelog("\n z ");
231 for (size_t n = 0; n < 5; n++) {
232 writelog(" {:>10s} ", componentName(i*5 + n));
233 }
234 writeline('-', 79, false, true);
235 for (size_t j = 0; j < m_points; j++) {
236 writelog("\n {:10.4g} ", m_z[j]);
237 for (size_t n = 0; n < 5; n++) {
238 writelog(" {:10.4g} ", x[index(i*5 + n, j)]);
239 }
240 }
241 writelog("\n");
242 }
243 size_t nrem = m_nv - 5*nn;
244 writeline('-', 79, false, true);
245 writelog("\n z ");
246 for (size_t n = 0; n < nrem; n++) {
247 writelog(" {:>10s} ", componentName(nn*5 + n));
248 }
249 writeline('-', 79, false, true);
250 for (size_t j = 0; j < m_points; j++) {
251 writelog("\n {:10.4g} ", m_z[j]);
252 for (size_t n = 0; n < nrem; n++) {
253 writelog(" {:10.4g} ", x[index(nn*5 + n, j)]);
254 }
255 }
256 writelog("\n");
257}
258
259void Domain1D::setRefineCriteria(double ratio, double slope, double curve, double prune)
260{
261 m_refiner->setCriteria(ratio, slope, curve, prune);
262}
263
265{
266 return m_refiner->getCriteria();
267}
268
269void Domain1D::_getInitialSoln(span<double> x)
270{
271 for (size_t j = 0; j < m_points; j++) {
272 for (size_t n = 0; n < m_nv; n++) {
273 x[index(n,j)] = initialValue(n,j);
274 }
275 }
276}
277
278double Domain1D::initialValue(size_t n, size_t j)
279{
280 throw NotImplementedError("Domain1D::initialValue");
281}
282
283void Domain1D::appendResiduals(SolutionArray& arr, span<const double> res) const
284{
285 // Skip domains with no data points in the SolutionArray
286 if (arr.size() == 0) {
287 return;
288 }
289 for (size_t i = 0; i < nComponents(); i++) {
290 string rname = "residual-" + componentName(i);
291 arr.addExtra(rname);
292 vector<double> data(nPoints());
293 for (size_t j = 0; j < nPoints(); j++) {
294 data[j] = res[index(i, j)];
295 }
297 value = data;
298 arr.setComponent(rname, value);
299 }
300}
301
302} // 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:94
size_t m_iloc
Starting location within the solution vector for unknowns that correspond to this domain.
Definition Domain1D.h:760
virtual void _getInitialSoln(span< double > x)
Writes some or all initial solution values into the global solution array, beginning at the location ...
Definition Domain1D.cpp:269
shared_ptr< Solution > m_solution
Composite thermo/kinetics/transport handler.
Definition Domain1D.h:778
Domain1D * m_left
Pointer to the domain to the left.
Definition Domain1D.h:766
OneDim * m_container
Parent OneDim simulation containing this and adjacent domains.
Definition Domain1D.h:751
virtual void setupGrid(span< const double > z)
Set up initial grid.
Definition Domain1D.cpp:208
size_t nComponents() const
Number of components at each grid point.
Definition Domain1D.h:143
double rtol(size_t n)
Relative tolerance of the nth component.
Definition Domain1D.h:224
vector< double > m_atol_ss
Absolute tolerances for steady mode.
Definition Domain1D.h:746
void setupUniformGrid(size_t points, double length, double start=0.)
Set up uniform grid.
Definition Domain1D.cpp:215
vector< double > m_rtol_ts
Relative tolerances for transient mode.
Definition Domain1D.h:745
vector< double > m_atol_ts
Absolute tolerances for transient mode.
Definition Domain1D.h:747
virtual void setMeta(const AnyMap &meta)
Retrieve meta data.
Definition Domain1D.cpp:156
span< double > grid()
Access the array of grid coordinates [m].
Definition Domain1D.h:669
size_t m_jstart
Index of the first point in this domain in the global point list.
Definition Domain1D.h:764
size_t m_nv
Number of solution components.
Definition Domain1D.h:739
size_t nPoints() const
Number of grid points in this domain.
Definition Domain1D.h:148
vector< string > m_name
Names of solution components.
Definition Domain1D.h:772
virtual string domainType() const
Domain type flag.
Definition Domain1D.h:47
string info(const vector< string > &keys, int rows=10, int width=80)
Return a concise summary of a Domain.
Definition Domain1D.cpp:32
virtual size_t componentIndex(const string &name, bool checkAlias=true) const
Index of component with name name.
Definition Domain1D.cpp:72
virtual void resize(size_t nv, size_t np)
Resize the domain to have nv components and np grid points.
Definition Domain1D.cpp:37
double z(size_t jlocal) const
Get the coordinate [m] of the point with local index jlocal.
Definition Domain1D.h:654
virtual void appendResiduals(SolutionArray &arr, span< const double > res) const
Append residual data to a SolutionArray as extra components.
Definition Domain1D.cpp:283
vector< double > m_rtol_ss
Relative tolerances for steady mode.
Definition Domain1D.h:744
vector< double > m_slast
Solution vector at the last time step.
Definition Domain1D.h:741
virtual double value(const string &component) const
Set a single component value at a boundary.
Definition Domain1D.h:400
Domain1D * m_right
Pointer to the domain to the right.
Definition Domain1D.h:767
void setSteadyTolerances(double rtol, double atol, size_t n=npos)
Set tolerances for steady-state mode.
Definition Domain1D.cpp:107
virtual string componentName(size_t n) const
Name of component n. May be overloaded.
Definition Domain1D.cpp:60
double atol(size_t n)
Absolute tolerance of the nth component.
Definition Domain1D.h:229
vector< double > m_z
1D spatial grid coordinates
Definition Domain1D.h:748
size_t m_points
Number of grid points.
Definition Domain1D.h:740
virtual void show(span< const double > x)
Print the solution.
Definition Domain1D.cpp:225
vector< double > m_max
Upper bounds on solution components.
Definition Domain1D.h:742
unique_ptr< Refiner > m_refiner
Refiner object used for placing grid points.
Definition Domain1D.h:771
vector< double > m_min
Lower bounds on solution components.
Definition Domain1D.h:743
virtual double initialValue(size_t n, size_t j)
Initial value of solution component n at grid point j.
Definition Domain1D.cpp:278
virtual shared_ptr< SolutionArray > toArray(bool normalize=false)
Save the state of this domain to a SolutionArray.
Definition Domain1D.h:525
void needJacUpdate()
Set this if something has changed in the governing equations (for example, the value of a constant ha...
Definition Domain1D.cpp:120
Domain1D(size_t nv=1, size_t points=1, double time=0.0)
Constructor.
Definition Domain1D.cpp:20
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:390
vector< double > getRefineCriteria()
Get the grid refinement criteria.
Definition Domain1D.cpp:264
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:188
virtual AnyMap getMeta() const
Retrieve meta data.
Definition Domain1D.cpp:128
virtual bool hasComponent(const string &name, bool checkAlias=true) const
Check whether the Domain contains a component.
Definition Domain1D.cpp:83
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:259
An array index is out of range.
An error indicating that an unimplemented function has been called.
A container class holding arrays of state information.
void setComponent(const string &name, const AnyValue &data)
Set a component of the SolutionArray by name.
void addExtra(const string &name, bool back=true)
Add auxiliary component to SolutionArray.
int size() const
Size of SolutionArray (number of entries).
void writelog(const string &fmt, const Args &... args)
Write a formatted message to the screen.
Definition global.h:171
void warn_user(const string &method, const string &msg, const Args &... args)
Print a user warning raised from method as CanteraWarning.
Definition global.h:263
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:183