Salome HOME
Updated copyright comment
[modules/homard.git] / src / tests / Test / test_6.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2024  CEA, EDF
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 """Python script for HOMARD - Test test_6"""
21 __revision__ = "V2.01"
22
23 import os
24 import sys
25
26 import salome
27 import SHAPERSTUDY
28 import SMESH
29 import HOMARD
30
31 from salome.shaper import model
32 from salome.smesh import smeshBuilder
33
34 # ==================================
35 PATH_HOMARD = os.getenv('HOMARD_ROOT_DIR')
36 # Repertoire des scripts utilitaires
37 REP_PYTHON = os.path.join(PATH_HOMARD, "bin", "salome", "test", "HOMARD")
38 REP_PYTHON = os.path.normpath(REP_PYTHON)
39 sys.path.append(REP_PYTHON)
40 from test_util import get_dir
41 from test_util import test_results
42 # ==================================
43
44 #========================================================================
45 TEST_NAME = "test_6"
46 DEBUG = False
47 VERBOSE = False
48 N_ITER_TEST_FILE = 3
49 TAILLE = 10.
50 LG_ARETE = TAILLE*2.5
51 # Répertoires pour ce test
52 REP_DATA, DIRCASE = get_dir(PATH_HOMARD, TEST_NAME, DEBUG)
53 #========================================================================
54
55 salome.salome_init_without_session()
56
57 #========================================================================
58 def create_cao_smesh ():
59   """CAO and mesh"""
60
61   structure_sh, xao_file = create_cao ()
62
63   error, mesh_file = create_mesh (structure_sh)
64
65   return error, xao_file, mesh_file
66 #========================================================================
67
68 #========================================================================
69 def create_cao ():
70   """CAO"""
71
72   if VERBOSE :
73     texte = "Géométrie '{}'\n".format(TEST_NAME)
74     texte += "Taille de base ={}".format(TAILLE)
75     print (texte)
76
77   model.begin()
78   partset = model.moduleDocument()
79
80   part_1 = model.addPart(partset)
81   part_1_doc = part_1.document()
82
83   ### Create Point
84   _ = model.addPoint(part_1_doc,  0.*TAILLE,  0.*TAILLE, 0.*TAILLE)
85
86   ### Create Point
87   _ = model.addPoint(part_1_doc,  5.*TAILLE,  2.*TAILLE, 0.*TAILLE)
88
89   ### Create Point
90   _ = model.addPoint(part_1_doc, 10.*TAILLE,  1.*TAILLE, 0.*TAILLE)
91
92   ### Create Point
93   _ = model.addPoint(part_1_doc, 16.*TAILLE,  4.*TAILLE, 0.*TAILLE)
94
95   ### Create Point
96   _ = model.addPoint(part_1_doc, 16.*TAILLE, 10.*TAILLE, 0.*TAILLE)
97
98   ### Create interpolation
99   interpolation_1_objects = [model.selection("VERTEX", "all-in-Point_1"), \
100                             model.selection("VERTEX", "all-in-Point_2"), \
101                             model.selection("VERTEX", "all-in-Point_3"), \
102                             model.selection("VERTEX", "all-in-Point_4"), \
103                             model.selection("VERTEX", "all-in-Point_5")]
104   _ = model.addInterpolation(part_1_doc, interpolation_1_objects, False, False)
105
106
107   ### Create Point
108   _ = model.addPoint(part_1_doc,  0.*TAILLE,  0.*TAILLE, 20.*TAILLE)
109
110   ### Create Point
111   _ = model.addPoint(part_1_doc,  6.*TAILLE, -5.*TAILLE, 20.*TAILLE)
112
113   ### Create Point
114   _ = model.addPoint(part_1_doc, 11.*TAILLE, -2.*TAILLE, 20.*TAILLE)
115
116   ### Create Point
117   _ = model.addPoint(part_1_doc, 12.*TAILLE,  3.*TAILLE, 20.*TAILLE)
118
119   ### Create Point
120   _ = model.addPoint(part_1_doc, 16.*TAILLE, 10.*TAILLE, 20.*TAILLE)
121
122   ### Create interpolation
123   interpolation_2_objects = [model.selection("VERTEX", "all-in-Point_6"), \
124                             model.selection("VERTEX", "all-in-Point_7"), \
125                             model.selection("VERTEX", "all-in-Point_8"), \
126                             model.selection("VERTEX", "all-in-Point_9"), \
127                             model.selection("VERTEX", "all-in-Point_10")]
128   _ = model.addInterpolation(part_1_doc, interpolation_2_objects, False, False)
129
130   ### Create Filling
131   structure_sh = model.addFilling(part_1_doc, [model.selection("EDGE", "Interpolation_1_1"), model.selection("EDGE", "Interpolation_2_1")])
132   structure_sh.setName(TEST_NAME)
133   structure_sh.result().setName(TEST_NAME)
134
135   ### Create Group
136   group_1 = model.addGroup(part_1_doc, "Faces", [model.selection("FACE", TEST_NAME)])
137   group_1.setName("Voile")
138   group_1.result().setName("Voile")
139
140   ### Create Group
141   group_2 = model.addGroup(part_1_doc, "Edges", [model.selection("EDGE", "Interpolation_1_1")])
142   group_2.setName("C_0")
143   group_2.result().setName("C_0")
144
145   ### Create Group
146   group_3 = model.addGroup(part_1_doc, "Edges", [model.selection("EDGE", "Interpolation_2_1")])
147   group_3.setName("C_1")
148   group_3.result().setName("C_1")
149
150   ### Create Group
151   group_4 = model.addGroup(part_1_doc, "Edges", [model.selection("EDGE", TEST_NAME+"/Edge_0_1")])
152   group_4.setName("D_0")
153   group_4.result().setName("D_0")
154
155   ### Create Group
156   group_5 = model.addGroup(part_1_doc, "Edges", [model.selection("EDGE", TEST_NAME+"/Edge_0_3")])
157   group_5.setName("D_1")
158   group_5.result().setName("D_1")
159
160   xao_file = os.path.join(DIRCASE, TEST_NAME+".xao")
161   model.exportToXAO(part_1_doc, xao_file, model.selection("FACE", TEST_NAME), "GN", TEST_NAME)
162
163   model.end()
164
165   return structure_sh, xao_file
166 #========================================================================
167
168 #========================================================================
169 def create_mesh (structure_sh):
170   """Mesh"""
171   error = 0
172   mesh_file = os.path.join(DIRCASE, 'maill.00.med')
173
174   if VERBOSE :
175     texte = "Maillage de '{}'\n".format(TEST_NAME)
176     texte += "lg_arete = {}\n".format(LG_ARETE)
177     print (texte)
178
179   while not error :
180
181 # 1. Importation to the study
182 # ===========================
183     model.publishToShaperStudy()
184     l_aux = SHAPERSTUDY.shape(model.featureStringId(structure_sh))
185
186 # 2. Creation of the mesh
187 # =======================
188     smesh = smeshBuilder.New()
189     structure_m = smesh.Mesh(l_aux[0])
190
191     MG_CADSurf = structure_m.Triangle(algo=smeshBuilder.MG_CADSurf)
192     smesh.SetName(MG_CADSurf.GetAlgorithm(), 'MG_CADSurf')
193
194     MG_CADSurf_Parameters = MG_CADSurf.Parameters()
195     smesh.SetName(MG_CADSurf_Parameters, 'MG_CADSurf Triangles')
196     MG_CADSurf_Parameters.SetPhySize( LG_ARETE )
197     MG_CADSurf_Parameters.SetMinSize( LG_ARETE/20. )
198     MG_CADSurf_Parameters.SetMaxSize( LG_ARETE*5. )
199     MG_CADSurf_Parameters.SetChordalError( LG_ARETE )
200     MG_CADSurf_Parameters.SetAngleMesh( 12. )
201
202 # Les groupes issus de la géométrie
203     for groupe in l_aux[1:]:
204       groupe_nom = groupe.GetName()
205       if ( groupe_nom == "Voile" ):
206         shape = SMESH.FACE
207       else:
208         shape = SMESH.EDGE
209       _ = structure_m.GroupOnGeom(groupe,groupe_nom,shape)
210
211 # Computation
212     isDone = structure_m.Compute()
213     if not isDone :
214       error = 1
215       break
216
217 # MED exportation
218     try:
219       structure_m.ExportMED(mesh_file)
220     except IOError as eee:
221       error = 2
222       raise Exception('ExportMED() failed. ' + str(eee))
223
224     break
225
226   return error, mesh_file
227 #========================================================================
228
229 #========================= Debut de la fonction ==================================
230
231 def homard_exec(xao_file, mesh_file):
232   """Python script for HOMARD"""
233   error = 0
234
235   while not error :
236     #
237     #HOMARD.UpdateStudy()
238     #
239     # Frontière
240     # =========
241     if VERBOSE :
242       print(". Frontière")
243     cao_name = "CAO_" + TEST_NAME
244     _ = HOMARD.CreateBoundaryCAO(cao_name, xao_file)
245     #
246     # Hypotheses
247     # ==========
248     if VERBOSE :
249       print(". Hypothèses")
250     hyponame = "hypo_" + TEST_NAME
251     l_hypothese = HOMARD.CreateHypothesis(hyponame)
252     l_hypothese.SetUnifRefinUnRef(1)
253     #
254     # Cas
255     # ===
256     if VERBOSE :
257       print(". Cas")
258     le_cas = HOMARD.CreateCase('case_'+TEST_NAME, TEST_NAME, mesh_file)
259     le_cas.SetDirName(DIRCASE)
260     le_cas.AddBoundary(cao_name)
261     #
262     # Creation of the iterations
263     # ==========================
264     if VERBOSE :
265       option = 2
266     else :
267       option = 1
268     #
269     for niter in range(1, N_ITER_TEST_FILE+1):
270       if VERBOSE :
271         print(". Itération numéro %d" % niter)
272       iter_name = "I_" + TEST_NAME + "_%02d" % niter
273       if ( niter == 1 ) :
274         l_iteration = le_cas.NextIteration(iter_name)
275       else :
276         l_iteration = l_iteration.NextIteration(iter_name)
277       l_iteration.SetMeshName(TEST_NAME)
278       mesh_file = os.path.join(DIRCASE, "maill.%02d.med" % niter)
279       l_iteration.SetMeshFile(mesh_file)
280       l_iteration.AssociateHypo(hyponame)
281       error = l_iteration.Compute(1, option)
282       if error :
283         error = niter
284         break
285     #
286     break
287
288   return error
289 #
290 #==========================  Fin de la fonction ==================================
291
292 # CAO and Mesh
293
294 try :
295   ERROR, XAO_FILE, MESH_FILE = create_cao_smesh()
296   if ERROR :
297     raise Exception('Pb in create_cao_smesh')
298 except RuntimeError as eee:
299   raise Exception('Pb in create_cao_smesh: '+str(eee.message))
300
301 HOMARD = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
302 assert HOMARD is not None, "Impossible to load homard engine"
303 HOMARD.SetLanguageShort("fr")
304
305 # Exec of HOMARD-SALOME
306
307 try :
308   ERROR = homard_exec(XAO_FILE, MESH_FILE)
309   if ERROR :
310     raise Exception('Pb in homard_exec at iteration %d' %ERROR )
311 except RuntimeError as eee:
312   raise Exception('Pb in homard_exec: '+str(eee.message))
313
314 # Test of the results
315
316 N_REP_TEST_FILE = N_ITER_TEST_FILE
317 DESTROY_DIR = not DEBUG
318 test_results(REP_DATA, TEST_NAME, DIRCASE, N_ITER_TEST_FILE, N_REP_TEST_FILE, DESTROY_DIR)
319
320 if salome.sg.hasDesktop():
321   salome.sg.updateObjBrowser()