From 430e28e41372fce1ba0264319f57ceb15abad37f Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Fri, 1 Feb 2013 23:40:31 +0000 Subject: [PATCH] [Cython] Added write_csv method to FlameBase --- .../examples/onedim/adiabatic_flame.py | 14 +------- .../cantera/examples/onedim/burner_flame.py | 16 +--------- .../examples/onedim/diffusion_flame.py | 16 +--------- interfaces/cython/cantera/onedim.pyx | 32 +++++++++++++++++++ 4 files changed, 35 insertions(+), 43 deletions(-) diff --git a/interfaces/cython/cantera/examples/onedim/adiabatic_flame.py b/interfaces/cython/cantera/examples/onedim/adiabatic_flame.py index e0b499963..516e19a91 100644 --- a/interfaces/cython/cantera/examples/onedim/adiabatic_flame.py +++ b/interfaces/cython/cantera/examples/onedim/adiabatic_flame.py @@ -3,7 +3,6 @@ A freely-propagating, premixed hydrogen flat flame with multicomponent transport properties. """ -import csv import cantera as ct # Simulation parameters @@ -58,15 +57,4 @@ f.save('h2_adiabatic.xml','energy_multi', 'solution with multicomponent transport') # write the velocity, temperature, density, and mole fractions to a CSV file -z = f.flame.grid -T = f.T -u = f.u -V = f.V - -with open('h2_adiabatic.csv', 'w') as csvfile: - writer = csv.writer(csvfile) - writer.writerow(['z (m)', 'u (m/s)', 'V (1/s)', 'T (K)', 'rho (kg/m3)'] + - list(gas.species_names)) - for n in range(f.flame.n_points): - f.set_gas_state(n) - writer.writerow([z[n], u[n], V[n], T[n], gas.density] + list(gas.X)) +f.write_csv('h2_adiabatic.csv', quiet=False) diff --git a/interfaces/cython/cantera/examples/onedim/burner_flame.py b/interfaces/cython/cantera/examples/onedim/burner_flame.py index acde753c8..f2c036e41 100644 --- a/interfaces/cython/cantera/examples/onedim/burner_flame.py +++ b/interfaces/cython/cantera/examples/onedim/burner_flame.py @@ -3,7 +3,6 @@ A burner-stabilized lean premixed hydrogen-oxygen flame at low pressure. """ import cantera as ct -import csv p = 0.05 * ct.one_atm tburner = 373.0 @@ -52,17 +51,4 @@ print('multicomponent flamespeed = ', f.u[0]) f.save('h2_burner_flame.xml','energy_multi', 'solution with the energy equation enabled and multicomponent transport') -z = f.flame.grid -T = f.T -u = f.u -V = f.V - -with open('h2_burner_flame.csv', 'w') as csvfile: - writer = csv.writer(csvfile) - writer.writerow(['z (m)', 'u (m/s)', 'V (1/s)', 'T (K)', 'rho (kg/m3)'] + - list(gas.species_names)) - for n in range(f.flame.n_points): - f.set_gas_state(n) - writer.writerow([z[n], u[n], V[n], T[n], gas.density] + list(gas.X)) - -print('solution saved to h2_burner_flame.csv') +f.write_csv('h2_burner_flame.csv', quiet=False) diff --git a/interfaces/cython/cantera/examples/onedim/diffusion_flame.py b/interfaces/cython/cantera/examples/onedim/diffusion_flame.py index e402ef87a..ceb33b8b3 100644 --- a/interfaces/cython/cantera/examples/onedim/diffusion_flame.py +++ b/interfaces/cython/cantera/examples/onedim/diffusion_flame.py @@ -4,7 +4,6 @@ An opposed-flow ethane/air diffusion flame import cantera as ct import numpy as np -import csv # Input parameters p = ct.one_atm # pressure @@ -68,20 +67,7 @@ f.solve(loglevel, refine_grid=refine_grid) f.show_solution() f.save('c2h6_diffusion.xml') -z = f.flame.grid -T = f.T -u = f.u -V = f.V - # write the velocity, temperature, and mole fractions to a CSV file -with open('c2h6_diffusion.csv', 'w') as csvfile: - writer = csv.writer(csvfile) - writer.writerow(['z (m)', 'u (m/s)', 'V (1/s)', 'T (K)', 'rho (kg/m3)'] + - list(gas.species_names)) - for n in range(f.flame.n_points): - f.set_gas_state(n) - writer.writerow([z[n], u[n], V[n], T[n], gas.density] + list(gas.X)) - -print('solution saved to c2h6_diffusion.csv') +f.write_csv('c2h6_diffusion.csv', quiet=False) f.show_stats(0) diff --git a/interfaces/cython/cantera/onedim.pyx b/interfaces/cython/cantera/onedim.pyx index 6a7f2c624..4ce022c2a 100644 --- a/interfaces/cython/cantera/onedim.pyx +++ b/interfaces/cython/cantera/onedim.pyx @@ -1,3 +1,5 @@ +import csv + cdef class Domain1D: cdef CxxDomain1D* domain def __cinit__(self, *args, **kwargs): @@ -831,6 +833,36 @@ class FlameBase(Sim1D): for k in range(k0, k0 + self.gas.n_species)] self.gas.TPY = self.value(self.flame, 'T', point), self.P, Y + def write_csv(self, filename, species='X', quiet=True): + """ + Write the velocity, temperature, density, and species profiles + to a CSV file. + + :param filename: + Output file name + :param species: + Attribute to use obtaining species profiles, e.g. ``X`` for + mole fractions or ``Y`` for mass fractions. + """ + + z = self.grid + T = self.T + u = self.u + V = self.V + + csvfile = open(filename, 'w') + writer = csv.writer(csvfile) + writer.writerow(['z (m)', 'u (m/s)', 'V (1/s)', + 'T (K)', 'rho (kg/m3)'] + self.gas.species_names) + for n in range(self.flame.n_points): + self.set_gas_state(n) + writer.writerow([z[n], u[n], V[n], T[n], self.gas.density] + + list(getattr(self.gas, species))) + csvfile.close() + if not quiet: + print("Solution saved to '{}'.".format(filename)) + + def _trim(docstring): """Remove block indentation from a docstring.""" if not docstring: