]> SALOME platform Git repositories - modules/homard.git/blob - tests/test_1.py
Salome HOME
Reorganisation des cas-tests et des tutoriaux
[modules/homard.git] / tests / test_1.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_1
24 """
25 __revision__ = "V1.13"
26
27 #========================================================================
28 Test_Name = "test_1"
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 zones
79   # =====================
80   # Creation of the box Zone_1_1
81     Zone_1_1 = homard.CreateZoneBox('Zone_1_1', -0.01, 1.01, -0.01, 0.4, -0.01, 0.6)
82
83   # Creation of the sphere Zone_1_2
84     Zone_1_2 = homard.CreateZoneSphere('Zone_1_2', 0.5, 0.6, 0.7, 0.75)
85   #
86   # Creation of the hypotheses
87   # ==========================
88   # Creation of the hypothesis a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM
89     HypoName_1 = "a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM"
90     print "-------- Creation of the hypothesis", HypoName_1
91     Hypo_1_1 = homard.CreateHypothesis(HypoName_1)
92     Hypo_1_1.SetAdapRefinUnRef(1, 1, 0)
93     Hypo_1_1.SetField('RESU____ERRE_ELEM_SIGM__________')
94     Hypo_1_1.SetUseComp(0)
95     Hypo_1_1.AddComp('ERREST')
96     Hypo_1_1.SetRefinThr(3, 10.1)
97     Hypo_1_1.AddFieldInterp('RESU____DEPL____________________')
98     Hypo_1_1.AddFieldInterp('RESU____ERRE_ELEM_SIGM__________')
99     print HypoName_1, " : zones utilisées :", Hypo_1_1.GetZones()
100     print HypoName_1, " : champ utilisé :", Hypo_1_1.GetFieldName()
101     print HypoName_1, " : composantes utilisées :", Hypo_1_1.GetComps()
102     if ( len (Hypo_1_1.GetFieldName()) > 0 ) :
103       print ".. caractéristiques de l'adaptation :", Hypo_1_1.GetField()
104     print HypoName_1, " : champs interpolés :", Hypo_1_1.GetFieldInterps()
105   # Creation of the hypothesis Zones_1_et_2
106     HypoName_2 = "Zones_1_et_2"
107     print "-------- Creation of the hypothesis", HypoName_2
108     Zones_1_et_2 = homard.CreateHypothesis(HypoName_2)
109     Zones_1_et_2.SetAdapRefinUnRef(0, 1, 0)
110     Zones_1_et_2.AddZone('Zone_1_1', 1)
111     Zones_1_et_2.AddZone('Zone_1_2', 1)
112     print HypoName_2, " : zones utilisées :", Zones_1_et_2.GetZones()
113     print HypoName_2, " : champ utilisé :", Zones_1_et_2.GetFieldName()
114     if ( len (Zones_1_et_2.GetFieldName()) > 0 ) :
115       print ".. caractéristiques de l'adaptation :", Zones_1_et_2.GetField()
116     print HypoName_2, " : champs interpolés :", Zones_1_et_2.GetFieldInterps()
117   #
118   # Creation of the cases
119   # =====================
120     # Creation of the case
121     CaseName = "Case_" + Test_Name
122     print "-------- Creation of the case", CaseName
123     MeshFile = os.path.join(Rep_Test, Test_Name + '.00.med')
124     Case_test_1 = homard.CreateCase(CaseName, 'MAILL', MeshFile)
125     Case_test_1.SetDirName(Rep_Test_Resu)
126     Case_test_1.SetConfType(1)
127   #
128   # Creation of the iterations
129   # ==========================
130   # Creation of the iteration 1
131     IterName = "I_" + Test_Name + "_1"
132     print "-------- Creation of the iteration", IterName
133     Iter_test_1_1 = Case_test_1.NextIteration(IterName)
134     Iter_test_1_1.AssociateHypo(HypoName_1)
135     print ". Hypothese :", HypoName_1
136     Iter_test_1_1.SetMeshName('M1')
137     Iter_test_1_1.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.01.med'))
138     Iter_test_1_1.SetFieldFile(os.path.join(Rep_Test, Test_Name + '.00.med'))
139     Iter_test_1_1.SetTimeStepRank(1, 1)
140     Iter_test_1_1.SetFieldInterpTimeStep('RESU____DEPL____________________', 1)
141     Iter_test_1_1.SetFieldInterpTimeStepRank('RESU____ERRE_ELEM_SIGM__________', 1, 1)
142     print ". Instants d'interpolation :", Iter_test_1_1.GetFieldInterpsTimeStepRank()
143     error = Iter_test_1_1.Compute(1, 1)
144     if error :
145       error = 1
146       break
147
148   # Creation of the iteration 2
149     IterName = "I_" + Test_Name + "_2"
150     print "-------- Creation of the iteration", IterName
151     Iter_test_1_2 = Iter_test_1_1.NextIteration(IterName)
152     Iter_test_1_2.AssociateHypo(HypoName_1)
153     print ". Hypothese :", HypoName_1
154     Iter_test_1_2.SetMeshName('M2')
155     Iter_test_1_2.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.02.med'))
156     Iter_test_1_2.SetFieldFile(os.path.join(Rep_Test, Test_Name + '.01.med'))
157     Iter_test_1_2.SetTimeStepRank(1, 1)
158     Iter_test_1_2.SetFieldInterpTimeStep('RESU____DEPL____________________', 1)
159     Iter_test_1_2.SetFieldInterpTimeStepRank('RESU____ERRE_ELEM_SIGM__________', 1, 1)
160     print ". Instants d'interpolation :", Iter_test_1_2.GetFieldInterpsTimeStepRank()
161     error = Iter_test_1_2.Compute(1, 1)
162     if error :
163       error = 2
164       break
165
166   # Creation of the iteration 3
167     IterName = "I_" + Test_Name + "_3"
168     print "-------- Creation of the iteration", IterName
169     Iter_test_1_3 = Iter_test_1_2.NextIteration(IterName)
170     Iter_test_1_3.AssociateHypo(HypoName_2)
171     print ". Hypothese :", HypoName_2
172     Iter_test_1_3.SetMeshName('M3')
173     Iter_test_1_3.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.03.med'))
174     Iter_test_1_2.SetFieldFile(os.path.join(Rep_Test, Test_Name + '.02.med'))
175     print ". Instants d'interpolation :", Iter_test_1_3.GetFieldInterpsTimeStepRank()
176     error = Iter_test_1_3.Compute(1, 1)
177     if error :
178       error = 3
179       break
180   #
181   # Creation of the schema YACS
182   # ===========================
183     ScriptFile = os.path.join(pathHomard, "share", "doc", "salome", "gui", "HOMARD", "en", "_downloads", "yacs_script.py")
184     ScriptFile = os.path.normpath(ScriptFile)
185     DirName = Rep_Test_Resu
186     YACS_test_1 = Case_test_1.CreateYACSSchema("YACS_test_1", ScriptFile, DirName, MeshFile)
187     error = YACS_test_1.Write()
188     if error :
189       error = 4
190       break
191   #
192     break
193   #
194   return error
195
196 #========================================================================
197
198 homard = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
199 assert homard is not None, "Impossible to load homard engine"
200
201 #
202 # Exec of HOMARD-SALOME
203 #
204 try :
205   error_main = homard_exec(salome.myStudy)
206   if error_main :
207     raise Exception('Pb in homard_exec at iteration %d' %error_main )
208 except Exception, e:
209   raise Exception('Pb in homard_exec: '+e.message)
210 #
211 # Test of the result
212 #
213 test_file_suff = "apad.%02d.bilan" % n_iter_test_file
214 rep_test_file = "I%02d" % n_iter_test_file
215 #
216 test_file = os.path.join(Rep_Test, Test_Name + "." + test_file_suff)
217 mess_error_ref = "\nReference file: " + test_file
218 try :
219   file = open (test_file, "r")
220   mess_ref = file.readlines()
221   file.close()
222 except :
223   mess_error = mess_error_ref + "\nThis file does not exist.\n"
224   raise Exception(mess_error)
225 #
226 test_file = os.path.join(Rep_Test_Resu, rep_test_file, test_file_suff)
227 if os.path.isfile (test_file) :
228   file = open (test_file, "r")
229   mess = file.readlines()
230   file.close()
231 else :
232   mess_error  = "\nResult file: " + test_file
233   mess_error += "\nThis file does not exist.\n"
234   raise Exception(mess_error)
235
236 nblign = len(mess_ref)
237 if ( len(mess) != nblign ):
238   mess_error = mess_error_ref +  "\nResult file: " + test_file
239   mess_error += "\nThe number of lines of the files are not the same.\n"
240   raise Exception(mess_error)
241
242 for num in range(nblign) :
243   if (( "creation" not in mess_ref[num] ) and ( mess_ref[num] != mess[num])) :
244     message_erreur = "\nRefe : " + mess_ref[num]
245     message_erreur += "Test : " + mess[num][:-1]
246     message_erreur += "\nThe test is different from the reference."
247     raise Exception(message_erreur)
248 #
249 remove_dir(Rep_Test_Resu)
250 #
251 if salome.sg.hasDesktop():
252   salome.sg.updateObjBrowser(1)
253   iparameters.getSession().restoreVisualState(1)
254