Salome HOME
Filtrage des suppressions de maillage pour une suppression d'itération
[modules/homard.git] / tests / test_3.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2012  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.5"
26
27 #========================================================================
28 Test_Name = "test_3"
29 n_boucle = 2
30 n_iter_test_file = 2
31 #========================================================================
32 import os
33 import sys
34 import tempfile
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_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_1 = homard.CreateBoundaryDi('courbes', 'COURBES', os.path.join(Rep_Test, Test_Name + '.fr.med'))
83 #
84 # Creation of the external cylinder
85     Boundary_2 = homard.CreateBoundaryCylinder('cyl_ext', 50.0, 25., -25., 1., 0., 0., 100.)
86 #
87 # Creation of the internal cylinder
88     Boundary_3 = homard.CreateBoundaryCylinder('cyl_int', 50.0, 25., -25., 1., 0., 0., 50.)
89 #
90 # Creation of the first sphere
91     Boundary_4 = homard.CreateBoundarySphere('sphere_1', 50.0, 25., -25., 100.)
92 #
93 # Creation of the second sphere
94     Boundary_5 = homard.CreateBoundarySphere('sphere_2', 450.0, 25., -25., 100.)
95 #
96 # Creation of the hypotheses
97 # ==========================
98 # Uniform refinement
99     Hypo = homard.CreateHypothesis('Hypo')
100     Hypo.SetAdapRefinUnRef(-1, 1, 0)
101 #
102 # Creation of the cases
103 # =====================
104 # Creation of the case Case_1
105     Case_1 = homard.CreateCase('Case_1', 'MOYEU', os.path.join(Rep_Test, Test_Name + '.00.med'))
106     Case_1.SetDirName(Rep_Test_Resu)
107     Case_1.SetConfType(1)
108     Case_1.AddBoundaryGroup('courbes', '')
109     Case_1.AddBoundaryGroup('cyl_ext', 'EXT')
110     Case_1.AddBoundaryGroup('cyl_int', 'INT')
111     Case_1.AddBoundaryGroup('sphere_1', 'END_1')
112     Case_1.AddBoundaryGroup('sphere_2', 'END_2')
113 #
114 # Creation and destruction of the iterations
115 # ==========================================
116 #
117     for iaux in range (n_boucle+1) :
118 #
119   # Creation of the iteration Iter_1
120       Iter_1 = Case_1.NextIteration('Iter_1')
121       Iter_1.SetMeshName('MOYEU_1')
122       Iter_1.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.01.med'))
123       Iter_1.AssociateHypo('Hypo')
124       error = Iter_1.Compute(1)
125       if error :
126         error = 10*iaux + 1
127         break
128
129   # Creation of the iteration Iter_2
130       Iter_2 = Iter_1.NextIteration('Iter_2')
131       Iter_2.SetMeshName('MOYEU_2')
132       Iter_2.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.02.med'))
133       Iter_2.AssociateHypo('Hypo')
134       error = Iter_2.Compute(1)
135       if error :
136         error = 10*iaux + 2
137         break
138
139   # Destruction
140       if ( iaux < n_boucle ) :
141   # Recursive destruction of the iterations
142         error = Iter_1.Delete(1)
143         if error :
144           error = 10*iaux + 3
145           break
146   # Destruction and creation of the hypothese
147         if ( iaux == 1 ) :
148           error = Hypo.Delete()
149           if error :
150             error = 10*iaux + 4
151             break
152           Hypo = homard.CreateHypothesis('Hypo')
153           Hypo.SetAdapRefinUnRef(-1, 1, 0)
154 #
155     break
156 #
157   return error
158
159 #========================================================================
160
161 homard = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
162 assert homard is not None, "Impossible to load homard engine"
163
164 #
165 # Exec of HOMARD-SALOME
166 #
167 try :
168   error_main = homard_exec(salome.myStudy)
169   if error_main :
170     raise Exception('Pb in homard_exec at iteration %d' %error_main )
171 except Exception, e:
172   raise Exception('Pb in homard_exec: '+e.message)
173
174 #
175 # Test of the result
176 #
177 test_file_suff = "apad.%02d.bilan" % n_iter_test_file
178 rep_test_file = "I%02d" % (n_iter_test_file*(n_boucle+1))
179 #
180 test_file = os.path.join(Rep_Test, Test_Name + "." + test_file_suff)
181 mess_error_ref = "\nReference file: " + test_file
182 try :
183   file = open (test_file, "r")
184   mess_ref = file.readlines()
185   file.close()
186 except :
187   mess_error = mess_error_ref + "\nThis file does not exist.\n"
188   raise Exception(mess_error)
189   sys.exit(2)
190 #
191 test_file = os.path.join(Rep_Test_Resu, rep_test_file, test_file_suff)
192 if os.path.isfile (test_file) :
193    file = open (test_file, "r")
194    mess = file.readlines()
195    file.close()
196 else :
197   mess_error  = "\nResult file: " + test_file
198   mess_error += "\nThis file does not exist.\n"
199   raise Exception(mess_error)
200   sys.exit(2)
201
202 nblign = len(mess_ref)
203 if ( len(mess) != nblign ):
204   mess_error = mess_error_ref +  "\nResult file: " + test_file
205   mess_error += "\nThe number of lines of the files are not the same.\n"
206   raise Exception(mess_error)
207   sys.exit(2)
208
209 for num in range(nblign) :
210    if (( "creation" not in mess_ref[num] ) and ( mess_ref[num] != mess[num])) :
211        message_erreur = "\nRefe : " + mess_ref[num]
212        message_erreur += "Test : " + mess[num][:-1]
213        message_erreur += "\nThe test is different from the reference."
214        raise Exception(message_erreur)
215        sys.exit(10)
216 #
217 remove_dir(Rep_Test_Resu)
218 #
219 if salome.sg.hasDesktop():
220   salome.sg.updateObjBrowser(1)
221   iparameters.getSession().restoreVisualState(1)
222