Skip to content

Commit

Permalink
first part, will not work... not complete
Browse files Browse the repository at this point in the history
  • Loading branch information
mari2895 committed Jan 19, 2024
1 parent 7b56701 commit 3ea0dee
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 200 deletions.
155 changes: 67 additions & 88 deletions src/fixup/fixup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,19 @@ TaskStatus ApplyFloorsImpl(T *rc, IndexDomain domain = IndexDomain::entire) {
namespace cr = radmoment_cons;
namespace ir = radmoment_internal;
namespace impl = internal_variables;

using parthenon::MakePackDescriptor;
auto *pmesh = rc->GetMeshPointer();
auto &resolved_pkg = pmesh->resolved_packages;
const int ndim = pmesh->ndim;
static auto desc = MakePackDescriptor<p::density, c::density, p::velocity, c::momentum,
p::energy, c::energy, p::bfield, p::ye,
c::ye, p::pressure, p::temperature, p::gamma1,
pr::J, pr::H, cr::E, cr::F,
impl::cell_signal_speed, impl::fail, ir::tilPi>(resolved_pkgs.get());
auto v = desc.GetPack();
const int nblocks = v.GetNBlocks();

IndexRange ib = rc->GetBoundsI(domain);
IndexRange jb = rc->GetBoundsJ(domain);
IndexRange kb = rc->GetBoundsK(domain);
Expand All @@ -306,39 +318,6 @@ TaskStatus ApplyFloorsImpl(T *rc, IndexDomain domain = IndexDomain::entire) {

if (!enable_floors) return TaskStatus::complete;

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(),
c::ye::name(), p::pressure::name(), p::temperature::name(), p::gamma1::name(),
pr::J::name(), pr::H::name(), cr::E::name(), cr::F::name(),
impl::cell_signal_speed::name(), impl::fail::name(), ir::tilPi::name()});

PackIndexMap imap;
auto v = rc->PackVariables(vars, imap);

const int prho = imap[p::density::name()].first;
const int crho = imap[c::density::name()].first;
const int pvel_lo = imap[p::velocity::name()].first;
const int pvel_hi = imap[p::velocity::name()].second;
const int cmom_lo = imap[c::momentum::name()].first;
const int cmom_hi = imap[c::momentum::name()].second;
const int peng = imap[p::energy::name()].first;
const int ceng = imap[c::energy::name()].first;
const int prs = imap[p::pressure::name()].first;
const int tmp = imap[p::temperature::name()].first;
const int gm1 = imap[p::gamma1::name()].first;
const int slo = imap[impl::cell_signal_speed::name()].first;
const int shi = imap[impl::cell_signal_speed::name()].second;
const int pb_lo = imap[p::bfield::name()].first;
const int pb_hi = imap[p::bfield::name()].second;
int pye = imap[p::ye::name()].second; // negative if not present
int cye = imap[c::ye::name()].second;
auto idx_J = imap.GetFlatIdx(pr::J::name(), false);
auto idx_H = imap.GetFlatIdx(pr::H::name(), false);
auto idx_E = imap.GetFlatIdx(cr::E::name(), false);
auto idx_F = imap.GetFlatIdx(cr::F::name(), false);
auto iTilPi = imap.GetFlatIdx(ir::tilPi::name(), false);

const int num_species = enable_rad_floors ? rad_pkg->Param<int>("num_species") : 0;

