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

WIP tracers #102

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4d0e643
Add Phoebus tracers
pgrete Mar 4, 2024
4c5a777
Fix/adapt tracers to AthenaPK
pgrete Mar 4, 2024
b993570
Add tracer support to turbulence pgen
pgrete Mar 4, 2024
1465cd4
Fix swarm interface
pgrete Mar 6, 2024
db3c876
Fix particle init
pgrete Mar 6, 2024
13505df
Call FillTracer every cycle
pgrete Mar 6, 2024
0e86320
Added brief instructions for new particle field
pgrete Mar 6, 2024
4973d8d
Update tracers.cpp
mbruggen Mar 6, 2024
7c57381
Simplify/dedup code
pgrete Mar 7, 2024
46b48e1
Add lookback time
pgrete Mar 7, 2024
ddc5c78
Update turbulence.cpp
evan1022 Mar 8, 2024
94c1eca
Update turbulence.cpp
evan1022 Mar 8, 2024
b81eb66
Update turbulence.cpp
mbruggen Mar 8, 2024
7be5d87
Update tracers.cpp
mbruggen Mar 8, 2024
bc39143
Update turbulence.cpp
mbruggen Mar 8, 2024
582dc2a
Update turbulence.cpp
mbruggen Mar 8, 2024
0a1a76e
Use MeshData in FillTracers to allow reduction
pgrete Mar 11, 2024
62b759a
Update FillTracers internals to loop over blocks logic
pgrete Mar 11, 2024
78ac98a
Calc correlations
pgrete Mar 11, 2024
b1ee6c0
Dump corr to file
pgrete Mar 11, 2024
4eeffab
Import interpolation from Phoebus
pgrete Mar 11, 2024
59dbdfa
Adjust interface
pgrete Mar 11, 2024
71c2cd4
User interpolation for FillTracers
pgrete Mar 11, 2024
6bb07d7
Use 2nd order time integrator for particles
pgrete Mar 11, 2024
d2dba04
Update tracers.cpp
evan1022 Mar 11, 2024
f5707cf
Merge remote-tracking branch 'origin/evan1022/tracers' into pgrete/tr…
pgrete Mar 12, 2024
60e8bc1
Fix csv header
pgrete Mar 12, 2024
79ddc4f
Add s and sdot to csv output
pgrete Mar 13, 2024
b4e65f7
Merge branch 'main' into pgrete/tracer
pgrete Sep 9, 2024
4e4581a
Fix interface
pgrete Jul 18, 2024
14976c1
Use upstream interfaces
pgrete Sep 9, 2024
da769ad
Fix swarm interface
pgrete Sep 9, 2024
c446ec4
Merge branch 'main' into pgrete/tracer
pgrete Sep 26, 2024
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
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ add_executable(
hydro/srcterms/tabular_cooling.cpp
refinement/gradient.cpp
refinement/other.cpp
tracers/tracers.cpp
tracers/tracers.hpp
utils/few_modes_ft.cpp
utils/few_modes_ft.hpp
)

add_subdirectory(pgen)
Expand Down
2 changes: 2 additions & 0 deletions src/hydro/hydro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "../recon/weno3_simple.hpp"
#include "../recon/wenoz_simple.hpp"
#include "../refinement/refinement.hpp"
#include "../tracers/tracers.hpp"
#include "../units.hpp"
#include "defs.hpp"
#include "diffusion/diffusion.hpp"
Expand Down Expand Up @@ -52,6 +53,7 @@ using parthenon::HistoryOutputVar;
parthenon::Packages_t ProcessPackages(std::unique_ptr<ParameterInput> &pin) {
parthenon::Packages_t packages;
packages.Add(Hydro::Initialize(pin.get()));
packages.Add(Tracers::Initialize(pin.get()));
return packages;
}

Expand Down
47 changes: 46 additions & 1 deletion src/hydro/hydro_driver.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//========================================================================================
// AthenaPK - a performance portable block structured AMR astrophysical MHD code.
// Copyright (c) 2020-2023, Athena-Parthenon Collaboration. All rights reserved.
// Copyright (c) 2020-2024, Athena-Parthenon Collaboration. All rights reserved.
// Licensed under the BSD 3-Clause License (the "LICENSE").
//========================================================================================

Expand All @@ -12,13 +12,16 @@

// Parthenon headers
#include "amr_criteria/refinement_package.hpp"
#include "basic_types.hpp"
#include "bvals/comms/bvals_in_one.hpp"
#include "prolong_restrict/prolong_restrict.hpp"
#include "utils/error_checking.hpp"
#include <parthenon/parthenon.hpp>
// AthenaPK headers
#include "../eos/adiabatic_hydro.hpp"
#include "../pgen/cluster/agn_triggering.hpp"
#include "../pgen/cluster/magnetic_tower.hpp"
#include "../tracers/tracers.hpp"
#include "diffusion/diffusion.hpp"
#include "glmmhd/glmmhd.hpp"
#include "hydro.hpp"
Expand Down Expand Up @@ -694,6 +697,48 @@ TaskCollection HydroDriver::MakeTaskCollection(BlockList_t &blocks, int stage) {
}
}

