Salome HOME
Merge from V6_main 01/04/2013
[modules/smesh.git] / src / Tools / padder / unittests / usecase_meshJobManager.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2013  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 # Author(s): Guillaume Boulant (23/03/2011)
22 #
23
24 # This script illustrates the standard use case of the component
25 # MeshJobManager from within a SALOME script. It could be used as a
26 # unit test of the component. The typical procedure is:
27 # $ <appli>/runAppli -t
28 # $ <appli>/runSession </path/to>/usecase_meshJobManager.py
29
30 #
31 # =======================================================================
32 # Preparing the configuration parameters
33 # =======================================================================
34 #
35 import sys
36 import os
37 import time
38 from salome.smesh.spadder.configreader import ConfigReader, printConfig, getPadderTestDir
39
40 configReader = ConfigReader()
41 defaultConfig = configReader.getDefaultConfig()
42 printConfig(defaultConfig)
43
44 from salome.smesh import spadder
45
46 import salome
47 import MESHJOB
48
49 #
50 # Setup the configuration in the component. We first have to load the
51 # catalog of SPADDER components, then load the component
52 # MeshJobManager, and finally configure this component.
53 #
54 spadder.loadSpadderCatalog()
55
56 salome.salome_init()
57 component = salome.lcc.FindOrLoadComponent("FactoryServer","MeshJobManager")
58 config = MESHJOB.ConfigParameter(resname=defaultConfig.resname,
59                                  binpath=defaultConfig.binpath,
60                                  envpath=defaultConfig.envpath)
61
62 configId = "localhost"
63 component.configure(configId,config)
64
65
66 #
67 # =======================================================================
68 # Define several datasets for the different use cases
69 # =======================================================================
70 #
71
72 # We define several functions that create each a dataset of med files
73 # for testing the component. The test function number corresponds to
74 # the number of the test defined in the SpherePadder installation
75 # directory.
76 PADDERTESTDIR = getPadderTestDir(defaultConfig)
77 #PADDERTESTDIR = spadder.getTestPadderDataDir()
78 #
79 # WARN: the above instruction (spadder.getTestPadderDataDir())
80 # localizes the PADDERTEST DIR using the PADDERDIR shell variable,
81 # while the previous one (getPadderTestDir) localizes this directory
82 # from data of the config (read from the configuration file
83 # padder.cfg).
84 #
85 def test00_parameters():
86     """Test using a concrete mesh and a single steelbar mesh""" 
87     file_concrete=os.path.join(spadder.getTestDataDir(),"concrete.med")
88     file_steelbar=os.path.join(spadder.getTestDataDir(),"ferraill.med")
89
90     meshJobParameterList = []
91     param = MESHJOB.MeshJobParameter(file_name=file_concrete,
92                                      file_type=MESHJOB.MED_CONCRETE,
93                                      group_name="concrete")
94     meshJobParameterList.append(param)
95
96     param = MESHJOB.MeshJobParameter(file_name=file_steelbar,
97                                      file_type=MESHJOB.MED_STEELBAR,
98                                      group_name="steelbar")
99     meshJobParameterList.append(param)
100     return meshJobParameterList
101
102 def test01_parameters():
103     """One concrete mesh and two steelbar meshes"""
104     datadir = os.path.join(PADDERTESTDIR,"test01")
105     meshJobParameterList = []
106
107     medfile = os.path.join(datadir,"concrete.med")
108     param = MESHJOB.MeshJobParameter(file_name=medfile,
109                                      file_type=MESHJOB.MED_CONCRETE,
110                                      group_name="concrete")
111     meshJobParameterList.append(param)
112     
113     medfile = os.path.join(datadir,"ferraill.med")
114     param = MESHJOB.MeshJobParameter(file_name=medfile,
115                                      file_type=MESHJOB.MED_STEELBAR,
116                                      group_name="ferraill")
117     meshJobParameterList.append(param)
118
119     medfile = os.path.join(datadir,"ferrtran.med")
120     param = MESHJOB.MeshJobParameter(file_name=medfile,
121                                      file_type=MESHJOB.MED_STEELBAR,
122                                      group_name="ferrtran")
123     meshJobParameterList.append(param)
124     
125     return meshJobParameterList
126
127 def test02_parameters():
128     """One steelbar mesh only, without a concrete mesh"""
129     datadir = os.path.join(PADDERTESTDIR,"test02")
130     meshJobParameterList = []
131
132     medfile = os.path.join(datadir,"cadreef.med")
133     param = MESHJOB.MeshJobParameter(file_name=medfile,
134                                      file_type=MESHJOB.MED_STEELBAR,
135                                      group_name="cadre")
136     meshJobParameterList.append(param)
137     return meshJobParameterList
138
139 def test03_parameters():
140     """One concrete mesh only, without a steelbar mesh"""
141     datadir = os.path.join(PADDERTESTDIR,"test03")
142     meshJobParameterList = []
143
144     medfile = os.path.join(datadir,"concrete.med")
145     param = MESHJOB.MeshJobParameter(file_name=medfile,
146                                      file_type=MESHJOB.MED_CONCRETE,
147                                      group_name="concrete")
148     meshJobParameterList.append(param)
149     return meshJobParameterList
150
151 #
152 # =======================================================================
153 # Prepare the job parameters and initialize the job
154 # =======================================================================
155 #
156
157 # Choose here the use case
158 #meshJobParameterList = test00_parameters()
159 #meshJobParameterList = test01_parameters()
160 #meshJobParameterList = test02_parameters()
161 meshJobParameterList = test03_parameters()
162
163 #
164 # Prepare, start and follow-up the job
165 #
166 jobid = component.initialize(meshJobParameterList, configId)
167 if jobid<0:
168     msg = component.getLastErrorMessage()
169     print "ERR: %s"%msg
170     sys.exit(1)
171     
172 created = False
173 nbiter  = 0
174 while not created:
175     state = component.getState(jobid)
176     print "MeshJobManager ["+str(nbiter)+"] : state = "+str(state)
177     if state == "CREATED":
178         created = True
179     time.sleep(0.5)
180     nbiter+=1
181
182
183 #
184 # =======================================================================
185 # Submit the job and start the supervision
186 # =======================================================================
187 #
188 # Start the execution of the job identified by its job id.
189 #
190 ok=component.start(jobid)
191 if not ok:
192     msg = component.getLastErrorMessage()
193     print "ERR: %s"%msg
194     sys.exit(1)
195
196 print "job started: %s"%ok
197
198 #
199 # This part illustrates how you can follow the execution of the job.
200 #
201 run_states = ["CREATED", "IN_PROCESS", "QUEUED", "RUNNING", "PAUSED"];
202 end_states = ["FINISHED", "ERROR"]
203 all_states = run_states+end_states;
204
205 ended  = False
206 nbiter = 0
207 while not ended:
208     state = component.getState(jobid)
209     print "MeshJobManager ["+str(nbiter)+"] : state = "+str(state)
210     if state not in run_states:
211         ended=True
212     time.sleep(0.5)
213     nbiter+=1
214         
215 if state not in end_states:
216     print "ERR: jobid = "+str(jobid)+" ended abnormally with state="+str(state)
217     msg = component.getLastErrorMessage()
218     print "ERR: %s"%msg    
219 else:
220     print "OK:  jobid = "+str(jobid)+" ended with state="+str(state)
221     meshJobResults = component.finalize(jobid)
222     print meshJobResults
223     if meshJobResults.status is not True:
224         print "ERR: the results are not OK: %s"%component.getLastErrorMessage()
225         print "ERR: see log files in %s"%meshJobResults.results_dirname