File: FluidStructureInteractionView.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 (878 lines) | stat: -rw-r--r-- 31,719 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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
# -*- 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 values of reference.

This module contains the following classes and function:
- FluidStructureInteractionAdvancedOptionsView
- StandardItemModel
- Coupling
- LineEditCoupling
- FormulaCoupling
- CheckBoxCoupling
- CouplingManager
- FluidStructureInteractionView
"""

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

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.Base.QtPage                          import DoubleValidator, IntValidator
from code_saturne.Base.QtPage                          import to_qvariant, from_qvariant
from code_saturne.Pages.FluidStructureInteractionForm  import Ui_FluidStructureInteractionForm
from code_saturne.model.FluidStructureInteractionModel import FluidStructureInteractionModel
from code_saturne.model.LocalizationModel              import LocalizationModel
from code_saturne.model.Boundary                       import Boundary
from code_saturne.Pages.FluidStructureInteractionAdvancedOptionsDialogForm import \
Ui_FluidStructureInteractionAdvancedOptionsDialogForm

from code_saturne.Pages.QMeiEditorView import QMeiEditorView
from code_saturne.model.NotebookModel import NotebookModel

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

logging.basicConfig()
log = logging.getLogger("FluidStructureInteractionView")

#-------------------------------------------------------------------------------
# Constants
#-------------------------------------------------------------------------------

displacement_prediction_alpha          = 'displacement_prediction_alpha'
displacement_prediction_beta           = 'displacement_prediction_beta'
stress_prediction_alpha                = 'stress_prediction_alpha'
monitor_point_synchronisation          = 'monitor_point_synchronisation'

#-------------------------------------------------------------------------------
# Advanced dialog
#-------------------------------------------------------------------------------

class FluidStructureInteractionAdvancedOptionsView(QDialog,
                        Ui_FluidStructureInteractionAdvancedOptionsDialogForm):
    """
    Advanced dialog
    """
    def __init__(self, parent, case, default):
        """
        Constructor
        """
        # Init base classes
        QDialog.__init__(self, parent)
        Ui_FluidStructureInteractionAdvancedOptionsDialogForm.__init__(self)
        self.setupUi(self)

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

        title = self.tr("Displacements prediction:")
        self.setWindowTitle(title)

        self.__default = default
        self.__result  = default.copy()
        self.__setValidator()
        self.__setInitialValues()

        self.case.undoStartGlobal()


    def __setValidator(self):
        """
        Set the validator
        """
        validator = DoubleValidator(self.lineEditDisplacementAlpha,
                                    min=0.0)
        self.lineEditDisplacementAlpha.setValidator(validator)

        validator = DoubleValidator(self.lineEditDisplacementBeta,
                                    min=0.0)
        self.lineEditDisplacementBeta.setValidator(validator)

        validator = DoubleValidator(self.lineEditStressAlpha, min=0.0)
        self.lineEditStressAlpha.setValidator(validator)


    def __setInitialValues(self):
        """
        Set the initial values for the 4 widgets
        """
        # Read from default
        displacementAlpha = str(self.__default[displacement_prediction_alpha])
        displacementBeta  = str(self.__default[displacement_prediction_beta ])
        stressAlpha       = str(self.__default[stress_prediction_alpha      ])

        isSynchronizationOn = self.__default[monitor_point_synchronisation] == 'on'

        # Update Widget
        self.lineEditDisplacementAlpha.setText(displacementAlpha)
        self.lineEditDisplacementBeta.setText(displacementBeta)
        self.lineEditStressAlpha.setText(stressAlpha)
        self.checkBoxSynchronization.setChecked(isSynchronizationOn)


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


    def accept(self):
        """
        Method called when user clicks 'OK'
        """
        # Read value from widget
        displacementAlpha = float(self.lineEditDisplacementAlpha.text())
        displacementBeta  = float(self.lineEditDisplacementBeta.text())
        stressAlpha       = float(self.lineEditStressAlpha.text())

        if self.checkBoxSynchronization.isChecked():
            synchronization = 'on'
        else:
            synchronization = 'off'

        # Set result attributes
        self.__result[displacement_prediction_alpha] = displacementAlpha
        self.__result[displacement_prediction_beta ] = displacementBeta
        self.__result[stress_prediction_alpha      ] = stressAlpha
        self.__result[monitor_point_synchronisation] = synchronization

        QDialog.accept(self)


    def reject(self):
        """
        Method called when user clicks 'Cancel'
        """
        QDialog.reject(self)


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


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

class StandardItemModel(QStandardItemModel):
    """
    StandardItemModel for table view
    """

    def __init__(self):
        """
        StandarItemModel for fluid structure interaction tableView
        """
        QStandardItemModel.__init__(self)

        # Define header
        self.headers = [self.tr("Structure number"),
                        self.tr("Label"),
                        self.tr("Location")]
        self.setColumnCount(len(self.headers))

        # Set attributes
        self.__data    = []


    def data(self, index, role):
        """
        Called when table need to read the data
        """
        # return value only for Qt.DisplayRole
        if index.isValid() and role == Qt.DisplayRole:
            row = index.row()
            col = index.column()
            return to_qvariant(self.__data[row][col])

        return to_qvariant()


    def flags(self, index):
        """
        Define which column is editable/selectable/enable.
        """
        return Qt.ItemIsEnabled | Qt.ItemIsSelectable


    def headerData(self, section, orientation, role):
        """
        Return the header column data.
        """
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return to_qvariant(self.headers[section])
        return to_qvariant()


    def setData(self, index, value, role):
        """
        Set a the data when table changed
        """
        raise Exception('Cannot edit column')


    def addItem(self, zone):
        """
        Add an element in the table view.
        """
        index = len(self.__data)
        line = [index + 1, zone.getLabel(), zone.getLocalization()]

        # Add the line and increment line number
        self.__data.append(line)
        row = self.rowCount()
        self.setRowCount(row + 1)


    def getLabel(self, index):
        """
        return the label
        """
        row = index.row()
        [index, label, localization] = self.__data[row]
        return label


#-------------------------------------------------------------------------------
# Coupling base class
#-------------------------------------------------------------------------------

class Coupling:
    """
    Coupling is the base class to manage all widgets whose value depend on the
    boundary.

    It provides getBoundaryDefinedValue/setBoundaryDefinedValue methods which
    gets/sets the value of a boundary attribute. Getter and setter are specified
    in the constructor

    It also automatically enables/disables the widget when boundary is present
    or not

    Derived classes can override onBoundarySet method to specify the initial
    value of the widget base on the boundary
    """

    def __init__(self, widget, getterStr, setterStr ):
        """
        Constructor. getterStr and setterStr are string
        """
        self.__widget    = widget
        self.__getterStr = getterStr
        self.__setterStr = setterStr
        self.__boundary  = None

        # As no boundary is selected disable the widget
        widget.setEnabled(False)


    def getWidget(self):
        """
        Return the widget
        """
        return self.__widget


    def setBoundary(self, boundary):
        """
        Set the current boundary
        """
        self.__boundary = boundary

        #Enable widget
        self.__widget.setEnabled(True)

        # call onBoundarySet for derived class
        self.onBoundarySet()


    def onBoundarySet(self):
        """
        Called when boundary is set. Nothing by default
        """
        pass


    def getBoundaryDefinedValue(self):
        """
        Return the value of the boundary using the getter function
        """
        return getattr(self.__boundary, self.__getterStr)()


    def setBoundaryDefinedValue(self, value):
        """
        Set the value of the boundary using the setter function
        """
        getattr(self.__boundary, self.__setterStr)(value)


#-------------------------------------------------------------------------------
# LineEdit Coupling class
#-------------------------------------------------------------------------------

class LineEditCoupling(Coupling):
    """
    LineEdit that depends on a boundary
    """

    def __init__(self, lineEdit, getter, setter):
        """
        Constructor
        """
        Coupling.__init__(self, lineEdit, getter, setter)

        # Add validator.
        validator = DoubleValidator(lineEdit)
        lineEdit.setValidator(validator)
        lineEdit.textChanged[str].connect(self.__slotTextChanged)


    def onBoundarySet(self):
        """
        Called when boundary is set. Update lineEdit text
        """
        value  = self.getBoundaryDefinedValue()
        self.getWidget().setText(str(value))


    # NOTE: using a decorated slot to connect to a signal is usually recommended,
    # as it is slightly faster and uses less memory, but is causes a crash
    # with PyQt5 (not PyQt4) when connecting to a signal from another class.

    # @pyqtSlot(str)
    def __slotTextChanged(self, text):
        """
        Update the model
        """
        self.setBoundaryDefinedValue(text)

#-------------------------------------------------------------------------------
# Formula Coupling class
#-------------------------------------------------------------------------------

class FormulaCoupling(Coupling):
    """
    Formula button that depend on a boundary
    """

    def __init__(self, button, getter, setter, default, required, symbols, examples):
        """
        Constructor
        """
        Coupling.__init__(self, button, getter, setter)

        self.__default  = default
        self.__required = required
        self.__examples = examples
        self.__symbols  = symbols

        button.clicked.connect(self.__slotFormula)


    def onBoundarySet(self):
        """
        Called when boundary is set.
        """
        # call getter to create default value if needed
        self.getBoundaryDefinedValue()


    # NOTE: as above, do not use decorator to avoid crash in PyQt5.

    # @pyqtSlot(bool)
    def __slotFormula(self, checked):
        """
        Run formula editor.
        """
        # Read current expression
        exp = self.getBoundaryDefinedValue()

        if not exp:
            exp = self.__default

        # run the editor
        dialog = QMeiEditorView(self.getWidget(),
                                check_syntax = None,
                                expression = exp,
                                required   = self.__required,
                                symbols    = self.__symbols,
                                examples   = self.__examples)
        if dialog.exec_():
            result = dialog.get_result()
            log.debug("FormulaCoupling -> %s" % str(result))
            self.setBoundaryDefinedValue(result)

#-------------------------------------------------------------------------------
# CheckBoxCouplings Coupling class
#-------------------------------------------------------------------------------

class CheckBoxCoupling(Coupling):
    """
    CheckBox that depend on a boundary
    """

    def __init__(self, checkBox, getter, setter):
        """
        Constructor
        """
        Coupling.__init__(self, checkBox, getter, setter)
        checkBox.stateChanged[int].connect(self.__slotStateChanged)


    def onBoundarySet(self):
        """
        Called when boundary is set.
        """
        value = self.getBoundaryDefinedValue()

        if value == "on":
            state = Qt.Checked
        else:
            state = Qt.Unchecked
        self.getWidget().setCheckState(state)

    # NOTE: as above, do not use decorator to avoid crash in PyQt5.

    # @pyqtSlot(int)
    def __slotStateChanged(self, state):
        """
        Called when checkbox state changed
        """
        value = "off"
        if state == Qt.Checked:
            value = "on"
        self.setBoundaryDefinedValue(value)

#-------------------------------------------------------------------------------
# CouplingManager class
#-------------------------------------------------------------------------------

class CouplingManager:
    """
    Manage and initialize coupling derived objects
    """

    def __init__(self, mainView, case,
                 internalTableView, internalTableModel,
                 externalTableView, externalTableModel):
        """
        Constructor
        """
        self.case               = case
        self.case.undoStopGlobal()
        self.__internalTableView = internalTableView
        self.__externalTableView = externalTableView
        self.__internalTableModel = internalTableModel
        self.__externalTableModel = externalTableModel
        self.__internalCouplings = []
        self.__externalCouplings = []

        # Init widgets
        self.__initLineEditCouplings(mainView)
        self.__initFormulaCouplings (mainView)
        self.__initCheckBoxCouplings(mainView)
        self.case.undoStartGlobal()


    def __initLineEditCouplings(self, mainView):
        """
        Initialize the creation of LineEditCoupling
        """
        couplings = []
        couplings.append(LineEditCoupling(mainView.lineEditInitialDisplacementX,
                                          "getInitialDisplacementX",
                                          "setInitialDisplacementX"))
        couplings.append(LineEditCoupling(mainView.lineEditInitialDisplacementY,
                                          "getInitialDisplacementY",
                                          "setInitialDisplacementY"))
        couplings.append(LineEditCoupling(mainView.lineEditInitialDisplacementZ,
                                          "getInitialDisplacementZ",
                                          "setInitialDisplacementZ"))

        couplings.append(LineEditCoupling(mainView.lineEditEquilibriumDisplacementX,
                                          "getEquilibriumDisplacementX",
                                          "setEquilibriumDisplacementX"))

        couplings.append(LineEditCoupling(mainView.lineEditEquilibriumDisplacementY,
                                          "getEquilibriumDisplacementY",
                                          "setEquilibriumDisplacementY"))
        couplings.append(LineEditCoupling(mainView.lineEditEquilibriumDisplacementZ,
                                          "getEquilibriumDisplacementZ",
                                          "setEquilibriumDisplacementZ"))

        couplings.append(LineEditCoupling(mainView.lineEditInitialVelocityX,
                                          "getInitialVelocityX",
                                          "setInitialVelocityX"))
        couplings.append(LineEditCoupling(mainView.lineEditInitialVelocityY,
                                          "getInitialVelocityY",
                                          "setInitialVelocityY"))
        couplings.append(LineEditCoupling(mainView.lineEditInitialVelocityZ,
                                          "getInitialVelocityZ",
                                          "setInitialVelocityZ"))
        self.__internalCouplings.extend(couplings)


    def __initCheckBoxCouplings(self, mainView):
        """
        Initialize the creation of the checkbox coupling
        """
        couplings = []
        couplings.append(CheckBoxCoupling(mainView.checkBoxDDLX,
                                          "getDDLX", "setDDLX"))
        couplings.append(CheckBoxCoupling(mainView.checkBoxDDLY,
                                          "getDDLY", "setDDLY"))
        couplings.append(CheckBoxCoupling(mainView.checkBoxDDLZ,
                                          "getDDLZ", "setDDLZ"))
        self.__externalCouplings.extend(couplings)


    def __initFormulaCouplings(self, mainView):
        """
        Initialize the creation of the formula button
        """
        default = "%(t)s11 = ;"
        defaultRequired = [('%(t)s11', '%(n)s matrix of the structure (1,1)'),
                           ('%(t)s22', '%(n)s matrix of the structure (2,2)'),
                           ('%(t)s33', '%(n)s matrix of the structure (3,3)'),
                           ('%(t)s12', '%(n)s matrix of the structure (1,2)'),
                           ('%(t)s13', '%(n)s matrix of the structure (1,3)'),
                           ('%(t)s23', '%(n)s matrix of the structure (2,3)'),
                           ('%(t)s21', '%(n)s matrix of the structure (2,1)'),
                           ('%(t)s31', '%(n)s matrix of the structure (3,1)'),
                           ('%(t)s32', '%(n)s matrix of the structure (3,2)')]
        symbols = [('dt', 'time step'),
                   ('t', 'current time'),
                   ('nbIter', 'number of iteration')]

        # Add notebook symbols
        self.notebook = NotebookModel(self.case)
        for (nme, val) in self.notebook.getNotebookList():
            symbols.append((nme, 'value (notebook) = ' + str(val)))

        m_default = default % {'t':'m'}
        c_default = default % {'t':'c'}
        k_default = default % {'t':'k'}

        m_default_required = []
        c_default_required = []
        k_default_required = []
        for v, s in defaultRequired:
            m_default_required.append((v % {'t':'m'}, s % {'n':'mass'}))
            c_default_required.append((v % {'t':'c'}, s % {'n':'damping'}))
            k_default_required.append((v % {'t':'k'}, s % {'n':'stiffness'}))

        m_examples = """# Mass of the structure: 5 kg
