Back to index

salome-med  6.5.0
MEDPARTITIONER_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 
00020 #include "MEDPARTITIONER_ParallelTopology.hxx"
00021 #include "MEDPARTITIONER_MeshCollectionDriver.hxx"
00022 #include "MEDPARTITIONER_MeshCollection.hxx"
00023 #include "MEDPARTITIONER_MeshCollectionMedAsciiDriver.hxx"
00024 #include "MEDPARTITIONER_ParaDomainSelector.hxx"
00025 #include "MEDPARTITIONER_Utils.hxx"
00026 
00027 #include "MEDCouplingUMesh.hxx"
00028 #include "MEDLoader.hxx"
00029 
00030 #include <map>
00031 #include <set>
00032 #include <vector>
00033 #include <string>
00034 #include <fstream>
00035 #include <iostream>
00036 
00037 #include <libxml/tree.h>
00038 #include <libxml/parser.h>
00039 #include <libxml/xpath.h>
00040 #include <libxml/xpathInternals.h>
00041 
00042 using namespace MEDPARTITIONER;
00043 
00044 MeshCollectionMedAsciiDriver::MeshCollectionMedAsciiDriver(MeshCollection* collection):MeshCollectionDriver(collection)
00045 {
00046 }
00047 
00055 int MeshCollectionMedAsciiDriver::read(const char* filename, ParaDomainSelector* domainSelector)
00056 {
00057   //distributed meshes
00058   std::vector<int*> cellglobal;
00059   std::vector<int*> nodeglobal;
00060   std::vector<int*> faceglobal;
00061   int nbdomain;
00062 
00063   //reading ascii master file
00064   try
00065     {
00066       std::ifstream asciiinput(filename);
00067       if (!asciiinput)
00068         throw INTERP_KERNEL::Exception("Master ASCII File does not exist");
00069       char charbuffer[512];
00070       asciiinput.getline(charbuffer,512);
00071 
00072       while (charbuffer[0]=='#')
00073         {
00074           asciiinput.getline(charbuffer,512);
00075         }
00076 
00077       //reading number of domains
00078       nbdomain=atoi(charbuffer);
00079       MyGlobals::_File_Names.resize(nbdomain);
00080       MyGlobals::_Mesh_Names.resize(nbdomain);
00081       (_collection->getMesh()).resize(nbdomain);
00082       cellglobal.resize(nbdomain);
00083       nodeglobal.resize(nbdomain);
00084       faceglobal.resize(nbdomain);
00085 
00086       if (nbdomain == 0)
00087         throw INTERP_KERNEL::Exception("Empty ASCII master file");
00088       for (int i=0; i<nbdomain;i++)
00089         {
00090           //reading information about the domain
00091           std::string mesh,host;
00092           int idomain;
00093           cellglobal[i]=0;
00094           faceglobal[i]=0;
00095           nodeglobal[i]=0;
00096 
00097           asciiinput >> mesh >> idomain >> MyGlobals::_Mesh_Names[i] >> host >> MyGlobals::_File_Names[i];
00098 
00099           //Setting the name of the global mesh (which should be is the same for all the subdomains)
00100           if (i==0)
00101             _collection->setName(mesh);
00102 
00103           if (idomain!=i+1)
00104             {
00105               throw INTERP_KERNEL::Exception("domain must be written from 1 to N in ASCII file descriptor");
00106             }
00107           if ( !domainSelector || domainSelector->isMyDomain(i))
00108             readSubdomain(cellglobal,faceglobal,nodeglobal, i);
00109 
00110         } //loop on domains
00111     } //of try
00112   catch(...)
00113     {
00114       throw INTERP_KERNEL::Exception("I/O error reading parallel MED file");
00115     }
00116 
00117   //creation of topology from mesh and connect zones
00118   ParallelTopology* aPT = new ParallelTopology((_collection->getMesh()), (_collection->getCZ()), cellglobal, nodeglobal, faceglobal);
00119   _collection->setTopology(aPT);
00120 
00121   for (int i=0; i<nbdomain; i++)
00122     {
00123       delete [] cellglobal[i];
00124       delete [] nodeglobal[i];
00125       delete [] faceglobal[i];
00126     }
00127   return 0;
00128 }
00129 
00130 
00135 void MeshCollectionMedAsciiDriver::write(const char* filename, ParaDomainSelector* domainSelector) const
00136 {
00137   int nbdomains=_collection->getMesh().size();
00138   std::vector<std::string> filenames;
00139   filenames.resize(nbdomains);
00140 
00141   //loop on the domains
00142   for (int idomain=0; idomain<nbdomains; idomain++)
00143     {
00144       std::string distfilename;
00145       std::ostringstream suffix;
00146       suffix << filename << idomain+1 << ".med";
00147       distfilename=suffix.str();
00148       filenames[idomain]=distfilename;
00149 
00150       if ( !domainSelector || domainSelector->isMyDomain( idomain ) )
00151         {
00152           if ( !_collection->getMesh()[idomain]->getNumberOfCells()==0 ) continue;//empty domain
00153           MEDLoader::WriteUMesh(distfilename.c_str(),(_collection->getMesh())[idomain],true);
00154           //writeSubdomain(idomain, nbdomains, distfilename.c_str(), domainSelector);
00155         }
00156     }
00157 
00158   //write master file
00159   if ( !domainSelector || domainSelector->rank() == 0 )
00160     {
00161       std::ofstream file(filename);
00162       file << "#MED Fichier V 2.3"<<" " << std::endl;
00163       file << "#" << " " << std::endl;
00164       file << _collection->getMesh().size() << " " << std::endl;
00165 
00166       for (int idomain=0; idomain<nbdomains; idomain++)
00167         file << _collection->getName() <<" "<< idomain+1 << " "
00168              << (_collection->getMesh())[idomain]->getName() << " localhost "
00169              << filenames[idomain] << " "<< std::endl;
00170     }
00171 
00172 }