Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eos information to floors, misc UserWorkBeforeOutput fix #210

Merged
merged 14 commits into from
Mar 20, 2024
Merged
35 changes: 24 additions & 11 deletions src/fixup/fixup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace fixup {
std::shared_ptr<StateDescriptor> Initialize(ParameterInput *pin) {
auto fix = std::make_shared<StateDescriptor>("fixup");
Params &params = fix->AllParams();
auto mutable_param = parthenon::Params::Mutability::Mutable;

const bool enable_mhd = pin->GetOrAddBoolean("fluid", "mhd", false);
const bool enable_rad = pin->GetOrAddBoolean("physics", "rad", false);
Expand Down Expand Up @@ -273,7 +274,8 @@ std::shared_ptr<StateDescriptor> Initialize(ParameterInput *pin) {
Bounds(params.Get<Floors>("floor"), params.Get<Ceilings>("ceiling"),
params.Get<MHDCeilings>("mhd_ceiling"),
params.Get<RadiationFloors>("rad_floor"),
params.Get<RadiationCeilings>("rad_ceiling")));
params.Get<RadiationCeilings>("rad_ceiling")),
mutable_param);

return fix;
}
Expand Down Expand Up @@ -307,6 +309,9 @@ TaskStatus ApplyFloorsImpl(T *rc, IndexDomain domain = IndexDomain::entire) {

if (!enable_floors) return TaskStatus::complete;

const Real ye_min = eos_pkg->Param<Real>("ye_min");
const Real ye_max = eos_pkg->Param<Real>("ye_max");

const std::vector<std::string> vars(
{p::density::name(), c::density::name(), p::velocity::name(), c::momentum::name(),
p::energy::name(), c::energy::name(), p::bfield::name(), p::ye::name(),
Expand Down Expand Up @@ -344,7 +349,13 @@ TaskStatus ApplyFloorsImpl(T *rc, IndexDomain domain = IndexDomain::entire) {

auto eos = eos_pkg->Param<EOS>("d.EOS");
auto geom = Geometry::GetCoordinateSystem(rc);
auto bounds = fix_pkg->Param<Bounds>("bounds");
Bounds *pbounds = fix_pkg->MutableParam<Bounds>("bounds");

// BLB: Setting EOS bnds for Ceilings/Floors here.
pbounds->SetEOSBnds(eos_pkg);

// copy bounds by value for kernel
Bounds bounds = *pbounds;

const Real c2p_tol = fluid_pkg->Param<Real>("c2p_tol");
const int c2p_max_iter = fluid_pkg->Param<int>("c2p_max_iter");
Expand Down Expand Up @@ -437,16 +448,18 @@ TaskStatus ApplyFloorsImpl(T *rc, IndexDomain domain = IndexDomain::entire) {

if (floor_applied) {
Real vp_normalobs[3] = {0}; // Inject floors at rest in normal observer frame
Real ye_prim_default = 0.5;
eos_lambda[0] = ye_prim_default;
Real ye = 0.5;
if (pye > 0) {
ye = v(b, cye, k, j, i);
}
eos_lambda[0] = ye;
Yurlungur marked this conversation as resolved.
Show resolved Hide resolved
Real dprs =
eos.PressureFromDensityInternalEnergy(drho, ratio(du, drho), eos_lambda);
Real dgm1 = ratio(
eos.BulkModulusFromDensityInternalEnergy(drho, ratio(du, drho), eos_lambda),
dprs);
prim2con::p2c(drho, vp_normalobs, bp, du, ye_prim_default, dprs, dgm1, gcov,
gammacon, betacon, alpha, sdetgam, dcrho, dS, dBcons, dtau,
dyecons);
prim2con::p2c(drho, vp_normalobs, bp, du, ye, dprs, dgm1, gcov, gammacon,
betacon, alpha, sdetgam, dcrho, dS, dBcons, dtau, dyecons);

// Update cons vars (not B field)
v(b, crho, k, j, i) += dcrho;
Expand All @@ -464,7 +477,7 @@ TaskStatus ApplyFloorsImpl(T *rc, IndexDomain domain = IndexDomain::entire) {
SPACELOOP(ii) { v(b, pvel_lo + ii, k, j, i) = vp_normalobs[ii]; }
v(b, peng, k, j, i) = du;
if (pye > 0) {
v(b, pye, k, j, i) = ye_prim_default;
v(b, pye, k, j, i) = ye_min;
}

// Update auxiliary primitives
Expand All @@ -476,9 +489,9 @@ TaskStatus ApplyFloorsImpl(T *rc, IndexDomain domain = IndexDomain::entire) {
eos_lambda);

// Update cons vars (not B field)
prim2con::p2c(drho, vp_normalobs, bp, du, ye_prim_default, dprs, dgm1, gcov,
gammacon, betacon, alpha, sdetgam, v(b, crho, k, j, i), dS,
dBcons, v(b, ceng, k, j, i), dyecons);
prim2con::p2c(drho, vp_normalobs, bp, du, ye, dprs, dgm1, gcov, gammacon,
betacon, alpha, sdetgam, v(b, crho, k, j, i), dS, dBcons,
v(b, ceng, k, j, i), dyecons);
SPACELOOP(ii) { v(b, cmom_lo + ii, k, j, i) = dS[ii]; }
if (pye > 0) {
v(b, cye, k, j, i) = dyecons;
Expand Down
42 changes: 36 additions & 6 deletions src/fixup/fixup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,37 +91,60 @@ class Floors {
KOKKOS_INLINE_FUNCTION
void GetFloors(const Real x1, const Real x2, const Real x3, Real &rflr,
Real &sflr) const {
if (!eos_bnds_set_) {
PARTHENON_FAIL("EOS bounds not set in floors.");
}
AstroBarker marked this conversation as resolved.
Show resolved Hide resolved

switch (floor_flag_) {
case FloorFlag::ConstantRhoSie:
rflr = r0_;
sflr = s0_;
rflr = std::max(rflr, rho_min_eos_);
sflr = std::max(sflr, sie_min_eos_);
break;
case FloorFlag::ExpX1RhoSie:
rflr = r0_ * std::exp(ralpha_ * x1);
sflr = s0_ * std::exp(salpha_ * x1);
rflr = std::max(rflr, rho_min_eos_);
sflr = std::max(sflr, sie_min_eos_);
break;
case FloorFlag::ExpX1RhoU: {
Real scratch = r0_ * std::exp(ralpha_ * x1);
sflr = s0_ * std::exp(salpha_ * x1) / std::max(rflr, scratch);
rflr = scratch;
rflr = std::max(rflr, rho_min_eos_);
sflr = std::max(sflr, sie_min_eos_);
} break;
case FloorFlag::X1RhoSie:
rflr = r0_ * std::min(1.0, std::pow(x1, ralpha_));
sflr = s0_ * std::min(1.0, std::pow(x1, salpha_));
rflr = std::max(rflr, rho_min_eos_);
sflr = std::max(sflr, sie_min_eos_);
break;
case FloorFlag::RRhoSie: {
Real r = std::sqrt(x1 * x1 + x2 * x2 + x3 * x3);
rflr = r0_ * std::min(1.0, std::pow(r, ralpha_));
sflr = s0_ * std::min(1.0, std::pow(r, salpha_));
rflr = std::max(rflr, rho_min_eos_);
sflr = std::max(sflr, sie_min_eos_);
} break;
default:
PARTHENON_FAIL("No valid floor set.");
}
}

void SetEOSBnds(StateDescriptor *eos_pkg) {
if (!eos_bnds_set_) {
rho_min_eos_ = eos_pkg->Param<Real>("rho_min");
sie_min_eos_ = eos_pkg->Param<Real>("sie_min");
eos_bnds_set_ = true;
}
}

private:
Real r0_, s0_, ralpha_, salpha_;
Real r0_, s0_, ralpha_, salpha_, rho_min_eos_, sie_min_eos_;
const FloorFlag floor_flag_;
bool eos_bnds_set_;
};

static struct ConstantJFloor {
Expand Down Expand Up @@ -173,6 +196,7 @@ class Ceilings {
KOKKOS_INLINE_FUNCTION
void GetCeilings(const Real x1, const Real x2, const Real x3, Real &gmax,
Real &smax) const {

switch (ceiling_flag_) {
case 1:
gmax = g0_;
Expand All @@ -184,8 +208,9 @@ class Ceilings {
}

private:
Real g0_, s0_;
Real g0_, s0_, sie_max_eos_;
Yurlungur marked this conversation as resolved.
Show resolved Hide resolved
const int ceiling_flag_;
bool eos_bnds_set_;
Yurlungur marked this conversation as resolved.
Show resolved Hide resolved
};

static struct ConstantBsqRatCeiling {
Expand Down Expand Up @@ -257,13 +282,18 @@ class Bounds {
const RadiationFloors &rfl, const RadiationCeilings &rcl)
: floors_(fl), ceilings_(cl), mhd_ceilings_(mcl), radiation_floors_(rfl),
radiation_ceilings_(rcl) {}
explicit Bounds(const Floors &fl)
explicit Bounds(Floors &fl)
: floors_(fl), ceilings_(Ceilings()), mhd_ceilings_(MHDCeilings()),
radiation_floors_(RadiationFloors()), radiation_ceilings_(RadiationCeilings()) {}
explicit Bounds(const Ceilings &cl)
explicit Bounds(Ceilings &cl)
: floors_(Floors()), ceilings_(cl), mhd_ceilings_(MHDCeilings()),
radiation_floors_(RadiationFloors()), radiation_ceilings_(RadiationCeilings()) {}

template <class... Args>
void SetEOSBnds(Args &&...args) {
floors_.SetEOSBnds(std::forward<Args>(args)...);
}

template <class... Args>
KOKKOS_INLINE_FUNCTION void GetFloors(Args &&...args) const {
floors_.GetFloors(std::forward<Args>(args)...);
Expand All @@ -290,8 +320,8 @@ class Bounds {
}

private:
const Floors floors_;
const Ceilings ceilings_;
Floors floors_;
Ceilings ceilings_;
const MHDCeilings mhd_ceilings_;
const RadiationFloors radiation_floors_;
const RadiationCeilings radiation_ceilings_;
Expand Down
3 changes: 2 additions & 1 deletion src/fixup/fixup_c2p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ TaskStatus ConservedToPrimitiveFixupImpl(T *rc) {

auto eos = eos_pkg->Param<Microphysics::EOS::EOS>("d.EOS");
auto geom = Geometry::GetCoordinateSystem(rc);
auto bounds = fix_pkg->Param<Bounds>("bounds");
Bounds *pbounds = fix_pkg->MutableParam<Bounds>("bounds");
Bounds bounds = *pbounds;

Coordinates_t coords = rc->GetParentPointer()->coords;

Expand Down
6 changes: 3 additions & 3 deletions src/fixup/fixup_radc2p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ TaskStatus RadConservedToPrimitiveFixupImpl(T *rc) {
}

auto geom = Geometry::GetCoordinateSystem(rc);
auto bounds = fix_pkg->Param<Bounds>("bounds");
Bounds *bounds = fix_pkg->MutableParam<Bounds>("bounds");

Coordinates_t coords = rc->GetParentPointer()->coords;

Expand All @@ -129,8 +129,8 @@ TaskStatus RadConservedToPrimitiveFixupImpl(T *rc) {
KOKKOS_LAMBDA(const int b, const int k, const int j, const int i) {
Real xi_max;
Real garbage;
bounds.GetRadiationCeilings(coords.Xc<1>(k, j, i), coords.Xc<2>(k, j, i),
coords.Xc<3>(k, j, i), xi_max, garbage);
bounds->GetRadiationCeilings(coords.Xc<1>(k, j, i), coords.Xc<2>(k, j, i),
coords.Xc<3>(k, j, i), xi_max, garbage);

// It is assumed that the fluid is already fixed up
auto fail = [&](const int k, const int j, const int i) {
Expand Down
10 changes: 5 additions & 5 deletions src/fixup/fixup_src.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ TaskStatus SourceFixupImpl(T *rc) {
}

auto eos = eos_pkg->Param<EOS>("d.EOS");
auto bounds = fix_pkg->Param<Bounds>("bounds");
Bounds *bounds = fix_pkg->MutableParam<Bounds>("bounds");

const std::vector<std::string> vars(
{p::density::name(), c::density::name(), p::velocity::name(), c::momentum::name(),
Expand Down Expand Up @@ -151,12 +151,12 @@ TaskStatus SourceFixupImpl(T *rc) {
kb.e, jb.s, jb.e, ib.s, ib.e,
KOKKOS_LAMBDA(const int b, const int k, const int j, const int i) {
double gamma_max, e_max;
bounds.GetCeilings(coords.Xc<1>(k, j, i), coords.Xc<2>(k, j, i),
coords.Xc<3>(k, j, i), gamma_max, e_max);
bounds->GetCeilings(coords.Xc<1>(k, j, i), coords.Xc<2>(k, j, i),
coords.Xc<3>(k, j, i), gamma_max, e_max);
Real xi_max;
Real garbage;
bounds.GetRadiationCeilings(coords.Xc<1>(k, j, i), coords.Xc<2>(k, j, i),
coords.Xc<3>(k, j, i), xi_max, garbage);
bounds->GetRadiationCeilings(coords.Xc<1>(k, j, i), coords.Xc<2>(k, j, i),
coords.Xc<3>(k, j, i), xi_max, garbage);

double eos_lambda[2]; // used for stellarcollapse eos and other EOS's that require
// root finding.
Expand Down
2 changes: 1 addition & 1 deletion src/fluid/con2prim_robust.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class Residual {
private:
const Real D_, q_, bsq_, bsq_rpsq_, rsq_, rbsq_, v0sq_;
const Microphysics::EOS::EOS &eos_;
const fixup::Bounds &bounds_;
const fixup::Bounds bounds_;
const Real x1_, x2_, x3_;
const Real floor_scale_fac_;
Real lambda_[2];
Expand Down
27 changes: 5 additions & 22 deletions src/fluid/fluid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include "fluid.hpp"

#include "analysis/analysis.hpp"
#include "analysis/history.hpp"
#include "con2prim.hpp"
#include "con2prim_robust.hpp"
Expand Down Expand Up @@ -208,8 +207,6 @@ std::shared_ptr<StateDescriptor> Initialize(ParameterInput *pin) {
physics->AddField(p::entropy::name(), mprim_scalar);
physics->AddField(p::cs::name(), mprim_scalar);
physics->AddField(diag::ratio_divv_cs::name(), mprim_scalar);
// physics->AddField(diag::localization_function::name(), mprim_scalar);
// (MG) currently not in use, turn on when needed.
physics->AddField(diag::entropy_z_0::name(), mprim_scalar);
physics->AddField(p::gamma1::name(), mprim_scalar);
if (ye) {
Expand Down Expand Up @@ -312,8 +309,6 @@ std::shared_ptr<StateDescriptor> Initialize(ParameterInput *pin) {
// Reductions
// By default compute integrated value of scalar conserved vars
auto HstSum = parthenon::UserHistoryOperation::sum;
auto HstMax = parthenon::UserHistoryOperation::max;
using History::ReduceInGain;
using History::ReduceOneVar;
using parthenon::HistoryOutputVar;
parthenon::HstVar_list hst_vars = {};
Expand All @@ -324,11 +319,6 @@ std::shared_ptr<StateDescriptor> Initialize(ParameterInput *pin) {
auto ReduceEn = [](MeshData<Real> *md) {
return ReduceOneVar<Kokkos::Sum<Real>>(md, fluid_cons::energy::name(), 0);
};
auto MaxDensity = [](MeshData<Real> *md) {
return ReduceOneVar<Kokkos::Max<Real>>(md, fluid_prim::density::name(), 0);
};

hst_vars.emplace_back(HistoryOutputVar(HstMax, MaxDensity, "maximum density"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this branch might be out of date with main... double check you and @mari2895 are in sync so your PRs don't undo each other.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is the same as current main, unless I missed something.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a diff showing here then? Odd.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see it now... not sure why it's different from main, don't think I did this myself..

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just merge main in again... see if that fixes it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AstroBarker Looks the same on my branch too. Don't see anything different.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but why is it deleted? should not be deleted

hst_vars.emplace_back(HistoryOutputVar(HstSum, ReduceMass, "total baryon number"));
hst_vars.emplace_back(HistoryOutputVar(HstSum, ReduceEn, "total conserved energy tau"));

Expand Down Expand Up @@ -451,15 +441,6 @@ TaskStatus ConservedToPrimitiveRegion(T *rc, const IndexRange &ib, const IndexRa
auto c2p = pkg->Param<c2p_type<T>>("c2p_func");
return c2p(rc, ib, jb, kb);
}
// JMM: Must specialize function for both potential use cases so we
// can keep it in this file.
template TaskStatus ConservedToPrimitiveRegion<MeshData<Real>>(MeshData<Real> *rc,
const IndexRange &ib,
const IndexRange &jb,
const IndexRange &kb);
template TaskStatus ConservedToPrimitiveRegion<MeshBlockData<Real>>(
MeshBlockData<Real> *rc, const IndexRange &ib, const IndexRange &jb,
const IndexRange &kb);

template <typename T>
TaskStatus ConservedToPrimitive(T *rc) {
Expand All @@ -476,7 +457,10 @@ TaskStatus ConservedToPrimitiveRobust(T *rc, const IndexRange &ib, const IndexRa
auto *pmb = rc->GetParentPointer();

StateDescriptor *fix_pkg = pmb->packages.Get("fixup").get();
auto bounds = fix_pkg->Param<fixup::Bounds>("bounds");
StateDescriptor *eos_pkg = pmb->packages.Get("eos").get();
fixup::Bounds *pbounds = fix_pkg->MutableParam<fixup::Bounds>("bounds");
pbounds->SetEOSBnds(eos_pkg);
fixup::Bounds bounds = *pbounds;

StateDescriptor *pkg = pmb->packages.Get("fluid").get();
const Real c2p_tol = pkg->Param<Real>("c2p_tol");
Expand All @@ -488,7 +472,6 @@ TaskStatus ConservedToPrimitiveRobust(T *rc, const IndexRange &ib, const IndexRa
c2p_floor_scale_fac, c2p_fail_on_floors,
c2p_fail_on_ceilings);

StateDescriptor *eos_pkg = pmb->packages.Get("eos").get();
auto eos = eos_pkg->Param<Microphysics::EOS::EOS>("d.EOS");
auto geom = Geometry::GetCoordinateSystem(rc);
auto coords = pmb->coords;
Expand Down Expand Up @@ -516,7 +499,7 @@ TaskStatus ConservedToPrimitiveClassic(T *rc, const IndexRange &ib, const IndexR
auto *pmesh = rc->GetMeshPointer();

StateDescriptor *fix_pkg = pmesh->packages.Get("fixup").get();
auto bounds = fix_pkg->Param<fixup::Bounds>("bounds");
fixup::Bounds *bounds = fix_pkg->MutableParam<fixup::Bounds>("bounds");

StateDescriptor *pkg = pmesh->packages.Get("fluid").get();
const Real c2p_tol = pkg->Param<Real>("c2p_tol");
Expand Down
10 changes: 0 additions & 10 deletions src/fluid/fluid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,9 @@ std::shared_ptr<StateDescriptor> Initialize(ParameterInput *pin);
TaskStatus PrimitiveToConserved(MeshBlockData<Real> *rc);
TaskStatus PrimitiveToConservedRegion(MeshBlockData<Real> *rc, const IndexRange &ib,
const IndexRange &jb, const IndexRange &kb);
// JMM: Not sure how the templated value here worked in the first
// place. But proper solution is need to tell the linker it's
// available elsewhere.
template <typename T>
TaskStatus ConservedToPrimitiveRegion(T *rc, const IndexRange &ib, const IndexRange &jb,
const IndexRange &kb);
extern template TaskStatus
ConservedToPrimitiveRegion<MeshData<Real>>(MeshData<Real> *rc, const IndexRange &ib,
const IndexRange &jb, const IndexRange &kb);
extern template TaskStatus ConservedToPrimitiveRegion<MeshBlockData<Real>>(
MeshBlockData<Real> *rc, const IndexRange &ib, const IndexRange &jb,
const IndexRange &kb);

template <typename T>
TaskStatus ConservedToPrimitive(T *rc);
template <typename T>
Expand Down
9 changes: 6 additions & 3 deletions src/fluid/riemann.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class FluxState {
const ParArrayND<Real> qr;
const Geometry::CoordSysMeshBlock geom;
const Coordinates_t coords;
fixup::Bounds *pbounds;
fixup::Bounds bounds;

private:
Expand All @@ -216,9 +217,11 @@ class FluxState {
ql(rc->Get("ql").data), qr(rc->Get("qr").data),
geom(Geometry::GetCoordinateSystem(rc)),
coords(rc->GetParentPointer()->coords), // problem for packs
bounds(rc->GetParentPointer()->packages.Get("fixup").get()->Param<fixup::Bounds>(
"bounds")),
prho(imap[fluid_prim::density::name()].first),
pbounds(rc->GetParentPointer()
->packages.Get("fixup")
.get()
->MutableParam<fixup::Bounds>("bounds")),
bounds(*pbounds), prho(imap[fluid_prim::density::name()].first),
pvel_lo(imap[fluid_prim::velocity::name()].first),
peng(imap[fluid_prim::energy::name()].first),
pb_lo(imap[fluid_prim::bfield::name()].first),
Expand Down
Loading
Loading