9 #include "cantera/base/config.h"
27 using namespace Cantera;
34 doublereal welldepth, diam, dipole, polar, rot;
37 static map<string, trdata> _trmap;
38 static bool _with_transport =
false;
40 static void getTransportData(
string trfile)
43 _with_transport =
true;
44 ifstream s(trfile.c_str());
46 "could not open transport database "+trfile);
69 getline(s, rest,
'\n');
77 if (rest.size() > 5 && rest[0] !=
'#' && rest[0] !=
'!') {
82 std::istringstream ioline(rest);
85 ioline >> nm >> t.geom >> t.welldepth >> t.diam
86 >> t.dipole >> t.polar >> t.rot;
99 if (nm[0] !=
'!' && !s.eof()) {
100 s >> t.geom >> t.welldepth >> t.diam
101 >> t.dipole >> t.polar >> t.rot;
104 getline(s, rest,
'\n');
115 static void addNASA(FILE* f,
117 doublereal minx, doublereal midx,
121 fprintf(f,
" thermo = (\n");
122 fprintf(f,
" NASA( [%8.2f, %8.2f], ", minx, midx);
123 fprintf(f,
"[%17.9E, %17.9E, \n", low[0], low[1]);
124 fprintf(f,
" %17.9E, %17.9E, %17.9E,\n",
125 low[2], low[3], low[4]);
126 fprintf(f,
" %17.9E, %17.9E] ),\n", low[5], low[6]);
127 fprintf(f,
" NASA( [%8.2f, %8.2f], ", midx, maxx);
128 fprintf(f,
"[%17.9E, %17.9E, \n", high[0], high[1]);
129 fprintf(f,
" %17.9E, %17.9E, %17.9E,\n",
130 high[2], high[3], high[4]);
131 fprintf(f,
" %17.9E, %17.9E] )\n", high[5], high[6]);
141 static void addNASA9(FILE* f,
142 const std::vector<vector_fp*> ®ion_coeffs,
145 size_t nReg = region_coeffs.size();
146 if (minTemps.size() != nReg) {
149 if (maxTemps.size() != nReg) {
153 fprintf(f,
" thermo = (\n");
154 for (
size_t i = 0; i < nReg; i++) {
155 double minT = minTemps[i];
156 double maxT = maxTemps[i];
157 const vector_fp& coeffs = *(region_coeffs[i]);
158 if ((
int) coeffs.size() != 9) {
161 fprintf(f,
" NASA9( [%8.2f, %8.2f], ", minT, maxT);
162 fprintf(f,
"[%17.9E, %17.9E, %17.9E,\n", coeffs[0],
163 coeffs[1], coeffs[2]);
164 fprintf(f,
" %17.9E, %17.9E, %17.9E,\n",
165 coeffs[3], coeffs[4], coeffs[5]);
166 fprintf(f,
" %17.9E, %17.9E, %17.9E] )",
167 coeffs[6], coeffs[7], coeffs[8]);
178 static void addTransportParams(FILE* f,
string name)
182 if (_with_transport && _trmap.find(name) != _trmap.end()) {
186 "no transport data for species "+name);
189 fprintf(f,
",\n transport = gas_transport(\n");
193 fprintf(f,
" geom = \"atom\",\n");
196 fprintf(f,
" geom = \"linear\",\n");
199 fprintf(f,
" geom = \"nonlinear\",\n");
203 "Unrecognized geometry flag for species " + name);
205 fprintf(f,
" diam = %g,\n",td.diam);
206 fprintf(f,
" well_depth = %g",td.welldepth);
207 if (td.polar != 0.0) {
208 fprintf(f,
",\n polar = %g",td.polar);
210 if (td.dipole != 0.0) {
211 fprintf(f,
",\n dipole = %g",td.dipole);
214 fprintf(f,
",\n rot_relax = %g",td.rot);
220 static void addFalloff(FILE* f,
string type,
223 if (type ==
"Troe") {
224 fprintf(f,
"%s", (
",\n falloff = Troe(A = " +
225 fp2str(params[0]) +
", T3 = " +
226 fp2str(params[1]) +
", T1 = " +
227 fp2str(params[2])).c_str());
228 if (params.size() >= 4) {
229 fprintf(f,
"%s", (
", T2 = " +
fp2str(params[3])).c_str());
232 }
else if (type ==
"SRI") {
233 fprintf(f,
"%s", (
",\n falloff = SRI(A = " +
234 fp2str(params[0]) +
", B = " +
235 fp2str(params[1]) +
", C = " +
236 fp2str(params[2])).c_str());
237 if (params.size() >= 5) {
238 fprintf(f,
"%s", (
", D = " +
fp2str(params[3]) +
239 ", E = " +
fp2str(params[4])).c_str());
249 static void addSpecies(FILE* f,
string idtag,
const ckr::Species& sp)
251 string spname = sp.
name;
252 if (spname.size() == 0) {
254 "Species name is empty");
256 fprintf(f,
"\nspecies(name = \"%s\",\n",spname.c_str());
257 int nel =
static_cast<int>(sp.
elements.size());
260 for (m = 0; m < nel; m++) {
266 nm[0] = (char) toupper(nm[0]);
267 if (nm.size() == 2) {
268 nm[1] = (char) tolower(nm[1]);
279 str +=
" "+nm+
":"+
int2str(num)+
" ";
283 fprintf(f,
" atoms = \"%s\",\n", str.c_str());
288 "Low Nasa Thermo Polynomial was not found");
292 "High Nasa Thermo Polynomial was not found");
296 "Low temp limit is greater or equal to high temp limit");
302 addNASA9(f, sp.region_coeffs, sp.minTemps, sp.maxTemps);
304 throw CanteraError(
"addSpecies",
"Unknown thermoFormatType");
307 if (_with_transport) {
308 addTransportParams(f, sp.
name);
310 if (sp.
id !=
"" || sp.m_commentsRef !=
"") {
311 fprintf(f,
",\n note = \"");
313 fprintf(f,
"%s", sp.
id.c_str());
315 if (sp.m_commentsRef !=
"") {
316 fprintf(f,
" %s", sp.m_commentsRef.c_str());
324 static void addReaction(FILE* f,
string idtag,
int i,
329 fprintf(f,
"%s", (
"\n# Reaction " +
int2str(i+1) +
"\n").c_str());
330 int nc =
static_cast<int>(rxn.
comment.size());
331 vector<string> options;
333 for (
int nn = 0; nn < nc; nn++)
334 if (rxn.
comment[nn] !=
"") fprintf(f,
"# %s \n",
340 fprintf(f,
"three_body_reaction( \"%s\",", eqn.c_str());
342 fprintf(f,
"falloff_reaction( \"%s\",", eqn.c_str());
344 fprintf(f,
"reaction( \"%s\",", eqn.c_str());
349 if (rxn.
kf.
type == ckr::Arrhenius) {
350 fprintf(f,
"\n kf = [%10.5E, %g, %g]", rxn.
kf.
A, rxn.
kf.
n, rxn.
kf.
E);
355 if (rxn.falloffType == ckr::Lindemann) {
356 addFalloff(f,
"Lindemann",rxn.falloffParameters);
357 }
else if (rxn.falloffType == ckr::Troe) {
358 addFalloff(f,
"Troe",rxn.falloffParameters);
359 }
else if (rxn.falloffType == ckr::SRI) {
360 addFalloff(f,
"SRI",rxn.falloffParameters);
362 throw CanteraError(
"addReaction",
"unknown falloff type");
365 if (rxn.
kf.
type == ckr::Arrhenius) {
366 fprintf(f,
" [%10.5E, %g, %g]", rxn.
kf.
A, rxn.
kf.
n, rxn.
kf.
E);
374 int nord =
static_cast<int>(rxn.fwdOrder.size());
376 map<string, double>::const_iterator b = rxn.fwdOrder.begin(),
377 e = rxn.fwdOrder.end();
379 for (; b != e; ++b) {
380 estr +=
" "+b->first+
":"+
fp2str(b->second)+
" ";
382 fprintf(f,
",\n order = \"%s\"", estr.c_str());
385 int ne =
static_cast<int>(rxn.
e3b.size());
389 }
else if (ne > 0.0) {
390 map<string, double>::const_iterator b = rxn.
e3b.begin(),
393 for (; b != e; ++b) {
394 estr +=
" "+b->first+
":"+
fp2str(b->second)+
" ";
396 fprintf(f,
",\n efficiencies = \"%s\"", estr.c_str());
399 if (rxn.
kf.
A <= 0.0) {
400 options.push_back(
"negative_A");
403 options.push_back(
"duplicate");
405 size_t nopt = options.size();
407 fprintf(f,
",\n options = [");
408 for (
size_t n = 0; n < nopt; n++) {
409 fprintf(f,
"\"%s\"", options[n].c_str());
419 void writeline(FILE* f)
421 fprintf(f,
"#-------------------------------------------------------------------------------\n");
434 void ck2ct(FILE* f,
string idtag,
ckr::CKReader& r,
bool hastransport)
438 doublereal version = 1.0;
440 fprintf(f,
"units(length = \"cm\", time = \"s\", quantity = \"mol\", ");
443 if (eunit == ckr::Cal_per_Mole) {
445 }
else if (eunit == ckr::Kcal_per_Mole) {
447 }
else if (eunit == ckr::Joules_per_Mole) {
449 }
else if (eunit == ckr::Kjoules_per_Mole) {
451 }
else if (eunit == ckr::Kelvin) {
453 }
else if (eunit == ckr::Electron_Volts) {
456 fprintf(f,
"act_energy = \"%s\")\n\n", e_unit.c_str());
459 fprintf(f,
"\nideal_gas(name = \"%s\",\n",idtag.c_str());
462 int nel =
static_cast<int>(r.
elements.size());
464 map<string, string> emap;
466 for (i = 0; i < nel; i++) {
468 elnm[0] = (char) toupper(elnm[0]);
469 if (elnm.size() == 2) {
470 elnm[1] = (char) tolower(elnm[1]);
473 enames +=
" "+elnm+
" ";
475 fprintf(f,
" elements = \"%s\",\n",enames.c_str());
478 int nsp =
static_cast<int>(r.
species.size());
479 for (i = 0; i < nsp; i++) {
480 spnames +=
" "+r.
species[i].name+
" ";
481 if ((i+1) % 10 == 0) {
485 fprintf(f,
" species = \"\"\"%s\"\"\",\n", spnames.c_str());
486 fprintf(f,
" reactions = \"all\",\n");
488 fprintf(f,
" transport = \"Mix\",\n");
490 fprintf(f,
" initial_state = state(temperature = 300.0,\n");
491 fprintf(f,
" pressure = OneAtm)");
494 fprintf(f,
"\n\n\n");
496 fprintf(f,
"# Species data \n");
499 for (i = 0; i < nsp; i++) {
500 addSpecies(f, idtag, r.
species[i]);
503 fprintf(f,
"\n\n\n");
505 fprintf(f,
"# Reaction data \n");
509 int nrxns =
static_cast<int>(r.
reactions.size());
512 string idktag = idtag;
513 for (i = 0; i < nrxns; i++) {
521 fprintf(f,
"\n# [CK Reaction (+%d)]\n",i+1);
522 addReaction(f, idktag, irxn,
525 fprintf(f,
"# [CK Reaction (-%d)]\n",i+1);
526 addReaction(f, idktag, irxn,
535 fprintf(f,
"\n# [CK Reaction (%d)]\n",i+1);
537 addReaction(f, idktag, irxn, r.
reactions[i],
577 int convert_ck(
const char* in_file,
const char* db_file,
578 const char* tr_file,
const char* id_tag,
bool debug,
bool validate)
586 string infile = string(in_file);
587 string dbfile = string(db_file);
588 string trfile = string(tr_file);
590 string idtag = string(id_tag);
599 string::size_type idot = infile.rfind(
'.');
602 ext = infile.substr(idot, infile.size());
603 ctifile = infile.substr(0,idot)+
".cti";
605 ctifile = infile+
".cti";
608 FILE* f = fopen(ctifile.c_str(),
"w");
613 newtime = localtime(&aclock);
631 logfile =
"ck2cti.log";
632 if (!r.
read(infile, dbfile, logfile)) {
634 "error encountered in input file " +
string(infile)
635 +
"\nsee file ck2cti.log for more information.\n");
639 fprintf(f,
"# Generated from file %s\n# by ck2cti on %s#\n",
640 infile.c_str(), asctime(newtime));
642 fprintf(f,
"# Transport data from file %s.\n\n",
644 getTransportData(trfile);
646 bool hastransport = (trfile !=
"");
647 ck2ct(f, idtag, r, hastransport);