Skip to content

Commit

Permalink
[samples] Update file IO
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Mar 21, 2023
1 parent b7717ac commit 3ea2510
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 21 deletions.
18 changes: 6 additions & 12 deletions samples/cxx/flamespeed/flamespeed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "cantera/onedim.h"
#include "cantera/oneD/DomainFactory.h"
#include "cantera/base/stringUtils.h"
#include <fstream>

using namespace Cantera;
using fmt::print;
Expand Down Expand Up @@ -125,10 +124,7 @@ int flamespeed(double phi, bool refine_grid, int loglevel)
} else {
fileName = "flamespeed.yaml";
}
if (std::ifstream(fileName).good()) {
std::remove(fileName.c_str());
}
flame.save(fileName, "initial-guess", "Initial guess", 0);
flame.save(fileName, "initial-guess", "Initial guess", true);

// Solve freely propagating flame

Expand All @@ -141,18 +137,16 @@ int flamespeed(double phi, bool refine_grid, int loglevel)
flame.solve(loglevel,refine_grid);
double flameSpeed_mix = flame.value(flowdomain,
flow->componentIndex("velocity"),0);
print("Flame speed with mixture-averaged transport: {} m/s\n",
flameSpeed_mix);
flame.save(fileName, "mix", "Solution with mixture-averaged transport", 0);
print("Flame speed with mixture-averaged transport: {} m/s\n", flameSpeed_mix);
flame.save(fileName, "mix", "Solution with mixture-averaged transport", true);

// now switch to multicomponent transport
flow->setTransportModel("multicomponent");
flame.solve(loglevel, refine_grid);
double flameSpeed_multi = flame.value(flowdomain,
flow->componentIndex("velocity"),0);
print("Flame speed with multicomponent transport: {} m/s\n",
flameSpeed_multi);
flame.save(fileName, "multi", "Solution with multicomponent transport", 0);
print("Flame speed with multicomponent transport: {} m/s\n", flameSpeed_multi);
flame.save(fileName, "multi", "Solution with multicomponent transport", true);

// now enable Soret diffusion
flow->enableSoret(true);
Expand All @@ -162,7 +156,7 @@ int flamespeed(double phi, bool refine_grid, int loglevel)
print("Flame speed with multicomponent transport + Soret: {} m/s\n",
flameSpeed_full);
flame.save(fileName, "soret",
"Solution with mixture-averaged transport and Soret", 0);
"Solution with mixture-averaged transport and Soret", true);

vector_fp zvec,Tvec,COvec,CO2vec,Uvec;

Expand Down
10 changes: 5 additions & 5 deletions samples/python/onedim/diffusion_flame_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ def names(test):
# Try solving the flame
f.solve(loglevel=0)
file_name, entry = names(f"pressure-loop/{p:05.1f}")
f.save(file_name, name=entry, loglevel=1, description=f"pressure = {p} bar")
f.save(file_name, name=entry, description=f"pressure = {p} bar")
p_previous = p
except ct.CanteraError as e:
print('Error occurred while solving:', e, 'Try next pressure level')
# If solution failed: Restore the last successful solution and continue
f.restore(file_name, name=entry, loglevel=0)
f.restore(file_name, name=entry)


# PART 3: STRAIN RATE LOOP
Expand All @@ -162,7 +162,7 @@ def names(test):

# Restore initial solution
file_name, entry = names("initial-solution")
f.restore(file_name, name=entry, loglevel=0)
f.restore(file_name, name=entry)

# Counter to identify the loop
n = 0
Expand All @@ -188,7 +188,7 @@ def names(test):
# Try solving the flame
f.solve(loglevel=0)
file_name, entry = names(f"strain-loop/{n:02d}")
f.save(file_name, name=entry, loglevel=1,
f.save(file_name, name=entry,
description=f"strain rate iteration {n}")
except FlameExtinguished:
print('Flame extinguished')
Expand Down Expand Up @@ -234,7 +234,7 @@ def names(test):
n_selected = range(1, n, 5)
for n in n_selected:
file_name, entry = names(f"strain-loop/{n:02d}")
f.restore(file_name, name=entry, loglevel=0)
f.restore(file_name, name=entry)
a_max = f.strain_rate('max') # the maximum axial strain rate

# Plot the temperature profiles for the strain rate loop (selected)
Expand Down
4 changes: 2 additions & 2 deletions samples/python/onedim/diffusion_flame_extinction.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ def names(test):

# Restore last burning solution
file_name, entry = names(f"extinction/{n_last_burning:04d}")
f.restore(file_name, entry, loglevel=0)
f.restore(file_name, entry)


# Print some parameters at the extinction point, after restoring the last burning
# solution
file_name, entry = names(f"extinction/{n_last_burning:04d}")
f.restore(file_name, entry, loglevel=0)
f.restore(file_name, entry)

print('----------------------------------------------------------------------')
print('Parameters at the extinction point:')
Expand Down
4 changes: 2 additions & 2 deletions samples/python/onedim/flame_initial_guess.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ def describe(flame):
print("Restore solution from YAML")
gas.TPX = Tin, p, reactants
f2 = ct.FreeFlame(gas, width=width)
f2.restore(yaml_filepath, name="solution", loglevel=0)
f2.restore(yaml_filepath, name="solution")
describe(f2)

if hdf_filepath:
print("Restore solution from HDF")
gas.TPX = Tin, p, reactants
f2 = ct.FreeFlame(gas, width=width)
f2.restore(hdf_filepath, name="freeflame", loglevel=0)
f2.restore(hdf_filepath, name="freeflame")
describe(f2)

# Restore the flame via initial guess
Expand Down

0 comments on commit 3ea2510

Please sign in to comment.