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 PlotFileData bindings #320

Merged
merged 23 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
017e48f
add PlotFileData bindings
BenWibking May 19, 2024
00ad861
Update PlotFileUtil.cpp
BenWibking May 20, 2024
66b9016
Update PlotFileUtil.cpp
BenWibking May 20, 2024
f3c6154
fix bugs
BenWibking May 21, 2024
192fb81
merge
BenWibking May 21, 2024
1067905
fix bindings for overloaded function
BenWibking May 21, 2024
4eddd00
add unit test for simple single-level 3D plotfile
BenWibking May 30, 2024
fb171ee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 30, 2024
3c9398f
fix box, string asserts
BenWibking May 30, 2024
4ecab8a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 30, 2024
415253f
fix lint error
BenWibking May 30, 2024
518d68e
add script to plot 2d plotfiles
BenWibking May 31, 2024
f4036b9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 31, 2024
669d816
Apply suggestions from code review
BenWibking Jun 2, 2024
f47457e
Tests: Import pytest
ax3l Jun 12, 2024
b8441fd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 12, 2024
b2fa202
Import amr
ax3l Jun 12, 2024
dfe92ee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 12, 2024
f57834f
generate test data on-the-fly
BenWibking Jul 1, 2024
5816247
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 1, 2024
e47b10d
remove extra import
BenWibking Jul 1, 2024
8a7c193
Merge branch 'add-plotfiledata' of github.com:BenWibking/pyamrex into…
BenWibking Jul 1, 2024
a784697
remove extra import (again)
BenWibking Jul 1, 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
52 changes: 35 additions & 17 deletions src/Base/PlotFileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,47 @@
#include "pyAMReX.H"

#include <AMReX_PlotFileUtil.H>
#include <AMReX_Vector.H>
#include <AMReX_Print.H>
#include <AMReX_Vector.H>

#include <sstream>
#include <string>

namespace py = pybind11;
using namespace amrex;

