Hi all,
I want to save gas species molar fraction, temperature, distance, time ...how can I do this with pandas? The code is below.
Best regards to all
Tullio Viola
"""
Created by
@author: Tullio Viola
email:
dblabtul...@gmail.com"""
from __future__ import division
from __future__ import print_function
from bigfloat import *
from builtins import *
from decimal import*
from math import*
import numpy as np
import time
# Initialize Cantera for numerical simulation
import pandas as pd
import numpy as np
import time
import cantera as ct
#######################################################################
# Input Parameters
#######################################################################
T_0 = 1500.0 # inlet temperature [K]
pressure = ct.one_atm # constant pressure [Pa]
composition_0 = 'H2:2, O2:1, AR:0.1'
length = 1.5e-7 # *approximate* PFR length [m]
u_0 = .006 # inflow velocity [m/s]
area = 1.e-4 # cross-sectional area [m**2]
# input file containing the reaction mechanism
reaction_mechanism = 'ARAMCO.cti'
# Resolution: The PFR will be simulated by 'n_steps' time steps or by a chain
# of 'n_steps' stirred reactors.
n_steps = 2000
#####################################################################
# import the gas model and set the initial conditions
gas = ct.Solution(reaction_mechanism)
gas.TPX = T_0, pressure, composition_0
mass_flow_rate = u_0 * gas.density * area
dz = length / n_steps
r_vol = area * dz
# create a new reactor
r = ct.IdealGasReactor(gas)
r.volume = r_vol
# create a reservoir to represent the reactor immediately upstream. Note
# that the gas object is set already to the state of the upstream reactor
upstream = ct.Reservoir(gas, name='upstream')
# create a reservoir for the reactor to exhaust into. The composition of
# this reservoir is irrelevant.
downstream = ct.Reservoir(gas, name='downstream')
# The mass flow rate into the reactor will be fixed by using a
# MassFlowController object.
m = ct.MassFlowController(upstream, r, mdot=mass_flow_rate)
# We need an outlet to the downstream reservoir. This will determine the
# pressure in the reactor. The value of K will only affect the transient
# pressure difference.
v = ct.PressureController(r, downstream, master=m, K=1e-5)
sim = ct.ReactorNet([r])
# Now compile a list of all variables for which we will store data
columnNames = [r.component_name(item) for item in range(r.n_vars)]
columnNames = ['pressure'] + columnNames
PFR_test = pd.DataFrame(columns=columnNames)
# Start the stopwatch
tic = time.time()
# define time, space, and other information vectors
z = (np.arange(n_steps) + 1) * dz
t_r = np.zeros_like(z) # residence time in each reactor
u = np.zeros_like(z)
t = np.zeros_like(z)
states = ct.SolutionArray(r.thermo)
# iterate through the PFR cells
for n in range(n_steps):
# Set the state of the reservoir to match that of the previous reactor
gas.TDY = r.thermo.TDY
upstream.syncState()
# integrate the reactor forward in time until steady state is reached
sim.reinitialize()
sim.advance_to_steady_state()
# compute velocity and transform into time
u[n] = mass_flow_rate / area / r.thermo.density
t_r[n] = r.mass / mass_flow_rate # residence time in this reactor
t[n] = np.sum(t_r)
# write output data
states.append(r.thermo.state)
# Stop the stopwatch
toc = time.time()
PFR_test.loc[t[n]] = states
#####################################################################
PFR_test.to_csv("PFR_test.csv")
#####################################################################
# Compare Results in matplotlib
#####################################################################
import matplotlib.pyplot as plt
plt.figure()
plt.plot(z, states.T, label='Lagrangian Particle')
plt.xlabel('$z$ [m]')
plt.ylabel('$T$ [K]')
plt.legend(loc=0)
plt.show()
plt.savefig('pfr_T_z.png')
plt.figure()
plt.plot(t, states.X[:, gas.species_index('H2')], label='Lagrangian Particle')
plt.xlabel('$t$ [s]')
plt.ylabel('$X_{H_2}$ [-]')
plt.legend(loc=0)
plt.show()
plt.savefig('pfr_XH2_t.png')