Skip to content

Commit

Permalink
[Python] Fix Numpy 2.0 compatibility issues
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Jun 9, 2024
1 parent 026c5c7 commit ee36344
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 20 deletions.
8 changes: 4 additions & 4 deletions samples/python/onedim/flame_speed_convergence_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,10 @@ def analyze_errors(grids, speeds, true_speed):
and compare these with our estimated errors.
This will show if our estimates are reasonable, or conservative, or too optimistic.
"""
true_speed_estimates = np.full_like(speeds, np.NaN)
total_percent_error_estimates = np.full_like(speeds, np.NaN)
actual_extrapolated_percent_errors = np.full_like(speeds, np.NaN)
actual_raw_percent_errors = np.full_like(speeds, np.NaN)
true_speed_estimates = np.full_like(speeds, np.nan)
total_percent_error_estimates = np.full_like(speeds, np.nan)
actual_extrapolated_percent_errors = np.full_like(speeds, np.nan)
actual_raw_percent_errors = np.full_like(speeds, np.nan)
for i in range(3, len(grids)):
print(grids[: i + 1])
true_speed_estimate, total_percent_error_estimate = extrapolate_uncertainty(
Expand Down
2 changes: 1 addition & 1 deletion samples/python/onedim/premixed_counterflow_twin_flame.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,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 = np.trapezoid(integrand, opposed_flame.grid)
Sc = total_heat_release / (Tb - Tu) / rho_u

return Sc
Expand Down
8 changes: 4 additions & 4 deletions samples/python/reactors/ic_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ def ca_ticks(t):
######################################################################

# heat release
Q = np.trapz(states.heat_release_rate * states.V, t)
Q = np.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 = np.trapezoid(states.dWv_dt, t)
print(output_str.format('Expansion power per cylinder (estimate):',
W / t[-1] / 1000., 'kW'))

Expand All @@ -268,6 +268,6 @@ def ca_ticks(t):

# 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 = np.trapezoid(MW * states.mdot_out * states('CO').X[:, 0], t)
CO_emission /= np.trapezoid(MW * states.mdot_out, t)
print(output_str.format('CO emission (estimate):', CO_emission * 1.e6, 'ppm'))
4 changes: 2 additions & 2 deletions site_scons/buildutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,9 +972,9 @@ def compare_profiles(
"pos. err"
))
header.append(f"{10*'-'} ----- {14*'-'} {14*'-'} {9*'-'} {9*'-'} {9*'-'}")
ref_ptp = reference.ptp(axis=1)
ref_ptp = np.ptp(reference, axis=1)
ref_max = np.abs(reference).max(axis=1)
sample_ptp = sample.ptp(axis=1)
sample_ptp = np.ptp(sample, axis=1)
sample_max = np.abs(sample).max(axis=1)
scale = np.maximum(
np.maximum(ref_ptp[1:], ref_max[1:]),
Expand Down
4 changes: 2 additions & 2 deletions test/python/test_onedim.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,11 @@ def test_unity_lewis(self):
self.sim.transport_model = 'unity-Lewis-number'
self.sim.set_refine_criteria(ratio=3.0, slope=0.08, curve=0.12)
self.sim.solve(loglevel=0, auto=True)
dh_unity_lewis = self.sim.enthalpy_mass.ptp()
dh_unity_lewis = np.ptp(self.sim.enthalpy_mass)

self.sim.transport_model = 'mixture-averaged'
self.sim.solve(loglevel=0)
dh_mix = self.sim.enthalpy_mass.ptp()
dh_mix = np.ptp(self.sim.enthalpy_mass)

# deviation of enthalpy should be much lower for unity Le model (tends
# towards zero as grid is refined)
Expand Down
2 changes: 1 addition & 1 deletion test/python/test_reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1841,7 +1841,7 @@ def test_interface_adjacent(self):
class InterfaceReactionTests(ReactionTests):
# test suite for surface reaction expressions

_value = np.NAN # reference value
_value = np.nan # reference value
_coverage_deps = None

@classmethod
Expand Down
6 changes: 3 additions & 3 deletions test/python/test_reactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2355,7 +2355,7 @@ def calc_tig(self, species, dH):
To = T[0]
Tf = T[-1]

return (t[-1]*T[-1] - np.trapz(T,t)) / (T[-1] - T[0])
return (t[-1]*T[-1] - np.trapezoid(T,t)) / (T[-1] - T[0])

def calc_dtdh(self, species):
gas, r, net = self.setup_ignition_delay()
Expand All @@ -2378,8 +2378,8 @@ def calc_dtdh(self, species):

To = T[0]
Tf = T[-1]
tig = (t[-1]*Tf - np.trapz(T,t))/(Tf-To)
dtdp = ((t[-1] - tig)*S[-1,:]*Tf - np.trapz(S*T[:,None], t, axis=0))/(Tf-To)
tig = (t[-1]*Tf - np.trapezoid(T,t))/(Tf-To)
dtdp = ((t[-1] - tig)*S[-1,:]*Tf - np.trapezoid(S*T[:,None], t, axis=0))/(Tf-To)
return dtdp

# See https://github.com/Cantera/enhancements/issues/55
Expand Down
2 changes: 2 additions & 0 deletions test/python/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,8 @@ def test_isotropic_velocity_electron_energy_distribution(self):
(ct.avogadro * ct.electron_charge))
self.assertNear(mean_electron_energy, self.phase.mean_electron_energy)

# @todo: replace np.trapz with np.trapezoid when dropping support for NumPy 1.x
@pytest.mark.filterwarnings("ignore:`trapz` is deprecated")
def test_discretized_electron_energy_distribution(self):
levels = np.array([0.0, 1.0, 10.0])
dist = np.array([0.0, 0.9, 0.01])
Expand Down
6 changes: 3 additions & 3 deletions test/python/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ def compareProfiles(reference, sample, rtol=1e-5, atol=1e-12, xtol=1e-5):
bad = []
template = '{0:9.4e} {1: 3d} {2:14.7e} {3:14.7e} {4:9.3e} {5:9.3e} {6:9.3e}'
for i in range(1, nVars):
scale = max(max(abs(reference[i])), reference[i].ptp(),
max(abs(sample[i])), sample[i].ptp())
scale = max(max(abs(reference[i])), np.ptp(reference[i]),
max(abs(sample[i])), np.ptp(sample[i]))
slope = np.zeros(nTimes)
slope[1:] = np.diff(reference[i]) / np.diff(reference[0]) * reference[0].ptp()
slope[1:] = np.diff(reference[i]) / np.diff(reference[0]) * np.ptp(reference[0])

comp = np.interp(reference[0], sample[0], sample[i])
for j in range(nTimes):
Expand Down

0 comments on commit ee36344

Please sign in to comment.