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..8c2dab63 --- /dev/null +++ b/src/Base/VisMF.cpp @@ -0,0 +1,46 @@ +/* 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") + .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") + ; +} 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__") =