From 4aacf7e3694b14f6e54c46f51fa21abeb40a8c9e Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 10:32:56 -0500 Subject: [PATCH 001/101] calc_tdust_3d_g: remove misc unused variables 1/4 --- src/clib/calc_tdust_3d_g.F | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/src/clib/calc_tdust_3d_g.F b/src/clib/calc_tdust_3d_g.F index 4637a4066..3863ae55a 100644 --- a/src/clib/calc_tdust_3d_g.F +++ b/src/clib/calc_tdust_3d_g.F @@ -205,16 +205,11 @@ subroutine calc_tdust_3d_g( & myisrf(in) ! Additional slice locals - R_PREC brem(in), ceHeI(in), ceHeII(in), ceHI(in), - & cieco(in), ciHeI(in), ciHeII(in), ciHeIS(in), - & ciHI(in), comp1(in), comp2(in), edot(in), + R_PREC comp1(in), comp2(in), edot(in), & energy(in), gammaha_eff(in), gasgr_tdust(in), - & gphdl(in), gpldl(in), h2k01(in), hdlow(in), - & hdlte(in), hyd01k(in), mmw(in), myde(in), - & mynh(in), p2d(in), regr(in), reHeII1(in), - & reHeIII(in), reHII(in), rhoH(in), reHeII(in), - & reHeII2(in), roth(in), rotl(in), tgasold(in), - & vibh(in) + & mmw(in), myde(in), + & mynh(in), p2d(in), regr(in), rhoH(in), + & tgasold(in) ! Iteration mask for multi_cool MASK_TYPE itmask(in) @@ -258,12 +253,6 @@ subroutine calc_tdust_3d_g( !$omp& tgas, tgasold, !$omp& tdust, metallicity, dust2gas, rhoH, mmw, !$omp& mynh, myde, gammaha_eff, gasgr_tdust, regr, edot, -!$omp& ceHI, ceHeI, ceHeII, -!$omp& ciHI, ciHeI, ciHeIS, ciHeII, -!$omp& reHII, reHeII1, reHeII2, reHeIII, -!$omp& brem, cieco, -!$omp& hyd01k, h2k01, vibh, roth, rotl, -!$omp& gpldl, gphdl, hdlte, hdlow, !$omp& itmask ) #endif do t = 0, dk*dj-1 @@ -767,12 +756,6 @@ subroutine calc_tdust_3d_g( !$omp& tgas, tgasold, !$omp& tdust, metallicity, dust2gas, rhoH, mmw, !$omp& mynh, myde, gammaha_eff, gasgr_tdust, regr, edot, -!$omp& ceHI, ceHeI, ceHeII, -!$omp& ciHI, ciHeI, ciHeIS, ciHeII, -!$omp& reHII, reHeII1, reHeII2, reHeIII, -!$omp& brem, cieco, -!$omp& hyd01k, h2k01, vibh, roth, rotl, -!$omp& gpldl, gphdl, hdlte, hdlow, !$omp& itmask ) #endif do t = 0, dk*dj-1 From 43fa8aeb6fefd89c6e2a39641815ef6ebb84d3f9 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 10:47:47 -0500 Subject: [PATCH 002/101] calc_tdust_3d_g: remove misc unused variables 2/4 --- src/clib/calc_tdust_3d_g.F | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/clib/calc_tdust_3d_g.F b/src/clib/calc_tdust_3d_g.F index 3863ae55a..4419b544b 100644 --- a/src/clib/calc_tdust_3d_g.F +++ b/src/clib/calc_tdust_3d_g.F @@ -206,10 +206,9 @@ subroutine calc_tdust_3d_g( ! Additional slice locals R_PREC comp1(in), comp2(in), edot(in), - & energy(in), gammaha_eff(in), gasgr_tdust(in), - & mmw(in), myde(in), - & mynh(in), p2d(in), regr(in), rhoH(in), - & tgasold(in) + & energy(in), + & mmw(in), + & p2d(in), rhoH(in) ! Iteration mask for multi_cool MASK_TYPE itmask(in) @@ -250,9 +249,9 @@ subroutine calc_tdust_3d_g( !$omp& comp1, comp2, energy, !$omp& indixe, !$omp& t1, t2, logtem, tdef, p2d, -!$omp& tgas, tgasold, +!$omp& tgas, !$omp& tdust, metallicity, dust2gas, rhoH, mmw, -!$omp& mynh, myde, gammaha_eff, gasgr_tdust, regr, edot, +!$omp& edot, !$omp& itmask ) #endif do t = 0, dk*dj-1 @@ -753,9 +752,9 @@ subroutine calc_tdust_3d_g( !$omp& comp1, comp2, energy, !$omp& indixe, !$omp& t1, t2, logtem, tdef, p2d, -!$omp& tgas, tgasold, +!$omp& tgas, !$omp& tdust, metallicity, dust2gas, rhoH, mmw, -!$omp& mynh, myde, gammaha_eff, gasgr_tdust, regr, edot, +!$omp& edot, !$omp& itmask ) #endif do t = 0, dk*dj-1 From 3e03795b445a9a4d4d360747b63aac5d6555eeae Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 10:52:42 -0500 Subject: [PATCH 003/101] calc_tdust_3d_g: remove misc unused variables 3/4 --- src/clib/calc_tdust_3d_g.F | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/clib/calc_tdust_3d_g.F b/src/clib/calc_tdust_3d_g.F index 4419b544b..8cc9b14cc 100644 --- a/src/clib/calc_tdust_3d_g.F +++ b/src/clib/calc_tdust_3d_g.F @@ -205,7 +205,7 @@ subroutine calc_tdust_3d_g( & myisrf(in) ! Additional slice locals - R_PREC comp1(in), comp2(in), edot(in), + R_PREC edot(in), & energy(in), & mmw(in), & p2d(in), rhoH(in) @@ -246,7 +246,7 @@ subroutine calc_tdust_3d_g( #ifdef _OPENMP !$omp parallel do schedule(runtime) private( !$omp& i, j, k, -!$omp& comp1, comp2, energy, +!$omp& energy, !$omp& indixe, !$omp& t1, t2, logtem, tdef, p2d, !$omp& tgas, @@ -749,7 +749,7 @@ subroutine calc_tdust_3d_g( #ifdef _OPENMP !$omp parallel do schedule(runtime) private( !$omp& i, j, k, -!$omp& comp1, comp2, energy, +!$omp& energy, !$omp& indixe, !$omp& t1, t2, logtem, tdef, p2d, !$omp& tgas, From 99dad7af63fb8afa257668823f2969710e49c2b3 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 10:56:02 -0500 Subject: [PATCH 004/101] calc_tdust_3d_g: remove misc unused variables 4/4 --- src/clib/calc_tdust_3d_g.F | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/clib/calc_tdust_3d_g.F b/src/clib/calc_tdust_3d_g.F index 8cc9b14cc..932de8743 100644 --- a/src/clib/calc_tdust_3d_g.F +++ b/src/clib/calc_tdust_3d_g.F @@ -204,11 +204,6 @@ subroutine calc_tdust_3d_g( & tgas(in), tdust(in), nh(in), gasgr(in), & myisrf(in) -! Additional slice locals - R_PREC edot(in), - & energy(in), - & mmw(in), - & p2d(in), rhoH(in) ! Iteration mask for multi_cool MASK_TYPE itmask(in) @@ -246,12 +241,10 @@ subroutine calc_tdust_3d_g( #ifdef _OPENMP !$omp parallel do schedule(runtime) private( !$omp& i, j, k, -!$omp& energy, !$omp& indixe, -!$omp& t1, t2, logtem, tdef, p2d, +!$omp& t1, t2, logtem, tdef, !$omp& tgas, -!$omp& tdust, metallicity, dust2gas, rhoH, mmw, -!$omp& edot, +!$omp& tdust, metallicity, dust2gas, !$omp& itmask ) #endif do t = 0, dk*dj-1 @@ -749,12 +742,10 @@ subroutine calc_tdust_3d_g( #ifdef _OPENMP !$omp parallel do schedule(runtime) private( !$omp& i, j, k, -!$omp& energy, !$omp& indixe, -!$omp& t1, t2, logtem, tdef, p2d, +!$omp& t1, t2, logtem, tdef, !$omp& tgas, -!$omp& tdust, metallicity, dust2gas, rhoH, mmw, -!$omp& edot, +!$omp& tdust, metallicity, dust2gas, !$omp& itmask ) #endif do t = 0, dk*dj-1 From 22ec78f5b94e6166ced96ffba80600cbb5654925 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 11:13:38 -0500 Subject: [PATCH 005/101] calc_tdust_3d_g: cleanup 1st parallel clause this parallel clause is used when converting from comoving to proper --- src/clib/calc_tdust_3d_g.F | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/clib/calc_tdust_3d_g.F b/src/clib/calc_tdust_3d_g.F index 932de8743..0782d0e10 100644 --- a/src/clib/calc_tdust_3d_g.F +++ b/src/clib/calc_tdust_3d_g.F @@ -239,13 +239,7 @@ subroutine calc_tdust_3d_g( ! parallelize the k and j loops with OpenMP ! flat j and k loops for better parallelism #ifdef _OPENMP -!$omp parallel do schedule(runtime) private( -!$omp& i, j, k, -!$omp& indixe, -!$omp& t1, t2, logtem, tdef, -!$omp& tgas, -!$omp& tdust, metallicity, dust2gas, -!$omp& itmask ) +!$omp parallel do schedule(runtime) private(i, j, k) #endif do t = 0, dk*dj-1 k = t/dj + ks+1 From d3a0ae7e74799ad082977b9f1280c98599fefe3b Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 11:19:57 -0500 Subject: [PATCH 006/101] calc_tdust_3d_g: cleanup 3rd parallel clause this parallel clause is used when converting from proper to comoving --- src/clib/calc_tdust_3d_g.F | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/clib/calc_tdust_3d_g.F b/src/clib/calc_tdust_3d_g.F index 0782d0e10..773f6af60 100644 --- a/src/clib/calc_tdust_3d_g.F +++ b/src/clib/calc_tdust_3d_g.F @@ -734,13 +734,7 @@ subroutine calc_tdust_3d_g( ! parallelize the k and j loops with OpenMP ! flat j and k loops for better parallelism #ifdef _OPENMP -!$omp parallel do schedule(runtime) private( -!$omp& i, j, k, -!$omp& indixe, -!$omp& t1, t2, logtem, tdef, -!$omp& tgas, -!$omp& tdust, metallicity, dust2gas, -!$omp& itmask ) +!$omp parallel do schedule(runtime) private(i, j, k) #endif do t = 0, dk*dj-1 k = t/dj + ks+1 From 8ebfc1c58bc947779f87167129d0d64a697d05b0 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 11:27:56 -0500 Subject: [PATCH 007/101] calc_tdust_3d_g: remove dust-chem metal species Removed unused local variables from `calc_tdust_3d_g` that followed the naming scheme used in solve_rate_cool for tracking the mass density for the mass densities of various metal species used in dust chemistry. There was logic to convert between proper and comoving units for each of these variables, but they were not actually used for anything (which is a good thing since they were never initialized). These variable declarations were probably blindly copied from somewhere else and were never removed. --- src/clib/calc_tdust_3d_g.F | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/src/clib/calc_tdust_3d_g.F b/src/clib/calc_tdust_3d_g.F index 773f6af60..f9035880e 100644 --- a/src/clib/calc_tdust_3d_g.F +++ b/src/clib/calc_tdust_3d_g.F @@ -98,8 +98,7 @@ subroutine calc_tdust_3d_g( & , SiOI(in,jn,kn) , SiO2I(in,jn,kn) , CH(in,jn,kn) & , CH2(in,jn,kn) , COII(in,jn,kn) , OII(in,jn,kn) & , OHII(in,jn,kn) , H2OII(in,jn,kn) , H3OII(in,jn,kn) - & , O2II(in,jn,kn) , Mg(in,jn,kn) , Al(in,jn,kn) - & , S(in,jn,kn) , Fe(in,jn,kn) + & , O2II(in,jn,kn) R_PREC SiM(in,jn,kn), FeM(in,jn,kn), Mg2SiO4(in,jn,kn) & , MgSiO3(in,jn,kn), Fe3O4(in,jn,kn), AC(in,jn,kn) & , SiO2D(in,jn,kn), MgO(in,jn,kn), FeS(in,jn,kn) @@ -283,16 +282,6 @@ subroutine calc_tdust_3d_g( H2OII(i,j,k) = H2OII(i,j,k)/real(aye**3, RKIND) H3OII(i,j,k) = H3OII(i,j,k)/real(aye**3, RKIND) O2II(i,j,k) = O2II(i,j,k)/real(aye**3, RKIND) - if ( ( igrgr .eq. 1 ) .or. ( idsub .eq. 1 ) ) then - if (idspecies .gt. 0) then - Mg(i,j,k) = Mg(i,j,k)/real(aye**3, RKIND) - endif - if (idspecies .gt. 1) then - Al(i,j,k) = Al(i,j,k)/real(aye**3, RKIND) - S(i,j,k) = S(i,j,k)/real(aye**3, RKIND) - Fe(i,j,k) = Fe(i,j,k)/real(aye**3, RKIND) - endif - endif endif !! endif enddo @@ -778,16 +767,6 @@ subroutine calc_tdust_3d_g( H2OII(i,j,k) = H2OII(i,j,k)*real(aye**3, RKIND) H3OII(i,j,k) = H3OII(i,j,k)*real(aye**3, RKIND) O2II(i,j,k) = O2II(i,j,k)*real(aye**3, RKIND) - if ( ( igrgr .eq. 1 ) .or. ( idsub .eq. 1 ) ) then - if (idspecies .gt. 0) then - Mg(i,j,k) = Mg(i,j,k)*real(aye**3, RKIND) - endif - if (idspecies .gt. 1) then - Al(i,j,k) = Al(i,j,k)*real(aye**3, RKIND) - S(i,j,k) = S(i,j,k)*real(aye**3, RKIND) - Fe(i,j,k) = Fe(i,j,k)*real(aye**3, RKIND) - endif - endif endif !! endif enddo From 36caa9843bda23b04b578f1a7acf2c815d0603de Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 11:31:30 -0500 Subject: [PATCH 008/101] calc_tdust_3d_g: remove metal-chem metal species Removed unused local variables from `calc_tdust_3d_g` that followed the naming scheme used in solve_rate_cool for tracking the mass density for tracking the mass densities of various metal species used in metal chemistry. There was logic to convert between proper and comoving units for each of these variables, but they were not actually used for anything (which is a good thing since they were never initialized). These variable declarations were probably blindly copied from somewhere else and were never removed. --- src/clib/calc_tdust_3d_g.F | 53 -------------------------------------- 1 file changed, 53 deletions(-) diff --git a/src/clib/calc_tdust_3d_g.F b/src/clib/calc_tdust_3d_g.F index f9035880e..c8116e4c7 100644 --- a/src/clib/calc_tdust_3d_g.F +++ b/src/clib/calc_tdust_3d_g.F @@ -92,13 +92,6 @@ subroutine calc_tdust_3d_g( & , metal_F50(in,jn,kn), metal_F80(in,jn,kn) & , metal_P170(in,jn,kn), metal_P200(in,jn,kn) & , metal_Y19(in,jn,kn) - & , CI(in,jn,kn) , CII(in,jn,kn) , CO(in,jn,kn) - & , CO2(in,jn,kn) , OI(in,jn,kn) , OH(in,jn,kn) - & , H2O(in,jn,kn) , O2(in,jn,kn) , SiI(in,jn,kn) - & , SiOI(in,jn,kn) , SiO2I(in,jn,kn) , CH(in,jn,kn) - & , CH2(in,jn,kn) , COII(in,jn,kn) , OII(in,jn,kn) - & , OHII(in,jn,kn) , H2OII(in,jn,kn) , H3OII(in,jn,kn) - & , O2II(in,jn,kn) R_PREC SiM(in,jn,kn), FeM(in,jn,kn), Mg2SiO4(in,jn,kn) & , MgSiO3(in,jn,kn), Fe3O4(in,jn,kn), AC(in,jn,kn) & , SiO2D(in,jn,kn), MgO(in,jn,kn), FeS(in,jn,kn) @@ -261,29 +254,6 @@ subroutine calc_tdust_3d_g( metal_P200(i,j,k)=metal_P200(i,j,k)/real(aye**3, RKIND) metal_Y19(i,j,k) = metal_Y19(i,j,k)/real(aye**3, RKIND) endif -!! if (metal(i,j,k) .gt. 1.d-9 * d(i,j,k)) then - if (imchem .eq. 1) then - CI(i,j,k) = CI(i,j,k)/real(aye**3, RKIND) - CII(i,j,k) = CII(i,j,k)/real(aye**3, RKIND) - CO(i,j,k) = CO(i,j,k)/real(aye**3, RKIND) - CO2(i,j,k) = CO2(i,j,k)/real(aye**3, RKIND) - OI(i,j,k) = OI(i,j,k)/real(aye**3, RKIND) - OH(i,j,k) = OH(i,j,k)/real(aye**3, RKIND) - H2O(i,j,k) = H2O(i,j,k)/real(aye**3, RKIND) - O2(i,j,k) = O2(i,j,k)/real(aye**3, RKIND) - SiI(i,j,k) = SiI(i,j,k)/real(aye**3, RKIND) - SiOI(i,j,k) = SiOI(i,j,k)/real(aye**3, RKIND) - SiO2I(i,j,k) = SiO2I(i,j,k)/real(aye**3, RKIND) - CH(i,j,k) = CH(i,j,k)/real(aye**3, RKIND) - CH2(i,j,k) = CH2(i,j,k)/real(aye**3, RKIND) - COII(i,j,k) = COII(i,j,k)/real(aye**3, RKIND) - OII(i,j,k) = OII(i,j,k)/real(aye**3, RKIND) - OHII(i,j,k) = OHII(i,j,k)/real(aye**3, RKIND) - H2OII(i,j,k) = H2OII(i,j,k)/real(aye**3, RKIND) - H3OII(i,j,k) = H3OII(i,j,k)/real(aye**3, RKIND) - O2II(i,j,k) = O2II(i,j,k)/real(aye**3, RKIND) - endif -!! endif enddo endif if (idustfield .eq. 1) then @@ -746,29 +716,6 @@ subroutine calc_tdust_3d_g( metal_P200(i,j,k)=metal_P200(i,j,k)*real(aye**3, RKIND) metal_Y19(i,j,k) = metal_Y19(i,j,k)*real(aye**3, RKIND) endif -!! if (metal(i,j,k) .gt. 1.d-9 * d(i,j,k)) then - if (imchem .eq. 1) then - CI(i,j,k) = CI(i,j,k)*real(aye**3, RKIND) - CII(i,j,k) = CII(i,j,k)*real(aye**3, RKIND) - CO(i,j,k) = CO(i,j,k)*real(aye**3, RKIND) - CO2(i,j,k) = CO2(i,j,k)*real(aye**3, RKIND) - OI(i,j,k) = OI(i,j,k)*real(aye**3, RKIND) - OH(i,j,k) = OH(i,j,k)*real(aye**3, RKIND) - H2O(i,j,k) = H2O(i,j,k)*real(aye**3, RKIND) - O2(i,j,k) = O2(i,j,k)*real(aye**3, RKIND) - SiI(i,j,k) = SiI(i,j,k)*real(aye**3, RKIND) - SiOI(i,j,k) = SiOI(i,j,k)*real(aye**3, RKIND) - SiO2I(i,j,k) = SiO2I(i,j,k)*real(aye**3, RKIND) - CH(i,j,k) = CH(i,j,k)*real(aye**3, RKIND) - CH2(i,j,k) = CH2(i,j,k)*real(aye**3, RKIND) - COII(i,j,k) = COII(i,j,k)*real(aye**3, RKIND) - OII(i,j,k) = OII(i,j,k)*real(aye**3, RKIND) - OHII(i,j,k) = OHII(i,j,k)*real(aye**3, RKIND) - H2OII(i,j,k) = H2OII(i,j,k)*real(aye**3, RKIND) - H3OII(i,j,k) = H3OII(i,j,k)*real(aye**3, RKIND) - O2II(i,j,k) = O2II(i,j,k)*real(aye**3, RKIND) - endif -!! endif enddo endif if (idustfield .eq. 1) then From 78cf9aa5bc659ad4ea07c7fc16f3795cab09629e Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 16:59:21 -0500 Subject: [PATCH 009/101] calc_tdust_3d_g: adopt more consistent type declarations with other parts of the code. --- src/clib/calc_tdust_3d_g.F | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/clib/calc_tdust_3d_g.F b/src/clib/calc_tdust_3d_g.F index c8116e4c7..ac360c180 100644 --- a/src/clib/calc_tdust_3d_g.F +++ b/src/clib/calc_tdust_3d_g.F @@ -191,8 +191,8 @@ subroutine calc_tdust_3d_g( ! Slice locals - integer indixe(in) - R_PREC t1(in), t2(in), logtem(in), tdef(in), + integer*8 indixe(in) + real*8 t1(in), t2(in), logtem(in), tdef(in), & tgas(in), tdust(in), nh(in), gasgr(in), & myisrf(in) From 33f398a052fb595f79dd715e3e0644a9988c44ce Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 15:40:46 -0500 Subject: [PATCH 010/101] cool1d_multi_g: tweak dust-related calcs --- src/clib/cool1d_multi_g.F | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/clib/cool1d_multi_g.F b/src/clib/cool1d_multi_g.F index 75fa7dedf..d4454adcb 100644 --- a/src/clib/cool1d_multi_g.F +++ b/src/clib/cool1d_multi_g.F @@ -1155,10 +1155,16 @@ subroutine cool1d_multi_g( endif endif -! Calculate heating from interstellar radiation field -! -> this is ONLY used when `itmask_metal .eq. MASK_TRUE` + ! compute dust temperature and cooling due to dust + if (anydust .ne. MASK_FALSE) then + ! Calculate heating from interstellar radiation field + ! -> this is ONLY used when `itmask_metal .eq. MASK_TRUE` + + ! NOTE: An earlier version of this logic would also compute + ! mygisrf (or the set of girsf[...]) when anydust + ! was MASK_FALSE and igammah > 1. However, there was + ! no need to do this. - if ((anydust .ne. MASK_FALSE) .or. (igammah .gt. 1)) then do i = is+1, ie+1 if ( itmask_metal(i) .ne. MASK_FALSE ) then @@ -1209,12 +1215,9 @@ subroutine cool1d_multi_g( endif enddo - endif ! --- Gas to grain heat transfer --- - if (anydust .ne. MASK_FALSE) then - ! Look up gas/grain heat transfer rates do i = is+1, ie+1 From 6c3ddf5a8ea875aafb0014001defae5452b4686d Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 16:14:29 -0500 Subject: [PATCH 011/101] factor out calc_all_tdust_gasgr_1d_g --- src/clib/CMakeLists.txt | 1 + src/clib/Make.config.objects | 1 + src/clib/calc_all_tdust_gasgr_1d_g.F | 384 +++++++++++++++++++++++++++ src/clib/cool1d_multi_g.F | 267 +++---------------- 4 files changed, 423 insertions(+), 230 deletions(-) create mode 100644 src/clib/calc_all_tdust_gasgr_1d_g.F diff --git a/src/clib/CMakeLists.txt b/src/clib/CMakeLists.txt index 2539ec015..25c507046 100644 --- a/src/clib/CMakeLists.txt +++ b/src/clib/CMakeLists.txt @@ -110,6 +110,7 @@ add_library(Grackle_Grackle utils-cpp.C utils-cpp.hpp # Fortran Source Files + calc_all_tdust_gasgr_1d_g.F calc_tdust_1d_g.F calc_tdust_3d_g.F calc_temp1d_cloudy_g.F diff --git a/src/clib/Make.config.objects b/src/clib/Make.config.objects index 9cdeebc9d..049e875e9 100644 --- a/src/clib/Make.config.objects +++ b/src/clib/Make.config.objects @@ -40,6 +40,7 @@ OBJS_CONFIG_LIB = \ solve_rate_cool_g.lo \ update_UVbackground_rates.lo \ lookup_cool_rates0d.lo \ + calc_all_tdust_gasgr_1d_g.lo \ calc_tdust_1d_g.lo \ calc_tdust_3d_g.lo \ calc_grain_size_increment_1d.lo \ diff --git a/src/clib/calc_all_tdust_gasgr_1d_g.F b/src/clib/calc_all_tdust_gasgr_1d_g.F new file mode 100644 index 000000000..c5ff7fc19 --- /dev/null +++ b/src/clib/calc_all_tdust_gasgr_1d_g.F @@ -0,0 +1,384 @@ + subroutine calc_all_tdust_gasgr_1d_g(in, jn, kn, nratec, + & idustfield, is, ie, j, k, fgr, gamma_isrfa, + & trad, gasgra, indixe, tdef, tgas, tdust, + & metallicity, dust2gas, mynh, gasgr_tdust, + & priGridRank, priGridDim, priDataSize, + & metGridRank, metGridDim, metDataSize, + & itmask_metal, LH2_N, LH2_Size, LHD_N, LHD_Size, + & LCI_N, LCI_Size, LCII_N, LCII_Size, LOI_N, + & LOI_Size, LCO_N, LCO_Size, LOH_N, LOH_Size, + & LH2O_N, LH2O_Size, alphap_N, alphap_Size, + & idspecies, itdmulti, SN0_N, gr_N, gr_Size, gr_dT, + & gr_Td, tSiM, tFeM, tMg2SiO4, tMgSiO3, tFe3O4, + & tAC, tSiO2D, tMgO, tFeS, tAl2O3, treforg, + & tvolorg, tH2Oice, gasgr2a, gamma_isrf2a, + & coolunit, gasgr, myisrf, sgSiM, sgFeM, sgMg2SiO4, + & sgMgSiO3, sgFe3O4, sgAC, sgSiO2D, sgMgO, sgFeS, + & sgAl2O3, sgreforg, sgvolorg, sgH2Oice, sgtot, + & alSiM, alFeM, alMg2SiO4, alMgSiO3, alFe3O4, alAC, + & alSiO2D, alMgO, alFeS, alAl2O3, alreforg, + & alvolorg, alH2Oice, altot, kpSiM, kpFeM, + & kpMg2SiO4, kpMgSiO3, kpFe3O4, kpAC, kpSiO2D, + & kpMgO, kpFeS, kpAl2O3, kpreforg, kpvolorg, + & kpH2Oice, kptot, gasSiM, gasFeM, gasMg2SiO4, + & gasMgSiO3, gasFe3O4, gasAC, gasSiO2D, gasMgO, + & gasFeS, gasAl2O3, gasreforg, gasvolorg, + & gasH2Oice, gasgr_tSiM, gasgr_tFeM, + & gasgr_tMg2SiO4, gasgr_tMgSiO3, gasgr_tFe3O4, + & gasgr_tAC, gasgr_tSiO2D, gasgr_tMgO, gasgr_tFeS, + & gasgr_tAl2O3, gasgr_treforg, gasgr_tvolorg, + & gasgr_tH2Oice, mygisrf, fv2k, fac, gisrfSiM, + & gisrfFeM, gisrfMg2SiO4, gisrfMgSiO3, gisrfFe3O4, + & gisrfAC, gisrfSiO2D, gisrfMgO, gisrfFeS, + & gisrfAl2O3, gisrfreforg, gisrfvolorg, + & gisrfH2Oice, anydust + & ) + +! PURPOSE: +! Calculate all dust temperature(s) and the gas to grain heat +! transfer rate(s) (the latter is commonly called gasgr) +! +! An argument could be made for directly using gasgr to compute +! contributions to edot within this routine, rather than returning +! the gasgr value(s). But that should be reconsidered in the future. +! +! We could significantly reduce the amount of buffer space allocated +! within the routine. But, we will hold off on doing that until after +! we have transcribed to C/C++ +! +! INPUTS: +! is,ie - start and end indicies of active region (zero-based!) +! +! PARAMETERS: +! + +!----------------------------------------------------------------------- + + implicit NONE +#include "grackle_fortran_types.def" +#include "phys_const.def" + +! Arguments + + integer in, jn, kn, is, ie, j, k, nratec, idustfield + + real*8 fgr + real*8 gasgra(nratec), gamma_isrfa + +! Cloudy cooling data + + integer*8 priGridRank, priDataSize, + & metGridRank, metDataSize, + & priGridDim(priGridRank), metGridDim(metGridRank) + +! Parameters + + real*8, parameter :: mh = mass_h !DPC + ! approx. mean molecular weight of metals + +! Locals + + integer i + real*8 trad, coolunit + +! Slice locals + + integer*8 indixe(in) + real*8 tdef(in), tgas(in), tdust(in), mynh(in), metallicity(in), + & dust2gas(in) + +! Cooling/heating slice locals + + real*8 gasgr(in), gasgr_tdust(in), myisrf(in) + integer*8 LH2_N(3), LH2_Size + integer*8 LHD_N(3), LHD_Size + integer*8 LCI_N(3), LCI_Size + integer*8 LCII_N(3), LCII_Size + integer*8 LOI_N(3), LOI_Size + integer*8 LCO_N(3), LCO_Size + integer*8 LOH_N(3), LOH_Size + integer*8 LH2O_N(3), LH2O_Size + integer*8 alphap_N(2), alphap_Size + integer idspecies, itdmulti + integer SN0_N +! opacity table + integer gr_N(2), gr_Size + real*8 gr_dT, gr_Td(gr_N(2)) +! grain growth + real*8 sgSiM(in), sgFeM(in), sgMg2SiO4(in) + & , sgMgSiO3(in), sgFe3O4(in), sgAC(in) + & , sgSiO2D(in), sgMgO(in), sgFeS(in) + & , sgAl2O3(in) + & , sgreforg(in), sgvolorg(in), sgH2Oice(in) + & , sgtot(in) + real*8 alSiM(gr_N(2),in), alFeM(gr_N(2),in) + & , alMg2SiO4(gr_N(2),in), alMgSiO3(gr_N(2),in) + & , alFe3O4(gr_N(2),in), alAC(gr_N(2),in) + & , alSiO2D(gr_N(2),in), alMgO(gr_N(2),in) + & , alFeS(gr_N(2),in), alAl2O3(gr_N(2),in) + & , alreforg(gr_N(2),in) + & , alvolorg(gr_N(2),in), alH2Oice(gr_N(2),in) + & , altot(gr_N(2),in) + real*8 kpSiM(in), kpFeM(in), kpMg2SiO4(in) + & , kpMgSiO3(in), kpFe3O4(in), kpAC(in) + & , kpSiO2D(in), kpMgO(in), kpFeS(in) + & , kpAl2O3(in) + & , kpreforg(in), kpvolorg(in), kpH2Oice(in) + & , kptot(in) +! grain temperature + real*8 tSiM(in), tFeM(in), tMg2SiO4(in) + & , tMgSiO3(in), tFe3O4(in), tAC(in) + & , tSiO2D(in), tMgO(in), tFeS(in) + & , tAl2O3(in) + & , treforg(in), tvolorg(in), tH2Oice(in) + real*8 gasgr2a(nratec), gamma_isrf2a + real*8 gasSiM(in), gasFeM(in), gasMg2SiO4(in) + & , gasMgSiO3(in), gasFe3O4(in), gasAC(in) + & , gasSiO2D(in), gasMgO(in), gasFeS(in) + & , gasAl2O3(in) + & , gasreforg(in), gasvolorg(in), gasH2Oice(in) + real*8 gasgr_tSiM(in), gasgr_tFeM(in), gasgr_tMg2SiO4(in) + & , gasgr_tMgSiO3(in), gasgr_tFe3O4(in), gasgr_tAC(in) + & , gasgr_tSiO2D(in), gasgr_tMgO(in), gasgr_tFeS(in) + & , gasgr_tAl2O3(in) + & , gasgr_treforg(in), gasgr_tvolorg(in), gasgr_tH2Oice(in) + real*8 mygisrf(in), fv2k, fac + real*8 gisrfSiM(in), gisrfFeM(in), gisrfMg2SiO4(in) + & , gisrfMgSiO3(in), gisrfFe3O4(in), gisrfAC(in) + & , gisrfSiO2D(in), gisrfMgO(in), gisrfFeS(in) + & , gisrfAl2O3(in) + & , gisrfreforg(in), gisrfvolorg(in), gisrfH2Oice(in) +! Iteration mask + + MASK_TYPE anydust + MASK_TYPE itmask_metal(in) + + ! Calculate heating from interstellar radiation field + ! -> this is ONLY used when `itmask_metal .eq. MASK_TRUE` + + do i = is+1, ie+1 + if ( itmask_metal(i) .ne. MASK_FALSE ) then + + if (idspecies .eq. 0 ) then + if (idustfield .gt. 0) then + mygisrf(i) = gamma_isrfa + & * fgr / dust2gas(i) * metallicity(i) + !! correct with the depletion or enhancement of condensation rate. + else + mygisrf(i) = gamma_isrfa + endif + + else ! idspecies + + if (itdmulti .eq. 0) then + + mygisrf(i) = gamma_isrf2a * sgtot(i) + !! in UV, absorption coefficient Q ~ 1 (Goldsmith 2001) + !! so we use the geometrical cross-section of grains [cgs] + + else + + if (idspecies .gt. 0) then + gisrfMgSiO3 (i) = gamma_isrf2a * sgMgSiO3 (i) +!! write(*,*) 'sil', d(i,j,k), gamma_isrf2a, sgMgSiO3(i) + gisrfAC (i) = gamma_isrf2a * sgAC (i) +!! write(*,*) 'car', d(i,j,k), gamma_isrf2a, sgMgSiO3(i) + endif + if (idspecies .gt. 1) then + gisrfSiM (i) = gamma_isrf2a * sgSiM (i) + gisrfFeM (i) = gamma_isrf2a * sgFeM (i) + gisrfMg2SiO4 (i) = gamma_isrf2a * sgMg2SiO4 (i) + gisrfFe3O4 (i) = gamma_isrf2a * sgFe3O4 (i) + gisrfSiO2D (i) = gamma_isrf2a * sgSiO2D (i) + gisrfMgO (i) = gamma_isrf2a * sgMgO (i) + gisrfFeS (i) = gamma_isrf2a * sgFeS (i) + gisrfAl2O3 (i) = gamma_isrf2a * sgAl2O3 (i) + endif + if (idspecies .gt. 2) then + gisrfreforg (i) = gamma_isrf2a * sgreforg (i) + gisrfvolorg (i) = gamma_isrf2a * sgvolorg (i) + gisrfH2Oice (i) = gamma_isrf2a * sgH2Oice (i) + endif + + endif + + endif ! idspecies + + endif + enddo + +! --- Gas to grain heat transfer --- + +! Look up gas/grain heat transfer rates + + do i = is+1, ie+1 + if ( itmask_metal(i) .ne. MASK_FALSE ) then + + if(idspecies .eq. 0) then + + gasgr(i) = gasgra(indixe(i)) + tdef(i) + & *(gasgra(indixe(i)+1) -gasgra(indixe(i))) + +!! gasgr_tdust(i) = fgr * gasgr(i) * coolunit / mh + gasgr_tdust(i) = (dust2gas(i) / metallicity(i)) + & * gasgr(i) * coolunit / mh + !! apply to (idustfield .eq. 1) GC20200701 + + else ! idspecies + + fv2k = gasgr2a(indixe(i)) + tdef(i) + & *(gasgr2a(indixe(i)+1) -gasgr2a(indixe(i))) + + fac = coolunit / mh + + if ( itdmulti .eq. 0 ) then + + gasgr(i) = fv2k * sgtot(i) + + gasgr_tdust(i) = gasgr(i) * fac + + else + + if (idspecies .gt. 0) then + gasMgSiO3 (i) = fv2k * sgMgSiO3 (i) + gasAC (i) = fv2k * sgAC (i) + endif + if (idspecies .gt. 1) then + gasSiM (i) = fv2k * sgSiM (i) + gasFeM (i) = fv2k * sgFeM (i) + gasMg2SiO4 (i) = fv2k * sgMg2SiO4 (i) + gasFe3O4 (i) = fv2k * sgFe3O4 (i) + gasSiO2D (i) = fv2k * sgSiO2D (i) + gasMgO (i) = fv2k * sgMgO (i) + gasFeS (i) = fv2k * sgFeS (i) + gasAl2O3 (i) = fv2k * sgAl2O3 (i) + endif + if (idspecies .gt. 2) then + gasreforg (i) = fv2k * sgreforg (i) + gasvolorg (i) = fv2k * sgvolorg (i) + gasH2Oice (i) = fv2k * sgH2Oice (i) + endif + + if (idspecies .gt. 0) then + gasgr_tMgSiO3 (i) = gasMgSiO3 (i) * fac + gasgr_tAC (i) = gasAC (i) * fac + endif + if (idspecies .gt. 1) then + gasgr_tSiM (i) = gasSiM (i) * fac + gasgr_tFeM (i) = gasFeM (i) * fac + gasgr_tMg2SiO4 (i) = gasMg2SiO4 (i) * fac + gasgr_tFe3O4 (i) = gasFe3O4 (i) * fac + gasgr_tSiO2D (i) = gasSiO2D (i) * fac + gasgr_tMgO (i) = gasMgO (i) * fac + gasgr_tFeS (i) = gasFeS (i) * fac + gasgr_tAl2O3 (i) = gasAl2O3 (i) * fac + endif + if (idspecies .gt. 2) then + gasgr_treforg (i) = gasreforg (i) * fac + gasgr_tvolorg (i) = gasvolorg (i) * fac + gasgr_tH2Oice (i) = gasH2Oice (i) * fac + endif + + endif + + endif ! idspecies + + endif + enddo + +! Compute dust temperature + + if (itdmulti .eq. 0) then + + call calc_tdust_1d_g(tdust, tgas, mynh, gasgr_tdust, + & mygisrf, myisrf, itmask_metal, trad, in, is, ie, j, k + & , gr_N, gr_Size, gr_dT, gr_Td, altot, kptot, idspecies) + + else + + if (idspecies .gt. 0) then + call calc_tdust_1d_g(tMgSiO3 , tgas, mynh, gasgr_tMgSiO3 , + & gisrfMgSiO3 , myisrf, itmask_metal, trad, + & in, is, ie, j, k + & , gr_N, gr_Size, gr_dT, gr_Td, alMgSiO3 , kpMgSiO3 + & , idspecies) + + call calc_tdust_1d_g(tAC , tgas, mynh, gasgr_tAC , + & gisrfAC , myisrf, itmask_metal, trad, + & in, is, ie, j, k + & , gr_N, gr_Size, gr_dT, gr_Td, alAC , kpAC + & , idspecies) + endif + + if (idspecies .gt. 1) then + call calc_tdust_1d_g(tSiM , tgas, mynh, gasgr_tSiM , + & gisrfSiM , myisrf, itmask_metal, trad, + & in, is, ie, j, k + & , gr_N, gr_Size, gr_dT, gr_Td, alSiM , kpSiM + & , idspecies) + + call calc_tdust_1d_g(tFeM , tgas, mynh, gasgr_tFeM , + & gisrfFeM , myisrf, itmask_metal, trad, + & in, is, ie, j, k + & , gr_N, gr_Size, gr_dT, gr_Td, alFeM , kpFeM + & , idspecies) + + call calc_tdust_1d_g(tMg2SiO4 , tgas, mynh, gasgr_tMg2SiO4 , + & gisrfMg2SiO4 , myisrf, itmask_metal, trad, + & in, is, ie, j, k + & , gr_N, gr_Size, gr_dT, gr_Td, alMg2SiO4 , kpMg2SiO4 + & , idspecies) + + call calc_tdust_1d_g(tFe3O4 , tgas, mynh, gasgr_tFe3O4 , + & gisrfFe3O4 , myisrf, itmask_metal, trad, + & in, is, ie, j, k + & , gr_N, gr_Size, gr_dT, gr_Td, alFe3O4 , kpFe3O4 + & , idspecies) + + call calc_tdust_1d_g(tSiO2D , tgas, mynh, gasgr_tSiO2D , + & gisrfSiO2D , myisrf, itmask_metal, trad, + & in, is, ie, j, k + & , gr_N, gr_Size, gr_dT, gr_Td, alSiO2D , kpSiO2D + & , idspecies) + + call calc_tdust_1d_g(tMgO , tgas, mynh, gasgr_tMgO , + & gisrfMgO , myisrf, itmask_metal, trad, + & in, is, ie, j, k + & , gr_N, gr_Size, gr_dT, gr_Td, alMgO , kpMgO + & , idspecies) + + call calc_tdust_1d_g(tFeS , tgas, mynh, gasgr_tFeS , + & gisrfFeS , myisrf, itmask_metal, trad, + & in, is, ie, j, k + & , gr_N, gr_Size, gr_dT, gr_Td, alFeS , kpFeS + & , idspecies) + + call calc_tdust_1d_g(tAl2O3 , tgas, mynh, gasgr_tAl2O3 , + & gisrfAl2O3 , myisrf, itmask_metal, trad, + & in, is, ie, j, k + & , gr_N, gr_Size, gr_dT, gr_Td, alAl2O3 , kpAl2O3 + & , idspecies) + endif + + if (idspecies .gt. 2) then + call calc_tdust_1d_g(treforg , tgas, mynh, gasgr_treforg , + & gisrfreforg , myisrf, itmask_metal, trad, + & in, is, ie, j, k + & , gr_N, gr_Size, gr_dT, gr_Td, alreforg , kpreforg + & , idspecies) + + call calc_tdust_1d_g(tvolorg , tgas, mynh, gasgr_tvolorg , + & gisrfvolorg , myisrf, itmask_metal, trad, + & in, is, ie, j, k + & , gr_N, gr_Size, gr_dT, gr_Td, alvolorg , kpvolorg + & , idspecies) + + call calc_tdust_1d_g(tH2Oice , tgas, mynh, gasgr_tH2Oice , + & gisrfH2Oice , myisrf, itmask_metal, trad, + & in, is, ie, j, k + & , gr_N, gr_Size, gr_dT, gr_Td, alH2Oice , kpH2Oice + & , idspecies) + endif + + endif + end diff --git a/src/clib/cool1d_multi_g.F b/src/clib/cool1d_multi_g.F index d4454adcb..3e6410d42 100644 --- a/src/clib/cool1d_multi_g.F +++ b/src/clib/cool1d_multi_g.F @@ -1157,241 +1157,48 @@ subroutine cool1d_multi_g( ! compute dust temperature and cooling due to dust if (anydust .ne. MASK_FALSE) then - ! Calculate heating from interstellar radiation field - ! -> this is ONLY used when `itmask_metal .eq. MASK_TRUE` - ! NOTE: An earlier version of this logic would also compute - ! mygisrf (or the set of girsf[...]) when anydust - ! was MASK_FALSE and igammah > 1. However, there was - ! no need to do this. - - do i = is+1, ie+1 - if ( itmask_metal(i) .ne. MASK_FALSE ) then - - if (idspecies .eq. 0 ) then - if (idustfield .gt. 0) then - mygisrf(i) = gamma_isrfa - & * fgr / dust2gas(i) * metallicity(i) - !! correct with the depletion or enhancement of condensation rate. - else - mygisrf(i) = gamma_isrfa - endif - - else ! idspecies - - if (itdmulti .eq. 0) then - - mygisrf(i) = gamma_isrf2a * sgtot(i) - !! in UV, absorption coefficient Q ~ 1 (Goldsmith 2001) - !! so we use the geometrical cross-section of grains [cgs] - - else - - if (idspecies .gt. 0) then - gisrfMgSiO3 (i) = gamma_isrf2a * sgMgSiO3 (i) -!! write(*,*) 'sil', d(i,j,k), gamma_isrf2a, sgMgSiO3(i) - gisrfAC (i) = gamma_isrf2a * sgAC (i) -!! write(*,*) 'car', d(i,j,k), gamma_isrf2a, sgMgSiO3(i) - endif - if (idspecies .gt. 1) then - gisrfSiM (i) = gamma_isrf2a * sgSiM (i) - gisrfFeM (i) = gamma_isrf2a * sgFeM (i) - gisrfMg2SiO4 (i) = gamma_isrf2a * sgMg2SiO4 (i) - gisrfFe3O4 (i) = gamma_isrf2a * sgFe3O4 (i) - gisrfSiO2D (i) = gamma_isrf2a * sgSiO2D (i) - gisrfMgO (i) = gamma_isrf2a * sgMgO (i) - gisrfFeS (i) = gamma_isrf2a * sgFeS (i) - gisrfAl2O3 (i) = gamma_isrf2a * sgAl2O3 (i) - endif - if (idspecies .gt. 2) then - gisrfreforg (i) = gamma_isrf2a * sgreforg (i) - gisrfvolorg (i) = gamma_isrf2a * sgvolorg (i) - gisrfH2Oice (i) = gamma_isrf2a * sgH2Oice (i) - endif - - endif - - endif ! idspecies - - endif - enddo - -! --- Gas to grain heat transfer --- - -! Look up gas/grain heat transfer rates - - do i = is+1, ie+1 - if ( itmask_metal(i) .ne. MASK_FALSE ) then - - if(idspecies .eq. 0) then - - gasgr(i) = gasgra(indixe(i)) + tdef(i) - & *(gasgra(indixe(i)+1) -gasgra(indixe(i))) - -!! gasgr_tdust(i) = fgr * gasgr(i) * coolunit / mh - gasgr_tdust(i) = (dust2gas(i) / metallicity(i)) - & * gasgr(i) * coolunit / mh - !! apply to (idustfield .eq. 1) GC20200701 - - else ! idspecies - - fv2k = gasgr2a(indixe(i)) + tdef(i) - & *(gasgr2a(indixe(i)+1) -gasgr2a(indixe(i))) - - fac = coolunit / mh - - if ( itdmulti .eq. 0 ) then - - gasgr(i) = fv2k * sgtot(i) - - gasgr_tdust(i) = gasgr(i) * fac - - else - - if (idspecies .gt. 0) then - gasMgSiO3 (i) = fv2k * sgMgSiO3 (i) - gasAC (i) = fv2k * sgAC (i) - endif - if (idspecies .gt. 1) then - gasSiM (i) = fv2k * sgSiM (i) - gasFeM (i) = fv2k * sgFeM (i) - gasMg2SiO4 (i) = fv2k * sgMg2SiO4 (i) - gasFe3O4 (i) = fv2k * sgFe3O4 (i) - gasSiO2D (i) = fv2k * sgSiO2D (i) - gasMgO (i) = fv2k * sgMgO (i) - gasFeS (i) = fv2k * sgFeS (i) - gasAl2O3 (i) = fv2k * sgAl2O3 (i) - endif - if (idspecies .gt. 2) then - gasreforg (i) = fv2k * sgreforg (i) - gasvolorg (i) = fv2k * sgvolorg (i) - gasH2Oice (i) = fv2k * sgH2Oice (i) - endif - - if (idspecies .gt. 0) then - gasgr_tMgSiO3 (i) = gasMgSiO3 (i) * fac - gasgr_tAC (i) = gasAC (i) * fac - endif - if (idspecies .gt. 1) then - gasgr_tSiM (i) = gasSiM (i) * fac - gasgr_tFeM (i) = gasFeM (i) * fac - gasgr_tMg2SiO4 (i) = gasMg2SiO4 (i) * fac - gasgr_tFe3O4 (i) = gasFe3O4 (i) * fac - gasgr_tSiO2D (i) = gasSiO2D (i) * fac - gasgr_tMgO (i) = gasMgO (i) * fac - gasgr_tFeS (i) = gasFeS (i) * fac - gasgr_tAl2O3 (i) = gasAl2O3 (i) * fac - endif - if (idspecies .gt. 2) then - gasgr_treforg (i) = gasreforg (i) * fac - gasgr_tvolorg (i) = gasvolorg (i) * fac - gasgr_tH2Oice (i) = gasH2Oice (i) * fac - endif - - endif - - endif ! idspecies - - endif - enddo - -! Compute dust temperature - - if (itdmulti .eq. 0) then - - call calc_tdust_1d_g(tdust, tgas, mynh, gasgr_tdust, - & mygisrf, myisrf, itmask_metal, comp2, in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, altot, kptot, idspecies) - - else - - if (idspecies .gt. 0) then - call calc_tdust_1d_g(tMgSiO3 , tgas, mynh, gasgr_tMgSiO3 , - & gisrfMgSiO3 , myisrf, itmask_metal, comp2, - & in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alMgSiO3 , kpMgSiO3 - & , idspecies) - - call calc_tdust_1d_g(tAC , tgas, mynh, gasgr_tAC , - & gisrfAC , myisrf, itmask_metal, comp2, - & in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alAC , kpAC - & , idspecies) - endif - - if (idspecies .gt. 1) then - call calc_tdust_1d_g(tSiM , tgas, mynh, gasgr_tSiM , - & gisrfSiM , myisrf, itmask_metal, comp2, - & in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alSiM , kpSiM - & , idspecies) - - call calc_tdust_1d_g(tFeM , tgas, mynh, gasgr_tFeM , - & gisrfFeM , myisrf, itmask_metal, comp2, - & in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alFeM , kpFeM - & , idspecies) - - call calc_tdust_1d_g(tMg2SiO4 , tgas, mynh, gasgr_tMg2SiO4 , - & gisrfMg2SiO4 , myisrf, itmask_metal, comp2, - & in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alMg2SiO4 , kpMg2SiO4 - & , idspecies) - - call calc_tdust_1d_g(tFe3O4 , tgas, mynh, gasgr_tFe3O4 , - & gisrfFe3O4 , myisrf, itmask_metal, comp2, - & in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alFe3O4 , kpFe3O4 - & , idspecies) - - call calc_tdust_1d_g(tSiO2D , tgas, mynh, gasgr_tSiO2D , - & gisrfSiO2D , myisrf, itmask_metal, comp2, - & in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alSiO2D , kpSiO2D - & , idspecies) - - call calc_tdust_1d_g(tMgO , tgas, mynh, gasgr_tMgO , - & gisrfMgO , myisrf, itmask_metal, comp2, - & in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alMgO , kpMgO - & , idspecies) - - call calc_tdust_1d_g(tFeS , tgas, mynh, gasgr_tFeS , - & gisrfFeS , myisrf, itmask_metal, comp2, - & in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alFeS , kpFeS - & , idspecies) - - call calc_tdust_1d_g(tAl2O3 , tgas, mynh, gasgr_tAl2O3 , - & gisrfAl2O3 , myisrf, itmask_metal, comp2, - & in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alAl2O3 , kpAl2O3 - & , idspecies) - endif - - if (idspecies .gt. 2) then - call calc_tdust_1d_g(treforg , tgas, mynh, gasgr_treforg , - & gisrfreforg , myisrf, itmask_metal, comp2, - & in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alreforg , kpreforg - & , idspecies) - - call calc_tdust_1d_g(tvolorg , tgas, mynh, gasgr_tvolorg , - & gisrfvolorg , myisrf, itmask_metal, comp2, - & in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alvolorg , kpvolorg - & , idspecies) - - call calc_tdust_1d_g(tH2Oice , tgas, mynh, gasgr_tH2Oice , - & gisrfH2Oice , myisrf, itmask_metal, comp2, - & in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alH2Oice , kpH2Oice - & , idspecies) - endif + call calc_all_tdust_gasgr_1d_g(in, jn, kn, nratec, + & idustfield, is, ie, j, k, fgr, gamma_isrfa, + & comp2, gasgra, indixe, tdef, tgas, tdust, + & metallicity, dust2gas, mynh, gasgr_tdust, + & priGridRank, priGridDim, priDataSize, + & metGridRank, metGridDim, metDataSize, + & itmask_metal, LH2_N, LH2_Size, LHD_N, LHD_Size, + & LCI_N, LCI_Size, LCII_N, LCII_Size, LOI_N, + & LOI_Size, LCO_N, LCO_Size, LOH_N, LOH_Size, + & LH2O_N, LH2O_Size, alphap_N, alphap_Size, + & idspecies, itdmulti, SN0_N, gr_N, gr_Size, gr_dT, + & gr_Td, tSiM, tFeM, tMg2SiO4, tMgSiO3, tFe3O4, + & tAC, tSiO2D, tMgO, tFeS, tAl2O3, treforg, + & tvolorg, tH2Oice, gasgr2a, gamma_isrf2a, + & coolunit, gasgr, myisrf, sgSiM, sgFeM, sgMg2SiO4, + & sgMgSiO3, sgFe3O4, sgAC, sgSiO2D, sgMgO, sgFeS, + & sgAl2O3, sgreforg, sgvolorg, sgH2Oice, sgtot, + & alSiM, alFeM, alMg2SiO4, alMgSiO3, alFe3O4, alAC, + & alSiO2D, alMgO, alFeS, alAl2O3, alreforg, + & alvolorg, alH2Oice, altot, kpSiM, kpFeM, + & kpMg2SiO4, kpMgSiO3, kpFe3O4, kpAC, kpSiO2D, + & kpMgO, kpFeS, kpAl2O3, kpreforg, kpvolorg, + & kpH2Oice, kptot, gasSiM, gasFeM, gasMg2SiO4, + & gasMgSiO3, gasFe3O4, gasAC, gasSiO2D, gasMgO, + & gasFeS, gasAl2O3, gasreforg, gasvolorg, + & gasH2Oice, gasgr_tSiM, gasgr_tFeM, + & gasgr_tMg2SiO4, gasgr_tMgSiO3, gasgr_tFe3O4, + & gasgr_tAC, gasgr_tSiO2D, gasgr_tMgO, gasgr_tFeS, + & gasgr_tAl2O3, gasgr_treforg, gasgr_tvolorg, + & gasgr_tH2Oice, mygisrf, fv2k, fac, gisrfSiM, + & gisrfFeM, gisrfMg2SiO4, gisrfMgSiO3, gisrfFe3O4, + & gisrfAC, gisrfSiO2D, gisrfMgO, gisrfFeS, + & gisrfAl2O3, gisrfreforg, gisrfvolorg, + & gisrfH2Oice, anydust + & ) endif ! Calculate dust cooling rate + if (anydust .ne. MASK_FALSE) then + do i = is+1, ie+1 if ( itmask_metal(i) .ne. MASK_FALSE ) then From 25e32d4546b03929a520d155caae97508c68a821 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 16:16:51 -0500 Subject: [PATCH 012/101] calc_all_tdust_gasgr_1d_g: remove some arguments --- src/clib/calc_all_tdust_gasgr_1d_g.F | 7 +------ src/clib/cool1d_multi_g.F | 12 +----------- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/src/clib/calc_all_tdust_gasgr_1d_g.F b/src/clib/calc_all_tdust_gasgr_1d_g.F index c5ff7fc19..60f9ede42 100644 --- a/src/clib/calc_all_tdust_gasgr_1d_g.F +++ b/src/clib/calc_all_tdust_gasgr_1d_g.F @@ -27,11 +27,7 @@ subroutine calc_all_tdust_gasgr_1d_g(in, jn, kn, nratec, & gasgr_tMg2SiO4, gasgr_tMgSiO3, gasgr_tFe3O4, & gasgr_tAC, gasgr_tSiO2D, gasgr_tMgO, gasgr_tFeS, & gasgr_tAl2O3, gasgr_treforg, gasgr_tvolorg, - & gasgr_tH2Oice, mygisrf, fv2k, fac, gisrfSiM, - & gisrfFeM, gisrfMg2SiO4, gisrfMgSiO3, gisrfFe3O4, - & gisrfAC, gisrfSiO2D, gisrfMgO, gisrfFeS, - & gisrfAl2O3, gisrfreforg, gisrfvolorg, - & gisrfH2Oice, anydust + & gasgr_tH2Oice & ) ! PURPOSE: @@ -150,7 +146,6 @@ subroutine calc_all_tdust_gasgr_1d_g(in, jn, kn, nratec, & , gisrfreforg(in), gisrfvolorg(in), gisrfH2Oice(in) ! Iteration mask - MASK_TYPE anydust MASK_TYPE itmask_metal(in) ! Calculate heating from interstellar radiation field diff --git a/src/clib/cool1d_multi_g.F b/src/clib/cool1d_multi_g.F index 3e6410d42..a9f69d96e 100644 --- a/src/clib/cool1d_multi_g.F +++ b/src/clib/cool1d_multi_g.F @@ -350,12 +350,6 @@ subroutine cool1d_multi_g( & , gasgr_tSiO2D(in), gasgr_tMgO(in), gasgr_tFeS(in) & , gasgr_tAl2O3(in) & , gasgr_treforg(in), gasgr_tvolorg(in), gasgr_tH2Oice(in) - real*8 mygisrf(in), fv2k, fac - real*8 gisrfSiM(in), gisrfFeM(in), gisrfMg2SiO4(in) - & , gisrfMgSiO3(in), gisrfFe3O4(in), gisrfAC(in) - & , gisrfSiO2D(in), gisrfMgO(in), gisrfFeS(in) - & , gisrfAl2O3(in) - & , gisrfreforg(in), gisrfvolorg(in), gisrfH2Oice(in) ! Iteration mask MASK_TYPE itmask(in), anydust, interp @@ -1187,11 +1181,7 @@ subroutine cool1d_multi_g( & gasgr_tMg2SiO4, gasgr_tMgSiO3, gasgr_tFe3O4, & gasgr_tAC, gasgr_tSiO2D, gasgr_tMgO, gasgr_tFeS, & gasgr_tAl2O3, gasgr_treforg, gasgr_tvolorg, - & gasgr_tH2Oice, mygisrf, fv2k, fac, gisrfSiM, - & gisrfFeM, gisrfMg2SiO4, gisrfMgSiO3, gisrfFe3O4, - & gisrfAC, gisrfSiO2D, gisrfMgO, gisrfFeS, - & gisrfAl2O3, gisrfreforg, gisrfvolorg, - & gisrfH2Oice, anydust + & gasgr_tH2Oice & ) endif From 98a291faeafdd392c2ca232f73d4c52c761ea715 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 16:19:13 -0500 Subject: [PATCH 013/101] remove slightly more arguments. --- src/clib/calc_all_tdust_gasgr_1d_g.F | 6 +----- src/clib/cool1d_multi_g.F | 11 +---------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/src/clib/calc_all_tdust_gasgr_1d_g.F b/src/clib/calc_all_tdust_gasgr_1d_g.F index 60f9ede42..5748627b6 100644 --- a/src/clib/calc_all_tdust_gasgr_1d_g.F +++ b/src/clib/calc_all_tdust_gasgr_1d_g.F @@ -23,11 +23,7 @@ subroutine calc_all_tdust_gasgr_1d_g(in, jn, kn, nratec, & kpH2Oice, kptot, gasSiM, gasFeM, gasMg2SiO4, & gasMgSiO3, gasFe3O4, gasAC, gasSiO2D, gasMgO, & gasFeS, gasAl2O3, gasreforg, gasvolorg, - & gasH2Oice, gasgr_tSiM, gasgr_tFeM, - & gasgr_tMg2SiO4, gasgr_tMgSiO3, gasgr_tFe3O4, - & gasgr_tAC, gasgr_tSiO2D, gasgr_tMgO, gasgr_tFeS, - & gasgr_tAl2O3, gasgr_treforg, gasgr_tvolorg, - & gasgr_tH2Oice + & gasH2Oice & ) ! PURPOSE: diff --git a/src/clib/cool1d_multi_g.F b/src/clib/cool1d_multi_g.F index a9f69d96e..f2dce2f80 100644 --- a/src/clib/cool1d_multi_g.F +++ b/src/clib/cool1d_multi_g.F @@ -345,11 +345,6 @@ subroutine cool1d_multi_g( & , gasSiO2D(in), gasMgO(in), gasFeS(in) & , gasAl2O3(in) & , gasreforg(in), gasvolorg(in), gasH2Oice(in) - real*8 gasgr_tSiM(in), gasgr_tFeM(in), gasgr_tMg2SiO4(in) - & , gasgr_tMgSiO3(in), gasgr_tFe3O4(in), gasgr_tAC(in) - & , gasgr_tSiO2D(in), gasgr_tMgO(in), gasgr_tFeS(in) - & , gasgr_tAl2O3(in) - & , gasgr_treforg(in), gasgr_tvolorg(in), gasgr_tH2Oice(in) ! Iteration mask MASK_TYPE itmask(in), anydust, interp @@ -1177,11 +1172,7 @@ subroutine cool1d_multi_g( & kpH2Oice, kptot, gasSiM, gasFeM, gasMg2SiO4, & gasMgSiO3, gasFe3O4, gasAC, gasSiO2D, gasMgO, & gasFeS, gasAl2O3, gasreforg, gasvolorg, - & gasH2Oice, gasgr_tSiM, gasgr_tFeM, - & gasgr_tMg2SiO4, gasgr_tMgSiO3, gasgr_tFe3O4, - & gasgr_tAC, gasgr_tSiO2D, gasgr_tMgO, gasgr_tFeS, - & gasgr_tAl2O3, gasgr_treforg, gasgr_tvolorg, - & gasgr_tH2Oice + & gasH2Oice & ) endif From c220239310c7c40ec20c768f98bcbf98f54ea3f5 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 16:24:57 -0500 Subject: [PATCH 014/101] remove even more args. --- src/clib/calc_all_tdust_gasgr_1d_g.F | 9 --------- src/clib/cool1d_multi_g.F | 2 -- 2 files changed, 11 deletions(-) diff --git a/src/clib/calc_all_tdust_gasgr_1d_g.F b/src/clib/calc_all_tdust_gasgr_1d_g.F index 5748627b6..7bfbee5ed 100644 --- a/src/clib/calc_all_tdust_gasgr_1d_g.F +++ b/src/clib/calc_all_tdust_gasgr_1d_g.F @@ -2,8 +2,6 @@ subroutine calc_all_tdust_gasgr_1d_g(in, jn, kn, nratec, & idustfield, is, ie, j, k, fgr, gamma_isrfa, & trad, gasgra, indixe, tdef, tgas, tdust, & metallicity, dust2gas, mynh, gasgr_tdust, - & priGridRank, priGridDim, priDataSize, - & metGridRank, metGridDim, metDataSize, & itmask_metal, LH2_N, LH2_Size, LHD_N, LHD_Size, & LCI_N, LCI_Size, LCII_N, LCII_Size, LOI_N, & LOI_Size, LCO_N, LCO_Size, LOH_N, LOH_Size, @@ -57,16 +55,9 @@ subroutine calc_all_tdust_gasgr_1d_g(in, jn, kn, nratec, real*8 fgr real*8 gasgra(nratec), gamma_isrfa -! Cloudy cooling data - - integer*8 priGridRank, priDataSize, - & metGridRank, metDataSize, - & priGridDim(priGridRank), metGridDim(metGridRank) - ! Parameters real*8, parameter :: mh = mass_h !DPC - ! approx. mean molecular weight of metals ! Locals diff --git a/src/clib/cool1d_multi_g.F b/src/clib/cool1d_multi_g.F index f2dce2f80..dbc0e5748 100644 --- a/src/clib/cool1d_multi_g.F +++ b/src/clib/cool1d_multi_g.F @@ -1151,8 +1151,6 @@ subroutine cool1d_multi_g( & idustfield, is, ie, j, k, fgr, gamma_isrfa, & comp2, gasgra, indixe, tdef, tgas, tdust, & metallicity, dust2gas, mynh, gasgr_tdust, - & priGridRank, priGridDim, priDataSize, - & metGridRank, metGridDim, metDataSize, & itmask_metal, LH2_N, LH2_Size, LHD_N, LHD_Size, & LCI_N, LCI_Size, LCII_N, LCII_Size, LOI_N, & LOI_Size, LCO_N, LCO_Size, LOH_N, LOH_Size, From 8fff8df0fcda087e1c83423d07e960dd64b2b055 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 16:26:32 -0500 Subject: [PATCH 015/101] even more tweaks. --- src/clib/calc_all_tdust_gasgr_1d_g.F | 17 ++--------------- src/clib/cool1d_multi_g.F | 7 ++----- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/src/clib/calc_all_tdust_gasgr_1d_g.F b/src/clib/calc_all_tdust_gasgr_1d_g.F index 7bfbee5ed..657eb8a84 100644 --- a/src/clib/calc_all_tdust_gasgr_1d_g.F +++ b/src/clib/calc_all_tdust_gasgr_1d_g.F @@ -2,11 +2,8 @@ subroutine calc_all_tdust_gasgr_1d_g(in, jn, kn, nratec, & idustfield, is, ie, j, k, fgr, gamma_isrfa, & trad, gasgra, indixe, tdef, tgas, tdust, & metallicity, dust2gas, mynh, gasgr_tdust, - & itmask_metal, LH2_N, LH2_Size, LHD_N, LHD_Size, - & LCI_N, LCI_Size, LCII_N, LCII_Size, LOI_N, - & LOI_Size, LCO_N, LCO_Size, LOH_N, LOH_Size, - & LH2O_N, LH2O_Size, alphap_N, alphap_Size, - & idspecies, itdmulti, SN0_N, gr_N, gr_Size, gr_dT, + & itmask_metal, + & idspecies, itdmulti, gr_N, gr_Size, gr_dT, & gr_Td, tSiM, tFeM, tMg2SiO4, tMgSiO3, tFe3O4, & tAC, tSiO2D, tMgO, tFeS, tAl2O3, treforg, & tvolorg, tH2Oice, gasgr2a, gamma_isrf2a, @@ -73,17 +70,7 @@ subroutine calc_all_tdust_gasgr_1d_g(in, jn, kn, nratec, ! Cooling/heating slice locals real*8 gasgr(in), gasgr_tdust(in), myisrf(in) - integer*8 LH2_N(3), LH2_Size - integer*8 LHD_N(3), LHD_Size - integer*8 LCI_N(3), LCI_Size - integer*8 LCII_N(3), LCII_Size - integer*8 LOI_N(3), LOI_Size - integer*8 LCO_N(3), LCO_Size - integer*8 LOH_N(3), LOH_Size - integer*8 LH2O_N(3), LH2O_Size - integer*8 alphap_N(2), alphap_Size integer idspecies, itdmulti - integer SN0_N ! opacity table integer gr_N(2), gr_Size real*8 gr_dT, gr_Td(gr_N(2)) diff --git a/src/clib/cool1d_multi_g.F b/src/clib/cool1d_multi_g.F index dbc0e5748..fa87e8e71 100644 --- a/src/clib/cool1d_multi_g.F +++ b/src/clib/cool1d_multi_g.F @@ -1151,11 +1151,8 @@ subroutine cool1d_multi_g( & idustfield, is, ie, j, k, fgr, gamma_isrfa, & comp2, gasgra, indixe, tdef, tgas, tdust, & metallicity, dust2gas, mynh, gasgr_tdust, - & itmask_metal, LH2_N, LH2_Size, LHD_N, LHD_Size, - & LCI_N, LCI_Size, LCII_N, LCII_Size, LOI_N, - & LOI_Size, LCO_N, LCO_Size, LOH_N, LOH_Size, - & LH2O_N, LH2O_Size, alphap_N, alphap_Size, - & idspecies, itdmulti, SN0_N, gr_N, gr_Size, gr_dT, + & itmask_metal, + & idspecies, itdmulti, gr_N, gr_Size, gr_dT, & gr_Td, tSiM, tFeM, tMg2SiO4, tMgSiO3, tFe3O4, & tAC, tSiO2D, tMgO, tFeS, tAl2O3, treforg, & tvolorg, tH2Oice, gasgr2a, gamma_isrf2a, From b0f75ab9978285704df74eb50fec7ab5ae98f602 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 16:34:51 -0500 Subject: [PATCH 016/101] calc_all_tdust_gasgr_1d_g: rename mynh -> nh --- src/clib/calc_all_tdust_gasgr_1d_g.F | 32 ++++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/clib/calc_all_tdust_gasgr_1d_g.F b/src/clib/calc_all_tdust_gasgr_1d_g.F index 657eb8a84..7e5c10c75 100644 --- a/src/clib/calc_all_tdust_gasgr_1d_g.F +++ b/src/clib/calc_all_tdust_gasgr_1d_g.F @@ -1,7 +1,7 @@ subroutine calc_all_tdust_gasgr_1d_g(in, jn, kn, nratec, & idustfield, is, ie, j, k, fgr, gamma_isrfa, & trad, gasgra, indixe, tdef, tgas, tdust, - & metallicity, dust2gas, mynh, gasgr_tdust, + & metallicity, dust2gas, nh, gasgr_tdust, & itmask_metal, & idspecies, itdmulti, gr_N, gr_Size, gr_dT, & gr_Td, tSiM, tFeM, tMg2SiO4, tMgSiO3, tFe3O4, @@ -64,7 +64,7 @@ subroutine calc_all_tdust_gasgr_1d_g(in, jn, kn, nratec, ! Slice locals integer*8 indixe(in) - real*8 tdef(in), tgas(in), tdust(in), mynh(in), metallicity(in), + real*8 tdef(in), tgas(in), tdust(in), nh(in), metallicity(in), & dust2gas(in) ! Cooling/heating slice locals @@ -259,20 +259,20 @@ subroutine calc_all_tdust_gasgr_1d_g(in, jn, kn, nratec, if (itdmulti .eq. 0) then - call calc_tdust_1d_g(tdust, tgas, mynh, gasgr_tdust, + call calc_tdust_1d_g(tdust, tgas, nh, gasgr_tdust, & mygisrf, myisrf, itmask_metal, trad, in, is, ie, j, k & , gr_N, gr_Size, gr_dT, gr_Td, altot, kptot, idspecies) else if (idspecies .gt. 0) then - call calc_tdust_1d_g(tMgSiO3 , tgas, mynh, gasgr_tMgSiO3 , + call calc_tdust_1d_g(tMgSiO3 , tgas, nh, gasgr_tMgSiO3 , & gisrfMgSiO3 , myisrf, itmask_metal, trad, & in, is, ie, j, k & , gr_N, gr_Size, gr_dT, gr_Td, alMgSiO3 , kpMgSiO3 & , idspecies) - call calc_tdust_1d_g(tAC , tgas, mynh, gasgr_tAC , + call calc_tdust_1d_g(tAC , tgas, nh, gasgr_tAC , & gisrfAC , myisrf, itmask_metal, trad, & in, is, ie, j, k & , gr_N, gr_Size, gr_dT, gr_Td, alAC , kpAC @@ -280,49 +280,49 @@ subroutine calc_all_tdust_gasgr_1d_g(in, jn, kn, nratec, endif if (idspecies .gt. 1) then - call calc_tdust_1d_g(tSiM , tgas, mynh, gasgr_tSiM , + call calc_tdust_1d_g(tSiM , tgas, nh, gasgr_tSiM , & gisrfSiM , myisrf, itmask_metal, trad, & in, is, ie, j, k & , gr_N, gr_Size, gr_dT, gr_Td, alSiM , kpSiM & , idspecies) - call calc_tdust_1d_g(tFeM , tgas, mynh, gasgr_tFeM , + call calc_tdust_1d_g(tFeM , tgas, nh, gasgr_tFeM , & gisrfFeM , myisrf, itmask_metal, trad, & in, is, ie, j, k & , gr_N, gr_Size, gr_dT, gr_Td, alFeM , kpFeM & , idspecies) - call calc_tdust_1d_g(tMg2SiO4 , tgas, mynh, gasgr_tMg2SiO4 , + call calc_tdust_1d_g(tMg2SiO4 , tgas, nh, gasgr_tMg2SiO4 , & gisrfMg2SiO4 , myisrf, itmask_metal, trad, & in, is, ie, j, k & , gr_N, gr_Size, gr_dT, gr_Td, alMg2SiO4 , kpMg2SiO4 & , idspecies) - call calc_tdust_1d_g(tFe3O4 , tgas, mynh, gasgr_tFe3O4 , + call calc_tdust_1d_g(tFe3O4 , tgas, nh, gasgr_tFe3O4 , & gisrfFe3O4 , myisrf, itmask_metal, trad, & in, is, ie, j, k & , gr_N, gr_Size, gr_dT, gr_Td, alFe3O4 , kpFe3O4 & , idspecies) - call calc_tdust_1d_g(tSiO2D , tgas, mynh, gasgr_tSiO2D , + call calc_tdust_1d_g(tSiO2D , tgas, nh, gasgr_tSiO2D , & gisrfSiO2D , myisrf, itmask_metal, trad, & in, is, ie, j, k & , gr_N, gr_Size, gr_dT, gr_Td, alSiO2D , kpSiO2D & , idspecies) - call calc_tdust_1d_g(tMgO , tgas, mynh, gasgr_tMgO , + call calc_tdust_1d_g(tMgO , tgas, nh, gasgr_tMgO , & gisrfMgO , myisrf, itmask_metal, trad, & in, is, ie, j, k & , gr_N, gr_Size, gr_dT, gr_Td, alMgO , kpMgO & , idspecies) - call calc_tdust_1d_g(tFeS , tgas, mynh, gasgr_tFeS , + call calc_tdust_1d_g(tFeS , tgas, nh, gasgr_tFeS , & gisrfFeS , myisrf, itmask_metal, trad, & in, is, ie, j, k & , gr_N, gr_Size, gr_dT, gr_Td, alFeS , kpFeS & , idspecies) - call calc_tdust_1d_g(tAl2O3 , tgas, mynh, gasgr_tAl2O3 , + call calc_tdust_1d_g(tAl2O3 , tgas, nh, gasgr_tAl2O3 , & gisrfAl2O3 , myisrf, itmask_metal, trad, & in, is, ie, j, k & , gr_N, gr_Size, gr_dT, gr_Td, alAl2O3 , kpAl2O3 @@ -330,19 +330,19 @@ subroutine calc_all_tdust_gasgr_1d_g(in, jn, kn, nratec, endif if (idspecies .gt. 2) then - call calc_tdust_1d_g(treforg , tgas, mynh, gasgr_treforg , + call calc_tdust_1d_g(treforg , tgas, nh, gasgr_treforg , & gisrfreforg , myisrf, itmask_metal, trad, & in, is, ie, j, k & , gr_N, gr_Size, gr_dT, gr_Td, alreforg , kpreforg & , idspecies) - call calc_tdust_1d_g(tvolorg , tgas, mynh, gasgr_tvolorg , + call calc_tdust_1d_g(tvolorg , tgas, nh, gasgr_tvolorg , & gisrfvolorg , myisrf, itmask_metal, trad, & in, is, ie, j, k & , gr_N, gr_Size, gr_dT, gr_Td, alvolorg , kpvolorg & , idspecies) - call calc_tdust_1d_g(tH2Oice , tgas, mynh, gasgr_tH2Oice , + call calc_tdust_1d_g(tH2Oice , tgas, nh, gasgr_tH2Oice , & gisrfH2Oice , myisrf, itmask_metal, trad, & in, is, ie, j, k & , gr_N, gr_Size, gr_dT, gr_Td, alH2Oice , kpH2Oice From 6a7ee60e8fc1b4d6bd59d6af4f540db964036242 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 17:30:34 -0500 Subject: [PATCH 017/101] add declaration of calc_all_tdust_gasgr_1d_g to fortran_func_decls.h --- src/clib/fortran_func_decls.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/clib/fortran_func_decls.h b/src/clib/fortran_func_decls.h index d9782ac0f..05c32cfc6 100644 --- a/src/clib/fortran_func_decls.h +++ b/src/clib/fortran_func_decls.h @@ -18,6 +18,34 @@ typedef int32_t gr_mask_type; #define MASK_TRUE 1 #define MASK_FALSE 0 +void FORTRAN_NAME(calc_all_tdust_gasgr_1d_g)( + int* in, int* jn, int* kn, int* nratec, int* idustfield, int* is, int* ie, + int* j, int* k, double* fgr, double* gamma_isrfa, double* trad, + double* gasgra, long long* indixe, double* tdef, double* tgas, double* tdust, + double* metallicity, double* dust2gas, double* nh, double* gasgr_tdust, + gr_mask_type* itmask_metal, int* idspecies, int* itdmulti, int* gr_N, + int* gr_Size, double* gr_dT, double* gr_Td, double* tSiM, double* tFeM, + double* tMg2SiO4, double* tMgSiO3, double* tFe3O4, double* tAC, + double* tSiO2D, double* tMgO, double* tFeS, double* tAl2O3, double* treforg, + double* tvolorg, double* tH2Oice, double* gasgr2a, double* gamma_isrf2a, + double* coolunit, double* gasgr, double* myisrf, double* sgSiM, double* sgFeM, + double* sgMg2SiO4, double* sgMgSiO3, double* sgFe3O4, double* sgAC, + double* sgSiO2D, double* sgMgO, double* sgFeS, double* sgAl2O3, + double* sgreforg, double* sgvolorg, double* sgH2Oice, double* sgtot, + double* alSiM_data_ptr, double* alFeM_data_ptr, double* alMg2SiO4_data_ptr, + double* alMgSiO3_data_ptr, double* alFe3O4_data_ptr, double* alAC_data_ptr, + double* alSiO2D_data_ptr, double* alMgO_data_ptr, double* alFeS_data_ptr, + double* alAl2O3_data_ptr, double* alreforg_data_ptr, + double* alvolorg_data_ptr, double* alH2Oice_data_ptr, double* altot_data_ptr, + double* kpSiM, double* kpFeM, double* kpMg2SiO4, double* kpMgSiO3, + double* kpFe3O4, double* kpAC, double* kpSiO2D, double* kpMgO, double* kpFeS, + double* kpAl2O3, double* kpreforg, double* kpvolorg, double* kpH2Oice, + double* kptot, double* gasSiM, double* gasFeM, double* gasMg2SiO4, + double* gasMgSiO3, double* gasFe3O4, double* gasAC, double* gasSiO2D, + double* gasMgO, double* gasFeS, double* gasAl2O3, double* gasreforg, + double* gasvolorg, double* gasH2Oice +); + void FORTRAN_NAME(calc_grain_size_increment_1d)( int* immulti, int* imabund, int* idspecies, int* igrgr, gr_mask_type* itmask, int* in, int* jn, int* kn, int* is, int* ie, int* j, int* k, double* dom, From 67514017d9a36ad6b31a86e6a2eccbfe983a1164 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 5 Jan 2025 17:20:13 -0500 Subject: [PATCH 018/101] calc_tdust_3d_g: start using calc_grain_size_increment_1d --- src/clib/calc_tdust_3d_g.F | 231 ++++--------------------------------- 1 file changed, 23 insertions(+), 208 deletions(-) diff --git a/src/clib/calc_tdust_3d_g.F b/src/clib/calc_tdust_3d_g.F index ac360c180..a067ea054 100644 --- a/src/clib/calc_tdust_3d_g.F +++ b/src/clib/calc_tdust_3d_g.F @@ -193,7 +193,7 @@ subroutine calc_tdust_3d_g( integer*8 indixe(in) real*8 t1(in), t2(in), logtem(in), tdef(in), - & tgas(in), tdust(in), nh(in), gasgr(in), + & tgas(in), tdust(in), nh(in), gasgr(in), gasgr_tdust(in), & myisrf(in) ! Iteration mask for multi_cool @@ -411,51 +411,6 @@ subroutine calc_tdust_3d_g( myisrf(i) = isrf endif - if (idspecies .eq. 0 ) then - if (idustfield .gt. 0) then - mygisrf(i) = gamma_isrfa - & * fgr / dust2gas(i) * metallicity(i) - !! correct with the depletion or enhancement of condensation rate. -!! write(*,*) 'a', mygisrf(i) - else - mygisrf(i) = gamma_isrfa - endif - - else ! idspecies - - if (itdmulti .eq. 0) then - - mygisrf(i) = gamma_isrf2a * sgtot(i) - !! in UV, absorption coefficient Q ~ 1 (Goldsmith 2001) - !! so we use the geometrical cross-section of grains [cgs] -!! write(*,*) 'b', mygisrf(i) / dust2gas(i) - - else - - if (idspecies .gt. 0) then - gisrfMgSiO3 (i) = gamma_isrf2a * sgMgSiO3 (i) -!! write(*,*) d(i,j,k), gamma_isrf2a, sgMgSiO3(i) - gisrfAC (i) = gamma_isrf2a * sgAC (i) - endif - if (idspecies .gt. 1) then - gisrfSiM (i) = gamma_isrf2a * sgSiM (i) - gisrfFeM (i) = gamma_isrf2a * sgFeM (i) - gisrfMg2SiO4 (i) = gamma_isrf2a * sgMg2SiO4 (i) - gisrfFe3O4 (i) = gamma_isrf2a * sgFe3O4 (i) - gisrfSiO2D (i) = gamma_isrf2a * sgSiO2D (i) - gisrfMgO (i) = gamma_isrf2a * sgMgO (i) - gisrfFeS (i) = gamma_isrf2a * sgFeS (i) - gisrfAl2O3 (i) = gamma_isrf2a * sgAl2O3 (i) - endif - if (idspecies .gt. 2) then - gisrfreforg (i) = gamma_isrf2a * sgreforg (i) - gisrfvolorg (i) = gamma_isrf2a * sgvolorg (i) - gisrfH2Oice (i) = gamma_isrf2a * sgH2Oice (i) - endif - - endif - - endif ! Compute hydrogen number density @@ -483,173 +438,33 @@ subroutine calc_tdust_3d_g( t2(i) = (logtem0 + (indixe(i) )*dlogtem) tdef(i) = (logtem(i) - t1(i)) / (t2(i) - t1(i)) -! Lookup values and do a linear temperature in log(T) -! Convert back to cgs - - if(idspecies .eq. 0) then - - gasgr(i) = gasgra(indixe(i)) + tdef(i) - & *(gasgra(indixe(i)+1) -gasgra(indixe(i))) - -!! gasgr(i) = fgr * gasgr(i) * coolunit / mh - gasgr(i) = (dust2gas(i) / metallicity(i)) - & * gasgr(i) * coolunit / mh - !! apply to (idustfield .eq. 1) GC20200701 -!! write(*,*) 'a', gasgr(i) - - else - - fv2k = gasgr2a(indixe(i)) + tdef(i) - & *(gasgr2a(indixe(i)+1) -gasgr2a(indixe(i))) - - fac = coolunit / mh - - if ( itdmulti .eq. 0 ) then - - gasgr(i) = fv2k * sgtot(i) * fac -!! write(*,*) 'b', gasgr(i) / metallicity(i) - - else - - if (idspecies .gt. 0) then - gasMgSiO3 (i) = fv2k * sgMgSiO3 (i) * fac -!! write(*,*) fv2k, sgMgSiO3(i), fac - gasAC (i) = fv2k * sgAC (i) * fac - endif - if (idspecies .gt. 1) then - gasSiM (i) = fv2k * sgSiM (i) * fac - gasFeM (i) = fv2k * sgFeM (i) * fac - gasMg2SiO4 (i) = fv2k * sgMg2SiO4 (i) * fac - gasFe3O4 (i) = fv2k * sgFe3O4 (i) * fac - gasSiO2D (i) = fv2k * sgSiO2D (i) * fac - gasMgO (i) = fv2k * sgMgO (i) * fac - gasFeS (i) = fv2k * sgFeS (i) * fac - gasAl2O3 (i) = fv2k * sgAl2O3 (i) * fac - endif - if (idspecies .gt. 2) then - gasreforg (i) = fv2k * sgreforg (i) * fac - gasvolorg (i) = fv2k * sgvolorg (i) * fac - gasH2Oice (i) = fv2k * sgH2Oice (i) * fac - endif - - endif - - endif - endif ! itmask enddo - -! if (idspecies .eq. 0) then -! do itd = 1, gr_N(2) -! do i = is+1, ie + 1 -! if(itmask(i)) then -! altot(itd,i)= -! & (SN0_kpFeM (4*itd,1)*SN0_fFeM (1) -! & /SN0_r0FeM (3 ,1) -! & +SN0_kpMg2SiO4(4*itd,1)*SN0_fMg2SiO4(1) -! & /SN0_r0Mg2SiO4(3 ,1) -! & +SN0_kpMgSiO3 (4*itd,1)*SN0_fMgSiO3 (1) -! & /SN0_r0MgSiO3 (3 ,1) -! & +SN0_kpFeS (4*itd,1)*SN0_fFeS (1) -! & /SN0_r0FeS (3 ,1) -! & +SN0_kpreforg (4*itd,1)*SN0_freforg (1) -! & /SN0_r0reforg (3 ,1) -! & +SN0_kpvolorg (4*itd,1)*SN0_fvolorg (1) -! & /SN0_r0volorg (3 ,1) -! & +SN0_kpH2Oice (4*itd,1)*SN0_fH2Oice (1) -! & /SN0_r0H2Oice (3 ,1)) -! & * metal(i,j,k) / d(i,j,k) -! endif -! enddo -! enddo -! endif - ! --- Compute dust temperature in a slice --- - if (itdmulti .eq. 0) then - - call calc_tdust_1d_g(tdust, tgas, nh, gasgr, - & mygisrf, myisrf, itmask, trad, in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, altot, kptot, idspecies) - -!! write(*,*) tdust - - else - - if (idspecies .gt. 0) then - call calc_tdust_1d_g(tMgSiO3 , tgas, nh, gasMgSiO3 , - & gisrfMgSiO3 , myisrf, itmask, trad, in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alMgSiO3 , kpMgSiO3 - & , idspecies) -!! write(*,*) 'bb2', gasMgSiO3, gisrfMgSiO3, myisrf, tMgSiO3 - - call calc_tdust_1d_g(tAC , tgas, nh, gasAC , - & gisrfAC , myisrf, itmask, trad, in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alAC , kpAC - & , idspecies) -!! write(*,*) 'bb2', gasAC , gisrfAC , myisrf, tAC - endif - - if (idspecies .gt. 1) then - call calc_tdust_1d_g(tSiM , tgas, nh, gasSiM , - & gisrfSiM , myisrf, itmask, trad, in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alSiM , kpSiM - & , idspecies) - - call calc_tdust_1d_g(tFeM , tgas, nh, gasFeM , - & gisrfFeM , myisrf, itmask, trad, in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alFeM , kpFeM - & , idspecies) - - call calc_tdust_1d_g(tMg2SiO4 , tgas, nh, gasMg2SiO4 , - & gisrfMg2SiO4 , myisrf, itmask, trad, in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alMg2SiO4 , kpMg2SiO4 - & , idspecies) - - call calc_tdust_1d_g(tFe3O4 , tgas, nh, gasFe3O4 , - & gisrfFe3O4 , myisrf, itmask, trad, in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alFe3O4 , kpFe3O4 - & , idspecies) - - call calc_tdust_1d_g(tSiO2D , tgas, nh, gasSiO2D , - & gisrfSiO2D , myisrf, itmask, trad, in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alSiO2D , kpSiO2D - & , idspecies) - - call calc_tdust_1d_g(tMgO , tgas, nh, gasMgO , - & gisrfMgO , myisrf, itmask, trad, in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alMgO , kpMgO - & , idspecies) - - call calc_tdust_1d_g(tFeS , tgas, nh, gasFeS , - & gisrfFeS , myisrf, itmask, trad, in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alFeS , kpFeS - & , idspecies) - - call calc_tdust_1d_g(tAl2O3 , tgas, nh, gasAl2O3 , - & gisrfAl2O3 , myisrf, itmask, trad, in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alAl2O3 , kpAl2O3 - & , idspecies) - endif - - if (idspecies .gt. 2) then - call calc_tdust_1d_g(treforg , tgas, nh, gasreforg , - & gisrfreforg , myisrf, itmask, trad, in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alreforg , kpreforg - & , idspecies) - - call calc_tdust_1d_g(tvolorg , tgas, nh, gasvolorg , - & gisrfvolorg , myisrf, itmask, trad, in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alvolorg , kpvolorg - & , idspecies) - - call calc_tdust_1d_g(tH2Oice , tgas, nh, gasH2Oice , - & gisrfH2Oice , myisrf, itmask, trad, in, is, ie, j, k - & , gr_N, gr_Size, gr_dT, gr_Td, alH2Oice , kpH2Oice - & , idspecies) - endif + call calc_all_tdust_gasgr_1d_g(in, jn, kn, nratec, + & idustfield, is, ie, j, k, fgr, gamma_isrfa, + & trad, gasgra, indixe, tdef, tgas, tdust, + & metallicity, dust2gas, nh, gasgr_tdust, + & itmask, + & idspecies, itdmulti, gr_N, gr_Size, gr_dT, + & gr_Td, tSiM, tFeM, tMg2SiO4, tMgSiO3, tFe3O4, + & tAC, tSiO2D, tMgO, tFeS, tAl2O3, treforg, + & tvolorg, tH2Oice, gasgr2a, gamma_isrf2a, + & coolunit, gasgr, myisrf, sgSiM, sgFeM, sgMg2SiO4, + & sgMgSiO3, sgFe3O4, sgAC, sgSiO2D, sgMgO, sgFeS, + & sgAl2O3, sgreforg, sgvolorg, sgH2Oice, sgtot, + & alSiM, alFeM, alMg2SiO4, alMgSiO3, alFe3O4, alAC, + & alSiO2D, alMgO, alFeS, alAl2O3, alreforg, + & alvolorg, alH2Oice, altot, kpSiM, kpFeM, + & kpMg2SiO4, kpMgSiO3, kpFe3O4, kpAC, kpSiO2D, + & kpMgO, kpFeS, kpAl2O3, kpreforg, kpvolorg, + & kpH2Oice, kptot, gasSiM, gasFeM, gasMg2SiO4, + & gasMgSiO3, gasFe3O4, gasAC, gasSiO2D, gasMgO, + & gasFeS, gasAl2O3, gasreforg, gasvolorg, + & gasH2Oice + & ) - endif ! Copy slice values back to grid From 581c9b9c37815be280cd7b59126d374805158302 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 10 Jan 2025 08:57:46 -0500 Subject: [PATCH 019/101] solve_chemistry.c: include the "grackle.h" header and remove now unnecessary forward-declarations (since they already exist within grackle.h). --- src/clib/solve_chemistry.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/clib/solve_chemistry.c b/src/clib/solve_chemistry.c index 6856d109e..7efbd45b6 100644 --- a/src/clib/solve_chemistry.c +++ b/src/clib/solve_chemistry.c @@ -13,19 +13,13 @@ #include #include +#include "grackle.h" #include "grackle_macros.h" -#include "grackle_types.h" -#include "grackle_chemistry_data.h" #include "phys_constants.h" #include "utils.h" -extern chemistry_data *grackle_data; -extern chemistry_data_storage grackle_rates; - /* function prototypes */ -double get_temperature_units(code_units *my_units); - int update_UVbackground_rates(chemistry_data *my_chemistry, chemistry_data_storage *my_rates, photo_rate_storage *my_uvb_rates, From 57194839dc4e091fc29127888d02c2d7f1e7a828 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 10 Jan 2025 09:13:58 -0500 Subject: [PATCH 020/101] initial port of the solve_rate_cool-g function from Fortran to C++ --- src/clib/CMakeLists.txt | 1 + src/clib/Make.config.objects | 1 + src/clib/fortran_func_decls.h | 127 --- src/clib/solve_chemistry.c | 476 +--------- src/clib/solve_rate_cool_g-cpp.C | 1454 ++++++++++++++++++++++++++++ src/clib/solve_rate_cool_g-cpp.h | 31 + src/clib/solve_rate_cool_g.F | 1519 +----------------------------- 7 files changed, 1497 insertions(+), 2112 deletions(-) create mode 100644 src/clib/solve_rate_cool_g-cpp.C create mode 100644 src/clib/solve_rate_cool_g-cpp.h diff --git a/src/clib/CMakeLists.txt b/src/clib/CMakeLists.txt index 25c507046..1576f9b68 100644 --- a/src/clib/CMakeLists.txt +++ b/src/clib/CMakeLists.txt @@ -107,6 +107,7 @@ add_library(Grackle_Grackle # C++ Source (and Private Header Files) cool_multi_time_g-cpp.C cool_multi_time_g-cpp.h internal_types.C internal_types.hpp + solve_rate_cool_g-cpp.C solve_rate_cool_g-cpp.h utils-cpp.C utils-cpp.hpp # Fortran Source Files diff --git a/src/clib/Make.config.objects b/src/clib/Make.config.objects index 049e875e9..8eba32d15 100644 --- a/src/clib/Make.config.objects +++ b/src/clib/Make.config.objects @@ -38,6 +38,7 @@ OBJS_CONFIG_LIB = \ set_default_chemistry_parameters.lo \ solve_chemistry.lo \ solve_rate_cool_g.lo \ + solve_rate_cool_g-cpp.lo \ update_UVbackground_rates.lo \ lookup_cool_rates0d.lo \ calc_all_tdust_gasgr_1d_g.lo \ diff --git a/src/clib/fortran_func_decls.h b/src/clib/fortran_func_decls.h index 05c32cfc6..a2a000d28 100644 --- a/src/clib/fortran_func_decls.h +++ b/src/clib/fortran_func_decls.h @@ -516,133 +516,6 @@ void FORTRAN_NAME(scale_fields_g)( gr_float* metal_Y19_data_ptr ); -void FORTRAN_NAME(solve_rate_cool_g)( - int* icool, gr_float* d_data_ptr, gr_float* e_data_ptr, gr_float* u_data_ptr, - gr_float* v_data_ptr, gr_float* w_data_ptr, gr_float* de_data_ptr, - gr_float* HI_data_ptr, gr_float* HII_data_ptr, gr_float* HeI_data_ptr, - gr_float* HeII_data_ptr, gr_float* HeIII_data_ptr, int* in, int* jn, int* kn, - int* nratec, int* iexpand, int* ispecies, int* imetal, int* imcool, - int* idust, int* idustall, int* idustfield, int* idim, int* is, int* js, - int* ks, int* ie, int* je, int* ke, int* ih2co, int* ipiht, int* idustrec, - int* igammah, double* dx, double* dt, double* aye, double* temstart, - double* temend, double* utem, double* uxyz, double* uaye, double* urho, - double* utim, double* gamma, double* fh, double* dtoh, double* z_solar, - double* fgr, double* k1a, double* k2a, double* k3a, double* k4a, double* k5a, - double* k6a, double* k7a, double* k8a, double* k9a, double* k10a, - double* k11a, double* k12a, double* k13a, double* k13dda_data_ptr, - double* k14a, double* k15a, double* k16a, double* k17a, double* k18a, - double* k19a, double* k22a, double* k24, double* k25, double* k26, - double* k27, double* k28, double* k29, double* k30, double* k31, double* k50a, - double* k51a, double* k52a, double* k53a, double* k54a, double* k55a, - double* k56a, double* k57a, double* k58a, int* ndratec, double* dtemstart, - double* dtemend, double* h2dusta_data_ptr, double* ncrna, double* ncrd1a, - double* ncrd2a, double* ceHIa, double* ceHeIa, double* ceHeIIa, double* ciHIa, - double* ciHeIa, double* ciHeISa, double* ciHeIIa, double* reHIIa, - double* reHeII1a, double* reHeII2a, double* reHeIIIa, double* brema, - double* compa, double* gammaha, double* isrf, double* regra, - double* gamma_isrfa, double* comp_xraya, double* comp_temp, double* piHI, - double* piHeI, double* piHeII, gr_float* HM_data_ptr, gr_float* H2I_data_ptr, - gr_float* H2II_data_ptr, gr_float* DI_data_ptr, gr_float* DII_data_ptr, - gr_float* HDI_data_ptr, gr_float* metal_data_ptr, gr_float* dust_data_ptr, - double* hyd01ka, double* h2k01a, double* vibha, double* rotha, double* rotla, - double* gpldla, double* gphdla, double* hdltea, double* hdlowa, double* gaHIa, - double* gaH2a, double* gaHea, double* gaHpa, double* gaela, double* h2ltea, - double* gasgra, int* iH2shield, int* iradshield, double* avgsighi, - double* avgsighei, double* avgsigheii, int* iradtrans, int* iradcoupled, - int* iradstep, int* irt_honly, gr_float* kphHI_data_ptr, - gr_float* kphHeI_data_ptr, gr_float* kphHeII_data_ptr, - gr_float* kdissH2I_data_ptr, gr_float* photogamma_data_ptr, - gr_float* xH2shield_data_ptr, int* ierr, int* ih2optical, int* iciecool, - int* ithreebody, int* ih2cr, int* ihdcr, double* ciecoa, int* icmbTfloor, - int* iClHeat, double* clEleFra, long long* priGridRank, long long* priGridDim, - double* priPar1, double* priPar2, double* priPar3, double* priPar4, - double* priPar5, long long* priDataSize, double* priCooling, - double* priHeating, double* priMMW, long long* metGridRank, - long long* metGridDim, double* metPar1, double* metPar2, double* metPar3, - double* metPar4, double* metPar5, long long* metDataSize, double* metCooling, - double* metHeating, int* clnew, int* iVheat, int* iMheat, - gr_float* Vheat_data_ptr, gr_float* Mheat_data_ptr, int* iTfloor, - double* Tfloor_scalar, gr_float* Tfloor_data_ptr, int* imchem, int* igrgr, - int* ipcont, double* tmcool, gr_float* DM_data_ptr, gr_float* HDII_data_ptr, - gr_float* HeHII_data_ptr, gr_float* CI_data_ptr, gr_float* CII_data_ptr, - gr_float* CO_data_ptr, gr_float* CO2_data_ptr, gr_float* OI_data_ptr, - gr_float* OH_data_ptr, gr_float* H2O_data_ptr, gr_float* O2_data_ptr, - gr_float* SiI_data_ptr, gr_float* SiOI_data_ptr, gr_float* SiO2I_data_ptr, - gr_float* CH_data_ptr, gr_float* CH2_data_ptr, gr_float* COII_data_ptr, - gr_float* OII_data_ptr, gr_float* OHII_data_ptr, gr_float* H2OII_data_ptr, - gr_float* H3OII_data_ptr, gr_float* O2II_data_ptr, gr_float* Mg_data_ptr, - gr_float* Al_data_ptr, gr_float* S_data_ptr, gr_float* Fe_data_ptr, - gr_float* SiM_data_ptr, gr_float* FeM_data_ptr, gr_float* Mg2SiO4_data_ptr, - gr_float* MgSiO3_data_ptr, gr_float* Fe3O4_data_ptr, gr_float* AC_data_ptr, - gr_float* SiO2D_data_ptr, gr_float* MgO_data_ptr, gr_float* FeS_data_ptr, - gr_float* Al2O3_data_ptr, gr_float* reforg_data_ptr, - gr_float* volorg_data_ptr, gr_float* H2Oice_data_ptr, double* k125a, - double* k129a, double* k130a, double* k131a, double* k132a, double* k133a, - double* k134a, double* k135a, double* k136a, double* k137a, double* k148a, - double* k149a, double* k150a, double* k151a, double* k152a, double* k153a, - double* kz15a, double* kz16a, double* kz17a, double* kz18a, double* kz19a, - double* kz20a, double* kz21a, double* kz22a, double* kz23a, double* kz24a, - double* kz25a, double* kz26a, double* kz27a, double* kz28a, double* kz29a, - double* kz30a, double* kz31a, double* kz32a, double* kz33a, double* kz34a, - double* kz35a, double* kz36a, double* kz37a, double* kz38a, double* kz39a, - double* kz40a, double* kz41a, double* kz42a, double* kz43a, double* kz44a, - double* kz45a, double* kz46a, double* kz47a, double* kz48a, double* kz49a, - double* kz50a, double* kz51a, double* kz52a, double* kz53a, double* kz54a, - double* cieY06a, long long* LH2_N, long long* LH2_Size, double* LH2_D, - double* LH2_T, double* LH2_H, double* LH2_dD, double* LH2_dT, double* LH2_dH, - double* LH2_L, long long* LHD_N, long long* LHD_Size, double* LHD_D, - double* LHD_T, double* LHD_H, double* LHD_dD, double* LHD_dT, double* LHD_dH, - double* LHD_L, long long* LCI_N, long long* LCI_Size, double* LCI_D, - double* LCI_T, double* LCI_H, double* LCI_dD, double* LCI_dT, double* LCI_dH, - double* LCI_L, long long* LCII_N, long long* LCII_Size, double* LCII_D, - double* LCII_T, double* LCII_H, double* LCII_dD, double* LCII_dT, - double* LCII_dH, double* LCII_L, long long* LOI_N, long long* LOI_Size, - double* LOI_D, double* LOI_T, double* LOI_H, double* LOI_dD, double* LOI_dT, - double* LOI_dH, double* LOI_L, long long* LCO_N, long long* LCO_Size, - double* LCO_D, double* LCO_T, double* LCO_H, double* LCO_dD, double* LCO_dT, - double* LCO_dH, double* LCO_L, long long* LOH_N, long long* LOH_Size, - double* LOH_D, double* LOH_T, double* LOH_H, double* LOH_dD, double* LOH_dT, - double* LOH_dH, double* LOH_L, long long* LH2O_N, long long* LH2O_Size, - double* LH2O_D, double* LH2O_T, double* LH2O_H, double* LH2O_dD, - double* LH2O_dT, double* LH2O_dH, double* LH2O_L, long long* alphap_N, - long long* alphap_Size, double* alphap_D, double* alphap_T, double* alphap_dD, - double* alphap_dT, double* alphap_Data, int* immulti, int* imabund, - int* idspecies, int* itdmulti, int* idsub, gr_float* metal_loc_data_ptr, - gr_float* metal_C13_data_ptr, gr_float* metal_C20_data_ptr, - gr_float* metal_C25_data_ptr, gr_float* metal_C30_data_ptr, - gr_float* metal_F13_data_ptr, gr_float* metal_F15_data_ptr, - gr_float* metal_F50_data_ptr, gr_float* metal_F80_data_ptr, - gr_float* metal_P170_data_ptr, gr_float* metal_P200_data_ptr, - gr_float* metal_Y19_data_ptr, int* SN0_N, double* SN0_XC, double* SN0_XO, - double* SN0_XMg, double* SN0_XAl, double* SN0_XSi, double* SN0_XS, - double* SN0_XFe, double* SN0_fC, double* SN0_fO, double* SN0_fMg, - double* SN0_fAl, double* SN0_fSi, double* SN0_fS, double* SN0_fFe, - double* SN0_fSiM, double* SN0_fFeM, double* SN0_fMg2SiO4, double* SN0_fMgSiO3, - double* SN0_fFe3O4, double* SN0_fAC, double* SN0_fSiO2D, double* SN0_fMgO, - double* SN0_fFeS, double* SN0_fAl2O3, double* SN0_freforg, - double* SN0_fvolorg, double* SN0_fH2Oice, double* SN0_r0SiM_data_ptr, - double* SN0_r0FeM_data_ptr, double* SN0_r0Mg2SiO4_data_ptr, - double* SN0_r0MgSiO3_data_ptr, double* SN0_r0Fe3O4_data_ptr, - double* SN0_r0AC_data_ptr, double* SN0_r0SiO2D_data_ptr, - double* SN0_r0MgO_data_ptr, double* SN0_r0FeS_data_ptr, - double* SN0_r0Al2O3_data_ptr, double* SN0_r0reforg_data_ptr, - double* SN0_r0volorg_data_ptr, double* SN0_r0H2Oice_data_ptr, int* gr_N, - int* gr_Size, double* gr_dT, double* gr_Td, double* SN0_kpSiM_data_ptr, - double* SN0_kpFeM_data_ptr, double* SN0_kpMg2SiO4_data_ptr, - double* SN0_kpMgSiO3_data_ptr, double* SN0_kpFe3O4_data_ptr, - double* SN0_kpAC_data_ptr, double* SN0_kpSiO2D_data_ptr, - double* SN0_kpMgO_data_ptr, double* SN0_kpFeS_data_ptr, - double* SN0_kpAl2O3_data_ptr, double* SN0_kpreforg_data_ptr, - double* SN0_kpvolorg_data_ptr, double* SN0_kpH2Oice_data_ptr, - double* h2dustSa, double* h2dustCa, double* gasgr2a, double* gamma_isrf2a, - double* grogra, int* idissHDI, gr_float* kdissHDI_data_ptr, int* iionZ, - gr_float* kphCI_data_ptr, gr_float* kphOI_data_ptr, int* idissZ, - gr_float* kdissCO_data_ptr, gr_float* kdissOH_data_ptr, - gr_float* kdissH2O_data_ptr, int* iuseH2shield, int* iisrffield, - gr_float* isrf_habing_data_ptr, int* iH2shieldcustom, - gr_float* f_shield_custom_data_ptr, int* itmax, int* exititmax -); - void FORTRAN_NAME(ceiling_species_g)( gr_float* d_data_ptr, gr_float* de_data_ptr, gr_float* HI_data_ptr, gr_float* HII_data_ptr, gr_float* HeI_data_ptr, gr_float* HeII_data_ptr, diff --git a/src/clib/solve_chemistry.c b/src/clib/solve_chemistry.c index 7efbd45b6..7316a5425 100644 --- a/src/clib/solve_chemistry.c +++ b/src/clib/solve_chemistry.c @@ -16,6 +16,7 @@ #include "grackle.h" #include "grackle_macros.h" #include "phys_constants.h" +#include "solve_rate_cool_g-cpp.h" #include "utils.h" /* function prototypes */ @@ -238,476 +239,11 @@ int local_solve_chemistry(chemistry_data *my_chemistry, int ierr = 0; - FORTRAN_NAME(solve_rate_cool_g)( - &my_chemistry->with_radiative_cooling, - my_fields->density, - my_fields->internal_energy, - my_fields->x_velocity, - my_fields->y_velocity, - my_fields->z_velocity, - my_fields->e_density, - my_fields->HI_density, - my_fields->HII_density, - my_fields->HeI_density, - my_fields->HeII_density, - my_fields->HeIII_density, - my_fields->grid_dimension+0, - my_fields->grid_dimension+1, - my_fields->grid_dimension+2, - &my_chemistry->NumberOfTemperatureBins, - &my_units->comoving_coordinates, - &my_chemistry->primordial_chemistry, - &metal_field_present, - &my_chemistry->metal_cooling, - &my_chemistry->h2_on_dust, - &my_chemistry->dust_chemistry, - &my_chemistry->use_dust_density_field, - &(my_fields->grid_rank), - my_fields->grid_start+0, - my_fields->grid_start+1, - my_fields->grid_start+2, - my_fields->grid_end+0, - my_fields->grid_end+1, - my_fields->grid_end+2, - &my_chemistry->ih2co, - &my_chemistry->ipiht, - &my_chemistry->dust_recombination_cooling, - &my_chemistry->photoelectric_heating, - &(my_fields->grid_dx), - &dt_value, - &my_units->a_value, - &my_chemistry->TemperatureStart, - &my_chemistry->TemperatureEnd, - &temperature_units, - &co_length_units, - &my_units->a_units, - &co_density_units, - &my_units->time_units, - &my_chemistry->Gamma, - &my_chemistry->HydrogenFractionByMass, - &my_chemistry->DeuteriumToHydrogenRatio, - &my_chemistry->SolarMetalFractionByMass, - &my_chemistry->local_dust_to_gas_ratio, - my_rates->k1, - my_rates->k2, - my_rates->k3, - my_rates->k4, - my_rates->k5, - my_rates->k6, - my_rates->k7, - my_rates->k8, - my_rates->k9, - my_rates->k10, - my_rates->k11, - my_rates->k12, - my_rates->k13, - my_rates->k13dd, - my_rates->k14, - my_rates->k15, - my_rates->k16, - my_rates->k17, - my_rates->k18, - my_rates->k19, - my_rates->k22, - &my_uvb_rates.k24, - &my_uvb_rates.k25, - &my_uvb_rates.k26, - &my_uvb_rates.k27, - &my_uvb_rates.k28, - &my_uvb_rates.k29, - &my_uvb_rates.k30, - &my_uvb_rates.k31, - my_rates->k50, - my_rates->k51, - my_rates->k52, - my_rates->k53, - my_rates->k54, - my_rates->k55, - my_rates->k56, - my_rates->k57, - my_rates->k58, - &my_chemistry->NumberOfDustTemperatureBins, - &my_chemistry->DustTemperatureStart, - &my_chemistry->DustTemperatureEnd, - my_rates->h2dust, - my_rates->n_cr_n, - my_rates->n_cr_d1, - my_rates->n_cr_d2, - my_rates->ceHI, - my_rates->ceHeI, - my_rates->ceHeII, - my_rates->ciHI, - my_rates->ciHeI, - my_rates->ciHeIS, - my_rates->ciHeII, - my_rates->reHII, - my_rates->reHeII1, - my_rates->reHeII2, - my_rates->reHeIII, - my_rates->brem, - &my_rates->comp, - &my_rates->gammah, - &my_chemistry->interstellar_radiation_field, - my_rates->regr, - &my_rates->gamma_isrf, - &my_uvb_rates.comp_xray, - &my_uvb_rates.temp_xray, - &my_uvb_rates.piHI, - &my_uvb_rates.piHeI, - &my_uvb_rates.piHeII, - my_fields->HM_density, - my_fields->H2I_density, - my_fields->H2II_density, - my_fields->DI_density, - my_fields->DII_density, - my_fields->HDI_density, - my_fields->metal_density, - my_fields->dust_density, - my_rates->hyd01k, - my_rates->h2k01, - my_rates->vibh, - my_rates->roth, - my_rates->rotl, - my_rates->GP99LowDensityLimit, - my_rates->GP99HighDensityLimit, - my_rates->HDlte, - my_rates->HDlow, - my_rates->GAHI, - my_rates->GAH2, - my_rates->GAHe, - my_rates->GAHp, - my_rates->GAel, - my_rates->H2LTE, - my_rates->gas_grain, - &my_chemistry->H2_self_shielding, - &my_chemistry->self_shielding_method, - &my_uvb_rates.crsHI, - &my_uvb_rates.crsHeI, - &my_uvb_rates.crsHeII, - &my_chemistry->use_radiative_transfer, - &my_chemistry->radiative_transfer_coupled_rate_solver, - &my_chemistry->radiative_transfer_intermediate_step, - &my_chemistry->radiative_transfer_hydrogen_only, - my_fields->RT_HI_ionization_rate, - my_fields->RT_HeI_ionization_rate, - my_fields->RT_HeII_ionization_rate, - my_fields->RT_H2_dissociation_rate, - my_fields->RT_heating_rate, - my_fields-> H2_self_shielding_length, - &ierr, - &my_chemistry->h2_optical_depth_approximation, - &my_chemistry->cie_cooling, - &my_chemistry->three_body_rate, - &my_chemistry->h2_cooling_rate, - &my_chemistry->hd_cooling_rate, - my_rates->cieco, - &my_chemistry->cmb_temperature_floor, - &my_chemistry->UVbackground, - &my_chemistry->cloudy_electron_fraction_factor, - &my_rates->cloudy_primordial.grid_rank, - my_rates->cloudy_primordial.grid_dimension, - my_rates->cloudy_primordial.grid_parameters[0], - my_rates->cloudy_primordial.grid_parameters[1], - my_rates->cloudy_primordial.grid_parameters[2], - my_rates->cloudy_primordial.grid_parameters[3], - my_rates->cloudy_primordial.grid_parameters[4], - &my_rates->cloudy_primordial.data_size, - my_rates->cloudy_primordial.cooling_data, - my_rates->cloudy_primordial.heating_data, - my_rates->cloudy_primordial.mmw_data, - &my_rates->cloudy_metal.grid_rank, - my_rates->cloudy_metal.grid_dimension, - my_rates->cloudy_metal.grid_parameters[0], - my_rates->cloudy_metal.grid_parameters[1], - my_rates->cloudy_metal.grid_parameters[2], - my_rates->cloudy_metal.grid_parameters[3], - my_rates->cloudy_metal.grid_parameters[4], - &my_rates->cloudy_metal.data_size, - my_rates->cloudy_metal.cooling_data, - my_rates->cloudy_metal.heating_data, - &my_rates->cloudy_data_new, - &my_chemistry->use_volumetric_heating_rate, - &my_chemistry->use_specific_heating_rate, - my_fields->volumetric_heating_rate, - my_fields->specific_heating_rate, - &my_chemistry->use_temperature_floor, - &my_chemistry->temperature_floor_scalar, - my_fields->temperature_floor, - &my_chemistry->metal_chemistry, - &my_chemistry->grain_growth, - &my_chemistry->use_primordial_continuum_opacity, - &my_chemistry->tabulated_cooling_minimum_temperature, - my_fields->DM_density, - my_fields->HDII_density, - my_fields->HeHII_density, - my_fields->CI_density, - my_fields->CII_density, - my_fields->CO_density, - my_fields->CO2_density, - my_fields->OI_density, - my_fields->OH_density, - my_fields->H2O_density, - my_fields->O2_density, - my_fields->SiI_density, - my_fields->SiOI_density, - my_fields->SiO2I_density, - my_fields->CH_density, - my_fields->CH2_density, - my_fields->COII_density, - my_fields->OII_density, - my_fields->OHII_density, - my_fields->H2OII_density, - my_fields->H3OII_density, - my_fields->O2II_density, - my_fields->Mg_density, - my_fields->Al_density, - my_fields->S_density, - my_fields->Fe_density, - my_fields->SiM_dust_density, - my_fields->FeM_dust_density, - my_fields->Mg2SiO4_dust_density, - my_fields->MgSiO3_dust_density, - my_fields->Fe3O4_dust_density, - my_fields->AC_dust_density, - my_fields->SiO2_dust_density, - my_fields->MgO_dust_density, - my_fields->FeS_dust_density, - my_fields->Al2O3_dust_density, - my_fields->ref_org_dust_density, - my_fields->vol_org_dust_density, - my_fields->H2O_ice_dust_density, - my_rates->k125, - my_rates->k129, - my_rates->k130, - my_rates->k131, - my_rates->k132, - my_rates->k133, - my_rates->k134, - my_rates->k135, - my_rates->k136, - my_rates->k137, - my_rates->k148, - my_rates->k149, - my_rates->k150, - my_rates->k151, - my_rates->k152, - my_rates->k153, - my_rates->kz15, - my_rates->kz16, - my_rates->kz17, - my_rates->kz18, - my_rates->kz19, - my_rates->kz20, - my_rates->kz21, - my_rates->kz22, - my_rates->kz23, - my_rates->kz24, - my_rates->kz25, - my_rates->kz26, - my_rates->kz27, - my_rates->kz28, - my_rates->kz29, - my_rates->kz30, - my_rates->kz31, - my_rates->kz32, - my_rates->kz33, - my_rates->kz34, - my_rates->kz35, - my_rates->kz36, - my_rates->kz37, - my_rates->kz38, - my_rates->kz39, - my_rates->kz40, - my_rates->kz41, - my_rates->kz42, - my_rates->kz43, - my_rates->kz44, - my_rates->kz45, - my_rates->kz46, - my_rates->kz47, - my_rates->kz48, - my_rates->kz49, - my_rates->kz50, - my_rates->kz51, - my_rates->kz52, - my_rates->kz53, - my_rates->kz54, - my_rates->cieY06, - my_rates->LH2.props.dimension, - &my_rates->LH2.props.data_size, - my_rates->LH2.props.parameters[0], - my_rates->LH2.props.parameters[1], - my_rates->LH2.props.parameters[2], - &my_rates->LH2.props.parameter_spacing[0], - &my_rates->LH2.props.parameter_spacing[1], - &my_rates->LH2.props.parameter_spacing[2], - my_rates->LH2.data, - my_rates->LHD.props.dimension, - &my_rates->LHD.props.data_size, - my_rates->LHD.props.parameters[0], - my_rates->LHD.props.parameters[1], - my_rates->LHD.props.parameters[2], - &my_rates->LHD.props.parameter_spacing[0], - &my_rates->LHD.props.parameter_spacing[1], - &my_rates->LHD.props.parameter_spacing[2], - my_rates->LHD.data, - my_rates->LCI.props.dimension, - &my_rates->LCI.props.data_size, - my_rates->LCI.props.parameters[0], - my_rates->LCI.props.parameters[1], - my_rates->LCI.props.parameters[2], - &my_rates->LCI.props.parameter_spacing[0], - &my_rates->LCI.props.parameter_spacing[1], - &my_rates->LCI.props.parameter_spacing[2], - my_rates->LCI.data, - my_rates->LCII.props.dimension, - &my_rates->LCII.props.data_size, - my_rates->LCII.props.parameters[0], - my_rates->LCII.props.parameters[1], - my_rates->LCII.props.parameters[2], - &my_rates->LCII.props.parameter_spacing[0], - &my_rates->LCII.props.parameter_spacing[1], - &my_rates->LCII.props.parameter_spacing[2], - my_rates->LCII.data, - my_rates->LOI.props.dimension, - &my_rates->LOI.props.data_size, - my_rates->LOI.props.parameters[0], - my_rates->LOI.props.parameters[1], - my_rates->LOI.props.parameters[2], - &my_rates->LOI.props.parameter_spacing[0], - &my_rates->LOI.props.parameter_spacing[1], - &my_rates->LOI.props.parameter_spacing[2], - my_rates->LOI.data, - my_rates->LCO.props.dimension, - &my_rates->LCO.props.data_size, - my_rates->LCO.props.parameters[0], - my_rates->LCO.props.parameters[1], - my_rates->LCO.props.parameters[2], - &my_rates->LCO.props.parameter_spacing[0], - &my_rates->LCO.props.parameter_spacing[1], - &my_rates->LCO.props.parameter_spacing[2], - my_rates->LCO.data, - my_rates->LOH.props.dimension, - &my_rates->LOH.props.data_size, - my_rates->LOH.props.parameters[0], - my_rates->LOH.props.parameters[1], - my_rates->LOH.props.parameters[2], - &my_rates->LOH.props.parameter_spacing[0], - &my_rates->LOH.props.parameter_spacing[1], - &my_rates->LOH.props.parameter_spacing[2], - my_rates->LOH.data, - my_rates->LH2O.props.dimension, - &my_rates->LH2O.props.data_size, - my_rates->LH2O.props.parameters[0], - my_rates->LH2O.props.parameters[1], - my_rates->LH2O.props.parameters[2], - &my_rates->LH2O.props.parameter_spacing[0], - &my_rates->LH2O.props.parameter_spacing[1], - &my_rates->LH2O.props.parameter_spacing[2], - my_rates->LH2O.data, - my_rates->alphap.props.dimension, - &my_rates->alphap.props.data_size, - my_rates->alphap.props.parameters[0], - my_rates->alphap.props.parameters[1], - &my_rates->alphap.props.parameter_spacing[0], - &my_rates->alphap.props.parameter_spacing[1], - my_rates->alphap.data, - &my_chemistry->multi_metals, - &my_chemistry->metal_abundances, - &my_chemistry->dust_species, - &my_chemistry->use_multiple_dust_temperatures, - &my_chemistry->dust_sublimation, - my_fields->local_ISM_metal_density, - my_fields->ccsn13_metal_density, - my_fields->ccsn20_metal_density, - my_fields->ccsn25_metal_density, - my_fields->ccsn30_metal_density, - my_fields->fsn13_metal_density, - my_fields->fsn15_metal_density, - my_fields->fsn50_metal_density, - my_fields->fsn80_metal_density, - my_fields->pisn170_metal_density, - my_fields->pisn200_metal_density, - my_fields->y19_metal_density, - &my_rates->SN0_N, - my_rates->SN0_XC, - my_rates->SN0_XO, - my_rates->SN0_XMg, - my_rates->SN0_XAl, - my_rates->SN0_XSi, - my_rates->SN0_XS, - my_rates->SN0_XFe, - my_rates->SN0_fC, - my_rates->SN0_fO, - my_rates->SN0_fMg, - my_rates->SN0_fAl, - my_rates->SN0_fSi, - my_rates->SN0_fS, - my_rates->SN0_fFe, - my_rates->SN0_fSiM, - my_rates->SN0_fFeM, - my_rates->SN0_fMg2SiO4, - my_rates->SN0_fMgSiO3, - my_rates->SN0_fFe3O4, - my_rates->SN0_fAC, - my_rates->SN0_fSiO2D, - my_rates->SN0_fMgO, - my_rates->SN0_fFeS, - my_rates->SN0_fAl2O3, - my_rates->SN0_freforg, - my_rates->SN0_fvolorg, - my_rates->SN0_fH2Oice, - my_rates->SN0_r0SiM, - my_rates->SN0_r0FeM, - my_rates->SN0_r0Mg2SiO4, - my_rates->SN0_r0MgSiO3, - my_rates->SN0_r0Fe3O4, - my_rates->SN0_r0AC, - my_rates->SN0_r0SiO2D, - my_rates->SN0_r0MgO, - my_rates->SN0_r0FeS, - my_rates->SN0_r0Al2O3, - my_rates->SN0_r0reforg, - my_rates->SN0_r0volorg, - my_rates->SN0_r0H2Oice, - my_rates->gr_N, - &my_rates->gr_Size, - &my_rates->gr_dT, - my_rates->gr_Td, - my_rates->SN0_kpSiM, - my_rates->SN0_kpFeM, - my_rates->SN0_kpMg2SiO4, - my_rates->SN0_kpMgSiO3, - my_rates->SN0_kpFe3O4, - my_rates->SN0_kpAC, - my_rates->SN0_kpSiO2D, - my_rates->SN0_kpMgO, - my_rates->SN0_kpFeS, - my_rates->SN0_kpAl2O3, - my_rates->SN0_kpreforg, - my_rates->SN0_kpvolorg, - my_rates->SN0_kpH2Oice, - my_rates->h2dustS, - my_rates->h2dustC, - my_rates->gas_grain2, - &my_rates->gamma_isrf2, - my_rates->grain_growth_rate, - &my_chemistry->radiative_transfer_HDI_dissociation, - my_fields->RT_HDI_dissociation_rate, - &my_chemistry->radiative_transfer_metal_ionization, - my_fields->RT_CI_ionization_rate, - my_fields->RT_OI_ionization_rate, - &my_chemistry->radiative_transfer_metal_dissociation, - my_fields->RT_CO_dissociation_rate, - my_fields->RT_OH_dissociation_rate, - my_fields->RT_H2O_dissociation_rate, - &my_chemistry->radiative_transfer_use_H2_shielding, - &my_chemistry->use_isrf_field, - my_fields->isrf_habing, - &my_chemistry->H2_custom_shielding, - my_fields->H2_custom_shielding_factor, - &my_chemistry->max_iterations, - &my_chemistry->exit_after_iterations_exceeded); + solve_rate_cool_g( + &metal_field_present, &dt_value, &temperature_units, &co_length_units, + &co_density_units, &ierr, my_chemistry, my_rates, my_units, my_fields, + &my_uvb_rates + ); if (ierr == FAIL) { fprintf(stderr, "Error in solve_rate_cool_g.\n"); diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C new file mode 100644 index 000000000..b3b175451 --- /dev/null +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -0,0 +1,1454 @@ +// See LICENSE file for license and copyright information + +/// @file solve_rate_cool_g-cpp.C +/// @brief Declares signature of solve_rate_cool_g + +// This file was initially generated automatically during conversion of the +// solve_rate_cool_g function from FORTRAN to C++ + +#include +#include + +#include "grackle.h" +#include "fortran_func_decls.h" +#include "utils-cpp.hpp" + +#include "solve_rate_cool_g-cpp.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +void solve_rate_cool_g( + int* imetal, double* dt, double* utem, double* uxyz, double* urho, int* ierr, + chemistry_data* my_chemistry, chemistry_data_storage* my_rates, + code_units* my_units, grackle_field_data* my_fields, + photo_rate_storage* my_uvb_rates +) +{ + + + // SOLVE MULTI-SPECIES RATE EQUATIONS AND RADIATIVE COOLING + + // written by: Yu Zhang, Peter Anninos and Tom Abel + // date: + // modified1: January, 1996 by Greg Bryan; converted to KRONOS + // modified2: October, 1996 by GB; adapted to AMR + // modified3: May, 1999 by GB and Tom Abel, 3bodyH2, solver, HD + // modified4: June, 2005 by GB to solve rate & cool at same time + // modified5: April, 2009 by JHW to include radiative transfer + // modified6: September, 2009 by BDS to include cloudy cooling + + // PURPOSE: + // Solve the multi-species rate and cool equations. + + // INPUTS: + // icool - flag to update energy from radiative cooling + // in,jn,kn - dimensions of 3D fields + + // d - total density field + // de - electron density field + // HI,HII - H density fields (neutral & ionized) + // HeI/II/III - He density fields + // DI/II - D density fields (neutral & ionized) + // HDI - neutral HD molecule density field + // HM - H- density field + // H2I - H_2 (molecular H) density field + // H2II - H_2+ density field + // metal - metal density field + // dust - dust density field + // kph* - photoionization fields + // gamma* - photoheating fields + // f_shield_custom - custom H2 shielding factor + + // is,ie - start and end indices of active region (zero based) + // iexpand - comoving coordinates flag (0 = off, 1 = on) + // idim - dimensionality (rank) of problem + // ispecies - chemistry module (1 - H/He only, 2 - molecular H, 3 - D) + // imetal - flag if metal field is active (0 = no, 1 = yes) + // imcool - flag if there is metal cooling + // idust - flag for H2 formation on dust grains + // idustall - flag for dust (0 - none, 1 - heating/cooling + H2 form.) + // idustfield - flag if a dust density field is present + // iisrffield - flag if a field for the interstellar radiation field is present + // ih2co - flag to include H2 cooling (1 = on, 0 = off) + // ipiht - flag to include photoionization heating (1 = on, 0 = off) + // idustrec - flag to include dust recombination cooling (1 = on, -1 = off) + // iH2shield - flag for approximate self-shielding of H2 (Wolcott-Green+ 2011) + // iradshield - flag for approximate self-shielding of UV background + // avgsighi - spectrum averaged ionization crs for HI for use with shielding + // avgsighei - spectrum averaged ionization crs for HeI for use with shielding + // avgsigheii - spectrum averaged ionization crs for HeII for use with shielding + // iradtrans - flag to include radiative transfer (1 = on, 0 = off) + // iradcoupled - flag to indicate coupled radiative transfer + // iradstep - flag to indicate intermediate coupled radiative transfer timestep + // irt_honly - flag to indicate applying RT ionization and heating to HI only + // iH2shieldcustom - flag to indicate a custom H2 shielding factor is provided + + // fh - Hydrogen mass fraction (typically 0.76) + // dtoh - Deuterium to H mass ratio + // z_solar - Solar metal mass fraction + // fgr - the local dust to gas ratio (by mass) + // dt - timestep to integrate over + // aye - expansion factor (in code units) + + // utim - time units (i.e. code units to CGS conversion factor) + // uaye - expansion factor conversion factor (uaye = 1/(1+zinit)) + // urho - density units + // uxyz - length units + // utem - temperature(-like) units + + // temstart, temend - start and end of temperature range for rate table + // nratec - dimensions of chemical rate arrays (functions of temperature) + // dtemstart, dtemend - start and end of dust temperature range + // ndratec - extra dimension for H2 formation on dust rate (dust temperature) + + // icmbTfloor - flag to include temperature floor from cmb + // iClHeat - flag to include cloudy heating + // priGridRank - rank of cloudy primordial cooling data grid + // priGridDim - array containing dimensions of cloudy primordial data + // priPar1, priPar2, priPar3 - arrays containing primordial grid parameter values + // priDataSize - total size of flattened 1D primordial cooling data array + // priCooling - primordial cooling data + // priHeating - primordial heating data + // priMMW - primordial mmw data + // metGridRank - rank of cloudy metal cooling data grid + // metGridDim - array containing dimensions of cloudy metal data + // metPar1, metPar2, metPar3 - arrays containing metal grid parameter values + // metDataSize - total size of flattened 1D metal cooling data array + // metCooling - metal cooling data + // metHeating - metal heating data + // iVheat - flag for using volumetric heating rate + // iMheat - flag for using specific heating rate + // Vheat - array of volumetric heating rates + // Mheat - array of specific heating rates + // iTfloor - flag for using temperature floor field + // Tfloor_scalar - scalar temperature floor value + // Tfloor - array of temperature floor values + // itmax - maximum allowed sub-cycle iterations + // exititmax - flag to exit if max iterations exceeded + + // OUTPUTS: + // update chemical rate densities (HI, HII, etc) + + // PARAMETERS: + // mh - H mass in cgs units + + // ----------------------------------------------------------------------- + + + // General Arguments + + int ierror; + + // -- removed line (previously just declared arg types) -- + + // Density, energy and velocity fields fields + + grackle::impl::View de(my_fields->e_density, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); + grackle::impl::View HI(my_fields->HI_density, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); + grackle::impl::View HII(my_fields->HII_density, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); + grackle::impl::View d(my_fields->density, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); + grackle::impl::View e(my_fields->internal_energy, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); + + // Radiative transfer fields + + grackle::impl::View kphHI(my_fields->RT_HI_ionization_rate, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); + + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + + // H2 self-shielding length-scale field + + + // Interstellar radiation field for dust heating + + + // Custom H2 shielding factor + + + // Cooling tables (coolings rates as a function of temperature) + + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // opacity table + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + + // -- removed line (previously just declared arg types) -- + + // Chemistry tables (rates as a function of temperature) + + // -- removed line (previously just declared arg types) -- + + // Cloudy cooling data + + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + + // Parameters + +#ifdef GRACKLE_FLOAT_4 + const gr_float tolerance = (gr_float)(1.0e-05); +#else + const gr_float tolerance = (gr_float)(1.0e-10); +#endif + + const double mh_local_var = mh_grflt; + const double pi_local_var = pi_fortran_val; + + // Locals + + int i, j, k, iter; + int t, dj, dk; + double ttmin, dom, energy, comp1, comp2; + double coolunit, dbase1, tbase1, xbase1, chunit, uvel; + double heq1, heq2, eqk221, eqk222, eqk131, eqk132, eqt1, eqt2, eqtdef, dheq, heq, dlogtem, dx_cgs, c_ljeans, min_metallicity; + gr_float factor; + + // row temporaries + + std::vector indixe(my_fields->grid_dimension[0]); + double olddtit; + std::vector t1(my_fields->grid_dimension[0]); + std::vector t2(my_fields->grid_dimension[0]); + std::vector logtem(my_fields->grid_dimension[0]); + std::vector tdef(my_fields->grid_dimension[0]); + std::vector dtit(my_fields->grid_dimension[0]); + std::vector ttot(my_fields->grid_dimension[0]); + std::vector p2d(my_fields->grid_dimension[0]); + std::vector tgas(my_fields->grid_dimension[0]); + std::vector tgasold(my_fields->grid_dimension[0]); + std::vector tdust(my_fields->grid_dimension[0]); + std::vector metallicity(my_fields->grid_dimension[0]); + std::vector dust2gas(my_fields->grid_dimension[0]); + std::vector rhoH(my_fields->grid_dimension[0]); + std::vector mmw(my_fields->grid_dimension[0]); + std::vector mynh(my_fields->grid_dimension[0]); + std::vector myde(my_fields->grid_dimension[0]); + std::vector gammaha_eff(my_fields->grid_dimension[0]); + std::vector gasgr_tdust(my_fields->grid_dimension[0]); + std::vector regr(my_fields->grid_dimension[0]); + std::vector ddom(my_fields->grid_dimension[0]); + + // Rate equation row temporaries + + std::vector HIp(my_fields->grid_dimension[0]); + std::vector HIIp(my_fields->grid_dimension[0]); + std::vector HeIp(my_fields->grid_dimension[0]); + std::vector HeIIp(my_fields->grid_dimension[0]); + std::vector HeIIIp(my_fields->grid_dimension[0]); + std::vector HMp(my_fields->grid_dimension[0]); + std::vector H2Ip(my_fields->grid_dimension[0]); + std::vector H2IIp(my_fields->grid_dimension[0]); + std::vector dep(my_fields->grid_dimension[0]); + std::vector dedot(my_fields->grid_dimension[0]); + std::vector HIdot(my_fields->grid_dimension[0]); + std::vector dedot_prev(my_fields->grid_dimension[0]); + std::vector DIp(my_fields->grid_dimension[0]); + std::vector DIIp(my_fields->grid_dimension[0]); + std::vector HDIp(my_fields->grid_dimension[0]); + std::vector HIdot_prev(my_fields->grid_dimension[0]); + std::vector k24shield(my_fields->grid_dimension[0]); + std::vector k25shield(my_fields->grid_dimension[0]); + std::vector k26shield(my_fields->grid_dimension[0]); + std::vector k28shield(my_fields->grid_dimension[0]); + std::vector k29shield(my_fields->grid_dimension[0]); + std::vector k30shield(my_fields->grid_dimension[0]); + std::vector k31shield(my_fields->grid_dimension[0]); + std::vector k1(my_fields->grid_dimension[0]); + std::vector k2(my_fields->grid_dimension[0]); + std::vector k3(my_fields->grid_dimension[0]); + std::vector k4(my_fields->grid_dimension[0]); + std::vector k5(my_fields->grid_dimension[0]); + std::vector k6(my_fields->grid_dimension[0]); + std::vector k7(my_fields->grid_dimension[0]); + std::vector k8(my_fields->grid_dimension[0]); + std::vector k9(my_fields->grid_dimension[0]); + std::vector k10(my_fields->grid_dimension[0]); + std::vector k11(my_fields->grid_dimension[0]); + std::vector k12(my_fields->grid_dimension[0]); + std::vector k13(my_fields->grid_dimension[0]); + std::vector k14(my_fields->grid_dimension[0]); + std::vector k15(my_fields->grid_dimension[0]); + std::vector k16(my_fields->grid_dimension[0]); + std::vector k17(my_fields->grid_dimension[0]); + std::vector k18(my_fields->grid_dimension[0]); + std::vector k19(my_fields->grid_dimension[0]); + std::vector k22(my_fields->grid_dimension[0]); + std::vector k50(my_fields->grid_dimension[0]); + std::vector k51(my_fields->grid_dimension[0]); + std::vector k52(my_fields->grid_dimension[0]); + std::vector k53(my_fields->grid_dimension[0]); + std::vector k54(my_fields->grid_dimension[0]); + std::vector k55(my_fields->grid_dimension[0]); + std::vector k56(my_fields->grid_dimension[0]); + std::vector k57(my_fields->grid_dimension[0]); + std::vector k58(my_fields->grid_dimension[0]); + std::vector k13dd(my_fields->grid_dimension[0] * 14); + std::vector h2dust(my_fields->grid_dimension[0]); + std::vector ncrn(my_fields->grid_dimension[0]); + std::vector ncrd1(my_fields->grid_dimension[0]); + std::vector ncrd2(my_fields->grid_dimension[0]); + std::vector DMp(my_fields->grid_dimension[0]); + std::vector HDIIp(my_fields->grid_dimension[0]); + std::vector HeHIIp(my_fields->grid_dimension[0]); + std::vector CIp(my_fields->grid_dimension[0]); + std::vector CIIp(my_fields->grid_dimension[0]); + std::vector COp(my_fields->grid_dimension[0]); + std::vector CO2p(my_fields->grid_dimension[0]); + std::vector OIp(my_fields->grid_dimension[0]); + std::vector OHp(my_fields->grid_dimension[0]); + std::vector H2Op(my_fields->grid_dimension[0]); + std::vector O2p(my_fields->grid_dimension[0]); + std::vector SiIp(my_fields->grid_dimension[0]); + std::vector SiOIp(my_fields->grid_dimension[0]); + std::vector SiO2Ip(my_fields->grid_dimension[0]); + std::vector CHp(my_fields->grid_dimension[0]); + std::vector CH2p(my_fields->grid_dimension[0]); + std::vector COIIp(my_fields->grid_dimension[0]); + std::vector OIIp(my_fields->grid_dimension[0]); + std::vector OHIIp(my_fields->grid_dimension[0]); + std::vector H2OIIp(my_fields->grid_dimension[0]); + std::vector H3OIIp(my_fields->grid_dimension[0]); + std::vector O2IIp(my_fields->grid_dimension[0]); + std::vector Mgp(my_fields->grid_dimension[0]); + std::vector Alp(my_fields->grid_dimension[0]); + std::vector Sp(my_fields->grid_dimension[0]); + std::vector Fep(my_fields->grid_dimension[0]); + std::vector SiMp(my_fields->grid_dimension[0]); + std::vector FeMp(my_fields->grid_dimension[0]); + std::vector Mg2SiO4p(my_fields->grid_dimension[0]); + std::vector MgSiO3p(my_fields->grid_dimension[0]); + std::vector Fe3O4p(my_fields->grid_dimension[0]); + std::vector ACp(my_fields->grid_dimension[0]); + std::vector SiO2Dp(my_fields->grid_dimension[0]); + std::vector MgOp(my_fields->grid_dimension[0]); + std::vector FeSp(my_fields->grid_dimension[0]); + std::vector Al2O3p(my_fields->grid_dimension[0]); + std::vector reforgp(my_fields->grid_dimension[0]); + std::vector volorgp(my_fields->grid_dimension[0]); + std::vector H2Oicep(my_fields->grid_dimension[0]); + + std::vector k125(my_fields->grid_dimension[0]); + std::vector k129(my_fields->grid_dimension[0]); + std::vector k130(my_fields->grid_dimension[0]); + std::vector k131(my_fields->grid_dimension[0]); + std::vector k132(my_fields->grid_dimension[0]); + std::vector k133(my_fields->grid_dimension[0]); + std::vector k134(my_fields->grid_dimension[0]); + std::vector k135(my_fields->grid_dimension[0]); + std::vector k136(my_fields->grid_dimension[0]); + std::vector k137(my_fields->grid_dimension[0]); + std::vector k148(my_fields->grid_dimension[0]); + std::vector k149(my_fields->grid_dimension[0]); + std::vector k150(my_fields->grid_dimension[0]); + std::vector k151(my_fields->grid_dimension[0]); + std::vector k152(my_fields->grid_dimension[0]); + std::vector k153(my_fields->grid_dimension[0]); + std::vector kz15(my_fields->grid_dimension[0]); + std::vector kz16(my_fields->grid_dimension[0]); + std::vector kz17(my_fields->grid_dimension[0]); + std::vector kz18(my_fields->grid_dimension[0]); + std::vector kz19(my_fields->grid_dimension[0]); + std::vector kz20(my_fields->grid_dimension[0]); + std::vector kz21(my_fields->grid_dimension[0]); + std::vector kz22(my_fields->grid_dimension[0]); + std::vector kz23(my_fields->grid_dimension[0]); + std::vector kz24(my_fields->grid_dimension[0]); + std::vector kz25(my_fields->grid_dimension[0]); + std::vector kz26(my_fields->grid_dimension[0]); + std::vector kz27(my_fields->grid_dimension[0]); + std::vector kz28(my_fields->grid_dimension[0]); + std::vector kz29(my_fields->grid_dimension[0]); + std::vector kz30(my_fields->grid_dimension[0]); + std::vector kz31(my_fields->grid_dimension[0]); + std::vector kz32(my_fields->grid_dimension[0]); + std::vector kz33(my_fields->grid_dimension[0]); + std::vector kz34(my_fields->grid_dimension[0]); + std::vector kz35(my_fields->grid_dimension[0]); + std::vector kz36(my_fields->grid_dimension[0]); + std::vector kz37(my_fields->grid_dimension[0]); + std::vector kz38(my_fields->grid_dimension[0]); + std::vector kz39(my_fields->grid_dimension[0]); + std::vector kz40(my_fields->grid_dimension[0]); + std::vector kz41(my_fields->grid_dimension[0]); + std::vector kz42(my_fields->grid_dimension[0]); + std::vector kz43(my_fields->grid_dimension[0]); + std::vector kz44(my_fields->grid_dimension[0]); + std::vector kz45(my_fields->grid_dimension[0]); + std::vector kz46(my_fields->grid_dimension[0]); + std::vector kz47(my_fields->grid_dimension[0]); + std::vector kz48(my_fields->grid_dimension[0]); + std::vector kz49(my_fields->grid_dimension[0]); + std::vector kz50(my_fields->grid_dimension[0]); + std::vector kz51(my_fields->grid_dimension[0]); + std::vector kz52(my_fields->grid_dimension[0]); + std::vector kz53(my_fields->grid_dimension[0]); + std::vector kz54(my_fields->grid_dimension[0]); + // -- removed line (previously just declared arg types) -- + // -- removed line (previously just declared arg types) -- + std::vector kdSiM(my_fields->grid_dimension[0]); + std::vector kdFeM(my_fields->grid_dimension[0]); + std::vector kdMg2SiO4(my_fields->grid_dimension[0]); + std::vector kdMgSiO3(my_fields->grid_dimension[0]); + std::vector kdFe3O4(my_fields->grid_dimension[0]); + std::vector kdAC(my_fields->grid_dimension[0]); + std::vector kdSiO2D(my_fields->grid_dimension[0]); + std::vector kdMgO(my_fields->grid_dimension[0]); + std::vector kdFeS(my_fields->grid_dimension[0]); + std::vector kdAl2O3(my_fields->grid_dimension[0]); + std::vector kdreforg(my_fields->grid_dimension[0]); + std::vector kdvolorg(my_fields->grid_dimension[0]); + std::vector kdH2Oice(my_fields->grid_dimension[0]); + // grain temperature + std::vector tSiM(my_fields->grid_dimension[0]); + std::vector tFeM(my_fields->grid_dimension[0]); + std::vector tMg2SiO4(my_fields->grid_dimension[0]); + std::vector tMgSiO3(my_fields->grid_dimension[0]); + std::vector tFe3O4(my_fields->grid_dimension[0]); + std::vector tAC(my_fields->grid_dimension[0]); + std::vector tSiO2D(my_fields->grid_dimension[0]); + std::vector tMgO(my_fields->grid_dimension[0]); + std::vector tFeS(my_fields->grid_dimension[0]); + std::vector tAl2O3(my_fields->grid_dimension[0]); + std::vector treforg(my_fields->grid_dimension[0]); + std::vector tvolorg(my_fields->grid_dimension[0]); + std::vector tH2Oice(my_fields->grid_dimension[0]); + + // Cooling/heating row locals + + std::vector ceHI(my_fields->grid_dimension[0]); + std::vector ceHeI(my_fields->grid_dimension[0]); + std::vector ceHeII(my_fields->grid_dimension[0]); + std::vector ciHI(my_fields->grid_dimension[0]); + std::vector ciHeI(my_fields->grid_dimension[0]); + std::vector ciHeIS(my_fields->grid_dimension[0]); + std::vector ciHeII(my_fields->grid_dimension[0]); + std::vector reHII(my_fields->grid_dimension[0]); + std::vector reHeII1(my_fields->grid_dimension[0]); + std::vector reHeII2(my_fields->grid_dimension[0]); + std::vector reHeIII(my_fields->grid_dimension[0]); + std::vector brem(my_fields->grid_dimension[0]); + std::vector edot(my_fields->grid_dimension[0]); + std::vector hyd01k(my_fields->grid_dimension[0]); + std::vector h2k01(my_fields->grid_dimension[0]); + std::vector vibh(my_fields->grid_dimension[0]); + std::vector roth(my_fields->grid_dimension[0]); + std::vector rotl(my_fields->grid_dimension[0]); + std::vector gpldl(my_fields->grid_dimension[0]); + std::vector gphdl(my_fields->grid_dimension[0]); + std::vector hdlte(my_fields->grid_dimension[0]); + std::vector hdlow(my_fields->grid_dimension[0]); + std::vector cieco(my_fields->grid_dimension[0]); + + // Iteration mask + + gr_mask_type anydust; + std::vector itmask(my_fields->grid_dimension[0]); + std::vector itmask_tmp(my_fields->grid_dimension[0]); + std::vector itmask_nr(my_fields->grid_dimension[0]); + std::vector itmask_metal(my_fields->grid_dimension[0]); + int itr; + std::vector imp_eng(my_fields->grid_dimension[0]); + + + // \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\///////////////////////////////// + // ======================================================================= + + // Set error indicator + + (*ierr) = 1; + + // Set flag for dust-related options + + if ((my_chemistry->h2_on_dust > 0) || (my_chemistry->dust_chemistry > 0)) { + anydust = MASK_TRUE; + } else { + anydust = MASK_FALSE; + } + + // ignore metal chemistry/cooling below this metallicity + min_metallicity = 1.e-9 / my_chemistry->SolarMetalFractionByMass; + + // Set units + + dom = (*urho)*(std::pow(my_units->a_value,3))/mh_local_var; + tbase1 = my_units->time_units; + xbase1 = (*uxyz)/(my_units->a_value*my_units->a_units); // uxyz is [x]*a = [x]*[a]*a' ' + dbase1 = (*urho)*std::pow((my_units->a_value*my_units->a_units),3); // urho is [dens]/a^3 = [dens]/([a]*a')^3 ' + coolunit = (std::pow(my_units->a_units,5) * std::pow(xbase1,2) * std::pow(mh_local_var,2)) / (std::pow(tbase1,3) * dbase1); + uvel = ((*uxyz)/my_units->a_value) / my_units->time_units; + // chunit = (1.60218e-12_DKIND)/(2._DKIND*uvel*uvel*mh) ! 1 eV per H2 formed + chunit = (1.60218e-12)/(uvel*uvel*mh_local_var); // 1 eV per REACTION (Feb 2020, Gen Chiaki) + + dx_cgs = my_fields->grid_dx * xbase1; + c_ljeans = std::sqrt((my_chemistry->Gamma * pi_local_var * kboltz_grflt) / + (GravConst_grflt * mh_local_var * dbase1)); + + dlogtem = (std::log(my_chemistry->TemperatureEnd) - std::log(my_chemistry->TemperatureStart))/(double)(my_chemistry->NumberOfTemperatureBins-1 ); + + // We better make consistent at first GC202002 + + if (my_chemistry->primordial_chemistry > 0) { + +#define ABUNDANCE_CORRECTION +#ifdef ABUNDANCE_CORRECTION + FORTRAN_NAME(make_consistent_g)(de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, + d.data(), &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, imetal, &my_chemistry->HydrogenFractionByMass, &my_chemistry->DeuteriumToHydrogenRatio, + &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, &dom, + my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, + my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, + my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, + my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, + my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, + my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, + my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, + my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, + my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, + &my_chemistry->multi_metals, &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, + my_fields->local_ISM_metal_density, + my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, + my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, + my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density, + &my_rates->SN0_N, + my_rates->SN0_XC, my_rates->SN0_XO, my_rates->SN0_XMg, my_rates->SN0_XAl, my_rates->SN0_XSi, + my_rates->SN0_XS, my_rates->SN0_XFe, + my_rates->SN0_fC, my_rates->SN0_fO, my_rates->SN0_fMg, my_rates->SN0_fAl, my_rates->SN0_fSi, + my_rates->SN0_fS, my_rates->SN0_fFe + ); +#endif + + } + + // Convert densities from comoving to proper + + if (my_units->comoving_coordinates == 1) { + + factor = (gr_float)(std::pow(my_units->a_value,(-3)) ); + + FORTRAN_NAME(scale_fields_g)( + &my_chemistry->primordial_chemistry, imetal, &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, + &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->multi_metals, &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, &factor, + &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], + d.data(), de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, + my_fields->metal_density, my_fields->dust_density, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, + my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, + my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, + my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, + my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, + my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, + my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, + my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, + my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, + my_fields->local_ISM_metal_density, my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, + my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, + my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density); + + } + +#ifdef ABUNDANCE_CORRECTION + FORTRAN_NAME(ceiling_species_g)(d.data(), de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, + &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, imetal, &my_chemistry->use_dust_density_field, + my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, + &my_chemistry->metal_abundances, &my_chemistry->metal_chemistry, &my_chemistry->dust_species, &my_chemistry->multi_metals, + &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, + my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, + my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, + my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, + my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, + my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, + my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, + my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, + my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, + my_fields->local_ISM_metal_density, + my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, + my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, + my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density); +#endif + + + // Loop over zones, and do an entire i-column in one go + dk = my_fields->grid_end[2] - my_fields->grid_start[2] + 1; + dj = my_fields->grid_end[1] - my_fields->grid_start[1] + 1; + + // parallelize the k and j loops with OpenMP + // flat j and k loops for better parallelism + //_// PORT: #ifdef _OPENMP + //_// PORT: ! ierr is declared as shared and should be modified with atomic operation + //_// PORT: !$omp parallel do schedule(runtime) private( + //_// PORT: !$omp& i, j, k, iter, + //_// PORT: !$omp& ttmin, energy, comp1, comp2, + //_// PORT: !$omp& heq1, heq2, eqk221, eqk222, eqk131, eqk132, + //_// PORT: !$omp& eqt1, eqt2, eqtdef, dheq, heq, + //_// PORT: !$omp& indixe, + //_// PORT: !$omp& t1, t2, logtem, tdef, + //_// PORT: !$omp& dtit, ttot, p2d, tgas, tgasold, + //_// PORT: !$omp& tdust, metallicity, dust2gas, rhoH, mmw, + //_// PORT: !$omp& mynh, myde, gammaha_eff, gasgr_tdust, regr, ddom, + //_// PORT: !$omp& olddtit, + //_// PORT: !$omp& HIp, HIIp, HeIp, HeIIp, HeIIIp, + //_// PORT: !$omp& HMp, H2Ip, H2IIp, + //_// PORT: !$omp& dep, dedot,HIdot, dedot_prev, + //_// PORT: !$omp& DIp, DIIp, HDIp, HIdot_prev, + //_// PORT: !$omp& k24shield, k25shield, k26shield, + //_// PORT: !$omp& k28shield, k29shield, k30shield, + //_// PORT: !$omp& k31shield, + //_// PORT: !$omp& k1 , k2 , k3 , k4 , k5, + //_// PORT: !$omp& k6 , k7 , k8 , k9 , k10, + //_// PORT: !$omp& k11, k12, k13, k14, k15, + //_// PORT: !$omp& k16, k17, k18, k19, k22, + //_// PORT: !$omp& k50, k51, k52, k53, k54, + //_// PORT: !$omp& k55, k56, k57, k58, k13dd, h2dust, + //_// PORT: !$omp& ncrn, ncrd1, ncrd2, + //_// PORT: !$omp& ceHI, ceHeI, ceHeII, + //_// PORT: !$omp& ciHI, ciHeI, ciHeIS, ciHeII, + //_// PORT: !$omp& reHII, reHeII1, reHeII2, reHeIII, + //_// PORT: !$omp& brem, edot, + //_// PORT: !$omp& hyd01k, h2k01, vibh, roth, rotl, + //_// PORT: !$omp& gpldl, gphdl, hdlte, hdlow, cieco, + //_// PORT: !$omp& itmask, itmask_metal ) + //_// PORT: #endif + //_// TODO_USE: OMP_PRAGMA("omp parallel") + { + //_// TODO: move relevant variable declarations to here to replace OMP private + //_// TODO_USE: OMP_PRAGMA("omp for") + for (t = 0; t<=(dk * dj - 1); t++) { + k = t/dj + my_fields->grid_start[2]+1; + j = grackle::impl::mod(t,dj) + my_fields->grid_start[1]+1; + + // tolerance = 1.0e-06_DKIND * dt + + // Initialize iteration mask to true for all cells. + + for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + itmask[i-1] = MASK_TRUE; + } + + // If we are using coupled radiation with intermediate stepping, + // set iteration mask to include only cells with radiation in the + // intermediate coupled chemistry / energy step + if (my_chemistry->use_radiative_transfer == 1) { + if (my_chemistry->radiative_transfer_coupled_rate_solver == 1 && my_chemistry->radiative_transfer_intermediate_step == 1) { + for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + if (kphHI(i-1,j-1,k-1) > 0) { + itmask[i-1] = MASK_TRUE; + } else { + itmask[i-1] = MASK_FALSE; + } + } + } + + // Normal rate solver, but don't double count cells with radiation + if (my_chemistry->radiative_transfer_coupled_rate_solver == 1 && my_chemistry->radiative_transfer_intermediate_step == 0) { + for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + if (kphHI(i-1,j-1,k-1) > 0) { + itmask[i-1] = MASK_FALSE; + } else { + itmask[i-1] = MASK_TRUE; + } + } + } + } + + // Set time elapsed to zero for each cell in 1D section + + for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + ttot[i-1] = 0.; + } + + // A useful slice variable since we do this a lot + + for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + ddom[i-1] = d(i-1,j-1,k-1) * dom; + } + + // ------------------ Loop over subcycles ---------------- + + for (iter = 1; iter<=(my_chemistry->max_iterations); iter++) { + + for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + if (itmask[i-1] != MASK_FALSE) { + dtit[i-1] = huge8; + } + } + + // Compute the cooling rate, tgas, tdust, and metallicity for this row + + FORTRAN_NAME(cool1d_multi_g)( + d.data(), e.data(), my_fields->x_velocity, my_fields->y_velocity, my_fields->z_velocity, de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, + &my_units->comoving_coordinates, &my_chemistry->primordial_chemistry, imetal, &my_chemistry->metal_cooling, + &my_chemistry->h2_on_dust, &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_chemistry->dust_recombination_cooling, + &my_fields->grid_rank, &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, &my_chemistry->ih2co, &my_chemistry->ipiht, &iter, &my_chemistry->photoelectric_heating, + &my_units->a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, + utem, uxyz, &my_units->a_units, urho, &my_units->time_units, + &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, + my_rates->ceHI, my_rates->ceHeI, my_rates->ceHeII, my_rates->ciHI, my_rates->ciHeI, + my_rates->ciHeIS, my_rates->ciHeII, my_rates->reHII, my_rates->reHeII1, + my_rates->reHeII2, my_rates->reHeIII, my_rates->brem, &my_rates->comp, &my_rates->gammah, + &my_chemistry->interstellar_radiation_field, my_rates->regr, &my_rates->gamma_isrf, &my_uvb_rates->comp_xray, &my_uvb_rates->temp_xray, + &my_uvb_rates->piHI, &my_uvb_rates->piHeI, &my_uvb_rates->piHeII, &comp1, &comp2, + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, + my_rates->hyd01k, my_rates->h2k01, my_rates->vibh, my_rates->roth, my_rates->rotl, + hyd01k.data(), h2k01.data(), vibh.data(), roth.data(), rotl.data(), + my_rates->GP99LowDensityLimit, my_rates->GP99HighDensityLimit, gpldl.data(), gphdl.data(), + my_rates->HDlte, my_rates->HDlow, hdlte.data(), hdlow.data(), + my_rates->GAHI, my_rates->GAH2, my_rates->GAHe, my_rates->GAHp, my_rates->GAel, + my_rates->H2LTE, my_rates->gas_grain, + ceHI.data(), ceHeI.data(), ceHeII.data(), ciHI.data(), ciHeI.data(), ciHeIS.data(), ciHeII.data(), + reHII.data(), reHeII1.data(), reHeII2.data(), reHeIII.data(), brem.data(), + indixe.data(), t1.data(), t2.data(), logtem.data(), tdef.data(), edot.data(), + tgas.data(), tgasold.data(), mmw.data(), p2d.data(), tdust.data(), metallicity.data(), + dust2gas.data(), rhoH.data(), mynh.data(), myde.data(), + gammaha_eff.data(), gasgr_tdust.data(), regr.data(), + &my_chemistry->self_shielding_method, &my_uvb_rates->crsHI, &my_uvb_rates->crsHeI, &my_uvb_rates->crsHeII, + &my_uvb_rates->k24, &my_uvb_rates->k26, + &my_chemistry->use_radiative_transfer, my_fields->RT_heating_rate, + &my_chemistry->h2_optical_depth_approximation, &my_chemistry->cie_cooling, &my_chemistry->h2_cooling_rate, &my_chemistry->hd_cooling_rate, my_rates->cieco, cieco.data(), + &my_chemistry->cmb_temperature_floor, &my_chemistry->UVbackground, &my_chemistry->cloudy_electron_fraction_factor, + &my_rates->cloudy_primordial.grid_rank, my_rates->cloudy_primordial.grid_dimension, + my_rates->cloudy_primordial.grid_parameters[0], my_rates->cloudy_primordial.grid_parameters[1], my_rates->cloudy_primordial.grid_parameters[2], my_rates->cloudy_primordial.grid_parameters[3], my_rates->cloudy_primordial.grid_parameters[4], + &my_rates->cloudy_primordial.data_size, my_rates->cloudy_primordial.cooling_data, my_rates->cloudy_primordial.heating_data, my_rates->cloudy_primordial.mmw_data, + &my_rates->cloudy_metal.grid_rank, my_rates->cloudy_metal.grid_dimension, + my_rates->cloudy_metal.grid_parameters[0], my_rates->cloudy_metal.grid_parameters[1], my_rates->cloudy_metal.grid_parameters[2], my_rates->cloudy_metal.grid_parameters[3], my_rates->cloudy_metal.grid_parameters[4], + &my_rates->cloudy_metal.data_size, my_rates->cloudy_metal.cooling_data, my_rates->cloudy_metal.heating_data, &my_rates->cloudy_data_new, + &my_chemistry->use_volumetric_heating_rate, &my_chemistry->use_specific_heating_rate, my_fields->volumetric_heating_rate, my_fields->specific_heating_rate, + &my_chemistry->use_temperature_floor, &my_chemistry->temperature_floor_scalar, my_fields->temperature_floor, + &my_chemistry->use_isrf_field, my_fields->isrf_habing, itmask.data(), itmask_metal.data(), + &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, &my_chemistry->use_primordial_continuum_opacity, &my_chemistry->tabulated_cooling_minimum_temperature, + my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, + my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, + my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, + my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, + my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, + my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, + my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, + my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, + my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, + my_rates->cieY06, + my_rates->LH2.props.dimension, &my_rates->LH2.props.data_size, + my_rates->LH2.props.parameters[0], my_rates->LH2.props.parameters[1], my_rates->LH2.props.parameters[2], + &my_rates->LH2.props.parameter_spacing[0], &my_rates->LH2.props.parameter_spacing[1], &my_rates->LH2.props.parameter_spacing[2], my_rates->LH2.data, + my_rates->LHD.props.dimension, &my_rates->LHD.props.data_size, + my_rates->LHD.props.parameters[0], my_rates->LHD.props.parameters[1], my_rates->LHD.props.parameters[2], + &my_rates->LHD.props.parameter_spacing[0], &my_rates->LHD.props.parameter_spacing[1], &my_rates->LHD.props.parameter_spacing[2], my_rates->LHD.data, + my_rates->LCI.props.dimension, &my_rates->LCI.props.data_size, + my_rates->LCI.props.parameters[0], my_rates->LCI.props.parameters[1], my_rates->LCI.props.parameters[2], + &my_rates->LCI.props.parameter_spacing[0], &my_rates->LCI.props.parameter_spacing[1], &my_rates->LCI.props.parameter_spacing[2], my_rates->LCI.data, + my_rates->LCII.props.dimension, &my_rates->LCII.props.data_size, + my_rates->LCII.props.parameters[0], my_rates->LCII.props.parameters[1], my_rates->LCII.props.parameters[2], + &my_rates->LCII.props.parameter_spacing[0], &my_rates->LCII.props.parameter_spacing[1], &my_rates->LCII.props.parameter_spacing[2], my_rates->LCII.data, + my_rates->LOI.props.dimension, &my_rates->LOI.props.data_size, + my_rates->LOI.props.parameters[0], my_rates->LOI.props.parameters[1], my_rates->LOI.props.parameters[2], + &my_rates->LOI.props.parameter_spacing[0], &my_rates->LOI.props.parameter_spacing[1], &my_rates->LOI.props.parameter_spacing[2], my_rates->LOI.data, + my_rates->LCO.props.dimension, &my_rates->LCO.props.data_size, + my_rates->LCO.props.parameters[0], my_rates->LCO.props.parameters[1], my_rates->LCO.props.parameters[2], + &my_rates->LCO.props.parameter_spacing[0], &my_rates->LCO.props.parameter_spacing[1], &my_rates->LCO.props.parameter_spacing[2], my_rates->LCO.data, + my_rates->LOH.props.dimension, &my_rates->LOH.props.data_size, + my_rates->LOH.props.parameters[0], my_rates->LOH.props.parameters[1], my_rates->LOH.props.parameters[2], + &my_rates->LOH.props.parameter_spacing[0], &my_rates->LOH.props.parameter_spacing[1], &my_rates->LOH.props.parameter_spacing[2], my_rates->LOH.data, + my_rates->LH2O.props.dimension, &my_rates->LH2O.props.data_size, + my_rates->LH2O.props.parameters[0], my_rates->LH2O.props.parameters[1], my_rates->LH2O.props.parameters[2], + &my_rates->LH2O.props.parameter_spacing[0], &my_rates->LH2O.props.parameter_spacing[1], &my_rates->LH2O.props.parameter_spacing[2], my_rates->LH2O.data, + my_rates->alphap.props.dimension, &my_rates->alphap.props.data_size, + my_rates->alphap.props.parameters[0], my_rates->alphap.props.parameters[1], &my_rates->alphap.props.parameter_spacing[0], &my_rates->alphap.props.parameter_spacing[1], + my_rates->alphap.data, + &my_chemistry->multi_metals, &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, + my_fields->local_ISM_metal_density, + my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, + my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, + my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density, + &my_rates->SN0_N, + my_rates->SN0_fSiM, my_rates->SN0_fFeM, my_rates->SN0_fMg2SiO4, my_rates->SN0_fMgSiO3, + my_rates->SN0_fFe3O4, my_rates->SN0_fAC, my_rates->SN0_fSiO2D, my_rates->SN0_fMgO, + my_rates->SN0_fFeS, my_rates->SN0_fAl2O3, + my_rates->SN0_freforg, my_rates->SN0_fvolorg, my_rates->SN0_fH2Oice, + my_rates->SN0_r0SiM, my_rates->SN0_r0FeM, my_rates->SN0_r0Mg2SiO4, my_rates->SN0_r0MgSiO3, + my_rates->SN0_r0Fe3O4, my_rates->SN0_r0AC, my_rates->SN0_r0SiO2D, my_rates->SN0_r0MgO, + my_rates->SN0_r0FeS, my_rates->SN0_r0Al2O3, + my_rates->SN0_r0reforg, my_rates->SN0_r0volorg, my_rates->SN0_r0H2Oice, + my_rates->gr_N, &my_rates->gr_Size, &my_rates->gr_dT, my_rates->gr_Td, + my_rates->SN0_kpSiM, my_rates->SN0_kpFeM, my_rates->SN0_kpMg2SiO4, my_rates->SN0_kpMgSiO3, + my_rates->SN0_kpFe3O4, my_rates->SN0_kpAC, my_rates->SN0_kpSiO2D, my_rates->SN0_kpMgO, + my_rates->SN0_kpFeS, my_rates->SN0_kpAl2O3, + my_rates->SN0_kpreforg, my_rates->SN0_kpvolorg, my_rates->SN0_kpH2Oice, + tSiM.data(), tFeM.data(), tMg2SiO4.data(), tMgSiO3.data(), tFe3O4.data(), + tAC.data(), tSiO2D.data(), tMgO.data(), tFeS.data(), tAl2O3.data(), + treforg.data(), tvolorg.data(), tH2Oice.data(), + my_rates->gas_grain2, &my_rates->gamma_isrf2 + ); + + if (my_chemistry->primordial_chemistry == 0) { + // This is some basic book-keeping to ensure that itmask_tmp has + // sensible values when ispecies is 0 + // -> There's room for optimization: when ispecies is 0, there is + // need to ever touch this variable. But, for now we focus on + // correct behavior before implementing this optimization + std::memcpy(itmask_tmp.data(), itmask.data(), sizeof(gr_mask_type)*my_fields->grid_dimension[0]); + + } else { + + // Look-up rates as a function of temperature for 1D set of zones + // (maybe should add itmask to this call) + + FORTRAN_NAME(lookup_cool_rates1d_g)(&my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->NumberOfTemperatureBins, &j, &k, + &my_fields->grid_start[0], &my_fields->grid_end[0], &my_chemistry->three_body_rate, + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, &anydust, + &my_chemistry->H2_self_shielding, &my_chemistry->self_shielding_method, + tgas.data(), mmw.data(), d.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, + tdust.data(), dust2gas.data(), + my_rates->k1, my_rates->k2, my_rates->k3, my_rates->k4, my_rates->k5, my_rates->k6, my_rates->k7, my_rates->k8, my_rates->k9, my_rates->k10, + my_rates->k11, my_rates->k12, my_rates->k13, my_rates->k13dd, my_rates->k14, my_rates->k15, my_rates->k16, + my_rates->k17, my_rates->k18, my_rates->k19, my_rates->k22, + my_rates->k50, my_rates->k51, my_rates->k52, my_rates->k53, my_rates->k54, my_rates->k55, my_rates->k56, + my_rates->k57, my_rates->k58, &my_chemistry->NumberOfDustTemperatureBins, &my_chemistry->DustTemperatureStart, &my_chemistry->DustTemperatureEnd, my_rates->h2dust, + my_rates->n_cr_n, my_rates->n_cr_d1, my_rates->n_cr_d2, + &my_uvb_rates->crsHI, &my_uvb_rates->crsHeI, &my_uvb_rates->crsHeII, &my_uvb_rates->piHI, &my_uvb_rates->piHeI, + k1.data(), k2.data(), k3.data(), k4.data(), k5.data(), k6.data(), k7.data(), k8.data(), k9.data(), k10.data(), + k11.data(), k12.data(), k13.data(), k14.data(), k15.data(), k16.data(), k17.data(), k18.data(), + k19.data(), k22.data(), &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k28, &my_uvb_rates->k29, &my_uvb_rates->k30, &my_uvb_rates->k31, + k50.data(), k51.data(), k52.data(), k53.data(), k54.data(), k55.data(), k56.data(), k57.data(), + k58.data(), k13dd.data(), k24shield.data(), k25shield.data(), k26shield.data(), + k28shield.data(), k29shield.data(), k30shield.data(), + k31shield.data(), h2dust.data(), ncrn.data(), ncrd1.data(), ncrd2.data(), + t1.data(), t2.data(), tdef.data(), logtem.data(), indixe.data(), + &dom, &coolunit, &tbase1, &xbase1, &dx_cgs, &c_ljeans, + &my_chemistry->use_radiative_transfer, my_fields->RT_H2_dissociation_rate, my_fields->H2_self_shielding_length, itmask.data(), + itmask_metal.data(), + &my_chemistry->HydrogenFractionByMass, my_fields->metal_density, + my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, imetal, &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, + my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, + my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, + my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, + my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, + my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, + my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, + my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, + my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, + my_rates->k125, my_rates->k129, my_rates->k130, my_rates->k131, my_rates->k132, + my_rates->k133, my_rates->k134, my_rates->k135, my_rates->k136, my_rates->k137, + my_rates->k148, my_rates->k149, my_rates->k150, my_rates->k151, my_rates->k152, + my_rates->k153, + my_rates->kz15, my_rates->kz16, my_rates->kz17, my_rates->kz18, my_rates->kz19, + my_rates->kz20, my_rates->kz21, my_rates->kz22, my_rates->kz23, my_rates->kz24, + my_rates->kz25, my_rates->kz26, my_rates->kz27, my_rates->kz28, my_rates->kz29, + my_rates->kz30, my_rates->kz31, my_rates->kz32, my_rates->kz33, my_rates->kz34, + my_rates->kz35, my_rates->kz36, my_rates->kz37, my_rates->kz38, my_rates->kz39, + my_rates->kz40, my_rates->kz41, my_rates->kz42, my_rates->kz43, my_rates->kz44, + my_rates->kz45, my_rates->kz46, my_rates->kz47, my_rates->kz48, my_rates->kz49, + my_rates->kz50, my_rates->kz51, my_rates->kz52, my_rates->kz53, my_rates->kz54, + k125.data(), k129.data(), k130.data(), k131.data(), k132.data(), + k133.data(), k134.data(), k135.data(), k136.data(), k137.data(), + k148.data(), k149.data(), k150.data(), k151.data(), k152.data(), + k153.data(), + kz15.data(), kz16.data(), kz17.data(), kz18.data(), kz19.data(), + kz20.data(), kz21.data(), kz22.data(), kz23.data(), kz24.data(), + kz25.data(), kz26.data(), kz27.data(), kz28.data(), kz29.data(), + kz30.data(), kz31.data(), kz32.data(), kz33.data(), kz34.data(), + kz35.data(), kz36.data(), kz37.data(), kz38.data(), kz39.data(), + kz40.data(), kz41.data(), kz42.data(), kz43.data(), kz44.data(), + kz45.data(), kz46.data(), kz47.data(), kz48.data(), kz49.data(), + kz50.data(), kz51.data(), kz52.data(), kz53.data(), kz54.data(), + &my_chemistry->multi_metals, &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, + my_fields->local_ISM_metal_density, + my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, + my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, + my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density, + &my_rates->SN0_N, + my_rates->SN0_fSiM, my_rates->SN0_fFeM, my_rates->SN0_fMg2SiO4, my_rates->SN0_fMgSiO3, + my_rates->SN0_fFe3O4, my_rates->SN0_fAC, my_rates->SN0_fSiO2D, my_rates->SN0_fMgO, + my_rates->SN0_fFeS, my_rates->SN0_fAl2O3, + my_rates->SN0_freforg, my_rates->SN0_fvolorg, my_rates->SN0_fH2Oice, + my_rates->SN0_r0SiM, my_rates->SN0_r0FeM, my_rates->SN0_r0Mg2SiO4, my_rates->SN0_r0MgSiO3, + my_rates->SN0_r0Fe3O4, my_rates->SN0_r0AC, my_rates->SN0_r0SiO2D, my_rates->SN0_r0MgO, + my_rates->SN0_r0FeS, my_rates->SN0_r0Al2O3, + my_rates->SN0_r0reforg, my_rates->SN0_r0volorg, my_rates->SN0_r0H2Oice, + my_rates->gr_N, &my_rates->gr_Size, &my_rates->gr_dT, my_rates->gr_Td, + my_rates->SN0_kpSiM, my_rates->SN0_kpFeM, my_rates->SN0_kpMg2SiO4, my_rates->SN0_kpMgSiO3, + my_rates->SN0_kpFe3O4, my_rates->SN0_kpAC, my_rates->SN0_kpSiO2D, my_rates->SN0_kpMgO, + my_rates->SN0_kpFeS, my_rates->SN0_kpAl2O3, + my_rates->SN0_kpreforg, my_rates->SN0_kpvolorg, my_rates->SN0_kpH2Oice, + my_rates->h2dustS, my_rates->h2dustC, rhoH.data(), my_rates->grain_growth_rate, dt, + kdSiM.data(), kdFeM.data(), kdMg2SiO4.data(), + kdMgSiO3.data(), kdFe3O4.data(), kdAC.data(), kdSiO2D.data(), kdMgO.data(), kdFeS.data(), + kdAl2O3.data(), kdreforg.data(), kdvolorg.data(), kdH2Oice.data(), + tSiM.data(), tFeM.data(), tMg2SiO4.data(), tMgSiO3.data(), tFe3O4.data(), + tAC.data(), tSiO2D.data(), tMgO.data(), tFeS.data(), tAl2O3.data(), + treforg.data(), tvolorg.data(), tH2Oice.data(), &my_chemistry->radiative_transfer_use_H2_shielding, + &my_chemistry->H2_custom_shielding, my_fields->H2_custom_shielding_factor + ); + + // Compute dedot and HIdot, the rates of change of de and HI + // (should add itmask to this call) + + FORTRAN_NAME(rate_timestep_g)( + dedot.data(), HIdot.data(), &my_chemistry->primordial_chemistry, &anydust, + de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, d.data(), + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, + k1.data(), k2.data(), k3.data(), k4.data(), k5.data(), k6.data(), k7.data(), k8.data(), k9.data(), k10.data(), k11.data(), + k12.data(), k13.data(), k14.data(), k15.data(), k16.data(), k17.data(), k18.data(), k19.data(), k22.data(), + &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k27, &my_uvb_rates->k28, &my_uvb_rates->k29, &my_uvb_rates->k30, + k50.data(), k51.data(), k52.data(), k53.data(), k54.data(), k55.data(), k56.data(), k57.data(), k58.data(), + h2dust.data(), ncrn.data(), ncrd1.data(), ncrd2.data(), rhoH.data(), + k24shield.data(), k25shield.data(), k26shield.data(), + k28shield.data(), k29shield.data(), k30shield.data(), k31shield.data(), + &my_chemistry->use_radiative_transfer, &my_chemistry->radiative_transfer_hydrogen_only, + kphHI.data(), my_fields->RT_HeI_ionization_rate, my_fields->RT_HeII_ionization_rate, + itmask.data(), edot.data(), &chunit, &dom, my_fields->metal_density, + my_fields->HDI_density, &my_chemistry->metal_chemistry, my_fields->CI_density, my_fields->OI_density, my_fields->OH_density, my_fields->CO_density, my_fields->H2O_density, + &my_chemistry->radiative_transfer_HDI_dissociation, my_fields->RT_HDI_dissociation_rate, &my_chemistry->radiative_transfer_metal_ionization, my_fields->RT_CI_ionization_rate, my_fields->RT_OI_ionization_rate, + &my_chemistry->radiative_transfer_metal_dissociation, my_fields->RT_CO_dissociation_rate, my_fields->RT_OH_dissociation_rate, my_fields->RT_H2O_dissociation_rate + ); + + // move itmask temporary array + // then split cells with low densities + // => Gauss-Seidel scheme + // and with high densities + // => Newton-Raphson scheme + + std::memcpy(itmask_tmp.data(), itmask.data(), sizeof(gr_mask_type)*my_fields->grid_dimension[0]); + std::memcpy(itmask_nr.data(), itmask.data(), sizeof(gr_mask_type)*my_fields->grid_dimension[0]); + for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + if ( itmask_tmp[i-1] != MASK_FALSE ) { + + if ( ( ((*imetal) == 0) + && (ddom[i-1] < 1.e8) ) + || ( ((*imetal) == 1) + && ( ( (metallicity[i-1] <= min_metallicity) + && (ddom[i-1] < 1.e8) ) + || ( (metallicity[i-1] > min_metallicity) + && (ddom[i-1] < 1.e6) ) ) ) ) { + itmask_nr[i-1] = MASK_FALSE; + } else { + itmask[i-1] = MASK_FALSE; + } + + } + } + + for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + if (itmask_nr[i-1] != MASK_FALSE) { + if ( (my_chemistry->with_radiative_cooling == 1) && (my_chemistry->primordial_chemistry > 1) && + ((ddom[i-1] > 1.e7) + && (tgas[i-1] > 1650.e0)) ) { + imp_eng[i-1] = 1; + } else { + imp_eng[i-1] = 0; + } + } + } + + // Find timestep that keeps relative chemical changes below 10% + + for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + if (itmask[i-1] != MASK_FALSE) { + // Bound from below to prevent numerical errors + + if (std::fabs(dedot[i-1]) < tiny8) + { dedot[i-1] = std::fmin(tiny_fortran_val,de(i-1,j-1,k-1)); } + if (std::fabs(HIdot[i-1]) < tiny8) + { HIdot[i-1] = std::fmin(tiny_fortran_val,HI(i-1,j-1,k-1)); } + + // If the net rate is almost perfectly balanced then set + // it to zero (since it is zero to available precision) + + if (std::fmin(std::fabs(k1[i-1]* de(i-1,j-1,k-1)*HI(i-1,j-1,k-1)), + std::fabs(k2[i-1]*HII(i-1,j-1,k-1)*de(i-1,j-1,k-1)))/ + std::fmax(std::fabs(dedot[i-1]),std::fabs(HIdot[i-1])) > + 1.0e6) { + dedot[i-1] = tiny8; + HIdot[i-1] = tiny8; + } + + // If the iteration count is high then take the smaller of + // the calculated dedot and last time step's actual dedot. + // This is intended to get around the problem of a low + // electron or HI fraction which is in equilibrium with high + // individual terms (which all nearly cancel). + + if (iter > 50) { + dedot[i-1] = std::fmin(std::fabs(dedot[i-1]), std::fabs(dedot_prev[i-1])); + HIdot[i-1] = std::fmin(std::fabs(HIdot[i-1]), std::fabs(HIdot_prev[i-1])); + } + + // compute minimum rate timestep + + olddtit = dtit[i-1]; + dtit[i-1] = grackle::impl::fmin(std::fabs(0.1*de(i-1,j-1,k-1)/dedot[i-1]), + std::fabs(0.1*HI(i-1,j-1,k-1)/HIdot[i-1]), + (*dt)-ttot[i-1], 0.5*(*dt)); + + if (ddom[i-1] > 1.e8 && + edot[i-1] > 0. && + my_chemistry->primordial_chemistry > 1) { + // Equilibrium value for H is: + // H = (-1._DKIND / (4*k22)) * (k13 - sqrt(8 k13 k22 rho + k13^2)) + // We now want this to change by 10% or less, but we're only + // differentiating by dT. We have de/dt. We need dT/de. + // T = (g-1)*p2d*utem/N; tgas == (g-1)(p2d*utem/N) + // dH_eq / dt = (dH_eq/dT) * (dT/de) * (de/dt) + // dH_eq / dT (see above; we can calculate the derivative here) + // dT / de = utem * (gamma - 1._DKIND) / N == tgas / p2d + // de / dt = edot + // Now we use our estimate of dT/de to get the estimated + // difference in the equilibrium + eqt2 = std::fmin(std::log(tgas[i-1]) + 0.1*dlogtem, t2[i-1]); + eqtdef = (eqt2 - t1[i-1])/(t2[i-1] - t1[i-1]); + eqk222 = my_rates->k22[indixe[i-1]-1] + + (my_rates->k22[indixe[i-1]+1-1] -my_rates->k22[indixe[i-1]-1])*eqtdef; + eqk132 = my_rates->k13[indixe[i-1]-1] + + (my_rates->k13[indixe[i-1]+1-1] -my_rates->k13[indixe[i-1]-1])*eqtdef; + heq2 = (-1. / (4.*eqk222)) * (eqk132- + std::sqrt(8.*eqk132*eqk222* + my_chemistry->HydrogenFractionByMass*d(i-1,j-1,k-1)+std::pow(eqk132,2.))); + + eqt1 = std::fmax(std::log(tgas[i-1]) - 0.1*dlogtem, t1[i-1]); + eqtdef = (eqt1 - t1[i-1])/(t2[i-1] - t1[i-1]); + eqk221 = my_rates->k22[indixe[i-1]-1] + + (my_rates->k22[indixe[i-1]+1-1] -my_rates->k22[indixe[i-1]-1])*eqtdef; + eqk131 = my_rates->k13[indixe[i-1]-1] + + (my_rates->k13[indixe[i-1]+1-1] -my_rates->k13[indixe[i-1]-1])*eqtdef; + heq1 = (-1. / (4.*eqk221)) * (eqk131- + std::sqrt(8.*eqk131*eqk221* + my_chemistry->HydrogenFractionByMass*d(i-1,j-1,k-1)+std::pow(eqk131,2.))); + + dheq = (std::fabs(heq2-heq1)/(std::exp(eqt2) - std::exp(eqt1))) + * (tgas[i-1]/p2d[i-1]) * edot[i-1]; + heq = (-1. / (4.*k22[i-1])) * (k13[i-1]- + std::sqrt(8.*k13[i-1]*k22[i-1]* + my_chemistry->HydrogenFractionByMass*d(i-1,j-1,k-1)+std::pow(k13[i-1],2.))); + dtit[i-1] = std::fmin(dtit[i-1], 0.1*heq/dheq); + } + if (iter>10LL) { + dtit[i-1] = std::fmin(olddtit*1.5, dtit[i-1]); + } + + } else if ((itmask_nr[i-1]!=MASK_FALSE) && + (imp_eng[i-1]==0)) { + dtit[i-1] = grackle::impl::fmin(std::fabs(0.1*e(i-1,j-1,k-1)/edot[i-1]*d(i-1,j-1,k-1)), + (*dt)-ttot[i-1], 0.5*(*dt)); + + } else if ((itmask_nr[i-1]!=MASK_FALSE) && + (imp_eng[i-1]==1)) { + dtit[i-1] = (*dt) - ttot[i-1]; + + } else { + dtit[i-1] = (*dt); + } + } + + } + + // Compute maximum timestep for cooling/heating + + for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + if (itmask[i-1] != MASK_FALSE) { + // Set energy per unit volume of this cell based in the pressure + // (the gamma used here is the right one even for H2 since p2d + // is calculated with this gamma). + + energy = std::fmax(p2d[i-1]/(my_chemistry->Gamma-1.), tiny8); + + // If the temperature is at the bottom of the temperature look-up + // table and edot < 0, then shut off the cooling. + + if (tgas[i-1] <= 1.01*my_chemistry->TemperatureStart && + edot[i-1] < 0.) + { edot[i-1] = tiny8; } + if (std::fabs(edot[i-1]) < tiny8) { edot[i-1] = tiny8; } + + // Compute timestep for 10% change + + dtit[i-1] = grackle::impl::fmin((double)(std::fabs(0.1* + energy/edot[i-1]) ), + (*dt)-ttot[i-1], dtit[i-1]); + + if (dtit[i-1] != dtit[i-1]) { + OMP_PRAGMA_CRITICAL + { + eprintf("HUGE dtit :: %g %g %g %g %g %g %g\n", + energy, + edot [ i-1 ], + dtit [ i-1 ], + (*dt), + ttot [ i-1 ], + std::fabs ( 0.1 * energy / edot [ i-1 ] ), + (double) ( std::fabs ( 0.1 * energy / edot [ i-1 ] ) )); + } + } + + } + } + + // Update total and gas energy + + if (my_chemistry->with_radiative_cooling == 1) { + for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + if (itmask[i-1] != MASK_FALSE) { + e(i-1,j-1,k-1) = e(i-1,j-1,k-1) + + (gr_float)(edot[i-1]/d(i-1,j-1,k-1)*dtit[i-1] ); + + } + } + } + + if (my_chemistry->primordial_chemistry > 0) { + + // Solve rate equations with one linearly implicit Gauss-Seidel + // sweep of a backward Euler method (for all cells specified by + // itmask) + + FORTRAN_NAME(step_rate_g)(de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, d.data(), + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, dtit.data(), + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, + &my_chemistry->primordial_chemistry, &anydust, + k1.data(), k2.data(), k3.data(), k4.data(), k5.data(), k6.data(), k7.data(), k8.data(), k9.data(), k10.data(), k11.data(), + k12.data(), k13.data(), k14.data(), k15.data(), k16.data(), k17.data(), k18.data(), k19.data(), k22.data(), + &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k27, &my_uvb_rates->k28, &my_uvb_rates->k29, &my_uvb_rates->k30, + k50.data(), k51.data(), k52.data(), k53.data(), k54.data(), k55.data(), k56.data(), k57.data(), k58.data(), + h2dust.data(), rhoH.data(), + k24shield.data(), k25shield.data(), k26shield.data(), + k28shield.data(), k29shield.data(), k30shield.data(), k31shield.data(), + HIp.data(), HIIp.data(), HeIp.data(), HeIIp.data(), HeIIIp.data(), dep.data(), + HMp.data(), H2Ip.data(), H2IIp.data(), DIp.data(), DIIp.data(), HDIp.data(), + dedot_prev.data(), HIdot_prev.data(), + &my_chemistry->use_radiative_transfer, &my_chemistry->radiative_transfer_hydrogen_only, + kphHI.data(), my_fields->RT_HeI_ionization_rate, my_fields->RT_HeII_ionization_rate, + itmask.data(), itmask_metal.data(), + my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, imetal, my_fields->metal_density, + &my_chemistry->metal_chemistry, &my_chemistry->dust_species, &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, + my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, + my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, + my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, + my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, + my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, + my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, + my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, + my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, + k125.data(), k129.data(), k130.data(), k131.data(), k132.data(), + k133.data(), k134.data(), k135.data(), k136.data(), k137.data(), + k148.data(), k149.data(), k150.data(), k151.data(), k152.data(), + k153.data(), + kz15.data(), kz16.data(), kz17.data(), kz18.data(), kz19.data(), + kz20.data(), kz21.data(), kz22.data(), kz23.data(), kz24.data(), + kz25.data(), kz26.data(), kz27.data(), kz28.data(), kz29.data(), + kz30.data(), kz31.data(), kz32.data(), kz33.data(), kz34.data(), + kz35.data(), kz36.data(), kz37.data(), kz38.data(), kz39.data(), + kz40.data(), kz41.data(), kz42.data(), kz43.data(), kz44.data(), + kz45.data(), kz46.data(), kz47.data(), kz48.data(), kz49.data(), + kz50.data(), kz51.data(), kz52.data(), kz53.data(), kz54.data(), + DMp.data(), HDIIp.data(), HeHIIp.data(), + CIp.data(), CIIp.data(), COp.data(), CO2p.data(), + OIp.data(), OHp.data(), H2Op.data(), O2p.data(), + SiIp.data(), SiOIp.data(), SiO2Ip.data(), + CHp.data(), CH2p.data(), COIIp.data(), OIIp.data(), + OHIIp.data(), H2OIIp.data(), H3OIIp.data(), O2IIp.data(), + Mgp.data(), Alp.data(), Sp.data(), Fep.data(), + SiMp.data(), FeMp.data(), Mg2SiO4p.data(), MgSiO3p.data(), Fe3O4p.data(), + ACp.data(), SiO2Dp.data(), MgOp.data(), FeSp.data(), Al2O3p.data(), + reforgp.data(), volorgp.data(), H2Oicep.data(), + kdSiM.data(), kdFeM.data(), kdMg2SiO4.data(), kdMgSiO3.data(), kdFe3O4.data(), + kdAC.data(), kdSiO2D.data(), kdMgO.data(), kdFeS.data(), kdAl2O3.data(), + kdreforg.data(), kdvolorg.data(), kdH2Oice.data(), + &my_chemistry->radiative_transfer_HDI_dissociation, my_fields->RT_HDI_dissociation_rate, &my_chemistry->radiative_transfer_metal_ionization, my_fields->RT_CI_ionization_rate, my_fields->RT_OI_ionization_rate, + &my_chemistry->radiative_transfer_metal_dissociation, my_fields->RT_CO_dissociation_rate, my_fields->RT_OH_dissociation_rate, my_fields->RT_H2O_dissociation_rate + ); + + + // Solve rate equations with one linearly implicit Gauss-Seidel + // sweep of a backward Euler method (for all cells specified by + // itmask_nr) + + FORTRAN_NAME(step_rate_newton_raphson)(&my_chemistry->with_radiative_cooling, d.data(), e.data(), my_fields->x_velocity, my_fields->y_velocity, my_fields->z_velocity, de.data(), HI.data(), + HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, + &my_units->comoving_coordinates, &my_chemistry->primordial_chemistry, imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, + &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_fields->grid_start[0], &my_fields->grid_end[0], &my_chemistry->ih2co, &my_chemistry->ipiht, + &my_chemistry->dust_recombination_cooling, &my_chemistry->photoelectric_heating, &my_units->a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, utem, + uxyz, &my_units->a_units, urho, &my_units->time_units, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, + my_rates->k1, my_rates->k2, my_rates->k3, my_rates->k4, my_rates->k5, my_rates->k6, my_rates->k7, my_rates->k8, my_rates->k9, + my_rates->k10, my_rates->k11, my_rates->k12, my_rates->k13, my_rates->k13dd, my_rates->k14, my_rates->k15, my_rates->k16, + my_rates->k17, my_rates->k18, my_rates->k19, my_rates->k22, &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k27, &my_uvb_rates->k28, + &my_uvb_rates->k29, &my_uvb_rates->k30, &my_uvb_rates->k31, my_rates->k50, my_rates->k51, my_rates->k52, my_rates->k53, my_rates->k54, + my_rates->k55, my_rates->k56, my_rates->k57, my_rates->k58, &my_chemistry->NumberOfDustTemperatureBins, &my_chemistry->DustTemperatureStart, + &my_chemistry->DustTemperatureEnd, my_rates->h2dust, my_rates->n_cr_n, my_rates->n_cr_d1, my_rates->n_cr_d2, my_rates->ceHI, + my_rates->ceHeI, my_rates->ceHeII, my_rates->ciHI, my_rates->ciHeI, my_rates->ciHeIS, my_rates->ciHeII, + my_rates->reHII, my_rates->reHeII1, my_rates->reHeII2, my_rates->reHeIII, my_rates->brem, + &my_rates->comp, &my_rates->gammah, &my_chemistry->interstellar_radiation_field, my_rates->regr, &my_rates->gamma_isrf, + &my_uvb_rates->comp_xray, &my_uvb_rates->temp_xray, &my_uvb_rates->piHI, &my_uvb_rates->piHeI, &my_uvb_rates->piHeII, my_fields->HM_density, + my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, my_rates->hyd01k, + my_rates->h2k01, my_rates->vibh, my_rates->roth, my_rates->rotl, my_rates->GP99LowDensityLimit, my_rates->GP99HighDensityLimit, + my_rates->HDlte, my_rates->HDlow, my_rates->GAHI, my_rates->GAH2, my_rates->GAHe, my_rates->GAHp, + my_rates->GAel, my_rates->H2LTE, my_rates->gas_grain, &my_chemistry->H2_self_shielding, &my_chemistry->self_shielding_method, + &my_uvb_rates->crsHI, &my_uvb_rates->crsHeI, &my_uvb_rates->crsHeII, &my_chemistry->use_radiative_transfer, + &my_chemistry->radiative_transfer_hydrogen_only, kphHI.data(), my_fields->RT_HeI_ionization_rate, my_fields->RT_HeII_ionization_rate, my_fields->RT_H2_dissociation_rate, + my_fields->RT_heating_rate, my_fields->H2_self_shielding_length, &my_chemistry->h2_optical_depth_approximation, &my_chemistry->cie_cooling, + &my_chemistry->three_body_rate, &my_chemistry->h2_cooling_rate, &my_chemistry->hd_cooling_rate, my_rates->cieco, &my_chemistry->cmb_temperature_floor, + &my_chemistry->UVbackground, &my_chemistry->cloudy_electron_fraction_factor, &my_rates->cloudy_primordial.grid_rank, my_rates->cloudy_primordial.grid_dimension, + my_rates->cloudy_primordial.grid_parameters[0], my_rates->cloudy_primordial.grid_parameters[1], my_rates->cloudy_primordial.grid_parameters[2], my_rates->cloudy_primordial.grid_parameters[3], my_rates->cloudy_primordial.grid_parameters[4], + &my_rates->cloudy_primordial.data_size, my_rates->cloudy_primordial.cooling_data, my_rates->cloudy_primordial.heating_data, my_rates->cloudy_primordial.mmw_data, + &my_rates->cloudy_metal.grid_rank, my_rates->cloudy_metal.grid_dimension, my_rates->cloudy_metal.grid_parameters[0], my_rates->cloudy_metal.grid_parameters[1], + my_rates->cloudy_metal.grid_parameters[2], my_rates->cloudy_metal.grid_parameters[3], my_rates->cloudy_metal.grid_parameters[4], &my_rates->cloudy_metal.data_size, + my_rates->cloudy_metal.cooling_data, my_rates->cloudy_metal.heating_data, &my_rates->cloudy_data_new, &my_chemistry->use_volumetric_heating_rate, &my_chemistry->use_specific_heating_rate, + my_fields->volumetric_heating_rate, my_fields->specific_heating_rate, &my_chemistry->use_temperature_floor, &my_chemistry->temperature_floor_scalar, my_fields->temperature_floor, + &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, &my_chemistry->use_primordial_continuum_opacity, &my_chemistry->tabulated_cooling_minimum_temperature, my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, my_fields->SiI_density, my_fields->SiOI_density, + my_fields->SiO2I_density, my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, + my_fields->O2II_density, my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, + my_fields->Fe3O4_dust_density, my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, my_fields->ref_org_dust_density, + my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, my_rates->k125, my_rates->k129, my_rates->k130, my_rates->k131, + my_rates->k132, my_rates->k133, my_rates->k134, my_rates->k135, my_rates->k136, my_rates->k137, my_rates->k148, + my_rates->k149, my_rates->k150, my_rates->k151, my_rates->k152, my_rates->k153, my_rates->kz15, my_rates->kz16, + my_rates->kz17, my_rates->kz18, my_rates->kz19, my_rates->kz20, my_rates->kz21, my_rates->kz22, my_rates->kz23, + my_rates->kz24, my_rates->kz25, my_rates->kz26, my_rates->kz27, my_rates->kz28, my_rates->kz29, my_rates->kz30, + my_rates->kz31, my_rates->kz32, my_rates->kz33, my_rates->kz34, my_rates->kz35, my_rates->kz36, my_rates->kz37, + my_rates->kz38, my_rates->kz39, my_rates->kz40, my_rates->kz41, my_rates->kz42, my_rates->kz43, my_rates->kz44, + my_rates->kz45, my_rates->kz46, my_rates->kz47, my_rates->kz48, my_rates->kz49, my_rates->kz50, my_rates->kz51, + my_rates->kz52, my_rates->kz53, my_rates->kz54, my_rates->cieY06, my_rates->LH2.props.dimension, &my_rates->LH2.props.data_size, + my_rates->LH2.props.parameters[0], my_rates->LH2.props.parameters[1], my_rates->LH2.props.parameters[2], &my_rates->LH2.props.parameter_spacing[0], &my_rates->LH2.props.parameter_spacing[1], &my_rates->LH2.props.parameter_spacing[2], + my_rates->LH2.data, my_rates->LHD.props.dimension, &my_rates->LHD.props.data_size, my_rates->LHD.props.parameters[0], my_rates->LHD.props.parameters[1], my_rates->LHD.props.parameters[2], + &my_rates->LHD.props.parameter_spacing[0], &my_rates->LHD.props.parameter_spacing[1], &my_rates->LHD.props.parameter_spacing[2], my_rates->LHD.data, my_rates->LCI.props.dimension, &my_rates->LCI.props.data_size, + my_rates->LCI.props.parameters[0], my_rates->LCI.props.parameters[1], my_rates->LCI.props.parameters[2], &my_rates->LCI.props.parameter_spacing[0], &my_rates->LCI.props.parameter_spacing[1], &my_rates->LCI.props.parameter_spacing[2], + my_rates->LCI.data, my_rates->LCII.props.dimension, &my_rates->LCII.props.data_size, my_rates->LCII.props.parameters[0], my_rates->LCII.props.parameters[1], my_rates->LCII.props.parameters[2], + &my_rates->LCII.props.parameter_spacing[0], &my_rates->LCII.props.parameter_spacing[1], &my_rates->LCII.props.parameter_spacing[2], my_rates->LCII.data, my_rates->LOI.props.dimension, + &my_rates->LOI.props.data_size, my_rates->LOI.props.parameters[0], my_rates->LOI.props.parameters[1], my_rates->LOI.props.parameters[2], &my_rates->LOI.props.parameter_spacing[0], &my_rates->LOI.props.parameter_spacing[1], + &my_rates->LOI.props.parameter_spacing[2], my_rates->LOI.data, my_rates->LCO.props.dimension, &my_rates->LCO.props.data_size, my_rates->LCO.props.parameters[0], my_rates->LCO.props.parameters[1], + my_rates->LCO.props.parameters[2], &my_rates->LCO.props.parameter_spacing[0], &my_rates->LCO.props.parameter_spacing[1], &my_rates->LCO.props.parameter_spacing[2], my_rates->LCO.data, my_rates->LOH.props.dimension, + &my_rates->LOH.props.data_size, my_rates->LOH.props.parameters[0], my_rates->LOH.props.parameters[1], my_rates->LOH.props.parameters[2], &my_rates->LOH.props.parameter_spacing[0], &my_rates->LOH.props.parameter_spacing[1], + &my_rates->LOH.props.parameter_spacing[2], my_rates->LOH.data, my_rates->LH2O.props.dimension, &my_rates->LH2O.props.data_size, my_rates->LH2O.props.parameters[0], my_rates->LH2O.props.parameters[1], + my_rates->LH2O.props.parameters[2], &my_rates->LH2O.props.parameter_spacing[0], &my_rates->LH2O.props.parameter_spacing[1], &my_rates->LH2O.props.parameter_spacing[2], my_rates->LH2O.data, + my_rates->alphap.props.dimension, &my_rates->alphap.props.data_size, my_rates->alphap.props.parameters[0], my_rates->alphap.props.parameters[1], + &my_rates->alphap.props.parameter_spacing[0], &my_rates->alphap.props.parameter_spacing[1], my_rates->alphap.data, &my_chemistry->multi_metals, + &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, my_fields->local_ISM_metal_density, + my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, + my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, + my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density, &my_rates->SN0_N, + my_rates->SN0_fSiM, my_rates->SN0_fFeM, my_rates->SN0_fMg2SiO4, my_rates->SN0_fMgSiO3, + my_rates->SN0_fFe3O4, my_rates->SN0_fAC, my_rates->SN0_fSiO2D, my_rates->SN0_fMgO, + my_rates->SN0_fFeS, my_rates->SN0_fAl2O3, my_rates->SN0_freforg, my_rates->SN0_fvolorg, + my_rates->SN0_fH2Oice, my_rates->SN0_r0SiM, my_rates->SN0_r0FeM, my_rates->SN0_r0Mg2SiO4, + my_rates->SN0_r0MgSiO3, my_rates->SN0_r0Fe3O4, my_rates->SN0_r0AC, my_rates->SN0_r0SiO2D, + my_rates->SN0_r0MgO, my_rates->SN0_r0FeS, my_rates->SN0_r0Al2O3, my_rates->SN0_r0reforg, + my_rates->SN0_r0volorg, my_rates->SN0_r0H2Oice, my_rates->gr_N, &my_rates->gr_Size, &my_rates->gr_dT, + my_rates->gr_Td, my_rates->SN0_kpSiM, my_rates->SN0_kpFeM, my_rates->SN0_kpMg2SiO4, + my_rates->SN0_kpMgSiO3, my_rates->SN0_kpFe3O4, my_rates->SN0_kpAC, my_rates->SN0_kpSiO2D, + my_rates->SN0_kpMgO, my_rates->SN0_kpFeS, my_rates->SN0_kpAl2O3, my_rates->SN0_kpreforg, + my_rates->SN0_kpvolorg, my_rates->SN0_kpH2Oice, my_rates->h2dustS, my_rates->h2dustC, + my_rates->gas_grain2, &my_rates->gamma_isrf2, my_rates->grain_growth_rate, &my_chemistry->radiative_transfer_HDI_dissociation, + my_fields->RT_HDI_dissociation_rate, &my_chemistry->radiative_transfer_metal_ionization, my_fields->RT_CI_ionization_rate, my_fields->RT_OI_ionization_rate, &my_chemistry->radiative_transfer_metal_dissociation, my_fields->RT_CO_dissociation_rate, + my_fields->RT_OH_dissociation_rate, my_fields->RT_H2O_dissociation_rate, &my_chemistry->radiative_transfer_use_H2_shielding, &my_chemistry->use_isrf_field, + my_fields->isrf_habing, &my_chemistry->H2_custom_shielding, my_fields->H2_custom_shielding_factor, + &ierror, &j, &k, &iter, &dom, &comp1, + &comp2, &coolunit, &tbase1, &xbase1, &chunit, &dx_cgs, + &c_ljeans, indixe.data(), t1.data(), t2.data(), logtem.data(), tdef.data(), dtit.data(), + p2d.data(), tgas.data(), tgasold.data(), tdust.data(), metallicity.data(), dust2gas.data(), + rhoH.data(), mmw.data(), mynh.data(), myde.data(), gammaha_eff.data(), gasgr_tdust.data(), + regr.data(), h2dust.data(), ncrn.data(), ncrd1.data(), ncrd2.data(), tSiM.data(), tFeM.data(), + tMg2SiO4.data(), tMgSiO3.data(), tFe3O4.data(), tAC.data(), tSiO2D.data(), tMgO.data(), + tFeS.data(), tAl2O3.data(), treforg.data(), tvolorg.data(), tH2Oice.data(), ceHI.data(), + ceHeI.data(), ceHeII.data(), ciHI.data(), ciHeI.data(), ciHeIS.data(), ciHeII.data(), + reHII.data(), reHeII1.data(), reHeII2.data(), reHeIII.data(), brem.data(), edot.data(), + hyd01k.data(), h2k01.data(), vibh.data(), roth.data(), rotl.data(), gpldl.data(), gphdl.data(), + hdlte.data(), hdlow.data(), cieco.data(), &anydust, itmask_nr.data(), + itmask_metal.data(), &itr, imp_eng.data() + ); + + } + + // return itmask + for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + itmask[i-1] = itmask_tmp[i-1]; + } + + // Add the timestep to the elapsed time for each cell and find + // minimum elapsed time step in this row + + ttmin = huge8; + for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + ttot[i-1] = std::fmin(ttot[i-1] + dtit[i-1], (*dt)); + if (std::fabs((*dt)-ttot[i-1]) < + tolerance*(*dt)) { itmask[i-1] = MASK_FALSE; } + if (ttot[i-1] my_chemistry->max_iterations) { + OMP_PRAGMA_CRITICAL + { + printf("inside if statement solve rate cool: %d %d\n", + my_fields->grid_start[0], + my_fields->grid_end[0]); + eprintf("MULTI_COOL iter > %d at j,k = %d %d\n", + my_chemistry->max_iterations, + j, + k); + printf("FATAL error (2) in MULTI_COOL\n"); + //_// PORT: write(0,'(" dt = ",1pe10.3," ttmin = ",1pe10.3)') dt, ttmin + //_// PORT: write(0,'((16(1pe8.1)))') (dtit(i),i=is+1,ie+1) + //_// PORT: write(0,'((16(1pe8.1)))') (ttot(i),i=is+1,ie+1) + //_// PORT: write(0,'((16(1pe8.1)))') (edot(i),i=is+1,ie+1) + //_// PORT: write(0,'((16(I3)))') (itmask(i),i=is+1,ie+1) + + if (my_chemistry->exit_after_iterations_exceeded == 1) { + (*ierr) = 0; + } + } + // WARNING_MESSAGE + } + + if (iter > my_chemistry->max_iterations/2) { + OMP_PRAGMA_CRITICAL + { + eprintf("MULTI_COOL iter,j,k = %d %d %d\n", iter, j, k); + } + } + + // Next j,k + } + } // OMP_PRAGMA("omp parallel") + + // If an error has been produced, return now. + + if ((*ierr) == 0) { + return; + } + + // Convert densities back to comoving from proper + + if (my_units->comoving_coordinates == 1) { + + factor = (gr_float)(std::pow(my_units->a_value,3) ); + + FORTRAN_NAME(scale_fields_g)( + &my_chemistry->primordial_chemistry, imetal, &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, + &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->multi_metals, &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, &factor, + &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], + d.data(), de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, + my_fields->metal_density, my_fields->dust_density, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, + my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, + my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, + my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, + my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, + my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, + my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, + my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, + my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, + my_fields->local_ISM_metal_density, my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, + my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, + my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density); + + } + + if (my_chemistry->primordial_chemistry > 0) { + + // Correct the species to ensure consistency (i.e. type conservation) + +#ifdef ABUNDANCE_CORRECTION + FORTRAN_NAME(make_consistent_g)(de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, + d.data(), &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, imetal, &my_chemistry->HydrogenFractionByMass, &my_chemistry->DeuteriumToHydrogenRatio, + &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, &dom, + my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, + my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, + my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, + my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, + my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, + my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, + my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, + my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, + my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, + &my_chemistry->multi_metals, &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, + my_fields->local_ISM_metal_density, + my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, + my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, + my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density, + &my_rates->SN0_N, + my_rates->SN0_XC, my_rates->SN0_XO, my_rates->SN0_XMg, my_rates->SN0_XAl, my_rates->SN0_XSi, + my_rates->SN0_XS, my_rates->SN0_XFe, + my_rates->SN0_fC, my_rates->SN0_fO, my_rates->SN0_fMg, my_rates->SN0_fAl, my_rates->SN0_fSi, + my_rates->SN0_fS, my_rates->SN0_fFe + ); + + FORTRAN_NAME(ceiling_species_g)(d.data(), de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, + &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, imetal, &my_chemistry->use_dust_density_field, + my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, + &my_chemistry->metal_abundances, &my_chemistry->metal_chemistry, &my_chemistry->dust_species, &my_chemistry->multi_metals, + &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, + my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, + my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, + my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, + my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, + my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, + my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, + my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, + my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, + my_fields->local_ISM_metal_density, + my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, + my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, + my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density); +#endif + + } + + return; +} + +#ifdef __cplusplus +} // extern "C" +#endif /* __cplusplus */ diff --git a/src/clib/solve_rate_cool_g-cpp.h b/src/clib/solve_rate_cool_g-cpp.h new file mode 100644 index 000000000..b56f32e91 --- /dev/null +++ b/src/clib/solve_rate_cool_g-cpp.h @@ -0,0 +1,31 @@ +// See LICENSE file for license and copyright information + +/// @file solve_rate_cool_g-cpp.h +/// @brief Declares signature of solve_rate_cool_g + +// This file was initially generated automatically during conversion of the +// solve_rate_cool_g function from FORTRAN to C++ + +#ifndef MY_FILE_CPP_H +#define MY_FILE_CPP_H + +#include "grackle.h" // gr_float +#include "fortran_func_decls.h" // gr_mask_int + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ +// the following function can be called from C or C++ + +void solve_rate_cool_g( + int* imetal, double* dt, double* utem, double* uxyz, double* urho, int* ierr, + chemistry_data* my_chemistry, chemistry_data_storage* my_rates, + code_units* my_units, grackle_field_data* my_fields, + photo_rate_storage* my_uvb_rates +); + +#ifdef __cplusplus +} // extern "C" +#endif /* __cplusplus */ + +#endif /* MY_FILE_CPP_H */ diff --git a/src/clib/solve_rate_cool_g.F b/src/clib/solve_rate_cool_g.F index e0894d1f4..15d396e03 100644 --- a/src/clib/solve_rate_cool_g.F +++ b/src/clib/solve_rate_cool_g.F @@ -1,1520 +1,9 @@ #include "phys_const.def" -!======================================================================= -!/////////////////// SUBROUTINE SOLVE_RATE_COOL_G \\\\\\\\\\\\\\\\\\\\\ - - subroutine solve_rate_cool_g(icool, d, e, u, v, w, de, - & HI, HII, HeI, HeII, HeIII, - & in, jn, kn, nratec, iexpand, - & ispecies, imetal, imcool, idust, - & idustall, idustfield, idim, - & is, js, ks, ie, je, ke, ih2co, ipiht, idustrec, - & igammah, - & dx, dt, aye, temstart, temend, - & utem, uxyz, uaye, urho, utim, - & gamma, fh, dtoh, z_solar, fgr, - & k1a, k2a, k3a, k4a, k5a, k6a, k7a, k8a, k9a, k10a, - & k11a, k12a, k13a, k13dda, k14a, k15a, - & k16a, k17a, k18a, k19a, k22a, - & k24, k25, k26, k27, k28, k29, k30, k31, - & k50a, k51a, k52a, k53a, k54a, k55a, k56a, - & k57a, k58a, - & ndratec, dtemstart, dtemend, h2dusta, - & ncrna, ncrd1a, ncrd2a, - & ceHIa, ceHeIa, ceHeIIa, ciHIa, ciHeIa, - & ciHeISa, ciHeIIa, reHIIa, reHeII1a, - & reHeII2a, reHeIIIa, brema, compa, gammaha, isrf, - & regra, gamma_isrfa, - & comp_xraya, comp_temp, piHI, piHeI, piHeII, - & HM, H2I, H2II, DI, DII, HDI, metal, dust, - & hyd01ka, h2k01a, vibha, rotha, rotla, - & gpldla, gphdla, hdltea, hdlowa, - & gaHIa, gaH2a, gaHea, gaHpa, gaela, - & h2ltea, gasgra, iH2shield, iradshield, - & avgsighi, avgsighei, avgsigheii, - & iradtrans, iradcoupled, iradstep, - & irt_honly, kphHI, kphHeI, kphHeII, kdissH2I, - & photogamma, xH2shield, - & ierr, - & ih2optical, iciecool, ithreebody, ih2cr, ihdcr, - & ciecoa, - & icmbTfloor, iClHeat, clEleFra, - & priGridRank, priGridDim, - & priPar1, priPar2, priPar3, priPar4, priPar5, - & priDataSize, priCooling, priHeating, priMMW, - & metGridRank, metGridDim, - & metPar1, metPar2, metPar3, metPar4, metPar5, - & metDataSize, metCooling, metHeating, clnew, - & iVheat, iMheat, Vheat, Mheat, - & iTfloor, Tfloor_scalar, Tfloor, - & imchem, igrgr, ipcont, tmcool - & , DM, HDII, HeHII - & , CI, CII, CO, CO2 - & , OI, OH, H2O, O2 - & , SiI, SiOI, SiO2I - & , CH, CH2, COII, OII - & , OHII, H2OII, H3OII, O2II - & , Mg, Al, S, Fe - & , SiM, FeM, Mg2SiO4, MgSiO3, Fe3O4 - & , AC, SiO2D, MgO, FeS, Al2O3 - & , reforg, volorg, H2Oice - & , k125a, k129a, k130a, k131a, k132a - & , k133a, k134a, k135a, k136a, k137a - & , k148a, k149a, k150a, k151a, k152a - & , k153a - & , kz15a, kz16a, kz17a, kz18a, kz19a - & , kz20a, kz21a, kz22a, kz23a, kz24a - & , kz25a, kz26a, kz27a, kz28a, kz29a - & , kz30a, kz31a, kz32a, kz33a, kz34a - & , kz35a, kz36a, kz37a, kz38a, kz39a - & , kz40a, kz41a, kz42a, kz43a, kz44a - & , kz45a, kz46a, kz47a, kz48a, kz49a - & , kz50a, kz51a, kz52a, kz53a, kz54a - & , cieY06a - & , LH2_N, LH2_Size - & , LH2_D, LH2_T, LH2_H - & , LH2_dD, LH2_dT, LH2_dH, LH2_L - & , LHD_N, LHD_Size - & , LHD_D, LHD_T, LHD_H - & , LHD_dD, LHD_dT, LHD_dH, LHD_L - & , LCI_N, LCI_Size - & , LCI_D, LCI_T, LCI_H - & , LCI_dD, LCI_dT, LCI_dH, LCI_L - & , LCII_N, LCII_Size - & , LCII_D, LCII_T, LCII_H - & , LCII_dD, LCII_dT, LCII_dH, LCII_L - & , LOI_N, LOI_Size - & , LOI_D, LOI_T, LOI_H - & , LOI_dD, LOI_dT, LOI_dH, LOI_L - & , LCO_N, LCO_Size - & , LCO_D, LCO_T, LCO_H - & , LCO_dD, LCO_dT, LCO_dH, LCO_L - & , LOH_N, LOH_Size - & , LOH_D, LOH_T, LOH_H - & , LOH_dD, LOH_dT, LOH_dH, LOH_L - & , LH2O_N, LH2O_Size - & , LH2O_D, LH2O_T, LH2O_H - & , LH2O_dD, LH2O_dT, LH2O_dH, LH2O_L - & , alphap_N, alphap_Size - & , alphap_D, alphap_T, alphap_dD, alphap_dT - & , alphap_Data - & , immulti, imabund, idspecies, itdmulti, idsub - & , metal_loc - & , metal_C13, metal_C20, metal_C25, metal_C30 - & , metal_F13, metal_F15, metal_F50, metal_F80 - & , metal_P170, metal_P200, metal_Y19 - & , SN0_N - & , SN0_XC , SN0_XO, SN0_XMg, SN0_XAl - & , SN0_XSi, SN0_XS, SN0_XFe - & , SN0_fC , SN0_fO, SN0_fMg, SN0_fAl - & , SN0_fSi, SN0_fS, SN0_fFe - & , SN0_fSiM, SN0_fFeM, SN0_fMg2SiO4, SN0_fMgSiO3 - & , SN0_fFe3O4, SN0_fAC, SN0_fSiO2D, SN0_fMgO - & , SN0_fFeS, SN0_fAl2O3 - & , SN0_freforg, SN0_fvolorg, SN0_fH2Oice - & , SN0_r0SiM, SN0_r0FeM, SN0_r0Mg2SiO4, SN0_r0MgSiO3 - & , SN0_r0Fe3O4, SN0_r0AC, SN0_r0SiO2D, SN0_r0MgO - & , SN0_r0FeS, SN0_r0Al2O3 - & , SN0_r0reforg, SN0_r0volorg, SN0_r0H2Oice - & , gr_N, gr_Size, gr_dT, gr_Td - & , SN0_kpSiM, SN0_kpFeM, SN0_kpMg2SiO4, SN0_kpMgSiO3 - & , SN0_kpFe3O4, SN0_kpAC, SN0_kpSiO2D, SN0_kpMgO - & , SN0_kpFeS, SN0_kpAl2O3 - & , SN0_kpreforg, SN0_kpvolorg, SN0_kpH2Oice - & , h2dustSa, h2dustCa, gasgr2a, gamma_isrf2a, grogra - & , idissHDI, kdissHDI, iionZ, kphCI, kphOI - & , idissZ, kdissCO, kdissOH, kdissH2O, iuseH2shield, - & iisrffield, isrf_habing, - & iH2shieldcustom, f_shield_custom, - & itmax, exititmax) - -! -! SOLVE MULTI-SPECIES RATE EQUATIONS AND RADIATIVE COOLING -! -! written by: Yu Zhang, Peter Anninos and Tom Abel -! date: -! modified1: January, 1996 by Greg Bryan; converted to KRONOS -! modified2: October, 1996 by GB; adapted to AMR -! modified3: May, 1999 by GB and Tom Abel, 3bodyH2, solver, HD -! modified4: June, 2005 by GB to solve rate & cool at same time -! modified5: April, 2009 by JHW to include radiative transfer -! modified6: September, 2009 by BDS to include cloudy cooling -! -! PURPOSE: -! Solve the multi-species rate and cool equations. -! -! INPUTS: -! icool - flag to update energy from radiative cooling -! in,jn,kn - dimensions of 3D fields -! -! d - total density field -! de - electron density field -! HI,HII - H density fields (neutral & ionized) -! HeI/II/III - He density fields -! DI/II - D density fields (neutral & ionized) -! HDI - neutral HD molecule density field -! HM - H- density field -! H2I - H_2 (molecular H) density field -! H2II - H_2+ density field -! metal - metal density field -! dust - dust density field -! kph* - photoionization fields -! gamma* - photoheating fields -! f_shield_custom - custom H2 shielding factor -! -! is,ie - start and end indices of active region (zero based) -! iexpand - comoving coordinates flag (0 = off, 1 = on) -! idim - dimensionality (rank) of problem -! ispecies - chemistry module (1 - H/He only, 2 - molecular H, 3 - D) -! imetal - flag if metal field is active (0 = no, 1 = yes) -! imcool - flag if there is metal cooling -! idust - flag for H2 formation on dust grains -! idustall - flag for dust (0 - none, 1 - heating/cooling + H2 form.) -! idustfield - flag if a dust density field is present -! iisrffield - flag if a field for the interstellar radiation field is present -! ih2co - flag to include H2 cooling (1 = on, 0 = off) -! ipiht - flag to include photoionization heating (1 = on, 0 = off) -! idustrec - flag to include dust recombination cooling (1 = on, -1 = off) -! iH2shield - flag for approximate self-shielding of H2 (Wolcott-Green+ 2011) -! iradshield - flag for approximate self-shielding of UV background -! avgsighi - spectrum averaged ionization crs for HI for use with shielding -! avgsighei - spectrum averaged ionization crs for HeI for use with shielding -! avgsigheii - spectrum averaged ionization crs for HeII for use with shielding -! iradtrans - flag to include radiative transfer (1 = on, 0 = off) -! iradcoupled - flag to indicate coupled radiative transfer -! iradstep - flag to indicate intermediate coupled radiative transfer timestep -! irt_honly - flag to indicate applying RT ionization and heating to HI only -! iH2shieldcustom - flag to indicate a custom H2 shielding factor is provided - -! fh - Hydrogen mass fraction (typically 0.76) -! dtoh - Deuterium to H mass ratio -! z_solar - Solar metal mass fraction -! fgr - the local dust to gas ratio (by mass) -! dt - timestep to integrate over -! aye - expansion factor (in code units) -! -! utim - time units (i.e. code units to CGS conversion factor) -! uaye - expansion factor conversion factor (uaye = 1/(1+zinit)) -! urho - density units -! uxyz - length units -! utem - temperature(-like) units -! -! temstart, temend - start and end of temperature range for rate table -! nratec - dimensions of chemical rate arrays (functions of temperature) -! dtemstart, dtemend - start and end of dust temperature range -! ndratec - extra dimension for H2 formation on dust rate (dust temperature) -! -! icmbTfloor - flag to include temperature floor from cmb -! iClHeat - flag to include cloudy heating -! priGridRank - rank of cloudy primordial cooling data grid -! priGridDim - array containing dimensions of cloudy primordial data -! priPar1, priPar2, priPar3 - arrays containing primordial grid parameter values -! priDataSize - total size of flattened 1D primordial cooling data array -! priCooling - primordial cooling data -! priHeating - primordial heating data -! priMMW - primordial mmw data -! metGridRank - rank of cloudy metal cooling data grid -! metGridDim - array containing dimensions of cloudy metal data -! metPar1, metPar2, metPar3 - arrays containing metal grid parameter values -! metDataSize - total size of flattened 1D metal cooling data array -! metCooling - metal cooling data -! metHeating - metal heating data -! iVheat - flag for using volumetric heating rate -! iMheat - flag for using specific heating rate -! Vheat - array of volumetric heating rates -! Mheat - array of specific heating rates -! iTfloor - flag for using temperature floor field -! Tfloor_scalar - scalar temperature floor value -! Tfloor - array of temperature floor values -! itmax - maximum allowed sub-cycle iterations -! exititmax - flag to exit if max iterations exceeded -! -! OUTPUTS: -! update chemical rate densities (HI, HII, etc) -! -! PARAMETERS: -! mh - H mass in cgs units -! -!----------------------------------------------------------------------- - - implicit NONE -#include "grackle_fortran_types.def" -#ifdef _OPENMP -#include "omp_lib.h" -#endif - -! General Arguments - - integer icool, in, jn, kn, is, js, ks, ie, je, ke, nratec, - & iexpand, ih2co, ipiht, ispecies, imetal, idim, - & ierror, imcool, idust, idustall, idustfield, idustrec, - & igammah, ih2optical, iciecool, ithreebody, ih2cr, ihdcr, - & ndratec, clnew, iVheat, iMheat, iTfloor, - & iH2shield, iradshield, - & iradtrans, iradcoupled, iradstep, irt_honly, - & imchem, igrgr, ipcont, - & iisrffield, iH2shieldcustom, ierr, itmax, exititmax - - real*8 dx, dt, aye, temstart, temend, gamma, - & utim, uxyz, uaye, urho, utem, fh, dtoh, z_solar, - & fgr, dtemstart, dtemend, clEleFra, Tfloor_scalar, - & tmcool - -! Density, energy and velocity fields fields - - R_PREC de(in,jn,kn), HI(in,jn,kn), HII(in,jn,kn), - & HeI(in,jn,kn), HeII(in,jn,kn), HeIII(in,jn,kn), - & HM(in,jn,kn), H2I(in,jn,kn), H2II(in,jn,kn), - & DI(in,jn,kn), DII(in,jn,kn), HDI(in,jn,kn), - & d(in,jn,kn), e(in,jn,kn), - & u(in,jn,kn), v(in,jn,kn), w(in,jn,kn), - & metal(in,jn,kn), dust(in,jn,kn), - & Vheat(in,jn,kn), Mheat(in,jn,kn), Tfloor(in,jn,kn) - R_PREC DM(in,jn,kn), HDII(in,jn,kn), HeHII(in,jn,kn) - & , CI(in,jn,kn) , CII(in,jn,kn) , CO(in,jn,kn) - & , CO2(in,jn,kn) , OI(in,jn,kn) , OH(in,jn,kn) - & , H2O(in,jn,kn) , O2(in,jn,kn) , SiI(in,jn,kn) - & , SiOI(in,jn,kn) , SiO2I(in,jn,kn) , CH(in,jn,kn) - & , CH2(in,jn,kn) , COII(in,jn,kn) , OII(in,jn,kn) - & , OHII(in,jn,kn) , H2OII(in,jn,kn) , H3OII(in,jn,kn) - & , O2II(in,jn,kn) , Mg(in,jn,kn) , Al(in,jn,kn) - & , S(in,jn,kn) , Fe(in,jn,kn) - R_PREC SiM(in,jn,kn), FeM(in,jn,kn), Mg2SiO4(in,jn,kn) - & , MgSiO3(in,jn,kn), Fe3O4(in,jn,kn), AC(in,jn,kn) - & , SiO2D(in,jn,kn), MgO(in,jn,kn), FeS(in,jn,kn) - & , Al2O3(in,jn,kn) - & , reforg(in,jn,kn), volorg(in,jn,kn), H2Oice(in,jn,kn) - R_PREC metal_loc(in,jn,kn) - & , metal_C13(in,jn,kn), metal_C20(in,jn,kn) - & , metal_C25(in,jn,kn), metal_C30(in,jn,kn) - & , metal_F13(in,jn,kn), metal_F15(in,jn,kn) - & , metal_F50(in,jn,kn), metal_F80(in,jn,kn) - & , metal_P170(in,jn,kn), metal_P200(in,jn,kn) - & , metal_Y19(in,jn,kn) - -! Radiative transfer fields - - R_PREC kphHI(in,jn,kn), kphHeI(in,jn,kn), kphHeII(in,jn,kn), - & kdissH2I(in,jn,kn), photogamma(in,jn,kn) - - integer idissHDI, iionZ, idissZ - R_PREC kdissHDI(in,jn,kn), kphCI(in,jn,kn), kphOI(in,jn,kn), - & kdissCO(in,jn,kn), kdissOH(in,jn,kn), kdissH2O(in,jn,kn) - integer iuseH2shield - -! H2 self-shielding length-scale field - - R_PREC xH2shield(in,jn,kn) - -! Interstellar radiation field for dust heating - - R_PREC isrf_habing(in,jn,kn) - -! Custom H2 shielding factor - - R_PREC f_shield_custom(in, jn, kn) - -! Cooling tables (coolings rates as a function of temperature) - - real*8 hyd01ka(nratec), h2k01a(nratec), vibha(nratec), - & rotha(nratec), rotla(nratec), gpldla(nratec), - & gphdla(nratec), hdltea(nratec), hdlowa(nratec), - & gaHIa(nratec), gaH2a(nratec), gaHea(nratec), - & gaHpa(nratec), gaela(nratec), h2ltea(nratec), - & gasgra(nratec), ciecoa(nratec), - & ceHIa(nratec), ceHeIa(nratec), ceHeIIa(nratec), - & ciHIa(nratec), ciHeIa(nratec), ciHeISa(nratec), - & ciHeIIa(nratec), reHIIa(nratec), reHeII1a(nratec), - & reHeII2a(nratec), reHeIIIa(nratec), brema(nratec), - & compa, piHI, piHeI, piHeII, comp_xraya, comp_temp, - & gammaha, isrf, regra(nratec), gamma_isrfa - real*8 cieY06a(nratec) - integer*8 LH2_N(3), LH2_Size - real*8 LH2_D(LH2_N(1)), LH2_T(LH2_N(2)), LH2_H(LH2_N(3)) - & , LH2_dD, LH2_dT, LH2_dH, LH2_L(LH2_Size) - integer*8 LHD_N(3), LHD_Size - real*8 LHD_D(LHD_N(1)), LHD_T(LHD_N(2)), LHD_H(LHD_N(3)) - & , LHD_dD, LHD_dT, LHD_dH, LHD_L(LHD_Size) - integer*8 LCI_N(3), LCI_Size - real*8 LCI_D(LCI_N(1)), LCI_T(LCI_N(2)), LCI_H(LCI_N(3)) - & , LCI_dD, LCI_dT, LCI_dH, LCI_L(LCI_Size) - integer*8 LCII_N(3), LCII_Size - real*8 LCII_D(LCII_N(1)), LCII_T(LCII_N(2)), LCII_H(LCII_N(3)) - & , LCII_dD, LCII_dT, LCII_dH, LCII_L(LCII_Size) - integer*8 LOI_N(3), LOI_Size - real*8 LOI_D(LOI_N(1)), LOI_T(LOI_N(2)), LOI_H(LOI_N(3)) - & , LOI_dD, LOI_dT, LOI_dH, LOI_L(LOI_Size) - integer*8 LCO_N(3), LCO_Size - real*8 LCO_D(LCO_N(1)), LCO_T(LCO_N(2)), LCO_H(LCO_N(3)) - & , LCO_dD, LCO_dT, LCO_dH, LCO_L(LCO_Size) - integer*8 LOH_N(3), LOH_Size - real*8 LOH_D(LOH_N(1)), LOH_T(LOH_N(2)), LOH_H(LOH_N(3)) - & , LOH_dD, LOH_dT, LOH_dH, LOH_L(LOH_Size) - integer*8 LH2O_N(3), LH2O_Size - real*8 LH2O_D(LH2O_N(1)), LH2O_T(LH2O_N(2)), LH2O_H(LH2O_N(3)) - & , LH2O_dD, LH2O_dT, LH2O_dH, LH2O_L(LH2O_Size) - integer*8 alphap_N(2), alphap_Size - real*8 alphap_D(alphap_N(1)), alphap_T(alphap_N(2)) - & , alphap_dD, alphap_dT - & , alphap_Data(alphap_Size) - integer immulti, imabund, idspecies, itdmulti, idsub - integer SN0_N - real*8 SN0_XC (SN0_N), SN0_XO(SN0_N), SN0_XMg(SN0_N) - & , SN0_XAl(SN0_N), SN0_XSi(SN0_N), SN0_XS(SN0_N) - & , SN0_XFe(SN0_N) - real*8 SN0_fC (SN0_N), SN0_fO(SN0_N), SN0_fMg(SN0_N) - & , SN0_fAl(SN0_N), SN0_fSi(SN0_N), SN0_fS(SN0_N) - & , SN0_fFe(SN0_N) - real*8 SN0_fSiM(SN0_N), SN0_fFeM(SN0_N), SN0_fMg2SiO4(SN0_N) - & , SN0_fMgSiO3(SN0_N), SN0_fFe3O4(SN0_N), SN0_fAC(SN0_N) - & , SN0_fSiO2D(SN0_N), SN0_fMgO(SN0_N), SN0_fFeS(SN0_N) - & , SN0_fAl2O3(SN0_N) - & , SN0_freforg(SN0_N), SN0_fvolorg(SN0_N), SN0_fH2Oice(SN0_N) - real*8 SN0_r0SiM(3,SN0_N), SN0_r0FeM(3,SN0_N) - & , SN0_r0Mg2SiO4(3,SN0_N), SN0_r0MgSiO3(3,SN0_N) - & , SN0_r0Fe3O4(3,SN0_N), SN0_r0AC(3,SN0_N) - & , SN0_r0SiO2D(3,SN0_N), SN0_r0MgO(3,SN0_N) - & , SN0_r0FeS(3,SN0_N), SN0_r0Al2O3(3,SN0_N) - & , SN0_r0reforg(3,SN0_N) - & , SN0_r0volorg(3,SN0_N), SN0_r0H2Oice(3,SN0_N) -! opacity table - integer gr_N(2), gr_Size - real*8 gr_dT, gr_Td(gr_N(2)) - real*8 SN0_kpSiM(gr_Size,SN0_N), SN0_kpFeM(gr_Size,SN0_N) - & , SN0_kpMg2SiO4(gr_Size,SN0_N), SN0_kpMgSiO3(gr_Size,SN0_N) - & , SN0_kpFe3O4(gr_Size,SN0_N), SN0_kpAC(gr_Size,SN0_N) - & , SN0_kpSiO2D(gr_Size,SN0_N), SN0_kpMgO(gr_Size,SN0_N) - & , SN0_kpFeS(gr_Size,SN0_N), SN0_kpAl2O3(gr_Size,SN0_N) - & , SN0_kpreforg(gr_Size,SN0_N) - & , SN0_kpvolorg(gr_Size,SN0_N), SN0_kpH2Oice(gr_Size,SN0_N) - real*8 gasgr2a(nratec), gamma_isrf2a - - real*8 avgsighi, avgsighei, avgsigheii - -! Chemistry tables (rates as a function of temperature) - - real*8 k1a (nratec), k2a (nratec), k3a (nratec), k4a (nratec), - & k5a (nratec), k6a (nratec), k7a (nratec), k8a (nratec), - & k9a (nratec), k10a(nratec), k11a(nratec), k12a(nratec), - & k13a(nratec), k14a(nratec), k15a(nratec), k16a(nratec), - & k17a(nratec), k18a(nratec), k19a(nratec), k22a(nratec), - & k50a(nratec), k51a(nratec), k52a(nratec), k53a(nratec), - & k54a(nratec), k55a(nratec), k56a(nratec), - & k57a(nratec), k58a(nratec), - & k13dda(nratec, 14), h2dusta(nratec, ndratec), - & ncrna(nratec), ncrd1a(nratec), ncrd2a(nratec), - & k24, k25, k26, k27, k28, k29, k30, k31 - real*8 k125a(nratec), k129a(nratec), k130a(nratec) - & , k131a(nratec), k132a(nratec), k133a(nratec) - & , k134a(nratec), k135a(nratec), k136a(nratec) - & , k137a(nratec), k148a(nratec), k149a(nratec) - & , k150a(nratec), k151a(nratec), k152a(nratec) - & , k153a(nratec) - & , kz15a(nratec), kz16a(nratec), kz17a(nratec) - & , kz18a(nratec), kz19a(nratec), kz20a(nratec) - & , kz21a(nratec), kz22a(nratec), kz23a(nratec) - & , kz24a(nratec), kz25a(nratec), kz26a(nratec) - & , kz27a(nratec), kz28a(nratec), kz29a(nratec) - & , kz30a(nratec), kz31a(nratec), kz32a(nratec) - & , kz33a(nratec), kz34a(nratec), kz35a(nratec) - & , kz36a(nratec), kz37a(nratec), kz38a(nratec) - & , kz39a(nratec), kz40a(nratec), kz41a(nratec) - & , kz42a(nratec), kz43a(nratec), kz44a(nratec) - & , kz45a(nratec), kz46a(nratec), kz47a(nratec) - & , kz48a(nratec), kz49a(nratec), kz50a(nratec) - & , kz51a(nratec), kz52a(nratec), kz53a(nratec) - & , kz54a(nratec) - -! Cloudy cooling data - - integer icmbTfloor, iClHeat - integer*8 priGridRank, priDataSize, - & metGridRank, metDataSize, - & priGridDim(priGridRank), metGridDim(metGridRank) - real*8 priPar1(priGridDim(1)), priPar2(priGridDim(2)), - & priPar3(priGridDim(3)), priPar4(priGridDim(4)), - & priPar5(priGridDim(5)), - & metPar1(metGridDim(1)), metPar2(metGridDim(2)), - & metPar3(metGridDim(3)), metPar4(metGridDim(4)), - & metPar5(metGridDim(5)), - & priCooling(priDataSize), priHeating(priDataSize), - & priMMW(priDataSize), - & metCooling(metDataSize), metHeating(metDataSize) - -! Parameters - -#ifdef GRACKLE_FLOAT_4 - R_PREC, parameter :: tolerance = 1.0e-05_RKIND -#else - R_PREC, parameter :: tolerance = 1.0e-10_RKIND -#endif - - real*8, parameter :: mh = mass_h - real*8, parameter :: pi = pi_val - -! Locals - - integer i, j, k, iter - integer t, dj, dk - real*8 ttmin, dom, energy, comp1, comp2 - real*8 coolunit, dbase1, tbase1, xbase1, chunit, uvel - real*8 heq1, heq2, eqk221, eqk222, eqk131, eqk132, - & eqt1, eqt2, eqtdef, dheq, heq, dlogtem, dx_cgs, - & c_ljeans, min_metallicity - R_PREC factor - -! row temporaries - - integer*8 indixe(in) - real*8 t1(in), t2(in), logtem(in), tdef(in), - & dtit(in), ttot(in), p2d(in), tgas(in), tgasold(in), - & tdust(in), metallicity(in), dust2gas(in), - & rhoH(in), mmw(in), mynh(in), myde(in), gammaha_eff(in), - & gasgr_tdust(in), regr(in), ddom(in), olddtit - -! Rate equation row temporaries - - real*8 HIp(in), HIIp(in), HeIp(in), HeIIp(in), HeIIIp(in), - & HMp(in), H2Ip(in), H2IIp(in), - & dep(in), dedot(in),HIdot(in), dedot_prev(in), - & DIp(in), DIIp(in), HDIp(in), HIdot_prev(in), - & k24shield(in), k25shield(in), k26shield(in), - & k28shield(in), k29shield(in), k30shield(in), - & k31shield(in), - & k1 (in), k2 (in), k3 (in), k4 (in), k5 (in), - & k6 (in), k7 (in), k8 (in), k9 (in), k10(in), - & k11(in), k12(in), k13(in), k14(in), k15(in), - & k16(in), k17(in), k18(in), k19(in), k22(in), - & k50(in), k51(in), k52(in), k53(in), k54(in), - & k55(in), k56(in), k57(in), k58(in), - & k13dd(in, 14), h2dust(in), - & ncrn(in), ncrd1(in), ncrd2(in) - real*8 DMp(in) , HDIIp(in) , HeHIIp(in) - & , CIp(in) , CIIp(in) , COp(in) - & , CO2p(in) , OIp(in) , OHp(in) - & , H2Op(in) , O2p(in) , SiIp(in) - & , SiOIp(in) , SiO2Ip(in) , CHp(in) - & , CH2p(in) , COIIp(in) , OIIp(in) - & , OHIIp(in) , H2OIIp(in) , H3OIIp(in) - & , O2IIp(in) , Mgp(in) , Alp(in) - & , Sp(in) , Fep(in) - R_PREC SiMp(in), FeMp(in), Mg2SiO4p(in) - & , MgSiO3p(in), Fe3O4p(in), ACp(in) - & , SiO2Dp(in), MgOp(in), FeSp(in) - & , Al2O3p(in) - & , reforgp(in), volorgp(in), H2Oicep(in) - - real*8 k125(in), k129(in), k130(in), k131(in), k132(in) - & , k133(in), k134(in), k135(in), k136(in), k137(in) - & , k148(in), k149(in), k150(in), k151(in), k152(in) - & , k153(in) - & , kz15(in), kz16(in), kz17(in), kz18(in), kz19(in) - & , kz20(in), kz21(in), kz22(in), kz23(in), kz24(in) - & , kz25(in), kz26(in), kz27(in), kz28(in), kz29(in) - & , kz30(in), kz31(in), kz32(in), kz33(in), kz34(in) - & , kz35(in), kz36(in), kz37(in), kz38(in), kz39(in) - & , kz40(in), kz41(in), kz42(in), kz43(in), kz44(in) - & , kz45(in), kz46(in), kz47(in), kz48(in), kz49(in) - & , kz50(in), kz51(in), kz52(in), kz53(in), kz54(in) - real*8 h2dustSa(nratec * ndratec), h2dustCa(nratec * ndratec) - real*8 grogra(nratec) - real*8 kdSiM(in), kdFeM(in), kdMg2SiO4(in) - & , kdMgSiO3(in), kdFe3O4(in), kdAC(in) - & , kdSiO2D(in), kdMgO(in), kdFeS(in) - & , kdAl2O3(in) - & , kdreforg(in), kdvolorg(in), kdH2Oice(in) -! grain temperature - real*8 tSiM(in), tFeM(in), tMg2SiO4(in) - & , tMgSiO3(in), tFe3O4(in), tAC(in) - & , tSiO2D(in), tMgO(in), tFeS(in) - & , tAl2O3(in) - & , treforg(in), tvolorg(in), tH2Oice(in) - -! Cooling/heating row locals - - real*8 ceHI(in), ceHeI(in), ceHeII(in), - & ciHI(in), ciHeI(in), ciHeIS(in), ciHeII(in), - & reHII(in), reHeII1(in), reHeII2(in), reHeIII(in), - & brem(in), edot(in) - real*8 hyd01k(in), h2k01(in), vibh(in), roth(in), rotl(in), - & gpldl(in), gphdl(in), hdlte(in), hdlow(in), cieco(in) - -! Iteration mask - - MASK_TYPE itmask(in), anydust - MASK_TYPE itmask_tmp(in), itmask_nr(in) - MASK_TYPE itmask_metal(in) - integer itr, imp_eng(in) - -! -!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\///////////////////////////////// -!======================================================================= - -! Set error indicator - - ierr = 1 - -! Set flag for dust-related options - - if ((idust .gt. 0) .or. (idustall .gt. 0)) then - anydust = MASK_TRUE - else - anydust = MASK_FALSE - endif - -! ignore metal chemistry/cooling below this metallicity - min_metallicity = 1.d-9 / z_solar - -! Set units - - dom = urho*(aye**3)/mh - tbase1 = utim - xbase1 = uxyz/(aye*uaye) ! uxyz is [x]*a = [x]*[a]*a' ' - dbase1 = urho*(aye*uaye)**3 ! urho is [dens]/a^3 = [dens]/([a]*a')^3 ' - coolunit = (uaye**5 * xbase1**2 * mh**2) / (tbase1**3 * dbase1) - uvel = (uxyz/aye) / utim -c chunit = (1.60218e-12_DKIND)/(2._DKIND*uvel*uvel*mh) ! 1 eV per H2 formed - chunit = (1.60218e-12_DKIND)/(uvel*uvel*mh) ! 1 eV per REACTION (Feb 2020, Gen Chiaki) - - dx_cgs = dx * xbase1 - c_ljeans = sqrt((gamma * pi * kboltz) / - & (GravConst * mh * dbase1)) - - dlogtem = (log(temend) - log(temstart))/real(nratec-1, DKIND) - -! We better make consistent at first GC202002 - - if (ispecies .gt. 0) then - -#define ABUNDANCE_CORRECTION -#ifdef ABUNDANCE_CORRECTION - call make_consistent_g(de, HI, HII, HeI, HeII, HeIII, - & HM, H2I, H2II, DI, DII, HDI, metal, dust, - & d, is, ie, js, je, ks, ke, - & in, jn, kn, ispecies, imetal, fh, dtoh - & , idustfield, imchem, igrgr, dom - & , DM, HDII, HeHII - & , CI, CII, CO, CO2 - & , OI, OH, H2O, O2 - & , SiI, SiOI, SiO2I - & , CH, CH2, COII, OII - & , OHII, H2OII, H3OII, O2II - & , Mg, Al, S, Fe - & , SiM, FeM, Mg2SiO4, MgSiO3, Fe3O4 - & , AC, SiO2D, MgO, FeS, Al2O3 - & , reforg, volorg, H2Oice - & , immulti, imabund, idspecies, itdmulti, idsub - & , metal_loc - & , metal_C13, metal_C20, metal_C25, metal_C30 - & , metal_F13, metal_F15, metal_F50, metal_F80 - & , metal_P170, metal_P200, metal_Y19 - & , SN0_N - & , SN0_XC, SN0_XO, SN0_XMg, SN0_XAl, SN0_XSi - & , SN0_XS, SN0_XFe - & , SN0_fC, SN0_fO, SN0_fMg, SN0_fAl, SN0_fSi - & , SN0_fS, SN0_fFe - & ) -#endif - - endif - -! Convert densities from comoving to proper - - if (iexpand .eq. 1) then - - factor = real(aye**(-3), RKIND) - - call scale_fields_g( - & ispecies, imetal, idustfield, imchem, - & imabund, idspecies, immulti, igrgr, idsub, factor, - & is, ie, js, je, ks, ke, in, jn, kn, - & d, de, HI, HII, HeI, HeII, HeIII, - & HM, H2I, H2II, DI, DII, HDI, DM, HDII, HeHII, - & metal, dust, - & CI, CII, CO, CO2, - & OI, OH, H2O, O2, - & SiI, SiOI, SiO2I, - & CH, CH2, COII, OII, - & OHII, H2OII, H3OII, O2II, - & Mg, Al, S, Fe, - & SiM, FeM, Mg2SiO4, MgSiO3, Fe3O4, - & AC, SiO2D, MgO, FeS, Al2O3, - & reforg, volorg, H2Oice, - & metal_loc, metal_C13, metal_C20, metal_C25, metal_C30, - & metal_F13, metal_F15, metal_F50, metal_F80, - & metal_P170, metal_P200, metal_Y19) - - endif - -#ifdef ABUNDANCE_CORRECTION - call ceiling_species_g(d, de, HI, HII, HeI, HeII, HeIII, - & HM, H2I, H2II, DI, DII, HDI, metal, dust, - & is, ie, js, je, ks, ke, - & in, jn, kn, ispecies, imetal, idustfield - & , DM, HDII, HeHII - & , imabund, imchem, idspecies, immulti - & , igrgr, idsub - & , CI, CII, CO, CO2 - & , OI, OH, H2O, O2 - & , SiI, SiOI, SiO2I - & , CH, CH2, COII, OII - & , OHII, H2OII, H3OII, O2II - & , Mg, Al, S, Fe - & , SiM, FeM, Mg2SiO4, MgSiO3, Fe3O4 - & , AC, SiO2D, MgO, FeS, Al2O3 - & , reforg, volorg, H2Oice - & , metal_loc - & , metal_C13, metal_C20, metal_C25, metal_C30 - & , metal_F13, metal_F15, metal_F50, metal_F80 - & , metal_P170, metal_P200, metal_Y19) -#endif - - -! Loop over zones, and do an entire i-column in one go - dk = ke - ks + 1 - dj = je - js + 1 - -! parallelize the k and j loops with OpenMP -! flat j and k loops for better parallelism -#ifdef _OPENMP -! ierr is declared as shared and should be modified with atomic operation -!$omp parallel do schedule(runtime) private( -!$omp& i, j, k, iter, -!$omp& ttmin, energy, comp1, comp2, -!$omp& heq1, heq2, eqk221, eqk222, eqk131, eqk132, -!$omp& eqt1, eqt2, eqtdef, dheq, heq, -!$omp& indixe, -!$omp& t1, t2, logtem, tdef, -!$omp& dtit, ttot, p2d, tgas, tgasold, -!$omp& tdust, metallicity, dust2gas, rhoH, mmw, -!$omp& mynh, myde, gammaha_eff, gasgr_tdust, regr, ddom, -!$omp& olddtit, -!$omp& HIp, HIIp, HeIp, HeIIp, HeIIIp, -!$omp& HMp, H2Ip, H2IIp, -!$omp& dep, dedot,HIdot, dedot_prev, -!$omp& DIp, DIIp, HDIp, HIdot_prev, -!$omp& k24shield, k25shield, k26shield, -!$omp& k28shield, k29shield, k30shield, -!$omp& k31shield, -!$omp& k1 , k2 , k3 , k4 , k5, -!$omp& k6 , k7 , k8 , k9 , k10, -!$omp& k11, k12, k13, k14, k15, -!$omp& k16, k17, k18, k19, k22, -!$omp& k50, k51, k52, k53, k54, -!$omp& k55, k56, k57, k58, k13dd, h2dust, -!$omp& ncrn, ncrd1, ncrd2, -!$omp& ceHI, ceHeI, ceHeII, -!$omp& ciHI, ciHeI, ciHeIS, ciHeII, -!$omp& reHII, reHeII1, reHeII2, reHeIII, -!$omp& brem, edot, -!$omp& hyd01k, h2k01, vibh, roth, rotl, -!$omp& gpldl, gphdl, hdlte, hdlow, cieco, -!$omp& itmask, itmask_metal ) -#endif - do t = 0, dk*dj-1 - k = t/dj + ks+1 - j = mod(t,dj) + js+1 - -! tolerance = 1.0e-06_DKIND * dt - -! Initialize iteration mask to true for all cells. - - do i = is+1, ie+1 - itmask(i) = MASK_TRUE - enddo - -! If we are using coupled radiation with intermediate stepping, -! set iteration mask to include only cells with radiation in the -! intermediate coupled chemistry / energy step - if (iradtrans .eq. 1) then - if (iradcoupled .eq. 1 .and. iradstep .eq. 1) then - do i = is+1, ie+1 - if (kphHI(i,j,k) .gt. 0) then - itmask(i) = MASK_TRUE - else - itmask(i) = MASK_FALSE - endif - enddo - endif - -! Normal rate solver, but don't double count cells with radiation - if (iradcoupled .eq. 1 .and. iradstep .eq. 0) then - do i = is+1, ie + 1 - if (kphHI(i,j,k) .gt. 0) then - itmask(i) = MASK_FALSE - else - itmask(i) = MASK_TRUE - endif - enddo - endif - endif ! end rad trans check (divergent from original code) - -! Set time elapsed to zero for each cell in 1D section - - do i = is+1, ie+1 - ttot(i) = 0._DKIND - enddo - -! A useful slice variable since we do this a lot - - do i = is+1, ie+1 - ddom(i) = d(i,j,k) * dom - enddo - -! ------------------ Loop over subcycles ---------------- - - do iter = 1, itmax - - do i = is+1, ie+1 - if (itmask(i) .ne. MASK_FALSE) then - dtit(i) = huge8 - endif - enddo - -! Compute the cooling rate, tgas, tdust, and metallicity for this row - - call cool1d_multi_g( - & d, e, u, v, w, de, HI, HII, HeI, HeII, HeIII, - & in, jn, kn, nratec, - & iexpand, ispecies, imetal, imcool, - & idust, idustall, idustfield, idustrec, - & idim, is, ie, j, k, ih2co, ipiht, iter, igammah, - & aye, temstart, temend, z_solar, fgr, - & utem, uxyz, uaye, urho, utim, - & gamma, fh, - & ceHIa, ceHeIa, ceHeIIa, ciHIa, ciHeIa, - & ciHeISa, ciHeIIa, reHIIa, reHeII1a, - & reHeII2a, reHeIIIa, brema, compa, gammaha, - & isrf, regra, gamma_isrfa, comp_xraya, comp_temp, - & piHI, piHeI, piHeII, comp1, comp2, - & HM, H2I, H2II, DI, DII, HDI, metal, dust, - & hyd01ka, h2k01a, vibha, rotha, rotla, - & hyd01k, h2k01, vibh, roth, rotl, - & gpldla, gphdla, gpldl, gphdl, - & hdltea, hdlowa, hdlte, hdlow, - & gaHIa, gaH2a, gaHea, gaHpa, gaela, - & h2ltea, gasgra, - & ceHI, ceHeI, ceHeII, ciHI, ciHeI, ciHeIS, ciHeII, - & reHII, reHeII1, reHeII2, reHeIII, brem, - & indixe, t1, t2, logtem, tdef, edot, - & tgas, tgasold, mmw, p2d, tdust, metallicity, - & dust2gas, rhoH, mynh, myde, - & gammaha_eff, gasgr_tdust, regr, - & iradshield, avgsighi, avgsighei, avgsigheii, - & k24, k26, - & iradtrans, photogamma, - & ih2optical, iciecool, ih2cr, ihdcr, ciecoa, cieco, - & icmbTfloor, iClHeat, clEleFra, - & priGridRank, priGridDim, - & priPar1, priPar2, priPar3, priPar4, priPar5, - & priDataSize, priCooling, priHeating, priMMW, - & metGridRank, metGridDim, - & metPar1, metPar2, metPar3, metPar4, metPar5, - & metDataSize, metCooling, metHeating, clnew, - & iVheat, iMheat, Vheat, Mheat, - & iTfloor, Tfloor_scalar, Tfloor, - & iisrffield, isrf_habing, itmask, itmask_metal - & , imchem, igrgr, ipcont, tmcool - & , DM, HDII, HeHII - & , CI, CII, CO, CO2 - & , OI, OH, H2O, O2 - & , SiI, SiOI, SiO2I - & , CH, CH2, COII, OII - & , OHII, H2OII, H3OII, O2II - & , Mg, Al, S, Fe - & , SiM, FeM, Mg2SiO4, MgSiO3, Fe3O4 - & , AC, SiO2D, MgO, FeS, Al2O3 - & , reforg, volorg, H2Oice - & , cieY06a - & , LH2_N, LH2_Size - & , LH2_D, LH2_T, LH2_H - & , LH2_dD, LH2_dT, LH2_dH, LH2_L - & , LHD_N, LHD_Size - & , LHD_D, LHD_T, LHD_H - & , LHD_dD, LHD_dT, LHD_dH, LHD_L - & , LCI_N, LCI_Size - & , LCI_D, LCI_T, LCI_H - & , LCI_dD, LCI_dT, LCI_dH, LCI_L - & , LCII_N, LCII_Size - & , LCII_D, LCII_T, LCII_H - & , LCII_dD, LCII_dT, LCII_dH, LCII_L - & , LOI_N, LOI_Size - & , LOI_D, LOI_T, LOI_H - & , LOI_dD, LOI_dT, LOI_dH, LOI_L - & , LCO_N, LCO_Size - & , LCO_D, LCO_T, LCO_H - & , LCO_dD, LCO_dT, LCO_dH, LCO_L - & , LOH_N, LOH_Size - & , LOH_D, LOH_T, LOH_H - & , LOH_dD, LOH_dT, LOH_dH, LOH_L - & , LH2O_N, LH2O_Size - & , LH2O_D, LH2O_T, LH2O_H - & , LH2O_dD, LH2O_dT, LH2O_dH, LH2O_L - & , alphap_N, alphap_Size - & , alphap_D, alphap_T, alphap_dD, alphap_dT - & , alphap_Data - & , immulti, imabund, idspecies, itdmulti, idsub - & , metal_loc - & , metal_C13, metal_C20, metal_C25, metal_C30 - & , metal_F13, metal_F15, metal_F50, metal_F80 - & , metal_P170, metal_P200, metal_Y19 - & , SN0_N - & , SN0_fSiM, SN0_fFeM, SN0_fMg2SiO4, SN0_fMgSiO3 - & , SN0_fFe3O4, SN0_fAC, SN0_fSiO2D, SN0_fMgO - & , SN0_fFeS, SN0_fAl2O3 - & , SN0_freforg, SN0_fvolorg, SN0_fH2Oice - & , SN0_r0SiM, SN0_r0FeM, SN0_r0Mg2SiO4, SN0_r0MgSiO3 - & , SN0_r0Fe3O4, SN0_r0AC, SN0_r0SiO2D, SN0_r0MgO - & , SN0_r0FeS, SN0_r0Al2O3 - & , SN0_r0reforg, SN0_r0volorg, SN0_r0H2Oice - & , gr_N, gr_Size, gr_dT, gr_Td - & , SN0_kpSiM, SN0_kpFeM, SN0_kpMg2SiO4, SN0_kpMgSiO3 - & , SN0_kpFe3O4, SN0_kpAC, SN0_kpSiO2D, SN0_kpMgO - & , SN0_kpFeS, SN0_kpAl2O3 - & , SN0_kpreforg, SN0_kpvolorg, SN0_kpH2Oice - & , tSiM, tFeM, tMg2SiO4, tMgSiO3, tFe3O4 - & , tAC, tSiO2D, tMgO, tFeS, tAl2O3 - & , treforg, tvolorg, tH2Oice - & , gasgr2a, gamma_isrf2a - & ) - - if (ispecies .eq. 0) then -! This is some basic book-keeping to ensure that itmask_tmp has -! sensible values when ispecies is 0 -! -> There's room for optimization: when ispecies is 0, there is -! need to ever touch this variable. But, for now we focus on -! correct behavior before implementing this optimization - itmask_tmp = itmask - - else - -! Look-up rates as a function of temperature for 1D set of zones -! (maybe should add itmask to this call) - - call lookup_cool_rates1d_g(temstart, temend, nratec, j, k, - & is, ie, ithreebody, - & in, jn, kn, ispecies, anydust, - & iH2shield, iradshield, - & tgas, mmw, d, HI, HII, HeI, HeII, HeIII, - & HM, H2I, H2II, DI, DII, HDI, - & tdust, dust2gas, - & k1a, k2a, k3a, k4a, k5a, k6a, k7a, k8a, k9a, k10a, - & k11a, k12a, k13a, k13dda, k14a, k15a, k16a, - & k17a, k18a, k19a, k22a, - & k50a, k51a, k52a, k53a, k54a, k55a, k56a, - & k57a, k58a, ndratec, dtemstart, dtemend, h2dusta, - & ncrna, ncrd1a, ncrd2a, - & avgsighi, avgsighei, avgsigheii, piHI, piHeI, - & k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, - & k11, k12, k13, k14, k15, k16, k17, k18, - & k19, k22, k24, k25, k26, k28, k29, k30, k31, - & k50, k51, k52, k53, k54, k55, k56, k57, - & k58, k13dd, k24shield, k25shield, k26shield, - & k28shield, k29shield, k30shield, - & k31shield, h2dust, ncrn, ncrd1, ncrd2, - & t1, t2, tdef, logtem, indixe, - & dom, coolunit, tbase1, xbase1, dx_cgs, c_ljeans, - & iradtrans, kdissH2I, xH2shield, itmask, - & itmask_metal - & , fh, metal - & , DM, HDII, HeHII, imetal, imchem, igrgr - & , CI, CII, CO, CO2 - & , OI, OH, H2O, O2 - & , SiI, SiOI, SiO2I - & , CH, CH2, COII, OII - & , OHII, H2OII, H3OII, O2II - & , Mg, Al, S, Fe - & , SiM, FeM, Mg2SiO4, MgSiO3, Fe3O4 - & , AC, SiO2D, MgO, FeS, Al2O3 - & , reforg, volorg, H2Oice - & , k125a, k129a, k130a, k131a, k132a - & , k133a, k134a, k135a, k136a, k137a - & , k148a, k149a, k150a, k151a, k152a - & , k153a - & , kz15a, kz16a, kz17a, kz18a, kz19a - & , kz20a, kz21a, kz22a, kz23a, kz24a - & , kz25a, kz26a, kz27a, kz28a, kz29a - & , kz30a, kz31a, kz32a, kz33a, kz34a - & , kz35a, kz36a, kz37a, kz38a, kz39a - & , kz40a, kz41a, kz42a, kz43a, kz44a - & , kz45a, kz46a, kz47a, kz48a, kz49a - & , kz50a, kz51a, kz52a, kz53a, kz54a - & , k125 , k129 , k130 , k131 , k132 - & , k133 , k134 , k135 , k136 , k137 - & , k148 , k149 , k150 , k151 , k152 - & , k153 - & , kz15 , kz16 , kz17 , kz18 , kz19 - & , kz20 , kz21 , kz22 , kz23 , kz24 - & , kz25 , kz26 , kz27 , kz28 , kz29 - & , kz30 , kz31 , kz32 , kz33 , kz34 - & , kz35 , kz36 , kz37 , kz38 , kz39 - & , kz40 , kz41 , kz42 , kz43 , kz44 - & , kz45 , kz46 , kz47 , kz48 , kz49 - & , kz50 , kz51 , kz52 , kz53 , kz54 - & , immulti, imabund, idspecies, itdmulti, idsub - & , metal_loc - & , metal_C13, metal_C20, metal_C25, metal_C30 - & , metal_F13, metal_F15, metal_F50, metal_F80 - & , metal_P170, metal_P200, metal_Y19 - & , SN0_N - & , SN0_fSiM, SN0_fFeM, SN0_fMg2SiO4, SN0_fMgSiO3 - & , SN0_fFe3O4, SN0_fAC, SN0_fSiO2D, SN0_fMgO - & , SN0_fFeS, SN0_fAl2O3 - & , SN0_freforg, SN0_fvolorg, SN0_fH2Oice - & , SN0_r0SiM, SN0_r0FeM, SN0_r0Mg2SiO4, SN0_r0MgSiO3 - & , SN0_r0Fe3O4, SN0_r0AC, SN0_r0SiO2D, SN0_r0MgO - & , SN0_r0FeS, SN0_r0Al2O3 - & , SN0_r0reforg, SN0_r0volorg, SN0_r0H2Oice - & , gr_N, gr_Size, gr_dT, gr_Td - & , SN0_kpSiM, SN0_kpFeM, SN0_kpMg2SiO4, SN0_kpMgSiO3 - & , SN0_kpFe3O4, SN0_kpAC, SN0_kpSiO2D, SN0_kpMgO - & , SN0_kpFeS, SN0_kpAl2O3 - & , SN0_kpreforg, SN0_kpvolorg, SN0_kpH2Oice - & , h2dustSa, h2dustCa, rhoH, grogra, dt - & , kdSiM, kdFeM, kdMg2SiO4 - & , kdMgSiO3, kdFe3O4, kdAC, kdSiO2D, kdMgO, kdFeS - & , kdAl2O3, kdreforg, kdvolorg, kdH2Oice - & , tSiM, tFeM, tMg2SiO4, tMgSiO3, tFe3O4 - & , tAC, tSiO2D, tMgO, tFeS, tAl2O3 - & , treforg, tvolorg, tH2Oice, iuseH2shield - & , iH2shieldcustom, f_shield_custom - & ) - -! Compute dedot and HIdot, the rates of change of de and HI -! (should add itmask to this call) - - call rate_timestep_g( - & dedot, HIdot, ispecies, anydust, - & de, HI, HII, HeI, HeII, HeIII, d, - & HM, H2I, H2II, - & in, jn, kn, is, ie, j, k, - & k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, - & k12, k13, k14, k15, k16, k17, k18, k19, k22, - & k24, k25, k26, k27, k28, k29, k30, - & k50, k51, k52, k53, k54, k55, k56, k57, k58, - & h2dust, ncrn, ncrd1, ncrd2, rhoH, - & k24shield, k25shield, k26shield, - & k28shield, k29shield, k30shield, k31shield, - & iradtrans, irt_honly, - & kphHI, kphHeI, kphHeII, - & itmask, edot, chunit, dom, metal - & , HDI, imchem, CI, OI, OH, CO, H2O - & , idissHDI, kdissHDI, iionZ, kphCI, kphOI - & , idissZ, kdissCO, kdissOH, kdissH2O - & ) - -! move itmask temporary array -! then split cells with low densities -! => Gauss-Seidel scheme -! and with high densities -! => Newton-Raphson scheme - - itmask_tmp = itmask - itmask_nr = itmask - do i = is+1, ie+1 - if ( itmask_tmp(i) .ne. MASK_FALSE ) then - - if ( ( (imetal .eq. 0) - & .and. (ddom(i) .lt. 1.d8) ) - & .or. ( (imetal .eq. 1) - & .and. ( ( (metallicity(i) .le. min_metallicity) - & .and. (ddom(i) .lt. 1.d8) ) - & .or. ( (metallicity(i) .gt. min_metallicity) - & .and. (ddom(i) .lt. 1.d6) ) ) ) ) then - itmask_nr(i) = MASK_FALSE - else - itmask(i) = MASK_FALSE - endif - - endif - enddo - - do i = is+1, ie+1 - if (itmask_nr(i) .ne. MASK_FALSE) then - if ( (icool .eq. 1) .and. (ispecies .gt. 1) .and. - & ((ddom(i) .gt. 1.d7) - & .and.(tgas(i) .gt. 1650.d0)) ) then - imp_eng(i) = 1 - else - imp_eng(i) = 0 - endif - endif - enddo - -! Find timestep that keeps relative chemical changes below 10% - - do i = is+1, ie+1 - if (itmask(i) .ne. MASK_FALSE) then -! Bound from below to prevent numerical errors - - if (abs(dedot(i)) .lt. tiny8) - & dedot(i) = min(tiny,de(i,j,k)) - if (abs(HIdot(i)) .lt. tiny8) - & HIdot(i) = min(tiny,HI(i,j,k)) - -! If the net rate is almost perfectly balanced then set -! it to zero (since it is zero to available precision) - - if (min(abs(k1(i)* de(i,j,k)*HI(i,j,k)), - & abs(k2(i)*HII(i,j,k)*de(i,j,k)))/ - & max(abs(dedot(i)),abs(HIdot(i))) .gt. - & 1.0e6_DKIND) then - dedot(i) = tiny8 - HIdot(i) = tiny8 - endif - -! If the iteration count is high then take the smaller of -! the calculated dedot and last time step's actual dedot. -! This is intended to get around the problem of a low -! electron or HI fraction which is in equilibrium with high -! individual terms (which all nearly cancel). - - if (iter .gt. 50) then - dedot(i) = min(abs(dedot(i)), abs(dedot_prev(i))) - HIdot(i) = min(abs(HIdot(i)), abs(HIdot_prev(i))) - endif - -! compute minimum rate timestep - - olddtit = dtit(i) - dtit(i) = min(abs(0.1_DKIND*de(i,j,k)/dedot(i)), - & abs(0.1_DKIND*HI(i,j,k)/HIdot(i)), - & dt-ttot(i), 0.5_DKIND*dt) - - if (ddom(i) .gt. 1.d8 .and. - & edot(i) .gt. 0._DKIND .and. - & ispecies .gt. 1) then -! Equilibrium value for H is: -! H = (-1._DKIND / (4*k22)) * (k13 - sqrt(8 k13 k22 rho + k13^2)) -! We now want this to change by 10% or less, but we're only -! differentiating by dT. We have de/dt. We need dT/de. -! T = (g-1)*p2d*utem/N; tgas == (g-1)(p2d*utem/N) -! dH_eq / dt = (dH_eq/dT) * (dT/de) * (de/dt) -! dH_eq / dT (see above; we can calculate the derivative here) -! dT / de = utem * (gamma - 1._DKIND) / N == tgas / p2d -! de / dt = edot -! Now we use our estimate of dT/de to get the estimated -! difference in the equilibrium - eqt2 = min(log(tgas(i)) + 0.1_DKIND*dlogtem, t2(i)) - eqtdef = (eqt2 - t1(i))/(t2(i) - t1(i)) - eqk222 = k22a(indixe(i)) + - & (k22a(indixe(i)+1) -k22a(indixe(i)))*eqtdef - eqk132 = k13a(indixe(i)) + - & (k13a(indixe(i)+1) -k13a(indixe(i)))*eqtdef - heq2 = (-1._DKIND / (4._DKIND*eqk222)) * (eqk132- - & sqrt(8._DKIND*eqk132*eqk222* - & fh*d(i,j,k)+eqk132**2._DKIND)) - - eqt1 = max(log(tgas(i)) - 0.1_DKIND*dlogtem, t1(i)) - eqtdef = (eqt1 - t1(i))/(t2(i) - t1(i)) - eqk221 = k22a(indixe(i)) + - & (k22a(indixe(i)+1) -k22a(indixe(i)))*eqtdef - eqk131 = k13a(indixe(i)) + - & (k13a(indixe(i)+1) -k13a(indixe(i)))*eqtdef - heq1 = (-1._DKIND / (4._DKIND*eqk221)) * (eqk131- - & sqrt(8._DKIND*eqk131*eqk221* - & fh*d(i,j,k)+eqk131**2._DKIND)) - - dheq = (abs(heq2-heq1)/(exp(eqt2) - exp(eqt1))) - & * (tgas(i)/p2d(i)) * edot(i) - heq = (-1._DKIND / (4._DKIND*k22(i))) * (k13(i)- - & sqrt(8._DKIND*k13(i)*k22(i)* - & fh*d(i,j,k)+k13(i)**2._DKIND)) - dtit(i) = min(dtit(i), 0.1_DKIND*heq/dheq) - endif - if (iter.gt.10_DKIND) then - dtit(i) = min(olddtit*1.5_DKIND, dtit(i)) - endif - - else if ((itmask_nr(i).ne.MASK_FALSE).and. - & (imp_eng(i).eq.0)) then - dtit(i) = min(abs(0.1_DKIND*e(i,j,k)/edot(i)*d(i,j,k)), - & dt-ttot(i), 0.5_DKIND*dt) - - else if ((itmask_nr(i).ne.MASK_FALSE).and. - & (imp_eng(i).eq.1)) then - dtit(i) = dt - ttot(i) - - else ! itmask - dtit(i) = dt - endif - enddo ! end loop over i - - endif ! end if (ispecies .gt. 0) - -! Compute maximum timestep for cooling/heating - - do i = is+1, ie+1 - if (itmask(i) .ne. MASK_FALSE) then -! Set energy per unit volume of this cell based in the pressure -! (the gamma used here is the right one even for H2 since p2d -! is calculated with this gamma). - - energy = max(p2d(i)/(gamma-1._DKIND), tiny8) - -! If the temperature is at the bottom of the temperature look-up -! table and edot < 0, then shut off the cooling. - - if (tgas(i) .le. 1.01_DKIND*temstart .and. - & edot(i) .lt. 0._DKIND) - & edot(i) = tiny8 - if (abs(edot(i)) .lt. tiny8) edot(i) = tiny8 - -! Compute timestep for 10% change - - dtit(i) = min(real(abs(0.1_DKIND* - & energy/edot(i)), DKIND), - & dt-ttot(i), dtit(i)) - - if (dtit(i) .ne. dtit(i)) then -#ifdef _OPENMP -!$omp critical -#endif - write(6,*) 'HUGE dtit :: ', energy, edot(i), dtit(i), - & dt, ttot(i), abs(0.1_DKIND*energy/edot(i)), - & real(abs(0.1_DKIND*energy/edot(i)), DKIND) -#ifdef _OPENMP -!$omp end critical -#endif - endif - - endif ! itmask - enddo ! end loop over i - -! Update total and gas energy - - if (icool .eq. 1) then - do i = is+1, ie+1 - if (itmask(i) .ne. MASK_FALSE) then - e(i,j,k) = e(i,j,k) + - & real(edot(i)/d(i,j,k)*dtit(i), RKIND) - - endif ! itmask - enddo - endif - - if (ispecies .gt. 0) then - -! Solve rate equations with one linearly implicit Gauss-Seidel -! sweep of a backward Euler method (for all cells specified by -! itmask) - - call step_rate_g(de, HI, HII, HeI, HeII, HeIII, d, - & HM, H2I, H2II, DI, DII, HDI, dtit, - & in, jn, kn, is, ie, j, k, - & ispecies, anydust, - & k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, - & k12, k13, k14, k15, k16, k17, k18, k19, k22, - & k24, k25, k26, k27, k28, k29, k30, - & k50, k51, k52, k53, k54, k55, k56, k57, k58, - & h2dust, rhoH, - & k24shield, k25shield, k26shield, - & k28shield, k29shield, k30shield, k31shield, - & HIp, HIIp, HeIp, HeIIp, HeIIIp, dep, - & HMp, H2Ip, H2IIp, DIp, DIIp, HDIp, - & dedot_prev, HIdot_prev, - & iradtrans, irt_honly, - & kphHI, kphHeI, kphHeII, - & itmask, itmask_metal - & , DM, HDII, HeHII, imetal, metal - & , imchem, idspecies, igrgr, idsub - & , CI, CII, CO, CO2 - & , OI, OH, H2O, O2 - & , SiI, SiOI, SiO2I - & , CH, CH2, COII, OII - & , OHII, H2OII, H3OII, O2II - & , Mg, Al, S, Fe - & , SiM, FeM, Mg2SiO4, MgSiO3, Fe3O4 - & , AC, SiO2D, MgO, FeS, Al2O3 - & , reforg, volorg, H2Oice - & , k125, k129, k130, k131, k132 - & , k133, k134, k135, k136, k137 - & , k148, k149, k150, k151, k152 - & , k153 - & , kz15 , kz16 , kz17 , kz18 , kz19 - & , kz20 , kz21 , kz22 , kz23 , kz24 - & , kz25 , kz26 , kz27 , kz28 , kz29 - & , kz30 , kz31 , kz32 , kz33 , kz34 - & , kz35 , kz36 , kz37 , kz38 , kz39 - & , kz40 , kz41 , kz42 , kz43 , kz44 - & , kz45 , kz46 , kz47 , kz48 , kz49 - & , kz50 , kz51 , kz52 , kz53 , kz54 - & , DMp, HDIIp, HeHIIp - & , CIp, CIIp, COp, CO2p - & , OIp, OHp, H2Op, O2p - & , SiIp, SiOIp, SiO2Ip - & , CHp, CH2p, COIIp, OIIp - & , OHIIp, H2OIIp, H3OIIp, O2IIp - & , Mgp, Alp, Sp, Fep - & , SiMp, FeMp, Mg2SiO4p, MgSiO3p, Fe3O4p - & , ACp, SiO2Dp, MgOp, FeSp, Al2O3p - & , reforgp, volorgp, H2Oicep - & , kdSiM, kdFeM, kdMg2SiO4, kdMgSiO3, kdFe3O4 - & , kdAC, kdSiO2D, kdMgO, kdFeS, kdAl2O3 - & , kdreforg, kdvolorg, kdH2Oice - & , idissHDI, kdissHDI, iionZ, kphCI, kphOI - & , idissZ, kdissCO, kdissOH, kdissH2O - & ) - - -! Solve rate equations with one linearly implicit Gauss-Seidel -! sweep of a backward Euler method (for all cells specified by -! itmask_nr) - - call step_rate_newton_raphson(icool, d, e, u, v, w, de, HI, - & HII, HeI, HeII, HeIII, in, jn, kn, nratec, - & iexpand, ispecies, imetal, imcool, idust, - & idustall, idustfield, is, ie, ih2co, ipiht, - & idustrec, igammah, aye, temstart, temend, utem, - & uxyz, uaye, urho, utim, gamma, fh, z_solar, fgr, - & k1a, k2a, k3a, k4a, k5a, k6a, k7a, k8a, k9a, - & k10a, k11a, k12a, k13a, k13dda, k14a, k15a, k16a, - & k17a, k18a, k19a, k22a, k24, k25, k26, k27, k28, - & k29, k30, k31, k50a, k51a, k52a, k53a, k54a, - & k55a, k56a, k57a, k58a, ndratec, dtemstart, - & dtemend, h2dusta, ncrna, ncrd1a, ncrd2a, ceHIa, - & ceHeIa, ceHeIIa, ciHIa, ciHeIa, ciHeISa, ciHeIIa, - & reHIIa, reHeII1a, reHeII2a, reHeIIIa, brema, - & compa, gammaha, isrf, regra, gamma_isrfa, - & comp_xraya, comp_temp, piHI, piHeI, piHeII, HM, - & H2I, H2II, DI, DII, HDI, metal, dust, hyd01ka, - & h2k01a, vibha, rotha, rotla, gpldla, gphdla, - & hdltea, hdlowa, gaHIa, gaH2a, gaHea, gaHpa, - & gaela, h2ltea, gasgra, iH2shield, iradshield, - & avgsighi, avgsighei, avgsigheii, iradtrans, - & irt_honly, kphHI, kphHeI, kphHeII, kdissH2I, - & photogamma, xH2shield, ih2optical, iciecool, - & ithreebody, ih2cr, ihdcr, ciecoa, icmbTfloor, - & iClHeat, clEleFra, priGridRank, priGridDim, - & priPar1, priPar2, priPar3, priPar4, priPar5, - & priDataSize, priCooling, priHeating, priMMW, - & metGridRank, metGridDim, metPar1, metPar2, - & metPar3, metPar4, metPar5, metDataSize, - & metCooling, metHeating, clnew, iVheat, iMheat, - & Vheat, Mheat, iTfloor, Tfloor_scalar, Tfloor, - & imchem, igrgr, ipcont, tmcool, DM, HDII, HeHII, - & CI, CII, CO, CO2, OI, OH, H2O, O2, SiI, SiOI, - & SiO2I, CH, CH2, COII, OII, OHII, H2OII, H3OII, - & O2II, Mg, Al, S, Fe, SiM, FeM, Mg2SiO4, MgSiO3, - & Fe3O4, AC, SiO2D, MgO, FeS, Al2O3, reforg, - & volorg, H2Oice, k125a, k129a, k130a, k131a, - & k132a, k133a, k134a, k135a, k136a, k137a, k148a, - & k149a, k150a, k151a, k152a, k153a, kz15a, kz16a, - & kz17a, kz18a, kz19a, kz20a, kz21a, kz22a, kz23a, - & kz24a, kz25a, kz26a, kz27a, kz28a, kz29a, kz30a, - & kz31a, kz32a, kz33a, kz34a, kz35a, kz36a, kz37a, - & kz38a, kz39a, kz40a, kz41a, kz42a, kz43a, kz44a, - & kz45a, kz46a, kz47a, kz48a, kz49a, kz50a, kz51a, - & kz52a, kz53a, kz54a, cieY06a, LH2_N, LH2_Size, - & LH2_D, LH2_T, LH2_H, LH2_dD, LH2_dT, LH2_dH, - & LH2_L, LHD_N, LHD_Size, LHD_D, LHD_T, LHD_H, - & LHD_dD, LHD_dT, LHD_dH, LHD_L, LCI_N, LCI_Size, - & LCI_D, LCI_T, LCI_H, LCI_dD, LCI_dT, LCI_dH, - & LCI_L, LCII_N, LCII_Size, LCII_D, LCII_T, LCII_H, - & LCII_dD, LCII_dT, LCII_dH, LCII_L, LOI_N, - & LOI_Size, LOI_D, LOI_T, LOI_H, LOI_dD, LOI_dT, - & LOI_dH, LOI_L, LCO_N, LCO_Size, LCO_D, LCO_T, - & LCO_H, LCO_dD, LCO_dT, LCO_dH, LCO_L, LOH_N, - & LOH_Size, LOH_D, LOH_T, LOH_H, LOH_dD, LOH_dT, - & LOH_dH, LOH_L, LH2O_N, LH2O_Size, LH2O_D, LH2O_T, - & LH2O_H, LH2O_dD, LH2O_dT, LH2O_dH, LH2O_L, - & alphap_N, alphap_Size, alphap_D, alphap_T, - & alphap_dD, alphap_dT, alphap_Data, immulti, - & imabund, idspecies, itdmulti, idsub, metal_loc, - & metal_C13, metal_C20, metal_C25, metal_C30, - & metal_F13, metal_F15, metal_F50, metal_F80, - & metal_P170, metal_P200, metal_Y19, SN0_N, - & SN0_fSiM, SN0_fFeM, SN0_fMg2SiO4, SN0_fMgSiO3, - & SN0_fFe3O4, SN0_fAC, SN0_fSiO2D, SN0_fMgO, - & SN0_fFeS, SN0_fAl2O3, SN0_freforg, SN0_fvolorg, - & SN0_fH2Oice, SN0_r0SiM, SN0_r0FeM, SN0_r0Mg2SiO4, - & SN0_r0MgSiO3, SN0_r0Fe3O4, SN0_r0AC, SN0_r0SiO2D, - & SN0_r0MgO, SN0_r0FeS, SN0_r0Al2O3, SN0_r0reforg, - & SN0_r0volorg, SN0_r0H2Oice, gr_N, gr_Size, gr_dT, - & gr_Td, SN0_kpSiM, SN0_kpFeM, SN0_kpMg2SiO4, - & SN0_kpMgSiO3, SN0_kpFe3O4, SN0_kpAC, SN0_kpSiO2D, - & SN0_kpMgO, SN0_kpFeS, SN0_kpAl2O3, SN0_kpreforg, - & SN0_kpvolorg, SN0_kpH2Oice, h2dustSa, h2dustCa, - & gasgr2a, gamma_isrf2a, grogra, idissHDI, - & kdissHDI, iionZ, kphCI, kphOI, idissZ, kdissCO, - & kdissOH, kdissH2O, iuseH2shield, iisrffield, - & isrf_habing, iH2shieldcustom, f_shield_custom, - & ierror, j, k, iter, dom, comp1, - & comp2, coolunit, tbase1, xbase1, chunit, dx_cgs, - & c_ljeans, indixe, t1, t2, logtem, tdef, dtit, - & p2d, tgas, tgasold, tdust, metallicity, dust2gas, - & rhoH, mmw, mynh, myde, gammaha_eff, gasgr_tdust, - & regr, h2dust, ncrn, ncrd1, ncrd2, tSiM, tFeM, - & tMg2SiO4, tMgSiO3, tFe3O4, tAC, tSiO2D, tMgO, - & tFeS, tAl2O3, treforg, tvolorg, tH2Oice, ceHI, - & ceHeI, ceHeII, ciHI, ciHeI, ciHeIS, ciHeII, - & reHII, reHeII1, reHeII2, reHeIII, brem, edot, - & hyd01k, h2k01, vibh, roth, rotl, gpldl, gphdl, - & hdlte, hdlow, cieco, anydust, itmask_nr, - & itmask_metal, itr, imp_eng - & ) - - endif ! if (ispecies .gt. 0) then - -! return itmask - do i = is+1, ie+1 - itmask(i) = itmask_tmp(i) - enddo - -! Add the timestep to the elapsed time for each cell and find -! minimum elapsed time step in this row - - ttmin = huge8 - do i = is+1, ie+1 - ttot(i) = min(ttot(i) + dtit(i), dt) - if (abs(dt-ttot(i)) .lt. - & tolerance*dt) itmask(i) = MASK_FALSE - if (ttot(i).lt.ttmin) ttmin = ttot(i) - enddo - -! If all cells are done (on this slice), then exit - - if (abs(dt-ttmin) .lt. tolerance*dt) go to 9999 - -! Next subcycle iteration - - enddo ! end of 'do iter = 1, itmax' - - 9999 continue - -! Abort if iteration count exceeds maximum - - if (iter .gt. itmax) then -#ifdef _OPENMP -!$omp critical -#endif - write(0,*) 'inside if statement solve rate cool:',is,ie - write(6,*) 'MULTI_COOL iter > ',itmax,' at j,k =',j,k - write(0,*) 'FATAL error (2) in MULTI_COOL' - write(0,'(" dt = ",1pe10.3," ttmin = ",1pe10.3)') dt, ttmin - write(0,'((16(1pe8.1)))') (dtit(i),i=is+1,ie+1) - write(0,'((16(1pe8.1)))') (ttot(i),i=is+1,ie+1) - write(0,'((16(1pe8.1)))') (edot(i),i=is+1,ie+1) - write(0,'((16(I3)))') (itmask(i),i=is+1,ie+1) - - if (exititmax .eq. 1) then - ierr = 0 - endif -#ifdef _OPENMP -!$omp end critical -#endif -c WARNING_MESSAGE - endif - - if (iter .gt. itmax/2) then -#ifdef _OPENMP -!$omp critical -#endif - write(6,*) 'MULTI_COOL iter,j,k =',iter,j,k -#ifdef _OPENMP -!$omp end critical -#endif - end if -! -! Next j,k -! - enddo ! end of 'do t = 0, dk*dj-1' -#ifdef _OPENMP -!$omp end parallel do -#endif - -! If an error has been produced, return now. - - if (ierr .eq. 0) then - return - endif - -! Convert densities back to comoving from proper - - if (iexpand .eq. 1) then - - factor = real(aye**3, RKIND) - - call scale_fields_g( - & ispecies, imetal, idustfield, imchem, - & imabund, idspecies, immulti, igrgr, idsub, factor, - & is, ie, js, je, ks, ke, in, jn, kn, - & d, de, HI, HII, HeI, HeII, HeIII, - & HM, H2I, H2II, DI, DII, HDI, DM, HDII, HeHII, - & metal, dust, - & CI, CII, CO, CO2, - & OI, OH, H2O, O2, - & SiI, SiOI, SiO2I, - & CH, CH2, COII, OII, - & OHII, H2OII, H3OII, O2II, - & Mg, Al, S, Fe, - & SiM, FeM, Mg2SiO4, MgSiO3, Fe3O4, - & AC, SiO2D, MgO, FeS, Al2O3, - & reforg, volorg, H2Oice, - & metal_loc, metal_C13, metal_C20, metal_C25, metal_C30, - & metal_F13, metal_F15, metal_F50, metal_F80, - & metal_P170, metal_P200, metal_Y19) - - endif - - if (ispecies .gt. 0) then - -! Correct the species to ensure consistency (i.e. type conservation) - -#ifdef ABUNDANCE_CORRECTION - call make_consistent_g(de, HI, HII, HeI, HeII, HeIII, - & HM, H2I, H2II, DI, DII, HDI, metal, dust, - & d, is, ie, js, je, ks, ke, - & in, jn, kn, ispecies, imetal, fh, dtoh - & , idustfield, imchem, igrgr, dom - & , DM, HDII, HeHII - & , CI, CII, CO, CO2 - & , OI, OH, H2O, O2 - & , SiI, SiOI, SiO2I - & , CH, CH2, COII, OII - & , OHII, H2OII, H3OII, O2II - & , Mg, Al, S, Fe - & , SiM, FeM, Mg2SiO4, MgSiO3, Fe3O4 - & , AC, SiO2D, MgO, FeS, Al2O3 - & , reforg, volorg, H2Oice - & , immulti, imabund, idspecies, itdmulti, idsub - & , metal_loc - & , metal_C13, metal_C20, metal_C25, metal_C30 - & , metal_F13, metal_F15, metal_F50, metal_F80 - & , metal_P170, metal_P200, metal_Y19 - & , SN0_N - & , SN0_XC, SN0_XO, SN0_XMg, SN0_XAl, SN0_XSi - & , SN0_XS, SN0_XFe - & , SN0_fC, SN0_fO, SN0_fMg, SN0_fAl, SN0_fSi - & , SN0_fS, SN0_fFe - & ) - - call ceiling_species_g(d, de, HI, HII, HeI, HeII, HeIII, - & HM, H2I, H2II, DI, DII, HDI, metal, dust, - & is, ie, js, je, ks, ke, - & in, jn, kn, ispecies, imetal, idustfield - & , DM, HDII, HeHII - & , imabund, imchem, idspecies, immulti - & , igrgr, idsub - & , CI, CII, CO, CO2 - & , OI, OH, H2O, O2 - & , SiI, SiOI, SiO2I - & , CH, CH2, COII, OII - & , OHII, H2OII, H3OII, O2II - & , Mg, Al, S, Fe - & , SiM, FeM, Mg2SiO4, MgSiO3, Fe3O4 - & , AC, SiO2D, MgO, FeS, Al2O3 - & , reforg, volorg, H2Oice - & , metal_loc - & , metal_C13, metal_C20, metal_C25, metal_C30 - & , metal_F13, metal_F15, metal_F50, metal_F80 - & , metal_P170, metal_P200, metal_Y19) -#endif - - endif - - return - end +! This file originally defined the solve_rate_cool_g subroutine and a +! number of helper subroutines +! - solve_rate_cool_g has now been transcribed to C++ +! - the remaining helper subroutines still need to be transcribed c ----------------------------------------------------------- ! This routine ensures that the species aren't below tiny. From c03fe6aa784a69c8e943b96348ab6729219f8ebc Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 10 Jan 2025 09:25:03 -0500 Subject: [PATCH 021/101] solve_rate_cool_g: move docstrings and remove some now unnecessary comments --- src/clib/solve_rate_cool_g-cpp.C | 172 +------------------------------ src/clib/solve_rate_cool_g-cpp.h | 11 ++ 2 files changed, 12 insertions(+), 171 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index b3b175451..9071c2e41 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -27,122 +27,10 @@ void solve_rate_cool_g( ) { - - // SOLVE MULTI-SPECIES RATE EQUATIONS AND RADIATIVE COOLING - - // written by: Yu Zhang, Peter Anninos and Tom Abel - // date: - // modified1: January, 1996 by Greg Bryan; converted to KRONOS - // modified2: October, 1996 by GB; adapted to AMR - // modified3: May, 1999 by GB and Tom Abel, 3bodyH2, solver, HD - // modified4: June, 2005 by GB to solve rate & cool at same time - // modified5: April, 2009 by JHW to include radiative transfer - // modified6: September, 2009 by BDS to include cloudy cooling - - // PURPOSE: - // Solve the multi-species rate and cool equations. - - // INPUTS: - // icool - flag to update energy from radiative cooling - // in,jn,kn - dimensions of 3D fields - - // d - total density field - // de - electron density field - // HI,HII - H density fields (neutral & ionized) - // HeI/II/III - He density fields - // DI/II - D density fields (neutral & ionized) - // HDI - neutral HD molecule density field - // HM - H- density field - // H2I - H_2 (molecular H) density field - // H2II - H_2+ density field - // metal - metal density field - // dust - dust density field - // kph* - photoionization fields - // gamma* - photoheating fields - // f_shield_custom - custom H2 shielding factor - - // is,ie - start and end indices of active region (zero based) - // iexpand - comoving coordinates flag (0 = off, 1 = on) - // idim - dimensionality (rank) of problem - // ispecies - chemistry module (1 - H/He only, 2 - molecular H, 3 - D) - // imetal - flag if metal field is active (0 = no, 1 = yes) - // imcool - flag if there is metal cooling - // idust - flag for H2 formation on dust grains - // idustall - flag for dust (0 - none, 1 - heating/cooling + H2 form.) - // idustfield - flag if a dust density field is present - // iisrffield - flag if a field for the interstellar radiation field is present - // ih2co - flag to include H2 cooling (1 = on, 0 = off) - // ipiht - flag to include photoionization heating (1 = on, 0 = off) - // idustrec - flag to include dust recombination cooling (1 = on, -1 = off) - // iH2shield - flag for approximate self-shielding of H2 (Wolcott-Green+ 2011) - // iradshield - flag for approximate self-shielding of UV background - // avgsighi - spectrum averaged ionization crs for HI for use with shielding - // avgsighei - spectrum averaged ionization crs for HeI for use with shielding - // avgsigheii - spectrum averaged ionization crs for HeII for use with shielding - // iradtrans - flag to include radiative transfer (1 = on, 0 = off) - // iradcoupled - flag to indicate coupled radiative transfer - // iradstep - flag to indicate intermediate coupled radiative transfer timestep - // irt_honly - flag to indicate applying RT ionization and heating to HI only - // iH2shieldcustom - flag to indicate a custom H2 shielding factor is provided - - // fh - Hydrogen mass fraction (typically 0.76) - // dtoh - Deuterium to H mass ratio - // z_solar - Solar metal mass fraction - // fgr - the local dust to gas ratio (by mass) - // dt - timestep to integrate over - // aye - expansion factor (in code units) - - // utim - time units (i.e. code units to CGS conversion factor) - // uaye - expansion factor conversion factor (uaye = 1/(1+zinit)) - // urho - density units - // uxyz - length units - // utem - temperature(-like) units - - // temstart, temend - start and end of temperature range for rate table - // nratec - dimensions of chemical rate arrays (functions of temperature) - // dtemstart, dtemend - start and end of dust temperature range - // ndratec - extra dimension for H2 formation on dust rate (dust temperature) - - // icmbTfloor - flag to include temperature floor from cmb - // iClHeat - flag to include cloudy heating - // priGridRank - rank of cloudy primordial cooling data grid - // priGridDim - array containing dimensions of cloudy primordial data - // priPar1, priPar2, priPar3 - arrays containing primordial grid parameter values - // priDataSize - total size of flattened 1D primordial cooling data array - // priCooling - primordial cooling data - // priHeating - primordial heating data - // priMMW - primordial mmw data - // metGridRank - rank of cloudy metal cooling data grid - // metGridDim - array containing dimensions of cloudy metal data - // metPar1, metPar2, metPar3 - arrays containing metal grid parameter values - // metDataSize - total size of flattened 1D metal cooling data array - // metCooling - metal cooling data - // metHeating - metal heating data - // iVheat - flag for using volumetric heating rate - // iMheat - flag for using specific heating rate - // Vheat - array of volumetric heating rates - // Mheat - array of specific heating rates - // iTfloor - flag for using temperature floor field - // Tfloor_scalar - scalar temperature floor value - // Tfloor - array of temperature floor values - // itmax - maximum allowed sub-cycle iterations - // exititmax - flag to exit if max iterations exceeded - - // OUTPUTS: - // update chemical rate densities (HI, HII, etc) - - // PARAMETERS: - // mh - H mass in cgs units - - // ----------------------------------------------------------------------- - - // General Arguments int ierror; - // -- removed line (previously just declared arg types) -- - // Density, energy and velocity fields fields grackle::impl::View de(my_fields->e_density, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); @@ -155,63 +43,7 @@ void solve_rate_cool_g( grackle::impl::View kphHI(my_fields->RT_HI_ionization_rate, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - - // H2 self-shielding length-scale field - - - // Interstellar radiation field for dust heating - - - // Custom H2 shielding factor - - - // Cooling tables (coolings rates as a function of temperature) - - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // opacity table - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - - // -- removed line (previously just declared arg types) -- - - // Chemistry tables (rates as a function of temperature) - - // -- removed line (previously just declared arg types) -- - - // Cloudy cooling data - - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- - - // Parameters + // Constants #ifdef GRACKLE_FLOAT_4 const gr_float tolerance = (gr_float)(1.0e-05); @@ -411,8 +243,6 @@ void solve_rate_cool_g( std::vector kz52(my_fields->grid_dimension[0]); std::vector kz53(my_fields->grid_dimension[0]); std::vector kz54(my_fields->grid_dimension[0]); - // -- removed line (previously just declared arg types) -- - // -- removed line (previously just declared arg types) -- std::vector kdSiM(my_fields->grid_dimension[0]); std::vector kdFeM(my_fields->grid_dimension[0]); std::vector kdMg2SiO4(my_fields->grid_dimension[0]); diff --git a/src/clib/solve_rate_cool_g-cpp.h b/src/clib/solve_rate_cool_g-cpp.h index b56f32e91..e1a83e63f 100644 --- a/src/clib/solve_rate_cool_g-cpp.h +++ b/src/clib/solve_rate_cool_g-cpp.h @@ -17,6 +17,17 @@ extern "C" { #endif /* __cplusplus */ // the following function can be called from C or C++ +/// Solve the multi-species rate and cooling equations +/// +/// @par History +/// written by: Yu Zhang, Peter Anninos and Tom Abel +/// modified1: January, 1996 by Greg Bryan; converted to KRONOS +/// modified2: October, 1996 by GB; adapted to AMR +/// modified3: May, 1999 by GB and Tom Abel, 3bodyH2, solver, HD +/// modified4: June, 2005 by GB to solve rate & cool at same time +/// modified5: April, 2009 by JHW to include radiative transfer +/// modified6: September, 2009 by BDS to include cloudy cooling +/// modified7: January, 2025 by Matthew Abruzzo; ported to C++ void solve_rate_cool_g( int* imetal, double* dt, double* utem, double* uxyz, double* urho, int* ierr, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, From ae2c2ebc94624e3a4d52c6cf71c2f5c100ea2e19 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 10 Jan 2025 09:51:24 -0500 Subject: [PATCH 022/101] solve_rate_cool_g: some minor formatting improvements --- src/clib/solve_rate_cool_g-cpp.C | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 9071c2e41..e5db6153e 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -1143,9 +1143,7 @@ void solve_rate_cool_g( if (std::fabs((*dt)-ttmin) < tolerance*(*dt)) { goto label_9999; } - // Next subcycle iteration - - } + } // subcycle iteration loop (for current row) label_9999: @@ -1172,18 +1170,16 @@ label_9999: (*ierr) = 0; } } - // WARNING_MESSAGE } - if (iter > my_chemistry->max_iterations/2) { + if (iter > my_chemistry->max_iterations/2) { // WARNING_MESSAGE OMP_PRAGMA_CRITICAL { eprintf("MULTI_COOL iter,j,k = %d %d %d\n", iter, j, k); } } - - // Next j,k - } + + } // loop over j,k pairs } // OMP_PRAGMA("omp parallel") // If an error has been produced, return now. From d5426e240521953bef1c828c9b05795dd0e12d0e Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 10 Jan 2025 10:06:03 -0500 Subject: [PATCH 023/101] solve_rate_cool_g-cpp: refactor to remove goto The goal in the transcription of solve_rate_cool_g is to avoid significant refactoring, but removing this goto statement is hugely beneficial (goto statements can invoke undefined behavior extremely easily in C++) --- src/clib/solve_rate_cool_g-cpp.C | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index e5db6153e..195cc7a73 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -313,7 +313,7 @@ void solve_rate_cool_g( // Set error indicator - (*ierr) = 1; + (*ierr) = GR_SUCCESS; // Set flag for dust-related options @@ -1139,15 +1139,12 @@ void solve_rate_cool_g( if (ttot[i-1] my_chemistry->max_iterations) { OMP_PRAGMA_CRITICAL @@ -1167,9 +1164,9 @@ label_9999: //_// PORT: write(0,'((16(I3)))') (itmask(i),i=is+1,ie+1) if (my_chemistry->exit_after_iterations_exceeded == 1) { - (*ierr) = 0; + (*ierr) = GR_FAIL; } - } + } // OMP_PRAGMA_CRITICAL } if (iter > my_chemistry->max_iterations/2) { // WARNING_MESSAGE @@ -1184,7 +1181,7 @@ label_9999: // If an error has been produced, return now. - if ((*ierr) == 0) { + if ((*ierr) != GR_SUCCESS) { return; } From 5bf7fae6a2608282704d1ed50d396d6a2973c313 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 10 Jan 2025 10:21:07 -0500 Subject: [PATCH 024/101] solve_rate_cool: directly return an exit code (rather than modify a pointer) --- src/clib/solve_chemistry.c | 8 +++----- src/clib/solve_rate_cool_g-cpp.C | 17 ++++++++--------- src/clib/solve_rate_cool_g-cpp.h | 6 ++++-- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/clib/solve_chemistry.c b/src/clib/solve_chemistry.c index 7316a5425..79c6b209b 100644 --- a/src/clib/solve_chemistry.c +++ b/src/clib/solve_chemistry.c @@ -237,15 +237,13 @@ int local_solve_chemistry(chemistry_data *my_chemistry, /* Call the fortran routine to solve cooling equations. */ - int ierr = 0; - - solve_rate_cool_g( + int ierr = solve_rate_cool_g( &metal_field_present, &dt_value, &temperature_units, &co_length_units, - &co_density_units, &ierr, my_chemistry, my_rates, my_units, my_fields, + &co_density_units, my_chemistry, my_rates, my_units, my_fields, &my_uvb_rates ); - if (ierr == FAIL) { + if (ierr == GR_FAIL) { fprintf(stderr, "Error in solve_rate_cool_g.\n"); } diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 195cc7a73..fcc6b4e47 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -19,8 +19,8 @@ extern "C" { #endif /* __cplusplus */ -void solve_rate_cool_g( - int* imetal, double* dt, double* utem, double* uxyz, double* urho, int* ierr, +int solve_rate_cool_g( + int* imetal, double* dt, double* utem, double* uxyz, double* urho, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, code_units* my_units, grackle_field_data* my_fields, photo_rate_storage* my_uvb_rates @@ -311,9 +311,8 @@ void solve_rate_cool_g( // \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\///////////////////////////////// // ======================================================================= - // Set error indicator - - (*ierr) = GR_SUCCESS; + // Set error indicator (we will return this value) + int ierr = GR_SUCCESS; // Set flag for dust-related options @@ -1164,7 +1163,7 @@ void solve_rate_cool_g( //_// PORT: write(0,'((16(I3)))') (itmask(i),i=is+1,ie+1) if (my_chemistry->exit_after_iterations_exceeded == 1) { - (*ierr) = GR_FAIL; + ierr = GR_FAIL; } } // OMP_PRAGMA_CRITICAL } @@ -1181,8 +1180,8 @@ void solve_rate_cool_g( // If an error has been produced, return now. - if ((*ierr) != GR_SUCCESS) { - return; + if (ierr != GR_SUCCESS) { + return ierr; } // Convert densities back to comoving from proper @@ -1269,7 +1268,7 @@ void solve_rate_cool_g( } - return; + return ierr; } #ifdef __cplusplus diff --git a/src/clib/solve_rate_cool_g-cpp.h b/src/clib/solve_rate_cool_g-cpp.h index e1a83e63f..60b33199b 100644 --- a/src/clib/solve_rate_cool_g-cpp.h +++ b/src/clib/solve_rate_cool_g-cpp.h @@ -28,8 +28,10 @@ extern "C" { /// modified5: April, 2009 by JHW to include radiative transfer /// modified6: September, 2009 by BDS to include cloudy cooling /// modified7: January, 2025 by Matthew Abruzzo; ported to C++ -void solve_rate_cool_g( - int* imetal, double* dt, double* utem, double* uxyz, double* urho, int* ierr, +/// +/// @return Returns GR_SUCCESS or GR_FAIL to indicate whether there was an error +int solve_rate_cool_g( + int* imetal, double* dt, double* utem, double* uxyz, double* urho, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, code_units* my_units, grackle_field_data* my_fields, photo_rate_storage* my_uvb_rates From 7593c0011b04d0470d97b94e45f2a6c980206517 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 10 Jan 2025 10:31:34 -0500 Subject: [PATCH 025/101] solve_rate_cool_g: translate remaining error-msgs --- src/clib/solve_rate_cool_g-cpp.C | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index fcc6b4e47..52ab0b1df 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -1156,11 +1156,19 @@ int solve_rate_cool_g( j, k); printf("FATAL error (2) in MULTI_COOL\n"); - //_// PORT: write(0,'(" dt = ",1pe10.3," ttmin = ",1pe10.3)') dt, ttmin - //_// PORT: write(0,'((16(1pe8.1)))') (dtit(i),i=is+1,ie+1) - //_// PORT: write(0,'((16(1pe8.1)))') (ttot(i),i=is+1,ie+1) - //_// PORT: write(0,'((16(1pe8.1)))') (edot(i),i=is+1,ie+1) - //_// PORT: write(0,'((16(I3)))') (itmask(i),i=is+1,ie+1) + printf("( dt = %.17e ttmin = %.17e )", *dt, ttmin); + grackle::impl::print_contiguous_row_( + dtit.data(), my_fields->grid_start[0], my_fields->grid_end[0]+1 + ); + grackle::impl::print_contiguous_row_( + ttot.data(), my_fields->grid_start[0], my_fields->grid_end[0]+1 + ); + grackle::impl::print_contiguous_row_( + edot.data(), my_fields->grid_start[0], my_fields->grid_end[0]+1 + ); + grackle::impl::print_contiguous_row_( + itmask.data(), my_fields->grid_start[0], my_fields->grid_end[0]+1 + ); if (my_chemistry->exit_after_iterations_exceeded == 1) { ierr = GR_FAIL; From 4e490f00f538511163c3d0333eb1fa6c7f3a11af Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 10 Jan 2025 10:42:28 -0500 Subject: [PATCH 026/101] solve_rate_cool_g: accept imetal, dt args by value --- src/clib/solve_chemistry.c | 2 +- src/clib/solve_rate_cool_g-cpp.C | 50 ++++++++++++++++---------------- src/clib/solve_rate_cool_g-cpp.h | 2 +- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/clib/solve_chemistry.c b/src/clib/solve_chemistry.c index 79c6b209b..b816b1a52 100644 --- a/src/clib/solve_chemistry.c +++ b/src/clib/solve_chemistry.c @@ -238,7 +238,7 @@ int local_solve_chemistry(chemistry_data *my_chemistry, /* Call the fortran routine to solve cooling equations. */ int ierr = solve_rate_cool_g( - &metal_field_present, &dt_value, &temperature_units, &co_length_units, + metal_field_present, dt_value, &temperature_units, &co_length_units, &co_density_units, my_chemistry, my_rates, my_units, my_fields, &my_uvb_rates ); diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 52ab0b1df..6ce766e45 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -20,7 +20,7 @@ extern "C" { #endif /* __cplusplus */ int solve_rate_cool_g( - int* imetal, double* dt, double* utem, double* uxyz, double* urho, + int imetal, double dt, double* utem, double* uxyz, double* urho, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, code_units* my_units, grackle_field_data* my_fields, photo_rate_storage* my_uvb_rates @@ -351,7 +351,7 @@ int solve_rate_cool_g( FORTRAN_NAME(make_consistent_g)(de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, d.data(), &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, imetal, &my_chemistry->HydrogenFractionByMass, &my_chemistry->DeuteriumToHydrogenRatio, + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->HydrogenFractionByMass, &my_chemistry->DeuteriumToHydrogenRatio, &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, &dom, my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, @@ -385,7 +385,7 @@ int solve_rate_cool_g( factor = (gr_float)(std::pow(my_units->a_value,(-3)) ); FORTRAN_NAME(scale_fields_g)( - &my_chemistry->primordial_chemistry, imetal, &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, + &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->multi_metals, &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, &factor, &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], d.data(), de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, @@ -410,7 +410,7 @@ int solve_rate_cool_g( FORTRAN_NAME(ceiling_species_g)(d.data(), de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, imetal, &my_chemistry->use_dust_density_field, + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->use_dust_density_field, my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, &my_chemistry->metal_abundances, &my_chemistry->metal_chemistry, &my_chemistry->dust_species, &my_chemistry->multi_metals, &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, @@ -540,7 +540,7 @@ int solve_rate_cool_g( FORTRAN_NAME(cool1d_multi_g)( d.data(), e.data(), my_fields->x_velocity, my_fields->y_velocity, my_fields->z_velocity, de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, - &my_units->comoving_coordinates, &my_chemistry->primordial_chemistry, imetal, &my_chemistry->metal_cooling, + &my_units->comoving_coordinates, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_chemistry->dust_recombination_cooling, &my_fields->grid_rank, &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, &my_chemistry->ih2co, &my_chemistry->ipiht, &iter, &my_chemistry->photoelectric_heating, &my_units->a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, @@ -681,7 +681,7 @@ int solve_rate_cool_g( &my_chemistry->use_radiative_transfer, my_fields->RT_H2_dissociation_rate, my_fields->H2_self_shielding_length, itmask.data(), itmask_metal.data(), &my_chemistry->HydrogenFractionByMass, my_fields->metal_density, - my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, imetal, &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, + my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, &imetal, &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, @@ -734,7 +734,7 @@ int solve_rate_cool_g( my_rates->SN0_kpFe3O4, my_rates->SN0_kpAC, my_rates->SN0_kpSiO2D, my_rates->SN0_kpMgO, my_rates->SN0_kpFeS, my_rates->SN0_kpAl2O3, my_rates->SN0_kpreforg, my_rates->SN0_kpvolorg, my_rates->SN0_kpH2Oice, - my_rates->h2dustS, my_rates->h2dustC, rhoH.data(), my_rates->grain_growth_rate, dt, + my_rates->h2dustS, my_rates->h2dustC, rhoH.data(), my_rates->grain_growth_rate, &dt, kdSiM.data(), kdFeM.data(), kdMg2SiO4.data(), kdMgSiO3.data(), kdFe3O4.data(), kdAC.data(), kdSiO2D.data(), kdMgO.data(), kdFeS.data(), kdAl2O3.data(), kdreforg.data(), kdvolorg.data(), kdH2Oice.data(), @@ -778,9 +778,9 @@ int solve_rate_cool_g( for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { if ( itmask_tmp[i-1] != MASK_FALSE ) { - if ( ( ((*imetal) == 0) + if ( ( (imetal == 0) && (ddom[i-1] < 1.e8) ) - || ( ((*imetal) == 1) + || ( (imetal == 1) && ( ( (metallicity[i-1] <= min_metallicity) && (ddom[i-1] < 1.e8) ) || ( (metallicity[i-1] > min_metallicity) @@ -843,7 +843,7 @@ int solve_rate_cool_g( olddtit = dtit[i-1]; dtit[i-1] = grackle::impl::fmin(std::fabs(0.1*de(i-1,j-1,k-1)/dedot[i-1]), std::fabs(0.1*HI(i-1,j-1,k-1)/HIdot[i-1]), - (*dt)-ttot[i-1], 0.5*(*dt)); + dt-ttot[i-1], 0.5*dt); if (ddom[i-1] > 1.e8 && edot[i-1] > 0. && @@ -893,14 +893,14 @@ int solve_rate_cool_g( } else if ((itmask_nr[i-1]!=MASK_FALSE) && (imp_eng[i-1]==0)) { dtit[i-1] = grackle::impl::fmin(std::fabs(0.1*e(i-1,j-1,k-1)/edot[i-1]*d(i-1,j-1,k-1)), - (*dt)-ttot[i-1], 0.5*(*dt)); + dt-ttot[i-1], 0.5*dt); } else if ((itmask_nr[i-1]!=MASK_FALSE) && (imp_eng[i-1]==1)) { - dtit[i-1] = (*dt) - ttot[i-1]; + dtit[i-1] = dt - ttot[i-1]; } else { - dtit[i-1] = (*dt); + dtit[i-1] = dt; } } @@ -928,7 +928,7 @@ int solve_rate_cool_g( dtit[i-1] = grackle::impl::fmin((double)(std::fabs(0.1* energy/edot[i-1]) ), - (*dt)-ttot[i-1], dtit[i-1]); + dt-ttot[i-1], dtit[i-1]); if (dtit[i-1] != dtit[i-1]) { OMP_PRAGMA_CRITICAL @@ -937,7 +937,7 @@ int solve_rate_cool_g( energy, edot [ i-1 ], dtit [ i-1 ], - (*dt), + dt, ttot [ i-1 ], std::fabs ( 0.1 * energy / edot [ i-1 ] ), (double) ( std::fabs ( 0.1 * energy / edot [ i-1 ] ) )); @@ -982,7 +982,7 @@ int solve_rate_cool_g( &my_chemistry->use_radiative_transfer, &my_chemistry->radiative_transfer_hydrogen_only, kphHI.data(), my_fields->RT_HeI_ionization_rate, my_fields->RT_HeII_ionization_rate, itmask.data(), itmask_metal.data(), - my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, imetal, my_fields->metal_density, + my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, &imetal, my_fields->metal_density, &my_chemistry->metal_chemistry, &my_chemistry->dust_species, &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, @@ -1029,7 +1029,7 @@ int solve_rate_cool_g( FORTRAN_NAME(step_rate_newton_raphson)(&my_chemistry->with_radiative_cooling, d.data(), e.data(), my_fields->x_velocity, my_fields->y_velocity, my_fields->z_velocity, de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, - &my_units->comoving_coordinates, &my_chemistry->primordial_chemistry, imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, + &my_units->comoving_coordinates, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_fields->grid_start[0], &my_fields->grid_end[0], &my_chemistry->ih2co, &my_chemistry->ipiht, &my_chemistry->dust_recombination_cooling, &my_chemistry->photoelectric_heating, &my_units->a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, utem, uxyz, &my_units->a_units, urho, &my_units->time_units, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, @@ -1132,14 +1132,14 @@ int solve_rate_cool_g( ttmin = huge8; for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - ttot[i-1] = std::fmin(ttot[i-1] + dtit[i-1], (*dt)); - if (std::fabs((*dt)-ttot[i-1]) < - tolerance*(*dt)) { itmask[i-1] = MASK_FALSE; } + ttot[i-1] = std::fmin(ttot[i-1] + dtit[i-1], dt); + if (std::fabs(dt-ttot[i-1]) < + tolerance*dt) { itmask[i-1] = MASK_FALSE; } if (ttot[i-1]grid_start[0], my_fields->grid_end[0]+1 ); @@ -1199,7 +1199,7 @@ int solve_rate_cool_g( factor = (gr_float)(std::pow(my_units->a_value,3) ); FORTRAN_NAME(scale_fields_g)( - &my_chemistry->primordial_chemistry, imetal, &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, + &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->multi_metals, &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, &factor, &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], d.data(), de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, @@ -1228,7 +1228,7 @@ int solve_rate_cool_g( FORTRAN_NAME(make_consistent_g)(de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, d.data(), &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, imetal, &my_chemistry->HydrogenFractionByMass, &my_chemistry->DeuteriumToHydrogenRatio, + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->HydrogenFractionByMass, &my_chemistry->DeuteriumToHydrogenRatio, &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, &dom, my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, @@ -1255,7 +1255,7 @@ int solve_rate_cool_g( FORTRAN_NAME(ceiling_species_g)(d.data(), de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, imetal, &my_chemistry->use_dust_density_field, + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->use_dust_density_field, my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, &my_chemistry->metal_abundances, &my_chemistry->metal_chemistry, &my_chemistry->dust_species, &my_chemistry->multi_metals, &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, diff --git a/src/clib/solve_rate_cool_g-cpp.h b/src/clib/solve_rate_cool_g-cpp.h index 60b33199b..a3f115924 100644 --- a/src/clib/solve_rate_cool_g-cpp.h +++ b/src/clib/solve_rate_cool_g-cpp.h @@ -31,7 +31,7 @@ extern "C" { /// /// @return Returns GR_SUCCESS or GR_FAIL to indicate whether there was an error int solve_rate_cool_g( - int* imetal, double* dt, double* utem, double* uxyz, double* urho, + int imetal, double dt, double *utem, double *uxyz, double *urho, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, code_units* my_units, grackle_field_data* my_fields, photo_rate_storage* my_uvb_rates From 2b7b7c910f816b181ba6aa86bb76336d82aa26e9 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 10 Jan 2025 10:50:19 -0500 Subject: [PATCH 027/101] solve_rate_cool_g: accept utem, uxyz, urho args by value --- src/clib/solve_chemistry.c | 4 ++-- src/clib/solve_rate_cool_g-cpp.C | 16 ++++++++-------- src/clib/solve_rate_cool_g-cpp.h | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/clib/solve_chemistry.c b/src/clib/solve_chemistry.c index b816b1a52..8854cbdae 100644 --- a/src/clib/solve_chemistry.c +++ b/src/clib/solve_chemistry.c @@ -238,8 +238,8 @@ int local_solve_chemistry(chemistry_data *my_chemistry, /* Call the fortran routine to solve cooling equations. */ int ierr = solve_rate_cool_g( - metal_field_present, dt_value, &temperature_units, &co_length_units, - &co_density_units, my_chemistry, my_rates, my_units, my_fields, + metal_field_present, dt_value, temperature_units, co_length_units, + co_density_units, my_chemistry, my_rates, my_units, my_fields, &my_uvb_rates ); diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 6ce766e45..7295da5a7 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -20,7 +20,7 @@ extern "C" { #endif /* __cplusplus */ int solve_rate_cool_g( - int imetal, double dt, double* utem, double* uxyz, double* urho, + int imetal, double dt, double utem, double uxyz, double urho, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, code_units* my_units, grackle_field_data* my_fields, photo_rate_storage* my_uvb_rates @@ -327,12 +327,12 @@ int solve_rate_cool_g( // Set units - dom = (*urho)*(std::pow(my_units->a_value,3))/mh_local_var; + dom = urho*(std::pow(my_units->a_value,3))/mh_local_var; tbase1 = my_units->time_units; - xbase1 = (*uxyz)/(my_units->a_value*my_units->a_units); // uxyz is [x]*a = [x]*[a]*a' ' - dbase1 = (*urho)*std::pow((my_units->a_value*my_units->a_units),3); // urho is [dens]/a^3 = [dens]/([a]*a')^3 ' + xbase1 = uxyz/(my_units->a_value*my_units->a_units); // uxyz is [x]*a = [x]*[a]*a' ' + dbase1 = urho*std::pow((my_units->a_value*my_units->a_units),3); // urho is [dens]/a^3 = [dens]/([a]*a')^3 ' coolunit = (std::pow(my_units->a_units,5) * std::pow(xbase1,2) * std::pow(mh_local_var,2)) / (std::pow(tbase1,3) * dbase1); - uvel = ((*uxyz)/my_units->a_value) / my_units->time_units; + uvel = (uxyz/my_units->a_value) / my_units->time_units; // chunit = (1.60218e-12_DKIND)/(2._DKIND*uvel*uvel*mh) ! 1 eV per H2 formed chunit = (1.60218e-12)/(uvel*uvel*mh_local_var); // 1 eV per REACTION (Feb 2020, Gen Chiaki) @@ -544,7 +544,7 @@ int solve_rate_cool_g( &my_chemistry->h2_on_dust, &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_chemistry->dust_recombination_cooling, &my_fields->grid_rank, &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, &my_chemistry->ih2co, &my_chemistry->ipiht, &iter, &my_chemistry->photoelectric_heating, &my_units->a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, - utem, uxyz, &my_units->a_units, urho, &my_units->time_units, + &utem, &uxyz, &my_units->a_units, &urho, &my_units->time_units, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, my_rates->ceHI, my_rates->ceHeI, my_rates->ceHeII, my_rates->ciHI, my_rates->ciHeI, my_rates->ciHeIS, my_rates->ciHeII, my_rates->reHII, my_rates->reHeII1, @@ -1031,8 +1031,8 @@ int solve_rate_cool_g( HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, &my_units->comoving_coordinates, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_fields->grid_start[0], &my_fields->grid_end[0], &my_chemistry->ih2co, &my_chemistry->ipiht, - &my_chemistry->dust_recombination_cooling, &my_chemistry->photoelectric_heating, &my_units->a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, utem, - uxyz, &my_units->a_units, urho, &my_units->time_units, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, + &my_chemistry->dust_recombination_cooling, &my_chemistry->photoelectric_heating, &my_units->a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &utem, + &uxyz, &my_units->a_units, &urho, &my_units->time_units, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, my_rates->k1, my_rates->k2, my_rates->k3, my_rates->k4, my_rates->k5, my_rates->k6, my_rates->k7, my_rates->k8, my_rates->k9, my_rates->k10, my_rates->k11, my_rates->k12, my_rates->k13, my_rates->k13dd, my_rates->k14, my_rates->k15, my_rates->k16, my_rates->k17, my_rates->k18, my_rates->k19, my_rates->k22, &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k27, &my_uvb_rates->k28, diff --git a/src/clib/solve_rate_cool_g-cpp.h b/src/clib/solve_rate_cool_g-cpp.h index a3f115924..858d0d643 100644 --- a/src/clib/solve_rate_cool_g-cpp.h +++ b/src/clib/solve_rate_cool_g-cpp.h @@ -31,7 +31,7 @@ extern "C" { /// /// @return Returns GR_SUCCESS or GR_FAIL to indicate whether there was an error int solve_rate_cool_g( - int imetal, double dt, double *utem, double *uxyz, double *urho, + int imetal, double dt, double utem, double uxyz, double urho, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, code_units* my_units, grackle_field_data* my_fields, photo_rate_storage* my_uvb_rates From ef11003bfcda5eabad8276bf5343d5aa3f49fa56 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 10 Jan 2025 10:52:13 -0500 Subject: [PATCH 028/101] remove FORTRAN_NAME(solve_rate_cool_g) solve_chemistry.c --- src/clib/solve_chemistry.c | 133 ------------------------------------- 1 file changed, 133 deletions(-) diff --git a/src/clib/solve_chemistry.c b/src/clib/solve_chemistry.c index 8854cbdae..2aa6b0ff3 100644 --- a/src/clib/solve_chemistry.c +++ b/src/clib/solve_chemistry.c @@ -26,139 +26,6 @@ int update_UVbackground_rates(chemistry_data *my_chemistry, photo_rate_storage *my_uvb_rates, code_units *my_units); -extern void FORTRAN_NAME(solve_rate_cool_g)( - int *icool, - gr_float *d, gr_float *e, gr_float *u, gr_float *v, gr_float *w, gr_float *de, - gr_float *HI, gr_float *HII, gr_float *HeI, gr_float *HeII, gr_float *HeIII, - int *in, int *jn, int *kn, int *nratec, int *iexpand, - int *ispecies, int *imetal, int *imcool, int *idust, int *idustall, - int *idustfield, int *idim, - int *is, int *js, int *ks, int *ie, int *je, int *ke, - int *ih2co, int *ipiht, int *idustrec, int *igammah, - double *dx, double *dt, double *aye, double *temstart, double *temend, - double *utem, double *uxyz, double *uaye, double *urho, double *utim, - double *gamma, double *fh, double *dtoh, double *z_solar, double *fgr, - double *k1a, double *k2a, double *k3a, double *k4a, double *k5a, - double *k6a, double *k7a, double *k8a, double *k9a, double *k10a, - double *k11a, double *k12a, double *k13a, double *k13dda, double *k14a, - double *k15a, double *k16a, double *k17a, double *k18a, double *k19a, - double *k22a, double *k24, double *k25, double *k26, double *k27, - double *k28, double *k29, double *k30, double *k31, - double *k50a, double *k51a, double *k52a, double *k53a, double *k54a, - double *k55a, double *k56a, double *k57a, double *k58a, - int *ndratec, double *dtemstart, double *dtemend, double *h2dusta, - double *ncrna, double *ncrd1a, double *ncrd2a, - double *ceHIa, double *ceHeIa, double *ceHeIIa, double *ciHIa, - double *ciHeIa, double *ciHeISa, double *ciHeIIa, - double *reHIIa, double *reHeII1a, double *reHeII2a, double *reHeIIIa, - double *brema, double *compa, double *gammaha, double *isrf, - double *regra, double *gamma_isrfa, double *comp_xraya, double *comp_temp, - double *piHI, double *piHeI, double *piHeII, - gr_float *HM, gr_float *H2I, gr_float *H2II, - gr_float *DI, gr_float *DII, gr_float *HDI, - gr_float *metal, gr_float *dust, - double *hyd01ka, double *h2k01a, double *vibha, - double *rotha, double *rotla, - double *gpldl, double *gphdl, double *HDltea, double *HDlowa, - double *gaHIa, double *gaH2a, double *gaHea, double *gaHpa, double *gaela, - double *h2ltea, double *gasgra, int *iH2shield, - int *iradshield, double *avgsighi, double *avgsighei, double *avgsigheii, - int *iradtrans, int *iradcoupled, int *iradstep, int *irt_honly, - gr_float *kphHI, gr_float *kphHeI, gr_float *kphHeII, gr_float *kdissH2I, - gr_float *photogamma, gr_float *xH2shield, - int *ierr, - int *ih2optical, int *iciecool, int *ithreebody, int *ih2cr, int *ihdcr, - double *ciecoa, - int *icmbTfloor, int *iClHeat, double *clEleFra, - long long *priGridRank, long long *priGridDim, - double *priPar1, double *priPar2, double *priPar3, - double *priPar4, double *priPar5, - long long *priDataSize, double *priCooling, - double *priHeating, double *priMMW, - long long *metGridRank, long long *metGridDim, - double *metPar1, double *metPar2, double *metPar3, - double *metPar4, double *metPar5, - long long *metDataSize, double *metCooling, - double *metHeating, int *clnew, - int *iVheat, int *iMheat, gr_float *Vheat, gr_float *Mheat, - int *iTfloor, gr_float *Tfloor_scalar, gr_float *Tfloor, - int *imchem, int *igrgr, int *ipcont, double *tmcool, - gr_float *DM, gr_float *HDII, gr_float *HeHII, - gr_float *CI, gr_float *CII, gr_float *CO, gr_float *CO2, - gr_float *OI, gr_float *OH, gr_float *H2O, gr_float *O2, - gr_float *SiI, gr_float *SiOI, gr_float *SiO2I, - gr_float *CH, gr_float *CH2, gr_float *COII, gr_float *OII, - gr_float *OHII, gr_float *H2OII, gr_float *H3OII, gr_float *O2II, - gr_float *Mg, gr_float *Al, gr_float *S, gr_float *Fe, - gr_float *SiM, gr_float *FeM, gr_float *Mg2SiO4, gr_float *MgSiO3, gr_float *Fe3O4, - gr_float *AC, gr_float *SiO2D, gr_float *MgO, gr_float *FeS, gr_float *Al2O3, - gr_float *reforg, gr_float *volorg, gr_float *H2Oice, - double *k125a, double *k129a, double *k130a, double *k131a, double *k132a, - double *k133a, double *k134a, double *k135a, double *k136a, double *k137a, - double *k148a, double *k149a, double *k150a, double *k151a, double *k152a, - double *k153a, - double *kz15a, double *kz16a, double *kz17a, double *kz18a, double *kz19a, - double *kz20a, double *kz21a, double *kz22a, double *kz23a, double *kz24a, - double *kz25a, double *kz26a, double *kz27a, double *kz28a, double *kz29a, - double *kz30a, double *kz31a, double *kz32a, double *kz33a, double *kz34a, - double *kz35a, double *kz36a, double *kz37a, double *kz38a, double *kz39a, - double *kz40a, double *kz41a, double *kz42a, double *kz43a, double *kz44a, - double *kz45a, double *kz46a, double *kz47a, double *kz48a, double *kz49a, - double *kz50a, double *kz51a, double *kz52a, double *kz53a, double *kz54a, - double *cieY06, - long long *LH2_N, long long *LH2_Size, - double *LH2_D, double *LH2_T, double *LH2_H, - double *LH2_dD, double *LH2_dT, double *LH2_dH, double *LH2_L, - long long *LHD_N, long long *LHD_Size, - double *LHD_D, double *LHD_T, double *LHD_H, - double *LHD_dD, double *LHD_dT, double *LHD_dH, double *LHD_L, - long long *LCI_N, long long *LCI_Size, - double *LCI_D, double *LCI_T, double *LCI_H, - double *LCI_dD, double *LCI_dT, double *LCI_dH, double *LCI_L, - long long *LCII_N, long long *LCII_Size, - double *LCII_D, double *LCII_T, double *LCII_H, - double *LCII_dD, double *LCII_dT, double *LCII_dH, double *LCII_L, - long long *LOI_N, long long *LOI_Size, - double *LOI_D, double *LOI_T, double *LOI_H, - double *LOI_dD, double *LOI_dT, double *LOI_dH, double *LOI_L, - long long *LCO_N, long long *LCO_Size, - double *LCO_D, double *LCO_T, double *LCO_H, - double *LCO_dD, double *LCO_dT, double *LCO_dH, double *LCO_L, - long long *LOH_N, long long *LOH_Size, - double *LOH_D, double *LOH_T, double *LOH_H, - double *LOH_dD, double *LOH_dT, double *LOH_dH, double *LOH_L, - long long *LH2O_N, long long *LH2O_Size, - double *LH2O_D, double *LH2O_T, double *LH2O_H, - double *LH2O_dD, double *LH2O_dT, double *LH2O_dH, double *LH2O_L, - long long *alphap_N, long long *alphap_Size, - double *alphap_D, double *alphap_T, double *alphap_dD, double *alphap_dT, - double *alphap_Data, - int *immulti, int *imabund, int *idspecies, int *itdmulti, int *idsub, - gr_float *metal_loc, gr_float *metal_C13, gr_float *metal_C20, gr_float *metal_C25, gr_float *metal_C30, - gr_float *metal_F13, gr_float *metal_F15, gr_float *metal_F50, gr_float *metal_F80, - gr_float *metal_P170, gr_float *metal_P200, gr_float *metal_Y19, - int *SN0_N, - double *SN0_XC , double *SN0_XO, double *SN0_XMg, double *SN0_XAl, - double *SN0_XSi, double *SN0_XS, double *SN0_XFe, - double *SN0_fC , double *SN0_fO, double *SN0_fMg, double *SN0_fAl, - double *SN0_fSi, double *SN0_fS, double *SN0_fFe, - double *SN0_fSiM, double *SN0_fFeM, double *SN0_fMg2SiO4, double *SN0_fMgSiO3, double *SN0_fFe3O4, - double *SN0_fAC, double *SN0_fSiO2D, double *SN0_fMgO, double *SN0_fFeS, double *SN0_fAl2O3, - double *SN0_freforg , double *SN0_fvolorg , double *SN0_fH2Oice, - double *SN0_r0SiM, double *SN0_r0FeM, double *SN0_r0Mg2SiO4, double *SN0_r0MgSiO3, double *SN0_r0Fe3O4, - double *SN0_r0AC, double *SN0_r0SiO2D, double *SN0_r0MgO, double *SN0_r0FeS, double *SN0_r0Al2O3, - double *SN0_r0reforg , double *SN0_r0volorg , double *SN0_r0H2Oice, - int *gr_N, int *gr_Size, double *gr_dT, double *gr_Td, - double *SN0_kpSiM, double *SN0_kpFeM, double *SN0_kpMg2SiO4, double *SN0_kpMgSiO3, double *SN0_kpFe3O4, - double *SN0_kpAC, double *SN0_kpSiO2D, double *SN0_kpMgO, double *SN0_kpFeS, double *SN0_kpAl2O3, - double *SN0_kpreforg , double *SN0_kpvolorg , double *SN0_kpH2Oice, - double *h2dustSa, double *h2dustCa, double *gasgr2a, double *gamma_isrf2a, double *grogra, - int *idissHDI, gr_float *kdissHDI, int *iionZ, gr_float *kphCI, gr_float *kphOI, - int *idissZ, gr_float *kdissCO, gr_float *kdissOH, gr_float *kdissH2O, int *iuseH2shield, - int *iisrffield, gr_float* isrf_habing, - int *iH2shieldcustom, gr_float* f_shield_custom, - int *itmax, int *exititmax); - int local_solve_chemistry(chemistry_data *my_chemistry, chemistry_data_storage *my_rates, code_units *my_units, From 05100578b5ccdf178b7462707fbd1144fe6f1369 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 10 Jan 2025 11:04:27 -0500 Subject: [PATCH 029/101] step_rate_newton_raphson: make `ierror` local var When I previously extracted the `step_rate_newton_raphson` subroutine out of `solve_rate_cool_g` (back when the latter was still written in Fortran), I mistakenly thought that `ierror` should be passed as an argument to `step_rate_newton_raphson` from `solve_rate_cool_g` It now became apparent to me that `ierror` is ONLY used within `step_rate_newton_raphson` as a local variable to determine control flow. As I understand it, `step_rate_newton_raphson` has a while loop and basically tries to run through the while-loop until it encounters no errors (and the answer is converged). I don't think `step_rate_newton_raphson` is able to gracefully fail (that is something to be addressed in the future). Consequently, this commit converts `ierror` from an argument of `step_rate_newton_raphson` to an internal local variable --- src/clib/fortran_func_decls.h | 2 +- src/clib/solve_rate_cool_g-cpp.C | 6 +----- src/clib/step_rate_newton_raphson.F | 11 +++++++++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/clib/fortran_func_decls.h b/src/clib/fortran_func_decls.h index a2a000d28..322c68917 100644 --- a/src/clib/fortran_func_decls.h +++ b/src/clib/fortran_func_decls.h @@ -893,7 +893,7 @@ void FORTRAN_NAME(step_rate_newton_raphson)( gr_float* kphOI_data_ptr, int* idissZ, gr_float* kdissCO_data_ptr, gr_float* kdissOH_data_ptr, gr_float* kdissH2O_data_ptr, int* iuseH2shield, int* iisrffield, gr_float* isrf_habing_data_ptr, int* iH2shieldcustom, - gr_float* f_shield_custom_data_ptr, int* ierror, int* j, int* k, int* iter, + gr_float* f_shield_custom_data_ptr, int* j, int* k, int* iter, double* dom, double* comp1, double* comp2, double* coolunit, double* tbase1, double* xbase1, double* chunit, double* dx_cgs, double* c_ljeans, long long* indixe, double* t1, double* t2, double* logtem, double* tdef, diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 7295da5a7..551bb357f 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -27,10 +27,6 @@ int solve_rate_cool_g( ) { - // General Arguments - - int ierror; - // Density, energy and velocity fields fields grackle::impl::View de(my_fields->e_density, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); @@ -1105,7 +1101,7 @@ int solve_rate_cool_g( my_fields->RT_HDI_dissociation_rate, &my_chemistry->radiative_transfer_metal_ionization, my_fields->RT_CI_ionization_rate, my_fields->RT_OI_ionization_rate, &my_chemistry->radiative_transfer_metal_dissociation, my_fields->RT_CO_dissociation_rate, my_fields->RT_OH_dissociation_rate, my_fields->RT_H2O_dissociation_rate, &my_chemistry->radiative_transfer_use_H2_shielding, &my_chemistry->use_isrf_field, my_fields->isrf_habing, &my_chemistry->H2_custom_shielding, my_fields->H2_custom_shielding_factor, - &ierror, &j, &k, &iter, &dom, &comp1, + &j, &k, &iter, &dom, &comp1, &comp2, &coolunit, &tbase1, &xbase1, &chunit, &dx_cgs, &c_ljeans, indixe.data(), t1.data(), t2.data(), logtem.data(), tdef.data(), dtit.data(), p2d.data(), tgas.data(), tgasold.data(), tdust.data(), metallicity.data(), dust2gas.data(), diff --git a/src/clib/step_rate_newton_raphson.F b/src/clib/step_rate_newton_raphson.F index 9aff620e1..dfe2764ba 100644 --- a/src/clib/step_rate_newton_raphson.F +++ b/src/clib/step_rate_newton_raphson.F @@ -76,7 +76,7 @@ subroutine step_rate_newton_raphson(icool, d, e, u, v, w, de, HI, & kdissHDI, iionZ, kphCI, kphOI, idissZ, kdissCO, & kdissOH, kdissH2O, iuseH2shield, iisrffield, & isrf_habing, iH2shieldcustom, f_shield_custom, - & ierror, j, k, iter, dom, comp1, + & j, k, iter, dom, comp1, & comp2, coolunit, tbase1, xbase1, chunit, dx_cgs, & c_ljeans, indixe, t1, t2, logtem, tdef, dtit, & p2d, tgas, tgasold, tdust, metallicity, dust2gas, @@ -101,7 +101,7 @@ subroutine step_rate_newton_raphson(icool, d, e, u, v, w, de, HI, ! General Arguments integer icool, in, jn, kn, is, ie, nratec, iexpand, ih2co, ipiht, - & ispecies, imetal, ierror, imcool, idust, idustall, + & ispecies, imetal, imcool, idust, idustall, & idustfield, idustrec, igammah, ih2optical, iciecool, & ithreebody, ih2cr, ihdcr, ndratec, clnew, iVheat, iMheat, & iTfloor, iH2shield, iradshield, iradtrans, irt_honly, @@ -331,6 +331,13 @@ subroutine step_rate_newton_raphson(icool, d, e, u, v, w, de, HI, MASK_TYPE itmask_nr(in) MASK_TYPE itmask_metal(in) integer itr, imp_eng(in) + +! ierror local variable +! - this variable is only used internally by this subroutine for +! determining control-flow +! - it does NOT report whether or not this function succeeded (maybe +! we should change the name?) + integer ierror ! Local variable integer itr_time integer nsp, isp, jsp, id From 4b0062e700467b44d4d9c5cb5b752de1d2b6a82c Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 10 Jan 2025 11:16:12 -0500 Subject: [PATCH 030/101] step_rate_newton_raphson: make `itr` local var --- src/clib/fortran_func_decls.h | 2 +- src/clib/solve_rate_cool_g-cpp.C | 3 +-- src/clib/step_rate_newton_raphson.F | 6 +++--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/clib/fortran_func_decls.h b/src/clib/fortran_func_decls.h index 322c68917..bd07dc7e5 100644 --- a/src/clib/fortran_func_decls.h +++ b/src/clib/fortran_func_decls.h @@ -909,7 +909,7 @@ void FORTRAN_NAME(step_rate_newton_raphson)( double* reHeIII, double* brem, double* edot, double* hyd01k, double* h2k01, double* vibh, double* roth, double* rotl, double* gpldl, double* gphdl, double* hdlte, double* hdlow, double* cieco, gr_mask_type* anydust, - gr_mask_type* itmask_nr, gr_mask_type* itmask_metal, int* itr, int* imp_eng + gr_mask_type* itmask_nr, gr_mask_type* itmask_metal, int* imp_eng ); diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 551bb357f..23e709193 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -300,7 +300,6 @@ int solve_rate_cool_g( std::vector itmask_tmp(my_fields->grid_dimension[0]); std::vector itmask_nr(my_fields->grid_dimension[0]); std::vector itmask_metal(my_fields->grid_dimension[0]); - int itr; std::vector imp_eng(my_fields->grid_dimension[0]); @@ -1113,7 +1112,7 @@ int solve_rate_cool_g( reHII.data(), reHeII1.data(), reHeII2.data(), reHeIII.data(), brem.data(), edot.data(), hyd01k.data(), h2k01.data(), vibh.data(), roth.data(), rotl.data(), gpldl.data(), gphdl.data(), hdlte.data(), hdlow.data(), cieco.data(), &anydust, itmask_nr.data(), - itmask_metal.data(), &itr, imp_eng.data() + itmask_metal.data(), imp_eng.data() ); } diff --git a/src/clib/step_rate_newton_raphson.F b/src/clib/step_rate_newton_raphson.F index dfe2764ba..cf0b4273d 100644 --- a/src/clib/step_rate_newton_raphson.F +++ b/src/clib/step_rate_newton_raphson.F @@ -88,7 +88,7 @@ subroutine step_rate_newton_raphson(icool, d, e, u, v, w, de, HI, & reHII, reHeII1, reHeII2, reHeIII, brem, edot, & hyd01k, h2k01, vibh, roth, rotl, gpldl, gphdl, & hdlte, hdlow, cieco, anydust, itmask_nr, - & itmask_metal, itr, imp_eng + & itmask_metal, imp_eng & ) ! PURPOSE: @@ -330,7 +330,7 @@ subroutine step_rate_newton_raphson(icool, d, e, u, v, w, de, HI, MASK_TYPE anydust MASK_TYPE itmask_nr(in) MASK_TYPE itmask_metal(in) - integer itr, imp_eng(in) + integer imp_eng(in) ! ierror local variable ! - this variable is only used internally by this subroutine for @@ -339,7 +339,7 @@ subroutine step_rate_newton_raphson(icool, d, e, u, v, w, de, HI, ! we should change the name?) integer ierror ! Local variable - integer itr_time + integer itr, itr_time integer nsp, isp, jsp, id real*8 dspj, err, err_max integer,parameter :: i_eng = 52 From 5c4a7b0f6d1e071f4467959db2b7e67b9b8447ce Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 10 Jan 2025 11:53:41 -0500 Subject: [PATCH 031/101] solve_rate_cool: start replacing individual local variables with previously defined structs --- src/clib/solve_rate_cool_g-cpp.C | 85 +++++++++++++++----------------- 1 file changed, 39 insertions(+), 46 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 23e709193..b4f14dd16 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -11,6 +11,7 @@ #include "grackle.h" #include "fortran_func_decls.h" +#include "internal_types.hpp" #include "utils-cpp.hpp" #include "solve_rate_cool_g-cpp.h" @@ -61,12 +62,7 @@ int solve_rate_cool_g( // row temporaries - std::vector indixe(my_fields->grid_dimension[0]); double olddtit; - std::vector t1(my_fields->grid_dimension[0]); - std::vector t2(my_fields->grid_dimension[0]); - std::vector logtem(my_fields->grid_dimension[0]); - std::vector tdef(my_fields->grid_dimension[0]); std::vector dtit(my_fields->grid_dimension[0]); std::vector ttot(my_fields->grid_dimension[0]); std::vector p2d(my_fields->grid_dimension[0]); @@ -252,20 +248,6 @@ int solve_rate_cool_g( std::vector kdreforg(my_fields->grid_dimension[0]); std::vector kdvolorg(my_fields->grid_dimension[0]); std::vector kdH2Oice(my_fields->grid_dimension[0]); - // grain temperature - std::vector tSiM(my_fields->grid_dimension[0]); - std::vector tFeM(my_fields->grid_dimension[0]); - std::vector tMg2SiO4(my_fields->grid_dimension[0]); - std::vector tMgSiO3(my_fields->grid_dimension[0]); - std::vector tFe3O4(my_fields->grid_dimension[0]); - std::vector tAC(my_fields->grid_dimension[0]); - std::vector tSiO2D(my_fields->grid_dimension[0]); - std::vector tMgO(my_fields->grid_dimension[0]); - std::vector tFeS(my_fields->grid_dimension[0]); - std::vector tAl2O3(my_fields->grid_dimension[0]); - std::vector treforg(my_fields->grid_dimension[0]); - std::vector tvolorg(my_fields->grid_dimension[0]); - std::vector tH2Oice(my_fields->grid_dimension[0]); // Cooling/heating row locals @@ -438,8 +420,6 @@ int solve_rate_cool_g( //_// PORT: !$omp& ttmin, energy, comp1, comp2, //_// PORT: !$omp& heq1, heq2, eqk221, eqk222, eqk131, eqk132, //_// PORT: !$omp& eqt1, eqt2, eqtdef, dheq, heq, - //_// PORT: !$omp& indixe, - //_// PORT: !$omp& t1, t2, logtem, tdef, //_// PORT: !$omp& dtit, ttot, p2d, tgas, tgasold, //_// PORT: !$omp& tdust, metallicity, dust2gas, rhoH, mmw, //_// PORT: !$omp& mynh, myde, gammaha_eff, gasgr_tdust, regr, ddom, @@ -468,7 +448,15 @@ int solve_rate_cool_g( //_// PORT: #endif //_// TODO_USE: OMP_PRAGMA("omp parallel") { - //_// TODO: move relevant variable declarations to here to replace OMP private + // TODO: move more relevant variable declarations to here to replace the + // OMP private-clause + + grackle::impl::GrainSpeciesCollection grain_temperatures = + grackle::impl::new_GrainSpeciesCollection(my_fields->grid_dimension[0]); + + grackle::impl::LogTLinInterpScratchBuf logTlininterp_buf = + grackle::impl::new_LogTLinInterpScratchBuf(my_fields->grid_dimension[0]); + //_// TODO_USE: OMP_PRAGMA("omp for") for (t = 0; t<=(dk * dj - 1); t++) { k = t/dj + my_fields->grid_start[2]+1; @@ -555,7 +543,7 @@ int solve_rate_cool_g( my_rates->H2LTE, my_rates->gas_grain, ceHI.data(), ceHeI.data(), ceHeII.data(), ciHI.data(), ciHeI.data(), ciHeIS.data(), ciHeII.data(), reHII.data(), reHeII1.data(), reHeII2.data(), reHeIII.data(), brem.data(), - indixe.data(), t1.data(), t2.data(), logtem.data(), tdef.data(), edot.data(), + logTlininterp_buf.indixe, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.logtem, logTlininterp_buf.tdef, edot.data(), tgas.data(), tgasold.data(), mmw.data(), p2d.data(), tdust.data(), metallicity.data(), dust2gas.data(), rhoH.data(), mynh.data(), myde.data(), gammaha_eff.data(), gasgr_tdust.data(), regr.data(), @@ -631,9 +619,9 @@ int solve_rate_cool_g( my_rates->SN0_kpFe3O4, my_rates->SN0_kpAC, my_rates->SN0_kpSiO2D, my_rates->SN0_kpMgO, my_rates->SN0_kpFeS, my_rates->SN0_kpAl2O3, my_rates->SN0_kpreforg, my_rates->SN0_kpvolorg, my_rates->SN0_kpH2Oice, - tSiM.data(), tFeM.data(), tMg2SiO4.data(), tMgSiO3.data(), tFe3O4.data(), - tAC.data(), tSiO2D.data(), tMgO.data(), tFeS.data(), tAl2O3.data(), - treforg.data(), tvolorg.data(), tH2Oice.data(), + grain_temperatures.SiM, grain_temperatures.FeM, grain_temperatures.Mg2SiO4, grain_temperatures.MgSiO3, grain_temperatures.Fe3O4, + grain_temperatures.AC, grain_temperatures.SiO2D, grain_temperatures.MgO, grain_temperatures.FeS, grain_temperatures.Al2O3, + grain_temperatures.reforg, grain_temperatures.volorg, grain_temperatures.H2Oice, my_rates->gas_grain2, &my_rates->gamma_isrf2 ); @@ -671,7 +659,7 @@ int solve_rate_cool_g( k58.data(), k13dd.data(), k24shield.data(), k25shield.data(), k26shield.data(), k28shield.data(), k29shield.data(), k30shield.data(), k31shield.data(), h2dust.data(), ncrn.data(), ncrd1.data(), ncrd2.data(), - t1.data(), t2.data(), tdef.data(), logtem.data(), indixe.data(), + logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.tdef, logTlininterp_buf.logtem, logTlininterp_buf.indixe, &dom, &coolunit, &tbase1, &xbase1, &dx_cgs, &c_ljeans, &my_chemistry->use_radiative_transfer, my_fields->RT_H2_dissociation_rate, my_fields->H2_self_shielding_length, itmask.data(), itmask_metal.data(), @@ -733,9 +721,9 @@ int solve_rate_cool_g( kdSiM.data(), kdFeM.data(), kdMg2SiO4.data(), kdMgSiO3.data(), kdFe3O4.data(), kdAC.data(), kdSiO2D.data(), kdMgO.data(), kdFeS.data(), kdAl2O3.data(), kdreforg.data(), kdvolorg.data(), kdH2Oice.data(), - tSiM.data(), tFeM.data(), tMg2SiO4.data(), tMgSiO3.data(), tFe3O4.data(), - tAC.data(), tSiO2D.data(), tMgO.data(), tFeS.data(), tAl2O3.data(), - treforg.data(), tvolorg.data(), tH2Oice.data(), &my_chemistry->radiative_transfer_use_H2_shielding, + grain_temperatures.SiM, grain_temperatures.FeM, grain_temperatures.Mg2SiO4, grain_temperatures.MgSiO3, grain_temperatures.Fe3O4, + grain_temperatures.AC, grain_temperatures.SiO2D, grain_temperatures.MgO, grain_temperatures.FeS, grain_temperatures.Al2O3, + grain_temperatures.reforg, grain_temperatures.volorg, grain_temperatures.H2Oice, &my_chemistry->radiative_transfer_use_H2_shielding, &my_chemistry->H2_custom_shielding, my_fields->H2_custom_shielding_factor ); @@ -854,22 +842,22 @@ int solve_rate_cool_g( // de / dt = edot // Now we use our estimate of dT/de to get the estimated // difference in the equilibrium - eqt2 = std::fmin(std::log(tgas[i-1]) + 0.1*dlogtem, t2[i-1]); - eqtdef = (eqt2 - t1[i-1])/(t2[i-1] - t1[i-1]); - eqk222 = my_rates->k22[indixe[i-1]-1] + - (my_rates->k22[indixe[i-1]+1-1] -my_rates->k22[indixe[i-1]-1])*eqtdef; - eqk132 = my_rates->k13[indixe[i-1]-1] + - (my_rates->k13[indixe[i-1]+1-1] -my_rates->k13[indixe[i-1]-1])*eqtdef; + eqt2 = std::fmin(std::log(tgas[i-1]) + 0.1*dlogtem, logTlininterp_buf.t2[i-1]); + eqtdef = (eqt2 - logTlininterp_buf.t1[i-1])/(logTlininterp_buf.t2[i-1] - logTlininterp_buf.t1[i-1]); + eqk222 = my_rates->k22[logTlininterp_buf.indixe[i-1]-1] + + (my_rates->k22[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k22[logTlininterp_buf.indixe[i-1]-1])*eqtdef; + eqk132 = my_rates->k13[logTlininterp_buf.indixe[i-1]-1] + + (my_rates->k13[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k13[logTlininterp_buf.indixe[i-1]-1])*eqtdef; heq2 = (-1. / (4.*eqk222)) * (eqk132- std::sqrt(8.*eqk132*eqk222* my_chemistry->HydrogenFractionByMass*d(i-1,j-1,k-1)+std::pow(eqk132,2.))); - eqt1 = std::fmax(std::log(tgas[i-1]) - 0.1*dlogtem, t1[i-1]); - eqtdef = (eqt1 - t1[i-1])/(t2[i-1] - t1[i-1]); - eqk221 = my_rates->k22[indixe[i-1]-1] + - (my_rates->k22[indixe[i-1]+1-1] -my_rates->k22[indixe[i-1]-1])*eqtdef; - eqk131 = my_rates->k13[indixe[i-1]-1] + - (my_rates->k13[indixe[i-1]+1-1] -my_rates->k13[indixe[i-1]-1])*eqtdef; + eqt1 = std::fmax(std::log(tgas[i-1]) - 0.1*dlogtem, logTlininterp_buf.t1[i-1]); + eqtdef = (eqt1 - logTlininterp_buf.t1[i-1])/(logTlininterp_buf.t2[i-1] - logTlininterp_buf.t1[i-1]); + eqk221 = my_rates->k22[logTlininterp_buf.indixe[i-1]-1] + + (my_rates->k22[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k22[logTlininterp_buf.indixe[i-1]-1])*eqtdef; + eqk131 = my_rates->k13[logTlininterp_buf.indixe[i-1]-1] + + (my_rates->k13[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k13[logTlininterp_buf.indixe[i-1]-1])*eqtdef; heq1 = (-1. / (4.*eqk221)) * (eqk131- std::sqrt(8.*eqk131*eqk221* my_chemistry->HydrogenFractionByMass*d(i-1,j-1,k-1)+std::pow(eqk131,2.))); @@ -1102,12 +1090,12 @@ int solve_rate_cool_g( my_fields->isrf_habing, &my_chemistry->H2_custom_shielding, my_fields->H2_custom_shielding_factor, &j, &k, &iter, &dom, &comp1, &comp2, &coolunit, &tbase1, &xbase1, &chunit, &dx_cgs, - &c_ljeans, indixe.data(), t1.data(), t2.data(), logtem.data(), tdef.data(), dtit.data(), + &c_ljeans, logTlininterp_buf.indixe, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.logtem, logTlininterp_buf.tdef, dtit.data(), p2d.data(), tgas.data(), tgasold.data(), tdust.data(), metallicity.data(), dust2gas.data(), rhoH.data(), mmw.data(), mynh.data(), myde.data(), gammaha_eff.data(), gasgr_tdust.data(), - regr.data(), h2dust.data(), ncrn.data(), ncrd1.data(), ncrd2.data(), tSiM.data(), tFeM.data(), - tMg2SiO4.data(), tMgSiO3.data(), tFe3O4.data(), tAC.data(), tSiO2D.data(), tMgO.data(), - tFeS.data(), tAl2O3.data(), treforg.data(), tvolorg.data(), tH2Oice.data(), ceHI.data(), + regr.data(), h2dust.data(), ncrn.data(), ncrd1.data(), ncrd2.data(), grain_temperatures.SiM, grain_temperatures.FeM, + grain_temperatures.Mg2SiO4, grain_temperatures.MgSiO3, grain_temperatures.Fe3O4, grain_temperatures.AC, grain_temperatures.SiO2D, grain_temperatures.MgO, + grain_temperatures.FeS, grain_temperatures.Al2O3, grain_temperatures.reforg, grain_temperatures.volorg, grain_temperatures.H2Oice, ceHI.data(), ceHeI.data(), ceHeII.data(), ciHI.data(), ciHeI.data(), ciHeIS.data(), ciHeII.data(), reHII.data(), reHeII1.data(), reHeII2.data(), reHeIII.data(), brem.data(), edot.data(), hyd01k.data(), h2k01.data(), vibh.data(), roth.data(), rotl.data(), gpldl.data(), gphdl.data(), @@ -1179,6 +1167,11 @@ int solve_rate_cool_g( } } // loop over j,k pairs + + // cleanup manually allocated temporaries + grackle::impl::drop_GrainSpeciesCollection(&grain_temperatures); + grackle::impl::drop_LogTLinInterpScratchBuf(&logTlininterp_buf); + } // OMP_PRAGMA("omp parallel") // If an error has been produced, return now. From b78447bf409df04ef8530c84a879437b4fadcfd9 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 10 Jan 2025 12:16:18 -0500 Subject: [PATCH 032/101] solve_rate_cool: replace more local variables with previously defined structs --- src/clib/solve_rate_cool_g-cpp.C | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index b4f14dd16..cb96de15a 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -67,17 +67,11 @@ int solve_rate_cool_g( std::vector ttot(my_fields->grid_dimension[0]); std::vector p2d(my_fields->grid_dimension[0]); std::vector tgas(my_fields->grid_dimension[0]); - std::vector tgasold(my_fields->grid_dimension[0]); std::vector tdust(my_fields->grid_dimension[0]); std::vector metallicity(my_fields->grid_dimension[0]); std::vector dust2gas(my_fields->grid_dimension[0]); std::vector rhoH(my_fields->grid_dimension[0]); std::vector mmw(my_fields->grid_dimension[0]); - std::vector mynh(my_fields->grid_dimension[0]); - std::vector myde(my_fields->grid_dimension[0]); - std::vector gammaha_eff(my_fields->grid_dimension[0]); - std::vector gasgr_tdust(my_fields->grid_dimension[0]); - std::vector regr(my_fields->grid_dimension[0]); std::vector ddom(my_fields->grid_dimension[0]); // Rate equation row temporaries @@ -420,9 +414,9 @@ int solve_rate_cool_g( //_// PORT: !$omp& ttmin, energy, comp1, comp2, //_// PORT: !$omp& heq1, heq2, eqk221, eqk222, eqk131, eqk132, //_// PORT: !$omp& eqt1, eqt2, eqtdef, dheq, heq, - //_// PORT: !$omp& dtit, ttot, p2d, tgas, tgasold, + //_// PORT: !$omp& dtit, ttot, p2d, tgas, //_// PORT: !$omp& tdust, metallicity, dust2gas, rhoH, mmw, - //_// PORT: !$omp& mynh, myde, gammaha_eff, gasgr_tdust, regr, ddom, + //_// PORT: !$omp& ddom, //_// PORT: !$omp& olddtit, //_// PORT: !$omp& HIp, HIIp, HeIp, HeIIp, HeIIIp, //_// PORT: !$omp& HMp, H2Ip, H2IIp, @@ -457,6 +451,9 @@ int solve_rate_cool_g( grackle::impl::LogTLinInterpScratchBuf logTlininterp_buf = grackle::impl::new_LogTLinInterpScratchBuf(my_fields->grid_dimension[0]); + grackle::impl::Cool1DMultiScratchBuf cool1dmulti_buf = + grackle::impl::new_Cool1DMultiScratchBuf(my_fields->grid_dimension[0]); + //_// TODO_USE: OMP_PRAGMA("omp for") for (t = 0; t<=(dk * dj - 1); t++) { k = t/dj + my_fields->grid_start[2]+1; @@ -544,9 +541,9 @@ int solve_rate_cool_g( ceHI.data(), ceHeI.data(), ceHeII.data(), ciHI.data(), ciHeI.data(), ciHeIS.data(), ciHeII.data(), reHII.data(), reHeII1.data(), reHeII2.data(), reHeIII.data(), brem.data(), logTlininterp_buf.indixe, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.logtem, logTlininterp_buf.tdef, edot.data(), - tgas.data(), tgasold.data(), mmw.data(), p2d.data(), tdust.data(), metallicity.data(), - dust2gas.data(), rhoH.data(), mynh.data(), myde.data(), - gammaha_eff.data(), gasgr_tdust.data(), regr.data(), + tgas.data(), cool1dmulti_buf.tgasold, mmw.data(), p2d.data(), tdust.data(), metallicity.data(), + dust2gas.data(), rhoH.data(), cool1dmulti_buf.mynh, cool1dmulti_buf.myde, + cool1dmulti_buf.gammaha_eff, cool1dmulti_buf.gasgr_tdust, cool1dmulti_buf.regr, &my_chemistry->self_shielding_method, &my_uvb_rates->crsHI, &my_uvb_rates->crsHeI, &my_uvb_rates->crsHeII, &my_uvb_rates->k24, &my_uvb_rates->k26, &my_chemistry->use_radiative_transfer, my_fields->RT_heating_rate, @@ -1091,9 +1088,9 @@ int solve_rate_cool_g( &j, &k, &iter, &dom, &comp1, &comp2, &coolunit, &tbase1, &xbase1, &chunit, &dx_cgs, &c_ljeans, logTlininterp_buf.indixe, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.logtem, logTlininterp_buf.tdef, dtit.data(), - p2d.data(), tgas.data(), tgasold.data(), tdust.data(), metallicity.data(), dust2gas.data(), - rhoH.data(), mmw.data(), mynh.data(), myde.data(), gammaha_eff.data(), gasgr_tdust.data(), - regr.data(), h2dust.data(), ncrn.data(), ncrd1.data(), ncrd2.data(), grain_temperatures.SiM, grain_temperatures.FeM, + p2d.data(), tgas.data(), cool1dmulti_buf.tgasold, tdust.data(), metallicity.data(), dust2gas.data(), + rhoH.data(), mmw.data(), cool1dmulti_buf.mynh, cool1dmulti_buf.myde, cool1dmulti_buf.gammaha_eff, cool1dmulti_buf.gasgr_tdust, + cool1dmulti_buf.regr, h2dust.data(), ncrn.data(), ncrd1.data(), ncrd2.data(), grain_temperatures.SiM, grain_temperatures.FeM, grain_temperatures.Mg2SiO4, grain_temperatures.MgSiO3, grain_temperatures.Fe3O4, grain_temperatures.AC, grain_temperatures.SiO2D, grain_temperatures.MgO, grain_temperatures.FeS, grain_temperatures.Al2O3, grain_temperatures.reforg, grain_temperatures.volorg, grain_temperatures.H2Oice, ceHI.data(), ceHeI.data(), ceHeII.data(), ciHI.data(), ciHeI.data(), ciHeIS.data(), ciHeII.data(), @@ -1171,6 +1168,7 @@ int solve_rate_cool_g( // cleanup manually allocated temporaries grackle::impl::drop_GrainSpeciesCollection(&grain_temperatures); grackle::impl::drop_LogTLinInterpScratchBuf(&logTlininterp_buf); + grackle::impl::drop_Cool1DMultiScratchBuf(&cool1dmulti_buf); } // OMP_PRAGMA("omp parallel") From 43560afd669f84335f1b276ad57ecbfec1ab5a70 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 10 Jan 2025 12:28:41 -0500 Subject: [PATCH 033/101] replace local variables with the last previously defined struct. --- src/clib/solve_rate_cool_g-cpp.C | 56 ++++++++++---------------------- 1 file changed, 17 insertions(+), 39 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index cb96de15a..6d5ed4697 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -245,29 +245,7 @@ int solve_rate_cool_g( // Cooling/heating row locals - std::vector ceHI(my_fields->grid_dimension[0]); - std::vector ceHeI(my_fields->grid_dimension[0]); - std::vector ceHeII(my_fields->grid_dimension[0]); - std::vector ciHI(my_fields->grid_dimension[0]); - std::vector ciHeI(my_fields->grid_dimension[0]); - std::vector ciHeIS(my_fields->grid_dimension[0]); - std::vector ciHeII(my_fields->grid_dimension[0]); - std::vector reHII(my_fields->grid_dimension[0]); - std::vector reHeII1(my_fields->grid_dimension[0]); - std::vector reHeII2(my_fields->grid_dimension[0]); - std::vector reHeIII(my_fields->grid_dimension[0]); - std::vector brem(my_fields->grid_dimension[0]); std::vector edot(my_fields->grid_dimension[0]); - std::vector hyd01k(my_fields->grid_dimension[0]); - std::vector h2k01(my_fields->grid_dimension[0]); - std::vector vibh(my_fields->grid_dimension[0]); - std::vector roth(my_fields->grid_dimension[0]); - std::vector rotl(my_fields->grid_dimension[0]); - std::vector gpldl(my_fields->grid_dimension[0]); - std::vector gphdl(my_fields->grid_dimension[0]); - std::vector hdlte(my_fields->grid_dimension[0]); - std::vector hdlow(my_fields->grid_dimension[0]); - std::vector cieco(my_fields->grid_dimension[0]); // Iteration mask @@ -432,12 +410,7 @@ int solve_rate_cool_g( //_// PORT: !$omp& k50, k51, k52, k53, k54, //_// PORT: !$omp& k55, k56, k57, k58, k13dd, h2dust, //_// PORT: !$omp& ncrn, ncrd1, ncrd2, - //_// PORT: !$omp& ceHI, ceHeI, ceHeII, - //_// PORT: !$omp& ciHI, ciHeI, ciHeIS, ciHeII, - //_// PORT: !$omp& reHII, reHeII1, reHeII2, reHeIII, - //_// PORT: !$omp& brem, edot, - //_// PORT: !$omp& hyd01k, h2k01, vibh, roth, rotl, - //_// PORT: !$omp& gpldl, gphdl, hdlte, hdlow, cieco, + //_// PORT: !$omp& edot, //_// PORT: !$omp& itmask, itmask_metal ) //_// PORT: #endif //_// TODO_USE: OMP_PRAGMA("omp parallel") @@ -454,6 +427,9 @@ int solve_rate_cool_g( grackle::impl::Cool1DMultiScratchBuf cool1dmulti_buf = grackle::impl::new_Cool1DMultiScratchBuf(my_fields->grid_dimension[0]); + grackle::impl::CoolHeatScratchBuf coolingheating_buf = + grackle::impl::new_CoolHeatScratchBuf(my_fields->grid_dimension[0]); + //_// TODO_USE: OMP_PRAGMA("omp for") for (t = 0; t<=(dk * dj - 1); t++) { k = t/dj + my_fields->grid_start[2]+1; @@ -533,13 +509,13 @@ int solve_rate_cool_g( &my_uvb_rates->piHI, &my_uvb_rates->piHeI, &my_uvb_rates->piHeII, &comp1, &comp2, my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, my_rates->hyd01k, my_rates->h2k01, my_rates->vibh, my_rates->roth, my_rates->rotl, - hyd01k.data(), h2k01.data(), vibh.data(), roth.data(), rotl.data(), - my_rates->GP99LowDensityLimit, my_rates->GP99HighDensityLimit, gpldl.data(), gphdl.data(), - my_rates->HDlte, my_rates->HDlow, hdlte.data(), hdlow.data(), + coolingheating_buf.hyd01k, coolingheating_buf.h2k01, coolingheating_buf.vibh, coolingheating_buf.roth, coolingheating_buf.rotl, + my_rates->GP99LowDensityLimit, my_rates->GP99HighDensityLimit, coolingheating_buf.gpldl, coolingheating_buf.gphdl, + my_rates->HDlte, my_rates->HDlow, coolingheating_buf.hdlte, coolingheating_buf.hdlow, my_rates->GAHI, my_rates->GAH2, my_rates->GAHe, my_rates->GAHp, my_rates->GAel, my_rates->H2LTE, my_rates->gas_grain, - ceHI.data(), ceHeI.data(), ceHeII.data(), ciHI.data(), ciHeI.data(), ciHeIS.data(), ciHeII.data(), - reHII.data(), reHeII1.data(), reHeII2.data(), reHeIII.data(), brem.data(), + coolingheating_buf.ceHI, coolingheating_buf.ceHeI, coolingheating_buf.ceHeII, coolingheating_buf.ciHI, coolingheating_buf.ciHeI, coolingheating_buf.ciHeIS, coolingheating_buf.ciHeII, + coolingheating_buf.reHII, coolingheating_buf.reHeII1, coolingheating_buf.reHeII2, coolingheating_buf.reHeIII, coolingheating_buf.brem, logTlininterp_buf.indixe, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.logtem, logTlininterp_buf.tdef, edot.data(), tgas.data(), cool1dmulti_buf.tgasold, mmw.data(), p2d.data(), tdust.data(), metallicity.data(), dust2gas.data(), rhoH.data(), cool1dmulti_buf.mynh, cool1dmulti_buf.myde, @@ -547,7 +523,7 @@ int solve_rate_cool_g( &my_chemistry->self_shielding_method, &my_uvb_rates->crsHI, &my_uvb_rates->crsHeI, &my_uvb_rates->crsHeII, &my_uvb_rates->k24, &my_uvb_rates->k26, &my_chemistry->use_radiative_transfer, my_fields->RT_heating_rate, - &my_chemistry->h2_optical_depth_approximation, &my_chemistry->cie_cooling, &my_chemistry->h2_cooling_rate, &my_chemistry->hd_cooling_rate, my_rates->cieco, cieco.data(), + &my_chemistry->h2_optical_depth_approximation, &my_chemistry->cie_cooling, &my_chemistry->h2_cooling_rate, &my_chemistry->hd_cooling_rate, my_rates->cieco, coolingheating_buf.cieco, &my_chemistry->cmb_temperature_floor, &my_chemistry->UVbackground, &my_chemistry->cloudy_electron_fraction_factor, &my_rates->cloudy_primordial.grid_rank, my_rates->cloudy_primordial.grid_dimension, my_rates->cloudy_primordial.grid_parameters[0], my_rates->cloudy_primordial.grid_parameters[1], my_rates->cloudy_primordial.grid_parameters[2], my_rates->cloudy_primordial.grid_parameters[3], my_rates->cloudy_primordial.grid_parameters[4], @@ -1092,11 +1068,11 @@ int solve_rate_cool_g( rhoH.data(), mmw.data(), cool1dmulti_buf.mynh, cool1dmulti_buf.myde, cool1dmulti_buf.gammaha_eff, cool1dmulti_buf.gasgr_tdust, cool1dmulti_buf.regr, h2dust.data(), ncrn.data(), ncrd1.data(), ncrd2.data(), grain_temperatures.SiM, grain_temperatures.FeM, grain_temperatures.Mg2SiO4, grain_temperatures.MgSiO3, grain_temperatures.Fe3O4, grain_temperatures.AC, grain_temperatures.SiO2D, grain_temperatures.MgO, - grain_temperatures.FeS, grain_temperatures.Al2O3, grain_temperatures.reforg, grain_temperatures.volorg, grain_temperatures.H2Oice, ceHI.data(), - ceHeI.data(), ceHeII.data(), ciHI.data(), ciHeI.data(), ciHeIS.data(), ciHeII.data(), - reHII.data(), reHeII1.data(), reHeII2.data(), reHeIII.data(), brem.data(), edot.data(), - hyd01k.data(), h2k01.data(), vibh.data(), roth.data(), rotl.data(), gpldl.data(), gphdl.data(), - hdlte.data(), hdlow.data(), cieco.data(), &anydust, itmask_nr.data(), + grain_temperatures.FeS, grain_temperatures.Al2O3, grain_temperatures.reforg, grain_temperatures.volorg, grain_temperatures.H2Oice, coolingheating_buf.ceHI, + coolingheating_buf.ceHeI, coolingheating_buf.ceHeII, coolingheating_buf.ciHI, coolingheating_buf.ciHeI, coolingheating_buf.ciHeIS, coolingheating_buf.ciHeII, + coolingheating_buf.reHII, coolingheating_buf.reHeII1, coolingheating_buf.reHeII2, coolingheating_buf.reHeIII, coolingheating_buf.brem, edot.data(), + coolingheating_buf.hyd01k, coolingheating_buf.h2k01, coolingheating_buf.vibh, coolingheating_buf.roth, coolingheating_buf.rotl, coolingheating_buf.gpldl, coolingheating_buf.gphdl, + coolingheating_buf.hdlte, coolingheating_buf.hdlow, coolingheating_buf.cieco, &anydust, itmask_nr.data(), itmask_metal.data(), imp_eng.data() ); @@ -1169,6 +1145,8 @@ int solve_rate_cool_g( grackle::impl::drop_GrainSpeciesCollection(&grain_temperatures); grackle::impl::drop_LogTLinInterpScratchBuf(&logTlininterp_buf); grackle::impl::drop_Cool1DMultiScratchBuf(&cool1dmulti_buf); + grackle::impl::drop_CoolHeatScratchBuf(&coolingheating_buf); + } // OMP_PRAGMA("omp parallel") From 141d11d0aac014fa99f7fb1735fbb0c6314a81b3 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Mon, 13 Jan 2025 08:54:09 -0500 Subject: [PATCH 034/101] solve_rate_cool_g: move some variable declarations to location where variable is initialized. --- src/clib/solve_rate_cool_g-cpp.C | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 6d5ed4697..20b07448a 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -57,7 +57,7 @@ int solve_rate_cool_g( int t, dj, dk; double ttmin, dom, energy, comp1, comp2; double coolunit, dbase1, tbase1, xbase1, chunit, uvel; - double heq1, heq2, eqk221, eqk222, eqk131, eqk132, eqt1, eqt2, eqtdef, dheq, heq, dlogtem, dx_cgs, c_ljeans, min_metallicity; + double eqt1, eqt2, eqtdef, heq, dlogtem, dx_cgs, c_ljeans, min_metallicity; gr_float factor; // row temporaries @@ -390,8 +390,7 @@ int solve_rate_cool_g( //_// PORT: !$omp parallel do schedule(runtime) private( //_// PORT: !$omp& i, j, k, iter, //_// PORT: !$omp& ttmin, energy, comp1, comp2, - //_// PORT: !$omp& heq1, heq2, eqk221, eqk222, eqk131, eqk132, - //_// PORT: !$omp& eqt1, eqt2, eqtdef, dheq, heq, + //_// PORT: !$omp& eqt1, eqt2, eqtdef, heq, //_// PORT: !$omp& dtit, ttot, p2d, tgas, //_// PORT: !$omp& tdust, metallicity, dust2gas, rhoH, mmw, //_// PORT: !$omp& ddom, @@ -817,25 +816,25 @@ int solve_rate_cool_g( // difference in the equilibrium eqt2 = std::fmin(std::log(tgas[i-1]) + 0.1*dlogtem, logTlininterp_buf.t2[i-1]); eqtdef = (eqt2 - logTlininterp_buf.t1[i-1])/(logTlininterp_buf.t2[i-1] - logTlininterp_buf.t1[i-1]); - eqk222 = my_rates->k22[logTlininterp_buf.indixe[i-1]-1] + + double eqk222 = my_rates->k22[logTlininterp_buf.indixe[i-1]-1] + (my_rates->k22[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k22[logTlininterp_buf.indixe[i-1]-1])*eqtdef; - eqk132 = my_rates->k13[logTlininterp_buf.indixe[i-1]-1] + + double eqk132 = my_rates->k13[logTlininterp_buf.indixe[i-1]-1] + (my_rates->k13[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k13[logTlininterp_buf.indixe[i-1]-1])*eqtdef; - heq2 = (-1. / (4.*eqk222)) * (eqk132- + double heq2 = (-1. / (4.*eqk222)) * (eqk132- std::sqrt(8.*eqk132*eqk222* my_chemistry->HydrogenFractionByMass*d(i-1,j-1,k-1)+std::pow(eqk132,2.))); eqt1 = std::fmax(std::log(tgas[i-1]) - 0.1*dlogtem, logTlininterp_buf.t1[i-1]); eqtdef = (eqt1 - logTlininterp_buf.t1[i-1])/(logTlininterp_buf.t2[i-1] - logTlininterp_buf.t1[i-1]); - eqk221 = my_rates->k22[logTlininterp_buf.indixe[i-1]-1] + + double eqk221 = my_rates->k22[logTlininterp_buf.indixe[i-1]-1] + (my_rates->k22[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k22[logTlininterp_buf.indixe[i-1]-1])*eqtdef; - eqk131 = my_rates->k13[logTlininterp_buf.indixe[i-1]-1] + + double eqk131 = my_rates->k13[logTlininterp_buf.indixe[i-1]-1] + (my_rates->k13[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k13[logTlininterp_buf.indixe[i-1]-1])*eqtdef; - heq1 = (-1. / (4.*eqk221)) * (eqk131- + double heq1 = (-1. / (4.*eqk221)) * (eqk131- std::sqrt(8.*eqk131*eqk221* my_chemistry->HydrogenFractionByMass*d(i-1,j-1,k-1)+std::pow(eqk131,2.))); - dheq = (std::fabs(heq2-heq1)/(std::exp(eqt2) - std::exp(eqt1))) + double dheq = (std::fabs(heq2-heq1)/(std::exp(eqt2) - std::exp(eqt1))) * (tgas[i-1]/p2d[i-1]) * edot[i-1]; heq = (-1. / (4.*k22[i-1])) * (k13[i-1]- std::sqrt(8.*k13[i-1]*k22[i-1]* From b916a103526613ec3c428ef8b2d4d933d12c9cbe Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Mon, 13 Jan 2025 08:58:15 -0500 Subject: [PATCH 035/101] solve_rate_cool_g: move some more variable declarations to scope where variable is initialized/used. --- src/clib/solve_rate_cool_g-cpp.C | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 20b07448a..986220a3a 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -57,7 +57,7 @@ int solve_rate_cool_g( int t, dj, dk; double ttmin, dom, energy, comp1, comp2; double coolunit, dbase1, tbase1, xbase1, chunit, uvel; - double eqt1, eqt2, eqtdef, heq, dlogtem, dx_cgs, c_ljeans, min_metallicity; + double dlogtem, dx_cgs, c_ljeans, min_metallicity; gr_float factor; // row temporaries @@ -390,7 +390,6 @@ int solve_rate_cool_g( //_// PORT: !$omp parallel do schedule(runtime) private( //_// PORT: !$omp& i, j, k, iter, //_// PORT: !$omp& ttmin, energy, comp1, comp2, - //_// PORT: !$omp& eqt1, eqt2, eqtdef, heq, //_// PORT: !$omp& dtit, ttot, p2d, tgas, //_// PORT: !$omp& tdust, metallicity, dust2gas, rhoH, mmw, //_// PORT: !$omp& ddom, @@ -814,8 +813,8 @@ int solve_rate_cool_g( // de / dt = edot // Now we use our estimate of dT/de to get the estimated // difference in the equilibrium - eqt2 = std::fmin(std::log(tgas[i-1]) + 0.1*dlogtem, logTlininterp_buf.t2[i-1]); - eqtdef = (eqt2 - logTlininterp_buf.t1[i-1])/(logTlininterp_buf.t2[i-1] - logTlininterp_buf.t1[i-1]); + double eqt2 = std::fmin(std::log(tgas[i-1]) + 0.1*dlogtem, logTlininterp_buf.t2[i-1]); + double eqtdef = (eqt2 - logTlininterp_buf.t1[i-1])/(logTlininterp_buf.t2[i-1] - logTlininterp_buf.t1[i-1]); double eqk222 = my_rates->k22[logTlininterp_buf.indixe[i-1]-1] + (my_rates->k22[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k22[logTlininterp_buf.indixe[i-1]-1])*eqtdef; double eqk132 = my_rates->k13[logTlininterp_buf.indixe[i-1]-1] + @@ -824,7 +823,7 @@ int solve_rate_cool_g( std::sqrt(8.*eqk132*eqk222* my_chemistry->HydrogenFractionByMass*d(i-1,j-1,k-1)+std::pow(eqk132,2.))); - eqt1 = std::fmax(std::log(tgas[i-1]) - 0.1*dlogtem, logTlininterp_buf.t1[i-1]); + double eqt1 = std::fmax(std::log(tgas[i-1]) - 0.1*dlogtem, logTlininterp_buf.t1[i-1]); eqtdef = (eqt1 - logTlininterp_buf.t1[i-1])/(logTlininterp_buf.t2[i-1] - logTlininterp_buf.t1[i-1]); double eqk221 = my_rates->k22[logTlininterp_buf.indixe[i-1]-1] + (my_rates->k22[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k22[logTlininterp_buf.indixe[i-1]-1])*eqtdef; @@ -836,7 +835,7 @@ int solve_rate_cool_g( double dheq = (std::fabs(heq2-heq1)/(std::exp(eqt2) - std::exp(eqt1))) * (tgas[i-1]/p2d[i-1]) * edot[i-1]; - heq = (-1. / (4.*k22[i-1])) * (k13[i-1]- + double heq = (-1. / (4.*k22[i-1])) * (k13[i-1]- std::sqrt(8.*k13[i-1]*k22[i-1]* my_chemistry->HydrogenFractionByMass*d(i-1,j-1,k-1)+std::pow(k13[i-1],2.))); dtit[i-1] = std::fmin(dtit[i-1], 0.1*heq/dheq); From b9f535b3a4d92427eedeb5070d438a683e6625f3 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Mon, 13 Jan 2025 09:53:05 -0500 Subject: [PATCH 036/101] solve_rate_cool_g: extract some logic into a helper function called `calc_Heq_div_dHeqdt_` --- src/clib/solve_rate_cool_g-cpp.C | 128 ++++++++++++++++++++++--------- 1 file changed, 91 insertions(+), 37 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 986220a3a..6305385b5 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -16,6 +16,88 @@ #include "solve_rate_cool_g-cpp.h" +/// Computes the timescale given by `ndens_Heq / (d ndens_Heq / d t)` +/// +/// This is used to help compute the subcycle timestep when using a primordial +/// chemistry solver +/// +/// @param my_chemistry holds a number of configuration parameters +/// @param my_rates holds assorted rate data. In this function, this is being +/// used to specify some the interpolation tables of some relevant reaction +/// rates (they are tabulated with respect to logT) +/// @param dlogtem Specifies the constant spacing shared by the relevant rate +/// interpolation tables +/// @param logTlininterp_buf Specifies the information related to the position +/// in the logT interpolations (for a number of chemistry zones) +/// @param k13, k22 1D arrays specifying the previously looked up, local values +/// of the k13 and k22 rates. +/// @param local_rho specifies the local (total) mass density +/// @param tgas, p2d, edot 1D arrays containing local values of temperature, +/// pressure divided by density, and the time derivative of internal energy. +/// @param i Specifies the index of the relevant zone in the 1D array. (**BE +/// AWARE:** this is a 1-based index for historical reasons) +/// +/// @note +/// The `static` annotation indicates that this function is only visible to the +/// current translation unit +static double calc_Heq_div_dHeqdt_( + const chemistry_data* my_chemistry, + const chemistry_data_storage* my_rates, + double dlogtem, + const grackle::impl::LogTLinInterpScratchBuf logTlininterp_buf, + const double* k13, + const double* k22, + double local_rho, + const double* tgas, + const double* p2d, + const double* edot, + int i +) { + + // Equilibrium value for H is: + // Heq = (-1._DKIND / (4*k22)) * (k13 - sqrt(8 k13 k22 rho + k13^2)) + // We want to know dH_eq/dt. + // - We can trivially get dH_eq/dT. + // - We have de/dt. + // - We need dT/de. + // + // T = (g-1)*p2d*utem/N; tgas == (g-1)(p2d*utem/N) + // dH_eq / dt = (dH_eq/dT) * (dT/de) * (de/dt) + // dH_eq / dT (see above; we can calculate the derivative here) + // dT / de = utem * (gamma - 1._DKIND) / N == tgas / p2d + // de / dt = edot + // Now we use our estimate of dT/de to get the estimated + // difference in the equilibrium + double eqt2 = std::fmin(std::log(tgas[i-1]) + 0.1*dlogtem, logTlininterp_buf.t2[i-1]); + double eqtdef = (eqt2 - logTlininterp_buf.t1[i-1])/(logTlininterp_buf.t2[i-1] - logTlininterp_buf.t1[i-1]); + double eqk222 = my_rates->k22[logTlininterp_buf.indixe[i-1]-1] + + (my_rates->k22[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k22[logTlininterp_buf.indixe[i-1]-1])*eqtdef; + double eqk132 = my_rates->k13[logTlininterp_buf.indixe[i-1]-1] + + (my_rates->k13[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k13[logTlininterp_buf.indixe[i-1]-1])*eqtdef; + double heq2 = (-1. / (4.*eqk222)) * (eqk132- + std::sqrt(8.*eqk132*eqk222* + my_chemistry->HydrogenFractionByMass*local_rho+ + std::pow(eqk132,2.))); + + double eqt1 = std::fmax(std::log(tgas[i-1]) - 0.1*dlogtem, logTlininterp_buf.t1[i-1]); + eqtdef = (eqt1 - logTlininterp_buf.t1[i-1])/(logTlininterp_buf.t2[i-1] - logTlininterp_buf.t1[i-1]); + double eqk221 = my_rates->k22[logTlininterp_buf.indixe[i-1]-1] + + (my_rates->k22[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k22[logTlininterp_buf.indixe[i-1]-1])*eqtdef; + double eqk131 = my_rates->k13[logTlininterp_buf.indixe[i-1]-1] + + (my_rates->k13[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k13[logTlininterp_buf.indixe[i-1]-1])*eqtdef; + double heq1 = (-1. / (4.*eqk221)) * (eqk131- + std::sqrt(8.*eqk131*eqk221* + my_chemistry->HydrogenFractionByMass*local_rho+std::pow(eqk131,2.))); + + double dheq = (std::fabs(heq2-heq1)/(std::exp(eqt2) - std::exp(eqt1))) + * (tgas[i-1]/p2d[i-1]) * edot[i-1]; + double heq = (-1. / (4.*k22[i-1])) * (k13[i-1]- + std::sqrt(8.*k13[i-1]*k22[i-1]* + my_chemistry->HydrogenFractionByMass*local_rho+std::pow(k13[i-1],2.))); + + return heq / dheq; +} + #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ @@ -802,43 +884,15 @@ int solve_rate_cool_g( if (ddom[i-1] > 1.e8 && edot[i-1] > 0. && my_chemistry->primordial_chemistry > 1) { - // Equilibrium value for H is: - // H = (-1._DKIND / (4*k22)) * (k13 - sqrt(8 k13 k22 rho + k13^2)) - // We now want this to change by 10% or less, but we're only - // differentiating by dT. We have de/dt. We need dT/de. - // T = (g-1)*p2d*utem/N; tgas == (g-1)(p2d*utem/N) - // dH_eq / dt = (dH_eq/dT) * (dT/de) * (de/dt) - // dH_eq / dT (see above; we can calculate the derivative here) - // dT / de = utem * (gamma - 1._DKIND) / N == tgas / p2d - // de / dt = edot - // Now we use our estimate of dT/de to get the estimated - // difference in the equilibrium - double eqt2 = std::fmin(std::log(tgas[i-1]) + 0.1*dlogtem, logTlininterp_buf.t2[i-1]); - double eqtdef = (eqt2 - logTlininterp_buf.t1[i-1])/(logTlininterp_buf.t2[i-1] - logTlininterp_buf.t1[i-1]); - double eqk222 = my_rates->k22[logTlininterp_buf.indixe[i-1]-1] + - (my_rates->k22[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k22[logTlininterp_buf.indixe[i-1]-1])*eqtdef; - double eqk132 = my_rates->k13[logTlininterp_buf.indixe[i-1]-1] + - (my_rates->k13[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k13[logTlininterp_buf.indixe[i-1]-1])*eqtdef; - double heq2 = (-1. / (4.*eqk222)) * (eqk132- - std::sqrt(8.*eqk132*eqk222* - my_chemistry->HydrogenFractionByMass*d(i-1,j-1,k-1)+std::pow(eqk132,2.))); - - double eqt1 = std::fmax(std::log(tgas[i-1]) - 0.1*dlogtem, logTlininterp_buf.t1[i-1]); - eqtdef = (eqt1 - logTlininterp_buf.t1[i-1])/(logTlininterp_buf.t2[i-1] - logTlininterp_buf.t1[i-1]); - double eqk221 = my_rates->k22[logTlininterp_buf.indixe[i-1]-1] + - (my_rates->k22[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k22[logTlininterp_buf.indixe[i-1]-1])*eqtdef; - double eqk131 = my_rates->k13[logTlininterp_buf.indixe[i-1]-1] + - (my_rates->k13[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k13[logTlininterp_buf.indixe[i-1]-1])*eqtdef; - double heq1 = (-1. / (4.*eqk221)) * (eqk131- - std::sqrt(8.*eqk131*eqk221* - my_chemistry->HydrogenFractionByMass*d(i-1,j-1,k-1)+std::pow(eqk131,2.))); - - double dheq = (std::fabs(heq2-heq1)/(std::exp(eqt2) - std::exp(eqt1))) - * (tgas[i-1]/p2d[i-1]) * edot[i-1]; - double heq = (-1. / (4.*k22[i-1])) * (k13[i-1]- - std::sqrt(8.*k13[i-1]*k22[i-1]* - my_chemistry->HydrogenFractionByMass*d(i-1,j-1,k-1)+std::pow(k13[i-1],2.))); - dtit[i-1] = std::fmin(dtit[i-1], 0.1*heq/dheq); + // here, we ensure that that the equilibrium mass density of + // Hydrogen changes by 10% or less + double Heq_div_dHeqdt = calc_Heq_div_dHeqdt_( + my_chemistry, my_rates, dlogtem, logTlininterp_buf, + k13.data(), k22.data(), d(i-1,j-1,k-1), tgas.data(), + p2d.data(), edot.data(), i + ); + + dtit[i-1] = std::fmin(dtit[i-1], 0.1*Heq_div_dHeqdt); } if (iter>10LL) { dtit[i-1] = std::fmin(olddtit*1.5, dtit[i-1]); From 7816dcd10fa765723e001e36621aadc474538fc6 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 15 Jan 2025 08:09:32 -0500 Subject: [PATCH 037/101] introduce SpLUT lookup table --- src/clib/LUT.hpp | 123 ++++++++++++++++++++++++++++++++++++ src/clib/internal_types.C | 19 ++++++ src/clib/internal_types.hpp | 40 ++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 src/clib/LUT.hpp diff --git a/src/clib/LUT.hpp b/src/clib/LUT.hpp new file mode 100644 index 000000000..bc5ef4bde --- /dev/null +++ b/src/clib/LUT.hpp @@ -0,0 +1,123 @@ +// See LICENSE file for license and copyright information + +/// @file LUT.hpp +/// @brief Declares some lookup-tables used internally by Grackle + +#ifndef LUT_HPP +#define LUT_HPP + +#ifndef __cplusplus +#error "This file can only be read by a c++ compiler" +#endif + +// This namespace holds enumerators defined for every species. The enumerator +// values can be used in a lookup table. +// - the idea here is that we can create arrays to hold data for every species +// (rather than structs). +// - in this picture, we can use the enumerator values to lookup the value +// corresponding to a given species (for example, we could use `SpLUT::HI` +// to look up values related to HI). This essentially gives us all the +// benefits of a struct (instead of an array). +// - if we are using an array to organize data instead of a struct, we can +// then write certain operations that are identical for all species (e.g. +// allocating buffers, deallocating buffers, initializing buffers) in a much +// more concise (more maintainable) way. +// +// IMPORTANT: this is a private implementation detail. We should never +// unintentionally expose this information directly in the public API (i.e. we +// want to maintain our flexibility). With that said, there are hypothetical +// scenarios where we might deliberately expose it (for performance purposes) +// -- but we should think long and hard about it before we do +// +// Why use a namespace? +// -------------------- +// - it lets us use short/simple names for each enumerator without feat of +// collision +// - we later have the option to convert SpLUT from a namespace to a struct +// (it still contains the enumerators). +// - This would be useful if we wanted to write a template function that +// accepts different choices of LUTs as a template argument (without +// modifying any existing code). This would be hypothetically useful if we +// wanted to create specializations of the code that only have LUTs for +// a subset of species (e.g. based on primordial_chemistry == 1) +// - we currently have no plans to do this. +// +// Other Thoughts +// -------------- +// - maybe we should store the dust species in a separate lookup table? +// - adopting data-structures that leverage this LUT, is probably an important +// stepping stone to implementing a system where reaction rates are +// dynamically specified. After adopting such a system, we may not even need +// a lookup table any more (this all assumes, of course, that such a system +// is adequately performant) +namespace SpLUT{ + +// in the future, we may want to reimplement the following in terms of the +// XMacros provided in grackle_field_data_fdatamembers.def (or we may need to +// slightly revise the system?) +enum { + e, + HI, + HII, + HeI, + HeII, + HeIII, + + HM, + H2I, + H2II, + + DI, + DII, + HDI, + + DM, + HDII, + HeHII, + + CI, + CII, + CO, + CO2, + OI, + OH, + H2O, + O2, + SiI, + SiOI, + SiO2I, + CH, + CH2, + COII, + OII, + OHII, + H2OII, + H3OII, + O2II, + + Mg, + Al, + S, + Fe, + + SiM, + FeM, + Mg2SiO4, + MgSiO3, + Fe3O4, + AC, + SiO2D, + MgO, + FeS, + Al2O3, + reforg, + volorg, + H2Oice, + + NUM_ENTRIES // <- this is always last (so it specifies the number of species) +}; // enum + +} // SpLUT namespace + + +#endif /* LUT_HPP */ diff --git a/src/clib/internal_types.C b/src/clib/internal_types.C index d5f4ca660..e1a2cfccf 100644 --- a/src/clib/internal_types.C +++ b/src/clib/internal_types.C @@ -191,4 +191,23 @@ void grackle::impl::drop_GrainSpeciesCollection( for_each_grainspeciescol_member(ptr, &cleanup_member_, NULL); } +// ----------------------------------------------------------------- +grackle::impl::SpeciesCollection grackle::impl::new_SpeciesCollection( + int nelem +) { + GRIMPL_REQUIRE(nelem > 0, "nelem must be positive"); + grackle::impl::SpeciesCollection out; + double* ptr = (double*)malloc(sizeof(double) * nelem * SpLUT::NUM_ENTRIES); + for (int i = 0; i < SpLUT::NUM_ENTRIES; i++) { + out.data[i] = ptr + (i * nelem); + } + return out; +} + +void grackle::impl::drop_SpeciesCollection( + grackle::impl::SpeciesCollection *ptr +) { + // since we only allocate a single pointer, we only need to call free once + free(ptr->data[0]); +} diff --git a/src/clib/internal_types.hpp b/src/clib/internal_types.hpp index f620b9d0d..d80be9efd 100644 --- a/src/clib/internal_types.hpp +++ b/src/clib/internal_types.hpp @@ -98,6 +98,8 @@ #error "This file must be used by a c++ compiler" #endif +#include "LUT.hpp" + namespace grackle::impl { /// Holds 1D arrays used for cooling and heating @@ -244,6 +246,44 @@ GrainSpeciesCollection new_GrainSpeciesCollection(int nelem); /// This effectively invokes a destructor void drop_GrainSpeciesCollection(GrainSpeciesCollection*); + +/// holds properties about each kind of species +/// +/// The basic premise is that we can use `SpLUT::` to lookup values +/// for the desired species +/// +/// @note +/// The way this is currently a lot like a struct of arrays (i.e. there is an +/// extra level of indirection). +/// - For concreteness, to access index `idx` of the data for HeI, you +/// would write `obj.data[SpLUT::HeI][idx]`. +/// - in terms of performance, this is very similar to what came before (and +/// is good enough for now) +/// - in the long term, it would be even better to replace this with a +/// `View`, but I want to wait until after we have transcribed the +/// bulk of grackle before we do that. +/// +/// @note +/// At the time of writing, both this data structure and the +/// GrainSpeciesCollection data structure both reserve space for each of the +/// dust species. Maybe this data structure shouldn't do this? (If we remove +/// the grain species, maybe we rename this so that it is called +/// ChemSpeciesCollection?) +struct SpeciesCollection { + double* data[SpLUT::NUM_ENTRIES]; +}; + + +/// allocates the contents of a new SpeciesCollection +/// +/// @param nelem The number of elements in each buffer +SpeciesCollection new_SpeciesCollection(int nelem); + +/// performs cleanup of the contents of SpeciesCollection +/// +/// This effectively invokes a destructor +void drop_SpeciesCollection(SpeciesCollection*); + } // namespace grackle::impl #endif /* INTERNAL_TYPES_HPP */ From 59e8db5a27a9e078866a37e134d7c15e112eff58 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 15 Jan 2025 08:49:01 -0500 Subject: [PATCH 038/101] solve_rate_cool: replace ~50 temporary variables with `species_tmpdens` These variables are all used to track temporary densities. In the process, I effectively changed the dtype of the grain-densities from `gr_float` to `double` (which is more consistent with the other dtypes used elsewhere) --- src/clib/solve_rate_cool_g-cpp.C | 86 ++++++++------------------------ 1 file changed, 20 insertions(+), 66 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 6305385b5..1f7259da1 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -158,21 +158,9 @@ int solve_rate_cool_g( // Rate equation row temporaries - std::vector HIp(my_fields->grid_dimension[0]); - std::vector HIIp(my_fields->grid_dimension[0]); - std::vector HeIp(my_fields->grid_dimension[0]); - std::vector HeIIp(my_fields->grid_dimension[0]); - std::vector HeIIIp(my_fields->grid_dimension[0]); - std::vector HMp(my_fields->grid_dimension[0]); - std::vector H2Ip(my_fields->grid_dimension[0]); - std::vector H2IIp(my_fields->grid_dimension[0]); - std::vector dep(my_fields->grid_dimension[0]); std::vector dedot(my_fields->grid_dimension[0]); std::vector HIdot(my_fields->grid_dimension[0]); std::vector dedot_prev(my_fields->grid_dimension[0]); - std::vector DIp(my_fields->grid_dimension[0]); - std::vector DIIp(my_fields->grid_dimension[0]); - std::vector HDIp(my_fields->grid_dimension[0]); std::vector HIdot_prev(my_fields->grid_dimension[0]); std::vector k24shield(my_fields->grid_dimension[0]); std::vector k25shield(my_fields->grid_dimension[0]); @@ -215,45 +203,6 @@ int solve_rate_cool_g( std::vector ncrn(my_fields->grid_dimension[0]); std::vector ncrd1(my_fields->grid_dimension[0]); std::vector ncrd2(my_fields->grid_dimension[0]); - std::vector DMp(my_fields->grid_dimension[0]); - std::vector HDIIp(my_fields->grid_dimension[0]); - std::vector HeHIIp(my_fields->grid_dimension[0]); - std::vector CIp(my_fields->grid_dimension[0]); - std::vector CIIp(my_fields->grid_dimension[0]); - std::vector COp(my_fields->grid_dimension[0]); - std::vector CO2p(my_fields->grid_dimension[0]); - std::vector OIp(my_fields->grid_dimension[0]); - std::vector OHp(my_fields->grid_dimension[0]); - std::vector H2Op(my_fields->grid_dimension[0]); - std::vector O2p(my_fields->grid_dimension[0]); - std::vector SiIp(my_fields->grid_dimension[0]); - std::vector SiOIp(my_fields->grid_dimension[0]); - std::vector SiO2Ip(my_fields->grid_dimension[0]); - std::vector CHp(my_fields->grid_dimension[0]); - std::vector CH2p(my_fields->grid_dimension[0]); - std::vector COIIp(my_fields->grid_dimension[0]); - std::vector OIIp(my_fields->grid_dimension[0]); - std::vector OHIIp(my_fields->grid_dimension[0]); - std::vector H2OIIp(my_fields->grid_dimension[0]); - std::vector H3OIIp(my_fields->grid_dimension[0]); - std::vector O2IIp(my_fields->grid_dimension[0]); - std::vector Mgp(my_fields->grid_dimension[0]); - std::vector Alp(my_fields->grid_dimension[0]); - std::vector Sp(my_fields->grid_dimension[0]); - std::vector Fep(my_fields->grid_dimension[0]); - std::vector SiMp(my_fields->grid_dimension[0]); - std::vector FeMp(my_fields->grid_dimension[0]); - std::vector Mg2SiO4p(my_fields->grid_dimension[0]); - std::vector MgSiO3p(my_fields->grid_dimension[0]); - std::vector Fe3O4p(my_fields->grid_dimension[0]); - std::vector ACp(my_fields->grid_dimension[0]); - std::vector SiO2Dp(my_fields->grid_dimension[0]); - std::vector MgOp(my_fields->grid_dimension[0]); - std::vector FeSp(my_fields->grid_dimension[0]); - std::vector Al2O3p(my_fields->grid_dimension[0]); - std::vector reforgp(my_fields->grid_dimension[0]); - std::vector volorgp(my_fields->grid_dimension[0]); - std::vector H2Oicep(my_fields->grid_dimension[0]); std::vector k125(my_fields->grid_dimension[0]); std::vector k129(my_fields->grid_dimension[0]); @@ -476,10 +425,8 @@ int solve_rate_cool_g( //_// PORT: !$omp& tdust, metallicity, dust2gas, rhoH, mmw, //_// PORT: !$omp& ddom, //_// PORT: !$omp& olddtit, - //_// PORT: !$omp& HIp, HIIp, HeIp, HeIIp, HeIIIp, - //_// PORT: !$omp& HMp, H2Ip, H2IIp, //_// PORT: !$omp& dep, dedot,HIdot, dedot_prev, - //_// PORT: !$omp& DIp, DIIp, HDIp, HIdot_prev, + //_// PORT: !$omp& HIdot_prev, //_// PORT: !$omp& k24shield, k25shield, k26shield, //_// PORT: !$omp& k28shield, k29shield, k30shield, //_// PORT: !$omp& k31shield, @@ -510,6 +457,12 @@ int solve_rate_cool_g( grackle::impl::CoolHeatScratchBuf coolingheating_buf = grackle::impl::new_CoolHeatScratchBuf(my_fields->grid_dimension[0]); + // buffers in the following data structure are used to temporarily hold + // the evolved density of various species as we evolve over a subcycle + grackle::impl::SpeciesCollection species_tmpdens = + grackle::impl::new_SpeciesCollection(my_fields->grid_dimension[0]); + + //_// TODO_USE: OMP_PRAGMA("omp for") for (t = 0; t<=(dk * dj - 1); t++) { k = t/dj + my_fields->grid_start[2]+1; @@ -984,8 +937,8 @@ int solve_rate_cool_g( h2dust.data(), rhoH.data(), k24shield.data(), k25shield.data(), k26shield.data(), k28shield.data(), k29shield.data(), k30shield.data(), k31shield.data(), - HIp.data(), HIIp.data(), HeIp.data(), HeIIp.data(), HeIIIp.data(), dep.data(), - HMp.data(), H2Ip.data(), H2IIp.data(), DIp.data(), DIIp.data(), HDIp.data(), + species_tmpdens.data[SpLUT::HI], species_tmpdens.data[SpLUT::HII], species_tmpdens.data[SpLUT::HeI], species_tmpdens.data[SpLUT::HeII], species_tmpdens.data[SpLUT::HeIII], species_tmpdens.data[SpLUT::e], + species_tmpdens.data[SpLUT::HM], species_tmpdens.data[SpLUT::H2I], species_tmpdens.data[SpLUT::H2II], species_tmpdens.data[SpLUT::DI], species_tmpdens.data[SpLUT::DII], species_tmpdens.data[SpLUT::HDI], dedot_prev.data(), HIdot_prev.data(), &my_chemistry->use_radiative_transfer, &my_chemistry->radiative_transfer_hydrogen_only, kphHI.data(), my_fields->RT_HeI_ionization_rate, my_fields->RT_HeII_ionization_rate, @@ -1013,16 +966,16 @@ int solve_rate_cool_g( kz40.data(), kz41.data(), kz42.data(), kz43.data(), kz44.data(), kz45.data(), kz46.data(), kz47.data(), kz48.data(), kz49.data(), kz50.data(), kz51.data(), kz52.data(), kz53.data(), kz54.data(), - DMp.data(), HDIIp.data(), HeHIIp.data(), - CIp.data(), CIIp.data(), COp.data(), CO2p.data(), - OIp.data(), OHp.data(), H2Op.data(), O2p.data(), - SiIp.data(), SiOIp.data(), SiO2Ip.data(), - CHp.data(), CH2p.data(), COIIp.data(), OIIp.data(), - OHIIp.data(), H2OIIp.data(), H3OIIp.data(), O2IIp.data(), - Mgp.data(), Alp.data(), Sp.data(), Fep.data(), - SiMp.data(), FeMp.data(), Mg2SiO4p.data(), MgSiO3p.data(), Fe3O4p.data(), - ACp.data(), SiO2Dp.data(), MgOp.data(), FeSp.data(), Al2O3p.data(), - reforgp.data(), volorgp.data(), H2Oicep.data(), + species_tmpdens.data[SpLUT::DM], species_tmpdens.data[SpLUT::HDII], species_tmpdens.data[SpLUT::HeHII], + species_tmpdens.data[SpLUT::CI], species_tmpdens.data[SpLUT::CII], species_tmpdens.data[SpLUT::CO], species_tmpdens.data[SpLUT::CO2], + species_tmpdens.data[SpLUT::OI], species_tmpdens.data[SpLUT::OH], species_tmpdens.data[SpLUT::H2O], species_tmpdens.data[SpLUT::O2], + species_tmpdens.data[SpLUT::SiI], species_tmpdens.data[SpLUT::SiOI], species_tmpdens.data[SpLUT::SiO2I], + species_tmpdens.data[SpLUT::CH], species_tmpdens.data[SpLUT::CH2], species_tmpdens.data[SpLUT::COII], species_tmpdens.data[SpLUT::OII], + species_tmpdens.data[SpLUT::OHII], species_tmpdens.data[SpLUT::H2OII], species_tmpdens.data[SpLUT::H3OII], species_tmpdens.data[SpLUT::O2II], + species_tmpdens.data[SpLUT::Mg], species_tmpdens.data[SpLUT::Al], species_tmpdens.data[SpLUT::S], species_tmpdens.data[SpLUT::Fe], + species_tmpdens.data[SpLUT::SiM], species_tmpdens.data[SpLUT::FeM], species_tmpdens.data[SpLUT::Mg2SiO4], species_tmpdens.data[SpLUT::MgSiO3], species_tmpdens.data[SpLUT::Fe3O4], + species_tmpdens.data[SpLUT::AC], species_tmpdens.data[SpLUT::SiO2D], species_tmpdens.data[SpLUT::MgO], species_tmpdens.data[SpLUT::FeS], species_tmpdens.data[SpLUT::Al2O3], + species_tmpdens.data[SpLUT::reforg], species_tmpdens.data[SpLUT::volorg], species_tmpdens.data[SpLUT::H2Oice], kdSiM.data(), kdFeM.data(), kdMg2SiO4.data(), kdMgSiO3.data(), kdFe3O4.data(), kdAC.data(), kdSiO2D.data(), kdMgO.data(), kdFeS.data(), kdAl2O3.data(), kdreforg.data(), kdvolorg.data(), kdH2Oice.data(), @@ -1198,6 +1151,7 @@ int solve_rate_cool_g( grackle::impl::drop_LogTLinInterpScratchBuf(&logTlininterp_buf); grackle::impl::drop_Cool1DMultiScratchBuf(&cool1dmulti_buf); grackle::impl::drop_CoolHeatScratchBuf(&coolingheating_buf); + grackle::impl::drop_SpeciesCollection(&species_tmpdens); } // OMP_PRAGMA("omp parallel") From 924c060ccdb276d9f8045826b4646dcb1363efa7 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 15 Jan 2025 09:05:42 -0500 Subject: [PATCH 039/101] introduce col_rec_rxn_rate_members.def file from the gen2024-rate_access_API branch --- src/clib/col_rec_rxn_rate_members.def | 133 ++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 src/clib/col_rec_rxn_rate_members.def diff --git a/src/clib/col_rec_rxn_rate_members.def b/src/clib/col_rec_rxn_rate_members.def new file mode 100644 index 000000000..ee88e6c5c --- /dev/null +++ b/src/clib/col_rec_rxn_rate_members.def @@ -0,0 +1,133 @@ +/*********************************************************************** +/ +/ this file lists each member of the collisional and recombination +/ species reaction rates that we interpolate in 1d as a function of +/ temperature. This list is intended to be used with X-Macros in *.c +/ files (to reduce the amount of code required to +/ interact with these fields) +/ +/ Currently, we just specify each rate name. In the future, we will +/ probably add other metadata to track the initializer function. +/ +/ Copyright (c) 2013, Enzo/Grackle Development Team. +/ +/ Distributed under the terms of the Enzo Public Licence. +/ +/ The full license is in the file LICENSE, distributed with this +/ software. +************************************************************************/ + + +/********************************** + * primordial chemistry rate data * + **********************************/ + +/* 6 species rates */ +ENTRY(k1) +ENTRY(k2) +ENTRY(k3) +ENTRY(k4) +ENTRY(k5) +ENTRY(k6) + +/* 9 species rates (including H2) */ +ENTRY(k7) +ENTRY(k8) +ENTRY(k9) +ENTRY(k10) +ENTRY(k11) +ENTRY(k12) +ENTRY(k13) +ENTRY(k14) +ENTRY(k15) +ENTRY(k16) +ENTRY(k17) +ENTRY(k18) +ENTRY(k19) +ENTRY(k20) /* currently not used */ +ENTRY(k21) /* currently not used */ +ENTRY(k22) /* 3-body H2 formation */ +ENTRY(k23) /* H2-H2 dissociation */ + +// k13dd is currently omitted from this list. It holds the density dependent +// version of k13 (collisional H2 dissociation) + +// radiative rates for 6-species (k24, k25, k26) are omitted +// radiative rates for 9-species (k27, k28, k29, k30, k31) are omitted + +// 12 species rates (with Deuterium) +ENTRY(k50) +ENTRY(k51) +ENTRY(k52) +ENTRY(k53) +ENTRY(k54) +ENTRY(k55) +ENTRY(k56) + +// New H-ionizing reactions, used for 6, 9 & 12 species chemistry +ENTRY(k57) +ENTRY(k58) + +// 15 species rates (with DM, HDII, HeHII) +ENTRY(k125) +ENTRY(k129) +ENTRY(k130) +ENTRY(k131) +ENTRY(k132) +ENTRY(k133) +ENTRY(k134) +ENTRY(k135) +ENTRY(k136) +ENTRY(k137) +ENTRY(k148) +ENTRY(k149) +ENTRY(k150) +ENTRY(k151) +ENTRY(k152) +ENTRY(k153) + +// Metal species chemistry rate data +// --------------------------------- + +ENTRY(kz15) +ENTRY(kz16) +ENTRY(kz17) +ENTRY(kz18) +ENTRY(kz19) +ENTRY(kz20) +ENTRY(kz21) +ENTRY(kz22) +ENTRY(kz23) +ENTRY(kz24) +ENTRY(kz25) +ENTRY(kz26) +ENTRY(kz27) +ENTRY(kz28) +ENTRY(kz29) +ENTRY(kz30) +ENTRY(kz31) +ENTRY(kz32) +ENTRY(kz33) +ENTRY(kz34) +ENTRY(kz35) +ENTRY(kz36) +ENTRY(kz37) +ENTRY(kz38) +ENTRY(kz39) +ENTRY(kz40) +ENTRY(kz41) +ENTRY(kz42) +ENTRY(kz43) +ENTRY(kz44) +ENTRY(kz45) +ENTRY(kz46) +ENTRY(kz47) +ENTRY(kz48) +ENTRY(kz49) +ENTRY(kz50) +ENTRY(kz51) +ENTRY(kz52) +ENTRY(kz53) +ENTRY(kz54) + + From 2f363080d78971de312fb9882c39e8f99d2090dc Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 15 Jan 2025 12:23:04 -0500 Subject: [PATCH 040/101] solve_rate_cool: replace ~90 temporary variables with `kcr_buf` These variables are all used to temorarily store collisional and recombination rates that are computed in each zone. --- src/clib/LUT.hpp | 14 +++ src/clib/internal_types.C | 23 ++++ src/clib/internal_types.hpp | 22 ++++ src/clib/solve_rate_cool_g-cpp.C | 176 +++++++++---------------------- 4 files changed, 106 insertions(+), 129 deletions(-) diff --git a/src/clib/LUT.hpp b/src/clib/LUT.hpp index bc5ef4bde..dd95003a4 100644 --- a/src/clib/LUT.hpp +++ b/src/clib/LUT.hpp @@ -120,4 +120,18 @@ enum { } // SpLUT namespace +// Defines the LUT for Standard Collisional and Recombination reaction rates +namespace ColRecRxnLUT { + +enum { + #define ENTRY(NAME) NAME, + #include "col_rec_rxn_rate_members.def" + #undef ENTRY + + NUM_ENTRIES // <- this is always last (so it specifies the number of species) +}; // enum + +} // ColRecRxnLUT namespace + + #endif /* LUT_HPP */ diff --git a/src/clib/internal_types.C b/src/clib/internal_types.C index e1a2cfccf..6127c3f69 100644 --- a/src/clib/internal_types.C +++ b/src/clib/internal_types.C @@ -211,3 +211,26 @@ void grackle::impl::drop_SpeciesCollection( // since we only allocate a single pointer, we only need to call free once free(ptr->data[0]); } + +// ----------------------------------------------------------------- + +grackle::impl::ColRecRxnRateCollection grackle::impl::new_ColRecRxnRateCollection( + int nelem +) { + GRIMPL_REQUIRE(nelem > 0, "nelem must be positive"); + grackle::impl::ColRecRxnRateCollection out; + double* ptr = (double*)malloc( + sizeof(double) * nelem * ColRecRxnLUT::NUM_ENTRIES + ); + for (int i = 0; i < ColRecRxnLUT::NUM_ENTRIES; i++) { + out.data[i] = ptr + (i * nelem); + } + return out; +} + +void grackle::impl::drop_ColRecRxnRateCollection( + grackle::impl::ColRecRxnRateCollection *ptr +) { + // since we only allocate a single pointer, we only need to call free once + free(ptr->data[0]); +} diff --git a/src/clib/internal_types.hpp b/src/clib/internal_types.hpp index d80be9efd..98136d626 100644 --- a/src/clib/internal_types.hpp +++ b/src/clib/internal_types.hpp @@ -284,6 +284,28 @@ SpeciesCollection new_SpeciesCollection(int nelem); /// This effectively invokes a destructor void drop_SpeciesCollection(SpeciesCollection*); + +/// holds properties about Collisional and Recombination Reaction Rates that +/// behave in the "standard" way (i.e. we interpolate in 1D with respect to +/// log T) +/// +/// This operates in a similar manner to SpeciesCollection (i.e. we use +/// `ColRecRxnLUT::` to lookup values for the desired rate). +struct ColRecRxnRateCollection { + double* data[ColRecRxnLUT::NUM_ENTRIES]; +}; + +/// allocates the contents of a new ColRecRxnRateCollection +/// +/// @param nelem The number of elements in each buffer +ColRecRxnRateCollection new_ColRecRxnRateCollection(int nelem); + +/// performs cleanup of the contents of ColRecRxnRateCollection +/// +/// This effectively invokes a destructor +void drop_ColRecRxnRateCollection(ColRecRxnRateCollection*); + + } // namespace grackle::impl #endif /* INTERNAL_TYPES_HPP */ diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 1f7259da1..2d27b6efd 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -169,97 +169,12 @@ int solve_rate_cool_g( std::vector k29shield(my_fields->grid_dimension[0]); std::vector k30shield(my_fields->grid_dimension[0]); std::vector k31shield(my_fields->grid_dimension[0]); - std::vector k1(my_fields->grid_dimension[0]); - std::vector k2(my_fields->grid_dimension[0]); - std::vector k3(my_fields->grid_dimension[0]); - std::vector k4(my_fields->grid_dimension[0]); - std::vector k5(my_fields->grid_dimension[0]); - std::vector k6(my_fields->grid_dimension[0]); - std::vector k7(my_fields->grid_dimension[0]); - std::vector k8(my_fields->grid_dimension[0]); - std::vector k9(my_fields->grid_dimension[0]); - std::vector k10(my_fields->grid_dimension[0]); - std::vector k11(my_fields->grid_dimension[0]); - std::vector k12(my_fields->grid_dimension[0]); - std::vector k13(my_fields->grid_dimension[0]); - std::vector k14(my_fields->grid_dimension[0]); - std::vector k15(my_fields->grid_dimension[0]); - std::vector k16(my_fields->grid_dimension[0]); - std::vector k17(my_fields->grid_dimension[0]); - std::vector k18(my_fields->grid_dimension[0]); - std::vector k19(my_fields->grid_dimension[0]); - std::vector k22(my_fields->grid_dimension[0]); - std::vector k50(my_fields->grid_dimension[0]); - std::vector k51(my_fields->grid_dimension[0]); - std::vector k52(my_fields->grid_dimension[0]); - std::vector k53(my_fields->grid_dimension[0]); - std::vector k54(my_fields->grid_dimension[0]); - std::vector k55(my_fields->grid_dimension[0]); - std::vector k56(my_fields->grid_dimension[0]); - std::vector k57(my_fields->grid_dimension[0]); - std::vector k58(my_fields->grid_dimension[0]); std::vector k13dd(my_fields->grid_dimension[0] * 14); std::vector h2dust(my_fields->grid_dimension[0]); std::vector ncrn(my_fields->grid_dimension[0]); std::vector ncrd1(my_fields->grid_dimension[0]); std::vector ncrd2(my_fields->grid_dimension[0]); - std::vector k125(my_fields->grid_dimension[0]); - std::vector k129(my_fields->grid_dimension[0]); - std::vector k130(my_fields->grid_dimension[0]); - std::vector k131(my_fields->grid_dimension[0]); - std::vector k132(my_fields->grid_dimension[0]); - std::vector k133(my_fields->grid_dimension[0]); - std::vector k134(my_fields->grid_dimension[0]); - std::vector k135(my_fields->grid_dimension[0]); - std::vector k136(my_fields->grid_dimension[0]); - std::vector k137(my_fields->grid_dimension[0]); - std::vector k148(my_fields->grid_dimension[0]); - std::vector k149(my_fields->grid_dimension[0]); - std::vector k150(my_fields->grid_dimension[0]); - std::vector k151(my_fields->grid_dimension[0]); - std::vector k152(my_fields->grid_dimension[0]); - std::vector k153(my_fields->grid_dimension[0]); - std::vector kz15(my_fields->grid_dimension[0]); - std::vector kz16(my_fields->grid_dimension[0]); - std::vector kz17(my_fields->grid_dimension[0]); - std::vector kz18(my_fields->grid_dimension[0]); - std::vector kz19(my_fields->grid_dimension[0]); - std::vector kz20(my_fields->grid_dimension[0]); - std::vector kz21(my_fields->grid_dimension[0]); - std::vector kz22(my_fields->grid_dimension[0]); - std::vector kz23(my_fields->grid_dimension[0]); - std::vector kz24(my_fields->grid_dimension[0]); - std::vector kz25(my_fields->grid_dimension[0]); - std::vector kz26(my_fields->grid_dimension[0]); - std::vector kz27(my_fields->grid_dimension[0]); - std::vector kz28(my_fields->grid_dimension[0]); - std::vector kz29(my_fields->grid_dimension[0]); - std::vector kz30(my_fields->grid_dimension[0]); - std::vector kz31(my_fields->grid_dimension[0]); - std::vector kz32(my_fields->grid_dimension[0]); - std::vector kz33(my_fields->grid_dimension[0]); - std::vector kz34(my_fields->grid_dimension[0]); - std::vector kz35(my_fields->grid_dimension[0]); - std::vector kz36(my_fields->grid_dimension[0]); - std::vector kz37(my_fields->grid_dimension[0]); - std::vector kz38(my_fields->grid_dimension[0]); - std::vector kz39(my_fields->grid_dimension[0]); - std::vector kz40(my_fields->grid_dimension[0]); - std::vector kz41(my_fields->grid_dimension[0]); - std::vector kz42(my_fields->grid_dimension[0]); - std::vector kz43(my_fields->grid_dimension[0]); - std::vector kz44(my_fields->grid_dimension[0]); - std::vector kz45(my_fields->grid_dimension[0]); - std::vector kz46(my_fields->grid_dimension[0]); - std::vector kz47(my_fields->grid_dimension[0]); - std::vector kz48(my_fields->grid_dimension[0]); - std::vector kz49(my_fields->grid_dimension[0]); - std::vector kz50(my_fields->grid_dimension[0]); - std::vector kz51(my_fields->grid_dimension[0]); - std::vector kz52(my_fields->grid_dimension[0]); - std::vector kz53(my_fields->grid_dimension[0]); - std::vector kz54(my_fields->grid_dimension[0]); std::vector kdSiM(my_fields->grid_dimension[0]); std::vector kdFeM(my_fields->grid_dimension[0]); std::vector kdMg2SiO4(my_fields->grid_dimension[0]); @@ -430,12 +345,7 @@ int solve_rate_cool_g( //_// PORT: !$omp& k24shield, k25shield, k26shield, //_// PORT: !$omp& k28shield, k29shield, k30shield, //_// PORT: !$omp& k31shield, - //_// PORT: !$omp& k1 , k2 , k3 , k4 , k5, - //_// PORT: !$omp& k6 , k7 , k8 , k9 , k10, - //_// PORT: !$omp& k11, k12, k13, k14, k15, - //_// PORT: !$omp& k16, k17, k18, k19, k22, - //_// PORT: !$omp& k50, k51, k52, k53, k54, - //_// PORT: !$omp& k55, k56, k57, k58, k13dd, h2dust, + //_// PORT: !$omp& k13dd, h2dust, //_// PORT: !$omp& ncrn, ncrd1, ncrd2, //_// PORT: !$omp& edot, //_// PORT: !$omp& itmask, itmask_metal ) @@ -462,6 +372,13 @@ int solve_rate_cool_g( grackle::impl::SpeciesCollection species_tmpdens = grackle::impl::new_SpeciesCollection(my_fields->grid_dimension[0]); + // buffers in the following data structure are used to temporarily hold + // the interpolated Collisional/Recombination Rates that have interpolated + // using the standard 1D log temperature table. + grackle::impl::ColRecRxnRateCollection kcr_buf = + grackle::impl::new_ColRecRxnRateCollection(my_fields->grid_dimension[0]); + + //_// TODO_USE: OMP_PRAGMA("omp for") for (t = 0; t<=(dk * dj - 1); t++) { @@ -658,11 +575,11 @@ int solve_rate_cool_g( my_rates->k57, my_rates->k58, &my_chemistry->NumberOfDustTemperatureBins, &my_chemistry->DustTemperatureStart, &my_chemistry->DustTemperatureEnd, my_rates->h2dust, my_rates->n_cr_n, my_rates->n_cr_d1, my_rates->n_cr_d2, &my_uvb_rates->crsHI, &my_uvb_rates->crsHeI, &my_uvb_rates->crsHeII, &my_uvb_rates->piHI, &my_uvb_rates->piHeI, - k1.data(), k2.data(), k3.data(), k4.data(), k5.data(), k6.data(), k7.data(), k8.data(), k9.data(), k10.data(), - k11.data(), k12.data(), k13.data(), k14.data(), k15.data(), k16.data(), k17.data(), k18.data(), - k19.data(), k22.data(), &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k28, &my_uvb_rates->k29, &my_uvb_rates->k30, &my_uvb_rates->k31, - k50.data(), k51.data(), k52.data(), k53.data(), k54.data(), k55.data(), k56.data(), k57.data(), - k58.data(), k13dd.data(), k24shield.data(), k25shield.data(), k26shield.data(), + kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], + kcr_buf.data[ColRecRxnLUT::k11], kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], + kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k28, &my_uvb_rates->k29, &my_uvb_rates->k30, &my_uvb_rates->k31, + kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], + kcr_buf.data[ColRecRxnLUT::k58], k13dd.data(), k24shield.data(), k25shield.data(), k26shield.data(), k28shield.data(), k29shield.data(), k30shield.data(), k31shield.data(), h2dust.data(), ncrn.data(), ncrd1.data(), ncrd2.data(), logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.tdef, logTlininterp_buf.logtem, logTlininterp_buf.indixe, @@ -692,18 +609,18 @@ int solve_rate_cool_g( my_rates->kz40, my_rates->kz41, my_rates->kz42, my_rates->kz43, my_rates->kz44, my_rates->kz45, my_rates->kz46, my_rates->kz47, my_rates->kz48, my_rates->kz49, my_rates->kz50, my_rates->kz51, my_rates->kz52, my_rates->kz53, my_rates->kz54, - k125.data(), k129.data(), k130.data(), k131.data(), k132.data(), - k133.data(), k134.data(), k135.data(), k136.data(), k137.data(), - k148.data(), k149.data(), k150.data(), k151.data(), k152.data(), - k153.data(), - kz15.data(), kz16.data(), kz17.data(), kz18.data(), kz19.data(), - kz20.data(), kz21.data(), kz22.data(), kz23.data(), kz24.data(), - kz25.data(), kz26.data(), kz27.data(), kz28.data(), kz29.data(), - kz30.data(), kz31.data(), kz32.data(), kz33.data(), kz34.data(), - kz35.data(), kz36.data(), kz37.data(), kz38.data(), kz39.data(), - kz40.data(), kz41.data(), kz42.data(), kz43.data(), kz44.data(), - kz45.data(), kz46.data(), kz47.data(), kz48.data(), kz49.data(), - kz50.data(), kz51.data(), kz52.data(), kz53.data(), kz54.data(), + kcr_buf.data[ColRecRxnLUT::k125], kcr_buf.data[ColRecRxnLUT::k129], kcr_buf.data[ColRecRxnLUT::k130], kcr_buf.data[ColRecRxnLUT::k131], kcr_buf.data[ColRecRxnLUT::k132], + kcr_buf.data[ColRecRxnLUT::k133], kcr_buf.data[ColRecRxnLUT::k134], kcr_buf.data[ColRecRxnLUT::k135], kcr_buf.data[ColRecRxnLUT::k136], kcr_buf.data[ColRecRxnLUT::k137], + kcr_buf.data[ColRecRxnLUT::k148], kcr_buf.data[ColRecRxnLUT::k149], kcr_buf.data[ColRecRxnLUT::k150], kcr_buf.data[ColRecRxnLUT::k151], kcr_buf.data[ColRecRxnLUT::k152], + kcr_buf.data[ColRecRxnLUT::k153], + kcr_buf.data[ColRecRxnLUT::kz15], kcr_buf.data[ColRecRxnLUT::kz16], kcr_buf.data[ColRecRxnLUT::kz17], kcr_buf.data[ColRecRxnLUT::kz18], kcr_buf.data[ColRecRxnLUT::kz19], + kcr_buf.data[ColRecRxnLUT::kz20], kcr_buf.data[ColRecRxnLUT::kz21], kcr_buf.data[ColRecRxnLUT::kz22], kcr_buf.data[ColRecRxnLUT::kz23], kcr_buf.data[ColRecRxnLUT::kz24], + kcr_buf.data[ColRecRxnLUT::kz25], kcr_buf.data[ColRecRxnLUT::kz26], kcr_buf.data[ColRecRxnLUT::kz27], kcr_buf.data[ColRecRxnLUT::kz28], kcr_buf.data[ColRecRxnLUT::kz29], + kcr_buf.data[ColRecRxnLUT::kz30], kcr_buf.data[ColRecRxnLUT::kz31], kcr_buf.data[ColRecRxnLUT::kz32], kcr_buf.data[ColRecRxnLUT::kz33], kcr_buf.data[ColRecRxnLUT::kz34], + kcr_buf.data[ColRecRxnLUT::kz35], kcr_buf.data[ColRecRxnLUT::kz36], kcr_buf.data[ColRecRxnLUT::kz37], kcr_buf.data[ColRecRxnLUT::kz38], kcr_buf.data[ColRecRxnLUT::kz39], + kcr_buf.data[ColRecRxnLUT::kz40], kcr_buf.data[ColRecRxnLUT::kz41], kcr_buf.data[ColRecRxnLUT::kz42], kcr_buf.data[ColRecRxnLUT::kz43], kcr_buf.data[ColRecRxnLUT::kz44], + kcr_buf.data[ColRecRxnLUT::kz45], kcr_buf.data[ColRecRxnLUT::kz46], kcr_buf.data[ColRecRxnLUT::kz47], kcr_buf.data[ColRecRxnLUT::kz48], kcr_buf.data[ColRecRxnLUT::kz49], + kcr_buf.data[ColRecRxnLUT::kz50], kcr_buf.data[ColRecRxnLUT::kz51], kcr_buf.data[ColRecRxnLUT::kz52], kcr_buf.data[ColRecRxnLUT::kz53], kcr_buf.data[ColRecRxnLUT::kz54], &my_chemistry->multi_metals, &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, my_fields->local_ISM_metal_density, my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, @@ -741,10 +658,10 @@ int solve_rate_cool_g( de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, d.data(), my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, - k1.data(), k2.data(), k3.data(), k4.data(), k5.data(), k6.data(), k7.data(), k8.data(), k9.data(), k10.data(), k11.data(), - k12.data(), k13.data(), k14.data(), k15.data(), k16.data(), k17.data(), k18.data(), k19.data(), k22.data(), + kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], kcr_buf.data[ColRecRxnLUT::k11], + kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k27, &my_uvb_rates->k28, &my_uvb_rates->k29, &my_uvb_rates->k30, - k50.data(), k51.data(), k52.data(), k53.data(), k54.data(), k55.data(), k56.data(), k57.data(), k58.data(), + kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], kcr_buf.data[ColRecRxnLUT::k58], h2dust.data(), ncrn.data(), ncrd1.data(), ncrd2.data(), rhoH.data(), k24shield.data(), k25shield.data(), k26shield.data(), k28shield.data(), k29shield.data(), k30shield.data(), k31shield.data(), @@ -808,8 +725,8 @@ int solve_rate_cool_g( // If the net rate is almost perfectly balanced then set // it to zero (since it is zero to available precision) - if (std::fmin(std::fabs(k1[i-1]* de(i-1,j-1,k-1)*HI(i-1,j-1,k-1)), - std::fabs(k2[i-1]*HII(i-1,j-1,k-1)*de(i-1,j-1,k-1)))/ + if (std::fmin(std::fabs(kcr_buf.data[ColRecRxnLUT::k1][i-1]* de(i-1,j-1,k-1)*HI(i-1,j-1,k-1)), + std::fabs(kcr_buf.data[ColRecRxnLUT::k2][i-1]*HII(i-1,j-1,k-1)*de(i-1,j-1,k-1)))/ std::fmax(std::fabs(dedot[i-1]),std::fabs(HIdot[i-1])) > 1.0e6) { dedot[i-1] = tiny8; @@ -841,7 +758,7 @@ int solve_rate_cool_g( // Hydrogen changes by 10% or less double Heq_div_dHeqdt = calc_Heq_div_dHeqdt_( my_chemistry, my_rates, dlogtem, logTlininterp_buf, - k13.data(), k22.data(), d(i-1,j-1,k-1), tgas.data(), + kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k22], d(i-1,j-1,k-1), tgas.data(), p2d.data(), edot.data(), i ); @@ -930,10 +847,10 @@ int solve_rate_cool_g( my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, dtit.data(), &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, &my_chemistry->primordial_chemistry, &anydust, - k1.data(), k2.data(), k3.data(), k4.data(), k5.data(), k6.data(), k7.data(), k8.data(), k9.data(), k10.data(), k11.data(), - k12.data(), k13.data(), k14.data(), k15.data(), k16.data(), k17.data(), k18.data(), k19.data(), k22.data(), + kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], kcr_buf.data[ColRecRxnLUT::k11], + kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k27, &my_uvb_rates->k28, &my_uvb_rates->k29, &my_uvb_rates->k30, - k50.data(), k51.data(), k52.data(), k53.data(), k54.data(), k55.data(), k56.data(), k57.data(), k58.data(), + kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], kcr_buf.data[ColRecRxnLUT::k58], h2dust.data(), rhoH.data(), k24shield.data(), k25shield.data(), k26shield.data(), k28shield.data(), k29shield.data(), k30shield.data(), k31shield.data(), @@ -954,18 +871,18 @@ int solve_rate_cool_g( my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, - k125.data(), k129.data(), k130.data(), k131.data(), k132.data(), - k133.data(), k134.data(), k135.data(), k136.data(), k137.data(), - k148.data(), k149.data(), k150.data(), k151.data(), k152.data(), - k153.data(), - kz15.data(), kz16.data(), kz17.data(), kz18.data(), kz19.data(), - kz20.data(), kz21.data(), kz22.data(), kz23.data(), kz24.data(), - kz25.data(), kz26.data(), kz27.data(), kz28.data(), kz29.data(), - kz30.data(), kz31.data(), kz32.data(), kz33.data(), kz34.data(), - kz35.data(), kz36.data(), kz37.data(), kz38.data(), kz39.data(), - kz40.data(), kz41.data(), kz42.data(), kz43.data(), kz44.data(), - kz45.data(), kz46.data(), kz47.data(), kz48.data(), kz49.data(), - kz50.data(), kz51.data(), kz52.data(), kz53.data(), kz54.data(), + kcr_buf.data[ColRecRxnLUT::k125], kcr_buf.data[ColRecRxnLUT::k129], kcr_buf.data[ColRecRxnLUT::k130], kcr_buf.data[ColRecRxnLUT::k131], kcr_buf.data[ColRecRxnLUT::k132], + kcr_buf.data[ColRecRxnLUT::k133], kcr_buf.data[ColRecRxnLUT::k134], kcr_buf.data[ColRecRxnLUT::k135], kcr_buf.data[ColRecRxnLUT::k136], kcr_buf.data[ColRecRxnLUT::k137], + kcr_buf.data[ColRecRxnLUT::k148], kcr_buf.data[ColRecRxnLUT::k149], kcr_buf.data[ColRecRxnLUT::k150], kcr_buf.data[ColRecRxnLUT::k151], kcr_buf.data[ColRecRxnLUT::k152], + kcr_buf.data[ColRecRxnLUT::k153], + kcr_buf.data[ColRecRxnLUT::kz15], kcr_buf.data[ColRecRxnLUT::kz16], kcr_buf.data[ColRecRxnLUT::kz17], kcr_buf.data[ColRecRxnLUT::kz18], kcr_buf.data[ColRecRxnLUT::kz19], + kcr_buf.data[ColRecRxnLUT::kz20], kcr_buf.data[ColRecRxnLUT::kz21], kcr_buf.data[ColRecRxnLUT::kz22], kcr_buf.data[ColRecRxnLUT::kz23], kcr_buf.data[ColRecRxnLUT::kz24], + kcr_buf.data[ColRecRxnLUT::kz25], kcr_buf.data[ColRecRxnLUT::kz26], kcr_buf.data[ColRecRxnLUT::kz27], kcr_buf.data[ColRecRxnLUT::kz28], kcr_buf.data[ColRecRxnLUT::kz29], + kcr_buf.data[ColRecRxnLUT::kz30], kcr_buf.data[ColRecRxnLUT::kz31], kcr_buf.data[ColRecRxnLUT::kz32], kcr_buf.data[ColRecRxnLUT::kz33], kcr_buf.data[ColRecRxnLUT::kz34], + kcr_buf.data[ColRecRxnLUT::kz35], kcr_buf.data[ColRecRxnLUT::kz36], kcr_buf.data[ColRecRxnLUT::kz37], kcr_buf.data[ColRecRxnLUT::kz38], kcr_buf.data[ColRecRxnLUT::kz39], + kcr_buf.data[ColRecRxnLUT::kz40], kcr_buf.data[ColRecRxnLUT::kz41], kcr_buf.data[ColRecRxnLUT::kz42], kcr_buf.data[ColRecRxnLUT::kz43], kcr_buf.data[ColRecRxnLUT::kz44], + kcr_buf.data[ColRecRxnLUT::kz45], kcr_buf.data[ColRecRxnLUT::kz46], kcr_buf.data[ColRecRxnLUT::kz47], kcr_buf.data[ColRecRxnLUT::kz48], kcr_buf.data[ColRecRxnLUT::kz49], + kcr_buf.data[ColRecRxnLUT::kz50], kcr_buf.data[ColRecRxnLUT::kz51], kcr_buf.data[ColRecRxnLUT::kz52], kcr_buf.data[ColRecRxnLUT::kz53], kcr_buf.data[ColRecRxnLUT::kz54], species_tmpdens.data[SpLUT::DM], species_tmpdens.data[SpLUT::HDII], species_tmpdens.data[SpLUT::HeHII], species_tmpdens.data[SpLUT::CI], species_tmpdens.data[SpLUT::CII], species_tmpdens.data[SpLUT::CO], species_tmpdens.data[SpLUT::CO2], species_tmpdens.data[SpLUT::OI], species_tmpdens.data[SpLUT::OH], species_tmpdens.data[SpLUT::H2O], species_tmpdens.data[SpLUT::O2], @@ -1152,6 +1069,7 @@ int solve_rate_cool_g( grackle::impl::drop_Cool1DMultiScratchBuf(&cool1dmulti_buf); grackle::impl::drop_CoolHeatScratchBuf(&coolingheating_buf); grackle::impl::drop_SpeciesCollection(&species_tmpdens); + grackle::impl::drop_ColRecRxnRateCollection(&kcr_buf); } // OMP_PRAGMA("omp parallel") From 57245ef1ac4ac812a3cd697a902dd78e74f3a798 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 15 Jan 2025 13:41:56 -0500 Subject: [PATCH 041/101] extract chem-heating-rates into a separate struct. --- src/clib/internal_types.C | 23 +++++++++++++++++++++++ src/clib/internal_types.hpp | 29 +++++++++++++++++++++++++++++ src/clib/solve_rate_cool_g-cpp.C | 15 ++++++++------- 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/src/clib/internal_types.C b/src/clib/internal_types.C index 6127c3f69..b641613e0 100644 --- a/src/clib/internal_types.C +++ b/src/clib/internal_types.C @@ -234,3 +234,26 @@ void grackle::impl::drop_ColRecRxnRateCollection( // since we only allocate a single pointer, we only need to call free once free(ptr->data[0]); } + +// ----------------------------------------------------------------- + +grackle::impl::ChemHeatingRates grackle::impl::new_ChemHeatingRates( + int nelem +) +{ + GRIMPL_REQUIRE(nelem > 0, "nelem must be positive"); + ChemHeatingRates out; + out.n_cr_n = (double*)malloc(sizeof(double)*nelem); + out.n_cr_d1 = (double*)malloc(sizeof(double)*nelem); + out.n_cr_d2 = (double*)malloc(sizeof(double)*nelem); + return out; +} + +void grackle::impl::drop_ChemHeatingRates( + grackle::impl::ChemHeatingRates* ptr +) +{ + GRACKLE_FREE(ptr->n_cr_n); + GRACKLE_FREE(ptr->n_cr_d1); + GRACKLE_FREE(ptr->n_cr_d2); +} diff --git a/src/clib/internal_types.hpp b/src/clib/internal_types.hpp index 98136d626..efb4b326c 100644 --- a/src/clib/internal_types.hpp +++ b/src/clib/internal_types.hpp @@ -306,6 +306,35 @@ ColRecRxnRateCollection new_ColRecRxnRateCollection(int nelem); void drop_ColRecRxnRateCollection(ColRecRxnRateCollection*); +/// holds reaction rates chemical heating reaction rates +/// +/// @note +/// In the future, it would be nice to use this within the general rate +/// storage struct +/// +/// @note +/// For now, the modelled rates are fairly limited, but the idea is this could +/// be extended in the future +struct ChemHeatingRates{ + // Chemical heating from H2 formation. + // numerator and denominator of Eq 23 of Omukai ea. 2000. + double *n_cr_n; + double *n_cr_d1; + double *n_cr_d2; +}; + +/// allocates the contents of a new ChemHeatingRates +/// +/// @param nelem The number of elements in each buffer +ChemHeatingRates new_ChemHeatingRates(int nelem); + +/// performs cleanup of the contents of ColRecRxnRateCollection +/// +/// This effectively invokes a destructor +void drop_ChemHeatingRates(ChemHeatingRates*); + + + } // namespace grackle::impl #endif /* INTERNAL_TYPES_HPP */ diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 2d27b6efd..2fa3cd52a 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -171,9 +171,6 @@ int solve_rate_cool_g( std::vector k31shield(my_fields->grid_dimension[0]); std::vector k13dd(my_fields->grid_dimension[0] * 14); std::vector h2dust(my_fields->grid_dimension[0]); - std::vector ncrn(my_fields->grid_dimension[0]); - std::vector ncrd1(my_fields->grid_dimension[0]); - std::vector ncrd2(my_fields->grid_dimension[0]); std::vector kdSiM(my_fields->grid_dimension[0]); std::vector kdFeM(my_fields->grid_dimension[0]); @@ -346,7 +343,6 @@ int solve_rate_cool_g( //_// PORT: !$omp& k28shield, k29shield, k30shield, //_// PORT: !$omp& k31shield, //_// PORT: !$omp& k13dd, h2dust, - //_// PORT: !$omp& ncrn, ncrd1, ncrd2, //_// PORT: !$omp& edot, //_// PORT: !$omp& itmask, itmask_metal ) //_// PORT: #endif @@ -378,6 +374,10 @@ int solve_rate_cool_g( grackle::impl::ColRecRxnRateCollection kcr_buf = grackle::impl::new_ColRecRxnRateCollection(my_fields->grid_dimension[0]); + // buffers in the following data structure are used to temporarily hold + // the interpolated chemistry-heating rates at each islice zone + grackle::impl::ChemHeatingRates chemheatrates_buf = + grackle::impl::new_ChemHeatingRates(my_fields->grid_dimension[0]); //_// TODO_USE: OMP_PRAGMA("omp for") @@ -581,7 +581,7 @@ int solve_rate_cool_g( kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], kcr_buf.data[ColRecRxnLUT::k58], k13dd.data(), k24shield.data(), k25shield.data(), k26shield.data(), k28shield.data(), k29shield.data(), k30shield.data(), - k31shield.data(), h2dust.data(), ncrn.data(), ncrd1.data(), ncrd2.data(), + k31shield.data(), h2dust.data(), chemheatrates_buf.n_cr_n, chemheatrates_buf.n_cr_d1, chemheatrates_buf.n_cr_d2, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.tdef, logTlininterp_buf.logtem, logTlininterp_buf.indixe, &dom, &coolunit, &tbase1, &xbase1, &dx_cgs, &c_ljeans, &my_chemistry->use_radiative_transfer, my_fields->RT_H2_dissociation_rate, my_fields->H2_self_shielding_length, itmask.data(), @@ -662,7 +662,7 @@ int solve_rate_cool_g( kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k27, &my_uvb_rates->k28, &my_uvb_rates->k29, &my_uvb_rates->k30, kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], kcr_buf.data[ColRecRxnLUT::k58], - h2dust.data(), ncrn.data(), ncrd1.data(), ncrd2.data(), rhoH.data(), + h2dust.data(), chemheatrates_buf.n_cr_n, chemheatrates_buf.n_cr_d1, chemheatrates_buf.n_cr_d2, rhoH.data(), k24shield.data(), k25shield.data(), k26shield.data(), k28shield.data(), k29shield.data(), k30shield.data(), k31shield.data(), &my_chemistry->use_radiative_transfer, &my_chemistry->radiative_transfer_hydrogen_only, @@ -988,7 +988,7 @@ int solve_rate_cool_g( &c_ljeans, logTlininterp_buf.indixe, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.logtem, logTlininterp_buf.tdef, dtit.data(), p2d.data(), tgas.data(), cool1dmulti_buf.tgasold, tdust.data(), metallicity.data(), dust2gas.data(), rhoH.data(), mmw.data(), cool1dmulti_buf.mynh, cool1dmulti_buf.myde, cool1dmulti_buf.gammaha_eff, cool1dmulti_buf.gasgr_tdust, - cool1dmulti_buf.regr, h2dust.data(), ncrn.data(), ncrd1.data(), ncrd2.data(), grain_temperatures.SiM, grain_temperatures.FeM, + cool1dmulti_buf.regr, h2dust.data(), chemheatrates_buf.n_cr_n, chemheatrates_buf.n_cr_d1, chemheatrates_buf.n_cr_d2, grain_temperatures.SiM, grain_temperatures.FeM, grain_temperatures.Mg2SiO4, grain_temperatures.MgSiO3, grain_temperatures.Fe3O4, grain_temperatures.AC, grain_temperatures.SiO2D, grain_temperatures.MgO, grain_temperatures.FeS, grain_temperatures.Al2O3, grain_temperatures.reforg, grain_temperatures.volorg, grain_temperatures.H2Oice, coolingheating_buf.ceHI, coolingheating_buf.ceHeI, coolingheating_buf.ceHeII, coolingheating_buf.ciHI, coolingheating_buf.ciHeI, coolingheating_buf.ciHeIS, coolingheating_buf.ciHeII, @@ -1070,6 +1070,7 @@ int solve_rate_cool_g( grackle::impl::drop_CoolHeatScratchBuf(&coolingheating_buf); grackle::impl::drop_SpeciesCollection(&species_tmpdens); grackle::impl::drop_ColRecRxnRateCollection(&kcr_buf); + grackle::impl::drop_ChemHeatingRates(&chemheatrates_buf); } // OMP_PRAGMA("omp parallel") From 6a80250c306bad9a778ff70bbe52ddb5b847898d Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 15 Jan 2025 15:40:11 -0500 Subject: [PATCH 042/101] solve_rate_cool_g: aggregate ~20 more local vars into structs. --- src/clib/internal_types.C | 40 ++++++++++++++++++++ src/clib/internal_types.hpp | 30 +++++++++++++++ src/clib/solve_rate_cool_g-cpp.C | 63 +++++++++++++------------------- 3 files changed, 95 insertions(+), 38 deletions(-) diff --git a/src/clib/internal_types.C b/src/clib/internal_types.C index b641613e0..241b907dd 100644 --- a/src/clib/internal_types.C +++ b/src/clib/internal_types.C @@ -235,6 +235,46 @@ void grackle::impl::drop_ColRecRxnRateCollection( free(ptr->data[0]); } +// ----------------------------------------------------------------- + +/// Apply a function to each data member of PhotoRxnRateCollection +/// +/// @note +/// If we are willing to embrace C++, then this should accept a template +/// argument (instead of a function pointer and the callback_ctx pointer) +static void for_each_kphotoCollection_member( + grackle::impl::PhotoRxnRateCollection* ptr, + modify_member_callback* fn, + void* callback_ctx +) { + fn(&ptr->k24, callback_ctx); + fn(&ptr->k25, callback_ctx); + fn(&ptr->k26, callback_ctx); + fn(&ptr->k27, callback_ctx); + fn(&ptr->k28, callback_ctx); + fn(&ptr->k29, callback_ctx); + fn(&ptr->k30, callback_ctx); + fn(&ptr->k31, callback_ctx); +} + +grackle::impl::PhotoRxnRateCollection grackle::impl::new_PhotoRxnRateCollection( + int nelem +) { + GRIMPL_REQUIRE(nelem > 0, "nelem must be positive"); + grackle::impl::PhotoRxnRateCollection out; + MemberAllocCtx_ ctx{nelem}; + for_each_kphotoCollection_member(&out, &allocate_member_, (void*)(&ctx)); + return out; +} + +void grackle::impl::drop_PhotoRxnRateCollection( + grackle::impl::PhotoRxnRateCollection* ptr +) +{ + for_each_kphotoCollection_member(ptr, &cleanup_member_, NULL); +} + + // ----------------------------------------------------------------- grackle::impl::ChemHeatingRates grackle::impl::new_ChemHeatingRates( diff --git a/src/clib/internal_types.hpp b/src/clib/internal_types.hpp index efb4b326c..efc5f48f0 100644 --- a/src/clib/internal_types.hpp +++ b/src/clib/internal_types.hpp @@ -305,6 +305,36 @@ ColRecRxnRateCollection new_ColRecRxnRateCollection(int nelem); /// This effectively invokes a destructor void drop_ColRecRxnRateCollection(ColRecRxnRateCollection*); +/// holds radiative reaction rate buffers +/// +/// @note +/// In the future, it would be nice to use this within the general rate +/// storage struct (and maybe also within the photo_rate_storage struct). +/// Since the storage struct only needs to store these rates as scalars we +/// would need to adopt the LUT strategy (or templates) +struct PhotoRxnRateCollection { + /* Radiative rates for 6-species. */ + double* k24; + double* k25; + double* k26; + + /* Radiative rates for 6-species. */ + double* k27; + double* k28; + double* k29; + double* k30; + double* k31; +}; + +/// allocates the contents of a new PhotoRxnRateCollection +/// +/// @param nelem The number of elements in each buffer +PhotoRxnRateCollection new_PhotoRxnRateCollection(int nelem); + +/// performs cleanup of the contents of PhotoRxnRateCollection +/// +/// This effectively invokes a destructor +void drop_PhotoRxnRateCollection(PhotoRxnRateCollection*); /// holds reaction rates chemical heating reaction rates /// diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 2fa3cd52a..a75db0785 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -162,30 +162,9 @@ int solve_rate_cool_g( std::vector HIdot(my_fields->grid_dimension[0]); std::vector dedot_prev(my_fields->grid_dimension[0]); std::vector HIdot_prev(my_fields->grid_dimension[0]); - std::vector k24shield(my_fields->grid_dimension[0]); - std::vector k25shield(my_fields->grid_dimension[0]); - std::vector k26shield(my_fields->grid_dimension[0]); - std::vector k28shield(my_fields->grid_dimension[0]); - std::vector k29shield(my_fields->grid_dimension[0]); - std::vector k30shield(my_fields->grid_dimension[0]); - std::vector k31shield(my_fields->grid_dimension[0]); std::vector k13dd(my_fields->grid_dimension[0] * 14); std::vector h2dust(my_fields->grid_dimension[0]); - std::vector kdSiM(my_fields->grid_dimension[0]); - std::vector kdFeM(my_fields->grid_dimension[0]); - std::vector kdMg2SiO4(my_fields->grid_dimension[0]); - std::vector kdMgSiO3(my_fields->grid_dimension[0]); - std::vector kdFe3O4(my_fields->grid_dimension[0]); - std::vector kdAC(my_fields->grid_dimension[0]); - std::vector kdSiO2D(my_fields->grid_dimension[0]); - std::vector kdMgO(my_fields->grid_dimension[0]); - std::vector kdFeS(my_fields->grid_dimension[0]); - std::vector kdAl2O3(my_fields->grid_dimension[0]); - std::vector kdreforg(my_fields->grid_dimension[0]); - std::vector kdvolorg(my_fields->grid_dimension[0]); - std::vector kdH2Oice(my_fields->grid_dimension[0]); - // Cooling/heating row locals std::vector edot(my_fields->grid_dimension[0]); @@ -339,9 +318,6 @@ int solve_rate_cool_g( //_// PORT: !$omp& olddtit, //_// PORT: !$omp& dep, dedot,HIdot, dedot_prev, //_// PORT: !$omp& HIdot_prev, - //_// PORT: !$omp& k24shield, k25shield, k26shield, - //_// PORT: !$omp& k28shield, k29shield, k30shield, - //_// PORT: !$omp& k31shield, //_// PORT: !$omp& k13dd, h2dust, //_// PORT: !$omp& edot, //_// PORT: !$omp& itmask, itmask_metal ) @@ -351,9 +327,14 @@ int solve_rate_cool_g( // TODO: move more relevant variable declarations to here to replace the // OMP private-clause + // holds computed grain temperatures: grackle::impl::GrainSpeciesCollection grain_temperatures = grackle::impl::new_GrainSpeciesCollection(my_fields->grid_dimension[0]); + // holds computed grain growth/destruction rates: + grackle::impl::GrainSpeciesCollection grain_growth_rates = + grackle::impl::new_GrainSpeciesCollection(my_fields->grid_dimension[0]); + grackle::impl::LogTLinInterpScratchBuf logTlininterp_buf = grackle::impl::new_LogTLinInterpScratchBuf(my_fields->grid_dimension[0]); @@ -374,12 +355,16 @@ int solve_rate_cool_g( grackle::impl::ColRecRxnRateCollection kcr_buf = grackle::impl::new_ColRecRxnRateCollection(my_fields->grid_dimension[0]); + // buffers in the following data structure are used to temporarily hold + // the computed radiative reaction rates + grackle::impl::PhotoRxnRateCollection kshield_buf = + grackle::impl::new_PhotoRxnRateCollection(my_fields->grid_dimension[0]); + // buffers in the following data structure are used to temporarily hold // the interpolated chemistry-heating rates at each islice zone grackle::impl::ChemHeatingRates chemheatrates_buf = grackle::impl::new_ChemHeatingRates(my_fields->grid_dimension[0]); - //_// TODO_USE: OMP_PRAGMA("omp for") for (t = 0; t<=(dk * dj - 1); t++) { k = t/dj + my_fields->grid_start[2]+1; @@ -579,9 +564,9 @@ int solve_rate_cool_g( kcr_buf.data[ColRecRxnLUT::k11], kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k28, &my_uvb_rates->k29, &my_uvb_rates->k30, &my_uvb_rates->k31, kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], - kcr_buf.data[ColRecRxnLUT::k58], k13dd.data(), k24shield.data(), k25shield.data(), k26shield.data(), - k28shield.data(), k29shield.data(), k30shield.data(), - k31shield.data(), h2dust.data(), chemheatrates_buf.n_cr_n, chemheatrates_buf.n_cr_d1, chemheatrates_buf.n_cr_d2, + kcr_buf.data[ColRecRxnLUT::k58], k13dd.data(), kshield_buf.k24, kshield_buf.k25, kshield_buf.k26, + kshield_buf.k28, kshield_buf.k29, kshield_buf.k30, + kshield_buf.k31, h2dust.data(), chemheatrates_buf.n_cr_n, chemheatrates_buf.n_cr_d1, chemheatrates_buf.n_cr_d2, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.tdef, logTlininterp_buf.logtem, logTlininterp_buf.indixe, &dom, &coolunit, &tbase1, &xbase1, &dx_cgs, &c_ljeans, &my_chemistry->use_radiative_transfer, my_fields->RT_H2_dissociation_rate, my_fields->H2_self_shielding_length, itmask.data(), @@ -641,9 +626,9 @@ int solve_rate_cool_g( my_rates->SN0_kpFeS, my_rates->SN0_kpAl2O3, my_rates->SN0_kpreforg, my_rates->SN0_kpvolorg, my_rates->SN0_kpH2Oice, my_rates->h2dustS, my_rates->h2dustC, rhoH.data(), my_rates->grain_growth_rate, &dt, - kdSiM.data(), kdFeM.data(), kdMg2SiO4.data(), - kdMgSiO3.data(), kdFe3O4.data(), kdAC.data(), kdSiO2D.data(), kdMgO.data(), kdFeS.data(), - kdAl2O3.data(), kdreforg.data(), kdvolorg.data(), kdH2Oice.data(), + grain_growth_rates.SiM, grain_growth_rates.FeM, grain_growth_rates.Mg2SiO4, + grain_growth_rates.MgSiO3, grain_growth_rates.Fe3O4, grain_growth_rates.AC, grain_growth_rates.SiO2D, grain_growth_rates.MgO, grain_growth_rates.FeS, + grain_growth_rates.Al2O3, grain_growth_rates.reforg, grain_growth_rates.volorg, grain_growth_rates.H2Oice, grain_temperatures.SiM, grain_temperatures.FeM, grain_temperatures.Mg2SiO4, grain_temperatures.MgSiO3, grain_temperatures.Fe3O4, grain_temperatures.AC, grain_temperatures.SiO2D, grain_temperatures.MgO, grain_temperatures.FeS, grain_temperatures.Al2O3, grain_temperatures.reforg, grain_temperatures.volorg, grain_temperatures.H2Oice, &my_chemistry->radiative_transfer_use_H2_shielding, @@ -663,8 +648,8 @@ int solve_rate_cool_g( &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k27, &my_uvb_rates->k28, &my_uvb_rates->k29, &my_uvb_rates->k30, kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], kcr_buf.data[ColRecRxnLUT::k58], h2dust.data(), chemheatrates_buf.n_cr_n, chemheatrates_buf.n_cr_d1, chemheatrates_buf.n_cr_d2, rhoH.data(), - k24shield.data(), k25shield.data(), k26shield.data(), - k28shield.data(), k29shield.data(), k30shield.data(), k31shield.data(), + kshield_buf.k24, kshield_buf.k25, kshield_buf.k26, + kshield_buf.k28, kshield_buf.k29, kshield_buf.k30, kshield_buf.k31, &my_chemistry->use_radiative_transfer, &my_chemistry->radiative_transfer_hydrogen_only, kphHI.data(), my_fields->RT_HeI_ionization_rate, my_fields->RT_HeII_ionization_rate, itmask.data(), edot.data(), &chunit, &dom, my_fields->metal_density, @@ -852,8 +837,8 @@ int solve_rate_cool_g( &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k27, &my_uvb_rates->k28, &my_uvb_rates->k29, &my_uvb_rates->k30, kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], kcr_buf.data[ColRecRxnLUT::k58], h2dust.data(), rhoH.data(), - k24shield.data(), k25shield.data(), k26shield.data(), - k28shield.data(), k29shield.data(), k30shield.data(), k31shield.data(), + kshield_buf.k24, kshield_buf.k25, kshield_buf.k26, + kshield_buf.k28, kshield_buf.k29, kshield_buf.k30, kshield_buf.k31, species_tmpdens.data[SpLUT::HI], species_tmpdens.data[SpLUT::HII], species_tmpdens.data[SpLUT::HeI], species_tmpdens.data[SpLUT::HeII], species_tmpdens.data[SpLUT::HeIII], species_tmpdens.data[SpLUT::e], species_tmpdens.data[SpLUT::HM], species_tmpdens.data[SpLUT::H2I], species_tmpdens.data[SpLUT::H2II], species_tmpdens.data[SpLUT::DI], species_tmpdens.data[SpLUT::DII], species_tmpdens.data[SpLUT::HDI], dedot_prev.data(), HIdot_prev.data(), @@ -893,9 +878,9 @@ int solve_rate_cool_g( species_tmpdens.data[SpLUT::SiM], species_tmpdens.data[SpLUT::FeM], species_tmpdens.data[SpLUT::Mg2SiO4], species_tmpdens.data[SpLUT::MgSiO3], species_tmpdens.data[SpLUT::Fe3O4], species_tmpdens.data[SpLUT::AC], species_tmpdens.data[SpLUT::SiO2D], species_tmpdens.data[SpLUT::MgO], species_tmpdens.data[SpLUT::FeS], species_tmpdens.data[SpLUT::Al2O3], species_tmpdens.data[SpLUT::reforg], species_tmpdens.data[SpLUT::volorg], species_tmpdens.data[SpLUT::H2Oice], - kdSiM.data(), kdFeM.data(), kdMg2SiO4.data(), kdMgSiO3.data(), kdFe3O4.data(), - kdAC.data(), kdSiO2D.data(), kdMgO.data(), kdFeS.data(), kdAl2O3.data(), - kdreforg.data(), kdvolorg.data(), kdH2Oice.data(), + grain_growth_rates.SiM, grain_growth_rates.FeM, grain_growth_rates.Mg2SiO4, grain_growth_rates.MgSiO3, grain_growth_rates.Fe3O4, + grain_growth_rates.AC, grain_growth_rates.SiO2D, grain_growth_rates.MgO, grain_growth_rates.FeS, grain_growth_rates.Al2O3, + grain_growth_rates.reforg, grain_growth_rates.volorg, grain_growth_rates.H2Oice, &my_chemistry->radiative_transfer_HDI_dissociation, my_fields->RT_HDI_dissociation_rate, &my_chemistry->radiative_transfer_metal_ionization, my_fields->RT_CI_ionization_rate, my_fields->RT_OI_ionization_rate, &my_chemistry->radiative_transfer_metal_dissociation, my_fields->RT_CO_dissociation_rate, my_fields->RT_OH_dissociation_rate, my_fields->RT_H2O_dissociation_rate ); @@ -1065,11 +1050,13 @@ int solve_rate_cool_g( // cleanup manually allocated temporaries grackle::impl::drop_GrainSpeciesCollection(&grain_temperatures); + grackle::impl::drop_GrainSpeciesCollection(&grain_growth_rates); grackle::impl::drop_LogTLinInterpScratchBuf(&logTlininterp_buf); grackle::impl::drop_Cool1DMultiScratchBuf(&cool1dmulti_buf); grackle::impl::drop_CoolHeatScratchBuf(&coolingheating_buf); grackle::impl::drop_SpeciesCollection(&species_tmpdens); grackle::impl::drop_ColRecRxnRateCollection(&kcr_buf); + grackle::impl::drop_PhotoRxnRateCollection(&kshield_buf); grackle::impl::drop_ChemHeatingRates(&chemheatrates_buf); From 0cb37e27b06bc7bf4f564ae5f5b36118022abda5 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 17 Jan 2025 09:52:15 -0500 Subject: [PATCH 043/101] Introduce `InternalGrUnits` type This type is intended to aid transcription. In the original fortran routines there is a lot of duplicated logic related to unit calculation. This serves a few purposes: 1. there is a lot of duplicated logic scattered throughout the routines being transcribed from Fortran. The idea here is to concentrate all of that logic into 1 place (this logic also has a nice unifying concept: Grackle's Internal Unit System) 2. this will also help us aggregate the local variables that we previously passed around inside of all of the Fortran routines (that are directly used in the duplicated logic) 3. there is also some repeated unit-related logic that is duplicated in the API functions that is directly relevant for computing the user-specified units from the `code_units` struct. That logic is only necessary for computing the variables that `InternalGrUnits` tracks and can trivially be combined with the rest of the logic that `InternalGrUnits` is responsible. 4. Perhaps most importantly, it provides a centralized location for us to better document both the various unit-related quantities themselves and the logic used to compute them (relevant for point 3) --- src/clib/internal_units.h | 205 +++++++++++++++++++++++++++++++ src/clib/solve_rate_cool_g-cpp.C | 17 ++- 2 files changed, 213 insertions(+), 9 deletions(-) create mode 100644 src/clib/internal_units.h diff --git a/src/clib/internal_units.h b/src/clib/internal_units.h new file mode 100644 index 000000000..4d19d47b6 --- /dev/null +++ b/src/clib/internal_units.h @@ -0,0 +1,205 @@ +// See LICENSE file for license and copyright information + +/// @file internal_units.h +/// @brief Declares/Implements the InternalGrUnits struct + +/// This file creates the InternalGrUnits type. +/// - it is fully C-compatible, but behaves in a loosely object-oriented way +/// (i.e. it has associated functions that act a little like methods) +/// - the impetus for creating the constructs in this file is to aide with +/// transcription from Fortran to C++ +/// - you should think of this type (and its associated methods) as a mechansim +/// for grouping together and encapsulating some common logic that was +/// previously repeated across the Fortran versions of the subroutines. +/// +/// As a consequence of this type's role in the transcription process: +/// - the logic was preserved almost **EXACTLY** from the original Fortran files +/// - This is done to maximize consistency and minimize the drift in the results +/// in Grackle's answer tests (modifying the logic could have cascading +/// impacts and plausibly make the results drift outside of tolerances, which +/// is obviously something we want to avoid during transcription) +/// - This means that we may be doing some round-about calculations and using +/// some slightly weird versions of some physical constants +/// +/// **AFTER WE COMPLETE TRANSCRIPTION**, we should simplify this logic +#ifndef INTERNAL_UNITS_HPP +#define INTERNAL_UNITS_HPP + +#ifndef __cplusplus +extern "C" { +#endif /* __cplusplus */ + + +#include + +#include "grackle.h" +#include "grackle_macros.h" +#include "phys_constants.h" + +/// Encapsulates Grackle’s Internal Unit System. +/// +/// Grackle internally employs an Enzo-style comoving coordinate system (and +/// internally tracks various quantities/rates in this unit system). +/// +/// This struct is commonly employed by Grackle’s internal functions that do +/// most of the heavy lifting. We coerce the user-specified units (described by +/// the frontend's `code_units` type) near the top of the Grackle’s internal +/// function call-stack, to an equivalent description in terms of Grackle’s +/// comoving unit system (the latter description encapsulated by this object). +/// +/// @note +/// Once we finish with transcription, we may wish to: +/// - better unify with the code units frontend +/// - adopt more meaningful variable names (most are preserved from the names +/// used in the routines from before transcription) +/// - consider whether or not we even need to track uxyz and urho (I suspect +/// the only reason that these quantities are passed around is that we use +/// them to recompute the other units) +typedef struct InternalGrUnits{ + /// gives the comoving scale-factor when multiplied by a_units + double a_value; + double a_units; + + /// specifies whether user specifies fields in Grackle's comoving coordinates + /// (while this info has historically been passed throughout internal funcs, + /// I've confirmed that it's only used for converting the field units) + int extfields_in_comoving; + + /// proper-frame: cm per 1 comoving length unit (depends on scale factor, a) + double uxyz; + /// comoving-frame: cm per 1 comoving length unit (independent of a) + double xbase1; + + /// proper frame: g/cm^3 per 1 comoving density unit (depends on a) + double urho; + /// comoving frame: g/cm^3 per 1 comoving density unit (independent of a) + double dbase1; + + /// seconds per time unit (this is defined in our comoving unit system + /// such that it's the same in the proper & comoving frames. In other words, + /// it is independent of a) + double tbase1; + + /// erg*cm^3/s per cooling rate unit (this is defined in our comoving unit + /// system such that it's the same in the proper & comoving frames. In other + /// words, it's independent of a). + /// + /// This is needed in the vast majority of cases where internal units is + /// accessed so we precompute it + double coolunit; + +} InternalGrUnits; + +/// Return the version of hydrogen mass constant used by the internal units +static inline double internalu_get_mh_(InternalGrUnits internalu) { + // purely for the sake of consistency, this value of the hydrogen mass is + // initialized as a floating point literal (with the same precision as + // gr_float and then it is casted to a double) + return (double)(mh_grflt); +} + +/// Return the cm*a_unit/s per 1 velocity unit +/// +/// The velocity unit is defined in our comoving unit system such that it's the +/// same in the proper & comoving frames. In other words, it's independent of a +static inline double internalu_get_uvel_(InternalGrUnits internalu) { + return (internalu.uxyz/internalu.a_value) / internalu.tbase1; +} + +/// Return the chunit conversion factor +/// +/// This quantity is used internally to help calculate the amount of heating +/// performed contributed by certain chemical reactions. +static inline double internalu_get_chunit_(InternalGrUnits internalu){ + double mh_local_var = internalu_get_mh_(internalu); + double uvel = internalu_get_uvel_(internalu); + + // before it was moved to this function, there were originally 2 versions of + // this calculation: + // -> the older, commented-out version had an extra factor of 2 in the + // denominator and had a comment stating "1 eV per H2 formed" + // -> the newer version, without the factor of 2 (shown below), had a + // comment stating "1 eV per REACTION (Feb 2020, Gen Chiaki)" + return (1.60218e-12)/(uvel*uvel*mh_local_var); +} + +/// calculates a standard quantity used throughout the codebase +static inline double internalu_calc_dom_(InternalGrUnits internalu) { + const double mh_local_var = internalu_get_mh_(internalu); + return internalu.urho*(std::pow(internalu.a_value,3))/mh_local_var; +} + +/// calculates coefficients used for computing the Jeans' length +/// +/// The main benefit of factoring this out is that we can be sure that we are +/// using a consistent choices in the versions of constants +static inline double internalu_calc_coef_ljeans_(InternalGrUnits internalu, + double gamma) { + const double mh_local_var = internalu_get_mh_(internalu); + return std::sqrt((gamma * pi_fortran_val * kboltz_grflt) / + (GravConst_grflt * mh_local_var * internalu.dbase1)); +} + +/// Construct an instance of InternalGrUnits from the frontend_units +static inline InternalGrUnits new_internalu_( + const code_units* frontend_units +) { + // rename the frontend_units object for convenience + const code_units* my_units = frontend_units; + + // determine the size of comoving length & comoving density units + // (that are equivalent to the frontend units), as measured in the proper + // reference frame + // - this logic (including variable names) has been preserved from the + // standard pattern for handling units pre-transcription + // - in the future, we can rename some of this + double co_length_units, co_density_units; + if (my_units->comoving_coordinates == TRUE) { + // in this case, the frontend unit-system is exactly the same as grackle's + // internal comoving unit system + co_length_units = my_units->length_units; + co_density_units = my_units->density_units; + } else { + // in this case, the frontend unit-system uses proper units. Here we need + // to do some coercion to properly represent the values using Grackle's + // internal unit system + co_length_units = my_units->length_units * + my_units->a_value * my_units->a_units; + co_density_units = my_units->density_units / + POW(my_units->a_value * my_units->a_units, 3); + } + + // initialize output units and copy some stuff from frontend units + InternalGrUnits internalu; + internalu.a_value = my_units->a_value; + internalu.a_units = my_units->a_units; + internalu.extfields_in_comoving = my_units->comoving_coordinates; + + // this represents the name remapping that would occur when passing + // co_length_units and co_density_units into a Fortran routine + internalu.uxyz = co_length_units; + internalu.urho = co_density_units; + + // store the fundamental units of the unit system + // + // TODO: (AFTER FINISIHING TRANSCRIPTION) Consider refactoring this logic for + // the case with `my_units->comoving_coordinates==0` (in that case, we do an + // unnecessary round-trip) + internalu.tbase1 = my_units->time_units; + internalu.xbase1 = internalu.uxyz/(my_units->a_value*my_units->a_units); // uxyz is [x]*a = [x]*[a]*a' + internalu.dbase1 = internalu.urho*pow((my_units->a_value*my_units->a_units),3); // urho is [dens]/a^3 = [dens]/([a]*a')^3 ' + + // lastly, compute coolunit (make sure we use the correct version of mh) + const double mh_local_var = internalu_get_mh_(internalu); + internalu.coolunit = ( + pow(my_units->a_units,5) * pow(internalu.xbase1,2) * pow(mh_local_var,2) + ) / (pow(internalu.tbase1,3) * internalu.dbase1); + + return internalu; +} + +#ifndef __cplusplus +} // extern "C" +#endif /* __cplusplus */ + +#endif /* INTERNAL_UNITS_HPP */ diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index a75db0785..7589aeb45 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -12,6 +12,7 @@ #include "grackle.h" #include "fortran_func_decls.h" #include "internal_types.hpp" +#include "internal_units.h" #include "utils-cpp.hpp" #include "solve_rate_cool_g-cpp.h" @@ -138,7 +139,7 @@ int solve_rate_cool_g( int i, j, k, iter; int t, dj, dk; double ttmin, dom, energy, comp1, comp2; - double coolunit, dbase1, tbase1, xbase1, chunit, uvel; + double coolunit, dbase1, tbase1, xbase1, chunit; double dlogtem, dx_cgs, c_ljeans, min_metallicity; gr_float factor; @@ -197,19 +198,17 @@ int solve_rate_cool_g( min_metallicity = 1.e-9 / my_chemistry->SolarMetalFractionByMass; // Set units + InternalGrUnits internalu = new_internalu_(my_units); dom = urho*(std::pow(my_units->a_value,3))/mh_local_var; tbase1 = my_units->time_units; - xbase1 = uxyz/(my_units->a_value*my_units->a_units); // uxyz is [x]*a = [x]*[a]*a' ' - dbase1 = urho*std::pow((my_units->a_value*my_units->a_units),3); // urho is [dens]/a^3 = [dens]/([a]*a')^3 ' - coolunit = (std::pow(my_units->a_units,5) * std::pow(xbase1,2) * std::pow(mh_local_var,2)) / (std::pow(tbase1,3) * dbase1); - uvel = (uxyz/my_units->a_value) / my_units->time_units; - // chunit = (1.60218e-12_DKIND)/(2._DKIND*uvel*uvel*mh) ! 1 eV per H2 formed - chunit = (1.60218e-12)/(uvel*uvel*mh_local_var); // 1 eV per REACTION (Feb 2020, Gen Chiaki) + xbase1 = internalu.xbase1; + dbase1 = internalu.dbase1; + coolunit = internalu.coolunit; + chunit = internalu_get_chunit_(internalu); dx_cgs = my_fields->grid_dx * xbase1; - c_ljeans = std::sqrt((my_chemistry->Gamma * pi_local_var * kboltz_grflt) / - (GravConst_grflt * mh_local_var * dbase1)); + c_ljeans = internalu_calc_coef_ljeans_(internalu, my_chemistry->Gamma); dlogtem = (std::log(my_chemistry->TemperatureEnd) - std::log(my_chemistry->TemperatureStart))/(double)(my_chemistry->NumberOfTemperatureBins-1 ); From 0cf325bb338acba4c90e478b6360e6ade3644df5 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 17 Jan 2025 10:19:36 -0500 Subject: [PATCH 044/101] solve_rate_cool_g-cpp: integrate InternalGrUnits --- src/clib/solve_rate_cool_g-cpp.C | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 7589aeb45..35ce1a284 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -139,7 +139,7 @@ int solve_rate_cool_g( int i, j, k, iter; int t, dj, dk; double ttmin, dom, energy, comp1, comp2; - double coolunit, dbase1, tbase1, xbase1, chunit; + double chunit; double dlogtem, dx_cgs, c_ljeans, min_metallicity; gr_float factor; @@ -200,14 +200,10 @@ int solve_rate_cool_g( // Set units InternalGrUnits internalu = new_internalu_(my_units); - dom = urho*(std::pow(my_units->a_value,3))/mh_local_var; - tbase1 = my_units->time_units; - xbase1 = internalu.xbase1; - dbase1 = internalu.dbase1; - coolunit = internalu.coolunit; + dom = internalu_calc_dom_(internalu); chunit = internalu_get_chunit_(internalu); - dx_cgs = my_fields->grid_dx * xbase1; + dx_cgs = my_fields->grid_dx * internalu.xbase1; c_ljeans = internalu_calc_coef_ljeans_(internalu, my_chemistry->Gamma); dlogtem = (std::log(my_chemistry->TemperatureEnd) - std::log(my_chemistry->TemperatureStart))/(double)(my_chemistry->NumberOfTemperatureBins-1 ); @@ -434,7 +430,7 @@ int solve_rate_cool_g( &my_chemistry->h2_on_dust, &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_chemistry->dust_recombination_cooling, &my_fields->grid_rank, &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, &my_chemistry->ih2co, &my_chemistry->ipiht, &iter, &my_chemistry->photoelectric_heating, &my_units->a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, - &utem, &uxyz, &my_units->a_units, &urho, &my_units->time_units, + &utem, &internalu.uxyz, &my_units->a_units, &internalu.urho, &my_units->time_units, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, my_rates->ceHI, my_rates->ceHeI, my_rates->ceHeII, my_rates->ciHI, my_rates->ciHeI, my_rates->ciHeIS, my_rates->ciHeII, my_rates->reHII, my_rates->reHeII1, @@ -567,7 +563,7 @@ int solve_rate_cool_g( kshield_buf.k28, kshield_buf.k29, kshield_buf.k30, kshield_buf.k31, h2dust.data(), chemheatrates_buf.n_cr_n, chemheatrates_buf.n_cr_d1, chemheatrates_buf.n_cr_d2, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.tdef, logTlininterp_buf.logtem, logTlininterp_buf.indixe, - &dom, &coolunit, &tbase1, &xbase1, &dx_cgs, &c_ljeans, + &dom, &internalu.coolunit, &internalu.tbase1, &internalu.xbase1, &dx_cgs, &c_ljeans, &my_chemistry->use_radiative_transfer, my_fields->RT_H2_dissociation_rate, my_fields->H2_self_shielding_length, itmask.data(), itmask_metal.data(), &my_chemistry->HydrogenFractionByMass, my_fields->metal_density, @@ -894,7 +890,7 @@ int solve_rate_cool_g( &my_units->comoving_coordinates, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_fields->grid_start[0], &my_fields->grid_end[0], &my_chemistry->ih2co, &my_chemistry->ipiht, &my_chemistry->dust_recombination_cooling, &my_chemistry->photoelectric_heating, &my_units->a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &utem, - &uxyz, &my_units->a_units, &urho, &my_units->time_units, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, + &internalu.uxyz, &my_units->a_units, &internalu.urho, &my_units->time_units, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, my_rates->k1, my_rates->k2, my_rates->k3, my_rates->k4, my_rates->k5, my_rates->k6, my_rates->k7, my_rates->k8, my_rates->k9, my_rates->k10, my_rates->k11, my_rates->k12, my_rates->k13, my_rates->k13dd, my_rates->k14, my_rates->k15, my_rates->k16, my_rates->k17, my_rates->k18, my_rates->k19, my_rates->k22, &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k27, &my_uvb_rates->k28, @@ -968,7 +964,7 @@ int solve_rate_cool_g( my_fields->RT_OH_dissociation_rate, my_fields->RT_H2O_dissociation_rate, &my_chemistry->radiative_transfer_use_H2_shielding, &my_chemistry->use_isrf_field, my_fields->isrf_habing, &my_chemistry->H2_custom_shielding, my_fields->H2_custom_shielding_factor, &j, &k, &iter, &dom, &comp1, - &comp2, &coolunit, &tbase1, &xbase1, &chunit, &dx_cgs, + &comp2, &internalu.coolunit, &internalu.tbase1, &internalu.xbase1, &chunit, &dx_cgs, &c_ljeans, logTlininterp_buf.indixe, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.logtem, logTlininterp_buf.tdef, dtit.data(), p2d.data(), tgas.data(), cool1dmulti_buf.tgasold, tdust.data(), metallicity.data(), dust2gas.data(), rhoH.data(), mmw.data(), cool1dmulti_buf.mynh, cool1dmulti_buf.myde, cool1dmulti_buf.gammaha_eff, cool1dmulti_buf.gasgr_tdust, From 0f51ed47477f918a958de33c04302f23ad33b287 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 17 Jan 2025 10:24:41 -0500 Subject: [PATCH 045/101] InternalGrUnits: docstring tweak --- src/clib/internal_units.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/clib/internal_units.h b/src/clib/internal_units.h index 4d19d47b6..97619cb33 100644 --- a/src/clib/internal_units.h +++ b/src/clib/internal_units.h @@ -61,8 +61,8 @@ typedef struct InternalGrUnits{ double a_units; /// specifies whether user specifies fields in Grackle's comoving coordinates - /// (while this info has historically been passed throughout internal funcs, - /// I've confirmed that it's only used for converting the field units) + /// - while this info has historically been passed deeply into the callstack + /// I've confirmed that it's only used for converting the field units int extfields_in_comoving; /// proper-frame: cm per 1 comoving length unit (depends on scale factor, a) From 8a226da5a2457ce85e49041535ce5689192cc468 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 17 Jan 2025 10:30:30 -0500 Subject: [PATCH 046/101] solve_rate_cool_g: replace most uses of with --- src/clib/solve_rate_cool_g-cpp.C | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 35ce1a284..406fc31d0 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -246,9 +246,9 @@ int solve_rate_cool_g( // Convert densities from comoving to proper - if (my_units->comoving_coordinates == 1) { + if (internalu.extfields_in_comoving == 1) { - factor = (gr_float)(std::pow(my_units->a_value,(-3)) ); + factor = (gr_float)(std::pow(internalu.a_value,(-3)) ); FORTRAN_NAME(scale_fields_g)( &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, @@ -426,11 +426,11 @@ int solve_rate_cool_g( FORTRAN_NAME(cool1d_multi_g)( d.data(), e.data(), my_fields->x_velocity, my_fields->y_velocity, my_fields->z_velocity, de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, - &my_units->comoving_coordinates, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, + &internalu.extfields_in_comoving, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_chemistry->dust_recombination_cooling, &my_fields->grid_rank, &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, &my_chemistry->ih2co, &my_chemistry->ipiht, &iter, &my_chemistry->photoelectric_heating, - &my_units->a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, - &utem, &internalu.uxyz, &my_units->a_units, &internalu.urho, &my_units->time_units, + &internalu.a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, + &utem, &internalu.uxyz, &internalu.a_units, &internalu.urho, &internalu.tbase1, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, my_rates->ceHI, my_rates->ceHeI, my_rates->ceHeII, my_rates->ciHI, my_rates->ciHeI, my_rates->ciHeIS, my_rates->ciHeII, my_rates->reHII, my_rates->reHeII1, @@ -887,10 +887,10 @@ int solve_rate_cool_g( FORTRAN_NAME(step_rate_newton_raphson)(&my_chemistry->with_radiative_cooling, d.data(), e.data(), my_fields->x_velocity, my_fields->y_velocity, my_fields->z_velocity, de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, - &my_units->comoving_coordinates, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, + &internalu.extfields_in_comoving, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_fields->grid_start[0], &my_fields->grid_end[0], &my_chemistry->ih2co, &my_chemistry->ipiht, - &my_chemistry->dust_recombination_cooling, &my_chemistry->photoelectric_heating, &my_units->a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &utem, - &internalu.uxyz, &my_units->a_units, &internalu.urho, &my_units->time_units, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, + &my_chemistry->dust_recombination_cooling, &my_chemistry->photoelectric_heating, &internalu.a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &utem, + &internalu.uxyz, &internalu.a_units, &internalu.urho, &internalu.tbase1, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, my_rates->k1, my_rates->k2, my_rates->k3, my_rates->k4, my_rates->k5, my_rates->k6, my_rates->k7, my_rates->k8, my_rates->k9, my_rates->k10, my_rates->k11, my_rates->k12, my_rates->k13, my_rates->k13dd, my_rates->k14, my_rates->k15, my_rates->k16, my_rates->k17, my_rates->k18, my_rates->k19, my_rates->k22, &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k27, &my_uvb_rates->k28, @@ -1065,9 +1065,9 @@ int solve_rate_cool_g( // Convert densities back to comoving from proper - if (my_units->comoving_coordinates == 1) { + if (internalu.extfields_in_comoving == 1) { - factor = (gr_float)(std::pow(my_units->a_value,3) ); + factor = (gr_float)(std::pow(internalu.a_value,3) ); FORTRAN_NAME(scale_fields_g)( &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, From f1462bcca5f35a40d082c9c50e205a1c87abe1a9 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 17 Jan 2025 10:57:25 -0500 Subject: [PATCH 047/101] internal_units.h: a few small tweaks to work with C files. --- src/clib/internal_units.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/clib/internal_units.h b/src/clib/internal_units.h index 97619cb33..b889e9ae1 100644 --- a/src/clib/internal_units.h +++ b/src/clib/internal_units.h @@ -25,7 +25,7 @@ #ifndef INTERNAL_UNITS_HPP #define INTERNAL_UNITS_HPP -#ifndef __cplusplus +#ifdef __cplusplus extern "C" { #endif /* __cplusplus */ @@ -126,7 +126,7 @@ static inline double internalu_get_chunit_(InternalGrUnits internalu){ /// calculates a standard quantity used throughout the codebase static inline double internalu_calc_dom_(InternalGrUnits internalu) { const double mh_local_var = internalu_get_mh_(internalu); - return internalu.urho*(std::pow(internalu.a_value,3))/mh_local_var; + return internalu.urho*(pow(internalu.a_value,3))/mh_local_var; } /// calculates coefficients used for computing the Jeans' length @@ -136,8 +136,8 @@ static inline double internalu_calc_dom_(InternalGrUnits internalu) { static inline double internalu_calc_coef_ljeans_(InternalGrUnits internalu, double gamma) { const double mh_local_var = internalu_get_mh_(internalu); - return std::sqrt((gamma * pi_fortran_val * kboltz_grflt) / - (GravConst_grflt * mh_local_var * internalu.dbase1)); + return sqrt((gamma * pi_fortran_val * kboltz_grflt) / + (GravConst_grflt * mh_local_var * internalu.dbase1)); } /// Construct an instance of InternalGrUnits from the frontend_units @@ -198,7 +198,7 @@ static inline InternalGrUnits new_internalu_( return internalu; } -#ifndef __cplusplus +#ifdef __cplusplus } // extern "C" #endif /* __cplusplus */ From 387b388973086f0cae9eb8f3d77e95b6a09e7d2f Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 17 Jan 2025 10:59:10 -0500 Subject: [PATCH 048/101] solve_rate_cool_g: replaced multiple args with InternalGrUnits --- src/clib/solve_chemistry.c | 18 ++++-------------- src/clib/solve_rate_cool_g-cpp.C | 7 ++----- src/clib/solve_rate_cool_g-cpp.h | 6 +++--- 3 files changed, 9 insertions(+), 22 deletions(-) diff --git a/src/clib/solve_chemistry.c b/src/clib/solve_chemistry.c index 2aa6b0ff3..ec03535fa 100644 --- a/src/clib/solve_chemistry.c +++ b/src/clib/solve_chemistry.c @@ -15,6 +15,7 @@ #include #include "grackle.h" #include "grackle_macros.h" +#include "internal_units.h" #include "phys_constants.h" #include "solve_rate_cool_g-cpp.h" #include "utils.h" @@ -80,17 +81,7 @@ int local_solve_chemistry(chemistry_data *my_chemistry, if (my_fields->metal_density == NULL) metal_field_present = FALSE; - double co_length_units, co_density_units; - if (my_units->comoving_coordinates == TRUE) { - co_length_units = my_units->length_units; - co_density_units = my_units->density_units; - } - else { - co_length_units = my_units->length_units * - my_units->a_value * my_units->a_units; - co_density_units = my_units->density_units / - POW(my_units->a_value * my_units->a_units, 3); - } + InternalGrUnits internalu = new_internalu_(my_units); /* Error checking for H2 shielding approximation */ if (self_shielding_err_check(my_chemistry, my_fields, @@ -105,9 +96,8 @@ int local_solve_chemistry(chemistry_data *my_chemistry, /* Call the fortran routine to solve cooling equations. */ int ierr = solve_rate_cool_g( - metal_field_present, dt_value, temperature_units, co_length_units, - co_density_units, my_chemistry, my_rates, my_units, my_fields, - &my_uvb_rates + metal_field_present, dt_value, temperature_units, internalu, + my_chemistry, my_rates, my_fields, &my_uvb_rates ); if (ierr == GR_FAIL) { diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 406fc31d0..5d555ce51 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -104,10 +104,9 @@ extern "C" { #endif /* __cplusplus */ int solve_rate_cool_g( - int imetal, double dt, double utem, double uxyz, double urho, + int imetal, double dt, double utem, InternalGrUnits internalu, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, - code_units* my_units, grackle_field_data* my_fields, - photo_rate_storage* my_uvb_rates + grackle_field_data* my_fields, photo_rate_storage* my_uvb_rates ) { @@ -198,8 +197,6 @@ int solve_rate_cool_g( min_metallicity = 1.e-9 / my_chemistry->SolarMetalFractionByMass; // Set units - InternalGrUnits internalu = new_internalu_(my_units); - dom = internalu_calc_dom_(internalu); chunit = internalu_get_chunit_(internalu); diff --git a/src/clib/solve_rate_cool_g-cpp.h b/src/clib/solve_rate_cool_g-cpp.h index 858d0d643..3a639f804 100644 --- a/src/clib/solve_rate_cool_g-cpp.h +++ b/src/clib/solve_rate_cool_g-cpp.h @@ -11,6 +11,7 @@ #include "grackle.h" // gr_float #include "fortran_func_decls.h" // gr_mask_int +#include "internal_units.h" // InternalGrUnits #ifdef __cplusplus extern "C" { @@ -31,10 +32,9 @@ extern "C" { /// /// @return Returns GR_SUCCESS or GR_FAIL to indicate whether there was an error int solve_rate_cool_g( - int imetal, double dt, double utem, double uxyz, double urho, + int imetal, double dt, double utem, InternalGrUnits internalu, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, - code_units* my_units, grackle_field_data* my_fields, - photo_rate_storage* my_uvb_rates + grackle_field_data* my_fields, photo_rate_storage* my_uvb_rates ); #ifdef __cplusplus From e7f53f6efdd0deabac954fb354f04830b6209732 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 17 Jan 2025 13:56:50 -0500 Subject: [PATCH 049/101] introduce a C++ wrapper for the scale_fields_g Fortran subroutine --- src/clib/CMakeLists.txt | 1 + src/clib/fortran_func_wrappers.hpp | 57 ++++++++++++++++++++++++++++++ src/clib/solve_rate_cool_g-cpp.C | 47 ++---------------------- 3 files changed, 61 insertions(+), 44 deletions(-) create mode 100644 src/clib/fortran_func_wrappers.hpp diff --git a/src/clib/CMakeLists.txt b/src/clib/CMakeLists.txt index 1576f9b68..b611523fc 100644 --- a/src/clib/CMakeLists.txt +++ b/src/clib/CMakeLists.txt @@ -109,6 +109,7 @@ add_library(Grackle_Grackle internal_types.C internal_types.hpp solve_rate_cool_g-cpp.C solve_rate_cool_g-cpp.h utils-cpp.C utils-cpp.hpp + fortran_func_wrappers.hpp # Fortran Source Files calc_all_tdust_gasgr_1d_g.F diff --git a/src/clib/fortran_func_wrappers.hpp b/src/clib/fortran_func_wrappers.hpp new file mode 100644 index 000000000..562963fd7 --- /dev/null +++ b/src/clib/fortran_func_wrappers.hpp @@ -0,0 +1,57 @@ +// See LICENSE file for license and copyright information + +/// @file fortran_func_wrappers.hpp +/// @brief Declares wrappers around routines that haven't been transcribed yet +/// from Fortran. +/// +/// THIS FILE WILL BE DELETED ONCE WE COMPLETE TRANSCRIPTION +/// +/// This file exists to aid the transcription process +/// - This file holds C++ functions that wrap untranscribed Fortran routines. +/// The idea is that these wrapper functions take reduced argument lists +/// (that may includes structs) before converting extended arg lists used +/// within the fortran subroutine. +/// - ideally the reduced argument list used by the wrapper function should +/// roughly approximate the final argument list that the routines will use +/// after transcription is complete. + +#ifndef FORTRAN_FUNC_WRAPPERS_HPP +#define FORTRAN_FUNC_WRAPPERS_HPP + +#ifndef __cplusplus +#error "This file must be read by a c++ compiler" +#endif + +#include "grackle.h" +#include "fortran_func_decls.h" + +inline void wrapped_scale_fields_g_( + int imetal, gr_float factor, chemistry_data* my_chemistry, + grackle_field_data* my_fields +) { + + FORTRAN_NAME(scale_fields_g)( + &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, + &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->multi_metals, &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, &factor, + &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], + my_fields->density, my_fields->e_density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, + my_fields->metal_density, my_fields->dust_density, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, + my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, + my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, + my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, + my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, + my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, + my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, + my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, + my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, + my_fields->local_ISM_metal_density, my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, + my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, + my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density); + +} + + +#endif /* FORTRAN_FUNC_WRAPPERS_HPP */ + diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 5d555ce51..3191348e5 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -11,6 +11,7 @@ #include "grackle.h" #include "fortran_func_decls.h" +#include "fortran_func_wrappers.hpp" #include "internal_types.hpp" #include "internal_units.h" #include "utils-cpp.hpp" @@ -244,29 +245,8 @@ int solve_rate_cool_g( // Convert densities from comoving to proper if (internalu.extfields_in_comoving == 1) { - factor = (gr_float)(std::pow(internalu.a_value,(-3)) ); - - FORTRAN_NAME(scale_fields_g)( - &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, - &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->multi_metals, &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, &factor, - &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], - d.data(), de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, - my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, - my_fields->metal_density, my_fields->dust_density, - my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, - my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, - my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, - my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, - my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, - my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, - my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, - my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, - my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, - my_fields->local_ISM_metal_density, my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, - my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, - my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density); - + wrapped_scale_fields_g_(imetal, factor, my_chemistry, my_fields); } #ifdef ABUNDANCE_CORRECTION @@ -1063,29 +1043,8 @@ int solve_rate_cool_g( // Convert densities back to comoving from proper if (internalu.extfields_in_comoving == 1) { - factor = (gr_float)(std::pow(internalu.a_value,3) ); - - FORTRAN_NAME(scale_fields_g)( - &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, - &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->multi_metals, &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, &factor, - &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], - d.data(), de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, - my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, - my_fields->metal_density, my_fields->dust_density, - my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, - my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, - my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, - my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, - my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, - my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, - my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, - my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, - my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, - my_fields->local_ISM_metal_density, my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, - my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, - my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density); - + wrapped_scale_fields_g_(imetal, factor, my_chemistry, my_fields); } if (my_chemistry->primordial_chemistry > 0) { From 106f2ca21af48f8e6ed44d300530c1b951dbb268 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 17 Jan 2025 14:21:01 -0500 Subject: [PATCH 050/101] introduce C++ wrapper for `ceiling_species_g_` subroutine --- src/clib/fortran_func_wrappers.hpp | 27 +++++++++++++++++++ src/clib/solve_rate_cool_g-cpp.C | 42 ++---------------------------- 2 files changed, 29 insertions(+), 40 deletions(-) diff --git a/src/clib/fortran_func_wrappers.hpp b/src/clib/fortran_func_wrappers.hpp index 562963fd7..a9db4c9f0 100644 --- a/src/clib/fortran_func_wrappers.hpp +++ b/src/clib/fortran_func_wrappers.hpp @@ -25,6 +25,33 @@ #include "grackle.h" #include "fortran_func_decls.h" +inline void wrapped_ceiling_species_g_( + int imetal, chemistry_data* my_chemistry, grackle_field_data* my_fields +) { + + FORTRAN_NAME(ceiling_species_g)(my_fields->density, my_fields->e_density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, + &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->use_dust_density_field, + my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, + &my_chemistry->metal_abundances, &my_chemistry->metal_chemistry, &my_chemistry->dust_species, &my_chemistry->multi_metals, + &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, + my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, + my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, + my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, + my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, + my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, + my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, + my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, + my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, + my_fields->local_ISM_metal_density, + my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, + my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, + my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density); + +} + inline void wrapped_scale_fields_g_( int imetal, gr_float factor, chemistry_data* my_chemistry, grackle_field_data* my_fields diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 3191348e5..114b776d7 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -250,26 +250,7 @@ int solve_rate_cool_g( } #ifdef ABUNDANCE_CORRECTION - FORTRAN_NAME(ceiling_species_g)(d.data(), de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, - my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, - &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->use_dust_density_field, - my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, - &my_chemistry->metal_abundances, &my_chemistry->metal_chemistry, &my_chemistry->dust_species, &my_chemistry->multi_metals, - &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, - my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, - my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, - my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, - my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, - my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, - my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, - my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, - my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, - my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, - my_fields->local_ISM_metal_density, - my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, - my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, - my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density); + wrapped_ceiling_species_g_(imetal, my_chemistry, my_fields); #endif @@ -1079,26 +1060,7 @@ int solve_rate_cool_g( my_rates->SN0_fS, my_rates->SN0_fFe ); - FORTRAN_NAME(ceiling_species_g)(d.data(), de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, - my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, - &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->use_dust_density_field, - my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, - &my_chemistry->metal_abundances, &my_chemistry->metal_chemistry, &my_chemistry->dust_species, &my_chemistry->multi_metals, - &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, - my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, - my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, - my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, - my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, - my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, - my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, - my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, - my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, - my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, - my_fields->local_ISM_metal_density, - my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, - my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, - my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density); + wrapped_ceiling_species_g_(imetal, my_chemistry, my_fields); #endif } From 08c6ae34fd4097595331beaad63d5e236b325073 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 17 Jan 2025 14:48:23 -0500 Subject: [PATCH 051/101] introduce C++ wrapper for `make_consistent_g_` Fortran subroutine --- src/clib/fortran_func_wrappers.hpp | 36 +++++++++++++++++++ src/clib/solve_rate_cool_g-cpp.C | 55 ++---------------------------- 2 files changed, 38 insertions(+), 53 deletions(-) diff --git a/src/clib/fortran_func_wrappers.hpp b/src/clib/fortran_func_wrappers.hpp index a9db4c9f0..fad4086e7 100644 --- a/src/clib/fortran_func_wrappers.hpp +++ b/src/clib/fortran_func_wrappers.hpp @@ -52,6 +52,42 @@ inline void wrapped_ceiling_species_g_( } + +// the following case was handcoded (so the argument order may shift when we +// actually transcribe the routine) +inline void wrapped_make_consistent_g_( + int imetal, double dom, chemistry_data* my_chemistry, + chemistry_data_storage* my_rates, grackle_field_data* my_fields +){ + FORTRAN_NAME(make_consistent_g)(my_fields->e_density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, + my_fields->density, &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->HydrogenFractionByMass, &my_chemistry->DeuteriumToHydrogenRatio, + &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, &dom, + my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, + my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, + my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, + my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, + my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, + my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, + my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, + my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, + my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, + &my_chemistry->multi_metals, &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, + my_fields->local_ISM_metal_density, + my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, + my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, + my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density, + &my_rates->SN0_N, + my_rates->SN0_XC, my_rates->SN0_XO, my_rates->SN0_XMg, my_rates->SN0_XAl, my_rates->SN0_XSi, + my_rates->SN0_XS, my_rates->SN0_XFe, + my_rates->SN0_fC, my_rates->SN0_fO, my_rates->SN0_fMg, my_rates->SN0_fAl, my_rates->SN0_fSi, + my_rates->SN0_fS, my_rates->SN0_fFe + ); +} + + inline void wrapped_scale_fields_g_( int imetal, gr_float factor, chemistry_data* my_chemistry, grackle_field_data* my_fields diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 114b776d7..54031a670 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -212,32 +212,7 @@ int solve_rate_cool_g( #define ABUNDANCE_CORRECTION #ifdef ABUNDANCE_CORRECTION - FORTRAN_NAME(make_consistent_g)(de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, - my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, - d.data(), &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->HydrogenFractionByMass, &my_chemistry->DeuteriumToHydrogenRatio, - &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, &dom, - my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, - my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, - my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, - my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, - my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, - my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, - my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, - my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, - my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, - my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, - &my_chemistry->multi_metals, &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, - my_fields->local_ISM_metal_density, - my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, - my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, - my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density, - &my_rates->SN0_N, - my_rates->SN0_XC, my_rates->SN0_XO, my_rates->SN0_XMg, my_rates->SN0_XAl, my_rates->SN0_XSi, - my_rates->SN0_XS, my_rates->SN0_XFe, - my_rates->SN0_fC, my_rates->SN0_fO, my_rates->SN0_fMg, my_rates->SN0_fAl, my_rates->SN0_fSi, - my_rates->SN0_fS, my_rates->SN0_fFe - ); + wrapped_make_consistent_g_(imetal, dom, my_chemistry, my_rates, my_fields); #endif } @@ -1033,33 +1008,7 @@ int solve_rate_cool_g( // Correct the species to ensure consistency (i.e. type conservation) #ifdef ABUNDANCE_CORRECTION - FORTRAN_NAME(make_consistent_g)(de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, - my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, - d.data(), &my_fields->grid_start[0], &my_fields->grid_end[0], &my_fields->grid_start[1], &my_fields->grid_end[1], &my_fields->grid_start[2], &my_fields->grid_end[2], - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->HydrogenFractionByMass, &my_chemistry->DeuteriumToHydrogenRatio, - &my_chemistry->use_dust_density_field, &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, &dom, - my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, - my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, - my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, - my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, - my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, - my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, - my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, - my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, - my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, - my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, - &my_chemistry->multi_metals, &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, - my_fields->local_ISM_metal_density, - my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, - my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, - my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density, - &my_rates->SN0_N, - my_rates->SN0_XC, my_rates->SN0_XO, my_rates->SN0_XMg, my_rates->SN0_XAl, my_rates->SN0_XSi, - my_rates->SN0_XS, my_rates->SN0_XFe, - my_rates->SN0_fC, my_rates->SN0_fO, my_rates->SN0_fMg, my_rates->SN0_fAl, my_rates->SN0_fSi, - my_rates->SN0_fS, my_rates->SN0_fFe - ); - + wrapped_make_consistent_g_(imetal, dom, my_chemistry, my_rates, my_fields); wrapped_ceiling_species_g_(imetal, my_chemistry, my_fields); #endif From 62c70f555c842fe657f13443f0be254a3f00fb50 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 19 Jan 2025 09:42:45 -0500 Subject: [PATCH 052/101] introduce C++ wrapper for `lookup_cool_rates1d_g` subroutine --- src/clib/fortran_func_wrappers.hpp | 111 +++++++++++++++++++++++++++++ src/clib/solve_rate_cool_g-cpp.C | 94 ++---------------------- 2 files changed, 118 insertions(+), 87 deletions(-) diff --git a/src/clib/fortran_func_wrappers.hpp b/src/clib/fortran_func_wrappers.hpp index fad4086e7..9ab0d78de 100644 --- a/src/clib/fortran_func_wrappers.hpp +++ b/src/clib/fortran_func_wrappers.hpp @@ -24,6 +24,9 @@ #include "grackle.h" #include "fortran_func_decls.h" +#include "internal_types.hpp" +#include "internal_units.h" +#include "LUT.hpp" inline void wrapped_ceiling_species_g_( int imetal, chemistry_data* my_chemistry, grackle_field_data* my_fields @@ -52,6 +55,114 @@ inline void wrapped_ceiling_species_g_( } +/// This routine uses the temperature to look up the chemical rates that are +/// tabulated in a log table as a function of temperature +inline void wrapped_lookup_cool_rates1d_g_( + int j, int k, gr_mask_type anydust, double* tgas1d, double* mmw, + double* tdust, double* dust2gas, double* k13dd, double* h2dust, + double dom, double dx_cgs, double c_ljeans, gr_mask_type* itmask, + gr_mask_type* itmask_metal, int imetal, gr_float* rhoH, double dt, + chemistry_data* my_chemistry, chemistry_data_storage* my_rates, + grackle_field_data* my_fields, photo_rate_storage my_uvb_rates, + InternalGrUnits internalu, + grackle::impl::GrainSpeciesCollection grain_growth_rates, + grackle::impl::GrainSpeciesCollection grain_temperatures, + grackle::impl::LogTLinInterpScratchBuf logTlininterp_buf, + grackle::impl::ColRecRxnRateCollection kcr_buf, + grackle::impl::PhotoRxnRateCollection kshield_buf, + grackle::impl::ChemHeatingRates chemheatrates_buf +) { + + FORTRAN_NAME(lookup_cool_rates1d_g)(&my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->NumberOfTemperatureBins, &j, &k, + &my_fields->grid_start[0], &my_fields->grid_end[0], &my_chemistry->three_body_rate, + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, &anydust, + &my_chemistry->H2_self_shielding, &my_chemistry->self_shielding_method, + tgas1d, mmw, my_fields->density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, + tdust, dust2gas, + my_rates->k1, my_rates->k2, my_rates->k3, my_rates->k4, my_rates->k5, my_rates->k6, my_rates->k7, my_rates->k8, my_rates->k9, my_rates->k10, + my_rates->k11, my_rates->k12, my_rates->k13, my_rates->k13dd, my_rates->k14, my_rates->k15, my_rates->k16, + my_rates->k17, my_rates->k18, my_rates->k19, my_rates->k22, + my_rates->k50, my_rates->k51, my_rates->k52, my_rates->k53, my_rates->k54, my_rates->k55, my_rates->k56, + my_rates->k57, my_rates->k58, &my_chemistry->NumberOfDustTemperatureBins, &my_chemistry->DustTemperatureStart, &my_chemistry->DustTemperatureEnd, my_rates->h2dust, + my_rates->n_cr_n, my_rates->n_cr_d1, my_rates->n_cr_d2, + &my_uvb_rates.crsHI, &my_uvb_rates.crsHeI, &my_uvb_rates.crsHeII, &my_uvb_rates.piHI, &my_uvb_rates.piHeI, + kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], + kcr_buf.data[ColRecRxnLUT::k11], kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], + kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], &my_uvb_rates.k24, &my_uvb_rates.k25, &my_uvb_rates.k26, &my_uvb_rates.k28, &my_uvb_rates.k29, &my_uvb_rates.k30, &my_uvb_rates.k31, + kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], + kcr_buf.data[ColRecRxnLUT::k58], k13dd, kshield_buf.k24, kshield_buf.k25, kshield_buf.k26, + kshield_buf.k28, kshield_buf.k29, kshield_buf.k30, + kshield_buf.k31, h2dust, chemheatrates_buf.n_cr_n, chemheatrates_buf.n_cr_d1, chemheatrates_buf.n_cr_d2, + logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.tdef, logTlininterp_buf.logtem, logTlininterp_buf.indixe, + &dom, &internalu.coolunit, &internalu.tbase1, &internalu.xbase1, &dx_cgs, &c_ljeans, + &my_chemistry->use_radiative_transfer, my_fields->RT_H2_dissociation_rate, my_fields->H2_self_shielding_length, itmask, + itmask_metal, + &my_chemistry->HydrogenFractionByMass, my_fields->metal_density, + my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, &imetal, &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, + my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, + my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, + my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, + my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, + my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, + my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, + my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, + my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, + my_rates->k125, my_rates->k129, my_rates->k130, my_rates->k131, my_rates->k132, + my_rates->k133, my_rates->k134, my_rates->k135, my_rates->k136, my_rates->k137, + my_rates->k148, my_rates->k149, my_rates->k150, my_rates->k151, my_rates->k152, + my_rates->k153, + my_rates->kz15, my_rates->kz16, my_rates->kz17, my_rates->kz18, my_rates->kz19, + my_rates->kz20, my_rates->kz21, my_rates->kz22, my_rates->kz23, my_rates->kz24, + my_rates->kz25, my_rates->kz26, my_rates->kz27, my_rates->kz28, my_rates->kz29, + my_rates->kz30, my_rates->kz31, my_rates->kz32, my_rates->kz33, my_rates->kz34, + my_rates->kz35, my_rates->kz36, my_rates->kz37, my_rates->kz38, my_rates->kz39, + my_rates->kz40, my_rates->kz41, my_rates->kz42, my_rates->kz43, my_rates->kz44, + my_rates->kz45, my_rates->kz46, my_rates->kz47, my_rates->kz48, my_rates->kz49, + my_rates->kz50, my_rates->kz51, my_rates->kz52, my_rates->kz53, my_rates->kz54, + kcr_buf.data[ColRecRxnLUT::k125], kcr_buf.data[ColRecRxnLUT::k129], kcr_buf.data[ColRecRxnLUT::k130], kcr_buf.data[ColRecRxnLUT::k131], kcr_buf.data[ColRecRxnLUT::k132], + kcr_buf.data[ColRecRxnLUT::k133], kcr_buf.data[ColRecRxnLUT::k134], kcr_buf.data[ColRecRxnLUT::k135], kcr_buf.data[ColRecRxnLUT::k136], kcr_buf.data[ColRecRxnLUT::k137], + kcr_buf.data[ColRecRxnLUT::k148], kcr_buf.data[ColRecRxnLUT::k149], kcr_buf.data[ColRecRxnLUT::k150], kcr_buf.data[ColRecRxnLUT::k151], kcr_buf.data[ColRecRxnLUT::k152], + kcr_buf.data[ColRecRxnLUT::k153], + kcr_buf.data[ColRecRxnLUT::kz15], kcr_buf.data[ColRecRxnLUT::kz16], kcr_buf.data[ColRecRxnLUT::kz17], kcr_buf.data[ColRecRxnLUT::kz18], kcr_buf.data[ColRecRxnLUT::kz19], + kcr_buf.data[ColRecRxnLUT::kz20], kcr_buf.data[ColRecRxnLUT::kz21], kcr_buf.data[ColRecRxnLUT::kz22], kcr_buf.data[ColRecRxnLUT::kz23], kcr_buf.data[ColRecRxnLUT::kz24], + kcr_buf.data[ColRecRxnLUT::kz25], kcr_buf.data[ColRecRxnLUT::kz26], kcr_buf.data[ColRecRxnLUT::kz27], kcr_buf.data[ColRecRxnLUT::kz28], kcr_buf.data[ColRecRxnLUT::kz29], + kcr_buf.data[ColRecRxnLUT::kz30], kcr_buf.data[ColRecRxnLUT::kz31], kcr_buf.data[ColRecRxnLUT::kz32], kcr_buf.data[ColRecRxnLUT::kz33], kcr_buf.data[ColRecRxnLUT::kz34], + kcr_buf.data[ColRecRxnLUT::kz35], kcr_buf.data[ColRecRxnLUT::kz36], kcr_buf.data[ColRecRxnLUT::kz37], kcr_buf.data[ColRecRxnLUT::kz38], kcr_buf.data[ColRecRxnLUT::kz39], + kcr_buf.data[ColRecRxnLUT::kz40], kcr_buf.data[ColRecRxnLUT::kz41], kcr_buf.data[ColRecRxnLUT::kz42], kcr_buf.data[ColRecRxnLUT::kz43], kcr_buf.data[ColRecRxnLUT::kz44], + kcr_buf.data[ColRecRxnLUT::kz45], kcr_buf.data[ColRecRxnLUT::kz46], kcr_buf.data[ColRecRxnLUT::kz47], kcr_buf.data[ColRecRxnLUT::kz48], kcr_buf.data[ColRecRxnLUT::kz49], + kcr_buf.data[ColRecRxnLUT::kz50], kcr_buf.data[ColRecRxnLUT::kz51], kcr_buf.data[ColRecRxnLUT::kz52], kcr_buf.data[ColRecRxnLUT::kz53], kcr_buf.data[ColRecRxnLUT::kz54], + &my_chemistry->multi_metals, &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, + my_fields->local_ISM_metal_density, + my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, + my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, + my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density, + &my_rates->SN0_N, + my_rates->SN0_fSiM, my_rates->SN0_fFeM, my_rates->SN0_fMg2SiO4, my_rates->SN0_fMgSiO3, + my_rates->SN0_fFe3O4, my_rates->SN0_fAC, my_rates->SN0_fSiO2D, my_rates->SN0_fMgO, + my_rates->SN0_fFeS, my_rates->SN0_fAl2O3, + my_rates->SN0_freforg, my_rates->SN0_fvolorg, my_rates->SN0_fH2Oice, + my_rates->SN0_r0SiM, my_rates->SN0_r0FeM, my_rates->SN0_r0Mg2SiO4, my_rates->SN0_r0MgSiO3, + my_rates->SN0_r0Fe3O4, my_rates->SN0_r0AC, my_rates->SN0_r0SiO2D, my_rates->SN0_r0MgO, + my_rates->SN0_r0FeS, my_rates->SN0_r0Al2O3, + my_rates->SN0_r0reforg, my_rates->SN0_r0volorg, my_rates->SN0_r0H2Oice, + my_rates->gr_N, &my_rates->gr_Size, &my_rates->gr_dT, my_rates->gr_Td, + my_rates->SN0_kpSiM, my_rates->SN0_kpFeM, my_rates->SN0_kpMg2SiO4, my_rates->SN0_kpMgSiO3, + my_rates->SN0_kpFe3O4, my_rates->SN0_kpAC, my_rates->SN0_kpSiO2D, my_rates->SN0_kpMgO, + my_rates->SN0_kpFeS, my_rates->SN0_kpAl2O3, + my_rates->SN0_kpreforg, my_rates->SN0_kpvolorg, my_rates->SN0_kpH2Oice, + my_rates->h2dustS, my_rates->h2dustC, rhoH, my_rates->grain_growth_rate, &dt, + grain_growth_rates.SiM, grain_growth_rates.FeM, grain_growth_rates.Mg2SiO4, + grain_growth_rates.MgSiO3, grain_growth_rates.Fe3O4, grain_growth_rates.AC, grain_growth_rates.SiO2D, grain_growth_rates.MgO, grain_growth_rates.FeS, + grain_growth_rates.Al2O3, grain_growth_rates.reforg, grain_growth_rates.volorg, grain_growth_rates.H2Oice, + grain_temperatures.SiM, grain_temperatures.FeM, grain_temperatures.Mg2SiO4, grain_temperatures.MgSiO3, grain_temperatures.Fe3O4, + grain_temperatures.AC, grain_temperatures.SiO2D, grain_temperatures.MgO, grain_temperatures.FeS, grain_temperatures.Al2O3, + grain_temperatures.reforg, grain_temperatures.volorg, grain_temperatures.H2Oice, &my_chemistry->radiative_transfer_use_H2_shielding, + &my_chemistry->H2_custom_shielding, my_fields->H2_custom_shielding_factor + ); + +} // the following case was handcoded (so the argument order may shift when we // actually transcribe the routine) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 54031a670..9ec8fbd0c 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -474,93 +474,13 @@ int solve_rate_cool_g( // Look-up rates as a function of temperature for 1D set of zones // (maybe should add itmask to this call) - FORTRAN_NAME(lookup_cool_rates1d_g)(&my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->NumberOfTemperatureBins, &j, &k, - &my_fields->grid_start[0], &my_fields->grid_end[0], &my_chemistry->three_body_rate, - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, &anydust, - &my_chemistry->H2_self_shielding, &my_chemistry->self_shielding_method, - tgas.data(), mmw.data(), d.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, - my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, - tdust.data(), dust2gas.data(), - my_rates->k1, my_rates->k2, my_rates->k3, my_rates->k4, my_rates->k5, my_rates->k6, my_rates->k7, my_rates->k8, my_rates->k9, my_rates->k10, - my_rates->k11, my_rates->k12, my_rates->k13, my_rates->k13dd, my_rates->k14, my_rates->k15, my_rates->k16, - my_rates->k17, my_rates->k18, my_rates->k19, my_rates->k22, - my_rates->k50, my_rates->k51, my_rates->k52, my_rates->k53, my_rates->k54, my_rates->k55, my_rates->k56, - my_rates->k57, my_rates->k58, &my_chemistry->NumberOfDustTemperatureBins, &my_chemistry->DustTemperatureStart, &my_chemistry->DustTemperatureEnd, my_rates->h2dust, - my_rates->n_cr_n, my_rates->n_cr_d1, my_rates->n_cr_d2, - &my_uvb_rates->crsHI, &my_uvb_rates->crsHeI, &my_uvb_rates->crsHeII, &my_uvb_rates->piHI, &my_uvb_rates->piHeI, - kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], - kcr_buf.data[ColRecRxnLUT::k11], kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], - kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k28, &my_uvb_rates->k29, &my_uvb_rates->k30, &my_uvb_rates->k31, - kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], - kcr_buf.data[ColRecRxnLUT::k58], k13dd.data(), kshield_buf.k24, kshield_buf.k25, kshield_buf.k26, - kshield_buf.k28, kshield_buf.k29, kshield_buf.k30, - kshield_buf.k31, h2dust.data(), chemheatrates_buf.n_cr_n, chemheatrates_buf.n_cr_d1, chemheatrates_buf.n_cr_d2, - logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.tdef, logTlininterp_buf.logtem, logTlininterp_buf.indixe, - &dom, &internalu.coolunit, &internalu.tbase1, &internalu.xbase1, &dx_cgs, &c_ljeans, - &my_chemistry->use_radiative_transfer, my_fields->RT_H2_dissociation_rate, my_fields->H2_self_shielding_length, itmask.data(), - itmask_metal.data(), - &my_chemistry->HydrogenFractionByMass, my_fields->metal_density, - my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, &imetal, &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, - my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, - my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, - my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, - my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, - my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, - my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, - my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, - my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, - my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, - my_rates->k125, my_rates->k129, my_rates->k130, my_rates->k131, my_rates->k132, - my_rates->k133, my_rates->k134, my_rates->k135, my_rates->k136, my_rates->k137, - my_rates->k148, my_rates->k149, my_rates->k150, my_rates->k151, my_rates->k152, - my_rates->k153, - my_rates->kz15, my_rates->kz16, my_rates->kz17, my_rates->kz18, my_rates->kz19, - my_rates->kz20, my_rates->kz21, my_rates->kz22, my_rates->kz23, my_rates->kz24, - my_rates->kz25, my_rates->kz26, my_rates->kz27, my_rates->kz28, my_rates->kz29, - my_rates->kz30, my_rates->kz31, my_rates->kz32, my_rates->kz33, my_rates->kz34, - my_rates->kz35, my_rates->kz36, my_rates->kz37, my_rates->kz38, my_rates->kz39, - my_rates->kz40, my_rates->kz41, my_rates->kz42, my_rates->kz43, my_rates->kz44, - my_rates->kz45, my_rates->kz46, my_rates->kz47, my_rates->kz48, my_rates->kz49, - my_rates->kz50, my_rates->kz51, my_rates->kz52, my_rates->kz53, my_rates->kz54, - kcr_buf.data[ColRecRxnLUT::k125], kcr_buf.data[ColRecRxnLUT::k129], kcr_buf.data[ColRecRxnLUT::k130], kcr_buf.data[ColRecRxnLUT::k131], kcr_buf.data[ColRecRxnLUT::k132], - kcr_buf.data[ColRecRxnLUT::k133], kcr_buf.data[ColRecRxnLUT::k134], kcr_buf.data[ColRecRxnLUT::k135], kcr_buf.data[ColRecRxnLUT::k136], kcr_buf.data[ColRecRxnLUT::k137], - kcr_buf.data[ColRecRxnLUT::k148], kcr_buf.data[ColRecRxnLUT::k149], kcr_buf.data[ColRecRxnLUT::k150], kcr_buf.data[ColRecRxnLUT::k151], kcr_buf.data[ColRecRxnLUT::k152], - kcr_buf.data[ColRecRxnLUT::k153], - kcr_buf.data[ColRecRxnLUT::kz15], kcr_buf.data[ColRecRxnLUT::kz16], kcr_buf.data[ColRecRxnLUT::kz17], kcr_buf.data[ColRecRxnLUT::kz18], kcr_buf.data[ColRecRxnLUT::kz19], - kcr_buf.data[ColRecRxnLUT::kz20], kcr_buf.data[ColRecRxnLUT::kz21], kcr_buf.data[ColRecRxnLUT::kz22], kcr_buf.data[ColRecRxnLUT::kz23], kcr_buf.data[ColRecRxnLUT::kz24], - kcr_buf.data[ColRecRxnLUT::kz25], kcr_buf.data[ColRecRxnLUT::kz26], kcr_buf.data[ColRecRxnLUT::kz27], kcr_buf.data[ColRecRxnLUT::kz28], kcr_buf.data[ColRecRxnLUT::kz29], - kcr_buf.data[ColRecRxnLUT::kz30], kcr_buf.data[ColRecRxnLUT::kz31], kcr_buf.data[ColRecRxnLUT::kz32], kcr_buf.data[ColRecRxnLUT::kz33], kcr_buf.data[ColRecRxnLUT::kz34], - kcr_buf.data[ColRecRxnLUT::kz35], kcr_buf.data[ColRecRxnLUT::kz36], kcr_buf.data[ColRecRxnLUT::kz37], kcr_buf.data[ColRecRxnLUT::kz38], kcr_buf.data[ColRecRxnLUT::kz39], - kcr_buf.data[ColRecRxnLUT::kz40], kcr_buf.data[ColRecRxnLUT::kz41], kcr_buf.data[ColRecRxnLUT::kz42], kcr_buf.data[ColRecRxnLUT::kz43], kcr_buf.data[ColRecRxnLUT::kz44], - kcr_buf.data[ColRecRxnLUT::kz45], kcr_buf.data[ColRecRxnLUT::kz46], kcr_buf.data[ColRecRxnLUT::kz47], kcr_buf.data[ColRecRxnLUT::kz48], kcr_buf.data[ColRecRxnLUT::kz49], - kcr_buf.data[ColRecRxnLUT::kz50], kcr_buf.data[ColRecRxnLUT::kz51], kcr_buf.data[ColRecRxnLUT::kz52], kcr_buf.data[ColRecRxnLUT::kz53], kcr_buf.data[ColRecRxnLUT::kz54], - &my_chemistry->multi_metals, &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, - my_fields->local_ISM_metal_density, - my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, - my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, - my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density, - &my_rates->SN0_N, - my_rates->SN0_fSiM, my_rates->SN0_fFeM, my_rates->SN0_fMg2SiO4, my_rates->SN0_fMgSiO3, - my_rates->SN0_fFe3O4, my_rates->SN0_fAC, my_rates->SN0_fSiO2D, my_rates->SN0_fMgO, - my_rates->SN0_fFeS, my_rates->SN0_fAl2O3, - my_rates->SN0_freforg, my_rates->SN0_fvolorg, my_rates->SN0_fH2Oice, - my_rates->SN0_r0SiM, my_rates->SN0_r0FeM, my_rates->SN0_r0Mg2SiO4, my_rates->SN0_r0MgSiO3, - my_rates->SN0_r0Fe3O4, my_rates->SN0_r0AC, my_rates->SN0_r0SiO2D, my_rates->SN0_r0MgO, - my_rates->SN0_r0FeS, my_rates->SN0_r0Al2O3, - my_rates->SN0_r0reforg, my_rates->SN0_r0volorg, my_rates->SN0_r0H2Oice, - my_rates->gr_N, &my_rates->gr_Size, &my_rates->gr_dT, my_rates->gr_Td, - my_rates->SN0_kpSiM, my_rates->SN0_kpFeM, my_rates->SN0_kpMg2SiO4, my_rates->SN0_kpMgSiO3, - my_rates->SN0_kpFe3O4, my_rates->SN0_kpAC, my_rates->SN0_kpSiO2D, my_rates->SN0_kpMgO, - my_rates->SN0_kpFeS, my_rates->SN0_kpAl2O3, - my_rates->SN0_kpreforg, my_rates->SN0_kpvolorg, my_rates->SN0_kpH2Oice, - my_rates->h2dustS, my_rates->h2dustC, rhoH.data(), my_rates->grain_growth_rate, &dt, - grain_growth_rates.SiM, grain_growth_rates.FeM, grain_growth_rates.Mg2SiO4, - grain_growth_rates.MgSiO3, grain_growth_rates.Fe3O4, grain_growth_rates.AC, grain_growth_rates.SiO2D, grain_growth_rates.MgO, grain_growth_rates.FeS, - grain_growth_rates.Al2O3, grain_growth_rates.reforg, grain_growth_rates.volorg, grain_growth_rates.H2Oice, - grain_temperatures.SiM, grain_temperatures.FeM, grain_temperatures.Mg2SiO4, grain_temperatures.MgSiO3, grain_temperatures.Fe3O4, - grain_temperatures.AC, grain_temperatures.SiO2D, grain_temperatures.MgO, grain_temperatures.FeS, grain_temperatures.Al2O3, - grain_temperatures.reforg, grain_temperatures.volorg, grain_temperatures.H2Oice, &my_chemistry->radiative_transfer_use_H2_shielding, - &my_chemistry->H2_custom_shielding, my_fields->H2_custom_shielding_factor + wrapped_lookup_cool_rates1d_g_( + j, k, anydust, tgas.data(), mmw.data(), + tdust.data(), dust2gas.data(), k13dd.data(), h2dust.data(), + dom, dx_cgs, c_ljeans, itmask.data(), itmask_metal.data(), + imetal, rhoH.data(), dt, my_chemistry, my_rates, my_fields, + *my_uvb_rates, internalu, grain_growth_rates, grain_temperatures, + logTlininterp_buf, kcr_buf, kshield_buf, chemheatrates_buf ); // Compute dedot and HIdot, the rates of change of de and HI From 41e874bdc1bb3c70c488b3fca8aba1e9f54520fd Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 19 Jan 2025 10:41:09 -0500 Subject: [PATCH 053/101] introduce C++ wrapper for `lookup_cool_rates1d_g` subroutine. --- src/clib/fortran_func_wrappers.hpp | 32 ++++++++++++++++++++++++++++++ src/clib/solve_rate_cool_g-cpp.C | 25 ++++++----------------- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/clib/fortran_func_wrappers.hpp b/src/clib/fortran_func_wrappers.hpp index 9ab0d78de..4ab9cf96f 100644 --- a/src/clib/fortran_func_wrappers.hpp +++ b/src/clib/fortran_func_wrappers.hpp @@ -198,6 +198,38 @@ inline void wrapped_make_consistent_g_( ); } +/// This routine calculates the electron and HI rates of change in order to +/// determine the maximum permitted timestep +inline void wrapped_rate_timestep_g_( + double* dedot, double* HIdot, gr_mask_type anydust, int j, int k, + double* h2dust, double* rhoH, gr_mask_type* itmask, double* edot, + double chunit, double dom, chemistry_data* my_chemistry, + grackle_field_data* my_fields, photo_rate_storage my_uvb_rates, + grackle::impl::ColRecRxnRateCollection kcr_buf, + grackle::impl::PhotoRxnRateCollection kshield_buf, + grackle::impl::ChemHeatingRates chemheatrates_buf +) { + FORTRAN_NAME(rate_timestep_g)( + dedot, HIdot, &my_chemistry->primordial_chemistry, &anydust, + my_fields->e_density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, my_fields->density, + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, + kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], kcr_buf.data[ColRecRxnLUT::k11], + kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], + &my_uvb_rates.k24, &my_uvb_rates.k25, &my_uvb_rates.k26, &my_uvb_rates.k27, &my_uvb_rates.k28, &my_uvb_rates.k29, &my_uvb_rates.k30, + kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], kcr_buf.data[ColRecRxnLUT::k58], + h2dust, chemheatrates_buf.n_cr_n, chemheatrates_buf.n_cr_d1, chemheatrates_buf.n_cr_d2, rhoH, + kshield_buf.k24, kshield_buf.k25, kshield_buf.k26, + kshield_buf.k28, kshield_buf.k29, kshield_buf.k30, kshield_buf.k31, + &my_chemistry->use_radiative_transfer, &my_chemistry->radiative_transfer_hydrogen_only, + my_fields->RT_HI_ionization_rate, my_fields->RT_HeI_ionization_rate, my_fields->RT_HeII_ionization_rate, + itmask, edot, &chunit, &dom, my_fields->metal_density, + my_fields->HDI_density, &my_chemistry->metal_chemistry, my_fields->CI_density, my_fields->OI_density, my_fields->OH_density, my_fields->CO_density, my_fields->H2O_density, + &my_chemistry->radiative_transfer_HDI_dissociation, my_fields->RT_HDI_dissociation_rate, &my_chemistry->radiative_transfer_metal_ionization, my_fields->RT_CI_ionization_rate, my_fields->RT_OI_ionization_rate, + &my_chemistry->radiative_transfer_metal_dissociation, my_fields->RT_CO_dissociation_rate, my_fields->RT_OH_dissociation_rate, my_fields->RT_H2O_dissociation_rate + ); +} + inline void wrapped_scale_fields_g_( int imetal, gr_float factor, chemistry_data* my_chemistry, diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 9ec8fbd0c..f1d7ac12f 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -486,25 +486,12 @@ int solve_rate_cool_g( // Compute dedot and HIdot, the rates of change of de and HI // (should add itmask to this call) - FORTRAN_NAME(rate_timestep_g)( - dedot.data(), HIdot.data(), &my_chemistry->primordial_chemistry, &anydust, - de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, d.data(), - my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, - kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], kcr_buf.data[ColRecRxnLUT::k11], - kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], - &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k27, &my_uvb_rates->k28, &my_uvb_rates->k29, &my_uvb_rates->k30, - kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], kcr_buf.data[ColRecRxnLUT::k58], - h2dust.data(), chemheatrates_buf.n_cr_n, chemheatrates_buf.n_cr_d1, chemheatrates_buf.n_cr_d2, rhoH.data(), - kshield_buf.k24, kshield_buf.k25, kshield_buf.k26, - kshield_buf.k28, kshield_buf.k29, kshield_buf.k30, kshield_buf.k31, - &my_chemistry->use_radiative_transfer, &my_chemistry->radiative_transfer_hydrogen_only, - kphHI.data(), my_fields->RT_HeI_ionization_rate, my_fields->RT_HeII_ionization_rate, - itmask.data(), edot.data(), &chunit, &dom, my_fields->metal_density, - my_fields->HDI_density, &my_chemistry->metal_chemistry, my_fields->CI_density, my_fields->OI_density, my_fields->OH_density, my_fields->CO_density, my_fields->H2O_density, - &my_chemistry->radiative_transfer_HDI_dissociation, my_fields->RT_HDI_dissociation_rate, &my_chemistry->radiative_transfer_metal_ionization, my_fields->RT_CI_ionization_rate, my_fields->RT_OI_ionization_rate, - &my_chemistry->radiative_transfer_metal_dissociation, my_fields->RT_CO_dissociation_rate, my_fields->RT_OH_dissociation_rate, my_fields->RT_H2O_dissociation_rate - ); + wrapped_rate_timestep_g_( + dedot.data(), HIdot.data(), anydust, j, k, h2dust.data(), + rhoH.data(), itmask.data(), edot.data(), chunit, dom, + my_chemistry, my_fields, *my_uvb_rates, kcr_buf, kshield_buf, + chemheatrates_buf + ); // move itmask temporary array // then split cells with low densities From c64b865db56ca8bd84b4f2848dbbcb8e4f74f041 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 19 Jan 2025 11:06:55 -0500 Subject: [PATCH 054/101] introduce C++ wrapper for `step_rate_g` subroutine --- src/clib/fortran_func_wrappers.hpp | 72 ++++++++++++++++++++++++++++++ src/clib/solve_rate_cool_g-cpp.C | 64 +++----------------------- 2 files changed, 79 insertions(+), 57 deletions(-) diff --git a/src/clib/fortran_func_wrappers.hpp b/src/clib/fortran_func_wrappers.hpp index 4ab9cf96f..e8eb0d7f3 100644 --- a/src/clib/fortran_func_wrappers.hpp +++ b/src/clib/fortran_func_wrappers.hpp @@ -258,6 +258,78 @@ inline void wrapped_scale_fields_g_( } +/// Uses one linearly implicit Gauss-Seidel sweep of a backward-Euler time +/// integrator to advance the rate equations by one (sub-)cycle (dtit). +inline void wrapped_step_rate_g_( + double* dtit, int j, int k, gr_mask_type anydust, double* h2dust, + double* rhoH, double* dedot_prev, double* HIdot_prev, + gr_mask_type* itmask, gr_mask_type* itmask_metal, int imetal, + chemistry_data* my_chemistry, grackle_field_data* my_fields, + photo_rate_storage my_uvb_rates, + grackle::impl::GrainSpeciesCollection grain_growth_rates, + grackle::impl::SpeciesCollection species_tmpdens, + grackle::impl::ColRecRxnRateCollection kcr_buf, + grackle::impl::PhotoRxnRateCollection kshield_buf +) { + FORTRAN_NAME(step_rate_g)(my_fields->e_density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, my_fields->density, + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, dtit, + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, + &my_chemistry->primordial_chemistry, &anydust, + kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], kcr_buf.data[ColRecRxnLUT::k11], + kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], + &my_uvb_rates.k24, &my_uvb_rates.k25, &my_uvb_rates.k26, &my_uvb_rates.k27, &my_uvb_rates.k28, &my_uvb_rates.k29, &my_uvb_rates.k30, + kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], kcr_buf.data[ColRecRxnLUT::k58], + h2dust, rhoH, + kshield_buf.k24, kshield_buf.k25, kshield_buf.k26, + kshield_buf.k28, kshield_buf.k29, kshield_buf.k30, kshield_buf.k31, + species_tmpdens.data[SpLUT::HI], species_tmpdens.data[SpLUT::HII], species_tmpdens.data[SpLUT::HeI], species_tmpdens.data[SpLUT::HeII], species_tmpdens.data[SpLUT::HeIII], species_tmpdens.data[SpLUT::e], + species_tmpdens.data[SpLUT::HM], species_tmpdens.data[SpLUT::H2I], species_tmpdens.data[SpLUT::H2II], species_tmpdens.data[SpLUT::DI], species_tmpdens.data[SpLUT::DII], species_tmpdens.data[SpLUT::HDI], + dedot_prev, HIdot_prev, + &my_chemistry->use_radiative_transfer, &my_chemistry->radiative_transfer_hydrogen_only, + my_fields->RT_HI_ionization_rate, my_fields->RT_HeI_ionization_rate, my_fields->RT_HeII_ionization_rate, + itmask, itmask_metal, + my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, &imetal, my_fields->metal_density, + &my_chemistry->metal_chemistry, &my_chemistry->dust_species, &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, + my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, + my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, + my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, + my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, + my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, + my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, + my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, + my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, + kcr_buf.data[ColRecRxnLUT::k125], kcr_buf.data[ColRecRxnLUT::k129], kcr_buf.data[ColRecRxnLUT::k130], kcr_buf.data[ColRecRxnLUT::k131], kcr_buf.data[ColRecRxnLUT::k132], + kcr_buf.data[ColRecRxnLUT::k133], kcr_buf.data[ColRecRxnLUT::k134], kcr_buf.data[ColRecRxnLUT::k135], kcr_buf.data[ColRecRxnLUT::k136], kcr_buf.data[ColRecRxnLUT::k137], + kcr_buf.data[ColRecRxnLUT::k148], kcr_buf.data[ColRecRxnLUT::k149], kcr_buf.data[ColRecRxnLUT::k150], kcr_buf.data[ColRecRxnLUT::k151], kcr_buf.data[ColRecRxnLUT::k152], + kcr_buf.data[ColRecRxnLUT::k153], + kcr_buf.data[ColRecRxnLUT::kz15], kcr_buf.data[ColRecRxnLUT::kz16], kcr_buf.data[ColRecRxnLUT::kz17], kcr_buf.data[ColRecRxnLUT::kz18], kcr_buf.data[ColRecRxnLUT::kz19], + kcr_buf.data[ColRecRxnLUT::kz20], kcr_buf.data[ColRecRxnLUT::kz21], kcr_buf.data[ColRecRxnLUT::kz22], kcr_buf.data[ColRecRxnLUT::kz23], kcr_buf.data[ColRecRxnLUT::kz24], + kcr_buf.data[ColRecRxnLUT::kz25], kcr_buf.data[ColRecRxnLUT::kz26], kcr_buf.data[ColRecRxnLUT::kz27], kcr_buf.data[ColRecRxnLUT::kz28], kcr_buf.data[ColRecRxnLUT::kz29], + kcr_buf.data[ColRecRxnLUT::kz30], kcr_buf.data[ColRecRxnLUT::kz31], kcr_buf.data[ColRecRxnLUT::kz32], kcr_buf.data[ColRecRxnLUT::kz33], kcr_buf.data[ColRecRxnLUT::kz34], + kcr_buf.data[ColRecRxnLUT::kz35], kcr_buf.data[ColRecRxnLUT::kz36], kcr_buf.data[ColRecRxnLUT::kz37], kcr_buf.data[ColRecRxnLUT::kz38], kcr_buf.data[ColRecRxnLUT::kz39], + kcr_buf.data[ColRecRxnLUT::kz40], kcr_buf.data[ColRecRxnLUT::kz41], kcr_buf.data[ColRecRxnLUT::kz42], kcr_buf.data[ColRecRxnLUT::kz43], kcr_buf.data[ColRecRxnLUT::kz44], + kcr_buf.data[ColRecRxnLUT::kz45], kcr_buf.data[ColRecRxnLUT::kz46], kcr_buf.data[ColRecRxnLUT::kz47], kcr_buf.data[ColRecRxnLUT::kz48], kcr_buf.data[ColRecRxnLUT::kz49], + kcr_buf.data[ColRecRxnLUT::kz50], kcr_buf.data[ColRecRxnLUT::kz51], kcr_buf.data[ColRecRxnLUT::kz52], kcr_buf.data[ColRecRxnLUT::kz53], kcr_buf.data[ColRecRxnLUT::kz54], + species_tmpdens.data[SpLUT::DM], species_tmpdens.data[SpLUT::HDII], species_tmpdens.data[SpLUT::HeHII], + species_tmpdens.data[SpLUT::CI], species_tmpdens.data[SpLUT::CII], species_tmpdens.data[SpLUT::CO], species_tmpdens.data[SpLUT::CO2], + species_tmpdens.data[SpLUT::OI], species_tmpdens.data[SpLUT::OH], species_tmpdens.data[SpLUT::H2O], species_tmpdens.data[SpLUT::O2], + species_tmpdens.data[SpLUT::SiI], species_tmpdens.data[SpLUT::SiOI], species_tmpdens.data[SpLUT::SiO2I], + species_tmpdens.data[SpLUT::CH], species_tmpdens.data[SpLUT::CH2], species_tmpdens.data[SpLUT::COII], species_tmpdens.data[SpLUT::OII], + species_tmpdens.data[SpLUT::OHII], species_tmpdens.data[SpLUT::H2OII], species_tmpdens.data[SpLUT::H3OII], species_tmpdens.data[SpLUT::O2II], + species_tmpdens.data[SpLUT::Mg], species_tmpdens.data[SpLUT::Al], species_tmpdens.data[SpLUT::S], species_tmpdens.data[SpLUT::Fe], + species_tmpdens.data[SpLUT::SiM], species_tmpdens.data[SpLUT::FeM], species_tmpdens.data[SpLUT::Mg2SiO4], species_tmpdens.data[SpLUT::MgSiO3], species_tmpdens.data[SpLUT::Fe3O4], + species_tmpdens.data[SpLUT::AC], species_tmpdens.data[SpLUT::SiO2D], species_tmpdens.data[SpLUT::MgO], species_tmpdens.data[SpLUT::FeS], species_tmpdens.data[SpLUT::Al2O3], + species_tmpdens.data[SpLUT::reforg], species_tmpdens.data[SpLUT::volorg], species_tmpdens.data[SpLUT::H2Oice], + grain_growth_rates.SiM, grain_growth_rates.FeM, grain_growth_rates.Mg2SiO4, grain_growth_rates.MgSiO3, grain_growth_rates.Fe3O4, + grain_growth_rates.AC, grain_growth_rates.SiO2D, grain_growth_rates.MgO, grain_growth_rates.FeS, grain_growth_rates.Al2O3, + grain_growth_rates.reforg, grain_growth_rates.volorg, grain_growth_rates.H2Oice, + &my_chemistry->radiative_transfer_HDI_dissociation, my_fields->RT_HDI_dissociation_rate, &my_chemistry->radiative_transfer_metal_ionization, my_fields->RT_CI_ionization_rate, my_fields->RT_OI_ionization_rate, + &my_chemistry->radiative_transfer_metal_dissociation, my_fields->RT_CO_dissociation_rate, my_fields->RT_OH_dissociation_rate, my_fields->RT_H2O_dissociation_rate + ); + +} + #endif /* FORTRAN_FUNC_WRAPPERS_HPP */ diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index f1d7ac12f..a3b4d7fdb 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -663,63 +663,13 @@ int solve_rate_cool_g( // sweep of a backward Euler method (for all cells specified by // itmask) - FORTRAN_NAME(step_rate_g)(de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, d.data(), - my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, dtit.data(), - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, - &my_chemistry->primordial_chemistry, &anydust, - kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], kcr_buf.data[ColRecRxnLUT::k11], - kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], - &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k27, &my_uvb_rates->k28, &my_uvb_rates->k29, &my_uvb_rates->k30, - kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], kcr_buf.data[ColRecRxnLUT::k58], - h2dust.data(), rhoH.data(), - kshield_buf.k24, kshield_buf.k25, kshield_buf.k26, - kshield_buf.k28, kshield_buf.k29, kshield_buf.k30, kshield_buf.k31, - species_tmpdens.data[SpLUT::HI], species_tmpdens.data[SpLUT::HII], species_tmpdens.data[SpLUT::HeI], species_tmpdens.data[SpLUT::HeII], species_tmpdens.data[SpLUT::HeIII], species_tmpdens.data[SpLUT::e], - species_tmpdens.data[SpLUT::HM], species_tmpdens.data[SpLUT::H2I], species_tmpdens.data[SpLUT::H2II], species_tmpdens.data[SpLUT::DI], species_tmpdens.data[SpLUT::DII], species_tmpdens.data[SpLUT::HDI], - dedot_prev.data(), HIdot_prev.data(), - &my_chemistry->use_radiative_transfer, &my_chemistry->radiative_transfer_hydrogen_only, - kphHI.data(), my_fields->RT_HeI_ionization_rate, my_fields->RT_HeII_ionization_rate, - itmask.data(), itmask_metal.data(), - my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, &imetal, my_fields->metal_density, - &my_chemistry->metal_chemistry, &my_chemistry->dust_species, &my_chemistry->grain_growth, &my_chemistry->dust_sublimation, - my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, - my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, - my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, - my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, - my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, - my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, - my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, - my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, - my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, - kcr_buf.data[ColRecRxnLUT::k125], kcr_buf.data[ColRecRxnLUT::k129], kcr_buf.data[ColRecRxnLUT::k130], kcr_buf.data[ColRecRxnLUT::k131], kcr_buf.data[ColRecRxnLUT::k132], - kcr_buf.data[ColRecRxnLUT::k133], kcr_buf.data[ColRecRxnLUT::k134], kcr_buf.data[ColRecRxnLUT::k135], kcr_buf.data[ColRecRxnLUT::k136], kcr_buf.data[ColRecRxnLUT::k137], - kcr_buf.data[ColRecRxnLUT::k148], kcr_buf.data[ColRecRxnLUT::k149], kcr_buf.data[ColRecRxnLUT::k150], kcr_buf.data[ColRecRxnLUT::k151], kcr_buf.data[ColRecRxnLUT::k152], - kcr_buf.data[ColRecRxnLUT::k153], - kcr_buf.data[ColRecRxnLUT::kz15], kcr_buf.data[ColRecRxnLUT::kz16], kcr_buf.data[ColRecRxnLUT::kz17], kcr_buf.data[ColRecRxnLUT::kz18], kcr_buf.data[ColRecRxnLUT::kz19], - kcr_buf.data[ColRecRxnLUT::kz20], kcr_buf.data[ColRecRxnLUT::kz21], kcr_buf.data[ColRecRxnLUT::kz22], kcr_buf.data[ColRecRxnLUT::kz23], kcr_buf.data[ColRecRxnLUT::kz24], - kcr_buf.data[ColRecRxnLUT::kz25], kcr_buf.data[ColRecRxnLUT::kz26], kcr_buf.data[ColRecRxnLUT::kz27], kcr_buf.data[ColRecRxnLUT::kz28], kcr_buf.data[ColRecRxnLUT::kz29], - kcr_buf.data[ColRecRxnLUT::kz30], kcr_buf.data[ColRecRxnLUT::kz31], kcr_buf.data[ColRecRxnLUT::kz32], kcr_buf.data[ColRecRxnLUT::kz33], kcr_buf.data[ColRecRxnLUT::kz34], - kcr_buf.data[ColRecRxnLUT::kz35], kcr_buf.data[ColRecRxnLUT::kz36], kcr_buf.data[ColRecRxnLUT::kz37], kcr_buf.data[ColRecRxnLUT::kz38], kcr_buf.data[ColRecRxnLUT::kz39], - kcr_buf.data[ColRecRxnLUT::kz40], kcr_buf.data[ColRecRxnLUT::kz41], kcr_buf.data[ColRecRxnLUT::kz42], kcr_buf.data[ColRecRxnLUT::kz43], kcr_buf.data[ColRecRxnLUT::kz44], - kcr_buf.data[ColRecRxnLUT::kz45], kcr_buf.data[ColRecRxnLUT::kz46], kcr_buf.data[ColRecRxnLUT::kz47], kcr_buf.data[ColRecRxnLUT::kz48], kcr_buf.data[ColRecRxnLUT::kz49], - kcr_buf.data[ColRecRxnLUT::kz50], kcr_buf.data[ColRecRxnLUT::kz51], kcr_buf.data[ColRecRxnLUT::kz52], kcr_buf.data[ColRecRxnLUT::kz53], kcr_buf.data[ColRecRxnLUT::kz54], - species_tmpdens.data[SpLUT::DM], species_tmpdens.data[SpLUT::HDII], species_tmpdens.data[SpLUT::HeHII], - species_tmpdens.data[SpLUT::CI], species_tmpdens.data[SpLUT::CII], species_tmpdens.data[SpLUT::CO], species_tmpdens.data[SpLUT::CO2], - species_tmpdens.data[SpLUT::OI], species_tmpdens.data[SpLUT::OH], species_tmpdens.data[SpLUT::H2O], species_tmpdens.data[SpLUT::O2], - species_tmpdens.data[SpLUT::SiI], species_tmpdens.data[SpLUT::SiOI], species_tmpdens.data[SpLUT::SiO2I], - species_tmpdens.data[SpLUT::CH], species_tmpdens.data[SpLUT::CH2], species_tmpdens.data[SpLUT::COII], species_tmpdens.data[SpLUT::OII], - species_tmpdens.data[SpLUT::OHII], species_tmpdens.data[SpLUT::H2OII], species_tmpdens.data[SpLUT::H3OII], species_tmpdens.data[SpLUT::O2II], - species_tmpdens.data[SpLUT::Mg], species_tmpdens.data[SpLUT::Al], species_tmpdens.data[SpLUT::S], species_tmpdens.data[SpLUT::Fe], - species_tmpdens.data[SpLUT::SiM], species_tmpdens.data[SpLUT::FeM], species_tmpdens.data[SpLUT::Mg2SiO4], species_tmpdens.data[SpLUT::MgSiO3], species_tmpdens.data[SpLUT::Fe3O4], - species_tmpdens.data[SpLUT::AC], species_tmpdens.data[SpLUT::SiO2D], species_tmpdens.data[SpLUT::MgO], species_tmpdens.data[SpLUT::FeS], species_tmpdens.data[SpLUT::Al2O3], - species_tmpdens.data[SpLUT::reforg], species_tmpdens.data[SpLUT::volorg], species_tmpdens.data[SpLUT::H2Oice], - grain_growth_rates.SiM, grain_growth_rates.FeM, grain_growth_rates.Mg2SiO4, grain_growth_rates.MgSiO3, grain_growth_rates.Fe3O4, - grain_growth_rates.AC, grain_growth_rates.SiO2D, grain_growth_rates.MgO, grain_growth_rates.FeS, grain_growth_rates.Al2O3, - grain_growth_rates.reforg, grain_growth_rates.volorg, grain_growth_rates.H2Oice, - &my_chemistry->radiative_transfer_HDI_dissociation, my_fields->RT_HDI_dissociation_rate, &my_chemistry->radiative_transfer_metal_ionization, my_fields->RT_CI_ionization_rate, my_fields->RT_OI_ionization_rate, - &my_chemistry->radiative_transfer_metal_dissociation, my_fields->RT_CO_dissociation_rate, my_fields->RT_OH_dissociation_rate, my_fields->RT_H2O_dissociation_rate - ); - + wrapped_step_rate_g_( + dtit.data(), j, k, anydust, h2dust.data(), rhoH.data(), + dedot_prev.data(), HIdot_prev.data(), itmask.data(), + itmask_metal.data(), imetal, my_chemistry, my_fields, + *my_uvb_rates, grain_growth_rates, species_tmpdens, kcr_buf, + kshield_buf + ); // Solve rate equations with one linearly implicit Gauss-Seidel // sweep of a backward Euler method (for all cells specified by From f8784d30105575421342fd1a9175ab44f2428223 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 19 Jan 2025 11:35:24 -0500 Subject: [PATCH 055/101] introduce C++ wrapper for `step_rate_newton_raphson` subroutine --- src/clib/fortran_func_wrappers.hpp | 114 +++++++++++++++++++++++++++++ src/clib/solve_rate_cool_g-cpp.C | 102 +++----------------------- 2 files changed, 124 insertions(+), 92 deletions(-) diff --git a/src/clib/fortran_func_wrappers.hpp b/src/clib/fortran_func_wrappers.hpp index e8eb0d7f3..29661bb4b 100644 --- a/src/clib/fortran_func_wrappers.hpp +++ b/src/clib/fortran_func_wrappers.hpp @@ -330,6 +330,120 @@ inline void wrapped_step_rate_g_( } +inline void wrapped_step_rate_newton_raphson_( + int imetal, double utem, int j, int k, int iter, double dom, double* comp1, + double* comp2, double chunit, double dx_cgs, double c_ljeans, double* dtit, + double* p2d, double* tgas, double* tdust, double* metallicity, + double* dust2gas, double* rhoH, double* mmw, double* h2dust, double* edot, + gr_mask_type anydust, gr_mask_type* itmask_nr, gr_mask_type* itmask_metal, + int* imp_eng, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, + grackle_field_data* my_fields, photo_rate_storage my_uvb_rates, + InternalGrUnits internalu, + grackle::impl::GrainSpeciesCollection grain_temperatures, + grackle::impl::LogTLinInterpScratchBuf logTlininterp_buf, + grackle::impl::Cool1DMultiScratchBuf cool1dmulti_buf, + grackle::impl::CoolHeatScratchBuf coolingheating_buf, + grackle::impl::ChemHeatingRates chemheatrates_buf +) { + FORTRAN_NAME(step_rate_newton_raphson)(&my_chemistry->with_radiative_cooling, my_fields->density, my_fields->internal_energy, my_fields->x_velocity, my_fields->y_velocity, my_fields->z_velocity, my_fields->e_density, my_fields->HI_density, + my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, + &internalu.extfields_in_comoving, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, + &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_fields->grid_start[0], &my_fields->grid_end[0], &my_chemistry->ih2co, &my_chemistry->ipiht, + &my_chemistry->dust_recombination_cooling, &my_chemistry->photoelectric_heating, &internalu.a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &utem, + &internalu.uxyz, &internalu.a_units, &internalu.urho, &internalu.tbase1, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, + my_rates->k1, my_rates->k2, my_rates->k3, my_rates->k4, my_rates->k5, my_rates->k6, my_rates->k7, my_rates->k8, my_rates->k9, + my_rates->k10, my_rates->k11, my_rates->k12, my_rates->k13, my_rates->k13dd, my_rates->k14, my_rates->k15, my_rates->k16, + my_rates->k17, my_rates->k18, my_rates->k19, my_rates->k22, &my_uvb_rates.k24, &my_uvb_rates.k25, &my_uvb_rates.k26, &my_uvb_rates.k27, &my_uvb_rates.k28, + &my_uvb_rates.k29, &my_uvb_rates.k30, &my_uvb_rates.k31, my_rates->k50, my_rates->k51, my_rates->k52, my_rates->k53, my_rates->k54, + my_rates->k55, my_rates->k56, my_rates->k57, my_rates->k58, &my_chemistry->NumberOfDustTemperatureBins, &my_chemistry->DustTemperatureStart, + &my_chemistry->DustTemperatureEnd, my_rates->h2dust, my_rates->n_cr_n, my_rates->n_cr_d1, my_rates->n_cr_d2, my_rates->ceHI, + my_rates->ceHeI, my_rates->ceHeII, my_rates->ciHI, my_rates->ciHeI, my_rates->ciHeIS, my_rates->ciHeII, + my_rates->reHII, my_rates->reHeII1, my_rates->reHeII2, my_rates->reHeIII, my_rates->brem, + &my_rates->comp, &my_rates->gammah, &my_chemistry->interstellar_radiation_field, my_rates->regr, &my_rates->gamma_isrf, + &my_uvb_rates.comp_xray, &my_uvb_rates.temp_xray, &my_uvb_rates.piHI, &my_uvb_rates.piHeI, &my_uvb_rates.piHeII, my_fields->HM_density, + my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, my_rates->hyd01k, + my_rates->h2k01, my_rates->vibh, my_rates->roth, my_rates->rotl, my_rates->GP99LowDensityLimit, my_rates->GP99HighDensityLimit, + my_rates->HDlte, my_rates->HDlow, my_rates->GAHI, my_rates->GAH2, my_rates->GAHe, my_rates->GAHp, + my_rates->GAel, my_rates->H2LTE, my_rates->gas_grain, &my_chemistry->H2_self_shielding, &my_chemistry->self_shielding_method, + &my_uvb_rates.crsHI, &my_uvb_rates.crsHeI, &my_uvb_rates.crsHeII, &my_chemistry->use_radiative_transfer, + &my_chemistry->radiative_transfer_hydrogen_only, my_fields->RT_HI_ionization_rate, my_fields->RT_HeI_ionization_rate, my_fields->RT_HeII_ionization_rate, my_fields->RT_H2_dissociation_rate, + my_fields->RT_heating_rate, my_fields->H2_self_shielding_length, &my_chemistry->h2_optical_depth_approximation, &my_chemistry->cie_cooling, + &my_chemistry->three_body_rate, &my_chemistry->h2_cooling_rate, &my_chemistry->hd_cooling_rate, my_rates->cieco, &my_chemistry->cmb_temperature_floor, + &my_chemistry->UVbackground, &my_chemistry->cloudy_electron_fraction_factor, &my_rates->cloudy_primordial.grid_rank, my_rates->cloudy_primordial.grid_dimension, + my_rates->cloudy_primordial.grid_parameters[0], my_rates->cloudy_primordial.grid_parameters[1], my_rates->cloudy_primordial.grid_parameters[2], my_rates->cloudy_primordial.grid_parameters[3], my_rates->cloudy_primordial.grid_parameters[4], + &my_rates->cloudy_primordial.data_size, my_rates->cloudy_primordial.cooling_data, my_rates->cloudy_primordial.heating_data, my_rates->cloudy_primordial.mmw_data, + &my_rates->cloudy_metal.grid_rank, my_rates->cloudy_metal.grid_dimension, my_rates->cloudy_metal.grid_parameters[0], my_rates->cloudy_metal.grid_parameters[1], + my_rates->cloudy_metal.grid_parameters[2], my_rates->cloudy_metal.grid_parameters[3], my_rates->cloudy_metal.grid_parameters[4], &my_rates->cloudy_metal.data_size, + my_rates->cloudy_metal.cooling_data, my_rates->cloudy_metal.heating_data, &my_rates->cloudy_data_new, &my_chemistry->use_volumetric_heating_rate, &my_chemistry->use_specific_heating_rate, + my_fields->volumetric_heating_rate, my_fields->specific_heating_rate, &my_chemistry->use_temperature_floor, &my_chemistry->temperature_floor_scalar, my_fields->temperature_floor, + &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, &my_chemistry->use_primordial_continuum_opacity, &my_chemistry->tabulated_cooling_minimum_temperature, my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, my_fields->SiI_density, my_fields->SiOI_density, + my_fields->SiO2I_density, my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, + my_fields->O2II_density, my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, + my_fields->Fe3O4_dust_density, my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, my_fields->ref_org_dust_density, + my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, my_rates->k125, my_rates->k129, my_rates->k130, my_rates->k131, + my_rates->k132, my_rates->k133, my_rates->k134, my_rates->k135, my_rates->k136, my_rates->k137, my_rates->k148, + my_rates->k149, my_rates->k150, my_rates->k151, my_rates->k152, my_rates->k153, my_rates->kz15, my_rates->kz16, + my_rates->kz17, my_rates->kz18, my_rates->kz19, my_rates->kz20, my_rates->kz21, my_rates->kz22, my_rates->kz23, + my_rates->kz24, my_rates->kz25, my_rates->kz26, my_rates->kz27, my_rates->kz28, my_rates->kz29, my_rates->kz30, + my_rates->kz31, my_rates->kz32, my_rates->kz33, my_rates->kz34, my_rates->kz35, my_rates->kz36, my_rates->kz37, + my_rates->kz38, my_rates->kz39, my_rates->kz40, my_rates->kz41, my_rates->kz42, my_rates->kz43, my_rates->kz44, + my_rates->kz45, my_rates->kz46, my_rates->kz47, my_rates->kz48, my_rates->kz49, my_rates->kz50, my_rates->kz51, + my_rates->kz52, my_rates->kz53, my_rates->kz54, my_rates->cieY06, my_rates->LH2.props.dimension, &my_rates->LH2.props.data_size, + my_rates->LH2.props.parameters[0], my_rates->LH2.props.parameters[1], my_rates->LH2.props.parameters[2], &my_rates->LH2.props.parameter_spacing[0], &my_rates->LH2.props.parameter_spacing[1], &my_rates->LH2.props.parameter_spacing[2], + my_rates->LH2.data, my_rates->LHD.props.dimension, &my_rates->LHD.props.data_size, my_rates->LHD.props.parameters[0], my_rates->LHD.props.parameters[1], my_rates->LHD.props.parameters[2], + &my_rates->LHD.props.parameter_spacing[0], &my_rates->LHD.props.parameter_spacing[1], &my_rates->LHD.props.parameter_spacing[2], my_rates->LHD.data, my_rates->LCI.props.dimension, &my_rates->LCI.props.data_size, + my_rates->LCI.props.parameters[0], my_rates->LCI.props.parameters[1], my_rates->LCI.props.parameters[2], &my_rates->LCI.props.parameter_spacing[0], &my_rates->LCI.props.parameter_spacing[1], &my_rates->LCI.props.parameter_spacing[2], + my_rates->LCI.data, my_rates->LCII.props.dimension, &my_rates->LCII.props.data_size, my_rates->LCII.props.parameters[0], my_rates->LCII.props.parameters[1], my_rates->LCII.props.parameters[2], + &my_rates->LCII.props.parameter_spacing[0], &my_rates->LCII.props.parameter_spacing[1], &my_rates->LCII.props.parameter_spacing[2], my_rates->LCII.data, my_rates->LOI.props.dimension, + &my_rates->LOI.props.data_size, my_rates->LOI.props.parameters[0], my_rates->LOI.props.parameters[1], my_rates->LOI.props.parameters[2], &my_rates->LOI.props.parameter_spacing[0], &my_rates->LOI.props.parameter_spacing[1], + &my_rates->LOI.props.parameter_spacing[2], my_rates->LOI.data, my_rates->LCO.props.dimension, &my_rates->LCO.props.data_size, my_rates->LCO.props.parameters[0], my_rates->LCO.props.parameters[1], + my_rates->LCO.props.parameters[2], &my_rates->LCO.props.parameter_spacing[0], &my_rates->LCO.props.parameter_spacing[1], &my_rates->LCO.props.parameter_spacing[2], my_rates->LCO.data, my_rates->LOH.props.dimension, + &my_rates->LOH.props.data_size, my_rates->LOH.props.parameters[0], my_rates->LOH.props.parameters[1], my_rates->LOH.props.parameters[2], &my_rates->LOH.props.parameter_spacing[0], &my_rates->LOH.props.parameter_spacing[1], + &my_rates->LOH.props.parameter_spacing[2], my_rates->LOH.data, my_rates->LH2O.props.dimension, &my_rates->LH2O.props.data_size, my_rates->LH2O.props.parameters[0], my_rates->LH2O.props.parameters[1], + my_rates->LH2O.props.parameters[2], &my_rates->LH2O.props.parameter_spacing[0], &my_rates->LH2O.props.parameter_spacing[1], &my_rates->LH2O.props.parameter_spacing[2], my_rates->LH2O.data, + my_rates->alphap.props.dimension, &my_rates->alphap.props.data_size, my_rates->alphap.props.parameters[0], my_rates->alphap.props.parameters[1], + &my_rates->alphap.props.parameter_spacing[0], &my_rates->alphap.props.parameter_spacing[1], my_rates->alphap.data, &my_chemistry->multi_metals, + &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, my_fields->local_ISM_metal_density, + my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, + my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, + my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density, &my_rates->SN0_N, + my_rates->SN0_fSiM, my_rates->SN0_fFeM, my_rates->SN0_fMg2SiO4, my_rates->SN0_fMgSiO3, + my_rates->SN0_fFe3O4, my_rates->SN0_fAC, my_rates->SN0_fSiO2D, my_rates->SN0_fMgO, + my_rates->SN0_fFeS, my_rates->SN0_fAl2O3, my_rates->SN0_freforg, my_rates->SN0_fvolorg, + my_rates->SN0_fH2Oice, my_rates->SN0_r0SiM, my_rates->SN0_r0FeM, my_rates->SN0_r0Mg2SiO4, + my_rates->SN0_r0MgSiO3, my_rates->SN0_r0Fe3O4, my_rates->SN0_r0AC, my_rates->SN0_r0SiO2D, + my_rates->SN0_r0MgO, my_rates->SN0_r0FeS, my_rates->SN0_r0Al2O3, my_rates->SN0_r0reforg, + my_rates->SN0_r0volorg, my_rates->SN0_r0H2Oice, my_rates->gr_N, &my_rates->gr_Size, &my_rates->gr_dT, + my_rates->gr_Td, my_rates->SN0_kpSiM, my_rates->SN0_kpFeM, my_rates->SN0_kpMg2SiO4, + my_rates->SN0_kpMgSiO3, my_rates->SN0_kpFe3O4, my_rates->SN0_kpAC, my_rates->SN0_kpSiO2D, + my_rates->SN0_kpMgO, my_rates->SN0_kpFeS, my_rates->SN0_kpAl2O3, my_rates->SN0_kpreforg, + my_rates->SN0_kpvolorg, my_rates->SN0_kpH2Oice, my_rates->h2dustS, my_rates->h2dustC, + my_rates->gas_grain2, &my_rates->gamma_isrf2, my_rates->grain_growth_rate, &my_chemistry->radiative_transfer_HDI_dissociation, + my_fields->RT_HDI_dissociation_rate, &my_chemistry->radiative_transfer_metal_ionization, my_fields->RT_CI_ionization_rate, my_fields->RT_OI_ionization_rate, &my_chemistry->radiative_transfer_metal_dissociation, my_fields->RT_CO_dissociation_rate, + my_fields->RT_OH_dissociation_rate, my_fields->RT_H2O_dissociation_rate, &my_chemistry->radiative_transfer_use_H2_shielding, &my_chemistry->use_isrf_field, + my_fields->isrf_habing, &my_chemistry->H2_custom_shielding, my_fields->H2_custom_shielding_factor, + &j, &k, &iter, &dom, comp1, + comp2, &internalu.coolunit, &internalu.tbase1, &internalu.xbase1, &chunit, &dx_cgs, + &c_ljeans, logTlininterp_buf.indixe, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.logtem, logTlininterp_buf.tdef, dtit, + p2d, tgas, cool1dmulti_buf.tgasold, tdust, metallicity, dust2gas, + rhoH, mmw, cool1dmulti_buf.mynh, cool1dmulti_buf.myde, cool1dmulti_buf.gammaha_eff, cool1dmulti_buf.gasgr_tdust, + cool1dmulti_buf.regr, h2dust, chemheatrates_buf.n_cr_n, chemheatrates_buf.n_cr_d1, chemheatrates_buf.n_cr_d2, grain_temperatures.SiM, grain_temperatures.FeM, + grain_temperatures.Mg2SiO4, grain_temperatures.MgSiO3, grain_temperatures.Fe3O4, grain_temperatures.AC, grain_temperatures.SiO2D, grain_temperatures.MgO, + grain_temperatures.FeS, grain_temperatures.Al2O3, grain_temperatures.reforg, grain_temperatures.volorg, grain_temperatures.H2Oice, coolingheating_buf.ceHI, + coolingheating_buf.ceHeI, coolingheating_buf.ceHeII, coolingheating_buf.ciHI, coolingheating_buf.ciHeI, coolingheating_buf.ciHeIS, coolingheating_buf.ciHeII, + coolingheating_buf.reHII, coolingheating_buf.reHeII1, coolingheating_buf.reHeII2, coolingheating_buf.reHeIII, coolingheating_buf.brem, edot, + coolingheating_buf.hyd01k, coolingheating_buf.h2k01, coolingheating_buf.vibh, coolingheating_buf.roth, coolingheating_buf.rotl, coolingheating_buf.gpldl, coolingheating_buf.gphdl, + coolingheating_buf.hdlte, coolingheating_buf.hdlow, coolingheating_buf.cieco, &anydust, itmask_nr, + itmask_metal, imp_eng + ); + + + + +} + + #endif /* FORTRAN_FUNC_WRAPPERS_HPP */ diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index a3b4d7fdb..4a4ad6830 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -675,98 +675,16 @@ int solve_rate_cool_g( // sweep of a backward Euler method (for all cells specified by // itmask_nr) - FORTRAN_NAME(step_rate_newton_raphson)(&my_chemistry->with_radiative_cooling, d.data(), e.data(), my_fields->x_velocity, my_fields->y_velocity, my_fields->z_velocity, de.data(), HI.data(), - HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, - &internalu.extfields_in_comoving, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, - &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_fields->grid_start[0], &my_fields->grid_end[0], &my_chemistry->ih2co, &my_chemistry->ipiht, - &my_chemistry->dust_recombination_cooling, &my_chemistry->photoelectric_heating, &internalu.a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &utem, - &internalu.uxyz, &internalu.a_units, &internalu.urho, &internalu.tbase1, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, - my_rates->k1, my_rates->k2, my_rates->k3, my_rates->k4, my_rates->k5, my_rates->k6, my_rates->k7, my_rates->k8, my_rates->k9, - my_rates->k10, my_rates->k11, my_rates->k12, my_rates->k13, my_rates->k13dd, my_rates->k14, my_rates->k15, my_rates->k16, - my_rates->k17, my_rates->k18, my_rates->k19, my_rates->k22, &my_uvb_rates->k24, &my_uvb_rates->k25, &my_uvb_rates->k26, &my_uvb_rates->k27, &my_uvb_rates->k28, - &my_uvb_rates->k29, &my_uvb_rates->k30, &my_uvb_rates->k31, my_rates->k50, my_rates->k51, my_rates->k52, my_rates->k53, my_rates->k54, - my_rates->k55, my_rates->k56, my_rates->k57, my_rates->k58, &my_chemistry->NumberOfDustTemperatureBins, &my_chemistry->DustTemperatureStart, - &my_chemistry->DustTemperatureEnd, my_rates->h2dust, my_rates->n_cr_n, my_rates->n_cr_d1, my_rates->n_cr_d2, my_rates->ceHI, - my_rates->ceHeI, my_rates->ceHeII, my_rates->ciHI, my_rates->ciHeI, my_rates->ciHeIS, my_rates->ciHeII, - my_rates->reHII, my_rates->reHeII1, my_rates->reHeII2, my_rates->reHeIII, my_rates->brem, - &my_rates->comp, &my_rates->gammah, &my_chemistry->interstellar_radiation_field, my_rates->regr, &my_rates->gamma_isrf, - &my_uvb_rates->comp_xray, &my_uvb_rates->temp_xray, &my_uvb_rates->piHI, &my_uvb_rates->piHeI, &my_uvb_rates->piHeII, my_fields->HM_density, - my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, my_rates->hyd01k, - my_rates->h2k01, my_rates->vibh, my_rates->roth, my_rates->rotl, my_rates->GP99LowDensityLimit, my_rates->GP99HighDensityLimit, - my_rates->HDlte, my_rates->HDlow, my_rates->GAHI, my_rates->GAH2, my_rates->GAHe, my_rates->GAHp, - my_rates->GAel, my_rates->H2LTE, my_rates->gas_grain, &my_chemistry->H2_self_shielding, &my_chemistry->self_shielding_method, - &my_uvb_rates->crsHI, &my_uvb_rates->crsHeI, &my_uvb_rates->crsHeII, &my_chemistry->use_radiative_transfer, - &my_chemistry->radiative_transfer_hydrogen_only, kphHI.data(), my_fields->RT_HeI_ionization_rate, my_fields->RT_HeII_ionization_rate, my_fields->RT_H2_dissociation_rate, - my_fields->RT_heating_rate, my_fields->H2_self_shielding_length, &my_chemistry->h2_optical_depth_approximation, &my_chemistry->cie_cooling, - &my_chemistry->three_body_rate, &my_chemistry->h2_cooling_rate, &my_chemistry->hd_cooling_rate, my_rates->cieco, &my_chemistry->cmb_temperature_floor, - &my_chemistry->UVbackground, &my_chemistry->cloudy_electron_fraction_factor, &my_rates->cloudy_primordial.grid_rank, my_rates->cloudy_primordial.grid_dimension, - my_rates->cloudy_primordial.grid_parameters[0], my_rates->cloudy_primordial.grid_parameters[1], my_rates->cloudy_primordial.grid_parameters[2], my_rates->cloudy_primordial.grid_parameters[3], my_rates->cloudy_primordial.grid_parameters[4], - &my_rates->cloudy_primordial.data_size, my_rates->cloudy_primordial.cooling_data, my_rates->cloudy_primordial.heating_data, my_rates->cloudy_primordial.mmw_data, - &my_rates->cloudy_metal.grid_rank, my_rates->cloudy_metal.grid_dimension, my_rates->cloudy_metal.grid_parameters[0], my_rates->cloudy_metal.grid_parameters[1], - my_rates->cloudy_metal.grid_parameters[2], my_rates->cloudy_metal.grid_parameters[3], my_rates->cloudy_metal.grid_parameters[4], &my_rates->cloudy_metal.data_size, - my_rates->cloudy_metal.cooling_data, my_rates->cloudy_metal.heating_data, &my_rates->cloudy_data_new, &my_chemistry->use_volumetric_heating_rate, &my_chemistry->use_specific_heating_rate, - my_fields->volumetric_heating_rate, my_fields->specific_heating_rate, &my_chemistry->use_temperature_floor, &my_chemistry->temperature_floor_scalar, my_fields->temperature_floor, - &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, &my_chemistry->use_primordial_continuum_opacity, &my_chemistry->tabulated_cooling_minimum_temperature, my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, - my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, my_fields->SiI_density, my_fields->SiOI_density, - my_fields->SiO2I_density, my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, - my_fields->O2II_density, my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, - my_fields->Fe3O4_dust_density, my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, my_fields->ref_org_dust_density, - my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, my_rates->k125, my_rates->k129, my_rates->k130, my_rates->k131, - my_rates->k132, my_rates->k133, my_rates->k134, my_rates->k135, my_rates->k136, my_rates->k137, my_rates->k148, - my_rates->k149, my_rates->k150, my_rates->k151, my_rates->k152, my_rates->k153, my_rates->kz15, my_rates->kz16, - my_rates->kz17, my_rates->kz18, my_rates->kz19, my_rates->kz20, my_rates->kz21, my_rates->kz22, my_rates->kz23, - my_rates->kz24, my_rates->kz25, my_rates->kz26, my_rates->kz27, my_rates->kz28, my_rates->kz29, my_rates->kz30, - my_rates->kz31, my_rates->kz32, my_rates->kz33, my_rates->kz34, my_rates->kz35, my_rates->kz36, my_rates->kz37, - my_rates->kz38, my_rates->kz39, my_rates->kz40, my_rates->kz41, my_rates->kz42, my_rates->kz43, my_rates->kz44, - my_rates->kz45, my_rates->kz46, my_rates->kz47, my_rates->kz48, my_rates->kz49, my_rates->kz50, my_rates->kz51, - my_rates->kz52, my_rates->kz53, my_rates->kz54, my_rates->cieY06, my_rates->LH2.props.dimension, &my_rates->LH2.props.data_size, - my_rates->LH2.props.parameters[0], my_rates->LH2.props.parameters[1], my_rates->LH2.props.parameters[2], &my_rates->LH2.props.parameter_spacing[0], &my_rates->LH2.props.parameter_spacing[1], &my_rates->LH2.props.parameter_spacing[2], - my_rates->LH2.data, my_rates->LHD.props.dimension, &my_rates->LHD.props.data_size, my_rates->LHD.props.parameters[0], my_rates->LHD.props.parameters[1], my_rates->LHD.props.parameters[2], - &my_rates->LHD.props.parameter_spacing[0], &my_rates->LHD.props.parameter_spacing[1], &my_rates->LHD.props.parameter_spacing[2], my_rates->LHD.data, my_rates->LCI.props.dimension, &my_rates->LCI.props.data_size, - my_rates->LCI.props.parameters[0], my_rates->LCI.props.parameters[1], my_rates->LCI.props.parameters[2], &my_rates->LCI.props.parameter_spacing[0], &my_rates->LCI.props.parameter_spacing[1], &my_rates->LCI.props.parameter_spacing[2], - my_rates->LCI.data, my_rates->LCII.props.dimension, &my_rates->LCII.props.data_size, my_rates->LCII.props.parameters[0], my_rates->LCII.props.parameters[1], my_rates->LCII.props.parameters[2], - &my_rates->LCII.props.parameter_spacing[0], &my_rates->LCII.props.parameter_spacing[1], &my_rates->LCII.props.parameter_spacing[2], my_rates->LCII.data, my_rates->LOI.props.dimension, - &my_rates->LOI.props.data_size, my_rates->LOI.props.parameters[0], my_rates->LOI.props.parameters[1], my_rates->LOI.props.parameters[2], &my_rates->LOI.props.parameter_spacing[0], &my_rates->LOI.props.parameter_spacing[1], - &my_rates->LOI.props.parameter_spacing[2], my_rates->LOI.data, my_rates->LCO.props.dimension, &my_rates->LCO.props.data_size, my_rates->LCO.props.parameters[0], my_rates->LCO.props.parameters[1], - my_rates->LCO.props.parameters[2], &my_rates->LCO.props.parameter_spacing[0], &my_rates->LCO.props.parameter_spacing[1], &my_rates->LCO.props.parameter_spacing[2], my_rates->LCO.data, my_rates->LOH.props.dimension, - &my_rates->LOH.props.data_size, my_rates->LOH.props.parameters[0], my_rates->LOH.props.parameters[1], my_rates->LOH.props.parameters[2], &my_rates->LOH.props.parameter_spacing[0], &my_rates->LOH.props.parameter_spacing[1], - &my_rates->LOH.props.parameter_spacing[2], my_rates->LOH.data, my_rates->LH2O.props.dimension, &my_rates->LH2O.props.data_size, my_rates->LH2O.props.parameters[0], my_rates->LH2O.props.parameters[1], - my_rates->LH2O.props.parameters[2], &my_rates->LH2O.props.parameter_spacing[0], &my_rates->LH2O.props.parameter_spacing[1], &my_rates->LH2O.props.parameter_spacing[2], my_rates->LH2O.data, - my_rates->alphap.props.dimension, &my_rates->alphap.props.data_size, my_rates->alphap.props.parameters[0], my_rates->alphap.props.parameters[1], - &my_rates->alphap.props.parameter_spacing[0], &my_rates->alphap.props.parameter_spacing[1], my_rates->alphap.data, &my_chemistry->multi_metals, - &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, my_fields->local_ISM_metal_density, - my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, - my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, - my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density, &my_rates->SN0_N, - my_rates->SN0_fSiM, my_rates->SN0_fFeM, my_rates->SN0_fMg2SiO4, my_rates->SN0_fMgSiO3, - my_rates->SN0_fFe3O4, my_rates->SN0_fAC, my_rates->SN0_fSiO2D, my_rates->SN0_fMgO, - my_rates->SN0_fFeS, my_rates->SN0_fAl2O3, my_rates->SN0_freforg, my_rates->SN0_fvolorg, - my_rates->SN0_fH2Oice, my_rates->SN0_r0SiM, my_rates->SN0_r0FeM, my_rates->SN0_r0Mg2SiO4, - my_rates->SN0_r0MgSiO3, my_rates->SN0_r0Fe3O4, my_rates->SN0_r0AC, my_rates->SN0_r0SiO2D, - my_rates->SN0_r0MgO, my_rates->SN0_r0FeS, my_rates->SN0_r0Al2O3, my_rates->SN0_r0reforg, - my_rates->SN0_r0volorg, my_rates->SN0_r0H2Oice, my_rates->gr_N, &my_rates->gr_Size, &my_rates->gr_dT, - my_rates->gr_Td, my_rates->SN0_kpSiM, my_rates->SN0_kpFeM, my_rates->SN0_kpMg2SiO4, - my_rates->SN0_kpMgSiO3, my_rates->SN0_kpFe3O4, my_rates->SN0_kpAC, my_rates->SN0_kpSiO2D, - my_rates->SN0_kpMgO, my_rates->SN0_kpFeS, my_rates->SN0_kpAl2O3, my_rates->SN0_kpreforg, - my_rates->SN0_kpvolorg, my_rates->SN0_kpH2Oice, my_rates->h2dustS, my_rates->h2dustC, - my_rates->gas_grain2, &my_rates->gamma_isrf2, my_rates->grain_growth_rate, &my_chemistry->radiative_transfer_HDI_dissociation, - my_fields->RT_HDI_dissociation_rate, &my_chemistry->radiative_transfer_metal_ionization, my_fields->RT_CI_ionization_rate, my_fields->RT_OI_ionization_rate, &my_chemistry->radiative_transfer_metal_dissociation, my_fields->RT_CO_dissociation_rate, - my_fields->RT_OH_dissociation_rate, my_fields->RT_H2O_dissociation_rate, &my_chemistry->radiative_transfer_use_H2_shielding, &my_chemistry->use_isrf_field, - my_fields->isrf_habing, &my_chemistry->H2_custom_shielding, my_fields->H2_custom_shielding_factor, - &j, &k, &iter, &dom, &comp1, - &comp2, &internalu.coolunit, &internalu.tbase1, &internalu.xbase1, &chunit, &dx_cgs, - &c_ljeans, logTlininterp_buf.indixe, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.logtem, logTlininterp_buf.tdef, dtit.data(), - p2d.data(), tgas.data(), cool1dmulti_buf.tgasold, tdust.data(), metallicity.data(), dust2gas.data(), - rhoH.data(), mmw.data(), cool1dmulti_buf.mynh, cool1dmulti_buf.myde, cool1dmulti_buf.gammaha_eff, cool1dmulti_buf.gasgr_tdust, - cool1dmulti_buf.regr, h2dust.data(), chemheatrates_buf.n_cr_n, chemheatrates_buf.n_cr_d1, chemheatrates_buf.n_cr_d2, grain_temperatures.SiM, grain_temperatures.FeM, - grain_temperatures.Mg2SiO4, grain_temperatures.MgSiO3, grain_temperatures.Fe3O4, grain_temperatures.AC, grain_temperatures.SiO2D, grain_temperatures.MgO, - grain_temperatures.FeS, grain_temperatures.Al2O3, grain_temperatures.reforg, grain_temperatures.volorg, grain_temperatures.H2Oice, coolingheating_buf.ceHI, - coolingheating_buf.ceHeI, coolingheating_buf.ceHeII, coolingheating_buf.ciHI, coolingheating_buf.ciHeI, coolingheating_buf.ciHeIS, coolingheating_buf.ciHeII, - coolingheating_buf.reHII, coolingheating_buf.reHeII1, coolingheating_buf.reHeII2, coolingheating_buf.reHeIII, coolingheating_buf.brem, edot.data(), - coolingheating_buf.hyd01k, coolingheating_buf.h2k01, coolingheating_buf.vibh, coolingheating_buf.roth, coolingheating_buf.rotl, coolingheating_buf.gpldl, coolingheating_buf.gphdl, - coolingheating_buf.hdlte, coolingheating_buf.hdlow, coolingheating_buf.cieco, &anydust, itmask_nr.data(), - itmask_metal.data(), imp_eng.data() - ); + wrapped_step_rate_newton_raphson_( + imetal, utem, j, k, iter, dom, &comp1, &comp2, chunit, dx_cgs, + c_ljeans, dtit.data(), p2d.data(), tgas.data(), tdust.data(), + metallicity.data(), dust2gas.data(), rhoH.data(), mmw.data(), + h2dust.data(), edot.data(), anydust, itmask_nr.data(), + itmask_metal.data(), imp_eng.data(), my_chemistry, my_rates, + my_fields, *my_uvb_rates, internalu, grain_temperatures, + logTlininterp_buf, cool1dmulti_buf, coolingheating_buf, + chemheatrates_buf + ); } From 11fc93c13aaa139105c3aa8afd9fb0f74e648b00 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 19 Jan 2025 12:00:16 -0500 Subject: [PATCH 056/101] introduce C++ wrapper for `cool1d_multi_g` subroutine. --- src/clib/fortran_func_wrappers.hpp | 119 +++++++++++++++++++++++++++++ src/clib/solve_rate_cool_g-cpp.C | 113 ++------------------------- 2 files changed, 127 insertions(+), 105 deletions(-) diff --git a/src/clib/fortran_func_wrappers.hpp b/src/clib/fortran_func_wrappers.hpp index 29661bb4b..914848cca 100644 --- a/src/clib/fortran_func_wrappers.hpp +++ b/src/clib/fortran_func_wrappers.hpp @@ -55,6 +55,125 @@ inline void wrapped_ceiling_species_g_( } +inline void wrapped_cool1d_multi_g_( + int imetal, int j, int k, int iter, double utem, double* comp1, double* comp2, + double* edot, double* tgas, double* mmw, double* p2d, double* tdust, + double* metallicity, double* dust2gas, double* rhoH, gr_mask_type* itmask, + gr_mask_type* itmask_metal, chemistry_data* my_chemistry, + chemistry_data_storage* my_rates, grackle_field_data* my_fields, + photo_rate_storage my_uvb_rates, InternalGrUnits internalu, + grackle::impl::GrainSpeciesCollection grain_temperatures, + grackle::impl::LogTLinInterpScratchBuf logTlininterp_buf, + grackle::impl::Cool1DMultiScratchBuf cool1dmulti_buf, + grackle::impl::CoolHeatScratchBuf coolingheating_buf +) { + FORTRAN_NAME(cool1d_multi_g)( + my_fields->density, my_fields->internal_energy, my_fields->x_velocity, my_fields->y_velocity, my_fields->z_velocity, my_fields->e_density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, + &internalu.extfields_in_comoving, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, + &my_chemistry->h2_on_dust, &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_chemistry->dust_recombination_cooling, + &my_fields->grid_rank, &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, &my_chemistry->ih2co, &my_chemistry->ipiht, &iter, &my_chemistry->photoelectric_heating, + &internalu.a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, + &utem, &internalu.uxyz, &internalu.a_units, &internalu.urho, &internalu.tbase1, + &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, + my_rates->ceHI, my_rates->ceHeI, my_rates->ceHeII, my_rates->ciHI, my_rates->ciHeI, + my_rates->ciHeIS, my_rates->ciHeII, my_rates->reHII, my_rates->reHeII1, + my_rates->reHeII2, my_rates->reHeIII, my_rates->brem, &my_rates->comp, &my_rates->gammah, + &my_chemistry->interstellar_radiation_field, my_rates->regr, &my_rates->gamma_isrf, &my_uvb_rates.comp_xray, &my_uvb_rates.temp_xray, + &my_uvb_rates.piHI, &my_uvb_rates.piHeI, &my_uvb_rates.piHeII, comp1, comp2, + my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, + my_rates->hyd01k, my_rates->h2k01, my_rates->vibh, my_rates->roth, my_rates->rotl, + coolingheating_buf.hyd01k, coolingheating_buf.h2k01, coolingheating_buf.vibh, coolingheating_buf.roth, coolingheating_buf.rotl, + my_rates->GP99LowDensityLimit, my_rates->GP99HighDensityLimit, coolingheating_buf.gpldl, coolingheating_buf.gphdl, + my_rates->HDlte, my_rates->HDlow, coolingheating_buf.hdlte, coolingheating_buf.hdlow, + my_rates->GAHI, my_rates->GAH2, my_rates->GAHe, my_rates->GAHp, my_rates->GAel, + my_rates->H2LTE, my_rates->gas_grain, + coolingheating_buf.ceHI, coolingheating_buf.ceHeI, coolingheating_buf.ceHeII, coolingheating_buf.ciHI, coolingheating_buf.ciHeI, coolingheating_buf.ciHeIS, coolingheating_buf.ciHeII, + coolingheating_buf.reHII, coolingheating_buf.reHeII1, coolingheating_buf.reHeII2, coolingheating_buf.reHeIII, coolingheating_buf.brem, + logTlininterp_buf.indixe, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.logtem, logTlininterp_buf.tdef, edot, + tgas, cool1dmulti_buf.tgasold, mmw, p2d, tdust, metallicity, + dust2gas, rhoH, cool1dmulti_buf.mynh, cool1dmulti_buf.myde, + cool1dmulti_buf.gammaha_eff, cool1dmulti_buf.gasgr_tdust, cool1dmulti_buf.regr, + &my_chemistry->self_shielding_method, &my_uvb_rates.crsHI, &my_uvb_rates.crsHeI, &my_uvb_rates.crsHeII, + &my_uvb_rates.k24, &my_uvb_rates.k26, + &my_chemistry->use_radiative_transfer, my_fields->RT_heating_rate, + &my_chemistry->h2_optical_depth_approximation, &my_chemistry->cie_cooling, &my_chemistry->h2_cooling_rate, &my_chemistry->hd_cooling_rate, my_rates->cieco, coolingheating_buf.cieco, + &my_chemistry->cmb_temperature_floor, &my_chemistry->UVbackground, &my_chemistry->cloudy_electron_fraction_factor, + &my_rates->cloudy_primordial.grid_rank, my_rates->cloudy_primordial.grid_dimension, + my_rates->cloudy_primordial.grid_parameters[0], my_rates->cloudy_primordial.grid_parameters[1], my_rates->cloudy_primordial.grid_parameters[2], my_rates->cloudy_primordial.grid_parameters[3], my_rates->cloudy_primordial.grid_parameters[4], + &my_rates->cloudy_primordial.data_size, my_rates->cloudy_primordial.cooling_data, my_rates->cloudy_primordial.heating_data, my_rates->cloudy_primordial.mmw_data, + &my_rates->cloudy_metal.grid_rank, my_rates->cloudy_metal.grid_dimension, + my_rates->cloudy_metal.grid_parameters[0], my_rates->cloudy_metal.grid_parameters[1], my_rates->cloudy_metal.grid_parameters[2], my_rates->cloudy_metal.grid_parameters[3], my_rates->cloudy_metal.grid_parameters[4], + &my_rates->cloudy_metal.data_size, my_rates->cloudy_metal.cooling_data, my_rates->cloudy_metal.heating_data, &my_rates->cloudy_data_new, + &my_chemistry->use_volumetric_heating_rate, &my_chemistry->use_specific_heating_rate, my_fields->volumetric_heating_rate, my_fields->specific_heating_rate, + &my_chemistry->use_temperature_floor, &my_chemistry->temperature_floor_scalar, my_fields->temperature_floor, + &my_chemistry->use_isrf_field, my_fields->isrf_habing, itmask, itmask_metal, + &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, &my_chemistry->use_primordial_continuum_opacity, &my_chemistry->tabulated_cooling_minimum_temperature, + my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, + my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, + my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, + my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, + my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, + my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, + my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, + my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, + my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, + my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, + my_rates->cieY06, + my_rates->LH2.props.dimension, &my_rates->LH2.props.data_size, + my_rates->LH2.props.parameters[0], my_rates->LH2.props.parameters[1], my_rates->LH2.props.parameters[2], + &my_rates->LH2.props.parameter_spacing[0], &my_rates->LH2.props.parameter_spacing[1], &my_rates->LH2.props.parameter_spacing[2], my_rates->LH2.data, + my_rates->LHD.props.dimension, &my_rates->LHD.props.data_size, + my_rates->LHD.props.parameters[0], my_rates->LHD.props.parameters[1], my_rates->LHD.props.parameters[2], + &my_rates->LHD.props.parameter_spacing[0], &my_rates->LHD.props.parameter_spacing[1], &my_rates->LHD.props.parameter_spacing[2], my_rates->LHD.data, + my_rates->LCI.props.dimension, &my_rates->LCI.props.data_size, + my_rates->LCI.props.parameters[0], my_rates->LCI.props.parameters[1], my_rates->LCI.props.parameters[2], + &my_rates->LCI.props.parameter_spacing[0], &my_rates->LCI.props.parameter_spacing[1], &my_rates->LCI.props.parameter_spacing[2], my_rates->LCI.data, + my_rates->LCII.props.dimension, &my_rates->LCII.props.data_size, + my_rates->LCII.props.parameters[0], my_rates->LCII.props.parameters[1], my_rates->LCII.props.parameters[2], + &my_rates->LCII.props.parameter_spacing[0], &my_rates->LCII.props.parameter_spacing[1], &my_rates->LCII.props.parameter_spacing[2], my_rates->LCII.data, + my_rates->LOI.props.dimension, &my_rates->LOI.props.data_size, + my_rates->LOI.props.parameters[0], my_rates->LOI.props.parameters[1], my_rates->LOI.props.parameters[2], + &my_rates->LOI.props.parameter_spacing[0], &my_rates->LOI.props.parameter_spacing[1], &my_rates->LOI.props.parameter_spacing[2], my_rates->LOI.data, + my_rates->LCO.props.dimension, &my_rates->LCO.props.data_size, + my_rates->LCO.props.parameters[0], my_rates->LCO.props.parameters[1], my_rates->LCO.props.parameters[2], + &my_rates->LCO.props.parameter_spacing[0], &my_rates->LCO.props.parameter_spacing[1], &my_rates->LCO.props.parameter_spacing[2], my_rates->LCO.data, + my_rates->LOH.props.dimension, &my_rates->LOH.props.data_size, + my_rates->LOH.props.parameters[0], my_rates->LOH.props.parameters[1], my_rates->LOH.props.parameters[2], + &my_rates->LOH.props.parameter_spacing[0], &my_rates->LOH.props.parameter_spacing[1], &my_rates->LOH.props.parameter_spacing[2], my_rates->LOH.data, + my_rates->LH2O.props.dimension, &my_rates->LH2O.props.data_size, + my_rates->LH2O.props.parameters[0], my_rates->LH2O.props.parameters[1], my_rates->LH2O.props.parameters[2], + &my_rates->LH2O.props.parameter_spacing[0], &my_rates->LH2O.props.parameter_spacing[1], &my_rates->LH2O.props.parameter_spacing[2], my_rates->LH2O.data, + my_rates->alphap.props.dimension, &my_rates->alphap.props.data_size, + my_rates->alphap.props.parameters[0], my_rates->alphap.props.parameters[1], &my_rates->alphap.props.parameter_spacing[0], &my_rates->alphap.props.parameter_spacing[1], + my_rates->alphap.data, + &my_chemistry->multi_metals, &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, + my_fields->local_ISM_metal_density, + my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, + my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, + my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density, + &my_rates->SN0_N, + my_rates->SN0_fSiM, my_rates->SN0_fFeM, my_rates->SN0_fMg2SiO4, my_rates->SN0_fMgSiO3, + my_rates->SN0_fFe3O4, my_rates->SN0_fAC, my_rates->SN0_fSiO2D, my_rates->SN0_fMgO, + my_rates->SN0_fFeS, my_rates->SN0_fAl2O3, + my_rates->SN0_freforg, my_rates->SN0_fvolorg, my_rates->SN0_fH2Oice, + my_rates->SN0_r0SiM, my_rates->SN0_r0FeM, my_rates->SN0_r0Mg2SiO4, my_rates->SN0_r0MgSiO3, + my_rates->SN0_r0Fe3O4, my_rates->SN0_r0AC, my_rates->SN0_r0SiO2D, my_rates->SN0_r0MgO, + my_rates->SN0_r0FeS, my_rates->SN0_r0Al2O3, + my_rates->SN0_r0reforg, my_rates->SN0_r0volorg, my_rates->SN0_r0H2Oice, + my_rates->gr_N, &my_rates->gr_Size, &my_rates->gr_dT, my_rates->gr_Td, + my_rates->SN0_kpSiM, my_rates->SN0_kpFeM, my_rates->SN0_kpMg2SiO4, my_rates->SN0_kpMgSiO3, + my_rates->SN0_kpFe3O4, my_rates->SN0_kpAC, my_rates->SN0_kpSiO2D, my_rates->SN0_kpMgO, + my_rates->SN0_kpFeS, my_rates->SN0_kpAl2O3, + my_rates->SN0_kpreforg, my_rates->SN0_kpvolorg, my_rates->SN0_kpH2Oice, + grain_temperatures.SiM, grain_temperatures.FeM, grain_temperatures.Mg2SiO4, grain_temperatures.MgSiO3, grain_temperatures.Fe3O4, + grain_temperatures.AC, grain_temperatures.SiO2D, grain_temperatures.MgO, grain_temperatures.FeS, grain_temperatures.Al2O3, + grain_temperatures.reforg, grain_temperatures.volorg, grain_temperatures.H2Oice, + my_rates->gas_grain2, &my_rates->gamma_isrf2 + ); + +} + /// This routine uses the temperature to look up the chemical rates that are /// tabulated in a log table as a function of temperature inline void wrapped_lookup_cool_rates1d_g_( diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 4a4ad6830..2418b1c59 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -10,7 +10,6 @@ #include #include "grackle.h" -#include "fortran_func_decls.h" #include "fortran_func_wrappers.hpp" #include "internal_types.hpp" #include "internal_units.h" @@ -356,110 +355,14 @@ int solve_rate_cool_g( // Compute the cooling rate, tgas, tdust, and metallicity for this row - FORTRAN_NAME(cool1d_multi_g)( - d.data(), e.data(), my_fields->x_velocity, my_fields->y_velocity, my_fields->z_velocity, de.data(), HI.data(), HII.data(), my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, - &internalu.extfields_in_comoving, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, - &my_chemistry->h2_on_dust, &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_chemistry->dust_recombination_cooling, - &my_fields->grid_rank, &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, &my_chemistry->ih2co, &my_chemistry->ipiht, &iter, &my_chemistry->photoelectric_heating, - &internalu.a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, - &utem, &internalu.uxyz, &internalu.a_units, &internalu.urho, &internalu.tbase1, - &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, - my_rates->ceHI, my_rates->ceHeI, my_rates->ceHeII, my_rates->ciHI, my_rates->ciHeI, - my_rates->ciHeIS, my_rates->ciHeII, my_rates->reHII, my_rates->reHeII1, - my_rates->reHeII2, my_rates->reHeIII, my_rates->brem, &my_rates->comp, &my_rates->gammah, - &my_chemistry->interstellar_radiation_field, my_rates->regr, &my_rates->gamma_isrf, &my_uvb_rates->comp_xray, &my_uvb_rates->temp_xray, - &my_uvb_rates->piHI, &my_uvb_rates->piHeI, &my_uvb_rates->piHeII, &comp1, &comp2, - my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, - my_rates->hyd01k, my_rates->h2k01, my_rates->vibh, my_rates->roth, my_rates->rotl, - coolingheating_buf.hyd01k, coolingheating_buf.h2k01, coolingheating_buf.vibh, coolingheating_buf.roth, coolingheating_buf.rotl, - my_rates->GP99LowDensityLimit, my_rates->GP99HighDensityLimit, coolingheating_buf.gpldl, coolingheating_buf.gphdl, - my_rates->HDlte, my_rates->HDlow, coolingheating_buf.hdlte, coolingheating_buf.hdlow, - my_rates->GAHI, my_rates->GAH2, my_rates->GAHe, my_rates->GAHp, my_rates->GAel, - my_rates->H2LTE, my_rates->gas_grain, - coolingheating_buf.ceHI, coolingheating_buf.ceHeI, coolingheating_buf.ceHeII, coolingheating_buf.ciHI, coolingheating_buf.ciHeI, coolingheating_buf.ciHeIS, coolingheating_buf.ciHeII, - coolingheating_buf.reHII, coolingheating_buf.reHeII1, coolingheating_buf.reHeII2, coolingheating_buf.reHeIII, coolingheating_buf.brem, - logTlininterp_buf.indixe, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.logtem, logTlininterp_buf.tdef, edot.data(), - tgas.data(), cool1dmulti_buf.tgasold, mmw.data(), p2d.data(), tdust.data(), metallicity.data(), - dust2gas.data(), rhoH.data(), cool1dmulti_buf.mynh, cool1dmulti_buf.myde, - cool1dmulti_buf.gammaha_eff, cool1dmulti_buf.gasgr_tdust, cool1dmulti_buf.regr, - &my_chemistry->self_shielding_method, &my_uvb_rates->crsHI, &my_uvb_rates->crsHeI, &my_uvb_rates->crsHeII, - &my_uvb_rates->k24, &my_uvb_rates->k26, - &my_chemistry->use_radiative_transfer, my_fields->RT_heating_rate, - &my_chemistry->h2_optical_depth_approximation, &my_chemistry->cie_cooling, &my_chemistry->h2_cooling_rate, &my_chemistry->hd_cooling_rate, my_rates->cieco, coolingheating_buf.cieco, - &my_chemistry->cmb_temperature_floor, &my_chemistry->UVbackground, &my_chemistry->cloudy_electron_fraction_factor, - &my_rates->cloudy_primordial.grid_rank, my_rates->cloudy_primordial.grid_dimension, - my_rates->cloudy_primordial.grid_parameters[0], my_rates->cloudy_primordial.grid_parameters[1], my_rates->cloudy_primordial.grid_parameters[2], my_rates->cloudy_primordial.grid_parameters[3], my_rates->cloudy_primordial.grid_parameters[4], - &my_rates->cloudy_primordial.data_size, my_rates->cloudy_primordial.cooling_data, my_rates->cloudy_primordial.heating_data, my_rates->cloudy_primordial.mmw_data, - &my_rates->cloudy_metal.grid_rank, my_rates->cloudy_metal.grid_dimension, - my_rates->cloudy_metal.grid_parameters[0], my_rates->cloudy_metal.grid_parameters[1], my_rates->cloudy_metal.grid_parameters[2], my_rates->cloudy_metal.grid_parameters[3], my_rates->cloudy_metal.grid_parameters[4], - &my_rates->cloudy_metal.data_size, my_rates->cloudy_metal.cooling_data, my_rates->cloudy_metal.heating_data, &my_rates->cloudy_data_new, - &my_chemistry->use_volumetric_heating_rate, &my_chemistry->use_specific_heating_rate, my_fields->volumetric_heating_rate, my_fields->specific_heating_rate, - &my_chemistry->use_temperature_floor, &my_chemistry->temperature_floor_scalar, my_fields->temperature_floor, - &my_chemistry->use_isrf_field, my_fields->isrf_habing, itmask.data(), itmask_metal.data(), - &my_chemistry->metal_chemistry, &my_chemistry->grain_growth, &my_chemistry->use_primordial_continuum_opacity, &my_chemistry->tabulated_cooling_minimum_temperature, - my_fields->DM_density, my_fields->HDII_density, my_fields->HeHII_density, - my_fields->CI_density, my_fields->CII_density, my_fields->CO_density, my_fields->CO2_density, - my_fields->OI_density, my_fields->OH_density, my_fields->H2O_density, my_fields->O2_density, - my_fields->SiI_density, my_fields->SiOI_density, my_fields->SiO2I_density, - my_fields->CH_density, my_fields->CH2_density, my_fields->COII_density, my_fields->OII_density, - my_fields->OHII_density, my_fields->H2OII_density, my_fields->H3OII_density, my_fields->O2II_density, - my_fields->Mg_density, my_fields->Al_density, my_fields->S_density, my_fields->Fe_density, - my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, - my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, - my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, - my_rates->cieY06, - my_rates->LH2.props.dimension, &my_rates->LH2.props.data_size, - my_rates->LH2.props.parameters[0], my_rates->LH2.props.parameters[1], my_rates->LH2.props.parameters[2], - &my_rates->LH2.props.parameter_spacing[0], &my_rates->LH2.props.parameter_spacing[1], &my_rates->LH2.props.parameter_spacing[2], my_rates->LH2.data, - my_rates->LHD.props.dimension, &my_rates->LHD.props.data_size, - my_rates->LHD.props.parameters[0], my_rates->LHD.props.parameters[1], my_rates->LHD.props.parameters[2], - &my_rates->LHD.props.parameter_spacing[0], &my_rates->LHD.props.parameter_spacing[1], &my_rates->LHD.props.parameter_spacing[2], my_rates->LHD.data, - my_rates->LCI.props.dimension, &my_rates->LCI.props.data_size, - my_rates->LCI.props.parameters[0], my_rates->LCI.props.parameters[1], my_rates->LCI.props.parameters[2], - &my_rates->LCI.props.parameter_spacing[0], &my_rates->LCI.props.parameter_spacing[1], &my_rates->LCI.props.parameter_spacing[2], my_rates->LCI.data, - my_rates->LCII.props.dimension, &my_rates->LCII.props.data_size, - my_rates->LCII.props.parameters[0], my_rates->LCII.props.parameters[1], my_rates->LCII.props.parameters[2], - &my_rates->LCII.props.parameter_spacing[0], &my_rates->LCII.props.parameter_spacing[1], &my_rates->LCII.props.parameter_spacing[2], my_rates->LCII.data, - my_rates->LOI.props.dimension, &my_rates->LOI.props.data_size, - my_rates->LOI.props.parameters[0], my_rates->LOI.props.parameters[1], my_rates->LOI.props.parameters[2], - &my_rates->LOI.props.parameter_spacing[0], &my_rates->LOI.props.parameter_spacing[1], &my_rates->LOI.props.parameter_spacing[2], my_rates->LOI.data, - my_rates->LCO.props.dimension, &my_rates->LCO.props.data_size, - my_rates->LCO.props.parameters[0], my_rates->LCO.props.parameters[1], my_rates->LCO.props.parameters[2], - &my_rates->LCO.props.parameter_spacing[0], &my_rates->LCO.props.parameter_spacing[1], &my_rates->LCO.props.parameter_spacing[2], my_rates->LCO.data, - my_rates->LOH.props.dimension, &my_rates->LOH.props.data_size, - my_rates->LOH.props.parameters[0], my_rates->LOH.props.parameters[1], my_rates->LOH.props.parameters[2], - &my_rates->LOH.props.parameter_spacing[0], &my_rates->LOH.props.parameter_spacing[1], &my_rates->LOH.props.parameter_spacing[2], my_rates->LOH.data, - my_rates->LH2O.props.dimension, &my_rates->LH2O.props.data_size, - my_rates->LH2O.props.parameters[0], my_rates->LH2O.props.parameters[1], my_rates->LH2O.props.parameters[2], - &my_rates->LH2O.props.parameter_spacing[0], &my_rates->LH2O.props.parameter_spacing[1], &my_rates->LH2O.props.parameter_spacing[2], my_rates->LH2O.data, - my_rates->alphap.props.dimension, &my_rates->alphap.props.data_size, - my_rates->alphap.props.parameters[0], my_rates->alphap.props.parameters[1], &my_rates->alphap.props.parameter_spacing[0], &my_rates->alphap.props.parameter_spacing[1], - my_rates->alphap.data, - &my_chemistry->multi_metals, &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, - my_fields->local_ISM_metal_density, - my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, - my_fields->fsn13_metal_density, my_fields->fsn15_metal_density, my_fields->fsn50_metal_density, my_fields->fsn80_metal_density, - my_fields->pisn170_metal_density, my_fields->pisn200_metal_density, my_fields->y19_metal_density, - &my_rates->SN0_N, - my_rates->SN0_fSiM, my_rates->SN0_fFeM, my_rates->SN0_fMg2SiO4, my_rates->SN0_fMgSiO3, - my_rates->SN0_fFe3O4, my_rates->SN0_fAC, my_rates->SN0_fSiO2D, my_rates->SN0_fMgO, - my_rates->SN0_fFeS, my_rates->SN0_fAl2O3, - my_rates->SN0_freforg, my_rates->SN0_fvolorg, my_rates->SN0_fH2Oice, - my_rates->SN0_r0SiM, my_rates->SN0_r0FeM, my_rates->SN0_r0Mg2SiO4, my_rates->SN0_r0MgSiO3, - my_rates->SN0_r0Fe3O4, my_rates->SN0_r0AC, my_rates->SN0_r0SiO2D, my_rates->SN0_r0MgO, - my_rates->SN0_r0FeS, my_rates->SN0_r0Al2O3, - my_rates->SN0_r0reforg, my_rates->SN0_r0volorg, my_rates->SN0_r0H2Oice, - my_rates->gr_N, &my_rates->gr_Size, &my_rates->gr_dT, my_rates->gr_Td, - my_rates->SN0_kpSiM, my_rates->SN0_kpFeM, my_rates->SN0_kpMg2SiO4, my_rates->SN0_kpMgSiO3, - my_rates->SN0_kpFe3O4, my_rates->SN0_kpAC, my_rates->SN0_kpSiO2D, my_rates->SN0_kpMgO, - my_rates->SN0_kpFeS, my_rates->SN0_kpAl2O3, - my_rates->SN0_kpreforg, my_rates->SN0_kpvolorg, my_rates->SN0_kpH2Oice, - grain_temperatures.SiM, grain_temperatures.FeM, grain_temperatures.Mg2SiO4, grain_temperatures.MgSiO3, grain_temperatures.Fe3O4, - grain_temperatures.AC, grain_temperatures.SiO2D, grain_temperatures.MgO, grain_temperatures.FeS, grain_temperatures.Al2O3, - grain_temperatures.reforg, grain_temperatures.volorg, grain_temperatures.H2Oice, - my_rates->gas_grain2, &my_rates->gamma_isrf2 - ); + wrapped_cool1d_multi_g_( + imetal, j, k, iter, utem, &comp1, &comp2, edot.data(), tgas.data(), + mmw.data(), p2d.data(), tdust.data(), metallicity.data(), + dust2gas.data(), rhoH.data(), itmask.data(), itmask_metal.data(), + my_chemistry, my_rates, my_fields, *my_uvb_rates, internalu, + grain_temperatures, logTlininterp_buf, cool1dmulti_buf, + coolingheating_buf + ); if (my_chemistry->primordial_chemistry == 0) { // This is some basic book-keeping to ensure that itmask_tmp has From 9bf2f13ba90a78b5d7608343db60caf51ae2ee08 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Mon, 20 Jan 2025 16:51:19 -0500 Subject: [PATCH 057/101] Add utem to InternalGrUnits struct --- src/clib/fortran_func_wrappers.hpp | 8 +++---- src/clib/internal_units.h | 37 +++++++++++++++++++++++++++--- src/clib/solve_chemistry.c | 8 ++----- src/clib/solve_rate_cool_g-cpp.C | 6 ++--- src/clib/solve_rate_cool_g-cpp.h | 2 +- 5 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/clib/fortran_func_wrappers.hpp b/src/clib/fortran_func_wrappers.hpp index 914848cca..a41768884 100644 --- a/src/clib/fortran_func_wrappers.hpp +++ b/src/clib/fortran_func_wrappers.hpp @@ -56,7 +56,7 @@ inline void wrapped_ceiling_species_g_( } inline void wrapped_cool1d_multi_g_( - int imetal, int j, int k, int iter, double utem, double* comp1, double* comp2, + int imetal, int j, int k, int iter, double* comp1, double* comp2, double* edot, double* tgas, double* mmw, double* p2d, double* tdust, double* metallicity, double* dust2gas, double* rhoH, gr_mask_type* itmask, gr_mask_type* itmask_metal, chemistry_data* my_chemistry, @@ -74,7 +74,7 @@ inline void wrapped_cool1d_multi_g_( &my_chemistry->h2_on_dust, &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_chemistry->dust_recombination_cooling, &my_fields->grid_rank, &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, &my_chemistry->ih2co, &my_chemistry->ipiht, &iter, &my_chemistry->photoelectric_heating, &internalu.a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, - &utem, &internalu.uxyz, &internalu.a_units, &internalu.urho, &internalu.tbase1, + &internalu.utem, &internalu.uxyz, &internalu.a_units, &internalu.urho, &internalu.tbase1, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, my_rates->ceHI, my_rates->ceHeI, my_rates->ceHeII, my_rates->ciHI, my_rates->ciHeI, my_rates->ciHeIS, my_rates->ciHeII, my_rates->reHII, my_rates->reHeII1, @@ -450,7 +450,7 @@ inline void wrapped_step_rate_g_( } inline void wrapped_step_rate_newton_raphson_( - int imetal, double utem, int j, int k, int iter, double dom, double* comp1, + int imetal, int j, int k, int iter, double dom, double* comp1, double* comp2, double chunit, double dx_cgs, double c_ljeans, double* dtit, double* p2d, double* tgas, double* tdust, double* metallicity, double* dust2gas, double* rhoH, double* mmw, double* h2dust, double* edot, @@ -468,7 +468,7 @@ inline void wrapped_step_rate_newton_raphson_( my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, &internalu.extfields_in_comoving, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_fields->grid_start[0], &my_fields->grid_end[0], &my_chemistry->ih2co, &my_chemistry->ipiht, - &my_chemistry->dust_recombination_cooling, &my_chemistry->photoelectric_heating, &internalu.a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &utem, + &my_chemistry->dust_recombination_cooling, &my_chemistry->photoelectric_heating, &internalu.a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &internalu.utem, &internalu.uxyz, &internalu.a_units, &internalu.urho, &internalu.tbase1, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, my_rates->k1, my_rates->k2, my_rates->k3, my_rates->k4, my_rates->k5, my_rates->k6, my_rates->k7, my_rates->k8, my_rates->k9, my_rates->k10, my_rates->k11, my_rates->k12, my_rates->k13, my_rates->k13dd, my_rates->k14, my_rates->k15, my_rates->k16, diff --git a/src/clib/internal_units.h b/src/clib/internal_units.h index b889e9ae1..6a1cdfb9f 100644 --- a/src/clib/internal_units.h +++ b/src/clib/internal_units.h @@ -88,6 +88,21 @@ typedef struct InternalGrUnits{ /// accessed so we precompute it double coolunit; + /// constant coefficients (including unit conversions) that must be + /// multiplied by `internal_energy` (in squared velocity code units) when + /// calculating temperature (this term neglects contributions from mmw and + /// adiabatic index). For more information, see the documentation of + /// `get_temperature_units` in the website docs. + /// + /// This isn't really a "unit", but we are including it in this struct + /// because we historically have called it a unit and it used in the vast + /// majority of cases where this struct gets used. + /// + /// Because of the way that relevant units (namely code velocity) in our + /// comoving unit system are defined, this is the same in the proper & + /// comoving frame + double utem; + } InternalGrUnits; /// Return the version of hydrogen mass constant used by the internal units @@ -147,6 +162,17 @@ static inline InternalGrUnits new_internalu_( // rename the frontend_units object for convenience const code_units* my_units = frontend_units; + // Part 1: Common logic extracted from solve_chemistry and + // calculate_(cooling_time|temperature|dust_temperature) that was + // historically used to determine unit-related quantities passed into + // the fortran subroutines + + // calculate temperature units (obviously, don't change this until we finish + // transcription -- under the hood, this calculation implicitly chooses a + // definition of mh that may differ from the value used elesewhere within the + // functions in the current file) + double temperature_units = get_temperature_units(my_units); + // determine the size of comoving length & comoving density units // (that are equivalent to the frontend units), as measured in the proper // reference frame @@ -169,19 +195,24 @@ static inline InternalGrUnits new_internalu_( POW(my_units->a_value * my_units->a_units, 3); } - // initialize output units and copy some stuff from frontend units + // Part 2: initialize output units and copy some stuff from frontend units InternalGrUnits internalu; internalu.a_value = my_units->a_value; internalu.a_units = my_units->a_units; internalu.extfields_in_comoving = my_units->comoving_coordinates; + // store the temperature_units (this name remapping would historically occur) + internalu.utem = temperature_units; + // this represents the name remapping that would occur when passing // co_length_units and co_density_units into a Fortran routine internalu.uxyz = co_length_units; internalu.urho = co_density_units; - // store the fundamental units of the unit system - // + // Part 3: Employ logic from fortran subroutines to compute fundamental unit + // basis of the unit-system and store them in internalu. Then compute + // the cooling units. + // TODO: (AFTER FINISIHING TRANSCRIPTION) Consider refactoring this logic for // the case with `my_units->comoving_coordinates==0` (in that case, we do an // unnecessary round-trip) diff --git a/src/clib/solve_chemistry.c b/src/clib/solve_chemistry.c index ec03535fa..130293bb8 100644 --- a/src/clib/solve_chemistry.c +++ b/src/clib/solve_chemistry.c @@ -89,14 +89,10 @@ int local_solve_chemistry(chemistry_data *my_chemistry, return FAIL; } - /* Calculate temperature units. */ - - double temperature_units = get_temperature_units(my_units); - - /* Call the fortran routine to solve cooling equations. */ + /* Call the routine to solve cooling equations. */ int ierr = solve_rate_cool_g( - metal_field_present, dt_value, temperature_units, internalu, + metal_field_present, dt_value, internalu, my_chemistry, my_rates, my_fields, &my_uvb_rates ); diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 2418b1c59..c39e979bd 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -104,7 +104,7 @@ extern "C" { #endif /* __cplusplus */ int solve_rate_cool_g( - int imetal, double dt, double utem, InternalGrUnits internalu, + int imetal, double dt, InternalGrUnits internalu, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, grackle_field_data* my_fields, photo_rate_storage* my_uvb_rates ) @@ -356,7 +356,7 @@ int solve_rate_cool_g( // Compute the cooling rate, tgas, tdust, and metallicity for this row wrapped_cool1d_multi_g_( - imetal, j, k, iter, utem, &comp1, &comp2, edot.data(), tgas.data(), + imetal, j, k, iter, &comp1, &comp2, edot.data(), tgas.data(), mmw.data(), p2d.data(), tdust.data(), metallicity.data(), dust2gas.data(), rhoH.data(), itmask.data(), itmask_metal.data(), my_chemistry, my_rates, my_fields, *my_uvb_rates, internalu, @@ -579,7 +579,7 @@ int solve_rate_cool_g( // itmask_nr) wrapped_step_rate_newton_raphson_( - imetal, utem, j, k, iter, dom, &comp1, &comp2, chunit, dx_cgs, + imetal, j, k, iter, dom, &comp1, &comp2, chunit, dx_cgs, c_ljeans, dtit.data(), p2d.data(), tgas.data(), tdust.data(), metallicity.data(), dust2gas.data(), rhoH.data(), mmw.data(), h2dust.data(), edot.data(), anydust, itmask_nr.data(), diff --git a/src/clib/solve_rate_cool_g-cpp.h b/src/clib/solve_rate_cool_g-cpp.h index 3a639f804..ee5319bb3 100644 --- a/src/clib/solve_rate_cool_g-cpp.h +++ b/src/clib/solve_rate_cool_g-cpp.h @@ -32,7 +32,7 @@ extern "C" { /// /// @return Returns GR_SUCCESS or GR_FAIL to indicate whether there was an error int solve_rate_cool_g( - int imetal, double dt, double utem, InternalGrUnits internalu, + int imetal, double dt, InternalGrUnits internalu, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, grackle_field_data* my_fields, photo_rate_storage* my_uvb_rates ); From 55bae14bc565036f8f1114b4b700f934128ffe8f Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 09:45:49 -0500 Subject: [PATCH 058/101] Tweak `index_helper.h` for C++ compatability I really didn't want to do this right now, because it involved touching some files that are otherwise unaffected by the gen2024 PR, but after mulling it over, I concluded that it was the right thing to do. The impetus of this commit is tweaking `index_helper.h` so it can be included by source files being compiled with a C++ compiler. - I needed to add the extern "C" blocks to ensure compatability with both C and C++ - Specifically, function names beginning with an underscore (`_`) aren't to be used with C++ - the main reason I feel this is necessary, is that I plan to introduce a new struct type to aide with the transcription process.[^1] That planned struct should really coexist with the other machinery defined in this file. The specific changes to `index_helper.h` include: 1. I renamed `grackle_index_range` to `field_flat_index_range` (this wasn't strictly necessary, but it will reduce confusion with the planned struct) 2. I renamed `_build_index_helper` -> `build_index_helper_` 3. I renamed `_inner_range` -> `inner_flat_range_` (the leading unscore **NEEDED** to be removed anyways. I chose to insert `flat` for consistency the name-change described in change-1. 4. I inserted `extern "C"` blocks 5. While I was here, also added some brief docstrings. I propogated the name changes to all other relevant files [^1]: Essentially this struct will store information about the 3D index range. It will be **REALLY** important for reducing confusion about 0-based and 1-based indexing as we incrementally transcribe subroutines. --- src/clib/calculate_gamma.c | 10 +++++----- src/clib/calculate_pressure.c | 8 +++++--- src/clib/calculate_temperature.c | 5 +++-- src/clib/index_helper.c | 2 +- src/clib/index_helper.h | 32 ++++++++++++++++++++++++++------ 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/clib/calculate_gamma.c b/src/clib/calculate_gamma.c index 1b177904a..bde450dc5 100644 --- a/src/clib/calculate_gamma.c +++ b/src/clib/calculate_gamma.c @@ -32,8 +32,6 @@ int local_calculate_temperature(chemistry_data *my_chemistry, grackle_field_data *my_fields, gr_float *temperature); -grackle_index_helper _build_index_helper(const grackle_field_data *my_fields); - int local_calculate_gamma(chemistry_data *my_chemistry, chemistry_data_storage *my_rates, code_units *my_units, @@ -44,7 +42,7 @@ int local_calculate_gamma(chemistry_data *my_chemistry, if (!my_chemistry->use_grackle) return SUCCESS; - const grackle_index_helper ind_helper = _build_index_helper(my_fields); + const grackle_index_helper ind_helper = build_index_helper_(my_fields); int outer_ind, index; /* If molecular hydrogen is not being used, just use monotonic. @@ -52,7 +50,8 @@ int local_calculate_gamma(chemistry_data *my_chemistry, for (outer_ind = 0; outer_ind < ind_helper.outer_ind_size; outer_ind++){ - const grackle_index_range range = _inner_range(outer_ind, &ind_helper); + const field_flat_index_range range = inner_flat_range_(outer_ind, + &ind_helper); for (index = range.start; index <= range.end; index++) { my_gamma[index] = my_chemistry->Gamma; @@ -83,7 +82,8 @@ int local_calculate_gamma(chemistry_data *my_chemistry, # endif for (outer_ind = 0; outer_ind < ind_helper.outer_ind_size; outer_ind++){ - const grackle_index_range range = _inner_range(outer_ind, &ind_helper); + const field_flat_index_range range = inner_flat_range_(outer_ind, + &ind_helper); for (index = range.start; index <= range.end; index++) { diff --git a/src/clib/calculate_pressure.c b/src/clib/calculate_pressure.c index 40f142741..2f2aa719a 100644 --- a/src/clib/calculate_pressure.c +++ b/src/clib/calculate_pressure.c @@ -39,7 +39,7 @@ int local_calculate_pressure(chemistry_data *my_chemistry, return SUCCESS; double tiny_number = 1.e-20; - const grackle_index_helper ind_helper = _build_index_helper(my_fields); + const grackle_index_helper ind_helper = build_index_helper_(my_fields); int outer_ind, index; /* parallelize the k and j loops with OpenMP @@ -49,7 +49,8 @@ int local_calculate_pressure(chemistry_data *my_chemistry, # endif for (outer_ind = 0; outer_ind < ind_helper.outer_ind_size; outer_ind++){ - const grackle_index_range range = _inner_range(outer_ind, &ind_helper); + const field_flat_index_range range = inner_flat_range_(outer_ind, + &ind_helper); for (index = range.start; index <= range.end; index++) { @@ -80,7 +81,8 @@ int local_calculate_pressure(chemistry_data *my_chemistry, # endif for (int outer_ind = 0; outer_ind < ind_helper.outer_ind_size; outer_ind++){ - const grackle_index_range range = _inner_range(outer_ind, &ind_helper); + const field_flat_index_range range = inner_flat_range_(outer_ind, + &ind_helper); for (index = range.start; index <= range.end; index++) { diff --git a/src/clib/calculate_temperature.c b/src/clib/calculate_temperature.c index c684f6084..a11f9ef5c 100644 --- a/src/clib/calculate_temperature.c +++ b/src/clib/calculate_temperature.c @@ -98,7 +98,7 @@ int local_calculate_temperature(chemistry_data *my_chemistry, } /* Compute properties used to index the field. */ - const grackle_index_helper ind_helper = _build_index_helper(my_fields); + const grackle_index_helper ind_helper = build_index_helper_(my_fields); int outer_ind, index; /* Compute temperature with mu calculated directly. */ @@ -111,7 +111,8 @@ int local_calculate_temperature(chemistry_data *my_chemistry, # endif for (outer_ind = 0; outer_ind < ind_helper.outer_ind_size; outer_ind++){ - const grackle_index_range range = _inner_range(outer_ind, &ind_helper); + const field_flat_index_range range = inner_flat_range_(outer_ind, + &ind_helper); for (index = range.start; index <= range.end; index++) { diff --git a/src/clib/index_helper.c b/src/clib/index_helper.c index da0818576..1f59eb186 100644 --- a/src/clib/index_helper.c +++ b/src/clib/index_helper.c @@ -14,7 +14,7 @@ #include "grackle_types.h" #include "index_helper.h" -grackle_index_helper _build_index_helper(const grackle_field_data *my_fields) +grackle_index_helper build_index_helper_(const grackle_field_data *my_fields) { grackle_index_helper out; const int rank = my_fields->grid_rank; diff --git a/src/clib/index_helper.h b/src/clib/index_helper.h index 7a923990f..143f6d5b8 100644 --- a/src/clib/index_helper.h +++ b/src/clib/index_helper.h @@ -15,12 +15,25 @@ #ifndef __GRACKLE_PRIVATE_H_ #define __GRACKLE_PRIVATE_H_ +#include "grackle_types.h" // grackle_field_data + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + /*********************************************************************** / / VARIABLE TYPES / ************************************************************************/ +/// internal type to assist with iterating over 3D index-space. +/// +/// An instance, `obj`, is commonly used in a 2-level nested for-loop: +/// - the outer loop iterates over index `t` for `0 <= t < obj.outer_ind_size`. +/// In 3D, this loop corresponds to `j`,`k` index pairs. +/// - the inner loop iterate over the "index-range" (constructed from `obj` +/// and `t`). In 3D, this loop corresponds to the `i` axis. typedef struct { int i_start; @@ -39,11 +52,13 @@ typedef struct } grackle_index_helper; +/// Specifies a range of indices for grackle's 3D fields, for use when you +/// treat the fields as flattened 1d arrays typedef struct { int start; int end; -} grackle_index_range; +} field_flat_index_range; /*********************************************************************** / @@ -54,17 +69,22 @@ typedef struct // to help the compiler optimize the associated for-loops, this function: // - is implemented inline // - returns results as a struct rather than by modifying pointer arguments -static inline grackle_index_range _inner_range(int outer_index, - const grackle_index_helper* ind_helper) +static inline field_flat_index_range inner_flat_range_( + int outer_index, const grackle_index_helper* ind_helper +) { int k = (outer_index / ind_helper->num_j_inds) + ind_helper->k_start; int j = (outer_index % ind_helper->num_j_inds) + ind_helper->j_start; int outer_offset = ind_helper->i_dim * (j + ind_helper->j_dim * k); - grackle_index_range out = {ind_helper->i_start + outer_offset, - ind_helper->i_end + outer_offset}; + field_flat_index_range out = {ind_helper->i_start + outer_offset, + ind_helper->i_end + outer_offset}; return out; } -grackle_index_helper _build_index_helper(const grackle_field_data *my_fields); +grackle_index_helper build_index_helper_(const grackle_field_data *my_fields); + +#ifdef __cplusplus +} // extern "C" +#endif /* __cplusplus */ #endif From 83253ce2254b6d05fbf19577bf4332b9c1b83813 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 12:29:39 -0500 Subject: [PATCH 059/101] introduce the IndexRange --- src/clib/index_helper.h | 88 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/src/clib/index_helper.h b/src/clib/index_helper.h index 143f6d5b8..b08677af3 100644 --- a/src/clib/index_helper.h +++ b/src/clib/index_helper.h @@ -60,6 +60,77 @@ typedef struct int end; } field_flat_index_range; +/// Specifies the range of indices for grackle's 3D fields. +/// +/// Essentially, you use this struct to easily get the 3D indices for elements +/// in an "islice". +/// - Grackle commonly uses buffers that are used to hold temporary variable +/// that you just use the i-indices to access +/// - If you want to use a 3D index to get an element from one of grackle's 3D +/// field, special logic is required to remap the 3 indices to a single index +/// in the underlying flat 1d buffer that holds a field's data. In some cases +/// (e.g. Fortran Arrays) the remapping is done behind the scenes and in +/// cases, it is more explicit (but the logic is always somewhere) +/// - For context, the `field_flat_index_range` can be used to specify the same +/// range for 3D fields, but the remapping logic is pre-applied (this makes +/// it much less useful when you have these custom buffers) +/// +/// @par Role in Transcription +/// This is intended to aide the transcription process of fortran subroutines +/// - prior to transcription, the 1-based `j` and `k` index values are passed +/// throughout the call-stack. Furthermore, the 0-based `i_start` & `i_end` +/// values are passed around. +/// - the automatic transcription process ends up making the indexing look a +/// little messy when transcribing from Fortran to C++ (in order to account +/// for the difference between 1-based & 0-based indexing). Thus gradual, +/// manual intervention is required to make indexing appear idiomatic. +/// - the manual process introduces a lot of opportunities for error if we +/// aren't extremely careful with keeping track of whether a local variable +/// still refers to a 1-based index or been converted to a 0-based index. +/// By using this struct, we can be very explicit and certain about whether +/// an expressions refers to a 1-based index or a 0-based index' +/// +/// @note +/// Naively, this all might sound like it involves a bunch of extra work, but +/// the following it will work really well with the transcription machinery: +/// - Consider a fortran subroutine, `my_subroutine`, where one of the +/// arguments is the `j` index (which is expected to use 1-based indexing) +/// and the signature looks like `void my_subroutine(..., int* j, ...)` +/// - Now suppose we have a C/C++ function that calls `my_subroutine` and that +/// there is an `IndexRange` in the function called `idx_range`. +/// - If the call to `my_subroutine` in the C/C++ function looks like +/// `my_subroutine(..., &idx_range.jp1, ...)`, then when applying the +/// transcription tools to `my_subroutine` we can have the transcription +/// machinery replace every reference to `j` in the transcribed function +/// with `idx_range.jp1` (it will also modify the argument list properly). +/// +/// @note +/// ultimately, it will be more idiomatic to make for-loops use `i_stop` rather +/// than `i_end`. Obviously, this is a **very low** priority (but this is why +/// we put both variables in this struct) +/// +/// @par Future Usage +/// If we continue using this type after we complete transcription, we can have +/// it take on a prominent role in adding GPU-support. +typedef struct IndexRange +{ + // specifies the fixed (0-based) j and k indices to be used with the range + int j; + int k; + + // j & k indices for 1-based indexing (remove when transcription is done) + int jp1; + int kp1; + + // specifies the starting and stopping (0-based) indices specifying the range + // along the i-axis (the stopping index isn't included in the range) + int i_start; + int i_stop; + + // holds the value of `i_stop-1` (remove when transcription is done) + int i_end; +} IndexRange; + /*********************************************************************** / / FUNCTION DECLARATIONS @@ -81,6 +152,23 @@ static inline field_flat_index_range inner_flat_range_( return out; } +/// constructs an IndexRange, which holds the 3D index information for an +/// "islice." +static inline IndexRange make_idx_range_( + int outer_index, const grackle_index_helper* idx_helper +) +{ + IndexRange out; + out.k = (outer_index / idx_helper->num_j_inds) + idx_helper->k_start; + out.j = (outer_index % idx_helper->num_j_inds) + idx_helper->j_start; + out.jp1 = out.j+1; + out.kp1 = out.k+1; + out.i_start = idx_helper->i_start; + out.i_end = idx_helper->i_end; + out.i_stop = idx_helper->i_end+1; + return out; +} + grackle_index_helper build_index_helper_(const grackle_field_data *my_fields); #ifdef __cplusplus From 1aaa0642d4f0780bf75664ae2db209efc485ca78 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 14:35:15 -0500 Subject: [PATCH 060/101] first step towards integrating IndexRange into solve_rate_cool_g --- src/clib/solve_rate_cool_g-cpp.C | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index c39e979bd..8f2c056eb 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -11,6 +11,7 @@ #include "grackle.h" #include "fortran_func_wrappers.hpp" +#include "index_helper.h" #include "internal_types.hpp" #include "internal_units.h" #include "utils-cpp.hpp" @@ -135,8 +136,7 @@ int solve_rate_cool_g( // Locals - int i, j, k, iter; - int t, dj, dk; + int i, iter; double ttmin, dom, energy, comp1, comp2; double chunit; double dlogtem, dx_cgs, c_ljeans, min_metallicity; @@ -227,17 +227,14 @@ int solve_rate_cool_g( wrapped_ceiling_species_g_(imetal, my_chemistry, my_fields); #endif - - // Loop over zones, and do an entire i-column in one go - dk = my_fields->grid_end[2] - my_fields->grid_start[2] + 1; - dj = my_fields->grid_end[1] - my_fields->grid_start[1] + 1; + const grackle_index_helper idx_helper = build_index_helper_(my_fields); // parallelize the k and j loops with OpenMP // flat j and k loops for better parallelism //_// PORT: #ifdef _OPENMP //_// PORT: ! ierr is declared as shared and should be modified with atomic operation //_// PORT: !$omp parallel do schedule(runtime) private( - //_// PORT: !$omp& i, j, k, iter, + //_// PORT: !$omp& i, iter, //_// PORT: !$omp& ttmin, energy, comp1, comp2, //_// PORT: !$omp& dtit, ttot, p2d, tgas, //_// PORT: !$omp& tdust, metallicity, dust2gas, rhoH, mmw, @@ -293,9 +290,11 @@ int solve_rate_cool_g( grackle::impl::new_ChemHeatingRates(my_fields->grid_dimension[0]); //_// TODO_USE: OMP_PRAGMA("omp for") - for (t = 0; t<=(dk * dj - 1); t++) { - k = t/dj + my_fields->grid_start[2]+1; - j = grackle::impl::mod(t,dj) + my_fields->grid_start[1]+1; + for (int t = 0; t < idx_helper.outer_ind_size; t++) { + // construct an index-range corresponding to "i-slice" + const IndexRange idx_range = make_idx_range_(t, &idx_helper); + const int k = idx_range.kp1; // use 1-based index for now + const int j = idx_range.jp1; // use 1-based index for now // tolerance = 1.0e-06_DKIND * dt @@ -652,7 +651,7 @@ int solve_rate_cool_g( } } - } // loop over j,k pairs + } // outer-loop (index t) - each of these correspond to j,k pairs // cleanup manually allocated temporaries grackle::impl::drop_GrainSpeciesCollection(&grain_temperatures); From c251745b60c3d77fb8566ea7b55fd8230afd0046 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 14:53:56 -0500 Subject: [PATCH 061/101] start integrating IndexRange into wrapped fortran subroutines --- src/clib/fortran_func_wrappers.hpp | 21 +++++++++++---------- src/clib/solve_rate_cool_g-cpp.C | 12 ++++++------ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/clib/fortran_func_wrappers.hpp b/src/clib/fortran_func_wrappers.hpp index a41768884..9d67c2c63 100644 --- a/src/clib/fortran_func_wrappers.hpp +++ b/src/clib/fortran_func_wrappers.hpp @@ -24,6 +24,7 @@ #include "grackle.h" #include "fortran_func_decls.h" +#include "index_helper.h" #include "internal_types.hpp" #include "internal_units.h" #include "LUT.hpp" @@ -56,7 +57,7 @@ inline void wrapped_ceiling_species_g_( } inline void wrapped_cool1d_multi_g_( - int imetal, int j, int k, int iter, double* comp1, double* comp2, + int imetal, IndexRange idx_range, int iter, double* comp1, double* comp2, double* edot, double* tgas, double* mmw, double* p2d, double* tdust, double* metallicity, double* dust2gas, double* rhoH, gr_mask_type* itmask, gr_mask_type* itmask_metal, chemistry_data* my_chemistry, @@ -72,7 +73,7 @@ inline void wrapped_cool1d_multi_g_( &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, &internalu.extfields_in_comoving, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_chemistry->dust_recombination_cooling, - &my_fields->grid_rank, &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, &my_chemistry->ih2co, &my_chemistry->ipiht, &iter, &my_chemistry->photoelectric_heating, + &my_fields->grid_rank, &my_fields->grid_start[0], &my_fields->grid_end[0], &idx_range.jp1, &idx_range.kp1, &my_chemistry->ih2co, &my_chemistry->ipiht, &iter, &my_chemistry->photoelectric_heating, &internalu.a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, &internalu.utem, &internalu.uxyz, &internalu.a_units, &internalu.urho, &internalu.tbase1, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, @@ -177,7 +178,7 @@ inline void wrapped_cool1d_multi_g_( /// This routine uses the temperature to look up the chemical rates that are /// tabulated in a log table as a function of temperature inline void wrapped_lookup_cool_rates1d_g_( - int j, int k, gr_mask_type anydust, double* tgas1d, double* mmw, + IndexRange idx_range, gr_mask_type anydust, double* tgas1d, double* mmw, double* tdust, double* dust2gas, double* k13dd, double* h2dust, double dom, double dx_cgs, double c_ljeans, gr_mask_type* itmask, gr_mask_type* itmask_metal, int imetal, gr_float* rhoH, double dt, @@ -192,7 +193,7 @@ inline void wrapped_lookup_cool_rates1d_g_( grackle::impl::ChemHeatingRates chemheatrates_buf ) { - FORTRAN_NAME(lookup_cool_rates1d_g)(&my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->NumberOfTemperatureBins, &j, &k, + FORTRAN_NAME(lookup_cool_rates1d_g)(&my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->NumberOfTemperatureBins, &idx_range.jp1, &idx_range.kp1, &my_fields->grid_start[0], &my_fields->grid_end[0], &my_chemistry->three_body_rate, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, &anydust, &my_chemistry->H2_self_shielding, &my_chemistry->self_shielding_method, @@ -320,7 +321,7 @@ inline void wrapped_make_consistent_g_( /// This routine calculates the electron and HI rates of change in order to /// determine the maximum permitted timestep inline void wrapped_rate_timestep_g_( - double* dedot, double* HIdot, gr_mask_type anydust, int j, int k, + double* dedot, double* HIdot, gr_mask_type anydust, IndexRange idx_range, double* h2dust, double* rhoH, gr_mask_type* itmask, double* edot, double chunit, double dom, chemistry_data* my_chemistry, grackle_field_data* my_fields, photo_rate_storage my_uvb_rates, @@ -332,7 +333,7 @@ inline void wrapped_rate_timestep_g_( dedot, HIdot, &my_chemistry->primordial_chemistry, &anydust, my_fields->e_density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, my_fields->density, my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_fields->grid_start[0], &my_fields->grid_end[0], &idx_range.jp1, &idx_range.kp1, kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], kcr_buf.data[ColRecRxnLUT::k11], kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], &my_uvb_rates.k24, &my_uvb_rates.k25, &my_uvb_rates.k26, &my_uvb_rates.k27, &my_uvb_rates.k28, &my_uvb_rates.k29, &my_uvb_rates.k30, @@ -380,7 +381,7 @@ inline void wrapped_scale_fields_g_( /// Uses one linearly implicit Gauss-Seidel sweep of a backward-Euler time /// integrator to advance the rate equations by one (sub-)cycle (dtit). inline void wrapped_step_rate_g_( - double* dtit, int j, int k, gr_mask_type anydust, double* h2dust, + double* dtit, IndexRange idx_range, gr_mask_type anydust, double* h2dust, double* rhoH, double* dedot_prev, double* HIdot_prev, gr_mask_type* itmask, gr_mask_type* itmask_metal, int imetal, chemistry_data* my_chemistry, grackle_field_data* my_fields, @@ -392,7 +393,7 @@ inline void wrapped_step_rate_g_( ) { FORTRAN_NAME(step_rate_g)(my_fields->e_density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, my_fields->density, my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, dtit, - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_fields->grid_start[0], &my_fields->grid_end[0], &j, &k, + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_fields->grid_start[0], &my_fields->grid_end[0], &idx_range.jp1, &idx_range.kp1, &my_chemistry->primordial_chemistry, &anydust, kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], kcr_buf.data[ColRecRxnLUT::k11], kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], @@ -450,7 +451,7 @@ inline void wrapped_step_rate_g_( } inline void wrapped_step_rate_newton_raphson_( - int imetal, int j, int k, int iter, double dom, double* comp1, + int imetal, IndexRange idx_range, int iter, double dom, double* comp1, double* comp2, double chunit, double dx_cgs, double c_ljeans, double* dtit, double* p2d, double* tgas, double* tdust, double* metallicity, double* dust2gas, double* rhoH, double* mmw, double* h2dust, double* edot, @@ -542,7 +543,7 @@ inline void wrapped_step_rate_newton_raphson_( my_fields->RT_HDI_dissociation_rate, &my_chemistry->radiative_transfer_metal_ionization, my_fields->RT_CI_ionization_rate, my_fields->RT_OI_ionization_rate, &my_chemistry->radiative_transfer_metal_dissociation, my_fields->RT_CO_dissociation_rate, my_fields->RT_OH_dissociation_rate, my_fields->RT_H2O_dissociation_rate, &my_chemistry->radiative_transfer_use_H2_shielding, &my_chemistry->use_isrf_field, my_fields->isrf_habing, &my_chemistry->H2_custom_shielding, my_fields->H2_custom_shielding_factor, - &j, &k, &iter, &dom, comp1, + &idx_range.jp1, &idx_range.kp1, &iter, &dom, comp1, comp2, &internalu.coolunit, &internalu.tbase1, &internalu.xbase1, &chunit, &dx_cgs, &c_ljeans, logTlininterp_buf.indixe, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.logtem, logTlininterp_buf.tdef, dtit, p2d, tgas, cool1dmulti_buf.tgasold, tdust, metallicity, dust2gas, diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 8f2c056eb..72c9da46d 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -355,7 +355,7 @@ int solve_rate_cool_g( // Compute the cooling rate, tgas, tdust, and metallicity for this row wrapped_cool1d_multi_g_( - imetal, j, k, iter, &comp1, &comp2, edot.data(), tgas.data(), + imetal, idx_range, iter, &comp1, &comp2, edot.data(), tgas.data(), mmw.data(), p2d.data(), tdust.data(), metallicity.data(), dust2gas.data(), rhoH.data(), itmask.data(), itmask_metal.data(), my_chemistry, my_rates, my_fields, *my_uvb_rates, internalu, @@ -377,7 +377,7 @@ int solve_rate_cool_g( // (maybe should add itmask to this call) wrapped_lookup_cool_rates1d_g_( - j, k, anydust, tgas.data(), mmw.data(), + idx_range, anydust, tgas.data(), mmw.data(), tdust.data(), dust2gas.data(), k13dd.data(), h2dust.data(), dom, dx_cgs, c_ljeans, itmask.data(), itmask_metal.data(), imetal, rhoH.data(), dt, my_chemistry, my_rates, my_fields, @@ -389,7 +389,7 @@ int solve_rate_cool_g( // (should add itmask to this call) wrapped_rate_timestep_g_( - dedot.data(), HIdot.data(), anydust, j, k, h2dust.data(), + dedot.data(), HIdot.data(), anydust, idx_range, h2dust.data(), rhoH.data(), itmask.data(), edot.data(), chunit, dom, my_chemistry, my_fields, *my_uvb_rates, kcr_buf, kshield_buf, chemheatrates_buf @@ -566,7 +566,7 @@ int solve_rate_cool_g( // itmask) wrapped_step_rate_g_( - dtit.data(), j, k, anydust, h2dust.data(), rhoH.data(), + dtit.data(), idx_range, anydust, h2dust.data(), rhoH.data(), dedot_prev.data(), HIdot_prev.data(), itmask.data(), itmask_metal.data(), imetal, my_chemistry, my_fields, *my_uvb_rates, grain_growth_rates, species_tmpdens, kcr_buf, @@ -578,7 +578,7 @@ int solve_rate_cool_g( // itmask_nr) wrapped_step_rate_newton_raphson_( - imetal, j, k, iter, dom, &comp1, &comp2, chunit, dx_cgs, + imetal, idx_range, iter, dom, &comp1, &comp2, chunit, dx_cgs, c_ljeans, dtit.data(), p2d.data(), tgas.data(), tdust.data(), metallicity.data(), dust2gas.data(), rhoH.data(), mmw.data(), h2dust.data(), edot.data(), anydust, itmask_nr.data(), @@ -609,7 +609,7 @@ int solve_rate_cool_g( // If all cells are done (on this slice), break out of subcycle loop if (std::fabs(dt-ttmin) < tolerance*dt) { break; } - } // subcycle iteration loop (for current row) + } // subcycle iteration loop (for current idx_range) // review number of iterations that were spent in the subcycle loop From 2a0f2298932d9f5439b00de49967657cb58cef3a Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 14:59:15 -0500 Subject: [PATCH 062/101] where available, prefer `IndexRange` to pass i_start and i_end (rather than using `grackle_field_data`'s `grid_start[0]` and `grid_end[0]` --- src/clib/fortran_func_wrappers.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/clib/fortran_func_wrappers.hpp b/src/clib/fortran_func_wrappers.hpp index 9d67c2c63..31ba34cc2 100644 --- a/src/clib/fortran_func_wrappers.hpp +++ b/src/clib/fortran_func_wrappers.hpp @@ -73,7 +73,7 @@ inline void wrapped_cool1d_multi_g_( &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, &internalu.extfields_in_comoving, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_chemistry->dust_recombination_cooling, - &my_fields->grid_rank, &my_fields->grid_start[0], &my_fields->grid_end[0], &idx_range.jp1, &idx_range.kp1, &my_chemistry->ih2co, &my_chemistry->ipiht, &iter, &my_chemistry->photoelectric_heating, + &my_fields->grid_rank, &idx_range.i_start, &idx_range.i_end, &idx_range.jp1, &idx_range.kp1, &my_chemistry->ih2co, &my_chemistry->ipiht, &iter, &my_chemistry->photoelectric_heating, &internalu.a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, &internalu.utem, &internalu.uxyz, &internalu.a_units, &internalu.urho, &internalu.tbase1, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, @@ -194,7 +194,7 @@ inline void wrapped_lookup_cool_rates1d_g_( ) { FORTRAN_NAME(lookup_cool_rates1d_g)(&my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &my_chemistry->NumberOfTemperatureBins, &idx_range.jp1, &idx_range.kp1, - &my_fields->grid_start[0], &my_fields->grid_end[0], &my_chemistry->three_body_rate, + &idx_range.i_start, &idx_range.i_end, &my_chemistry->three_body_rate, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->primordial_chemistry, &anydust, &my_chemistry->H2_self_shielding, &my_chemistry->self_shielding_method, tgas1d, mmw, my_fields->density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, @@ -333,7 +333,7 @@ inline void wrapped_rate_timestep_g_( dedot, HIdot, &my_chemistry->primordial_chemistry, &anydust, my_fields->e_density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, my_fields->density, my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_fields->grid_start[0], &my_fields->grid_end[0], &idx_range.jp1, &idx_range.kp1, + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &idx_range.i_start, &idx_range.i_end, &idx_range.jp1, &idx_range.kp1, kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], kcr_buf.data[ColRecRxnLUT::k11], kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], &my_uvb_rates.k24, &my_uvb_rates.k25, &my_uvb_rates.k26, &my_uvb_rates.k27, &my_uvb_rates.k28, &my_uvb_rates.k29, &my_uvb_rates.k30, @@ -393,7 +393,7 @@ inline void wrapped_step_rate_g_( ) { FORTRAN_NAME(step_rate_g)(my_fields->e_density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, my_fields->density, my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, dtit, - &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_fields->grid_start[0], &my_fields->grid_end[0], &idx_range.jp1, &idx_range.kp1, + &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &idx_range.i_start, &idx_range.i_end, &idx_range.jp1, &idx_range.kp1, &my_chemistry->primordial_chemistry, &anydust, kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], kcr_buf.data[ColRecRxnLUT::k11], kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], @@ -468,7 +468,7 @@ inline void wrapped_step_rate_newton_raphson_( FORTRAN_NAME(step_rate_newton_raphson)(&my_chemistry->with_radiative_cooling, my_fields->density, my_fields->internal_energy, my_fields->x_velocity, my_fields->y_velocity, my_fields->z_velocity, my_fields->e_density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, &internalu.extfields_in_comoving, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, - &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &my_fields->grid_start[0], &my_fields->grid_end[0], &my_chemistry->ih2co, &my_chemistry->ipiht, + &my_chemistry->dust_chemistry, &my_chemistry->use_dust_density_field, &idx_range.i_start, &idx_range.i_end, &my_chemistry->ih2co, &my_chemistry->ipiht, &my_chemistry->dust_recombination_cooling, &my_chemistry->photoelectric_heating, &internalu.a_value, &my_chemistry->TemperatureStart, &my_chemistry->TemperatureEnd, &internalu.utem, &internalu.uxyz, &internalu.a_units, &internalu.urho, &internalu.tbase1, &my_chemistry->Gamma, &my_chemistry->HydrogenFractionByMass, &my_chemistry->SolarMetalFractionByMass, &my_chemistry->local_dust_to_gas_ratio, my_rates->k1, my_rates->k2, my_rates->k3, my_rates->k4, my_rates->k5, my_rates->k6, my_rates->k7, my_rates->k8, my_rates->k9, From ddd2a8ee949e61eeb6590ab6dcb3ef567fc3f81d Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 15:51:26 -0500 Subject: [PATCH 063/101] solve_rate_cool_g: shift error & warning messages to print 0-based index values for j and k --- src/clib/solve_rate_cool_g-cpp.C | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 72c9da46d..30556498e 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -619,10 +619,8 @@ int solve_rate_cool_g( printf("inside if statement solve rate cool: %d %d\n", my_fields->grid_start[0], my_fields->grid_end[0]); - eprintf("MULTI_COOL iter > %d at j,k = %d %d\n", - my_chemistry->max_iterations, - j, - k); + eprintf("MULTI_COOL iter > %d at j_0based,k_0based = %d %d\n", + my_chemistry->max_iterations, idx_range.j, idx_range.k); printf("FATAL error (2) in MULTI_COOL\n"); printf("( dt = %.17e ttmin = %.17e )", dt, ttmin); grackle::impl::print_contiguous_row_( @@ -647,7 +645,8 @@ int solve_rate_cool_g( if (iter > my_chemistry->max_iterations/2) { // WARNING_MESSAGE OMP_PRAGMA_CRITICAL { - eprintf("MULTI_COOL iter,j,k = %d %d %d\n", iter, j, k); + eprintf("MULTI_COOL iter,j_0based,k_0based = %d %d %d\n", + iter, idx_range.j, idx_range.k); } } From 37dede7fa747e8500697535a6e4f531da7eeaec7 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 16:12:24 -0500 Subject: [PATCH 064/101] solve_rate_cool_g: all occurences of `j` and `k` now refer to the 0-based index value (we have removed all previously associated `-1` values) --- src/clib/solve_rate_cool_g-cpp.C | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 30556498e..4515a4017 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -293,8 +293,8 @@ int solve_rate_cool_g( for (int t = 0; t < idx_helper.outer_ind_size; t++) { // construct an index-range corresponding to "i-slice" const IndexRange idx_range = make_idx_range_(t, &idx_helper); - const int k = idx_range.kp1; // use 1-based index for now - const int j = idx_range.jp1; // use 1-based index for now + const int k = idx_range.k; // use 0-based index + const int j = idx_range.j; // use 0-based index // tolerance = 1.0e-06_DKIND * dt @@ -310,7 +310,7 @@ int solve_rate_cool_g( if (my_chemistry->use_radiative_transfer == 1) { if (my_chemistry->radiative_transfer_coupled_rate_solver == 1 && my_chemistry->radiative_transfer_intermediate_step == 1) { for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - if (kphHI(i-1,j-1,k-1) > 0) { + if (kphHI(i-1,j,k) > 0) { itmask[i-1] = MASK_TRUE; } else { itmask[i-1] = MASK_FALSE; @@ -321,7 +321,7 @@ int solve_rate_cool_g( // Normal rate solver, but don't double count cells with radiation if (my_chemistry->radiative_transfer_coupled_rate_solver == 1 && my_chemistry->radiative_transfer_intermediate_step == 0) { for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - if (kphHI(i-1,j-1,k-1) > 0) { + if (kphHI(i-1,j,k) > 0) { itmask[i-1] = MASK_FALSE; } else { itmask[i-1] = MASK_TRUE; @@ -339,7 +339,7 @@ int solve_rate_cool_g( // A useful slice variable since we do this a lot for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - ddom[i-1] = d(i-1,j-1,k-1) * dom; + ddom[i-1] = d(i-1,j,k) * dom; } // ------------------ Loop over subcycles ---------------- @@ -440,15 +440,15 @@ int solve_rate_cool_g( // Bound from below to prevent numerical errors if (std::fabs(dedot[i-1]) < tiny8) - { dedot[i-1] = std::fmin(tiny_fortran_val,de(i-1,j-1,k-1)); } + { dedot[i-1] = std::fmin(tiny_fortran_val,de(i-1,j,k)); } if (std::fabs(HIdot[i-1]) < tiny8) - { HIdot[i-1] = std::fmin(tiny_fortran_val,HI(i-1,j-1,k-1)); } + { HIdot[i-1] = std::fmin(tiny_fortran_val,HI(i-1,j,k)); } // If the net rate is almost perfectly balanced then set // it to zero (since it is zero to available precision) - if (std::fmin(std::fabs(kcr_buf.data[ColRecRxnLUT::k1][i-1]* de(i-1,j-1,k-1)*HI(i-1,j-1,k-1)), - std::fabs(kcr_buf.data[ColRecRxnLUT::k2][i-1]*HII(i-1,j-1,k-1)*de(i-1,j-1,k-1)))/ + if (std::fmin(std::fabs(kcr_buf.data[ColRecRxnLUT::k1][i-1]* de(i-1,j,k)*HI(i-1,j,k)), + std::fabs(kcr_buf.data[ColRecRxnLUT::k2][i-1]*HII(i-1,j,k)*de(i-1,j,k)))/ std::fmax(std::fabs(dedot[i-1]),std::fabs(HIdot[i-1])) > 1.0e6) { dedot[i-1] = tiny8; @@ -469,8 +469,8 @@ int solve_rate_cool_g( // compute minimum rate timestep olddtit = dtit[i-1]; - dtit[i-1] = grackle::impl::fmin(std::fabs(0.1*de(i-1,j-1,k-1)/dedot[i-1]), - std::fabs(0.1*HI(i-1,j-1,k-1)/HIdot[i-1]), + dtit[i-1] = grackle::impl::fmin(std::fabs(0.1*de(i-1,j,k)/dedot[i-1]), + std::fabs(0.1*HI(i-1,j,k)/HIdot[i-1]), dt-ttot[i-1], 0.5*dt); if (ddom[i-1] > 1.e8 && @@ -480,7 +480,7 @@ int solve_rate_cool_g( // Hydrogen changes by 10% or less double Heq_div_dHeqdt = calc_Heq_div_dHeqdt_( my_chemistry, my_rates, dlogtem, logTlininterp_buf, - kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k22], d(i-1,j-1,k-1), tgas.data(), + kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k22], d(i-1,j,k), tgas.data(), p2d.data(), edot.data(), i ); @@ -492,7 +492,7 @@ int solve_rate_cool_g( } else if ((itmask_nr[i-1]!=MASK_FALSE) && (imp_eng[i-1]==0)) { - dtit[i-1] = grackle::impl::fmin(std::fabs(0.1*e(i-1,j-1,k-1)/edot[i-1]*d(i-1,j-1,k-1)), + dtit[i-1] = grackle::impl::fmin(std::fabs(0.1*e(i-1,j,k)/edot[i-1]*d(i-1,j,k)), dt-ttot[i-1], 0.5*dt); } else if ((itmask_nr[i-1]!=MASK_FALSE) && @@ -552,8 +552,8 @@ int solve_rate_cool_g( if (my_chemistry->with_radiative_cooling == 1) { for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { if (itmask[i-1] != MASK_FALSE) { - e(i-1,j-1,k-1) = e(i-1,j-1,k-1) + - (gr_float)(edot[i-1]/d(i-1,j-1,k-1)*dtit[i-1] ); + e(i-1,j,k) = e(i-1,j,k) + + (gr_float)(edot[i-1]/d(i-1,j,k)*dtit[i-1] ); } } From 72b3a2d673d4abced8fd901c665034978e19a3e8 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 16:21:13 -0500 Subject: [PATCH 065/101] solve_rate_cool_g: move declaration of `i` to within the init-statement of each for loop where it is used --- src/clib/solve_rate_cool_g-cpp.C | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 4515a4017..1885e9711 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -136,7 +136,7 @@ int solve_rate_cool_g( // Locals - int i, iter; + int iter; double ttmin, dom, energy, comp1, comp2; double chunit; double dlogtem, dx_cgs, c_ljeans, min_metallicity; @@ -234,7 +234,7 @@ int solve_rate_cool_g( //_// PORT: #ifdef _OPENMP //_// PORT: ! ierr is declared as shared and should be modified with atomic operation //_// PORT: !$omp parallel do schedule(runtime) private( - //_// PORT: !$omp& i, iter, + //_// PORT: !$omp& iter, //_// PORT: !$omp& ttmin, energy, comp1, comp2, //_// PORT: !$omp& dtit, ttot, p2d, tgas, //_// PORT: !$omp& tdust, metallicity, dust2gas, rhoH, mmw, @@ -300,7 +300,7 @@ int solve_rate_cool_g( // Initialize iteration mask to true for all cells. - for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { itmask[i-1] = MASK_TRUE; } @@ -309,7 +309,7 @@ int solve_rate_cool_g( // intermediate coupled chemistry / energy step if (my_chemistry->use_radiative_transfer == 1) { if (my_chemistry->radiative_transfer_coupled_rate_solver == 1 && my_chemistry->radiative_transfer_intermediate_step == 1) { - for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { if (kphHI(i-1,j,k) > 0) { itmask[i-1] = MASK_TRUE; } else { @@ -320,7 +320,7 @@ int solve_rate_cool_g( // Normal rate solver, but don't double count cells with radiation if (my_chemistry->radiative_transfer_coupled_rate_solver == 1 && my_chemistry->radiative_transfer_intermediate_step == 0) { - for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { if (kphHI(i-1,j,k) > 0) { itmask[i-1] = MASK_FALSE; } else { @@ -332,13 +332,13 @@ int solve_rate_cool_g( // Set time elapsed to zero for each cell in 1D section - for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { ttot[i-1] = 0.; } // A useful slice variable since we do this a lot - for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { ddom[i-1] = d(i-1,j,k) * dom; } @@ -346,7 +346,7 @@ int solve_rate_cool_g( for (iter = 1; iter<=(my_chemistry->max_iterations); iter++) { - for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { if (itmask[i-1] != MASK_FALSE) { dtit[i-1] = huge8; } @@ -403,7 +403,7 @@ int solve_rate_cool_g( std::memcpy(itmask_tmp.data(), itmask.data(), sizeof(gr_mask_type)*my_fields->grid_dimension[0]); std::memcpy(itmask_nr.data(), itmask.data(), sizeof(gr_mask_type)*my_fields->grid_dimension[0]); - for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { if ( itmask_tmp[i-1] != MASK_FALSE ) { if ( ( (imetal == 0) @@ -421,7 +421,7 @@ int solve_rate_cool_g( } } - for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { if (itmask_nr[i-1] != MASK_FALSE) { if ( (my_chemistry->with_radiative_cooling == 1) && (my_chemistry->primordial_chemistry > 1) && ((ddom[i-1] > 1.e7) @@ -435,7 +435,7 @@ int solve_rate_cool_g( // Find timestep that keeps relative chemical changes below 10% - for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { if (itmask[i-1] != MASK_FALSE) { // Bound from below to prevent numerical errors @@ -508,7 +508,7 @@ int solve_rate_cool_g( // Compute maximum timestep for cooling/heating - for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { if (itmask[i-1] != MASK_FALSE) { // Set energy per unit volume of this cell based in the pressure // (the gamma used here is the right one even for H2 since p2d @@ -550,7 +550,7 @@ int solve_rate_cool_g( // Update total and gas energy if (my_chemistry->with_radiative_cooling == 1) { - for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { if (itmask[i-1] != MASK_FALSE) { e(i-1,j,k) = e(i-1,j,k) + (gr_float)(edot[i-1]/d(i-1,j,k)*dtit[i-1] ); @@ -591,7 +591,7 @@ int solve_rate_cool_g( } // return itmask - for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { itmask[i-1] = itmask_tmp[i-1]; } @@ -599,7 +599,7 @@ int solve_rate_cool_g( // minimum elapsed time step in this row ttmin = huge8; - for (i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { + for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { ttot[i-1] = std::fmin(ttot[i-1] + dtit[i-1], dt); if (std::fabs(dt-ttot[i-1]) < tolerance*dt) { itmask[i-1] = MASK_FALSE; } From 1073a2d6cf577f47af25b59dcab4e1838f949bd9 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 16:32:11 -0500 Subject: [PATCH 066/101] solve_rate_cool_g: modified the first 6 for-loops (of 13) that iterate over i so that they treat i as a 0-based index --- src/clib/solve_rate_cool_g-cpp.C | 34 ++++++++++++++++---------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 1885e9711..8b29cca38 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -300,8 +300,8 @@ int solve_rate_cool_g( // Initialize iteration mask to true for all cells. - for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - itmask[i-1] = MASK_TRUE; + for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + itmask[i] = MASK_TRUE; } // If we are using coupled radiation with intermediate stepping, @@ -309,22 +309,22 @@ int solve_rate_cool_g( // intermediate coupled chemistry / energy step if (my_chemistry->use_radiative_transfer == 1) { if (my_chemistry->radiative_transfer_coupled_rate_solver == 1 && my_chemistry->radiative_transfer_intermediate_step == 1) { - for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - if (kphHI(i-1,j,k) > 0) { - itmask[i-1] = MASK_TRUE; + for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + if (kphHI(i,j,k) > 0) { + itmask[i] = MASK_TRUE; } else { - itmask[i-1] = MASK_FALSE; + itmask[i] = MASK_FALSE; } } } // Normal rate solver, but don't double count cells with radiation if (my_chemistry->radiative_transfer_coupled_rate_solver == 1 && my_chemistry->radiative_transfer_intermediate_step == 0) { - for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - if (kphHI(i-1,j,k) > 0) { - itmask[i-1] = MASK_FALSE; + for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + if (kphHI(i,j,k) > 0) { + itmask[i] = MASK_FALSE; } else { - itmask[i-1] = MASK_TRUE; + itmask[i] = MASK_TRUE; } } } @@ -332,23 +332,23 @@ int solve_rate_cool_g( // Set time elapsed to zero for each cell in 1D section - for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - ttot[i-1] = 0.; + for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + ttot[i] = 0.; } // A useful slice variable since we do this a lot - for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - ddom[i-1] = d(i-1,j,k) * dom; + for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + ddom[i] = d(i,j,k) * dom; } // ------------------ Loop over subcycles ---------------- for (iter = 1; iter<=(my_chemistry->max_iterations); iter++) { - for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - if (itmask[i-1] != MASK_FALSE) { - dtit[i-1] = huge8; + for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + if (itmask[i] != MASK_FALSE) { + dtit[i] = huge8; } } From 6a6cae20f4b239e2126c783d5bc5fc006e9fb0a0 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 16:39:22 -0500 Subject: [PATCH 067/101] solve_rate_cool_g: modified the first 7th and 8th for-loops (of 13) that iterate over i so that they treat i as a 0-based index --- src/clib/solve_rate_cool_g-cpp.C | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 8b29cca38..c28cdd129 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -403,32 +403,32 @@ int solve_rate_cool_g( std::memcpy(itmask_tmp.data(), itmask.data(), sizeof(gr_mask_type)*my_fields->grid_dimension[0]); std::memcpy(itmask_nr.data(), itmask.data(), sizeof(gr_mask_type)*my_fields->grid_dimension[0]); - for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - if ( itmask_tmp[i-1] != MASK_FALSE ) { + for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + if ( itmask_tmp[i] != MASK_FALSE ) { if ( ( (imetal == 0) - && (ddom[i-1] < 1.e8) ) + && (ddom[i] < 1.e8) ) || ( (imetal == 1) - && ( ( (metallicity[i-1] <= min_metallicity) - && (ddom[i-1] < 1.e8) ) - || ( (metallicity[i-1] > min_metallicity) - && (ddom[i-1] < 1.e6) ) ) ) ) { - itmask_nr[i-1] = MASK_FALSE; + && ( ( (metallicity[i] <= min_metallicity) + && (ddom[i] < 1.e8) ) + || ( (metallicity[i] > min_metallicity) + && (ddom[i] < 1.e6) ) ) ) ) { + itmask_nr[i] = MASK_FALSE; } else { - itmask[i-1] = MASK_FALSE; + itmask[i] = MASK_FALSE; } } } - for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - if (itmask_nr[i-1] != MASK_FALSE) { + for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + if (itmask_nr[i] != MASK_FALSE) { if ( (my_chemistry->with_radiative_cooling == 1) && (my_chemistry->primordial_chemistry > 1) && - ((ddom[i-1] > 1.e7) - && (tgas[i-1] > 1650.e0)) ) { - imp_eng[i-1] = 1; + ((ddom[i] > 1.e7) + && (tgas[i] > 1650.e0)) ) { + imp_eng[i] = 1; } else { - imp_eng[i-1] = 0; + imp_eng[i] = 0; } } } From f1bad9c6c036b5d07483055198638d05ed88cb95 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 16:47:55 -0500 Subject: [PATCH 068/101] solve_rate_cool_g: modified the 9th for-loop (of 13) that iterate over i so that it treats i as a 0-based index --- src/clib/solve_rate_cool_g-cpp.C | 63 ++++++++++++++++---------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index c28cdd129..02c5f22bc 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -435,24 +435,24 @@ int solve_rate_cool_g( // Find timestep that keeps relative chemical changes below 10% - for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - if (itmask[i-1] != MASK_FALSE) { + for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + if (itmask[i] != MASK_FALSE) { // Bound from below to prevent numerical errors - if (std::fabs(dedot[i-1]) < tiny8) - { dedot[i-1] = std::fmin(tiny_fortran_val,de(i-1,j,k)); } - if (std::fabs(HIdot[i-1]) < tiny8) - { HIdot[i-1] = std::fmin(tiny_fortran_val,HI(i-1,j,k)); } + if (std::fabs(dedot[i]) < tiny8) + { dedot[i] = std::fmin(tiny_fortran_val,de(i,j,k)); } + if (std::fabs(HIdot[i]) < tiny8) + { HIdot[i] = std::fmin(tiny_fortran_val,HI(i,j,k)); } // If the net rate is almost perfectly balanced then set // it to zero (since it is zero to available precision) - if (std::fmin(std::fabs(kcr_buf.data[ColRecRxnLUT::k1][i-1]* de(i-1,j,k)*HI(i-1,j,k)), - std::fabs(kcr_buf.data[ColRecRxnLUT::k2][i-1]*HII(i-1,j,k)*de(i-1,j,k)))/ - std::fmax(std::fabs(dedot[i-1]),std::fabs(HIdot[i-1])) > + if (std::fmin(std::fabs(kcr_buf.data[ColRecRxnLUT::k1][i]* de(i,j,k)*HI(i,j,k)), + std::fabs(kcr_buf.data[ColRecRxnLUT::k2][i]*HII(i,j,k)*de(i,j,k)))/ + std::fmax(std::fabs(dedot[i]),std::fabs(HIdot[i])) > 1.0e6) { - dedot[i-1] = tiny8; - HIdot[i-1] = tiny8; + dedot[i] = tiny8; + HIdot[i] = tiny8; } // If the iteration count is high then take the smaller of @@ -462,45 +462,46 @@ int solve_rate_cool_g( // individual terms (which all nearly cancel). if (iter > 50) { - dedot[i-1] = std::fmin(std::fabs(dedot[i-1]), std::fabs(dedot_prev[i-1])); - HIdot[i-1] = std::fmin(std::fabs(HIdot[i-1]), std::fabs(HIdot_prev[i-1])); + dedot[i] = std::fmin(std::fabs(dedot[i]), std::fabs(dedot_prev[i])); + HIdot[i] = std::fmin(std::fabs(HIdot[i]), std::fabs(HIdot_prev[i])); } // compute minimum rate timestep - olddtit = dtit[i-1]; - dtit[i-1] = grackle::impl::fmin(std::fabs(0.1*de(i-1,j,k)/dedot[i-1]), - std::fabs(0.1*HI(i-1,j,k)/HIdot[i-1]), - dt-ttot[i-1], 0.5*dt); + olddtit = dtit[i]; + dtit[i] = grackle::impl::fmin(std::fabs(0.1*de(i,j,k)/dedot[i]), + std::fabs(0.1*HI(i,j,k)/HIdot[i]), + dt-ttot[i], 0.5*dt); - if (ddom[i-1] > 1.e8 && - edot[i-1] > 0. && + if (ddom[i] > 1.e8 && + edot[i] > 0. && my_chemistry->primordial_chemistry > 1) { // here, we ensure that that the equilibrium mass density of // Hydrogen changes by 10% or less double Heq_div_dHeqdt = calc_Heq_div_dHeqdt_( my_chemistry, my_rates, dlogtem, logTlininterp_buf, - kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k22], d(i-1,j,k), tgas.data(), - p2d.data(), edot.data(), i + kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k22], d(i,j,k), tgas.data(), + p2d.data(), edot.data(), + i+1 // the function currently expects i to be a 1-based index ); - dtit[i-1] = std::fmin(dtit[i-1], 0.1*Heq_div_dHeqdt); + dtit[i] = std::fmin(dtit[i], 0.1*Heq_div_dHeqdt); } if (iter>10LL) { - dtit[i-1] = std::fmin(olddtit*1.5, dtit[i-1]); + dtit[i] = std::fmin(olddtit*1.5, dtit[i]); } - } else if ((itmask_nr[i-1]!=MASK_FALSE) && - (imp_eng[i-1]==0)) { - dtit[i-1] = grackle::impl::fmin(std::fabs(0.1*e(i-1,j,k)/edot[i-1]*d(i-1,j,k)), - dt-ttot[i-1], 0.5*dt); + } else if ((itmask_nr[i]!=MASK_FALSE) && + (imp_eng[i]==0)) { + dtit[i] = grackle::impl::fmin(std::fabs(0.1*e(i,j,k)/edot[i]*d(i,j,k)), + dt-ttot[i], 0.5*dt); - } else if ((itmask_nr[i-1]!=MASK_FALSE) && - (imp_eng[i-1]==1)) { - dtit[i-1] = dt - ttot[i-1]; + } else if ((itmask_nr[i]!=MASK_FALSE) && + (imp_eng[i]==1)) { + dtit[i] = dt - ttot[i]; } else { - dtit[i-1] = dt; + dtit[i] = dt; } } From 769df115ea2236bd47fb0dc63bb47a2f67d26828 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 16:54:10 -0500 Subject: [PATCH 069/101] solve_rate_cool_g: modified the 10th for-loop (of 13) that iterates over i so that it treats i as a 0-based index --- src/clib/solve_rate_cool_g-cpp.C | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 02c5f22bc..88cdcb9af 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -509,39 +509,39 @@ int solve_rate_cool_g( // Compute maximum timestep for cooling/heating - for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - if (itmask[i-1] != MASK_FALSE) { + for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + if (itmask[i] != MASK_FALSE) { // Set energy per unit volume of this cell based in the pressure // (the gamma used here is the right one even for H2 since p2d // is calculated with this gamma). - energy = std::fmax(p2d[i-1]/(my_chemistry->Gamma-1.), tiny8); + energy = std::fmax(p2d[i]/(my_chemistry->Gamma-1.), tiny8); // If the temperature is at the bottom of the temperature look-up // table and edot < 0, then shut off the cooling. - if (tgas[i-1] <= 1.01*my_chemistry->TemperatureStart && - edot[i-1] < 0.) - { edot[i-1] = tiny8; } - if (std::fabs(edot[i-1]) < tiny8) { edot[i-1] = tiny8; } + if (tgas[i] <= 1.01*my_chemistry->TemperatureStart && + edot[i] < 0.) + { edot[i] = tiny8; } + if (std::fabs(edot[i]) < tiny8) { edot[i] = tiny8; } // Compute timestep for 10% change - dtit[i-1] = grackle::impl::fmin((double)(std::fabs(0.1* - energy/edot[i-1]) ), - dt-ttot[i-1], dtit[i-1]); + dtit[i] = grackle::impl::fmin((double)(std::fabs(0.1* + energy/edot[i]) ), + dt-ttot[i], dtit[i]); - if (dtit[i-1] != dtit[i-1]) { + if (dtit[i] != dtit[i]) { OMP_PRAGMA_CRITICAL { eprintf("HUGE dtit :: %g %g %g %g %g %g %g\n", energy, - edot [ i-1 ], - dtit [ i-1 ], + edot [ i ], + dtit [ i ], dt, - ttot [ i-1 ], - std::fabs ( 0.1 * energy / edot [ i-1 ] ), - (double) ( std::fabs ( 0.1 * energy / edot [ i-1 ] ) )); + ttot [ i ], + std::fabs ( 0.1 * energy / edot [ i ] ), + (double) ( std::fabs ( 0.1 * energy / edot [ i ] ) )); } } From f25e1c124603f8e094b349eaf7a905c2141e8674 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 17:01:40 -0500 Subject: [PATCH 070/101] solve_rate_cool_g: modified the final 3 for-loops (of 13) that iterate over i so that they treat i as a 0-based index --- src/clib/solve_rate_cool_g-cpp.C | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 88cdcb9af..33759901d 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -551,10 +551,10 @@ int solve_rate_cool_g( // Update total and gas energy if (my_chemistry->with_radiative_cooling == 1) { - for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - if (itmask[i-1] != MASK_FALSE) { - e(i-1,j,k) = e(i-1,j,k) + - (gr_float)(edot[i-1]/d(i-1,j,k)*dtit[i-1] ); + for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + if (itmask[i] != MASK_FALSE) { + e(i,j,k) = e(i,j,k) + + (gr_float)(edot[i]/d(i,j,k)*dtit[i] ); } } @@ -592,19 +592,19 @@ int solve_rate_cool_g( } // return itmask - for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - itmask[i-1] = itmask_tmp[i-1]; + for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + itmask[i] = itmask_tmp[i]; } // Add the timestep to the elapsed time for each cell and find // minimum elapsed time step in this row ttmin = huge8; - for (int i = my_fields->grid_start[0] + 1; i<=(my_fields->grid_end[0] + 1); i++) { - ttot[i-1] = std::fmin(ttot[i-1] + dtit[i-1], dt); - if (std::fabs(dt-ttot[i-1]) < - tolerance*dt) { itmask[i-1] = MASK_FALSE; } - if (ttot[i-1]grid_start[0]; i<=my_fields->grid_end[0]; i++) { + ttot[i] = std::fmin(ttot[i] + dtit[i], dt); + if (std::fabs(dt-ttot[i]) < + tolerance*dt) { itmask[i] = MASK_FALSE; } + if (ttot[i] Date: Tue, 21 Jan 2025 17:29:05 -0500 Subject: [PATCH 071/101] solve_rate_cool_g: refactored the helper function, `calc_Heq_div_dHeqdt_` treat the `i` variable as a 0-based index --- src/clib/solve_rate_cool_g-cpp.C | 37 ++++++++++++++++---------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 33759901d..6ed62e53c 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -37,7 +37,7 @@ /// @param tgas, p2d, edot 1D arrays containing local values of temperature, /// pressure divided by density, and the time derivative of internal energy. /// @param i Specifies the index of the relevant zone in the 1D array. (**BE -/// AWARE:** this is a 1-based index for historical reasons) +/// AWARE:** this is a 0-based index) /// /// @note /// The `static` annotation indicates that this function is only visible to the @@ -70,32 +70,32 @@ static double calc_Heq_div_dHeqdt_( // de / dt = edot // Now we use our estimate of dT/de to get the estimated // difference in the equilibrium - double eqt2 = std::fmin(std::log(tgas[i-1]) + 0.1*dlogtem, logTlininterp_buf.t2[i-1]); - double eqtdef = (eqt2 - logTlininterp_buf.t1[i-1])/(logTlininterp_buf.t2[i-1] - logTlininterp_buf.t1[i-1]); - double eqk222 = my_rates->k22[logTlininterp_buf.indixe[i-1]-1] + - (my_rates->k22[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k22[logTlininterp_buf.indixe[i-1]-1])*eqtdef; - double eqk132 = my_rates->k13[logTlininterp_buf.indixe[i-1]-1] + - (my_rates->k13[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k13[logTlininterp_buf.indixe[i-1]-1])*eqtdef; + double eqt2 = std::fmin(std::log(tgas[i]) + 0.1*dlogtem, logTlininterp_buf.t2[i]); + double eqtdef = (eqt2 - logTlininterp_buf.t1[i])/(logTlininterp_buf.t2[i] - logTlininterp_buf.t1[i]); + double eqk222 = my_rates->k22[logTlininterp_buf.indixe[i]-1] + + (my_rates->k22[logTlininterp_buf.indixe[i]+1-1] -my_rates->k22[logTlininterp_buf.indixe[i]-1])*eqtdef; + double eqk132 = my_rates->k13[logTlininterp_buf.indixe[i]-1] + + (my_rates->k13[logTlininterp_buf.indixe[i]+1-1] -my_rates->k13[logTlininterp_buf.indixe[i]-1])*eqtdef; double heq2 = (-1. / (4.*eqk222)) * (eqk132- std::sqrt(8.*eqk132*eqk222* my_chemistry->HydrogenFractionByMass*local_rho+ std::pow(eqk132,2.))); - double eqt1 = std::fmax(std::log(tgas[i-1]) - 0.1*dlogtem, logTlininterp_buf.t1[i-1]); - eqtdef = (eqt1 - logTlininterp_buf.t1[i-1])/(logTlininterp_buf.t2[i-1] - logTlininterp_buf.t1[i-1]); - double eqk221 = my_rates->k22[logTlininterp_buf.indixe[i-1]-1] + - (my_rates->k22[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k22[logTlininterp_buf.indixe[i-1]-1])*eqtdef; - double eqk131 = my_rates->k13[logTlininterp_buf.indixe[i-1]-1] + - (my_rates->k13[logTlininterp_buf.indixe[i-1]+1-1] -my_rates->k13[logTlininterp_buf.indixe[i-1]-1])*eqtdef; + double eqt1 = std::fmax(std::log(tgas[i]) - 0.1*dlogtem, logTlininterp_buf.t1[i]); + eqtdef = (eqt1 - logTlininterp_buf.t1[i])/(logTlininterp_buf.t2[i] - logTlininterp_buf.t1[i]); + double eqk221 = my_rates->k22[logTlininterp_buf.indixe[i]-1] + + (my_rates->k22[logTlininterp_buf.indixe[i]+1-1] -my_rates->k22[logTlininterp_buf.indixe[i]-1])*eqtdef; + double eqk131 = my_rates->k13[logTlininterp_buf.indixe[i]-1] + + (my_rates->k13[logTlininterp_buf.indixe[i]+1-1] -my_rates->k13[logTlininterp_buf.indixe[i]-1])*eqtdef; double heq1 = (-1. / (4.*eqk221)) * (eqk131- std::sqrt(8.*eqk131*eqk221* my_chemistry->HydrogenFractionByMass*local_rho+std::pow(eqk131,2.))); double dheq = (std::fabs(heq2-heq1)/(std::exp(eqt2) - std::exp(eqt1))) - * (tgas[i-1]/p2d[i-1]) * edot[i-1]; - double heq = (-1. / (4.*k22[i-1])) * (k13[i-1]- - std::sqrt(8.*k13[i-1]*k22[i-1]* - my_chemistry->HydrogenFractionByMass*local_rho+std::pow(k13[i-1],2.))); + * (tgas[i]/p2d[i]) * edot[i]; + double heq = (-1. / (4.*k22[i])) * (k13[i]- + std::sqrt(8.*k13[i]*k22[i]* + my_chemistry->HydrogenFractionByMass*local_rho+std::pow(k13[i],2.))); return heq / dheq; } @@ -481,8 +481,7 @@ int solve_rate_cool_g( double Heq_div_dHeqdt = calc_Heq_div_dHeqdt_( my_chemistry, my_rates, dlogtem, logTlininterp_buf, kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k22], d(i,j,k), tgas.data(), - p2d.data(), edot.data(), - i+1 // the function currently expects i to be a 1-based index + p2d.data(), edot.data(), i ); dtit[i] = std::fmin(dtit[i], 0.1*Heq_div_dHeqdt); From 1b2d6fa52d39fb18a8be96abcd7c58f8f564155a Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 17:35:31 -0500 Subject: [PATCH 072/101] solve_rate_cool_g: for loops over i now use idx_range.i_(start|stop) for limits --- src/clib/solve_rate_cool_g-cpp.C | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 6ed62e53c..aeb02c10f 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -300,7 +300,7 @@ int solve_rate_cool_g( // Initialize iteration mask to true for all cells. - for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { itmask[i] = MASK_TRUE; } @@ -309,7 +309,7 @@ int solve_rate_cool_g( // intermediate coupled chemistry / energy step if (my_chemistry->use_radiative_transfer == 1) { if (my_chemistry->radiative_transfer_coupled_rate_solver == 1 && my_chemistry->radiative_transfer_intermediate_step == 1) { - for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { if (kphHI(i,j,k) > 0) { itmask[i] = MASK_TRUE; } else { @@ -320,7 +320,7 @@ int solve_rate_cool_g( // Normal rate solver, but don't double count cells with radiation if (my_chemistry->radiative_transfer_coupled_rate_solver == 1 && my_chemistry->radiative_transfer_intermediate_step == 0) { - for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { if (kphHI(i,j,k) > 0) { itmask[i] = MASK_FALSE; } else { @@ -332,13 +332,13 @@ int solve_rate_cool_g( // Set time elapsed to zero for each cell in 1D section - for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { ttot[i] = 0.; } // A useful slice variable since we do this a lot - for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { ddom[i] = d(i,j,k) * dom; } @@ -346,7 +346,7 @@ int solve_rate_cool_g( for (iter = 1; iter<=(my_chemistry->max_iterations); iter++) { - for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { if (itmask[i] != MASK_FALSE) { dtit[i] = huge8; } @@ -403,7 +403,7 @@ int solve_rate_cool_g( std::memcpy(itmask_tmp.data(), itmask.data(), sizeof(gr_mask_type)*my_fields->grid_dimension[0]); std::memcpy(itmask_nr.data(), itmask.data(), sizeof(gr_mask_type)*my_fields->grid_dimension[0]); - for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { if ( itmask_tmp[i] != MASK_FALSE ) { if ( ( (imetal == 0) @@ -421,7 +421,7 @@ int solve_rate_cool_g( } } - for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { if (itmask_nr[i] != MASK_FALSE) { if ( (my_chemistry->with_radiative_cooling == 1) && (my_chemistry->primordial_chemistry > 1) && ((ddom[i] > 1.e7) @@ -435,7 +435,7 @@ int solve_rate_cool_g( // Find timestep that keeps relative chemical changes below 10% - for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { if (itmask[i] != MASK_FALSE) { // Bound from below to prevent numerical errors @@ -508,7 +508,7 @@ int solve_rate_cool_g( // Compute maximum timestep for cooling/heating - for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { if (itmask[i] != MASK_FALSE) { // Set energy per unit volume of this cell based in the pressure // (the gamma used here is the right one even for H2 since p2d @@ -550,7 +550,7 @@ int solve_rate_cool_g( // Update total and gas energy if (my_chemistry->with_radiative_cooling == 1) { - for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { if (itmask[i] != MASK_FALSE) { e(i,j,k) = e(i,j,k) + (gr_float)(edot[i]/d(i,j,k)*dtit[i] ); @@ -591,7 +591,7 @@ int solve_rate_cool_g( } // return itmask - for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { itmask[i] = itmask_tmp[i]; } @@ -599,7 +599,7 @@ int solve_rate_cool_g( // minimum elapsed time step in this row ttmin = huge8; - for (int i = my_fields->grid_start[0]; i<=my_fields->grid_end[0]; i++) { + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { ttot[i] = std::fmin(ttot[i] + dtit[i], dt); if (std::fabs(dt-ttot[i]) < tolerance*dt) { itmask[i] = MASK_FALSE; } From d4be5e02d2f182dec0df77bba5150a559ed4660e Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 17:44:12 -0500 Subject: [PATCH 073/101] made some quantities computed outside the loop into constants. --- src/clib/solve_rate_cool_g-cpp.C | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index aeb02c10f..abd5192bb 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -137,9 +137,7 @@ int solve_rate_cool_g( // Locals int iter; - double ttmin, dom, energy, comp1, comp2; - double chunit; - double dlogtem, dx_cgs, c_ljeans, min_metallicity; + double ttmin, energy, comp1, comp2; gr_float factor; // row temporaries @@ -194,16 +192,21 @@ int solve_rate_cool_g( } // ignore metal chemistry/cooling below this metallicity - min_metallicity = 1.e-9 / my_chemistry->SolarMetalFractionByMass; + const double min_metallicity = 1.e-9 / my_chemistry->SolarMetalFractionByMass; // Set units - dom = internalu_calc_dom_(internalu); - chunit = internalu_get_chunit_(internalu); - - dx_cgs = my_fields->grid_dx * internalu.xbase1; - c_ljeans = internalu_calc_coef_ljeans_(internalu, my_chemistry->Gamma); - - dlogtem = (std::log(my_chemistry->TemperatureEnd) - std::log(my_chemistry->TemperatureStart))/(double)(my_chemistry->NumberOfTemperatureBins-1 ); + const double dom = internalu_calc_dom_(internalu); + const double chunit = internalu_get_chunit_(internalu); + + const double dx_cgs = my_fields->grid_dx * internalu.xbase1; + const double c_ljeans = internalu_calc_coef_ljeans_(internalu, + my_chemistry->Gamma); + + const double dlogtem = ( + (std::log(my_chemistry->TemperatureEnd) - + std::log(my_chemistry->TemperatureStart)) / + (double)(my_chemistry->NumberOfTemperatureBins-1 ) + ); // We better make consistent at first GC202002 From 6bc2d486b83975ee18a30a9b401ea3196082d57e Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 17:50:44 -0500 Subject: [PATCH 074/101] solve_rate_cool_g: move declarations of some other scalar quantities to where they are used. --- src/clib/solve_rate_cool_g-cpp.C | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index abd5192bb..bcb18d7ca 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -137,12 +137,10 @@ int solve_rate_cool_g( // Locals int iter; - double ttmin, energy, comp1, comp2; - gr_float factor; + double ttmin, comp1, comp2; // row temporaries - double olddtit; std::vector dtit(my_fields->grid_dimension[0]); std::vector ttot(my_fields->grid_dimension[0]); std::vector p2d(my_fields->grid_dimension[0]); @@ -222,7 +220,7 @@ int solve_rate_cool_g( // Convert densities from comoving to proper if (internalu.extfields_in_comoving == 1) { - factor = (gr_float)(std::pow(internalu.a_value,(-3)) ); + gr_float factor = (gr_float)(std::pow(internalu.a_value,(-3)) ); wrapped_scale_fields_g_(imetal, factor, my_chemistry, my_fields); } @@ -238,11 +236,10 @@ int solve_rate_cool_g( //_// PORT: ! ierr is declared as shared and should be modified with atomic operation //_// PORT: !$omp parallel do schedule(runtime) private( //_// PORT: !$omp& iter, - //_// PORT: !$omp& ttmin, energy, comp1, comp2, + //_// PORT: !$omp& ttmin, comp1, comp2, //_// PORT: !$omp& dtit, ttot, p2d, tgas, //_// PORT: !$omp& tdust, metallicity, dust2gas, rhoH, mmw, //_// PORT: !$omp& ddom, - //_// PORT: !$omp& olddtit, //_// PORT: !$omp& dep, dedot,HIdot, dedot_prev, //_// PORT: !$omp& HIdot_prev, //_// PORT: !$omp& k13dd, h2dust, @@ -471,7 +468,7 @@ int solve_rate_cool_g( // compute minimum rate timestep - olddtit = dtit[i]; + double olddtit = dtit[i]; dtit[i] = grackle::impl::fmin(std::fabs(0.1*de(i,j,k)/dedot[i]), std::fabs(0.1*HI(i,j,k)/HIdot[i]), dt-ttot[i], 0.5*dt); @@ -517,7 +514,7 @@ int solve_rate_cool_g( // (the gamma used here is the right one even for H2 since p2d // is calculated with this gamma). - energy = std::fmax(p2d[i]/(my_chemistry->Gamma-1.), tiny8); + double energy = std::fmax(p2d[i]/(my_chemistry->Gamma-1.), tiny8); // If the temperature is at the bottom of the temperature look-up // table and edot < 0, then shut off the cooling. @@ -678,7 +675,7 @@ int solve_rate_cool_g( // Convert densities back to comoving from proper if (internalu.extfields_in_comoving == 1) { - factor = (gr_float)(std::pow(internalu.a_value,3) ); + gr_float factor = (gr_float)(std::pow(internalu.a_value,3) ); wrapped_scale_fields_g_(imetal, factor, my_chemistry, my_fields); } From 2e81fcd56624b9903c3c0a2bea64fac863482b14 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 19:18:29 -0500 Subject: [PATCH 075/101] solve_rate_cool_g: tweaked appearance of coupled-radiative-transfer logic (the logic is still the same, I just rearranged to an if-elseif statement and added more comments) --- src/clib/solve_rate_cool_g-cpp.C | 39 ++++++++++++++++---------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index bcb18d7ca..8a3920ab7 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -304,28 +304,29 @@ int solve_rate_cool_g( itmask[i] = MASK_TRUE; } - // If we are using coupled radiation with intermediate stepping, - // set iteration mask to include only cells with radiation in the - // intermediate coupled chemistry / energy step - if (my_chemistry->use_radiative_transfer == 1) { - if (my_chemistry->radiative_transfer_coupled_rate_solver == 1 && my_chemistry->radiative_transfer_intermediate_step == 1) { + // adjust iteration mask if the caller indicates that they're using + // Grackle in a coupled radiative-transfer/chemistry-energy calculation + // (that has intermediate steps) + if (my_chemistry->use_radiative_transfer == 1 && + my_chemistry->radiative_transfer_coupled_rate_solver == 1) { + // we only define behavior for radiative_transfer_intermediate_step + // values of 0 or 1 + + if (my_chemistry->radiative_transfer_intermediate_step == 1) { + // the caller has invoked this chemistry-energy solver as an + // intermediate step of a coupled radiative-transfer/chemistry-energy + // calculation and they only want the solver consider cells where + // the radiation is non-zero for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { - if (kphHI(i,j,k) > 0) { - itmask[i] = MASK_TRUE; - } else { - itmask[i] = MASK_FALSE; - } + itmask[i] = (kphHI(i,j,k) > 0) ? MASK_TRUE : MASK_FALSE; } - } - - // Normal rate solver, but don't double count cells with radiation - if (my_chemistry->radiative_transfer_coupled_rate_solver == 1 && my_chemistry->radiative_transfer_intermediate_step == 0) { + } else if (my_chemistry->radiative_transfer_intermediate_step == 0) { + // the caller has invoked this chemistry-energy solver outside + // of their coupled radiative-transfer/chemistry-energy calculation. + // They want to apply the solver to cells where radiation is 0 (i.e. + // locations where skipped by the coupled calculation) for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { - if (kphHI(i,j,k) > 0) { - itmask[i] = MASK_FALSE; - } else { - itmask[i] = MASK_TRUE; - } + itmask[i] = (kphHI(i,j,k) > 0) ? MASK_FALSE : MASK_TRUE; } } } From e315fc16c1f4c6eb15bb8d5e8516c2dd9eaa53ca Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 20:00:32 -0500 Subject: [PATCH 076/101] factor itmask modification logic out into its own function --- src/clib/solve_rate_cool_g-cpp.C | 80 +++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 8a3920ab7..3301a6197 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -100,6 +100,57 @@ static double calc_Heq_div_dHeqdt_( return heq / dheq; } +// ------------------------------------------------------------- + +/// Updates the iteration mask in the case where the user has specified that we +/// are using grackle as a part of a coupled radiative transfer calculation +/// +/// @param[out] itmask the mask that will be overriden +/// @param[in] idx_range specifies the index-range +/// @param[in] kphHI view of the HI photo-ionization rate field +/// @param[in] my_chemistry specifies grackle settings (we probably don't need +/// to pass in everything) +static inline void coupled_rt_modify_itmask_( + gr_mask_type* itmask, + IndexRange idx_range, + grackle::impl::View kphHI, + const chemistry_data* my_chemistry +) +{ + // adjust iteration mask if the caller indicates that they're using + // Grackle in a coupled radiative-transfer/chemistry-energy calculation + // (that has intermediate steps) + if (my_chemistry->use_radiative_transfer == 1 && + my_chemistry->radiative_transfer_coupled_rate_solver == 1) { + // we only define behavior for radiative_transfer_intermediate_step + // values of 0 or 1 + + const int j = idx_range.j; + const int k = idx_range.k; + + if (my_chemistry->radiative_transfer_intermediate_step == 1) { + // the caller has invoked this chemistry-energy solver as an + // intermediate step of a coupled radiative-transfer/chemistry-energy + // calculation and they only want the solver consider cells where + // the radiation is non-zero + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { + itmask[i] = (kphHI(i,j,k) > 0) ? MASK_TRUE : MASK_FALSE; + } + } else if (my_chemistry->radiative_transfer_intermediate_step == 0) { + // the caller has invoked this chemistry-energy solver outside + // of their coupled radiative-transfer/chemistry-energy calculation. + // They want to apply the solver to cells where radiation is 0 (i.e. + // locations where skipped by the coupled calculation) + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { + itmask[i] = (kphHI(i,j,k) > 0) ? MASK_FALSE : MASK_TRUE; + } + } + } +} + + +// ------------------------------------------------------------- + #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ @@ -304,32 +355,9 @@ int solve_rate_cool_g( itmask[i] = MASK_TRUE; } - // adjust iteration mask if the caller indicates that they're using - // Grackle in a coupled radiative-transfer/chemistry-energy calculation - // (that has intermediate steps) - if (my_chemistry->use_radiative_transfer == 1 && - my_chemistry->radiative_transfer_coupled_rate_solver == 1) { - // we only define behavior for radiative_transfer_intermediate_step - // values of 0 or 1 - - if (my_chemistry->radiative_transfer_intermediate_step == 1) { - // the caller has invoked this chemistry-energy solver as an - // intermediate step of a coupled radiative-transfer/chemistry-energy - // calculation and they only want the solver consider cells where - // the radiation is non-zero - for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { - itmask[i] = (kphHI(i,j,k) > 0) ? MASK_TRUE : MASK_FALSE; - } - } else if (my_chemistry->radiative_transfer_intermediate_step == 0) { - // the caller has invoked this chemistry-energy solver outside - // of their coupled radiative-transfer/chemistry-energy calculation. - // They want to apply the solver to cells where radiation is 0 (i.e. - // locations where skipped by the coupled calculation) - for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { - itmask[i] = (kphHI(i,j,k) > 0) ? MASK_FALSE : MASK_TRUE; - } - } - } + // adjust iteration mask (but only if using Grackle in a coupled + // radiative-transfer calculation) + coupled_rt_modify_itmask_(itmask.data(), idx_range, kphHI, my_chemistry); // Set time elapsed to zero for each cell in 1D section From c1e33dab0819687150cf079b55b635ee0b906464 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Tue, 21 Jan 2025 20:41:41 -0500 Subject: [PATCH 077/101] solve_rate_cool_g: making some if-statements legible --- src/clib/solve_rate_cool_g-cpp.C | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 3301a6197..805bb2329 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -435,13 +435,13 @@ int solve_rate_cool_g( for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { if ( itmask_tmp[i] != MASK_FALSE ) { - if ( ( (imetal == 0) - && (ddom[i] < 1.e8) ) - || ( (imetal == 1) - && ( ( (metallicity[i] <= min_metallicity) - && (ddom[i] < 1.e8) ) - || ( (metallicity[i] > min_metallicity) - && (ddom[i] < 1.e6) ) ) ) ) { + if ( (imetal == 0) && (ddom[i] < 1.e8) ) { + itmask_nr[i] = MASK_FALSE; + } else if ( + (imetal == 1) && + (((metallicity[i] <= min_metallicity) && (ddom[i] < 1.e8)) || + ((metallicity[i] > min_metallicity) && (ddom[i] < 1.e6))) + ) { itmask_nr[i] = MASK_FALSE; } else { itmask[i] = MASK_FALSE; @@ -452,9 +452,9 @@ int solve_rate_cool_g( for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { if (itmask_nr[i] != MASK_FALSE) { - if ( (my_chemistry->with_radiative_cooling == 1) && (my_chemistry->primordial_chemistry > 1) && - ((ddom[i] > 1.e7) - && (tgas[i] > 1650.e0)) ) { + if ((my_chemistry->with_radiative_cooling == 1) && + (my_chemistry->primordial_chemistry > 1) && + (ddom[i] > 1.e7) && (tgas[i] > 1650.0)) { imp_eng[i] = 1; } else { imp_eng[i] = 0; From 65594716bca4fde8d0ea8040592547972515428b Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 22 Jan 2025 08:12:36 -0500 Subject: [PATCH 078/101] solve_rate_cool_g: relocate declarations for 2 more local variables into primariy loop --- src/clib/solve_rate_cool_g-cpp.C | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 805bb2329..89938cb29 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -187,8 +187,7 @@ int solve_rate_cool_g( // Locals - int iter; - double ttmin, comp1, comp2; + double comp1, comp2; // row temporaries @@ -286,8 +285,7 @@ int solve_rate_cool_g( //_// PORT: #ifdef _OPENMP //_// PORT: ! ierr is declared as shared and should be modified with atomic operation //_// PORT: !$omp parallel do schedule(runtime) private( - //_// PORT: !$omp& iter, - //_// PORT: !$omp& ttmin, comp1, comp2, + //_// PORT: !$omp& comp1, comp2, //_// PORT: !$omp& dtit, ttot, p2d, tgas, //_// PORT: !$omp& tdust, metallicity, dust2gas, rhoH, mmw, //_// PORT: !$omp& ddom, @@ -371,6 +369,11 @@ int solve_rate_cool_g( ddom[i] = d(i,j,k) * dom; } + // declare 2 variables (primarily used for subcycling, but also used in + // error reporting) + int iter; + double ttmin; + // ------------------ Loop over subcycles ---------------- for (iter = 1; iter<=(my_chemistry->max_iterations); iter++) { From 89ac8288b99e5dda3e4fc2c44f2a97d77f5b48cf Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 22 Jan 2025 08:24:36 -0500 Subject: [PATCH 079/101] solve_rate_cool_g: remove comp[12] local variables Historically, these have passed as arguments to the `cool1d_multi_g` Fortran subroutine, but it turns out that they are only used internally as local variable needs to read the value. These variables also needed to be passed to `step_rate_newton_raphson`, but they are again only passed to internal calls of `cool1d_multi_g`. Modifying `step_rate_newton_raphson` and `cool1d_multi_g` is somewhat beyond the scope of this set of commits, so we currently declare these variables to the functions that wrap these subroutines with a note explaining that they really should be local variables within the called functions --- src/clib/fortran_func_wrappers.hpp | 41 +++++++++++++++++++++--------- src/clib/solve_rate_cool_g-cpp.C | 11 +++----- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/clib/fortran_func_wrappers.hpp b/src/clib/fortran_func_wrappers.hpp index 31ba34cc2..25576188c 100644 --- a/src/clib/fortran_func_wrappers.hpp +++ b/src/clib/fortran_func_wrappers.hpp @@ -57,9 +57,9 @@ inline void wrapped_ceiling_species_g_( } inline void wrapped_cool1d_multi_g_( - int imetal, IndexRange idx_range, int iter, double* comp1, double* comp2, - double* edot, double* tgas, double* mmw, double* p2d, double* tdust, - double* metallicity, double* dust2gas, double* rhoH, gr_mask_type* itmask, + int imetal, IndexRange idx_range, int iter, double* edot, double* tgas, + double* mmw, double* p2d, double* tdust, double* metallicity, + double* dust2gas, double* rhoH, gr_mask_type* itmask, gr_mask_type* itmask_metal, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, grackle_field_data* my_fields, photo_rate_storage my_uvb_rates, InternalGrUnits internalu, @@ -68,6 +68,16 @@ inline void wrapped_cool1d_multi_g_( grackle::impl::Cool1DMultiScratchBuf cool1dmulti_buf, grackle::impl::CoolHeatScratchBuf coolingheating_buf ) { + + // the following 2 variables should not be arguments (they are only inside + // of cool1d_multi_g to temporarily store a local value) + // + // We will fix this in the future + double comp1, comp2; + + // TODO: we should really pass in a value for min_metallicity (this is + // computed in a few independent places) + FORTRAN_NAME(cool1d_multi_g)( my_fields->density, my_fields->internal_energy, my_fields->x_velocity, my_fields->y_velocity, my_fields->z_velocity, my_fields->e_density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, @@ -81,7 +91,7 @@ inline void wrapped_cool1d_multi_g_( my_rates->ciHeIS, my_rates->ciHeII, my_rates->reHII, my_rates->reHeII1, my_rates->reHeII2, my_rates->reHeIII, my_rates->brem, &my_rates->comp, &my_rates->gammah, &my_chemistry->interstellar_radiation_field, my_rates->regr, &my_rates->gamma_isrf, &my_uvb_rates.comp_xray, &my_uvb_rates.temp_xray, - &my_uvb_rates.piHI, &my_uvb_rates.piHeI, &my_uvb_rates.piHeII, comp1, comp2, + &my_uvb_rates.piHI, &my_uvb_rates.piHeI, &my_uvb_rates.piHeII, &comp1, &comp2, my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, my_fields->metal_density, my_fields->dust_density, my_rates->hyd01k, my_rates->h2k01, my_rates->vibh, my_rates->roth, my_rates->rotl, coolingheating_buf.hyd01k, coolingheating_buf.h2k01, coolingheating_buf.vibh, coolingheating_buf.roth, coolingheating_buf.rotl, @@ -451,12 +461,12 @@ inline void wrapped_step_rate_g_( } inline void wrapped_step_rate_newton_raphson_( - int imetal, IndexRange idx_range, int iter, double dom, double* comp1, - double* comp2, double chunit, double dx_cgs, double c_ljeans, double* dtit, - double* p2d, double* tgas, double* tdust, double* metallicity, - double* dust2gas, double* rhoH, double* mmw, double* h2dust, double* edot, - gr_mask_type anydust, gr_mask_type* itmask_nr, gr_mask_type* itmask_metal, - int* imp_eng, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, + int imetal, IndexRange idx_range, int iter, double dom, double chunit, + double dx_cgs, double c_ljeans, double* dtit, double* p2d, double* tgas, + double* tdust, double* metallicity, double* dust2gas, double* rhoH, + double* mmw, double* h2dust, double* edot, gr_mask_type anydust, + gr_mask_type* itmask_nr, gr_mask_type* itmask_metal, int* imp_eng, + chemistry_data* my_chemistry, chemistry_data_storage* my_rates, grackle_field_data* my_fields, photo_rate_storage my_uvb_rates, InternalGrUnits internalu, grackle::impl::GrainSpeciesCollection grain_temperatures, @@ -465,6 +475,13 @@ inline void wrapped_step_rate_newton_raphson_( grackle::impl::CoolHeatScratchBuf coolingheating_buf, grackle::impl::ChemHeatingRates chemheatrates_buf ) { + + // the following 2 variables should not be arguments (they are only inside + // of cool1d_multi_g to temporarily store a local value) + // + // We will fix this in the future + double comp1, comp2; + FORTRAN_NAME(step_rate_newton_raphson)(&my_chemistry->with_radiative_cooling, my_fields->density, my_fields->internal_energy, my_fields->x_velocity, my_fields->y_velocity, my_fields->z_velocity, my_fields->e_density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &my_chemistry->NumberOfTemperatureBins, &internalu.extfields_in_comoving, &my_chemistry->primordial_chemistry, &imetal, &my_chemistry->metal_cooling, &my_chemistry->h2_on_dust, @@ -543,8 +560,8 @@ inline void wrapped_step_rate_newton_raphson_( my_fields->RT_HDI_dissociation_rate, &my_chemistry->radiative_transfer_metal_ionization, my_fields->RT_CI_ionization_rate, my_fields->RT_OI_ionization_rate, &my_chemistry->radiative_transfer_metal_dissociation, my_fields->RT_CO_dissociation_rate, my_fields->RT_OH_dissociation_rate, my_fields->RT_H2O_dissociation_rate, &my_chemistry->radiative_transfer_use_H2_shielding, &my_chemistry->use_isrf_field, my_fields->isrf_habing, &my_chemistry->H2_custom_shielding, my_fields->H2_custom_shielding_factor, - &idx_range.jp1, &idx_range.kp1, &iter, &dom, comp1, - comp2, &internalu.coolunit, &internalu.tbase1, &internalu.xbase1, &chunit, &dx_cgs, + &idx_range.jp1, &idx_range.kp1, &iter, &dom, &comp1, + &comp2, &internalu.coolunit, &internalu.tbase1, &internalu.xbase1, &chunit, &dx_cgs, &c_ljeans, logTlininterp_buf.indixe, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.logtem, logTlininterp_buf.tdef, dtit, p2d, tgas, cool1dmulti_buf.tgasold, tdust, metallicity, dust2gas, rhoH, mmw, cool1dmulti_buf.mynh, cool1dmulti_buf.myde, cool1dmulti_buf.gammaha_eff, cool1dmulti_buf.gasgr_tdust, diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 89938cb29..05a5d7b03 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -185,10 +185,6 @@ int solve_rate_cool_g( const double mh_local_var = mh_grflt; const double pi_local_var = pi_fortran_val; - // Locals - - double comp1, comp2; - // row temporaries std::vector dtit(my_fields->grid_dimension[0]); @@ -285,7 +281,6 @@ int solve_rate_cool_g( //_// PORT: #ifdef _OPENMP //_// PORT: ! ierr is declared as shared and should be modified with atomic operation //_// PORT: !$omp parallel do schedule(runtime) private( - //_// PORT: !$omp& comp1, comp2, //_// PORT: !$omp& dtit, ttot, p2d, tgas, //_// PORT: !$omp& tdust, metallicity, dust2gas, rhoH, mmw, //_// PORT: !$omp& ddom, @@ -387,7 +382,7 @@ int solve_rate_cool_g( // Compute the cooling rate, tgas, tdust, and metallicity for this row wrapped_cool1d_multi_g_( - imetal, idx_range, iter, &comp1, &comp2, edot.data(), tgas.data(), + imetal, idx_range, iter, edot.data(), tgas.data(), mmw.data(), p2d.data(), tdust.data(), metallicity.data(), dust2gas.data(), rhoH.data(), itmask.data(), itmask_metal.data(), my_chemistry, my_rates, my_fields, *my_uvb_rates, internalu, @@ -610,8 +605,8 @@ int solve_rate_cool_g( // itmask_nr) wrapped_step_rate_newton_raphson_( - imetal, idx_range, iter, dom, &comp1, &comp2, chunit, dx_cgs, - c_ljeans, dtit.data(), p2d.data(), tgas.data(), tdust.data(), + imetal, idx_range, iter, dom, chunit, dx_cgs, c_ljeans, + dtit.data(), p2d.data(), tgas.data(), tdust.data(), metallicity.data(), dust2gas.data(), rhoH.data(), mmw.data(), h2dust.data(), edot.data(), anydust, itmask_nr.data(), itmask_metal.data(), imp_eng.data(), my_chemistry, my_rates, From f550baa750d81337d72338fb6c173e3b30a96417 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 22 Jan 2025 08:35:05 -0500 Subject: [PATCH 080/101] solve_rate_cool_g: make `any_dust` a `const` variable --- src/clib/solve_rate_cool_g-cpp.C | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 05a5d7b03..e91e15c77 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -213,7 +213,6 @@ int solve_rate_cool_g( // Iteration mask - gr_mask_type anydust; std::vector itmask(my_fields->grid_dimension[0]); std::vector itmask_tmp(my_fields->grid_dimension[0]); std::vector itmask_nr(my_fields->grid_dimension[0]); @@ -228,12 +227,10 @@ int solve_rate_cool_g( int ierr = GR_SUCCESS; // Set flag for dust-related options - - if ((my_chemistry->h2_on_dust > 0) || (my_chemistry->dust_chemistry > 0)) { - anydust = MASK_TRUE; - } else { - anydust = MASK_FALSE; - } + const gr_mask_type anydust = + ((my_chemistry->h2_on_dust > 0) || (my_chemistry->dust_chemistry > 0)) + ? MASK_TRUE + : MASK_FALSE; // ignore metal chemistry/cooling below this metallicity const double min_metallicity = 1.e-9 / my_chemistry->SolarMetalFractionByMass; From e895f56806f295293db6198f700df8254ac18ddb Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 22 Jan 2025 08:44:23 -0500 Subject: [PATCH 081/101] solve_rate_cool_g: remove 2 constants (these became unnecessary back when we introduced InternalGrUnits --- src/clib/solve_rate_cool_g-cpp.C | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index e91e15c77..80b17838d 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -182,9 +182,6 @@ int solve_rate_cool_g( const gr_float tolerance = (gr_float)(1.0e-10); #endif - const double mh_local_var = mh_grflt; - const double pi_local_var = pi_fortran_val; - // row temporaries std::vector dtit(my_fields->grid_dimension[0]); From 5ba72178ffba435ec308ed6359d481e34064c952 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 22 Jan 2025 11:51:08 -0500 Subject: [PATCH 082/101] solve_rate_cool_g: extracted the logic for finding the timestep that keeps relative chemical changes below 10% out to its own function called `set_subcycle_dt_from_chemistry_scheme_` (this was a direct copy-paste -- the only change was in the number of indentations) --- src/clib/solve_rate_cool_g-cpp.C | 231 +++++++++++++++++++++---------- 1 file changed, 159 insertions(+), 72 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 80b17838d..7c87fde65 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -102,6 +102,158 @@ static double calc_Heq_div_dHeqdt_( // ------------------------------------------------------------- +/// Sets the current subcycle timestep for each index in the index-range +/// if it exceeds maximum the allowed chemistry-rate timestep. +/// +/// @param[out] dtit buffer tracking the current subcycle timestep for each +/// index in the index-range. Values will be modified in place. +/// @param[in] idx_range Specifies the current index-range +/// @param[in] iter current subcycle iteration +/// @param[in] dt tracks the full timestep that all the subcycles will +/// eventually add up to +/// @param[in] ttot tracks the total time that has already elapsed from +/// previous subcycles for each location in `idx_range` +/// @param[in] itmask Specifies the `idx_range`'s iteration-mask for the +/// Gauss-Seidel scheme +/// @param[in] itmask_nr Specifies the `idx_range`'s iteration-mask for the +/// Newton-Raphson scheme +/// @param[in] imp_eng Specifies how Newton-Raphson scheme handles energy +/// evolution at each `idx_range` location +/// @param[in] dedot, HIdot respectively specify the time derivative of the +/// free electrons and HI for the `idx_range` +/// @param[in] dedot_prev, HIdot_prev respectively specify the time derivative +/// of the free electron density and HI density for the `idx_range` from the +/// previous subcycle (they're allowed to hold garbage data in 1st subcycle) +/// @param[in] ddom specifies precomputed product of mass density and the +/// `dom` quantity for each location in `idx_range` +/// @param[in] tgas, p2d, edot arrays that respectively specify the precomputed +/// gas temperature, pressure divided by density, and the time derivative of +/// internal energy density (for each location in `idx_range`) +/// @param[in] my_chemistry holds a number of configuration parameters +/// @param[in] my_rates holds assorted rate data. In this function, this is +/// being used to specify the interpolation tables of some relevant reaction +/// rates (they are tabulated with respect to logT) +/// @param[in] dlogtem Specifies the constant spacing shared by the relevant +/// rate interpolation tables +/// @param[in] logTlininterp_buf Specifies the information related to the +/// position in the logT interpolations (for a number of chemistry zones) +/// @param[in] my_fields specifies the field data +/// @param[in] kcr_buf holds various pre-computed chemical reaction rates for +/// each location in `idx_range`. +/// +/// @todo +/// Consider breaking this into 2 functions that separately determines dtit for +/// Gauss-Seidel and Newton-Raphson. (At the time of writing, the included +/// logic for Newton-Raphson doesn't care about the chemistry-rates, instead +/// it sets the timestep based on the energy evolution) +static void set_subcycle_dt_from_chemistry_scheme_( + double* dtit, IndexRange idx_range, int iter, double dt, const double* ttot, + const gr_mask_type* itmask, const gr_mask_type* itmask_nr, const int* imp_eng, + double* dedot, double* HIdot, + const double* dedot_prev, const double* HIdot_prev, + const double* ddom, const double* tgas, const double* p2d, const double* edot, + const chemistry_data* my_chemistry, const chemistry_data_storage* my_rates, + double dlogtem, + const grackle::impl::LogTLinInterpScratchBuf logTlininterp_buf, + grackle_field_data* my_fields, grackle::impl::ColRecRxnRateCollection kcr_buf +) { + const int j = idx_range.j; + const int k = idx_range.k; + + grackle::impl::View de(my_fields->e_density, + my_fields->grid_dimension[0], + my_fields->grid_dimension[1], + my_fields->grid_dimension[2]); + grackle::impl::View HI(my_fields->HI_density, + my_fields->grid_dimension[0], + my_fields->grid_dimension[1], + my_fields->grid_dimension[2]); + grackle::impl::View HII(my_fields->HII_density, + my_fields->grid_dimension[0], + my_fields->grid_dimension[1], + my_fields->grid_dimension[2]); + grackle::impl::View d(my_fields->density, + my_fields->grid_dimension[0], + my_fields->grid_dimension[1], + my_fields->grid_dimension[2]); + grackle::impl::View e(my_fields->internal_energy, + my_fields->grid_dimension[0], + my_fields->grid_dimension[1], + my_fields->grid_dimension[2]); + + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { + if (itmask[i] != MASK_FALSE) { + // Bound from below to prevent numerical errors + + if (std::fabs(dedot[i]) < tiny8) + { dedot[i] = std::fmin(tiny_fortran_val,de(i,j,k)); } + if (std::fabs(HIdot[i]) < tiny8) + { HIdot[i] = std::fmin(tiny_fortran_val,HI(i,j,k)); } + + // If the net rate is almost perfectly balanced then set + // it to zero (since it is zero to available precision) + + if (std::fmin(std::fabs(kcr_buf.data[ColRecRxnLUT::k1][i]* de(i,j,k)*HI(i,j,k)), + std::fabs(kcr_buf.data[ColRecRxnLUT::k2][i]*HII(i,j,k)*de(i,j,k)))/ + std::fmax(std::fabs(dedot[i]),std::fabs(HIdot[i])) > + 1.0e6) { + dedot[i] = tiny8; + HIdot[i] = tiny8; + } + + // If the iteration count is high then take the smaller of + // the calculated dedot and last time step's actual dedot. + // This is intended to get around the problem of a low + // electron or HI fraction which is in equilibrium with high + // individual terms (which all nearly cancel). + + if (iter > 50) { + dedot[i] = std::fmin(std::fabs(dedot[i]), std::fabs(dedot_prev[i])); + HIdot[i] = std::fmin(std::fabs(HIdot[i]), std::fabs(HIdot_prev[i])); + } + + // compute minimum rate timestep + + double olddtit = dtit[i]; + dtit[i] = grackle::impl::fmin(std::fabs(0.1*de(i,j,k)/dedot[i]), + std::fabs(0.1*HI(i,j,k)/HIdot[i]), + dt-ttot[i], 0.5*dt); + + if (ddom[i] > 1.e8 && + edot[i] > 0. && + my_chemistry->primordial_chemistry > 1) { + // here, we ensure that that the equilibrium mass density of + // Hydrogen changes by 10% or less + double Heq_div_dHeqdt = calc_Heq_div_dHeqdt_( + my_chemistry, my_rates, dlogtem, logTlininterp_buf, + kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k22], + d(i,j,k), tgas, p2d, edot, i + ); + + dtit[i] = std::fmin(dtit[i], 0.1*Heq_div_dHeqdt); + } + if (iter>10LL) { + dtit[i] = std::fmin(olddtit*1.5, dtit[i]); + } + + } else if ((itmask_nr[i]!=MASK_FALSE) && + (imp_eng[i]==0)) { + dtit[i] = grackle::impl::fmin(std::fabs(0.1*e(i,j,k)/edot[i]*d(i,j,k)), + dt-ttot[i], 0.5*dt); + + } else if ((itmask_nr[i]!=MASK_FALSE) && + (imp_eng[i]==1)) { + dtit[i] = dt - ttot[i]; + + } else { + dtit[i] = dt; + } + } + +} + +// ------------------------------------------------------------- + /// Updates the iteration mask in the case where the user has specified that we /// are using grackle as a part of a coupled radiative transfer calculation /// @@ -164,9 +316,6 @@ int solve_rate_cool_g( // Density, energy and velocity fields fields - grackle::impl::View de(my_fields->e_density, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); - grackle::impl::View HI(my_fields->HI_density, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); - grackle::impl::View HII(my_fields->HII_density, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); grackle::impl::View d(my_fields->density, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); grackle::impl::View e(my_fields->internal_energy, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); @@ -456,75 +605,13 @@ int solve_rate_cool_g( // Find timestep that keeps relative chemical changes below 10% - for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { - if (itmask[i] != MASK_FALSE) { - // Bound from below to prevent numerical errors - - if (std::fabs(dedot[i]) < tiny8) - { dedot[i] = std::fmin(tiny_fortran_val,de(i,j,k)); } - if (std::fabs(HIdot[i]) < tiny8) - { HIdot[i] = std::fmin(tiny_fortran_val,HI(i,j,k)); } - - // If the net rate is almost perfectly balanced then set - // it to zero (since it is zero to available precision) - - if (std::fmin(std::fabs(kcr_buf.data[ColRecRxnLUT::k1][i]* de(i,j,k)*HI(i,j,k)), - std::fabs(kcr_buf.data[ColRecRxnLUT::k2][i]*HII(i,j,k)*de(i,j,k)))/ - std::fmax(std::fabs(dedot[i]),std::fabs(HIdot[i])) > - 1.0e6) { - dedot[i] = tiny8; - HIdot[i] = tiny8; - } - - // If the iteration count is high then take the smaller of - // the calculated dedot and last time step's actual dedot. - // This is intended to get around the problem of a low - // electron or HI fraction which is in equilibrium with high - // individual terms (which all nearly cancel). - - if (iter > 50) { - dedot[i] = std::fmin(std::fabs(dedot[i]), std::fabs(dedot_prev[i])); - HIdot[i] = std::fmin(std::fabs(HIdot[i]), std::fabs(HIdot_prev[i])); - } - - // compute minimum rate timestep - - double olddtit = dtit[i]; - dtit[i] = grackle::impl::fmin(std::fabs(0.1*de(i,j,k)/dedot[i]), - std::fabs(0.1*HI(i,j,k)/HIdot[i]), - dt-ttot[i], 0.5*dt); - - if (ddom[i] > 1.e8 && - edot[i] > 0. && - my_chemistry->primordial_chemistry > 1) { - // here, we ensure that that the equilibrium mass density of - // Hydrogen changes by 10% or less - double Heq_div_dHeqdt = calc_Heq_div_dHeqdt_( - my_chemistry, my_rates, dlogtem, logTlininterp_buf, - kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k22], d(i,j,k), tgas.data(), - p2d.data(), edot.data(), i - ); - - dtit[i] = std::fmin(dtit[i], 0.1*Heq_div_dHeqdt); - } - if (iter>10LL) { - dtit[i] = std::fmin(olddtit*1.5, dtit[i]); - } - - } else if ((itmask_nr[i]!=MASK_FALSE) && - (imp_eng[i]==0)) { - dtit[i] = grackle::impl::fmin(std::fabs(0.1*e(i,j,k)/edot[i]*d(i,j,k)), - dt-ttot[i], 0.5*dt); - - } else if ((itmask_nr[i]!=MASK_FALSE) && - (imp_eng[i]==1)) { - dtit[i] = dt - ttot[i]; - - } else { - dtit[i] = dt; - } - } - + set_subcycle_dt_from_chemistry_scheme_( + dtit.data(), idx_range, iter, dt, ttot.data(), itmask.data(), + itmask_nr.data(), imp_eng.data(), dedot.data(), HIdot.data(), + dedot_prev.data(), HIdot_prev.data(), ddom.data(), tgas.data(), + p2d.data(), edot.data(), my_chemistry, my_rates, dlogtem, + logTlininterp_buf, my_fields, kcr_buf + ); } // Compute maximum timestep for cooling/heating From 82c9f161b627f3dcc22c2307ea18b5c6804a878e Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 22 Jan 2025 12:21:52 -0500 Subject: [PATCH 083/101] solve_rate_cool_g: apply some superficial tweaks to `set_subcycle_dt_from_chemistry_scheme_` to improve legibility --- src/clib/solve_rate_cool_g-cpp.C | 71 ++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 27 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 7c87fde65..ae3abc0c5 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -181,24 +181,35 @@ static void set_subcycle_dt_from_chemistry_scheme_( my_fields->grid_dimension[1], my_fields->grid_dimension[2]); + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { - if (itmask[i] != MASK_FALSE) { + if (itmask[i] != MASK_FALSE) { + // in this case, the chemical network will be evolved with Gauss-Seidel + + // Part 1 of 2: adjust values of dedot and HIdot + // --------------------------------------------- + // Bound from below to prevent numerical errors - - if (std::fabs(dedot[i]) < tiny8) - { dedot[i] = std::fmin(tiny_fortran_val,de(i,j,k)); } - if (std::fabs(HIdot[i]) < tiny8) - { HIdot[i] = std::fmin(tiny_fortran_val,HI(i,j,k)); } + if (std::fabs(dedot[i]) < tiny8) { + dedot[i] = std::fmin(tiny_fortran_val, de(i,j,k)); + } + if (std::fabs(HIdot[i]) < tiny8){ + HIdot[i] = std::fmin(tiny_fortran_val, HI(i,j,k)); + } // If the net rate is almost perfectly balanced then set // it to zero (since it is zero to available precision) - - if (std::fmin(std::fabs(kcr_buf.data[ColRecRxnLUT::k1][i]* de(i,j,k)*HI(i,j,k)), - std::fabs(kcr_buf.data[ColRecRxnLUT::k2][i]*HII(i,j,k)*de(i,j,k)))/ - std::fmax(std::fabs(dedot[i]),std::fabs(HIdot[i])) > - 1.0e6) { - dedot[i] = tiny8; - HIdot[i] = tiny8; + { + double ion_rate = std::fabs(kcr_buf.data[ColRecRxnLUT::k1][i] * + de(i,j,k) * HI(i,j,k)); + double recomb_rate = std::fabs(kcr_buf.data[ColRecRxnLUT::k2][i] * + HII(i,j,k) * de(i,j,k)); + double ratio = (std::fmin(ion_rate, recomb_rate) / + std::fmax(std::fabs(dedot[i]), std::fabs(HIdot[i]))); + if (ratio > 1.0e6) { + dedot[i] = tiny8; + HIdot[i] = tiny8; + } } // If the iteration count is high then take the smaller of @@ -206,21 +217,21 @@ static void set_subcycle_dt_from_chemistry_scheme_( // This is intended to get around the problem of a low // electron or HI fraction which is in equilibrium with high // individual terms (which all nearly cancel). - if (iter > 50) { dedot[i] = std::fmin(std::fabs(dedot[i]), std::fabs(dedot_prev[i])); HIdot[i] = std::fmin(std::fabs(HIdot[i]), std::fabs(HIdot_prev[i])); } - // compute minimum rate timestep + // Part 2 of 2: compute minimum rate timestep + // ------------------------------------------ double olddtit = dtit[i]; dtit[i] = grackle::impl::fmin(std::fabs(0.1*de(i,j,k)/dedot[i]), - std::fabs(0.1*HI(i,j,k)/HIdot[i]), - dt-ttot[i], 0.5*dt); + std::fabs(0.1*HI(i,j,k)/HIdot[i]), + dt-ttot[i], + 0.5*dt); - if (ddom[i] > 1.e8 && - edot[i] > 0. && + if (ddom[i] > 1.e8 && edot[i] > 0. && my_chemistry->primordial_chemistry > 1) { // here, we ensure that that the equilibrium mass density of // Hydrogen changes by 10% or less @@ -232,24 +243,26 @@ static void set_subcycle_dt_from_chemistry_scheme_( dtit[i] = std::fmin(dtit[i], 0.1*Heq_div_dHeqdt); } - if (iter>10LL) { + + if (iter > 10) { dtit[i] = std::fmin(olddtit*1.5, dtit[i]); } - } else if ((itmask_nr[i]!=MASK_FALSE) && - (imp_eng[i]==0)) { + } else if ((itmask_nr[i]!=MASK_FALSE) && (imp_eng[i]==0)) { + // we may want to handle this case and the next case in a separate + // function (they determine the timestep using very different logic than + // in the above case) dtit[i] = grackle::impl::fmin(std::fabs(0.1*e(i,j,k)/edot[i]*d(i,j,k)), - dt-ttot[i], 0.5*dt); + dt-ttot[i], + 0.5*dt); - } else if ((itmask_nr[i]!=MASK_FALSE) && - (imp_eng[i]==1)) { + } else if ((itmask_nr[i]!=MASK_FALSE) && (imp_eng[i]==1)) { dtit[i] = dt - ttot[i]; } else { dtit[i] = dt; } } - } // ------------------------------------------------------------- @@ -603,7 +616,11 @@ int solve_rate_cool_g( } } - // Find timestep that keeps relative chemical changes below 10% + // Set the max timestep for the current subcycle based on our scheme + // for updating the chemical network: + // - for Gauss-Seidel, pick a timestep that keeps relative chemical + // changes below 10% + // - do something else for Newton-Raphson set_subcycle_dt_from_chemistry_scheme_( dtit.data(), idx_range, iter, dt, ttot.data(), itmask.data(), From fbf76b218a5efd258b193ee85b859129dd3df1f4 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 22 Jan 2025 13:36:44 -0500 Subject: [PATCH 084/101] solve_rate_cool_g: extract logic that computes the max timestep for cooling/heating into a separate function --- src/clib/solve_rate_cool_g-cpp.C | 117 ++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 40 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index ae3abc0c5..936f870cb 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -18,6 +18,76 @@ #include "solve_rate_cool_g-cpp.h" +/// overrides the subcycle timestep for each index in the index-range that is +/// selected by the given itmask with the maximum allowed heating/cooling +/// timestep when the current value is larger. +/// +/// @param[out] dtit buffer tracking the current subcycle timestep for each +/// index in the index-range. If the current value exceeds the maximum +/// allowed heating/cooling timestep, the values will overwritten +/// @param[in] idx_range Specifies the current index-range +/// @param[in] dt tracks the full timestep that all the subcycles will +/// eventually add up to +/// @param[in] ttot tracks the total time that has already elapsed from +/// previous subcycles for each location in `idx_range` +/// @param[in] itmask Specifies the `idx_range`'s iteration-mask for this +/// calculation +/// @param[in] tgas specifies the gas temperatures for the `idx_range` +/// @param[in] p2d specifies the pressures for the `idx_range`. This is +/// computed user-specified nominal adiabatic index value (i.e. no attempts +/// are made to correct for presence of H2) +/// @param[in,out] edot specifies the time derivative of internal energy +/// density for each location in `idx_range`. This may be overwritten to +/// enforce the floor. +static void enforce_max_heatcool_subcycle_dt_( + double* dtit, IndexRange idx_range, double dt, const double* ttot, + const gr_mask_type* itmask, const double* tgas, const double* p2d, + double* edot, const chemistry_data* my_chemistry +) { + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { + if (itmask[i] != MASK_FALSE) { + // Set energy per unit volume of this cell based in the pressure + // (the gamma used here is the right one even for H2 since p2d + // is calculated with this gamma). + + double energy = std::fmax(p2d[i]/(my_chemistry->Gamma-1.), tiny8); + + // If the temperature is at the bottom of the temperature look-up + // table and edot < 0, then shut off the cooling. + + if (tgas[i] <= 1.01*my_chemistry->TemperatureStart && + edot[i] < 0.) + { edot[i] = tiny8; } + if (std::fabs(edot[i]) < tiny8) { edot[i] = tiny8; } + + // Compute timestep for 10% change + + dtit[i] = grackle::impl::fmin((double)(std::fabs(0.1* + energy/edot[i]) ), + dt-ttot[i], dtit[i]); + + if (dtit[i] != dtit[i]) { + OMP_PRAGMA_CRITICAL + { + eprintf("HUGE dtit :: %g %g %g %g %g %g %g\n", + energy, + edot [ i ], + dtit [ i ], + dt, + ttot [ i ], + std::fabs ( 0.1 * energy / edot [ i ] ), + (double) ( std::fabs ( 0.1 * energy / edot [ i ] ) )); + } + } + + } + } + +} + +// ------------------------------------------------------------- + + /// Computes the timescale given by `ndens_Heq / (d ndens_Heq / d t)` /// /// This is used to help compute the subcycle timestep when using a primordial @@ -631,46 +701,13 @@ int solve_rate_cool_g( ); } - // Compute maximum timestep for cooling/heating - - for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { - if (itmask[i] != MASK_FALSE) { - // Set energy per unit volume of this cell based in the pressure - // (the gamma used here is the right one even for H2 since p2d - // is calculated with this gamma). - - double energy = std::fmax(p2d[i]/(my_chemistry->Gamma-1.), tiny8); - - // If the temperature is at the bottom of the temperature look-up - // table and edot < 0, then shut off the cooling. - - if (tgas[i] <= 1.01*my_chemistry->TemperatureStart && - edot[i] < 0.) - { edot[i] = tiny8; } - if (std::fabs(edot[i]) < tiny8) { edot[i] = tiny8; } - - // Compute timestep for 10% change - - dtit[i] = grackle::impl::fmin((double)(std::fabs(0.1* - energy/edot[i]) ), - dt-ttot[i], dtit[i]); - - if (dtit[i] != dtit[i]) { - OMP_PRAGMA_CRITICAL - { - eprintf("HUGE dtit :: %g %g %g %g %g %g %g\n", - energy, - edot [ i ], - dtit [ i ], - dt, - ttot [ i ], - std::fabs ( 0.1 * energy / edot [ i ] ), - (double) ( std::fabs ( 0.1 * energy / edot [ i ] ) )); - } - } - - } - } + // Update dtit (the current subcycle timestep) to ensure it doesn't + // exceed the max timestep for cooling/heating + // -> zones that will use Newton-Raphson scheme are ignored + enforce_max_heatcool_subcycle_dt_( + dtit.data(), idx_range, dt, ttot.data(), itmask.data(), tgas.data(), + p2d.data(), edot.data(), my_chemistry + ); // Update total and gas energy From 620087284ab099c37a73743894fcd45cde48f5fa Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 22 Jan 2025 13:41:17 -0500 Subject: [PATCH 085/101] correct the description of p2d in the docstring of calc_Heq_div_dHeqdt --- src/clib/solve_rate_cool_g-cpp.C | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 936f870cb..e6de9b620 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -104,8 +104,12 @@ static void enforce_max_heatcool_subcycle_dt_( /// @param k13, k22 1D arrays specifying the previously looked up, local values /// of the k13 and k22 rates. /// @param local_rho specifies the local (total) mass density -/// @param tgas, p2d, edot 1D arrays containing local values of temperature, -/// pressure divided by density, and the time derivative of internal energy. +/// @param tgas 1D array specifying the temperature +/// @param p2d 1D array specifying the pressures values. This is computed from +/// the user-specified nominal adiabatic index value (i.e. no attempts +/// are made to correct for presence of H2) +/// @param edot 1D array specifying the time derivative of the internal energy +/// density /// @param i Specifies the index of the relevant zone in the 1D array. (**BE /// AWARE:** this is a 0-based index) /// From 105547327445a136d443efef669278934d83cf60 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 22 Jan 2025 13:44:37 -0500 Subject: [PATCH 086/101] correct the description of p2d in the docstring of set_subcycle_dt_from_chemistry_scheme_ --- src/clib/solve_rate_cool_g-cpp.C | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index e6de9b620..d6b1e4569 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -180,29 +180,32 @@ static double calc_Heq_div_dHeqdt_( /// if it exceeds maximum the allowed chemistry-rate timestep. /// /// @param[out] dtit buffer tracking the current subcycle timestep for each -/// index in the index-range. Values will be modified in place. +/// index in the index-range. Values will be modified in place. /// @param[in] idx_range Specifies the current index-range /// @param[in] iter current subcycle iteration /// @param[in] dt tracks the full timestep that all the subcycles will -/// eventually add up to +/// eventually add up to /// @param[in] ttot tracks the total time that has already elapsed from -/// previous subcycles for each location in `idx_range` +/// previous subcycles for each location in `idx_range` /// @param[in] itmask Specifies the `idx_range`'s iteration-mask for the -/// Gauss-Seidel scheme +/// Gauss-Seidel scheme /// @param[in] itmask_nr Specifies the `idx_range`'s iteration-mask for the -/// Newton-Raphson scheme +/// Newton-Raphson scheme /// @param[in] imp_eng Specifies how Newton-Raphson scheme handles energy -/// evolution at each `idx_range` location +/// evolution at each `idx_range` location /// @param[in] dedot, HIdot respectively specify the time derivative of the -/// free electrons and HI for the `idx_range` +/// free electrons and HI for the `idx_range` /// @param[in] dedot_prev, HIdot_prev respectively specify the time derivative -/// of the free electron density and HI density for the `idx_range` from the -/// previous subcycle (they're allowed to hold garbage data in 1st subcycle) +/// of the free electron density and HI density for the `idx_range` from the +/// previous subcycle (they're allowed to hold garbage data in 1st subcycle) /// @param[in] ddom specifies precomputed product of mass density and the /// `dom` quantity for each location in `idx_range` -/// @param[in] tgas, p2d, edot arrays that respectively specify the precomputed -/// gas temperature, pressure divided by density, and the time derivative of -/// internal energy density (for each location in `idx_range`) +/// @param[in] tgas specifies the gas temperatures for the `idx_range` +/// @param[in] p2d specifies the pressures for the `idx_range`. This is +/// computed user-specified nominal adiabatic index value (i.e. no attempts +/// are made to correct for presence of H2) +/// @param[in] edot specifies the time derivative of the internal energy +/// density for the `idx_range`. /// @param[in] my_chemistry holds a number of configuration parameters /// @param[in] my_rates holds assorted rate data. In this function, this is /// being used to specify the interpolation tables of some relevant reaction From 7b39db9607c830dda385c279029d5e37445baf66 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 22 Jan 2025 13:52:10 -0500 Subject: [PATCH 087/101] solve_rate_cool_g: adjust formatting of logic that computes the max timestep for cooling/heating so that it is a little more legible --- src/clib/solve_rate_cool_g-cpp.C | 35 +++++++++++++++----------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index d6b1e4569..0e00e4a72 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -18,8 +18,8 @@ #include "solve_rate_cool_g-cpp.h" -/// overrides the subcycle timestep for each index in the index-range that is -/// selected by the given itmask with the maximum allowed heating/cooling +/// overrides the subcycle timestep (for each index in the index-range that is +/// selected by the given itmask) with the maximum allowed heating/cooling /// timestep when the current value is larger. /// /// @param[out] dtit buffer tracking the current subcycle timestep for each @@ -44,39 +44,36 @@ static void enforce_max_heatcool_subcycle_dt_( const gr_mask_type* itmask, const double* tgas, const double* p2d, double* edot, const chemistry_data* my_chemistry ) { + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { - if (itmask[i] != MASK_FALSE) { + + if (itmask[i] != MASK_FALSE) { // Set energy per unit volume of this cell based in the pressure // (the gamma used here is the right one even for H2 since p2d // is calculated with this gamma). - double energy = std::fmax(p2d[i]/(my_chemistry->Gamma-1.), tiny8); // If the temperature is at the bottom of the temperature look-up // table and edot < 0, then shut off the cooling. + if (tgas[i] <= 1.01*my_chemistry->TemperatureStart && edot[i] < 0.) { + edot[i] = tiny8; + } - if (tgas[i] <= 1.01*my_chemistry->TemperatureStart && - edot[i] < 0.) - { edot[i] = tiny8; } + // enforce the floor if (std::fabs(edot[i]) < tiny8) { edot[i] = tiny8; } // Compute timestep for 10% change + dtit[i] = grackle::impl::fmin( + (double)(std::fabs(0.1 * energy / edot[i])), dt - ttot[i], dtit[i] + ); - dtit[i] = grackle::impl::fmin((double)(std::fabs(0.1* - energy/edot[i]) ), - dt-ttot[i], dtit[i]); - - if (dtit[i] != dtit[i]) { + if (dtit[i] != dtit[i]) { OMP_PRAGMA_CRITICAL { eprintf("HUGE dtit :: %g %g %g %g %g %g %g\n", - energy, - edot [ i ], - dtit [ i ], - dt, - ttot [ i ], - std::fabs ( 0.1 * energy / edot [ i ] ), - (double) ( std::fabs ( 0.1 * energy / edot [ i ] ) )); + energy, edot[i], dtit[i], dt, ttot[i], + std::fabs(0.1 * energy / edot[i]), + (double)(std::fabs(0.1 * energy / edot [i])) ); } } From 9f033fe284ef6858fd1565c9396ac2b6eea10c9a Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 22 Jan 2025 13:56:58 -0500 Subject: [PATCH 088/101] solve_rate_cool_g-cpp: adjust a comment --- src/clib/solve_rate_cool_g-cpp.C | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 0e00e4a72..063cc96f0 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -570,10 +570,10 @@ int solve_rate_cool_g( const int k = idx_range.k; // use 0-based index const int j = idx_range.j; // use 0-based index - // tolerance = 1.0e-06_DKIND * dt + // `tolerance = 1.0e-06_DKIND * dt` was some commented logic in the + // original fortran subroutine in this location // Initialize iteration mask to true for all cells. - for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { itmask[i] = MASK_TRUE; } From f59fef6f44e511b6db4d38cd2a43ea36d22374ae Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Wed, 22 Jan 2025 15:08:39 -0500 Subject: [PATCH 089/101] solve_rate_cool_g: extract the logic for picking a chemistry-network update scheme into a helper function --- src/clib/solve_rate_cool_g-cpp.C | 128 ++++++++++++++++++++++--------- 1 file changed, 91 insertions(+), 37 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 063cc96f0..9c7ebf0c3 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -84,6 +84,84 @@ static void enforce_max_heatcool_subcycle_dt_( // ------------------------------------------------------------- +/// Selects the scheme that will be used to evolve the chemistry network and +/// then accordingly updates the iteration masks (and `imp_eng`) +/// +/// There are 2 schemes: +/// 1. Gauss-Seidel (for low-density zones) +/// 2. Newton-Raphson (for high-density zones) in 2 modes: +/// - internal energy evolution is operator-split (handled separately from +/// chemistry network) +/// - internal energy is coupled with the rest of the chemistry network +/// +/// @param[in] idx_range Specifies the current index-range +/// @param[in,out] itmask Initially specifies all locations to be evolved +/// during the current subcycle (in `idx_range`). Will be updated to only +/// specify the locations to apply Gauss-Seidel scheme +/// @param[out] itmask_nr Buffer for `idx_range` that is used to specify +/// locations where we will apply Newton-Raphson scheme +/// @param[out] imp_eng Buffer for `idx_range` where the choice of +/// energy-evolution handling is recorded for the Newton-Raphson scheme +/// @param[out] itmask_tmp Buffer where the initial values of itmask are +/// copied into +/// @param[in] mask_len the length of the iteration masks +/// @param[in] imetal specifies whether or not the caller provided a metal +/// density field +/// @param[in] min_metallicity specifies the minimum metallicity where we +/// consider metal chemistry/cooling +/// @param[in] ddom specifies precomputed product of mass density and the +/// `dom` quantity for each location in `idx_range` +/// @param[in] tgas specifies the gas temperatures for the `idx_range` +/// @param[in] metallicity specifies the metallicity for the `idx_range` +/// +/// @todo +/// It might make more sense to create `itmask_tmp` before calling this +/// function and then completely ignore the initial values in `itmask` (this +/// would be far less confusing) +static void select_chem_scheme_update_masks_( + IndexRange idx_range, gr_mask_type* itmask, gr_mask_type* itmask_nr, + int* imp_eng, gr_mask_type* itmask_tmp, int mask_len, int imetal, + double min_metallicity, const double* ddom, const double* tgas, + const double* metallicity, const chemistry_data* my_chemistry +) { + + std::memcpy(itmask_tmp, itmask, sizeof(gr_mask_type)*mask_len); + std::memcpy(itmask_nr, itmask, sizeof(gr_mask_type)*mask_len); + + // would it be more robust to use my_chemistry->metal_cooling than imetal? + + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { + if ( itmask_tmp[i] != MASK_FALSE ) { + + if ( (imetal == 0) && (ddom[i] < 1.e8) ) { + itmask_nr[i] = MASK_FALSE; + } else if ( + (imetal == 1) && + (((metallicity[i] <= min_metallicity) && (ddom[i] < 1.e8)) || + ((metallicity[i] > min_metallicity) && (ddom[i] < 1.e6))) + ) { + itmask_nr[i] = MASK_FALSE; + } else { + itmask[i] = MASK_FALSE; + } + + } + } + + for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { + if (itmask_nr[i] != MASK_FALSE) { + if ((my_chemistry->with_radiative_cooling == 1) && + (my_chemistry->primordial_chemistry > 1) && + (ddom[i] > 1.e7) && (tgas[i] > 1650.0)) { + imp_eng[i] = 1; + } else { + imp_eng[i] = 0; + } + } + } +} + +// ------------------------------------------------------------- /// Computes the timescale given by `ndens_Heq / (d ndens_Heq / d t)` /// @@ -652,43 +730,19 @@ int solve_rate_cool_g( chemheatrates_buf ); - // move itmask temporary array - // then split cells with low densities - // => Gauss-Seidel scheme - // and with high densities - // => Newton-Raphson scheme - - std::memcpy(itmask_tmp.data(), itmask.data(), sizeof(gr_mask_type)*my_fields->grid_dimension[0]); - std::memcpy(itmask_nr.data(), itmask.data(), sizeof(gr_mask_type)*my_fields->grid_dimension[0]); - for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { - if ( itmask_tmp[i] != MASK_FALSE ) { - - if ( (imetal == 0) && (ddom[i] < 1.e8) ) { - itmask_nr[i] = MASK_FALSE; - } else if ( - (imetal == 1) && - (((metallicity[i] <= min_metallicity) && (ddom[i] < 1.e8)) || - ((metallicity[i] > min_metallicity) && (ddom[i] < 1.e6))) - ) { - itmask_nr[i] = MASK_FALSE; - } else { - itmask[i] = MASK_FALSE; - } - - } - } - - for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { - if (itmask_nr[i] != MASK_FALSE) { - if ((my_chemistry->with_radiative_cooling == 1) && - (my_chemistry->primordial_chemistry > 1) && - (ddom[i] > 1.e7) && (tgas[i] > 1650.0)) { - imp_eng[i] = 1; - } else { - imp_eng[i] = 0; - } - } - } + // First, copy itmask's current values into a temporary array + // (itmask_tmp). Then setup masks to identify which chemistry schemes + // to use. We split cells by density: + // => low-density: Gauss-Seidel scheme, tracked by itmask + // => high-density: Newton-Raphson scheme, tracked by itmask_nr + // + // (the values stored within itmask will change within the function) + select_chem_scheme_update_masks_( + idx_range, itmask.data(), itmask_nr.data(), imp_eng.data(), + itmask_tmp.data(), my_fields->grid_dimension[0], imetal, + min_metallicity, ddom.data(), tgas.data(), metallicity.data(), + my_chemistry + ); // Set the max timestep for the current subcycle based on our scheme // for updating the chemical network: From 36f828e385d54710e2f3891430c96f70f09b673d Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Thu, 23 Jan 2025 12:59:30 -0500 Subject: [PATCH 090/101] solve_rate_cool_g: large step toward aggregating all local variables only used for solving rates --- src/clib/solve_rate_cool_g-cpp.C | 185 +++++++++++++++++++++++-------- 1 file changed, 138 insertions(+), 47 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 9c7ebf0c3..f6485deb6 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -7,6 +7,7 @@ // solve_rate_cool_g function from FORTRAN to C++ #include +#include // std::malloc, std::free #include #include "grackle.h" @@ -465,6 +466,103 @@ static inline void coupled_rt_modify_itmask_( } } +// ------------------------------------------------------------- + +namespace grackle::impl { + +/// Aggregates buffers used as scratch space in rate-related calculations +/// +/// This exists to encapsulate the logic for all of the local buffers used in +/// rate calculations within solve_rate_cool_g (this is useful given the size +/// of the function). It is not expected to be used outside of the function. +/// +/// @note +/// The purpose of this type is similar in spirit to the purpose of the +/// ScratchBuf data structrues described in the inernal_types.hpp header and +/// we consequently observe those conventions relating to (con|de)structors. +/// +/// @note +/// The majority of the time, functions only need a subset of data stored by +/// this struct. In these cases, you should strongly prefer to only pass the +/// members that are required as function arguments (if we unnecessarily pass +/// this whole struct, that can make it difficult to visualize the data flow) +struct SpeciesRateSolverScratchBuf { + + /// specifies precomputed product of mass density and the `dom` quantity for + /// each location in the index-range. Used to pick the scheme for solving the + /// rate equations and in setting the max allowed chemistry-timstep + double* ddom; + + /// buffers to hold time derivatives of free electron and HI mass densities + /// for index_range + double *dedot, *HIdot; + /// buffers to hold time derivatives of free electron and HI mass densities + /// for index_range computed during the previous cycle + double *dedot_prev, *HIdot_prev; + + /// buffer used to track the rate of H2 formation on dust grains + double* h2dust; + + /// scratch space used only within lookup_cool_rates1d_g. This is 14 times + /// larger than most of the other buffers. + /// + /// (with minimal refactoring, this buffer could probably be removed) + double *k13dd; + + /// iteration mask denoting where the Newton-Raphson scheme will be used + gr_mask_type* itmask_nr; + + /// buffer specifying how the Newton-Raphson scheme handles energy evolution + int* imp_eng; + +}; + +/// allocates the contents of a new SpeciesRateSolverScratchBuf +/// +/// @param nelem The number of elements a buffer is expected to have in order +/// to store values for the standard sized index-range +SpeciesRateSolverScratchBuf new_SpeciesRateSolverScratchBuf(int nelem) { + SpeciesRateSolverScratchBuf out; + + out.ddom = (double*)malloc(sizeof(double)*nelem); + + out.dedot = (double*)malloc(sizeof(double)*nelem); + out.HIdot = (double*)malloc(sizeof(double)*nelem); + out.dedot_prev = (double*)malloc(sizeof(double)*nelem); + out.HIdot_prev = (double*)malloc(sizeof(double)*nelem); + + out.k13dd = (double*)malloc(sizeof(double)*nelem*14); + + out.h2dust = (double*)malloc(sizeof(double)*nelem); + + out.itmask_nr = (gr_mask_type*)malloc(sizeof(gr_mask_type)*nelem); + + out.imp_eng = (int*)malloc(sizeof(int)*nelem); + + return out; +} + +/// performs cleanup of the contents of SpeciesRateSolverScratchBuf +/// +/// This effectively invokes a destructor +void drop_SpeciesRateSolverScratchBuf(SpeciesRateSolverScratchBuf* ptr) { + free(ptr->ddom); + + free(ptr->dedot); + free(ptr->HIdot); + free(ptr->dedot_prev); + free(ptr->HIdot_prev); + + free(ptr->k13dd); + + free(ptr->h2dust); + + free(ptr->itmask_nr); + free(ptr->imp_eng); +} + + +} // namespace grackle::impl // ------------------------------------------------------------- @@ -507,16 +605,6 @@ int solve_rate_cool_g( std::vector dust2gas(my_fields->grid_dimension[0]); std::vector rhoH(my_fields->grid_dimension[0]); std::vector mmw(my_fields->grid_dimension[0]); - std::vector ddom(my_fields->grid_dimension[0]); - - // Rate equation row temporaries - - std::vector dedot(my_fields->grid_dimension[0]); - std::vector HIdot(my_fields->grid_dimension[0]); - std::vector dedot_prev(my_fields->grid_dimension[0]); - std::vector HIdot_prev(my_fields->grid_dimension[0]); - std::vector k13dd(my_fields->grid_dimension[0] * 14); - std::vector h2dust(my_fields->grid_dimension[0]); // Cooling/heating row locals @@ -526,11 +614,8 @@ int solve_rate_cool_g( std::vector itmask(my_fields->grid_dimension[0]); std::vector itmask_tmp(my_fields->grid_dimension[0]); - std::vector itmask_nr(my_fields->grid_dimension[0]); std::vector itmask_metal(my_fields->grid_dimension[0]); - std::vector imp_eng(my_fields->grid_dimension[0]); - // \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\///////////////////////////////// // ======================================================================= @@ -591,10 +676,6 @@ int solve_rate_cool_g( //_// PORT: !$omp parallel do schedule(runtime) private( //_// PORT: !$omp& dtit, ttot, p2d, tgas, //_// PORT: !$omp& tdust, metallicity, dust2gas, rhoH, mmw, - //_// PORT: !$omp& ddom, - //_// PORT: !$omp& dep, dedot,HIdot, dedot_prev, - //_// PORT: !$omp& HIdot_prev, - //_// PORT: !$omp& k13dd, h2dust, //_// PORT: !$omp& edot, //_// PORT: !$omp& itmask, itmask_metal ) //_// PORT: #endif @@ -641,6 +722,15 @@ int solve_rate_cool_g( grackle::impl::ChemHeatingRates chemheatrates_buf = grackle::impl::new_ChemHeatingRates(my_fields->grid_dimension[0]); + // holds buffers exclusively used for solving species rate equations + // (i.e. in the future, we could have the constructor skip allocations of + // all contained data structures when using primordial_chemistry == 0) + grackle::impl::SpeciesRateSolverScratchBuf spsolvbuf = + grackle::impl::new_SpeciesRateSolverScratchBuf( + my_fields->grid_dimension[0] + ); + + //_// TODO_USE: OMP_PRAGMA("omp for") for (int t = 0; t < idx_helper.outer_ind_size; t++) { // construct an index-range corresponding to "i-slice" @@ -669,7 +759,7 @@ int solve_rate_cool_g( // A useful slice variable since we do this a lot for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { - ddom[i] = d(i,j,k) * dom; + spsolvbuf.ddom[i] = d(i,j,k) * dom; } // declare 2 variables (primarily used for subcycling, but also used in @@ -688,7 +778,6 @@ int solve_rate_cool_g( } // Compute the cooling rate, tgas, tdust, and metallicity for this row - wrapped_cool1d_multi_g_( imetal, idx_range, iter, edot.data(), tgas.data(), mmw.data(), p2d.data(), tdust.data(), metallicity.data(), @@ -704,7 +793,8 @@ int solve_rate_cool_g( // -> There's room for optimization: when ispecies is 0, there is // need to ever touch this variable. But, for now we focus on // correct behavior before implementing this optimization - std::memcpy(itmask_tmp.data(), itmask.data(), sizeof(gr_mask_type)*my_fields->grid_dimension[0]); + std::memcpy(itmask_tmp.data(), itmask.data(), + sizeof(gr_mask_type)*my_fields->grid_dimension[0]); } else { @@ -712,8 +802,8 @@ int solve_rate_cool_g( // (maybe should add itmask to this call) wrapped_lookup_cool_rates1d_g_( - idx_range, anydust, tgas.data(), mmw.data(), - tdust.data(), dust2gas.data(), k13dd.data(), h2dust.data(), + idx_range, anydust, tgas.data(), mmw.data(), tdust.data(), + dust2gas.data(), spsolvbuf.k13dd, spsolvbuf.h2dust, dom, dx_cgs, c_ljeans, itmask.data(), itmask_metal.data(), imetal, rhoH.data(), dt, my_chemistry, my_rates, my_fields, *my_uvb_rates, internalu, grain_growth_rates, grain_temperatures, @@ -724,10 +814,10 @@ int solve_rate_cool_g( // (should add itmask to this call) wrapped_rate_timestep_g_( - dedot.data(), HIdot.data(), anydust, idx_range, h2dust.data(), - rhoH.data(), itmask.data(), edot.data(), chunit, dom, - my_chemistry, my_fields, *my_uvb_rates, kcr_buf, kshield_buf, - chemheatrates_buf + spsolvbuf.dedot, spsolvbuf.HIdot, anydust, idx_range, + spsolvbuf.h2dust, rhoH.data(), itmask.data(), edot.data(), + chunit, dom, my_chemistry, my_fields, *my_uvb_rates, kcr_buf, + kshield_buf, chemheatrates_buf ); // First, copy itmask's current values into a temporary array @@ -738,10 +828,10 @@ int solve_rate_cool_g( // // (the values stored within itmask will change within the function) select_chem_scheme_update_masks_( - idx_range, itmask.data(), itmask_nr.data(), imp_eng.data(), + idx_range, itmask.data(), spsolvbuf.itmask_nr, spsolvbuf.imp_eng, itmask_tmp.data(), my_fields->grid_dimension[0], imetal, - min_metallicity, ddom.data(), tgas.data(), metallicity.data(), - my_chemistry + min_metallicity, spsolvbuf.ddom, tgas.data(), + metallicity.data(), my_chemistry ); // Set the max timestep for the current subcycle based on our scheme @@ -752,10 +842,12 @@ int solve_rate_cool_g( set_subcycle_dt_from_chemistry_scheme_( dtit.data(), idx_range, iter, dt, ttot.data(), itmask.data(), - itmask_nr.data(), imp_eng.data(), dedot.data(), HIdot.data(), - dedot_prev.data(), HIdot_prev.data(), ddom.data(), tgas.data(), - p2d.data(), edot.data(), my_chemistry, my_rates, dlogtem, - logTlininterp_buf, my_fields, kcr_buf + spsolvbuf.itmask_nr, spsolvbuf.imp_eng, + spsolvbuf.dedot, spsolvbuf.HIdot, + spsolvbuf.dedot_prev, spsolvbuf.HIdot_prev, + spsolvbuf.ddom, tgas.data(), p2d.data(), edot.data(), + my_chemistry, my_rates, dlogtem, logTlininterp_buf, my_fields, + kcr_buf ); } @@ -768,13 +860,11 @@ int solve_rate_cool_g( ); // Update total and gas energy - + // -> zones that will use Newton-Raphson scheme are ignored if (my_chemistry->with_radiative_cooling == 1) { for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { - if (itmask[i] != MASK_FALSE) { - e(i,j,k) = e(i,j,k) + - (gr_float)(edot[i]/d(i,j,k)*dtit[i] ); - + if (itmask[i] != MASK_FALSE) { + e(i,j,k) = e(i,j,k) + (gr_float)(edot[i]/d(i,j,k)*dtit[i]); } } } @@ -786,8 +876,8 @@ int solve_rate_cool_g( // itmask) wrapped_step_rate_g_( - dtit.data(), idx_range, anydust, h2dust.data(), rhoH.data(), - dedot_prev.data(), HIdot_prev.data(), itmask.data(), + dtit.data(), idx_range, anydust, spsolvbuf.h2dust, rhoH.data(), + spsolvbuf.dedot_prev, spsolvbuf.HIdot_prev, itmask.data(), itmask_metal.data(), imetal, my_chemistry, my_fields, *my_uvb_rates, grain_growth_rates, species_tmpdens, kcr_buf, kshield_buf @@ -801,8 +891,8 @@ int solve_rate_cool_g( imetal, idx_range, iter, dom, chunit, dx_cgs, c_ljeans, dtit.data(), p2d.data(), tgas.data(), tdust.data(), metallicity.data(), dust2gas.data(), rhoH.data(), mmw.data(), - h2dust.data(), edot.data(), anydust, itmask_nr.data(), - itmask_metal.data(), imp_eng.data(), my_chemistry, my_rates, + spsolvbuf.h2dust, edot.data(), anydust, spsolvbuf.itmask_nr, + itmask_metal.data(), spsolvbuf.imp_eng, my_chemistry, my_rates, my_fields, *my_uvb_rates, internalu, grain_temperatures, logTlininterp_buf, cool1dmulti_buf, coolingheating_buf, chemheatrates_buf @@ -810,23 +900,23 @@ int solve_rate_cool_g( } - // return itmask + // restore the values of itmask from the backup that we made earlier for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { itmask[i] = itmask_tmp[i]; } // Add the timestep to the elapsed time for each cell and find // minimum elapsed time step in this row - ttmin = huge8; for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { ttot[i] = std::fmin(ttot[i] + dtit[i], dt); - if (std::fabs(dt-ttot[i]) < - tolerance*dt) { itmask[i] = MASK_FALSE; } + + if (std::fabs(dt-ttot[i]) < tolerance*dt) { itmask[i] = MASK_FALSE; } + if (ttot[i] Date: Fri, 24 Jan 2025 08:50:57 -0500 Subject: [PATCH 091/101] solve_rate_cool_g: finished aggregating local variables just used for solving rates --- src/clib/solve_rate_cool_g-cpp.C | 82 +++++++++++++++++--------------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index f6485deb6..d996404dd 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -515,6 +515,27 @@ struct SpeciesRateSolverScratchBuf { /// buffer specifying how the Newton-Raphson scheme handles energy evolution int* imp_eng; + // buffers in the following data structure are used to temporarily hold + // the evolved density of various species as we evolve over a subcycle + // (currently only used by step_rate_g) + grackle::impl::SpeciesCollection species_tmpdens; + + // buffers in the following data structure are used to temporarily hold + // the interpolated Collisional/Recombination Rates that have been + // interpolated using the standard 1D log temperature table. + grackle::impl::ColRecRxnRateCollection kcr_buf; + + // buffers in the following data structure are used to temporarily hold + // the computed radiative reaction rates + grackle::impl::PhotoRxnRateCollection kshield_buf; + + // buffers in the following data structure are used to temporarily hold + // the interpolated chemistry-heating rates at each index-range location + grackle::impl::ChemHeatingRates chemheatrates_buf; + + // holds computed grain growth/destruction rates: + grackle::impl::GrainSpeciesCollection grain_growth_rates; + }; /// allocates the contents of a new SpeciesRateSolverScratchBuf @@ -539,6 +560,12 @@ SpeciesRateSolverScratchBuf new_SpeciesRateSolverScratchBuf(int nelem) { out.imp_eng = (int*)malloc(sizeof(int)*nelem); + out.species_tmpdens = grackle::impl::new_SpeciesCollection(nelem); + out.kcr_buf = grackle::impl::new_ColRecRxnRateCollection(nelem); + out.kshield_buf = grackle::impl::new_PhotoRxnRateCollection(nelem); + out.chemheatrates_buf = grackle::impl::new_ChemHeatingRates(nelem); + out.grain_growth_rates = grackle::impl::new_GrainSpeciesCollection(nelem); + return out; } @@ -559,6 +586,12 @@ void drop_SpeciesRateSolverScratchBuf(SpeciesRateSolverScratchBuf* ptr) { free(ptr->itmask_nr); free(ptr->imp_eng); + + grackle::impl::drop_SpeciesCollection(&ptr->species_tmpdens); + grackle::impl::drop_ColRecRxnRateCollection(&ptr->kcr_buf); + grackle::impl::drop_PhotoRxnRateCollection(&ptr->kshield_buf); + grackle::impl::drop_ChemHeatingRates(&ptr->chemheatrates_buf); + grackle::impl::drop_GrainSpeciesCollection(&ptr->grain_growth_rates); } @@ -688,10 +721,6 @@ int solve_rate_cool_g( grackle::impl::GrainSpeciesCollection grain_temperatures = grackle::impl::new_GrainSpeciesCollection(my_fields->grid_dimension[0]); - // holds computed grain growth/destruction rates: - grackle::impl::GrainSpeciesCollection grain_growth_rates = - grackle::impl::new_GrainSpeciesCollection(my_fields->grid_dimension[0]); - grackle::impl::LogTLinInterpScratchBuf logTlininterp_buf = grackle::impl::new_LogTLinInterpScratchBuf(my_fields->grid_dimension[0]); @@ -701,27 +730,6 @@ int solve_rate_cool_g( grackle::impl::CoolHeatScratchBuf coolingheating_buf = grackle::impl::new_CoolHeatScratchBuf(my_fields->grid_dimension[0]); - // buffers in the following data structure are used to temporarily hold - // the evolved density of various species as we evolve over a subcycle - grackle::impl::SpeciesCollection species_tmpdens = - grackle::impl::new_SpeciesCollection(my_fields->grid_dimension[0]); - - // buffers in the following data structure are used to temporarily hold - // the interpolated Collisional/Recombination Rates that have interpolated - // using the standard 1D log temperature table. - grackle::impl::ColRecRxnRateCollection kcr_buf = - grackle::impl::new_ColRecRxnRateCollection(my_fields->grid_dimension[0]); - - // buffers in the following data structure are used to temporarily hold - // the computed radiative reaction rates - grackle::impl::PhotoRxnRateCollection kshield_buf = - grackle::impl::new_PhotoRxnRateCollection(my_fields->grid_dimension[0]); - - // buffers in the following data structure are used to temporarily hold - // the interpolated chemistry-heating rates at each islice zone - grackle::impl::ChemHeatingRates chemheatrates_buf = - grackle::impl::new_ChemHeatingRates(my_fields->grid_dimension[0]); - // holds buffers exclusively used for solving species rate equations // (i.e. in the future, we could have the constructor skip allocations of // all contained data structures when using primordial_chemistry == 0) @@ -806,8 +814,9 @@ int solve_rate_cool_g( dust2gas.data(), spsolvbuf.k13dd, spsolvbuf.h2dust, dom, dx_cgs, c_ljeans, itmask.data(), itmask_metal.data(), imetal, rhoH.data(), dt, my_chemistry, my_rates, my_fields, - *my_uvb_rates, internalu, grain_growth_rates, grain_temperatures, - logTlininterp_buf, kcr_buf, kshield_buf, chemheatrates_buf + *my_uvb_rates, internalu, spsolvbuf.grain_growth_rates, + grain_temperatures, logTlininterp_buf, spsolvbuf.kcr_buf, + spsolvbuf.kshield_buf, spsolvbuf.chemheatrates_buf ); // Compute dedot and HIdot, the rates of change of de and HI @@ -816,8 +825,9 @@ int solve_rate_cool_g( wrapped_rate_timestep_g_( spsolvbuf.dedot, spsolvbuf.HIdot, anydust, idx_range, spsolvbuf.h2dust, rhoH.data(), itmask.data(), edot.data(), - chunit, dom, my_chemistry, my_fields, *my_uvb_rates, kcr_buf, - kshield_buf, chemheatrates_buf + chunit, dom, my_chemistry, my_fields, *my_uvb_rates, + spsolvbuf.kcr_buf, spsolvbuf.kshield_buf, + spsolvbuf.chemheatrates_buf ); // First, copy itmask's current values into a temporary array @@ -847,7 +857,7 @@ int solve_rate_cool_g( spsolvbuf.dedot_prev, spsolvbuf.HIdot_prev, spsolvbuf.ddom, tgas.data(), p2d.data(), edot.data(), my_chemistry, my_rates, dlogtem, logTlininterp_buf, my_fields, - kcr_buf + spsolvbuf.kcr_buf ); } @@ -879,8 +889,8 @@ int solve_rate_cool_g( dtit.data(), idx_range, anydust, spsolvbuf.h2dust, rhoH.data(), spsolvbuf.dedot_prev, spsolvbuf.HIdot_prev, itmask.data(), itmask_metal.data(), imetal, my_chemistry, my_fields, - *my_uvb_rates, grain_growth_rates, species_tmpdens, kcr_buf, - kshield_buf + *my_uvb_rates, spsolvbuf.grain_growth_rates, + spsolvbuf.species_tmpdens, spsolvbuf.kcr_buf, spsolvbuf.kshield_buf ); // Solve rate equations with one linearly implicit Gauss-Seidel @@ -895,7 +905,7 @@ int solve_rate_cool_g( itmask_metal.data(), spsolvbuf.imp_eng, my_chemistry, my_rates, my_fields, *my_uvb_rates, internalu, grain_temperatures, logTlininterp_buf, cool1dmulti_buf, coolingheating_buf, - chemheatrates_buf + spsolvbuf.chemheatrates_buf ); } @@ -964,14 +974,10 @@ int solve_rate_cool_g( // cleanup manually allocated temporaries grackle::impl::drop_GrainSpeciesCollection(&grain_temperatures); - grackle::impl::drop_GrainSpeciesCollection(&grain_growth_rates); grackle::impl::drop_LogTLinInterpScratchBuf(&logTlininterp_buf); grackle::impl::drop_Cool1DMultiScratchBuf(&cool1dmulti_buf); grackle::impl::drop_CoolHeatScratchBuf(&coolingheating_buf); - grackle::impl::drop_SpeciesCollection(&species_tmpdens); - grackle::impl::drop_ColRecRxnRateCollection(&kcr_buf); - grackle::impl::drop_PhotoRxnRateCollection(&kshield_buf); - grackle::impl::drop_ChemHeatingRates(&chemheatrates_buf); + grackle::impl::drop_SpeciesRateSolverScratchBuf(&spsolvbuf); From 0897411075da49c8df7fdc50d4d7b1b08df9ef10 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 24 Jan 2025 11:03:47 -0500 Subject: [PATCH 092/101] solve_rate_cool_g: describe potential refactoring Add comments describing 2 potential ways to slightly refactor the logic of `solve_rate_cool_g` (I chose to make these comments since I'm trying to avoid changing/reordering logic in this commit-set) --- src/clib/solve_rate_cool_g-cpp.C | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index d996404dd..9d7ff5db1 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -765,7 +765,7 @@ int solve_rate_cool_g( } // A useful slice variable since we do this a lot - + // -> we don't need it for primordial_chemistry==0 for (int i = idx_range.i_start; i < idx_range.i_stop; i++) { spsolvbuf.ddom[i] = d(i,j,k) * dom; } @@ -798,9 +798,8 @@ int solve_rate_cool_g( if (my_chemistry->primordial_chemistry == 0) { // This is some basic book-keeping to ensure that itmask_tmp has // sensible values when ispecies is 0 - // -> There's room for optimization: when ispecies is 0, there is - // need to ever touch this variable. But, for now we focus on - // correct behavior before implementing this optimization + // -> see the comment following this if-else statement suggesting how + // we could refactor itmask-handling (eliminating this branch) std::memcpy(itmask_tmp.data(), itmask.data(), sizeof(gr_mask_type)*my_fields->grid_dimension[0]); @@ -861,6 +860,22 @@ int solve_rate_cool_g( ); } + // TODO: Consider refactoring the iteration mask handling: + // 1. introduce `itmask_gs` as a member of `spsolvbuf` and have the + // `select_chem_scheme_update_masks_` function store locations in + // `spsolvbuf.itmask_gs` where we will apply Gauss-Seidel scheme + // 2. replace `itmask` with `spsolvbuf.itmask_gs` in arg-lists of + // `set_subcycle_dt_from_chemistry_scheme_` & `wrapped_step_rate_g_` + // 3. insert following chunk of logic RIGHT HERE + // > const gr_mask_type* energy_itmask = + // > (my_chemistry->primordial_chemistry == 0) + // > ? itmask.data() : spsolvbuf.itmask_gs; + // 4. replace `itmask` with `energy_itmask` in arg-list of + // `enforce_max_heatcool_subcycle_dt_` & within energy update loop + // 5. stop modifying `itmask` in `select_chem_scheme_update_masks_` + // 6. remove declaration of `itmask_tmp`, logic that initializes, and + // the loop that uses it to override `itmask` + // Update dtit (the current subcycle timestep) to ensure it doesn't // exceed the max timestep for cooling/heating // -> zones that will use Newton-Raphson scheme are ignored From a39801e5e65817f9d7dd1d4c00e693baa2e0d23b Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 24 Jan 2025 11:12:29 -0500 Subject: [PATCH 093/101] solve_rate_cool_g: moved View creation of radiation field into the helper function where it is used. --- src/clib/solve_rate_cool_g-cpp.C | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 9d7ff5db1..f4c3d324e 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -425,16 +425,22 @@ static void set_subcycle_dt_from_chemistry_scheme_( /// /// @param[out] itmask the mask that will be overriden /// @param[in] idx_range specifies the index-range -/// @param[in] kphHI view of the HI photo-ionization rate field /// @param[in] my_chemistry specifies grackle settings (we probably don't need /// to pass in everything) +/// @param[in] my_fields used to access HI photo-ionization rate field + static inline void coupled_rt_modify_itmask_( gr_mask_type* itmask, IndexRange idx_range, - grackle::impl::View kphHI, - const chemistry_data* my_chemistry + const chemistry_data* my_chemistry, + grackle_field_data* my_fields ) { + grackle::impl::View kphHI(my_fields->RT_HI_ionization_rate, + my_fields->grid_dimension[0], + my_fields->grid_dimension[1], + my_fields->grid_dimension[2]); + // adjust iteration mask if the caller indicates that they're using // Grackle in a coupled radiative-transfer/chemistry-energy calculation // (that has intermediate steps) @@ -614,11 +620,7 @@ int solve_rate_cool_g( grackle::impl::View d(my_fields->density, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); grackle::impl::View e(my_fields->internal_energy, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); - - // Radiative transfer fields - - grackle::impl::View kphHI(my_fields->RT_HI_ionization_rate, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); - + // Constants #ifdef GRACKLE_FLOAT_4 @@ -756,7 +758,8 @@ int solve_rate_cool_g( // adjust iteration mask (but only if using Grackle in a coupled // radiative-transfer calculation) - coupled_rt_modify_itmask_(itmask.data(), idx_range, kphHI, my_chemistry); + coupled_rt_modify_itmask_(itmask.data(), idx_range, my_chemistry, + my_fields); // Set time elapsed to zero for each cell in 1D section From 5c71925ffad0c2cb03b35e6029d35ffaf8cfa705 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Fri, 24 Jan 2025 11:40:27 -0500 Subject: [PATCH 094/101] solve_rate_cool_g: finish most reloactions -- this function should now theoretically run with OpenMP --- src/clib/solve_rate_cool_g-cpp.C | 84 ++++++++++++++------------------ 1 file changed, 37 insertions(+), 47 deletions(-) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index f4c3d324e..3c7a3616e 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -616,44 +616,12 @@ int solve_rate_cool_g( ) { - // Density, energy and velocity fields fields - - grackle::impl::View d(my_fields->density, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); - grackle::impl::View e(my_fields->internal_energy, my_fields->grid_dimension[0], my_fields->grid_dimension[1], my_fields->grid_dimension[2]); - - // Constants - #ifdef GRACKLE_FLOAT_4 const gr_float tolerance = (gr_float)(1.0e-05); #else const gr_float tolerance = (gr_float)(1.0e-10); #endif - // row temporaries - - std::vector dtit(my_fields->grid_dimension[0]); - std::vector ttot(my_fields->grid_dimension[0]); - std::vector p2d(my_fields->grid_dimension[0]); - std::vector tgas(my_fields->grid_dimension[0]); - std::vector tdust(my_fields->grid_dimension[0]); - std::vector metallicity(my_fields->grid_dimension[0]); - std::vector dust2gas(my_fields->grid_dimension[0]); - std::vector rhoH(my_fields->grid_dimension[0]); - std::vector mmw(my_fields->grid_dimension[0]); - - // Cooling/heating row locals - - std::vector edot(my_fields->grid_dimension[0]); - - // Iteration mask - - std::vector itmask(my_fields->grid_dimension[0]); - std::vector itmask_tmp(my_fields->grid_dimension[0]); - std::vector itmask_metal(my_fields->grid_dimension[0]); - - // \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\///////////////////////////////// - // ======================================================================= - // Set error indicator (we will return this value) int ierr = GR_SUCCESS; @@ -704,20 +672,10 @@ int solve_rate_cool_g( const grackle_index_helper idx_helper = build_index_helper_(my_fields); - // parallelize the k and j loops with OpenMP - // flat j and k loops for better parallelism - //_// PORT: #ifdef _OPENMP - //_// PORT: ! ierr is declared as shared and should be modified with atomic operation - //_// PORT: !$omp parallel do schedule(runtime) private( - //_// PORT: !$omp& dtit, ttot, p2d, tgas, - //_// PORT: !$omp& tdust, metallicity, dust2gas, rhoH, mmw, - //_// PORT: !$omp& edot, - //_// PORT: !$omp& itmask, itmask_metal ) - //_// PORT: #endif - //_// TODO_USE: OMP_PRAGMA("omp parallel") + OMP_PRAGMA("omp parallel") { - // TODO: move more relevant variable declarations to here to replace the - // OMP private-clause + // each OMP thread separately initializes/allocates variables defined in + // the current scope and then enters the for-loop // holds computed grain temperatures: grackle::impl::GrainSpeciesCollection grain_temperatures = @@ -740,8 +698,41 @@ int solve_rate_cool_g( my_fields->grid_dimension[0] ); + // the following variables aren't embedded in structs because they are used + // in a number of different internal routines. Sorting these into + // additional structs (or leaving them free-standing) will become more + // obvious as we transcribe more routines. + std::vector dtit(my_fields->grid_dimension[0]); + std::vector ttot(my_fields->grid_dimension[0]); + std::vector p2d(my_fields->grid_dimension[0]); + std::vector tgas(my_fields->grid_dimension[0]); + std::vector tdust(my_fields->grid_dimension[0]); + std::vector metallicity(my_fields->grid_dimension[0]); + std::vector dust2gas(my_fields->grid_dimension[0]); + std::vector rhoH(my_fields->grid_dimension[0]); + std::vector mmw(my_fields->grid_dimension[0]); + std::vector edot(my_fields->grid_dimension[0]); + + // iteration masks + std::vector itmask(my_fields->grid_dimension[0]); + std::vector itmask_tmp(my_fields->grid_dimension[0]); + std::vector itmask_metal(my_fields->grid_dimension[0]); + + // create views of density and internal energy fields to support 3D access + grackle::impl::View d(my_fields->density, + my_fields->grid_dimension[0], + my_fields->grid_dimension[1], + my_fields->grid_dimension[2]); + grackle::impl::View e(my_fields->internal_energy, + my_fields->grid_dimension[0], + my_fields->grid_dimension[1], + my_fields->grid_dimension[2]); - //_// TODO_USE: OMP_PRAGMA("omp for") + // The following for-loop is a flattened loop over every k,j combination. + // OpenMP divides this loop between all threads. Within the loop, we + // complete calculations for the constructed index-range construct + // (an index range corresponds to an "i-slice") + OMP_PRAGMA("omp for schedule(runtime)") for (int t = 0; t < idx_helper.outer_ind_size; t++) { // construct an index-range corresponding to "i-slice" const IndexRange idx_range = make_idx_range_(t, &idx_helper); @@ -998,7 +989,6 @@ int solve_rate_cool_g( grackle::impl::drop_SpeciesRateSolverScratchBuf(&spsolvbuf); - } // OMP_PRAGMA("omp parallel") // If an error has been produced, return now. From e38de31f0516aedc4e67d4652944d9d9b3bb09b5 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sat, 25 Jan 2025 13:15:26 -0500 Subject: [PATCH 095/101] address some namespace topics. --- src/clib/fortran_func_wrappers.hpp | 23 ++++++++++++++--------- src/clib/solve_rate_cool_g-cpp.C | 26 ++++++++++++++------------ src/clib/solve_rate_cool_g-cpp.h | 6 ++++++ 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/clib/fortran_func_wrappers.hpp b/src/clib/fortran_func_wrappers.hpp index 25576188c..326165cb9 100644 --- a/src/clib/fortran_func_wrappers.hpp +++ b/src/clib/fortran_func_wrappers.hpp @@ -29,7 +29,12 @@ #include "internal_units.h" #include "LUT.hpp" -inline void wrapped_ceiling_species_g_( + +// callers of these functions are generally expected to locally shorten the +// namespace name when they call these routines +namespace grackle::impl::fortran_wrapper { + +inline void ceiling_species_g( int imetal, chemistry_data* my_chemistry, grackle_field_data* my_fields ) { @@ -56,7 +61,7 @@ inline void wrapped_ceiling_species_g_( } -inline void wrapped_cool1d_multi_g_( +inline void cool1d_multi_g( int imetal, IndexRange idx_range, int iter, double* edot, double* tgas, double* mmw, double* p2d, double* tdust, double* metallicity, double* dust2gas, double* rhoH, gr_mask_type* itmask, @@ -187,7 +192,7 @@ inline void wrapped_cool1d_multi_g_( /// This routine uses the temperature to look up the chemical rates that are /// tabulated in a log table as a function of temperature -inline void wrapped_lookup_cool_rates1d_g_( +inline void lookup_cool_rates1d_g( IndexRange idx_range, gr_mask_type anydust, double* tgas1d, double* mmw, double* tdust, double* dust2gas, double* k13dd, double* h2dust, double dom, double dx_cgs, double c_ljeans, gr_mask_type* itmask, @@ -296,7 +301,7 @@ inline void wrapped_lookup_cool_rates1d_g_( // the following case was handcoded (so the argument order may shift when we // actually transcribe the routine) -inline void wrapped_make_consistent_g_( +inline void make_consistent_g( int imetal, double dom, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, grackle_field_data* my_fields ){ @@ -330,7 +335,7 @@ inline void wrapped_make_consistent_g_( /// This routine calculates the electron and HI rates of change in order to /// determine the maximum permitted timestep -inline void wrapped_rate_timestep_g_( +inline void rate_timestep_g( double* dedot, double* HIdot, gr_mask_type anydust, IndexRange idx_range, double* h2dust, double* rhoH, gr_mask_type* itmask, double* edot, double chunit, double dom, chemistry_data* my_chemistry, @@ -361,7 +366,7 @@ inline void wrapped_rate_timestep_g_( } -inline void wrapped_scale_fields_g_( +inline void scale_fields_g( int imetal, gr_float factor, chemistry_data* my_chemistry, grackle_field_data* my_fields ) { @@ -390,7 +395,7 @@ inline void wrapped_scale_fields_g_( /// Uses one linearly implicit Gauss-Seidel sweep of a backward-Euler time /// integrator to advance the rate equations by one (sub-)cycle (dtit). -inline void wrapped_step_rate_g_( +inline void step_rate_g( double* dtit, IndexRange idx_range, gr_mask_type anydust, double* h2dust, double* rhoH, double* dedot_prev, double* HIdot_prev, gr_mask_type* itmask, gr_mask_type* itmask_metal, int imetal, @@ -460,7 +465,7 @@ inline void wrapped_step_rate_g_( } -inline void wrapped_step_rate_newton_raphson_( +inline void step_rate_newton_raphson( int imetal, IndexRange idx_range, int iter, double dom, double chunit, double dx_cgs, double c_ljeans, double* dtit, double* p2d, double* tgas, double* tdust, double* metallicity, double* dust2gas, double* rhoH, @@ -580,7 +585,7 @@ inline void wrapped_step_rate_newton_raphson_( } - +} // namespace grackle::impl::fortran_wrapper #endif /* FORTRAN_FUNC_WRAPPERS_HPP */ diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index 3c7a3616e..2eda847ca 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -615,6 +615,8 @@ int solve_rate_cool_g( grackle_field_data* my_fields, photo_rate_storage* my_uvb_rates ) { + // shorten `grackle::impl::fortran_wrapper` to `f_wrap` within this function + namespace f_wrap = ::grackle::impl::fortran_wrapper; #ifdef GRACKLE_FLOAT_4 const gr_float tolerance = (gr_float)(1.0e-05); @@ -654,7 +656,7 @@ int solve_rate_cool_g( #define ABUNDANCE_CORRECTION #ifdef ABUNDANCE_CORRECTION - wrapped_make_consistent_g_(imetal, dom, my_chemistry, my_rates, my_fields); + f_wrap::make_consistent_g(imetal, dom, my_chemistry, my_rates, my_fields); #endif } @@ -663,11 +665,11 @@ int solve_rate_cool_g( if (internalu.extfields_in_comoving == 1) { gr_float factor = (gr_float)(std::pow(internalu.a_value,(-3)) ); - wrapped_scale_fields_g_(imetal, factor, my_chemistry, my_fields); + f_wrap::scale_fields_g(imetal, factor, my_chemistry, my_fields); } #ifdef ABUNDANCE_CORRECTION - wrapped_ceiling_species_g_(imetal, my_chemistry, my_fields); + f_wrap::ceiling_species_g(imetal, my_chemistry, my_fields); #endif const grackle_index_helper idx_helper = build_index_helper_(my_fields); @@ -780,7 +782,7 @@ int solve_rate_cool_g( } // Compute the cooling rate, tgas, tdust, and metallicity for this row - wrapped_cool1d_multi_g_( + f_wrap::cool1d_multi_g( imetal, idx_range, iter, edot.data(), tgas.data(), mmw.data(), p2d.data(), tdust.data(), metallicity.data(), dust2gas.data(), rhoH.data(), itmask.data(), itmask_metal.data(), @@ -802,7 +804,7 @@ int solve_rate_cool_g( // Look-up rates as a function of temperature for 1D set of zones // (maybe should add itmask to this call) - wrapped_lookup_cool_rates1d_g_( + f_wrap::lookup_cool_rates1d_g( idx_range, anydust, tgas.data(), mmw.data(), tdust.data(), dust2gas.data(), spsolvbuf.k13dd, spsolvbuf.h2dust, dom, dx_cgs, c_ljeans, itmask.data(), itmask_metal.data(), @@ -815,7 +817,7 @@ int solve_rate_cool_g( // Compute dedot and HIdot, the rates of change of de and HI // (should add itmask to this call) - wrapped_rate_timestep_g_( + f_wrap::rate_timestep_g( spsolvbuf.dedot, spsolvbuf.HIdot, anydust, idx_range, spsolvbuf.h2dust, rhoH.data(), itmask.data(), edot.data(), chunit, dom, my_chemistry, my_fields, *my_uvb_rates, @@ -859,7 +861,7 @@ int solve_rate_cool_g( // `select_chem_scheme_update_masks_` function store locations in // `spsolvbuf.itmask_gs` where we will apply Gauss-Seidel scheme // 2. replace `itmask` with `spsolvbuf.itmask_gs` in arg-lists of - // `set_subcycle_dt_from_chemistry_scheme_` & `wrapped_step_rate_g_` + // `set_subcycle_dt_from_chemistry_scheme_` & `f_wrap::step_rate_g` // 3. insert following chunk of logic RIGHT HERE // > const gr_mask_type* energy_itmask = // > (my_chemistry->primordial_chemistry == 0) @@ -894,7 +896,7 @@ int solve_rate_cool_g( // sweep of a backward Euler method (for all cells specified by // itmask) - wrapped_step_rate_g_( + f_wrap::step_rate_g( dtit.data(), idx_range, anydust, spsolvbuf.h2dust, rhoH.data(), spsolvbuf.dedot_prev, spsolvbuf.HIdot_prev, itmask.data(), itmask_metal.data(), imetal, my_chemistry, my_fields, @@ -906,7 +908,7 @@ int solve_rate_cool_g( // sweep of a backward Euler method (for all cells specified by // itmask_nr) - wrapped_step_rate_newton_raphson_( + f_wrap::step_rate_newton_raphson( imetal, idx_range, iter, dom, chunit, dx_cgs, c_ljeans, dtit.data(), p2d.data(), tgas.data(), tdust.data(), metallicity.data(), dust2gas.data(), rhoH.data(), mmw.data(), @@ -1001,7 +1003,7 @@ int solve_rate_cool_g( if (internalu.extfields_in_comoving == 1) { gr_float factor = (gr_float)(std::pow(internalu.a_value,3) ); - wrapped_scale_fields_g_(imetal, factor, my_chemistry, my_fields); + f_wrap::scale_fields_g(imetal, factor, my_chemistry, my_fields); } if (my_chemistry->primordial_chemistry > 0) { @@ -1009,8 +1011,8 @@ int solve_rate_cool_g( // Correct the species to ensure consistency (i.e. type conservation) #ifdef ABUNDANCE_CORRECTION - wrapped_make_consistent_g_(imetal, dom, my_chemistry, my_rates, my_fields); - wrapped_ceiling_species_g_(imetal, my_chemistry, my_fields); + f_wrap::make_consistent_g(imetal, dom, my_chemistry, my_rates, my_fields); + f_wrap::ceiling_species_g(imetal, my_chemistry, my_fields); #endif } diff --git a/src/clib/solve_rate_cool_g-cpp.h b/src/clib/solve_rate_cool_g-cpp.h index ee5319bb3..76a2868ea 100644 --- a/src/clib/solve_rate_cool_g-cpp.h +++ b/src/clib/solve_rate_cool_g-cpp.h @@ -31,6 +31,12 @@ extern "C" { /// modified7: January, 2025 by Matthew Abruzzo; ported to C++ /// /// @return Returns GR_SUCCESS or GR_FAIL to indicate whether there was an error +/// +/// @todo +/// Once the file where this routine called is adjusted to be compiled with a +/// C++ compiler, modify this function (prototype & implementation) such that: +/// - it's not enclosed by a `extern "C"` block +/// - it's defined within a `grackle::impl` namespace int solve_rate_cool_g( int imetal, double dt, InternalGrUnits internalu, chemistry_data* my_chemistry, chemistry_data_storage* my_rates, From a2fb3e1f6ebd2295d7768bba25b4460eff79cc00 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sat, 25 Jan 2025 16:25:53 -0500 Subject: [PATCH 096/101] convert SpLUT to a struct (from a namespace) --- src/clib/LUT.hpp | 234 +++++++++++++++++++++++++---------------------- 1 file changed, 126 insertions(+), 108 deletions(-) diff --git a/src/clib/LUT.hpp b/src/clib/LUT.hpp index dd95003a4..75b19ce17 100644 --- a/src/clib/LUT.hpp +++ b/src/clib/LUT.hpp @@ -10,114 +10,132 @@ #error "This file can only be read by a c++ compiler" #endif -// This namespace holds enumerators defined for every species. The enumerator -// values can be used in a lookup table. -// - the idea here is that we can create arrays to hold data for every species -// (rather than structs). -// - in this picture, we can use the enumerator values to lookup the value -// corresponding to a given species (for example, we could use `SpLUT::HI` -// to look up values related to HI). This essentially gives us all the -// benefits of a struct (instead of an array). -// - if we are using an array to organize data instead of a struct, we can -// then write certain operations that are identical for all species (e.g. -// allocating buffers, deallocating buffers, initializing buffers) in a much -// more concise (more maintainable) way. -// -// IMPORTANT: this is a private implementation detail. We should never -// unintentionally expose this information directly in the public API (i.e. we -// want to maintain our flexibility). With that said, there are hypothetical -// scenarios where we might deliberately expose it (for performance purposes) -// -- but we should think long and hard about it before we do -// -// Why use a namespace? -// -------------------- -// - it lets us use short/simple names for each enumerator without feat of -// collision -// - we later have the option to convert SpLUT from a namespace to a struct -// (it still contains the enumerators). -// - This would be useful if we wanted to write a template function that -// accepts different choices of LUTs as a template argument (without -// modifying any existing code). This would be hypothetically useful if we -// wanted to create specializations of the code that only have LUTs for -// a subset of species (e.g. based on primordial_chemistry == 1) -// - we currently have no plans to do this. -// -// Other Thoughts -// -------------- -// - maybe we should store the dust species in a separate lookup table? -// - adopting data-structures that leverage this LUT, is probably an important -// stepping stone to implementing a system where reaction rates are -// dynamically specified. After adopting such a system, we may not even need -// a lookup table any more (this all assumes, of course, that such a system -// is adequately performant) -namespace SpLUT{ - -// in the future, we may want to reimplement the following in terms of the -// XMacros provided in grackle_field_data_fdatamembers.def (or we may need to -// slightly revise the system?) -enum { - e, - HI, - HII, - HeI, - HeII, - HeIII, - - HM, - H2I, - H2II, - - DI, - DII, - HDI, - - DM, - HDII, - HeHII, - - CI, - CII, - CO, - CO2, - OI, - OH, - H2O, - O2, - SiI, - SiOI, - SiO2I, - CH, - CH2, - COII, - OII, - OHII, - H2OII, - H3OII, - O2II, - - Mg, - Al, - S, - Fe, - - SiM, - FeM, - Mg2SiO4, - MgSiO3, - Fe3O4, - AC, - SiO2D, - MgO, - FeS, - Al2O3, - reforg, - volorg, - H2Oice, - - NUM_ENTRIES // <- this is always last (so it specifies the number of species) -}; // enum - -} // SpLUT namespace +/// This is collection of enumerators (localized to the `SpLUT::` scope), with +/// an enumerator named for EVERY species (primordial-species, metal-species, +/// grain-species, etc). The enumerator values are intended to be used in a +/// compile-time lookup table (LUT), that incur no overhead at runtime. +/// +/// To understand this entity's purpose, imagine that we want to temporarily +/// track data for all N species known to Grackle (the data for each species is +/// stored in a buffer of common size determined at runtime). Consider the +/// following 3 ways we could accomplish this: +/// 1. define a separate variable holding a pointer for every single species +/// - this is what the fortran routines historically did +/// - this won't scale well with N (e.g. a function that requires all of +/// this info needs to take N arguments) +/// 2. define a single struct with a separate data member holding a pointer +/// for every single species. +/// - still doesn't scale well if for applying a common operation to the +/// data of each species (e.g. allocating/deallocating/zeroing-out) +/// 3. track each data-member in a statically sized array that holds pointers +/// for each species +/// - the array length is `SpLUT::NUM_ENTRIES` (known at compile time) +/// - it is now trivial to apply a common operation to each pointer +/// - we use the enumerator values in this LUT to lookup the value +/// corresponding to a given species (for example, we could use +/// `SpLUT::HI` to look up values related to HI) +/// +/// > [!important] +/// > This is a private implementation detail. We should never unintentionally +/// > expose this information directly in the public API (i.e. we want to +/// > maintain our flexibility). With that said, there are hypothetical +/// > scenarios where we might deliberately expose it (for performance purposes) +/// > -- but we should think long and hard about it before we do +/// +/// Implementation +/// -------------- +/// We implement choose to implement this entity by defining the enumerators +/// within a "stateless struct" (i.e. has a size of 0) that won't be +/// instantiated. The enclosing struct acts exactly like a pseudo-namespace +/// - doing this lets us use short/simple names for each enumerator without +/// fear of collision +/// - there are 2 advantages of using a struct over a namespace: +/// 1. it forces us to localize the definition in 1 location (in comparison, +/// C++ lets us add identifiers to a namespace in other files) +/// 2. it provides the flexibility to experiment with passing the LUT as a +/// template parameter +/// - a function that take accepts a specialized LUT (e.g. picked based +/// on primordial_chemistry) could reduce branching within loops and +/// and plausibly produce faster code. +/// - **WE DON'T CURRENTLY HAVE PLANS TO PURSUE THIS.** There are +/// much more immediately useful things to pursue first +/// +/// Other Thoughts +/// -------------- +/// - maybe we should store the dust species in a separate lookup table? +/// - adopting data-structures that leverage this LUT, is probably an important +/// stepping stone to implementing a system where reaction rates are +/// dynamically specified. After adopting such a system, we may not even need +/// a lookup table any more (this all assumes, of course, that such a system +/// is adequately performant) +struct SpLUT { + + // in the future, we may want to reimplement the following in terms of the + // XMacros provided in grackle_field_data_fdatamembers.def (or we may need to + // slightly revise the system?) + enum { + e, + HI, + HII, + HeI, + HeII, + HeIII, + + HM, + H2I, + H2II, + + DI, + DII, + HDI, + + DM, + HDII, + HeHII, + + CI, + CII, + CO, + CO2, + OI, + OH, + H2O, + O2, + SiI, + SiOI, + SiO2I, + CH, + CH2, + COII, + OII, + OHII, + H2OII, + H3OII, + O2II, + + Mg, + Al, + S, + Fe, + + SiM, + FeM, + Mg2SiO4, + MgSiO3, + Fe3O4, + AC, + SiO2D, + MgO, + FeS, + Al2O3, + reforg, + volorg, + H2Oice, + + NUM_ENTRIES // <- always last (so it specifies the number of species) + }; // enum + +}; // SpLUT struct // Defines the LUT for Standard Collisional and Recombination reaction rates From 03df051b858be6017c47be1aede8b51b76527237 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sat, 25 Jan 2025 17:32:01 -0500 Subject: [PATCH 097/101] Convert ColRecRxnLUT from namespace to struct --- src/clib/LUT.hpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/clib/LUT.hpp b/src/clib/LUT.hpp index 75b19ce17..c298bb9c5 100644 --- a/src/clib/LUT.hpp +++ b/src/clib/LUT.hpp @@ -138,18 +138,18 @@ struct SpLUT { }; // SpLUT struct -// Defines the LUT for Standard Collisional and Recombination reaction rates -namespace ColRecRxnLUT { +/// Defines the LUT for Standard Collisional and Recombination reaction rates +struct ColRecRxnLUT { -enum { - #define ENTRY(NAME) NAME, - #include "col_rec_rxn_rate_members.def" - #undef ENTRY + enum { + #define ENTRY(NAME) NAME, + #include "col_rec_rxn_rate_members.def" + #undef ENTRY - NUM_ENTRIES // <- this is always last (so it specifies the number of species) -}; // enum + NUM_ENTRIES // <- always last (so it specifies the number of species) + }; // enum -} // ColRecRxnLUT namespace +}; // ColRecRxnLUT struct #endif /* LUT_HPP */ From d8df5caec7c63d54a72f9d63a8a800796ae46307 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sun, 26 Jan 2025 12:00:47 -0500 Subject: [PATCH 098/101] add a note near the top of LUT.hpp about moving contents into a namespace --- src/clib/LUT.hpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/clib/LUT.hpp b/src/clib/LUT.hpp index c298bb9c5..8462b17d2 100644 --- a/src/clib/LUT.hpp +++ b/src/clib/LUT.hpp @@ -1,7 +1,7 @@ // See LICENSE file for license and copyright information /// @file LUT.hpp -/// @brief Declares some lookup-tables used internally by Grackle +/// @brief Declares some lookup-tables (LUTs) used internally by Grackle #ifndef LUT_HPP #define LUT_HPP @@ -10,6 +10,19 @@ #error "This file can only be read by a c++ compiler" #endif +// once we have transcribed more code, we should really put this header's +// contents inside of the grackle::impl namespace +// - if a lookup-table is called {name}, then its fully qualified name will be +// `grackle::impl::{name}`. +// - when using the LUT in a function, we can shorten its name, within the +// function to just {name} by inserting `using grackle::impl::{name};` near +// the start of the function +// - we should hold off on doing this until more code is transcribed (since it +// will be hard for the transcription tools to automatically handle the +// shortenning of the fully qualified name) + + + /// This is collection of enumerators (localized to the `SpLUT::` scope), with /// an enumerator named for EVERY species (primordial-species, metal-species, /// grain-species, etc). The enumerator values are intended to be used in a From d3350bd6797b410ca2b9e4a357b36e8285bd15a4 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Sat, 17 May 2025 18:32:22 -0400 Subject: [PATCH 099/101] add a missing include-directive --- src/clib/solve_rate_cool_g-cpp.C | 1 + 1 file changed, 1 insertion(+) diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index ea1bb1619..dd27cf55f 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -8,6 +8,7 @@ #include #include // std::malloc, std::free +#include // std::memcpy #include #include "grackle.h" From 91290b648d8483b1dc37703d49e576cf29d84361 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Thu, 12 Jun 2025 12:33:53 -0400 Subject: [PATCH 100/101] pull in the most recent change from gen2024-rate_access_API --- src/clib/LUT.hpp | 2 +- ...rate_members.def => collisional_rxn_rate_members.def} | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) rename src/clib/{col_rec_rxn_rate_members.def => collisional_rxn_rate_members.def} (88%) diff --git a/src/clib/LUT.hpp b/src/clib/LUT.hpp index 8462b17d2..4e355347e 100644 --- a/src/clib/LUT.hpp +++ b/src/clib/LUT.hpp @@ -156,7 +156,7 @@ struct ColRecRxnLUT { enum { #define ENTRY(NAME) NAME, - #include "col_rec_rxn_rate_members.def" + #include "collisional_rxn_rate_members.def" #undef ENTRY NUM_ENTRIES // <- always last (so it specifies the number of species) diff --git a/src/clib/col_rec_rxn_rate_members.def b/src/clib/collisional_rxn_rate_members.def similarity index 88% rename from src/clib/col_rec_rxn_rate_members.def rename to src/clib/collisional_rxn_rate_members.def index ee88e6c5c..141636222 100644 --- a/src/clib/col_rec_rxn_rate_members.def +++ b/src/clib/collisional_rxn_rate_members.def @@ -1,10 +1,9 @@ /*********************************************************************** / -/ this file lists each member of the collisional and recombination -/ species reaction rates that we interpolate in 1d as a function of -/ temperature. This list is intended to be used with X-Macros in *.c -/ files (to reduce the amount of code required to -/ interact with these fields) +/ this file lists each member of the collisional species reaction rates +/ that we interpolate in 1d as a function of temperature. This list is +/ intended to be used with X-Macros (to reduce the amount of code +/ required to interact with these data-members) / / Currently, we just specify each rate name. In the future, we will / probably add other metadata to track the initializer function. From 12c01363b8f39a8258cdc1fecb9e7d7cf5ef1220 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Thu, 12 Jun 2025 13:02:02 -0400 Subject: [PATCH 101/101] Replace ColRecRxn with CollisionalRxn This is going to make a lot of merge conflicts --- src/clib/LUT.hpp | 6 +-- src/clib/fortran_func_wrappers.hpp | 76 +++++++++++++++--------------- src/clib/internal_types.C | 12 ++--- src/clib/internal_types.hpp | 21 ++++----- src/clib/solve_rate_cool_g-cpp.C | 18 +++---- 5 files changed, 67 insertions(+), 66 deletions(-) diff --git a/src/clib/LUT.hpp b/src/clib/LUT.hpp index 4e355347e..64326ab3d 100644 --- a/src/clib/LUT.hpp +++ b/src/clib/LUT.hpp @@ -151,8 +151,8 @@ struct SpLUT { }; // SpLUT struct -/// Defines the LUT for Standard Collisional and Recombination reaction rates -struct ColRecRxnLUT { +/// Defines the LUT for Standard Collisional reaction rates +struct CollisionalRxnLUT { enum { #define ENTRY(NAME) NAME, @@ -162,7 +162,7 @@ struct ColRecRxnLUT { NUM_ENTRIES // <- always last (so it specifies the number of species) }; // enum -}; // ColRecRxnLUT struct +}; // CollisionalRxnLUT struct #endif /* LUT_HPP */ diff --git a/src/clib/fortran_func_wrappers.hpp b/src/clib/fortran_func_wrappers.hpp index 9ffdfb791..3796a04ec 100644 --- a/src/clib/fortran_func_wrappers.hpp +++ b/src/clib/fortran_func_wrappers.hpp @@ -203,7 +203,7 @@ inline void lookup_cool_rates1d_g( grackle::impl::GrainSpeciesCollection grain_growth_rates, grackle::impl::GrainSpeciesCollection grain_temperatures, grackle::impl::LogTLinInterpScratchBuf logTlininterp_buf, - grackle::impl::ColRecRxnRateCollection kcr_buf, + grackle::impl::CollisionalRxnRateCollection kcr_buf, grackle::impl::PhotoRxnRateCollection kshield_buf, grackle::impl::ChemHeatingRates chemheatrates_buf ) { @@ -222,11 +222,11 @@ inline void lookup_cool_rates1d_g( my_rates->k57, my_rates->k58, &my_chemistry->NumberOfDustTemperatureBins, &my_chemistry->DustTemperatureStart, &my_chemistry->DustTemperatureEnd, my_rates->h2dust, my_rates->n_cr_n, my_rates->n_cr_d1, my_rates->n_cr_d2, &my_uvb_rates.crsHI, &my_uvb_rates.crsHeI, &my_uvb_rates.crsHeII, &my_uvb_rates.piHI, &my_uvb_rates.piHeI, - kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], - kcr_buf.data[ColRecRxnLUT::k11], kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], - kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], &my_uvb_rates.k24, &my_uvb_rates.k25, &my_uvb_rates.k26, &my_uvb_rates.k28, &my_uvb_rates.k29, &my_uvb_rates.k30, &my_uvb_rates.k31, - kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], - kcr_buf.data[ColRecRxnLUT::k58], k13dd, kshield_buf.k24, kshield_buf.k25, kshield_buf.k26, + kcr_buf.data[CollisionalRxnLUT::k1], kcr_buf.data[CollisionalRxnLUT::k2], kcr_buf.data[CollisionalRxnLUT::k3], kcr_buf.data[CollisionalRxnLUT::k4], kcr_buf.data[CollisionalRxnLUT::k5], kcr_buf.data[CollisionalRxnLUT::k6], kcr_buf.data[CollisionalRxnLUT::k7], kcr_buf.data[CollisionalRxnLUT::k8], kcr_buf.data[CollisionalRxnLUT::k9], kcr_buf.data[CollisionalRxnLUT::k10], + kcr_buf.data[CollisionalRxnLUT::k11], kcr_buf.data[CollisionalRxnLUT::k12], kcr_buf.data[CollisionalRxnLUT::k13], kcr_buf.data[CollisionalRxnLUT::k14], kcr_buf.data[CollisionalRxnLUT::k15], kcr_buf.data[CollisionalRxnLUT::k16], kcr_buf.data[CollisionalRxnLUT::k17], kcr_buf.data[CollisionalRxnLUT::k18], + kcr_buf.data[CollisionalRxnLUT::k19], kcr_buf.data[CollisionalRxnLUT::k22], &my_uvb_rates.k24, &my_uvb_rates.k25, &my_uvb_rates.k26, &my_uvb_rates.k28, &my_uvb_rates.k29, &my_uvb_rates.k30, &my_uvb_rates.k31, + kcr_buf.data[CollisionalRxnLUT::k50], kcr_buf.data[CollisionalRxnLUT::k51], kcr_buf.data[CollisionalRxnLUT::k52], kcr_buf.data[CollisionalRxnLUT::k53], kcr_buf.data[CollisionalRxnLUT::k54], kcr_buf.data[CollisionalRxnLUT::k55], kcr_buf.data[CollisionalRxnLUT::k56], kcr_buf.data[CollisionalRxnLUT::k57], + kcr_buf.data[CollisionalRxnLUT::k58], k13dd, kshield_buf.k24, kshield_buf.k25, kshield_buf.k26, kshield_buf.k28, kshield_buf.k29, kshield_buf.k30, kshield_buf.k31, h2dust, chemheatrates_buf.n_cr_n, chemheatrates_buf.n_cr_d1, chemheatrates_buf.n_cr_d2, logTlininterp_buf.t1, logTlininterp_buf.t2, logTlininterp_buf.tdef, logTlininterp_buf.logtem, logTlininterp_buf.indixe, @@ -256,18 +256,18 @@ inline void lookup_cool_rates1d_g( my_rates->kz40, my_rates->kz41, my_rates->kz42, my_rates->kz43, my_rates->kz44, my_rates->kz45, my_rates->kz46, my_rates->kz47, my_rates->kz48, my_rates->kz49, my_rates->kz50, my_rates->kz51, my_rates->kz52, my_rates->kz53, my_rates->kz54, - kcr_buf.data[ColRecRxnLUT::k125], kcr_buf.data[ColRecRxnLUT::k129], kcr_buf.data[ColRecRxnLUT::k130], kcr_buf.data[ColRecRxnLUT::k131], kcr_buf.data[ColRecRxnLUT::k132], - kcr_buf.data[ColRecRxnLUT::k133], kcr_buf.data[ColRecRxnLUT::k134], kcr_buf.data[ColRecRxnLUT::k135], kcr_buf.data[ColRecRxnLUT::k136], kcr_buf.data[ColRecRxnLUT::k137], - kcr_buf.data[ColRecRxnLUT::k148], kcr_buf.data[ColRecRxnLUT::k149], kcr_buf.data[ColRecRxnLUT::k150], kcr_buf.data[ColRecRxnLUT::k151], kcr_buf.data[ColRecRxnLUT::k152], - kcr_buf.data[ColRecRxnLUT::k153], - kcr_buf.data[ColRecRxnLUT::kz15], kcr_buf.data[ColRecRxnLUT::kz16], kcr_buf.data[ColRecRxnLUT::kz17], kcr_buf.data[ColRecRxnLUT::kz18], kcr_buf.data[ColRecRxnLUT::kz19], - kcr_buf.data[ColRecRxnLUT::kz20], kcr_buf.data[ColRecRxnLUT::kz21], kcr_buf.data[ColRecRxnLUT::kz22], kcr_buf.data[ColRecRxnLUT::kz23], kcr_buf.data[ColRecRxnLUT::kz24], - kcr_buf.data[ColRecRxnLUT::kz25], kcr_buf.data[ColRecRxnLUT::kz26], kcr_buf.data[ColRecRxnLUT::kz27], kcr_buf.data[ColRecRxnLUT::kz28], kcr_buf.data[ColRecRxnLUT::kz29], - kcr_buf.data[ColRecRxnLUT::kz30], kcr_buf.data[ColRecRxnLUT::kz31], kcr_buf.data[ColRecRxnLUT::kz32], kcr_buf.data[ColRecRxnLUT::kz33], kcr_buf.data[ColRecRxnLUT::kz34], - kcr_buf.data[ColRecRxnLUT::kz35], kcr_buf.data[ColRecRxnLUT::kz36], kcr_buf.data[ColRecRxnLUT::kz37], kcr_buf.data[ColRecRxnLUT::kz38], kcr_buf.data[ColRecRxnLUT::kz39], - kcr_buf.data[ColRecRxnLUT::kz40], kcr_buf.data[ColRecRxnLUT::kz41], kcr_buf.data[ColRecRxnLUT::kz42], kcr_buf.data[ColRecRxnLUT::kz43], kcr_buf.data[ColRecRxnLUT::kz44], - kcr_buf.data[ColRecRxnLUT::kz45], kcr_buf.data[ColRecRxnLUT::kz46], kcr_buf.data[ColRecRxnLUT::kz47], kcr_buf.data[ColRecRxnLUT::kz48], kcr_buf.data[ColRecRxnLUT::kz49], - kcr_buf.data[ColRecRxnLUT::kz50], kcr_buf.data[ColRecRxnLUT::kz51], kcr_buf.data[ColRecRxnLUT::kz52], kcr_buf.data[ColRecRxnLUT::kz53], kcr_buf.data[ColRecRxnLUT::kz54], + kcr_buf.data[CollisionalRxnLUT::k125], kcr_buf.data[CollisionalRxnLUT::k129], kcr_buf.data[CollisionalRxnLUT::k130], kcr_buf.data[CollisionalRxnLUT::k131], kcr_buf.data[CollisionalRxnLUT::k132], + kcr_buf.data[CollisionalRxnLUT::k133], kcr_buf.data[CollisionalRxnLUT::k134], kcr_buf.data[CollisionalRxnLUT::k135], kcr_buf.data[CollisionalRxnLUT::k136], kcr_buf.data[CollisionalRxnLUT::k137], + kcr_buf.data[CollisionalRxnLUT::k148], kcr_buf.data[CollisionalRxnLUT::k149], kcr_buf.data[CollisionalRxnLUT::k150], kcr_buf.data[CollisionalRxnLUT::k151], kcr_buf.data[CollisionalRxnLUT::k152], + kcr_buf.data[CollisionalRxnLUT::k153], + kcr_buf.data[CollisionalRxnLUT::kz15], kcr_buf.data[CollisionalRxnLUT::kz16], kcr_buf.data[CollisionalRxnLUT::kz17], kcr_buf.data[CollisionalRxnLUT::kz18], kcr_buf.data[CollisionalRxnLUT::kz19], + kcr_buf.data[CollisionalRxnLUT::kz20], kcr_buf.data[CollisionalRxnLUT::kz21], kcr_buf.data[CollisionalRxnLUT::kz22], kcr_buf.data[CollisionalRxnLUT::kz23], kcr_buf.data[CollisionalRxnLUT::kz24], + kcr_buf.data[CollisionalRxnLUT::kz25], kcr_buf.data[CollisionalRxnLUT::kz26], kcr_buf.data[CollisionalRxnLUT::kz27], kcr_buf.data[CollisionalRxnLUT::kz28], kcr_buf.data[CollisionalRxnLUT::kz29], + kcr_buf.data[CollisionalRxnLUT::kz30], kcr_buf.data[CollisionalRxnLUT::kz31], kcr_buf.data[CollisionalRxnLUT::kz32], kcr_buf.data[CollisionalRxnLUT::kz33], kcr_buf.data[CollisionalRxnLUT::kz34], + kcr_buf.data[CollisionalRxnLUT::kz35], kcr_buf.data[CollisionalRxnLUT::kz36], kcr_buf.data[CollisionalRxnLUT::kz37], kcr_buf.data[CollisionalRxnLUT::kz38], kcr_buf.data[CollisionalRxnLUT::kz39], + kcr_buf.data[CollisionalRxnLUT::kz40], kcr_buf.data[CollisionalRxnLUT::kz41], kcr_buf.data[CollisionalRxnLUT::kz42], kcr_buf.data[CollisionalRxnLUT::kz43], kcr_buf.data[CollisionalRxnLUT::kz44], + kcr_buf.data[CollisionalRxnLUT::kz45], kcr_buf.data[CollisionalRxnLUT::kz46], kcr_buf.data[CollisionalRxnLUT::kz47], kcr_buf.data[CollisionalRxnLUT::kz48], kcr_buf.data[CollisionalRxnLUT::kz49], + kcr_buf.data[CollisionalRxnLUT::kz50], kcr_buf.data[CollisionalRxnLUT::kz51], kcr_buf.data[CollisionalRxnLUT::kz52], kcr_buf.data[CollisionalRxnLUT::kz53], kcr_buf.data[CollisionalRxnLUT::kz54], &my_chemistry->multi_metals, &my_chemistry->metal_abundances, &my_chemistry->dust_species, &my_chemistry->use_multiple_dust_temperatures, &my_chemistry->dust_sublimation, my_fields->local_ISM_metal_density, my_fields->ccsn13_metal_density, my_fields->ccsn20_metal_density, my_fields->ccsn25_metal_density, my_fields->ccsn30_metal_density, @@ -340,7 +340,7 @@ inline void rate_timestep_g( double* h2dust, double* rhoH, gr_mask_type* itmask, double* edot, double chunit, double dom, chemistry_data* my_chemistry, grackle_field_data* my_fields, photo_rate_storage my_uvb_rates, - grackle::impl::ColRecRxnRateCollection kcr_buf, + grackle::impl::CollisionalRxnRateCollection kcr_buf, grackle::impl::PhotoRxnRateCollection kshield_buf, grackle::impl::ChemHeatingRates chemheatrates_buf ) { @@ -349,10 +349,10 @@ inline void rate_timestep_g( my_fields->e_density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, my_fields->density, my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &idx_range.i_start, &idx_range.i_end, &idx_range.jp1, &idx_range.kp1, - kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], kcr_buf.data[ColRecRxnLUT::k11], - kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], + kcr_buf.data[CollisionalRxnLUT::k1], kcr_buf.data[CollisionalRxnLUT::k2], kcr_buf.data[CollisionalRxnLUT::k3], kcr_buf.data[CollisionalRxnLUT::k4], kcr_buf.data[CollisionalRxnLUT::k5], kcr_buf.data[CollisionalRxnLUT::k6], kcr_buf.data[CollisionalRxnLUT::k7], kcr_buf.data[CollisionalRxnLUT::k8], kcr_buf.data[CollisionalRxnLUT::k9], kcr_buf.data[CollisionalRxnLUT::k10], kcr_buf.data[CollisionalRxnLUT::k11], + kcr_buf.data[CollisionalRxnLUT::k12], kcr_buf.data[CollisionalRxnLUT::k13], kcr_buf.data[CollisionalRxnLUT::k14], kcr_buf.data[CollisionalRxnLUT::k15], kcr_buf.data[CollisionalRxnLUT::k16], kcr_buf.data[CollisionalRxnLUT::k17], kcr_buf.data[CollisionalRxnLUT::k18], kcr_buf.data[CollisionalRxnLUT::k19], kcr_buf.data[CollisionalRxnLUT::k22], &my_uvb_rates.k24, &my_uvb_rates.k25, &my_uvb_rates.k26, &my_uvb_rates.k27, &my_uvb_rates.k28, &my_uvb_rates.k29, &my_uvb_rates.k30, - kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], kcr_buf.data[ColRecRxnLUT::k58], + kcr_buf.data[CollisionalRxnLUT::k50], kcr_buf.data[CollisionalRxnLUT::k51], kcr_buf.data[CollisionalRxnLUT::k52], kcr_buf.data[CollisionalRxnLUT::k53], kcr_buf.data[CollisionalRxnLUT::k54], kcr_buf.data[CollisionalRxnLUT::k55], kcr_buf.data[CollisionalRxnLUT::k56], kcr_buf.data[CollisionalRxnLUT::k57], kcr_buf.data[CollisionalRxnLUT::k58], h2dust, chemheatrates_buf.n_cr_n, chemheatrates_buf.n_cr_d1, chemheatrates_buf.n_cr_d2, rhoH, kshield_buf.k24, kshield_buf.k25, kshield_buf.k26, kshield_buf.k28, kshield_buf.k29, kshield_buf.k30, kshield_buf.k31, @@ -403,17 +403,17 @@ inline void step_rate_g( photo_rate_storage my_uvb_rates, grackle::impl::GrainSpeciesCollection grain_growth_rates, grackle::impl::SpeciesCollection species_tmpdens, - grackle::impl::ColRecRxnRateCollection kcr_buf, + grackle::impl::CollisionalRxnRateCollection kcr_buf, grackle::impl::PhotoRxnRateCollection kshield_buf ) { FORTRAN_NAME(step_rate_g)(my_fields->e_density, my_fields->HI_density, my_fields->HII_density, my_fields->HeI_density, my_fields->HeII_density, my_fields->HeIII_density, my_fields->density, my_fields->HM_density, my_fields->H2I_density, my_fields->H2II_density, my_fields->DI_density, my_fields->DII_density, my_fields->HDI_density, dtit, &my_fields->grid_dimension[0], &my_fields->grid_dimension[1], &my_fields->grid_dimension[2], &idx_range.i_start, &idx_range.i_end, &idx_range.jp1, &idx_range.kp1, &my_chemistry->primordial_chemistry, &anydust, - kcr_buf.data[ColRecRxnLUT::k1], kcr_buf.data[ColRecRxnLUT::k2], kcr_buf.data[ColRecRxnLUT::k3], kcr_buf.data[ColRecRxnLUT::k4], kcr_buf.data[ColRecRxnLUT::k5], kcr_buf.data[ColRecRxnLUT::k6], kcr_buf.data[ColRecRxnLUT::k7], kcr_buf.data[ColRecRxnLUT::k8], kcr_buf.data[ColRecRxnLUT::k9], kcr_buf.data[ColRecRxnLUT::k10], kcr_buf.data[ColRecRxnLUT::k11], - kcr_buf.data[ColRecRxnLUT::k12], kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k14], kcr_buf.data[ColRecRxnLUT::k15], kcr_buf.data[ColRecRxnLUT::k16], kcr_buf.data[ColRecRxnLUT::k17], kcr_buf.data[ColRecRxnLUT::k18], kcr_buf.data[ColRecRxnLUT::k19], kcr_buf.data[ColRecRxnLUT::k22], + kcr_buf.data[CollisionalRxnLUT::k1], kcr_buf.data[CollisionalRxnLUT::k2], kcr_buf.data[CollisionalRxnLUT::k3], kcr_buf.data[CollisionalRxnLUT::k4], kcr_buf.data[CollisionalRxnLUT::k5], kcr_buf.data[CollisionalRxnLUT::k6], kcr_buf.data[CollisionalRxnLUT::k7], kcr_buf.data[CollisionalRxnLUT::k8], kcr_buf.data[CollisionalRxnLUT::k9], kcr_buf.data[CollisionalRxnLUT::k10], kcr_buf.data[CollisionalRxnLUT::k11], + kcr_buf.data[CollisionalRxnLUT::k12], kcr_buf.data[CollisionalRxnLUT::k13], kcr_buf.data[CollisionalRxnLUT::k14], kcr_buf.data[CollisionalRxnLUT::k15], kcr_buf.data[CollisionalRxnLUT::k16], kcr_buf.data[CollisionalRxnLUT::k17], kcr_buf.data[CollisionalRxnLUT::k18], kcr_buf.data[CollisionalRxnLUT::k19], kcr_buf.data[CollisionalRxnLUT::k22], &my_uvb_rates.k24, &my_uvb_rates.k25, &my_uvb_rates.k26, &my_uvb_rates.k27, &my_uvb_rates.k28, &my_uvb_rates.k29, &my_uvb_rates.k30, - kcr_buf.data[ColRecRxnLUT::k50], kcr_buf.data[ColRecRxnLUT::k51], kcr_buf.data[ColRecRxnLUT::k52], kcr_buf.data[ColRecRxnLUT::k53], kcr_buf.data[ColRecRxnLUT::k54], kcr_buf.data[ColRecRxnLUT::k55], kcr_buf.data[ColRecRxnLUT::k56], kcr_buf.data[ColRecRxnLUT::k57], kcr_buf.data[ColRecRxnLUT::k58], + kcr_buf.data[CollisionalRxnLUT::k50], kcr_buf.data[CollisionalRxnLUT::k51], kcr_buf.data[CollisionalRxnLUT::k52], kcr_buf.data[CollisionalRxnLUT::k53], kcr_buf.data[CollisionalRxnLUT::k54], kcr_buf.data[CollisionalRxnLUT::k55], kcr_buf.data[CollisionalRxnLUT::k56], kcr_buf.data[CollisionalRxnLUT::k57], kcr_buf.data[CollisionalRxnLUT::k58], h2dust, rhoH, kshield_buf.k24, kshield_buf.k25, kshield_buf.k26, kshield_buf.k28, kshield_buf.k29, kshield_buf.k30, kshield_buf.k31, @@ -434,18 +434,18 @@ inline void step_rate_g( my_fields->SiM_dust_density, my_fields->FeM_dust_density, my_fields->Mg2SiO4_dust_density, my_fields->MgSiO3_dust_density, my_fields->Fe3O4_dust_density, my_fields->AC_dust_density, my_fields->SiO2_dust_density, my_fields->MgO_dust_density, my_fields->FeS_dust_density, my_fields->Al2O3_dust_density, my_fields->ref_org_dust_density, my_fields->vol_org_dust_density, my_fields->H2O_ice_dust_density, - kcr_buf.data[ColRecRxnLUT::k125], kcr_buf.data[ColRecRxnLUT::k129], kcr_buf.data[ColRecRxnLUT::k130], kcr_buf.data[ColRecRxnLUT::k131], kcr_buf.data[ColRecRxnLUT::k132], - kcr_buf.data[ColRecRxnLUT::k133], kcr_buf.data[ColRecRxnLUT::k134], kcr_buf.data[ColRecRxnLUT::k135], kcr_buf.data[ColRecRxnLUT::k136], kcr_buf.data[ColRecRxnLUT::k137], - kcr_buf.data[ColRecRxnLUT::k148], kcr_buf.data[ColRecRxnLUT::k149], kcr_buf.data[ColRecRxnLUT::k150], kcr_buf.data[ColRecRxnLUT::k151], kcr_buf.data[ColRecRxnLUT::k152], - kcr_buf.data[ColRecRxnLUT::k153], - kcr_buf.data[ColRecRxnLUT::kz15], kcr_buf.data[ColRecRxnLUT::kz16], kcr_buf.data[ColRecRxnLUT::kz17], kcr_buf.data[ColRecRxnLUT::kz18], kcr_buf.data[ColRecRxnLUT::kz19], - kcr_buf.data[ColRecRxnLUT::kz20], kcr_buf.data[ColRecRxnLUT::kz21], kcr_buf.data[ColRecRxnLUT::kz22], kcr_buf.data[ColRecRxnLUT::kz23], kcr_buf.data[ColRecRxnLUT::kz24], - kcr_buf.data[ColRecRxnLUT::kz25], kcr_buf.data[ColRecRxnLUT::kz26], kcr_buf.data[ColRecRxnLUT::kz27], kcr_buf.data[ColRecRxnLUT::kz28], kcr_buf.data[ColRecRxnLUT::kz29], - kcr_buf.data[ColRecRxnLUT::kz30], kcr_buf.data[ColRecRxnLUT::kz31], kcr_buf.data[ColRecRxnLUT::kz32], kcr_buf.data[ColRecRxnLUT::kz33], kcr_buf.data[ColRecRxnLUT::kz34], - kcr_buf.data[ColRecRxnLUT::kz35], kcr_buf.data[ColRecRxnLUT::kz36], kcr_buf.data[ColRecRxnLUT::kz37], kcr_buf.data[ColRecRxnLUT::kz38], kcr_buf.data[ColRecRxnLUT::kz39], - kcr_buf.data[ColRecRxnLUT::kz40], kcr_buf.data[ColRecRxnLUT::kz41], kcr_buf.data[ColRecRxnLUT::kz42], kcr_buf.data[ColRecRxnLUT::kz43], kcr_buf.data[ColRecRxnLUT::kz44], - kcr_buf.data[ColRecRxnLUT::kz45], kcr_buf.data[ColRecRxnLUT::kz46], kcr_buf.data[ColRecRxnLUT::kz47], kcr_buf.data[ColRecRxnLUT::kz48], kcr_buf.data[ColRecRxnLUT::kz49], - kcr_buf.data[ColRecRxnLUT::kz50], kcr_buf.data[ColRecRxnLUT::kz51], kcr_buf.data[ColRecRxnLUT::kz52], kcr_buf.data[ColRecRxnLUT::kz53], kcr_buf.data[ColRecRxnLUT::kz54], + kcr_buf.data[CollisionalRxnLUT::k125], kcr_buf.data[CollisionalRxnLUT::k129], kcr_buf.data[CollisionalRxnLUT::k130], kcr_buf.data[CollisionalRxnLUT::k131], kcr_buf.data[CollisionalRxnLUT::k132], + kcr_buf.data[CollisionalRxnLUT::k133], kcr_buf.data[CollisionalRxnLUT::k134], kcr_buf.data[CollisionalRxnLUT::k135], kcr_buf.data[CollisionalRxnLUT::k136], kcr_buf.data[CollisionalRxnLUT::k137], + kcr_buf.data[CollisionalRxnLUT::k148], kcr_buf.data[CollisionalRxnLUT::k149], kcr_buf.data[CollisionalRxnLUT::k150], kcr_buf.data[CollisionalRxnLUT::k151], kcr_buf.data[CollisionalRxnLUT::k152], + kcr_buf.data[CollisionalRxnLUT::k153], + kcr_buf.data[CollisionalRxnLUT::kz15], kcr_buf.data[CollisionalRxnLUT::kz16], kcr_buf.data[CollisionalRxnLUT::kz17], kcr_buf.data[CollisionalRxnLUT::kz18], kcr_buf.data[CollisionalRxnLUT::kz19], + kcr_buf.data[CollisionalRxnLUT::kz20], kcr_buf.data[CollisionalRxnLUT::kz21], kcr_buf.data[CollisionalRxnLUT::kz22], kcr_buf.data[CollisionalRxnLUT::kz23], kcr_buf.data[CollisionalRxnLUT::kz24], + kcr_buf.data[CollisionalRxnLUT::kz25], kcr_buf.data[CollisionalRxnLUT::kz26], kcr_buf.data[CollisionalRxnLUT::kz27], kcr_buf.data[CollisionalRxnLUT::kz28], kcr_buf.data[CollisionalRxnLUT::kz29], + kcr_buf.data[CollisionalRxnLUT::kz30], kcr_buf.data[CollisionalRxnLUT::kz31], kcr_buf.data[CollisionalRxnLUT::kz32], kcr_buf.data[CollisionalRxnLUT::kz33], kcr_buf.data[CollisionalRxnLUT::kz34], + kcr_buf.data[CollisionalRxnLUT::kz35], kcr_buf.data[CollisionalRxnLUT::kz36], kcr_buf.data[CollisionalRxnLUT::kz37], kcr_buf.data[CollisionalRxnLUT::kz38], kcr_buf.data[CollisionalRxnLUT::kz39], + kcr_buf.data[CollisionalRxnLUT::kz40], kcr_buf.data[CollisionalRxnLUT::kz41], kcr_buf.data[CollisionalRxnLUT::kz42], kcr_buf.data[CollisionalRxnLUT::kz43], kcr_buf.data[CollisionalRxnLUT::kz44], + kcr_buf.data[CollisionalRxnLUT::kz45], kcr_buf.data[CollisionalRxnLUT::kz46], kcr_buf.data[CollisionalRxnLUT::kz47], kcr_buf.data[CollisionalRxnLUT::kz48], kcr_buf.data[CollisionalRxnLUT::kz49], + kcr_buf.data[CollisionalRxnLUT::kz50], kcr_buf.data[CollisionalRxnLUT::kz51], kcr_buf.data[CollisionalRxnLUT::kz52], kcr_buf.data[CollisionalRxnLUT::kz53], kcr_buf.data[CollisionalRxnLUT::kz54], species_tmpdens.data[SpLUT::DM], species_tmpdens.data[SpLUT::HDII], species_tmpdens.data[SpLUT::HeHII], species_tmpdens.data[SpLUT::CI], species_tmpdens.data[SpLUT::CII], species_tmpdens.data[SpLUT::CO], species_tmpdens.data[SpLUT::CO2], species_tmpdens.data[SpLUT::OI], species_tmpdens.data[SpLUT::OH], species_tmpdens.data[SpLUT::H2O], species_tmpdens.data[SpLUT::O2], diff --git a/src/clib/internal_types.C b/src/clib/internal_types.C index 241b907dd..5ca6ab01a 100644 --- a/src/clib/internal_types.C +++ b/src/clib/internal_types.C @@ -214,22 +214,22 @@ void grackle::impl::drop_SpeciesCollection( // ----------------------------------------------------------------- -grackle::impl::ColRecRxnRateCollection grackle::impl::new_ColRecRxnRateCollection( +grackle::impl::CollisionalRxnRateCollection grackle::impl::new_CollisionalRxnRateCollection( int nelem ) { GRIMPL_REQUIRE(nelem > 0, "nelem must be positive"); - grackle::impl::ColRecRxnRateCollection out; + grackle::impl::CollisionalRxnRateCollection out; double* ptr = (double*)malloc( - sizeof(double) * nelem * ColRecRxnLUT::NUM_ENTRIES + sizeof(double) * nelem * CollisionalRxnLUT::NUM_ENTRIES ); - for (int i = 0; i < ColRecRxnLUT::NUM_ENTRIES; i++) { + for (int i = 0; i < CollisionalRxnLUT::NUM_ENTRIES; i++) { out.data[i] = ptr + (i * nelem); } return out; } -void grackle::impl::drop_ColRecRxnRateCollection( - grackle::impl::ColRecRxnRateCollection *ptr +void grackle::impl::drop_CollisionalRxnRateCollection( + grackle::impl::CollisionalRxnRateCollection *ptr ) { // since we only allocate a single pointer, we only need to call free once free(ptr->data[0]); diff --git a/src/clib/internal_types.hpp b/src/clib/internal_types.hpp index efc5f48f0..11f0d2075 100644 --- a/src/clib/internal_types.hpp +++ b/src/clib/internal_types.hpp @@ -285,25 +285,24 @@ SpeciesCollection new_SpeciesCollection(int nelem); void drop_SpeciesCollection(SpeciesCollection*); -/// holds properties about Collisional and Recombination Reaction Rates that -/// behave in the "standard" way (i.e. we interpolate in 1D with respect to -/// log T) +/// holds properties about Collisional Reaction Rates that behave in the +/// "standard" way (i.e. we interpolate in 1D with respect to log T) /// /// This operates in a similar manner to SpeciesCollection (i.e. we use -/// `ColRecRxnLUT::` to lookup values for the desired rate). -struct ColRecRxnRateCollection { - double* data[ColRecRxnLUT::NUM_ENTRIES]; +/// `CollisionalRxnLUT::` to lookup values for the desired rate). +struct CollisionalRxnRateCollection { + double* data[CollisionalRxnLUT::NUM_ENTRIES]; }; -/// allocates the contents of a new ColRecRxnRateCollection +/// allocates the contents of a new CollisionalRxnRateCollection /// /// @param nelem The number of elements in each buffer -ColRecRxnRateCollection new_ColRecRxnRateCollection(int nelem); +CollisionalRxnRateCollection new_CollisionalRxnRateCollection(int nelem); -/// performs cleanup of the contents of ColRecRxnRateCollection +/// performs cleanup of the contents of CollisionalRxnRateCollection /// /// This effectively invokes a destructor -void drop_ColRecRxnRateCollection(ColRecRxnRateCollection*); +void drop_CollisionalRxnRateCollection(CollisionalRxnRateCollection*); /// holds radiative reaction rate buffers /// @@ -358,7 +357,7 @@ struct ChemHeatingRates{ /// @param nelem The number of elements in each buffer ChemHeatingRates new_ChemHeatingRates(int nelem); -/// performs cleanup of the contents of ColRecRxnRateCollection +/// performs cleanup of the contents of ChemHeatingRates /// /// This effectively invokes a destructor void drop_ChemHeatingRates(ChemHeatingRates*); diff --git a/src/clib/solve_rate_cool_g-cpp.C b/src/clib/solve_rate_cool_g-cpp.C index dd27cf55f..7e02b076e 100644 --- a/src/clib/solve_rate_cool_g-cpp.C +++ b/src/clib/solve_rate_cool_g-cpp.C @@ -309,7 +309,8 @@ static void set_subcycle_dt_from_chemistry_scheme_( const chemistry_data* my_chemistry, const chemistry_data_storage* my_rates, double dlogtem, const grackle::impl::LogTLinInterpScratchBuf logTlininterp_buf, - grackle_field_data* my_fields, grackle::impl::ColRecRxnRateCollection kcr_buf + grackle_field_data* my_fields, + grackle::impl::CollisionalRxnRateCollection kcr_buf ) { const int j = idx_range.j; const int k = idx_range.k; @@ -354,9 +355,9 @@ static void set_subcycle_dt_from_chemistry_scheme_( // If the net rate is almost perfectly balanced then set // it to zero (since it is zero to available precision) { - double ion_rate = std::fabs(kcr_buf.data[ColRecRxnLUT::k1][i] * + double ion_rate = std::fabs(kcr_buf.data[CollisionalRxnLUT::k1][i] * de(i,j,k) * HI(i,j,k)); - double recomb_rate = std::fabs(kcr_buf.data[ColRecRxnLUT::k2][i] * + double recomb_rate = std::fabs(kcr_buf.data[CollisionalRxnLUT::k2][i] * HII(i,j,k) * de(i,j,k)); double ratio = (std::fmin(ion_rate, recomb_rate) / std::fmax(std::fabs(dedot[i]), std::fabs(HIdot[i]))); @@ -391,7 +392,8 @@ static void set_subcycle_dt_from_chemistry_scheme_( // Hydrogen changes by 10% or less double Heq_div_dHeqdt = calc_Heq_div_dHeqdt_( my_chemistry, my_rates, dlogtem, logTlininterp_buf, - kcr_buf.data[ColRecRxnLUT::k13], kcr_buf.data[ColRecRxnLUT::k22], + kcr_buf.data[CollisionalRxnLUT::k13], + kcr_buf.data[CollisionalRxnLUT::k22], d(i,j,k), tgas, p2d, edot, i ); @@ -528,9 +530,9 @@ struct SpeciesRateSolverScratchBuf { grackle::impl::SpeciesCollection species_tmpdens; // buffers in the following data structure are used to temporarily hold - // the interpolated Collisional/Recombination Rates that have been + // the interpolated Collisional Rxn Rates that have been // interpolated using the standard 1D log temperature table. - grackle::impl::ColRecRxnRateCollection kcr_buf; + grackle::impl::CollisionalRxnRateCollection kcr_buf; // buffers in the following data structure are used to temporarily hold // the computed radiative reaction rates @@ -568,7 +570,7 @@ SpeciesRateSolverScratchBuf new_SpeciesRateSolverScratchBuf(int nelem) { out.imp_eng = (int*)malloc(sizeof(int)*nelem); out.species_tmpdens = grackle::impl::new_SpeciesCollection(nelem); - out.kcr_buf = grackle::impl::new_ColRecRxnRateCollection(nelem); + out.kcr_buf = grackle::impl::new_CollisionalRxnRateCollection(nelem); out.kshield_buf = grackle::impl::new_PhotoRxnRateCollection(nelem); out.chemheatrates_buf = grackle::impl::new_ChemHeatingRates(nelem); out.grain_growth_rates = grackle::impl::new_GrainSpeciesCollection(nelem); @@ -595,7 +597,7 @@ void drop_SpeciesRateSolverScratchBuf(SpeciesRateSolverScratchBuf* ptr) { free(ptr->imp_eng); grackle::impl::drop_SpeciesCollection(&ptr->species_tmpdens); - grackle::impl::drop_ColRecRxnRateCollection(&ptr->kcr_buf); + grackle::impl::drop_CollisionalRxnRateCollection(&ptr->kcr_buf); grackle::impl::drop_PhotoRxnRateCollection(&ptr->kshield_buf); grackle::impl::drop_ChemHeatingRates(&ptr->chemheatrates_buf); grackle::impl::drop_GrainSpeciesCollection(&ptr->grain_growth_rates);