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