Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDSPLITTER / MEDSPLITTER_MESHCollectionMedAsciiDriver.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 #include <vector>
20 #include <string>
21 #include <map>
22 #include <set>
23
24 #include <iostream>
25 #include <fstream>
26
27 #include <libxml/tree.h>
28 #include <libxml/parser.h>
29 #include <libxml/xpath.h>
30 #include <libxml/xpathInternals.h>
31
32 #ifndef WIN32
33 #include <sys/time.h>
34 #endif
35 //Debug macros
36 #include "MEDMEM_Utilities.hxx"
37
38 //MEDMEM includes
39 #include "MEDMEM_DriversDef.hxx"
40 #include "MEDMEM_Mesh.hxx"
41 #include "MEDMEM_Field.hxx"
42 #include "MEDMEM_Meshing.hxx"
43 #include "MEDMEM_CellModel.hxx"
44 #include "MEDMEM_SkyLineArray.hxx"
45 #include "MEDMEM_ConnectZone.hxx"
46
47 //MEDSPLITTER includes
48 #include "MEDSPLITTER_Topology.hxx"
49 #include "MEDSPLITTER_ParallelTopology.hxx"
50 #include "MEDSPLITTER_SequentialTopology.hxx"
51 #include "MEDSPLITTER_MESHCollectionDriver.hxx"
52 #include "MEDSPLITTER_MESHCollection.hxx"
53 #include "MEDSPLITTER_MESHCollectionMedAsciiDriver.hxx"
54 #include "MEDSPLITTER_ParaDomainSelector.hxx"
55
56 using namespace MEDSPLITTER;
57
58 //template inclusion
59 #include "MEDSPLITTER_MESHCollectionMedAsciiDriver.H"
60
61
62 MESHCollectionMedAsciiDriver::MESHCollectionMedAsciiDriver(MESHCollection* collection):MESHCollectionDriver(collection)
63 {
64 }
65
66 /*!reads a MED File v>=2.3
67  * and mounts the corresponding meshes in memory
68  * the connect zones are created from the joints
69  * 
70  *\param filename ascii file containing the list of MED v2.3 files
71  * */
72
73 int MESHCollectionMedAsciiDriver::read(char* filename, ParaDomainSelector* domainSelector)
74 {
75
76   const char* LOC = "MEDSPLITTER::MESHCollectionDriver::read()";
77   BEGIN_OF_MED(LOC);
78
79   //ditributed meshes
80   vector<int*> cellglobal;
81   vector<int*> nodeglobal;
82   vector<int*> faceglobal;
83
84   int nbdomain;
85
86   // reading ascii master file
87   try{
88     MESSAGE_MED("Start reading");
89     ifstream asciiinput(filename);
90
91     if (!asciiinput)     
92       throw MEDEXCEPTION("MEDSPLITTER read - Master File does not exist");
93
94     char charbuffer[512];
95     asciiinput.getline(charbuffer,512);
96
97     while (charbuffer[0]=='#')
98     {
99       asciiinput.getline(charbuffer,512);
100     }
101
102     //reading number of domains
103     nbdomain=atoi(charbuffer);
104     cout << "nb domain "<<nbdomain<<endl;
105     //    asciiinput>>nbdomain;
106     _filename.resize(nbdomain);
107     _meshname.resize(nbdomain);
108     (_collection->getMesh()).resize(nbdomain);
109     cellglobal.resize(nbdomain);
110     nodeglobal.resize(nbdomain);
111     faceglobal.resize(nbdomain);
112
113     if (nbdomain == 0)
114       throw MEDEXCEPTION("Empty ASCII master file");
115     for (int i=0; i<nbdomain;i++)
116     {
117
118       //reading information about the domain
119       string mesh;
120       int idomain;
121       string host;
122       cellglobal[i]=0;
123       faceglobal[i]=0;
124       nodeglobal[i]=0;
125
126       asciiinput >> mesh >> idomain >> _meshname[i] >> host >> _filename[i];
127
128       //Setting the name of the global mesh (which is the same
129       //for all the subdomains)
130       if (i==0)
131         _collection->setName(mesh);
132
133       if (idomain!=i+1)
134       {
135         cerr<<"Error : domain must be written from 1 to N in asciifile descriptor"<<endl;
136         return 1;
137       }
138       if ( !domainSelector || domainSelector->isMyDomain(i))
139         readSubdomain(cellglobal,faceglobal,nodeglobal, i);
140
141     }//loop on domains
142     MESSAGE_MED("end of read");
143   }//of try
144   catch(...)
145   {
146     cerr << "I/O error reading parallel MED file"<<endl;
147     throw;
148   }
149
150   //creation of topology from mesh and connect zones
151   ParallelTopology* aPT = new ParallelTopology
152     ((_collection->getMesh()), (_collection->getCZ()), cellglobal, nodeglobal, faceglobal);
153   _collection->setTopology(aPT);
154
155   for (int i=0; i<nbdomain; i++)
156   {
157     if (cellglobal[i]!=0) delete[] cellglobal[i];
158     if (nodeglobal[i]!=0) delete[] nodeglobal[i];
159     if (faceglobal[i]!=0) delete[] faceglobal[i];
160   }
161
162   END_OF_MED(LOC);
163   return 0;
164 }
165
166
167 /*! writes the collection of meshes in a 
168  * MED v2.3 file
169  * with the connect zones being written as joints
170  * \param filename name of the ascii file containing the meshes description
171  */
172 void MESHCollectionMedAsciiDriver::write(char* filename, ParaDomainSelector* domainSelector)
173 {
174
175   const char* LOC = "MEDSPLITTER::MESHCollectionDriver::write()";
176   BEGIN_OF_MED(LOC);
177
178   int nbdomains= _collection->getMesh().size();
179   _filename.resize(nbdomains);
180
181   //loop on the domains
182   for (int idomain=0; idomain<nbdomains;idomain++)
183   {
184     char distfilename[256];
185
186     ostringstream suffix;
187     suffix << filename<< idomain+1 <<".med";
188
189     strcpy(distfilename,suffix.str().c_str());
190
191     _filename[idomain]=string(distfilename);
192
193     MESSAGE_MED("File name "<<string(distfilename));
194
195     if ( !domainSelector || domainSelector->isMyDomain( idomain ) )
196     {
197       if ( !_collection->getMesh()[idomain]->getConnectivityptr() ) continue;//empty domain
198
199       int id=(_collection->getMesh())[idomain]->addDriver(MEDMEM::MED_DRIVER,distfilename,(_collection->getMesh())[idomain]->getName(),MED_EN::WRONLY);
200
201       MESSAGE_MED("Start writing");
202       (_collection->getMesh())[idomain]->write(id);
203
204       writeSubdomain(idomain, nbdomains, distfilename, domainSelector);
205     }
206   }
207
208   // write master file
209   if ( !domainSelector || domainSelector->rank() == 0 )
210   {
211     ofstream file(filename);
212
213     file <<"#MED Fichier V 2.3"<<" "<<endl;
214     file <<"#"<<" "<<endl;
215     file<<_collection->getMesh().size()<<" "<<endl;
216
217     for (int idomain=0; idomain<nbdomains;idomain++)
218       file << _collection->getName() <<" "<< idomain+1 << " "
219            << (_collection->getMesh())[idomain]->getName() << " localhost "
220            << _filename[idomain] << " "<<endl;
221   }
222
223   END_OF_MED(LOC);
224
225 }