Skip to content

Commit 51f8cb3

Browse files
WeiqunZhangax3l
andauthored
Start EB (#504)
This is still work in progress. You can build EB and create EBFArrayBoxFactory objects from python. --------- Signed-off-by: Axel Huebl <axel.huebl@plasma.ninja> Co-authored-by: Axel Huebl <axel.huebl@plasma.ninja>
1 parent ad20d38 commit 51f8cb3

File tree

9 files changed

+167
-1
lines changed

9 files changed

+167
-1
lines changed

docs/source/usage/api.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,19 @@ This is for the legacy, AoS + SoA particle containers only:
280280
.. autoclass:: amrex.space3d.Particle_2_1
281281
:members:
282282
:undoc-members:
283+
284+
.. _usage-api-eb:
285+
286+
Embedded Boundaries
287+
-------------------
288+
289+
Embedded boundary (EB) support in pyAMReX is still minimal. To build pyAMReX with
290+
EB support, you need to add ``-DAMReX_EB=ON`` to CMake build options.
291+
292+
.. autofunction:: amrex.space3d.EB2_Build
293+
294+
.. autoclass:: amrex.space3d.EBFArrayBoxFactory
295+
:members:
296+
:undoc-members:
297+
298+
.. autofunction:: amrex.space3d.makeEBFabFactory

pyAMReXConfig.cmake.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ set(pyAMReX_CUDA @AMReX_CUDA@)
2626
set(pyAMReX_SYCL @AMReX_SYCL@)
2727
set(pyAMReX_HIP @AMReX_HIP@)
2828
set(pyAMReX_GPU_BACKEND @AMReX_GPU_BACKEND@)
29+
set(pyAMReX_EB @AMReX_EB@)
2930

3031
# define central pyAMReX::pyAMReX_${D}d targets
3132
include("${CMAKE_CURRENT_LIST_DIR}/pyAMReXTargets.cmake")

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def build_extension(self, ext):
9797
"-DAMReX_MPI:BOOL=" + AMReX_MPI,
9898
"-DAMReX_PRECISION=" + AMReX_PRECISION,
9999
"-DAMReX_PARTICLES_PRECISION=" + AMReX_PARTICLES_PRECISION,
100+
"-DAMReX_EB:BOOL=" + AMReX_EB,
100101
"-DpyAMReX_CCACHE=" + PYAMREX_CCACHE,
101102
"-DpyAMReX_IPO=" + PYAMREX_IPO,
102103
## dependency control (developers & package managers)
@@ -176,6 +177,7 @@ def build_extension(self, ext):
176177
AMReX_MPI = os.environ.get("AMREX_MPI", "OFF")
177178
AMReX_PRECISION = os.environ.get("AMREX_PRECISION", "DOUBLE")
178179
AMReX_PARTICLES_PRECISION = os.environ.get("AMREX_PARTICLES_PRECISION", "DOUBLE")
180+
AMReX_EB = os.environ.get("AMREX_EB", "OFF")
179181
# single value or as a list 1;2;3
180182
AMReX_SPACEDIM = os.environ.get("AMREX_SPACEDIM", "1;2;3")
181183
BUILD_SHARED_LIBS = os.environ.get("AMREX_BUILD_SHARED_LIBS", "OFF")

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
add_subdirectory(AmrCore)
33
add_subdirectory(Base)
44
#add_subdirectory(Boundary)
5-
#add_subdirectory(EB)
5+
add_subdirectory(EB)
66
#add_subdirectory(Extern)
77
#add_subdirectory(LinearSolvers)
88
add_subdirectory(Particle)

src/EB/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
if (NOT AMReX_EB)
2+
return()
3+
endif()
4+
5+
foreach(D IN LISTS AMReX_SPACEDIM)
6+
if (D GREATER 1)
7+
target_sources(pyAMReX_${D}d
8+
PRIVATE
9+
EB.cpp
10+
EBFabFactory.cpp
11+
)
12+
endif()
13+
endforeach()

src/EB/EB.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* Copyright 2022 The AMReX Community
2+
*
3+
* Authors: Weiqun Zhang, Axel Huebl
4+
* License: BSD-3-Clause-LBNL
5+
*/
6+
#include "pyAMReX.H"
7+
8+
#include <AMReX_EB2.H>
9+
10+
11+
void init_EBFabFactory (py::module& m);
12+
13+
void init_EB (py::module& m)
14+
{
15+
using namespace amrex;
16+
17+
m.def(
18+
"EB2_Build",
19+
[] (Geometry const& geom, int required_coarsening_level, int max_coarsening_level,
20+
int ngrow, bool build_coarse_level_by_coarsening, bool extend_domain_face,
21+
int num_coarsen_opt)
22+
{
23+
EB2::Build(geom, required_coarsening_level, max_coarsening_level, ngrow,
24+
build_coarse_level_by_coarsening, extend_domain_face, num_coarsen_opt);
25+
},
26+
py::arg("geom"), py::arg("required_coarsening_level"), py::arg("max_coarsening_level"),
27+
py::arg("ngrow") = 4, py::arg("build_coarse_level_by_coarsening") = true,
28+
py::arg("extend_domain_face") = EB2::ExtendDomainFace(),
29+
py::arg("num_coarsen_opt") = EB2::NumCoarsenOpt(),
30+
"EB generation"
31+
);
32+
33+
init_EBFabFactory(m);
34+
}

src/EB/EBFabFactory.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Copyright 2022 The AMReX Community
2+
*
3+
* Authors: Weiqun Zhang, Axel Huebl
4+
* License: BSD-3-Clause-LBNL
5+
*/
6+
#include "pyAMReX.H"
7+
8+
#include <AMReX_EBFabFactory.H>
9+
#include <AMReX_MultiFab.H>
10+
11+
12+
void init_EBFabFactory (py::module& m)
13+
{
14+
using namespace amrex;
15+
16+
py::class_<EBFArrayBoxFactory, FabFactory<FArrayBox>>(m, "EBFArrayBoxFactory")
17+
.def("getVolFrac", &EBFArrayBoxFactory::getVolFrac,
18+
py::return_value_policy::reference_internal,
19+
"Return volume faction MultiFab");
20+
21+
py::native_enum<EBSupport>(m, "EBSupport", "enum.Enum")
22+
.value("basic", EBSupport::basic)
23+
.value("volume", EBSupport::volume)
24+
.value("full", EBSupport::full)
25+
.export_values()
26+
.finalize()
27+
;
28+
29+
m.def(
30+
"makeEBFabFactory",
31+
[] (Geometry const& geom, BoxArray const& ba, DistributionMapping const& dm,
32+
Vector<int> const& ngrow, EBSupport support)
33+
{
34+
return makeEBFabFactory(geom, ba, dm, ngrow, support);
35+
},
36+
py::arg("geom"), py::arg("ba"), py::arg("dm"), py::arg("ngrow"),
37+
py::arg("support"),
38+
"Make EBFArrayBoxFactory for given Geometry, BoxArray and DistributionMapping"
39+
);
40+
}

src/pyAMReX.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ void init_Utility(py::module &);
4848
void init_Vector(py::module &);
4949
void init_Version(py::module &);
5050
void init_VisMF(py::module &);
51+
#ifdef AMREX_USE_EB
52+
void init_EB(py::module &);
53+
#endif
5154

5255
#if AMREX_SPACEDIM == 1
5356
PYBIND11_MODULE(amrex_1d_pybind, m) {
@@ -139,6 +142,10 @@ PYBIND11_MODULE(amrex_3d_pybind, m) {
139142
init_Version(m);
140143
init_VisMF(m);
141144

145+
#ifdef AMREX_USE_EB
146+
init_EB(m);
147+
#endif
148+
142149
// authors
143150
m.attr("__author__") =
144151
"Axel Huebl, Ryan T. Sandberg, Shreyas Ananthan, David P. Grote, "

tests/test_eb.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import numpy
4+
import pytest
5+
6+
import amrex.space3d as amr
7+
8+
9+
@pytest.mark.skipif(not amr.Config.have_eb, reason="Requires -DAMReX_EB=ON")
10+
def test_makeEBFabFactory():
11+
n_cell = 64
12+
max_grid_size = 16
13+
14+
# Build Geometry
15+
domain = amr.Box(
16+
amr.IntVect(0, 0, 0), amr.IntVect(n_cell - 1, n_cell - 1, n_cell - 1)
17+
)
18+
real_box = amr.RealBox([0.0, 0.0, 0.0], [1.0, 1.0, 1.0])
19+
coord = 0 # Cartesian
20+
is_per = [1, 1, 1] # is periodic?
21+
geom = amr.Geometry(domain, real_box, coord, is_per)
22+
23+
# EB parameters
24+
pp = amr.ParmParse("eb2")
25+
pp.add("geom_type", "sphere")
26+
pp.addarr("sphere_center", [0.5, 0.5, 0.5])
27+
rsphere = 0.25
28+
pp.add("sphere_radius", rsphere)
29+
pp.add("sphere_has_fluid_inside", 1)
30+
31+
# EB generation
32+
eb_requried_level = 0
33+
eb_max_level = 2
34+
amr.EB2_Build(geom, eb_requried_level, eb_max_level)
35+
36+
# Build BoxArray
37+
ba = amr.BoxArray(domain)
38+
ba.max_size(max_grid_size)
39+
40+
# Build DistributionMapping
41+
dm = amr.DistributionMapping(ba)
42+
43+
# Make EB Factory
44+
ng = amr.Vector_int([1, 1, 1])
45+
factory = amr.makeEBFabFactory(geom, ba, dm, ng, amr.EBSupport.full)
46+
47+
# Get EB data
48+
vfrac = factory.getVolFrac()
49+
50+
dx = geom.data().CellSize()
51+
total_vol = vfrac.sum() * dx[0] * dx[1] * dx[2]
52+
sphere_vol = 4.0 / 3.0 * numpy.pi * rsphere**3
53+
assert abs(sphere_vol - total_vol) / sphere_vol < 2.0e-3

0 commit comments

Comments
 (0)