Cantera  2.1.2
Domain1D.cpp
Go to the documentation of this file.
1 /**
2  * @file Domain1D.cpp
3  */
4 
6  #include "cantera/base/ctml.h"
7 
8 #include <cstdio>
9 
10 using namespace std;
11 using namespace ctml;
12 
13 namespace Cantera
14 {
15 
16 void Domain1D::
17 setTolerances(size_t nr, const doublereal* rtol,
18  size_t na, const doublereal* atol, int ts)
19 {
20  warn_deprecated("Domain1D::setTolerances",
21  "Use setTransientTolerances or setSteadyTolerances.");
22  if (nr < m_nv || na < m_nv)
23  throw CanteraError("Domain1D::setTolerances",
24  "wrong array size for solution error tolerances. "
25  "Size should be at least "+int2str(m_nv));
26  if (ts >= 0) {
27  copy(rtol, rtol + m_nv, m_rtol_ss.begin());
28  copy(atol, atol + m_nv, m_atol_ss.begin());
29  }
30  if (ts <= 0) {
31  copy(rtol, rtol + m_nv, m_rtol_ts.begin());
32  copy(atol, atol + m_nv, m_atol_ts.begin());
33  }
34 }
35 
36 void Domain1D::
37 setTolerances(size_t n, doublereal rtol, doublereal atol, int ts)
38 {
39  warn_deprecated("Domain1D::setTolerances",
40  "Use setTransientTolerances or setSteadyTolerances.");
41  if (ts >= 0) {
42  m_rtol_ss[n] = rtol;
43  m_atol_ss[n] = atol;
44  }
45  if (ts <= 0) {
46  m_rtol_ts[n] = rtol;
47  m_atol_ts[n] = atol;
48  }
49 }
50 
51 void Domain1D::
52 setTolerances(doublereal rtol, doublereal atol,int ts)
53 {
54  warn_deprecated("Domain1D::setTolerances",
55  "Use setTransientTolerances or setSteadyTolerances.");
56  for (size_t n = 0; n < m_nv; n++) {
57  if (ts >= 0) {
58  m_rtol_ss[n] = rtol;
59  m_atol_ss[n] = atol;
60  }
61  if (ts <= 0) {
62  m_rtol_ts[n] = rtol;
63  m_atol_ts[n] = atol;
64  }
65  }
66 }
67 
68 void Domain1D::setTransientTolerances(doublereal rtol, doublereal atol, size_t n)
69 {
70  if (n == npos) {
71  for (n = 0; n < m_nv; n++) {
72  m_rtol_ts[n] = rtol;
73  m_atol_ts[n] = atol;
74  }
75  } else {
76  m_rtol_ts[n] = rtol;
77  m_atol_ts[n] = atol;
78  }
79 }
80 
81 void Domain1D::setTolerancesTS(doublereal rtol, doublereal atol, size_t n)
82 {
83  warn_deprecated("Domain1D::setTolerancesTS",
84  "Use setTransientTolerances");
85  setTransientTolerances(rtol, atol, n);
86 }
87 
88 void Domain1D::setSteadyTolerances(doublereal rtol, doublereal atol, size_t n)
89 {
90  if (n == npos) {
91  for (n = 0; n < m_nv; n++) {
92  m_rtol_ss[n] = rtol;
93  m_atol_ss[n] = atol;
94  }
95  } else {
96  m_rtol_ss[n] = rtol;
97  m_atol_ss[n] = atol;
98  }
99 }
100 
101 void Domain1D::setTolerancesSS(doublereal rtol, doublereal atol, size_t n)
102 {
103  warn_deprecated("Domain1D::setTolerancesSS",
104  "Use setSteadyTolerances");
105  setSteadyTolerances(rtol, atol, n);
106 }
107 
108 void Domain1D::
109 eval(size_t jg, doublereal* xg, doublereal* rg,
110  integer* mask, doublereal rdt)
111 {
112 
113  if (jg != npos && (jg + 1 < firstPoint() || jg > lastPoint() + 1)) {
114  return;
115  }
116 
117  // if evaluating a Jacobian, compute the steady-state residual
118  if (jg != npos) {
119  rdt = 0.0;
120  }
121 
122  // start of local part of global arrays
123  doublereal* x = xg + loc();
124  doublereal* rsd = rg + loc();
125  integer* diag = mask + loc();
126 
127  size_t jmin, jmax, jpt, j, i;
128  jpt = jg - firstPoint();
129 
130  if (jg == npos) { // evaluate all points
131  jmin = 0;
132  jmax = m_points - 1;
133  } else { // evaluate points for Jacobian
134  jmin = std::max<size_t>(jpt, 1) - 1;
135  jmax = std::min(jpt+1,m_points-1);
136  }
137 
138  for (j = jmin; j <= jmax; j++) {
139  if (j == 0 || j == m_points - 1) {
140  for (i = 0; i < m_nv; i++) {
141  rsd[index(i,j)] = residual(x,i,j);
142  diag[index(i,j)] = 0;
143  }
144  } else {
145  for (i = 0; i < m_nv; i++) {
146  rsd[index(i,j)] = residual(x,i,j)
147  - timeDerivativeFlag(i)*rdt*(value(x,i,j) - prevSoln(i,j));
148  diag[index(i,j)] = timeDerivativeFlag(i);
149  }
150  }
151  }
152 }
153 
154 XML_Node& Domain1D::save(XML_Node& o, const doublereal* const sol)
155 {
156  XML_Node& d = o.addChild("domain");
157  d.addAttribute("points", nPoints());
158  d.addAttribute("components", nComponents());
159  d.addAttribute("id", id());
160  addFloatArray(d, "abstol_transient", nComponents(), &m_atol_ts[0]);
161  addFloatArray(d, "reltol_transient", nComponents(), &m_rtol_ts[0]);
162  addFloatArray(d, "abstol_steady", nComponents(), &m_atol_ss[0]);
163  addFloatArray(d, "reltol_steady", nComponents(), &m_rtol_ss[0]);
164  return d;
165 }
166 
167 void Domain1D::restore(const XML_Node& dom, doublereal* soln, int loglevel)
168 {
169  vector_fp values;
170  vector<XML_Node*> nodes;
171  dom.getChildren("floatArray", nodes);
172  for (size_t i = 0; i < nodes.size(); i++) {
173  string title = nodes[i]->attrib("title");
174  getFloatArray(*nodes[i], values, false);
175  if (values.size() != nComponents()) {
176  if (loglevel > 0) {
177  writelog("Warning: Domain1D::restore: Got an array of length " +
178  int2str(values.size()) + " when one of length " +
179  int2str(nComponents()) + " was expected. " +
180  "Tolerances for individual species may not be preserved.\n");
181  }
182  // The number of components will differ when restoring from a
183  // mechanism with a different number of species. Assuming that
184  // tolerances are the same for all species, we can just copy the
185  // tolerance from the last species.
186  if (!values.empty()) {
187  values.resize(nComponents(), values[values.size()-1]);
188  } else {
189  // If the tolerance vector is empty, just leave the defaults
190  // in place.
191  continue;
192  }
193  }
194  if (title == "abstol_transient") {
195  m_atol_ts = values;
196  } else if (title == "reltol_transient") {
197  m_rtol_ts = values;
198  } else if (title == "abstol_steady") {
199  m_atol_ss = values;
200  } else if (title == "reltol_steady") {
201  m_rtol_ss = values;
202  } else {
203  throw CanteraError("Domain1D::restore",
204  "Got an unexpected array, '" + title + "'");
205  }
206  }
207 }
208 
209 void Domain1D::setupGrid(size_t n, const doublereal* z)
210 {
211  if (n > 1) {
212  resize(m_nv, n);
213  for (size_t j = 0; j < m_points; j++) {
214  m_z[j] = z[j];
215  }
216  }
217 }
218 
219 void drawline()
220 {
221  writelog("\n-------------------------------------"
222  "------------------------------------------");
223 }
224 
225 void Domain1D::showSolution(const doublereal* x)
226 {
227  size_t nn = m_nv/5;
228  size_t i, j, n;
229  //char* buf = new char[100];
230  char buf[100];
231  doublereal v;
232  for (i = 0; i < nn; i++) {
233  drawline();
234  sprintf(buf, "\n z ");
235  writelog(buf);
236  for (n = 0; n < 5; n++) {
237  sprintf(buf, " %10s ",componentName(i*5 + n).c_str());
238  writelog(buf);
239  }
240  drawline();
241  for (j = 0; j < m_points; j++) {
242  sprintf(buf, "\n %10.4g ",m_z[j]);
243  writelog(buf);
244  for (n = 0; n < 5; n++) {
245  v = value(x, i*5+n, j);
246  sprintf(buf, " %10.4g ",v);
247  writelog(buf);
248  }
249  }
250  writelog("\n");
251  }
252  size_t nrem = m_nv - 5*nn;
253  drawline();
254  sprintf(buf, "\n z ");
255  writelog(buf);
256  for (n = 0; n < nrem; n++) {
257  sprintf(buf, " %10s ", componentName(nn*5 + n).c_str());
258  writelog(buf);
259  }
260  drawline();
261  for (j = 0; j < m_points; j++) {
262  sprintf(buf, "\n %10.4g ",m_z[j]);
263  writelog(buf);
264  for (n = 0; n < nrem; n++) {
265  v = value(x, nn*5+n, j);
266  sprintf(buf, " %10.4g ", v);
267  writelog(buf);
268  }
269  }
270  writelog("\n");
271 }
272 
273 void Domain1D::_getInitialSoln(doublereal* x)
274 {
275  for (size_t j = 0; j < m_points; j++) {
276  for (size_t n = 0; n < m_nv; n++) {
277  x[index(n,j)] = initialValue(n,j);
278  }
279  }
280 }
281 
282 doublereal Domain1D::initialValue(size_t n, size_t j)
283 {
284  throw CanteraError("Domain1D::initialValue",
285  "base class method called!");
286  return 0.0;
287 }
288 
289 } // namespace
std::string int2str(const int n, const std::string &fmt)
Convert an int to a string using a format converter.
Definition: stringUtils.cpp:40
CTML ("Cantera Markup Language") is the variant of XML that Cantera uses to store data...
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:173
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:100
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:76
void addFloatArray(Cantera::XML_Node &node, const std::string &title, const size_t n, const doublereal *const vals, const std::string &units, const std::string &type, const doublereal minval, const doublereal maxval)
This function adds a child node with the name, "floatArray", with a value consisting of a comma separ...
Definition: ctml.cpp:68
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:68
void addAttribute(const std::string &attrib, const std::string &value)
Add or modify an attribute of the current node.
Definition: xml.cpp:518
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:165
void writelog(const std::string &msg)
Write a message to the screen.
Definition: global.cpp:43
size_t getFloatArray(const Cantera::XML_Node &node, std::vector< doublereal > &v, const bool convert, const std::string &unitsString, const std::string &nodeName)
This function reads the current node or a child node of the current node with the default name...
Definition: ctml.cpp:419
void getChildren(const std::string &name, std::vector< XML_Node * > &children) const
Get a vector of pointers to XML_Node containing all of the children of the current node which matches...
Definition: xml.cpp:916