Salome HOME
Merge V8_3_BR branch.
[modules/smesh.git] / src / Tools / padder / unittests / usecase_meshJobManager.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2016  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 # 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     meshJobFileList = []
91     param = MESHJOB.MeshJobFile(file_name=file_concrete,
92                                      file_type=MESHJOB.MED_CONCRETE,
93                                      group_name="concrete")
94     meshJobFileList.append(param)
95
96     param = MESHJOB.MeshJobFile(file_name=file_steelbar,
97                                      file_type=MESHJOB.MED_STEELBAR,
98                                      group_name="steelbar")
99     meshJobFileList.append(param)
100     return meshJobFileList
101
102 def test01_parameters():
103     """One concrete mesh and two steelbar meshes"""
104     datadir = os.path.join(PADDERTESTDIR,"test01")
105     meshJobFileList = []
106
107     medfile = os.path.join(datadir,"concrete.med")
108     param = MESHJOB.MeshJobFile(file_name=medfile,
109                                      file_type=MESHJOB.MED_CONCRETE,
110                                      group_name="concrete")
111     meshJobFileList.append(param)
112
113     medfile = os.path.join(datadir,"ferraill.med")
114     param = MESHJOB.MeshJobFile(file_name=medfile,
115                                      file_type=MESHJOB.MED_STEELBAR,
116                                      group_name="ferraill")
117     meshJobFileList.append(param)
118
119     medfile = os.path.join(datadir,"ferrtran.med")
120     param = MESHJOB.MeshJobFile(file_name=medfile,
121                                      file_type=MESHJOB.MED_STEELBAR,
122                                      group_name="ferrtran")
123     meshJobFileList.append(param)
124
125     return meshJobFileList
126
127 def test02_parameters():
128     """One steelbar mesh only, without a concrete mesh"""
129     datadir = os.path.join(PADDERTESTDIR,"test02")
130     meshJobFileList = []
131
132     medfile = os.path.join(datadir,"cadreef.med")
133     param = MESHJOB.MeshJobFile(file_name=medfile,
134                                      file_type=MESHJOB.MED_STEELBAR,
135                                      group_name="cadre")
136     meshJobFileList.append(param)
137     return meshJobFileList
138
139 def test03_parameters():
140     """One concrete mesh only, without a steelbar mesh"""
141     datadir = os.path.join(PADDERTESTDIR,"test03")
142     meshJobFileList = []
143
144     medfile = os.path.join(datadir,"concrete.med")
145     param = MESHJOB.MeshJobFile(file_name=medfile,
146                                      file_type=MESHJOB.MED_CONCRETE,
147                                      group_name="concrete")
148     meshJobFileList.append(param)
149     return meshJobFileList
150
151 #
152 # =======================================================================
153 # Prepare the job parameters and initialize the job
154 # =======================================================================
155 #
156
157 # Choose here the use case
158 #meshJobFileList = test00_parameters()
159 #meshJobFileList = test01_parameters()
160 #meshJobFileList = test02_parameters()
161 meshJobFileList = test03_parameters()
162
163 meshJobParameterList = []
164 param = MESHJOB.MeshJobParameter(name="RmaxRmin",value="1.5")
165 meshJobParameterList.append(param)
166 param = MESHJOB.MeshJobParameter(name="NbIteration",value="3")
167 meshJobParameterList.append(param)
168
169 #
170 # Prepare, start and follow-up the job
171 #
172 jobid = component.initialize(meshJobFileList, meshJobParameterList, configId)
173 if jobid<0:
174     msg = component.getLastErrorMessage()
175     print "ERR: %s"%msg
176     sys.exit(1)
177
178 created = False
179 nbiter  = 0
180 while not created:
181     state = component.getState(jobid)
182     print "MeshJobManager ["+str(nbiter)+"] : state = "+str(state)
183     if state == "CREATED":
184         created = True
185     time.sleep(0.5)
186     nbiter+=1
187
188
189 #
190 # =======================================================================
191 # Submit the job and start the supervision
192 # =======================================================================
193 #
194 # Start the execution of the job identified by its job id.
195 #
196 ok=component.start(jobid)
197 if not ok:
198     msg = component.getLastErrorMessage()
199     print "ERR: %s"%msg
200     sys.exit(1)
201
202 print "job started: %s"%ok
203
204 #
205 # This part illustrates how you can follow the execution of the job.
206 #
207 run_states = ["CREATED", "IN_PROCESS", "QUEUED", "RUNNING", "PAUSED"];
208 end_states = ["FINISHED", "ERROR"]
209 all_states = run_states+end_states;
210
211 ended  = False
212 nbiter = 0
213 while not ended:
214     state = component.getState(jobid)
215     print "MeshJobManager ["+str(nbiter)+"] : state = "+str(state)
216     if state not in run_states:
217         ended=True
218     time.sleep(0.5)
219     nbiter+=1
220
221 if state not in end_states:
222     print "ERR: jobid = "+str(jobid)+" ended abnormally with state="+str(state)
223     msg = component.getLastErrorMessage()
224     print "ERR: %s"%msg
225 else:
226     print "OK:  jobid = "+str(jobid)+" ended with state="+str(state)
227     meshJobResults = component.finalize(jobid)
228     print meshJobResults
229     if meshJobResults.status is not True:
230         print "ERR: the results are not OK: %s"%component.getLastErrorMessage()
231         print "ERR: see log files in %s"%meshJobResults.results_dirname