Salome HOME
7a1226e013ddd7203af9d06a33bbcb2f1b18e838
[modules/homard.git] / src / tests / Test / test_6.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2016  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 """
21 Python script for HOMARD
22 Test test_6
23 """
24 __revision__ = "V1.02"
25
26 #========================================================================
27 TEST_NAME = "test_6"
28 DEBUG = False
29 N_ITER_TEST_FILE = 3
30 #
31 TAILLE = 10.
32 LG_ARETE = TAILLE*2.5
33 #========================================================================
34 import os
35 import tempfile
36 import sys
37 import HOMARD
38 import salome
39 #
40 # ==================================
41 PATH_HOMARD = os.getenv('HOMARD_ROOT_DIR')
42 # Repertoire des scripts utilitaires
43 REP_PYTHON = os.path.join(PATH_HOMARD, "bin", "salome", "test", "HOMARD")
44 REP_PYTHON = os.path.normpath(REP_PYTHON)
45 sys.path.append(REP_PYTHON)
46 from test_util import remove_dir
47 from test_util import test_results
48 from test_util import saveGeometry
49 # Repertoire des donnees du test
50 REP_DATA = os.path.join(PATH_HOMARD, "share", "salome", "homardsamples")
51 REP_DATA = os.path.normpath(REP_DATA)
52 # Repertoire des resultats
53 if DEBUG :
54   DIRCASE = os.path.join("/tmp", TEST_NAME)
55   if ( os.path.isdir(DIRCASE) ) :
56     remove_dir(DIRCASE)
57   os.mkdir(DIRCASE)
58 else :
59   DIRCASE = tempfile.mkdtemp(prefix=TEST_NAME)
60
61 salome.salome_init()
62 import iparameters
63 IPAR = iparameters.IParameters(salome.myStudy.GetCommonParameters("Interface Applicative", 1))
64 IPAR.append("AP_MODULES_LIST", "Homard")
65 #
66 #========================= Debut de la fonction ==================================
67 #
68 def create_geom(nom_obj, taille, verbose=False) :
69   """
70 Création de la géométrie
71   """
72 #
73   erreur = 0
74 #
75   if verbose :
76     texte = "Geometrie '%s'\n" % nom_obj
77     texte += "Taille de base = %f" % taille
78     print (texte)
79 #
80   import GEOM
81   from salome.geom import geomBuilder
82   geompy = geomBuilder.New(salome.myStudy)
83 #
84 # 1. Les sommets et la première ligne
85 #
86   vertex_1 = geompy.MakeVertex( 0.*taille,  0.*taille, 0.*taille, theName = "V1")
87   vertex_2 = geompy.MakeVertex( 5.*taille,  2.*taille, 0.*taille, theName = "V2")
88   vertex_3 = geompy.MakeVertex(10.*taille,  1.*taille, 0.*taille, theName = "V3")
89   vertex_4 = geompy.MakeVertex(16.*taille,  4.*taille, 0.*taille, theName = "V4")
90   vertex_5 = geompy.MakeVertex(16.*taille, 10.*taille, 0.*taille, theName = "V5")
91 #
92   courbe_0 = geompy.MakeInterpol([vertex_1, vertex_2, vertex_3, vertex_4, vertex_5], False, False, theName="courbe_0")
93 #
94 # 2. Les sommets et la seconde ligne
95 #
96   sommet_1 = geompy.MakeVertex( 0.*taille,  0.*taille, 20.*taille, theName = "S1")
97   sommet_2 = geompy.MakeVertex( 6.*taille, -5.*taille, 20.*taille, theName = "S2")
98   sommet_3 = geompy.MakeVertex(11.*taille, -2.*taille, 20.*taille, theName = "S3")
99   sommet_4 = geompy.MakeVertex(12.*taille,  3.*taille, 20.*taille, theName = "S4")
100   sommet_5 = geompy.MakeVertex(16.*taille, 10.*taille, 20.*taille, theName = "S5")
101 #
102   courbe_1 = geompy.MakeInterpol([sommet_1, sommet_2, sommet_3, sommet_4, sommet_5], False, False, theName="courbe_1")
103 #
104 # 3. La face de base
105 #
106   structure_g = geompy.MakeFilling([courbe_0, courbe_1], theName=nom_obj)
107 #
108 # 4. Groupes : on cherche les entites par des proximités avec des shapes bien choisies
109 #
110   l_groupes_g = list()
111 #
112   shape = geompy.GetFaceNearPoint (structure_g, vertex_2)
113   nom = "Voile"
114   groupe_g = geompy.CreateGroup(structure_g, geompy.ShapeType["FACE"], nom)
115   geompy.UnionList ( groupe_g, [shape] )
116   l_groupes_g.append( (nom, groupe_g, 2) )
117 #
118   shape = geompy.GetEdgeNearPoint (structure_g, vertex_2)
119   nom = "C_0"
120   groupe_g = geompy.CreateGroup(structure_g, geompy.ShapeType["EDGE"], nom)
121   geompy.UnionList ( groupe_g, [shape] )
122   l_groupes_g.append( (nom, groupe_g, 1) )
123 #
124   shape = geompy.GetEdgeNearPoint (structure_g, sommet_2)
125   nom = "C_1"
126   groupe_g = geompy.CreateGroup(structure_g, geompy.ShapeType["EDGE"], nom)
127   geompy.UnionList ( groupe_g, [shape] )
128   l_groupes_g.append( (nom, groupe_g, 1) )
129 #
130   shape = geompy.GetEdge (structure_g, vertex_1, sommet_1)
131   nom = "D_0"
132   groupe_g = geompy.CreateGroup(structure_g, geompy.ShapeType["EDGE"], nom)
133   geompy.UnionList ( groupe_g, [shape] )
134   l_groupes_g.append( (nom, groupe_g, 1) )
135 #
136   shape = geompy.GetEdge (structure_g, vertex_5, sommet_5)
137   nom = "D_1"
138   groupe_g = geompy.CreateGroup(structure_g, geompy.ShapeType["EDGE"], nom)
139   geompy.UnionList ( groupe_g, [shape] )
140   l_groupes_g.append( (nom, groupe_g, 1) )
141 #
142   return erreur, structure_g, l_groupes_g
143 #
144 #==========================  Fin de la fonction ==================================
145 #
146 #========================= Debut de la fonction ==================================
147 #
148 def create_mail(lg_arete, structure_g, l_groupes_g, rep_mail, verbose=False) :
149   """
150 Création du maillage
151   """
152 #
153   erreur = 0
154   message = ""
155   ficmed = ""
156 #
157   while not erreur :
158 #
159     nom = structure_g.GetName()
160     if verbose :
161       texte = "Maillage de '%s'\n" % nom
162       texte += "lg_arete = %f\n" % lg_arete
163       texte += "rep_mail = '%s'" % rep_mail
164       print (texte)
165 #
166     import SMESH
167     from salome.smesh import smeshBuilder
168     smesh = smeshBuilder.New(salome.myStudy)
169 #
170 # 2. Maillage de calcul
171 #
172     maill_00 = smesh.Mesh(structure_g)
173     smesh.SetName(maill_00.GetMesh(), nom)
174 #
175     MG_CADSurf = maill_00.Triangle(algo=smeshBuilder.MG_CADSurf)
176     smesh.SetName(MG_CADSurf.GetAlgorithm(), 'MG_CADSurf')
177 #
178     MG_CADSurf_Parameters = MG_CADSurf.Parameters()
179     smesh.SetName(MG_CADSurf_Parameters, 'MG_CADSurf Triangles')
180     MG_CADSurf_Parameters.SetPhySize( lg_arete )
181     MG_CADSurf_Parameters.SetMinSize( lg_arete/20. )
182     MG_CADSurf_Parameters.SetMaxSize( lg_arete*5. )
183     MG_CADSurf_Parameters.SetChordalError( lg_arete )
184     MG_CADSurf_Parameters.SetAngleMesh( 12. )
185 #
186 # 3. Les groupes issus de la géométrie
187 #
188     for taux in l_groupes_g :
189       groupe_m = maill_00.Group(taux[1])
190       smesh.SetName(groupe_m, taux[0])
191 #
192 # 4. Calcul
193 #
194     isDone = maill_00.Compute()
195     if not isDone :
196       message += "Probleme dans le maillage de la surface."
197       erreur = 13
198       break
199 #
200 # 5. Export MED
201 #
202     ficmed = os.path.join(rep_mail,'maill.00.med')
203     texte = "Ecriture du fichier '%s'" % ficmed
204     if verbose :
205       print (texte)
206     maill_00.ExportMED( ficmed, 0, SMESH.MED_V2_2, 1 )
207 #
208     break
209 #
210   return erreur, message, ficmed
211 #
212 #==========================  Fin de la fonction ==================================
213 #
214 #========================= Debut de la fonction ==================================
215 #
216 def homard_exec(nom, ficmed, xao_file, verbose=False):
217   """
218 Python script for HOMARD
219   """
220   erreur = 0
221   message = ""
222 #
223   while not erreur :
224     #
225     HOMARD.SetCurrentStudy(salome.myStudy)
226     #
227     # Frontière
228     # =========
229     if verbose :
230       print(". Frontière")
231     cao_name = "CAO_" + nom
232     la_frontiere = HOMARD.CreateBoundaryCAO(cao_name, xao_file)
233     #
234     # Hypotheses
235     # ==========
236     if verbose :
237       print(". Hypothèses")
238     hyponame = "hypo_" + nom
239     l_hypothese = HOMARD.CreateHypothesis(hyponame)
240     l_hypothese.SetUnifRefinUnRef(1)
241     #
242     # Cas
243     # ===
244     if verbose :
245       print(". Cas")
246     le_cas = HOMARD.CreateCase('case_'+nom, nom, ficmed)
247     le_cas.SetDirName(DIRCASE)
248     le_cas.AddBoundary(cao_name)
249     #
250     # Creation of the iterations
251     # ==========================
252     if verbose :
253       option = 2
254     else :
255       option = 1
256     #
257     for niter in range(1, N_ITER_TEST_FILE+1):
258       if verbose :
259         print(". Itération numéro %d" % niter)
260       iter_name = "I_" + nom + "_%02d" % niter
261       if ( niter == 1 ) :
262         l_iteration = le_cas.NextIteration(iter_name)
263       else :
264         l_iteration = l_iteration.NextIteration(iter_name)
265       l_iteration.SetMeshName(nom)
266       mesh_file = os.path.join(DIRCASE, "maill.%02d.med" % niter)
267       l_iteration.SetMeshFile(mesh_file)
268       l_iteration.AssociateHypo(hyponame)
269       erreur = l_iteration.Compute(1, option)
270       if erreur :
271         erreur = niter
272         break
273     #
274     break
275   #
276   if erreur :
277     message += "Erreur au calcul de l'itération %d" % erreur
278   #
279   return erreur, message
280 #
281 #==========================  Fin de la fonction ==================================
282 #
283 #
284 ERREUR = 0
285 MESSAGE = ""
286 while not ERREUR :
287   #
288   VERBOSE = DEBUG
289   #
290   # A. Geometry
291   #
292   ERREUR, STRUCTURE_G, L_GROUPES_G = create_geom(TEST_NAME, TAILLE, VERBOSE)
293   if ERREUR :
294     MESSAGE = "The construction of the geometry failed."
295     break
296   #
297   # B. Save the geometry
298   #
299   XAO_FILE = os.path.join(DIRCASE, TEST_NAME+".xao")
300   try :
301     ERREUR = saveGeometry(XAO_FILE, TEST_NAME, "test_salome_"+TEST_NAME)
302   except Exception, eee:
303     ERREUR = 2000
304     MESSAGE = eee.message
305   #
306   if ERREUR :
307     MESSAGE += "Pb in saveGeometry"
308     break
309   #
310   # C. Mesh
311   #
312   ERREUR, MESSAGE, FICMED = create_mail(LG_ARETE, STRUCTURE_G, L_GROUPES_G, DIRCASE, VERBOSE)
313   if ERREUR :
314     break
315   #
316   # D. Exec of HOMARD-SALOME
317   #
318   HOMARD = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
319   assert HOMARD is not None, "Impossible to load homard engine"
320   HOMARD.SetLanguageShort("fr")
321 #
322   try:
323     ERREUR, MESSAGE = homard_exec(TEST_NAME, FICMED, XAO_FILE, VERBOSE)
324   except Exception, eee:
325     ERREUR = 4000
326     MESSAGE = eee.message
327   #
328   if ERREUR :
329     MESSAGE += "Pb in homard_exec"
330     break
331   #
332   # E. Test of the results
333   #
334   N_REP_TEST_FILE = N_ITER_TEST_FILE
335   DESTROY_DIR = not DEBUG
336   test_results(REP_DATA, TEST_NAME, DIRCASE, N_ITER_TEST_FILE, N_REP_TEST_FILE, DESTROY_DIR)
337   #
338   break
339 #
340 if ERREUR:
341   MESSAGE = "\nErreur numéro %d\n" % ERREUR + MESSAGE
342   raise Exception(MESSAGE)
343 #
344 if salome.sg.hasDesktop():
345   salome.sg.updateObjBrowser(True)
346   iparameters.getSession().restoreVisualState(1)
347