File: ImmersedBoundariesViewNeptune.py

package info (click to toggle)
code-saturne 7.0.2%2Brepack-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 62,868 kB
  • sloc: ansic: 395,271; f90: 100,755; python: 86,746; cpp: 6,227; makefile: 4,247; xml: 2,389; sh: 1,091; javascript: 69
file content (636 lines) | stat: -rw-r--r-- 21,880 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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# -*- coding: utf-8 -*-

#-------------------------------------------------------------------------------

# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2021 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 Immersed boundaries view data management.

This module contains the following classes and function:
- SyrthesVerbosityDelegate
- ProjectionAxisDelegate
- SelectionCriteriaDelegate
- StandardItemModelSyrthes
- ConjugateHeatTransferView
"""

#-------------------------------------------------------------------------------
# Standard modules
#-------------------------------------------------------------------------------

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 LABEL_LENGTH_MAX, GuiParam
from code_saturne.Base.QtPage import IntValidator, DoubleValidator, RegExpValidator, ComboModel
from code_saturne.Base.QtPage import from_qvariant, to_text_string
from code_saturne.Pages.ImmersedBoundariesNeptune import Ui_ImmersedBoundariesNeptune
from code_saturne.model.ImmersedBoundariesModel import ImmersedBoundariesModel
from code_saturne.Pages.QMegEditorView import QMegEditorView

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

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

#-------------------------------------------------------------------------------
# QLineEdit delegate to attach a label to the FSI object
#-------------------------------------------------------------------------------

class FSIObjectNameDelegate(QItemDelegate):

    def __init__(self, parent = None):
        super(FSIObjectNameDelegate, self).__init__(parent)
        self.parent = parent


    def createEditor(self, parent, option, index):
        editor = QLineEdit(parent)
        return editor


    def setEditorData(self, editor, index):
        self.value = from_qvariant(index.model().data(index, Qt.DisplayRole),
                                   to_text_string)
        editor.setText(self.value)


    def setModelData(self, editor, model, index):
        value = editor.text()

        if str(value) != "":
            model.setData(index, value, Qt.DisplayRole)


#-------------------------------------------------------------------------------
# QComboBox delegate for the FSI type : Set motion or computed from fluid forces
#-------------------------------------------------------------------------------

class FSITypeDelegate(QItemDelegate):
    """
    Use of a combobox to set the fsi interaction type
    """

    def __init__(self, parent, mdl):
        super(FSITypeDelegate, self).__init__(parent)
        self.parent  = parent
        self.mdl     = mdl


    def createEditor(self, parent, option, index):
        editor = QComboBox(parent)

        for itm in ["imposed", "computed"]:
            editor.addItem(itm)

        editor.installEventFilter(self)
        return editor


    def setEditorData(self, comboBox, index):
        row = index.row()
        col = index.column()
        string = index.model().dataFSI[row][col]
        comboBox.setEditText(string)


    def setModelData(self, comboBox, model, index):
        value = comboBox.currentText()
        model.setData(index, value, Qt.DisplayRole)

#-------------------------------------------------------------------------------
# QLineEdit delegate for validation of Syrthes verbosity or visualization
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# QComboBox delegate for Axis Projection in Conjugate Heat Transfer table
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# StandarItemModel class
#-------------------------------------------------------------------------------

class StandardItemModelFSI(QStandardItemModel):

    def __init__(self, model):
        """
        """
        QStandardItemModel.__init__(self)

        self.headers = [self.tr("Object name"),
                        self.tr("Interaction type")]
        self.tooltip = [self.tr("Name of solid object"),
                        self.tr("Type of motion interaction with the flow")]

        self.setColumnCount(len(self.headers))
        self.dataFSI = []
        self.__model = model


    def data(self, index, role):
        if not index.isValid():
            return None

        # Tooltips
        if role == Qt.ToolTipRole:
            return self.tooltip[index.column()]

        # Display
        if role == Qt.DisplayRole:
            return self.dataFSI[index.row()][index.column()]
        elif role == Qt.TextAlignmentRole:
            return Qt.AlignCenter

        return None


    def flags(self, index):
        if not index.isValid():
            return Qt.ItemIsEnabled

        return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable


    def headerData(self, section, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self.headers[section]
        return None


    def setData(self, index, value, role):
        if not index.isValid():
            return

        row = index.row()
        col = index.column()

        self.dataFSI[row][col] = str(from_qvariant(value, to_text_string))

        num = row + 1
        self.__model.setObjectName(num, self.dataFSI[row][0])
        self.__model.setObjectInteraction(num, self.dataFSI[row][1])

#        self.dataChanged.emit(index, index)

        id1 = self.index(0, 0)
        id2 = self.index(self.rowCount(), 0)
        self.dataChanged.emit(id1, id2)
        return True


    def getData(self, index):
        row = index.row()
        return self.dataFSI[row]

    def addItem(self, object_name, interaction_type):
        """
        Add a row in the table.
        """
        self.dataFSI.append([object_name, interaction_type])
        row = self.rowCount()
        self.setRowCount(row+1)


    def deleteRow(self, row):
        """
        Delete the row in the model
        """
        del self.dataFSI[row]
        row = self.rowCount()
        self.setRowCount(row-1)

    def getItem(self, row):
        return self.dataFSI[row]

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

class ImmersedBoundariesViewNeptune(QWidget, Ui_ImmersedBoundariesNeptune):
    """
    """
    def __init__(self, parent, case):
        """
        Constructor
        """
        QWidget.__init__(self, parent)

        Ui_ImmersedBoundariesNeptune.__init__(self)
        self.setupUi(self)

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

        self.ibm = ImmersedBoundariesModel(self.case)

        self.current_obj = None

        # Models
        self.modelFSI = StandardItemModelFSI(self.ibm)
        self.tableViewFSI.setModel(self.modelFSI)

        for obj in range(1,self.ibm.getNumberOfFSIObjects()+1):
            self.modelFSI.addItem(self.ibm.getObjectName(obj),
                                  self.ibm.getObjectInteraction(obj))

        if QT_API == "PYQT4":
            self.tableViewFSI.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
            self.tableViewFSI.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
#            self.tableViewFSI.horizontalHeader().setResizeMode(2, QHeaderView.Stretch)
        elif QT_API == "PYQT5":
            self.tableViewFSI.verticalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
            self.tableViewFSI.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
#            self.tableViewFSI.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch)

        self.modelFSI.dataChanged.connect(self.dataChanged)

        delegateObjectLabel  = FSIObjectNameDelegate(self.tableViewFSI)
        self.tableViewFSI.setItemDelegateForColumn(0, delegateObjectLabel)

        delegateObjectType   = FSITypeDelegate(self.tableViewFSI, self.ibm)
        self.tableViewFSI.setItemDelegateForColumn(1, delegateObjectType)

        self.checkBoxActivate.stateChanged.connect(self.slotCheckActivate)

        self.tableViewFSI.clicked[QModelIndex].connect(self.slotChangedSelection)


        for ind in ['Explicit', 'MEDCoupling']:
            eval('self.radioButton'+ind+'.toggled.connect(self.slotRadioButton)')

        # Connections
        self.pushButtonAddFSI.clicked.connect(self.slotAddFSI)
        self.pushButtonDeleteFSI.clicked.connect(self.slotDeleteFSI)

        self.pushButtonExplicit.clicked.connect(self.slotExplicitFormula)

        validatorDensity = DoubleValidator(self.lineEditObjDensity, min = 0.0)
        self.lineEditObjDensity.setValidator(validatorDensity)
        self.lineEditObjDensity.textChanged[str].connect(self.slotObjDensity)

        validatorStiffness = DoubleValidator(self.lineEditObjStiffness, min = 0.0)
        self.lineEditObjStiffness.setValidator(validatorStiffness)
        self.lineEditObjStiffness.textChanged[str].connect(self.slotObjStiffness)

        validatorDamping = DoubleValidator(self.lineEditObjDamping, min = 0.0)
        self.lineEditObjDamping.setValidator(validatorDamping)
        self.lineEditObjDamping.textChanged[str].connect(self.slotObjDamping)

        self.lineEditXInit.textChanged[str].connect(self.slotObjXinit)
        self.lineEditYInit.textChanged[str].connect(self.slotObjYinit)
        self.lineEditZInit.textChanged[str].connect(self.slotObjZinit)

        self.lineEditXEq.textChanged[str].connect(self.slotObjXeq)
        self.lineEditYEq.textChanged[str].connect(self.slotObjYeq)
        self.lineEditZEq.textChanged[str].connect(self.slotObjZeq)

        self.lineEditVelXInit.textChanged[str].connect(self.slotObjVelXinit)
        self.lineEditVelYInit.textChanged[str].connect(self.slotObjVelYinit)
        self.lineEditVelZInit.textChanged[str].connect(self.slotObjVelZinit)

        self.lineEditAccXInit.textChanged[str].connect(self.slotObjAccXinit)
        self.lineEditAccYInit.textChanged[str].connect(self.slotObjAccYinit)
        self.lineEditAccZInit.textChanged[str].connect(self.slotObjAccZinit)

        # Check for MEDCoupling presence
        from code_saturne import cs_config
        cfg = cs_config.config()
        self.has_medcoupling = cfg.libs['medcoupling'].have == 'yes'
        # deactivated for the moment
        self.has_medcoupling = False

        self.radioButtonMEDCoupling.setEnabled(self.has_medcoupling)
        if self.ibm.getMethod() == 'medcoupling' and self.has_medcoupling == False:
            self.setMethod('explicit')

        # Show/hide widgets on start
        if self.ibm.getOnOff() == 'off':
            self.groupBoxMethod.hide()
            self.groupBoxObjects.hide()
            self.groupBoxObjProperties.hide()
            self.groupBoxExplicit.hide()
            self.groupBoxMEDCoupling.hide()
        else:
            self.groupBoxMethod.show()
            self.groupBoxObjects.show()
            if self.ibm.getMethod() == 'explicit':
                self.groupBoxExplicit.show()
            else:
                self.groupBoxMEDCoupling.show()

        self.updatePageView()

        self.case.undoStartGlobal()


    @pyqtSlot("QModelIndex")
    def slotChangedSelection(self, index):
        """
        detect change in selection and update view
        """
        row = self.tableViewFSI.currentIndex().row()
        self.current_obj = row + 1
        self.updatePageView()


    @pyqtSlot(int)
    def slotCheckActivate(self, val):

        # Set the method state
        if val == 0:
            self.ibm.setOnOff('off')
        else:
            self.ibm.setOnOff('on')

        # Update the view if needed
        self.updatePageView()

    def dataChanged(self, topLeft, bottomRight):
        self.updatePageView()

    def updatePageView(self):

        if self.ibm.getOnOff() == 'off':
            self.checkBoxActivate.setChecked(False)
            self.groupBoxMethod.hide()
            self.groupBoxObjects.hide()
            self.groupBoxExplicit.hide()
            self.groupBoxMEDCoupling.hide()
            self.radioButtonExplicit.setChecked(False)
            self.radioButtonMEDCoupling.setChecked(False)

        else:
            self.checkBoxActivate.setChecked(True)
            self.groupBoxMethod.show()
            self.groupBoxObjects.show()

            # Which button to show for the solid definition
            if self.current_obj:
                if self.ibm.getMethod() == 'explicit':
                    self.groupBoxExplicit.show()
                    self.groupBoxMEDCoupling.hide()
                    self.radioButtonExplicit.setChecked(True)
                    self.radioButtonMEDCoupling.setChecked(False)
                elif self.ibm.getMethod() == 'medcoupling':
                    self.groupBoxExplicit.hide()
                    self.groupBoxMEDCoupling.show()
                    self.radioButtonExplicit.setChecked(False)
                    self.radioButtonMEDCoupling.setChecked(True)
                else:
                    self.radioButtonExplicit.setChecked(False)
                    self.radioButtonMEDCoupling.setChecked(False)
                    self.groupBoxExplicit.hide()
                    self.groupBoxMEDCoupling.hide()

                if self.ibm.getObjectInteraction(self.current_obj) == 'computed':
                    self.groupBoxObjProperties.show()
                    # Set correct values for each slot
                    self.lineEditObjDensity.setText(str(
                            self.ibm.getObjectDensity(self.current_obj)))

                    self.lineEditObjStiffness.setText(str(
                            self.ibm.getObjectStiffness(self.current_obj)))

                    self.lineEditObjDamping.setText(str(
                            self.ibm.getObjectDamping(self.current_obj)))

                    x0,y0,z0 = self.ibm.getObjectInitPosition(self.current_obj)
                    self.lineEditXInit.setText(x0)
                    self.lineEditYInit.setText(y0)
                    self.lineEditZInit.setText(z0)

                    xe,ye,ze = self.ibm.getObjectEqPosition(self.current_obj)
                    self.lineEditXEq.setText(xe)
                    self.lineEditYEq.setText(ye)
                    self.lineEditZEq.setText(ze)

                    vx,vy,vz = self.ibm.getObjectInitVel(self.current_obj)
                    self.lineEditVelXInit.setText(vx)
                    self.lineEditVelYInit.setText(vy)
                    self.lineEditVelZInit.setText(vz)

                    ax,ay,az = self.ibm.getObjectInitAcc(self.current_obj)
                    self.lineEditAccXInit.setText(ax)
                    self.lineEditAccYInit.setText(ay)
                    self.lineEditAccZInit.setText(az)
                else:
                    self.groupBoxObjProperties.hide()

            else:
                self.groupBoxExplicit.hide()
                self.groupBoxMEDCoupling.hide()
                self.groupBoxObjProperties.hide()


    @pyqtSlot()
    def slotRadioButton(self):

        for ind in ['Explicit', 'MEDCoupling']:

            radioButton = eval('self.radioButton'+ind)
            if radioButton.isChecked():
                self.ibm.setMethod(ind.lower())

        self.updatePageView()


    @pyqtSlot()
    def slotAddFSI(self):

        name        = '_'.join([self.ibm.defaultValues()['fsi_object_name'],
                                str(self.ibm.getNumberOfFSIObjects()+1)])
        interaction = self.ibm.defaultValues()['fsi_interaction']

        num = self.ibm.addFSIObject(name, interaction)
        self.modelFSI.addItem(name, interaction)


    @pyqtSlot()
    def slotDeleteFSI(self):
        row = self.tableViewFSI.currentIndex().row()
        log.debug("slotDeleteFSI -> %s" % (row,))
        if row == -1:
            title = self.tr("Warning")
            msg   = self.tr("You must select an existing object")
            QMessageBox.information(self, title, msg)
        else:
            self.modelFSI.deleteRow(row)
            self.ibm.deleteFSIObject(row+1)


    @pyqtSlot()
    def slotExplicitFormula(self):
        """
        Explicit formula for variable porosity
        """

        objId = self.current_obj

        exp, req, sym = self.ibm.getIBMFormulaComponents(objId-1)
        exa = """if (x < 0.5)
                   indicator = 0;
                 else
                   indicator = 1;"""

        name = self.ibm.getObjectName(objId)

        dialog = QMegEditorView(parent        = self,
                                function_type = 'ibm',
                                zone_name     = name,
                                variable_name = 'porosity',
                                expression    = exp,
                                required      = req,
                                symbols       = sym,
                                known_fields  = [],
                                examples      = exa)

        if dialog.exec_():
            result = dialog.get_result()
            log.debug("slotExplicitFormula -> %s" % str(result))
            self.ibm.setObjectFormula(objId-1, result)
            self.pushButtonExplicit.setStyleSheet("background-color: green")
            self.pushButtonExplicit.setToolTip(exp)


    @pyqtSlot(str)
    def slotObjDensity(self, text):
        num = self.tableViewFSI.currentIndex().row() + 1
        val = float(text)
        self.ibm.setObjectDensity(num, val)


    @pyqtSlot(str)
    def slotObjStiffness(self, text):
        num = self.tableViewFSI.currentIndex().row() + 1
        val = float(text)
        self.ibm.setObjectStiffness(num, val)


    @pyqtSlot(str)
    def slotObjDamping(self, text):
        num = self.tableViewFSI.currentIndex().row() + 1
        val = float(text)
        self.ibm.setObjectDamping(num, val)


    @pyqtSlot(str)
    def slotObjXinit(self, text):
        num = self.tableViewFSI.currentIndex().row() + 1
        val = float(text)
        self.ibm.setObjectInitPosition(num, xini=val)


    @pyqtSlot(str)
    def slotObjYinit(self, text):
        num = self.tableViewFSI.currentIndex().row() + 1
        val = float(text)
        self.ibm.setObjectInitPosition(num, yini=val)


    @pyqtSlot(str)
    def slotObjZinit(self, text):
        num = self.tableViewFSI.currentIndex().row() + 1
        val = float(text)
        self.ibm.setObjectInitPosition(num, zini=val)


    @pyqtSlot(str)
    def slotObjXeq(self, text):
        num = self.tableViewFSI.currentIndex().row() + 1
        val = float(text)
        self.ibm.setObjectEqPosition(num, xeq=val)


    @pyqtSlot(str)
    def slotObjYeq(self, text):
        num = self.tableViewFSI.currentIndex().row() + 1
        val = float(text)
        self.ibm.setObjectEqPosition(num, yeq=val)


    @pyqtSlot(str)
    def slotObjZeq(self, text):
        num = self.tableViewFSI.currentIndex().row() + 1
        val = float(text)
        self.ibm.setObjectEqPosition(num, zeq=val)


    @pyqtSlot(str)
    def slotObjVelXinit(self, text):
        num = self.tableViewFSI.currentIndex().row() + 1
        val = float(text)
        self.ibm.setObjectInitVel(num, vx=val)


    @pyqtSlot(str)
    def slotObjVelYinit(self, text):
        num = self.tableViewFSI.currentIndex().row() + 1
        val = float(text)
        self.ibm.setObjectInitVel(num, vy=val)


    @pyqtSlot(str)
    def slotObjVelZinit(self, text):
        num = self.tableViewFSI.currentIndex().row() + 1
        val = float(text)
        self.ibm.setObjectInitVel(num, vz=val)


    @pyqtSlot(str)
    def slotObjAccXinit(self, text):

        num = self.tableViewFSI.currentIndex().row() + 1
        val = float(text)
        self.ibm.setObjectInitAcc(num, ax=val)

    @pyqtSlot(str)
    def slotObjAccYinit(self, text):

        num = self.tableViewFSI.currentIndex().row() + 1
        val = float(text)
        self.ibm.setObjectInitAcc(num, ay=val)

    @pyqtSlot(str)
    def slotObjAccZinit(self, text):

        num = self.tableViewFSI.currentIndex().row() + 1
        val = float(text)
        self.ibm.setObjectInitAcc(num, az=val)


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