Skip to content

Commit

Permalink
Update check_powi.py to handle floats properly (#1598)
Browse files Browse the repository at this point in the history
Also check instances of admath::pow in addition to std::pow and gcem::pow.
  • Loading branch information
yut23 authored Jun 28, 2024
1 parent ed417bc commit 3641c24
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
21 changes: 13 additions & 8 deletions .github/workflows/check_powi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@


def pow_to_powi(text):
# Finds all possible std::pow(x, n) or gcem::pow(x, n)
# Finds all possible std::pow(x, n), gcem::pow(x, n), or admath::pow(x, n)
# where n is a potential integer to amrex::Math::powi<n>(x)

# Check for all positive and negative integer, whole float numbers
# Match all positive and negative integers, whole float numbers
# with and without _rt
match_pattern = r"([^,]+),\s*(-?(?:\d+\.0*_rt?|\d))"
integer_pattern = r"-?(?:\d+\.0*(?:e0)?(?:_rt)?|\d)"

# Check for an integer in the second argument
match_pattern = rf"([^,]+),\s*({integer_pattern})"

# Match fails when there is a nested pow, so only inner most pow is matched
negate_pattern = r"(?![\s\S]*(?:std|gcem)::pow\((?:[^,]+),\s*(?:-?(?:\d+\.0*_rt?|\d))\))"
negate_pattern = rf"(?![\s\S]*(?:std|gcem|admath)::pow\((?:[^,]+),\s*(?:{integer_pattern})\))"

# Final pattern
pattern = rf"(?:std|gcem)::pow\({negate_pattern}{match_pattern}\)"
pattern = rf"(?:std|gcem|admath)::pow\({negate_pattern}{match_pattern}\)"
# pattern = rf"(?:std|gcem)::pow\((?![\s\S]*(?:std|gcem)::pow\((?:[^,]+),\s*(?:-?(?:\d+\.0*_rt?|\d))\))([^,]+),\s*(-?(?:\d+\.0*_rt?|\d))\)"

def replacement(match):
Expand All @@ -33,6 +36,8 @@ def replacement(match):
def process_content(dir_path):
# This function processes all text in the given directory
for root, dirs, filenames in os.walk(dir_path):
if "util/autodiff/" in root:
continue
for filename in filenames:
if filename.endswith(".H") or filename.endswith(".cpp"):
filepath = os.path.join(root, filename)
Expand All @@ -58,9 +63,9 @@ def git_diff():

# Print out suggested change and raise error after detecting modification
if git_diff_output.stdout:
print("Detected potential usage to replace std::pow" +
"with integer powers via amrex::Math::powi\n")
print("Below are the suggested change:\n")
print("Detected potential usage to replace std::pow"
" with integer powers via amrex::Math::powi\n")
print("Below are the suggested changes:\n")
print(git_diff_output.stdout)

raise RuntimeError("Changes detected after modification")
Expand Down
52 changes: 26 additions & 26 deletions networks/primordial_chem/actual_rhs.H
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void rhs_specie(const burn_t& state,

Real x46 = x45*x6;

Real x47 = std::pow(10.0, -0.12690000000000001*std::pow(x44, -3.0)*std::pow(x6, 3.0) + 1.1180000000000001*std::pow(x44, -2.0)*std::pow(x6, 2.0) - 1.5229999999999999*x46 - 19.379999999999999);
Real x47 = std::pow(10.0, -0.12690000000000001*amrex::Math::powi<-3>(x44)*amrex::Math::powi<3>(x6) + 1.1180000000000001*amrex::Math::powi<-2>(x44)*amrex::Math::powi<2>(x6) - 1.5229999999999999*x46 - 19.379999999999999);

Real x48 = X(1)*X(5);

Expand Down Expand Up @@ -2098,13 +2098,13 @@ void jac_nuc(const burn_t& state,

x35 = x32*x34;

x36 = std::pow(x33, -3.0);
x36 = amrex::Math::powi<-3>(x33);

x37 = std::pow(x33, -2.0);
x37 = amrex::Math::powi<-2>(x33);

x38 = std::pow(x32, 2.0);
x38 = amrex::Math::powi<2>(x32);

x39 = std::pow(10.0, -0.12690000000000001*std::pow(x32, 3.0)*x36 - 1.5229999999999999*x35 + 1.1180000000000001*x37*x38 - 19.379999999999999);
x39 = std::pow(10.0, -0.12690000000000001*amrex::Math::powi<3>(x32)*x36 - 1.5229999999999999*x35 + 1.1180000000000001*x37*x38 - 19.379999999999999);

x40 = X(5)*x39;

Expand Down Expand Up @@ -2251,7 +2251,7 @@ void jac_nuc(const burn_t& state,
)
: (
0
)) - X(1)*x40*(5.1485802679346868*x18*std::pow(x32, 1.0)*x37 - 0.87659414490283338*x18*x36*x38 - 3.5068370966299316*x64) + 7.2084342424042629e-17*X(12)*X(2)*x21 + X(2)*X(4)*((x26) ? (
)) - X(1)*x40*(5.1485802679346868*x18*amrex::Math::powi<1>(x32)*x37 - 0.87659414490283338*x18*x36*x38 - 3.5068370966299316*x64) + 7.2084342424042629e-17*X(12)*X(2)*x21 + X(2)*X(4)*((x26) ? (
6.7980000000000007e-9*std::pow(T, -1.6040000000000001)*x56 - 6.7396000000000002e-10*std::pow(T, -1.3320000000000001) + 8.1576000000000009e-11*std::pow(T, -0.60399999999999998)*x56
)
: (
Expand Down Expand Up @@ -2654,13 +2654,13 @@ void jac_nuc(const burn_t& state,
0
));

x175 = std::pow(x58, -3.0);
x175 = amrex::Math::powi<-3>(x58);

x176 = std::pow(x58, -2.0);
x176 = amrex::Math::powi<-2>(x58);

x177 = std::pow(x48, 2.0);
x177 = amrex::Math::powi<2>(x48);

x178 = std::pow(10.0, -0.12690000000000001*x175*std::pow(x48, 3.0) + 1.1180000000000001*x176*x177 - 1.5229999999999999*x60 - 19.379999999999999);
x178 = std::pow(10.0, -0.12690000000000001*x175*amrex::Math::powi<3>(x48) + 1.1180000000000001*x176*x177 - 1.5229999999999999*x60 - 19.379999999999999);

x179 = X(4)*x178;

Expand Down Expand Up @@ -2881,7 +2881,7 @@ void jac_nuc(const burn_t& state,
)
: (
0
)) - X(2)*x179*(-0.87659414490283338*x175*x177*x7 + 5.1485802679346868*x176*std::pow(x48, 1.0)*x7 - 3.5068370966299316*x216) + X(3)*X(5)*x208 - 2.4999999999999998e-6*X(3)*X(6)*x209 + X(5)*X(8)*((x196) ? (
)) - X(2)*x179*(-0.87659414490283338*x175*x177*x7 + 5.1485802679346868*x176*amrex::Math::powi<1>(x48)*x7 - 3.5068370966299316*x216) + X(3)*X(5)*x208 - 2.4999999999999998e-6*X(3)*X(6)*x209 + X(5)*X(8)*((x196) ? (
x195*x58*(1.5894349999999999*x193*x221 - 8.6761199999999992*x194*x220 + 5.8888600000000002*x216 + 6.7520699999999998*x218 + 14.393840000000001*x226)
)
: (
Expand Down Expand Up @@ -3083,13 +3083,13 @@ void jac_nuc(const burn_t& state,

x12 = x10*x11;

x13 = std::pow(x9, -3.0);
x13 = amrex::Math::powi<-3>(x9);

x14 = std::pow(x9, -2.0);
x14 = amrex::Math::powi<-2>(x9);

x15 = std::pow(x11, 2.0);
x15 = amrex::Math::powi<2>(x11);

x16 = std::pow(10.0, -0.12690000000000001*std::pow(x11, 3.0)*x13 - 1.5229999999999999*x12 + 1.1180000000000001*x14*x15 - 19.379999999999999);
x16 = std::pow(10.0, -0.12690000000000001*amrex::Math::powi<3>(x11)*x13 - 1.5229999999999999*x12 + 1.1180000000000001*x14*x15 - 19.379999999999999);

x17 = X(4)*x16;

Expand Down Expand Up @@ -3159,7 +3159,7 @@ void jac_nuc(const burn_t& state,
)
: (
0
)) - X(2)*x17*(5.1485802679346868*x1*std::pow(x11, 1.0)*x14 - 0.87659414490283338*x1*x13*x15 - 3.5068370966299316*x21) + X(4)*X(8)*(2.7400000000000004e-10*x1*x11*x19 - 8.4600000000000008e-10*x21) + 4.5700000000000003e-7*X(1)*X(10)*x2/amrex::Math::powi<2>(T))/(X(0)*x23 + X(1)*x23 + X(10)*x24 + X(11)*x23 + X(12)*x23 + X(13)*x23 + X(2)*x23 + X(3)*x23 + X(4)*x23 + X(5)*x23 + X(6)*x24 + X(7)*x23 + X(8)*x24 + X(9)*x24);
)) - X(2)*x17*(5.1485802679346868*x1*amrex::Math::powi<1>(x11)*x14 - 0.87659414490283338*x1*x13*x15 - 3.5068370966299316*x21) + X(4)*X(8)*(2.7400000000000004e-10*x1*x11*x19 - 8.4600000000000008e-10*x21) + 4.5700000000000003e-7*X(1)*X(10)*x2/amrex::Math::powi<2>(T))/(X(0)*x23 + X(1)*x23 + X(10)*x24 + X(11)*x23 + X(12)*x23 + X(13)*x23 + X(2)*x23 + X(3)*x23 + X(4)*x23 + X(5)*x23 + X(6)*x24 + X(7)*x23 + X(8)*x24 + X(9)*x24);


