Salome HOME
Merge from V6_main 13/12/2012
[modules/homard.git] / tests / test_1.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 2010
23 Test test_1
24 """
25 __revision__ = "V1.6"
26
27 ######################################################################################
28 Test_Name = "test_1"
29 n_iter_test_file = 3
30 ######################################################################################
31 import os
32 import sys
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_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 homard_exec(theStudy):
49   """
50 Python script for HOMARD
51 Copyright EDF-R&D 2010
52   """
53   error = 0
54 #
55   while not error :
56   #
57     homard.SetCurrentStudy(theStudy)
58   #
59   # Creation of the zones
60   # =====================
61   # Creation of the box Zone_1
62     Zone_1 = homard.CreateZoneBox('Zone_1', -0.01, 1.01, -0.01, 0.4, -0.01, 0.6)
63
64   # Creation of the sphere Zone_2
65     Zone_2 = homard.CreateZoneSphere('Zone_2', 0.5, 0.6, 0.7, 0.75)
66   #
67   # Creation of the hypotheses
68   # ==========================
69   # Creation of the hypothesis a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM
70     a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM = homard.CreateHypothesis('a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM')
71     a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM.SetAdapRefinUnRef(1, 1, 0)
72     a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM.SetField('RESU____ERRE_ELEM_SIGM__________')
73     a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM.SetUseComp(0)
74     a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM.AddComp('ERREST')
75     a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM.SetRefinThr(3, 10.1)
76     a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM.SetTypeFieldInterp(2)
77     a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM.AddFieldInterp('RESU____DEPL____________________')
78     a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM.AddFieldInterp('RESU____ERRE_ELEM_SIGM__________')
79   # Creation of the hypothesis Zones_1_et_2
80     Zones_1_et_2 = homard.CreateHypothesis('Zones_1_et_2')
81     Zones_1_et_2.SetAdapRefinUnRef(0, 1, 0)
82     homard.AssociateHypoZone('Zones_1_et_2', 'Zone_1', 1)
83     homard.AssociateHypoZone('Zones_1_et_2', 'Zone_2', 1)
84   #
85   # Creation of the cases
86   # =====================
87     # Creation of the case zzzz121b
88     Case_1 = homard.CreateCase('zzzz121b', 'MAILL', os.path.join(Rep_Test, Test_Name + '.00.med'))
89     Case_1.SetDirName(Rep_Test_Resu)
90     Case_1.SetConfType(1)
91   #
92   # Creation of the iterations
93   # ==========================
94   # Creation of the iteration I1
95     I1 = homard.CreateIteration('I1', Case_1.GetIter0Name() )
96     I1.SetMeshName('M1')
97     I1.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.01.med'))
98     I1.SetFieldFile(os.path.join(Rep_Test, Test_Name + '.00.med'))
99     I1.SetTimeStepRank(1, 1)
100     homard.AssociateIterHypo('I1', 'a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM')
101     error = I1.Compute(1)
102     if error :
103       error = 1
104       break
105
106   # Creation of the iteration I2
107     I2 = homard.CreateIteration('I2', 'I1')
108     I2.SetMeshName('M2')
109     I2.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.02.med'))
110     I2.SetFieldFile(os.path.join(Rep_Test, Test_Name + '.01.med'))
111     I2.SetTimeStepRank(1, 1)
112     homard.AssociateIterHypo('I2', 'a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM')
113     error = I2.Compute(1)
114     if error :
115       error = 2
116       break
117
118   # Creation of the iteration I3
119     I3 = homard.CreateIteration('I3', 'I2')
120     I3.SetMeshName('M3')
121     I3.SetMeshFile(os.path.join(Rep_Test_Resu, 'maill.03.med'))
122     homard.AssociateIterHypo('I3', 'Zones_1_et_2')
123     error = I3.Compute(1)
124     if error :
125       error = 3
126       break
127   #
128     break
129   #
130     return error
131
132 ######################################################################################
133
134 homard = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
135 assert homard is not None, "Impossible to load homard engine"
136
137 #
138 # Exec of HOMARD-SALOME
139 #
140 try :
141   error_main = homard_exec(salome.myStudy)
142   if error_main :
143     raise Exception('Pb in homard_exec at iteration %d' %error_main )
144 except Exception, e:
145   raise Exception('Pb in homard_exec: '+e.message)
146
147 #
148 # Test of the result
149 #
150 s_iter_test_file = str(n_iter_test_file)
151 test_file_suff = "apad.0" + s_iter_test_file + ".bilan"
152 rep_test_file = "I0" + s_iter_test_file
153 #
154 test_file = os.path.join(Rep_Test, Test_Name + "." + test_file_suff)
155 mess_error_ref = "\nReference file: " + test_file
156 try :
157   file = open (test_file, "r")
158   mess_ref = file.readlines()
159   file.close()
160 except :
161   mess_error = mess_error_ref + "\nThis file does not exist.\n"
162   raise Exception(mess_error)
163   sys.exit(2)
164 #
165 test_file = os.path.join(Rep_Test_Resu, rep_test_file, test_file_suff)
166 if os.path.isfile (test_file) :
167    file = open (test_file, "r")
168    mess = file.readlines()
169    file.close()
170 else :
171   mess_error  = "\nResult file: " + test_file
172   mess_error += "\nThis file does not exist.\n"
173   raise Exception(mess_error)
174   sys.exit(2)
175
176 nblign = len(mess_ref)
177 if ( len(mess) != nblign ):
178   mess_error = mess_error_ref +  "\nResult file: " + test_file
179   mess_error += "\nThe number of lines of the files are not the same.\n"
180   raise Exception(mess_error)
181   sys.exit(2)
182
183 for num in range(nblign) :
184    if (( "creation" not in mess_ref[num] ) and ( mess_ref[num] != mess[num])) :
185        message_erreur = "\nRefe : " + mess_ref[num]
186        message_erreur += "Test : " + mess[num][:-1]
187        message_erreur += "\nThe test is different from the reference."
188        raise Exception(message_erreur)
189        sys.exit(10)
190 #
191 if salome.sg.hasDesktop():
192   salome.sg.updateObjBrowser(1)
193   iparameters.getSession().restoreVisualState(1)
194