Back to index

salome-med  6.5.0
MEDSPLITTER_MESHCollectionMedAsciiDriver.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 #include <vector>
00020 #include <string>
00021 #include <map>
00022 #include <set>
00023 
00024 #include <iostream>
00025 #include <fstream>
00026 
00027 #include <libxml/tree.h>
00028 #include <libxml/parser.h>
00029 #include <libxml/xpath.h>
00030 #include <libxml/xpathInternals.h>
00031 
00032 #ifndef WIN32
00033 #include <sys/time.h>
00034 #endif
00035 //Debug macros
00036 #include "MEDMEM_Utilities.hxx"
00037 
00038 //MEDMEM includes
00039 #include "MEDMEM_DriversDef.hxx"
00040 #include "MEDMEM_Mesh.hxx"
00041 #include "MEDMEM_Field.hxx"
00042 #include "MEDMEM_Meshing.hxx"
00043 #include "MEDMEM_CellModel.hxx"
00044 #include "MEDMEM_SkyLineArray.hxx"
00045 #include "MEDMEM_ConnectZone.hxx"
00046 
00047 //MEDSPLITTER includes
00048 #include "MEDSPLITTER_Topology.hxx"
00049 #include "MEDSPLITTER_ParallelTopology.hxx"
00050 #include "MEDSPLITTER_SequentialTopology.hxx"
00051 #include "MEDSPLITTER_MESHCollectionDriver.hxx"
00052 #include "MEDSPLITTER_MESHCollection.hxx"
00053 #include "MEDSPLITTER_MESHCollectionMedAsciiDriver.hxx"
00054 #include "MEDSPLITTER_ParaDomainSelector.hxx"
00055 
00056 using namespace MEDSPLITTER;
00057 
00058 //template inclusion
00059 #include "MEDSPLITTER_MESHCollectionMedAsciiDriver.H"
00060 
00061 
00062 MESHCollectionMedAsciiDriver::MESHCollectionMedAsciiDriver(MESHCollection* collection):MESHCollectionDriver(collection)
00063 {
00064 }
00065 
00073 int MESHCollectionMedAsciiDriver::read(char* filename, ParaDomainSelector* domainSelector)
00074 {
00075 
00076   const char* LOC = "MEDSPLITTER::MESHCollectionDriver::read()";
00077   BEGIN_OF_MED(LOC);
00078 
00079   //ditributed meshes
00080   vector<int*> cellglobal;
00081   vector<int*> nodeglobal;
00082   vector<int*> faceglobal;
00083 
00084   int nbdomain;
00085 
00086   // reading ascii master file
00087   try{
00088     MESSAGE_MED("Start reading");
00089     ifstream asciiinput(filename);
00090 
00091     if (!asciiinput)     
00092       throw MEDEXCEPTION("MEDSPLITTER read - Master File does not exist");
00093 
00094     char charbuffer[512];
00095     asciiinput.getline(charbuffer,512);
00096 
00097     while (charbuffer[0]=='#')
00098     {
00099       asciiinput.getline(charbuffer,512);
00100     }
00101 
00102     //reading number of domains
00103     nbdomain=atoi(charbuffer);
00104     cout << "nb domain "<<nbdomain<<endl;
00105     //    asciiinput>>nbdomain;
00106     _filename.resize(nbdomain);
00107     _meshname.resize(nbdomain);
00108     (_collection->getMesh()).resize(nbdomain);
00109     cellglobal.resize(nbdomain);
00110     nodeglobal.resize(nbdomain);
00111     faceglobal.resize(nbdomain);
00112 
00113     if (nbdomain == 0)
00114       throw MEDEXCEPTION("Empty ASCII master file");
00115     for (int i=0; i<nbdomain;i++)
00116     {
00117 
00118       //reading information about the domain
00119       string mesh;
00120       int idomain;
00121       string host;
00122       cellglobal[i]=0;
00123       faceglobal[i]=0;
00124       nodeglobal[i]=0;
00125 
00126       asciiinput >> mesh >> idomain >> _meshname[i] >> host >> _filename[i];
00127 
00128       //Setting the name of the global mesh (which is the same
00129       //for all the subdomains)
00130       if (i==0)
00131         _collection->setName(mesh);
00132 
00133       if (idomain!=i+1)
00134       {
00135         cerr<<"Error : domain must be written from 1 to N in asciifile descriptor"<<endl;
00136         return 1;
00137       }
00138       if ( !domainSelector || domainSelector->isMyDomain(i))
00139         readSubdomain(cellglobal,faceglobal,nodeglobal, i);
00140 
00141     }//loop on domains
00142     MESSAGE_MED("end of read");
00143   }//of try
00144   catch(...)
00145   {
00146     cerr << "I/O error reading parallel MED file"<<endl;
00147     throw;
00148   }
00149 
00150   //creation of topology from mesh and connect zones
00151   ParallelTopology* aPT = new ParallelTopology
00152     ((_collection->getMesh()), (_collection->getCZ()), cellglobal, nodeglobal, faceglobal);
00153   _collection->setTopology(aPT);
00154 
00155   for (int i=0; i<nbdomain; i++)
00156   {
00157     if (cellglobal[i]!=0) delete[] cellglobal[i];
00158     if (nodeglobal[i]!=0) delete[] nodeglobal[i];
00159     if (faceglobal[i]!=0) delete[] faceglobal[i];
00160   }
00161 
00162   END_OF_MED(LOC);
00163   return 0;
00164 }
00165 
00166 
00172 void MESHCollectionMedAsciiDriver::write(char* filename, ParaDomainSelector* domainSelector)
00173 {
00174 
00175   const char* LOC = "MEDSPLITTER::MESHCollectionDriver::write()";
00176   BEGIN_OF_MED(LOC);
00177 
00178   int nbdomains= _collection->getMesh().size();
00179   _filename.resize(nbdomains);
00180 
00181   //loop on the domains
00182   for (int idomain=0; idomain<nbdomains;idomain++)
00183   {
00184     char distfilename[256];
00185 
00186     ostringstream suffix;
00187     suffix << filename<< idomain+1 <<".med";
00188 
00189     strcpy(distfilename,suffix.str().c_str());
00190 
00191     _filename[idomain]=string(distfilename);
00192 
00193     MESSAGE_MED("File name "<<string(distfilename));
00194 
00195     if ( !domainSelector || domainSelector->isMyDomain( idomain ) )
00196     {
00197       if ( !_collection->getMesh()[idomain]->getConnectivityptr() ) continue;//empty domain
00198 
00199       int id=(_collection->getMesh())[idomain]->addDriver(MEDMEM::MED_DRIVER,distfilename,(_collection->getMesh())[idomain]->getName(),MED_EN::WRONLY);
00200 
00201       MESSAGE_MED("Start writing");
00202       (_collection->getMesh())[idomain]->write(id);
00203 
00204       writeSubdomain(idomain, nbdomains, distfilename, domainSelector);
00205     }
00206   }
00207 
00208   // write master file
00209   if ( !domainSelector || domainSelector->rank() == 0 )
00210   {
00211     ofstream file(filename);
00212 
00213     file <<"#MED Fichier V 2.3"<<" "<<endl;
00214     file <<"#"<<" "<<endl;
00215     file<<_collection->getMesh().size()<<" "<<endl;
00216 
00217     for (int idomain=0; idomain<nbdomains;idomain++)
00218       file << _collection->getName() <<" "<< idomain+1 << " "
00219            << (_collection->getMesh())[idomain]->getName() << " localhost "
00220            << _filename[idomain] << " "<<endl;
00221   }
00222 
00223   END_OF_MED(LOC);
00224 
00225 }