Skip to content

Commit

Permalink
Merge pull request #184 from lanl/fix_typo_cooling
Browse files Browse the repository at this point in the history
Inconsistent behavior between CPU and GPU for liebendorf deleptonization and cooling
  • Loading branch information
Yurlungur authored Dec 13, 2023
2 parents 0aaac0c + 3be4e88 commit 44f0637
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/radiation/cooling_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ TaskStatus LightBulbCalcTau(MeshBlockData<Real> *rc) {

auto &unit_conv =
pmesh->packages.Get("phoebus")->Param<phoebus::UnitConversions>("unit_conv");
const Real density_conversion_factor = unit_conv.GetMassDensityCodeToCGS();
parthenon::par_for(
DEFAULT_LOOP_PATTERN, "CalcTau", DevExecSpace(), kb.s, kb.e, jb.s, jb.e, ib.s, ib.e,
KOKKOS_LAMBDA(const int k, const int j, const int i) {
const Real rho =
v(prho, k, j, i) * unit_conv.GetMassDensityCodeToCGS(); // Density in CGS
const Real rho = v(prho, k, j, i) * density_conversion_factor; // Density in CGS
const Real lRho = std::log10(rho);
// Calculate tau
constexpr Real xl1 = LightBulb::HeatAndCool::XL1;
Expand Down Expand Up @@ -156,6 +156,16 @@ TaskStatus CoolingFunctionCalculateFourForce(MeshBlockData<Real> *rc, const doub
rad->Param<bool>("do_nu_electron_anti"),
rad->Param<bool>("do_nu_heavy")};

// Code to CGS
const Real density_conversion_factor = unit_conv.GetMassDensityCodeToCGS();
const Real temperature_conversion_factor = unit_conv.GetTemperatureCodeToCGS();
const Real length_conversion_factor = unit_conv.GetLengthCodeToCGS();

// CGS to code
const Real energy_conversion_factor = unit_conv.GetEnergyCGSToCode();
const Real mass_conversion_factor = unit_conv.GetMassCGSToCode();
const Real time_conversion_factor = unit_conv.GetTimeCGSToCode();

parthenon::par_for(
DEFAULT_LOOP_PATTERN, "CoolingFunctionCalculateFourForce", DevExecSpace(), kb.s,
kb.e, jb.s, jb.e, ib.s, ib.e, KOKKOS_LAMBDA(const int k, const int j, const int i) {
Expand All @@ -179,14 +189,14 @@ TaskStatus CoolingFunctionCalculateFourForce(MeshBlockData<Real> *rc, const doub
const parthenon::AllReduce<bool> *pdo_gain_reducer =
rad->MutableParam<parthenon::AllReduce<bool>>("do_gain_reducer");
const bool do_gain = pdo_gain_reducer->val;

parthenon::par_for(
DEFAULT_LOOP_PATTERN, "CoolingFunctionCalculateFourForce", DevExecSpace(), kb.s,
kb.e, jb.s, jb.e, ib.s, ib.e,
KOKKOS_LAMBDA(const int k, const int j, const int i) {
const Real r = std::abs(coords.Xc<1>(k, j, i)); // TODO(MG) coord transform game
const Real rho =
v(prho, k, j, i) * unit_conv.GetMassDensityCodeToCGS(); // Density in CGS
const Real cdensity = v(crho, k, j, i); // conserved density
const Real rho = v(prho, k, j, i) * density_conversion_factor; // Density in CGS
const Real cdensity = v(crho, k, j, i); // conserved density
Real Gcov[4][4];
geom.SpacetimeMetric(CellLocation::Cent, k, j, i, Gcov);
Real Ucon[4];
Expand Down Expand Up @@ -245,17 +255,15 @@ TaskStatus CoolingFunctionCalculateFourForce(MeshBlockData<Real> *rc, const doub
Real lambda[2];
lambda[0] = Ye;
eos_sc.MassFractionsFromDensityTemperature(
rho, v(ptemp, k, j, i) * unit_conv.GetTemperatureCodeToCGS(), Xa, Xh, Xn,
Xp, Abar, Zbar, lambda);
rho, v(ptemp, k, j, i) * temperature_conversion_factor, Xa, Xh, Xn, Xp,
Abar, Zbar, lambda);
heat = do_gain * (Xn + Xp) * hfac * std::exp(-tau) *
pow((rnorm / (r * unit_conv.GetLengthCodeToCGS())), 2);
cool =
(Xn + Xp) * cfac * std::exp(-tau) *
pow((v(ptemp, k, j, i) * unit_conv.GetTemperatureCodeToCGS() / Tnorm), 6);
pow((rnorm / (r * length_conversion_factor)), 2);
cool = (Xn + Xp) * cfac * std::exp(-tau) *
pow((v(ptemp, k, j, i) * temperature_conversion_factor / Tnorm), 6);

Real CGSToCodeFact = unit_conv.GetEnergyCGSToCode() /
unit_conv.GetMassCGSToCode() /
unit_conv.GetTimeCGSToCode();
Real CGSToCodeFact =
energy_conversion_factor / mass_conversion_factor / time_conversion_factor;

Real tempr = 1 / 30.76 / 9e20;
Real H = heat * CGSToCodeFact;
Expand All @@ -268,10 +276,8 @@ TaskStatus CoolingFunctionCalculateFourForce(MeshBlockData<Real> *rc, const doub
// detg included above
Kokkos::atomic_add(&(v(mu, k, j, i)), -Gcov_coord[mu - Gcov_lo]);
}
v(GcovHeat, k, j, i) =
v(prho, k, j, i) * unit_conv.GetMassDensityCodeToCGS() * heat;
v(GcovCool, k, j, i) =
v(prho, k, j, i) * unit_conv.GetMassDensityCodeToCGS() * cool;
v(GcovHeat, k, j, i) = v(prho, k, j, i) * density_conversion_factor * heat;
v(GcovCool, k, j, i) = v(prho, k, j, i) * density_conversion_factor * cool;
Kokkos::atomic_add(&(v(Gye, k, j, i)), Jye);
});
#else
Expand Down

0 comments on commit 44f0637

Please sign in to comment.