auto eos = eos_pkg->Param<EOS>("d.EOS");
Expand All @@ -363,7 +342,7 @@ TaskStatus ApplyFloorsImpl(T *rc, IndexDomain domain = IndexDomain::entire) {
double eos_lambda[2]; // used for stellarcollapse eos and
// other EOS's that require root
// finding.
eos_lambda[1] = std::log10(v(b, tmp, k, j, i)); // use last temp as initial guess
eos_lambda[1] = std::log10(v(b, p::temperature(), k, j, i)); // use last temp as initial guess

double rho_floor, sie_floor;
bounds.GetFloors(coords.Xc<1>(k, j, i), coords.Xc<2>(k, j, i),
Expand Down Expand Up @@ -400,10 +379,10 @@ TaskStatus ApplyFloorsImpl(T *rc, IndexDomain domain = IndexDomain::entire) {
if (enable_mhd_floors) {
Real Bsq = 0.0;
Real Bdotv = 0.0;
const Real vp[3] = {v(b, pvel_lo, k, j, i), v(b, pvel_lo + 1, k, j, i),
v(b, pvel_lo + 2, k, j, i)};
const Real bp[3] = {v(b, pb_lo, k, j, i), v(pb_lo + 1, k, j, i),
v(pb_lo + 2, k, j, i)};
const Real vp[3] = {v(b, p::velocity(0), k, j, i), v(b, p::velocity(1), k, j, i),
v(b, p::velocity(2), k, j, i)};
const Real bp[3] = {v(b, p::bfield(0), k, j, i), v(p::bfield(1), k, j, i),
v(p::bfield(2), k, j, i)};
const Real W = phoebus::GetLorentzFactor(vp, gcov);
const Real iW = 1.0 / W;
SPACELOOP2(ii, jj) {
Expand All @@ -417,11 +396,11 @@ TaskStatus ApplyFloorsImpl(T *rc, IndexDomain domain = IndexDomain::entire) {
u_floor_max = std::max<Real>(u_floor_max, bsq / bsqou_max);

rho_floor_max = std::max<Real>(
rho_floor_max, std::max<Real>(v(b, peng, k, j, i), u_floor_max) / e_max);
rho_floor_max, std::max<Real>(v(b, p::energy(), k, j, i), u_floor_max) / e_max);
}

Real drho = rho_floor_max - v(b, prho, k, j, i);
Real du = u_floor_max - v(b, peng, k, j, i);
Real drho = rho_floor_max - v(b, p::density(), k, j, i);
Real du = u_floor_max - v(b, p::energy(), k, j, i);
if (drho > 0. || du > 0.) {
floor_applied = true;
drho = std::max<Real>(drho, du / sie_floor);
Expand All @@ -431,7 +410,7 @@ TaskStatus ApplyFloorsImpl(T *rc, IndexDomain domain = IndexDomain::entire) {
Real dcrho, dS[3], dBcons[3], dtau, dyecons;
Real bp[3] = {0};
if (pb_hi > 0) {
SPACELOOP(ii) { bp[ii] = v(b, pb_lo + ii, k, j, i); }
SPACELOOP(ii) { bp[ii] = v(b, p::bfield(ii), k, j, i); }
}

if (floor_applied) {
Expand All @@ -448,90 +427,90 @@ TaskStatus ApplyFloorsImpl(T *rc, IndexDomain domain = IndexDomain::entire) {
dyecons);

// Update cons vars (not B field)
v(b, crho, k, j, i) += dcrho;
SPACELOOP(ii) { v(b, cmom_lo + ii, k, j, i) += dS[ii]; }
v(b, ceng, k, j, i) += dtau;
if (pye > 0) {
v(b, cye, k, j, i) += dyecons;
v(b, c::debsity(), k, j, i) += dcrho;
SPACELOOP(ii) { v(b, c::momentum(ii), k, j, i) += dS[ii]; }
v(b, c::engergy(), k, j, i) += dtau;
if (v.Contains(b,p::ye())) {
v(b, c::ye(), k, j, i) += dyecons;
}

// fluid c2p
auto status = invert(geom, eos, coords, k, j, i);
if (status == con2prim_robust::ConToPrimStatus::failure) {
// If fluid c2p fails, set to floors
v(b, prho, k, j, i) = drho;
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, p::density(), k, j, i) = drho;
SPACELOOP(ii) { v(b, p::velocity(ii), k, j, i) = vp_normalobs[ii]; }
v(b, p::energy(), k, j, i) = du;
if (v.Contains(b,p::ye())) {
v(b, p::ye(), k, j, i) = ye_prim_default;
}

// Update auxiliary primitives
if (pye > 0) {
eos_lambda[0] = v(b, pye, k, j, i);
if (v.Contains(b,p::ye())) {
eos_lambda[0] = v(b, p::ye(), k, j, i);
}
v(b, tmp, k, j, i) = eos.TemperatureFromDensityInternalEnergy(
v(b, prho, k, j, i), v(b, peng, k, j, i) / v(b, prho, k, j, i),
v(b, p::temperature(), k, j, i) = eos.TemperatureFromDensityInternalEnergy(
v(b, p::density(), k, j, i), v(b, p::energy(), k, j, i) / v(b, p::density(), k, j, i),
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);
SPACELOOP(ii) { v(b, cmom_lo + ii, k, j, i) = dS[ii]; }
if (pye > 0) {
v(b, cye, k, j, i) = dyecons;
gammacon, betacon, alpha, sdetgam, v(b, c::density(), k, j, i), dS,
dBcons, v(b, c::energy(), k, j, i), dyecons);
SPACELOOP(ii) { v(b, c::momentum(ii), k, j, i) = dS[ii]; }
if (v.Contains(b,p::ye())) {
v(b, c::ye(), k, j, i) = dyecons;
}
}
}

// Fluid ceilings?
Real vpcon[3] = {v(b, pvel_lo, k, j, i), v(b, pvel_lo + 1, k, j, i),
v(b, pvel_lo + 2, k, j, i)};
Real vpcon[3] = {v(b, p::velocity(0), k, j, i), v(b, p::velocity(1), k, j, i),
v(b, p::velocity(2), k, j, i)};
const Real W = phoebus::GetLorentzFactor(vpcon, gcov);
if (W > gamma_max || v(b, peng, k, j, i) / v(b, prho, k, j, i) > e_max) {
if (W > gamma_max || v(b, p::engergy(), k, j, i) / v(b, p::density(), k, j, i) > e_max) {
ceiling_applied = true;
}

if (ceiling_applied) {
const Real rescale = std::sqrt(gamma_max * gamma_max - 1.) / (W * W - 1.);
SPACELOOP(ii) { vpcon[ii] *= rescale; }
SPACELOOP(ii) { v(b, pvel_lo + ii, k, j, i) = vpcon[ii]; }
SPACELOOP(ii) { v(b, p::velocity(ii), k, j, i) = vpcon[ii]; }

Real ye = 0.;
if (pye > 0) {
ye = v(b, pye, k, j, i);
if (v.Contains(b,p::ye())) {
ye = v(b, p::ye(), k, j, i);
}
prim2con::p2c(v(b, prho, k, j, i), vpcon, bp, v(b, peng, k, j, i), ye,
v(b, prs, k, j, i), v(b, gm1, k, j, i), gcov, gammacon, betacon,
alpha, sdetgam, v(b, crho, k, j, i), dS, dBcons,
prim2con::p2c(v(b, p::density(), k, j, i), vpcon, bp, v(b, p::energy, k, j, i), ye,
v(b, p::pressure(), k, j, i), v(b, p::gamma1(), k, j, i), gcov, gammacon, betacon,
alpha, sdetgam, v(b, c::density, 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;
SPACELOOP(ii) { v(b, c::momentum(ii), k, j, i) = dS[ii]; }
if (v.Contains(b,p::ye())) {
v(b, c::ye(), k, j, i) = dyecons;
}
}

if (floor_applied || ceiling_applied) {
// Update derived prims
if (pye > 0) eos_lambda[0] = v(b, pye, k, j, i);
v(b, tmp, k, j, i) = eos.TemperatureFromDensityInternalEnergy(
v(b, prho, k, j, i), ratio(v(b, peng, k, j, i), v(b, prho, k, j, i)),
if (v.Contains(b,p::ye())) eos_lambda[0] = v(b, p::ye(), k, j, i);
v(b, p::temperature(), k, j, i) = eos.TemperatureFromDensityInternalEnergy(
v(b, p::density(), k, j, i), ratio(v(b, p::energy(), k, j, i), v(b, p::density(), k, j, i)),
eos_lambda);
v(b, prs, k, j, i) = eos.PressureFromDensityTemperature(
v(b, prho, k, j, i), v(b, tmp, k, j, i), eos_lambda);
v(b, gm1, k, j, i) =
ratio(eos.BulkModulusFromDensityTemperature(v(b, prho, k, j, i),
v(b, tmp, k, j, i), eos_lambda),
v(b, prs, k, j, i));
v(b, p::pressure(), k, j, i) = eos.PressureFromDensityTemperature(
v(b, p::density(), k, j, i), v(b, p::temperature, k, j, i), eos_lambda);
v(b, p::gamma1, k, j, i) =
ratio(eos.BulkModulusFromDensityTemperature(v(b, p::density(), k, j, i),
v(b, p::temperature(), k, j, i), eos_lambda),
v(b, p::pressure(), k, j, i));
}

if (rad_active) {
Vec con_vp{v(b, pvel_lo, k, j, i), v(b, pvel_lo + 1, k, j, i),
v(b, pvel_lo + 2, k, j, i)};
Vec con_vp{v(b, p::velocity(0), k, j, i), v(b, p::velocity(1), k, j, i),
v(b, p::velocity(2), k, j, i)};
const Real W = phoebus::GetLorentzFactor(con_vp.data, gcov);
Vec con_v{v(b, pvel_lo, k, j, i) / W, v(b, pvel_lo + 1, k, j, i) / W,
v(b, pvel_lo + 2, k, j, i) / W};
Vec con_v{v(b, p::velocity(0), k, j, i) / W, v(b, p::velocity(1), k, j, i) / W,
v(b, p::velocity(2), k, j, i) / W};
typename CLOSURE::LocalGeometryType g(geom, CellLocation::Cent, b, k, j, i);
Real E;
Real J;
Expand All @@ -552,7 +531,7 @@ TaskStatus ApplyFloorsImpl(T *rc, IndexDomain domain = IndexDomain::entire) {
// We need the real conTilPi
if (iTilPi.IsValid()) {
SPACELOOP2(ii, jj) {
con_TilPi(ii, jj) = v(b, iTilPi(ispec, ii, jj), k, j, i);
con_TilPi(ii, jj) = v(b, ir::tilPi()(ispec, ii, jj), k, j, i);
}
} else {
// TODO(BRR) don't be lazy and actually retrieve these
Expand Down Expand Up @@ -703,7 +682,7 @@ TaskStatus ApplyFloors(T *rc) {
return TaskStatus::fail;
}

template TaskStatus ApplyFloors<MeshBlockData<Real>>(MeshBlockData<Real> *rc);
template TaskStatus ApplyFloors<MeshData<Real>>(MeshData<Real> *rc);

TaskStatus FixFluxes(MeshBlockData<Real> *rc) {
using parthenon::BoundaryFace;
Expand Down
Loading

0 comments on commit 3ea0dee

Please sign in to comment.