From d253d9597e24b9e59aefd87ff5a771a3634ba898 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Sat, 22 Feb 2020 23:34:05 +0100 Subject: [PATCH] Wrap of GetFamiliesGroupsInfo for // partitioner --- src/MEDCoupling_Swig/MEDCouplingTypemaps.i | 42 ++++++++++++++++++++++ src/MEDLoader/MEDLoader.cxx | 33 ++++++++++++++++- src/MEDLoader/MEDLoader.hxx | 5 ++- src/MEDLoader/Swig/MEDLoaderCommon.i | 14 ++++++++ 4 files changed, 90 insertions(+), 4 deletions(-) diff --git a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i index 080def392..b4af9fb28 100644 --- a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i +++ b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i @@ -33,6 +33,48 @@ #include "MEDCouplingPartDefinition.hxx" #include "MEDCouplingCartesianAMRMesh.hxx" +#include + +auto PyDeleter = [](PyObject *ob) { Py_DECREF(ob) ; }; + +class AutoPyPtr : std::unique_ptr +{ + public: + AutoPyPtr(PyObject *ob):std::unique_ptr(ob,PyDeleter) { } + PyObject *retn() { return this->release(); } + operator PyObject *() const { return this->get(); } +}; + +AutoPyPtr convertMapStringInt(const std::map& cpp) +{ + AutoPyPtr ret(PyDict_New()); + for(auto it : cpp) + { + AutoPyPtr st(PyString_FromString(it.first.c_str())); + AutoPyPtr val(PyInt_FromLong(it.second)); + PyDict_SetItem(ret,st,val); + } + return ret; +} + +AutoPyPtr convertMapStringVectString(const std::map>& cpp) +{ + AutoPyPtr ret(PyDict_New()); + for(auto it : cpp) + { + AutoPyPtr st(PyString_FromString(it.first.c_str())); + AutoPyPtr vec(PyList_New(it.second.size())); + std::size_t itc(0); + for(auto it2 : it.second) + { + AutoPyPtr st2(PyString_FromString(it2.c_str())); + PyList_SetItem(vec,itc++,st2.retn()); + } + PyDict_SetItem(ret,st,vec.retn()); + } + return ret; +} + static PyObject *convertMesh(MEDCoupling::MEDCouplingMesh *mesh, int owner) { PyObject *ret=0; diff --git a/src/MEDLoader/MEDLoader.cxx b/src/MEDLoader/MEDLoader.cxx index 5a1124b89..597b25fc1 100644 --- a/src/MEDLoader/MEDLoader.cxx +++ b/src/MEDLoader/MEDLoader.cxx @@ -47,7 +47,7 @@ #include #include #include - +#include med_geometry_type typmai[MED_N_CELL_FIXED_GEO] = { MED_POINT1, MED_SEG2, @@ -482,6 +482,37 @@ std::vector< std::pair > MEDCoupling::GetComponentsName throw INTERP_KERNEL::Exception(oss.str().c_str()); } +// see reference : https://en.cppreference.com/w/cpp/iterator/iterator +class MEDVectorStringIterator : public std::iterator< std::input_iterator_tag, long, long, const std::string*, std::string > +{ + long _num = 0; + char *_data = nullptr; +public: + explicit MEDVectorStringIterator(long num , char *data) : _num(num),_data(data) {} + MEDVectorStringIterator& operator++() { ++_num; return *this;} + bool operator==(const MEDVectorStringIterator& other) const {return _num == other._num;} + bool operator!=(const MEDVectorStringIterator& other) const {return !(*this == other);} + reference operator*() const {return MEDLoaderBase::buildStringFromFortran(_data+_num*MED_LNAME_SIZE,MED_LNAME_SIZE);} +}; + +void MEDCoupling::GetFamiliesGroupsInfo(const std::string& fileName, const std::string& meshName, std::map& families, std::map>& groupsOnFam) +{ + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); + med_int nbFams(MEDnFamily(fid,meshName.c_str())); + char nomfam[MED_NAME_SIZE+1]; + for(med_int i=0;i gro{new char[MED_LNAME_SIZE*nbGrps+1]}; + MEDFILESAFECALLERRD0(MEDfamilyInfo,(fid,meshName.c_str(),i+1,nomfam,&famId,gro.get())); + std::string fam(MEDLoaderBase::buildStringFromFortran(nomfam,MED_NAME_SIZE)); + families[fam] = FromMedInt(famId); + std::vector v(nbGrps); + std::copy(MEDVectorStringIterator(0,gro.get()),MEDVectorStringIterator(nbGrps,gro.get()),v.begin()); + groupsOnFam[fam] = v; + } +} + std::vector MEDCoupling::GetMeshNamesOnField(const std::string& fileName, const std::string& fieldName) { std::vector ret; diff --git a/src/MEDLoader/MEDLoader.hxx b/src/MEDLoader/MEDLoader.hxx index 6dfadb3f6..2a9c17681 100644 --- a/src/MEDLoader/MEDLoader.hxx +++ b/src/MEDLoader/MEDLoader.hxx @@ -18,8 +18,7 @@ // // Author : Anthony Geay (CEA/DEN) -#ifndef __MEDLOADER_HXX__ -#define __MEDLOADER_HXX__ +#pragma once #include "MEDLoaderDefines.hxx" #include "InterpKernelException.hxx" @@ -52,6 +51,7 @@ namespace MEDCoupling MEDLOADER_EXPORT std::vector< std::vector< std::pair > > GetUMeshGlobalInfo(const std::string& fileName, const std::string& meshName, int &meshDim, int& spaceDim, mcIdType& numberOfNodes); MEDLOADER_EXPORT std::vector< std::pair > GetComponentsNamesOfField(const std::string& fileName, const std::string& fieldName); MEDLOADER_EXPORT std::vector GetMeshNamesOnField(const std::string& fileName, const std::string& fieldName); + MEDLOADER_EXPORT void GetFamiliesGroupsInfo(const std::string& fileName, const std::string& meshName, std::map& families, std::map>& groupsOnFam); MEDLOADER_EXPORT std::vector GetMeshGroupsNames(const std::string& fileName, const std::string& meshName); MEDLOADER_EXPORT std::vector GetMeshFamiliesNames(const std::string& fileName, const std::string& meshName); MEDLOADER_EXPORT std::vector GetMeshFamiliesNamesOnGroup(const std::string& fileName, const std::string& meshName, const std::string& grpName); @@ -105,4 +105,3 @@ namespace MEDCoupling MEDLOADER_EXPORT void AssignStaticWritePropertiesTo(MEDCoupling::MEDFileWritable& obj); } -#endif diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index a0cbc6dab..a66fb85dc 100644 --- a/src/MEDLoader/Swig/MEDLoaderCommon.i +++ b/src/MEDLoader/Swig/MEDLoaderCommon.i @@ -38,6 +38,7 @@ #include "MEDFileEntities.hxx" #include "MEDFileMeshReadSelector.hxx" #include "MEDFileFieldOverView.hxx" +#include "MEDCouplingTypemaps.i" #include "MEDLoaderTypemaps.i" #include "SauvReader.hxx" #include "SauvWriter.hxx" @@ -391,6 +392,7 @@ namespace MEDCoupling %rename (ReadUMeshFromGroups) ReadUMeshFromGroupsSwig; %rename (ReadUMeshFromFamilies) ReadUMeshFromFamiliesSwig; %rename (ReadField) ReadFieldSwig; +%rename (GetFamiliesGroupsInfo) GetFamiliesGroupsInfoSwig; %inline { @@ -410,6 +412,18 @@ namespace MEDCoupling return 8*sizeof(med_int); } + PyObject *GetFamiliesGroupsInfoSwig(const std::string& fileName, const std::string& meshName) + { + std::map families; + std::map> groupsOnFam; + MEDCoupling::GetFamiliesGroupsInfo(fileName,meshName,families,groupsOnFam); + AutoPyPtr a(convertMapStringInt(families)),b(convertMapStringVectString(groupsOnFam)); + AutoPyPtr ret(PyTuple_New(2)); + PyTuple_SetItem(ret,0,a.retn()); + PyTuple_SetItem(ret,1,b.retn()); + return ret.retn(); + } + MEDCoupling::MEDCouplingField *ReadFieldSwig(const std::string& fileName) { MCAuto ret(MEDCoupling::ReadField(fileName)); -- 2.39.2