Salome HOME
a46460065a218284a14c08a164aa6a4906db0e7b
[plugins/ghs3dplugin.git] / src / GHS3DPlugin / GHS3DPluginBuilder.py
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 ##
21 # @package GHS3DPluginBuilder
22 # Python API for the MG-Tetra meshing plug-in module.
23
24 import omniORB
25 from salome.smesh.smesh_algorithm import Mesh_Algorithm
26 from salome.smesh.smeshBuilder import AssureGeomPublished
27
28 # import GHS3DPlugin module if possible
29 noGHS3DPlugin = 0
30 try:
31     import GHS3DPlugin
32 except ImportError:
33     noGHS3DPlugin = 1
34     pass
35
36 # Optimization level of MG-Tetra
37 # V3.1
38 None_Optimization, Light_Optimization, Medium_Optimization, Strong_Optimization = 0,1,2,3
39 # V4.1 (partialy redefines V3.1). Issue 0020574
40 None_Optimization, Light_Optimization, Standard_Optimization, StandardPlus_Optimization, Strong_Optimization = 0,1,2,3,4
41
42 # import items of enums
43 for e in GHS3DPlugin.Mode._items: exec('%s = GHS3DPlugin.%s'%(e,e))
44 for e in GHS3DPlugin.PThreadsMode._items: exec('%s = GHS3DPlugin.%s'%(e,e))
45 Mode_NO, Mode_YES, Mode_ONLY = GHS3DPlugin.Mode._items
46 Mode_SAFE, Mode_AGGRESSIVE, Mode_NONE = GHS3DPlugin.PThreadsMode._items
47
48 #----------------------------
49 # Mesh algo type identifiers
50 #----------------------------
51
52 ## Algorithm type: MG-Tetra tetrahedron 3D algorithm, see GHS3D_Algorithm
53 MG_Tetra = "MG-Tetra"
54 GHS3D = MG_Tetra
55 MG_Tetra_Optimization = "MG-Tetra Optimization"
56
57 ## Tetrahedron MG-Tetra 3D algorithm
58 #  
59 #  It can be created by calling smeshBuilder.Mesh.Tetrahedron( smeshBuilder.MG_Tetra, geom=0 )
60 class GHS3D_Algorithm(Mesh_Algorithm):
61
62     ## name of the dynamic method in smeshBuilder.Mesh class
63     #  @internal
64     meshMethod = "Tetrahedron"
65     ## type of algorithm used with helper function in smeshBuilder.Mesh class
66     #  @internal
67     algoType   = MG_Tetra
68     ## doc string of the method in smeshBuilder.Mesh class
69     #  @internal
70     docHelper  = "Creates tetrahedron 3D algorithm"
71
72     ## Private constructor.
73     #  @param mesh parent mesh object algorithm is assigned to
74     #  @param geom geometry (shape/sub-shape) algorithm is assigned to;
75     #              if it is @c 0 (default), the algorithm is assigned to the main shape
76     def __init__(self, mesh, geom=0):
77         Mesh_Algorithm.__init__(self)
78         if noGHS3DPlugin: print("Warning: GHS3DPlugin module unavailable")
79         self.Create(mesh, geom, self.algoType, "libGHS3DEngine.so")
80         self.params = None
81         pass
82
83     ## Defines hypothesis having several parameters
84     #  @return hypothesis object
85     def Parameters(self):
86         if not self.params:
87             self.params = self.Hypothesis("MG-Tetra Parameters", [],
88                                           "libGHS3DEngine.so", UseExisting=0)
89             pass
90         return self.params
91
92     ## Set lower boundary of mesh element size
93     #  Set it to zero to deactivate this option
94     def SetMinSize(self, theMinSize):
95         self.Parameters().SetMinSize(theMinSize)
96         return
97
98     ## Set upper boundary of mesh element size
99     #  Set it to zero to deactivate this option
100     def SetMaxSize(self, theMaxSize):
101         self.Parameters().SetMaxSize(theMaxSize)
102         return
103
104     ## Activate/deactivate volume proximity computation
105     #
106     def SetVolumeProximity(self, toUse ):
107         self.Parameters().SetVolumeProximity(toUse)
108         return
109
110     ## Set number of surface element layers to be generated due to volume proximity
111     #
112     def SetNbVolumeProximityLayers(self, nbLayers ):
113         self.Parameters().SetNbVolumeProximityLayers(nbLayers)
114         return
115
116     ## To mesh "holes" in a solid or not. Default is to mesh.
117     #  @param toMesh "mesh holes" flag value
118     def SetToMeshHoles(self, toMesh):
119         self.Parameters().SetToMeshHoles(toMesh)
120         pass
121
122     ## To make groups of volumes of different domains when mesh is generated from skin.
123     #  Default is to make groups.
124     # This option works only (1) for the mesh w/o shape and (2) if GetToMeshHoles() == true
125     #  @param toMakeGroups "Make groups of domains" flag value
126     def SetToMakeGroupsOfDomains(self, toMakeGroups):
127         self.Parameters().SetToMakeGroupsOfDomains(toMakeGroups)
128         pass
129
130     ## Set Optimization level:
131     #  @param level optimization level, one of the following values
132     #  - None_Optimization
133     #  - Light_Optimization
134     #  - Standard_Optimization
135     #  - StandardPlus_Optimization
136     #  - Strong_Optimization.
137     #  .
138     #  Default is Standard_Optimization
139     def SetOptimizationLevel(self, level):
140         self.Parameters().SetOptimizationLevel(level)
141         pass
142
143     ## Set maximal size of memory to be used by the algorithm (in Megabytes).
144     #  @param MB maximal size of memory
145     def SetMaximumMemory(self, MB):
146         self.Parameters().SetMaximumMemory(MB)
147         pass
148
149     ## Set initial size of memory to be used by the algorithm (in Megabytes) in
150     #  automatic memory adjustment mode.
151     #  @param MB initial size of memory
152     def SetInitialMemory(self, MB):
153         self.Parameters().SetInitialMemory(MB)
154         pass
155
156     ## Set path to working directory.
157     #  @param path working directory
158     def SetWorkingDirectory(self, path):
159         self.Parameters().SetWorkingDirectory(path)
160         pass
161
162     ## To keep working files or remove them.
163     #  @param toKeep "keep working files" flag value
164     def SetKeepFiles(self, toKeep):
165         self.Parameters().SetKeepFiles(toKeep)
166         pass
167     
168     ## Remove or not the log file (if any) in case of successful computation.
169     #  The log file remains in case of errors anyway. If 
170     #  the "keep working files" flag is set to true, this option
171     #  has no effect.
172     #  @param toRemove "remove log on success" flag value
173     def SetRemoveLogOnSuccess(self, toRemove):
174         self.Parameters().SetRemoveLogOnSuccess(toRemove)
175         pass
176     
177     ## Print the the log in a file. If set to false, the
178     # log is printed on the standard output
179     #  @param toPrintLogInFile "print log in a file" flag value
180     def SetPrintLogInFile(self, toPrintLogInFile):
181         self.Parameters().SetStandardOutputLog(not toPrintLogInFile)
182         pass
183
184     ## Set verbosity level [0-10].
185     #  @param level verbosity level
186     #  - 0 - no standard output,
187     #  - 2 - prints the data, quality statistics of the skin and final meshes and
188     #    indicates when the final mesh is being saved. In addition the software
189     #    gives indication regarding the CPU time.
190     #  - 10 - same as 2 plus the main steps in the computation, quality statistics
191     #    histogram of the skin mesh, quality statistics histogram together with
192     #    the characteristics of the final mesh.
193     def SetVerboseLevel(self, level):
194         self.Parameters().SetVerboseLevel(level)
195         pass
196
197     ## To create new nodes.
198     #  @param toCreate "create new nodes" flag value
199     def SetToCreateNewNodes(self, toCreate):
200         self.Parameters().SetToCreateNewNodes(toCreate)
201         pass
202
203     ## To use boundary recovery version which tries to create mesh on a very poor
204     #  quality surface mesh.
205     #  @param toUse "use boundary recovery version" flag value
206     def SetToUseBoundaryRecoveryVersion(self, toUse):
207         self.Parameters().SetToUseBoundaryRecoveryVersion(toUse)
208         pass
209
210     ## Applies finite-element correction by replacing overconstrained elements where
211     #  it is possible. The process is cutting first the overconstrained edges and
212     #  second the overconstrained facets. This insure that no edges have two boundary
213     #  vertices and that no facets have three boundary vertices.
214     #  @param toUseFem "apply finite-element correction" flag value
215     def SetFEMCorrection(self, toUseFem):
216         self.Parameters().SetFEMCorrection(toUseFem)
217         pass
218
219     ## To remove initial central point.
220     #  @param toRemove "remove initial central point" flag value
221     def SetToRemoveCentralPoint(self, toRemove):
222         self.Parameters().SetToRemoveCentralPoint(toRemove)
223         pass
224
225     ## To set the algorithm id
226     #  @param algorithm ID 0 MGTetra HPC - 1 MGTetra
227     def SetAlgorithm(self,algoId):
228         self.Parameters().SetAlgorithm(algoId)    
229         pass
230     
231     ## To use parallel mode
232     #  @param toUseNumOfThreads "use max number of threads" flag value
233     def SetUseNumOfThreads(self,useThreads):
234         self.Parameters().SetUseNumOfThreads(useThreads)    
235         pass
236
237     ## To set the number of threads to be used
238     #  @param numberOfThreads define max_num_threads for MGTetra and MGTetra HPC
239     def SetNumOfThreads(self,numThreads):
240         self.Parameters().SetNumOfThreads(numThreads)    
241         pass
242     
243     ## Set SetPthreadMode.
244     #  @param pthreadMode for parallel algorithm in MGTetra
245     #  - 0 - none
246     #  - 1 - aggresive
247     #  - 2 - safe
248     def SetPthreadMode(self,pthreadMode):
249         self.Parameters().SetPthreadMode(pthreadMode)    
250         pass
251     
252     ## Set SetParallelMode.
253     #  @param parallelMode for parallel algorithm in MGTetra HPC
254     #  - 0 - none
255     #  - 1 - reproducible_given_max_number_of_threads
256     #  - 2 - reproducible
257     #  - 3 - aggressive 
258     def SetPthreadMode(self,parallelMode):
259         self.Parameters().SetParallelMode(parallelMode)    
260         pass
261
262     ## To set an enforced vertex.
263     #  @param x            : x coordinate
264     #  @param y            : y coordinate
265     #  @param z            : z coordinate
266     #  @param size         : size of 1D element around enforced vertex
267     #  @param vertexName   : name of the enforced vertex
268     #  @param groupName    : name of the group
269     def SetEnforcedVertex(self, x, y, z, size, vertexName = "", groupName = ""):
270         if vertexName == "":
271             if groupName == "":
272                 return self.Parameters().SetEnforcedVertex(x, y, z, size)
273             else:
274                 return self.Parameters().SetEnforcedVertexWithGroup(x, y, z, size, groupName)
275             pass
276         else:
277             if groupName == "":
278                 return self.Parameters().SetEnforcedVertexNamed(x, y, z, size, vertexName)
279             else:
280                 return self.Parameters().SetEnforcedVertexNamedWithGroup(x, y, z, size, vertexName, groupName)
281             pass
282         pass
283
284     ## To set an enforced vertex given a GEOM vertex, group or compound.
285     #  @param theVertex    : GEOM vertex (or group, compound) to be projected on theFace.
286     #  @param size         : size of 1D element around enforced vertex
287     #  @param groupName    : name of the group
288     def SetEnforcedVertexGeom(self, theVertex, size, groupName = ""):
289         AssureGeomPublished( self.mesh, theVertex )
290         if groupName == "":
291             return self.Parameters().SetEnforcedVertexGeom(theVertex, size)
292         else:
293             return self.Parameters().SetEnforcedVertexGeomWithGroup(theVertex, size, groupName)
294         pass
295
296     ## To remove an enforced vertex.
297     #  @param x            : x coordinate
298     #  @param y            : y coordinate
299     #  @param z            : z coordinate
300     def RemoveEnforcedVertex(self, x, y, z):
301         return self.Parameters().RemoveEnforcedVertex(x, y, z)
302
303     ## To remove an enforced vertex given a GEOM vertex, group or compound.
304     #  @param theVertex    : GEOM vertex (or group, compound) to be projected on theFace.
305     def RemoveEnforcedVertexGeom(self, theVertex):
306         AssureGeomPublished( self.mesh, theVertex )
307         return self.Parameters().RemoveEnforcedVertexGeom(theVertex)
308
309     ## To set an enforced mesh with given size and add the enforced elements in the group "groupName".
310     #  @param theSource    : source mesh which provides constraint elements/nodes
311     #  @param elementType  : SMESH.ElementType (NODE, EDGE or FACE)
312     #  @param size         : size of elements around enforced elements. Unused if -1.
313     #  @param groupName    : group in which enforced elements will be added. Unused if "".
314     def SetEnforcedMesh(self, theSource, elementType, size = -1, groupName = ""):
315         if size < 0:
316             if groupName == "":
317                 return self.Parameters().SetEnforcedMesh(theSource, elementType)
318             else:
319                 return self.Parameters().SetEnforcedMeshWithGroup(theSource, elementType, groupName)
320             pass
321         else:
322             if groupName == "":
323                 return self.Parameters().SetEnforcedMeshSize(theSource, elementType, size)
324             else:
325                 return self.Parameters().SetEnforcedMeshSizeWithGroup(theSource, elementType, size, groupName)
326             pass
327         pass
328
329     ## Set advanced option value
330     #  @param optionName option name
331     #  @param optionValue option value
332     def SetOptionValue(self, optionName, optionValue):
333         self.Parameters().SetOptionValue( optionName, optionValue )
334         pass
335     
336     ## Sets command line option as text.
337     #  @param optionAndValue command line option in a form "option value"
338     def SetAdvancedOption(self, optionAndValue):
339         self.Parameters().SetAdvancedOption(optionAndValue)
340         pass
341     
342     ## OBSOLETE Sets command line option as text.
343     #  @param option command line option
344     def SetTextOption(self, option):
345         self.Parameters().SetAdvancedOption(option)
346         pass
347     
348     pass # end of GHS3D_Algorithm class
349
350
351 ## MG-Tetra Optimization algorithm - optimizer of tetrahedral meshes
352 #
353 #  It can be created by calling smeshBuilder.Mesh.Tetrahedron( smeshBuilder.MG_Tetra_Optimization )
354 class GHS3D_Optimizer(GHS3D_Algorithm):
355
356     ## name of the dynamic method in smeshBuilder.Mesh class
357     #  @internal
358     meshMethod = "Tetrahedron"
359     ## type of algorithm used with helper function in smeshBuilder.Mesh class
360     #  @internal
361     algoType   = MG_Tetra_Optimization
362     ## doc string of the method in smeshBuilder.Mesh class
363     #  @internal
364     docHelper  = "Creates MG-Tetra optimizer of tetrahedral meshes"
365
366     ## Private constructor.
367     #  @param mesh parent mesh object algorithm is assigned to
368     #  @param geom - not used
369     def __init__(self, mesh, geom=0):
370         GHS3D_Algorithm.__init__(self, mesh)
371
372         # remove some inherited methods
373         # del self.SetToMeshHoles
374         # del self.SetToMakeGroupsOfDomains
375         # del self.SetToUseBoundaryRecoveryVersion
376         # del self.SetFEMCorrection
377         # del self.SetToRemoveCentralPoint
378         # del self.SetEnforcedVertex
379         # del self.SetEnforcedVertexGeom
380         # del self.RemoveEnforcedVertex
381         # del self.RemoveEnforcedVertexGeom
382         # del self.SetEnforcedMesh
383         # del self.SetTextOption
384         pass
385
386     ## Defines hypothesis having several parameters
387     #  @return hypothesis object
388     def Parameters(self):
389         if not self.params:
390             self.params = self.Hypothesis("MG-Tetra Optimization Parameters", [],
391                                           "libGHS3DEngine.so", UseExisting=0)
392             pass
393         return self.params
394
395     ## Set Optimization mode
396     #  @param optMode optimization mode, one of the following values:
397     #  smeshBuilder.Mode_NO,
398     #  smeshBuilder.Mode_YES (default),
399     #  smeshBuilder.MODE_ONLY
400     def SetOptimizationOnly(self, optMode ):
401         self.Parameters().SetOptimizationOnly(optMode)
402         pass
403
404     ## Set mode of splitting over-constrained elements
405     #  @param ovcMode, one of the following values
406     #  smeshBuilder.Mode_NO (default),
407     #  smeshBuilder.Mode_YES,
408     #  smeshBuilder.Mode_ONLY
409     def SetSplitOverConstrained(self, ovcMode ):
410         self.Parameters().SetSplitOverConstrained(ovcMode)
411         pass
412
413     ## Activate smoothing sliver elements:
414     #  @param toSmooth - Boolean flag
415     def SetSmoothOffSlivers(self, toSmooth ):
416         self.Parameters().SetSmoothOffSlivers(toSmooth)
417         pass
418
419     ## Set multithread mode
420     #  @param mode - the mode, one of the following values:
421     #  smeshBuilder.Mode_SAFE,
422     #  smeshBuilder.Mode_AGGRESSIVE,
423     #  smeshBuilder.Mode_NONE (default)
424     def SetPThreadsMode(self, mode ):
425         self.Parameters().SetPThreadsMode(mode)
426         pass
427
428     ## Set maximal number of threads
429     #  @param nb - number of threads
430     def SetMaximalNumberOfThreads(self, nb ):
431         self.Parameters().SetMaximalNumberOfThreads(nb)
432         pass
433
434
435     ## Set Optimization level:
436     #  @param level optimization level, one of the following values
437     #  - None_Optimization
438     #  - Light_Optimization
439     #  - Standard_Optimization
440     #  - StandardPlus_Optimization
441     #  - Strong_Optimization.
442     #  .
443     #  Default is Standard_Optimization
444     def SetOptimizationLevel(self, level):
445         self.Parameters().SetOptimizationLevel(level)
446         pass
447
448     ## Set maximal size of memory to be used by the algorithm (in Megabytes).
449     #  @param MB maximal size of memory
450     def SetMaximumMemory(self, MB):
451         self.Parameters().SetMaximumMemory(MB)
452         pass
453
454     ## Set initial size of memory to be used by the algorithm (in Megabytes) in
455     #  automatic memory adjustment mode.
456     #  @param MB initial size of memory
457     def SetInitialMemory(self, MB):
458         self.Parameters().SetInitialMemory(MB)
459         pass
460
461     ## Set path to working directory.
462     #  @param path working directory
463     def SetWorkingDirectory(self, path):
464         self.Parameters().SetWorkingDirectory(path)
465         pass
466
467     ## To keep working files or remove them.
468     #  @param toKeep "keep working files" flag value
469     def SetKeepFiles(self, toKeep):
470         self.Parameters().SetKeepFiles(toKeep)
471         pass
472
473     ## Remove or not the log file (if any) in case of successful computation.
474     #  The log file remains in case of errors anyway. If
475     #  the "keep working files" flag is set to true, this option
476     #  has no effect.
477     #  @param toRemove "remove log on success" flag value
478     def SetRemoveLogOnSuccess(self, toRemove):
479         self.Parameters().SetRemoveLogOnSuccess(toRemove)
480         pass
481
482     ## Print the the log in a file. If set to false, the
483     # log is printed on the standard output
484     #  @param toPrintLogInFile "print log in a file" flag value
485     def SetPrintLogInFile(self, toPrintLogInFile):
486         self.Parameters().SetStandardOutputLog(not toPrintLogInFile)
487         pass
488
489     ## Set verbosity level [0-10].
490     #  @param level verbosity level
491     #  - 0 - no standard output,
492     #  - 2 - prints the data, quality statistics of the skin and final meshes and
493     #    indicates when the final mesh is being saved. In addition the software
494     #    gives indication regarding the CPU time.
495     #  - 10 - same as 2 plus the main steps in the computation, quality statistics
496     #    histogram of the skin mesh, quality statistics histogram together with
497     #    the characteristics of the final mesh.
498     def SetVerboseLevel(self, level):
499         self.Parameters().SetVerboseLevel(level)
500         pass
501
502     ## To create new nodes.
503     #  @param toCreate "create new nodes" flag value
504     def SetToCreateNewNodes(self, toCreate):
505         self.Parameters().SetToCreateNewNodes(toCreate)
506         pass
507
508     ## Sets command line option as text.
509     #  @param option command line option
510     def SetAdvancedOption(self, option):
511         self.Parameters().SetAdvancedOption(option)
512         pass
513
514     pass # end of GHS3D_Optimizer class
515
516 class hypoProxy(GHS3DPlugin._objref_GHS3DPlugin_Hypothesis):
517     """
518     Private class wrapping to provide backward compatibility with deprecated API.
519     """
520     def __init__(self, *args):
521         GHS3DPlugin._objref_GHS3DPlugin_Hypothesis.__init__(self, *args)
522     def __deepcopy__(self, memo=None):
523         new = self.__class__(self)
524         return new
525     def SetPrintLogInFile(self, value):
526         self.SetStandardOutputLog(not value)
527 omniORB.registerObjref(GHS3DPlugin._objref_GHS3DPlugin_Hypothesis._NP_RepositoryId, hypoProxy)
528 del hypoProxy