Salome HOME
Copyright update 2020
[tools/medcoupling.git] / src / ParaMEDLoader / ParaMEDFileMesh.cxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (EDF R&D)
20
21 #include "ParaMEDFileMesh.hxx"
22 #include "MCAuto.hxx"
23 #include "MEDFileMesh.hxx"
24 #include "MEDFileMeshLL.hxx"
25 #include "MEDLoader.hxx"
26
27 using namespace MEDCoupling;
28
29 MEDFileMesh *ParaMEDFileMesh::New(int iPart, int nbOfParts, const std::string& fileName, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs)
30 {
31   MEDFileUtilities::CheckFileForRead(fileName);
32   MEDCoupling::MEDCouplingMeshType meshType;
33   MEDFileUtilities::AutoFid fid(MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY));
34   int dummy0,dummy1;
35   std::string dummy2;
36   MEDCoupling::MEDCouplingAxisType dummy3;
37   MEDFileMeshL2::GetMeshIdFromName(fid,mName,meshType,dummy3,dummy0,dummy1,dummy2);
38   switch(meshType)
39   {
40     case UNSTRUCTURED:
41       {
42         return ParaMEDFileUMesh::New(iPart,nbOfParts,fileName,mName,dt,it,mrs);
43       }
44     default:
45       throw INTERP_KERNEL::Exception("ParaMEDFileMesh::New : only unstructured mesh supported for the moment !");
46   }
47 }
48
49 MEDFileMesh *ParaMEDFileMesh::ParaNew(int iPart, int nbOfParts, const MPI_Comm& com, const MPI_Info& nfo, const std::string& fileName, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs)
50 {
51   MEDFileUtilities::CheckFileForRead(fileName);
52   MEDCoupling::MEDCouplingMeshType meshType;
53   MEDFileUtilities::AutoFid fid(MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY));
54   int dummy0,dummy1;
55   std::string dummy2;
56   MEDCoupling::MEDCouplingAxisType dummy3;
57   MEDFileMeshL2::GetMeshIdFromName(fid,mName,meshType,dummy3,dummy0,dummy1,dummy2);
58   switch(meshType)
59   {
60     case UNSTRUCTURED:
61       {
62         return ParaMEDFileUMesh::ParaNew(iPart,nbOfParts,com,nfo,fileName,mName,dt,it,mrs);
63       }
64     default:
65       throw INTERP_KERNEL::Exception("ParaMEDFileMesh::ParaNew : only unstructured mesh supported for the moment !");
66   }
67 }
68
69 MEDFileUMesh *ParaMEDFileUMesh::New(int iPart, int nbOfParts, const std::string& fileName, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs)
70 {
71   MEDFileUtilities::CheckFileForRead(fileName);
72   MEDFileUtilities::AutoFid fid(MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY));
73   return ParaMEDFileUMesh::NewPrivate(fid,iPart,nbOfParts,fileName,mName,dt,it,mrs);
74 }
75
76 // MPI_COMM_WORLD, MPI_INFO_NULL 
77 MEDFileUMesh *ParaMEDFileUMesh::ParaNew(int iPart, int nbOfParts, const MPI_Comm& com, const MPI_Info& nfo, const std::string& fileName, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs)
78 {
79   MEDFileUtilities::CheckFileForRead(fileName);
80 #ifdef HDF5_IS_PARALLEL
81   MEDFileUtilities::AutoFid fid(MEDparFileOpen(fileName.c_str(),MED_ACC_RDONLY,com,nfo));
82 #else
83   MEDFileUtilities::AutoFid fid(MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY));
84 #endif
85   return ParaMEDFileUMesh::NewPrivate(fid,iPart,nbOfParts,fileName,mName,dt,it,mrs);
86 }
87
88 MEDFileUMesh *ParaMEDFileUMesh::NewPrivate(med_idt fid, int iPart, int nbOfParts, const std::string& fileName, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs)
89 {
90   MCAuto<MEDFileUMesh> ret;
91   int meshDim, spaceDim;
92   mcIdType numberOfNodes;
93   std::vector< std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > > typesDistrib(GetUMeshGlobalInfo(fileName,mName,meshDim,spaceDim,numberOfNodes));
94   std::vector<INTERP_KERNEL::NormalizedCellType> types;
95   std::vector<mcIdType> distrib;
96   for(std::vector< std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > >::const_iterator it0=typesDistrib.begin();it0!=typesDistrib.end();it0++)
97     for(std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> >::const_iterator it1=(*it0).begin();it1!=(*it0).end();it1++)
98       {
99         types.push_back((*it1).first);
100         mcIdType tmp[3];
101         DataArray::GetSlice(0,(*it1).second,1,iPart,nbOfParts,tmp[0],tmp[1]);
102         tmp[2]=1;
103         distrib.insert(distrib.end(),tmp,tmp+3);
104       }
105   ret=MEDFileUMesh::LoadPartOf(fid,mName,types,distrib,dt,it,mrs);
106   return ret.retn();
107 }
108
109 MEDFileMeshes *ParaMEDFileMeshes::New(int iPart, int nbOfParts, const std::string& fileName)
110 {
111   std::vector<std::string> ms(GetMeshNames(fileName));
112   MCAuto<MEDFileMeshes> ret(MEDFileMeshes::New());
113   for(std::vector<std::string>::const_iterator it=ms.begin();it!=ms.end();it++)
114     {
115       MCAuto<MEDFileMesh> mesh(ParaMEDFileMesh::New(iPart,nbOfParts,fileName,(*it)));
116       ret->pushMesh(mesh);
117     }
118   return ret.retn();
119 }
120
121 MEDFileMeshes *ParaMEDFileMeshes::ParaNew(int iPart, int nbOfParts, const MPI_Comm& com, const MPI_Info& nfo, const std::string& fileName)
122 {
123   std::vector<std::string> ms(GetMeshNames(fileName));
124   MCAuto<MEDFileMeshes> ret(MEDFileMeshes::New());
125   for(std::vector<std::string>::const_iterator it=ms.begin();it!=ms.end();it++)
126     {
127       MCAuto<MEDFileMesh> mesh(ParaMEDFileMesh::ParaNew(iPart,nbOfParts,com,nfo,fileName,(*it)));
128       ret->pushMesh(mesh);
129     }
130   return ret.retn();
131 }