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