Salome HOME
*** empty log message ***
[modules/multipr.git] / src / MULTIPR / MULTIPR_MeshDis.hxx
1 // Project MULTIPR, IOLS WP1.2.1 - EDF/CS
2 // Partitioning/decimation module for the SALOME v3.2 platform
3
4 /**
5  * \file    MULTIPR_MeshDis.hxx
6  *
7  * \brief   Class MeshDis: distributed mesh.
8  *          = MASTER file (ASCII) -> list of sequential MED file.
9  *
10  * \author  Olivier LE ROUX - CS, Virtual Reality Dpt
11  * 
12  * \date    01/2007
13  */
14
15 #ifndef MULTIPR_MESHDIS_HXX
16 #define MULTIPR_MESHDIS_HXX
17
18 //*****************************************************************************
19 // Includes section
20 //*****************************************************************************
21
22 extern "C"
23 {
24         #include "med.h"
25 }
26
27 #include <iostream>
28 #include <vector>
29
30 // include MEDSPLITTER used to split mesh using METIS or SCOTCH
31 #include "MEDMEM_define.hxx"
32 #include "MEDMEM_Mesh.hxx"
33 #include "MEDMEM_Family.hxx"
34 #include "MEDSPLITTER_Graph.hxx"
35 #include "MEDSPLITTER_MESHCollection.hxx"
36 #include "MEDSPLITTER_Topology.hxx"
37
38
39 namespace multipr
40 {
41
42 //*****************************************************************************
43 // Pre-declaration
44 //*****************************************************************************
45
46 class Mesh;
47
48
49 //*****************************************************************************
50 // Class MeshDisPart = a sub-part of a distributed mesh.
51 //
52 // It can be :
53 // - a sequential MED file representing a Group (scenario 1)
54 // - a sequential MED file representing a part of a Group (scenario 2 -> MEDSPLITTER)
55 // - a lower resolution of a part (decimation)
56 //*****************************************************************************
57
58 class MeshDisPart
59 {
60
61 public:
62
63         /**
64          * Action to be done for this part on next writing on disk.
65          */
66         enum OnNextWrite
67         {
68                 MULTIPR_UNDEFINED,
69                 MULTIPR_KEEP_AS_IT,
70                 MULTIPR_WRITE_MESH,
71                 MULTIPR_WRITE_PARTS
72         };
73         
74 public:
75
76         /**
77          * Builds an empty part of a distributed mesh (default constructor).
78          */
79         MeshDisPart();
80         
81         /**
82          * Destructor. Removes everything.
83          */
84         ~MeshDisPart();
85         
86         /**
87          * Resets this object in its state by default (empty). Cleans memory.
88          */
89         void reset();
90         
91         /**
92          * Creates a MeshDisPart.
93          * \param  pToDoOnNextWrite
94          * \param  pMeshName
95          * \param  pId
96          * \param  pPartName
97          * \param  pPath
98          * \param  pMEDFileName
99          * \param  pMesh can be NULL.
100          */
101         void create(
102                 OnNextWrite pToDoOnNextWrite,
103                 const char* pMeshName, 
104                 int         pId,
105                 const char* pPartName,
106                 const char* pPath,
107                 const char* pMEDFileName,
108                 Mesh*       pMesh);
109         
110         //---------------------------------------------------------------------
111         // Basic accessors/mutators
112         //---------------------------------------------------------------------
113         
114         /**
115          * Returns the name of this part.
116          * \return the name of this part.
117          */
118         const char* getPartName() const { return mPartName; }
119         
120         /**
121          * Returns the name of the mesh of this part.
122          * \return the name of the mesh of this part.
123          */
124         const char* getMeshName() const { return mMeshName; }
125         
126         /**
127          * Returns the MED filename which contain this part.
128          * \return the MED filename which contain this part.
129          */
130         const char* getMEDFileName() const { return mMEDFileName; }
131         
132         /**
133          * Returns the suffix of the related MED filename (without .med extension).
134          * For examples:
135          * 1. "agregat100grains_12pas_grain97.med"                       -> "grain97"
136          * 2. "agregat100grains_12pas_grain100_part2.med"                -> "grain100_part2"
137          * 3. "aagregat100grains_12pas_grain98_gradmoy-low-25.0-0.3.med" -> "grain98_gradmoy-low-25-0.3"
138          */
139         const char* getMEDFileNameSuffix() const;
140         
141         /**
142          * Returns the action to be performed on this part on next write.
143          * \return the action to be performed on this part on next write.
144          */
145         OnNextWrite getOnNextWrite() const { return mToDoOnNextWrite; }
146          
147         //---------------------------------------------------------------------
148         // I/O
149         //---------------------------------------------------------------------
150         
151         /**
152          * Reads the sequentiel MED file corresponding to this part.
153          * \throw  IOException if an i/o error occurs.
154          */
155         void readMED();
156          
157         /**
158          * Dumps any MeshDisPart to the given output stream.
159          * \param  pOs any output stream.
160          * \param  pM  any MeshDisPart.
161          * \return the output stream pOs.
162          */
163         friend std::ostream& operator<<(std::ostream& pOs, MeshDisPart& pM);
164         
165 private:
166
167         // MeshDisPart can be used:
168         // 1 (KEEP_AS_IT)  : To store data read from one line of an ASCII master file (distributed MED file)
169         // 2 (WRITE_MESH)  : As a temporary structure to store all infos about a mesh corresponding to a group (before writing on disk).
170         // 3 (WRITE_PARTS) : As a temporary structure to store all infos about splitting using MEDSPLITTER.
171         
172         OnNextWrite  mToDoOnNextWrite;               /**< See enum OnNextWrite. */
173         
174         //---------------------------------------------------------------------
175         // Case 1, 2, and 3
176         //---------------------------------------------------------------------         
177         char   mMeshName[MED_TAILLE_NOM + 1];        /**< Name of the mesh. */
178         int    mId;                                  /**< Id of this part in [1..n]. */
179         char   mPartName[MED_TAILLE_NOM + 1];        /**< Name of this part. */
180         char   mPath[256];                           /**< Path of the MED file. */
181         char   mMEDFileName[256];                    /**< Name of the MED file which contain this part. */
182         
183         //---------------------------------------------------------------------
184         // Case 2: mesh of the related sequential MED file (can be NULL if not loaded)
185         //---------------------------------------------------------------------
186         Mesh*  mMesh;                                /**< Mesh associated with this part; can be NULL. */
187         
188         //---------------------------------------------------------------------
189         // Case 3 only: temporary result of MEDSPLITTER
190         //---------------------------------------------------------------------
191         int                          mSplit;         /**< For MEDSPLITTER: number of parts. Temporary. */
192         MEDSPLITTER::MESHCollection* mCollection;    /**< New data after splitting. */
193         MEDSPLITTER::MESHCollection* mOldCollection; /**< Data before splitting (we need them when we want to write new data on disk. */
194         
195 private:
196
197         // do not allow copy constructor
198         MeshDisPart(const MeshDisPart&);
199         
200         // do not allow copy
201         MeshDisPart& operator=(const MeshDisPart&);
202         
203         // do not allow operator ==
204         bool operator==(const MeshDisPart&); 
205         
206         //---------------------------------------------------------------------
207         // Friends
208         //--------------------------------------------------------------------- 
209         friend class MeshDis;
210         
211 }; // class MeshDisPart
212
213
214 //*****************************************************************************
215 // Class MeshDis
216 //*****************************************************************************
217
218 class MeshDis
219 {
220 public:
221
222         /**
223          * Builds an empty mesh (default constructor).
224          */
225         MeshDis();
226          
227         /**
228          * Destructor. Removes everything.
229          */
230         ~MeshDis();
231          
232         /**
233          * Resets this object in its state by default (empty). Clean memory.
234          */
235         void reset();
236         
237         //---------------------------------------------------------------------
238         // Basic accessors/mutators
239         //---------------------------------------------------------------------
240
241         /**
242          * Returns the name of this distributed MED file (=name of the master file).
243          * \return the name of this distributed MED file (=name of the master file).
244          */
245         const char* getFilename() const { return mMEDfilename; }
246          
247         /**
248          * Adds a new part to this distributed mesh.
249          * Used by the split process (extract groups).
250          * \param  pToDoOnNextWrite
251          * \param  pMeshName
252          * \param  pId
253          * \param  pPartName
254          * \param  pPath
255          * \param  pMEDFileName
256          * \param  pMesh can be NULL.
257          */
258         void addMesh(
259                 MeshDisPart::OnNextWrite pToDoOnNextWrite,
260                 const char* pMeshName, 
261                 int         pId,
262                 const char* pPartName,
263                 const char* pPath,
264                 const char* pMEDFileName,
265                 Mesh*       pMesh);
266         
267         /**
268          * Inserts a new part to this distributed mesh.
269          * Used by the decimation process.
270          * \param  pToDoOnNextWrite
271          * \param  pMeshName
272          * \param  pId
273          * \param  pPartName
274          * \param  pPath
275          * \param  pMEDFileName
276          * \param  pMesh can be NULL.
277          * \param  pPosition insert after this position. Start at 1.
278          */
279         void insertMesh(
280                 MeshDisPart::OnNextWrite pToDoOnNextWrite,
281                 const char* pMeshName, 
282                 int         pId,
283                 const char* pPartName,
284                 const char* pPath,
285                 const char* pMEDFileName,
286                 Mesh*       pMesh,
287                 int         pPosition);
288         
289         /**
290          * Removes all the part beginning by pPrefixPartName from this distributed mesh.
291          * \param  pPrefixPartName name of the part.
292          */
293         void removeParts(const char* pPrefixPartName);
294         
295         /**
296          * Returns the current number of parts in this distributed mesh.
297          * \return the current number of parts in this distributed mesh.
298          */
299         int getNumParts() const { return mParts.size(); }
300         
301         /**
302          * Returns the nth part of this distributed mesh.
303          * \param  pIndex index of the part (in 0..getNumParts()-1).
304          * \return the nth part of this distributed mesh.
305          */
306         MeshDisPart* getPart(int pIndex) const { return mParts[pIndex]; }
307         
308         /**
309          * Returns the list of meshes contained in this distributed MED file.
310          * \return the list of meshes contained in this distributed MED file.
311          */
312         std::vector<std::string> getMeshes() const; 
313         
314         /**
315          * Returns the list of fields contained in this distributed MED file.
316          * \return the list of fields contained in this distributed MED file.
317          */
318         std::vector<std::string> getFields() const; 
319         
320         /**
321          * Returns the number of iteration for a given field.
322          * \param  pFieldName field name.
323          * \return the number of iteration for a given field.
324          */
325         int getTimeStamps(const char* pFieldName) const; 
326         
327         /**
328          * Returns all information about a part.
329          * \param  pPartName name of the part.
330          * \return all information about a part.
331          */
332         std::string getPartInfo(const char* pPartName);
333          
334         //---------------------------------------------------------------------
335         // Algorithms
336         //---------------------------------------------------------------------
337         
338         /**
339          * Finds a part of this distributed mesh by its name.
340          * Returns NULL if the part does not exist.
341          * \param  pPartName part to be found; must not be NULL.
342          * \return a pointer towards the part if it exists, NULL otherwise.
343          * \throw  NullArgumentException if pPartName is NULL.
344          */
345         MeshDisPart* findPart(const char* pPartName);
346         
347         /**
348          * Updates this distributed mesh by splitting one of its part.
349          * This splitting method leans on medsplitter, by V. Bergeaud (CEA).
350          * \param  pPartName     name of the part to be splitted.
351          * \param  pNbParts      number of sub-parts.
352          * \param  pPartitionner MULTIPR_METIS or MULTIPR_SCOTCH.
353          * \throw  RuntimeException if any error occurs.
354          */
355         void splitPart(const char* pPartName, int pNbParts, int pPartitionner);
356         
357         /**
358          * Creates 3 resolution (CURRENT = FULL, MEDIUM and LOW) of a part of this distributed mesh.
359          * Names of new meshes are <original_name>_MED and <original_name>_LOW.
360          * \param  pPartName
361          * \param  pFielName
362          * \param  pFieldIt
363          * \param  pFilterName
364          * \param  pTMed        threshold used to generate MEDIUM resolution.
365          * \param  pTLow        threshold used to generate LOW resolution (must be >= pTMed).
366          * \param  pRadius
367          * \param  pBoxing number of cells along each axis; e.g. if 100 then grid will have 100*100*100 = 10**6 cells; 100 by default.
368          * \throw  RuntimeException if any error occurs.
369          */
370         void decimatePart(
371                 const char* pPartName, 
372                 const char* pFieldName,
373                 med_int     pFieldIt,
374                 const char* pFilterName,
375                 med_float   pTMed, 
376                 med_float   pTLow,
377                 med_float   pRadius,
378                 int         pBoxing = 100);
379         
380         //---------------------------------------------------------------------
381         // I/O
382         //---------------------------------------------------------------------
383         
384         /**
385          * Reads the master file of a distributed MED file.
386          * \param  pMEDfilename
387          * \throw  NullArgumentException if pMEDfilename is NULL.
388          * \throw  IOException if any i/o error occurs.
389          */
390         void readDistributedMED(const char* pMEDfilename);
391         
392         /**
393          * Writes this distributed MED file (including master file and sub MED files if necessary).
394          * \param  pMEDfilenamePrefix
395          * \throw  NullArgumentException if pMEDfilename is NULL.
396          * \throw  IOException if any i/o error occurs.
397          */
398         void writeDistributedMED(const char* pMEDfilenamePrefix);
399         
400         /**
401          * Dumps any MeshDis to the given output stream.
402          * \param  pOs any output stream.
403          * \param  pM  any MeshDis.
404          * \return the output stream pOs.
405          */
406         friend std::ostream& operator<<(std::ostream& pOs, MeshDis& pM);
407         
408 private:
409
410         /**
411          * Recomputes the number of parts in this distributed mesh.
412          * This method is used by writeDistributedMED().
413          * \return the number of parts in this distributed mesh.
414          */
415         int computeNumParts();
416         
417 private:
418
419         char                        mMEDfilename[256];  /** Name of this distributed MED file (= name of the master file). */
420         std::vector<MeshDisPart*>   mParts;             /**< Table of sub-parts; a distributed mesh is composed of N sub-part, where N = mParts.size(). */
421         //MULTIPR_ProgressCallback*   mProgressCallback;
422         
423 private:
424
425         // do not allow copy constructor
426         MeshDis(const MeshDis&);
427         
428         // do not allow copy
429         MeshDis& operator=(const MeshDis&);
430         
431         // do not allow operator ==
432         bool operator==(const MeshDis&); 
433         
434 }; // class MeshDis
435
436
437 } // namespace MULTIPR
438
439
440 #endif // MULTIPR_MESHDIS_HXX
441
442 // EOF