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 constant derivative boundary condition and expose GenericBC #884

Merged
merged 143 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
143 commits
Select commit Hold shift + click to select a range
28305f1
Change CellV* to V*
lroberts36 Apr 28, 2023
fda7c62
Make boundary communication fail if non-cell centered variables have …
lroberts36 Apr 28, 2023
cffbbf4
Remove cc
lroberts36 Apr 28, 2023
b1d813c
Rename namespace cell_centered_bvars to var_boundary_comm and fix error
lroberts36 Apr 28, 2023
ec8fb9c
Remove cc from bvals_cc_in_one
lroberts36 Apr 28, 2023
b839cd7
Format and lint
lroberts36 Apr 28, 2023
8cd3df5
Update changelog
lroberts36 Apr 28, 2023
1faa84d
Add multipointer
lroberts36 Apr 28, 2023
51bf023
Allow for arbitrary changes of ParArrayND rank and move all array to …
lroberts36 Apr 28, 2023
65e5722
Fix particle indexing
lroberts36 Apr 28, 2023
dda13a1
Simple sparse pack based interface for face, edge and node variables
lroberts36 Apr 29, 2023
2bc8c93
Format and lint
lroberts36 Apr 29, 2023
f2587f4
Update copyright
lroberts36 Apr 29, 2023
bfa67be
update year
lroberts36 Apr 29, 2023
668ee3c
Merge branch 'lroberts36/rename-cell-variable' into lroberts36/local-…
lroberts36 Apr 29, 2023
92ceec1
Allow for variables w/o topology metadata in packs
lroberts36 Apr 30, 2023
fa67eeb
Allow ParArrayGeneric::Get to take pairs as args
lroberts36 Apr 30, 2023
e812033
Remove extraneous ParArray types
lroberts36 Apr 30, 2023
6bb710d
Forgot to add new header to CMake
lroberts36 Apr 30, 2023
8c544b2
Actually add array to tuple
lroberts36 Apr 30, 2023
d377f13
cleanup, format, and lint
lroberts36 Apr 30, 2023
65f6f1e
Allow for packing other topological types
lroberts36 May 2, 2023
6a33c98
Add small test of packing face variables
lroberts36 May 2, 2023
fc0e14e
format and lint
lroberts36 May 2, 2023
356dc54
Update changelog
lroberts36 May 2, 2023
7f3abc7
try to fix HIP error and small change
lroberts36 May 2, 2023
3ccd277
Act on brryan's comments
lroberts36 May 2, 2023
3933663
Merge branch 'lroberts36/rename-cell-variable' into lroberts36/local-…
lroberts36 May 2, 2023
a73a9f5
Small
lroberts36 May 2, 2023
9723741
Format and lint
lroberts36 May 2, 2023
76b793e
Merge branch 'lroberts36/rename-cell-variable' into lroberts36/local-…
lroberts36 May 2, 2023
c3fb0f0
Update domain functions to deal with varying topological elements
lroberts36 May 2, 2023
91f63c4
Add Indexer class template
lroberts36 May 3, 2023
f59138b
Use Indexer for buffer packing
lroberts36 May 3, 2023
a1420e4
Start rewriting index calculations
lroberts36 May 3, 2023
912d3f6
Remove GetTensor stuff
lroberts36 May 3, 2023
9959160
Comment on chosen numbering
lroberts36 May 3, 2023
85fe2cc
Add some comments
lroberts36 May 3, 2023
ffb0093
Update sparse pack interface to take topological elements
lroberts36 May 3, 2023
568d407
Merge branch 'lroberts36/local-face-fields' into lroberts36/face-boun…
lroberts36 May 3, 2023
595440b
Switch to multiple indexers and format
lroberts36 May 3, 2023
faa9ab9
Remove var_boundary_com namespace
lroberts36 May 3, 2023
6a8930d
Merge branch 'lroberts36/rename-cell-variable' into lroberts36/local-…
lroberts36 May 3, 2023
d729aa7
Merge branch 'lroberts36/local-face-fields' into lroberts36/face-boun…
lroberts36 May 3, 2023
61d939f
Start switching to new indexing
lroberts36 May 3, 2023
91ccaba
working for sparse advection
lroberts36 May 4, 2023
a0c7892
Replace buffer index calculation routines and generalize
lroberts36 May 4, 2023
e55f1b3
In theory should communication should work now for all field types
lroberts36 May 4, 2023
8029db6
Avoid duplication
lroberts36 May 4, 2023
c4896b6
Actually set the number of topological elements for a buffer
lroberts36 May 4, 2023
9d4372a
Update buffer size calculation
lroberts36 May 4, 2023
4e77430
Small
lroberts36 May 4, 2023
3cebd35
Merge branch 'develop' into lroberts36/rename-cell-variable
lroberts36 May 8, 2023
62dd1a4
Merge branch 'lroberts36/rename-cell-variable' into lroberts36/local-…
lroberts36 May 8, 2023
d9e4454
Merge branch 'lroberts36/local-face-fields' into lroberts36/face-boun…
lroberts36 May 8, 2023
700c6ce
Fix out of range error
lroberts36 May 9, 2023
c59a401
Change internal storage so that we don't trigger HtoD
lroberts36 May 9, 2023
235d131
Merge branch 'lroberts36/local-face-fields' into lroberts36/face-boun…
lroberts36 May 9, 2023
6cbff12
Fix bug in assertion
lroberts36 May 9, 2023
e0a4323
Merge branch 'lroberts36/local-face-fields' into lroberts36/face-boun…
lroberts36 May 9, 2023
1fce1b7
Switch to std::array since that seems to give no complaints on device
lroberts36 May 9, 2023
1c44d4f
Remove explicit indices from BndInfo
lroberts36 May 9, 2023
d66d096
Remove defaulted functions
lroberts36 May 10, 2023
9b33f12
Switch to enum for choosing boundaries
lroberts36 May 10, 2023
1827278
Add template option for ghost restriction range
lroberts36 May 10, 2023
9a83310
Switch to separate indexers for prolongation and restriction
lroberts36 May 10, 2023
7825b6e
higher precision error output
lroberts36 May 10, 2023
e96a7fe
Stop using RestrictGhostHalos
lroberts36 May 11, 2023
1ee329e
Remove RestrictGhostHalos
lroberts36 May 11, 2023
e2a2975
Remove BoundaryType::restricted
lroberts36 May 11, 2023
0b207ca
Switch to prolongation in one
lroberts36 May 11, 2023
4ba2256
Format and lint
lroberts36 May 11, 2023
702a122
Remove old boundary prolongation machinery
lroberts36 May 11, 2023
f93512f
A temporary fix for MPI runs
lroberts36 May 11, 2023
7230f51
format and lint
lroberts36 May 11, 2023
3e19cf1
Use indexers directly
lroberts36 May 11, 2023
fbe4b80
Fix device compilation errors
lroberts36 May 15, 2023
9e4c914
Format and lint
lroberts36 May 15, 2023
97249d6
fix typo
lroberts36 May 15, 2023
310e6c3
Add generalized volume function
lroberts36 May 15, 2023
6603fed
Add function for recovering generic central positions
lroberts36 May 15, 2023
ee44018
Generalize restriction
lroberts36 May 15, 2023
849defa
Still working...
lroberts36 May 15, 2023
022ea82
Still passing...
lroberts36 May 15, 2023
48cbdf2
Generalize shared restriction
lroberts36 May 15, 2023
94d55e4
Generalize prolongation and restriction registration to work for all …
lroberts36 May 16, 2023
82e1ecb
Index indexers correctly
lroberts36 May 16, 2023
39627de
add element index
lroberts36 May 16, 2023
0074bc9
Base routine for doing general internal prolongation
lroberts36 May 16, 2023
3efe763
Move submanifold routine and add routines to check if operations are …
lroberts36 May 16, 2023
058fd77
Add OperationRequired checks to tests
lroberts36 May 16, 2023
6580a90
Add all possible cell element types
lroberts36 May 16, 2023
31f5cd3
Remove error causing loop
lroberts36 May 16, 2023
f4f72d2
Only select submanifolds, not the current manifold
lroberts36 May 17, 2023
2dc7b56
Add internal prolongation calls
lroberts36 May 17, 2023
a18303b
format and lint
lroberts36 May 17, 2023
d8d46a2
format and lint...
lroberts36 May 17, 2023
44a840a
Add internal prolongation, part 2
lroberts36 May 17, 2023
35355c4
fix debug issue
lroberts36 May 17, 2023
c3e5343
Add some clarifying comments
lroberts36 May 17, 2023
ee67b95
Switch to using loop indexers required for internal prolongation
lroberts36 May 17, 2023
c5bfcaf
Add some comments and a diagram
lroberts36 May 19, 2023
a7adc71
small
lroberts36 May 19, 2023
22d2d7f
Fix indexing bug when switching to new indexing
lroberts36 May 23, 2023
7dd95c3
Add debug requirement suggested by brryan
lroberts36 May 24, 2023
6a65a7a
fix bug
lroberts36 May 25, 2023
615f259
Move metadata constructor to cpp file
lroberts36 May 25, 2023
044989e
Rename topological elements
lroberts36 May 31, 2023
a75d166
Merge branch 'develop' into lroberts36/local-face-fields
lroberts36 May 31, 2023
b5d3f16
Merge branch 'develop' into lroberts36/local-face-fields
lroberts36 May 31, 2023
8bf40e1
format and lint
lroberts36 May 31, 2023
f987a6a
Merge branch 'lroberts36/local-face-fields' into lroberts36/face-boun…
lroberts36 May 31, 2023
101b074
Rename and catch some bugs
lroberts36 May 31, 2023
12ea9cb
format and lint
lroberts36 May 31, 2023
0a5f661
Only update cell variables
lroberts36 May 31, 2023
d58491b
Remove cell boundary communication checks
lroberts36 May 31, 2023
591fb8b
fix sparse pack bug for face and edge fields
lroberts36 May 31, 2023
3256398
format and lint
lroberts36 May 31, 2023
0be7dd4
format and lint
lroberts36 May 31, 2023
48c075f
Fix indexing bugs
lroberts36 Jun 1, 2023
85b85e2
Fill boundary arrays correctly for face and edge variables
lroberts36 Jun 1, 2023
eb04fa6
Merge branch 'develop' into lroberts36/face-boundary-communication
lroberts36 Jun 1, 2023
2f8c1d8
Remove weird merge duplication
lroberts36 Jun 1, 2023
7de5eb2
format and lint
lroberts36 Jun 1, 2023
dd338e4
Generalize boundary conditions to non-cell centered fields
lroberts36 Jun 1, 2023
874900d
Revert sparse pack with fluxes behavior
lroberts36 Jun 1, 2023
1217b29
Format and lint
lroberts36 Jun 1, 2023
3876ad0
Rename refinement ops
lroberts36 Jun 1, 2023
49ff82b
update changelog
lroberts36 Jun 1, 2023
c9005d3
Initialize size_ to zero
lroberts36 Jun 1, 2023
2cd3adb
respond to comments
lroberts36 Jun 7, 2023
35d292d
Correctly fill vector components
lroberts36 Jun 7, 2023
d6cd8ca
Remove unused dims_
lroberts36 Jun 7, 2023
fbe2fef
Switch to sparse packs for GenericBoundary conditions
lroberts36 Jun 7, 2023
7460f9d
Add constant derivative BC and move GenericBCs to a new header
lroberts36 Jun 7, 2023
6658862
Format and lint
lroberts36 Jun 7, 2023
40d4798
update changelog
lroberts36 Jun 8, 2023
7e40873
Add fixed value boundary condition
lroberts36 Jun 8, 2023
02d0f1e
MeshData boundary conditions
lroberts36 Jun 8, 2023
191b883
Merge branch 'develop' into lroberts36/composable-boundary-conditions
lroberts36 Jun 9, 2023
d86eb74
Merge branch 'develop' into lroberts36/composable-boundary-conditions
lroberts36 Jun 19, 2023
8469e0c
Merge branch 'develop' into lroberts36/composable-boundary-conditions
lroberts36 Jun 20, 2023
e6b8890
Update to new spase pack interface
lroberts36 Jun 20, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Current develop

