From 9a8b2eeeb4994e71c4cc949ae9add132929e9975 Mon Sep 17 00:00:00 2001 From: yut23 Date: Fri, 28 Jun 2024 15:33:39 -0400 Subject: [PATCH 1/5] Check admath::pow for replacement with amrex::Math::powi --- .github/workflows/check_powi.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/check_powi.py b/.github/workflows/check_powi.py index 686a8269eb..96f857d9c1 100644 --- a/.github/workflows/check_powi.py +++ b/.github/workflows/check_powi.py @@ -5,7 +5,7 @@ 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(x) # Check for all positive and negative integer, whole float numbers @@ -13,10 +13,10 @@ def pow_to_powi(text): match_pattern = r"([^,]+),\s*(-?(?:\d+\.0*_rt?|\d))" # 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 = r"(?![\s\S]*(?:std|gcem|admath)::pow\((?:[^,]+),\s*(?:-?(?:\d+\.0*_rt?|\d))\))" # 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): From 9ee32e3f769ff9dde44143bce2b4d2ff06e2a1fd Mon Sep 17 00:00:00 2001 From: yut23 Date: Fri, 28 Jun 2024 15:34:36 -0400 Subject: [PATCH 2/5] Improve integer regex * properly match `_rt` suffix for floats * allow `e0` at end of float --- .github/workflows/check_powi.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/check_powi.py b/.github/workflows/check_powi.py index 96f857d9c1..04d5374ddb 100644 --- a/.github/workflows/check_powi.py +++ b/.github/workflows/check_powi.py @@ -8,12 +8,15 @@ def pow_to_powi(text): # 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(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|admath)::pow\((?:[^,]+),\s*(?:-?(?:\d+\.0*_rt?|\d))\))" + negate_pattern = rf"(?![\s\S]*(?:std|gcem|admath)::pow\((?:[^,]+),\s*(?:{integer_pattern})\))" # Final pattern pattern = rf"(?:std|gcem|admath)::pow\({negate_pattern}{match_pattern}\)" From 48906cac2399e47ace462fe556d219a8abb72abe Mon Sep 17 00:00:00 2001 From: yut23 Date: Fri, 28 Jun 2024 15:51:30 -0400 Subject: [PATCH 3/5] Ignore files under util/autodiff --- .github/workflows/check_powi.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/check_powi.py b/.github/workflows/check_powi.py index 04d5374ddb..6686ad24b1 100644 --- a/.github/workflows/check_powi.py +++ b/.github/workflows/check_powi.py @@ -36,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) From f85a1567bde90916943552d6101c7c239b56d311 Mon Sep 17 00:00:00 2001 From: yut23 Date: Fri, 28 Jun 2024 15:54:33 -0400 Subject: [PATCH 4/5] Replace std::pow with amrex::Math::powi in primordial_chem --- networks/primordial_chem/actual_rhs.H | 52 +++++++++++++-------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/networks/primordial_chem/actual_rhs.H b/networks/primordial_chem/actual_rhs.H index 907959033a..e43432135f 100644 --- a/networks/primordial_chem/actual_rhs.H +++ b/networks/primordial_chem/actual_rhs.H @@ -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); @@ -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; @@ -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 ) : ( @@ -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; @@ -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) ) : ( @@ -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; @@ -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); @@ -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; @@ -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)) ) : ( @@ -3958,13 +3958,13 @@ 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; @@ -3972,7 +3972,7 @@ void jac_nuc(const burn_t& state, 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)); From 402ab93e4cc871dd64462eeff0f964cd8cc0fddb Mon Sep 17 00:00:00 2001 From: yut23 Date: Fri, 28 Jun 2024 15:55:49 -0400 Subject: [PATCH 5/5] Clean up error message --- .github/workflows/check_powi.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/check_powi.py b/.github/workflows/check_powi.py index 6686ad24b1..c1a0341d3e 100644 --- a/.github/workflows/check_powi.py +++ b/.github/workflows/check_powi.py @@ -63,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")