Salome HOME
644e9e68e3b599689fd1cdd66f78f6de3e936872
[tools/medcoupling.git] / src / MEDPartitioner / MEDPARTITIONER_MeshCollection.hxx
1 // Copyright (C) 2007-2016  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 #ifndef __MEDPARTITIONER_MESHCOLLECTION_HXX__
21 #define __MEDPARTITIONER_MESHCOLLECTION_HXX__
22
23 #include "MEDPARTITIONER.hxx"
24 #include "MEDPARTITIONER_Graph.hxx"
25 #include "MEDPARTITIONER_Utils.hxx"
26
27 #include <map>
28 #include <vector>
29 #include <string>
30
31 #include "BBTree.txx"
32
33 namespace MEDCoupling
34 {
35   class MEDCouplingUMesh;
36   class DataArrayInt;
37   class MEDCouplingSkyLineArray;
38 }
39
40 namespace MEDPARTITIONER
41 {
42   class Topology;
43   class MeshCollectionDriver;
44   class ParaDomainSelector;
45   class ConnectZone;
46   class JointFinder;
47   
48   typedef enum{MedAscii, MedXml, Undefined} DriverType;
49   typedef std::multimap<std::pair<int,int>, std::pair<int,int> > NodeMapping ;
50   typedef std::vector<std::pair<int,int> >  NodeList;
51   
52   class MEDPARTITIONER_EXPORT MeshCollection
53   {
54   public:
55     MeshCollection();
56     //Constructing from an existing mesh and a new topology
57     MeshCollection(MeshCollection&, Topology*, bool family_splitting=false, bool create_empty_groups=false);
58     //Constructing the mesh collection from a file
59     MeshCollection(const std::string& filename);
60     //Constructing the mesh collection from a file
61     MeshCollection(const std::string& filename, ParaDomainSelector& domainSelector);
62     //Constructing the mesh collection from a file
63     MeshCollection(const std::string& filename, const std::string& meshname);
64     ~MeshCollection();
65     bool isParallelMode() const { return _domain_selector; }
66     
67     //writing to a distributed file
68     void write(const std::string& filename);
69     
70     //getting the driver
71     MeshCollectionDriver *retrieveDriver();
72     MeshCollectionDriver *getDriver() const;
73     void setDriverType(MEDPARTITIONER::DriverType type) { _driver_type=type; }
74
75     //creation of the cell graph
76     void buildCellGraph(MEDCoupling::MEDCouplingSkyLineArray* & array,int *& edgeweights );
77    //creation of the cell graph
78     void buildParallelCellGraph(MEDCoupling::MEDCouplingSkyLineArray* & array,int *& edgeweights );
79
80     //creation and partition of the associated graph
81     Topology* createPartition(int nbdomain, Graph::splitter_type type = Graph::METIS,
82                               const std::string& ="", int* edgeweights=0, int* verticesweights=0);
83
84     //creation of a user specified partition
85     Topology* createPartition(const int* partition);
86
87     //getting mesh dimension
88     int getMeshDimension() const;
89     int getNbOfLocalMeshes() const;
90     int getNbOfGlobalMeshes() const { return _mesh.size(); }
91     int getNbOfLocalCells() const;
92     int getNbOfLocalFaces() const;
93     
94     //getting a reference to mesh vector
95     std::vector<MEDCoupling::MEDCouplingUMesh*>& getMesh();
96     std::vector<MEDCoupling::MEDCouplingUMesh*>& getFaceMesh();
97     std::vector<std::vector<MEDCoupling::MEDCouplingUMesh*> >& getGroupMeshes();
98
99     MEDCoupling::MEDCouplingUMesh* getMesh(int idomain) const;
100     MEDCoupling::MEDCouplingUMesh* getFaceMesh(int idomain);
101     std::vector<MEDCoupling::MEDCouplingUMesh*>& getGroupMeshes(int idomain);
102
103     std::vector<MEDCoupling::DataArrayInt*>& getCellFamilyIds() { return _cell_family_ids; }
104     std::vector<MEDCoupling::DataArrayInt*>& getFaceFamilyIds() { return _face_family_ids; }
105     
106     std::map<std::string, MEDCoupling::DataArrayInt*>& getMapDataArrayInt() { return _map_dataarray_int; }
107     std::map<std::string, MEDCoupling::DataArrayDouble*>& getMapDataArrayDouble() { return _map_dataarray_double; }
108
109     std::map<std::string,int>& getFamilyInfo() { return _family_info; }
110     std::map<std::string, std::vector<std::string> >& getGroupInfo() { return _group_info; }
111
112     MEDCoupling::DataArrayDouble* getField(std::string descriptionField, int iold);
113     std::vector<std::string>&  getFieldDescriptions() { return _field_descriptions; }
114     void prepareFieldDescriptions();
115     void filterFaceOnCell();
116      
117     //getting a reference to connect zones vector
118     std::vector<MEDPARTITIONER::ConnectZone*>& getCZ();
119
120     //getting a pointer to topology
121     Topology* getTopology() const ;
122     ParaDomainSelector* getParaDomainSelector() const { return _domain_selector; }
123     void setParaDomainSelector(ParaDomainSelector* pds) { _domain_selector = pds; }
124     //setting a new topology
125     void setTopology(Topology* topology, bool takeOwneship);
126
127     //getting/setting the name of the global mesh (as opposed 
128     //to the name of a subdomain \a nn, which is name_nn) 
129     std::string getName() const { return _name; }
130     void setName(const std::string& name) { _name=name; }
131     void setDomainNames(const std::string& name);
132
133     void setNonEmptyMesh(int number) { _i_non_empty_mesh=number;}
134
135     //getting/setting the description of the global mesh
136     std::string getDescription() const { return _description; }
137     void setDescription(const std::string& name) { _description=name; }
138
139     //creates the node mapping between an old collection and the present one
140     void createNodeMapping(MeshCollection& initialCollection, 
141                            std::multimap<std::pair<int,int>,std::pair<int,int> >& nodeMapping);
142     
143     void castCellMeshes(MeshCollection& initialCollection, 
144                         std::vector<std::vector<std::vector<int> > >& new2oldIds,
145                         std::vector<MEDCoupling::DataArrayInt*> & o2nRenumber);
146     
147     //creates faces on the new collection
148     void castFaceMeshes(MeshCollection& initialCollection,
149                         const std::multimap<std::pair<int,int>, std::pair<int,int> >& nodeMapping,
150                         std::vector<std::vector<std::vector<int> > >& new2oldIds);
151
152     //constructing connect zones
153     void buildConnectZones( const NodeMapping& nodeMapping,
154                             const std::vector<MEDCoupling::DataArrayInt*> & o2nRenumber,
155                             int nbInitialDomains );
156
157     // Find faces common with neighbor domains and put them in groups
158     void buildBoundaryFaces();
159
160   private:
161     void castIntField(std::vector<MEDCoupling::MEDCouplingUMesh*>& meshesCastFrom,
162                        std::vector<MEDCoupling::MEDCouplingUMesh*>& meshesCastTo,
163                        std::vector<MEDCoupling::DataArrayInt*>& arrayFrom,
164                        std::string nameArrayTo);
165
166     void castAllFields(MeshCollection& initialCollection,
167                        std::string nameArrayTo);
168
169     void findCommonDistantNodes(std::vector<std::vector<std::multimap<int,int> > >& commonDistantNodes);
170
171     
172     void remapIntField(int inew, int iold, 
173                        const MEDCoupling::MEDCouplingUMesh& sourceMesh,
174                        const MEDCoupling::MEDCouplingUMesh& targetMesh,
175                        const int* fromArray,
176                        std::string nameArrayTo,
177                        const BBTreeOfDim* tree);
178
179     void remapDoubleField(int inew, int iold,
180                            MEDCoupling::DataArrayDouble* fromArray,
181                            std::string nameArrayTo,
182                            std::string descriptionField);
183
184     void createJointGroup( const std::vector< int >& faces,
185                            const int                 inew1,
186                            const int                 inew2,
187                            const bool                is2nd );
188   private:
189
190     //link to mesh_collection topology
191     Topology* _topology;
192     
193     //control over topology
194     bool _owns_topology;
195     
196     //Driver for read/write operations
197     MeshCollectionDriver* _driver;
198     
199     //Parallelizer - mark of parallel execution mode
200     ParaDomainSelector* _domain_selector;
201     
202     //links to meshes
203     std::vector<MEDCoupling::MEDCouplingUMesh*> _mesh;
204     std::vector<MEDCoupling::MEDCouplingUMesh*> _face_mesh;
205     
206     //index of a non empty mesh within _mesh (in parallel mode all of meshes can be empty)
207     int _i_non_empty_mesh;
208     
209     //family ids storages
210     std::vector<MEDCoupling::DataArrayInt*> _cell_family_ids;
211     std::vector<MEDCoupling::DataArrayInt*> _face_family_ids;
212     
213     //DataArrayInt* storages
214     std::map<std::string, MEDCoupling::DataArrayInt*> _map_dataarray_int;
215     //DataArrayDouble* storages
216     std::map<std::string, MEDCoupling::DataArrayDouble*> _map_dataarray_double;
217     
218     //fields to be partitioned
219     std::vector<std::string> _field_descriptions;
220     
221     //group family conversion
222     std::map<std::string, int> _family_info;
223     std::map<std::string, std::vector<std::string> > _group_info;
224   
225     //list of groups that are not to be split
226     std::vector<std::string> _indivisible_regions;
227
228     //name of global mesh
229     std::string _name;
230
231     //description of global mesh
232     std::string _description;
233
234     //specifies the driver associated to the collection
235     DriverType _driver_type;
236
237     //flag specifying that the splitter should create boundary constituent entity
238     //so that they are written in joints
239     bool _subdomain_boundary_creates;
240
241     //flag specifying that families must be preserved by the splitting
242     bool _family_splitting;
243
244     //flag specifying that groups must be created on all domains, even if they are empty
245     bool _create_empty_groups;
246
247     JointFinder* _joint_finder;
248   };
249 }
250 #endif