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

Maintain sanity (or madness) #165

Merged
merged 28 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e82a155
First attempt
brryan Mar 30, 2023
90645c0
Merge branch 'main' of github.com:lanl/phoebus into brryan/maintain_s…
brryan Apr 18, 2023
32f4655
First attempt
brryan Apr 18, 2023
884a4da
sing?
brryan Apr 18, 2023
1910658
This is better maybe it even works
brryan Apr 18, 2023
35ce897
Fix bug in A
brryan Apr 19, 2023
9a5e2f4
start time for phi adjustment
brryan Apr 19, 2023
96bce20
phi not Phi
brryan Apr 19, 2023
e495489
Formatting
brryan Apr 19, 2023
2186e97
Update torus input file
brryan Apr 19, 2023
abfff6a
Cleanup
brryan Apr 19, 2023
6602af1
Hopefully fix CI
brryan Apr 20, 2023
047a8ae
Have gold_comparison in regression_test provide more (any) informatio…
brryan Apr 27, 2023
54529db
Test of git with vscode
brryan Apr 27, 2023
da90bf9
Merge branch 'brryan/maintain_sanity' of github.com:lanl/phoebus into…
brryan Apr 27, 2023
5f95e36
Working on net field tasking
brryan Apr 27, 2023
29d7281
First pass seems to work with new machinery
brryan Apr 28, 2023
fce5dc1
Better dphi_dt damper
brryan Apr 28, 2023
fd227af
history geom bugfix
brryan Apr 28, 2023
a37c8fa
working pre-formatting
brryan Apr 28, 2023
02c0f1c
Merge branch 'main' of github.com:lanl/phoebus into brryan/maintain_s…
brryan May 2, 2023
36dd811
Properly deal with phoedf geom issue
brryan May 2, 2023
9f7883b
Back to main versions of submodules
brryan May 2, 2023
dec72de
More general ghost cell removal
brryan May 2, 2023
4c490e1
Remove unused variable
brryan May 2, 2023
780b9ea
Remove unnecessary check
brryan May 3, 2023
09ed3a5
No default value for enforced phi
brryan May 3, 2023
c3bfe65
Only set other phi enforcement values if active
brryan May 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions inputs/torus.pin
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ variables = p.density, &
c.energy, &
pressure, &
g.c.coord, &
g.f1.coord, &
g.f2.coord, &
g.f3.coord, &
g.f1.coord, &
g.f2.coord, &
g.f3.coord, &
g.n.coord, &
g.c.detgam, &
g.c.bcon, &
Expand All @@ -40,6 +40,10 @@ variables = p.density, &
file_type = hdf5 # Tabular data dump
dt = 5.0 # time increment between outputs

<parthenon/output2>
file_type = hst # History data dump
dt = 1.0 # Time increment between outputs

<parthenon/time>
nlim = -1 # cycle limit
tlim = 2000.0 # time limit
Expand Down Expand Up @@ -136,6 +140,10 @@ ceiling_type = ConstantGamSie
sie0_ceiling = 1.0
gam0_ceiling = 10.0
enable_ox1_fmks_inflow_check = true
enable_phi_enforcement = false
enforced_phi = 1.
enforced_phi_timescale = 500.
enforced_phi_cadence = 1.

<torus>
target_beta = 100.
Expand Down
2 changes: 1 addition & 1 deletion scripts/python/phoedf.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def flatten_indices(mu, nu):
self.gcov = np.zeros([self.NumBlocks, 4, 4, self.Nx3, self.Nx2, self.Nx1])
for mu in range(4):
for nu in range(4):
self.gcov[:,mu,nu,:,:,:] = self.flatgcov[:,flatten_indices(mu,nu),:,:,:]
self.gcov[:,mu,nu,:,:,:] = self.flatgcov[:,flatten_indices(mu,nu),:,4:-4,4:-4]
del(self.flatgcov)
self.gcon = np.zeros([self.NumBlocks, 4, 4, self.Nx3, self.Nx2, self.Nx1])
for b in range(self.NumBlocks):
Expand Down
169 changes: 169 additions & 0 deletions src/fixup/fixup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
#include <bvals/bvals_interfaces.hpp>
#include <defs.hpp>

