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