#
m11 = 5;\nm22 = 5;\nm33 = 5;\nm12 = 0;\nm13 = 0;\nm23 = 0;\nm21 = 0;\nm31 = 0;\nm32 = 0;
"""
        c_examples = """# Damping of the structure: 3 kg.s
#
c11 = 3;\nc22 = 3;\nc33 = 3;\nc12 = 0;\nc13 = 0;\nc23 = 0;\nc21 = 0;\nc31 = 0;\nc32 = 0;
"""
        k_examples = """# Stiffness of the structure: 2 N/m
#
k11 = 2;\nk22 = 2;\nk33 = 2;\nk12 = 0;\nk13 = 0;\nk23 = 0;\nk21 = 0;\nk31 = 0;\nk32 = 0;
"""

        couplings = []
        couplings.append(FormulaCoupling(mainView.pushButtonMassMatrix,
                                         "getMassMatrix", "setMassMatrix",
                                         m_default, m_default_required,
                                         symbols, m_examples))

        couplings.append(FormulaCoupling(mainView.pushButtonDampingMatrix,
                                         "getDampingMatrix", "setDampingMatrix",
                                         c_default, c_default_required,
                                         symbols, c_examples))

        couplings.append(FormulaCoupling(mainView.pushButtonStiffnessMatrix,
                                         "getStiffnessMatrix", "setStiffnessMatrix",
                                         k_default, k_default_required,
                                         symbols, k_examples))

        defaultFluidForce  = "fx = "
        requiredFluidForce = [('fx', 'force applied to the structure along X'),
                              ('fy', 'force applied to the structure along Y'),
                              ('fz', 'force applied to the structure along Z')]
        symbolsFluidForce = symbols[:];
        symbolsFluidForce.append(('fluid_fx', 'force of flow along X'))
        symbolsFluidForce.append(('fluid_fy', 'force of flow along Y'))
        symbolsFluidForce.append(('fluid_fz', 'force of flow along Z'))

        examplesFluidForce = """# The fluid force is zero in the Y direction.
