Salome HOME
c024e86fce0f08c19f39c6410f36bbbd0923233a
[tools/medcoupling.git] / src / ParaMEDLoader / ParaMEDLoader.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 (CEA/DEN)
20
21 #include "ParaMEDLoader.hxx"
22 #include "MEDLoader.hxx"
23 #include "ParaMESH.hxx"
24 #include "BlockTopology.hxx"
25 #include "MEDCouplingUMesh.hxx"
26
27 #include <fstream>
28 #include <sstream>
29
30 using namespace MEDCoupling;
31
32 ParaMEDLoader::ParaMEDLoader()
33 {
34 }
35
36 void ParaMEDLoader::WriteParaMesh(const char *fileName, MEDCoupling::ParaMESH *mesh)
37 {
38   if(!mesh->getBlockTopology()->getProcGroup()->containsMyRank())
39     return ;
40   int myRank=mesh->getBlockTopology()->getProcGroup()->myRank();
41   int nbDomains=mesh->getBlockTopology()->getProcGroup()->size();
42   std::vector<std::string> fileNames(nbDomains);
43   for(int i=0;i<nbDomains;i++)
44     {
45       std::ostringstream sstr;
46       sstr << fileName << i+1 << ".med";
47       fileNames[i]=sstr.str();
48     }
49   if(myRank==0)
50     WriteMasterFile(fileName,fileNames,mesh->getCellMesh()->getName().c_str());
51   WriteUMesh(fileNames[myRank].c_str(),dynamic_cast<MEDCouplingUMesh *>(mesh->getCellMesh()),true);
52 }
53
54 /*!
55  * This method builds the master file 'fileName' of a parallel MED file defined in 'fileNames'.
56  */
57 void ParaMEDLoader::WriteMasterFile(const char *fileName, const std::vector<std::string>& fileNames, const char *meshName)
58 {
59   std::size_t nbOfDom=fileNames.size();
60   std::ofstream fs(fileName);
61   fs << "#MED Fichier V 2.3" << " " << std::endl;
62   fs << "#"<<" " << std::endl;
63   fs << nbOfDom <<" " << std::endl;
64   for(std::size_t i=0;i<nbOfDom;i++)
65     fs << meshName << " " << i+1 << " " << meshName << "_" << i+1 << " localhost " << fileNames[i] << " " << std::endl;
66 }