Salome HOME
Add examples to salome test. And add some checks to test the generated mesh
[plugins/ghs3dplugin.git] / doc / salome / examples / ghs3d_enfvert.py
index 14c25cd5074bc93bdc99fbd32c70610693ff5fb8..c4cf16588eb7f345de0865ed3dd020ac286c7355 100644 (file)
@@ -10,6 +10,8 @@
 # Ex1: Add one enforced vertex with coordinates (50,50,100) 
 #      and physical size 2.
 
+import math
+
 import salome
 salome.salome_init()
 
@@ -20,6 +22,13 @@ import SMESH
 from salome.smesh import smeshBuilder
 smesh =  smeshBuilder.New()
 
+## check if a node is at the coords given the tolerance
+def isNodeAtCoord(node_id, x, y, z, tol=1e-12):
+  xn, yn, zn = mgtetraMesh.GetNodeXYZ(node_id)
+  dist = math.sqrt((x-xn)**2+(y-yn)**2+(z-zn)**2)
+  print("dist: ", dist)
+  return dist < tol
+
 # create a box
 box = geompy.MakeBoxDXDYDZ(200., 200., 200.)
 geompy.addToStudy(box, "box")
@@ -28,7 +37,9 @@ mgtetraMesh = smesh.Mesh(box,"box: MG-Tetra and MG-CADSurf mesh")
 # create a MG-CADSurf algorithm for faces
 mgtetraMesh.Triangle(algo=smeshBuilder.MG_CADSurf)
 # compute the mesh
-mgtetraMesh.Compute()
+ok  = mgtetraMesh.Compute()
+if not ok:
+  raise Exception("Error when computing MG-CADSurf mesh")
 
 # Make a copy of the 2D mesh
 mgtetraMesh_wo_geometry = smesh.CopyMesh( mgtetraMesh, 'MG-Tetra w/o geometry', 0, 0)
@@ -37,10 +48,19 @@ mgtetraMesh_wo_geometry = smesh.CopyMesh( mgtetraMesh, 'MG-Tetra w/o geometry',
 MG_Tetra = mgtetraMesh.Tetrahedron( smeshBuilder.MG_Tetra )
 MG_Tetra_Parameters = MG_Tetra.Parameters()
 # Create the enforced vertex
-MG_Tetra_Parameters.SetEnforcedVertex( 50, 50, 100, 2) # no group
+x1 = 50
+y1 = 50
+z1 = 100
+MG_Tetra_Parameters.SetEnforcedVertex( x1, y1, z1, 2) # no group
 # Compute the mesh
-mgtetraMesh.Compute()
+ok = mgtetraMesh.Compute()
+if not ok:
+  raise Exception("Error when computing MG_Tetra mesh")
+
+# Check that the enforced node is at the enforced coords
+node_closest = mgtetraMesh.FindNodeClosestTo(x1, y1, z1)
 
+assert isNodeAtCoord(node_closest, x1, y1, z1)
 
 # Ex2: Add one vertex enforced by a GEOM vertex at (50,50,100) 
 #      with physical size 5 and add it to a group called "My special nodes"
@@ -51,13 +71,26 @@ mgtetraMesh_wo_geometry.AddHypothesis( MG_Tetra )
 mgtetraMesh_wo_geometry.AddHypothesis( MG_Tetra_Parameters_wo_geometry )
 
 # Create the enforced vertex
-p1 = geompy.MakeVertex(150, 150, 100)
-geompy.addToStudy(p1, "p1")
-MG_Tetra_Parameters_wo_geometry.SetEnforcedVertexGeomWithGroup( p1, 5 , "My special nodes")
+x2 = 50
+y2 = 50
+z2 = 100
+p2 = geompy.MakeVertex(x2, y2, z2)
+geompy.addToStudy(p2, "p2")
+gr_enforced_name = "My special nodes"
+MG_Tetra_Parameters_wo_geometry.SetEnforcedVertexGeomWithGroup( p2, 5 , gr_enforced_name)
 #MG_Tetra_Parameters.SetEnforcedVertexGeom( p1, 5 ) # no group
 
 # compute the mesh
-mgtetraMesh_wo_geometry.Compute()
+ok = mgtetraMesh_wo_geometry.Compute()
+if not ok:
+  raise Exception("Error when computing MG_Tetra mesh without geometry")
+
+# Check that the enforced node is at the enforced coords
+gr_enforced_nodes = mgtetraMesh_wo_geometry.GetGroupByName(gr_enforced_name)[0]
+assert (gr_enforced_nodes.Size() == 1)
+node_enforced = gr_enforced_nodes.GetIDs()[0]
+
+assert isNodeAtCoord(node_enforced, x2, y2, z2)
 
 # Erase all enforced vertices
 MG_Tetra_Parameters.ClearEnforcedVertices()