x0 = 2.5950363272655348e-10*std::pow(T, -0.75);
Expand Down Expand Up @@ -3197,13 +3197,13 @@ void jac_nuc(const burn_t& state,

x14 = x12*x13;

x15 = std::pow(x11, -3.0);
x15 = amrex::Math::powi<-3>(x11);

x16 = std::pow(x11, -2.0);
x16 = amrex::Math::powi<-2>(x11);

x17 = std::pow(x13, 2.0);
x17 = amrex::Math::powi<2>(x13);

x18 = std::pow(10.0, -0.12690000000000001*std::pow(x13, 3.0)*x15 - 1.5229999999999999*x14 + 1.1180000000000001*x16*x17 - 19.379999999999999);
x18 = std::pow(10.0, -0.12690000000000001*amrex::Math::powi<3>(x13)*x15 - 1.5229999999999999*x14 + 1.1180000000000001*x16*x17 - 19.379999999999999);

x19 = X(5)*x18;

Expand Down Expand Up @@ -3319,7 +3319,7 @@ void jac_nuc(const burn_t& state,
)
: (
0
)) - X(1)*x19*(5.1485802679346868*std::pow(x13, 1.0)*x16*x7 - 0.87659414490283338*x15*x17*x7 - 3.5068370966299316*x41) - 3.9837168574084181e-7*X(1)*x39 + X(10)*X(2)*((x23) ? (
)) - X(1)*x19*(5.1485802679346868*amrex::Math::powi<1>(x13)*x16*x7 - 0.87659414490283338*x15*x17*x7 - 3.5068370966299316*x41) - 3.9837168574084181e-7*X(1)*x39 + X(10)*X(2)*((x23) ? (
x22*(4430.0*x21 - 347800.0/amrex::Math::powi<3>(T))
)
: (
Expand Down Expand Up @@ -3958,21 +3958,21 @@ void jac_nuc(const burn_t& state,

x3 = 1.0/x2;

x4 = std::pow(x2, -3.0);
x4 = amrex::Math::powi<-3>(x2);

x5 = std::pow(x2, -2.0);
x5 = amrex::Math::powi<-2>(x2);

x6 = std::pow(x1, 2.0);
x6 = amrex::Math::powi<2>(x1);

x7 = std::pow(10.0, -1.5229999999999999*x1*x3 - 0.12690000000000001*std::pow(x1, 3.0)*x4 + 1.1180000000000001*x5*x6 - 19.379999999999999);
x7 = std::pow(10.0, -1.5229999999999999*x1*x3 - 0.12690000000000001*amrex::Math::powi<3>(x1)*x4 + 1.1180000000000001*x5*x6 - 19.379999999999999);

x8 = X(5)*x7;

x9 = X(4)*x7;

x10 = 1.0/T;

x11 = 5.1485802679346868*std::pow(x1, 1.0)*x10*x5 - 3.5068370966299316*x10*x3 - 0.87659414490283338*x10*x4*x6;
x11 = 5.1485802679346868*amrex::Math::powi<1>(x1)*x10*x5 - 3.5068370966299316*x10*x3 - 0.87659414490283338*x10*x4*x6;

x12 = 1.0/(9.1093818800000008e-28*X(0) + 1.6726215800000001e-24*X(1) + 5.01956503638e-24*X(10) + 6.6902431600000005e-24*X(11) + 6.6911540981899994e-24*X(12) + 6.6920650363799998e-24*X(13) + 1.6735325181900001e-24*X(2) + 1.6744434563800001e-24*X(3) + 3.3451215800000003e-24*X(4) + 3.3460325181899999e-24*X(5) + 3.3461540981899999e-24*X(6) + 3.3469434563800003e-24*X(7) + 3.3470650363800003e-24*X(8) + 5.0186540981899997e-24*X(9));

Expand Down

0 comments on commit 3641c24

Please sign in to comment.