1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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"
27 #include "MEDCouplingUMesh.hxx"
28 #include "MEDLoader.hxx"
37 #include <libxml/tree.h>
38 #include <libxml/parser.h>
39 #include <libxml/xpath.h>
40 #include <libxml/xpathInternals.h>
42 using namespace MEDPARTITIONER;
44 MeshCollectionMedAsciiDriver::MeshCollectionMedAsciiDriver(MeshCollection* collection):MeshCollectionDriver(collection)
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
52 *\param filename ascii file containing the list of MED v2.3 files
55 int MeshCollectionMedAsciiDriver::read(const char* filename, ParaDomainSelector* domainSelector)
58 std::vector<int*> cellglobal;
59 std::vector<int*> nodeglobal;
60 std::vector<int*> faceglobal;
63 //reading ascii master file
66 std::ifstream asciiinput(filename);
68 throw INTERP_KERNEL::Exception("Master ASCII File does not exist");
70 asciiinput.getline(charbuffer,512);
72 while (charbuffer[0]=='#')
74 asciiinput.getline(charbuffer,512);
77 //reading number of domains
78 nbdomain=atoi(charbuffer);
79 MyGlobals::_File_Names.resize(nbdomain);
80 MyGlobals::_Mesh_Names.resize(nbdomain);
81 (_collection->getMesh()).resize(nbdomain);
82 cellglobal.resize(nbdomain);
83 nodeglobal.resize(nbdomain);
84 faceglobal.resize(nbdomain);
87 throw INTERP_KERNEL::Exception("Empty ASCII master file");
88 for (int i=0; i<nbdomain;i++)
90 //reading information about the domain
91 std::string mesh,host;
97 asciiinput >> mesh >> idomain >> MyGlobals::_Mesh_Names[i] >> host >> MyGlobals::_File_Names[i];
99 //Setting the name of the global mesh (which should be is the same for all the subdomains)
101 _collection->setName(mesh);
105 throw INTERP_KERNEL::Exception("domain must be written from 1 to N in ASCII file descriptor");
107 if ( !domainSelector || domainSelector->isMyDomain(i))
108 readSubdomain(cellglobal,faceglobal,nodeglobal, i);
114 throw INTERP_KERNEL::Exception("I/O error reading parallel MED file");
117 //creation of topology from mesh and connect zones
118 ParallelTopology* aPT = new ParallelTopology((_collection->getMesh()), (_collection->getCZ()), cellglobal, nodeglobal, faceglobal);
119 _collection->setTopology(aPT);
121 for (int i=0; i<nbdomain; i++)
123 delete [] cellglobal[i];
124 delete [] nodeglobal[i];
125 delete [] faceglobal[i];
131 /*! writes the collection of meshes in a MED v2.3 file
132 * with the connect zones being written as joints
133 * \param filename name of the ascii file containing the meshes description
135 void MeshCollectionMedAsciiDriver::write(const char* filename, ParaDomainSelector* domainSelector) const
137 int nbdomains=_collection->getMesh().size();
138 std::vector<std::string> filenames;
139 filenames.resize(nbdomains);
141 //loop on the domains
142 for (int idomain=0; idomain<nbdomains; idomain++)
144 std::string distfilename;
145 std::ostringstream suffix;
146 suffix << filename << idomain+1 << ".med";
147 distfilename=suffix.str();
148 filenames[idomain]=distfilename;
150 if ( !domainSelector || domainSelector->isMyDomain( idomain ) )
152 if ( !_collection->getMesh()[idomain]->getNumberOfCells()==0 ) continue;//empty domain
153 MEDLoader::WriteUMesh(distfilename.c_str(),(_collection->getMesh())[idomain],true);
154 //writeSubdomain(idomain, nbdomains, distfilename.c_str(), domainSelector);
159 if ( !domainSelector || domainSelector->rank() == 0 )
161 std::ofstream file(filename);
162 file << "#MED Fichier V 2.3"<<" " << std::endl;
163 file << "#" << " " << std::endl;
164 file << _collection->getMesh().size() << " " << std::endl;
166 for (int idomain=0; idomain<nbdomains; idomain++)
167 file << _collection->getName() <<" "<< idomain+1 << " "
168 << (_collection->getMesh())[idomain]->getName() << " localhost "
169 << filenames[idomain] << " "<< std::endl;