]> SALOME platform Git repositories - modules/homard.git/blob - tests/test_3.py
Salome HOME
Reorganisation des cas-tests et des tutoriaux
[modules/homard.git] / tests / test_3.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2013  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.
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 Copyright EDF-R&D 2011, 2013
23 Test test_3
24 """
25 __revision__ = "V1.12"
26
27 #========================================================================
28 Test_Name = "test_3"
29 n_boucle = 2
30 n_iter_test_file = 2
31 #========================================================================
32 import os
33 import tempfile
34 import HOMARD
35 import salome
36 #
37 pathHomard = os.getenv('HOMARD_ROOT_DIR')
38 Rep_Test = os.path.join(pathHomard, "share", "salome", "resources", "homard")
39 Rep_Test = os.path.normpath(Rep_Test)
40 Rep_Test_Resu = tempfile.mktemp()
41 os.mkdir(Rep_Test_Resu)
42
43 salome.salome_init()
44 import iparameters
45 ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters("Interface Applicative", 1))
46 ipar.append("AP_MODULES_LIST", "Homard")
47 #========================================================================
48 #========================================================================
49 def remove_dir(directory) :
50   """
51 Empties, then removes a directory.
52 Copyright EDF-R&D 2013
53   """
54 #
55   l_aux = os.listdir(directory)
56   for fic in l_aux :
57     fic_a = os.path.join(directory, fic)
58     if os.path.isdir(fic_a) :
59       remove_dir(fic_a)
60     else :
61       os.remove(fic_a)
62   os.rmdir(directory)
63 #
64   return
65 #
66 #========================================================================
67 #========================================================================
68 def homard_exec(theStudy):
69   """
70 Python script for HOMARD
71 Copyright EDF-R&D 2010, 2013
72   """
73   error = 0
74 #
75   while not error :
76 #
77     homard.SetCurrentStudy(theStudy)
78 #
79 # Creation of the boundaries
80 # ==========================
81 # Creation of the discrete boundary
82     Boundary_3_1 = homard.CreateBoundaryDi('courbes', 'COURBES', os.path.join(Rep_Test, Test_Name + '.fr.med'))
83 #
84 # Creation of the external cylinder
85     Boundary_3_2 = homard.CreateBoundaryCylinder('cyl_ext', 50.0, 25., -25., 1., 0., 0., 100.)
86 #
87 # Creation of the internal cylinder
88     Boundary_3_3 = homard.CreateBoundaryCylinder('cyl_int', 50.0, 25., -25., 1., 0., 0., 50.)
89 #
90 # Creation of the first sphere
91     Boundary_3_4 = homard.CreateBoundarySphere('sphere_1', 50.0, 25., -25., 100.)
92 #
93 # Creation of the second sphere
94     Boundary_3_5 = homard.CreateBoundarySphere('sphere_2', 450.0, 25., -25., 100.)
95 #
96 # Creation of the hypotheses
97 # ==========================
98 # Uniform refinement
99     HypoName = "Hypo_" + Test_Name
100     print "-------- Creation of the hypothesis", HypoName
101     Hypo_test_3 = homard.CreateHypothesis(HypoName)
102     Hypo_test_3.SetAdapRefinUnRef(-1, 1, 0)
103     print HypoName, " : zones utilisées :", Hypo_test_3.GetZones()
104     print HypoName, " : champ utilisé :", Hypo_test_3.GetFieldName()
105     print HypoName, " : composantes utilisées :", Hypo_test_3.GetComps()
106 #
107     for num in range (n_boucle+1) :
108 #
109       print "-------- num =", num, "--------"
110 #
111 # Creation of the case Case_test_3
112 # ===========================
113       if ( num <= 1 ) :
114         CaseName = "Case_" + Test_Name
115         print "-------- Creation of the case", CaseName
116         MeshFile = os.path.join(Rep_Test, Test_Name + '.00.med')
117         Case_test_3 = homard.CreateCase(CaseName, 'MOYEU', MeshFile)
118         Case_test_3.SetDirName(Rep_Test_Resu)
119         Case_test_3.SetConfType(1)
120         Case_test_3.AddBoundaryGroup('courbes', '')
121         Case_test_3.AddBoundaryGroup('cyl_ext', 'EXT')
122         Case_test_3.AddBoundaryGroup('cyl_int', 'INT')
123         Case_test_3.AddBoundaryGroup('sphere_1', 'END_1')
124         Case_test_3.AddBoundaryGroup('sphere_2', 'END_2')
125 #
126 # Creation of the iterations
127 # ==========================
128   # Creation of the iteration  1
129       IterName = "I_" + Test_Name + "_1"
130       print "-------- Creation of the iteration", IterName
131       Iter_test_3_1 = Case_test_3.NextIteration(IterName)
132       Iter_test_3_1.SetMeshName('MOYEU_1')
133       Iter_test_3_1.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.01.med'))
134       Iter_test_3_1.AssociateHypo('Hypo_test_3')
135       error = Iter_test_3_1.Compute(1, 1)
136       if error :
137         error = 10*num + 1
138         break
139
140   # Creation of the iteration 2
141       IterName = "I_" + Test_Name + "_2"
142       print "-------- Creation of the iteration", IterName
143       Iter_test_3_2 = Iter_test_3_1.NextIteration(IterName)
144       Iter_test_3_2.SetMeshName('MOYEU_2')
145       Iter_test_3_2.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.02.med'))
146       Iter_test_3_2.AssociateHypo('Hypo_test_3')
147       error = Iter_test_3_2.Compute(1, 1)
148       if error :
149         error = 10*num + 2
150         break
151   #
152   # Creation of the schema YACS
153   # ===========================
154       ScriptFile = os.path.join(pathHomard, "share", "doc", "salome", "gui", "HOMARD", "en", "_downloads", "yacs_script.py")
155       ScriptFile = os.path.normpath(ScriptFile)
156       DirName = Rep_Test_Resu
157       YACSName = "YACS_" + Test_Name
158       print "-------- Creation of the schema", YACSName
159       YACS_test_3 = Case_test_3.CreateYACSSchema(YACSName, ScriptFile, DirName, MeshFile)
160       YACS_test_3.SetType(2)
161       error = YACS_test_3.Write()
162       if error :
163         error = 10*num + 5
164         break
165
166   # Destructions
167   # ============
168   # Destruction of the schema, sauf a la fin
169       if ( num < n_boucle ) :
170         print "-------- Destruction of the schema", YACS_test_3.GetName()
171         error = YACS_test_3.Delete(1)
172         if error :
173           error = 10*num + 6
174           break
175   # After the first loop, the case is deleted, except the final mesh files
176   # All the iterations are deleted
177       if ( num == 0 ) :
178         print "-------- Destruction of the case", Case_test_3.GetName()
179         error = Case_test_3.Delete(0)
180         if error :
181           break
182   # After the second loop, the iterations are deleted, with the final mesh files
183       elif ( num == 1 ) :
184   # Recursive destruction of the iterations
185         print "-------- Recursive destruction of the iteration", Iter_test_3_1.GetName()
186         error = Iter_test_3_1.Delete(1)
187         if error :
188           error = 10*num + 3
189           break
190   # Destruction and creation of the hypothese
191         if ( num == 1 ) :
192           print "-------- Destruction of the hypothese", Hypo_test_3.GetName()
193           error = Hypo_test_3.Delete()
194           if error :
195             error = 10*num + 4
196             break
197           HypoName = "Hypo_test_3"
198           print "-------- Creation of the hypothesis", HypoName
199           Hypo_test_3 = homard.CreateHypothesis(HypoName)
200           Hypo_test_3.SetAdapRefinUnRef(-1, 1, 0)
201 #
202     break
203 #
204   return error
205
206 #========================================================================
207
208 homard = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
209 assert homard is not None, "Impossible to load homard engine"
210 #
211 # Exec of HOMARD-SALOME
212 #
213 try :
214   error_main = homard_exec(salome.myStudy)
215   if error_main :
216     raise Exception('Pb in homard_exec at iteration %d' %error_main )
217 except Exception, e:
218   raise Exception('Pb in homard_exec: '+e.message)
219 #
220 # Test of the result
221 #
222 test_file_suff = "apad.%02d.bilan" % n_iter_test_file
223 rep_test_file = "I%02d" % (n_iter_test_file*n_boucle)
224 #
225 test_file = os.path.join(Rep_Test, Test_Name + "." + test_file_suff)
226 mess_error_ref = "\nReference file: " + test_file
227 try :
228   file = open (test_file, "r")
229   mess_ref = file.readlines()
230   file.close()
231 except :
232   mess_error = mess_error_ref + "\nThis file does not exist.\n"
233   raise Exception(mess_error)
234 #
235 test_file = os.path.join(Rep_Test_Resu, rep_test_file, test_file_suff)
236 if os.path.isfile (test_file) :
237   file = open (test_file, "r")
238   mess = file.readlines()
239   file.close()
240 else :
241   mess_error  = "\nResult file: " + test_file
242   mess_error += "\nThis file does not exist.\n"
243   raise Exception(mess_error)
244
245 nblign = len(mess_ref)
246 if ( len(mess) != nblign ):
247   mess_error = mess_error_ref +  "\nResult file: " + test_file
248   mess_error += "\nThe number of lines of the files are not the same.\n"
249   raise Exception(mess_error)
250
251 for num in range(nblign) :
252   if (( "creation" not in mess_ref[num] ) and ( mess_ref[num] != mess[num])) :
253     message_erreur = "\nRefe : " + mess_ref[num]
254     message_erreur += "Test : " + mess[num][:-1]
255     message_erreur += "\nThe test is different from the reference."
256     raise Exception(message_erreur)
257 #
258 remove_dir(Rep_Test_Resu)
259 #
260 if salome.sg.hasDesktop():
261   salome.sg.updateObjBrowser(1)
262   iparameters.getSession().restoreVisualState(1)
263