From b8baa14c841ff5ed759713a93bb1f1a265c41159 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 1 Sep 2025 16:39:53 +0200 Subject: [PATCH] benchmark scripts added --- tests/benchmark_cp.py | 42 ++++++++++++++++++++++++++ tests/benchmark_cp_comp.py | 55 ++++++++++++++++++++++++++++++++++ tests/benchmark_equalibrium.py | 54 +++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 tests/benchmark_cp.py create mode 100644 tests/benchmark_cp_comp.py create mode 100644 tests/benchmark_equalibrium.py diff --git a/tests/benchmark_cp.py b/tests/benchmark_cp.py new file mode 100644 index 0000000..fb5ff0a --- /dev/null +++ b/tests/benchmark_cp.py @@ -0,0 +1,42 @@ +import cantera as ct +import numpy as np +import time +import gaspype as gp + +gas = ct.Solution("gri30.yaml") +composition = {"H2": 0.3, "H2O": 0.3, "N2": 0.4} + +n_species = gas.n_species +n_states = 1_000_000 + +# Random temperatures and pressures +temperatures = np.linspace(300.0, 2500.0, n_states) +pressures = np.full(n_states, ct.one_atm) + +# Create a SolutionArray with many states at once +states = ct.SolutionArray(gas, len(temperatures)) + +time.sleep(0.5) + +# Vectorized assignment +t0 = time.perf_counter() +states.TPX = temperatures, pressures, composition +cp_values = states.cp_mole +elapsed = time.perf_counter() - t0 + +print(f"Computed {n_states} Cp values in {elapsed:.4f} seconds (vectorized cantera)") +print("First 5 Cp values (J/mol-K):", cp_values[:5] / 1000) + + +# Vectorized fluid creation +fluid = gp.fluid(composition) + +time.sleep(0.5) + +# Benchmark: calculate Cp for all states at once +t0 = time.perf_counter() +cp_values = fluid.get_cp(t=temperatures) +elapsed = time.perf_counter() - t0 + +print(f"Computed {n_states} Cp values in {elapsed:.4f} seconds (vectorized Gaspype)") +print("First 5 Cp values (J/mol·K):", cp_values[:5]) \ No newline at end of file diff --git a/tests/benchmark_cp_comp.py b/tests/benchmark_cp_comp.py new file mode 100644 index 0000000..5b3a555 --- /dev/null +++ b/tests/benchmark_cp_comp.py @@ -0,0 +1,55 @@ +import cantera as ct +import numpy as np +import time +import gaspype as gp + +gas = ct.Solution("gri30.yaml") +n_species = gas.n_species +n_states = 1_000_000 + +# Random temperatures and pressures +temperatures = np.linspace(300.0, 2500.0, n_states) +pressures = np.full(n_states, ct.one_atm) + +# Generate random compositions for H2, H2O, N2 +rng = np.random.default_rng(seed=42) +fractions = rng.random((n_states, 3)) +fractions /= fractions.sum(axis=1)[:, None] # normalize + +# Convert to full 53-species mole fraction array +X = np.zeros((n_states, n_species)) +X[:, gas.species_index('H2')] = fractions[:, 0] +X[:, gas.species_index('H2O')] = fractions[:, 1] +X[:, gas.species_index('N2')] = fractions[:, 2] + +# Build SolutionArray +states = ct.SolutionArray(gas, n_states) + +time.sleep(0.5) + +# Vectorized assignment +t0 = time.perf_counter() +states.TPX = temperatures, pressures, X +cp_values = states.cp_mole +elapsed = time.perf_counter() - t0 + +print(f"Computed {n_states} Cp values in {elapsed:.4f} seconds (vectorized cantera)") +print("First 5 Cp values (J/mol-K):", cp_values[:5] / 1000) + + +# Vectorized fluid creation +fluid = ( + gp.fluid({'H2': 1}) * fractions[:, 0] + + gp.fluid({'H2O': 1}) * fractions[:, 1] + + gp.fluid({'N2': 1}) * fractions[:, 2] +) + +time.sleep(0.5) + +# Benchmark: calculate Cp for all states at once +t0 = time.perf_counter() +cp_values = fluid.get_cp(t=temperatures) +elapsed = time.perf_counter() - t0 + +print(f"Computed {n_states} Cp values in {elapsed:.4f} seconds (vectorized Gaspype)") +print("First 5 Cp values (J/mol·K):", cp_values[:5]) \ No newline at end of file diff --git a/tests/benchmark_equalibrium.py b/tests/benchmark_equalibrium.py new file mode 100644 index 0000000..748e98d --- /dev/null +++ b/tests/benchmark_equalibrium.py @@ -0,0 +1,54 @@ +import cantera as ct +import gaspype as gp +import numpy as np +import time +from gaspype import fluid_system + +# ----------------------- +# Settings +# ----------------------- +n_temps = 1000 +temps_C = np.linspace(300, 1000, n_temps) # °C +temperatures = temps_C + 273.15 # K +pressure = 101325 # Pa (1 atm) + +composition = {"CH4": 0.8, "H2O": 0.2} +species_to_track = ["CH4", "H2O", "CO", "CO2", "H2", "O2"] + +# ----------------------- +# Cantera benchmark +# ----------------------- +gas = ct.Solution("gri30.yaml") +gas.TPX = temperatures[0], pressure, composition + +eq_cantera = np.zeros((n_temps, len(species_to_track))) + +time.sleep(0.5) +t0 = time.perf_counter() +for i, T in enumerate(temperatures): + gas.TP = T, pressure + gas.equilibrate('TP') + eq_cantera[i, :] = [gas.X[gas.species_index(s)] for s in species_to_track] +elapsed_cantera = time.perf_counter() - t0 +print(f"Cantera: {elapsed_cantera:.4f} s") + +# ----------------------- +# Gaspype benchmark +# ----------------------- +# Construct the fluid with composition and tracked species +fluid = gp.fluid(composition, fs=fluid_system(species_to_track)) + +time.sleep(0.5) +t0 = time.perf_counter() +eq_gaspype = gp.equilibrium(fluid, t=temperatures, p=pressure) +elapsed_gaspype = time.perf_counter() - t0 +print(f"Gaspype: {elapsed_gaspype:.4f} s") + +# ----------------------- +# Compare first 5 results +# ----------------------- +print("First 5 equilibrium compositions (mole fractions):") +for i in range(5): + print(f"T = {temperatures[i]:.1f} K") + print(" Cantera:", eq_cantera[i]) + print(" Gaspype :", eq_gaspype.array_composition[i])