Salome HOME
tests et tutorial
[modules/homard.git] / src / tests / Test / test_4.py
index 3c243f7b07d1786ea30cb3cd57879c63b9f8a304..ddb7d7d1d83fdf9f2e0c80583d5d30f9cf9ac572 100755 (executable)
@@ -21,7 +21,7 @@
 Python script for HOMARD
 Test test_4
 """
-__revision__ = "V2.2"
+__revision__ = "V3.03"
 
 #========================================================================
 TEST_NAME = "test_4"
@@ -32,7 +32,6 @@ DY = 400.
 DZ = 200.
 #========================================================================
 import os
-import tempfile
 import sys
 import numpy as np
 import salome
@@ -48,19 +47,11 @@ PATH_HOMARD = os.getenv('HOMARD_ROOT_DIR')
 REP_PYTHON = os.path.join(PATH_HOMARD, "bin", "salome", "test", "HOMARD")
 REP_PYTHON = os.path.normpath(REP_PYTHON)
 sys.path.append(REP_PYTHON)
-from test_util import remove_dir
+from test_util import get_dir
 from test_util import test_results
-# Repertoire des donnees du test
-REP_DATA = os.path.join(PATH_HOMARD, "share", "salome", "homardsamples")
-REP_DATA = os.path.normpath(REP_DATA)
-# Repertoire des resultats
-if DEBUG :
-  DIRCASE = os.path.join("/tmp", TEST_NAME)
-  if ( os.path.isdir(DIRCASE) ) :
-    remove_dir(DIRCASE)
-  os.mkdir(DIRCASE)
-else :
-  DIRCASE = tempfile.mkdtemp()
+# ==================================
+# Répertoires pour ce test
+REP_DATA, DIRCASE = get_dir(PATH_HOMARD, TEST_NAME, DEBUG)
 # ==================================
 
 salome.salome_init()
@@ -78,7 +69,7 @@ IPAR.append("AP_MODULES_LIST", "Homard")
 #
 #========================================================================
 #========================================================================
-def geom_smesh_exec(theStudy):
+def geom_smesh_exec():
   """
 Python script for GEOM and SMESH
   """
@@ -86,7 +77,7 @@ Python script for GEOM and SMESH
 #
   while not error :
   #
-    geompy = geomBuilder.New(theStudy)
+    geompy = geomBuilder.New()
   #
   # Creation of the box
   # ===================
@@ -94,7 +85,7 @@ Python script for GEOM and SMESH
 
   # Creation of the mesh
   # ====================
-    smesh = smeshBuilder.New(theStudy)
+    smesh = smeshBuilder.New()
     box_m = smesh.Mesh(box_g)
     smesh.SetName(box_m.GetMesh(), 'MESH')
   #
@@ -127,10 +118,10 @@ Python script for GEOM and SMESH
   #
     try:
       ficmed = os.path.join(DIRCASE, 'maill.00.med')
-      box_m.ExportMED( ficmed, 0, SMESH.MED_V2_2, 1, None, 1)
-    except Exception, eee:
+      box_m.ExportMED(ficmed)
+    except IOError as eee:
       error = 2
-      raise Exception('ExportToMEDX() failed. '+eee.message)
+      raise Exception('ExportMED() failed. ' + str(eee))
   #
     break
   #
@@ -139,7 +130,7 @@ Python script for GEOM and SMESH
 #========================================================================
 #
 #========================================================================
-def field_exec(theStudy, niter):
+def field_exec(niter):
   """
 Python script for MEDCoupling
   """
@@ -166,7 +157,7 @@ Python script for MEDCoupling
       valeur[iaux] = 1.e0 / max ( 1.e-5, np.sqrt(distance) )
     #print ". valeur", valeur
     nparr = valeur.toNumPyArray()
-    print ". mini/maxi", nparr.min(), nparr.max()
+    print(". mini/maxi", nparr.min(), nparr.max())
   #
   # Creation of the field
   # =====================
@@ -185,7 +176,7 @@ Python script for MEDCoupling
 
 #========================================================================
 #========================================================================
-def homard_exec(theStudy):
+def homard_exec():
   """
 Python script for HOMARD
   """
@@ -213,31 +204,31 @@ Python script for HOMARD
     dico["-1"] = "deraffinement"
   # Creation of the hypothesis hypo_4_1
     hyponame_1 = "Zone_1"
-    print "-------- Creation of the hypothesis", hyponame_1
+    print("-------- Creation of the hypothesis", hyponame_1)
     hypo_4_1 = HOMARD.CreateHypothesis(hyponame_1)
     hypo_4_1.AddZone('Zone_4_1', 1)
     hypo_4_1.SetExtraOutput(2)
     laux = hypo_4_1.GetZones()
-    nbzone = len(laux)/2
+    nbzone = len(laux) // 2
     jaux = 0
-    for iaux in range(nbzone) :
-      print hyponame_1, " : ", dico[laux[jaux+1]], "sur la zone", laux[jaux]
+    for _ in range(nbzone) :
+      print(hyponame_1, " : ", dico[laux[jaux+1]], "sur la zone", laux[jaux])
       jaux += 2
   # Creation of the hypothesis hypo_4_2
     hyponame_2 = "Zone_2"
-    print "-------- Creation of the hypothesis", hyponame_2
+    print("-------- Creation of the hypothesis", hyponame_2)
     hypo_4_2 = HOMARD.CreateHypothesis(hyponame_2)
     hypo_4_2.AddZone('Zone_4_2', 1)
     hypo_4_2.SetExtraOutput(2)
     laux = hypo_4_2.GetZones()
-    nbzone = len(laux)/2
+    nbzone = len(laux) // 2
     jaux = 0
-    for iaux in range(nbzone) :
-      print hyponame_2, " : ", dico[laux[jaux+1]], "sur la zone", laux[jaux]
+    for _ in range(nbzone) :
+      print(hyponame_2, " : ", dico[laux[jaux+1]], "sur la zone", laux[jaux])
       jaux += 2
   # Creation of the hypothesis DISTANCE INVERSE
     hyponame_3 = "DISTANCE INVERSE"
-    print "-------- Creation of the hypothesis", hyponame_3
+    print("-------- Creation of the hypothesis", hyponame_3)
     hypo_4_3 = HOMARD.CreateHypothesis(hyponame_3)
     hypo_4_3.SetField('DISTANCE')
     hypo_4_3.SetUseComp(0)
@@ -245,17 +236,17 @@ Python script for HOMARD
     hypo_4_3.SetUnRefThr(1, 0.2)
     hypo_4_3.AddFieldInterp('DISTANCE')
     hypo_4_3.SetExtraOutput(2)
-    print hyponame_3, " : zones utilisées :", hypo_4_3.GetZones()
-    print hyponame_3, " : champ utilisé :", hypo_4_3.GetFieldName()
-    print hyponame_3, " : composantes utilisées :", hypo_4_3.GetComps()
+    print(hyponame_3, " : zones utilisées :", hypo_4_3.GetZones())
+    print(hyponame_3, " : champ utilisé :", hypo_4_3.GetFieldName())
+    print(hyponame_3, " : composantes utilisées :", hypo_4_3.GetComps())
     if ( len (hypo_4_3.GetFieldName()) > 0 ) :
-      print ".. caractéristiques de l'adaptation :", hypo_4_3.GetField()
-    print hyponame_3, " : champs interpolés :", hypo_4_3.GetFieldInterps()
+      print(".. caractéristiques de l'adaptation :", hypo_4_3.GetField())
+    print(hyponame_3, " : champs interpolés :", hypo_4_3.GetFieldInterps())
   #
   # Creation of the cases
   # =====================
     # Creation of the case
-    print "-------- Creation of the case", TEST_NAME
+    print("-------- Creation of the case", TEST_NAME)
     mesh_file = os.path.join(DIRCASE, 'maill.00.med')
     case_test_4 = HOMARD.CreateCase(TEST_NAME, 'MESH', mesh_file)
     case_test_4.SetDirName(DIRCASE)
@@ -264,10 +255,10 @@ Python script for HOMARD
   # ==========================
   # Creation of the iteration 1
     iter_name = "I_" + TEST_NAME + "_1"
-    print "-------- Creation of the iteration", iter_name
+    print("-------- Creation of the iteration", iter_name)
     iter_test_4_1 = case_test_4.NextIteration(iter_name)
     iter_test_4_1.AssociateHypo(hyponame_1)
-    print ". Hypothese :", hyponame_1
+    print(". Hypothese :", hyponame_1)
     iter_test_4_1.SetMeshName('M1')
     iter_test_4_1.SetMeshFile(os.path.join(DIRCASE, 'maill.01.med'))
     error = iter_test_4_1.Compute(1, 2)
@@ -277,10 +268,10 @@ Python script for HOMARD
 
   # Creation of the iteration 2
     iter_name = "I_" + TEST_NAME + "_2"
-    print "-------- Creation of the iteration", iter_name
+    print("-------- Creation of the iteration", iter_name)
     iter_test_4_2 = iter_test_4_1.NextIteration(iter_name)
     iter_test_4_2.AssociateHypo(hyponame_2)
-    print ". Hypothese :", hyponame_2
+    print(". Hypothese :", hyponame_2)
     iter_test_4_2.SetMeshName('M2')
     iter_test_4_2.SetMeshFile(os.path.join(DIRCASE, 'maill.02.med'))
     error = iter_test_4_2.Compute(1, 2)
@@ -290,16 +281,16 @@ Python script for HOMARD
 
   # Creation of the iteration 3
   #
-    error = field_exec(theStudy, 2)
+    error = field_exec(2)
     if error :
       error = 30
       break
   #
     iter_name = "I_" + TEST_NAME + "_3"
-    print "-------- Creation of the iteration", iter_name
+    print("-------- Creation of the iteration", iter_name)
     iter_test_4_3 = iter_test_4_2.NextIteration(iter_name)
     iter_test_4_3.AssociateHypo(hyponame_3)
-    print ". Hypothese :", hyponame_3
+    print(". Hypothese :", hyponame_3)
     iter_test_4_3.SetMeshName('M3')
     iter_test_4_3.SetFieldFile(os.path.join(DIRCASE, 'maill.02.med'))
     iter_test_4_3.SetMeshFile(os.path.join(DIRCASE, 'maill.03.med'))
@@ -317,11 +308,11 @@ Python script for HOMARD
 # Geometry and Mesh
 #
 try :
-  ERROR = geom_smesh_exec(salome.myStudy)
+  ERROR = geom_smesh_exec()
   if ERROR :
     raise Exception('Pb in geom_smesh_exec')
-except Exception, eee:
-  raise Exception('Pb in geom_smesh_exec: '+eee.message)
+except RuntimeError as eee:
+  raise Exception('Pb in geom_smesh_exec: '+str(eee.message))
 
 HOMARD = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
 assert HOMARD is not None, "Impossible to load homard engine"
@@ -330,11 +321,11 @@ HOMARD.SetLanguageShort("fr")
 # Exec of HOMARD-SALOME
 #
 try :
-  ERROR = homard_exec(salome.myStudy)
+  ERROR = homard_exec()
   if ERROR :
     raise Exception('Pb in homard_exec at iteration %d' %ERROR )
-except Exception, eee:
-  raise Exception('Pb in homard_exec: '+eee.message)
+except RuntimeError as eee:
+  raise Exception('Pb in homard_exec: '+str(eee.message))
 #
 # Test of the results
 #
@@ -343,6 +334,6 @@ DESTROY_DIR = not DEBUG
 test_results(REP_DATA, TEST_NAME, DIRCASE, N_ITER_TEST_FILE, N_REP_TEST_FILE, DESTROY_DIR)
 #
 if salome.sg.hasDesktop():
-  salome.sg.updateObjBrowser(True)
+  salome.sg.updateObjBrowser()
   iparameters.getSession().restoreVisualState(1)