-
-
Notifications
You must be signed in to change notification settings - Fork 243
VTXWriter: support file modes #4177
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
base: main
Are you sure you want to change the base?
Changes from all commits
a1669b5
29e1102
3db6a54
5cbaf28
f79b1ee
476f551
0d7f6de
1911667
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,13 +18,13 @@ | |
|
|
||
| //----------------------------------------------------------------------------- | ||
| ADIOS2Writer::ADIOS2Writer(MPI_Comm comm, const std::filesystem::path& filename, | ||
| std::string tag, std::string engine) | ||
| adios2::Mode mode, std::string tag, | ||
| std::string engine) | ||
| : _adios(std::make_unique<adios2::ADIOS>(comm)), | ||
| _io(std::make_unique<adios2::IO>(_adios->DeclareIO(std::move(tag)))) | ||
| { | ||
| _io->SetEngine(std::move(engine)); | ||
| _engine = std::make_unique<adios2::Engine>( | ||
| _io->Open(filename, adios2::Mode::Write)); | ||
| _engine = std::make_unique<adios2::Engine>(_io->Open(filename, mode)); | ||
| } | ||
| //----------------------------------------------------------------------------- | ||
| ADIOS2Writer::~ADIOS2Writer() { close(); } | ||
|
|
@@ -38,6 +38,24 @@ | |
| _engine->Close(); | ||
| } | ||
| //----------------------------------------------------------------------------- | ||
| adios2::Mode impl_adios2::mode(const std::string& mode) | ||
|
Check warning on line 41 in cpp/dolfinx/io/ADIOS2Writers.cpp
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use non-owning |
||
| { | ||
| if (mode == "a") | ||
| return adios2::Mode::Append; | ||
|
|
||
| if (mode == "w") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. else if and else? |
||
| return adios2::Mode::Write; | ||
|
|
||
| // unsupported adios2 modes: | ||
| // - Undefined | ||
| // - Read | ||
| // - ReadRandomAccess | ||
| // - Sync | ||
| // - Deferred | ||
|
|
||
| throw std::runtime_error("Invalid adios2 mode."); | ||
|
Check warning on line 56 in cpp/dolfinx/io/ADIOS2Writers.cpp
|
||
| } | ||
| //----------------------------------------------------------------------------- | ||
| std::stringstream | ||
| io::impl_vtx::create_vtk_schema(const std::vector<std::string>& point_data, | ||
| const std::vector<std::string>& cell_data) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
|
|
||
| #include "vtk_utils.h" | ||
| #include <adios2.h> | ||
| #include <adios2/common/ADIOSTypes.h> | ||
| #include <algorithm> | ||
| #include <basix/mdspan.hpp> | ||
| #include <cassert> | ||
|
|
@@ -59,11 +60,12 @@ class ADIOS2Writer | |
| /// @brief Create an ADIOS2-based writer | ||
| /// @param[in] comm The MPI communicator | ||
| /// @param[in] filename Name of output file | ||
| /// @param[in] mode Mode to open file with | ||
| /// @param[in] tag The ADIOS2 object name | ||
| /// @param[in] engine ADIOS2 engine type. See | ||
| /// https://adios2.readthedocs.io/en/latest/engines/engines.html. | ||
| ADIOS2Writer(MPI_Comm comm, const std::filesystem::path& filename, | ||
| std::string tag, std::string engine); | ||
| adios2::Mode mode, std::string tag, std::string engine); | ||
|
|
||
| /// @brief Move constructor | ||
| ADIOS2Writer(ADIOS2Writer&& writer) = default; | ||
|
|
@@ -157,6 +159,11 @@ extract_common_mesh(const typename adios2_writer::U<T>& u) | |
| return mesh; | ||
| } | ||
|
|
||
| /// Convert string to corresponding adios2 mode. | ||
| /// @param[in] mode Mode in string representation, either "w" or "a". | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving aside Enum vs no Enum, it's generally most transparent to map modes directly e.g. "Read" -> "adios2::Mode::Read". Then I have a clear idea what "Read" means in the context of the underlying library. |
||
| /// @returns Adios2 mode. | ||
| adios2::Mode mode(const std::string& mode); | ||
|
|
||
| } // namespace impl_adios2 | ||
|
|
||
| /// @privatesection | ||
|
|
@@ -461,16 +468,18 @@ class VTXWriter : public ADIOS2Writer | |
| /// | ||
| /// @param[in] comm MPI communicator to open the file on. | ||
| /// @param[in] filename Name of output file. | ||
| /// @param[in] mode Mode to open file with | ||
| /// @param[in] mesh Mesh to write. | ||
| /// @param[in] engine ADIOS2 engine type. | ||
| /// @note This format supports arbitrary degree meshes. | ||
| /// @note The mesh geometry can be updated between write steps but the | ||
| /// topology should not be changed between write steps. | ||
| VTXWriter(MPI_Comm comm, const std::filesystem::path& filename, | ||
| std::shared_ptr<const mesh::Mesh<T>> mesh, | ||
| const std::string& mode, std::shared_ptr<const mesh::Mesh<T>> mesh, | ||
| std::string engine = "BPFile") | ||
| : ADIOS2Writer(comm, filename, "VTX mesh writer", engine), _mesh(mesh), | ||
| _mesh_reuse_policy(VTXMeshPolicy::update), | ||
| : ADIOS2Writer(comm, filename, impl_adios2::mode(mode), "VTX mesh writer", | ||
| engine), | ||
| _mesh(mesh), _mesh_reuse_policy(VTXMeshPolicy::update), | ||
| _has_piecewise_constant(false) | ||
| { | ||
| // Define VTK scheme attribute for mesh | ||
|
|
@@ -484,6 +493,7 @@ class VTXWriter : public ADIOS2Writer | |
| /// | ||
| /// @param[in] comm The MPI communicator to open the file on | ||
| /// @param[in] filename Name of output file | ||
| /// @param[in] mode Mode to open file with | ||
| /// @param[in] u List of functions. The functions must (1) share the | ||
| /// same mesh and (2) be (discontinuous) Lagrange functions. The | ||
| /// element family and degree, and degree-of-freedom map (up to the | ||
|
|
@@ -494,9 +504,11 @@ class VTXWriter : public ADIOS2Writer | |
| /// step. | ||
| /// @note This format supports arbitrary degree meshes. | ||
| VTXWriter(MPI_Comm comm, const std::filesystem::path& filename, | ||
| const typename adios2_writer::U<T>& u, std::string engine, | ||
| const std::string& mode, const typename adios2_writer::U<T>& u, | ||
| std::string engine, | ||
| VTXMeshPolicy mesh_policy = VTXMeshPolicy::update) | ||
| : ADIOS2Writer(comm, filename, "VTX function writer", engine), | ||
| : ADIOS2Writer(comm, filename, impl_adios2::mode(mode), | ||
| "VTX function writer", engine), | ||
| _mesh(impl_adios2::extract_common_mesh<T>(u)), _u(u), | ||
| _mesh_reuse_policy(mesh_policy), _has_piecewise_constant(false) | ||
| { | ||
|
|
@@ -595,6 +607,7 @@ class VTXWriter : public ADIOS2Writer | |
| /// | ||
| /// @param[in] comm The MPI communicator to open the file on | ||
| /// @param[in] filename Name of output file | ||
| /// @param[in] mode Mode to open file with | ||
| /// @param[in] u List of functions. The functions must (1) share the | ||
| /// same mesh and (2) be (discontinuous) Lagrange functions. The | ||
| /// element family and degree must be the same for all functions. | ||
|
|
@@ -603,9 +616,9 @@ class VTXWriter : public ADIOS2Writer | |
| /// step. | ||
| /// @note This format supports arbitrary degree meshes. | ||
| VTXWriter(MPI_Comm comm, const std::filesystem::path& filename, | ||
| const typename adios2_writer::U<T>& u, | ||
| const std::string& mode, const typename adios2_writer::U<T>& u, | ||
| VTXMeshPolicy mesh_policy = VTXMeshPolicy::update) | ||
| : VTXWriter(comm, filename, u, "BPFile", mesh_policy) | ||
| : VTXWriter(comm, filename, mode, u, "BPFile", mesh_policy) | ||
| { | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ def __init__( | |
| self, | ||
| comm: _MPI.Comm, | ||
| filename: str | Path, | ||
| mode: str, | ||
| output: Mesh | Function | list[Function] | tuple[Function], | ||
| engine: str = "BPFile", | ||
| mesh_policy: VTXMeshPolicy = VTXMeshPolicy.update, | ||
|
|
@@ -56,6 +57,8 @@ def __init__( | |
| Args: | ||
| comm: The MPI communicator | ||
| filename: The output filename | ||
| mode: The filemode to open the file in, one of 'a' (append) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove "one of", automatic when using "or". |
||
| or 'w' (write). | ||
| output: The data to output. Either a mesh, a single | ||
| (discontinuous) Lagrange Function or list of | ||
| (discontinuous) Lagrange Functions. | ||
|
|
@@ -87,14 +90,16 @@ def __init__( | |
| raise RuntimeError(f"VTXWriter does not support dtype={dtype}.") | ||
|
|
||
| if isinstance(output, Mesh): | ||
| self._cpp_object = _vtxwriter(comm, filename, output._cpp_object, engine) # type: ignore[union-attr] | ||
| self._cpp_object = _vtxwriter(comm, filename, mode, output._cpp_object, engine) # type: ignore[union-attr] | ||
| else: | ||
| cpp_objects = ( | ||
| [output._cpp_object] | ||
| if isinstance(output, Function) | ||
| else [o._cpp_object for o in output] | ||
| ) | ||
| self._cpp_object = _vtxwriter(comm, filename, cpp_objects, engine, mesh_policy) | ||
| self._cpp_object = _vtxwriter( | ||
| comm, filename, mode, cpp_objects, engine, mesh_policy | ||
| ) | ||
|
|
||
| def __enter__(self): | ||
| """Enter context manager.""" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you consider just wrapping
adios2::Modedirectly as an enum?