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