Salome HOME
0022875: EDF 7690 MED: Creating joints with medpartitioner in the MEDCoupling API
[tools/medcoupling.git] / src / MEDPartitioner / MEDPARTITIONER_MEDPartitioner.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_MEDPartitioner.hxx"
21 #include "MEDPARTITIONER_MeshCollection.hxx"
22 #include "MEDPARTITIONER_Topology.hxx"
23 #include "MEDPARTITIONER_ParaDomainSelector.hxx"
24 #include "MEDPARTITIONER_ParallelTopology.hxx"
25 #include "MEDPARTITIONER_Utils.hxx"
26 #include "MEDPARTITIONER_Graph.hxx"
27 #include "MEDPARTITIONER_MetisGraph.hxx"
28 #include "MEDPARTITIONER_ScotchGraph.hxx"
29 #include "MEDPARTITIONER_MeshCollectionDriver.hxx"
30 #include "MEDCouplingUMesh.hxx"
31
32 #include <iostream>
33 #include <vector>
34
35 MEDPARTITIONER::MEDPartitioner::MEDPartitioner(const std::string& filename, int ndomains, const std::string& library,bool creates_boundary_faces, bool create_joints, bool mesure_memory):
36   _input_collection( 0 ), _output_collection( 0 ), _new_topology( 0 )
37 {
38   MyGlobals::_World_Size = 1;
39   MyGlobals::_Rank = 0;
40   MyGlobals::_Creates_Boundary_Faces = creates_boundary_faces;
41   MyGlobals::_Create_Joints = create_joints;
42
43   ParaDomainSelector parallelizer(mesure_memory);
44   _input_collection=new MeshCollection(filename,parallelizer);
45   _input_collection->setParaDomainSelector( &parallelizer );
46
47   MEDPARTITIONER::ParallelTopology* aPT =
48     (MEDPARTITIONER::ParallelTopology*) _input_collection->getTopology();
49   aPT->setGlobalNumerotationDefault( _input_collection->getParaDomainSelector() );
50   _input_collection->prepareFieldDescriptions();
51   createPartitionCollection(ndomains, library, creates_boundary_faces, create_joints, mesure_memory);
52
53   parallelizer.evaluateMemory();
54 }
55
56 MEDPARTITIONER::MEDPartitioner::MEDPartitioner(const ParaMEDMEM::MEDFileData* filedata, int ndomains, const std::string& library,bool creates_boundary_faces, bool create_joints, bool mesure_memory):
57   _input_collection( 0 ), _output_collection( 0 ), _new_topology( 0 )
58 {
59   MyGlobals::_World_Size = 1;
60   MyGlobals::_Rank = 0;
61   MyGlobals::_Creates_Boundary_Faces = creates_boundary_faces;
62   MyGlobals::_Create_Joints = create_joints;
63
64   ParaDomainSelector parallelizer(mesure_memory);
65   _input_collection=new MeshCollection();
66   _input_collection->setParaDomainSelector( &parallelizer );
67   _input_collection->retrieveDriver()->readMEDFileData(filedata);
68
69   MEDPARTITIONER::ParallelTopology* aPT =
70     (MEDPARTITIONER::ParallelTopology*) _input_collection->getTopology();
71   aPT->setGlobalNumerotationDefault( _input_collection->getParaDomainSelector() );
72   _input_collection->prepareFieldDescriptions();
73   createPartitionCollection(ndomains, library, creates_boundary_faces, create_joints, mesure_memory);
74
75   parallelizer.evaluateMemory();
76 }
77
78 MEDPARTITIONER::MEDPartitioner::MEDPartitioner(const ParaMEDMEM::MEDFileData* filedata, MEDPARTITIONER ::Graph* graph, bool creates_boundary_faces, bool create_joints, bool mesure_memory):
79   _input_collection( 0 ), _output_collection( 0 ), _new_topology( 0 )
80 {
81   MyGlobals::_World_Size = 1;
82   MyGlobals::_Rank = 0;
83   MyGlobals::_Creates_Boundary_Faces = creates_boundary_faces;
84   MyGlobals::_Create_Joints = create_joints;
85
86   ParaDomainSelector parallelizer(mesure_memory);
87   _input_collection=new MeshCollection();
88   _input_collection->setParaDomainSelector( &parallelizer );
89   _input_collection->retrieveDriver()->readMEDFileData(filedata);
90
91   MEDPARTITIONER::ParallelTopology* aPT =
92     (MEDPARTITIONER::ParallelTopology*) _input_collection->getTopology();
93   aPT->setGlobalNumerotationDefault( _input_collection->getParaDomainSelector() );
94   _input_collection->prepareFieldDescriptions();
95
96   _new_topology = new MEDPARTITIONER::ParallelTopology( graph, aPT, graph->nbDomains(), _input_collection->getMeshDimension() );
97   _output_collection=new MeshCollection(*_input_collection,_new_topology,false,false);
98   _output_collection->filterFaceOnCell();
99
100   parallelizer.evaluateMemory();
101 }
102
103 MEDPARTITIONER::MEDPartitioner::~MEDPartitioner()
104 {
105   delete _input_collection; _input_collection = 0;
106   delete _output_collection; _output_collection = 0;
107   delete _new_topology; _new_topology = 0;
108 }
109
110 void MEDPARTITIONER::MEDPartitioner::createPartitionCollection(int ndomains, const std::string& library,bool creates_boundary_faces, bool create_joints, bool mesure_memory)
111 {
112   //ParallelTopology* aPT = (ParallelTopology*) _input_collection->getTopology();
113   if (library == "metis")
114     _new_topology = _input_collection->createPartition(ndomains,MEDPARTITIONER::Graph::METIS);
115   else
116     _new_topology = _input_collection->createPartition(ndomains,MEDPARTITIONER::Graph::SCOTCH);
117   _output_collection=new MeshCollection(*_input_collection,_new_topology,false,false);
118   _output_collection->filterFaceOnCell();
119 }
120
121 void MEDPARTITIONER::MEDPartitioner::write(const std::string& filename)
122 {
123   ParaDomainSelector parallelizer(false);
124   _output_collection->setParaDomainSelector( &parallelizer );
125   _output_collection->write(filename);
126   parallelizer.evaluateMemory();
127 }
128
129 ParaMEDMEM::MEDFileData* MEDPARTITIONER::MEDPartitioner::getMEDFileData()
130 {
131   return _output_collection->retrieveDriver()->getMEDFileData();
132 }
133
134 MEDPARTITIONER::Graph* MEDPARTITIONER::MEDPartitioner::Graph(ParaMEDMEM::MEDCouplingSkyLineArray* graph, Graph::splitter_type split, int* edgeweight)
135 {
136   MEDPARTITIONER::Graph* cellGraph=0;
137   ParaMEDMEM::MEDCouplingSkyLineArray* arr = new ParaMEDMEM::MEDCouplingSkyLineArray(graph->getIndexArray(), graph->getValueArray());
138   switch (split)
139     {
140     case Graph::METIS:
141       if ( !cellGraph )
142         {
143 #ifdef MED_ENABLE_METIS
144           cellGraph=new METISGraph(arr,edgeweight);
145 #endif
146         }
147       if ( !cellGraph )
148         throw INTERP_KERNEL::Exception("MEDPartitioner::Graph : PARMETIS/METIS is not available. Check your products, please.");
149       break;
150     case Graph::SCOTCH:
151 #ifdef MED_ENABLE_SCOTCH
152       cellGraph=new SCOTCHGraph(arr,edgeweight);
153 #else
154       throw INTERP_KERNEL::Exception("MEDPartitioner::Graph : SCOTCH is not available. Check your products, please.");
155 #endif
156       break;
157     }
158   return cellGraph;
159 }