diff --git a/include/cantera/base/ctml.h b/include/cantera/base/ctml.h index a068852e8..a430501aa 100644 --- a/include/cantera/base/ctml.h +++ b/include/cantera/base/ctml.h @@ -895,6 +895,17 @@ void get_CTML_Tree(Cantera::XML_Node* node, const std::string file, * @ingroup inputfiles */ void ct2ctml(const char* file, const int debug = 0); + +//! Convert a Chemkin-format mechanism into a CTI file. +/*! + * @param in_file input file containing species and reactions + * @param thermo_file optional input file containing thermo data + * @param transport_file optional input file containing transport parameters + * @param id_tag id of the phase + */ +void ck2cti(const std::string& in_file, const std::string& thermo_file="", + const std::string& transport_file="", + const std::string& id_tag="gas"); } #endif diff --git a/interfaces/matlab/toolbox/ck2cti.m b/interfaces/matlab/toolbox/ck2cti.m index 4d2fefafb..926f09968 100644 --- a/interfaces/matlab/toolbox/ck2cti.m +++ b/interfaces/matlab/toolbox/ck2cti.m @@ -13,15 +13,6 @@ function f = ck2cti(infile, thermo, transport) % created will also contain transport property parameters. The % function return value is a string containing the output file % name. -% -%prog = [ctbin,'/ck2cti']; - -% set this to zero to turn off mechanism validation -validate = 1; - -% set this to one to turn on debugging. Use only if ck2cti -% fails, and you want to see how the parser is parsing the input file. -debug = 0; if nargin == 0 error('input file name must be supplied') @@ -41,18 +32,6 @@ else outfile = [infile '.cti']; end -iok = ctmethods(0,1, infile, thermo, transport, idtag, debug, validate); +iok = ctmethods(0,1, infile, thermo, transport, idtag, 0, 0); -%iok = system([prog,' -i ',infile,' -t ',thermo,' -tr ',transport, ... -% ' -id ',idtag,' > ',outfile]); -if iok - %ierr2 = system([prog,' > log']) - %if ierr2 - % error(['Program ck2cti is not found at ',prog,['. Edit file' ... - % [' ctbin.m to point to the Cantera bin directory.']]]) - % else - error(['Error occurred while running ck2cti. Check file ck2cti.log' ... - ' for error messages.']); - %end -end f = outfile; diff --git a/src/base/ct2ctml.cpp b/src/base/ct2ctml.cpp index 0a9a1280c..59c484e3f 100644 --- a/src/base/ct2ctml.cpp +++ b/src/base/ct2ctml.cpp @@ -135,6 +135,90 @@ void ct2ctml(const char* file, const int debug) } } +void ck2cti(const std::string& in_file, const std::string& thermo_file, + const std::string& transport_file, const std::string& id_tag) +{ +#ifdef HAS_NO_PYTHON + /* + * Section to bomb out if python is not + * present in the computation environment. + */ + string ppath = file; + throw CanteraError("ct2ctml", + "python ck to cti conversion requested for file, " + ppath + + ", but not available in this computational environment"); +#endif + + string python_output; + int python_exit_code; + try { + exec_stream_t python; + python.set_wait_timeout(exec_stream_t::s_all, 1800000); // 30 minutes + python.start(pypath(), "-i"); + stringstream output_stream; + + ostream& pyin = python.in(); + pyin << "if True:\n"; // Use this so that the rest is a single block + pyin << " import sys\n"; + pyin << " sys.stderr = sys.stdout\n"; + pyin << " import ck2cti\n"; + pyin << " ck2cti.convertMech(r'" << in_file << "',"; + if (thermo_file != "" && thermo_file != "-") { + pyin << " thermoFile=r'" << thermo_file << "',"; + } + if (transport_file != "" && transport_file != "-") { + pyin << " transportFile=r'" << transport_file << "',"; + } + pyin << " phaseName='" << id_tag << "',"; + pyin << " quiet=True)\n"; + pyin << " sys.exit(0)\n\n"; + pyin << "sys.exit(7)\n"; + python.close_in(); + + std::string line; + while (python.out().good()) { + std::getline(python.out(), line); + output_stream << line << std::endl;; + } + python.close(); + python_exit_code = python.exit_code(); + python_output = stripws(output_stream.str()); + } catch (std::exception& err) { + // Report failure to execute Python + stringstream message; + message << "Error executing python while converting input file:\n"; + message << "Python command was: '" << pypath() << "'\n"; + message << err.what() << std::endl; + throw CanteraError("ct2ctml", message.str()); + } + + if (python_exit_code != 0) { + // Report a failure in the conversion process + stringstream message; + message << "Error converting input file \"" << in_file << "\" to CTI.\n"; + message << "Python command was: '" << pypath() << "'\n"; + message << "The exit code was: " << python_exit_code << "\n"; + if (python_output.size() > 0) { + message << "-------------- start of converter log --------------\n"; + message << python_output << std::endl; + message << "--------------- end of converter log ---------------"; + } else { + message << "The command did not produce any output." << endl; + } + throw CanteraError("ck2cti", message.str()); + } + + if (python_output.size() > 0) { + // Warn if there was any output from the conversion process + stringstream message; + message << "Warning: Unexpected output from CTI converter\n"; + message << "-------------- start of converter log --------------\n"; + message << python_output << std::endl; + message << "--------------- end of converter log ---------------\n"; + writelog(message.str()); + } +} + // Read an ctml file from a file and fill up an XML tree /* diff --git a/src/clib/ct.cpp b/src/clib/ct.cpp index 3894332c7..70dbc11fe 100644 --- a/src/clib/ct.cpp +++ b/src/clib/ct.cpp @@ -18,7 +18,6 @@ #include "cantera/base/ctml.h" #include "cantera/kinetics/importKinetics.h" #include "cantera/thermo/ThermoFactory.h" -#include "converters/ck2ct.h" #include "Cabinet.h" #include "cantera/kinetics/InterfaceKinetics.h" #include "cantera/thermo/PureFluidPhase.h" @@ -1552,9 +1551,8 @@ extern "C" { char* id_tag, int debug, int validate) { try { - bool dbg = (debug != 0); - bool val = (validate != 0); - return pip::convert_ck(in_file, db_file, tr_file, id_tag, dbg, val); + ctml::ck2cti(in_file, db_file, tr_file, id_tag); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); }