From 6b28275b8862c4b67372573c65acbcda6fcbe3e3 Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Sun, 13 Feb 2022 19:02:51 +0100 Subject: [PATCH] double conversion on C++ side --- libsrc/meshing/python_mesh.cpp | 24 ++++++++++++++++++------ python/read_meshio.py | 6 ++++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/libsrc/meshing/python_mesh.cpp b/libsrc/meshing/python_mesh.cpp index 30fc1b88..38586eb2 100644 --- a/libsrc/meshing/python_mesh.cpp +++ b/libsrc/meshing/python_mesh.cpp @@ -914,16 +914,23 @@ DLL_HEADER void ExportNetgenMeshing(py::module &m) return self.AddFaceDescriptor (fd); }) - .def ("AddPoints", [](Mesh & self, py::buffer b) + .def ("AddPoints", [](Mesh & self, py::buffer b1) { static Timer timer("Mesh::AddPoints"); + static Timer timercast("Mesh::AddPoints - casting"); RegionTimer reg(timer); + + timercast.Start(); + // casting from here: https://github.com/pybind/pybind11/issues/1908 + auto b = b1.cast>(); + timercast.Stop(); py::buffer_info info = b.request(); + // cout << "data format = " << info.format << endl; if (info.ndim != 2) throw std::runtime_error("AddPoints needs buffer of dimension 2"); - if (info.format != py::format_descriptor::format()) - throw std::runtime_error("AddPoints needs buffer of type double"); + // if (info.format != py::format_descriptor::format()) + // throw std::runtime_error("AddPoints needs buffer of type double"); if (info.strides[0] != sizeof(double)*info.shape[1]) throw std::runtime_error("AddPoints needs packed array"); double * ptr = static_cast (info.ptr); @@ -942,16 +949,21 @@ DLL_HEADER void ExportNetgenMeshing(py::module &m) ptr += 3; } }) - .def ("AddElements", [](Mesh & self, int dim, int index, py::buffer b, int base) + .def ("AddElements", [](Mesh & self, int dim, int index, py::buffer b1, int base) { static Timer timer("Mesh::AddElements"); + static Timer timercast("Mesh::AddElements casting"); RegionTimer reg(timer); + + timercast.Start(); + auto b = b1.cast>(); + timercast.Stop(); py::buffer_info info = b.request(); if (info.ndim != 2) throw std::runtime_error("AddElements needs buffer of dimension 2"); - if (info.format != py::format_descriptor::format()) - throw std::runtime_error("AddPoints needs buffer of type int"); + // if (info.format != py::format_descriptor::format()) + // throw std::runtime_error("AddPoints needs buffer of type int"); int * ptr = static_cast (info.ptr); if (dim == 2) diff --git a/python/read_meshio.py b/python/read_meshio.py index cbb84bae..5cd8a4f1 100644 --- a/python/read_meshio.py +++ b/python/read_meshio.py @@ -10,11 +10,13 @@ def ReadViaMeshIO(filename): pts = m.points mesh = Mesh(dim=pts.shape[1]) - mesh.AddPoints ( np.asarray(pts, dtype=np.float64) ) # needed for correct little/big endian + # mesh.AddPoints ( np.asarray(pts, dtype=np.float64) ) # needed for correct little/big endian + mesh.AddPoints ( pts ) fd = mesh.Add (FaceDescriptor(bc=1)) for cb in m.cells: - mesh.AddElements(dim=cb.dim, index=1, data=np.asarray(cb.data, dtype=np.int32), base=0) + # mesh.AddElements(dim=cb.dim, index=1, data=np.asarray(cb.data, dtype=np.int32), base=0) + mesh.AddElements(dim=cb.dim, index=1, data=cb.data, base=0) return mesh