Salome HOME
Merge branch 'gn/evol'
[modules/homard.git] / tests / test_1.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2015  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, or (at your option) any later version.
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 Test test_1
23 """
24 __revision__ = "V2.6"
25
26 #========================================================================
27 Test_Name = "test_1"
28 debug=False
29 n_iter_test_file = 3
30 #========================================================================
31 import os
32 import tempfile
33 import sys
34 import HOMARD
35 import salome
36 #
37 # ==================================
38 pathHomard = os.getenv('HOMARD_ROOT_DIR')
39 # Repertoire des donnees du test
40 Rep_Test = os.path.join(pathHomard, "share", "salome", "resources", "homard")
41 Rep_Test = os.path.normpath(Rep_Test)
42 sys.path.append(Rep_Test)
43 from test_util import remove_dir
44 from test_util import test_results
45 # Repertoire des resultats
46 if debug :
47   dircase = os.path.join("/tmp", Test_Name)
48   if ( os.path.isdir(dircase) ) :
49     remove_dir(dircase)
50   os.mkdir(dircase)
51 else :
52   dircase = tempfile.mkdtemp()
53 # ==================================
54
55 salome.salome_init()
56 import iparameters
57 ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters("Interface Applicative", 1))
58 ipar.append("AP_MODULES_LIST", "Homard")
59 #
60 #========================================================================
61 #========================================================================
62 def homard_exec(theStudy):
63   """
64 Python script for HOMARD
65   """
66   error = 0
67 #
68   while not error :
69   #
70     homard.SetCurrentStudy(theStudy)
71   #
72   # Creation of the zones
73   # =====================
74   # Creation of the box Zone_1_1
75     Zone_1_1 = homard.CreateZoneBox('Zone_1_1', -0.01, 1.01, -0.01, 0.4, -0.01, 0.6)
76
77   # Creation of the sphere Zone_1_2
78     Zone_1_2 = homard.CreateZoneSphere('Zone_1_2', 0.5, 0.6, 0.7, 0.75)
79   #
80   # Creation of the hypotheses
81   # ==========================
82     dico = {}
83     dico["1"] = "raffinement"
84     dico["-1"] = "deraffinement"
85   # Creation of the hypothesis a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM
86     HypoName_1 = "a10_1pc_de_mailles_a_raffiner_sur_ERRE_ELEM_SIGM"
87     print "-------- Creation of the hypothesis", HypoName_1
88     Hypo_1_1 = homard.CreateHypothesis(HypoName_1)
89     Hypo_1_1.SetField('RESU____ERRE_ELEM_SIGM__________')
90     Hypo_1_1.SetUseComp(0)
91     Hypo_1_1.AddComp('ERREST')
92     Hypo_1_1.SetRefinThr(3, 10.1)
93     Hypo_1_1.AddFieldInterp('RESU____DEPL____________________')
94     Hypo_1_1.AddFieldInterp('RESU____ERRE_ELEM_SIGM__________')
95     print HypoName_1, " : champ utilisé :", Hypo_1_1.GetFieldName()
96     print HypoName_1, " : composantes utilisées :", Hypo_1_1.GetComps()
97     if ( len (Hypo_1_1.GetFieldName()) > 0 ) :
98       print ".. caractéristiques de l'adaptation :", Hypo_1_1.GetField()
99     print HypoName_1, " : champs interpolés :", Hypo_1_1.GetFieldInterps()
100   # Creation of the hypothesis Zones_1_et_2
101     HypoName_2 = "Zones_1_et_2"
102     print "-------- Creation of the hypothesis", HypoName_2
103     Zones_1_et_2 = homard.CreateHypothesis(HypoName_2)
104     Zones_1_et_2.AddZone('Zone_1_1', 1)
105     Zones_1_et_2.AddZone('Zone_1_2', 1)
106     laux = Zones_1_et_2.GetZones()
107     nbzone = len(laux)/2
108     jaux = 0
109     for iaux in range(nbzone) :
110       print HypoName_2, " : ", dico[laux[jaux+1]], "sur la zone", laux[jaux]
111       jaux += 2
112     print HypoName_2, " : champ utilisé :", Zones_1_et_2.GetFieldName()
113     if ( len (Zones_1_et_2.GetFieldName()) > 0 ) :
114       print ".. caractéristiques de l'adaptation :", Zones_1_et_2.GetField()
115     print HypoName_2, " : champs interpolés :", Zones_1_et_2.GetFieldInterps()
116   #
117   # Creation of the cases
118   # =====================
119     # Creation of the case
120     CaseName = "Case_" + Test_Name
121     print "-------- Creation of the case", CaseName
122     MeshFile = os.path.join(Rep_Test, Test_Name + '.00.med')
123     Case_test_1 = homard.CreateCase(CaseName, 'MAILL', MeshFile)
124     Case_test_1.SetDirName(dircase)
125   #
126   # Creation of the iterations
127   # ==========================
128   # Creation of the iteration 1
129     IterName = "I_" + Test_Name + "_1"
130     print "-------- Creation of the iteration", IterName
131     Iter_test_1_1 = Case_test_1.NextIteration(IterName)
132     Iter_test_1_1.AssociateHypo(HypoName_1)
133     print ". Hypothese :", HypoName_1
134     Iter_test_1_1.SetMeshName('M1')
135     Iter_test_1_1.SetMeshFile(os.path.join(dircase, 'maill.01.med'))
136     Iter_test_1_1.SetFieldFile(os.path.join(Rep_Test, Test_Name + '.00.med'))
137     Iter_test_1_1.SetTimeStepRank(1, 1)
138     Iter_test_1_1.SetFieldInterpTimeStep('RESU____DEPL____________________', 1)
139     Iter_test_1_1.SetFieldInterpTimeStepRank('RESU____ERRE_ELEM_SIGM__________', 1, 1)
140     print ". Instants d'interpolation :", Iter_test_1_1.GetFieldInterpsTimeStepRank()
141     error = Iter_test_1_1.Compute(1, 1)
142     if error :
143       error = 1
144       break
145
146   # Creation of the iteration 2
147     IterName = "I_" + Test_Name + "_2"
148     print "-------- Creation of the iteration", IterName
149     Iter_test_1_2 = Iter_test_1_1.NextIteration(IterName)
150     Iter_test_1_2.AssociateHypo(HypoName_1)
151     print ". Hypothese :", HypoName_1
152     Iter_test_1_2.SetMeshName('M2')
153     Iter_test_1_2.SetMeshFile(os.path.join(dircase, 'maill.02.med'))
154     Iter_test_1_2.SetFieldFile(os.path.join(Rep_Test, Test_Name + '.01.med'))
155     Iter_test_1_2.SetTimeStepRank(1, 1)
156     Iter_test_1_2.SetFieldInterpTimeStep('RESU____DEPL____________________', 1)
157     Iter_test_1_2.SetFieldInterpTimeStepRank('RESU____ERRE_ELEM_SIGM__________', 1, 1)
158     print ". Instants d'interpolation :", Iter_test_1_2.GetFieldInterpsTimeStepRank()
159     error = Iter_test_1_2.Compute(1, 1)
160     if error :
161       error = 2
162       break
163
164   # Creation of the iteration 3
165     IterName = "I_" + Test_Name + "_3"
166     print "-------- Creation of the iteration", IterName
167     Iter_test_1_3 = Iter_test_1_2.NextIteration(IterName)
168     Iter_test_1_3.AssociateHypo(HypoName_2)
169     print ". Hypothese :", HypoName_2
170     Iter_test_1_3.SetMeshName('M3')
171     Iter_test_1_3.SetMeshFile(os.path.join(dircase, 'maill.03.med'))
172     Iter_test_1_2.SetFieldFile(os.path.join(Rep_Test, Test_Name + '.02.med'))
173     print ". Instants d'interpolation :", Iter_test_1_3.GetFieldInterpsTimeStepRank()
174     error = Iter_test_1_3.Compute(1, 1)
175     if error :
176       error = 3
177       break
178   #
179   # Creation of the schema YACS
180   # ===========================
181     ScriptFile = os.path.join(pathHomard, "share", "doc", "salome", "gui", "HOMARD", "en", "_downloads", "yacs_script_test.py")
182     ScriptFile = os.path.normpath(ScriptFile)
183     DirName = dircase
184     YACS_test_1 = Case_test_1.CreateYACSSchema("YACS_test_1", ScriptFile, DirName, MeshFile)
185     error = YACS_test_1.Write()
186     if error :
187       error = 4
188       break
189   #
190     break
191   #
192   return error
193
194 #========================================================================
195
196 homard = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
197 assert homard is not None, "Impossible to load homard engine"
198 homard.SetLanguageShort("fr")
199 #
200 # Exec of HOMARD-SALOME
201 #
202 try :
203   error_main = homard_exec(salome.myStudy)
204   if error_main :
205     raise Exception('Pb in homard_exec at iteration %d' %error_main )
206 except Exception, e:
207   raise Exception('Pb in homard_exec: '+e.message)
208 #
209 # Test of the results
210 #
211 n_rep_test_file = n_iter_test_file
212 destroy_dir = not debug
213 test_results(Rep_Test, Test_Name, dircase, n_iter_test_file, n_rep_test_file, destroy_dir)
214 #
215 if salome.sg.hasDesktop():
216   salome.sg.updateObjBrowser(1)
217   iparameters.getSession().restoreVisualState(1)
218