### Added (new features/APIs/variables/...)
- [[PR 884]](https://github.com/parthenon-hpc-lab/parthenon/pull/884) Add constant derivative BC and expose GenericBC
- [[PR 892]](https://github.com/parthenon-hpc-lab/parthenon/pull/892) Cost-based load balancing and memory diagnostics
- [[PR 889]](https://github.com/parthenon-hpc-lab/parthenon/pull/889) Add PreCommFillDerived
- [[PR 872]](https://github.com/parthenon-hpc-lab/parthenon/pull/872) Boundary communication for non-cell centered fields
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ add_library(parthenon
bvals/comms/tag_map.cpp
bvals/comms/tag_map.hpp

bvals/boundary_conditions_generic.hpp
bvals/boundary_conditions.cpp
bvals/boundary_conditions.hpp

Expand Down
117 changes: 24 additions & 93 deletions src/bvals/boundary_conditions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <vector>

#include "bvals/boundary_conditions.hpp"
#include "bvals/boundary_conditions_generic.hpp"
#include "bvals/bvals_interfaces.hpp"
#include "defs.hpp"
#include "interface/meshblock_data.hpp"
Expand Down Expand Up @@ -52,136 +53,66 @@ TaskStatus ApplyBoundaryConditionsOnCoarseOrFine(std::shared_ptr<MeshBlockData<R

namespace BoundaryFunction {

enum class BCSide { Inner, Outer };
enum class BCType { Outflow, Reflect };

template <CoordinateDirection DIR, BCSide SIDE, BCType TYPE>
void GenericBC(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse,
TopologicalElement el = TopologicalElement::CC) {
// make sure DIR is X[123]DIR so we don't have to check again
static_assert(DIR == X1DIR || DIR == X2DIR || DIR == X3DIR, "DIR must be X[123]DIR");

// convenient shorthands
constexpr bool X1 = (DIR == X1DIR);
constexpr bool X2 = (DIR == X2DIR);
constexpr bool X3 = (DIR == X3DIR);
constexpr bool INNER = (SIDE == BCSide::Inner);
TaskStatus ApplyBoundaryConditionsMD(std::shared_ptr<MeshData<Real>> &pmd) {
for (int b = 0; b < pmd->NumBlocks(); ++b)
ApplyBoundaryConditions(pmd->GetBlockData(b));
return TaskStatus::complete;
}

std::shared_ptr<MeshBlock> pmb = rc->GetBlockPointer();
const auto &bounds = coarse ? pmb->c_cellbounds : pmb->cellbounds;

const auto &range = X1 ? bounds.GetBoundsI(IndexDomain::interior, el)
: (X2 ? bounds.GetBoundsJ(IndexDomain::interior, el)
: bounds.GetBoundsK(IndexDomain::interior, el));
const int ref = INNER ? range.s : range.e;

std::vector<MetadataFlag> flags{Metadata::FillGhost};
if (GetTopologicalType(el) == TopologicalType::Cell) flags.push_back(Metadata::Cell);
if (GetTopologicalType(el) == TopologicalType::Face) flags.push_back(Metadata::Face);
if (GetTopologicalType(el) == TopologicalType::Edge) flags.push_back(Metadata::Edge);
if (GetTopologicalType(el) == TopologicalType::Node) flags.push_back(Metadata::Node);

auto q = rc->PackVariables(flags, coarse);
auto nb = IndexRange{0, q.GetDim(4) - 1};

std::string label = (TYPE == BCType::Reflect ? "Reflect" : "Outflow");
label += (INNER ? "Inner" : "Outer");
label += "X" + std::to_string(DIR);

constexpr IndexDomain domain =
INNER ? (X1 ? IndexDomain::inner_x1
: (X2 ? IndexDomain::inner_x2 : IndexDomain::inner_x3))
: (X1 ? IndexDomain::outer_x1
: (X2 ? IndexDomain::outer_x2 : IndexDomain::outer_x3));

// used for reflections
const int offset = 2 * ref + (INNER ? -1 : 1);

pmb->par_for_bndry(
label, nb, domain, el, coarse,
KOKKOS_LAMBDA(const int &l, const int &k, const int &j, const int &i) {
if (!q.IsAllocated(l)) return;
if (TYPE == BCType::Reflect) {
const bool reflect = (q.VectorComponent(l) == DIR);
q(el, l, k, j, i) =
(reflect ? -1.0 : 1.0) *
q(el, l, X3 ? offset - k : k, X2 ? offset - j : j, X1 ? offset - i : i);
} else {
q(el, l, k, j, i) = q(el, l, X3 ? ref : k, X2 ? ref : j, X1 ? ref : i);
}
});
inline TaskStatus
ApplyBoundaryConditionsOnCoarseOrFineMD(std::shared_ptr<MeshData<Real>> &pmd,
bool coarse) {
for (int b = 0; b < pmd->NumBlocks(); ++b)
ApplyBoundaryConditionsOnCoarseOrFine(pmd->GetBlockData(b), coarse);
return TaskStatus::complete;
}

void OutflowInnerX1(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse) {
using TE = TopologicalElement;
for (auto el : {TE::CC, TE::F1, TE::F2, TE::F3, TE::E1, TE::E2, TE::E3, TE::NN})
GenericBC<X1DIR, BCSide::Inner, BCType::Outflow>(rc, coarse, el);
GenericBC<X1DIR, BCSide::Inner, BCType::Outflow, variable_names::any>(rc, coarse);
}

void OutflowOuterX1(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse) {
using TE = TopologicalElement;
for (auto el : {TE::CC, TE::F1, TE::F2, TE::F3, TE::E1, TE::E2, TE::E3, TE::NN})
GenericBC<X1DIR, BCSide::Outer, BCType::Outflow>(rc, coarse, el);
GenericBC<X1DIR, BCSide::Outer, BCType::Outflow, variable_names::any>(rc, coarse);
}

void OutflowInnerX2(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse) {
using TE = TopologicalElement;
for (auto el : {TE::CC, TE::F1, TE::F2, TE::F3, TE::E1, TE::E2, TE::E3, TE::NN})
GenericBC<X2DIR, BCSide::Inner, BCType::Outflow>(rc, coarse, el);
GenericBC<X2DIR, BCSide::Inner, BCType::Outflow, variable_names::any>(rc, coarse);
}

void OutflowOuterX2(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse) {
using TE = TopologicalElement;
for (auto el : {TE::CC, TE::F1, TE::F2, TE::F3, TE::E1, TE::E2, TE::E3, TE::NN})
GenericBC<X2DIR, BCSide::Outer, BCType::Outflow>(rc, coarse, el);
GenericBC<X2DIR, BCSide::Outer, BCType::Outflow, variable_names::any>(rc, coarse);
}

void OutflowInnerX3(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse) {
using TE = TopologicalElement;
for (auto el : {TE::CC, TE::F1, TE::F2, TE::F3, TE::E1, TE::E2, TE::E3, TE::NN})
GenericBC<X3DIR, BCSide::Inner, BCType::Outflow>(rc, coarse, el);
GenericBC<X3DIR, BCSide::Inner, BCType::Outflow, variable_names::any>(rc, coarse);
}

void OutflowOuterX3(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse) {
using TE = TopologicalElement;
for (auto el : {TE::CC, TE::F1, TE::F2, TE::F3, TE::E1, TE::E2, TE::E3, TE::NN})
GenericBC<X3DIR, BCSide::Outer, BCType::Outflow>(rc, coarse, el);
GenericBC<X3DIR, BCSide::Outer, BCType::Outflow, variable_names::any>(rc, coarse);
}

void ReflectInnerX1(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse) {
using TE = TopologicalElement;
for (auto el : {TE::CC, TE::F1, TE::F2, TE::F3, TE::E1, TE::E2, TE::E3, TE::NN})
GenericBC<X1DIR, BCSide::Inner, BCType::Reflect>(rc, coarse, el);
GenericBC<X1DIR, BCSide::Inner, BCType::Reflect, variable_names::any>(rc, coarse);
}

void ReflectOuterX1(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse) {
using TE = TopologicalElement;
for (auto el : {TE::CC, TE::F1, TE::F2, TE::F3, TE::E1, TE::E2, TE::E3, TE::NN})
GenericBC<X1DIR, BCSide::Outer, BCType::Reflect>(rc, coarse, el);
GenericBC<X1DIR, BCSide::Outer, BCType::Reflect, variable_names::any>(rc, coarse);
}

void ReflectInnerX2(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse) {
using TE = TopologicalElement;
for (auto el : {TE::CC, TE::F1, TE::F2, TE::F3, TE::E1, TE::E2, TE::E3, TE::NN})
GenericBC<X2DIR, BCSide::Inner, BCType::Reflect>(rc, coarse, el);
GenericBC<X2DIR, BCSide::Inner, BCType::Reflect, variable_names::any>(rc, coarse);
}

void ReflectOuterX2(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse) {
using TE = TopologicalElement;
for (auto el : {TE::CC, TE::F1, TE::F2, TE::F3, TE::E1, TE::E2, TE::E3, TE::NN})
GenericBC<X2DIR, BCSide::Outer, BCType::Reflect>(rc, coarse, el);
GenericBC<X2DIR, BCSide::Outer, BCType::Reflect, variable_names::any>(rc, coarse);
}

void ReflectInnerX3(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse) {
using TE = TopologicalElement;
for (auto el : {TE::CC, TE::F1, TE::F2, TE::F3, TE::E1, TE::E2, TE::E3, TE::NN})
GenericBC<X3DIR, BCSide::Inner, BCType::Reflect>(rc, coarse, el);
GenericBC<X3DIR, BCSide::Inner, BCType::Reflect, variable_names::any>(rc, coarse);
}

void ReflectOuterX3(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse) {
using TE = TopologicalElement;
for (auto el : {TE::CC, TE::F1, TE::F2, TE::F3, TE::E1, TE::E2, TE::E3, TE::NN})
GenericBC<X3DIR, BCSide::Outer, BCType::Reflect>(rc, coarse, el);
GenericBC<X3DIR, BCSide::Outer, BCType::Reflect, variable_names::any>(rc, coarse);
}

} // namespace BoundaryFunction
Expand Down
5 changes: 5 additions & 0 deletions src/bvals/boundary_conditions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ inline TaskStatus ApplyBoundaryConditions(std::shared_ptr<MeshBlockData<Real>> &
return ApplyBoundaryConditionsOnCoarseOrFine(rc, false);
}

TaskStatus ApplyBoundaryConditionsMD(std::shared_ptr<MeshData<Real>> &pmd);

TaskStatus ApplyBoundaryConditionsOnCoarseOrFineMD(std::shared_ptr<MeshData<Real>> &pmd,
bool coarse);

namespace BoundaryFunction {

void OutflowInnerX1(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse);
Expand Down
130 changes: 130 additions & 0 deletions src/bvals/boundary_conditions_generic.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//========================================================================================
// (C) (or copyright) 2020-2022. 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. Department of Energy/National Nuclear Security Administration. All rights
// in the program are reserved by Triad National Security, LLC, and the U.S. Department
// of Energy/National Nuclear Security Administration. The Government is granted for
// itself and others acting on its behalf a nonexclusive, paid-up, irrevocable worldwide
// license in this material to reproduce, prepare derivative works, distribute copies to
// the public, perform publicly and display publicly, and to permit others to do so.
//========================================================================================

#ifndef BVALS_BOUNDARY_CONDITIONS_GENERIC_HPP_
#define BVALS_BOUNDARY_CONDITIONS_GENERIC_HPP_

#include <functional>
#include <memory>
#include <set>
#include <string>
#include <vector>

#include "basic_types.hpp"
#include "interface/meshblock_data.hpp"
#include "interface/sparse_pack.hpp"
#include "mesh/domain.hpp"
#include "mesh/mesh.hpp"
#include "mesh/meshblock.hpp"

namespace parthenon {
namespace BoundaryFunction {

enum class BCSide { Inner, Outer };
enum class BCType { Outflow, Reflect, ConstantDeriv, Fixed };

template <CoordinateDirection DIR, BCSide SIDE, BCType TYPE, class... var_ts>
void GenericBC(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse,
TopologicalElement el, Real val) {
// make sure DIR is X[123]DIR so we don't have to check again
static_assert(DIR == X1DIR || DIR == X2DIR || DIR == X3DIR, "DIR must be X[123]DIR");

// convenient shorthands
constexpr bool X1 = (DIR == X1DIR);
constexpr bool X2 = (DIR == X2DIR);
constexpr bool X3 = (DIR == X3DIR);
constexpr bool INNER = (SIDE == BCSide::Inner);

std::vector<MetadataFlag> flags{Metadata::FillGhost};
if (GetTopologicalType(el) == TopologicalType::Cell) flags.push_back(Metadata::Cell);
if (GetTopologicalType(el) == TopologicalType::Face) flags.push_back(Metadata::Face);
if (GetTopologicalType(el) == TopologicalType::Edge) flags.push_back(Metadata::Edge);
if (GetTopologicalType(el) == TopologicalType::Node) flags.push_back(Metadata::Node);

std::set<PDOpt> opts;
if (coarse) opts = {PDOpt::Coarse};
auto desc = MakePackDescriptor<var_ts...>(
rc->GetBlockPointer()->pmy_mesh->resolved_packages.get(), flags, opts);
auto q = desc.GetPack(rc.get());
const int b = 0;
const int lstart = q.GetLowerBoundHost(b);
const int lend = q.GetUpperBoundHost(b);
if (lend < lstart) return;
auto nb = IndexRange{lstart, lend};

std::shared_ptr<MeshBlock> pmb = rc->GetBlockPointer();
const auto &bounds = coarse ? pmb->c_cellbounds : pmb->cellbounds;

const auto &range = X1 ? bounds.GetBoundsI(IndexDomain::interior, el)
: (X2 ? bounds.GetBoundsJ(IndexDomain::interior, el)
: bounds.GetBoundsK(IndexDomain::interior, el));
const int ref = INNER ? range.s : range.e;

std::string label = (TYPE == BCType::Reflect ? "Reflect" : "Outflow");
label += (INNER ? "Inner" : "Outer");
label += "X" + std::to_string(DIR);

constexpr IndexDomain domain =
INNER ? (X1 ? IndexDomain::inner_x1
: (X2 ? IndexDomain::inner_x2 : IndexDomain::inner_x3))
: (X1 ? IndexDomain::outer_x1
: (X2 ? IndexDomain::outer_x2 : IndexDomain::outer_x3));

// used for reflections
const int offset = 2 * ref + (INNER ? -1 : 1);

// used for derivatives
const int offsetin = INNER;
const int offsetout = !INNER;
pmb->par_for_bndry(
label, nb, domain, el, coarse,
KOKKOS_LAMBDA(const int &l, const int &k, const int &j, const int &i) {
if (TYPE == BCType::Reflect) {
lroberts36 marked this conversation as resolved.
Show resolved Hide resolved
const bool reflect = (q(b, el, l).vector_component == DIR);
q(b, el, l, k, j, i) =
(reflect ? -1.0 : 1.0) *
q(b, el, l, X3 ? offset - k : k, X2 ? offset - j : j, X1 ? offset - i : i);
} else if (TYPE == BCType::ConstantDeriv) {
Real dq = q(b, el, l, X3 ? ref + offsetin : k, X2 ? ref + offsetin : j,
X1 ? ref + offsetin : i) -
q(b, el, l, X3 ? ref - offsetout : k, X2 ? ref - offsetout : j,
X1 ? ref - offsetout : i);
Real delta = 0.0;
if (X1) {
delta = i - ref;
} else if (X2) {
delta = j - ref;
} else {
delta = k - ref;
}
q(b, el, l, k, j, i) =
q(b, el, l, X3 ? ref : k, X2 ? ref : j, X1 ? ref : i) + delta * dq;
} else if (TYPE == BCType::Fixed) {
q(b, el, l, k, j, i) = val;
} else {
q(b, el, l, k, j, i) = q(b, el, l, X3 ? ref : k, X2 ? ref : j, X1 ? ref : i);
}
});
}

template <CoordinateDirection DIR, BCSide SIDE, BCType TYPE, class... var_ts>
void GenericBC(std::shared_ptr<MeshBlockData<Real>> &rc, bool coarse, Real val = 0.0) {
using TE = TopologicalElement;
for (auto el : {TE::CC, TE::F1, TE::F2, TE::F3, TE::E1, TE::E2, TE::E3, TE::NN})
GenericBC<DIR, SIDE, TYPE, var_ts...>(rc, coarse, el, val);
}

} // namespace BoundaryFunction
} // namespace parthenon

#endif // BVALS_BOUNDARY_CONDITIONS_GENERIC_HPP_
14 changes: 8 additions & 6 deletions src/interface/sparse_pack_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,21 @@ SparsePackBase SparsePackBase::Build(T *pmd, const PackDescriptor &desc) {
pack_h(1, b, idx) = pv->data.Get(1, t, u, v);
pack_h(2, b, idx) = pv->data.Get(2, t, u, v);
}
if (pv->IsSet(Metadata::Vector)) {
pack_h(0, b, idx).vector_component = X1DIR;
pack_h(1, b, idx).vector_component = X2DIR;
pack_h(2, b, idx).vector_component = X3DIR;
}
} else { // This is a cell, node, or a variable that doesn't have
// topology information
if (pack.coarse_) {
pack_h(0, b, idx) = pv->coarse_s.Get(0, t, u, v);
} else {
pack_h(0, b, idx) = pv->data.Get(0, t, u, v);
}
if (pv->IsSet(Metadata::Vector))
pack_h(0, b, idx).vector_component = v + 1;

if (desc.with_fluxes && pv->IsSet(Metadata::WithFluxes)) {
pack_h(1, b, idx) = pv->flux[X1DIR].Get(0, t, u, v);
pack_h(2, b, idx) = pv->flux[X2DIR].Get(0, t, u, v);
Expand Down Expand Up @@ -266,15 +274,9 @@ SparsePackBase SparsePackBase::Build(T *pmd, const PackDescriptor &desc) {
// Record the maximum for easy access
pack.bounds_h_(1, block, nvar) = idx - 1;
});

Kokkos::deep_copy(pack.pack_, pack_h);
Kokkos::deep_copy(pack.bounds_, pack.bounds_h_);
Kokkos::deep_copy(pack.coords_, coords_h);
pack.dims_[1] = pack.nblocks_;
pack.dims_[2] = -1; // Not allowed to ask for the ragged dimension anyway
pack.dims_[3] = pack_h(0, 0, 0).extent_int(0);
pack.dims_[4] = pack_h(0, 0, 0).extent_int(2);
pack.dims_[5] = pack_h(0, 0, 0).extent_int(3);

return pack;
}
Expand Down
1 change: 0 additions & 1 deletion src/interface/sparse_pack_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ class SparsePackBase {
bool coarse_;
bool flat_;
int nblocks_;
int dims_[6];
int nvar_;
int size_;
};
Expand Down