From ff9584105bd72155a34929f613521fbd8eb2bbca Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 30 Jan 2014 00:47:37 +0000 Subject: [PATCH] Do implicit CTI to XML conversions without generating .xml files When instantiating a phase from a .cti file, do the conversion in memory, without writing the XML representation to disk. This eliminates the unrequrested XML files that Cantera normally generates, and also avoids errors when running Cantera from a directory where the user does not have write permissons. --- include/cantera/base/ctml.h | 9 ++ interfaces/cython/cantera/ctml_writer.py | 15 ++- src/base/application.cpp | 11 ++- src/base/ct2ctml.cpp | 116 ++++++++++++++--------- test/SConscript | 37 ++++---- test/matlab/testImport.m | 1 - test/thermo/phaseConstructors.cpp | 35 +++++++ test_problems/SConscript | 7 +- 8 files changed, 156 insertions(+), 75 deletions(-) diff --git a/include/cantera/base/ctml.h b/include/cantera/base/ctml.h index f0293d217..c45a98011 100644 --- a/include/cantera/base/ctml.h +++ b/include/cantera/base/ctml.h @@ -818,6 +818,15 @@ Cantera::XML_Node getCtmlTree(const std::string& file); */ void ct2ctml(const char* file, const int debug = 0); +//! Get a string with the ctml representation of a cti file. +/*! + * @param file Path to the input file in CTI format + * @return String containing the xml representation of the input file + * + * @ingroup inputfiles + */ +std::string ct2ctml_string(const std::string& file); + //! Convert a Chemkin-format mechanism into a CTI file. /*! * @param in_file input file containing species and reactions diff --git a/interfaces/cython/cantera/ctml_writer.py b/interfaces/cython/cantera/ctml_writer.py index afa6e6b94..245f759e9 100644 --- a/interfaces/cython/cantera/ctml_writer.py +++ b/interfaces/cython/cantera/ctml_writer.py @@ -18,6 +18,8 @@ from __future__ import print_function +import sys + class CTI_Error(Exception): """Exception raised if an error is encountered while parsing the input file. @@ -130,8 +132,11 @@ class XMLnode(object): s = ['\n'] self._write(s, 0) s.append('\n') - with open(filename, 'w') as f: - f.write(''.join(s)) + if isinstance(filename, str): + with open(filename, 'w') as f: + f.write(''.join(s)) + else: + filename.write(''.join(s)) def write_comment(self, s, level): s.append('\n'+indent[level]+'