#
fx = fluid_fx;\nfy = 0;\nfz = fluid_fz;"""
        couplings.append( FormulaCoupling(mainView.pushButtonFluidForce,
                                          "getFluidForceMatrix",
                                          "setFluidForceMatrix",
                                          defaultFluidForce,
                                          requiredFluidForce,
                                          symbolsFluidForce,
                                          examplesFluidForce))
        self.__internalCouplings.extend(couplings)


    # NOTE: as above, do not use decorator to avoid crash in PyQt5.

    # @pyqtSlot(QItemSelection, QItemSelection)
    def slotInternalSelectionChanged(self, selected, deselected):
        """
        Called when internal tableView selection changed
        """
        self.__selectionChanged(self.__internalTableView,
                                self.__internalTableModel,
                                self.__internalCouplings, selected)

    # NOTE: as above, do not use decorator to avoid crash in PyQt5.

    # @pyqtSlot(QItemSelection, QItemSelection)
    def slotExternalSelectionChanged(self, selected, deselected):
        """
        Called when external tableView selection changed
        """
        self.__selectionChanged(self.__externalTableView,
                                self.__externalTableModel,
                                self.__externalCouplings, selected)


    def __selectionChanged(self, tableView, tableModel, couplings, selected):
        """
        Called when a tableView selection changed
        """
        # Get Boundary
        index = tableView.currentIndex()
        label = tableModel.getLabel(index)

        boundary = Boundary("coupling_mobile_boundary", label, self.case)

        # Set boundary for coupling
        for coupling in couplings:
            coupling.setBoundary(boundary)

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

class FluidStructureInteractionView(QWidget, Ui_FluidStructureInteractionForm):
    """
    Main class.
    """
    def __init__(self, parent, case):
        """
        Constructor
        """
        # Init base classes
        QWidget.__init__(self, parent)

        Ui_FluidStructureInteractionForm.__init__(self)
        self.setupUi(self)

        self.case = case
        self.case.undoStopGlobal()
        self.__model = FluidStructureInteractionModel(case)

        self.__defineConnection()
        self.__addValidators()
        self.__setInitialValues()

        # Use localization model for column 0, 1, 3
        modelLocalization  = LocalizationModel("BoundaryZone", case)

        # Store modelLocalization as attribut to avoid garbage collector to clean it
        self.__modelLocalization = modelLocalization

        # Initialize the internal and external TableViewItemModel
        self.__internalTableModel = self.__createTableViewItemModel(modelLocalization,
                                                                    'internal_coupling')
        self.__externalTableModel = self.__createTableViewItemModel(modelLocalization,
                                                                    'external_coupling')

        # Coupling Manager
        couplingManager = CouplingManager(self, case,
                                          self.tableInternalCoupling,
                                          self.__internalTableModel,
                                          self.tableExternalCoupling,
                                          self.__externalTableModel)
        # Avoid garbage collector to delete couplingManager
        self.__couplingManager = couplingManager

        # Initialize internal / external table view
        self.__initTableView(self.tableInternalCoupling,
                             self.__internalTableModel,
                             couplingManager.slotInternalSelectionChanged)

        self.__initTableView(self.tableExternalCoupling,
                             self.__externalTableModel,
                             couplingManager.slotExternalSelectionChanged)
        self.case.undoStartGlobal()


    def __defineConnection(self):
        """
        Define connection for widgets that do not depend on the boundary
        """
        self.lineEditNALIMX.textChanged[str].connect(self.__slotNalimx)

        self.lineEditEPALIM.textChanged[str].connect(self.__slotEpalim)
        self.pushButtonAdvanced.clicked.connect(self.__slotAdvanced)


    def __addValidators(self):
        """
        Add the validator for NALIMX and EPALIM
        """
        validatorNALIMX = IntValidator(self.lineEditNALIMX, min=1)
        self.lineEditNALIMX.setValidator(validatorNALIMX)

        validatorEPALIM = DoubleValidator(self.lineEditEPALIM, min=0.0)
        validatorEPALIM.setExclusiveMin(True)
        self.lineEditEPALIM.setValidator(validatorEPALIM)


    def __setInitialValues(self):
        """
        Set Widget initial values that do not depend on the boundary
        """
        nalimx = self.__model.getMaxIterations()
        self.lineEditNALIMX.setText(str(nalimx))
        epalim = self.__model.getPrecision()
        self.lineEditEPALIM.setText(str(epalim))


    def __createTableViewItemModel(self, modelLocalization, filterALE):
        """
        Create the table view item model
        """
        tableViewItemModel = StandardItemModel()

        # Populate QTableView model
        for zone in modelLocalization.getZones():
            boundary = Boundary(zone.getNature(), zone.getLabel(), self.case)
            if boundary.getALEChoice() == filterALE:
                tableViewItemModel.addItem(zone)
        return tableViewItemModel


    def __initTableView(self, tableView, tableViewItemModel, slotSelectionChanged):
        """
        Initialize the main table view
        """
        # Set the model
        tableView.setModel(tableViewItemModel)

        # set the column size
        if QT_API == "PYQT4":
            tableView.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
            tableView.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
            tableView.horizontalHeader().setResizeMode(2, QHeaderView.Stretch)
        elif QT_API == "PYQT5":
            tableView.verticalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
            tableView.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
            tableView.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch)

        # Connect slot when selection changed
        tableView.setSelectionBehavior(QAbstractItemView.SelectRows)
        selectionModel = QItemSelectionModel(tableViewItemModel, tableView)
        tableView.setSelectionModel(selectionModel)
        selectionModel.selectionChanged.connect(slotSelectionChanged)


    @pyqtSlot(str)
    def __slotNalimx(self, text):
        """
        Input viscosity type of mesh : isotrop or orthotrop.
        """
        if self.sender().validator().state == QValidator.Acceptable:
            nalimx = from_qvariant(text, int)
            self.__model.setMaxIterations(nalimx)


    @pyqtSlot(str)
    def __slotEpalim(self, text):
        """
        Input viscosity type of mesh : isotrop or orthotrop.
        """
        if self.sender().validator().state == QValidator.Acceptable:
            epalim = from_qvariant(text, float)
            self.__model.setPrecision(epalim)


    @pyqtSlot()
    def __slotAdvanced(self):
        """
        Private slot.
        Ask one popup for advanced specifications
        """
        # Set the default value
        default = {}
        default[displacement_prediction_alpha] = self.__model.getDisplacementPredictionAlpha()
        default[displacement_prediction_beta ] = self.__model.getDisplacementPredictionBeta()
        default[stress_prediction_alpha      ] = self.__model.getStressPredictionAlpha()
        default[monitor_point_synchronisation] = \
                            self.__model.getMonitorPointSynchronisation()
        log.debug("slotAdvancedOptions -> %s" % str(default))

        # run the dialog
        dialog = FluidStructureInteractionAdvancedOptionsView(self, self.case, default)
        if dialog.exec_():
            # Set the model with the dialog results
            result = dialog.get_result()
            log.debug("slotAdvanced -> %s" % str(result))
            self.__model.setDisplacementPredictionAlpha(result[displacement_prediction_alpha])
            self.__model.setDisplacementPredictionBeta(result[displacement_prediction_beta])
            self.__model.setStressPredictionAlpha(result[stress_prediction_alpha])
            self.__model.setMonitorPointSynchronisation(result[monitor_point_synchronisation])


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

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