Salome HOME
updated copyright message
[plugins/ghs3dplugin.git] / src / GHS3DPlugin / GHS3DPlugin_Hypothesis.hxx
1 // Copyright (C) 2004-2023  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 //  GHS3DPlugin : C++ implementation
21 // File      : GHS3DPlugin_Hypothesis.hxx
22 // Created   : Wed Apr  2 12:21:17 2008
23 // Author    : Edward AGAPOV (eap)
24 //
25 #ifndef GHS3DPlugin_Hypothesis_HeaderFile
26 #define GHS3DPlugin_Hypothesis_HeaderFile
27
28 #include "GHS3DPlugin_Defs.hxx"
29
30 #include <SMDS_MeshNode.hxx>
31
32 #include "SMESH_Hypothesis.hxx"
33 #include "SMESH_Mesh_i.hxx"
34 #include "SMESH_Gen_i.hxx"
35 #include "SMESH_TypeDefs.hxx"
36 #include "utilities.h"
37
38 #include <stdexcept>
39 #include <map>
40 #include <vector>
41 #include <cstdio>
42
43 class GHS3DPLUGIN_EXPORT GHS3DPlugin_Hypothesis: public SMESH_Hypothesis
44 {
45 public:
46
47   GHS3DPlugin_Hypothesis(int hypId, SMESH_Gen * gen);
48
49   typedef std::map<std::vector<double>,double> TGHS3DEnforcedVertexCoordsValues;
50   typedef std::map<std::string,double> TGHS3DEnforcedVertexEntryValues;
51   
52   struct TGHS3DEnforcedVertex {
53     std::string name;
54     std::string geomEntry;
55     bool isCompound;
56     std::vector<double> coords;
57     std::string groupName;
58     double size;
59   };
60   
61   struct CompareGHS3DEnforcedVertex {
62     bool operator () (const TGHS3DEnforcedVertex* e1, const TGHS3DEnforcedVertex* e2) const {
63       if (e1 && e2) {
64         if (e1->coords.size() && e2->coords.size())
65           return (e1->coords < e2->coords);
66         else
67           return (e1->geomEntry < e2->geomEntry);
68       }
69       return false;
70     }
71   };
72   typedef std::set< TGHS3DEnforcedVertex*, CompareGHS3DEnforcedVertex > TGHS3DEnforcedVertexList;
73   // Map Coords / Enforced node
74   typedef std::map< std::vector<double>, TGHS3DEnforcedVertex* > TCoordsGHS3DEnforcedVertexMap;
75   // Map geom entry / Enforced node
76   typedef std::map< std::string, TGHS3DEnforcedVertex* > TGeomEntryGHS3DEnforcedVertexMap;
77   // Map groupName / Enforced node
78   typedef std::map< std::string, TGHS3DEnforcedVertexList > TGroupNameGHS3DEnforcedVertexMap;
79   
80   ////////////////////
81   // Enforced meshes
82   ////////////////////
83   
84   struct TGHS3DEnforcedMesh {
85     int         persistID;
86     std::string name;
87     std::string entry;
88     std::string groupName;
89     SMESH::ElementType elementType;
90   };
91   
92   struct CompareGHS3DEnforcedMesh {
93     bool operator () (const TGHS3DEnforcedMesh* e1, const TGHS3DEnforcedMesh* e2) const {
94       if (e1 && e2) {
95         if (e1->entry == e2->entry)
96           return (e1->elementType < e2->elementType);
97         else
98           return (e1->entry < e2->entry);
99       }
100       else
101         return false;
102     }
103   };
104   typedef std::set< TGHS3DEnforcedMesh*, CompareGHS3DEnforcedMesh > TGHS3DEnforcedMeshList;
105   // Map mesh entry / Enforced mesh list
106   // ex: 0:1:2:1 -> [ ("Mesh_1", "0:1:2:1", TopAbs_NODE, ""),
107   //                  ("Mesh_1", "0:1:2:1", TopAbs_EDGE, "edge group")]
108   typedef std::map< std::string, TGHS3DEnforcedMeshList > TEntryGHS3DEnforcedMeshListMap;
109   
110   typedef std::map<int,double> TID2SizeMap;
111
112   struct TIDMeshIDCompare {
113     bool operator () (const SMDS_MeshElement* e1, const SMDS_MeshElement* e2) const
114     { return e1->GetMesh() == e2->GetMesh() ? e1->GetID() < e2->GetID() : e1->GetMesh() < e2->GetMesh(); }
115   };
116   
117   typedef std::map<const SMDS_MeshElement*, std::string, TIDMeshIDCompare > TIDSortedElemGroupMap;
118   typedef std::map<const SMDS_MeshNode*, std::string, TIDMeshIDCompare > TIDSortedNodeGroupMap;
119   typedef std::set<std::string> TSetStrings;
120
121   static const char* GetHypType() { return "MG-Tetra Parameters"; }
122
123   void SetMinSize(double theMinSize);
124   double GetMinSize() const { return myMinSize; }
125
126   void SetMaxSize(double theMaxSize);
127   double GetMaxSize() const { return myMaxSize; }
128
129   void SetUseVolumeProximity( bool toUse );
130   bool GetUseVolumeProximity() const { return myUseVolumeProximity; }
131
132   void SetNbVolumeProximityLayers( int nbLayers );
133   int GetNbVolumeProximityLayers() const { return myNbVolumeProximityLayers; }
134
135   /*!
136    * To mesh "holes" in a solid or not. Default is to mesh.
137    */
138   void SetToMeshHoles(bool toMesh);
139   bool GetToMeshHoles(bool checkFreeOption = false) const;
140   /*!
141    * To make groups of volumes of different domains when mesh is generated from skin.
142    * Default is to make groups.
143    * This option works only (1) for the mesh w/o shape and (2) if GetToMeshHoles() == true
144    */
145   void SetToMakeGroupsOfDomains(bool toMakeGroups);
146   bool GetToMakeGroupsOfDomains() const;
147   /*!
148    * Maximal size of memory to be used by the algorithm (in Megabytes)
149    */
150   void SetMaximumMemory(float MB);
151   float GetMaximumMemory() const;
152   /*!
153    * Initial size of memory to be used by the algorithm (in Megabytes) in
154    * automatic memory adjustment mode. Default is zero
155    */
156   void SetInitialMemory(float MB);
157   float GetInitialMemory() const;
158   /*!
159    * Optimization level: 0-none, 1-light, 2-medium, 3-standard+, 4-strong. Default is medium
160    */
161   enum OptimizationLevel { None = 0, Light, Medium, StandardPlus, Strong };
162   void SetOptimizationLevel(OptimizationLevel level);
163   OptimizationLevel GetOptimizationLevel() const;
164   /*!
165    * Path to working directory
166    */
167   void SetWorkingDirectory(const std::string& path);
168   std::string GetWorkingDirectory() const;
169   /*!
170    * To keep working files or remove them. Log file remains in case of errors anyway.
171    */
172   void SetKeepFiles(bool toKeep);
173   bool GetKeepFiles() const;
174   /*!
175    * Verbose level [0-10]
176    *  0 - no standard output,
177    *  2 - prints the data, quality statistics of the skin and final meshes and
178    *     indicates when the final mesh is being saved. In addition the software
179    *     gives indication regarding the CPU time.
180    * 10 - same as 2 plus the main steps in the computation, quality statistics
181    *     histogram of the skin mesh, quality statistics histogram together with
182    *     the characteristics of the final mesh.
183    */
184   void SetVerboseLevel(short level);
185   short GetVerboseLevel() const;
186   /*!
187    * Implemented algorithms to be executed [0,1]
188    *  0 - MGTetra-HPC
189    *  1 - MGTetra
190    */
191   enum ImplementedAlgorithms { MGTetraHPC = 0,  MGTetra };
192   void SetAlgorithm(ImplementedAlgorithms algoId);
193   ImplementedAlgorithms GetAlgorithm() const;
194   /*!
195    * Set Get for flag to use pthread parallel version of the algorithm
196    */
197   void SetUseNumOfThreads(bool setUseOfThreads);
198   bool GetUseNumOfThreads() const;
199    /*!
200    * Set Get num of threads to be used by MGTetra algorithms
201    */
202   void SetNumOfThreads(short numOfThreads);
203   short GetNumOfThreads() const;
204   /*!
205   * Set Get pthread mode used for MGTetra
206   */
207   enum PThreadMode { PThreadNone = 0, PThreadAggressive,  Safe };
208   void SetPthreadMode(PThreadMode pthreadMode );
209   PThreadMode GetPthreadMode() const;
210   /*!
211   * Set Get paralle mode used for MGTetra HPC
212   */
213   enum ParallelMode { ParallelNone = 0, ReproducibleGivenMaxNumThreads, Reproducible, ParallelAggressive };
214   void SetParallelMode(ParallelMode parallelMode );
215   ParallelMode GetParallelMode() const;
216   /*!
217    * To create new nodes
218    */
219   void SetToCreateNewNodes(bool toCreate);
220   bool GetToCreateNewNodes() const;
221   /*!
222    * To use boundary recovery version which tries to create mesh on a very poor
223    * quality surface mesh
224    */
225   void SetToUseBoundaryRecoveryVersion(bool toUse);
226   bool GetToUseBoundaryRecoveryVersion() const;
227   /*!
228    * Applies finite-element correction by replacing overconstrained elements where
229    * it is possible. The process is cutting first the overconstrained edges and
230    * second the overconstrained facets. This insure that no edges have two boundary
231    * vertices and that no facets have three boundary vertices.
232    */
233   void SetFEMCorrection(bool toUseFem);
234   bool GetFEMCorrection() const;
235   /*!
236    * To removes initial central point.
237    */
238   void SetToRemoveCentralPoint(bool toRemove);
239   bool GetToRemoveCentralPoint() const;
240   /*!
241    * To set hiden/undocumented/advanced options
242    */
243   void SetAdvancedOption(const std::string& option);
244   std::string GetAdvancedOption() const;  
245   /*!
246   * To define the volumic gradation
247   */
248   void SetGradation(double gradation);
249   double GetGradation() const;
250   /*!
251   * Print log in standard output
252   */
253   void SetStandardOutputLog(bool logInStandardOutput);
254   bool GetStandardOutputLog() const;
255   /*!
256   * Remove log file on success
257   */
258   void SetRemoveLogOnSuccess(bool removeLogOnSuccess);
259   bool GetRemoveLogOnSuccess() const;
260     
261
262   typedef std::map< std::string, std::string > TOptionValues;
263   typedef std::set< std::string >              TOptionNames;
264
265   void SetOptionValue(const std::string& optionName,
266                       const std::string& optionValue);
267   std::string GetOptionValue(const std::string& optionName,
268                              bool*              isDefault=0) const;
269   bool HasOptionDefined( const std::string& optionName ) const;
270   void ClearOption(const std::string& optionName);
271   TOptionValues        GetOptionValues()       const;
272   const TOptionValues& GetCustomOptionValues() const { return _customOption2value; }
273
274   // bool isMGTetraHPCOptions( std::string & option ) const;
275   /*!
276    * \brief To set the advanced options in the execution command line
277    */
278   void SetAdvancedOptionsInCommandLine( std::string & cmd ) const;
279   
280   //static inline const char* NoValue() { return "_"; }  
281 //   struct TEnforcedEdge {
282 //     long ID;
283 //     long node1;
284 //     long node2;
285 //     std::string groupName;
286 //   };
287   /*!
288    * \brief Return command to run MG-Tetra mesher excluding file prefix (-f)
289    */
290   static std::string CommandToRun(const GHS3DPlugin_Hypothesis* hyp,
291                                   const bool                    hasShapeToMesh,
292                                   const bool                    forExucutable);
293   /*!
294    * \brief Return a unique file name
295    */
296   static std::string GetFileName(const GHS3DPlugin_Hypothesis* hyp);
297   /*!
298    * \brief Return a unique file name for MGTetraHPC will have a GHS3D prefix
299    */
300   static std::string GetFileNameHPC(const GHS3DPlugin_Hypothesis* hyp);
301   /*!
302    * \brief Return the name of executable
303    */
304   static std::string GetExeName( ImplementedAlgorithms algoId );
305
306   /*!
307    * To set an enforced vertex
308    */
309   bool SetEnforcedVertex(std::string aName, std::string anEntry, std::string aGroupName,
310                          double size, double x=0.0, double y=0.0, double z=0.0, bool isCompound = false);
311   TGHS3DEnforcedVertex* GetEnforcedVertex(double x, double y, double z);
312   TGHS3DEnforcedVertex* GetEnforcedVertex(const std::string anEntry);
313   bool RemoveEnforcedVertex(double x=0.0, double y=0.0, double z=0.0, const std::string anEntry="" );
314   const TGHS3DEnforcedVertexCoordsValues& _GetEnforcedVerticesCoordsSize() const {return _enfVertexCoordsSizeList; }
315   const TGHS3DEnforcedVertexEntryValues & _GetEnforcedVerticesEntrySize() const {return _enfVertexEntrySizeList; }
316   const TGHS3DEnforcedVertexList        & _GetEnforcedVertices() const { return _enfVertexList; }
317   const TCoordsGHS3DEnforcedVertexMap   & _GetEnforcedVerticesByCoords() const { return _coordsEnfVertexMap; }
318   const TGeomEntryGHS3DEnforcedVertexMap& _GetEnforcedVerticesByEntry() const { return _geomEntryEnfVertexMap; }
319   void ClearEnforcedVertices();
320
321   /*!
322    * To set enforced elements
323    */
324   bool SetEnforcedMesh(SMESH_Mesh& theMesh, SMESH::ElementType elementType, std::string name, std::string entry, std::string groupName = "");
325   bool SetEnforcedGroup(const SMESHDS_Mesh* theMeshDS, SMESH::smIdType_array_var theIDs, SMESH::ElementType elementType, std::string name, std::string entry, std::string groupName = "");
326   bool SetEnforcedElements(TIDSortedElemSet theElemSet, SMESH::ElementType elementType, std::string groupName = "");
327   const TGHS3DEnforcedMeshList _GetEnforcedMeshes() const { return _enfMeshList; }
328   const TEntryGHS3DEnforcedMeshListMap _GetEnforcedMeshesByEntry() const { return _entryEnfMeshMap; }
329   void ClearEnforcedMeshes();
330   const TIDSortedNodeGroupMap _GetEnforcedNodes() const { return _enfNodes; }
331   const TIDSortedElemGroupMap _GetEnforcedEdges() const { return _enfEdges; }
332   const TIDSortedElemGroupMap _GetEnforcedTriangles() const { return _enfTriangles; }
333   const TID2SizeMap _GetNodeIDToSizeMap() const {return _nodeIDToSizeMap; }
334   const TSetStrings _GetGroupsToRemove() const {return _groupsToRemove; }
335   void RestoreEnfElemsByMeshes(); // persistence
336   /*!
337    * \brief Return the enforced vertices
338    */
339   static TGHS3DEnforcedVertexList GetEnforcedVertices(const GHS3DPlugin_Hypothesis* hyp);
340   static TGHS3DEnforcedVertexCoordsValues GetEnforcedVerticesCoordsSize(const GHS3DPlugin_Hypothesis* hyp);
341   static TGHS3DEnforcedVertexEntryValues  GetEnforcedVerticesEntrySize(const GHS3DPlugin_Hypothesis* hyp);
342   static TCoordsGHS3DEnforcedVertexMap GetEnforcedVerticesByCoords(const GHS3DPlugin_Hypothesis* hyp);
343   static TGeomEntryGHS3DEnforcedVertexMap GetEnforcedVerticesByEntry(const GHS3DPlugin_Hypothesis* hyp);
344   
345   static TGHS3DEnforcedMeshList GetEnforcedMeshes(const GHS3DPlugin_Hypothesis* hyp);
346   static TEntryGHS3DEnforcedMeshListMap GetEnforcedMeshesByEntry(const GHS3DPlugin_Hypothesis* hyp);
347   static TIDSortedNodeGroupMap GetEnforcedNodes(const GHS3DPlugin_Hypothesis* hyp);
348   static TIDSortedElemGroupMap GetEnforcedEdges(const GHS3DPlugin_Hypothesis* hyp);
349   static TIDSortedElemGroupMap GetEnforcedTriangles(const GHS3DPlugin_Hypothesis* hyp);
350   static TID2SizeMap GetNodeIDToSizeMap(const GHS3DPlugin_Hypothesis* hyp);
351   static TSetStrings GetGroupsToRemove(const GHS3DPlugin_Hypothesis* hyp);
352   static bool GetToMakeGroupsOfDomains(const GHS3DPlugin_Hypothesis* hyp);
353   void ClearGroupsToRemove();
354   
355   static bool   DefaultMeshHoles();
356   static bool   DefaultToMakeGroupsOfDomains();
357   static float  DefaultMaximumMemory();
358   static float  DefaultInitialMemory();
359   static short  DefaultOptimizationLevel();
360   static std::string DefaultWorkingDirectory();
361   static bool   DefaultKeepFiles();
362   static short  DefaultVerboseLevel();
363   static bool   DefaultToCreateNewNodes();
364   static bool   DefaultToUseBoundaryRecoveryVersion();
365   static bool   DefaultToUseFEMCorrection();
366   static bool   DefaultToRemoveCentralPoint();
367   static bool   DefaultStandardOutputLog();
368   static bool   DefaultRemoveLogOnSuccess();
369   static inline double DefaultGradation() { return 1.05; }
370   static bool   DefaultUseVolumeProximity() { return false; }
371   static int    DefaultNbVolumeProximityLayers() { return 2; }
372   static short  DefaultAlgorithm() { return MGTetra; }
373   static short  DefaultNumOfThreads() { return 4; }
374   static bool   DefaultUseNumOfThreads() { return true; }
375   static short  DefaultMyPthreadMode() { return 2; }    //reproducible_given_max_of_threads
376   static short  DefaultMyPthreadModeHPC() { return 1; } // safe
377    
378   void SetMinMaxSizeDefault( double theMinSize, double theMaxSize )
379   { myMinSizeDefault = theMinSize; myMaxSizeDefault = theMaxSize; }
380   double GetMinSizeDefault() const { return myMinSizeDefault; }
381   double GetMaxSizeDefault() const { return myMaxSizeDefault; }
382
383   // Persistence
384   virtual std::ostream & SaveTo(std::ostream & save);
385   virtual std::istream & LoadFrom(std::istream & load);
386
387   /*!
388    * \brief Does nothing
389    */
390   virtual bool SetParametersByMesh(const SMESH_Mesh* theMesh, const TopoDS_Shape& theShape);
391
392   /*!
393    * \brief Sets myToMakeGroupsOfDomains depending on whether theMesh is on shape or not
394    */
395   virtual bool SetParametersByDefaults(const TDefaults& dflts, const SMESH_Mesh* theMesh=0);
396
397   static bool  ToBool(const std::string& str, bool* isOk=0);
398   static double ToDbl(const std::string& str, bool* isOk=0);
399   static int    ToInt(const std::string& str, bool* isOk=0);
400
401 protected:
402
403   bool        myToMeshHoles;
404   bool        myToMakeGroupsOfDomains;
405   float       myMaximumMemory;
406   float       myInitialMemory;
407   short       myOptimizationLevel;
408   bool        myKeepFiles;
409   std::string myWorkingDirectory;
410   short       myVerboseLevel;
411   bool        myToCreateNewNodes;
412   bool        myToUseBoundaryRecoveryVersion;
413   bool        myToUseFemCorrection;
414   bool        myToRemoveCentralPoint;
415   bool        myLogInStandardOutput;
416   bool        myRemoveLogOnSuccess;
417   double      myGradation;
418   bool        myUseVolumeProximity;
419   int         myNbVolumeProximityLayers;
420   short       myAlgorithm;  //member used to pivot between MG-Tetra and MG-Tetra
421   short       myNumOfThreads;
422   bool        myUseNumOfThreads;
423   short       myPthreadModeMG;
424   short       myPthreadModeMGHPC;
425   double      myMinSize, myMinSizeDefault;
426   double      myMaxSize, myMaxSizeDefault;
427   //std::string myTextOption;
428
429   
430   TOptionValues _option2value, _customOption2value;         // user defined values
431   TOptionValues _defaultOptionValues;                       // default values
432   TOptionNames  _doubleOptions, _charOptions, _boolOptions; // to find a type of option  
433
434   TGHS3DEnforcedVertexList         _enfVertexList;
435   TGHS3DEnforcedVertexCoordsValues _enfVertexCoordsSizeList;
436   TGHS3DEnforcedVertexEntryValues  _enfVertexEntrySizeList;
437   // map to get "manual" enf vertex (through the coordinates)
438   TCoordsGHS3DEnforcedVertexMap    _coordsEnfVertexMap;
439   // map to get "geom" enf vertex (through the geom entries)
440   TGeomEntryGHS3DEnforcedVertexMap _geomEntryEnfVertexMap;
441   
442   
443   TGHS3DEnforcedMeshList                   _enfMeshList;
444   // map to get enf meshes through the entries
445   TEntryGHS3DEnforcedMeshListMap           _entryEnfMeshMap;
446   TIDSortedNodeGroupMap                    _enfNodes;
447   TIDSortedElemGroupMap                    _enfEdges;
448   TIDSortedElemGroupMap                    _enfTriangles;
449   TID2SizeMap                              _nodeIDToSizeMap;
450   
451   TSetStrings _groupsToRemove;
452 };
453
454
455 #endif