File: StartRestartView.py

package info (click to toggle)
code-saturne 6.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 63,340 kB
  • sloc: ansic: 354,724; f90: 119,812; python: 87,716; makefile: 4,653; cpp: 4,272; xml: 2,839; sh: 1,228; lex: 170; yacc: 100
file content (563 lines) | stat: -rw-r--r-- 19,113 bytes parent folder | download
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
# -*- 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 defines the 'Start/Restart' page.

This module contains the following classes:
- StartRestartAdvancedDialogView
- StartRestartView
"""

#-------------------------------------------------------------------------------
# Library modules import
#-------------------------------------------------------------------------------

import os, sys, types, shutil
import logging

#-------------------------------------------------------------------------------
# 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, from_qvariant
from code_saturne.model.SolutionDomainModel import RelOrAbsPath
from code_saturne.Pages.StartRestartForm import Ui_StartRestartForm
from code_saturne.Pages.StartRestartAdvancedDialogForm import Ui_StartRestartAdvancedDialogForm
from code_saturne.model.StartRestartModel import StartRestartModel, getRestartInfo

#-------------------------------------------------------------------------------
# log config
#-------------------------------------------------------------------------------

logging.basicConfig()
log = logging.getLogger("StartRestartView")
log.setLevel(GuiParam.DEBUG)

#-------------------------------------------------------------------------------
# Popup window class
#-------------------------------------------------------------------------------

class StartRestartAdvancedDialogView(QDialog, Ui_StartRestartAdvancedDialogForm):
    """
    Building of popup window for advanced options.
    """
    def __init__(self, parent, case, default):
        """
        Constructor
        """
        QDialog.__init__(self, parent)

        Ui_StartRestartAdvancedDialogForm.__init__(self)
        self.setupUi(self)

        self.case = case
        self.case.undoStopGlobal()

        self.setWindowTitle(self.tr("Advanced options"))
        self.default = default
        self.result = self.default.copy()

        # Combo models and items
        self.modelFreq   = ComboModel(self.comboBoxFreq, 4, 1)

        self.modelFreq.addItem(self.tr("Never"), 'Never')
        self.modelFreq.addItem(self.tr("Only at the end of the calculation"),
                               'At the end')
        self.modelFreq.addItem(self.tr("4 restart checkpoints"), '4 output')
        self.modelFreq.addItem(self.tr("Checkpoints frequency :"), 'Frequency')

        # Connections

        self.comboBoxFreq.activated[str].connect(self.slotFreq)
        self.lineEditNSUIT.textChanged[str].connect(self.slotNsuit)

        # Validator

        validatorNSUIT = IntValidator(self.lineEditNSUIT, min=0)
        self.lineEditNSUIT.setValidator(validatorNSUIT)

        # Read of auxiliary file if calculation restart is asked

        if self.default['restart']:
            self.groupBoxRestart.show()

            if self.default['restart_with_auxiliary'] == 'on':
                self.checkBoxReadAuxFile.setChecked(True)
            else:
                self.checkBoxReadAuxFile.setChecked(False)
        else:
            self.groupBoxRestart.hide()

        # Frequency of rescue of restart file

        if self.default['restart_rescue'] == -2:
            self.nsuit = -2
            self.lineEditNSUIT.setDisabled(True)
            self.freq = 'Never'
        elif self.default['restart_rescue'] == -1:
            self.nsuit = -1
            self.lineEditNSUIT.setDisabled(True)
            self.freq = 'At the end'
        elif self.default['restart_rescue'] == 0:
            self.nsuit = 0
            self.lineEditNSUIT.setDisabled(True)
            self.freq = '4 output'
        else:
            self.nsuit = self.default['restart_rescue']
            self.lineEditNSUIT.setEnabled(True)
            self.freq = 'Frequency'
        self.modelFreq.setItem(str_model=self.freq)
        self.lineEditNSUIT.setText(str(self.nsuit))

        self.case.undoStartGlobal()


    @pyqtSlot(str)
    def slotFreq(self, text):
        """
        Creation of popup window's widgets
        """
        self.freq = self.modelFreq.dicoV2M[str(text)]
        log.debug("getFreq-> %s" % self.freq)

        if self.freq == "Never":
            self.nsuit = -2
            self.lineEditNSUIT.setText(str(self.nsuit))
            self.lineEditNSUIT.setDisabled(True)

        elif self.freq == "At the end":
            self.nsuit = -1
            self.lineEditNSUIT.setText(str(self.nsuit))
            self.lineEditNSUIT.setDisabled(True)

        elif self.freq == "4 output":
            self.nsuit = 0
            self.lineEditNSUIT.setText(str(self.nsuit))
            self.lineEditNSUIT.setDisabled(True)

        elif self.freq == "Frequency":
            if self.nsuit <= 0: self.nsuit = 1
            self.lineEditNSUIT.setText(str(self.nsuit))
            self.lineEditNSUIT.setEnabled(True)


    @pyqtSlot(str)
    def slotNsuit(self, text):
        if self.lineEditNSUIT.validator().state == QValidator.Acceptable:
            n = from_qvariant(text, int)
            self.nsuit = n
            log.debug("getNsuit-> nsuit = %s" % n)


    def accept(self):
        """
        What to do when user clicks on 'OK'.
        """
        if self.checkBoxReadAuxFile.isChecked():
            self.result['restart_with_auxiliary'] = 'on'
        else:
            self.result['restart_with_auxiliary'] = 'off'

        self.result['restart_rescue'] = self.nsuit
        self.result['period_rescue']  = self.freq

        QDialog.accept(self)


    def reject(self):
        """
        Method called when 'Cancel' button is clicked
        """
        QDialog.reject(self)


    def get_result(self):
        """
        Method to get the result
        """
        return self.result


    def tr(self, text):
        """
        Translation
        """
        return text

#-------------------------------------------------------------------------------
# Main class
#-------------------------------------------------------------------------------

class StartRestartView(QWidget, Ui_StartRestartForm):
    """
    This page is devoted to the start/restart control.
    """
    def __init__(self, parent, case):
        """
        Constructor
        """
        QWidget.__init__(self, parent)

        Ui_StartRestartForm.__init__(self)
        self.setupUi(self)

        self.case = case
        self.case.undoStopGlobal()

        self.radioButtonYes.clicked.connect(self.slotStartRestart)
        self.radioButtonNo.clicked.connect(self.slotStartRestart)
        self.radioButtonAuto.clicked.connect(self.slotStartRestart)
        self.toolButton.pressed.connect(self.slotSearchRestartDirectory)
        self.toolButtonRestartMesh.pressed.connect(self.slotSearchRestartMesh)
        self.checkBox.clicked.connect(self.slotFrozenField)
        self.toolButtonAdvanced.pressed.connect(self.slotAdvancedOptions)
        self.checkBoxRestartMesh.stateChanged.connect(self.slotRestartMesh)

        self.model = StartRestartModel(self.case)

        # Widget initialization

        self.restart_path = self.model.getRestartPath()
        self.restart_mesh_path = self.model.getRestartMeshPath()

        if self.restart_path:
            if self.restart_path == '*':
                self.radioButtonNo.setChecked(False)
                self.radioButtonYes.setChecked(False)
                self.radioButtonAuto.setChecked(True)
            else:
                if not os.path.isdir(os.path.join(self.case['case_path'],
                                                  self.restart_path)):
                    title = self.tr("WARNING")
                    msg   = self.tr("Invalid path in %s!" % self.restart_path)
                    QMessageBox.warning(self, title, msg)

                self.radioButtonNo.setChecked(False)
                self.radioButtonYes.setChecked(True)
                self.radioButtonAuto.setChecked(False)

        else:
            self.radioButtonNo.setChecked(True)
            self.radioButtonYes.setChecked(False)
            self.radioButtonAuto.setChecked(False)

        self.slotStartRestart()

        if self.model.getFrozenField() == 'on':
            self.checkBox.setChecked(True)
        else:
            self.checkBox.setChecked(False)

        self.updateRestartTimes()
        self.updateRestartMeshView()

        self.case.undoStartGlobal()


    def updateRestartTimes(self):
        """
        Update information on restart times
        """

        # FIXME: ensure correct path is used in coupling situations;
        # currently, if in doubt, leave it empty (we prefer to have
        # no information than false information)
        restart_dir = None
        restart_path = None

        if self.restart_path == '*':
            d = os.path.join(os.path.split(self.case['case_path'])[0],
                             'RESU_COUPLING')
            if not os.path.isdir(d):
                restart_dir = os.path.join(self.case['case_path'], 'RESU')

            # isdir returns an error if restart_dir == None, hence the test
            if restart_dir:
                if os.path.isdir(restart_dir):
                    restart_path = '*'
                else:
                    restart_dir = None
        elif self.restart_path:
            if os.path.isabs(self.restart_path):
                restart_path = self.restart_path
            else:
                restart_path = os.path.join(self.case['case_path'],
                                            self.restart_path)

        rinfo = getRestartInfo(self.case['package'],
                               restart_dir,
                               restart_path)

        self.lineEdit.setEnabled(self.restart_path != '*')
        self.lineEdit.setFrame(self.restart_path != '*')

        if rinfo:
            self.lineEdit.setText(rinfo[0])
            self.labelIteration.show()
            self.labelTime.show()
            self.lineEditIteration.show()
            self.lineEditTime.show()
            self.lineEditIteration.setText(str(rinfo[1]))
            self.lineEditTime.setText(str(rinfo[2]))
        else:
            self.labelIteration.hide()
            self.labelTime.hide()
            self.lineEditIteration.hide()
            self.lineEditTime.hide()


    def updateRestartMeshView(self):
        """
        Upate restart mesh path view
        """
        if self.restart_mesh_path:
            self.checkBoxRestartMesh.setChecked(True)
            self.lineEditRestartMesh.setText(self.restart_mesh_path)
            self.lineEditRestartMesh.show()
            self.toolButtonRestartMesh.show()
        else:
            self.checkBoxRestartMesh.setChecked(False)
            self.lineEditRestartMesh.setText("")
            self.lineEditRestartMesh.hide()
            self.toolButtonRestartMesh.hide()


    @pyqtSlot()
    def slotSearchRestartDirectory(self):
        """
        Search restart file (directory) in list of directories
        """

        default = None
        l_restart_dirs = []
        for d in [os.path.join(os.path.split(self.case['case_path'])[0],
                               'RESU_COUPLING'),
                  os.path.join(self.case['case_path'], 'RESU')]:
            if os.path.isdir(d):
                l_restart_dirs.append(QUrl.fromLocalFile(d))
                if not default:
                    default = d

        if not default:
            default = self.case['case_path']

        title = self.tr("Select checkpoint/restart directory")
        options = QFileDialog.DontUseNativeDialog | QFileDialog.ReadOnly | QFileDialog.ShowDirsOnly

        dialog = QFileDialog()
        dialog.setWindowTitle(title)
        dialog.setDirectory(default)

        dialog.setOptions(options)
        dialog.setSidebarUrls(l_restart_dirs)
        dialog.setFileMode(QFileDialog.Directory)

        name_filter = str(self.tr("Checkpoint directory (checkpoint*)"))
        dialog.setNameFilter(name_filter)

        dialog.setLabelText(QFileDialog.Accept, str(self.tr("Select")))

        if dialog.exec_() == 1:

            s = dialog.selectedFiles()

            dir_path = str(s[0])
            dir_path = os.path.abspath(dir_path)

            self.restart_path = RelOrAbsPath(dir_path, self.case['case_path'])
            self.model.setRestartPath(self.restart_path)
            self.lineEdit.setText(self.restart_path)
            self.updateRestartTimes()

            log.debug("slotSearchRestartDirectory-> %s" % self.restart_path)


    @pyqtSlot()
    def slotSearchRestartMesh(self):
        """
        Search restart mesh (file) in list of directories
        """
        title    = self.tr("Select checkpoint/restart mesh_input/output")

        default = None
        l_restart_dirs = []
        for d in [os.path.join(os.path.split(self.case['case_path'])[0],
                               'RESU_COUPLING'),
                  os.path.join(self.case['case_path'], 'RESU')]:
            if os.path.isdir(d):
                l_restart_dirs.append(QUrl.fromLocalFile(d))
                if not default:
                    default = d

        if not default:
            default = self.case['case_path']

        options  = QFileDialog.DontUseNativeDialog | QFileDialog.ReadOnly

        dialog = QFileDialog()
        dialog.setWindowTitle(title)
        dialog.setDirectory(default)

        dialog.setOptions(options)
        dialog.setSidebarUrls(l_restart_dirs)
        dialog.setFileMode(QFileDialog.ExistingFile)

        name_filter = str(self.tr("Imported or preprocessed meshes (mesh_input mesh_output)"))
        dialog.setNameFilter(name_filter)

        dialog.setLabelText(QFileDialog.Accept, str(self.tr("Select")))

        if dialog.exec_() == 1:

            s = dialog.selectedFiles()

            path = str(s[0])
            path = os.path.abspath(path)

            self.restart_mesh_path = RelOrAbsPath(path, self.case['case_path'])
            self.model.setRestartMeshPath(self.restart_mesh_path)
            self.lineEditRestartMesh.setText(self.restart_mesh_path)

            log.debug("slotSearchRestartDirectory-> %s" % self.restart_mesh_path)


    @pyqtSlot()
    def slotStartRestart(self):
        """
        Input IRESTART Code_Saturne keyword.
        """
        if self.radioButtonYes.isChecked():
            if not self.restart_path or self.restart_path == '*':
                self.slotSearchRestartDirectory()
        elif self.radioButtonAuto.isChecked():
            self.restart_path = '*'
        else:
            self.restart_path = None

        if self.restart_path:
            self.model.setRestartPath(self.restart_path)
            if self.restart_path == '*':
                self.radioButtonYes.setChecked(False)
                self.radioButtonAuto.setChecked(True)
                self.labelRestartDir.setEnabled(False)
                self.toolButton.hide()
                self.updateRestartTimes()
            else:
                self.radioButtonYes.setChecked(True)
                self.radioButtonAuto.setChecked(False)
                self.labelRestartDir.setEnabled(True)
                self.toolButton.show()
            self.radioButtonNo.setChecked(False)
            self.frameRestart.show()
        else:
            self.model.setRestartPath(None)
            self.model.setRestartMeshPath(None)
            self.model.setFrozenField("off")
            self.radioButtonYes.setChecked(False)
            self.radioButtonNo.setChecked(True)
            self.checkBoxRestartMesh.setChecked(False)
            self.frameRestart.hide()
            self.lineEdit.setText("")
            self.updateRestartTimes()

        self.updateRestartMeshView()


    @pyqtSlot()
    def slotRestartMesh(self):
        """
        Input different restart mesh.
        """
        if self.checkBoxRestartMesh.isChecked():
            if not self.restart_mesh_path:
                self.slotSearchRestartMesh()

        else:
            self.restart_mesh_path = None

        self.model.setRestartMeshPath(self.restart_mesh_path)

        self.updateRestartMeshView()


    @pyqtSlot()
    def slotFrozenField(self):
        """
        Input if calculation on frozen velocity and pressure fields or not
        """
        if self.checkBox.isChecked():
            self.model.setFrozenField('on')
        else:
            self.model.setFrozenField('off')


    @pyqtSlot()
    def slotAdvancedOptions(self):
        """
        Ask one popup for advanced specifications
        """
        freq, period = self.model.getRestartRescue()

        default                           = {}
        default['restart']                = self.model.getRestartPath()
        default['restart_with_auxiliary'] = self.model.getRestartWithAuxiliaryStatus()
        default['restart_rescue']         = freq
        default['period_rescue']          = period
        log.debug("slotAdvancedOptions -> %s" % str(default))

        dialog = StartRestartAdvancedDialogView(self, self.case, default)

        if dialog.exec_():
            result = dialog.get_result()
            log.debug("slotAdvancedOptions -> %s" % str(result))
            self.model.setRestartWithAuxiliaryStatus(result['restart_with_auxiliary'])
            self.model.setRestartRescue(result['restart_rescue'])


    def tr(self, text):
        """
        Translation
        """
        return text

#-------------------------------------------------------------------------------
# Testing part
#-------------------------------------------------------------------------------

if __name__ == "__main__":
    pass

#-------------------------------------------------------------------------------
# End
#-------------------------------------------------------------------------------