From 2096d8d1d0b4c148a492038018176fa6d24649d7 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Tue, 29 Sep 2020 17:24:19 -0500 Subject: [PATCH 01/19] Start on converting pyvisfile.silo to pybind11 --- setup.py | 43 ++++---- src/wrapper/wrap_silo.cpp | 199 +++++++++++++++++++------------------- 2 files changed, 122 insertions(+), 120 deletions(-) diff --git a/setup.py b/setup.py index 63a5ae5..17b68da 100644 --- a/setup.py +++ b/setup.py @@ -9,8 +9,6 @@ def get_config_schema(): return ConfigSchema(make_boost_base_options() + [ Switch("USE_SILO", False, "Compile libsilo interface"), - BoostLibraries("python"), - IncludeDir("SILO", []), LibraryDir("SILO", []), Libraries("SILO", ["siloh5"]), @@ -22,22 +20,18 @@ def get_config_schema(): def main(): from setuptools import find_packages - from aksetup_helper import hack_distutils, get_config, setup, \ - PyUblasExtension + from aksetup_helper import (hack_distutils, get_config, setup, + ExtensionUsingNumpy, check_pybind11, PybindBuildExtCommand, + get_pybind_include) hack_distutils() conf = get_config(get_config_schema(), warn_about_no_config=False) - INCLUDE_DIRS = conf["BOOST_INC_DIR"] # noqa - - LIBRARY_DIRS = conf["BOOST_LIB_DIR"] # noqa - LIBRARIES = conf["BOOST_PYTHON_LIBNAME"] # noqa - - EXTRA_DEFINES = {} # noqa - EXTRA_INCLUDE_DIRS = [] # noqa - EXTRA_LIBRARY_DIRS = [] # noqa - EXTRA_LIBRARIES = [] # noqa + extra_defines = {} + extra_include_dirs = [] + extra_library_dirs = [] + extra_libraries = [] ver_dic = {} ver_file_name = "pyvisfile/__init__.py" @@ -48,19 +42,23 @@ def main(): ext_modules = [] if conf["USE_SILO"]: - EXTRA_DEFINES["USE_SILO"] = 1 - EXTRA_INCLUDE_DIRS.extend(conf["SILO_INC_DIR"]) - EXTRA_LIBRARY_DIRS.extend(conf["SILO_LIB_DIR"]) - EXTRA_LIBRARIES.extend(conf["SILO_LIBNAME"]) + check_pybind11() + + extra_defines["USE_SILO"] = 1 + extra_include_dirs.extend(conf["SILO_INC_DIR"]) + extra_library_dirs.extend(conf["SILO_LIB_DIR"]) + extra_libraries.extend(conf["SILO_LIBNAME"]) - ext_modules.append(PyUblasExtension("_internal", + ext_modules.append(ExtensionUsingNumpy("_internal", ["src/wrapper/wrap_silo.cpp"], - include_dirs=INCLUDE_DIRS + EXTRA_INCLUDE_DIRS, - library_dirs=LIBRARY_DIRS + EXTRA_LIBRARY_DIRS, - libraries=LIBRARIES + EXTRA_LIBRARIES, + include_dirs=[get_pybind_include()] + extra_include_dirs, + library_dirs=extra_library_dirs, + libraries=extra_libraries, extra_compile_args=conf["CXXFLAGS"], - define_macros=list(EXTRA_DEFINES.items()), + define_macros=list(extra_defines.items()), + language="c++", )) + requirements.append("pybind11>=2.5.0") setup(name="pyvisfile", version=ver_dic["VERSION_TEXT"], @@ -100,6 +98,7 @@ def main(): ext_package="pyvisfile.silo", ext_modules=ext_modules, + cmdclass={"build_ext": PybindBuildExtCommand}, zip_safe=False) diff --git a/src/wrapper/wrap_silo.cpp b/src/wrapper/wrap_silo.cpp index df4a6a6..0d8455a 100644 --- a/src/wrapper/wrap_silo.cpp +++ b/src/wrapper/wrap_silo.cpp @@ -2,15 +2,11 @@ // Copyright (C) 2007 Andreas Kloeckner +#include +#include - -#include #include -#include -#include #include -#include -#include #include #include #include @@ -40,7 +36,7 @@ #define ENUM_VALUE(NAME) \ value(#NAME, NAME) #define DEF_SIMPLE_FUNCTION(NAME) \ - def(#NAME, &NAME) + m.def(#NAME, &NAME) #define DEF_SIMPLE_METHOD(NAME) \ def(#NAME, &cl::NAME) #define DEF_SIMPLE_METHOD_WITH_ARGS(NAME, ARGS) \ @@ -50,20 +46,16 @@ - -using namespace boost::python; -using namespace pyublas; - - +namespace py = pybind11; namespace { // basics ------------------------------------------------------------------- template - std::auto_ptr > construct_vector(object iterable) + std::unique_ptr > construct_vector(py::object iterable) { - std::auto_ptr > result(new std::vector()); + std::unique_ptr > result(new std::vector()); copy( stl_input_iterator(iterable), stl_input_iterator(), @@ -76,9 +68,9 @@ namespace // {{{ constants ------------------------------------------------------------ - dict symbols() + py::dict symbols() { - dict result; + py::dict result; #define EXPORT_CONSTANT(NAME) \ result[#NAME] = NAME @@ -334,15 +326,15 @@ namespace NAME##_ptrs.push_back(s.data()); #define PYTHON_FOREACH(NAME, ITERABLE) \ - BOOST_FOREACH(object NAME, \ + BOOST_FOREACH(py::object NAME, \ std::make_pair( \ - stl_input_iterator(ITERABLE), \ - stl_input_iterator())) + stl_input_iterator(ITERABLE), \ + stl_input_iterator())) - NPY_TYPES get_varlist_dtype(object varlist) + NPY_TYPES get_varlist_dtype(py::object varlist) { bool first = true; NPY_TYPES result = NPY_NOTYPE; @@ -427,7 +419,7 @@ namespace )); } - void add_option(int option, tuple values_py) + void add_option(int option, py::tuple values_py) { std::vector values; @@ -508,21 +500,21 @@ namespace TYPE NAME() const { return m_data->NAME; } #define PYVISFILE_STRING_ACCESSOR(NAME) \ - object NAME() const \ + py::object NAME() const \ { \ if (m_data) \ - return object(std::string(m_data->NAME)); \ + return py::cast(std::string(m_data->NAME)); \ else \ - return object(); \ + return py::none(); \ } #define PYVISFILE_TYPED_ARRAY_ACCESSOR(CAST_TO, NAME, SIZE) \ - object NAME() const \ + py::object NAME() const \ { \ - list py_list_result; \ + py::sequence py_list_result; \ for (unsigned i = 0; i < SIZE; ++i) \ py_list_result.append(CAST_TO(m_data->NAME[i])); \ - return tuple(py_list_result); \ + return py::make_tuple(py_list_result); \ } // }}} @@ -555,7 +547,7 @@ namespace }; #define PYVISFILE_CURVE_DATA_GETTER(COORD) \ - handle<> curve_##COORD(object py_curve) \ + handle<> curve_##COORD(py::object py_curve) \ { \ DBcurveWrapper &curve((extract(py_curve))); \ npy_intp dims[] = { curve.m_data->npts }; \ @@ -620,11 +612,11 @@ namespace PYVISFILE_STRING_ACCESSOR(mrgtree_name); }; - object quadmesh_coords(object py_quadmesh) + py::object quadmesh_coords(py::object py_quadmesh) { DBquadmeshWrapper &quadmesh((extract(py_quadmesh))); - list result; + py::list result; for (unsigned i = 0; i < quadmesh.m_data->ndims; ++i) { npy_intp dims[] = { quadmesh.m_data->dims[i] }; @@ -636,7 +628,7 @@ namespace result.append(coord_array); } - return tuple(result); + return py::make_tuple(result); } // }}} @@ -687,7 +679,7 @@ namespace // TODO: region_pnames }; - object quadvar_vals(object py_quadvar) + py::object quadvar_vals(py::object py_quadvar) { DBquadvarWrapper &quadvar((extract(py_quadvar))); @@ -702,7 +694,7 @@ namespace else ary_flags |= NPY_FARRAY; - list result; + py::list result; for (unsigned i = 0; i < quadvar.m_data->nvals; ++i) { PyArray_Descr *tp_descr; @@ -721,7 +713,7 @@ namespace result.append(val_array); } - return tuple(result); + return py::make_tuple(result); } // }}} @@ -734,30 +726,30 @@ namespace // {{{ DBtoc copy ----------------------------------------------------------- struct DBtocCopy : boost::noncopyable { - list curve_names; - list multimesh_names; - list multimeshadj_names; - list multivar_names; - list multimat_names; - list multimatspecies_names; - list csgmesh_names; - list csgvar_names; - list defvars_names; - list qmesh_names; - list qvar_names; - list ucdmesh_names; - list ucdvar_names; - list ptmesh_names; - list ptvar_names; - list mat_names; - list matspecies_names; - list var_names; - list obj_names; - list dir_names; - list array_names; - list mrgtree_names; - list groupelmap_names; - list mrgvar_names; + py::list curve_names; + py::list multimesh_names; + py::list multimeshadj_names; + py::list multivar_names; + py::list multimat_names; + py::list multimatspecies_names; + py::list csgmesh_names; + py::list csgvar_names; + py::list defvars_names; + py::list qmesh_names; + py::list qvar_names; + py::list ucdmesh_names; + py::list ucdvar_names; + py::list ptmesh_names; + py::list ptvar_names; + py::list mat_names; + py::list matspecies_names; + py::list var_names; + py::list obj_names; + py::list dir_names; + py::list array_names; + py::list mrgtree_names; + py::list groupelmap_names; + py::list mrgvar_names; }; // }}} @@ -771,7 +763,7 @@ namespace if (!obj) \ throw std::runtime_error("DBGet" #CAMEL_TYPE " failed"); \ return new DB##LOWER_TYPE##Wrapper(obj); \ - } + } @@ -873,7 +865,7 @@ namespace // {{{ ucd mesh/var template void put_ucdmesh(const char *name, - object coordnames_py, numpy_vector coords, + py::object coordnames_py, numpy_vector coords, int nzones, const char *zonel_name, const char *facel_name, DBoptlistWrapper &optlist) { @@ -920,13 +912,13 @@ namespace template void put_ucdvar_backend(const char *vname, const char *mname, - object varnames_py, object vars_py, + py::object varnames_py, py::object vars_py, /*float *mixvars[], int mixlen,*/ int centering, DBoptlistWrapper &optlist) { ensure_db_open(); - if (len(varnames_py) != len(vars_py)) + if (py::len(varnames_py) != py::len(vars_py)) PYTHON_ERROR(ValueError, "varnames and vars must have the same length"); COPY_PY_LIST(std::string, varnames); @@ -953,7 +945,7 @@ namespace } CALL_GUARDED(DBPutUcdvar, (m_dbfile, vname, mname, - len(vars_py), + py::len(vars_py), const_cast(&varnames_ptrs.front()), &vars.front(), vlength, @@ -965,7 +957,7 @@ namespace void put_ucdvar(const char *vname, const char *mname, - object varnames_py, object vars_py, + py::object varnames_py, py::object vars_py, /*float *mixvars[], int mixlen,*/ int centering, DBoptlistWrapper &optlist) { @@ -988,7 +980,7 @@ namespace // {{{ defvars - void put_defvars(std::string id, object vars_py) + void put_defvars(std::string id, py::object vars_py) { ensure_db_open(); @@ -1003,7 +995,7 @@ namespace { varnames_container.push_back(extract(entry[0])); vardefs_container.push_back(extract(entry[1])); - if (len(entry) == 2) + if (py::len(entry) == 2) { vartypes.push_back(DB_VARTYPE_SCALAR); varopts.push_back(NULL); @@ -1011,26 +1003,26 @@ namespace else { vartypes.push_back(extract(entry[2])); - if (len(entry) == 4) + if (py::len(entry) == 4) varopts.push_back(extract(entry[3])()->get_optlist()); else varopts.push_back(NULL); } } - for (int i = 0; i < len(vars_py); i++) + for (int i = 0; i < py::len(vars_py); i++) { varnames.push_back(varnames_container[i].data()); vardefs.push_back(vardefs_container[i].data()); } #if PYVISFILE_SILO_VERSION_GE(4,6,1) - CALL_GUARDED(DBPutDefvars, (m_dbfile, id.data(), len(vars_py), + CALL_GUARDED(DBPutDefvars, (m_dbfile, id.data(), py::len(vars_py), const_cast(&varnames.front()), &vartypes.front(), const_cast(&vardefs.front()), &varopts.front())); #else - CALL_GUARDED(DBPutDefvars, (m_dbfile, id.data(), len(vars_py), + CALL_GUARDED(DBPutDefvars, (m_dbfile, id.data(), py::len(vars_py), &varnames.front(), &vartypes.front(), &vardefs.front(), &varopts.front())); #endif @@ -1080,7 +1072,7 @@ namespace template void put_pointvar_backend(const char *vname, const char *mname, - object vars_py, + py::object vars_py, DBoptlistWrapper &optlist) { ensure_db_open(); @@ -1107,7 +1099,7 @@ namespace } CALL_GUARDED(DBPutPointvar, (m_dbfile, vname, mname, - len(vars_py), &vars.front(), vlength, + py::len(vars_py), &vars.front(), vlength, get_datatype(T()), optlist.get_optlist())); } @@ -1116,7 +1108,7 @@ namespace void put_pointvar(const char *vname, const char *mname, - object vars_py, DBoptlistWrapper &optlist) + py::object vars_py, DBoptlistWrapper &optlist) { switch (get_varlist_dtype(vars_py)) { @@ -1136,7 +1128,7 @@ namespace // {{{ quad mesh/var template - void put_quadmesh_backend(const char *name, object coords_py, + void put_quadmesh_backend(const char *name, py::object coords_py, int coordtype, DBoptlistWrapper &optlist) { std::vector dims; @@ -1162,7 +1154,7 @@ namespace - void put_quadmesh(const char *name, object coords_py, + void put_quadmesh(const char *name, py::object coords_py, int coordtype, DBoptlistWrapper &optlist) { switch (get_varlist_dtype(coords_py)) @@ -1184,13 +1176,13 @@ namespace template void put_quadvar_backend(const char *vname, const char *mname, - object varnames_py, object vars_py, object dims_py, + py::object varnames_py, py::object vars_py, py::object dims_py, /*float *mixvar, int mixlen, */int centering, DBoptlistWrapper &optlist) { COPY_PY_LIST(int, dims); - if (len(varnames_py) != len(vars_py)) + if (py::len(varnames_py) != py::len(vars_py)) PYTHON_ERROR(ValueError, "varnames and vars must have the same length"); COPY_PY_LIST(std::string, varnames); @@ -1232,7 +1224,7 @@ namespace void put_quadvar(const char *vname, const char *mname, - object varnames_py, object vars_py, object dims_py, + py::object varnames_py, py::object vars_py, py::object dims_py, /*float *mixvar, int mixlen, */int centering, DBoptlistWrapper &optlist) { @@ -1256,7 +1248,7 @@ namespace template void put_quadvar1(const char *vname, const char *mname, - numpy_vector var, object dims_py, + numpy_vector var, py::object dims_py, /*float *mixvar, int mixlen, */int centering, DBoptlistWrapper &optlist) { @@ -1279,7 +1271,7 @@ namespace // {{{ multi mesh/var - void put_multimesh(const char *name, object names_and_types, + void put_multimesh(const char *name, py::object names_and_types, DBoptlistWrapper &optlist) { ensure_db_open(); @@ -1304,7 +1296,7 @@ namespace - void put_multivar(const char *name, object names_and_types, + void put_multivar(const char *name, py::object names_and_types, DBoptlistWrapper &optlist) { ensure_db_open(); @@ -1359,7 +1351,7 @@ namespace DBtocCopy *get_toc() { DBtoc *toc = DBGetToc(m_dbfile); - std::auto_ptr result(new DBtocCopy()); + std::unique_ptr result(new DBtocCopy()); #define PYVISFILE_COPY_TOC_LIST(NAME, CNT) \ for (int i = 0; i < toc->CNT; ++i) \ @@ -1410,12 +1402,12 @@ namespace - tuple get_silo_version() + py::tuple get_silo_version() { #if PYVISFILE_SILO_VERSION_GE(4,6,1) - return make_tuple(SILO_VERS_MAJ, SILO_VERS_MIN, SILO_VERS_PAT); + return py::make_tuple(SILO_VERS_MAJ, SILO_VERS_MIN, SILO_VERS_PAT); #else - return make_tuple(4,5,1); + return py::make_tuple(4,5,1); #endif } @@ -1432,11 +1424,20 @@ namespace // {{{ main wrapper function -------------------------------------------------- -BOOST_PYTHON_MODULE(_internal) +static bool import_numpy_helper() { + import_array1(false); + return true; +} + +PYBIND11_MODULE(_internal, m) +{ + if (!import_numpy_helper()) + throw py::error_already_set(); + DEF_SIMPLE_FUNCTION(symbols); - enum_("DBdatatype") + py::enum_(m, "DBdatatype") .ENUM_VALUE(DB_INT) .ENUM_VALUE(DB_SHORT) .ENUM_VALUE(DB_LONG) @@ -1449,7 +1450,7 @@ BOOST_PYTHON_MODULE(_internal) #endif ; - enum_("DBObjectType") + py::enum_(m, "DBObjectType") .ENUM_VALUE(DB_INVALID_OBJECT) .ENUM_VALUE(DB_QUADMESH) .ENUM_VALUE(DB_QUADVAR) @@ -1483,8 +1484,9 @@ BOOST_PYTHON_MODULE(_internal) { typedef DBfileWrapper cl; - class_("DBFile", init()) - .def(init()) + py::class_(m, "DBFile") + .def(py::init()) + .def(py::init()) .DEF_SIMPLE_METHOD(close) .DEF_SIMPLE_METHOD(put_zonelist) @@ -1530,17 +1532,18 @@ BOOST_PYTHON_MODULE(_internal) { typedef DBoptlistWrapper cl; - class_("DBOptlist", init()) + py::class_(m, "DBOptlist") + .def(py::init()) .def("add_int_option", (void (cl::*)(int, int)) &cl::add_option) .def("add_option", (void (cl::*)(int, double)) &cl::add_option) .def("add_option", (void (cl::*)(int, const std::string &)) &cl::add_option) - .def("add_option", (void (cl::*)(int, tuple)) &cl::add_option) + .def("add_option", (void (cl::*)(int, py::tuple)) &cl::add_option) ; } { typedef DBcurveWrapper cl; - class_("DBCurve", no_init) + py::class_(m, "DBCurve") .DEF_SIMPLE_RO_PROPERTY(id) .DEF_SIMPLE_RO_PROPERTY(origin) .DEF_SIMPLE_RO_PROPERTY(title) @@ -1558,7 +1561,7 @@ BOOST_PYTHON_MODULE(_internal) { typedef DBquadmeshWrapper cl; - class_("DBQuadMesh", no_init) + py::class_("DBQuadMesh") .DEF_SIMPLE_RO_PROPERTY(id) .DEF_SIMPLE_RO_PROPERTY(block_no) .DEF_SIMPLE_RO_PROPERTY(group_no) @@ -1593,7 +1596,7 @@ BOOST_PYTHON_MODULE(_internal) { typedef DBquadvarWrapper cl; - class_("DBQuadVar", no_init) + py::class_(m, "DBQuadVar") .DEF_SIMPLE_RO_PROPERTY(id) .DEF_SIMPLE_RO_PROPERTY(name) .DEF_SIMPLE_RO_PROPERTY(units) @@ -1625,7 +1628,7 @@ BOOST_PYTHON_MODULE(_internal) { typedef DBtocCopy cl; - class_("DBToc", no_init) + py::class_(m, "DBToc") .DEF_SIMPLE_RO_PROPERTY(curve_names) .DEF_SIMPLE_RO_PROPERTY(multimesh_names) .DEF_SIMPLE_RO_PROPERTY(multimeshadj_names) @@ -1654,9 +1657,9 @@ BOOST_PYTHON_MODULE(_internal) } { typedef std::vector cl; - class_("IntVector") + py::class_(m, "IntVector") .def("__init__", make_constructor(construct_vector)) - .def("reserve", &cl::reserve, arg("advised_size")) + .def("reserve", &cl::reserve, py::arg("advised_size")) .def(vector_indexing_suite ()) ; } -- GitLab From f225af1da8d57149d3424763ec5384de57bb1b58 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 30 Sep 2020 21:51:41 -0500 Subject: [PATCH 02/19] change extract to cast --- src/wrapper/wrap_silo.cpp | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/wrapper/wrap_silo.cpp b/src/wrapper/wrap_silo.cpp index 0d8455a..5977c6d 100644 --- a/src/wrapper/wrap_silo.cpp +++ b/src/wrapper/wrap_silo.cpp @@ -53,9 +53,9 @@ namespace { // basics ------------------------------------------------------------------- template - std::unique_ptr > construct_vector(py::object iterable) + std::unique_ptr> construct_vector(py::object iterable) { - std::unique_ptr > result(new std::vector()); + std::unique_ptr> result(new std::vector()); copy( stl_input_iterator(iterable), stl_input_iterator(), @@ -425,7 +425,7 @@ namespace PYTHON_FOREACH(value_py, values_py) { - int value = extract (value_py); + int value = valye.cast(); values.push_back(value); } CALL_GUARDED(DBAddOption,(m_optlist, option, @@ -549,7 +549,7 @@ namespace #define PYVISFILE_CURVE_DATA_GETTER(COORD) \ handle<> curve_##COORD(py::object py_curve) \ { \ - DBcurveWrapper &curve((extract(py_curve))); \ + DBcurveWrapper &curve(py_curve.cast()); \ npy_intp dims[] = { curve.m_data->npts }; \ handle<> result(PyArray_SimpleNewFromData(1, dims, \ silo_typenum_to_numpy_typenum(curve.m_data->datatype), \ @@ -614,7 +614,7 @@ namespace py::object quadmesh_coords(py::object py_quadmesh) { - DBquadmeshWrapper &quadmesh((extract(py_quadmesh))); + DBquadmeshWrapper &quadmesh(py_quadmesh.cast()); py::list result; for (unsigned i = 0; i < quadmesh.m_data->ndims; ++i) @@ -681,7 +681,7 @@ namespace py::object quadvar_vals(py::object py_quadvar) { - DBquadvarWrapper &quadvar((extract(py_quadvar))); + DBquadvarWrapper &quadvar(py_quadvar.cast()); npy_intp dims[3]; @@ -930,7 +930,7 @@ namespace PYTHON_FOREACH(var_py, vars_py) { - numpy_vector v = extract >(var_py); + numpy_vector v = var_py.cast>(); if (first) { vlength = v.size(); @@ -993,8 +993,8 @@ namespace PYTHON_FOREACH(entry, vars_py) { - varnames_container.push_back(extract(entry[0])); - vardefs_container.push_back(extract(entry[1])); + varnames_container.push_back(entry[0].cast()); + vardefs_container.push_back(entry[1].cast()); if (py::len(entry) == 2) { vartypes.push_back(DB_VARTYPE_SCALAR); @@ -1002,9 +1002,9 @@ namespace } else { - vartypes.push_back(extract(entry[2])); + vartypes.push_back(entry[2].cast()); if (py::len(entry) == 4) - varopts.push_back(extract(entry[3])()->get_optlist()); + varopts.push_back(entry[3].cast()->get_optlist()); else varopts.push_back(NULL); } @@ -1083,7 +1083,7 @@ namespace PYTHON_FOREACH(var_py, vars_py) { - numpy_vector v = extract >(var_py); + numpy_vector v = var_py.cast>(); if (first) { vlength = v.size(); @@ -1136,7 +1136,7 @@ namespace PYTHON_FOREACH(coord_dim_py, coords_py) { - numpy_vector coord_dim = extract >(coord_dim_py); + numpy_vector coord_dim = coord_dim_py.cast>(); dims.push_back(coord_dim.size()); coords.push_back((float *) coord_dim.data().data()); } @@ -1194,7 +1194,7 @@ namespace PYTHON_FOREACH(var_py, vars_py) { - numpy_vector v = extract >(var_py); + numpy_vector v = var_py.cast>(); if (first) { vlength = v.size(); @@ -1281,8 +1281,8 @@ namespace PYTHON_FOREACH(name_and_type, names_and_types) { - meshnames.push_back(extract(name_and_type[0])); - meshtypes.push_back(extract(name_and_type[1])); + meshnames.push_back(name_and_type[0].cast()); + meshtypes.push_back(name_and_type[1].cast()); } MAKE_STRING_POINTER_VECTOR(meshnames) @@ -1306,8 +1306,8 @@ namespace PYTHON_FOREACH(name_and_type, names_and_types) { - varnames.push_back(extract(name_and_type[0])); - vartypes.push_back(extract(name_and_type[1])); + varnames.push_back(name_and_type[0].cast()); + vartypes.push_back(name_and_type[1].cast()); } MAKE_STRING_POINTER_VECTOR(varnames) -- GitLab From 50c196a84cb56825fbdb1bcdc4859bf61780f641 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 30 Sep 2020 21:56:18 -0500 Subject: [PATCH 03/19] rename variables --- src/wrapper/wrap_silo.cpp | 88 +++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/src/wrapper/wrap_silo.cpp b/src/wrapper/wrap_silo.cpp index 5977c6d..55f9388 100644 --- a/src/wrapper/wrap_silo.cpp +++ b/src/wrapper/wrap_silo.cpp @@ -419,13 +419,13 @@ namespace )); } - void add_option(int option, py::tuple values_py) + void add_option(int option, py::tuple py_values) { std::vector values; - PYTHON_FOREACH(value_py, values_py) + PYTHON_FOREACH(py_value, py_values) { - int value = valye.cast(); + int value = py_value.cast(); values.push_back(value); } CALL_GUARDED(DBAddOption,(m_optlist, option, @@ -865,7 +865,7 @@ namespace // {{{ ucd mesh/var template void put_ucdmesh(const char *name, - py::object coordnames_py, numpy_vector coords, + py::object py_coordnames, numpy_vector coords, int nzones, const char *zonel_name, const char *facel_name, DBoptlistWrapper &optlist) { @@ -912,13 +912,13 @@ namespace template void put_ucdvar_backend(const char *vname, const char *mname, - py::object varnames_py, py::object vars_py, + py::object py_varnames, py::object py_vars, /*float *mixvars[], int mixlen,*/ int centering, DBoptlistWrapper &optlist) { ensure_db_open(); - if (py::len(varnames_py) != py::len(vars_py)) + if (py::len(py_varnames) != py::len(py_vars)) PYTHON_ERROR(ValueError, "varnames and vars must have the same length"); COPY_PY_LIST(std::string, varnames); @@ -928,9 +928,9 @@ namespace bool first = true; int vlength = 0; - PYTHON_FOREACH(var_py, vars_py) + PYTHON_FOREACH(py_var, py_vars) { - numpy_vector v = var_py.cast>(); + numpy_vector v = py_var.cast>(); if (first) { vlength = v.size(); @@ -945,7 +945,7 @@ namespace } CALL_GUARDED(DBPutUcdvar, (m_dbfile, vname, mname, - py::len(vars_py), + py::len(py_vars), const_cast(&varnames_ptrs.front()), &vars.front(), vlength, @@ -957,18 +957,18 @@ namespace void put_ucdvar(const char *vname, const char *mname, - py::object varnames_py, py::object vars_py, + py::object py_varnames, py::object py_vars, /*float *mixvars[], int mixlen,*/ int centering, DBoptlistWrapper &optlist) { - switch (get_varlist_dtype(vars_py)) + switch (get_varlist_dtype(py_vars)) { case NPY_FLOAT: - put_ucdvar_backend(vname, mname, varnames_py, vars_py, + put_ucdvar_backend(vname, mname, py_varnames, py_vars, centering, optlist); break; case NPY_DOUBLE: - put_ucdvar_backend(vname, mname, varnames_py, vars_py, + put_ucdvar_backend(vname, mname, py_varnames, py_vars, centering, optlist); break; default: @@ -980,7 +980,7 @@ namespace // {{{ defvars - void put_defvars(std::string id, py::object vars_py) + void put_defvars(std::string id, py::object py_vars) { ensure_db_open(); @@ -991,7 +991,7 @@ namespace std::vector vartypes; std::vector varopts; - PYTHON_FOREACH(entry, vars_py) + PYTHON_FOREACH(entry, py_vars) { varnames_container.push_back(entry[0].cast()); vardefs_container.push_back(entry[1].cast()); @@ -1010,19 +1010,19 @@ namespace } } - for (int i = 0; i < py::len(vars_py); i++) + for (int i = 0; i < py::len(py_vars); i++) { varnames.push_back(varnames_container[i].data()); vardefs.push_back(vardefs_container[i].data()); } #if PYVISFILE_SILO_VERSION_GE(4,6,1) - CALL_GUARDED(DBPutDefvars, (m_dbfile, id.data(), py::len(vars_py), + CALL_GUARDED(DBPutDefvars, (m_dbfile, id.data(), py::len(py_vars), const_cast(&varnames.front()), &vartypes.front(), const_cast(&vardefs.front()), &varopts.front())); #else - CALL_GUARDED(DBPutDefvars, (m_dbfile, id.data(), py::len(vars_py), + CALL_GUARDED(DBPutDefvars, (m_dbfile, id.data(), py::len(py_vars), &varnames.front(), &vartypes.front(), &vardefs.front(), &varopts.front())); #endif @@ -1072,7 +1072,7 @@ namespace template void put_pointvar_backend(const char *vname, const char *mname, - py::object vars_py, + py::object py_vars, DBoptlistWrapper &optlist) { ensure_db_open(); @@ -1081,9 +1081,9 @@ namespace bool first = true; int vlength = 0; - PYTHON_FOREACH(var_py, vars_py) + PYTHON_FOREACH(py_var, py_vars) { - numpy_vector v = var_py.cast>(); + numpy_vector v = py_var.cast>(); if (first) { vlength = v.size(); @@ -1099,7 +1099,7 @@ namespace } CALL_GUARDED(DBPutPointvar, (m_dbfile, vname, mname, - py::len(vars_py), &vars.front(), vlength, + py::len(py_vars), &vars.front(), vlength, get_datatype(T()), optlist.get_optlist())); } @@ -1108,15 +1108,15 @@ namespace void put_pointvar(const char *vname, const char *mname, - py::object vars_py, DBoptlistWrapper &optlist) + py::object py_vars, DBoptlistWrapper &optlist) { - switch (get_varlist_dtype(vars_py)) + switch (get_varlist_dtype(py_vars)) { case NPY_FLOAT: - put_pointvar_backend(vname, mname, vars_py, optlist); + put_pointvar_backend(vname, mname, py_vars, optlist); break; case NPY_DOUBLE: - put_pointvar_backend(vname, mname, vars_py, optlist); + put_pointvar_backend(vname, mname, py_vars, optlist); break; default: PYUBLAS_PYERROR(TypeError, "unsupported variable type"); @@ -1128,15 +1128,15 @@ namespace // {{{ quad mesh/var template - void put_quadmesh_backend(const char *name, py::object coords_py, + void put_quadmesh_backend(const char *name, py::object py_coords, int coordtype, DBoptlistWrapper &optlist) { std::vector dims; std::vector coords; - PYTHON_FOREACH(coord_dim_py, coords_py) + PYTHON_FOREACH(py_coord_dim, py_coords) { - numpy_vector coord_dim = coord_dim_py.cast>(); + numpy_vector coord_dim = py_coord_dim.cast>(); dims.push_back(coord_dim.size()); coords.push_back((float *) coord_dim.data().data()); } @@ -1154,16 +1154,16 @@ namespace - void put_quadmesh(const char *name, py::object coords_py, + void put_quadmesh(const char *name, py::object py_coords, int coordtype, DBoptlistWrapper &optlist) { - switch (get_varlist_dtype(coords_py)) + switch (get_varlist_dtype(py_coords)) { case NPY_FLOAT: - put_quadmesh_backend(name, coords_py, coordtype, optlist); + put_quadmesh_backend(name, py_coords, coordtype, optlist); break; case NPY_DOUBLE: - put_quadmesh_backend(name, coords_py, coordtype, optlist); + put_quadmesh_backend(name, py_coords, coordtype, optlist); break; default: PYUBLAS_PYERROR(TypeError, "unsupported variable type"); @@ -1176,13 +1176,13 @@ namespace template void put_quadvar_backend(const char *vname, const char *mname, - py::object varnames_py, py::object vars_py, py::object dims_py, + py::object py_varnames, py::object py_vars, py::object py_dims, /*float *mixvar, int mixlen, */int centering, DBoptlistWrapper &optlist) { COPY_PY_LIST(int, dims); - if (py::len(varnames_py) != py::len(vars_py)) + if (py::len(py_varnames) != py::len(py_vars)) PYTHON_ERROR(ValueError, "varnames and vars must have the same length"); COPY_PY_LIST(std::string, varnames); @@ -1192,9 +1192,9 @@ namespace bool first = true; int vlength = 0; - PYTHON_FOREACH(var_py, vars_py) + PYTHON_FOREACH(py_var, py_vars) { - numpy_vector v = var_py.cast>(); + numpy_vector v = py_var.cast>(); if (first) { vlength = v.size(); @@ -1224,19 +1224,19 @@ namespace void put_quadvar(const char *vname, const char *mname, - py::object varnames_py, py::object vars_py, py::object dims_py, + py::object py_varnames, py::object py_vars, py::object py_dims, /*float *mixvar, int mixlen, */int centering, DBoptlistWrapper &optlist) { - switch (get_varlist_dtype(vars_py)) + switch (get_varlist_dtype(py_vars)) { case NPY_FLOAT: - put_quadvar_backend(vname, mname, varnames_py, vars_py, - dims_py, centering, optlist); + put_quadvar_backend(vname, mname, py_varnames, py_vars, + py_dims, centering, optlist); break; case NPY_DOUBLE: - put_quadvar_backend(vname, mname, varnames_py, vars_py, - dims_py, centering, optlist); + put_quadvar_backend(vname, mname, py_varnames, py_vars, + py_dims, centering, optlist); break; default: PYUBLAS_PYERROR(TypeError, "unsupported variable type"); @@ -1248,7 +1248,7 @@ namespace template void put_quadvar1(const char *vname, const char *mname, - numpy_vector var, py::object dims_py, + numpy_vector var, py::object py_dims, /*float *mixvar, int mixlen, */int centering, DBoptlistWrapper &optlist) { -- GitLab From bac796be81968d3eacf2bcbc354c82861b2b9d94 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 30 Sep 2020 21:59:25 -0500 Subject: [PATCH 04/19] add modeline --- src/wrapper/wrap_silo.cpp | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/wrapper/wrap_silo.cpp b/src/wrapper/wrap_silo.cpp index 55f9388..2155d74 100644 --- a/src/wrapper/wrap_silo.cpp +++ b/src/wrapper/wrap_silo.cpp @@ -16,42 +16,41 @@ #ifdef SILO_VERS_MAJ -#define PYVISFILE_SILO_VERSION_GE(Maj,Min,Rel) \ - (((SILO_VERS_MAJ==Maj) && (SILO_VERS_MIN==Min) && (SILO_VERS_PAT>=Rel)) || \ - ((SILO_VERS_MAJ==Maj) && (SILO_VERS_MIN>Min)) || \ - (SILO_VERS_MAJ>Maj)) +#define PYVISFILE_SILO_VERSION_GE(Maj, Min, Rel) \ + (((SILO_VERS_MAJ == Maj) && (SILO_VERS_MIN == Min) && (SILO_VERS_PAT >= Rel)) || \ + ((SILO_VERS_MAJ == Maj) && (SILO_VERS_MIN > Min)) || \ + (SILO_VERS_MAJ > Maj)) #else -#define PYVISFILE_SILO_VERSION_GE(Maj,Min,Rel) 0 +#define PYVISFILE_SILO_VERSION_GE(Maj, Min, Rel) 0 #endif #define PYTHON_ERROR(TYPE, REASON) \ { \ PyErr_SetString(PyExc_##TYPE, REASON); \ - throw boost::python::error_already_set(); \ + throw py::error_already_set(); \ } - - - #define ENUM_VALUE(NAME) \ value(#NAME, NAME) + #define DEF_SIMPLE_FUNCTION(NAME) \ m.def(#NAME, &NAME) + #define DEF_SIMPLE_METHOD(NAME) \ def(#NAME, &cl::NAME) + #define DEF_SIMPLE_METHOD_WITH_ARGS(NAME, ARGS) \ def(#NAME, &cl::NAME, args ARGS) + #define DEF_SIMPLE_RO_PROPERTY(NAME) \ add_property(#NAME, &cl::NAME) - - namespace py = pybind11; - namespace { - // basics ------------------------------------------------------------------- + // {{{ helpers + template std::unique_ptr> construct_vector(py::object iterable) { @@ -63,7 +62,7 @@ namespace return result; } - + // }}} @@ -972,7 +971,7 @@ namespace centering, optlist); break; default: - PYUBLAS_PYERROR(TypeError, "unsupported variable type"); + PYTHON_ERROR(TypeError, "unsupported variable type"); } } @@ -1119,7 +1118,7 @@ namespace put_pointvar_backend(vname, mname, py_vars, optlist); break; default: - PYUBLAS_PYERROR(TypeError, "unsupported variable type"); + PYTHON_ERROR(TypeError, "unsupported variable type"); } } @@ -1166,7 +1165,7 @@ namespace put_quadmesh_backend(name, py_coords, coordtype, optlist); break; default: - PYUBLAS_PYERROR(TypeError, "unsupported variable type"); + PYTHON_ERROR(TypeError, "unsupported variable type"); } } @@ -1239,7 +1238,7 @@ namespace py_dims, centering, optlist); break; default: - PYUBLAS_PYERROR(TypeError, "unsupported variable type"); + PYTHON_ERROR(TypeError, "unsupported variable type"); } } @@ -1674,4 +1673,4 @@ PYBIND11_MODULE(_internal, m) // }}} -// vim: foldmethod=marker +// vim: ts=2:sw=2:foldmethod=marker -- GitLab From 74015384e7e6da28cc8d3578fbdf83be3f9e8f92 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 30 Sep 2020 22:09:25 -0500 Subject: [PATCH 05/19] use c++ for --- src/wrapper/wrap_silo.cpp | 62 +++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 35 deletions(-) diff --git a/src/wrapper/wrap_silo.cpp b/src/wrapper/wrap_silo.cpp index 2155d74..f509635 100644 --- a/src/wrapper/wrap_silo.cpp +++ b/src/wrapper/wrap_silo.cpp @@ -1,20 +1,15 @@ // PyVisfile - A Python wrapper around Silo // Copyright (C) 2007 Andreas Kloeckner - #include #include -#include -#include #include #include #include #include - - #ifdef SILO_VERS_MAJ #define PYVISFILE_SILO_VERSION_GE(Maj, Min, Rel) \ (((SILO_VERS_MAJ == Maj) && (SILO_VERS_MIN == Min) && (SILO_VERS_PAT >= Rel)) || \ @@ -24,6 +19,8 @@ #define PYVISFILE_SILO_VERSION_GE(Maj, Min, Rel) 0 #endif +// {{{ macros + #define PYTHON_ERROR(TYPE, REASON) \ { \ PyErr_SetString(PyExc_##TYPE, REASON); \ @@ -45,6 +42,8 @@ #define DEF_SIMPLE_RO_PROPERTY(NAME) \ add_property(#NAME, &cl::NAME) +// }}} + namespace py = pybind11; namespace @@ -320,25 +319,18 @@ namespace back_inserter(NAME)); \ #define MAKE_STRING_POINTER_VECTOR(NAME) \ +{ \ std::vector NAME##_ptrs; \ - BOOST_FOREACH(const std::string &s, NAME) \ - NAME##_ptrs.push_back(s.data()); - -#define PYTHON_FOREACH(NAME, ITERABLE) \ - BOOST_FOREACH(py::object NAME, \ - std::make_pair( \ - stl_input_iterator(ITERABLE), \ - stl_input_iterator())) - - - + for(const std::string &s: NAME) \ + NAME##_ptrs.push_back(s.data()); \ +} - NPY_TYPES get_varlist_dtype(py::object varlist) + NPY_TYPES get_varlist_dtype(py::sequence varlist) { bool first = true; NPY_TYPES result = NPY_NOTYPE; - PYTHON_FOREACH(var, varlist) + for(py::object var: varlist) { if (!PyArray_Check(var.ptr())) PYTHON_ERROR(TypeError, "component of variable list is not numpy array"); @@ -363,7 +355,7 @@ namespace { private: DBoptlist *m_optlist; - boost::scoped_array m_option_storage; + char *m_option_storage; unsigned m_option_storage_size; unsigned m_option_storage_occupied; @@ -422,7 +414,7 @@ namespace { std::vector values; - PYTHON_FOREACH(py_value, py_values) + for(py::object py_value: py_values) { int value = py_value.cast(); values.push_back(value); @@ -911,7 +903,7 @@ namespace template void put_ucdvar_backend(const char *vname, const char *mname, - py::object py_varnames, py::object py_vars, + py::sequence py_varnames, py::sequence py_vars, /*float *mixvars[], int mixlen,*/ int centering, DBoptlistWrapper &optlist) { @@ -927,7 +919,7 @@ namespace bool first = true; int vlength = 0; - PYTHON_FOREACH(py_var, py_vars) + for(py::object py_var: py_vars) { numpy_vector v = py_var.cast>(); if (first) @@ -979,7 +971,7 @@ namespace // {{{ defvars - void put_defvars(std::string id, py::object py_vars) + void put_defvars(std::string id, py::sequence py_vars) { ensure_db_open(); @@ -990,7 +982,7 @@ namespace std::vector vartypes; std::vector varopts; - PYTHON_FOREACH(entry, py_vars) + for(py::object entry: py_vars) { varnames_container.push_back(entry[0].cast()); vardefs_container.push_back(entry[1].cast()); @@ -1071,7 +1063,7 @@ namespace template void put_pointvar_backend(const char *vname, const char *mname, - py::object py_vars, + py::sequence py_vars, DBoptlistWrapper &optlist) { ensure_db_open(); @@ -1080,7 +1072,7 @@ namespace bool first = true; int vlength = 0; - PYTHON_FOREACH(py_var, py_vars) + for(py::object py_var: py_vars) { numpy_vector v = py_var.cast>(); if (first) @@ -1127,13 +1119,13 @@ namespace // {{{ quad mesh/var template - void put_quadmesh_backend(const char *name, py::object py_coords, + void put_quadmesh_backend(const char *name, py::list py_coords, int coordtype, DBoptlistWrapper &optlist) { std::vector dims; std::vector coords; - PYTHON_FOREACH(py_coord_dim, py_coords) + for(py::object py_coord_dim: py_coords) { numpy_vector coord_dim = py_coord_dim.cast>(); dims.push_back(coord_dim.size()); @@ -1175,8 +1167,8 @@ namespace template void put_quadvar_backend(const char *vname, const char *mname, - py::object py_varnames, py::object py_vars, py::object py_dims, - /*float *mixvar, int mixlen, */int centering, + py::sequence py_varnames, py::sequence py_vars, py::sequence py_dims, + /* float *mixvar, int mixlen, */ int centering, DBoptlistWrapper &optlist) { COPY_PY_LIST(int, dims); @@ -1191,7 +1183,7 @@ namespace bool first = true; int vlength = 0; - PYTHON_FOREACH(py_var, py_vars) + for(py::object py_var: py_vars) { numpy_vector v = py_var.cast>(); if (first) @@ -1270,7 +1262,7 @@ namespace // {{{ multi mesh/var - void put_multimesh(const char *name, py::object names_and_types, + void put_multimesh(const char *name, py::sequence names_and_types, DBoptlistWrapper &optlist) { ensure_db_open(); @@ -1278,7 +1270,7 @@ namespace std::vector meshnames; std::vector meshtypes; - PYTHON_FOREACH(name_and_type, names_and_types) + for(py::object name_and_type: names_and_types) { meshnames.push_back(name_and_type[0].cast()); meshtypes.push_back(name_and_type[1].cast()); @@ -1295,7 +1287,7 @@ namespace - void put_multivar(const char *name, py::object names_and_types, + void put_multivar(const char *name, py::sequence names_and_types, DBoptlistWrapper &optlist) { ensure_db_open(); @@ -1303,7 +1295,7 @@ namespace std::vector varnames; std::vector vartypes; - PYTHON_FOREACH(name_and_type, names_and_types) + for(py::object name_and_type: names_and_types) { varnames.push_back(name_and_type[0].cast()); vartypes.push_back(name_and_type[1].cast()); -- GitLab From ec1de0c9eef5a53c6f897f3cb56b16fecaccb5af Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 30 Sep 2020 22:14:08 -0500 Subject: [PATCH 06/19] add noncopyable from pyopencl --- src/wrapper/wrap_silo.cpp | 52 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/wrapper/wrap_silo.cpp b/src/wrapper/wrap_silo.cpp index f509635..e6354cc 100644 --- a/src/wrapper/wrap_silo.cpp +++ b/src/wrapper/wrap_silo.cpp @@ -61,11 +61,21 @@ namespace return result; } - // }}} + // https://stackoverflow.com/a/44175911 + class noncopyable { + public: + noncopyable() = default; + ~noncopyable() = default; + + private: + noncopyable(const noncopyable&) = delete; + noncopyable& operator=(const noncopyable&) = delete; + }; + // }}} + // {{{ constants - // {{{ constants ------------------------------------------------------------ py::dict symbols() { py::dict result; @@ -347,11 +357,9 @@ namespace return result; } + // {{{ DBoptlist wrapper - - - // {{{ DBoptlist wrapper ---------------------------------------------------- - class DBoptlistWrapper : boost::noncopyable + class DBoptlistWrapper: noncopyable { private: DBoptlist *m_optlist; @@ -512,7 +520,7 @@ namespace // {{{ curve wrapper - class DBcurveWrapper : boost::noncopyable + class DBcurveWrapper: noncopyable { public: DBcurve *m_data; @@ -557,7 +565,7 @@ namespace // {{{ quad mesh wrapper - class DBquadmeshWrapper : boost::noncopyable + class DBquadmeshWrapper: noncopyable { public: DBquadmesh *m_data; @@ -626,7 +634,7 @@ namespace // {{{ quad var wrapper - class DBquadvarWrapper : boost::noncopyable + class DBquadvarWrapper: noncopyable { public: DBquadvar *m_data; @@ -714,8 +722,9 @@ namespace - // {{{ DBtoc copy ----------------------------------------------------------- - struct DBtocCopy : boost::noncopyable + // {{{ DBtoc copy + + struct DBtocCopy : noncopyable { py::list curve_names; py::list multimesh_names; @@ -745,7 +754,7 @@ namespace // }}} - // {{{ DBfile wrapper ------------------------------------------------------- + // {{{ DBfile wrapper #define PYVISFILE_DBFILE_GET_WRAPPER(LOWER_TYPE, CAMEL_TYPE) \ DB##LOWER_TYPE##Wrapper *get_##LOWER_TYPE(const char *name) \ @@ -759,7 +768,7 @@ namespace - class DBfileWrapper : boost::noncopyable + class DBfileWrapper: noncopyable { public: // {{{ construction and administrativa @@ -928,10 +937,7 @@ namespace first = false; } else if (vlength != int(v.size())) - PYTHON_ERROR(ValueError, - boost::str(boost::format( - "field components of '%s' need to have matching lengths") - % vname).c_str()); + PYTHON_ERROR(ValueError, "field components need to have matching lengths"); vars.push_back((float *) v.data().data()); } @@ -1081,10 +1087,7 @@ namespace first = false; } else if (vlength != int(v.size())) - PYTHON_ERROR(ValueError, - boost::str(boost::format( - "field components of '%s' need to have matching lengths") - % vname).c_str()); + PYTHON_ERROR(ValueError, "field components need to have matching lengths"); vars.push_back((float *) v.data().data()); } @@ -1192,10 +1195,7 @@ namespace first = false; } else if (vlength != int(v.size())) - PYTHON_ERROR(ValueError, - boost::str(boost::format( - "field components of '%s' need to have matching lengths") - % vname).c_str()); + PYTHON_ERROR(ValueError, "field components need to have matching lengths"); vars.push_back((float *) v.data().data()); } @@ -1619,7 +1619,7 @@ PYBIND11_MODULE(_internal, m) { typedef DBtocCopy cl; - py::class_(m, "DBToc") + py::class_(m, "DBToc") .DEF_SIMPLE_RO_PROPERTY(curve_names) .DEF_SIMPLE_RO_PROPERTY(multimesh_names) .DEF_SIMPLE_RO_PROPERTY(multimeshadj_names) -- GitLab From c16bc174c25ec5c949d3ea386ceb85e8a6be9d06 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 30 Sep 2020 22:47:57 -0500 Subject: [PATCH 07/19] port IntVector --- src/wrapper/wrap_silo.cpp | 40 ++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/wrapper/wrap_silo.cpp b/src/wrapper/wrap_silo.cpp index e6354cc..2543267 100644 --- a/src/wrapper/wrap_silo.cpp +++ b/src/wrapper/wrap_silo.cpp @@ -46,21 +46,13 @@ namespace py = pybind11; +// NOTE: for IntVector +PYBIND11_MAKE_OPAQUE(std::vector); + namespace { // {{{ helpers - template - std::unique_ptr> construct_vector(py::object iterable) - { - std::unique_ptr> result(new std::vector()); - copy( - stl_input_iterator(iterable), - stl_input_iterator(), - back_inserter(*result)); - return result; - } - // https://stackoverflow.com/a/44175911 class noncopyable { public: @@ -322,11 +314,11 @@ namespace throw std::runtime_error(#NAME " failed"); #define COPY_PY_LIST(TYPE, NAME) \ - std::vector NAME; \ - std::copy( \ - stl_input_iterator(NAME##_py), \ - stl_input_iterator(), \ - back_inserter(NAME)); \ + { \ + std::vector NAME; \ + for (auto it: py_##NAME) \ + NAME.push_back(it.cast()); \ + } #define MAKE_STRING_POINTER_VECTOR(NAME) \ { \ @@ -1646,12 +1638,22 @@ PYBIND11_MODULE(_internal, m) .DEF_SIMPLE_RO_PROPERTY(mrgvar_names) ; } + { typedef std::vector cl; py::class_(m, "IntVector") - .def("__init__", make_constructor(construct_vector)) - .def("reserve", &cl::reserve, py::arg("advised_size")) - .def(vector_indexing_suite ()) + .def(py::init<>()) + .def("reserve", &cl::reserve, + py::arg("advised_size")) + .def("__len__", &cl.size) + .def("append", &cl::push_back, + py::arg("value")) + .def("extend", [](std::vector &self, const py::sequence &py_other) + { + for(const py::object &item:: py_other) + self.push_back(item.cast()); + }, + py::arg("iterable")) ; } -- GitLab From 4f030e9a817b1bdaa0cc63accf7156d34bb7a3d7 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 30 Sep 2020 23:09:40 -0500 Subject: [PATCH 08/19] fix some porting errors --- src/wrapper/wrap_silo.cpp | 59 +++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/src/wrapper/wrap_silo.cpp b/src/wrapper/wrap_silo.cpp index 2543267..96c7380 100644 --- a/src/wrapper/wrap_silo.cpp +++ b/src/wrapper/wrap_silo.cpp @@ -314,18 +314,14 @@ namespace throw std::runtime_error(#NAME " failed"); #define COPY_PY_LIST(TYPE, NAME) \ - { \ std::vector NAME; \ for (auto it: py_##NAME) \ NAME.push_back(it.cast()); \ - } #define MAKE_STRING_POINTER_VECTOR(NAME) \ -{ \ std::vector NAME##_ptrs; \ for(const std::string &s: NAME) \ NAME##_ptrs.push_back(s.data()); \ -} NPY_TYPES get_varlist_dtype(py::sequence varlist) { @@ -371,7 +367,7 @@ namespace } ~DBoptlistWrapper() { - CALL_GUARDED(DBFreeOptlist, (m_optlist)); + DBFreeOptlist(m_optlist); } void add_option(int option, int value) @@ -414,11 +410,11 @@ namespace { std::vector values; - for(py::object py_value: py_values) + for(size_t i = 0; i < py::len(py_values); ++i) { - int value = py_value.cast(); - values.push_back(value); + values.push_back(py_values[i].cast()); } + CALL_GUARDED(DBAddOption,(m_optlist, option, add_storage_data((void *) &values.front(), values.size()*sizeof(int)) )); @@ -436,7 +432,7 @@ namespace throw std::runtime_error("silo option list storage exhausted" "--specify bigger storage size"); - void *dest = m_option_storage.get() + m_option_storage_occupied; + void *dest = m_option_storage + m_option_storage_occupied; memcpy(dest, data, size); m_option_storage_occupied += size; return dest; @@ -471,8 +467,8 @@ namespace case DB_DOUBLE: return NPY_DOUBLE; case DB_CHAR: - return NPY_CHAR; -#if SILO_VERSION_GE(4,7,2) + return NPY_STRING; +#if SILO_VERSION_GE(4, 7, 2) case DB_LONG_LONG: return NPY_LONGLONG; #endif @@ -502,7 +498,7 @@ namespace #define PYVISFILE_TYPED_ARRAY_ACCESSOR(CAST_TO, NAME, SIZE) \ py::object NAME() const \ { \ - py::sequence py_list_result; \ + py::list py_list_result; \ for (unsigned i = 0; i < SIZE; ++i) \ py_list_result.append(CAST_TO(m_data->NAME[i])); \ return py::make_tuple(py_list_result); \ @@ -538,15 +534,15 @@ namespace }; #define PYVISFILE_CURVE_DATA_GETTER(COORD) \ - handle<> curve_##COORD(py::object py_curve) \ + py::handle curve_##COORD(py::object py_curve) \ { \ DBcurveWrapper &curve(py_curve.cast()); \ npy_intp dims[] = { curve.m_data->npts }; \ - handle<> result(PyArray_SimpleNewFromData(1, dims, \ + py::handle result(PyArray_SimpleNewFromData(1, dims, \ silo_typenum_to_numpy_typenum(curve.m_data->datatype), \ curve.m_data->COORD)); \ - PyArray_BASE(result.get()) = py_curve.ptr(); \ - Py_INCREF(PyArray_BASE(result.get())); \ + PyArray_BASE(result.ptr()) = py_curve.ptr(); \ + Py_INCREF(PyArray_BASE(result.ptr())); \ return result; \ } @@ -608,14 +604,14 @@ namespace DBquadmeshWrapper &quadmesh(py_quadmesh.cast()); py::list result; - for (unsigned i = 0; i < quadmesh.m_data->ndims; ++i) + for (int i = 0; i < quadmesh.m_data->ndims; ++i) { npy_intp dims[] = { quadmesh.m_data->dims[i] }; - handle<> coord_array(PyArray_SimpleNewFromData(1, dims, + py::handle coord_array(PyArray_SimpleNewFromData(1, dims, silo_typenum_to_numpy_typenum(quadmesh.m_data->datatype), quadmesh.m_data->coords[i])); - PyArray_BASE(coord_array.get()) = py_quadmesh.ptr(); - Py_INCREF(PyArray_BASE(coord_array.get())); + PyArray_BASE(coord_array.ptr()) = py_quadmesh.ptr(); + Py_INCREF(PyArray_BASE(coord_array.ptr())); result.append(coord_array); } @@ -676,7 +672,7 @@ namespace npy_intp dims[3]; - for (unsigned i = 0; i < quadvar.m_data->ndims; ++i) + for (int i = 0; i < quadvar.m_data->ndims; ++i) dims[i] = quadvar.m_data->dims[i]; int ary_flags = 0; @@ -686,21 +682,21 @@ namespace ary_flags |= NPY_FARRAY; py::list result; - for (unsigned i = 0; i < quadvar.m_data->nvals; ++i) + for (int i = 0; i < quadvar.m_data->nvals; ++i) { PyArray_Descr *tp_descr; tp_descr = PyArray_DescrNewFromType( silo_typenum_to_numpy_typenum(quadvar.m_data->datatype)); if (tp_descr == 0) - throw error_already_set(); + throw py::error_already_set(); - handle<> val_array(PyArray_NewFromDescr( + py::handle val_array(PyArray_NewFromDescr( &PyArray_Type, tp_descr, quadvar.m_data->ndims, dims, /*strides*/ NULL, quadvar.m_data->vals[i], ary_flags, /*obj*/NULL)); - PyArray_BASE(val_array.get()) = py_quadvar.ptr(); - Py_INCREF(PyArray_BASE(val_array.get())); + PyArray_BASE(val_array.ptr()) = py_quadvar.ptr(); + Py_INCREF(PyArray_BASE(val_array.ptr())); result.append(val_array); } @@ -980,8 +976,9 @@ namespace std::vector vartypes; std::vector varopts; - for(py::object entry: py_vars) + for(py::object py_entry: py_vars) { + py::tuple entry = py_entry.cast(); varnames_container.push_back(entry[0].cast()); vardefs_container.push_back(entry[1].cast()); if (py::len(entry) == 2) @@ -999,7 +996,7 @@ namespace } } - for (int i = 0; i < py::len(py_vars); i++) + for (size_t i = 0; i < py::len(py_vars); i++) { varnames.push_back(varnames_container[i].data()); vardefs.push_back(vardefs_container[i].data()); @@ -1387,10 +1384,10 @@ namespace py::tuple get_silo_version() { -#if PYVISFILE_SILO_VERSION_GE(4,6,1) +#if PYVISFILE_SILO_VERSION_GE(4, 6, 1) return py::make_tuple(SILO_VERS_MAJ, SILO_VERS_MIN, SILO_VERS_PAT); #else - return py::make_tuple(4,5,1); + return py::make_tuple(4, 5, 1); #endif } @@ -1650,7 +1647,7 @@ PYBIND11_MODULE(_internal, m) py::arg("value")) .def("extend", [](std::vector &self, const py::sequence &py_other) { - for(const py::object &item:: py_other) + for(const py::object &item: py_other) self.push_back(item.cast()); }, py::arg("iterable")) -- GitLab From bac5401f92c7587d248f754f951a3aa24bb316fa Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 30 Sep 2020 23:26:40 -0500 Subject: [PATCH 09/19] port uses of numpy_vector --- src/wrapper/wrap_silo.cpp | 92 ++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 49 deletions(-) diff --git a/src/wrapper/wrap_silo.cpp b/src/wrapper/wrap_silo.cpp index 96c7380..fa570a0 100644 --- a/src/wrapper/wrap_silo.cpp +++ b/src/wrapper/wrap_silo.cpp @@ -2,6 +2,7 @@ // Copyright (C) 2007 Andreas Kloeckner #include +#include #include #include @@ -42,6 +43,20 @@ #define DEF_SIMPLE_RO_PROPERTY(NAME) \ add_property(#NAME, &cl::NAME) +#define CALL_GUARDED(NAME, ARGLIST) \ + if (NAME ARGLIST) \ + throw std::runtime_error(#NAME " failed"); + +#define COPY_PY_LIST(TYPE, NAME) \ + std::vector NAME; \ + for (auto it: py_##NAME) \ + NAME.push_back(it.cast()); \ + +#define MAKE_STRING_POINTER_VECTOR(NAME) \ + std::vector NAME##_ptrs; \ + for(const std::string &s: NAME) \ + NAME##_ptrs.push_back(s.data()); \ + // }}} namespace py = pybind11; @@ -306,23 +321,6 @@ namespace // }}} - - - -#define CALL_GUARDED(NAME, ARGLIST) \ - if (NAME ARGLIST) \ - throw std::runtime_error(#NAME " failed"); - -#define COPY_PY_LIST(TYPE, NAME) \ - std::vector NAME; \ - for (auto it: py_##NAME) \ - NAME.push_back(it.cast()); \ - -#define MAKE_STRING_POINTER_VECTOR(NAME) \ - std::vector NAME##_ptrs; \ - for(const std::string &s: NAME) \ - NAME##_ptrs.push_back(s.data()); \ - NPY_TYPES get_varlist_dtype(py::sequence varlist) { bool first = true; @@ -853,7 +851,7 @@ namespace // {{{ ucd mesh/var template void put_ucdmesh(const char *name, - py::object py_coordnames, numpy_vector coords, + py::sequence py_coordnames, py::array_t coords, int nzones, const char *zonel_name, const char *facel_name, DBoptlistWrapper &optlist) { @@ -862,12 +860,13 @@ namespace if (coords.ndim() != 2) throw std::invalid_argument("need 2d array"); - int ndims = coords.dims()[0]; - int nnodes = coords.dims()[1]; + int ndims = coords.shape(0); + int nnodes = coords.shape(1); + auto coords_unchecked = coords.unchecked<2>(); std::vector coord_starts; for (int d = 0; d < ndims; d++) - coord_starts.push_back(&coords.sub(d, 0)); + coord_starts.push_back(&coords_unchecked(d, 0)); CALL_GUARDED(DBPutUcdmesh, (m_dbfile, name, ndims, /* coordnames*/ NULL, @@ -876,19 +875,16 @@ namespace get_datatype(T()), optlist.get_optlist())); } - - - template void put_ucdvar1(const char *vname, const char *mname, - const numpy_vector &v, + const py::array_t &v, /*float *mixvar, int mixlen, */int centering, DBoptlistWrapper &optlist) { ensure_db_open(); CALL_GUARDED(DBPutUcdvar1, (m_dbfile, vname, mname, - (float *) &v.sub(0), + (float *) v.data(), v.size(), /* mixvar */ NULL, /* mixlen */ 0, get_datatype(T()), centering, @@ -918,7 +914,7 @@ namespace for(py::object py_var: py_vars) { - numpy_vector v = py_var.cast>(); + py::array_t v = py_var.cast>(); if (first) { vlength = v.size(); @@ -926,7 +922,7 @@ namespace } else if (vlength != int(v.size())) PYTHON_ERROR(ValueError, "field components need to have matching lengths"); - vars.push_back((float *) v.data().data()); + vars.push_back((float *) v.data()); } CALL_GUARDED(DBPutUcdvar, (m_dbfile, vname, mname, @@ -1019,7 +1015,7 @@ namespace // {{{ point mesh/var template - void put_pointmesh(const char *id, const numpy_vector &coords, + void put_pointmesh(const char *id, const py::array_t &coords, DBoptlistWrapper &optlist) { ensure_db_open(); @@ -1027,12 +1023,13 @@ namespace if (coords.ndim() != 2) throw std::invalid_argument("need 2d array"); - int ndims = coords.dims()[0]; - int npoints = coords.dims()[1]; + int ndims = coords.shape(0); + int npoints = coords.shape(1); + auto coords_unchecked = coords.unchecked<2>(); std::vector coord_starts; for (int d = 0; d < ndims; d++) - coord_starts.push_back((float *) &coords.sub(d,0)); + coord_starts.push_back((float *) &coords_unchecked(d, 0)); CALL_GUARDED(DBPutPointmesh, (m_dbfile, id, ndims, &coord_starts.front(), npoints, @@ -1044,12 +1041,12 @@ namespace template void put_pointvar1(const char *vname, const char *mname, - const numpy_vector &v, DBoptlistWrapper &optlist) + const py::array_t &v, DBoptlistWrapper &optlist) { ensure_db_open(); CALL_GUARDED(DBPutPointvar1, (m_dbfile, vname, mname, - (float *) v.data().data(), v.size(), + (float *) v.data(), v.size(), get_datatype(T()), optlist.get_optlist())); } @@ -1069,7 +1066,7 @@ namespace for(py::object py_var: py_vars) { - numpy_vector v = py_var.cast>(); + py::array_t v = py_var.cast>(); if (first) { vlength = v.size(); @@ -1078,7 +1075,7 @@ namespace else if (vlength != int(v.size())) PYTHON_ERROR(ValueError, "field components need to have matching lengths"); - vars.push_back((float *) v.data().data()); + vars.push_back((float *) v.data()); } CALL_GUARDED(DBPutPointvar, (m_dbfile, vname, mname, @@ -1119,9 +1116,9 @@ namespace for(py::object py_coord_dim: py_coords) { - numpy_vector coord_dim = py_coord_dim.cast>(); + py::array_t coord_dim = py_coord_dim.cast>(); dims.push_back(coord_dim.size()); - coords.push_back((float *) coord_dim.data().data()); + coords.push_back((float *) coord_dim.data()); } CALL_GUARDED(DBPutQuadmesh, (m_dbfile, name, @@ -1177,7 +1174,7 @@ namespace for(py::object py_var: py_vars) { - numpy_vector v = py_var.cast>(); + py::array_t v = py_var.cast>(); if (first) { vlength = v.size(); @@ -1185,7 +1182,7 @@ namespace } else if (vlength != int(v.size())) PYTHON_ERROR(ValueError, "field components need to have matching lengths"); - vars.push_back((float *) v.data().data()); + vars.push_back((float *) v.data()); } CALL_GUARDED(DBPutQuadvar, (m_dbfile, vname, mname, @@ -1223,19 +1220,16 @@ namespace } } - - - template void put_quadvar1(const char *vname, const char *mname, - numpy_vector var, py::object py_dims, + py::array_t var, py::sequence py_dims, /*float *mixvar, int mixlen, */int centering, DBoptlistWrapper &optlist) { COPY_PY_LIST(int, dims); CALL_GUARDED(DBPutQuadvar1, (m_dbfile, vname, mname, - (float *) var.data().data(), + (float *) var.data(), &dims.front(), dims.size(), /* mix stuff */ NULL, 0, @@ -1305,16 +1299,16 @@ namespace template void put_curve(const char *curvename, - const numpy_vector &xvals, - const numpy_vector &yvals, + const py::array_t &xvals, + const py::array_t &yvals, DBoptlistWrapper &optlist) { if (xvals.size() != yvals.size()) PYTHON_ERROR(ValueError, "xvals and yvals must have the same length"); int npoints = xvals.size(); CALL_GUARDED(DBPutCurve, (m_dbfile, curvename, - const_cast(reinterpret_cast(xvals.data().data())), - const_cast(reinterpret_cast(yvals.data().data())), + const_cast(reinterpret_cast(xvals.data())), + const_cast(reinterpret_cast(yvals.data())), get_datatype(T()), npoints, optlist.get_optlist())); -- GitLab From abb6762ea9662fecae4e02fe966281d722573423 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 30 Sep 2020 23:52:52 -0500 Subject: [PATCH 10/19] finally compiling --- src/wrapper/wrap_silo.cpp | 189 +++++++++++++++----------------------- 1 file changed, 72 insertions(+), 117 deletions(-) diff --git a/src/wrapper/wrap_silo.cpp b/src/wrapper/wrap_silo.cpp index fa570a0..b3ccc55 100644 --- a/src/wrapper/wrap_silo.cpp +++ b/src/wrapper/wrap_silo.cpp @@ -40,8 +40,11 @@ #define DEF_SIMPLE_METHOD_WITH_ARGS(NAME, ARGS) \ def(#NAME, &cl::NAME, args ARGS) +#define DEF_SIMPLE_RO_MEMBER(NAME) \ + def_readonly(#NAME, &cl::NAME) + #define DEF_SIMPLE_RO_PROPERTY(NAME) \ - add_property(#NAME, &cl::NAME) + def_property_readonly(#NAME, &cl::get##NAME) #define CALL_GUARDED(NAME, ARGLIST) \ if (NAME ARGLIST) \ @@ -79,6 +82,22 @@ namespace noncopyable& operator=(const noncopyable&) = delete; }; + py::tuple get_silo_version() + { +#if PYVISFILE_SILO_VERSION_GE(4, 6, 1) + return py::make_tuple(SILO_VERS_MAJ, SILO_VERS_MIN, SILO_VERS_PAT); +#else + return py::make_tuple(4, 5, 1); +#endif + } + +#if !PYVISFILE_SILO_VERSION_GE(4, 6, 1) + int set_dep_warning_dummy(int) + { + return 0; + } +#endif + // }}} // {{{ constants @@ -440,9 +459,6 @@ namespace // }}} - - - int get_datatype(int) { return DB_INT; } int get_datatype(short) { return DB_SHORT; } int get_datatype(long) { return DB_LONG; } @@ -475,17 +491,14 @@ namespace } } - - - // {{{ data wrappers // {{{ data wrapper helpers #define PYVISFILE_TYPED_ACCESSOR(TYPE,NAME) \ - TYPE NAME() const { return m_data->NAME; } + TYPE get##NAME() const { return m_data->NAME; } #define PYVISFILE_STRING_ACCESSOR(NAME) \ - py::object NAME() const \ + py::object get##NAME() const \ { \ if (m_data) \ return py::cast(std::string(m_data->NAME)); \ @@ -494,7 +507,7 @@ namespace } #define PYVISFILE_TYPED_ARRAY_ACCESSOR(CAST_TO, NAME, SIZE) \ - py::object NAME() const \ + py::object get##NAME() const \ { \ py::list py_list_result; \ for (unsigned i = 0; i < SIZE; ++i) \ @@ -705,9 +718,6 @@ namespace // }}} - - - // {{{ DBtoc copy struct DBtocCopy : noncopyable @@ -751,9 +761,6 @@ namespace return new DB##LOWER_TYPE##Wrapper(obj); \ } - - - class DBfileWrapper: noncopyable { public: @@ -820,10 +827,7 @@ namespace )); } - - - -#if PYVISFILE_SILO_VERSION_GE(4,6,1) +#if PYVISFILE_SILO_VERSION_GE(4, 6, 1) void put_zonelist_2(const char *name, int nzones, int ndims, const std::vector &nodelist, int lo_offset, int hi_offset, const std::vector &shapetype, @@ -863,7 +867,7 @@ namespace int ndims = coords.shape(0); int nnodes = coords.shape(1); - auto coords_unchecked = coords.unchecked<2>(); + auto coords_unchecked = coords.unchecked(); std::vector coord_starts; for (int d = 0; d < ndims; d++) coord_starts.push_back(&coords_unchecked(d, 0)); @@ -891,9 +895,6 @@ namespace optlist.get_optlist())); } - - - template void put_ucdvar_backend(const char *vname, const char *mname, py::sequence py_varnames, py::sequence py_vars, @@ -934,9 +935,6 @@ namespace get_datatype(T()), centering, optlist.get_optlist())); } - - - void put_ucdvar(const char *vname, const char *mname, py::object py_varnames, py::object py_vars, /*float *mixvars[], int mixlen,*/ @@ -998,7 +996,7 @@ namespace vardefs.push_back(vardefs_container[i].data()); } -#if PYVISFILE_SILO_VERSION_GE(4,6,1) +#if PYVISFILE_SILO_VERSION_GE(4, 6, 1) CALL_GUARDED(DBPutDefvars, (m_dbfile, id.data(), py::len(py_vars), const_cast(&varnames.front()), &vartypes.front(), @@ -1026,7 +1024,7 @@ namespace int ndims = coords.shape(0); int npoints = coords.shape(1); - auto coords_unchecked = coords.unchecked<2>(); + auto coords_unchecked = coords.unchecked(); std::vector coord_starts; for (int d = 0; d < ndims; d++) coord_starts.push_back((float *) &coords_unchecked(d, 0)); @@ -1036,9 +1034,6 @@ namespace get_datatype(T()), optlist.get_optlist())); } - - - template void put_pointvar1(const char *vname, const char *mname, const py::array_t &v, DBoptlistWrapper &optlist) @@ -1050,9 +1045,6 @@ namespace get_datatype(T()), optlist.get_optlist())); } - - - template void put_pointvar_backend(const char *vname, const char *mname, py::sequence py_vars, @@ -1084,9 +1076,6 @@ namespace optlist.get_optlist())); } - - - void put_pointvar(const char *vname, const char *mname, py::object py_vars, DBoptlistWrapper &optlist) { @@ -1108,7 +1097,7 @@ namespace // {{{ quad mesh/var template - void put_quadmesh_backend(const char *name, py::list py_coords, + void put_quadmesh_backend(const char *name, py::sequence py_coords, int coordtype, DBoptlistWrapper &optlist) { std::vector dims; @@ -1131,9 +1120,6 @@ namespace optlist.get_optlist())); } - - - void put_quadmesh(const char *name, py::object py_coords, int coordtype, DBoptlistWrapper &optlist) { @@ -1150,10 +1136,6 @@ namespace } } - - - - template void put_quadvar_backend(const char *vname, const char *mname, py::sequence py_varnames, py::sequence py_vars, py::sequence py_dims, @@ -1197,9 +1179,6 @@ namespace optlist.get_optlist())); } - - - void put_quadvar(const char *vname, const char *mname, py::object py_varnames, py::object py_vars, py::object py_dims, /*float *mixvar, int mixlen, */int centering, @@ -1245,7 +1224,7 @@ namespace // {{{ multi mesh/var - void put_multimesh(const char *name, py::sequence names_and_types, + void put_multimesh(const char *name, py::sequence py_names_and_types, DBoptlistWrapper &optlist) { ensure_db_open(); @@ -1253,8 +1232,9 @@ namespace std::vector meshnames; std::vector meshtypes; - for(py::object name_and_type: names_and_types) + for(py::object py_name_and_type: py_names_and_types) { + py::tuple name_and_type = py_name_and_type.cast(); meshnames.push_back(name_and_type[0].cast()); meshtypes.push_back(name_and_type[1].cast()); } @@ -1267,10 +1247,7 @@ namespace optlist.get_optlist())); } - - - - void put_multivar(const char *name, py::sequence names_and_types, + void put_multivar(const char *name, py::sequence py_names_and_types, DBoptlistWrapper &optlist) { ensure_db_open(); @@ -1278,8 +1255,9 @@ namespace std::vector varnames; std::vector vartypes; - for(py::object name_and_type: names_and_types) + for(py::object py_name_and_type: py_names_and_types) { + py::tuple name_and_type = py_name_and_type.cast(); varnames.push_back(name_and_type[0].cast()); vartypes.push_back(name_and_type[1].cast()); } @@ -1314,8 +1292,6 @@ namespace optlist.get_optlist())); } - - PYVISFILE_DBFILE_GET_WRAPPER(curve, Curve); // }}} @@ -1373,30 +1349,9 @@ namespace }; // }}} - - - - py::tuple get_silo_version() - { -#if PYVISFILE_SILO_VERSION_GE(4, 6, 1) - return py::make_tuple(SILO_VERS_MAJ, SILO_VERS_MIN, SILO_VERS_PAT); -#else - return py::make_tuple(4, 5, 1); -#endif - } - - - - int set_dep_warning_dummy(int) - { - return 0; - } } - - - -// {{{ main wrapper function -------------------------------------------------- +// {{{ main wrapper function static bool import_numpy_helper() { @@ -1493,14 +1448,14 @@ PYBIND11_MODULE(_internal, m) .def("put_curve", &cl::put_curve) .def("put_curve", &cl::put_curve) .def("get_curve", &cl::get_curve, - return_value_policy()) + py::return_value_policy::take_ownership) .def("get_quadmesh", &cl::get_quadmesh, - return_value_policy()) + py::return_value_policy::take_ownership) .def("get_quadvar", &cl::get_quadvar, - return_value_policy()) + py::return_value_policy::take_ownership) .def("get_toc", &cl::get_toc, - return_value_policy()) + py::return_value_policy::take_ownership) ; } @@ -1528,14 +1483,14 @@ PYBIND11_MODULE(_internal, m) .DEF_SIMPLE_RO_PROPERTY(xunits) .DEF_SIMPLE_RO_PROPERTY(yunits) .DEF_SIMPLE_RO_PROPERTY(reference) - .add_property("x", make_function(curve_x)) - .add_property("y", make_function(curve_y)) + .def_property_readonly("x", &curve_x) + .def_property_readonly("y", &curve_y) ; } { typedef DBquadmeshWrapper cl; - py::class_("DBQuadMesh") + py::class_(m, "DBQuadMesh") .DEF_SIMPLE_RO_PROPERTY(id) .DEF_SIMPLE_RO_PROPERTY(block_no) .DEF_SIMPLE_RO_PROPERTY(group_no) @@ -1564,7 +1519,7 @@ PYBIND11_MODULE(_internal, m) .DEF_SIMPLE_RO_PROPERTY(size_index) .DEF_SIMPLE_RO_PROPERTY(guihide) .DEF_SIMPLE_RO_PROPERTY(mrgtree_name) - .add_property("coords", make_function(quadmesh_coords)) + .def_property_readonly("coords", &quadmesh_coords) ; } @@ -1596,37 +1551,37 @@ PYBIND11_MODULE(_internal, m) .DEF_SIMPLE_RO_PROPERTY(ascii_labels) .DEF_SIMPLE_RO_PROPERTY(meshname) .DEF_SIMPLE_RO_PROPERTY(guihide) - .add_property("vals", make_function(quadvar_vals)) + .def_property_readonly("vals", &quadvar_vals) ; } { typedef DBtocCopy cl; py::class_(m, "DBToc") - .DEF_SIMPLE_RO_PROPERTY(curve_names) - .DEF_SIMPLE_RO_PROPERTY(multimesh_names) - .DEF_SIMPLE_RO_PROPERTY(multimeshadj_names) - .DEF_SIMPLE_RO_PROPERTY(multivar_names) - .DEF_SIMPLE_RO_PROPERTY(multimat_names) - .DEF_SIMPLE_RO_PROPERTY(multimatspecies_names) - .DEF_SIMPLE_RO_PROPERTY(csgmesh_names) - .DEF_SIMPLE_RO_PROPERTY(csgvar_names) - .DEF_SIMPLE_RO_PROPERTY(defvars_names) - .DEF_SIMPLE_RO_PROPERTY(qmesh_names) - .DEF_SIMPLE_RO_PROPERTY(qvar_names) - .DEF_SIMPLE_RO_PROPERTY(ucdmesh_names) - .DEF_SIMPLE_RO_PROPERTY(ucdvar_names) - .DEF_SIMPLE_RO_PROPERTY(ptmesh_names) - .DEF_SIMPLE_RO_PROPERTY(ptvar_names) - .DEF_SIMPLE_RO_PROPERTY(mat_names) - .DEF_SIMPLE_RO_PROPERTY(matspecies_names) - .DEF_SIMPLE_RO_PROPERTY(var_names) - .DEF_SIMPLE_RO_PROPERTY(obj_names) - .DEF_SIMPLE_RO_PROPERTY(dir_names) - .DEF_SIMPLE_RO_PROPERTY(array_names) - .DEF_SIMPLE_RO_PROPERTY(mrgtree_names) - .DEF_SIMPLE_RO_PROPERTY(groupelmap_names) - .DEF_SIMPLE_RO_PROPERTY(mrgvar_names) + .DEF_SIMPLE_RO_MEMBER(curve_names) + .DEF_SIMPLE_RO_MEMBER(multimesh_names) + .DEF_SIMPLE_RO_MEMBER(multimeshadj_names) + .DEF_SIMPLE_RO_MEMBER(multivar_names) + .DEF_SIMPLE_RO_MEMBER(multimat_names) + .DEF_SIMPLE_RO_MEMBER(multimatspecies_names) + .DEF_SIMPLE_RO_MEMBER(csgmesh_names) + .DEF_SIMPLE_RO_MEMBER(csgvar_names) + .DEF_SIMPLE_RO_MEMBER(defvars_names) + .DEF_SIMPLE_RO_MEMBER(qmesh_names) + .DEF_SIMPLE_RO_MEMBER(qvar_names) + .DEF_SIMPLE_RO_MEMBER(ucdmesh_names) + .DEF_SIMPLE_RO_MEMBER(ucdvar_names) + .DEF_SIMPLE_RO_MEMBER(ptmesh_names) + .DEF_SIMPLE_RO_MEMBER(ptvar_names) + .DEF_SIMPLE_RO_MEMBER(mat_names) + .DEF_SIMPLE_RO_MEMBER(matspecies_names) + .DEF_SIMPLE_RO_MEMBER(var_names) + .DEF_SIMPLE_RO_MEMBER(obj_names) + .DEF_SIMPLE_RO_MEMBER(dir_names) + .DEF_SIMPLE_RO_MEMBER(array_names) + .DEF_SIMPLE_RO_MEMBER(mrgtree_names) + .DEF_SIMPLE_RO_MEMBER(groupelmap_names) + .DEF_SIMPLE_RO_MEMBER(mrgvar_names) ; } @@ -1636,8 +1591,8 @@ PYBIND11_MODULE(_internal, m) .def(py::init<>()) .def("reserve", &cl::reserve, py::arg("advised_size")) - .def("__len__", &cl.size) - .def("append", &cl::push_back, + .def("__len__", &cl::size) + .def("append", (void (std::vector::*)(const int &)) &cl::push_back, py::arg("value")) .def("extend", [](std::vector &self, const py::sequence &py_other) { @@ -1649,9 +1604,9 @@ PYBIND11_MODULE(_internal, m) } #if PYVISFILE_SILO_VERSION_GE(4,6,1) - def("set_deprecate_warnings", DBSetDeprecateWarnings); + m.def("set_deprecate_warnings", DBSetDeprecateWarnings); #else - def("set_deprecate_warnings", set_dep_warning_dummy); + m.def("set_deprecate_warnings", set_dep_warning_dummy); #endif DEF_SIMPLE_FUNCTION(get_silo_version); } -- GitLab From 3faafa6f78659c7b2a310190d4174f4dd62c5fb5 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 30 Sep 2020 23:58:57 -0500 Subject: [PATCH 11/19] fixes to actually make examples run --- pyvisfile/silo/__init__.py | 1 - src/wrapper/wrap_silo.cpp | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pyvisfile/silo/__init__.py b/pyvisfile/silo/__init__.py index da90505..7382f51 100644 --- a/pyvisfile/silo/__init__.py +++ b/pyvisfile/silo/__init__.py @@ -43,7 +43,6 @@ _ignore_extra_int_vector_warning() import sys -import pyublas # noqa try: import pyvisfile.silo._internal # noqa except ImportError: diff --git a/src/wrapper/wrap_silo.cpp b/src/wrapper/wrap_silo.cpp index b3ccc55..d3cd426 100644 --- a/src/wrapper/wrap_silo.cpp +++ b/src/wrapper/wrap_silo.cpp @@ -1557,7 +1557,7 @@ PYBIND11_MODULE(_internal, m) { typedef DBtocCopy cl; - py::class_(m, "DBToc") + py::class_(m, "DBToc") .DEF_SIMPLE_RO_MEMBER(curve_names) .DEF_SIMPLE_RO_MEMBER(multimesh_names) .DEF_SIMPLE_RO_MEMBER(multimeshadj_names) @@ -1594,9 +1594,9 @@ PYBIND11_MODULE(_internal, m) .def("__len__", &cl::size) .def("append", (void (std::vector::*)(const int &)) &cl::push_back, py::arg("value")) - .def("extend", [](std::vector &self, const py::sequence &py_other) + .def("extend", [](std::vector &self, const py::iterable &py_other) { - for(const py::object &item: py_other) + for(const py::handle &item: py_other) self.push_back(item.cast()); }, py::arg("iterable")) -- GitLab From ef79b41d3ca08c375a45edee9511374d4e2e865a Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Thu, 1 Oct 2020 00:08:12 -0500 Subject: [PATCH 12/19] export constants correctly --- pyvisfile/silo/__init__.py | 96 +++++++++++++------------------------- src/wrapper/wrap_silo.cpp | 8 ++-- 2 files changed, 36 insertions(+), 68 deletions(-) diff --git a/pyvisfile/silo/__init__.py b/pyvisfile/silo/__init__.py index 7382f51..f66ead4 100644 --- a/pyvisfile/silo/__init__.py +++ b/pyvisfile/silo/__init__.py @@ -33,18 +33,9 @@ SiloFile. """ -def _ignore_extra_int_vector_warning(): - from warnings import filterwarnings - filterwarnings("ignore", module="pyvisfile.silo", - category=RuntimeWarning, lineno=43) - - -_ignore_extra_int_vector_warning() - - import sys try: - import pyvisfile.silo._internal # noqa + import pyvisfile.silo._internal as _silo except ImportError: from warnings import warn warn("Importing the native-code parts of PyVisfile's silo component failed. " @@ -53,50 +44,29 @@ except ImportError: "This requires the libsilo library.") raise -# hackety hack -- not sure why this is needed -_intnl = sys.modules["pyvisfile.silo._internal"] - - -# {{{ handle symbols - -_intnl_symbols = _intnl.symbols() - - -def _export_symbols(): - for name, value in _intnl_symbols.items(): - globals()[name] = value - - -_export_symbols() - -# These are technically redundant, but they help avoid Flake8 warnings below. - -DB_COLLINEAR = _intnl_symbols["DB_COLLINEAR"] -DB_LOCAL = _intnl_symbols["DB_LOCAL"] -DB_NOCLOBBER = _intnl_symbols["DB_NOCLOBBER"] -DB_UNKNOWN = _intnl_symbols["DB_UNKNOWN"] -DB_PDB = _intnl_symbols["DB_PDB"] -DB_APPEND = _intnl_symbols["DB_APPEND"] - -# }}} +from pyvisfile.silo._internal import ( + # types + DBObjectType, DBdatatype, -DBObjectType = _intnl.DBObjectType -DBdatatype = _intnl.DBdatatype + # classes + DBToc, DBCurve, DBQuadMesh, DBQuadVar, IntVector, + get_silo_version, set_deprecate_warnings, -DBToc = _intnl.DBToc -DBCurve = _intnl.DBCurve -DBQuadMesh = _intnl.DBQuadMesh -DBQuadVar = _intnl.DBQuadVar + # constants + DB_LOCAL, DB_COLLINEAR, DB_CLOBBER, DB_NOCLOBBER, DB_PDB, DB_NODECENT, + DB_HDF5, DB_READ, DB_UNKNOWN, + DB_ZONETYPE_TRIANGLE, DB_ZONECENT, -IntVector = _intnl.IntVector -get_silo_version = _intnl.get_silo_version -set_deprecate_warnings = _intnl.set_deprecate_warnings + DBOPT_CYCLE, DBOPT_DTIME, DBOPT_XUNITS, DBOPT_YUNITS, DBOPT_ZUNITS, + DBOPT_XLABEL, DBOPT_YLABEL, DBOPT_ZLABEL, + DBOPT_UNITS, DBOPT_HI_OFFSET, DBOPT_LO_OFFSET + ) def _convert_optlist(ol_dict): optcount = len(ol_dict) + 1 - ol = _intnl.DBOptlist(optcount, optcount * 150) + ol = _silo.DBOptlist(optcount, optcount * 150) for key, value in ol_dict.items(): if isinstance(value, int): @@ -113,7 +83,7 @@ def _convert_optlist(ol_dict): return ol -class SiloFile(_intnl.DBFile): +class SiloFile(_silo.DBFile): """This class can be used in a Python 2.5 *with* statement.""" def __enter__(self): return self @@ -129,34 +99,34 @@ class SiloFile(_intnl.DBFile): mode = DB_NOCLOBBER if filetype is None: filetype = DB_PDB - _intnl.DBFile.__init__(self, pathname, mode, target, + _silo.DBFile.__init__(self, pathname, mode, target, fileinfo, filetype) else: if mode is None: mode = DB_APPEND if filetype is None: filetype = DB_UNKNOWN - _intnl.DBFile.__init__(self, pathname, filetype, mode) + _silo.DBFile.__init__(self, pathname, filetype, mode) def put_zonelist_2(self, names, nzones, ndims, nodelist, lo_offset, hi_offset, shapetype, shapesize, shapecounts, optlist={}): - _intnl.DBFile.put_zonelist_2(self, names, nzones, ndims, + _silo.DBFile.put_zonelist_2(self, names, nzones, ndims, nodelist, lo_offset, hi_offset, shapetype, shapesize, shapecounts, _convert_optlist(optlist)) def put_ucdmesh(self, mname, coordnames, coords, nzones, zonel_name, facel_name, optlist={}): - _intnl.DBFile.put_ucdmesh(self, mname, coordnames, coords, + _silo.DBFile.put_ucdmesh(self, mname, coordnames, coords, nzones, zonel_name, facel_name, _convert_optlist(optlist)) def put_ucdvar1(self, vname, mname, vec, centering, optlist={}): - _intnl.DBFile.put_ucdvar1(self, vname, mname, vec, centering, + _silo.DBFile.put_ucdvar1(self, vname, mname, vec, centering, _convert_optlist(optlist)) def put_ucdvar(self, vname, mname, varnames, vars, centering, optlist={}): - _intnl.DBFile.put_ucdvar(self, vname, mname, varnames, vars, centering, + _silo.DBFile.put_ucdvar(self, vname, mname, varnames, vars, centering, _convert_optlist(optlist)) def put_defvars(self, vname, vars): @@ -171,43 +141,43 @@ class SiloFile(_intnl.DBFile): If the type is not specified, scalar is assumed. """ - _intnl.DBFile.put_defvars(self, vname, vars) + _silo.DBFile.put_defvars(self, vname, vars) def put_pointmesh(self, mname, coords, optlist={}): - _intnl.DBFile.put_pointmesh(self, mname, coords, + _silo.DBFile.put_pointmesh(self, mname, coords, _convert_optlist(optlist)) def put_pointvar1(self, vname, mname, var, optlist={}): - _intnl.DBFile.put_pointvar1(self, vname, mname, var, + _silo.DBFile.put_pointvar1(self, vname, mname, var, _convert_optlist(optlist)) def put_pointvar(self, vname, mname, vars, optlist={}): - _intnl.DBFile.put_pointvar(self, vname, mname, vars, + _silo.DBFile.put_pointvar(self, vname, mname, vars, _convert_optlist(optlist)) def put_quadmesh(self, mname, coords, coordtype=DB_COLLINEAR, optlist={}): - _intnl.DBFile.put_quadmesh(self, mname, coords, coordtype, + _silo.DBFile.put_quadmesh(self, mname, coords, coordtype, _convert_optlist(optlist)) def put_quadvar1(self, vname, mname, var, dims, centering, optlist={}): - _intnl.DBFile.put_quadvar1(self, vname, mname, var, dims, centering, + _silo.DBFile.put_quadvar1(self, vname, mname, var, dims, centering, _convert_optlist(optlist)) def put_quadvar(self, vname, mname, varnames, vars, dims, centering, optlist={}): - _intnl.DBFile.put_quadvar(self, vname, mname, + _silo.DBFile.put_quadvar(self, vname, mname, varnames, vars, dims, centering, _convert_optlist(optlist)) def put_multimesh(self, mname, mnames_and_types, optlist={}): - _intnl.DBFile.put_multimesh(self, mname, + _silo.DBFile.put_multimesh(self, mname, mnames_and_types, _convert_optlist(optlist)) def put_multivar(self, vname, vnames_and_types, optlist={}): - _intnl.DBFile.put_multivar(self, vname, + _silo.DBFile.put_multivar(self, vname, vnames_and_types, _convert_optlist(optlist)) def put_curve(self, curvename, xvals, yvals, optlist={}): - _intnl.DBFile.put_curve(self, curvename, xvals, yvals, + _silo.DBFile.put_curve(self, curvename, xvals, yvals, _convert_optlist(optlist)) diff --git a/src/wrapper/wrap_silo.cpp b/src/wrapper/wrap_silo.cpp index d3cd426..b443385 100644 --- a/src/wrapper/wrap_silo.cpp +++ b/src/wrapper/wrap_silo.cpp @@ -102,11 +102,10 @@ namespace // {{{ constants - py::dict symbols() + void silo_expose_symbols(py::module &m) { - py::dict result; #define EXPORT_CONSTANT(NAME) \ - result[#NAME] = NAME + m.attr(#NAME) = NAME /* Drivers */ EXPORT_CONSTANT(DB_NETCDF); @@ -335,7 +334,6 @@ namespace #endif #undef EXPORT_CONSTANT - return result; } // }}} @@ -1364,7 +1362,7 @@ PYBIND11_MODULE(_internal, m) if (!import_numpy_helper()) throw py::error_already_set(); - DEF_SIMPLE_FUNCTION(symbols); + silo_expose_symbols(m); py::enum_(m, "DBdatatype") .ENUM_VALUE(DB_INT) -- GitLab From 8c1102ed07444b2ed9d3302d2d3f1bf147df901d Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Thu, 1 Oct 2020 00:09:09 -0500 Subject: [PATCH 13/19] flake8 fixes --- pyvisfile/silo/__init__.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pyvisfile/silo/__init__.py b/pyvisfile/silo/__init__.py index f66ead4..d9474de 100644 --- a/pyvisfile/silo/__init__.py +++ b/pyvisfile/silo/__init__.py @@ -32,8 +32,6 @@ ParallelSiloFile to automatically create a master file along with your SiloFile. """ - -import sys try: import pyvisfile.silo._internal as _silo except ImportError: @@ -45,7 +43,7 @@ except ImportError: raise -from pyvisfile.silo._internal import ( +from pyvisfile.silo._internal import ( # noqa: F401 # types DBObjectType, DBdatatype, @@ -55,7 +53,7 @@ from pyvisfile.silo._internal import ( # constants DB_LOCAL, DB_COLLINEAR, DB_CLOBBER, DB_NOCLOBBER, DB_PDB, DB_NODECENT, - DB_HDF5, DB_READ, DB_UNKNOWN, + DB_HDF5, DB_READ, DB_UNKNOWN, DB_APPEND, DB_ZONETYPE_TRIANGLE, DB_ZONECENT, DBOPT_CYCLE, DBOPT_DTIME, DBOPT_XUNITS, DBOPT_YUNITS, DBOPT_ZUNITS, -- GitLab From c8f693c2e99ba27a9eb614b353ce490908f85af3 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Thu, 1 Oct 2020 12:30:44 -0500 Subject: [PATCH 14/19] add some more constants --- pyvisfile/silo/__init__.py | 5 ++- src/wrapper/wrap_silo.cpp | 84 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 6 deletions(-) diff --git a/pyvisfile/silo/__init__.py b/pyvisfile/silo/__init__.py index d9474de..99838a5 100644 --- a/pyvisfile/silo/__init__.py +++ b/pyvisfile/silo/__init__.py @@ -44,12 +44,13 @@ except ImportError: from pyvisfile.silo._internal import ( # noqa: F401 - # types + get_silo_version, set_deprecate_warnings, + + # enums DBObjectType, DBdatatype, # classes DBToc, DBCurve, DBQuadMesh, DBQuadVar, IntVector, - get_silo_version, set_deprecate_warnings, # constants DB_LOCAL, DB_COLLINEAR, DB_CLOBBER, DB_NOCLOBBER, DB_PDB, DB_NODECENT, diff --git a/src/wrapper/wrap_silo.cpp b/src/wrapper/wrap_silo.cpp index b443385..695673b 100644 --- a/src/wrapper/wrap_silo.cpp +++ b/src/wrapper/wrap_silo.cpp @@ -1,6 +1,9 @@ // PyVisfile - A Python wrapper around Silo // Copyright (C) 2007 Andreas Kloeckner +// docs for the various types in v.4.10.2 can be found here (2020-10-01) +// https://wci.llnl.gov/content/assets/docs/simulation/computer-codes/silo/LLNL-SM-654357.pdf + #include #include #include @@ -113,14 +116,23 @@ namespace EXPORT_CONSTANT(DB_TAURUS); EXPORT_CONSTANT(DB_UNKNOWN); EXPORT_CONSTANT(DB_DEBUG); + EXPORT_CONSTANT(DB_HDF5X); + EXPORT_CONSTANT(DB_PDBP); + EXPORT_CONSTANT(DB_HDF5); -#if PYVISFILE_SILO_VERSION_GE(4,6,1) +#if PYVISFILE_SILO_VERSION_GE(4, 6, 1) EXPORT_CONSTANT(DB_HDF5_SEC2); EXPORT_CONSTANT(DB_HDF5_STDIO); EXPORT_CONSTANT(DB_HDF5_CORE); + EXPORT_CONSTANT(DB_HDF5_LOG); + EXPORT_CONSTANT(DB_HDF5_SPLIT); + EXPORT_CONSTANT(DB_HDF5_DIRECT); + EXPORT_CONSTANT(DB_HDF5_FAMILY); EXPORT_CONSTANT(DB_HDF5_MPIO); EXPORT_CONSTANT(DB_HDF5_MPIOP); + EXPORT_CONSTANT(DB_HDF5_MPIP); + EXPORT_CONSTANT(DB_HDF5_SILO); #endif /* Flags for DBCreate */ @@ -192,6 +204,36 @@ namespace EXPORT_CONSTANT(DBOPT_REGNAMES); EXPORT_CONSTANT(DBOPT_ZONENAMES); EXPORT_CONSTANT(DBOPT_HIDE_FROM_GUI); + EXPORT_CONSTANT(DBOPT_TOPO_DIM); + EXPORT_CONSTANT(DBOPT_REFERENCE); + EXPORT_CONSTANT(DBOPT_GROUPINGS_SIZE); + EXPORT_CONSTANT(DBOPT_GROUPINGS); + EXPORT_CONSTANT(DBOPT_GROUPINGNAMES); + EXPORT_CONSTANT(DBOPT_ALLOWMAT0); + EXPORT_CONSTANT(DBOPT_MRGTREE_NAME); + EXPORT_CONSTANT(DBOPT_REGION_PNAMES); + EXPORT_CONSTANT(DBOPT_TENSOR_RANK); + EXPORT_CONSTANT(DBOPT_MMESH_NAME); + EXPORT_CONSTANT(DBOPT_TV_CONNECTIVITY); + EXPORT_CONSTANT(DBOPT_DISJOINT_MODE); + EXPORT_CONSTANT(DBOPT_MRGV_ONAMES); + EXPORT_CONSTANT(DBOPT_MRGV_RNAMES); + EXPORT_CONSTANT(DBOPT_SPECNAMES); + EXPORT_CONSTANT(DBOPT_SPECCOLORS); + EXPORT_CONSTANT(DBOPT_LLONGNZNUM); + EXPORT_CONSTANT(DBOPT_CONSERVED); + EXPORT_CONSTANT(DBOPT_EXTENSIVE); + EXPORT_CONSTANT(DBOPT_MB_FILE_NS); + EXPORT_CONSTANT(DBOPT_MB_BLOCK_NS); + EXPORT_CONSTANT(DBOPT_MB_BLOCK_TYPE); + EXPORT_CONSTANT(DBOPT_MB_EMPTY_LIST); + EXPORT_CONSTANT(DBOPT_MB_EMPTY_COUNT); + EXPORT_CONSTANT(DBOPT_MB_REPR_BLOCK_IDX); + EXPORT_CONSTANT(DBOPT_MISSING_VALUE); + EXPORT_CONSTANT(DBOPT_ALT_ZONENUM_VARS); + EXPORT_CONSTANT(DBOPT_ALT_NODENUM_VARS); + EXPORT_CONSTANT(DBOPT_GHOST_NODE_LABELS); + EXPORT_CONSTANT(DBOPT_GHOST_ZONE_LABELS); /* Error trapping method */ EXPORT_CONSTANT(DB_TOP); @@ -200,6 +242,7 @@ namespace EXPORT_CONSTANT(DB_ABORT); EXPORT_CONSTANT(DB_SUSPEND); EXPORT_CONSTANT(DB_RESUME); + EXPORT_CONSTANT(DB_ALL_AND_DRVR); /* Errors */ EXPORT_CONSTANT(E_NOERROR); @@ -213,7 +256,7 @@ namespace EXPORT_CONSTANT(E_NOTFOUND); EXPORT_CONSTANT(E_TAURSTATE); EXPORT_CONSTANT(E_MSERVER); - EXPORT_CONSTANT(E_PROTO ); + EXPORT_CONSTANT(E_PROTO); EXPORT_CONSTANT(E_NOTDIR); EXPORT_CONSTANT(E_MAXOPEN); EXPORT_CONSTANT(E_NOTFILTER); @@ -226,7 +269,17 @@ namespace EXPORT_CONSTANT(E_INVALIDNAME); EXPORT_CONSTANT(E_NOOVERWRITE); EXPORT_CONSTANT(E_CHECKSUM); - EXPORT_CONSTANT(E_NERRORS); + EXPORT_CONSTANT(E_COMPRESSION); + EXPORT_CONSTANT(E_GRABBED); + EXPORT_CONSTANT(E_NOTREG); + EXPORT_CONSTANT(E_CONCURRENT); + EXPORT_CONSTANT(E_DRVRCANTOPEN); + EXPORT_CONSTANT(E_BADOPTCLASS); + EXPORT_CONSTANT(E_NOTENABLEDINBUILD); + EXPORT_CONSTANT(E_MAXFILEOPTSETS); + EXPORT_CONSTANT(E_NOHDF5); + EXPORT_CONSTANT(E_EMPTYOBJECT); + EXPORT_CONSTANT(E_OBJBUFFULL); /* Definitions for MAJOR_ORDER */ EXPORT_CONSTANT(DB_ROWMAJOR); @@ -244,6 +297,8 @@ namespace EXPORT_CONSTANT(DB_ZONECENT); EXPORT_CONSTANT(DB_FACECENT); EXPORT_CONSTANT(DB_BNDCENT); + EXPORT_CONSTANT(DB_EDGECENT); + EXPORT_CONSTANT(DB_BLOCKCENT); /* Definitions for COORD_SYSTEM */ EXPORT_CONSTANT(DB_CARTESIAN); @@ -259,10 +314,15 @@ namespace /* Definitions for PLANAR */ EXPORT_CONSTANT(DB_AREA); EXPORT_CONSTANT(DB_VOLUME); + /* Definitions for flag values */ EXPORT_CONSTANT(DB_ON); EXPORT_CONSTANT(DB_OFF); + /* Definitions for disjoint flags */ + EXPORT_CONSTANT(DB_ABUTTING); + EXPORT_CONSTANT(DB_FLOATING); + /* Definitions for derived variable types */ EXPORT_CONSTANT(DB_VARTYPE_SCALAR); EXPORT_CONSTANT(DB_VARTYPE_VECTOR); @@ -273,6 +333,10 @@ namespace EXPORT_CONSTANT(DB_VARTYPE_SPECIES); EXPORT_CONSTANT(DB_VARTYPE_LABEL); + /* Definitions for ghost labels */ + EXPORT_CONSTANT(DB_GHOSTTYPE_NOGHOST); + EXPORT_CONSTANT(DB_GHOSTTYPE_INTDUP); + /* Definitions for CSG boundary types */ EXPORT_CONSTANT(DBCSG_QUADRIC_G); EXPORT_CONSTANT(DBCSG_SPHERE_PR); @@ -321,6 +385,11 @@ namespace EXPORT_CONSTANT(DBCSG_XFORM); EXPORT_CONSTANT(DBCSG_SWEEP); + /* Definitions for MRG tree traversal flags */ + EXPORT_CONSTANT(DB_PREORDER); + EXPORT_CONSTANT(DB_POSTORDER); + EXPORT_CONSTANT(DB_FROMCWR); + #if PYVISFILE_SILO_VERSION_GE(4,6,1) /* Shape types */ EXPORT_CONSTANT(DB_ZONETYPE_BEAM); @@ -1372,7 +1441,7 @@ PYBIND11_MODULE(_internal, m) .ENUM_VALUE(DB_DOUBLE) .ENUM_VALUE(DB_CHAR) .ENUM_VALUE(DB_NOTYPE) -#if SILO_VERSION_GE(4,7,2) +#if SILO_VERSION_GE(4, 7, 2) .ENUM_VALUE(DB_LONG_LONG) #endif ; @@ -1406,7 +1475,14 @@ PYBIND11_MODULE(_internal, m) .ENUM_VALUE(DB_ARRAY) .ENUM_VALUE(DB_DIR) .ENUM_VALUE(DB_VARIABLE) + .ENUM_VALUE(DB_MRGTREE) + .ENUM_VALUE(DB_GROUPELMAP) + .ENUM_VALUE(DB_MRGVAR) .ENUM_VALUE(DB_USERDEF) + .value("DB_QUADRECT", DB_QUADRECT) + .value("DB_QUADCURV", DB_QUADCURV) + .value("DB_MULTIBLOCKMESH", DB_MULTIMESH) + .value("DB_MULTIBLOCKVAR", DB_MULTIVAR) ; { -- GitLab From 1bf98f3cd554b58d06c1c5663fe1d4884e93fd18 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Thu, 1 Oct 2020 14:22:47 -0500 Subject: [PATCH 15/19] actually export constants --- pyvisfile/silo/__init__.py | 105 +++++++++++++++++++++++++++++++++---- src/wrapper/wrap_silo.cpp | 6 +-- 2 files changed, 97 insertions(+), 14 deletions(-) diff --git a/pyvisfile/silo/__init__.py b/pyvisfile/silo/__init__.py index 99838a5..33ed991 100644 --- a/pyvisfile/silo/__init__.py +++ b/pyvisfile/silo/__init__.py @@ -45,22 +45,107 @@ except ImportError: from pyvisfile.silo._internal import ( # noqa: F401 get_silo_version, set_deprecate_warnings, - # enums DBObjectType, DBdatatype, - # classes DBToc, DBCurve, DBQuadMesh, DBQuadVar, IntVector, + ) - # constants - DB_LOCAL, DB_COLLINEAR, DB_CLOBBER, DB_NOCLOBBER, DB_PDB, DB_NODECENT, - DB_HDF5, DB_READ, DB_UNKNOWN, DB_APPEND, - DB_ZONETYPE_TRIANGLE, DB_ZONECENT, - - DBOPT_CYCLE, DBOPT_DTIME, DBOPT_XUNITS, DBOPT_YUNITS, DBOPT_ZUNITS, - DBOPT_XLABEL, DBOPT_YLABEL, DBOPT_ZLABEL, - DBOPT_UNITS, DBOPT_HI_OFFSET, DBOPT_LO_OFFSET +from pyvisfile.silo._internal import ( # noqa: F401 + DB_NETCDF, DB_PDB, DB_TAURUS, DB_UNKNOWN, DB_DEBUG, DB_HDF5X, + DB_PDBP, DB_HDF5, + ) +from pyvisfile.silo._internal import DB_CLOBBER, DB_NOCLOBBER # noqa: F401 +from pyvisfile.silo._internal import DB_READ, DB_APPEND # noqa: F401 +from pyvisfile.silo._internal import ( # noqa: F401 + DB_LOCAL, DB_SUN3, DB_SUN4, DB_SGI, DB_RS6000, DB_CRAY, DB_INTEL, + ) +from pyvisfile.silo._internal import ( # noqa: F401 + DBOPT_ALIGN, DBOPT_COORDSYS, DBOPT_CYCLE, DBOPT_FACETYPE, DBOPT_HI_OFFSET, + DBOPT_LO_OFFSET, DBOPT_LABEL, DBOPT_XLABEL, DBOPT_YLABEL, DBOPT_ZLABEL, + DBOPT_MAJORORDER, DBOPT_NSPACE, DBOPT_ORIGIN, DBOPT_PLANAR, DBOPT_TIME, + DBOPT_UNITS, DBOPT_XUNITS, DBOPT_YUNITS, DBOPT_ZUNITS, DBOPT_DTIME, + DBOPT_USESPECMF, DBOPT_XVARNAME, DBOPT_YVARNAME, DBOPT_ZVARNAME, + DBOPT_ASCII_LABEL, DBOPT_MATNOS, DBOPT_NMATNOS, DBOPT_MATNAME, DBOPT_NMAT, + DBOPT_NMATSPEC, DBOPT_BASEINDEX, DBOPT_ZONENUM, DBOPT_NODENUM, + DBOPT_BLOCKORIGIN, DBOPT_GROUPNUM, DBOPT_GROUPORIGIN, DBOPT_NGROUPS, + DBOPT_MATNAMES, DBOPT_EXTENTS_SIZE, DBOPT_EXTENTS, DBOPT_MATCOUNTS, + DBOPT_MATLISTS, DBOPT_MIXLENS, DBOPT_ZONECOUNTS, DBOPT_HAS_EXTERNAL_ZONES, + DBOPT_PHZONELIST, DBOPT_MATCOLORS, DBOPT_BNDNAMES, DBOPT_REGNAMES, + DBOPT_ZONENAMES, DBOPT_HIDE_FROM_GUI, DBOPT_TOPO_DIM, DBOPT_REFERENCE, + DBOPT_GROUPINGS_SIZE, DBOPT_GROUPINGS, DBOPT_GROUPINGNAMES, DBOPT_ALLOWMAT0, + DBOPT_MRGTREE_NAME, DBOPT_REGION_PNAMES, DBOPT_TENSOR_RANK, DBOPT_MMESH_NAME, + DBOPT_TV_CONNECTIVITY, DBOPT_DISJOINT_MODE, DBOPT_MRGV_ONAMES, + DBOPT_MRGV_RNAMES, DBOPT_SPECNAMES, DBOPT_SPECCOLORS, DBOPT_LLONGNZNUM, + DBOPT_CONSERVED, DBOPT_EXTENSIVE, DBOPT_MB_FILE_NS, DBOPT_MB_BLOCK_NS, + DBOPT_MB_BLOCK_TYPE, DBOPT_MB_EMPTY_LIST, DBOPT_MB_EMPTY_COUNT, + DBOPT_MB_REPR_BLOCK_IDX, DBOPT_MISSING_VALUE, DBOPT_ALT_ZONENUM_VARS, + DBOPT_ALT_NODENUM_VARS, DBOPT_GHOST_NODE_LABELS, DBOPT_GHOST_ZONE_LABELS, + ) +from pyvisfile.silo._internal import ( # noqa: F401 + DB_TOP, DB_NONE, DB_ALL, DB_ABORT, DB_SUSPEND, DB_RESUME, DB_ALL_AND_DRVR, + ) +from pyvisfile.silo._internal import ( # noqa: F401 + E_NOERROR, E_BADFTYPE, E_NOTIMP, E_NOFILE, E_INTERNAL, E_NOMEM, E_BADARGS, + E_CALLFAIL, E_NOTFOUND, E_TAURSTATE, E_MSERVER, E_PROTO, E_NOTDIR, + E_MAXOPEN, E_NOTFILTER, E_MAXFILTERS, E_FEXIST, E_FILEISDIR, E_FILENOREAD, + E_SYSTEMERR, E_FILENOWRITE, E_INVALIDNAME, E_NOOVERWRITE, E_CHECKSUM, + E_COMPRESSION, E_GRABBED, E_NOTREG, E_CONCURRENT, E_DRVRCANTOPEN, + E_BADOPTCLASS, E_NOTENABLEDINBUILD, E_MAXFILEOPTSETS, E_NOHDF5, + E_EMPTYOBJECT, E_OBJBUFFULL, + ) +from pyvisfile.silo._internal import DB_ROWMAJOR, DB_COLMAJOR # noqa: F401 +from pyvisfile.silo._internal import ( # noqa: F401 + DB_COLLINEAR, DB_NONCOLLINEAR, DB_QUAD_RECT, DB_QUAD_CURV, ) +from pyvisfile.silo._internal import ( # noqa: F401 + DB_NOTCENT, DB_NODECENT, DB_ZONECENT, DB_FACECENT, DB_BNDCENT, + DB_EDGECENT, DB_BLOCKCENT, + ) +from pyvisfile.silo._internal import ( # noqa: F401 + DB_CARTESIAN, DB_CYLINDRICAL, DB_SPHERICAL, DB_NUMERICAL, DB_OTHER, + ) +from pyvisfile.silo._internal import DB_RECTILINEAR, DB_CURVILINEAR # noqa: F401 +from pyvisfile.silo._internal import DB_AREA, DB_VOLUME # noqa: F401 +from pyvisfile.silo._internal import DB_ON, DB_OFF # noqa: F401 +from pyvisfile.silo._internal import DB_ABUTTING, DB_FLOATING # noqa: F401 +from pyvisfile.silo._internal import ( # noqa: F401 + DB_VARTYPE_SCALAR, DB_VARTYPE_VECTOR, DB_VARTYPE_TENSOR, + DB_VARTYPE_SYMTENSOR, DB_VARTYPE_ARRAY, DB_VARTYPE_MATERIAL, + DB_VARTYPE_SPECIES, DB_VARTYPE_LABEL, + ) +from pyvisfile.silo._internal import ( # noqa: F401 + DB_GHOSTTYPE_NOGHOST, DB_GHOSTTYPE_INTDUP, + ) +from pyvisfile.silo._internal import ( # noqa: F401 + DBCSG_QUADRIC_G, DBCSG_SPHERE_PR, DBCSG_ELLIPSOID_PRRR, DBCSG_PLANE_G, + DBCSG_PLANE_X, DBCSG_PLANE_Y, DBCSG_PLANE_Z, DBCSG_PLANE_PN, + DBCSG_PLANE_PPP, DBCSG_CYLINDER_PNLR, DBCSG_CYLINDER_PPR, + DBCSG_BOX_XYZXYZ, DBCSG_CONE_PNLA, DBCSG_CONE_PPA, DBCSG_POLYHEDRON_KF, + DBCSG_HEX_6F, DBCSG_TET_4F, DBCSG_PYRAMID_5F, DBCSG_PRISM_5F, + ) +from pyvisfile.silo._internal import ( # noqa: F401 + DBCSG_QUADRATIC_G, DBCSG_CIRCLE_PR, DBCSG_ELLIPSE_PRR, DBCSG_LINE_G, + DBCSG_LINE_X, DBCSG_LINE_Y, DBCSG_LINE_PN, DBCSG_LINE_PP, DBCSG_BOX_XYXY, + DBCSG_ANGLE_PNLA, DBCSG_ANGLE_PPA, DBCSG_POLYGON_KP, DBCSG_TRI_3P, + DBCSG_QUAD_4P, + ) +from pyvisfile.silo._internal import ( # noqa: F401 + DBCSG_INNER, DBCSG_OUTER, DBCSG_ON, DBCSG_UNION, DBCSG_INTERSECT, + DBCSG_DIFF, DBCSG_COMPLIMENT, DBCSG_XFORM, DBCSG_SWEEP, + ) + +if get_silo_version() >= (4, 6, 1): + from pyvisfile.silo._internal import ( # noqa: F401 + DB_HDF5_SEC2, DB_HDF5_STDIO, DB_HDF5_CORE, DB_HDF5_LOG, + DB_HDF5_SPLIT, DB_HDF5_DIRECT, DB_HDF5_FAMILY, DB_HDF5_MPIO, + DB_HDF5_MPIOP, DB_HDF5_MPIP, DB_HDF5_SILO, + ) + from pyvisfile.silo._internal import ( # noqa: F401 + DB_ZONETYPE_BEAM, DB_ZONETYPE_TRIANGLE, DB_ZONETYPE_QUAD, + DB_ZONETYPE_POLYHEDRON, DB_ZONETYPE_TET, DB_ZONETYPE_PYRAMID, + DB_ZONETYPE_PRISM, DB_ZONETYPE_HEX, + ) def _convert_optlist(ol_dict): diff --git a/src/wrapper/wrap_silo.cpp b/src/wrapper/wrap_silo.cpp index 695673b..50773c4 100644 --- a/src/wrapper/wrap_silo.cpp +++ b/src/wrapper/wrap_silo.cpp @@ -1448,6 +1448,8 @@ PYBIND11_MODULE(_internal, m) py::enum_(m, "DBObjectType") .ENUM_VALUE(DB_INVALID_OBJECT) + .ENUM_VALUE(DB_QUADRECT) + .ENUM_VALUE(DB_QUADCURV) .ENUM_VALUE(DB_QUADMESH) .ENUM_VALUE(DB_QUADVAR) .ENUM_VALUE(DB_UCDMESH) @@ -1479,10 +1481,6 @@ PYBIND11_MODULE(_internal, m) .ENUM_VALUE(DB_GROUPELMAP) .ENUM_VALUE(DB_MRGVAR) .ENUM_VALUE(DB_USERDEF) - .value("DB_QUADRECT", DB_QUADRECT) - .value("DB_QUADCURV", DB_QUADCURV) - .value("DB_MULTIBLOCKMESH", DB_MULTIMESH) - .value("DB_MULTIBLOCKVAR", DB_MULTIVAR) ; { -- GitLab From 6233489f6d69497c5870f54bccd912f28104e35d Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Thu, 1 Oct 2020 15:24:45 -0500 Subject: [PATCH 16/19] restore string formatting --- src/wrapper/wrap_silo.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/wrapper/wrap_silo.cpp b/src/wrapper/wrap_silo.cpp index 50773c4..9a0be8c 100644 --- a/src/wrapper/wrap_silo.cpp +++ b/src/wrapper/wrap_silo.cpp @@ -74,6 +74,19 @@ namespace { // {{{ helpers + // https://stackoverflow.com/a/26221725 + template + char *string_format(const std::string& format, Args ... args) + { + size_t size = snprintf(nullptr, 0, format.c_str(), args ...) + 1; + if (size <= 0) + throw std::runtime_error("Error during formatting."); + + std::unique_ptr buf(new char[size]); + snprintf(buf.get(), size, format.c_str(), args ...); + return buf.get(); + } + // https://stackoverflow.com/a/44175911 class noncopyable { public: @@ -989,7 +1002,9 @@ namespace first = false; } else if (vlength != int(v.size())) - PYTHON_ERROR(ValueError, "field components need to have matching lengths"); + PYTHON_ERROR(ValueError, string_format( + "field components of '%s' need to have matching lengths", + vname)); vars.push_back((float *) v.data()); } @@ -1132,7 +1147,9 @@ namespace first = false; } else if (vlength != int(v.size())) - PYTHON_ERROR(ValueError, "field components need to have matching lengths"); + PYTHON_ERROR(ValueError, string_format( + "field components of '%s' need to have matching lengths", + vname)); vars.push_back((float *) v.data()); } @@ -1230,7 +1247,9 @@ namespace first = false; } else if (vlength != int(v.size())) - PYTHON_ERROR(ValueError, "field components need to have matching lengths"); + PYTHON_ERROR(ValueError, string_format( + "field components of '%s' need to have matching lengths", + vname)); vars.push_back((float *) v.data()); } -- GitLab From 3dab5d9b34ddca2282a9461b7434188de51c7d11 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Wed, 7 Oct 2020 15:33:44 -0500 Subject: [PATCH 17/19] Add minimal CI testing --- examples/silo-unstructured.py | 368 ------------------------- examples/vtk-structured.py | 24 -- examples/vtk-unstructured-points.py | 33 --- test/test_silo.py | 410 ++++++++++++++++++++++++++++ test/test_vtk.py | 60 ++++ 5 files changed, 470 insertions(+), 425 deletions(-) delete mode 100644 examples/silo-unstructured.py delete mode 100644 examples/vtk-structured.py delete mode 100644 examples/vtk-unstructured-points.py create mode 100644 test/test_silo.py create mode 100644 test/test_vtk.py diff --git a/examples/silo-unstructured.py b/examples/silo-unstructured.py deleted file mode 100644 index 331541b..0000000 --- a/examples/silo-unstructured.py +++ /dev/null @@ -1,368 +0,0 @@ -import numpy as np - -# flake8: noqa: E201 - -class Mesh: - dimensions = 2 - - def __init__(self): - self.vertex_coordinates = np.array([ - [ 1.00000000e+00, 1.00000000e+00], - [-1.00000000e+00, 1.00000000e+00], - [-1.00000000e+00, -1.00000000e+00], - [ 1.00000000e+00, -1.00000000e+00], - [ 1.00000000e+00, 0.00000000e+00], - [ 5.00000000e-01, 0.00000000e+00], - [ 4.04508497e-01, 2.93892626e-01], - [ 1.54508497e-01, 4.75528258e-01], - [-1.54508497e-01, 4.75528258e-01], - [-4.04508497e-01, 2.93892626e-01], - [-5.00000000e-01, 6.12323400e-17], - [-4.04508497e-01, -2.93892626e-01], - [-1.54508497e-01, -4.75528258e-01], - [ 1.54508497e-01, -4.75528258e-01], - [ 4.04508497e-01, -2.93892626e-01], - [ 5.00000000e-01, -1.22464680e-16], - [-1.00000000e+00, 0.00000000e+00], - [ 0.00000000e+00, -1.00000000e+00], - [ 0.00000000e+00, 1.00000000e+00], - [ 7.50000000e-01, -6.12323400e-17], - [ 3.76380160e-01, -6.92475869e-01], - [ 2.53742158e-01, 7.19073297e-01], - [ 6.28756830e-01, 4.63210153e-01], - [-5.74443055e-01, -3.53084198e-01], - [-6.18581756e-01, 2.31042725e-01], - [-2.26660566e-01, -6.52489609e-01], - [-1.38777878e-16, -7.15005157e-01], - [-8.32667268e-17, -4.75528258e-01], - [ 1.31420055e-01, -6.30214034e-01], - [ 3.52012743e-01, -5.60408551e-01], - [ 6.68920108e-01, -6.90243593e-01], - [-7.72542486e-02, -5.95266707e-01], - [ 3.23649069e-02, -5.81200949e-01], - [ 7.72542486e-02, -5.09659838e-01], - [-7.72542486e-02, -5.10475584e-01], - [-3.86612967e-01, -6.49034477e-01], - [-5.00000000e-01, -1.00000000e+00], - [-2.65876650e-01, -4.97380593e-01], - [-2.79508497e-01, -3.84710442e-01], - [ 5.00000000e-01, -1.00000000e+00], - [ 2.79508497e-01, -3.84710442e-01], - [ 2.49036745e-01, -4.74202452e-01], - [ 4.17444286e-01, -4.43129989e-01], - [ 3.66059344e-01, -3.72404685e-01], - [ 1.00000000e+00, -5.00000000e-01], - [-2.17008497e-01, -4.30119350e-01], - [-4.29734374e-01, -4.60045845e-01], - [-3.74119094e-01, -3.83497979e-01], - [-1.00000000e+00, -5.00000000e-01], - [ 5.57224765e-01, -3.55834603e-01], - [ 7.21409218e-01, -3.41603211e-01], - [-4.52254249e-01, -1.46946313e-01], - [ 1.85001438e-01, -8.57502578e-01], - [ 6.23193603e-01, -1.62706897e-01], - [ 4.52254249e-01, -1.46946313e-01], - [ 5.12185038e-01, -2.47648931e-01], - [ 4.28381373e-01, -2.20419470e-01], - [ 5.43214916e-01, -9.52713015e-02], - [-6.92549940e-01, -1.43793192e-01], - [-5.15515268e-01, -2.48730988e-01], - [-4.28381373e-01, -2.20419470e-01], - [-5.71866870e-01, -1.04580885e-01], - [-4.76127124e-01, -7.34731565e-02], - [-6.89960366e-01, 5.35552226e-02], - [-2.77500572e-01, -8.09255088e-01], - [-7.30529084e-01, 3.04896602e-01], - [-4.52254249e-01, 1.46946313e-01], - [-5.72946346e-01, 1.04931629e-01], - [-4.76127124e-01, 7.34731565e-02], - [-4.84687225e-01, 2.38714350e-01], - [-4.19217263e-01, 5.95504869e-01], - [-6.41386449e-01, 5.21452559e-01], - [-1.00000000e+00, 5.00000000e-01], - [-5.29677214e-01, 7.27277579e-01], - [-2.79508497e-01, 3.84710442e-01], - [-3.98323250e-01, 4.16812142e-01], - [-3.42008497e-01, 3.39301534e-01], - [-2.72282842e-01, 5.06197960e-01], - [-2.17008497e-01, 4.30119350e-01], - [-5.00000000e-01, 1.00000000e+00], - [ 7.72542486e-02, 7.19631585e-01], - [ 2.77555756e-17, 4.75528258e-01], - [-7.72542486e-02, 5.73039943e-01], - [-7.72542486e-02, 4.75528258e-01], - [ 7.72542486e-02, 5.85355142e-01], - [ 7.72542486e-02, 4.75528258e-01], - [ 4.28743328e-01, 7.41939961e-01], - [-1.00000000e+00, -2.50000000e-01], - [ 2.79508497e-01, 3.84710442e-01], - [ 2.50009912e-01, 5.42623972e-01], - [ 5.00000000e-01, 1.00000000e+00], - [ 2.37711541e-01, 4.58614646e-01], - [ 3.65111148e-01, 4.81894882e-01], - [ 3.75966372e-01, 3.86040540e-01], - [ 1.00000000e+00, 5.00000000e-01], - [-5.43296861e-01, -7.92213176e-01], - [-7.96662390e-01, -3.40516303e-01], - [-6.52705475e-01, -6.10334816e-01], - [ 8.75000000e-01, -1.82460056e-01], - [-2.09778923e-01, 6.41770688e-01], - [-1.04175415e-01, 8.20467199e-01], - [-5.56672529e-01, 3.83610683e-01], - [-8.27449562e-01, 7.55663220e-01], - [-2.50000000e-01, 1.00000000e+00], - [ 7.50000000e-01, 0.00000000e+00], - [ 5.04937767e-01, 5.85487598e-01], - [ 7.87954754e-01, 7.48243953e-01], - [-8.66944614e-01, 1.53933167e-01], - [ 5.52449169e-01, 3.31115121e-01], - [ 4.52254249e-01, 1.46946313e-01], - [ 4.96110705e-01, 2.42426064e-01], - [ 5.77759442e-01, 1.47110460e-01], - [ 4.76127124e-01, 7.34731565e-02], - [ 6.25000000e-01, 0.00000000e+00], - [ 8.06678337e-01, 2.72341866e-01], - [ 7.26562966e-01, 1.46946320e-01], - [ 7.50000000e-01, -1.00000000e+00], - [ 7.97833359e-01, -5.31380557e-01], - [-3.36478386e-01, 8.08917810e-01], - [-8.88250684e-01, -7.50000000e-01], - [ 2.50000000e-01, 1.00000000e+00]]) - self.elements = np.array([[ 50, 44, 98], - [ 25, 37, 35], - [ 99, 8, 82], - [ 25, 31, 12], - [ 98, 19, 53], - [ 21, 120, 80], - [ 98, 53, 50], - [ 60, 51, 59], - [ 73, 71, 70], - [ 44, 50, 117], - [ 9, 75, 101], - [ 69, 9, 101], - [106, 90, 86], - [105, 86, 92], - [ 44, 117, 3], - [ 49, 14, 42], - [ 56, 14, 55], - [ 13, 27, 33], - [ 26, 52, 28], - [118, 73, 70], - [108, 93, 6], - [ 30, 29, 20], - [ 92, 93, 108], - [ 23, 96, 97], - [ 35, 97, 95], - [ 89, 91, 92], - [ 24, 67, 69], - [ 14, 43, 42], - [ 73, 79, 102], - [119, 97, 48], - [ 64, 17, 26], - [ 26, 31, 25], - [ 32, 26, 28], - [ 52, 26, 17], - [ 32, 34, 31], - [ 28, 13, 33], - [ 58, 16, 87], - [ 17, 39, 52], - [ 39, 20, 52], - [ 49, 50, 53], - [ 28, 52, 20], - [ 25, 12, 37], - [ 46, 35, 37], - [ 34, 32, 27], - [ 26, 32, 31], - [ 27, 32, 33], - [ 28, 33, 32], - [ 27, 12, 34], - [ 12, 31, 34], - [ 47, 38, 11], - [ 26, 25, 64], - [ 38, 47, 37], - [ 17, 64, 36], - [ 46, 47, 23], - [ 45, 38, 37], - [ 58, 61, 63], - [ 97, 35, 46], - [ 28, 29, 41], - [ 40, 41, 42], - [ 3, 30, 116], - [ 41, 13, 28], - [ 40, 13, 41], - [ 19, 98, 4], - [ 29, 42, 41], - [ 14, 40, 43], - [ 40, 42, 43], - [ 42, 29, 30], - [ 49, 30, 117], - [ 37, 12, 45], - [ 64, 35, 95], - [ 23, 47, 11], - [ 37, 47, 46], - [ 48, 97, 96], - [ 35, 64, 25], - [ 42, 30, 49], - [ 28, 20, 29], - [ 39, 30, 20], - [ 49, 55, 14], - [ 10, 61, 62], - [ 23, 11, 59], - [ 57, 54, 55], - [ 15, 57, 19], - [ 49, 53, 55], - [ 19, 57, 53], - [ 57, 55, 53], - [ 55, 54, 56], - [ 15, 54, 57], - [ 36, 95, 119], - [ 61, 59, 51], - [ 58, 23, 59], - [101, 24, 69], - [ 59, 11, 60], - [ 59, 61, 58], - [ 9, 69, 66], - [ 61, 51, 62], - [ 63, 67, 24], - [ 87, 96, 58], - [ 46, 23, 97], - [ 63, 10, 67], - [ 68, 66, 67], - [ 10, 63, 61], - [ 69, 67, 66], - [ 63, 24, 65], - [102, 79, 1], - [ 67, 10, 68], - [ 70, 71, 101], - [118, 70, 99], - [ 70, 101, 75], - [118, 79, 73], - [ 72, 65, 71], - [100, 82, 80], - [ 99, 77, 8], - [ 8, 77, 78], - [ 70, 75, 77], - [ 63, 107, 16], - [ 76, 74, 75], - [ 75, 74, 77], - [ 89, 21, 84], - [ 75, 9, 76], - [103, 118, 100], - [ 85, 7, 84], - [ 77, 74, 78], - [ 99, 70, 77], - [ 80, 84, 21], - [ 83, 81, 82], - [ 84, 82, 81], - [ 80, 18, 100], - [103, 100, 18], - [ 84, 7, 89], - [ 82, 8, 83], - [ 82, 84, 80], - [ 89, 7, 91], - [ 84, 81, 85], - [ 86, 90, 120], - [ 87, 48, 96], - [109, 112, 111], - [ 89, 86, 21], - [ 92, 86, 89], - [106, 105, 22], - [105, 106, 86], - [ 7, 88, 91], - [ 92, 91, 88], - [ 93, 92, 88], - [ 18, 80, 120], - [ 88, 6, 93], - [111, 115, 108], - [111, 108, 110], - [ 95, 97, 119], - [101, 71, 65], - [ 1, 72, 102], - [ 16, 58, 63], - [ 58, 96, 23], - [ 64, 95, 36], - [ 44, 4, 98], - [102, 71, 73], - [ 63, 65, 107], - [100, 118, 99], - [ 24, 101, 65], - [ 72, 71, 102], - [ 82, 100, 99], - [115, 111, 113], - [108, 114, 22], - [108, 105, 92], - [ 22, 94, 106], - [ 94, 0, 106], - [ 90, 106, 0], - [ 65, 72, 107], - [ 72, 16, 107], - [ 22, 105, 108], - [110, 108, 6], - [ 6, 109, 110], - [109, 111, 110], - [104, 115, 113], - [ 4, 94, 114], - [114, 115, 4], - [112, 113, 111], - [113, 112, 5], - [ 94, 22, 114], - [ 4, 115, 104], - [108, 115, 114], - [ 49, 117, 50], - [ 30, 39, 116], - [ 3, 117, 30], - [ 79, 118, 103], - [ 48, 2, 119], - [119, 2, 36], - [ 86, 120, 21]]) - - def __len__(self): - return len(self.elements) - - -def add_to_silo_file(silo, mesh, cell_data=[], point_data=[], - line_integral_rules=[], - mesh_name="mesh", - real_only=False): - zonelist_name = mesh_name + "_zonelist" - - from pyvisfile.silo import IntVector - - nodelist = IntVector() - nodelist.extend(int(i) for i in mesh.elements.flat) - - shapetypes = IntVector() - from pyvisfile.silo import DB_ZONETYPE_TRIANGLE - shapetypes.append(DB_ZONETYPE_TRIANGLE) - - shapesizes = IntVector() - shapesizes.append(3) - - shapecounts = IntVector() - shapecounts.append(len(mesh)) - - silo.put_zonelist_2(zonelist_name, len(mesh), mesh.dimensions, nodelist, - 0, 0, shapetypes, shapesizes, shapecounts) - - silo.put_ucdmesh(mesh_name, [], - np.asarray(mesh.vertex_coordinates.T, order="C"), len(mesh), - zonelist_name, None) - - from pyvisfile.silo import DB_NODECENT, DB_ZONECENT - silo.put_ucdvar1("myvar", mesh_name, - np.arange(mesh.vertex_coordinates.shape[0], dtype=np.double), - DB_NODECENT) - silo.put_ucdvar1("myvar2", mesh_name, - np.arange(len(mesh), dtype=np.double), - DB_ZONECENT) - - -def main(): - from pyvisfile.silo import SiloFile - silo = SiloFile("out.silo") - add_to_silo_file(silo, Mesh()) - silo.close() - - -if __name__ == "__main__": - main() diff --git a/examples/vtk-structured.py b/examples/vtk-structured.py deleted file mode 100644 index 3ef71d8..0000000 --- a/examples/vtk-structured.py +++ /dev/null @@ -1,24 +0,0 @@ -from pyvisfile.vtk import write_structured_grid - -import numpy as np - -angle_mesh = np.mgrid[1:2:10j, 0:2*np.pi:20j, 0:np.pi:30j] - -r = angle_mesh[0, np.newaxis] -phi = angle_mesh[1, np.newaxis] -theta = angle_mesh[2, np.newaxis] -mesh = np.vstack(( - r*np.sin(theta)*np.cos(phi), - r*np.sin(theta)*np.sin(phi), - r*np.cos(theta), - )) - -from pytools.obj_array import make_obj_array -vec = make_obj_array([ - np.sin(theta)*np.cos(phi), - np.sin(theta)*np.sin(phi), - np.cos(theta), - ]) - -write_structured_grid("yo.vts", mesh, - point_data=[("phi", phi), ("vec", vec)]) diff --git a/examples/vtk-unstructured-points.py b/examples/vtk-unstructured-points.py deleted file mode 100644 index bba2aa3..0000000 --- a/examples/vtk-unstructured-points.py +++ /dev/null @@ -1,33 +0,0 @@ -import numpy as np -from pyvisfile.vtk import ( - UnstructuredGrid, DataArray, - AppendedDataXMLGenerator, - VTK_VERTEX, VF_LIST_OF_VECTORS, VF_LIST_OF_COMPONENTS) - -n = 5000 -points = np.random.randn(n, 3) - -data = [ - ("p", np.random.randn(n)), - ("vel", np.random.randn(3, n)), -] -file_name = "points.vtu" -compressor = None - -grid = UnstructuredGrid( - (n, DataArray("points", points, vector_format=VF_LIST_OF_VECTORS)), - cells=np.arange(n, dtype=np.uint32), - cell_types=np.asarray([VTK_VERTEX] * n, dtype=np.uint8)) - -for name, field in data: - grid.add_pointdata(DataArray(name, field, - vector_format=VF_LIST_OF_COMPONENTS)) - -from os.path import exists -if exists(file_name): - raise RuntimeError("output file '%s' already exists" - % file_name) - -outf = open(file_name, "w") -AppendedDataXMLGenerator(compressor)(grid).write(outf) -outf.close() diff --git a/test/test_silo.py b/test/test_silo.py new file mode 100644 index 0000000..3fd72a1 --- /dev/null +++ b/test/test_silo.py @@ -0,0 +1,410 @@ +import numpy as np + + +# {{{ mesh object thingy + + +class Mesh: + dimensions = 2 + + def __init__(self): + self.vertex_coordinates = np.array( + [ + [1.00000000e00, 1.00000000e00], + [-1.00000000e00, 1.00000000e00], + [-1.00000000e00, -1.00000000e00], + [1.00000000e00, -1.00000000e00], + [1.00000000e00, 0.00000000e00], + [5.00000000e-01, 0.00000000e00], + [4.04508497e-01, 2.93892626e-01], + [1.54508497e-01, 4.75528258e-01], + [-1.54508497e-01, 4.75528258e-01], + [-4.04508497e-01, 2.93892626e-01], + [-5.00000000e-01, 6.12323400e-17], + [-4.04508497e-01, -2.93892626e-01], + [-1.54508497e-01, -4.75528258e-01], + [1.54508497e-01, -4.75528258e-01], + [4.04508497e-01, -2.93892626e-01], + [5.00000000e-01, -1.22464680e-16], + [-1.00000000e00, 0.00000000e00], + [0.00000000e00, -1.00000000e00], + [0.00000000e00, 1.00000000e00], + [7.50000000e-01, -6.12323400e-17], + [3.76380160e-01, -6.92475869e-01], + [2.53742158e-01, 7.19073297e-01], + [6.28756830e-01, 4.63210153e-01], + [-5.74443055e-01, -3.53084198e-01], + [-6.18581756e-01, 2.31042725e-01], + [-2.26660566e-01, -6.52489609e-01], + [-1.38777878e-16, -7.15005157e-01], + [-8.32667268e-17, -4.75528258e-01], + [1.31420055e-01, -6.30214034e-01], + [3.52012743e-01, -5.60408551e-01], + [6.68920108e-01, -6.90243593e-01], + [-7.72542486e-02, -5.95266707e-01], + [3.23649069e-02, -5.81200949e-01], + [7.72542486e-02, -5.09659838e-01], + [-7.72542486e-02, -5.10475584e-01], + [-3.86612967e-01, -6.49034477e-01], + [-5.00000000e-01, -1.00000000e00], + [-2.65876650e-01, -4.97380593e-01], + [-2.79508497e-01, -3.84710442e-01], + [5.00000000e-01, -1.00000000e00], + [2.79508497e-01, -3.84710442e-01], + [2.49036745e-01, -4.74202452e-01], + [4.17444286e-01, -4.43129989e-01], + [3.66059344e-01, -3.72404685e-01], + [1.00000000e00, -5.00000000e-01], + [-2.17008497e-01, -4.30119350e-01], + [-4.29734374e-01, -4.60045845e-01], + [-3.74119094e-01, -3.83497979e-01], + [-1.00000000e00, -5.00000000e-01], + [5.57224765e-01, -3.55834603e-01], + [7.21409218e-01, -3.41603211e-01], + [-4.52254249e-01, -1.46946313e-01], + [1.85001438e-01, -8.57502578e-01], + [6.23193603e-01, -1.62706897e-01], + [4.52254249e-01, -1.46946313e-01], + [5.12185038e-01, -2.47648931e-01], + [4.28381373e-01, -2.20419470e-01], + [5.43214916e-01, -9.52713015e-02], + [-6.92549940e-01, -1.43793192e-01], + [-5.15515268e-01, -2.48730988e-01], + [-4.28381373e-01, -2.20419470e-01], + [-5.71866870e-01, -1.04580885e-01], + [-4.76127124e-01, -7.34731565e-02], + [-6.89960366e-01, 5.35552226e-02], + [-2.77500572e-01, -8.09255088e-01], + [-7.30529084e-01, 3.04896602e-01], + [-4.52254249e-01, 1.46946313e-01], + [-5.72946346e-01, 1.04931629e-01], + [-4.76127124e-01, 7.34731565e-02], + [-4.84687225e-01, 2.38714350e-01], + [-4.19217263e-01, 5.95504869e-01], + [-6.41386449e-01, 5.21452559e-01], + [-1.00000000e00, 5.00000000e-01], + [-5.29677214e-01, 7.27277579e-01], + [-2.79508497e-01, 3.84710442e-01], + [-3.98323250e-01, 4.16812142e-01], + [-3.42008497e-01, 3.39301534e-01], + [-2.72282842e-01, 5.06197960e-01], + [-2.17008497e-01, 4.30119350e-01], + [-5.00000000e-01, 1.00000000e00], + [7.72542486e-02, 7.19631585e-01], + [2.77555756e-17, 4.75528258e-01], + [-7.72542486e-02, 5.73039943e-01], + [-7.72542486e-02, 4.75528258e-01], + [7.72542486e-02, 5.85355142e-01], + [7.72542486e-02, 4.75528258e-01], + [4.28743328e-01, 7.41939961e-01], + [-1.00000000e00, -2.50000000e-01], + [2.79508497e-01, 3.84710442e-01], + [2.50009912e-01, 5.42623972e-01], + [5.00000000e-01, 1.00000000e00], + [2.37711541e-01, 4.58614646e-01], + [3.65111148e-01, 4.81894882e-01], + [3.75966372e-01, 3.86040540e-01], + [1.00000000e00, 5.00000000e-01], + [-5.43296861e-01, -7.92213176e-01], + [-7.96662390e-01, -3.40516303e-01], + [-6.52705475e-01, -6.10334816e-01], + [8.75000000e-01, -1.82460056e-01], + [-2.09778923e-01, 6.41770688e-01], + [-1.04175415e-01, 8.20467199e-01], + [-5.56672529e-01, 3.83610683e-01], + [-8.27449562e-01, 7.55663220e-01], + [-2.50000000e-01, 1.00000000e00], + [7.50000000e-01, 0.00000000e00], + [5.04937767e-01, 5.85487598e-01], + [7.87954754e-01, 7.48243953e-01], + [-8.66944614e-01, 1.53933167e-01], + [5.52449169e-01, 3.31115121e-01], + [4.52254249e-01, 1.46946313e-01], + [4.96110705e-01, 2.42426064e-01], + [5.77759442e-01, 1.47110460e-01], + [4.76127124e-01, 7.34731565e-02], + [6.25000000e-01, 0.00000000e00], + [8.06678337e-01, 2.72341866e-01], + [7.26562966e-01, 1.46946320e-01], + [7.50000000e-01, -1.00000000e00], + [7.97833359e-01, -5.31380557e-01], + [-3.36478386e-01, 8.08917810e-01], + [-8.88250684e-01, -7.50000000e-01], + [2.50000000e-01, 1.00000000e00], + ] + ) + self.elements = np.array( + [ + [50, 44, 98], + [25, 37, 35], + [99, 8, 82], + [25, 31, 12], + [98, 19, 53], + [21, 120, 80], + [98, 53, 50], + [60, 51, 59], + [73, 71, 70], + [44, 50, 117], + [9, 75, 101], + [69, 9, 101], + [106, 90, 86], + [105, 86, 92], + [44, 117, 3], + [49, 14, 42], + [56, 14, 55], + [13, 27, 33], + [26, 52, 28], + [118, 73, 70], + [108, 93, 6], + [30, 29, 20], + [92, 93, 108], + [23, 96, 97], + [35, 97, 95], + [89, 91, 92], + [24, 67, 69], + [14, 43, 42], + [73, 79, 102], + [119, 97, 48], + [64, 17, 26], + [26, 31, 25], + [32, 26, 28], + [52, 26, 17], + [32, 34, 31], + [28, 13, 33], + [58, 16, 87], + [17, 39, 52], + [39, 20, 52], + [49, 50, 53], + [28, 52, 20], + [25, 12, 37], + [46, 35, 37], + [34, 32, 27], + [26, 32, 31], + [27, 32, 33], + [28, 33, 32], + [27, 12, 34], + [12, 31, 34], + [47, 38, 11], + [26, 25, 64], + [38, 47, 37], + [17, 64, 36], + [46, 47, 23], + [45, 38, 37], + [58, 61, 63], + [97, 35, 46], + [28, 29, 41], + [40, 41, 42], + [3, 30, 116], + [41, 13, 28], + [40, 13, 41], + [19, 98, 4], + [29, 42, 41], + [14, 40, 43], + [40, 42, 43], + [42, 29, 30], + [49, 30, 117], + [37, 12, 45], + [64, 35, 95], + [23, 47, 11], + [37, 47, 46], + [48, 97, 96], + [35, 64, 25], + [42, 30, 49], + [28, 20, 29], + [39, 30, 20], + [49, 55, 14], + [10, 61, 62], + [23, 11, 59], + [57, 54, 55], + [15, 57, 19], + [49, 53, 55], + [19, 57, 53], + [57, 55, 53], + [55, 54, 56], + [15, 54, 57], + [36, 95, 119], + [61, 59, 51], + [58, 23, 59], + [101, 24, 69], + [59, 11, 60], + [59, 61, 58], + [9, 69, 66], + [61, 51, 62], + [63, 67, 24], + [87, 96, 58], + [46, 23, 97], + [63, 10, 67], + [68, 66, 67], + [10, 63, 61], + [69, 67, 66], + [63, 24, 65], + [102, 79, 1], + [67, 10, 68], + [70, 71, 101], + [118, 70, 99], + [70, 101, 75], + [118, 79, 73], + [72, 65, 71], + [100, 82, 80], + [99, 77, 8], + [8, 77, 78], + [70, 75, 77], + [63, 107, 16], + [76, 74, 75], + [75, 74, 77], + [89, 21, 84], + [75, 9, 76], + [103, 118, 100], + [85, 7, 84], + [77, 74, 78], + [99, 70, 77], + [80, 84, 21], + [83, 81, 82], + [84, 82, 81], + [80, 18, 100], + [103, 100, 18], + [84, 7, 89], + [82, 8, 83], + [82, 84, 80], + [89, 7, 91], + [84, 81, 85], + [86, 90, 120], + [87, 48, 96], + [109, 112, 111], + [89, 86, 21], + [92, 86, 89], + [106, 105, 22], + [105, 106, 86], + [7, 88, 91], + [92, 91, 88], + [93, 92, 88], + [18, 80, 120], + [88, 6, 93], + [111, 115, 108], + [111, 108, 110], + [95, 97, 119], + [101, 71, 65], + [1, 72, 102], + [16, 58, 63], + [58, 96, 23], + [64, 95, 36], + [44, 4, 98], + [102, 71, 73], + [63, 65, 107], + [100, 118, 99], + [24, 101, 65], + [72, 71, 102], + [82, 100, 99], + [115, 111, 113], + [108, 114, 22], + [108, 105, 92], + [22, 94, 106], + [94, 0, 106], + [90, 106, 0], + [65, 72, 107], + [72, 16, 107], + [22, 105, 108], + [110, 108, 6], + [6, 109, 110], + [109, 111, 110], + [104, 115, 113], + [4, 94, 114], + [114, 115, 4], + [112, 113, 111], + [113, 112, 5], + [94, 22, 114], + [4, 115, 104], + [108, 115, 114], + [49, 117, 50], + [30, 39, 116], + [3, 117, 30], + [79, 118, 103], + [48, 2, 119], + [119, 2, 36], + [86, 120, 21], + ] + ) + + def __len__(self): + return len(self.elements) + + +# }}} + + +# {{{ add_to_silo_file + + +def add_to_silo_file( + silo, + mesh, + cell_data=[], + point_data=[], + line_integral_rules=[], + mesh_name="mesh", + real_only=False, +): + zonelist_name = mesh_name + "_zonelist" + + from pyvisfile.silo import IntVector + + nodelist = IntVector() + nodelist.extend(int(i) for i in mesh.elements.flat) + + shapetypes = IntVector() + from pyvisfile.silo import DB_ZONETYPE_TRIANGLE + + shapetypes.append(DB_ZONETYPE_TRIANGLE) + + shapesizes = IntVector() + shapesizes.append(3) + + shapecounts = IntVector() + shapecounts.append(len(mesh)) + + silo.put_zonelist_2( + zonelist_name, + len(mesh), + mesh.dimensions, + nodelist, + 0, + 0, + shapetypes, + shapesizes, + shapecounts, + ) + + silo.put_ucdmesh( + mesh_name, + [], + np.asarray(mesh.vertex_coordinates.T, order="C"), + len(mesh), + zonelist_name, + None, + ) + + from pyvisfile.silo import DB_NODECENT, DB_ZONECENT + + silo.put_ucdvar1( + "myvar", + mesh_name, + np.arange(mesh.vertex_coordinates.shape[0], dtype=np.double), + DB_NODECENT, + ) + silo.put_ucdvar1( + "myvar2", mesh_name, np.arange(len(mesh), dtype=np.double), DB_ZONECENT + ) + + +# }}} + + +def test_silo_unstructured(): + from pyvisfile.silo import SiloFile + + silo = SiloFile("out.silo") + add_to_silo_file(silo, Mesh()) + silo.close() + + +# vim: foldmethod=marker diff --git a/test/test_vtk.py b/test/test_vtk.py new file mode 100644 index 0000000..f9f1120 --- /dev/null +++ b/test/test_vtk.py @@ -0,0 +1,60 @@ +import numpy as np +from pyvisfile.vtk import ( + UnstructuredGrid, DataArray, + AppendedDataXMLGenerator, + VTK_VERTEX, VF_LIST_OF_VECTORS, VF_LIST_OF_COMPONENTS) + +from pyvisfile.vtk import write_structured_grid + + +def test_vtk_unstructured_points(): + n = 5000 + points = np.random.randn(n, 3) + + data = [ + ("p", np.random.randn(n)), + ("vel", np.random.randn(3, n)), + ] + file_name = "points.vtu" + compressor = None + + grid = UnstructuredGrid( + (n, DataArray("points", points, vector_format=VF_LIST_OF_VECTORS)), + cells=np.arange(n, dtype=np.uint32), + cell_types=np.asarray([VTK_VERTEX] * n, dtype=np.uint8)) + + for name, field in data: + grid.add_pointdata(DataArray(name, field, + vector_format=VF_LIST_OF_COMPONENTS)) + + from os.path import exists + if exists(file_name): + raise RuntimeError("output file '%s' already exists" + % file_name) + + outf = open(file_name, "w") + AppendedDataXMLGenerator(compressor)(grid).write(outf) + outf.close() + + +def test_vtk_structured_grid(): + angle_mesh = np.mgrid[1:2:10j, 0:2*np.pi:20j, 0:np.pi:30j] + + r = angle_mesh[0, np.newaxis] + phi = angle_mesh[1, np.newaxis] + theta = angle_mesh[2, np.newaxis] + mesh = np.vstack(( + r*np.sin(theta)*np.cos(phi), + r*np.sin(theta)*np.sin(phi), + r*np.cos(theta), + )) + + from pytools.obj_array import make_obj_array + vec = make_obj_array([ + np.sin(theta)*np.cos(phi), + np.sin(theta)*np.sin(phi), + np.cos(theta), + ]) + + write_structured_grid("yo.vts", mesh, + point_data=[("phi", phi), ("vec", vec)]) -- GitLab From c2b952057c289839d74e0a49a2a4053e784281c7 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Wed, 7 Oct 2020 15:38:07 -0500 Subject: [PATCH 18/19] Add CI config for test runs --- .github/workflows/ci.yml | 22 +++++++++++++++++++++- .gitlab-ci.yml | 17 ++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0f428b1..21f032c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,5 +22,25 @@ jobs: - name: "Main Script" run: | curl -L -O -k https://gitlab.tiker.net/inducer/ci-support/raw/master/prepare-and-run-flake8.sh - . ./prepare-and-run-flake8.sh "$(basename $GITHUB_REPOSITORY)" examples + . ./prepare-and-run-flake8.sh "$(basename $GITHUB_REPOSITORY)" test examples + pytest: + name: Pytest on Py${{ matrix.python-version }} + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [3.6, 3.7, 3.8] + steps: + - uses: actions/checkout@v2 + - + uses: actions/setup-python@v1 + with: + python-version: ${{ matrix.python-version }} + - name: "Main Script" + run: | + sudo apt update + sudo apt install libsilo-dev + EXTRA_INSTALL="numpy pybind11" + ./configure.py --use-silo + curl -L -O -k https://gitlab.tiker.net/inducer/ci-support/raw/master/build-and-test-py-project.sh + . ./build-and-test-py-project.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index bf0c862..33794ba 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,12 +1,27 @@ Flake8: script: - curl -L -O -k https://gitlab.tiker.net/inducer/ci-support/raw/master/prepare-and-run-flake8.sh - - . ./prepare-and-run-flake8.sh "$CI_PROJECT_NAME" examples + - . ./prepare-and-run-flake8.sh "$CI_PROJECT_NAME" test examples tags: - python3 except: - tags +Python 3: + script: | + py_version=3 + ./configure.py --use-silo + EXTRA_INSTALL="numpy pybind11" + curl -L -O -k https://gitlab.tiker.net/inducer/ci-support/raw/master/build-and-test-py-project.sh + . ./build-and-test-py-project.sh + tags: + - python3 + except: + - tags + artifacts: + reports: + junit: test/pytest.xml + Documentation: script: - EXTRA_INSTALL="numpy" -- GitLab From 186d14feb5c81c83e36ad569877cbd16c2953d96 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Wed, 7 Oct 2020 15:44:28 -0500 Subject: [PATCH 19/19] Gitlab CI: use docker runner --- .gitlab-ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 33794ba..1fd3297 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,13 +9,15 @@ Flake8: Python 3: script: | + sudo apt update + sudo apt install libsilo-dev py_version=3 ./configure.py --use-silo EXTRA_INSTALL="numpy pybind11" curl -L -O -k https://gitlab.tiker.net/inducer/ci-support/raw/master/build-and-test-py-project.sh . ./build-and-test-py-project.sh tags: - - python3 + - docker-runner except: - tags artifacts: -- GitLab