Salome HOME
90dd3952bfda9a4580c1646d5ee018b877dfa21f
[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.9"
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_3"
100     print "-------- Creation of the hypothesis", HypoName
101     Hypo_3 = homard.CreateHypothesis(HypoName)
102     Hypo_3.SetAdapRefinUnRef(-1, 1, 0)
103 #
104     for num in range (n_boucle+1) :
105 #
106       print "-------- num =", num, "--------"
107 #
108 # Creation of the case Case_3
109 # ===========================
110       if ( num <= 1 ) :
111         CaseName = "Case_3"
112         print "-------- Creation of the case", CaseName
113         MeshFile = os.path.join(Rep_Test, Test_Name + '.00.med')
114         Case_3 = homard.CreateCase(CaseName, 'MOYEU', MeshFile)
115         Case_3.SetDirName(Rep_Test_Resu)
116         Case_3.SetConfType(1)
117         Case_3.AddBoundaryGroup('courbes', '')
118         Case_3.AddBoundaryGroup('cyl_ext', 'EXT')
119         Case_3.AddBoundaryGroup('cyl_int', 'INT')
120         Case_3.AddBoundaryGroup('sphere_1', 'END_1')
121         Case_3.AddBoundaryGroup('sphere_2', 'END_2')
122 #
123 # Creation of the iterations
124 # ==========================
125   # Creation of the iteration Iter_3_1
126       IterName = "Iter_3_1"
127       print "-------- Creation of the iteration", IterName
128       Iter_3_1 = Case_3.NextIteration(IterName)
129       Iter_3_1.SetMeshName('MOYEU_1')
130       Iter_3_1.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.01.med'))
131       Iter_3_1.AssociateHypo('Hypo_3')
132       error = Iter_3_1.Compute(1, 1)
133       if error :
134         error = 10*num + 1
135         break
136
137   # Creation of the iteration Iter_3_2Iter_3_1
138       IterName = "Iter_3_2"
139       print "-------- Creation of the iteration", IterName
140       Iter_3_2 = Iter_3_1.NextIteration(IterName)
141       Iter_3_2.SetMeshName('MOYEU_2')
142       Iter_3_2.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.02.med'))
143       Iter_3_2.AssociateHypo('Hypo_3')
144       error = Iter_3_2.Compute(1, 1)
145       if error :
146         error = 10*num + 2
147         break
148   #
149   # Creation of the schema YACS
150   # ===========================
151       ScriptFile = os.path.join(pathHomard, "share", "doc", "salome", "gui", "HOMARD", "en", "_downloads", "yacs_script.py")
152       ScriptFile = os.path.normpath(ScriptFile)
153       DirName = Rep_Test_Resu
154       YACSName = "YACS_3"
155       print "-------- Creation of the schema", YACSName
156       YACS_3 = Case_3.CreateYACSSchema(YACSName, ScriptFile, DirName, MeshFile)
157       error = YACS_3.Write()
158       if error :
159         error = 10*num + 5
160         break
161
162   # Destructions
163   # ============
164   # Destruction of the schema, sauf a la fin
165       if ( num < n_boucle ) :
166         print "-------- Destruction of the schema", YACS_3.GetName()
167         error = YACS_3.Delete(1)
168         if error :
169           error = 10*num + 6
170           break
171   # After the first loop, the case is deleted, except the final mesh files
172   # All the iterations are deleted
173       if ( num == 0 ) :
174         print "-------- Destruction of the case", Case_3.GetName()
175         error = Case_3.Delete(0)
176         if error :
177           break
178   # After the second loop, the iterations are deleted, with the final mesh files
179       elif ( num == 1 ) :
180   # Recursive destruction of the iterations
181         print "-------- Recursive destruction of the iteration", Iter_3_1.GetName()
182         error = Iter_3_1.Delete(1)
183         if error :
184           error = 10*num + 3
185           break
186   # Destruction and creation of the hypothese
187         if ( num == 1 ) :
188           print "-------- Destruction of the hypothese", Hypo_3.GetName()
189           error = Hypo_3.Delete()
190           if error :
191             error = 10*num + 4
192             break
193           HypoName = "Hypo_3"
194           print "-------- Creation of the hypothesis", HypoName
195           Hypo_3 = homard.CreateHypothesis(HypoName)
196           Hypo_3.SetAdapRefinUnRef(-1, 1, 0)
197 #
198     break
199 #
200   return error
201
202 #========================================================================
203
204 homard = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
205 assert homard is not None, "Impossible to load homard engine"
206 #
207 # Exec of HOMARD-SALOME
208 #
209 try :
210   error_main = homard_exec(salome.myStudy)
211   if error_main :
212     raise Exception('Pb in homard_exec at iteration %d' %error_main )
213 except Exception, e:
214   raise Exception('Pb in homard_exec: '+e.message)
215 #
216 # Test of the result
217 #
218 test_file_suff = "apad.%02d.bilan" % n_iter_test_file
219 rep_test_file = "I%02d" % (n_iter_test_file*n_boucle)
220 #
221 test_file = os.path.join(Rep_Test, Test_Name + "." + test_file_suff)
222 mess_error_ref = "\nReference file: " + test_file
223 try :
224   file = open (test_file, "r")
225   mess_ref = file.readlines()
226   file.close()
227 except :
228   mess_error = mess_error_ref + "\nThis file does not exist.\n"
229   raise Exception(mess_error)
230 #
231 test_file = os.path.join(Rep_Test_Resu, rep_test_file, test_file_suff)
232 if os.path.isfile (test_file) :
233   file = open (test_file, "r")
234   mess = file.readlines()
235   file.close()
236 else :
237   mess_error  = "\nResult file: " + test_file
238   mess_error += "\nThis file does not exist.\n"
239   raise Exception(mess_error)
240
241 nblign = len(mess_ref)
242 if ( len(mess) != nblign ):
243   mess_error = mess_error_ref +  "\nResult file: " + test_file
244   mess_error += "\nThe number of lines of the files are not the same.\n"
245   raise Exception(mess_error)
246
247 for num in range(nblign) :
248   if (( "creation" not in mess_ref[num] ) and ( mess_ref[num] != mess[num])) :
249     message_erreur = "\nRefe : " + mess_ref[num]
250     message_erreur += "Test : " + mess[num][:-1]
251     message_erreur += "\nThe test is different from the reference."
252     raise Exception(message_erreur)
253 #
254 remove_dir(Rep_Test_Resu)
255 #
256 if salome.sg.hasDesktop():
257   salome.sg.updateObjBrowser(1)
258   iparameters.getSession().restoreVisualState(1)
259