[Python] Handle removal of numpy.trapz

Numpy 2.4.0 will remove trapz. To maintain compatibility with Numpy 1.x for now,
we need to dynamically select the correct function.
This commit is contained in:
Ray Speth
2025-10-21 18:11:56 -04:00
committed by Ray Speth
parent 457cb1f32b
commit 335486c9d3
6 changed files with 30 additions and 17 deletions
@@ -26,6 +26,10 @@ logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logging.basicConfig(stream=sys.stdout)
# Workaround to support both Numpy 1.x and 2.4.0+
# TODO: Replace when dropping Numpy 1.x support
trapezoid = getattr(np, "trapezoid", None) or np.trapz
# %%
# Flame Initialization
# --------------------
@@ -162,7 +166,7 @@ for i in range(n_max):
data.append({
'T_max': max(f.T),
'strain_rate': strain_rate,
'heat_release_rate': np.trapz(f.heat_release_rate, f.grid),
'heat_release_rate': trapezoid(f.heat_release_rate, f.grid),
'n_points': len(f.grid),
'flame_width': width,
'Tc_increment': temperature_increment,
@@ -25,6 +25,9 @@ from pathlib import Path
import numpy as np
import cantera as ct
# Workaround to support both Numpy 1.x and 2.4.0+
# TODO: Replace when dropping Numpy 1.x support
trapezoid = getattr(np, "trapezoid", None) or np.trapz
# Differentiation function for data that has variable grid spacing Used here to
# compute normal strain-rate
@@ -63,7 +66,7 @@ def compute_consumption_speed(opposed_flame):
integrand = opposed_flame.heat_release_rate / opposed_flame.cp
total_heat_release = np.trapz(integrand, opposed_flame.grid)
total_heat_release = trapezoid(integrand, opposed_flame.grid)
Sc = total_heat_release / (Tb - Tu) / rho_u
return Sc
+8 -4
View File
@@ -17,6 +17,10 @@ import numpy as np
import matplotlib.pyplot as plt
# Workaround to support both Numpy 1.x and 2.4.0+
# TODO: Replace when dropping Numpy 1.x support
trapezoid = getattr(np, "trapezoid", None) or np.trapz
#########################################################################
# Input Parameters
#########################################################################
@@ -252,13 +256,13 @@ plt.show()
######################################################################
# heat release
Q = np.trapz(states.heat_release_rate * states.V, t)
Q = trapezoid(states.heat_release_rate * states.V, t)
output_str = '{:45s}{:>4.1f} {}'
print(output_str.format('Heat release rate per cylinder (estimate):',
Q / t[-1] / 1000., 'kW'))
# expansion power
W = np.trapz(states.dWv_dt, t)
W = trapezoid(states.dWv_dt, t)
print(output_str.format('Expansion power per cylinder (estimate):',
W / t[-1] / 1000., 'kW'))
@@ -268,6 +272,6 @@ print(output_str.format('Efficiency (estimate):', eta * 100., '%'))
# CO emissions
MW = states.mean_molecular_weight
CO_emission = np.trapz(MW * states.mdot_out * states('CO').X[:, 0], t)
CO_emission /= np.trapz(MW * states.mdot_out, t)
CO_emission = trapezoid(MW * states.mdot_out * states('CO').X[:, 0], t)
CO_emission /= trapezoid(MW * states.mdot_out, t)
print(output_str.format('CO emission (estimate):', CO_emission * 1.e6, 'ppm'))