#include "analysis/history.hpp"
#include "fluid/con2prim_robust.hpp"
#include "fluid/fluid.hpp"
#include "fluid/prim2con.hpp"
#include "geometry/geometry.hpp"
#include "microphysics/eos_phoebus/eos_phoebus.hpp"
#include "phoebus_utils/programming_utils.hpp"
#include "phoebus_utils/reduction.hpp"
#include "phoebus_utils/relativity_utils.hpp"
#include "phoebus_utils/robust.hpp"
#include "phoebus_utils/variables.hpp"
Expand Down Expand Up @@ -71,6 +73,23 @@ std::shared_ptr<StateDescriptor> Initialize(ParameterInput *pin) {
bool enable_mhd_floors = pin->GetOrAddBoolean("fixup", "enable_mhd_floors", false);
bool enable_rad_floors = pin->GetOrAddBoolean("fixup", "enable_rad_floors", false);
bool enable_rad_ceilings = pin->GetOrAddBoolean("fixup", "enable_rad_ceilings", false);
bool enable_phi_enforcement =
pin->GetOrAddBoolean("fixup", "enable_phi_enforcement", false);
params.Add("enable_phi_enforcement", enable_phi_enforcement);
Real enforced_phi = pin->GetOrAddReal("fixup", "enforced_phi", 0.);
brryan marked this conversation as resolved.
Show resolved Hide resolved
params.Add("enforced_phi", enforced_phi);
Real enforced_phi_timescale =
pin->GetOrAddReal("fixup", "enforced_phi_timescale", 1.e3);
params.Add("enforced_phi_timescale", enforced_phi_timescale);
Real enforced_phi_cadence = pin->GetOrAddReal("fixup", "enforced_phi_cadence", 10.);
params.Add("enforced_phi_cadence", enforced_phi_cadence);
Real enforced_phi_start_time =
pin->GetOrAddReal("fixup", "enforced_phi_start_time", 175.);
params.Add("enforced_phi_start_time", enforced_phi_start_time);
if (enable_phi_enforcement) {
PARTHENON_REQUIRE(typeid(PHOEBUS_GEOMETRY) == typeid(Geometry::FMKS),
"Phi enforcement only supported for BH geometry!");
}

if (enable_mhd_floors && !enable_mhd) {
enable_mhd_floors = false;
Expand Down Expand Up @@ -645,6 +664,156 @@ TaskStatus ApplyFloorsImpl(T *rc, IndexDomain domain = IndexDomain::entire) {
return TaskStatus::complete;
}

static Real dphi_dt = 0.;
static Real next_dphi_dt_update_time = 0.;
static Real phi_factor = 0.;

TaskStatus EndOfStepModify(MeshData<Real> *md, const Real t, const Real dt,
bool last_stage) {
auto *pm = md->GetParentPointer();
StateDescriptor *fix_pkg = pm->packages.Get("fixup").get();

const bool enable_phi_enforcement = fix_pkg->Param<bool>("enable_phi_enforcement");

namespace p = fluid_prim;
namespace c = fluid_cons;
const std::vector<std::string> vars({p::bfield, c::bfield});
PackIndexMap imap;
auto pack = md->PackVariables(vars, imap);

const int pblo = imap[p::bfield].first;
const int cblo = imap[c::bfield].first;

const auto ib = md->GetBoundsI(IndexDomain::interior);
const auto jb = md->GetBoundsJ(IndexDomain::interior);
const auto kb = md->GetBoundsK(IndexDomain::interior);

auto geom = Geometry::GetCoordinateSystem(md);
auto gpkg = pm->packages.Get("geometry");
bool derefine_poles = gpkg->Param<bool>("derefine_poles");
Real h = gpkg->Param<Real>("h");
Real xt = gpkg->Param<Real>("xt");
Real alpha = gpkg->Param<Real>("alpha");
Real x0 = gpkg->Param<Real>("x0");
Real smooth = gpkg->Param<Real>("smooth");
auto tr = Geometry::McKinneyGammieRyan(derefine_poles, h, xt, alpha, x0, smooth);

if (enable_phi_enforcement) {
const Real enforced_phi = fix_pkg->Param<Real>("enforced_phi");
const Real enforced_phi_timescale = fix_pkg->Param<Real>("enforced_phi_timescale");
const Real enforced_phi_cadence = fix_pkg->Param<Real>("enforced_phi_cadence");
const Real enforced_phi_start_time = fix_pkg->Param<Real>("enforced_phi_start_time");
if (t > enforced_phi_start_time) {

// This only needs to be done at initialization
Yurlungur marked this conversation as resolved.
Show resolved Hide resolved
ParArrayND<Real> A("vector potential", pack.GetDim(5), jb.e + 2, ib.e + 2);
// Update B field
parthenon::par_for(
parthenon::LoopPatternMDRange(), "Phoebus::Fixup::EndOfStepModify::EvaluateQ",
DevExecSpace(), 0, pack.GetDim(5) - 1, jb.s, jb.e + 1, ib.s, ib.e + 1,
KOKKOS_LAMBDA(const int b, const int j, const int i) {
const auto &coords = pack.GetCoords(b);
const Real x1 = coords.Xf<1>(0, j, i);
const Real x2 = coords.Xf<2>(0, j, i);
const Real r = tr.bl_radius(x1);
const Real th = tr.bl_theta(x1, x2);

const Real x = r * sin(th);
const Real z = r * cos(th);
const Real a_hyp = 1.2;
const Real b_hyp = 3. * a_hyp;
const Real x_hyp = a_hyp * sqrt(1. + pow(z / b_hyp, 2.));
const Real q = (pow(x, 2) - pow(x_hyp, 2)) / pow(x_hyp, 2.);
if (x < x_hyp) {
A(b, j, i) = q;
}
});

// Recalculate dphi_dt occasionally
if (last_stage && t >= next_dphi_dt_update_time) {
next_dphi_dt_update_time += enforced_phi_cadence;

// Record mdot
const Real mdot_proc = History::ReduceMassAccretionRate(md);
const Real mdot = reduction::Sum(mdot_proc);

// Measure phi
const Real phi_proc = History::ReduceMagneticFluxPhi(md);
const Real phi = reduction::Sum(phi_proc) / std::sqrt(mdot);
Yurlungur marked this conversation as resolved.
Show resolved Hide resolved

// Calculate amount of phi to add this timestep
const Real dphi = (enforced_phi - phi) * dt / enforced_phi_timescale;
dphi_dt = dphi / dt;

parthenon::par_for(
parthenon::LoopPatternMDRange(),
"Phoebus::Fixup::EndOfStepModify::EvaluateBField", DevExecSpace(), 0,
pack.GetDim(5) - 1, kb.s, kb.e, jb.s, jb.e - 1, ib.s, ib.e - 1,
KOKKOS_LAMBDA(const int b, const int k, const int j, const int i) {
const auto &coords = pack.GetCoords(b);
const Real gammadet = geom.DetGamma(CellLocation::Cent, b, k, j, i);
pack(b, pblo, k, j, i) +=
(-(A(b, j, i) - A(b, j + 1, i) + A(b, j, i + 1) - A(b, j + 1, i + 1)) /
(2.0 * coords.CellWidthFA(X2DIR, k, j, i) * gammadet));
pack(b, pblo + 1, k, j, i) +=
((A(b, j, i) + A(b, j + 1, i) - A(b, j, i + 1) - A(b, j + 1, i + 1)) /
(2.0 * coords.CellWidthFA(X1DIR, k, j, i) * gammadet));

// Used by ReduceMagneticFluxPhi
pack(b, cblo, k, j, i) = pack(b, pblo, k, j, i) * gammadet;
pack(b, cblo + 1, k, j, i) = pack(b, pblo + 1, k, j, i) * gammadet;
});

// Measure change in phi
const Real fiducial_phi_proc = History::ReduceMagneticFluxPhi(md);
const Real fiducial_phi = reduction::Sum(fiducial_phi_proc) / std::sqrt(mdot);
brryan marked this conversation as resolved.
Show resolved Hide resolved
phi_factor = dphi / (fiducial_phi - phi) / dt;

// Remove the fiducial contribution
parthenon::par_for(
parthenon::LoopPatternMDRange(),
"Phoebus::Fixup::EndOfStepModify::EvaluateBField", DevExecSpace(), 0,
pack.GetDim(5) - 1, kb.s, kb.e, jb.s, jb.e - 1, ib.s, ib.e - 1,
KOKKOS_LAMBDA(const int b, const int k, const int j, const int i) {
const auto &coords = pack.GetCoords(b);
const Real gammadet = geom.DetGamma(CellLocation::Cent, b, k, j, i);

pack(b, pblo, k, j, i) -=
(-(A(b, j, i) - A(b, j + 1, i) + A(b, j, i + 1) - A(b, j + 1, i + 1)) /
(2.0 * coords.CellWidthFA(X2DIR, k, j, i) * gammadet));
pack(b, pblo + 1, k, j, i) -=
((A(b, j, i) + A(b, j + 1, i) - A(b, j, i + 1) - A(b, j + 1, i + 1)) /
(2.0 * coords.CellWidthFA(X1DIR, k, j, i) * gammadet));

pack(b, cblo, k, j, i) = pack(b, pblo, k, j, i) * gammadet;
pack(b, cblo + 1, k, j, i) = pack(b, pblo + 1, k, j, i) * gammadet;
});
}

// Properly update B field
parthenon::par_for(
parthenon::LoopPatternMDRange(),
"Phoebus::Fixup::EndOfStepModify::EvaluateBField", DevExecSpace(), 0,
pack.GetDim(5) - 1, kb.s, kb.e, jb.s, jb.e, ib.s, ib.e,
KOKKOS_LAMBDA(const int b, const int k, const int j, const int i) {
const auto &coords = pack.GetCoords(b);
const Real gammadet = geom.DetGamma(CellLocation::Cent, b, k, j, i);

pack(b, pblo, k, j, i) +=
phi_factor * dt *
(-(A(b, j, i) - A(b, j + 1, i) + A(b, j, i + 1) - A(b, j + 1, i + 1)) /
(2.0 * coords.CellWidthFA(X2DIR, k, j, i) * gammadet));
pack(b, pblo + 1, k, j, i) +=
phi_factor * dt *
((A(b, j, i) + A(b, j + 1, i) - A(b, j, i + 1) - A(b, j + 1, i + 1)) /
(2.0 * coords.CellWidthFA(X1DIR, k, j, i) * gammadet));
});
Yurlungur marked this conversation as resolved.
Show resolved Hide resolved
} // enforced_phi_start_time
} // enable_phi_enforcement

return TaskStatus::complete;
}

template <typename T>
TaskStatus ApplyFloors(T *rc) {
auto *pm = rc->GetParentPointer().get();
Expand Down
2 changes: 2 additions & 0 deletions src/fixup/fixup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ template <typename T>
TaskStatus ConservedToPrimitiveFixup(T *rc);
template <typename T>
TaskStatus SourceFixup(T *rc);
TaskStatus EndOfStepModify(MeshData<Real> *, const Real t, const Real dt,
const bool last_stage);

static struct ConstantRhoSieFloor {
} constant_rho_sie_floor_tag;
Expand Down
23 changes: 23 additions & 0 deletions src/phoebus_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ TaskCollection PhoebusDriver::RungeKuttaStage(const int stage) {
BlockList_t &blocks = pmesh->block_list;

const Real beta = integrator->beta[stage - 1];
const Real t = tm.time;
const Real dt = integrator->dt;
const auto &stage_name = integrator->stage_name;

Expand Down Expand Up @@ -351,6 +352,28 @@ TaskCollection PhoebusDriver::RungeKuttaStage(const int stage) {
}
}

// Extra per-step user work
TaskRegion &sync_region_5 = tc.AddRegion(num_partitions);
Yurlungur marked this conversation as resolved.
Show resolved Hide resolved
PARTHENON_REQUIRE(num_partitions == 1,
"Reductions don't work for multiple partitions?");
brryan marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < num_partitions; i++) {
auto &tl = sync_region_5[i];

auto &md = pmesh->mesh_data.GetOrAdd(stage_name[stage], i);

auto end_mods = tl.AddTask(none, fixup::EndOfStepModify, md.get(), t, dt,
stage == integrator->nstages);
}
// This is a bad pattern. Having a per-mesh data p2c would help.
TaskRegion &async_region_4 = tc.AddRegion(num_independent_task_lists);
for (int i = 0; i < blocks.size(); i++) {
auto &pmb = blocks[i];
auto &tl = async_region_4[i];
auto &sc = pmb->meshblock_data.Get(stage_name[stage]);

auto p2c = tl.AddTask(none, fluid::PrimitiveToConserved, sc.get());
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This new set of regions is good motivation for getting the rest of the hydro to be per-meshdata I think


TaskRegion &sync_region_2 = tc.AddRegion(num_partitions);
for (int ib = 0; ib < num_partitions; ib++) {
auto &base = pmesh->mesh_data.GetOrAdd("base", ib);
Expand Down
9 changes: 8 additions & 1 deletion src/phoebus_utils/reduction.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// © 2022. Triad National Security, LLC. All rights reserved. This
// © 2022-2023. Triad National Security, LLC. All rights reserved. This
// program was produced under U.S. Government contract
// 89233218CNA000001 for Los Alamos National Laboratory (LANL), which
// is operated by Triad National Security, LLC for the U.S.
Expand Down Expand Up @@ -32,10 +32,17 @@ Real inline Min(const Real &x) {
return xmin;
}

Real inline Sum(const Real &x) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not actually used now but I think we can leave this in in case the need arises in a problem generator file at some point

Real xsum;
MPI_Allreduce(&x, &xsum, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
return xsum;
}

#else

Real inline Max(const Real &x) { return x; }
Real inline Min(const Real &x) { return x; }
Real inline Sum(const Real &x) { return x; }

#endif // MPI_PARALLEL

Expand Down