Salome HOME
4eb8360cba2a12b811c0785ad12a30ace2d2aae3
[modules/med.git] / src / MEDPartitioner / MEDPARTITIONER_MeshCollectionMedAsciiDriver.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
20 #include "MEDPARTITIONER_ParallelTopology.hxx"
21 #include "MEDPARTITIONER_MeshCollectionDriver.hxx"
22 #include "MEDPARTITIONER_MeshCollection.hxx"
23 #include "MEDPARTITIONER_MeshCollectionMedAsciiDriver.hxx"
24 #include "MEDPARTITIONER_ParaDomainSelector.hxx"
25 #include "MEDPARTITIONER_Utils.hxx"
26
27 #include "MEDCouplingUMesh.hxx"
28 #include "MEDLoader.hxx"
29
30 #include <map>
31 #include <set>
32 #include <vector>
33 #include <string>
34 #include <fstream>
35 #include <iostream>
36
37 #include <libxml/tree.h>
38 #include <libxml/parser.h>
39 #include <libxml/xpath.h>
40 #include <libxml/xpathInternals.h>
41
42 using namespace MEDPARTITIONER;
43
44 MeshCollectionMedAsciiDriver::MeshCollectionMedAsciiDriver(MeshCollection* collection):MeshCollectionDriver(collection)
45 {
46 }
47
48 /*!reads a MED File v>=2.3
49  * and mounts the corresponding meshes in memory
50  * the connect zones are created from the joints
51  * 
52  *\param filename ascii file containing the list of MED v2.3 files
53  * */
54
55 int MeshCollectionMedAsciiDriver::read(ParaMEDMEM::MEDFileData* filedata)
56 {
57   readMEDFileData(filedata);
58
59   std::vector<MEDPARTITIONER::ConnectZone*> cz; // to fill from filedata
60   std::vector<int*> cellglobal;
61   std::vector<int*> nodeglobal;
62   std::vector<int*> faceglobal;
63   int size = (_collection->getMesh()).size();
64   cellglobal.resize(size);
65   nodeglobal.resize(size);
66   faceglobal.resize(size);
67   for ( int idomain = 0; idomain < size; ++idomain )
68     {
69       cellglobal[idomain]=0;
70       faceglobal[idomain]=0;
71       nodeglobal[idomain]=0;
72       if ( (_collection->getMesh())[idomain] && (_collection->getMesh())[idomain]->getNumberOfNodes() > 0 )
73         _collection->setNonEmptyMesh(idomain);
74     }
75   //creation of topology from mesh and connect zones
76   ParallelTopology* aPT = new ParallelTopology((_collection->getMesh()), cz, cellglobal, nodeglobal, faceglobal);
77   _collection->setTopology(aPT,true);
78
79   return 0;
80 }
81
82 /*!reads a MED File v>=2.3
83  * and mounts the corresponding meshes in memory
84  * the connect zones are created from the joints
85  *
86  *\param filename ascii file containing the list of MED v2.3 files
87  * */
88
89 int MeshCollectionMedAsciiDriver::read(const char* filename, ParaDomainSelector* domainSelector)
90 {
91   //distributed meshes
92   std::vector<int*> cellglobal;
93   std::vector<int*> nodeglobal;
94   std::vector<int*> faceglobal;
95   int nbdomain;
96
97   //reading ascii master file
98   try
99     {
100       std::ifstream asciiinput(filename);
101       if (!asciiinput)
102         throw INTERP_KERNEL::Exception("Master ASCII File does not exist");
103       char charbuffer[512];
104       asciiinput.getline(charbuffer,512);
105
106       while (charbuffer[0]=='#')
107         {
108           asciiinput.getline(charbuffer,512);
109         }
110
111       //reading number of domains
112       nbdomain=atoi(charbuffer);
113       MyGlobals::_File_Names.resize(nbdomain);
114       MyGlobals::_Mesh_Names.resize(nbdomain);
115       (_collection->getMesh()).resize(nbdomain);
116       cellglobal.resize(nbdomain);
117       nodeglobal.resize(nbdomain);
118       faceglobal.resize(nbdomain);
119
120       if (nbdomain == 0)
121         throw INTERP_KERNEL::Exception("Empty ASCII master file");
122       for (int i=0; i<nbdomain;i++)
123         {
124           //reading information about the domain
125           std::string mesh,host;
126           int idomain;
127           cellglobal[i]=0;
128           faceglobal[i]=0;
129           nodeglobal[i]=0;
130
131           asciiinput >> mesh >> idomain >> MyGlobals::_Mesh_Names[i] >> host >> MyGlobals::_File_Names[i];
132
133           //Setting the name of the global mesh (which should be is the same for all the subdomains)
134           if (i==0)
135             _collection->setName(mesh);
136
137           if (idomain!=i+1)
138             {
139               throw INTERP_KERNEL::Exception("domain must be written from 1 to N in ASCII file descriptor");
140             }
141           if ( !domainSelector || domainSelector->isMyDomain(i))
142             readSubdomain(i);
143
144         } //loop on domains
145     } //of try
146   catch(...)
147     {
148       throw INTERP_KERNEL::Exception("I/O error reading parallel MED file");
149     }
150
151   //creation of topology from mesh and connect zones
152   ParallelTopology* aPT = new ParallelTopology((_collection->getMesh()), (_collection->getCZ()), cellglobal, nodeglobal, faceglobal);
153   _collection->setTopology(aPT, true);
154
155   for (int i=0; i<nbdomain; i++)
156     {
157       delete [] cellglobal[i];
158       delete [] nodeglobal[i];
159       delete [] faceglobal[i];
160     }
161   return 0;
162 }
163
164 /*! writes the collection of meshes in a MED v2.3 file
165  * with the connect zones being written as joints
166  * \param filename name of the ascii file containing the meshes description
167  */
168 void MeshCollectionMedAsciiDriver::write(const char* filename, ParaDomainSelector* domainSelector) const
169 {
170   int nbdomains=_collection->getMesh().size();
171   std::vector<std::string> filenames;
172   filenames.resize(nbdomains);
173
174   //loop on the domains
175   for (int idomain=0; idomain<nbdomains; idomain++)
176     {
177       std::string distfilename;
178       std::ostringstream suffix;
179       suffix << filename << idomain+1 << ".med";
180       distfilename=suffix.str();
181       filenames[idomain]=distfilename;
182
183       if ( !domainSelector || domainSelector->isMyDomain( idomain ) )
184         {
185           if ( !_collection->getMesh()[idomain]->getNumberOfCells()==0 ) continue;//empty domain
186           MEDLoader::WriteUMesh(distfilename.c_str(),(_collection->getMesh())[idomain],true);
187           //writeSubdomain(idomain, nbdomains, distfilename.c_str(), domainSelector);
188         }
189     }
190
191   //write master file
192   if ( !domainSelector || domainSelector->rank() == 0 )
193     {
194       std::ofstream file(filename);
195       file << "#MED Fichier V 2.3"<<" " << std::endl;
196       file << "#" << " " << std::endl;
197       file << _collection->getMesh().size() << " " << std::endl;
198
199       for (int idomain=0; idomain<nbdomains; idomain++)
200         file << _collection->getName() <<" "<< idomain+1 << " "
201              << (_collection->getMesh())[idomain]->getName() << " localhost "
202              << filenames[idomain] << " "<< std::endl;
203     }
204
205 }