auto tracers_pkg = pmesh->packages.Get("tracers");
// First order operator split tracer advection
if (stage == integrator->nstages && tracers_pkg->Param<bool>("enabled")) {
const std::string swarm_name = "tracers";
TaskRegion &sync_region_tr = tc.AddRegion(1);
{
for (auto &pmb : blocks) {
auto &tl = sync_region_tr[0];
auto &sd = pmb->meshblock_data.Get()->GetSwarmData();
auto reset_comms =
tl.AddTask(none, &SwarmContainer::ResetCommunication, sd.get());
}
}

TaskRegion &async_region_tr = tc.AddRegion(blocks.size());
for (int n = 0; n < blocks.size(); n++) {
auto &tl = async_region_tr[n];
auto &pmb = blocks[n];
auto &sd = pmb->meshblock_data.Get()->GetSwarmData();
auto &mbd0 = pmb->meshblock_data.Get("base");
auto tracer_advect =
tl.AddTask(none, Tracers::AdvectTracers, mbd0.get(), integrator->dt);

auto send = tl.AddTask(tracer_advect, &SwarmContainer::Send, sd.get(),
BoundaryCommSubset::all);

auto receive =
tl.AddTask(send, &SwarmContainer::Receive, sd.get(), BoundaryCommSubset::all);
}
// TODO(pgrete) Fix/cleanup once we got swarm packs.
// We need just a single region with a single task in order to be able to use plain
// reductions.
PARTHENON_REQUIRE_THROWS(num_partitions == 1,
"Only pack_size=-1 currently supported for tracers.")
TaskRegion &single_tasklist_per_pack_region_4 = tc.AddRegion(num_partitions);
for (int i = 0; i < num_partitions; i++) {
auto &tl = single_tasklist_per_pack_region_4[i];
auto &mu0 = pmesh->mesh_data.GetOrAdd("base", i);
auto fill = tl.AddTask(none, Tracers::FillTracers, mu0.get(), tm);
}
}

if (stage == integrator->nstages && pmesh->adaptive) {
TaskRegion &async_region_4 = tc.AddRegion(num_task_lists_executed_independently);
for (int i = 0; i < blocks.size(); i++) {
Expand Down
59 changes: 58 additions & 1 deletion src/pgen/turbulence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

// AthenaPK headers
#include "../main.hpp"
#include "../tracers/tracers.hpp"
#include "../units.hpp"
#include "../utils/few_modes_ft.hpp"

Expand Down Expand Up @@ -333,6 +334,63 @@ void ProblemGenerator(Mesh *pmesh, ParameterInput *pin, MeshData<Real> *md) {
0.5 * (SQR(u(IB1, k, j, i)) + SQR(u(IB2, k, j, i)) + SQR(u(IB3, k, j, i)));
}
});

/* tracer init section */
auto tracer_pkg = pmb->packages.Get("tracers");
if (tracer_pkg->Param<bool>("enabled")) {

for (int b = 0; b < md->NumBlocks(); b++) {
pmb = md->GetBlockData(b)->GetBlockPointer();
auto &swarm = pmb->meshblock_data.Get()->GetSwarmData()->Get("tracers");
auto rng_pool = tracer_pkg->Param<RNGPool>("rng_pool");

const Real &x_min = pmb->coords.Xf<1>(ib.s);
const Real &y_min = pmb->coords.Xf<2>(jb.s);
const Real &z_min = pmb->coords.Xf<3>(kb.s);
const Real &x_max = pmb->coords.Xf<1>(ib.e + 1);
const Real &y_max = pmb->coords.Xf<2>(jb.e + 1);
const Real &z_max = pmb->coords.Xf<3>(kb.e + 1);

// as for num_tracers on each block... will get too many on multiple blocks
// TODO: distribute amongst blocks.
const auto num_tracers_per_cell = tracer_pkg->Param<Real>("num_tracers_per_cell");
const auto num_tracers_per_block =
static_cast<int>(pmb->GetNumberOfMeshBlockCells() * num_tracers_per_cell);

// Create new particles and get accessor
auto new_particles_context = swarm->AddEmptyParticles(num_tracers_per_block);

auto &x = swarm->Get<Real>(swarm_position::x::name()).Get();
auto &y = swarm->Get<Real>(swarm_position::y::name()).Get();
auto &z = swarm->Get<Real>(swarm_position::z::name()).Get();
auto &id = swarm->Get<int>("id").Get();

auto swarm_d = swarm->GetDeviceContext();

const int gid = pmb->gid;
pmb->par_for(
"ProblemGenerator::Turbulence::DistributeTracers", 0,
new_particles_context.GetNewParticlesMaxIndex(),
KOKKOS_LAMBDA(const int new_n) {
auto rng_gen = rng_pool.get_state();
const int n = new_particles_context.GetNewParticleIndex(new_n);

x(n) = x_min + rng_gen.drand() * (x_max - x_min);
y(n) = y_min + rng_gen.drand() * (y_max - y_min);
z(n) = z_min + rng_gen.drand() * (z_max - z_min);
// Note that his works only during one time init.
// If (somehwere else) we eventually add dynamic particles, then we need to
// manage ids (not indices) more globally.
id(n) = num_tracers_per_block * gid + n;

rng_pool.free_state(rng_gen);

// TODO(pgrete) check if this actually required
bool on_current_mesh_block = true;
swarm_d.GetNeighborBlockIndex(n, x(n), y(n), z(n), on_current_mesh_block);
});
}
}
}

//----------------------------------------------------------------------------------------
Expand Down Expand Up @@ -478,5 +536,4 @@ void UserWorkBeforeOutput(MeshBlock *pmb, ParameterInput *pin,
auto state_dist = few_modes_ft.GetDistState();
pin->SetString("problem/turbulence", "state_dist", state_dist);
}

} // namespace turbulence
Loading
Loading