X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2Ftests%2FTest%2Ftest_4.py;h=6b230c0c401fd54c08867415429d864369ea1f48;hb=refs%2Ftags%2FV9_13_0b1;hp=995850109708c6479230449034e2cf682031ba41;hpb=2711971fb8085834eafdae7845ca7f70d72ee8b2;p=modules%2Fhomard.git diff --git a/src/tests/Test/test_4.py b/src/tests/Test/test_4.py index 99585010..6b230c0c 100755 --- a/src/tests/Test/test_4.py +++ b/src/tests/Test/test_4.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (C) 2011-2015 CEA/DEN, EDF R&D +# Copyright (C) 2011-2024 CEA, EDF # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -17,141 +17,139 @@ # # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com # -""" -Python script for HOMARD -Test test_4 -""" -__revision__ = "V1.0" +"""Python script for HOMARD - Test test_4""" +__revision__ = "V4.01" -#======================================================================== -Test_Name = "test_4" -debug=False -n_iter_test_file = 3 -DX = 600. -DY = 400. -DZ = 200. -#======================================================================== import os -import tempfile import sys import numpy as np + import salome -import GEOM +import SHAPERSTUDY import SMESH import HOMARD -import MEDCoupling as mc +import medcoupling as mc import MEDLoader as ml -# + +from salome.shaper import model +from salome.smesh import smeshBuilder +from MEDCouplingRemapper import MEDCouplingRemapper + # ================================== -pathHomard = os.getenv('HOMARD_ROOT_DIR') -# Repertoire des donnees du test -Rep_Test = os.path.join(pathHomard, "share", "salome", "resources", "homard") -Rep_Test = os.path.normpath(Rep_Test) -sys.path.append(Rep_Test) -from test_util import remove_dir +PATH_HOMARD = os.getenv('HOMARD_ROOT_DIR') +# Repertoire des scripts utilitaires +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 get_dir from test_util import test_results -# 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() # ================================== -salome.salome_init() +#======================================================================== +TEST_NAME = "test_4" +DEBUG = False +N_ITER_TEST_FILE = 3 +DX = 600. +DY = 400. +DZ = 200. +# Répertoires pour ce test +REP_DATA, DIRCASE = get_dir(PATH_HOMARD, TEST_NAME, DEBUG) +#======================================================================== -import SALOMEDS -from salome.geom import geomBuilder -from salome.smesh import smeshBuilder -from salome.StdMeshers import StdMeshersBuilder -# -from MEDLoader import MEDLoader -from MEDCouplingRemapper import MEDCouplingRemapper +salome.salome_init_without_session() -import iparameters -ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters("Interface Applicative", 1)) -ipar.append("AP_MODULES_LIST", "Homard") -# #======================================================================== +def create_cao_smesh (): + """CAO and mesh""" + + structure_sh = create_cao () + + error, mesh_file = create_mesh (structure_sh) + + return error, mesh_file +#======================================================================== + +#======================================================================== +def create_cao (): + """CAO""" + + model.begin() + partset = model.moduleDocument() + + part_1 = model.addPart(partset) + part_1_doc = part_1.document() + + structure_sh = model.addBox(part_1_doc, DX, DY, DZ,) + structure_sh.setName(TEST_NAME) + structure_sh.result().setName(TEST_NAME) + + model.end() + + return structure_sh +#======================================================================== + #======================================================================== -def geom_smesh_exec(theStudy): - """ -Python script for GEOM and SMESH - """ +def create_mesh (structure_sh): + """Mesh""" error = 0 -# + mesh_file = os.path.join(DIRCASE, 'maill.00.med') while not error : - # - geompy = geomBuilder.New(theStudy) - # - # Creation of the box - # =================== - BOX = geompy.MakeBoxDXDYDZ(DX, DY, DZ, "BOX") - - # Creation of the mesh - # ==================== - smesh = smeshBuilder.New(theStudy) - MESH = smesh.Mesh(BOX) - smesh.SetName(MESH.GetMesh(), 'MESH') - # - # Creation of the hypotheses - # ========================== - Regular_1D = MESH.Segment() - smesh.SetName(Regular_1D.GetAlgorithm(), 'Regular_1D') - Length = min(DX, DY, DZ) / 5. - Local_Length = Regular_1D.LocalLength(Length,None,1e-07) - smesh.SetName(Local_Length, 'Local Length') - # - Quadrangle_2D = MESH.Quadrangle(algo=smeshBuilder.QUADRANGLE) - smesh.SetName(Quadrangle_2D.GetAlgorithm(), 'Quadrangle_2D') - Quadrangle_Parameters = Quadrangle_2D.QuadrangleParameters(StdMeshersBuilder.QUAD_STANDARD,-1,[],[]) - smesh.SetName(Quadrangle_Parameters, 'Quadrangle Parameters') - # - Hexa_3D = MESH.Hexahedron(algo=smeshBuilder.Hexa) - smesh.SetName(Hexa_3D.GetAlgorithm(), 'Hexa_3D') - # - # Computation - # =========== - # - isDone = MESH.Compute() + +# 1. Importation to the study +# =========================== + model.publishToShaperStudy() + l_aux = SHAPERSTUDY.shape(model.featureStringId(structure_sh)) + +# 2. Creation of the mesh +# ======================= + smesh = smeshBuilder.New() + structure_m = smesh.Mesh(l_aux[0]) + +# Creation of the hypotheses + regular_1d = structure_m.Segment() + smesh.SetName(regular_1d.GetAlgorithm(), 'Regular_1D') + length = min(DX, DY, DZ) / 5. + local_length = regular_1d.LocalLength(length, None, 1e-07) + smesh.SetName(local_length, 'Local Length') + + quadrangle_2d = structure_m.Quadrangle(algo=smeshBuilder.QUADRANGLE) + smesh.SetName(quadrangle_2d.GetAlgorithm(), 'Quadrangle_2D') + + hexa_3d = structure_m.Hexahedron(algo=smeshBuilder.Hexa) + smesh.SetName(hexa_3d.GetAlgorithm(), 'Hexa_3D') + +# Computation + isDone = structure_m.Compute() if not isDone : error = 1 break - # - # MED exportation - # =============== - # + +# MED exportation try: - ficmed = os.path.join(dircase, 'maill.00.med') - MESH.ExportMED( ficmed, 0, SMESH.MED_V2_2, 1, None ,1) - except Exception, e: - raise Exception('ExportToMEDX() failed. '+e.message) + structure_m.ExportMED(mesh_file) + except IOError as eee: error = 2 - # + raise Exception('ExportMED() failed. ' + str(eee)) + break - # - return error + return error, mesh_file #======================================================================== -# + #======================================================================== -def field_exec(theStudy, niter): - """ -Python script for MEDCoupling - """ +def field_exec(niter): + """Python script for MEDCoupling""" error = 0 -# + while not error : - # - # The mesh - # ======== - ficmed = os.path.join(dircase, 'maill.%02d.med' % niter) + +# 1. The mesh +# =========== + ficmed = os.path.join(DIRCASE, 'maill.%02d.med' % niter) meshMEDFileRead = ml.MEDFileMesh.New(ficmed) meshRead0 = meshMEDFileRead.getMeshAtLevel(0) - # Valeurs of the field - # ==================== +# 2. Values of the field +# ====================== nbNodes = meshRead0.getNumberOfNodes() valeur = mc.DataArrayDouble(nbNodes) for iaux, taux in enumerate(meshRead0.getCoords()) : @@ -164,184 +162,213 @@ 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() - # - # Creation of the field - # ===================== + print(". mini/maxi {}/{}".format(nparr.min(),nparr.max())) + +# 3. Creation of the field +# ======================== field = ml.MEDCouplingFieldDouble(ml.ON_NODES, ml.ONE_TIME) field.setArray(valeur) field.setMesh(meshRead0) field.setName("DISTANCE") - # + fMEDFile_ch = ml.MEDFileField1TS() fMEDFile_ch.setFieldNoProfileSBT(field) # No profile desired on the field, Sort By Type fMEDFile_ch.write(ficmed, 0) # 0 to indicate that we *append* (and no overwrite) to the MED file - # + break - # + return error #======================================================================== + #======================================================================== -def homard_exec(theStudy): - """ -Python script for HOMARD - """ +def homard_exec(mesh_file): + """Python script for HOMARD""" error = 0 -# + while not error : - # - homard.SetCurrentStudy(theStudy) - # - # Creation of the zones - # ===================== - # + +# 1. Creation of the zones +# ======================== +# Creation of the box zone_4_1 epsilon = min(DX, DY, DZ) / 100. - # Creation of the box Zone_4_1 - Zone_4_1 = homard.CreateZoneBox('Zone_4_1', -epsilon, DX/3.+epsilon, DY/4.-epsilon, 3.*DY/4.+epsilon, 4.*DZ/5.-epsilon, DZ+epsilon) + _ = HOMARD.CreateZoneBox('Zone_4_1', -epsilon, DX/3.+epsilon, DY/4.-epsilon, 3.*DY/4.+epsilon, 4.*DZ/5.-epsilon, DZ+epsilon) - # Creation of the sphere Zone_4_2 +# Creation of the sphere zone_4_2 rayon = min(DX, DY, DZ) / 4. - Zone_4_2 = homard.CreateZoneSphere('Zone_4_2', DX/3., DY*0.3, DZ*0.6, rayon) - # - # Creation of the hypotheses - # ========================== - dico = {} + _ = HOMARD.CreateZoneSphere('Zone_4_2', DX/3., DY*0.3, DZ*0.6, rayon) + +# 2. Creation of the hypotheses +# ============================= + error, hyponame = homard_exec_hypo () + if error : + break + +# 3. Creation of the cases +# ======================== + # Creation of the case + print("-------- Creation of the case {}".format(TEST_NAME)) + case_test_4 = HOMARD.CreateCase(TEST_NAME, TEST_NAME, mesh_file) + case_test_4.SetDirName(DIRCASE) + +# 4. Creation of the iterations +# ============================= + error = homard_exec_iter(case_test_4, hyponame) + + break + + return error + +#======================================================================== + +#======================================================================== +def homard_exec_hypo(): + """Python script for HOMARD - Creation of the hypotheses""" + + error = 0 + while not error : + + dico = dict() dico["1"] = "raffinement" dico["-1"] = "deraffinement" - # Creation of the hypothesis Hypo_4_1 - HypoName_1 = "Zone_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 + hyponame = list() + +# 1. Creation of the hypothesis hypo_4_1 +# ====================================== + hyponame.append("Zone_1") + print("-------- Creation of the hypothesis {}".format(hyponame[0])) + hypo_4_1 = HOMARD.CreateHypothesis(hyponame[0]) + hypo_4_1.AddZone('Zone_4_1', 1) + hypo_4_1.SetExtraOutput(2) + laux = hypo_4_1.GetZones() + 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[0], " : ", 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 - 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 +# 2. Creation of the hypothesis hypo_4_2 +# ====================================== + hyponame.append("Zone_2") + print("-------- Creation of the hypothesis {}".format(hyponame[1])) + hypo_4_2 = HOMARD.CreateHypothesis(hyponame[1]) + hypo_4_2.AddZone('Zone_4_2', 1) + hypo_4_2.SetExtraOutput(2) + laux = hypo_4_2.GetZones() + 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[1], " : ", 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 - Hypo_4_3 = homard.CreateHypothesis(HypoName_3) - Hypo_4_3.SetField('DISTANCE') - Hypo_4_3.SetUseComp(0) - Hypo_4_3.SetRefinThr(1, 0.3) - 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() - 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() - # - # Creation of the cases - # ===================== - # Creation of the case - CaseName = "Case_" + Test_Name - print "-------- Creation of the case", CaseName - MeshFile = os.path.join(dircase, 'maill.00.med') - Case_test_4 = homard.CreateCase(CaseName, 'MESH', MeshFile) - Case_test_4.SetDirName(dircase) - # - # Creation of the iterations - # ========================== - # Creation of the iteration 1 - IterName = "I_" + Test_Name + "_1" - print "-------- Creation of the iteration", IterName - Iter_test_4_1 = Case_test_4.NextIteration(IterName) - Iter_test_4_1.AssociateHypo(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) +# 3. Creation of the hypothesis DISTANCE INVERSE +# ============================================== + hyponame.append("DISTANCE INVERSE") + print("-------- Creation of the hypothesis {}".format(hyponame[2])) + hypo_4_3 = HOMARD.CreateHypothesis(hyponame[2]) + hypo_4_3.SetField('DISTANCE') + hypo_4_3.SetUseComp(0) + hypo_4_3.SetRefinThr(1, 0.3) + hypo_4_3.SetUnRefThr(1, 0.2) + hypo_4_3.AddFieldInterp('DISTANCE') + hypo_4_3.SetExtraOutput(2) + print(hyponame[2], " : zones utilisées : {}".format(hypo_4_3.GetZones())) + print(hyponame[2], " : champ utilisé : {}".format(hypo_4_3.GetFieldName())) + print(hyponame[2], " : composantes utilisées : {}".format(hypo_4_3.GetComps())) + if len (hypo_4_3.GetFieldName()): + print(".. caractéristiques de l'adaptation : {}".format(hypo_4_3.GetField())) + print(hyponame[2], " : champs interpolés : {}".format(hypo_4_3.GetFieldInterps())) + + break + + return error, hyponame + +#======================================================================== + +#======================================================================== +def homard_exec_iter(case_test_4, hyponame): + """Python script for HOMARD - Creation of the iterations""" + + error = 0 + while not error : + +# 1. Creation of the iteration 1 + iter_name = "I_" + TEST_NAME + "_1" + print("-------- Creation of the iteration", iter_name) + iter_test_4_1 = case_test_4.NextIteration(iter_name) + iter_test_4_1.AssociateHypo(hyponame[0]) + print(". Hypothese :", hyponame[0]) + 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) if error : error = 1 break - # Creation of the iteration 2 - IterName = "I_" + Test_Name + "_2" - print "-------- Creation of the iteration", IterName - Iter_test_4_2 = Iter_test_4_1.NextIteration(IterName) - Iter_test_4_2.AssociateHypo(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) +# 2. Creation of the iteration 2 + iter_name = "I_" + TEST_NAME + "_2" + print("-------- Creation of the iteration", iter_name) + iter_test_4_2 = iter_test_4_1.NextIteration(iter_name) + iter_test_4_2.AssociateHypo(hyponame[1]) + print(". Hypothese :", hyponame[1]) + 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) if error : error = 2 break - # Creation of the iteration 3 - # - error = field_exec(theStudy, 2) +# 3. Creation of the iteration 3 + + error = field_exec(2) if error : error = 30 break - # - IterName = "I_" + Test_Name + "_3" - print "-------- Creation of the iteration", IterName - Iter_test_4_3 = Iter_test_4_2.NextIteration(IterName) - Iter_test_4_3.AssociateHypo(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')) - error = Iter_test_4_3.Compute(1, 2) + + iter_name = "I_" + TEST_NAME + "_3" + print("-------- Creation of the iteration", iter_name) + iter_test_4_3 = iter_test_4_2.NextIteration(iter_name) + iter_test_4_3.AssociateHypo(hyponame[2]) + print(". Hypothese :", hyponame[2]) + 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')) + error = iter_test_4_3.Compute(1, 2) if error : error = 3 break - # + break - # + return error #======================================================================== -# -# Geometry and Mesh -# + +# CAO and Mesh + try : - error_main = geom_smesh_exec(salome.myStudy) - if error_main : - raise Exception('Pb in geom_smesh_exec') -except Exception, e: - raise Exception('Pb in geom_smesh_exec: '+e.message) - -homard = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD') -assert homard is not None, "Impossible to load homard engine" -homard.SetLanguageShort("fr") -# + ERROR, MESH_FILE = create_cao_smesh() + if ERROR : + raise Exception('Pb in create_cao_smesh') +except RuntimeError as eee: + raise Exception('Pb in create_cao_smesh: '+str(eee.message)) + +HOMARD = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD') +assert HOMARD is not None, "Impossible to load homard engine" +HOMARD.SetLanguageShort("fr") + # Exec of HOMARD-SALOME -# + try : - error_main = homard_exec(salome.myStudy) - if error_main : - raise Exception('Pb in homard_exec at iteration %d' %error_main ) -except Exception, e: - raise Exception('Pb in homard_exec: '+e.message) -# + ERROR = homard_exec(MESH_FILE) + if ERROR : + raise Exception('Pb in homard_exec at iteration %d' %ERROR ) +except RuntimeError as eee: + raise Exception('Pb in homard_exec: '+str(eee.message)) + # Test of the results -# -n_rep_test_file = n_iter_test_file -destroy_dir = not debug -test_results(Rep_Test, Test_Name, dircase, n_iter_test_file, n_rep_test_file, destroy_dir) -# -if salome.sg.hasDesktop(): - salome.sg.updateObjBrowser(1) - iparameters.getSession().restoreVisualState(1) +N_REP_TEST_FILE = N_ITER_TEST_FILE +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()