From c48a2e69eb806f4cc902fa70b9c3b8467921969b Mon Sep 17 00:00:00 2001 From: Renaud Barate Date: Thu, 6 Dec 2012 11:31:02 +0100 Subject: [PATCH] Add batch job generation --- resources/generate_job.png | Bin 0 -> 984 bytes src/PARAMETRIC/PARAMETRIC.py | 16 +-- src/PARAMETRICGUI/PARAMETRICGUI.py | 32 ++++-- src/salome/parametric/__init__.py | 3 + src/salome/parametric/genjob.py | 118 +++++++++++++++++++++++ src/salome/parametric/gui/CMakeLists.txt | 2 +- src/salome/parametric/gui/__init__.py | 2 + src/salome/parametric/gui/genjob.py | 55 +++++++++++ src/salome/parametric/gui/genjob.ui | 110 +++++++++++++++++++++ 9 files changed, 314 insertions(+), 24 deletions(-) create mode 100644 resources/generate_job.png create mode 100644 src/salome/parametric/genjob.py create mode 100644 src/salome/parametric/gui/genjob.py create mode 100644 src/salome/parametric/gui/genjob.ui diff --git a/resources/generate_job.png b/resources/generate_job.png new file mode 100644 index 0000000000000000000000000000000000000000..a4429c6849aca15ad045a4a2f534b420cd44c04a GIT binary patch literal 984 zcmV;}11J26P)Px#24YJ`L;(K){{a7>y{D4^000SaNLh0L01FcU01FcV0GgZ_00007bV*G`2iyz= z5DOj0(tV`>00US_L_t(I%TLVBO+g?2h^hnY@$I_J!s z-$i4=Z~eVb-n@_Cu4{bxJfmMfO|1Q=*B)*vKT^5LTkpE6G?`w#(ffD$yF&M|bK=ks zsEfxpGU9FkkA2&+t2y3#;>r48T|A(|eoeSaaXas*YeP%o`(G17)6@A!(DmE&90fqG zCqE$mLU&75O=RGS2P*?jbrEz;iutTfI_F^7O4P*z9Nt}ZUo_^s^5+?-hKgkV*u<7{FCU*7BNjH9NaYbgbv*10|7W4`-dNx4(wa)lu43pOJi118EI_=>OJdRC z@^B8*(AGCExm1+-#Fo19{Th0e7I!qha(eQwk>Tk!zp07(su0pJ@OgELB}I8au=5_1t}8Q` zh9q87b+Y5R_&Wd|fXY3eGSoLXaq#e?$Gp1mtlCN_;h`d85cWxero^WUvIT`d;7fKs z|Lz+wnB4&6WvyJbMAN_@{Udu1?5c0xzPapBOBqJMucWSn5MoW~Bop(0p8DvEQ&h&5 z$n?%_0Lb. import study +import genjob ParametricStudy = study.ParametricStudy ParametricVariable = study.ParametricVariable ParametricStudyEditor = study.ParametricStudyEditor +generate_job = genjob.generate_job +parse_entry = genjob.parse_entry diff --git a/src/salome/parametric/genjob.py b/src/salome/parametric/genjob.py new file mode 100644 index 0000000..351d4bd --- /dev/null +++ b/src/salome/parametric/genjob.py @@ -0,0 +1,118 @@ +# Copyright (C) 2012 EDF +# +# This file is part of SALOME PARAMETRIC module. +# +# SALOME PARAMETRIC module is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# SALOME PARAMETRIC module is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with SALOME PARAMETRIC module. If not, see . + +import os +import re +import tempfile +from datetime import datetime + +import salome +from salome.kernel.studyedit import getStudyEditor +from study import ParametricStudy + +job_script_template = """ +#!/usr/bin/env python + +import salome +import PARAMETRIC + +salome.salome_init() + +# load study +study = salome.myStudyManager.Open("%(input_study)s") + +# start container and load PARAMETRIC component +comp = salome.lcc.FindOrLoadComponent("ParametricContainer", "PARAMETRIC") + +# run parametric study +comp.RunStudy(study._get_StudyId(), "%(param_entry)s") + +# save study +salome.myStudyManager.SaveAs("%(output_study)s", study, False) +""" + +def generate_job(param_study, result_study_file_name, result_dir, resource): + """ + Create a Launcher job using the parameters specified by the user. + """ + # Save Salome study + ed = getStudyEditor() + name_wo_space = param_study.name.replace(" ", "_") + (fd, input_study) = tempfile.mkstemp(prefix = name_wo_space + "_Input_", suffix = ".hdf") + os.close(fd) + salome.myStudyManager.SaveAs(input_study, ed.study, False) + + # Generate job script + job_script = job_script_template % {"input_study": os.path.basename(input_study), + "param_entry": param_study.entry, + "output_study": result_study_file_name} + (fd, job_script_file) = tempfile.mkstemp(prefix = "job_" + name_wo_space + "_", suffix = ".py") + os.close(fd) + f = open(job_script_file, "w") + f.write(job_script) + f.close() + + # Define job parameters + job_params = salome.JobParameters() + job_params.job_name = name_wo_space + job_params.job_type = "python_salome" + job_params.job_file = job_script_file + job_params.in_files = [input_study] + job_params.out_files = [result_study_file_name] + job_params.result_directory = result_dir + + # Add files to transfer from the computation code + if param_study.solver_code_type == ParametricStudy.SALOME_COMPONENT: + code = param_study.salome_component_name + comp = salome.lcc.FindOrLoadComponent("FactoryServer", code) + if comp is not None and hasattr(comp, "GetFilesToTransfer"): + (code_in_files, code_out_files) = comp.GetFilesToTransfer(ed.studyId, + parse_entry(param_study.solver_case_entry)) + job_params.in_files += code_in_files + job_params.out_files += code_out_files + + # Define resource parameters + job_params.resource_required = salome.ResourceParameters() + job_params.resource_required.name = resource + job_params.resource_required.nb_proc = param_study.nb_parallel_computations + 1 + + # Generate name for the working directory + res_manager = salome.naming_service.Resolve("/ResourcesManager") + res_definition = res_manager.GetResourceDefinition(resource) + res_work_dir = res_definition.working_directory + if res_work_dir != "": + timestr = datetime.now().ctime() + timestr = timestr.replace('/', '_') + timestr = timestr.replace('-', '_') + timestr = timestr.replace(':', '_') + timestr = timestr.replace(' ', '_') + work_dir = res_work_dir + "/" + job_params.job_name + "_" + timestr + job_params.work_directory = work_dir + + # Create Launcher job + launcher = salome.naming_service.Resolve('/SalomeLauncher') + launcher.createJob(job_params) + +def parse_entry(selected_value): + """ + Find entry if selected_value is something like "name (entry)" + """ + entry = selected_value + match = re.search("\((.*)\)$", entry) + if match is not None: + entry = match.group(1) + return entry diff --git a/src/salome/parametric/gui/CMakeLists.txt b/src/salome/parametric/gui/CMakeLists.txt index f1cf018..89f17ca 100644 --- a/src/salome/parametric/gui/CMakeLists.txt +++ b/src/salome/parametric/gui/CMakeLists.txt @@ -15,7 +15,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with SALOME PARAMETRIC module. If not, see . -SET(PYUIC_FILES wizard_ui.py varrange_ui.py execparams_ui.py) +SET(PYUIC_FILES wizard_ui.py varrange_ui.py execparams_ui.py genjob_ui.py) SET(INSTALL_DIR ${PYTHONDIR}/salome/parametric/gui) FOREACH(OUTPUT ${PYUIC_FILES}) diff --git a/src/salome/parametric/gui/__init__.py b/src/salome/parametric/gui/__init__.py index 882fff9..5563aa7 100644 --- a/src/salome/parametric/gui/__init__.py +++ b/src/salome/parametric/gui/__init__.py @@ -16,5 +16,7 @@ # along with SALOME PARAMETRIC module. If not, see . import mainpanel +import genjob MainPanel = mainpanel.MainPanel +GenJobDialog = genjob.GenJobDialog diff --git a/src/salome/parametric/gui/genjob.py b/src/salome/parametric/gui/genjob.py new file mode 100644 index 0000000..976842b --- /dev/null +++ b/src/salome/parametric/gui/genjob.py @@ -0,0 +1,55 @@ +# Copyright (C) 2012 EDF +# +# This file is part of SALOME PARAMETRIC module. +# +# SALOME PARAMETRIC module is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# SALOME PARAMETRIC module is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with SALOME PARAMETRIC module. If not, see . + +import os +from PyQt4 import QtGui, QtCore + +import salome +from salome.parametric import generate_job +from genjob_ui import Ui_GenJobDialog + + +class GenJobDialog(QtGui.QDialog, Ui_GenJobDialog): + + def __init__(self, parent, param_study): + QtGui.QDialog.__init__(self, parent) + self.setupUi(self) + self.connect(self.dialogButtonBox, QtCore.SIGNAL("accepted()"), self.validate) + self.connect(self.dialogButtonBox, QtCore.SIGNAL("rejected()"), self.close) + self.connect(self.chooseResultDirectoryButton, QtCore.SIGNAL("clicked()"), self.choose_result_dir) + self.resultStudyLE.setText(param_study.name.replace(" ", "_") + "_Result.hdf") + self.resultDirectoryLE.setText(os.getcwd()) + + # Populate resource combo box + res_manager = salome.naming_service.Resolve("/ResourcesManager") + res_params = salome.ResourceParameters() + res_list = res_manager.GetFittingResources(res_params) + self.resourceCB.addItems(res_list) + + self.param_study = param_study + + def choose_result_dir(self): + directory = QtGui.QFileDialog.getExistingDirectory(self, + directory = self.resultDirectoryLE.text(), + options = QtGui.QFileDialog.ShowDirsOnly) + if not directory.isNull(): + self.resultDirectoryLE.setText(directory) + + def validate(self): + generate_job(self.param_study, str(self.resultStudyLE.text()), + str(self.resultDirectoryLE.text()), str(self.resourceCB.currentText())) + self.close() diff --git a/src/salome/parametric/gui/genjob.ui b/src/salome/parametric/gui/genjob.ui new file mode 100644 index 0000000..ec126b3 --- /dev/null +++ b/src/salome/parametric/gui/genjob.ui @@ -0,0 +1,110 @@ + + + GenJobDialog + + + + 0 + 0 + 636 + 202 + + + + Generate Job + + + + + + + + Result Study Name: + + + + + + + + + + Result Directory: + + + + + + + + + + 0 + 0 + + + + + + + + ... + + + + + + + + + Resource + + + + + + + + + + + + Qt::Vertical + + + + 20 + 43 + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + + -- 2.39.2