14#include "../../ext/libexecstream/exec-stream.h"
47 const char* py = getenv(
"PYTHON_CMD");
58void ct2ctml(
const char* file,
const int debug)
61 string out_name = file;
65 std::replace_if(out_name.begin(), out_name.end(),
66 std::bind2nd(std::equal_to<char>(),
'\\'),
'/');
68 size_t idir = out_name.rfind(
'/');
70 out_name = out_name.substr(idir+1, out_name.size());
72 size_t idot = out_name.rfind(
'.');
74 out_name = out_name.substr(0, idot) +
".xml";
78 std::ofstream out(out_name);
82static std::string call_ctml_writer(
const std::string& text,
bool isfile)
84 std::string file, arg;
88 arg =
"r'" + text +
"'";
91 arg =
"text=r'''" + text +
"'''";
94 string python_output, error_output;
98 python.set_wait_timeout(exec_stream_t::s_all, 1800000);
99 stringstream output_stream, error_stream;
100 python.start(
pypath(),
"");
101 ostream& pyin = python.in();
103 pyin <<
"from __future__ import print_function\n"
107 " from cantera import ctml_writer\n"
108 " except ImportError:\n"
109 " print('sys.path: ' + repr(sys.path) + '\\n', file=sys.stderr)\n"
111 " ctml_writer.convert(";
112 pyin << arg <<
", outName='STDOUT')\n";
113 pyin <<
" sys.exit(0)\n\n";
114 pyin <<
"sys.exit(7)\n";
119 while (python.out().good()) {
120 std::getline(python.out(), line);
121 output_stream << line << std::endl;
129 while (python.err().good()) {
130 std::getline(python.err(), line);
131 error_stream << line << std::endl;
134 python_exit_code = python.exit_code();
135 error_output =
trimCopy(error_stream.str());
136 python_output = output_stream.str();
137 }
catch (std::exception& err) {
139 stringstream message;
140 message <<
"Error executing python while converting input file:\n";
141 message <<
"Python command was: '" <<
pypath() <<
"'\n";
142 message << err.what() << std::endl;
143 throw CanteraError(
"call_ctml_writer", message.str());
146 if (python_exit_code != 0) {
148 stringstream message;
149 message <<
"Error converting input file \"" << file <<
"\" to CTML.\n";
150 message <<
"Python command was: '" <<
pypath() <<
"'\n";
151 message <<
"The exit code was: " << python_exit_code <<
"\n";
152 if (error_output.size() > 0) {
153 message <<
"-------------- start of converter log --------------\n";
154 message << error_output << std::endl;
155 message <<
"--------------- end of converter log ---------------";
157 message <<
"The command did not produce any output." << endl;
159 throw CanteraError(
"call_ctml_writer", message.str());
162 if (error_output.size() > 0) {
164 stringstream message;
165 message <<
"Warning: Unexpected output from CTI converter\n";
166 message <<
"-------------- start of converter log --------------\n";
167 message << error_output << std::endl;
168 message <<
"--------------- end of converter log ---------------\n";
172 return python_output;
177 return call_ctml_writer(file,
true);
182 return call_ctml_writer(cti,
false);
185void ck2cti(
const std::string& in_file,
const std::string& thermo_file,
186 const std::string& transport_file,
const std::string& id_tag)
188 string python_output;
189 int python_exit_code;
191 exec_stream_t python;
192 python.set_wait_timeout(exec_stream_t::s_all, 1800000);
193 python.start(
pypath(),
"-i");
194 stringstream output_stream;
196 ostream& pyin = python.in();
197 pyin <<
"if True:\n" <<
199 " sys.stderr = sys.stdout\n" <<
201 " from cantera import ck2cti\n" <<
202 " except ImportError:\n" <<
203 " print('sys.path: ' + repr(sys.path))\n" <<
205 " _ = ck2cti.convertMech(r'" << in_file <<
"',";
206 if (thermo_file !=
"" && thermo_file !=
"-") {
207 pyin <<
" thermoFile=r'" << thermo_file <<
"',";
209 if (transport_file !=
"" && transport_file !=
"-") {
210 pyin <<
" transportFile=r'" << transport_file <<
"',";
212 pyin <<
" phaseName='" << id_tag <<
"',";
213 pyin <<
" permissive=True,";
214 pyin <<
" quiet=True)\n";
215 pyin <<
" sys.exit(0)\n\n";
216 pyin <<
"sys.exit(7)\n";
220 while (python.out().good()) {
221 std::getline(python.out(), line);
222 output_stream << line << std::endl;;
225 python_exit_code = python.exit_code();
226 python_output =
trimCopy(output_stream.str());
227 }
catch (std::exception& err) {
229 stringstream message;
230 message <<
"Error executing python while converting input file:\n";
231 message <<
"Python command was: '" <<
pypath() <<
"'\n";
232 message << err.what() << std::endl;
236 if (python_exit_code != 0) {
238 stringstream message;
239 message <<
"Error converting input file \"" << in_file <<
"\" to CTI.\n";
240 message <<
"Python command was: '" <<
pypath() <<
"'\n";
241 message <<
"The exit code was: " << python_exit_code <<
"\n";
242 if (python_output.size() > 0) {
243 message <<
"-------------- start of converter log --------------\n";
244 message << python_output << std::endl;
245 message <<
"--------------- end of converter log ---------------";
247 message <<
"The command did not produce any output." << endl;
252 if (python_output.size() > 0) {
254 stringstream message;
255 message <<
"Warning: Unexpected output from ck2cti\n";
256 message <<
"-------------- start of converter log --------------\n";
257 message << python_output << std::endl;
258 message <<
"--------------- end of converter log ---------------\n";
Base class for exceptions thrown by Cantera classes.
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
CTML ("Cantera Markup Language") is the variant of XML that Cantera uses to store data.
This file contains definitions for utility functions and text for modules, inputfiles,...
void writelog(const std::string &fmt, const Args &... args)
Write a formatted message to the screen.
Namespace for the Cantera kernel.
const size_t npos
index returned by functions to indicate "no position"
void ck2cti(const std::string &in_file, const std::string &thermo_file="", const std::string &transport_file="", const std::string &id_tag="gas")
Convert a Chemkin-format mechanism into a CTI file.
std::string trimCopy(const std::string &input)
Trim.
static string pypath()
return the full path to the Python interpreter.
Contains declarations for string manipulation functions within Cantera.