Skip to content

Commit

Permalink
Create meshes as a container
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Nov 9, 2022
1 parent f35f06c commit b9750aa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 40 deletions.
17 changes: 6 additions & 11 deletions include/openPMD/binding/python/Container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,14 @@ Class_ bind_container(Class_ &cl, std::string const &name)
return cl;
}

template <
typename Map,
typename holder_type = std::unique_ptr<Map>,
typename... Args>
py::class_<Map, holder_type, Attributable> create_and_bind_container(
py::handle scope, std::string const &name, Args &&...args)
template <typename Map, typename... Args>
py::class_<Map, std::unique_ptr<Map>, Args...>
create_and_bind_container(py::handle scope, std::string const &name)
{
using holder_type = std::unique_ptr<Map>;
using KeyType = typename Map::key_type;
using MappedType = typename Map::mapped_type;
using Class_ = py::class_<Map, holder_type, Attributable>;
using Class_ = py::class_<Map, holder_type, Args...>;

// If either type is a non-module-local bound type then make the map
// binding non-local as well; otherwise (e.g. both types are either
Expand All @@ -127,11 +125,8 @@ py::class_<Map, holder_type, Attributable> create_and_bind_container(
scope,
name.c_str(),
py::module_local(local),
py::multiple_inheritance(),
std::forward<Args>(args)...);
py::multiple_inheritance());

// maybe move this to bind_container
cl.def(py::init<Map const &>());
return bind_container<Map>(cl, name);
}
} // namespace openPMD::detail
17 changes: 4 additions & 13 deletions src/binding/python/BaseRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "openPMD/backend/Container.hpp"
#include "openPMD/backend/MeshRecordComponent.hpp"
#include "openPMD/backend/PatchRecordComponent.hpp"
#include "openPMD/binding/python/Container.hpp"
#include "openPMD/binding/python/UnitDimension.hpp"

namespace py = pybind11;
Expand All @@ -37,29 +38,19 @@ void init_BaseRecord(py::module &m)
Returns true if this record only contains a single component.
)docstr";

// py::class_<
// BaseRecord<BaseRecordComponent>,
// Container<BaseRecordComponent> >(m,
// "Base_Record_Base_Record_Component") .def_property_readonly(
// "unit_dimension",
// &BaseRecord<BaseRecordComponent>::unitDimension,
// python::doc_unit_dimension)
// .def_property_readonly(
// "scalar", &BaseRecord<BaseRecordComponent>::scalar, doc_scalar);

py::class_<
detail::create_and_bind_container<
BaseRecord<RecordComponent>,
Container<RecordComponent>,
RecordComponent>(m, "Base_Record_Record_Component")
.def_property_readonly(
"scalar", &BaseRecord<RecordComponent>::scalar, doc_scalar);
py::class_<
detail::create_and_bind_container<
BaseRecord<MeshRecordComponent>,
Container<MeshRecordComponent>,
MeshRecordComponent>(m, "Base_Record_Mesh_Record_Component")
.def_property_readonly(
"scalar", &BaseRecord<MeshRecordComponent>::scalar, doc_scalar);
py::class_<
detail::create_and_bind_container<
BaseRecord<PatchRecordComponent>,
Container<PatchRecordComponent>,
PatchRecordComponent>(m, "Base_Record_Patch_Record_Component")
Expand Down
47 changes: 31 additions & 16 deletions src/binding/python/Container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ using PyPatchRecordContainer = Container<PatchRecord>;
using PyRecordComponentContainer = Container<RecordComponent>;
using PyMeshRecordComponentContainer = Container<MeshRecordComponent>;
using PyPatchRecordComponentContainer = Container<PatchRecordComponent>;
using PyBaseRecordComponentContainer = Container<BaseRecordComponent>;
// using PyBaseRecordComponentContainer = Container<BaseRecordComponent>;
PYBIND11_MAKE_OPAQUE(PyIterationContainer)
PYBIND11_MAKE_OPAQUE(PyMeshContainer)
PYBIND11_MAKE_OPAQUE(PyPartContainer)
Expand All @@ -61,27 +61,42 @@ PYBIND11_MAKE_OPAQUE(PyPatchRecordContainer)
PYBIND11_MAKE_OPAQUE(PyRecordComponentContainer)
PYBIND11_MAKE_OPAQUE(PyMeshRecordComponentContainer)
PYBIND11_MAKE_OPAQUE(PyPatchRecordComponentContainer)
PYBIND11_MAKE_OPAQUE(PyBaseRecordComponentContainer)
// PYBIND11_MAKE_OPAQUE(PyBaseRecordComponentContainer)

namespace openPMD::detail
{
template <typename Map>
void wrap_bind_container(py::handle scope, std::string const &name)
{
create_and_bind_container<Map, Attributable>(std::move(scope), name)
.def(py::init<Map const &>());
}
} // namespace openPMD::detail

void init_Container(py::module &m)
{
::detail::create_and_bind_container<PyIterationContainer>(
::detail::create_and_bind_container<PyIterationContainer, Attributable>(
m, "Iteration_Container");
::detail::create_and_bind_container<PyMeshContainer>(m, "Mesh_Container");
::detail::create_and_bind_container<PyPartContainer>(
::detail::create_and_bind_container<PyMeshContainer, Attributable>(
m, "Mesh_Container");
::detail::create_and_bind_container<PyPartContainer, Attributable>(
m, "Particle_Container");
::detail::create_and_bind_container<PyPatchContainer>(
::detail::create_and_bind_container<PyPatchContainer, Attributable>(
m, "Particle_Patches_Container");
::detail::create_and_bind_container<PyRecordContainer>(
::detail::create_and_bind_container<PyRecordContainer, Attributable>(
m, "Record_Container");
::detail::create_and_bind_container<PyPatchRecordContainer>(
::detail::create_and_bind_container<PyPatchRecordContainer, Attributable>(
m, "Patch_Record_Container");
::detail::create_and_bind_container<PyRecordComponentContainer>(
m, "Record_Component_Container");
::detail::create_and_bind_container<PyMeshRecordComponentContainer>(
m, "Mesh_Record_Component_Container");
::detail::create_and_bind_container<PyPatchRecordComponentContainer>(
m, "Patch_Record_Component_Container");
::detail::create_and_bind_container<PyBaseRecordComponentContainer>(
m, "Base_Record_Component_Container");
::detail::
create_and_bind_container<PyRecordComponentContainer, Attributable>(
m, "Record_Component_Container");
::detail::
create_and_bind_container<PyMeshRecordComponentContainer, Attributable>(
m, "Mesh_Record_Component_Container");
::detail::create_and_bind_container<
PyPatchRecordComponentContainer,
Attributable>(m, "Patch_Record_Component_Container");
// ::detail::create_and_bind_container<PyBaseRecordComponentContainer,
// Attributable>(
// m, "Base_Record_Component_Container");
}

0 comments on commit b9750aa

Please sign in to comment.