Skip to content

Commit

Permalink
Merge pull request firemodels#13285 from cxp484/master
Browse files Browse the repository at this point in the history
Reduce number of timesteps to write in Cantera output;Add Cantera output csv.
  • Loading branch information
mcgratta authored Aug 6, 2024
2 parents f51591a + 3d3da13 commit 4eb777b
Show file tree
Hide file tree
Showing 2 changed files with 249 additions and 5 deletions.
32 changes: 27 additions & 5 deletions Utilities/Python/scripts/cantera_ignition_delay.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
equivRatios = np.array([0.6, 1.0, 1.4])
T = np.array([900, 1000, 1100, 1200])


csvdata = pd.DataFrame()
caseCount = 0
writeInterval = 20

for phi in equivRatios:
estimated_ignition_delay_times = np.ones_like(T, dtype=float)
Expand All @@ -38,6 +38,7 @@
for i, state in enumerate(ignition_delays):
caseCount = caseCount +1
stateArr = []
stateArrReduced = []
# Setup the gas and reactor
gas.TPX = state.TPX
r = ct.IdealGasReactor(contents=gas, name="Batch Reactor")
Expand All @@ -54,17 +55,29 @@
time_history.append(t)
reference_species_history.append(gas[reference_species].X[0])
stateArr.append([t, gas[reference_species].Y[0],gas.T-273.15 ])

i_ign = np.array(reference_species_history).argmax()
tau = time_history[i_ign]
t1 = time.time()
stateArrDF = pd.DataFrame(stateArr)

# Reduce number of timesteps to reduce file size
# Before ignition delay+0.1 s write after every writeInterval variable
# After that write all the times.
tCount = 0
for j in range(len(stateArr)):
t= stateArr[j][0]
if (t < tau + 0.1):
if (tCount%writeInterval ==0):
stateArrReduced.append(stateArr[j])
else:
stateArrReduced.append(stateArr[j])
tCount = tCount+1
stateArrDF = pd.DataFrame(stateArrReduced)

caseIndx = str(caseCount)
if caseCount ==1:
csvdata = stateArrDF
else:
# Pad the smaller DataFrame with NaNs to match the number of rows
#Pad the smaller DataFrame with NaNs to match the number of rows
if csvdata.shape[0] > stateArrDF.shape[0]:
pad_rows = csvdata.shape[0] - stateArrDF.shape[0]
stateArrDF = pd.concat([stateArrDF, pd.DataFrame(np.full((pad_rows, stateArrDF.shape[1]), np.nan))], axis=0, ignore_index=True)
Expand All @@ -78,11 +91,20 @@

ignition_delays[i].tau = tau

# Set Dataframe column headers
# Set Dataframe column headers and writing format
timeFormat = '{:10.5f}'
OHFormat = '{:.3E}'
TMPFormat = '{:10.2f}'
colPerCase = 3 # Time, OH, TMP
for i in range(caseCount):
caseIndx = str(i+1)
csvdata = csvdata.rename(columns={i*colPerCase: 'Time'+caseIndx, i*colPerCase+1: 'OH'+caseIndx, i*colPerCase+2: 'TMP'+caseIndx})
csvdata['Time'+caseIndx] = csvdata['Time'+caseIndx].map(lambda x: timeFormat.format(x))
csvdata['OH'+caseIndx] = csvdata['OH'+caseIndx].map(lambda x: OHFormat.format(x))
csvdata['TMP'+caseIndx] = csvdata['TMP'+caseIndx].map(lambda x: TMPFormat.format(x))

csvdata = csvdata.apply(lambda x: x.str.strip() if x.dtype == 'object' else x)
csvdata.replace('nan','',inplace=True)
csvdata.replace('NAN','',inplace=True)
csvdata.to_csv(Chemistry_DIR+"cantera_ignition_delay.csv",index=False)

Loading

0 comments on commit 4eb777b

Please sign in to comment.