Salome HOME
Be able to use library by setting MG_Hybrid_Parameters.SetToUseLibrary(True)
[plugins/hybridplugin.git] / src / HYBRIDPlugin / HYBRIDPlugin_Hypothesis.hxx
1 // Copyright (C) 2007-2024  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 // ---
21 // File   : HYBRIDPlugin_Hypothesis.hxx
22 // Author : Christian VAN WAMBEKE (CEA)
23 // ---
24 //
25 #ifndef HYBRIDPlugin_Hypothesis_HeaderFile
26 #define HYBRIDPlugin_Hypothesis_HeaderFile
27
28 #include "HYBRIDPlugin_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 HYBRIDPLUGIN_EXPORT HYBRIDPlugin_Hypothesis: public SMESH_Hypothesis
44 {
45 public:
46
47   HYBRIDPlugin_Hypothesis(int hypId, SMESH_Gen * gen);
48
49   typedef std::map<std::vector<double>,double> THYBRIDEnforcedVertexCoordsValues;
50   typedef std::map<std::string,double> THYBRIDEnforcedVertexEntryValues;
51   
52   struct THYBRIDEnforcedVertex {
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 CompareHYBRIDEnforcedVertex {
62     bool operator () (const THYBRIDEnforcedVertex* e1, const THYBRIDEnforcedVertex* 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< THYBRIDEnforcedVertex*, CompareHYBRIDEnforcedVertex > THYBRIDEnforcedVertexList;
73   // Map Coords / Enforced node
74   typedef std::map< std::vector<double>, THYBRIDEnforcedVertex* > TCoordsHYBRIDEnforcedVertexMap;
75   // Map geom entry / Enforced node
76   typedef std::map< std::string, THYBRIDEnforcedVertex* > TGeomEntryHYBRIDEnforcedVertexMap;
77   // Map groupName / Enforced node
78   typedef std::map< std::string, THYBRIDEnforcedVertexList > TGroupNameHYBRIDEnforcedVertexMap;
79   
80   ////////////////////
81   // Enforced meshes
82   ////////////////////
83   
84   struct THYBRIDEnforcedMesh {
85     int         persistID;
86     std::string name;
87     std::string entry;
88     std::string groupName;
89     SMESH::ElementType elementType;
90   };
91   
92   struct CompareHYBRIDEnforcedMesh {
93     bool operator () (const THYBRIDEnforcedMesh* e1, const THYBRIDEnforcedMesh* 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< THYBRIDEnforcedMesh*, CompareHYBRIDEnforcedMesh > THYBRIDEnforcedMeshList;
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, THYBRIDEnforcedMeshList > TEntryHYBRIDEnforcedMeshListMap;
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 "HYBRID_Parameters"; }
122
123
124   /*!
125    * Sizes of boundary layers are relative to the surface size. Default no
126    */
127   void SetHeightIsRelative( bool isRelative );
128   bool GetHeightIsRelative() const { return myHeightIsRelative; }
129   /*!
130    * Maximum internal angles of boundary elements (in degree)
131    */
132   void SetBoundaryLayersMaxElemAngle( double angle );
133   double GetBoundaryLayersMaxElemAngle() const { return myBoundaryLayersMaxElemAngle; }
134   /*!
135    * To call MG-Hybrid by library. Default is no (i.e. by executable)
136    */
137   void SetToUseLibrary(bool toUseLib);
138   bool GetToUseLibrary() const;
139   /*!
140    * To mesh layers on all wrap. Default is yes.
141    */
142   void SetLayersOnAllWrap(bool toMesh);
143   bool GetLayersOnAllWrap(bool checkFreeOption = false) const;
144   /*!
145    * IDs of faces to grow the layers on
146    */
147   bool SetFacesWithLayers(const std::vector<int>& theVal);
148   const std::vector<int>& GetFacesWithLayers() const;
149   /*!
150    * IDs of faces to imprint the layers on
151    */
152   bool SetFacesWithImprinting(const std::vector<int>& theVal);
153   const std::vector<int>& GetFacesWithImprinting() const;
154   /*!
155    * IDs of faces with snapping (faces that already have surface layers)
156    */
157   bool SetFacesWithSnapping(const std::vector<int>& theVal);
158   const std::vector<int>& GetFacesWithSnapping() const;
159   /*!
160    * To make groups of volumes of different domains when mesh is generated from skin.
161    * Default is to make groups.
162    * This option works only (1) for the mesh w/o shape and (2) if GetToMeshHoles() == true
163    */
164   void SetToMakeGroupsOfDomains(bool toMakeGroups);
165   bool GetToMakeGroupsOfDomains() const;
166   /*!
167    * Maximal size of memory to be used by the algorithm (in Megabytes)
168    */
169   void SetMaximumMemory(double MB);
170   double GetMaximumMemory() const;
171   /*!
172    * Initial size of memory to be used by the algorithm (in Megabytes) in
173    * automatic memory adjustment mode. Default is zero
174    */
175   void SetInitialMemory(double MB);
176   double GetInitialMemory() const;
177   /*!
178    * Optimization level: 0-none, 1-light, 2-medium, 3-standard+, 4-strong. Default is medium
179    */
180   enum OptimizationLevel { None = 0, Light, Medium, StandardPlus, Strong };
181   void SetOptimizationLevel(OptimizationLevel level);
182   OptimizationLevel GetOptimizationLevel() const;
183
184   /*!
185    * Collision Mode: 0-decrease, 1-stop. Default is decrease
186    */
187   enum CollisionMode { Decrease = 0, Stop };
188   void SetCollisionMode(CollisionMode mode);
189   CollisionMode GetCollisionMode() const;
190   /*!
191    * BoundaryLayersGrowth: 0-Layer_Growth_Inward, 1-Layer_Growth_Outward. Default is Layer_Growth_Inward
192    */
193   enum BoundaryLayersGrowth { Layer_Growth_Inward = 0, Layer_Growth_Outward };
194   void SetBoundaryLayersGrowth(BoundaryLayersGrowth level);
195   BoundaryLayersGrowth GetBoundaryLayersGrowth() const;
196   /*!
197    * ElementGeneration: 0-Generation_Tetra_Dominant, 1-Generation_Hexa_Dominant, 2-Generation_Cartesian_Core. Default is Generation_Tetra_Dominant
198    */
199   enum ElementGeneration { Generation_Tetra_Dominant = 0, Generation_Hexa_Dominant, Generation_Cartesian_Core, Generation_Extrusion_Only };
200   void SetElementGeneration(ElementGeneration level);
201   ElementGeneration GetElementGeneration() const;
202     /*!
203      * To mesh adding extra normals at opening ridges and corners.
204      * Default is no.
205      */
206     void SetAddMultinormals(bool toAddMultinormals);
207     bool GetAddMultinormals() const;
208     /*!
209      * To mesh smoothing normals at closed ridges and corners.
210      * Default is no.
211      */
212     void SetSmoothNormals(bool toSmoothNormals);
213     bool GetSmoothNormals() const;
214     /*!
215      * To set height of the first layer.
216      */
217     void SetHeightFirstLayer(double HFL);
218     double GetHeightFirstLayer() const;
219     /*!
220      * To set boundary layers coefficient of geometric progression.
221      * Default is 1.0
222      */
223     void SetBoundaryLayersProgression(double BLP);
224     double GetBoundaryLayersProgression() const;
225     /*!
226      * Set core elements size.
227      * Default is 0.0
228      */
229     void SetCoreSize(double CS);
230     double GetCoreSize() const;
231     /*!
232      * To set multinormals angle threshold at opening ridges.
233      * Default is 30.0
234      */
235     void SetMultinormalsAngle(double MNA);
236     double GetMultinormalsAngle() const;
237     /*!
238      * To set number of boundary layers.
239      * Default is 1
240      */
241     void SetNbOfBoundaryLayers(short NBL);
242     short GetNbOfBoundaryLayers() const;
243
244
245   /*!
246    * Path to working directory
247    */
248   void SetWorkingDirectory(const std::string& path);
249   std::string GetWorkingDirectory() const;
250   /*!
251    * To keep working files or remove them. Log file remains in case of errors anyway.
252    */
253   void SetKeepFiles(bool toKeep);
254   bool GetKeepFiles() const;
255   /*!
256    * Verbose level [0-10]
257    *  0 - no standard output,
258    *  2 - prints the data, quality statistics of the skin and final meshes and
259    *     indicates when the final mesh is being saved. In addition the software
260    *     gives indication regarding the CPU time.
261    * 10 - same as 2 plus the main steps in the computation, quality statistics
262    *     histogram of the skin mesh, quality statistics histogram together with
263    *     the characteristics of the final mesh.
264    */
265   void SetVerboseLevel(short level);
266   short GetVerboseLevel() const;
267   /*!
268    * To create new nodes
269    */
270   void SetToCreateNewNodes(bool toCreate);
271   bool GetToCreateNewNodes() const;
272   /*!
273    * To use boundary recovery version which tries to create mesh on a very poor
274    * quality surface mesh
275    */
276   void SetToUseBoundaryRecoveryVersion(bool toUse);
277   bool GetToUseBoundaryRecoveryVersion() const;
278   /*!
279    * Applies finite-element correction by replacing overconstrained elements where
280    * it is possible. The process is cutting first the overconstrained edges and
281    * second the overconstrained facets. This insure that no edges have two boundary
282    * vertices and that no facets have three boundary vertices.
283    */
284   void SetFEMCorrection(bool toUseFem);
285   bool GetFEMCorrection() const;
286   /*!
287    * To removes initial central point.
288    */
289   void SetToRemoveCentralPoint(bool toRemove);
290   bool GetToRemoveCentralPoint() const;
291   /*!
292    * To set hiden/undocumented/advanced options
293    */
294   void SetAdvancedOption(const std::string& option);
295   std::string GetAdvancedOption() const;
296   /*!
297    * To define the volumic gradation
298    */
299   void SetGradation(double gradation);
300   double GetGradation() const ;
301   /*!
302    * Print log in standard output
303    */
304   void SetStandardOutputLog(bool logInStandardOutput);
305   bool GetStandardOutputLog() const ;
306   /*!
307    * Remove log file on success
308    */
309   void SetRemoveLogOnSuccess(bool removeLogOnSuccess);
310   bool GetRemoveLogOnSuccess() const ;
311
312
313   typedef std::map< std::string, std::string > TOptionValues;
314   typedef std::set< std::string >              TOptionNames;
315
316   void SetOptionValue(const std::string& optionName,
317                       const std::string& optionValue);
318   std::string GetOptionValue(const std::string& optionName,
319                              bool*              isDefault=0) const;
320   bool HasOptionDefined( const std::string& optionName ) const;
321   void ClearOption(const std::string& optionName);
322   TOptionValues        GetOptionValues()       const;
323   const TOptionValues& GetCustomOptionValues() const { return _customOption2value; }
324
325   static bool  ToBool(const std::string& str, bool* isOk=0);
326   static double ToDbl(const std::string& str, bool* isOk=0);
327   static int    ToInt(const std::string& str, bool* isOk=0);
328
329
330   /*!
331    * \brief Return command to run hybrid mesher excluding file prefix (-f)
332    */
333   static std::string CommandToRun(const HYBRIDPlugin_Hypothesis* hyp);
334   /*!
335    * \brief Return a unique file name
336    */
337   static std::string GetFileName(const HYBRIDPlugin_Hypothesis* hyp);
338   /*!
339    * \brief Return the name of executable
340    */
341   static std::string GetExeName();
342   /*!
343    * \brief Return a tag of enforced triangles
344    */
345   static int EnforcedTag() { return -5; }
346
347   /*!
348    * To set an enforced vertex
349    */
350   bool SetEnforcedVertex(std::string aName, std::string anEntry, std::string aGroupName,
351                          double size, double x=0.0, double y=0.0, double z=0.0, bool isCompound = false);
352   THYBRIDEnforcedVertex* GetEnforcedVertex(double x, double y, double z);
353   THYBRIDEnforcedVertex* GetEnforcedVertex(const std::string anEntry);
354   bool RemoveEnforcedVertex(double x=0.0, double y=0.0, double z=0.0, const std::string anEntry="" );
355   const THYBRIDEnforcedVertexCoordsValues _GetEnforcedVerticesCoordsSize() const {return _enfVertexCoordsSizeList; }
356   const THYBRIDEnforcedVertexEntryValues  _GetEnforcedVerticesEntrySize() const {return _enfVertexEntrySizeList; }
357   const THYBRIDEnforcedVertexList         _GetEnforcedVertices() const { return _enfVertexList; }
358   const TCoordsHYBRIDEnforcedVertexMap    _GetEnforcedVerticesByCoords() const { return _coordsEnfVertexMap; }
359   const TGeomEntryHYBRIDEnforcedVertexMap _GetEnforcedVerticesByEntry() const { return _geomEntryEnfVertexMap; }
360   void ClearEnforcedVertices();
361
362   /*!
363    * To set enforced elements
364    */
365   bool SetEnforcedMesh(SMESH_Mesh& theMesh, SMESH::ElementType elementType, std::string name, std::string entry, std::string groupName = "");
366   bool SetEnforcedGroup(const SMESHDS_Mesh* theMeshDS, SMESH::smIdType_array_var theIDs, SMESH::ElementType elementType, std::string name, std::string entry, std::string groupName = "");
367   bool SetEnforcedElements(TIDSortedElemSet theElemSet, SMESH::ElementType elementType, std::string groupName = "");
368   const THYBRIDEnforcedMeshList _GetEnforcedMeshes() const { return _enfMeshList; }
369   const TEntryHYBRIDEnforcedMeshListMap _GetEnforcedMeshesByEntry() const { return _entryEnfMeshMap; }
370   void ClearEnforcedMeshes();
371   const TIDSortedNodeGroupMap _GetEnforcedNodes() const { return _enfNodes; }
372   const TIDSortedElemGroupMap _GetEnforcedEdges() const { return _enfEdges; }
373   const TIDSortedElemGroupMap _GetEnforcedTriangles() const { return _enfTriangles; }
374   const TID2SizeMap _GetNodeIDToSizeMap() const {return _nodeIDToSizeMap; }
375   const TSetStrings _GetGroupsToRemove() const {return _groupsToRemove; }
376   void RestoreEnfElemsByMeshes(); // persistence
377   /*!
378    * \brief Return the enforced vertices
379    */
380   static THYBRIDEnforcedVertexList GetEnforcedVertices(const HYBRIDPlugin_Hypothesis* hyp);
381   static THYBRIDEnforcedVertexCoordsValues GetEnforcedVerticesCoordsSize(const HYBRIDPlugin_Hypothesis* hyp);
382   static THYBRIDEnforcedVertexEntryValues  GetEnforcedVerticesEntrySize(const HYBRIDPlugin_Hypothesis* hyp);
383   static TCoordsHYBRIDEnforcedVertexMap GetEnforcedVerticesByCoords(const HYBRIDPlugin_Hypothesis* hyp);
384   static TGeomEntryHYBRIDEnforcedVertexMap GetEnforcedVerticesByEntry(const HYBRIDPlugin_Hypothesis* hyp);
385   
386   static THYBRIDEnforcedMeshList GetEnforcedMeshes(const HYBRIDPlugin_Hypothesis* hyp);
387   static TEntryHYBRIDEnforcedMeshListMap GetEnforcedMeshesByEntry(const HYBRIDPlugin_Hypothesis* hyp);
388   static TIDSortedNodeGroupMap GetEnforcedNodes(const HYBRIDPlugin_Hypothesis* hyp);
389   static TIDSortedElemGroupMap GetEnforcedEdges(const HYBRIDPlugin_Hypothesis* hyp);
390   static TIDSortedElemGroupMap GetEnforcedTriangles(const HYBRIDPlugin_Hypothesis* hyp);
391   static TID2SizeMap GetNodeIDToSizeMap(const HYBRIDPlugin_Hypothesis* hyp);
392   static TSetStrings GetGroupsToRemove(const HYBRIDPlugin_Hypothesis* hyp);
393   static bool GetToMakeGroupsOfDomains(const HYBRIDPlugin_Hypothesis* hyp);
394   static bool GetToUseLibrary(const HYBRIDPlugin_Hypothesis* hyp);
395   void ClearGroupsToRemove();
396   
397   static bool   DefaultHeightIsRelative() { return false; }
398   static double DefaultBoundaryLayersMaxElemAngle() { return 165.0; }
399   static bool   DefaultMeshHoles();
400   static bool   DefaultToUseLib();
401   static bool   DefaultLayersOnAllWrap();
402   static bool   DefaultToMakeGroupsOfDomains();
403   static double DefaultMaximumMemory();
404   static double DefaultInitialMemory();
405   static short  DefaultOptimizationLevel();
406   static short  DefaultCollisionMode();
407   static short  DefaultBoundaryLayersGrowth();
408   static short  DefaultElementGeneration();
409   static std::string DefaultWorkingDirectory();
410   static bool   DefaultKeepFiles();
411   static short  DefaultVerboseLevel();
412   static bool   DefaultToCreateNewNodes();
413   static bool   DefaultToUseBoundaryRecoveryVersion();
414   static bool   DefaultToUseFEMCorrection();
415   static bool   DefaultToRemoveCentralPoint();
416   static bool   DefaultStandardOutputLog();
417   static bool   DefaultRemoveLogOnSuccess();
418   static double DefaultGradation();
419   static bool   DefaultAddMultinormals();
420   static bool   DefaultSmoothNormals();
421   static short  DefaultNbOfBoundaryLayers();
422   static double DefaultHeightFirstLayer();
423   static double DefaultBoundaryLayersProgression();
424   static double DefaultCoreSize();
425   static double DefaultMultinormalsAngle();
426
427   /*!
428    * To mesh "holes" in a solid or not. Default is to mesh.
429    */
430   void SetToMeshHoles(bool toMesh);
431   bool GetToMeshHoles(bool checkFreeOption = false) const;
432
433   // Persistence
434   virtual std::ostream & SaveTo(std::ostream & save);
435   virtual std::istream & LoadFrom(std::istream & load);
436
437   /*!
438    * \brief Does nothing
439    */
440   virtual bool SetParametersByMesh(const SMESH_Mesh* theMesh, const TopoDS_Shape& theShape);
441
442   /*!
443    * \brief Sets myToMakeGroupsOfDomains depending on whether theMesh is on shape or not
444    */
445   virtual bool SetParametersByDefaults(const TDefaults& dflts, const SMESH_Mesh* theMesh=0);
446
447 private:
448
449   short            myNbOfBoundaryLayers;
450   double           myHeightFirstLayer;
451   bool             myHeightIsRelative;
452   short            myBoundaryLayersGrowth;
453   double           myBoundaryLayersMaxElemAngle;
454   double           myBoundaryLayersProgression;
455
456   short            myElementGeneration;
457   double           myCoreSize;
458
459   bool             myLayersOnAllWrap;
460   std::vector<int> myFacesWithImprinting;
461   std::vector<int> myFacesWithSnapping;
462   std::vector<int> myFacesWithLayers;
463
464   // advanced (main params before redesign for 9.6.2)
465   short            myCollisionMode;
466   bool             myAddMultinormals;
467   bool             mySmoothNormals;
468   double           myMultinormalsAngle;
469   double           myGradation;
470
471   std::string      myWorkingDirectory;
472   short            myVerboseLevel;
473   bool             myLogInStandardOutput;
474   bool             myRemoveLogOnSuccess;
475   bool             myKeepFiles;
476
477   TOptionValues _option2value, _customOption2value;         // user defined values
478   TOptionValues _defaultOptionValues;                       // default values
479   TOptionNames  _doubleOptions, _charOptions, _boolOptions; // to find a type of option
480
481   short            myOptimizationLevel;            // missing from plugin
482   bool             myToMakeGroupsOfDomains;        // missing from plugin
483
484   bool             myToMeshHoles;                  // missing from hybrid
485   double           myMaximumMemory;                // missing from hybrid
486   double           myInitialMemory;                // missing from hybrid
487   bool             myToCreateNewNodes;             // missing from hybrid
488   bool             myToUseBoundaryRecoveryVersion; // missing from hybrid
489   bool             myToUseFemCorrection;           // missing from hybrid
490   bool             myToRemoveCentralPoint;         // missing from hybrid
491   bool             myUseLib;
492
493
494   THYBRIDEnforcedVertexList _enfVertexList;
495   THYBRIDEnforcedVertexCoordsValues _enfVertexCoordsSizeList;
496   THYBRIDEnforcedVertexEntryValues _enfVertexEntrySizeList;
497   // map to get "manual" enf vertex (through the coordinates)
498   TCoordsHYBRIDEnforcedVertexMap _coordsEnfVertexMap;
499   // map to get "geom" enf vertex (through the geom entries)
500   TGeomEntryHYBRIDEnforcedVertexMap _geomEntryEnfVertexMap;
501   
502   
503   THYBRIDEnforcedMeshList _enfMeshList;
504   // map to get enf meshes through the entries
505   TEntryHYBRIDEnforcedMeshListMap _entryEnfMeshMap;
506   TIDSortedNodeGroupMap _enfNodes;
507   TIDSortedElemGroupMap _enfEdges;
508   TIDSortedElemGroupMap _enfTriangles;
509   TID2SizeMap _nodeIDToSizeMap;
510   std::map<std::string, TIDSortedElemSet > _entryToElemsMap;
511   
512   TSetStrings _groupsToRemove;
513 };
514
515
516 #endif