Skip to content

Commit

Permalink
std::clamp -> amrex::clamp (#1711)
Browse files Browse the repository at this point in the history
HIP + GCC 14 doesn't recognize std::clamp as constexpr
  • Loading branch information
zingale authored Jan 12, 2025
1 parent f0c0d2a commit f2094f9
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 39 deletions.
12 changes: 7 additions & 5 deletions EOS/helmholtz/actual_eos.H
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <AMReX.H>
#include <AMReX_REAL.H>
#include <AMReX_ParallelDescriptor.H>
#include <AMReX_Algorithm.H>

#include <extern_parameters.H>
#include <fundamental_constants.H>
#include <eos_type.H>
Expand Down Expand Up @@ -154,9 +156,9 @@ void apply_electrons (T& state)

// hash locate this temperature and density
int jat = int((std::log10(state.T) - tlo) * tstpi) + 1;
jat = std::clamp(jat, 1, jmax-1) - 1;
jat = amrex::Clamp(jat, 1, jmax-1) - 1;
int iat = int((std::log10(din) - dlo) * dstpi) + 1;
iat = std::clamp(iat, 1, imax-1) - 1;
iat = amrex::Clamp(iat, 1, imax-1) - 1;

amrex::Real fi[36];

Expand Down Expand Up @@ -1003,7 +1005,7 @@ void single_iter_update (T& state, int var, int dvar,
amrex::Real xnew = x - (v - v_want) / dvdx;

// Don't let the temperature/density change by more than a factor of two
xnew = std::clamp(xnew, 0.5_rt * x, 2.0_rt * x);
xnew = amrex::Clamp(xnew, 0.5_rt * x, 2.0_rt * x);

// Don't let us freeze/evacuate
xnew = amrex::max(smallx, xnew);
Expand Down Expand Up @@ -1118,8 +1120,8 @@ void double_iter_update (T& state, int var1, int var2,

// Don't let the temperature or density change by more
// than a factor of two
tnew = std::clamp(tnew, 0.5e0_rt * told, 2.0e0_rt * told);
rnew = std::clamp(rnew, 0.5e0_rt * rold, 2.0e0_rt * rold);
tnew = amrex::Clamp(tnew, 0.5e0_rt * told, 2.0e0_rt * told);
rnew = amrex::Clamp(rnew, 0.5e0_rt * rold, 2.0e0_rt * rold);

// Don't let us freeze or evacuate
tnew = amrex::max(EOSData::mintemp, tnew);
Expand Down
4 changes: 3 additions & 1 deletion integration/BackwardEuler/be_integrator.H
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef BE_INTEGRATOR_H
#define BE_INTEGRATOR_H

#include <AMReX_Algorithm.H>

#include <be_type.H>
#include <network.H>
#include <actual_network.H>
Expand Down Expand Up @@ -272,7 +274,7 @@ int be_integrator (BurnT& state, BeT& be)
// backward-Euler has a local truncation error of dt**2

amrex::Real dt_new = dt_sub * std::sqrt(1.0_rt / rel_error);
dt_sub = std::clamp(dt_new, dt_sub / 2.0, 2.0 * dt_sub);
dt_sub = amrex::Clamp(dt_new, dt_sub / 2.0, 2.0 * dt_sub);

} else {

Expand Down
4 changes: 3 additions & 1 deletion integration/ForwardEuler/actual_integrator.H
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef actual_integrator_H
#define actual_integrator_H

#include <AMReX_Algorithm.H>

#include <network.H>
#include <actual_network.H>
#ifdef NEW_NETWORK_IMPLEMENTATION
Expand Down Expand Up @@ -84,7 +86,7 @@ void clean_state (const amrex::Real time, IntT& int_state, BurnT& state)

// Ensure that the temperature always stays within reasonable limits.

state.T = std::clamp(state.T, EOSData::mintemp, integrator_rp::MAX_TEMP);
state.T = amrex::Clamp(state.T, EOSData::mintemp, integrator_rp::MAX_TEMP);

}

Expand Down
4 changes: 3 additions & 1 deletion integration/RKC/rkc.H
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef RKC_H
#define RKC_H

#include <AMReX_Algorithm.H>

#include <rkc_type.H>
#include <rkc_util.H>
#include <burn_type.H>
Expand Down Expand Up @@ -306,7 +308,7 @@ int rkclow (BurnT& state, RkcT& rstate)
}
}
absh = std::max(0.1_rt, fac) * absh;
absh = std::clamp(absh, hmin, rstate.hmax);
absh = amrex::Clamp(absh, hmin, rstate.hmax);
errold = err;
hold = h;
h = tdir * absh;
Expand Down
3 changes: 2 additions & 1 deletion integration/VODE/vode_dvhin.H
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define VODE_DVHIN_H

