-
Notifications
You must be signed in to change notification settings - Fork 92
MultiFab tutorial with pyamrex #95
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ca87b6b
MultiFab tutorial with pyamrex
EZoni e61d360
Replace `for` loop with NumPy operations
EZoni 193c499
Print only from IO process, write plotfile
EZoni e72e171
Two methods to fill array (NumPy, Array4)
EZoni 7c5010e
Finalize TODOs
ax3l 3ff1f80
Docs: Python
ax3l 121be95
Python: Shared Libs
ax3l 545561e
CI: Python
ax3l bc2a906
CUDA/HIP Support and Transpose Arrays
ax3l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,17 +16,21 @@ jobs: | |
| - name: Build & Install | ||
| run: | | ||
| cd ExampleCodes | ||
| mkdir build | ||
| cd build | ||
| cmake .. \ | ||
| cmake -S . -B build \ | ||
| -DCMAKE_BUILD_TYPE=Debug \ | ||
| -DCMAKE_VERBOSE_MAKEFILE=ON \ | ||
| -DAMReX_LINEAR_SOLVERS=ON \ | ||
| -DAMReX_FORTRAN=ON \ | ||
| -DAMReX_FORTRAN_INTERFACES=ON \ | ||
| -DAMReX_EB=ON \ | ||
| -DAMReX_PARTICLES=ON | ||
| make -j 2 | ||
| -DAMReX_PARTICLES=ON \ | ||
| -DTUTORIAL_PYTHON=ON | ||
| cmake --build build -j 2 | ||
| cmake --build build -j 2 --target pyamrex_pip_install | ||
| - name: Run Python | ||
| run: | | ||
| cd GuidedTutorials/MultiFab/ | ||
| python main.py | ||
|
|
||
| # Build all tutorials | ||
| tutorials_cxx20: | ||
|
|
@@ -40,22 +44,26 @@ jobs: | |
| - name: Build & Install | ||
| run: | | ||
| cd ExampleCodes | ||
| mkdir build | ||
| cd build | ||
| cmake .. \ | ||
| cmake -S . -B build \ | ||
| -DCMAKE_BUILD_TYPE=Debug \ | ||
| -DCMAKE_VERBOSE_MAKEFILE=ON \ | ||
| -DAMReX_OMP=ON \ | ||
| -DAMReX_PARTICLES=ON \ | ||
| -DAMReX_LINEAR_SOLVERS=ON \ | ||
| -DAMReX_FORTRAN=ON \ | ||
| -DAMReX_FORTRAN_INTERFACES=ON \ | ||
| -DAMReX_EB=ON \ | ||
| -DAMReX_EB=ON \ | ||
| -DTUTORIAL_PYTHON=ON \ | ||
| -DCMAKE_CXX_STANDARD=20 \ | ||
| -DCMAKE_C_COMPILER=$(which gcc-10) \ | ||
| -DCMAKE_CXX_COMPILER=$(which g++-10) \ | ||
| -DCMAKE_Fortran_COMPILER=$(which mpif90) | ||
| make -j 2 | ||
| cmake --build build -j 2 | ||
| cmake --build build -j 2 --target pyamrex_pip_install | ||
| - name: Run Python | ||
| run: | | ||
| cd GuidedTutorials/MultiFab/ | ||
| python main.py | ||
|
|
||
| tutorials_clang: | ||
| name: [email protected] C++14 SP Particles DP Mesh Debug [tutorials] | ||
|
|
@@ -68,9 +76,7 @@ jobs: | |
| - name: Build & Install | ||
| run: | | ||
| cd ExampleCodes | ||
| mkdir build | ||
| cd build | ||
| cmake .. \ | ||
| cmake -S . -B build \ | ||
| -DCMAKE_BUILD_TYPE=Debug \ | ||
| -DCMAKE_VERBOSE_MAKEFILE=ON \ | ||
| -DAMReX_MPI=OFF \ | ||
|
|
@@ -81,11 +87,17 @@ jobs: | |
| -DAMReX_EB=ON \ | ||
| -DAMReX_PRECISION=DOUBLE \ | ||
| -DAMReX_PARTICLES_PRECISION=SINGLE \ | ||
| -DTUTORIAL_PYTHON=ON \ | ||
| -DCMAKE_CXX_STANDARD=14 \ | ||
| -DCMAKE_C_COMPILER=$(which clang) \ | ||
| -DCMAKE_CXX_COMPILER=$(which clang++) \ | ||
| -DCMAKE_Fortran_COMPILER=$(which gfortran) | ||
| make -j 2 | ||
| cmake --build build -j 2 | ||
| cmake --build build -j 2 --target pyamrex_pip_install | ||
| - name: Run Python | ||
| run: | | ||
| cd GuidedTutorials/MultiFab/ | ||
| python main.py | ||
|
|
||
| # Build all tutorials w/o MPI | ||
| tutorials-nonmpi: | ||
|
|
@@ -99,18 +111,22 @@ jobs: | |
| - name: Build & Install | ||
| run: | | ||
| cd ExampleCodes | ||
| mkdir build | ||
| cd build | ||
| cmake .. \ | ||
| cmake -S . -B build \ | ||
| -DCMAKE_BUILD_TYPE=Debug \ | ||
| -DCMAKE_VERBOSE_MAKEFILE=ON \ | ||
| -DAMReX_MPI=OFF \ | ||
| -DAMReX_LINEAR_SOLVERS=ON \ | ||
| -DAMReX_FORTRAN=ON \ | ||
| -DAMReX_FORTRAN_INTERFACES=ON \ | ||
| -DAMReX_EB=ON \ | ||
| -DAMReX_PARTICLES=ON | ||
| make -j 2 | ||
| -DAMReX_PARTICLES=ON \ | ||
| -DTUTORIAL_PYTHON=ON | ||
| cmake --build build -j 2 | ||
| cmake --build build -j 2 --target pyamrex_pip_install | ||
| - name: Run Python | ||
| run: | | ||
| cd GuidedTutorials/MultiFab/ | ||
| python main.py | ||
|
|
||
| # Build all tutorials | ||
| tutorials-nofortran: | ||
|
|
@@ -124,16 +140,20 @@ jobs: | |
| - name: Build & Install | ||
| run: | | ||
| cd ExampleCodes | ||
| mkdir build | ||
| cd build | ||
| cmake .. \ | ||
| cmake -S . -B build \ | ||
| -DCMAKE_BUILD_TYPE=Debug \ | ||
| -DCMAKE_VERBOSE_MAKEFILE=ON \ | ||
| -DAMReX_LINEAR_SOLVERS=ON \ | ||
| -DAMReX_EB=ON \ | ||
| -DAMReX_PARTICLES=ON \ | ||
| -DAMReX_FORTRAN=OFF | ||
| make -j 2 | ||
| -DAMReX_FORTRAN=OFF \ | ||
| -DTUTORIAL_PYTHON=ON | ||
| cmake --build build -j 2 | ||
| cmake --build build -j 2 --target pyamrex_pip_install | ||
| - name: Run Python | ||
| run: | | ||
| cd GuidedTutorials/MultiFab/ | ||
| python main.py | ||
|
|
||
| # Build all tutorials with CUDA 11.0.2 (recent supported) | ||
| tutorials-cuda11: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| .. _tutorials_python: | ||
|
|
||
| ====== | ||
| Python | ||
| ====== | ||
|
|
||
| These examples show how to use AMReX from Python. | ||
| AMReX applications can also be interfaced to Python with the same logic. | ||
|
|
||
| In order to build the Python tutorials, add ``-DTUTORIAL_PYTHON=ON`` to the CMake configuration options. | ||
| Then install with ``cmake --build build --target pyamrex_pip_install``. | ||
|
|
||
| Examples: | ||
|
|
||
| - ``GuidedTutorials/MultiFab/main.py`` | ||
|
|
||
| Please see `pyAMReX <https://github.com/AMReX-Codes/pyamrex/>`__ for more details. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import numpy as np | ||
| import amrex.space3d as amr | ||
|
|
||
| # Initialize AMReX | ||
| amr.initialize([]) | ||
|
|
||
| if amr.ParallelDescriptor.IOProcessor(): | ||
| print(f"Hello world from pyAMReX version {amr.__version__}\n") | ||
|
|
||
| # Goals: | ||
| # * Define a MultiFab | ||
| # * Fill a MultiFab with data | ||
| # * Plot it | ||
|
|
||
| # Parameters | ||
|
|
||
| # Number of data components at each grid point in the MultiFab | ||
| ncomp = 1 | ||
| # How many grid cells in each direction over the problem domain | ||
| n_cell = 32 | ||
| # How many grid cells are allowed in each direction over each box | ||
| max_grid_size = 16 | ||
|
|
||
| # BoxArray: abstract domain setup | ||
|
|
||
| # Integer vector indicating the lower coordinate bounds | ||
| dom_lo = amr.IntVect(0,0,0) | ||
| # Integer vector indicating the upper coordinate bounds | ||
| dom_hi = amr.IntVect(n_cell-1, n_cell-1, n_cell-1) | ||
| # Box containing the coordinates of this domain | ||
| domain = amr.Box(dom_lo, dom_hi) | ||
|
|
||
| # Will contain a list of boxes describing the problem domain | ||
| ba = amr.BoxArray(domain) | ||
|
|
||
| # Chop the single grid into many small boxes | ||
| ba.max_size(max_grid_size) | ||
|
|
||
| # Distribution Mapping | ||
| dm = amr.DistributionMapping(ba) | ||
|
|
||
| # Define MultiFab | ||
| mf = amr.MultiFab(ba, dm, ncomp, 0) | ||
| mf.set_val(0.) | ||
|
|
||
| # Geometry: physical properties for data on our domain | ||
| real_box = amr.RealBox([0., 0., 0.], [1. , 1., 1.]) | ||
|
|
||
| coord = 0 # Cartesian | ||
| is_per = [0, 0, 0] # periodicity | ||
| geom = amr.Geometry(domain, real_box, coord, is_per) | ||
|
|
||
| # Calculate cell sizes | ||
| dx = geom.data().CellSize() # dx[0]=dx dx[1]=dy dx[2]=dz | ||
|
|
||
| # Fill a MultiFab with data | ||
| mf_val = 0. | ||
| for mfi in mf: | ||
| bx = mfi.validbox() | ||
| # Preferred way to fill array using NumPy operations: | ||
| # - mf_array is indexed in reversed order (n,z,y,x) | ||
| # - indices are local (range from 0 to box size) | ||
| mf_array = np.array(mf.array(mfi), copy=False) | ||
ax3l marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| x = (np.arange(bx.small_end[0], bx.big_end[0]+1)+0.5)*dx[0] | ||
| y = (np.arange(bx.small_end[1], bx.big_end[1]+1)+0.5)*dx[1] | ||
| z = (np.arange(bx.small_end[2], bx.big_end[2]+1)+0.5)*dx[2] | ||
| v = (x[np.newaxis,np.newaxis,:] | ||
| + y[np.newaxis,:,np.newaxis]*0.1 | ||
| + z[:,np.newaxis,np.newaxis]*0.01) | ||
| mf_array = 1. + np.exp(-v) | ||
| # Alternative way to fill array using standard for loop over box indices: | ||
| # - mf_array_ref is indexed in standard order (x,y,z,n) | ||
| # - indices are global | ||
| mf_array_ref = mf.array(mfi) | ||
| for i, j, k in bx: | ||
| x = (i+0.5)*dx[0] | ||
| y = (j+0.5)*dx[1] | ||
| z = (k+0.5)*dx[2] | ||
| v = x + y*0.1 + z*0.01 | ||
| mf_array_ref[i,j,k,0] = 1. + np.exp(-v) | ||
| # Check that mf_array has been filled correctly, compare it with | ||
| # mf_array_ref (filled as Array4 and transformed to NumPy array) | ||
| mf_array_ref_np = np.array(mf_array_ref, copy=False) | ||
ax3l marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assert np.allclose(mf_array, mf_array_ref_np, rtol=1e-16, atol=1e-16) | ||
|
|
||
| # Plot MultiFab data | ||
| plotfile = amr.concatenate(root="plt", num=1, mindigits=3) | ||
| varnames = amr.Vector_string(["comp0"]) | ||
| amr.write_single_level_plotfile(plotfile, mf, varnames, geom, time=0., level_step=0) | ||
|
|
||
| # Finalize AMReX | ||
| amr.finalize() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.