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