1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
|
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2019 EDF S.A.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program 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 General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.
#-------------------------------------------------------------------------------
"""
This module contains the following classes and function:
- OpenTurnsDialogView
"""
#-------------------------------------------------------------------------------
# Library modules import
#-------------------------------------------------------------------------------
import logging
try:
import ConfigParser # Python2
configparser = ConfigParser
except Exception:
import configparser # Python3
import os
import subprocess
#-------------------------------------------------------------------------------
# Third-party modules
#-------------------------------------------------------------------------------
from code_saturne.Base.QtCore import *
from code_saturne.Base.QtGui import *
from code_saturne.Base.QtWidgets import *
#-------------------------------------------------------------------------------
# Application modules import
#-------------------------------------------------------------------------------
from code_saturne.model.Common import GuiParam
from code_saturne.Base.QtPage import ComboModel, IntValidator, DoubleValidator, from_qvariant
from code_saturne.Pages.OpenTurnsDialogForm import Ui_OpenTurnsDialogForm
from code_saturne.model.OpenTurnsModel import OpenTurnsModel
#-------------------------------------------------------------------------------
# log config
#-------------------------------------------------------------------------------
logging.basicConfig()
log = logging.getLogger("OpenTurnsDialogView")
log.setLevel(GuiParam.DEBUG)
#-------------------------------------------------------------------------------
# Main class
#-------------------------------------------------------------------------------
class OpenTurnsDialogView(QDialog, Ui_OpenTurnsDialogForm):
"""
OpenTurns Page viewer class
"""
def __init__(self, parent, case):
"""
Constructor
"""
QDialog.__init__(self, parent)
Ui_OpenTurnsDialogForm.__init__(self)
self.setupUi(self)
self.case = case
self.parent = parent
title = self.tr("OpenTURNS parameters")
self.setWindowTitle(title)
self.case.undoStopGlobal()
case_is_saved = not self.case.isModified()
self.mdl = OpenTurnsModel(case)
if not self.mdl.getHostName():
self.mdl.setDefaultOptions()
# Combo model
config = configparser.ConfigParser()
config.read(self.case['package'].get_configfiles())
self.nmodes = 1
dist_hosts = None
if config.has_section('distant_hosts'):
if len(config.options('distant_hosts')) != 0:
dist_hosts = config.options('distant_hosts')
self.nmodes += len(dist_hosts)
self.modelOtStudyHosts = ComboModel(self.comboBoxStudyMode,self.nmodes,1)
self.modelOtStudyHosts.addItem(self.tr("Localhost"), 'localhost')
self.hosts_bmgr = {}
if config.has_option('install', 'batch'):
self.hosts_bmgr['localhost'] = (config.get('install', 'batch')).lower()
else:
self.hosts_bmgr['localhost'] = 'none'
# Check for distant builds:
# Hosts are stored in the form <batch_rm>_<host_name> hence the split
# used hereafter to determine the "real" host name
self.hosts_binpath = {}
self.hosts_binpath['localhost'] = 'default'
self.distant_host_builds = None
if dist_hosts != None:
self.distant_host_builds = {}
for key in dist_hosts:
host_name = key.split('_')[1]
self.hosts_bmgr[host_name] = key.split('_')[0]
self.hosts_binpath[host_name] = config.get('distant_hosts',
key)
self.addDistantBuilds(host_name)
dh_not_found = False
if self.distant_host_builds[host_name] == None:
self.distant_host_builds[host_name] = ['none found']
dh_not_found = True
host_tag = 'distant : ' + host_name
self.modelOtStudyHosts.addItem(self.tr(host_tag),
host_name,
warn=dh_not_found)
if dh_not_found:
self.modelOtStudyHosts.disableItem(str_model=host_name)
# ---------------------------------------
# Connections:
self.comboBoxStudyMode.activated[str].connect(self.slotOtStudyMode)
self.comboBoxDistantBuilds.activated[str].connect(self.slotBuildChoice)
self.lineEditDistWorkDir.textChanged[str].connect(self.slotOtDistWdir)
self.spinBoxNumberNodes.valueChanged[int].connect(self.slotUpdateNodesNumber)
self.spinBoxNumberTasks.valueChanged[int].connect(self.slotUpdateTasksNumber)
self.spinBoxNumberThreads.valueChanged[int].connect(self.slotUpdateThreadsNumber)
self.spinBoxNumberDays.valueChanged[int].connect(self.slotUpdateWCDays)
self.spinBoxNumberHours.valueChanged[int].connect(self.slotUpdateWCHours)
self.spinBoxNumberMinutes.valueChanged[int].connect(self.slotUpdateWCMinutes)
self.spinBoxNumberSeconds.valueChanged[int].connect(self.slotUpdateWCSeconds)
self.lineEditWCKEY.textChanged[str].connect(self.slotUpdateWckey)
self.pushButtonLaunchOT.clicked.connect(self.slotLaunchCsOt)
self.pushButtonCancel.clicked.connect(self.slotCancel)
# ---------------------------------------
# Hide/Show initial elements
if self.nmodes == 1:
self.groupBoxLocalLaunch.show()
self.groupBoxDistantLaunch.hide()
self.setAvailableBuildsList('localhost')
else:
self.setAvailableBuildsList(self.mdl.getHostName())
if self.mdl.getHostName() == "localhost":
self.groupBoxLocalLaunch.show()
self.groupBoxDistantLaunch.hide()
else:
self.groupBoxLocalLaunch.hide()
self.groupBoxDistantLaunch.show()
# ---------------------------------------
# Initial values
if dist_hosts != None:
self.modelOtStudyHosts.setItem(str_model=self.mdl.host_name)
else:
self.modelOtStudyHosts.setItem(str_model='localhost')
self.spinBoxLocalProcs.setValue(int(self.mdl.nprocs))
self.spinBoxLocalThreads.setValue(1)
self.spinBoxNumberNodes.setValue(int(self.mdl.nnodes))
self.spinBoxNumberTasks.setValue(int(self.mdl.ntasks))
self.spinBoxNumberThreads.setValue(int(self.mdl.nthreads))
wct = self.mdl.getWallClockTime()
self.spinBoxNumberDays.setValue(int(wct[0]))
self.spinBoxNumberHours.setValue(int(wct[1]))
self.spinBoxNumberMinutes.setValue(int(wct[2]))
self.spinBoxNumberSeconds.setValue(int(wct[3]))
self.lineEditWCKEY.setText(self.mdl.getWCKEY())
self.case.undoStartGlobal()
@pyqtSlot(str)
def slotOtStudyMode(self, text):
"""
Host type: localhost or a distant one (defined in code_saturne.cfg).
"""
host_name = self.modelOtStudyHosts.dicoV2M[str(text)]
self.mdl.setHostName(host_name)
self.mdl.setBatchManager(self.hosts_bmgr[host_name])
if host_name != 'localhost':
self.groupBoxDistantLaunch.show()
else:
self.groupBoxDistantLaunch.hide()
self.setAvailableBuildsList(host_name)
self.mdl.arch_path = self.hosts_binpath[host_name]
@pyqtSlot(str)
def slotBuildChoice(self, text):
"""
Sets the hostname
"""
self.mdl.setBuildName(text)
@pyqtSlot(str)
def slotOtDistWdir(self, text):
"""
Set the distant workdir path
"""
self.mdl.setDistWorkdir(text)
@pyqtSlot(int)
def slotUpdateNodesNumber(self, v):
"""
Update the number of required computation nodes
"""
n = int(self.spinBoxNumberNodes.text())
self.mdl.setClusterParams(nnodes=n)
@pyqtSlot(int)
def slotUpdateNprocs(self, v):
"""
Update the number of required processes
"""
n = int(self.spinBoxLocalProcs.text())
self.mdl.setNprocs(n)
@pyqtSlot(int)
def slotUpdateTasksNumber(self, v):
"""
Update the number of required mpi tasks per node
"""
n = int(self.spinBoxNumberTasks.text())
self.mdl.setClusterParams(ntasks=n)
@pyqtSlot(int)
def slotUpdateThreadsNumber(self, v):
"""
Update the number of required threads per processor
"""
n = int(self.spinBoxNumberThreads.text())
self.mdl.setClusterParams(nthreads=n)
@pyqtSlot(int)
def slotUpdateWCDays(self, v):
"""
Update the wall clock days value
"""
d, h, m, s = self.mdl.getWallClockTime()
d = str(int(self.spinBoxNumberDays.text()))
self.mdl.setWallClockTime(d, h, m, s)
@pyqtSlot(int)
def slotUpdateWCHours(self, v):
"""
Update the wall clock hours value
"""
d, h, m, s = self.mdl.getWallClockTime()
h = str(int(self.spinBoxNumberHours.text()))
self.mdl.setWallClockTime(d, h, m, s)
@pyqtSlot(int)
def slotUpdateWCMinutes(self, v):
"""
Update the wall clock minutes value
"""
d, h, m, s = self.mdl.getWallClockTime()
m = str(int(self.spinBoxNumberMinutes.text()))
self.mdl.setWallClockTime(d, h, m, s)
@pyqtSlot(int)
def slotUpdateWCSeconds(self, v):
"""
Update the wall clock seconds value
"""
d, h, m, s = self.mdl.getWallClockTime()
s = str(int(self.spinBoxNumberSeconds.text()))
self.mdl.setWallClockTime(d, h, m, s)
@pyqtSlot(str)
def slotUpdateWckey(self, text):
"""
Update the WCKEY variable
"""
self.mdl.setWCKEY(text)
@pyqtSlot()
def slotLaunchCsOt(self):
"""
Translate the Code_Sature reference case and study into an OpenTurs
physical model and study
"""
QDialog.accept(self)
if self.case['salome']:
import salome_ot
# Update the study cfg file
self.mdl.update_cfg_file()
# Generating OpenTurns _exec function
self.create_cs_exec_function()
# Load the Code_Saturne cas as Physical model and launch
# OpenTurns GUI
cs_exec_script_name = os.path.join(self.mdl.otstudy_path, 'cs_execute_job.py')
salome_ot.loadYacsPyStudy(cs_exec_script_name)
else:
print("This option is only available within the SALOME_CFD platform")
@pyqtSlot()
def slotCancel(self):
"""
Close dialog with no modifications
"""
QDialog.accept(self)
def tr(self, text):
"""
Translation
"""
return text
def addDistantBuilds(self, host_name):
"""
Search for the distant builds of Code_Saturne for the given distant
host.
"""
host_path = self.hosts_binpath[host_name]
builds_list = __getListOfDistantBuilds__(host_name,
host_path)
self.distant_host_builds[host_name] = builds_list
def setAvailableBuildsList(self, host_name):
"""
Set the list of available builds per host
"""
if host_name == 'localhost':
self.groupBoxDistantLaunch.hide()
self.groupBoxLocalLaunch.show()
self.comboBoxDistantBuilds.hide()
self.labelDistantBuilds.hide()
return
else:
self.groupBoxDistantLaunch.show()
self.groupBoxLocalLaunch.hide()
self.comboBoxDistantBuilds.show()
self.labelDistantBuilds.show()
dist_builds_list = self.distant_host_builds[host_name]
self.modelOtDistantBuilds = ComboModel(self.comboBoxDistantBuilds,
len(dist_builds_list),
1)
for db in dist_builds_list:
self.modelOtDistantBuilds.addItem(self.tr(db), db)
def create_cs_exec_function(self):
"""
This function generates the _exec function needed by OpenTurns for a study
using distant launching on clusters.
Takes as input:
- Code_Saturne study path
- OT_params.cfg name
- list of OTurns input variables
- list of OTurns output variables
- the requested cluster name
- the result file name which contains the output values
"""
cluster = self.mdl.host_name
exec_file_name = os.path.join(self.mdl.otstudy_path, 'cs_execute_job.py')
f = open(exec_file_name, 'w')
script_cmd = "\n"
script_cmd += "# ============================================================================== \n"
script_cmd += "# OPENTURNS EXEC FUNCTION WHICH LAUNCHES CODE_SATURNE ON A DISTANT CLUSTER \n"
script_cmd += "# ============================================================================== \n"
script_cmd += "\n\n"
nvars = len(self.mdl.input_variables)
script_cmd = 'def _exec('
vars_dict = '{'
toffset = ' '
cmd1 = 'cfd_eval = cfd_openturns_study('
loffset1 = toffset
for i in range(len(cmd1)):
loffset1 += ' '
loffset2 = ' '
iv = -1
for i in range(nvars):
if i == 0:
script_cmd += self.mdl.input_variables[i]
vars_dict += '"' + self.mdl.input_variables[i] + '":'
vars_dict += self.mdl.input_variables[i]
else:
script_cmd += ", " + self.mdl.input_variables[i]
vars_dict += ', \n'
vars_dict += loffset1 + loffset2 + ' '
vars_dict += '"' + self.mdl.input_variables[i] + '":'
vars_dict += self.mdl.input_variables[i]
script_cmd += '):\n\n'
vars_dict += '}'
script_cmd += toffset + "import sys\n"
salome_pydir = os.path.join(self.case['package'].dirs['pythondir'][1],
'salome')
script_cmd += toffset
script_cmd += "sys.path.insert(-1, '%s')\n\n" % salome_pydir
script_cmd += toffset + "from CFDSTUDYOTURNS_StudyInterface import cfd_openturns_study"
script_cmd += "\n\n"
script_cmd += toffset + cmd1 + 'study_path = "' + self.mdl.otstudy_path + '",\n'
script_cmd += loffset1 + 'study_cfg = "openturns_study.cfg",\n'
script_cmd += loffset1 + 'vars_dico = ' + vars_dict + ')\n\n'
script_cmd += toffset + 'cfd_eval.study2code() \n\n'
script_cmd += toffset + 'cfd_eval.run() \n\n'
n_vals = len(self.mdl.output_variables)
vals_list = ""
for i in range(n_vals):
if i != 0:
vals_list += ', '
vals_list += self.mdl.output_variables[i]
cmd_m1 = '(%s,) = cfd_eval.code2study(n_values=%d)\n' %(vals_list, n_vals)
script_cmd += toffset + cmd_m1
script_cmd += '\n'
script_cmd += toffset + 'return '
script_cmd += vals_list + '\n'
f.write(script_cmd)
f.close()
#-------------------------------------------------------------------------------
# Utility functions
#-------------------------------------------------------------------------------
def __getListOfDistantBuilds__(host_name, search_path):
"""
This functions retrieve the list of Code_Saturne builds in a given
directory on a distant cluster.
Returns None if no builds are found.
"""
# Constructing the search command
search_cmd = ''
search_cmd += "cd "+ search_path + "\n"
search_cmd += "build_list=\n"
search_cmd += "find . -mindepth 1 -maxdepth 1 -type d |\n"
search_cmd += "while read -r d; do\n"
search_cmd += " echo ${d:2}\n"
search_cmd += "done\n"
search_cmd += "'"
ssh_cmd = subprocess.Popen(["ssh", host_name, search_cmd],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
vr = ssh_cmd.stdout.readlines()
ve = ssh_cmd.stderr.readlines()
if vr == [] or vr == None or 'Could not resolve hostname' in ve:
dist_versions = None
else:
vr.sort()
dist_versions = []
for iv in range(len(vr)):
version_name = vr[iv][:-1].decode('utf-8')
dist_versions.append(version_name)
return dist_versions
#-------------------------------------------------------------------------------
# Testing part
#-------------------------------------------------------------------------------
if __name__ == "__main__":
pass
#-------------------------------------------------------------------------------
# End
#-------------------------------------------------------------------------------
|