10 #include "../../ext/libexecstream/exec-stream.h"
44 const char* py = getenv(
"PYTHON_CMD");
47 string sp =
stripws(
string(py));
55 void ct2ctml(
const char* file,
const int debug)
58 string out_name = file;
62 std::replace_if(out_name.begin(), out_name.end(),
63 std::bind2nd(std::equal_to<char>(),
'\\'),
'/') ;
65 size_t idir = out_name.rfind(
'/');
67 out_name = out_name.substr(idir+1, out_name.size());
69 size_t idot = out_name.rfind(
'.');
71 out_name = out_name.substr(0, idot) +
".xml";
75 std::ofstream out(out_name.c_str());
79 static std::string call_ctml_writer(
const std::string& text,
bool isfile)
81 std::string file, arg;
84 arg =
"r'" + text +
"'";
87 arg =
"text=r'''" + text +
"'''";
95 throw CanteraError(
"ct2ctml",
96 "python cti to ctml conversion requested for file, " + file +
97 ", but not available in this computational environment");
100 string python_output, error_output;
101 int python_exit_code;
103 exec_stream_t python;
104 python.set_wait_timeout(exec_stream_t::s_all, 1800000);
105 stringstream output_stream, error_stream;
106 std::vector<string> args;
107 args.push_back(
"-c");
110 "from __future__ import print_function\n"
113 " from cantera import ctml_writer\n"
114 "except ImportError:\n"
115 " print('sys.path: ' + repr(sys.path) + '\\n', file=sys.stderr)\n"
117 "ctml_writer.convert(" + arg +
", outName='STDOUT')\n"
120 python.start(
pypath(), args.begin(), args.end());
123 while (python.out().good()) {
124 std::getline(python.out(), line);
125 output_stream << line << std::endl;
133 while (python.err().good()) {
134 std::getline(python.err(), line);
135 error_stream << line << std::endl;
138 python_exit_code = python.exit_code();
139 error_output =
stripws(error_stream.str());
140 python_output = output_stream.str();
141 }
catch (std::exception& err) {
143 stringstream message;
144 message <<
"Error executing python while converting input file:\n";
145 message <<
"Python command was: '" <<
pypath() <<
"'\n";
146 message << err.what() << std::endl;
147 throw CanteraError(
"ct2ctml_string", message.str());
150 if (python_exit_code != 0) {
152 stringstream message;
153 message <<
"Error converting input file \"" << file <<
"\" to CTML.\n";
154 message <<
"Python command was: '" <<
pypath() <<
"'\n";
155 message <<
"The exit code was: " << python_exit_code <<
"\n";
156 if (error_output.size() > 0) {
157 message <<
"-------------- start of converter log --------------\n";
158 message << error_output << std::endl;
159 message <<
"--------------- end of converter log ---------------";
161 message <<
"The command did not produce any output." << endl;
163 throw CanteraError(
"ct2ctml_string", message.str());
166 if (error_output.size() > 0) {
168 stringstream message;
169 message <<
"Warning: Unexpected output from CTI converter\n";
170 message <<
"-------------- start of converter log --------------\n";
171 message << error_output << std::endl;
172 message <<
"--------------- end of converter log ---------------\n";
175 return python_output;
180 return call_ctml_writer(file,
true);
185 return call_ctml_writer(cti,
false);
188 void ck2cti(
const std::string& in_file,
const std::string& thermo_file,
189 const std::string& transport_file,
const std::string& id_tag)
196 string ppath = in_file;
198 "python ck to cti conversion requested for file, " + ppath +
199 ", but not available in this computational environment");
202 string python_output;
203 int python_exit_code;
205 exec_stream_t python;
206 python.set_wait_timeout(exec_stream_t::s_all, 1800000);
207 python.start(
pypath(),
"-i");
208 stringstream output_stream;
210 ostream& pyin = python.in();
211 pyin <<
"if True:\n" <<
213 " sys.stderr = sys.stdout\n" <<
215 " from cantera import ck2cti\n" <<
216 " except ImportError:\n" <<
217 " print('sys.path: ' + repr(sys.path))\n" <<
219 " ck2cti.Parser().convertMech(r'" << in_file <<
"',";
220 if (thermo_file !=
"" && thermo_file !=
"-") {
221 pyin <<
" thermoFile=r'" << thermo_file <<
"',";
223 if (transport_file !=
"" && transport_file !=
"-") {
224 pyin <<
" transportFile=r'" << transport_file <<
"',";
226 pyin <<
" phaseName='" << id_tag <<
"',";
227 pyin <<
" permissive=True,";
228 pyin <<
" quiet=True)\n";
229 pyin <<
" sys.exit(0)\n\n";
230 pyin <<
"sys.exit(7)\n";
234 while (python.out().good()) {
235 std::getline(python.out(), line);
236 output_stream << line << std::endl;;
239 python_exit_code = python.exit_code();
240 python_output =
stripws(output_stream.str());
241 }
catch (std::exception& err) {
243 stringstream message;
244 message <<
"Error executing python while converting input file:\n";
245 message <<
"Python command was: '" <<
pypath() <<
"'\n";
246 message << err.what() << std::endl;
250 if (python_exit_code != 0) {
252 stringstream message;
253 message <<
"Error converting input file \"" << in_file <<
"\" to CTI.\n";
254 message <<
"Python command was: '" <<
pypath() <<
"'\n";
255 message <<
"The exit code was: " << python_exit_code <<
"\n";
256 if (python_output.size() > 0) {
257 message <<
"-------------- start of converter log --------------\n";
258 message << python_output << std::endl;
259 message <<
"--------------- end of converter log ---------------";
261 message <<
"The command did not produce any output." << endl;
266 if (python_output.size() > 0) {
268 stringstream message;
269 message <<
"Warning: Unexpected output from CTI converter\n";
270 message <<
"-------------- start of converter log --------------\n";
271 message << python_output << std::endl;
272 message <<
"--------------- end of converter log ---------------\n";
280 "Use get_XML_File instead.");
288 "Use get_XML_File instead.");
XML_Node * get_XML_File(const std::string &file, int debug)
Return a pointer to the XML tree for a Cantera input file.
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.
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
XML_Node getCtmlTree(const std::string &file)
Read an ctml file from a file and fill up an XML tree.
std::string stripws(const std::string &s)
Strip the leading and trailing white space from a string.
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 get_CTML_Tree(XML_Node *rootPtr, const std::string &file, const int debug)
Read an ctml file from a file and fill up an XML tree.
Base class for exceptions thrown by Cantera classes.
void copy(XML_Node *const node_dest) const
Copy all of the information in the current XML_Node tree into the destination XML_Node tree...
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.