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