]> SALOME platform Git repositories - modules/homard.git/blob - tests/test_2.py
Salome HOME
Reorganisation des cas-tests et des tutoriaux
[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.12"
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 1
86     HypoName_1 = "Hypo_" + Test_Name + "_1"
87     print "-------- Creation of the hypothesis", HypoName_1
88     Hypo_test_2_1 = homard.CreateHypothesis(HypoName_1)
89     Hypo_test_2_1.SetAdapRefinUnRef(-1, 1, 0)
90     Hypo_test_2_1.AddGroup('EG')
91     Hypo_test_2_1.AddGroup('BANDE')
92     print HypoName_1, " : zones utilisées :", Hypo_test_2_1.GetZones()
93     print HypoName_1, " : champ utilisé :", Hypo_test_2_1.GetFieldName()
94     print HypoName_1, " : composantes utilisées :", Hypo_test_2_1.GetComps()
95     if ( len (Hypo_test_2_1.GetFieldName()) > 0 ) :
96       print ".. caractéristiques de l'adaptation :", Hypo_test_2_1.GetField()
97
98   # Creation of the hypothesis 2
99     HypoName_2 = "Hypo_" + Test_Name + "_2"
100     print "-------- Creation of the hypothesis", HypoName_2
101     Hypo_test_2_2 = homard.CreateHypothesis(HypoName_2)
102     Hypo_test_2_2.SetAdapRefinUnRef(-1, 1, 0)
103     Hypo_test_2_2.AddGroup('M_D')
104     print HypoName_2, " : zones utilisées :", Hypo_test_2_2.GetZones()
105     print HypoName_2, " : champ utilisé :", Hypo_test_2_2.GetFieldName()
106     print HypoName_2, " : composantes utilisées :", Hypo_test_2_2.GetComps()
107     if ( len (Hypo_test_2_2.GetFieldName()) > 0 ) :
108       print ".. caractéristiques de l'adaptation :", Hypo_test_2_2.GetField()
109   #
110   # Creation of the cases
111   # =====================
112     # Creation of the case
113     CaseName = "Case_" + Test_Name
114     MeshFile = os.path.join(Rep_Test, Test_Name + '.00.med')
115     Case_test_2 = homard.CreateCase(CaseName, 'PLAQUE_0', MeshFile)
116     Case_test_2.SetDirName(Rep_Test_Resu)
117     Case_test_2.SetConfType(1)
118     Case_test_2.AddBoundaryGroup('internal_boundary', '')
119   #
120   # Creation of the iterations
121   # ==========================
122   # Creation of the iteration 1
123     IterName = "I_" + Test_Name + "_1"
124     Iter_test_2_1 = Case_test_2.NextIteration(IterName)
125     Iter_test_2_1.SetMeshName('PLAQUE_1')
126     Iter_test_2_1.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.01.med'))
127     Iter_test_2_1.AssociateHypo(HypoName_1)
128     error = Iter_test_2_1.Compute(1, 1)
129     if error :
130       error = 1
131       break
132
133   # Creation of the iteration 2
134     IterName = "I_" + Test_Name + "_2"
135     Iter_test_2_2 = Iter_test_2_1.NextIteration(IterName)
136     Iter_test_2_2.SetMeshName('PLAQUE_2')
137     Iter_test_2_2.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.02.med'))
138     Iter_test_2_2.AssociateHypo(HypoName_1)
139     error = Iter_test_2_2.Compute(1, 1)
140     if error :
141       error = 2
142       break
143
144   # Creation of the iteration 3
145     IterName = "I_" + Test_Name + "_3"
146     Iter_test_2_3 = Iter_test_2_2.NextIteration(IterName)
147     Iter_test_2_3.SetMeshName('PLAQUE_3')
148     Iter_test_2_3.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.03.med'))
149     Iter_test_2_3.AssociateHypo(HypoName_2)
150     error = Iter_test_2_3.Compute(1, 1)
151     if error :
152       error = 3
153       break
154   #
155   # Creation of the schema YACS
156   # ===========================
157     ScriptFile = os.path.join(pathHomard, "share", "doc", "salome", "gui", "HOMARD", "en", "_downloads", "yacs_script.py")
158     ScriptFile = os.path.normpath(ScriptFile)
159     DirName = Rep_Test_Resu
160     YACS_test_2 = Case_test_2.CreateYACSSchema("YACS_test_2", ScriptFile, DirName, MeshFile)
161     YACS_test_2.SetType(1)
162     filexml = os.path.join(Rep_Test_Resu, 'YACS_test_2.xml')
163     error = YACS_test_2.WriteOnFile(filexml)
164     if error :
165       error = 4
166       break
167   #
168     break
169   #
170   return error
171
172 #========================================================================
173
174 homard = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
175 assert homard is not None, "Impossible to load homard engine"
176 #
177 # Exec of HOMARD-SALOME
178 #
179 try :
180   error_main = homard_exec(salome.myStudy)
181   if error_main :
182     raise Exception('Pb in homard_exec at iteration %d' %error_main )
183 except Exception, e:
184   raise Exception('Pb in homard_exec: '+e.message)
185
186 #
187 # Test of the result
188 #
189 test_file_suff = "apad.%02d.bilan" % n_iter_test_file
190 rep_test_file = "I%02d" % n_iter_test_file
191 #
192 test_file = os.path.join(Rep_Test, Test_Name + "." + test_file_suff)
193 mess_error_ref = "\nReference file: " + test_file
194 try :
195   file = open (test_file, "r")
196   mess_ref = file.readlines()
197   file.close()
198 except :
199   mess_error = mess_error_ref + "\nThis file does not exist.\n"
200   raise Exception(mess_error)
201 #
202 test_file = os.path.join(Rep_Test_Resu, rep_test_file, test_file_suff)
203 if os.path.isfile (test_file) :
204   file = open (test_file, "r")
205   mess = file.readlines()
206   file.close()
207 else :
208   mess_error  = "\nResult file: " + test_file
209   mess_error += "\nThis file does not exist.\n"
210   raise Exception(mess_error)
211
212 nblign = len(mess_ref)
213 if ( len(mess) != nblign ):
214   mess_error = mess_error_ref +  "\nResult file: " + test_file
215   mess_error += "\nThe number of lines of the files are not the same.\n"
216   raise Exception(mess_error)
217
218 for num in range(nblign) :
219   if (( "creation" not in mess_ref[num] ) and ( mess_ref[num] != mess[num])) :
220     message_erreur = "\nRefe : " + mess_ref[num]
221     message_erreur += "Test : " + mess[num][:-1]
222     message_erreur += "\nThe test is different from the reference."
223     raise Exception(message_erreur)
224 #
225 remove_dir(Rep_Test_Resu)
226 #
227 if salome.sg.hasDesktop():
228   salome.sg.updateObjBrowser(1)
229   iparameters.getSession().restoreVisualState(1)
230