void init_PlotFileUtil(py::module& m)
{
m.def("write_single_level_plotfile",
&amrex::WriteSingleLevelPlotfile,
"Writes single level plotfile",
py::arg("plotfilename"),
py::arg("mf"),
py::arg("varnames"),
py::arg("geom"),
py::arg("time"),
py::arg("level_step"),
py::arg("versionName")="HyperCLaw-V1.1",
py::arg("levelPrefix")="Level_",
py::arg("mfPrefix")="Cell",
py::arg_v("extra_dirs", Vector<std::string>(), "list[str]")
);
void init_PlotFileUtil(py::module &m) {
m.def("write_single_level_plotfile", &amrex::WriteSingleLevelPlotfile,
"Writes single level plotfile", py::arg("plotfilename"), py::arg("mf"),
py::arg("varnames"), py::arg("geom"), py::arg("time"),
py::arg("level_step"), py::arg("versionName") = "HyperCLaw-V1.1",
py::arg("levelPrefix") = "Level_", py::arg("mfPrefix") = "Cell",
py::arg_v("extra_dirs", Vector<std::string>(), "list[str]"));

py::class_<PlotFileData>(m, "PlotFileData")
// explicitly provide constructor argument types
.def(py::init<std::string const&>())

.def("spaceDim", &PlotFileData::spaceDim)
.def("time", &PlotFileData::time)
.def("finestLevel", &PlotFileData::finestLevel)
.def("refRatio", &PlotFileData::refRatio)
.def("levelStep", &PlotFileData::levelStep)
.def("boxArray", &PlotFileData::boxArray)
.def("DistributionMap", &PlotFileData::DistributionMap)
.def("syncDistributionMap", py::overload_cast<PlotFileData const&>(&PlotFileData::syncDistributionMap))
.def("syncDistributionMap", py::overload_cast<int, PlotFileData const&>(&PlotFileData::syncDistributionMap))

.def("coordSys", &PlotFileData::coordSys)
.def("probDomain", &PlotFileData::probDomain)
.def("probSize", &PlotFileData::probSize)
.def("probLo", &PlotFileData::probLo)
.def("probHi", &PlotFileData::probHi)
.def("cellSize", &PlotFileData::cellSize)
.def("varNames", &PlotFileData::varNames)
.def("nComp", &PlotFileData::nComp)
.def("nGrowVect", &PlotFileData::nGrowVect)

.def("get", py::overload_cast<int>(&PlotFileData::get))
.def("get", py::overload_cast<int, std::string const&>(&PlotFileData::get));
}
22 changes: 22 additions & 0 deletions tests/projz04000/Header
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
HyperCLaw-V1.1
BenWibking marked this conversation as resolved.
Show resolved Hide resolved
3
nH_wind
nH_cloud
nH
3
56649980960510.086
0
0 0 0
2.4688e+21 6.172e+20 6.172e+20

((0,0,0) (1023,255,0) (0,0,0))
4000
2.4109375e+18 2.4109375e+18 6.172e+20
0
0
0 1 56649980960510.086
4000
0 2.4688e+21
0 6.172e+20
0 6.172e+20
Level_0/Cell
Binary file added tests/projz04000/Level_0/Cell_D_00000
BenWibking marked this conversation as resolved.
Show resolved Hide resolved
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/projz04000/Level_0/Cell_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
1
1
3
0
(1 0
((0,0,0) (1023,255,0) (0,0,0))
)
1
FabOnDisk: Cell_D_00000 0

1,3
2.0621599037663667e+18,0.0000000000000000e+00,2.0621599037663667e+18,

1,3
2.0681399821329768e+19,5.5521812980561425e+20,5.6820568202245806e+20,
57 changes: 57 additions & 0 deletions tests/test_plotfiledata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-

import os
from pathlib import Path

ax3l marked this conversation as resolved.
Show resolved Hide resolved
import pytest
ax3l marked this conversation as resolved.
Show resolved Hide resolved

import amrex.space3d as amr

BenWibking marked this conversation as resolved.
Show resolved Hide resolved

@pytest.mark.skipif(amr.Config.spacedim != 3, reason="Requires AMREX_SPACEDIM = 3")
def test_plotfiledata_read():
BenWibking marked this conversation as resolved.
Show resolved Hide resolved
import amrex.space3d as amr
Fixed Show fixed Hide fixed

parent_path = Path(os.path.abspath(__file__)).parent
plt = amr.PlotFileData(str(parent_path / "projz04000"))

assert plt.spaceDim() == 3
assert plt.time() == 56649980960510.086
assert plt.finestLevel() == 0
assert plt.refRatio(0) == 0
assert plt.coordSys() == amr.CoordSys.cartesian

probDomain = plt.probDomain(0)
probSize = plt.probSize()
probLo = plt.probLo()
probHi = plt.probHi()
cellSize = plt.cellSize(0)
varNames = plt.varNames()
nComp = plt.nComp()
nGrowVect = plt.nGrowVect(0)

# assert probDomain == amr.Box([0,0,0], [1023,255,0]) # doesn't work
assert probDomain.small_end == amr.IntVect(0, 0, 0)
assert probDomain.big_end == amr.IntVect(1023, 255, 0)

assert probSize == [2.4688e21, 6.172e20, 6.172e20]
assert probLo == [0.0, 0.0, 0.0]
assert probHi == [2.4688e21, 6.172e20, 6.172e20]
assert cellSize == [2.4109375e18, 2.4109375e18, 6.172e20]
assert varNames == amr.Vector_string(["nH_wind", "nH_cloud", "nH"])
assert nComp == 3
assert nGrowVect == amr.IntVect(0, 0, 0)

for compname in varNames:
mfab_comp = plt.get(0, compname)
nboxes = 0

for mfi in mfab_comp:
marr = mfab_comp.array(mfi)
# numpy/cupy representation: non-copying view, including the
# guard/ghost region
marr_xp = marr.to_xp()
assert marr_xp.shape == (1024, 256, 1, 1)
nboxes += 1

assert nboxes == 1
61 changes: 61 additions & 0 deletions tools/plot_plotfile_2d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import matplotlib.pyplot as plt
import numpy as np
import pytest
ax3l marked this conversation as resolved.
Show resolved Hide resolved

import amrex.space3d as amr

ax3l marked this conversation as resolved.
Show resolved Hide resolved

def plot_mf(arr, compname, plo, phi):
plt.plot()
im = plt.imshow(
arr.T,
origin="lower",
interpolation="none",
extent=[plo[0], phi[0], plo[1], phi[1]],
aspect="equal",
)
plt.colorbar(im)
plt.tight_layout()
plt.savefig(f"{compname}.png", dpi=150)
plt.close()


@pytest.mark.skipif(amr.Config.spacedim != 2, reason="Requires AMREX_SPACEDIM = 2")
def plot_plotfile_2d(filename, level=0):
BenWibking marked this conversation as resolved.
Show resolved Hide resolved
import amrex.space2d as amr
Fixed Show fixed Hide fixed

plt = amr.PlotFileData(filename)
assert level <= plt.finestLevel()

probDomain = plt.probDomain(level)
probLo = plt.probLo()
probHi = plt.probHi()
varNames = plt.varNames()

for compname in varNames:
mfab_comp = plt.get(level, compname)
arr = np.zeros((probDomain.big_end - probDomain.small_end + 1))
for mfi in mfab_comp:
bx = mfi.tilebox()
marr = mfab_comp.array(mfi)
marr_xp = marr.to_xp()
i_s, j_s = tuple(bx.small_end)
i_e, j_e = tuple(bx.big_end)
arr[i_s : i_e + 1, j_s : j_e + 1] = marr_xp[:, :, 0, 0]
plot_mf(arr, compname, probLo, probHi)


if __name__ == "__main__":
import argparse
BenWibking marked this conversation as resolved.
Show resolved Hide resolved

import amrex.space2d as amr

parser = argparse.ArgumentParser(
"Plots each variable in a 2D plotfile using matplotlib."
)
parser.add_argument("filename", help="AMReX 2D plotfile to read")
parser.add_argument("level", type=int, help="AMR level to plot (default: 0)")
args = parser.parse_args()

amr.initialize([])
plot_plotfile_2d(args.filename, level=args.level)
Loading