#include <AMReX_REAL.H>
#include <AMReX_Algorithm.H>

#ifdef STRANG
#include <integrator_rhs_strang.H>
Expand Down Expand Up @@ -124,7 +125,7 @@ void dvhin (BurnT& state, DvodeT& vstate, amrex::Real& H0, int& NITER, int& IER)

// Iteration done. Apply bounds and bias factor. Then exit.
H0 = hnew * 0.5_rt;
H0 = std::clamp(H0, HLB, HUB);
H0 = amrex::Clamp(H0, HLB, HUB);

}

Expand Down
4 changes: 3 additions & 1 deletion integration/integrator_type_sdc.H
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef INTEGRATOR_TYPE_SDC_H
#define INTEGRATOR_TYPE_SDC_H

#include <AMReX_Algorithm.H>

#include <eos.H>
#include <eos_composition.H>
#include <burn_type.H>
Expand All @@ -19,7 +21,7 @@ void clean_state(const amrex::Real time, BurnT& state, T& int_state)
if (integrator_rp::do_species_clip) {
for (int n = 1; n <= NumSpec; ++n) {
// we use 1-based indexing, so we need to offset SFS
int_state.y(SFS+n) = std::clamp(int_state.y(SFS+n),
int_state.y(SFS+n) = amrex::Clamp(int_state.y(SFS+n),
state.rho * integrator_rp::SMALL_X_SAFE, state.rho);
}
}
Expand Down
4 changes: 3 additions & 1 deletion integration/integrator_type_strang.H
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef INTEGRATOR_TYPE_STRANG_H
#define INTEGRATOR_TYPE_STRANG_H

#include <AMReX_Algorithm.H>

#include <eos.H>
#include <integrator_type.H>

Expand Down Expand Up @@ -53,7 +55,7 @@ void clean_state (const amrex::Real time, BurnT& state, I& int_state)

if (integrator_rp::do_species_clip) {
for (int n = 1; n <= NumSpec; ++n) {
int_state.y(n) = std::clamp(int_state.y(n), integrator_rp::SMALL_X_SAFE, 1.0_rt);
int_state.y(n) = amrex::Clamp(int_state.y(n), integrator_rp::SMALL_X_SAFE, 1.0_rt);
}
}

Expand Down
6 changes: 4 additions & 2 deletions integration/nse_update_sdc.H
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef NSE_UPDATE_H
#define NSE_UPDATE_H

#include <AMReX_Algorithm.H>

#include <iostream>
#include <fstream>
#include <actual_network.H>
Expand Down Expand Up @@ -271,7 +273,7 @@ void sdc_nse_burn(BurnT& state, const amrex::Real dt) {

amrex::Real sum_X{0.0_rt};
for (auto & xn : nse_state.X) {
xn = std::clamp(xn, small_x, 1.0_rt);
xn = amrex::Clamp(xn, small_x, 1.0_rt);
sum_X += xn;
}

Expand Down Expand Up @@ -581,7 +583,7 @@ void sdc_nse_burn(BurnT& state, const amrex::Real dt) {

amrex::Real sum_X{0.0_rt};
for (auto & xn : nse_state.xn) {
xn = std::clamp(xn, small_x, 1.0_rt);
xn = amrex::Clamp(xn, small_x, 1.0_rt);
sum_X += xn;
}

Expand Down
4 changes: 3 additions & 1 deletion integration/utils/initial_timestep.H
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define INITIAL_TIMESTEP_H

#include <AMReX_REAL.H>
#include <AMReX_Algorithm.H>

#include <actual_network.H>
#ifdef NEW_NETWORK_IMPLEMENTATION
#include <rhs.H>
Expand Down Expand Up @@ -87,7 +89,7 @@ amrex::Real initial_react_dt (BurnT& burn_state, IntT& int_state,
// Save the final timestep, with a bias factor.

amrex::Real dt = h / 2.0_rt;
dt = std::clamp(h, hL, hU);
dt = amrex::Clamp(h, hL, hU);

dt = amrex::min(dt, integrator_rp::ode_max_dt);

Expand Down
3 changes: 2 additions & 1 deletion nse_solver/nse_eos.H
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define NSE_EOS_H

#include <AMReX_REAL.H>
#include <AMReX_Algorithm.H>

#include <eos.H>

Expand Down Expand Up @@ -152,7 +153,7 @@ nse_T_abar_from_e(const amrex::Real rho, const amrex::Real e_in,

// update the temperature and abar

T = std::clamp(T + dT, 0.25 * T, 4.0 * T);
T = amrex::Clamp(T + dT, 0.25 * T, 4.0 * T);
abar = eos_state.abar;

// update mu_p and mu_n
Expand Down
9 changes: 5 additions & 4 deletions nse_tabular/nse_eos.H
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define NSE_EOS_H

#include <AMReX_REAL.H>
#include <AMReX_Algorithm.H>

#include <eos.H>

Expand Down Expand Up @@ -84,7 +85,7 @@ nse_T_abar_from_e(const amrex::Real rho, const amrex::Real e_in, const amrex::Re
+ Ye * eos_state.dedZ * dabar_dT);

// update the temperature
T = std::clamp(T + dT, 0.25 * T, 4.0 * T);
T = amrex::Clamp(T + dT, 0.25 * T, 4.0 * T);

// check convergence

Expand Down Expand Up @@ -175,7 +176,7 @@ nse_rho_abar_from_e(const amrex::Real T, const amrex::Real e_in, const amrex::Re
+ Ye * eos_state.dedZ * dabar_drho);

// update the density
rho = std::clamp(rho + drho, 0.25 * rho, 4.0 * rho);
rho = amrex::Clamp(rho + drho, 0.25 * rho, 4.0 * rho);

// check convergence

Expand Down Expand Up @@ -266,7 +267,7 @@ nse_T_abar_from_p(const amrex::Real rho, const amrex::Real p_in, const amrex::Re
+ Ye * eos_state.dpdZ * dabar_dT);

// update the temperature
T = std::clamp(T + dT, 0.25 * T, 4.0 * T);
T = amrex::Clamp(T + dT, 0.25 * T, 4.0 * T);

// check convergence

Expand Down Expand Up @@ -357,7 +358,7 @@ nse_rho_abar_from_p(const amrex::Real T, const amrex::Real p_in, const amrex::Re
+ Ye * eos_state.dpdZ * dabar_drho);

// update the density
rho = std::clamp(rho + drho, 0.25 * rho, 4.0 * rho);
rho = amrex::Clamp(rho + drho, 0.25 * rho, 4.0 * rho);

// check convergence

Expand Down
40 changes: 20 additions & 20 deletions nse_tabular/nse_table.H
Original file line number Diff line number Diff line change
Expand Up @@ -405,23 +405,23 @@ void nse_interp(nse_table_t& nse_state, bool skip_X_fill=false) {
amrex::Real rmin = nse_table_size::logrho_min;
amrex::Real rmax = nse_table_size::logrho_max;

rholog = std::clamp(rholog, rmin, rmax);
rholog = amrex::Clamp(rholog, rmin, rmax);
}

amrex::Real tlog = std::log10(nse_state.T);
{
amrex::Real tmin = nse_table_size::logT_min;
amrex::Real tmax = nse_table_size::logT_max;

tlog = std::clamp(tlog, tmin, tmax);
tlog = amrex::Clamp(tlog, tmin, tmax);
}

amrex::Real yet = nse_state.Ye;
{
amrex::Real yemin = nse_table_size::ye_min;
amrex::Real yemax = nse_table_size::ye_max;

yet = std::clamp(yet, yemin, yemax);
yet = amrex::Clamp(yet, yemin, yemax);
}

if (nse_table_interp_linear) {
Expand All @@ -443,7 +443,7 @@ void nse_interp(nse_table_t& nse_state, bool skip_X_fill=false) {
for (int n = 1; n <= NumSpec; n++) {
amrex::Real _X = trilinear(ir1, it1, ic1, rholog, tlog, yet,
[=] (const int i) {return massfractab(n, i);});
nse_state.X[n-1] = std::clamp(_X, 0.0_rt, 1.0_rt);
nse_state.X[n-1] = amrex::Clamp(_X, 0.0_rt, 1.0_rt);
}
}

Expand All @@ -455,13 +455,13 @@ void nse_interp(nse_table_t& nse_state, bool skip_X_fill=false) {
// so we offset one to the left and also ensure that we don't go off the table

int ir0 = nse_get_logrho_index(rholog) - 1;
ir0 = std::clamp(ir0, 1, nse_table_size::nden-3);
ir0 = amrex::Clamp(ir0, 1, nse_table_size::nden-3);

int it0 = nse_get_logT_index(tlog) - 1;
it0 = std::clamp(it0, 1, nse_table_size::ntemp-3);
it0 = amrex::Clamp(it0, 1, nse_table_size::ntemp-3);

int ic0 = nse_get_ye_index(yet) - 1;
ic0 = std::clamp(ic0, 1, nse_table_size::nye-3);
ic0 = amrex::Clamp(ic0, 1, nse_table_size::nye-3);

nse_state.abar = tricubic(ir0, it0, ic0, rholog, tlog, yet, abartab);
nse_state.bea = tricubic(ir0, it0, ic0, rholog, tlog, yet, beatab);
Expand All @@ -476,7 +476,7 @@ void nse_interp(nse_table_t& nse_state, bool skip_X_fill=false) {
for (int n = 1; n <= NumSpec; n++) {
amrex::Real _X = tricubic(ir0, it0, ic0, rholog, tlog, yet,
[=] (const int i) {return massfractab(n, i);});
nse_state.X[n-1] = std::clamp(_X, 0.0_rt, 1.0_rt);
nse_state.X[n-1] = amrex::Clamp(_X, 0.0_rt, 1.0_rt);
}
}
}
Expand All @@ -498,33 +498,33 @@ nse_interp_dT(const amrex::Real temp, const amrex::Real rho, const amrex::Real y
amrex::Real rmin = nse_table_size::logrho_min;
amrex::Real rmax = nse_table_size::logrho_max;

rholog = std::clamp(rholog, rmin, rmax);
rholog = amrex::Clamp(rholog, rmin, rmax);
}

amrex::Real tlog = std::log10(temp);
{
amrex::Real tmin = nse_table_size::logT_min;
amrex::Real tmax = nse_table_size::logT_max;

tlog = std::clamp(tlog, tmin, tmax);
tlog = amrex::Clamp(tlog, tmin, tmax);
}

amrex::Real yet = ye;
{
amrex::Real yemin = nse_table_size::ye_min;
amrex::Real yemax = nse_table_size::ye_max;

yet = std::clamp(yet, yemin, yemax);
yet = amrex::Clamp(yet, yemin, yemax);
}

int ir0 = nse_get_logrho_index(rholog) - 1;
ir0 = std::clamp(ir0, 1, nse_table_size::nden-3);
ir0 = amrex::Clamp(ir0, 1, nse_table_size::nden-3);

int it0 = nse_get_logT_index(tlog) - 1;
it0 = std::clamp(it0, 1, nse_table_size::ntemp-3);
it0 = amrex::Clamp(it0, 1, nse_table_size::ntemp-3);

int ic0 = nse_get_ye_index(yet) - 1;
ic0 = std::clamp(ic0, 1, nse_table_size::nye-3);
ic0 = amrex::Clamp(ic0, 1, nse_table_size::nye-3);

// note: this is returning the derivative wrt log10(T), so we need to
// convert to d/dT
Expand All @@ -551,33 +551,33 @@ nse_interp_drho(const amrex::Real temp, const amrex::Real rho, const amrex::Real
amrex::Real rmin = nse_table_size::logrho_min;
amrex::Real rmax = nse_table_size::logrho_max;

rholog = std::clamp(rholog, rmin, rmax);
rholog = amrex::Clamp(rholog, rmin, rmax);
}

amrex::Real tlog = std::log10(temp);
{
amrex::Real tmin = nse_table_size::logT_min;
amrex::Real tmax = nse_table_size::logT_max;

tlog = std::clamp(tlog, tmin, tmax);
tlog = amrex::Clamp(tlog, tmin, tmax);
}

amrex::Real yet = ye;
{
amrex::Real yemin = nse_table_size::ye_min;
amrex::Real yemax = nse_table_size::ye_max;

yet = std::clamp(yet, yemin, yemax);
yet = amrex::Clamp(yet, yemin, yemax);
}

int ir0 = nse_get_logrho_index(rholog) - 1;
ir0 = std::clamp(ir0, 1, nse_table_size::nden-3);
ir0 = amrex::Clamp(ir0, 1, nse_table_size::nden-3);

int it0 = nse_get_logT_index(tlog) - 1;
it0 = std::clamp(it0, 1, nse_table_size::ntemp-3);
it0 = amrex::Clamp(it0, 1, nse_table_size::ntemp-3);

int ic0 = nse_get_ye_index(yet) - 1;
ic0 = std::clamp(ic0, 1, nse_table_size::nye-3);
ic0 = amrex::Clamp(ic0, 1, nse_table_size::nye-3);

// note: this is returning the derivative wrt log10(rho), so we need to
// convert to d/drho
Expand Down

0 comments on commit f2094f9

Please sign in to comment.