15 static void sim1D_drawline()
36 for (
size_t n = 0; n <
m_nd; n++) {
38 domain(n).m_adiabatic=
false;
52 for (
size_t dom=0; dom<
m_nd; dom++) {
55 for (
size_t comp=0; comp<ncomp; comp++) {
63 void Sim1D::setValue(
size_t dom,
size_t comp,
size_t localPoint, doublereal value)
67 "Index out of bounds:" +
int2str(iloc) +
" > " +
72 doublereal
Sim1D::value(
size_t dom,
size_t comp,
size_t localPoint)
const
76 "Index out of bounds:" +
int2str(iloc) +
" > " +
81 doublereal Sim1D::workValue(
size_t dom,
size_t comp,
size_t localPoint)
const
85 "Index out of bounds:" +
int2str(iloc) +
" > " +
95 doublereal z0 = d.zmin();
96 doublereal z1 = d.zmax();
97 doublereal zpt, frac, v;
98 for (
size_t n = 0; n < d.
nPoints(); n++) {
100 frac = (zpt - z0)/(z1 - z0);
106 void Sim1D::save(
const std::string& fname,
const std::string&
id,
107 const std::string& desc,
int loglevel)
109 OneDim::save(fname,
id, desc,
DATA_PTR(
m_x), loglevel);
112 void Sim1D::saveResidual(
const std::string& fname,
const std::string&
id,
113 const std::string& desc,
int loglevel)
117 OneDim::save(fname,
id, desc, &res[0], loglevel);
123 ifstream s(fname.c_str());
126 "could not open input file "+fname);
134 throw CanteraError(
"Sim1D::restore",
"No solution with id = "+
id);
137 vector<XML_Node*> xd;
139 if (xd.size() !=
m_nd) {
140 throw CanteraError(
"Sim1D::restore",
"Solution does not contain the "
141 " correct number of domains. Found " +
142 int2str(xd.size()) +
"expected " +
146 for (
size_t m = 0; m <
m_nd; m++) {
147 if (loglevel > 0 && xd[m]->attrib(
"id") !=
domain(m).id()) {
148 writelog(
"Warning: domain names do not match: '" +
149 (*xd[m])[
"id"] + +
"' and '" +
domain(m).
id() +
"'\n");
155 for (
size_t m = 0; m <
m_nd; m++) {
166 for (n = 0; n < np; n++) {
171 void Sim1D::showSolution(ostream& s)
173 for (
size_t n = 0; n <
m_nd; n++) {
174 if (
domain(n).domainType() != cEmptyType) {
180 void Sim1D::showSolution()
182 for (
size_t n = 0; n <
m_nd; n++) {
183 if (
domain(n).domainType() != cEmptyType) {
185 +
" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\n");
191 void Sim1D::getInitialSoln()
193 for (
size_t n = 0; n <
m_nd; n++) {
200 for (
size_t n = 0; n <
m_nd; n++) {
205 void Sim1D::setTimeStep(doublereal stepsize,
size_t n, integer* tsteps)
209 for (
size_t i = 0; i < n; i++) {
220 }
else if (m > -10) {
224 "ERROR: OneDim::solve returned m = " +
int2str(m) +
"\n");
228 void Sim1D::solve(
int loglevel,
bool refine_grid)
233 int soln_number = -1;
236 while (new_points > 0) {
246 writelog(
"Attempt Newton solution of steady-state problem...", loglevel);
253 for (
size_t mm = 1; mm <
nDomains(); mm+=2) {
262 save(
"debug_sim1d.xml",
"debug",
263 "After successful Newton solve");
266 saveResidual(
"debug_sim1d.xml",
"residual",
267 "After successful Newton solve");
275 save(
"debug_sim1d.xml",
"debug",
276 "After unsuccessful Newton solve");
279 saveResidual(
"debug_sim1d.xml",
"residual",
280 "After unsuccessful Newton solve");
286 save(
"debug_sim1d.xml",
"debug",
"After timestepping");
289 saveResidual(
"debug_sim1d.xml",
"residual",
290 "After timestepping");
294 sprintf(buf,
" %10.4g %10.4g \n", dt,
318 new_points =
refine(loglevel);
324 if (new_points && loglevel > 6) {
325 save(
"debug_sim1d.xml",
"debug",
"After regridding");
327 if (new_points && loglevel > 7) {
328 saveResidual(
"debug_sim1d.xml",
"residual",
331 if (new_points < 0) {
332 writelog(
"Maximum number of grid points reached.");
336 writelog(
"grid refinement disabled.\n", loglevel);
344 int ianalyze, np = 0;
346 doublereal xmid, zmid;
347 std::vector<size_t> dsize;
349 for (
size_t n = 0; n <
m_nd; n++) {
354 ianalyze = r.analyze(d.grid().size(),
364 np += r.nNewPoints();
369 size_t nstart = znew.size();
370 for (
size_t m = 0; m < npnow; m++) {
372 if (r.keepPoint(m)) {
374 znew.push_back(d.grid(m));
377 for (
size_t i = 0; i < comp; i++) {
378 xnew.push_back(
value(n, i, m));
385 if (r.newPointNeeded(m) && m + 1 < npnow) {
388 zmid = 0.5*(d.grid(m) + d.grid(m+1));
389 znew.push_back(zmid);
395 for (
size_t i = 0; i < comp; i++) {
396 xmid = 0.5*(
value(n, i, m) +
value(n, i, m+1));
397 xnew.push_back(xmid);
401 writelog(
"refine: discarding point at "+
fp2str(d.grid(m))+
"\n", loglevel);
404 dsize.push_back(znew.size() - nstart);
412 size_t gridstart = 0, gridsize;
413 for (
size_t n = 0; n <
m_nd; n++) {
418 gridstart += gridsize;
422 m_x.resize(xnew.size());
423 copy(xnew.begin(), xnew.end(),
m_x.begin());
426 m_xnew.resize(xnew.size());
440 doublereal zfixed,interp_factor;
441 doublereal z1 = 0.0, z2 = 0.0, t1,t2;
444 std::vector<size_t> dsize;
447 for (n = 0; n <
m_nd; n++) {
455 size_t nstart = znew.size();
456 for (m = 0; m < npnow-1; m++) {
457 if (
value(n,2,m) == t) {
464 }
else if ((
value(n,2,m)<t) && (
value(n,2,m+1)>t)) {
471 zfixed = (z1-z2)/(t1-t2)*(t-t2)+z2;
482 for (m = 0; m < npnow; m++) {
484 znew.push_back(d.grid(m));
487 for (i = 0; i < comp; i++) {
488 xnew.push_back(
value(n, i, m));
490 if (m==m1 && addnewpt) {
492 znew.push_back(zfixed);
494 interp_factor = (zfixed-z2) / (z1-z2);
497 for (i = 0; i < comp; i++) {
498 xmid = interp_factor*(
value(n, i, m) -
value(n, i, m+1)) +
value(n,i,m+1);
499 xnew.push_back(xmid);
505 dsize.push_back(znew.size() - nstart);
513 size_t gridstart = 0, gridsize;
514 for (n = 0; n <
m_nd; n++) {
519 gridstart += gridsize;
523 m_x.resize(xnew.size());
524 copy(xnew.begin(), xnew.end(),
m_x.begin());
527 m_xnew.resize(xnew.size());
529 copy(xnew.begin(), xnew.end(),
m_xnew.begin());
536 void Sim1D::setAdiabaticFlame(
void)
538 for (
size_t n = 0; n <
m_nd; n++) {
545 doublereal slope, doublereal curve, doublereal prune)
551 for (
size_t n = 0; n <
m_nd; n++) {
564 for (
size_t n = 0; n <
m_nd; n++) {
571 void Sim1D::setMaxGridPoints(
int dom,
int npoints)
577 for (
size_t n = 0; n <
m_nd; n++) {
589 void Sim1D::evalSSJacobian()
Container class for multiple-domain 1D problems.
size_t m_nd
number of domains
void showSolution(std::ostream &s)
Print to stream s the current solution for all domains.
std::string int2str(const int n, const std::string &fmt)
Convert an int to a string using a format converter.
virtual void _getInitialSoln(doublereal *x)
Writes some or all initial solution values into the global solution array, beginning at the location ...
size_t nPoints() const
Number of grid points in this domain.
void restore(const std::string &fname, const std::string &id, int loglevel=2)
Initialize the solution with a previously-saved solution.
double timeStep(int nsteps, double dt, double *x, double *r, int loglevel)
vector_fp m_x
the solution vector
void resize()
Call after one or more grids has been refined.
doublereal value(size_t dom, size_t comp, size_t localPoint) const
Get one entry in the solution vector.
doublereal ssnorm(doublereal *x, doublereal *r)
Steady-state max norm (infinity norm) of the residual evaluated using solution x. ...
void eval(size_t j, double *x, double *r, doublereal rdt=-1.0, int count=1)
Evaluate the multi-domain residual function.
MultiJac & jacobian()
Return a reference to the Jacobian evaluator.
const size_t npos
index returned by functions to indicate "no position"
virtual std::string componentName(size_t n) const
Name of the nth component. May be overloaded.
void setValue(size_t dom, size_t comp, size_t localPoint, doublereal value)
Set a single value in the solution vector.
virtual size_t loc(size_t j=0) const
Location of the start of the local solution vector in the global solution vector,.
void setRefineCriteria(int dom=-1, doublereal ratio=10.0, doublereal slope=0.8, doublereal curve=0.8, doublereal prune=-0.1)
Set grid refinement criteria.
int solve(doublereal *x0, doublereal *x1, int loglevel)
Solve F(x) = 0, where F(x) is the multi-domain residual function.
Class XML_Node is a tree-based representation of the contents of an XML file.
void setInitialGuess(const std::string &component, vector_fp &locs, vector_fp &vals)
Set initial guess based on equilibrium.
virtual void restore(const XML_Node &dom, doublereal *soln, int loglevel)
Restore the solution for this domain from an XML_Node.
size_t loc(size_t jg)
Location in the solution vector of the first component of global point jg.
vector_int m_steps
array of number of steps to take before re-attempting the steady-state solution
int newtonSolve(int loglevel)
#define AssertThrowMsg(expr, procedure, message)
Assertion must be true or an error is thrown.
size_t nComponents() const
Number of components at each grid point.
Base class for one-dimensional domains.
int setFixedTemperature(doublereal t)
Add node for fixed temperature point of freely propagating flame.
void setCriteria(doublereal ratio=10.0, doublereal slope=0.8, doublereal curve=0.8, doublereal prune=-0.1)
Set grid refinement criteria.
size_t start(size_t i) const
The index of the start of domain i in the solution vector.
void setMaxPoints(int npmax)
Set the maximum number of points allowed in the domain.
Domain1D & domain(size_t i) const
Return a reference to domain i.
std::string fp2str(const double x, const std::string &fmt)
Convert a double into a c++ string.
void finalize()
Calls method _finalize in each domain.
virtual void _finalize(const doublereal *x)
In some cases, a domain may need to set parameters that depend on the initial solution estimate...
virtual void showSolution(const doublereal *x)
Print the solution.
Base class for exceptions thrown by Cantera classes.
doublereal m_tstep
timestep
doublereal & value(size_t i, size_t j)
Return a changeable reference to element (i,j).
size_t nDomains() const
Number of domains.
void setGridMin(double gridmin)
Set the minimum allowable spacing between adjacent grid points [m].
void setFlatProfile(size_t dom, size_t comp, doublereal v)
Set component 'comp' of domain 'dom' to value 'v' at all points.
Refiner & refiner()
Return a reference to the grid refiner.
Sim1D()
Default constructor.
int intValue(const std::string &val)
Translate a string into one integer value.
vector_fp m_xnew
a work array used to hold the residual or the new solution
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
int refine(int loglevel=0)
Refine the grid in all domains.
XML_Node * findID(const std::string &id, const int depth=100) const
This routine carries out a recursive search for an XML node based on the xml element attribute...
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.
#define DATA_PTR(vec)
Creates a pointer to the start of the raw data for a vector.
Refine Domain1D grids so that profiles satisfy adaptation tolerances.
void writelog(const std::string &msg)
Write a message to the screen.
size_t size() const
Total solution vector length;.
void setGridMin(int dom, double gridmin)
Set the minimum grid spacing in the specified domain(s).
void build(std::istream &f)
Main routine to create an tree-like representation of an XML file.
virtual void setupGrid(size_t n, const doublereal *z)
called to set up initial grid, and after grid refinement
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...
doublereal linearInterp(doublereal x, const vector_fp &xpts, const vector_fp &fpts)
Linearly interpolate a function defined on a discrete grid.