12 #include "../../ext/libexecstream/exec-stream.h"
45 const char* py = getenv(
"PYTHON_CMD");
56 void ct2ctml(
const char* file,
const int debug)
59 string out_name = file;
63 std::replace_if(out_name.begin(), out_name.end(),
64 std::bind2nd(std::equal_to<char>(),
'\\'),
'/');
66 size_t idir = out_name.rfind(
'/');
68 out_name = out_name.substr(idir+1, out_name.size());
70 size_t idot = out_name.rfind(
'.');
72 out_name = out_name.substr(0, idot) +
".xml";
76 std::ofstream out(out_name);
80 static std::string call_ctml_writer(
const std::string& text,
bool isfile)
82 std::string file, arg;
86 arg =
"r'" + text +
"'";
89 arg =
"text=r'''" + text +
"'''";
92 string python_output, error_output;
96 python.set_wait_timeout(exec_stream_t::s_all, 1800000);
97 stringstream output_stream, error_stream;
98 python.start(
pypath(),
"");
99 ostream& pyin = python.in();
101 pyin <<
"from __future__ import print_function\n"
105 " from cantera import ctml_writer\n"
106 " except ImportError:\n"
107 " print('sys.path: ' + repr(sys.path) + '\\n', file=sys.stderr)\n"
109 " ctml_writer.convert(";
110 pyin << arg <<
", outName='STDOUT')\n";
111 pyin <<
" sys.exit(0)\n\n";
112 pyin <<
"sys.exit(7)\n";
117 while (python.out().good()) {
118 std::getline(python.out(), line);
119 output_stream << line << std::endl;
127 while (python.err().good()) {
128 std::getline(python.err(), line);
129 error_stream << line << std::endl;
132 python_exit_code = python.exit_code();
133 error_output =
trimCopy(error_stream.str());
134 python_output = output_stream.str();
135 }
catch (std::exception& err) {
137 stringstream message;
138 message <<
"Error executing python while converting input file:\n";
139 message <<
"Python command was: '" <<
pypath() <<
"'\n";
140 message << err.what() << std::endl;
141 throw CanteraError(
"call_ctml_writer", message.str());
144 if (python_exit_code != 0) {
146 stringstream message;
147 message <<
"Error converting input file \"" << file <<
"\" to CTML.\n";
148 message <<
"Python command was: '" <<
pypath() <<
"'\n";
149 message <<
"The exit code was: " << python_exit_code <<
"\n";
150 if (error_output.size() > 0) {
151 message <<
"-------------- start of converter log --------------\n";
152 message << error_output << std::endl;
153 message <<
"--------------- end of converter log ---------------";
155 message <<
"The command did not produce any output." << endl;
157 throw CanteraError(
"call_ctml_writer", message.str());
160 if (error_output.size() > 0) {
162 stringstream message;
163 message <<
"Warning: Unexpected output from CTI converter\n";
164 message <<
"-------------- start of converter log --------------\n";
165 message << error_output << std::endl;
166 message <<
"--------------- end of converter log ---------------\n";
170 return python_output;
175 return call_ctml_writer(file,
true);
180 return call_ctml_writer(cti,
false);
183 void ck2cti(
const std::string& in_file,
const std::string& thermo_file,
184 const std::string& transport_file,
const std::string& id_tag)
186 string python_output;
187 int python_exit_code;
189 exec_stream_t python;
190 python.set_wait_timeout(exec_stream_t::s_all, 1800000);
191 python.start(
pypath(),
"-i");
192 stringstream output_stream;
194 ostream& pyin = python.in();
195 pyin <<
"if True:\n" <<
197 " sys.stderr = sys.stdout\n" <<
199 " from cantera import ck2cti\n" <<
200 " except ImportError:\n" <<
201 " print('sys.path: ' + repr(sys.path))\n" <<
203 " _ = ck2cti.convertMech(r'" << in_file <<
"',";
204 if (thermo_file !=
"" && thermo_file !=
"-") {
205 pyin <<
" thermoFile=r'" << thermo_file <<
"',";
207 if (transport_file !=
"" && transport_file !=
"-") {
208 pyin <<
" transportFile=r'" << transport_file <<
"',";
210 pyin <<
" phaseName='" << id_tag <<
"',";
211 pyin <<
" permissive=True,";
212 pyin <<
" quiet=True)\n";
213 pyin <<
" sys.exit(0)\n\n";
214 pyin <<
"sys.exit(7)\n";
218 while (python.out().good()) {
219 std::getline(python.out(), line);
220 output_stream << line << std::endl;;
223 python_exit_code = python.exit_code();
224 python_output =
trimCopy(output_stream.str());
225 }
catch (std::exception& err) {
227 stringstream message;
228 message <<
"Error executing python while converting input file:\n";
229 message <<
"Python command was: '" <<
pypath() <<
"'\n";
230 message << err.what() << std::endl;
234 if (python_exit_code != 0) {
236 stringstream message;
237 message <<
"Error converting input file \"" << in_file <<
"\" to CTI.\n";
238 message <<
"Python command was: '" <<
pypath() <<
"'\n";
239 message <<
"The exit code was: " << python_exit_code <<
"\n";
240 if (python_output.size() > 0) {
241 message <<
"-------------- start of converter log --------------\n";
242 message << python_output << std::endl;
243 message <<
"--------------- end of converter log ---------------";
245 message <<
"The command did not produce any output." << endl;
250 if (python_output.size() > 0) {
252 stringstream message;
253 message <<
"Warning: Unexpected output from ck2cti\n";
254 message <<
"-------------- start of converter log --------------\n";
255 message << python_output << std::endl;
256 message <<
"--------------- end of converter log ---------------\n";
Base class for exceptions thrown by Cantera classes.
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"
void writelog(const std::string &fmt, const Args &... args)
Write a formatted message to the screen.
Namespace for the Cantera kernel.
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.
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.