Salome HOME
Management of int64 fields into medcoupling.
[tools/medcoupling.git] / src / MEDLoader / MEDLoader.cxx
index 07d3e939303ae91c27ad3f7c39bae035f8d70f48..687d0bcbf35a6d6c4b3b25c6b29c96bf83af4a3b 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2020  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -30,7 +30,7 @@
 #include "MEDCouplingMemArray.hxx"
 #include "MEDCouplingFieldDouble.hxx"
 #include "MEDCouplingFieldFloat.hxx"
-#include "MEDCouplingFieldInt.hxx"
+#include "MEDCouplingFieldInt32.hxx"
 #include "MEDCouplingGaussLocalization.hxx"
 #include "MEDCouplingTraits.hxx"
 #include "MCAuto.hxx"
@@ -47,7 +47,7 @@
 #include <numeric>
 #include <iterator>
 #include <algorithm>
-
+#include <memory>
 
 med_geometry_type typmai[MED_N_CELL_FIXED_GEO] = { MED_POINT1,
   MED_SEG2,
@@ -101,7 +101,7 @@ INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO] = { INTERP_KERNE
   INTERP_KERNEL::NORM_QPOLYG,
   INTERP_KERNEL::NORM_POLYHED };
 
-med_geometry_type typmai3[34] = { MED_POINT1,//0
+med_geometry_type typmai3[INTERP_KERNEL::NORM_MAXTYPE] = { MED_POINT1,//0
   MED_SEG2,//1
   MED_SEG3,//2
   MED_TRIA3,//3
@@ -133,8 +133,7 @@ med_geometry_type typmai3[34] = { MED_POINT1,//0
   MED_NONE,//29
   MED_HEXA20,//30
   MED_POLYHEDRON,//31
-  MED_POLYGON2,//32
-  MED_NONE//33
+  MED_POLYGON2//32
 };
 
 double _EPS_FOR_NODE_COMP=1.e-12;
@@ -147,6 +146,20 @@ using namespace MEDCoupling;
 
 /// @cond INTERNAL
 
+INTERP_KERNEL::NormalizedCellType ConvertGeometryType(med_geometry_type geotype)
+{
+  INTERP_KERNEL::NormalizedCellType result=INTERP_KERNEL::NORM_ERROR;
+  for(int i=0; i<MED_N_CELL_FIXED_GEO; i++)
+    {
+      if (typmai[i]==geotype)
+        {
+          result=typmai2[i];
+          break;
+        }
+    }
+  return result;
+}
+
 /*!
  * This method returns a first quick overview of mesh with name \a meshName into the file \a fileName.
  * @param possibilities the relativeToMeshDim authorized to returned maxdim. This vector is systematically cleared at the begin of this method.
@@ -483,6 +496,37 @@ std::vector< std::pair<std::string,std::string> > 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<std::string,mcIdType>& families, std::map<std::string,std::vector<std::string>>& 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<nbFams;++i)
+    {
+      med_int nbGrps(MEDnFamilyGroup(fid,meshName.c_str(),i+1)),famId;
+      std::unique_ptr<char[]> 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<mcIdType>(famId);
+      std::vector<std::string> v(nbGrps);
+      std::copy(MEDVectorStringIterator(0,gro.get()),MEDVectorStringIterator(nbGrps,gro.get()),v.begin());
+      groupsOnFam[fam] = v;
+    }
+}
+
 std::vector<std::string> MEDCoupling::GetMeshNamesOnField(const std::string& fileName, const std::string& fieldName)
 {
   std::vector<std::string> ret;
@@ -1219,11 +1263,11 @@ MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string&
       }
   }
   {
-    MCAuto<MEDFileIntField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileIntField1TS>(f));
+    MCAuto<MEDFileInt32Field1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileInt32Field1TS>(f));
     if(f1.isNotNull())
       {
-        MCAuto<MEDCoupling::MEDCouplingFieldInt> ret(f1->field(mesh));
-        return MEDCoupling::DynamicCast<MEDCouplingFieldInt,MEDCouplingField>(ret);
+        MCAuto<MEDCoupling::MEDCouplingFieldInt32> ret(f1->field(mesh));
+        return MEDCoupling::DynamicCast<MEDCouplingFieldInt32,MEDCouplingField>(ret);
       }
   }
   {
@@ -1346,10 +1390,10 @@ namespace MEDCoupling
         }
     }
     {
-      MCAuto<MEDFileIntField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileIntField1TS>(f));
+      MCAuto<MEDFileInt32Field1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileInt32Field1TS>(f));
       if(f1.isNotNull())
         {
-          MCAuto<MEDCoupling::MEDCouplingFieldInt> ret(ReadFieldCellLikeT<int>(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
+          MCAuto<MEDCoupling::MEDCouplingFieldInt32> ret(ReadFieldCellLikeT<int>(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
           return ret.retn();
         }
     }
@@ -1423,10 +1467,10 @@ MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldNode(const std::string& fil
       }
   }
   {
-    MCAuto<MEDFileIntField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileIntField1TS>(f));
+    MCAuto<MEDFileInt32Field1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileInt32Field1TS>(f));
     if(f1.isNotNull())
       {
-        MCAuto<MEDCoupling::MEDCouplingFieldInt> ret(ReadFieldNodeT<int>(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
+        MCAuto<MEDCoupling::MEDCouplingFieldInt32> ret(ReadFieldNodeT<int>(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
         return ret.retn();
       }
   }
@@ -1700,7 +1744,7 @@ void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MED
       }
   }
   {
-    const MEDCoupling::MEDCouplingFieldInt *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldInt *>(f));
+    const MEDCoupling::MEDCouplingFieldInt32 *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldInt32 *>(f));
     if(f1)
       {
         WriteFieldT<int>(fileName,f1,writeFromScratch);
@@ -1761,7 +1805,7 @@ void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName,
     return ;
   }
   {
-    const MEDCoupling::MEDCouplingFieldInt *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldInt *>(f));
+    const MEDCoupling::MEDCouplingFieldInt32 *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldInt32 *>(f));
     if(f1)
       WriteFieldUsingAlreadyWrittenMeshT<int>(fileName,f1);
     return ;