13 #include "../../ext/libexecstream/exec-stream.h"
19 using namespace Cantera;
44 const char* py = getenv(
"PYTHON_CMD");
47 string sp =
stripws(
string(py));
55 void ct2ctml(
const char* file,
const int debug)
64 "python cti to ctml conversion requested for file, " + ppath +
65 ", but not available in this computational environment");
72 python.set_wait_timeout(exec_stream_t::s_all, 1800000);
73 python.start(
pypath(),
"-i");
74 stringstream output_stream;
78 " sys.stderr = sys.stdout\n" <<
80 " from cantera import ctml_writer\n" <<
81 " except ImportError:\n" <<
82 " import ctml_writer\n" <<
83 " ctml_writer.convert(r'" << file <<
"')\n" <<
88 while (python.out().good()) {
89 std::getline(python.out(), line);
90 output_stream << line << std::endl;;
93 python_exit_code = python.exit_code();
94 python_output =
stripws(output_stream.str());
95 }
catch (std::exception& err) {
98 message <<
"Error executing python while converting input file:\n";
99 message <<
"Python command was: '" <<
pypath() <<
"'\n";
100 message << err.what() << std::endl;
104 if (python_exit_code != 0) {
106 stringstream message;
107 message <<
"Error converting input file \"" << file <<
"\" to CTML.\n";
108 message <<
"Python command was: '" <<
pypath() <<
"'\n";
109 message <<
"The exit code was: " << python_exit_code <<
"\n";
110 if (python_output.size() > 0) {
111 message <<
"-------------- start of converter log --------------\n";
112 message << python_output << std::endl;
113 message <<
"--------------- end of converter log ---------------";
115 message <<
"The command did not produce any output." << endl;
120 if (python_output.size() > 0) {
122 stringstream message;
123 message <<
"Warning: Unexpected output from CTI converter\n";
124 message <<
"-------------- start of converter log --------------\n";
125 message << python_output << std::endl;
126 message <<
"--------------- end of converter log ---------------\n";
131 void ck2cti(
const std::string& in_file,
const std::string& thermo_file,
132 const std::string& transport_file,
const std::string& id_tag)
139 string ppath = in_file;
141 "python ck to cti conversion requested for file, " + ppath +
142 ", but not available in this computational environment");
145 string python_output;
146 int python_exit_code;
148 exec_stream_t python;
149 python.set_wait_timeout(exec_stream_t::s_all, 1800000);
150 python.start(
pypath(),
"-i");
151 stringstream output_stream;
153 ostream& pyin = python.in();
154 pyin <<
"if True:\n" <<
156 " sys.stderr = sys.stdout\n" <<
158 " from cantera import ck2cti\n" <<
159 " except ImportError:\n" <<
160 " import ck2cti\n" <<
161 " ck2cti.Parser().convertMech(r'" << in_file <<
"',";
162 if (thermo_file !=
"" && thermo_file !=
"-") {
163 pyin <<
" thermoFile=r'" << thermo_file <<
"',";
165 if (transport_file !=
"" && transport_file !=
"-") {
166 pyin <<
" transportFile=r'" << transport_file <<
"',";
168 pyin <<
" phaseName='" << id_tag <<
"',";
169 pyin <<
" permissive=True,";
170 pyin <<
" quiet=True)\n";
171 pyin <<
" sys.exit(0)\n\n";
172 pyin <<
"sys.exit(7)\n";
176 while (python.out().good()) {
177 std::getline(python.out(), line);
178 output_stream << line << std::endl;;
181 python_exit_code = python.exit_code();
182 python_output =
stripws(output_stream.str());
183 }
catch (std::exception& err) {
185 stringstream message;
186 message <<
"Error executing python while converting input file:\n";
187 message <<
"Python command was: '" <<
pypath() <<
"'\n";
188 message << err.what() << std::endl;
192 if (python_exit_code != 0) {
194 stringstream message;
195 message <<
"Error converting input file \"" << in_file <<
"\" to CTI.\n";
196 message <<
"Python command was: '" <<
pypath() <<
"'\n";
197 message <<
"The exit code was: " << python_exit_code <<
"\n";
198 if (python_output.size() > 0) {
199 message <<
"-------------- start of converter log --------------\n";
200 message << python_output << std::endl;
201 message <<
"--------------- end of converter log ---------------";
203 message <<
"The command did not produce any output." << endl;
208 if (python_output.size() > 0) {
210 stringstream message;
211 message <<
"Warning: Unexpected output from CTI converter\n";
212 message <<
"-------------- start of converter log --------------\n";
213 message << python_output << std::endl;
214 message <<
"--------------- end of converter log ---------------\n";
221 std::string ff, ext =
"";
225 writelog(
"Found file: "+inname+
"\n", debug);
228 throw CanteraError(
"get_CTML_Tree",
"file "+file+
" not found");
235 std::string::size_type idot = inname.rfind(
'.');
237 ext = inname.substr(idot, inname.size());
239 if (ext !=
".xml" && ext !=
".ctml") {
242 }
catch (std::exception& err) {
243 writelog(
"get_CTML_Tree: caught an exception:\n");
246 string ffull = inname.substr(0,idot) +
".xml";
249 writelogf(
"ffull name = %s\n", ffull.c_str());
255 writelog(
"Attempting to parse xml file " + ff +
"\n", debug);
256 ifstream fin(ff.c_str());
260 "XML file " + ff +
" not found");
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"
Class XML_Node is a tree-based representation of the contents of an XML file.
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
This file contains definitions for utility functions and text for modules, inputfiles, logs, textlogs, HTML_logs (see Input File Handling, Diagnostic Output, Writing messages to the screen and Writing HTML Logfiles).
std::string getBaseName(const std::string &path)
Get the file name without the path or extension.
void ck2cti(const std::string &in_file, const std::string &thermo_file, const std::string &transport_file, const std::string &id_tag)
Convert a Chemkin-format mechanism into a CTI file.
void writelogf(const char *fmt,...)
Write a formatted message to the screen.
std::string stripws(const std::string &s)
Strip the leading and trailing white space from a string.
Base class for exceptions thrown by Cantera classes.
static string pypath()
return the full path to the Python interpreter.
Contains declarations for string manipulation functions within Cantera.
void writelog(const std::string &msg)
Write a message to the screen.
void get_CTML_Tree(Cantera::XML_Node *rootPtr, const std::string &file, const int debug)
Read an ctml file from a file and fill up an XML tree.
void build(std::istream &f)
Main routine to create an tree-like representation of an XML file.
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
Cantera::XML_Node getCtmlTree(const std::string &file)
Read an ctml file from a file and fill up an XML tree.