From 526206a67e1f09106460bd4e414a51ac661e6be6 Mon Sep 17 00:00:00 2001 From: David Grote Date: Tue, 29 Oct 2024 16:37:39 -0700 Subject: [PATCH 1/4] Fix case when only empty tuple is passed in --- src/amrex/extensions/MultiFab.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/amrex/extensions/MultiFab.py b/src/amrex/extensions/MultiFab.py index 3f6b4144..c313b532 100644 --- a/src/amrex/extensions/MultiFab.py +++ b/src/amrex/extensions/MultiFab.py @@ -314,13 +314,17 @@ def _process_index(self, index): # If only one slice or integer passed in, it was not wrapped in a tuple index = [index] elif isinstance(index, tuple): - index = list(index) - for i in range(len(index)): - if index[i] == Ellipsis: - index = ( - index[:i] + (dims + 2 - len(index)) * [slice(None)] + index[i + 1 :] - ) - break + if len(index) == 0: + # The empty tuple specifies all valid and ghost cells + index = [index] + else: + index = list(index) + for i in range(len(index)): + if index[i] == Ellipsis: + index = ( + index[:i] + (dims + 2 - len(index)) * [slice(None)] + index[i + 1 :] + ) + break else: raise Exception("MultiFab.__getitem__: unexpected index type") From 8c11e802591165cd9c5754b22b0c25d23b747c94 Mon Sep 17 00:00:00 2001 From: David Grote Date: Thu, 31 Oct 2024 18:30:12 -0700 Subject: [PATCH 2/4] Add VisMF wrapper --- src/Base/CMakeLists.txt | 1 + src/Base/VisMF.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/pyAMReX.cpp | 3 +++ 3 files changed, 41 insertions(+) create mode 100644 src/Base/VisMF.cpp diff --git a/src/Base/CMakeLists.txt b/src/Base/CMakeLists.txt index 5a8169eb..b80c820b 100644 --- a/src/Base/CMakeLists.txt +++ b/src/Base/CMakeLists.txt @@ -33,6 +33,7 @@ foreach(D IN LISTS AMReX_SPACEDIM) Utility.cpp Vector.cpp Version.cpp + VisMF.cpp ) if (AMReX_MPI) diff --git a/src/Base/VisMF.cpp b/src/Base/VisMF.cpp new file mode 100644 index 00000000..93e30b42 --- /dev/null +++ b/src/Base/VisMF.cpp @@ -0,0 +1,37 @@ +/* Copyright 2024 The AMReX Community + * + * Authors: David Grote + * License: BSD-3-Clause-LBNL + */ +#include "pyAMReX.H" + +#include +#include + +void init_VisMF(py::module &m) +{ + py::class_< amrex::VisMF > py_VisMF(m, "VisMF"); + + py_VisMF + .def_static("Write", + [](const amrex::FabArray &mf, const std::string& name) { + return amrex::VisMF::Write(mf, name); + }, + py::arg("mf"), py::arg("name"), + "Writes a Multifab to the specified file") + .def_static("Read", + [](const std::string &name) { + amrex::MultiFab mf; + if (amrex::VisMF::Exist(name)) { + amrex::VisMF::Read(mf, name); + } else { + throw std::runtime_error("MultiFab file " + name + " couldn't be found!"); + } + return mf; + }, + py::return_value_policy::move, + py::arg("name"), + "Reads a MultiFab from the specified file") + ; +} + diff --git a/src/pyAMReX.cpp b/src/pyAMReX.cpp index e952e64b..aebe7403 100644 --- a/src/pyAMReX.cpp +++ b/src/pyAMReX.cpp @@ -38,6 +38,7 @@ void init_PODVector(py::module &); void init_Utility(py::module &); void init_Vector(py::module &); void init_Version(py::module &); +void init_VisMF(py::module &); #ifdef AMREX_USE_MPI void init_MPMD(py::module &); #endif @@ -82,6 +83,7 @@ PYBIND11_MODULE(amrex_3d_pybind, m) { StructOfArrays Utility Vector + VisMF )pbdoc"; // note: order from parent to child classes and argument usage @@ -117,6 +119,7 @@ PYBIND11_MODULE(amrex_3d_pybind, m) { init_PlotFileUtil(m); init_Utility(m); init_Version(m); + init_VisMF(m); // authors m.attr("__author__") = From d071ae4f7bb6020fae149127fd1477bf9b006d50 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 1 Nov 2024 16:22:31 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/Base/VisMF.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Base/VisMF.cpp b/src/Base/VisMF.cpp index 93e30b42..18efde19 100644 --- a/src/Base/VisMF.cpp +++ b/src/Base/VisMF.cpp @@ -34,4 +34,3 @@ void init_VisMF(py::module &m) "Reads a MultiFab from the specified file") ; } - From 4c4ee46e5b0cfa6ed5040bef2d0a0ab699ea78be Mon Sep 17 00:00:00 2001 From: David Grote Date: Fri, 1 Nov 2024 10:37:16 -0700 Subject: [PATCH 4/4] Add version of Read that takes MultiFab as argument --- src/Base/VisMF.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Base/VisMF.cpp b/src/Base/VisMF.cpp index 18efde19..8c2dab63 100644 --- a/src/Base/VisMF.cpp +++ b/src/Base/VisMF.cpp @@ -32,5 +32,15 @@ void init_VisMF(py::module &m) py::return_value_policy::move, py::arg("name"), "Reads a MultiFab from the specified file") + .def_static("Read", + [](const std::string &name, amrex::MultiFab &mf) { + if (amrex::VisMF::Exist(name)) { + amrex::VisMF::Read(mf, name); + } else { + throw std::runtime_error("MultiFab file " + name + " couldn't be found!"); + } + }, + py::arg("name"), py::arg("mf"), + "Reads a MultiFab from the specified file into the given MultiFab. The BoxArray on the disk must match the BoxArray * in mf") ; }