Salome HOME
Correction pour les types des schémas.
[modules/homard.git] / tests / test_2.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 2010, 2013
23 Test test_2
24 """
25 __revision__ = "V1.10"
26
27 #========================================================================
28 Test_Name = "test_2"
29 n_iter_test_file = 3
30 #========================================================================
31 import os
32 import tempfile
33 import HOMARD
34 import salome
35 #
36 pathHomard = os.getenv('HOMARD_ROOT_DIR')
37 Rep_Test = os.path.join(pathHomard, "share", "salome", "resources", "homard")
38 Rep_Test = os.path.normpath(Rep_Test)
39 Rep_Test_Resu = tempfile.mktemp()
40 os.mkdir(Rep_Test_Resu)
41
42 salome.salome_init()
43 import iparameters
44 ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters("Interface Applicative", 1))
45 ipar.append("AP_MODULES_LIST", "Homard")
46 #========================================================================
47 #========================================================================
48 def remove_dir(directory) :
49   """
50 Empties, then removes a directory.
51 Copyright EDF-R&D 2013
52   """
53 #
54   l_aux = os.listdir(directory)
55   for fic in l_aux :
56     fic_a = os.path.join(directory, fic)
57     if os.path.isdir(fic_a) :
58       remove_dir(fic_a)
59     else :
60       os.remove(fic_a)
61   os.rmdir(directory)
62 #
63   return
64 #
65 #========================================================================
66 #========================================================================
67 def homard_exec(theStudy):
68   """
69 Python script for HOMARD
70 Copyright EDF-R&D 2010, 2013
71   """
72   error = 0
73 #
74   while not error :
75   #
76     homard.SetCurrentStudy(theStudy)
77   #
78   # Creation of the boundaries
79   # ==========================
80   # Creation of the discrete boundary Boundary_1
81     Boundary_1 = homard.CreateBoundaryDi('internal_boundary', 'plaque', os.path.join(Rep_Test, Test_Name + '.fr.med'))
82   #
83   # Creation of the hypotheses
84   # ==========================
85   # Creation of the hypothesis Hypo_2_1
86     Hypo_2_1 = homard.CreateHypothesis('Hypo_2_1')
87     Hypo_2_1.SetAdapRefinUnRef(-1, 1, 0)
88     Hypo_2_1.AddGroup('EG')
89     Hypo_2_1.AddGroup('BANDE')
90
91   # Creation of the hypothesis Hypo_2_2
92     Hypo_2_2 = homard.CreateHypothesis('Hypo_2_2')
93     Hypo_2_2.SetAdapRefinUnRef(-1, 1, 0)
94     Hypo_2_2.AddGroup('M_D')
95   #
96   # Creation of the cases
97   # =====================
98     # Creation of the case Case_2
99     MeshFile = os.path.join(Rep_Test, Test_Name + '.00.med')
100     Case_2 = homard.CreateCase('Case_2', 'PLAQUE_0', MeshFile)
101     Case_2.SetDirName(Rep_Test_Resu)
102     Case_2.SetConfType(1)
103     Case_2.AddBoundaryGroup('internal_boundary', '')
104   #
105   # Creation of the iterations
106   # ==========================
107   # Creation of the iteration Iter_2_1
108     Iter_2_1 = Case_2.NextIteration('Iter_2_1')
109     Iter_2_1.SetMeshName('PLAQUE_1')
110     Iter_2_1.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.01.med'))
111     Iter_2_1.AssociateHypo('Hypo_2_1')
112     error = Iter_2_1.Compute(1, 1)
113     if error :
114       error = 1
115       break
116
117   # Creation of the iteration Iter_2_2
118     Iter_2_2 = Iter_2_1.NextIteration('Iter_2_2')
119     Iter_2_2.SetMeshName('PLAQUE_2')
120     Iter_2_2.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.02.med'))
121     Iter_2_2.AssociateHypo('Hypo_2_1')
122     error = Iter_2_2.Compute(1, 1)
123     if error :
124       error = 2
125       break
126
127   # Creation of the iteration Iter_2_3
128     Iter_2_3 = Iter_2_2.NextIteration('Iter_2_3')
129     Iter_2_3.SetMeshName('PLAQUE_3')
130     Iter_2_3.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.03.med'))
131     Iter_2_3.AssociateHypo('Hypo_2_2')
132     error = Iter_2_3.Compute(1, 1)
133     if error :
134       error = 3
135       break
136   #
137   # Creation of the schema YACS
138   # ===========================
139     ScriptFile = os.path.join(pathHomard, "share", "doc", "salome", "gui", "HOMARD", "en", "_downloads", "yacs_script.py")
140     ScriptFile = os.path.normpath(ScriptFile)
141     DirName = Rep_Test_Resu
142     YACS_2 = Case_2.CreateYACSSchema("YACS_2", ScriptFile, DirName, MeshFile)
143     YACS_2.SetType(1)
144     filexml = os.path.join(Rep_Test_Resu, 'YACS_2.xml')
145     error = YACS_2.WriteOnFile(filexml)
146     if error :
147       error = 4
148       break
149   #
150     break
151   #
152   return error
153
154 #========================================================================
155
156 homard = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
157 assert homard is not None, "Impossible to load homard engine"
158 #
159 # Exec of HOMARD-SALOME
160 #
161 try :
162   error_main = homard_exec(salome.myStudy)
163   if error_main :
164     raise Exception('Pb in homard_exec at iteration %d' %error_main )
165 except Exception, e:
166   raise Exception('Pb in homard_exec: '+e.message)
167
168 #
169 # Test of the result
170 #
171 test_file_suff = "apad.%02d.bilan" % n_iter_test_file
172 rep_test_file = "I%02d" % n_iter_test_file
173 #
174 test_file = os.path.join(Rep_Test, Test_Name + "." + test_file_suff)
175 mess_error_ref = "\nReference file: " + test_file
176 try :
177   file = open (test_file, "r")
178   mess_ref = file.readlines()
179   file.close()
180 except :
181   mess_error = mess_error_ref + "\nThis file does not exist.\n"
182   raise Exception(mess_error)
183 #
184 test_file = os.path.join(Rep_Test_Resu, rep_test_file, test_file_suff)
185 if os.path.isfile (test_file) :
186   file = open (test_file, "r")
187   mess = file.readlines()
188   file.close()
189 else :
190   mess_error  = "\nResult file: " + test_file
191   mess_error += "\nThis file does not exist.\n"
192   raise Exception(mess_error)
193
194 nblign = len(mess_ref)
195 if ( len(mess) != nblign ):
196   mess_error = mess_error_ref +  "\nResult file: " + test_file
197   mess_error += "\nThe number of lines of the files are not the same.\n"
198   raise Exception(mess_error)
199
200 for num in range(nblign) :
201   if (( "creation" not in mess_ref[num] ) and ( mess_ref[num] != mess[num])) :
202     message_erreur = "\nRefe : " + mess_ref[num]
203     message_erreur += "Test : " + mess[num][:-1]
204     message_erreur += "\nThe test is different from the reference."
205     raise Exception(message_erreur)
206 #
207 remove_dir(Rep_Test_Resu)
208 #
209 if salome.sg.hasDesktop():
210   salome.sg.updateObjBrowser(1)
211   iparameters.getSession().restoreVisualState(1)
212