# -*- coding: iso-8859-1 -*-
#
#-------------------------------------------------------------------------------
#                    Code_Saturne version 1.3
#                    ------------------------
#
#
#     This file is part of the Code_Saturne User Interface, element of the
#     Code_Saturne CFD tool.
#
#     Copyright (C) 1998-2007 EDF S.A., France
#
#     contact: saturne-support@edf.fr
#
#     The Code_Saturne User Interface 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.
#
#     The Code_Saturne User Interface 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 the Code_Saturne Kernel; if not, write to the
#     Free Software Foundation, Inc.,
#     51 Franklin St, Fifth Floor,
#     Boston, MA  02110-1301  USA
#
#-------------------------------------------------------------------------------


"""
This module defines the lagrangian two phase flow modelling management.

This module contains the following classes and function:
- LagrangianModel
- LagrangianView
- PageText
- LagrangianTestCase
"""


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


import sys, unittest
import Tix
from Tkconstants import *


#-------------------------------------------------------------------------------
# Application modules import
#-------------------------------------------------------------------------------


from Base.Common import *
import Base.Toolbox as Tool
import Base.Dialog as Dialog
import Base.TkPage as TkPage


#-------------------------------------------------------------------------------
# lagrangian model class
#-------------------------------------------------------------------------------


class LagrangianModel:
    """
    """
    def __init__(self, case):
        """
        Constructor.
        """
        self.case = case

        nModels         = self.case.xmlGetNode('thermophysical_models')
        self.node_gas   = nModels.xmlGetNode('gas_combustion',  'model')
        self.node_coal  = nModels.xmlGetNode('pulverized_coal', 'model')
        self.node_joule = nModels.xmlGetNode('joule_effect',    'model')
        self.node_therm = nModels.xmlGetNode('thermal_scalar',  'model')

        self.node_lagr = self.case.xmlGetNode('lagrangian')

        self.lagrangianModel = ('off',
                                'one_way',
                                'two_way',
                                'frozen')

#    def getTurbulenceModel(self):
#        """
#	Return the turbulence model. Warning, this method doesn't handle
#	the initial default value of the turbulence model. Therefore, in
#	the XMLmodel class, the order of the call TurbulenceModel, and
#        LagrangianModel should not be change.
#	"""
#	turb = nModels.xmlGetNode('turbulence',  'model')
#	if not turb: exit(1)
#	model = turb['model']
#	if not model: exit(1)
#	return model
 

    def defaultLagrangianValues(self):
        """
        Return in a dictionnary which contains default values.
        """
        default = {}
        default['model'] = "off"

        return default


    def getAllLagrangianModels(self):
        """
        Return all defined lagrangian models in a tuple.
        """
        return self.lagrangianModel


    def lagrangianModelsList(self):
        """
        Create a tuple with the lagrangian models allowed
        by the analysis features.
        """
        import Turbulence
        model = Turbulence.TurbulenceModel(self.case).getTurbulenceModel()
        del Turbulence
        if model not in ('off',
                         'k-epsilon',
                         'k-epsilon-PL',
                         'Rij-epsilon',
                         'Rij-SSG',
                         'v2f-phi',
                         'k-omega-SST'):
            return ('off',)
        else:
             return self.lagrangianModel
        


    def setLagrangianModel(self, model):
        """
        Update the lagrangian model markup from the XML document.
        """
        if model not in self.lagrangianModelsList():
#TODO MSG
            sys.exit(1)

        self.node_lagr['model'] = model

        if model == 'off':
            import CoalCombustion
            coal = CoalCombustion.CoalCombustionModel(self.case).getCoalCombustionModel()
            if coal == 'coal_lagr':
                CoalCombustion.CoalCombustionModel(self.case).setCoalCombustion('off')
            del CoalCombustion


    def getLagrangianModel(self):
        """
        Return the current lagrangian model.
        """
        model = self.node_lagr['model']
        if model not in self.lagrangianModelsList():
            model = self.defaultLagrangianValues()['model']
            self.setLagrangianModel(model)

        return model


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


class LagrangianView(TkPage.Page):
    """
    Class to open Lagrangian Page.
    """
    def _dependsPages(self, name):
        """
        Construction of the list of dependencies Pages.
        """
        pass


    def _tkControlVariables(self):
        """
        Tkinter variables declaration.
        """
        self.lagrangian = StringVar()


    def _pageControl(self):
        """
        Instantiate the lagrangian modelling class.
        """
        self.model = LagrangianModel(self.case)


    def putLagrangianModel(self, event=None):
        """
        """
        self.model.setLagrangianModel(self.lagrangian.get())


    def _createWidgets(self):
        """
        Create the Page layout.
        """
        t = PageText()
            
        lf = Tix.LabelFrame(self.myPage, bd=2, label=t.LAGRANGIAN, relief=FLAT)
        lf.label.config(font=fT)
        lf.pack(side=TOP, fill=X, padx=10, pady=10)

        # Button for the lagrangian modelling choice
        #
        self.b = Tix.OptionMenu(lf.frame, options='menubutton.width 45')
        self.b.menubutton.config(bd=2, relief=RAISED)
        self.balloon.bind_widget(self.b, statusmsg=t.MSG_LAGR,
                                 balloonmsg=t.KEYWORD+'IILAGR')
        
        self.b.pack(side=TOP, pady=15, padx=10)

        self.b.add_command('off', label=t.NO_LAGR)
        self.b.add_separator('')
        self.b.add_command('one_way', label=t.ONE_WAY)
        self.b.add_command('two_way', label=t.TWO_WAY)
        self.b.add_command('frozen',  label=t.FROZEN)

        self.b.config(variable=self.lagrangian ,
                    command= self.putLagrangianModel)


    def _initializeWidgets(self):
        """
        Extract resquested informations from XML document.
        This informations are used to initialize the widgets.
        For this page default values of all widgets are necessary
        included in the XML file.
        """
        # Update the lagrangian list with the analysis features
        #
        for mdl in self.model.lagrangianModel:
            if mdl not in self.model.lagrangianModelsList():
                self.b.disable(mdl)

        # Select the lagrangian model
        #
        model = self.model.getLagrangianModel()
        self.b.config(value=model)


#-------------------------------------------------------------------------------
# Text and messages for this page
#-------------------------------------------------------------------------------


class PageText:
    """
    Storage of all texts and messages for this page.
    """
    def __init__(self):
        
        # 1) Texts
        #
        if Tool.GuiParam.lang == 'fr':
            self.LAGRANGIAN        = "Écoulement multiphasique Eulérien/Lagangien"
            self.KEYWORD           = "Mots clé Code_Saturne : "
            self.NO_LAGR           = "Pas de modélisation lagrangienne"
            self.ONE_WAY           = "Couplage simple"
            self.TWO_WAY           = "Couplage retour"
            self.FROZEN            = "Particules sur champs figés"
        else:
            self.LAGRANGIAN        = "Eulerian/Lagangian Multi-phase Treatment"
            self.KEYWORD           = "Code_Saturne key words: "
            self.NO_LAGR           = "No lagrangian modelling"
            self.ONE_WAY           = "One way coupling"
            self.TWO_WAY           = "Two way coupling"
            self.FROZEN            = "Particles on frozen fields"

        # 2) Messages
        #
        if Tool.GuiParam.lang == 'fr':
            self.MSG_LAGR     = "Sélectionner un modèle lagrangien"
        else:
            self.MSG_LAGR     = "Select a lagrangian modelling"


#-------------------------------------------------------------------------------
# Lagrangian test case
#-------------------------------------------------------------------------------


class LagrangianTestCase(unittest.TestCase):
    """
    """
    def setUp(self):
        """
        This method is executed before all "check" methods.
        """
        from Base.XMLengine import Case
        from Base.XMLinitialize import XMLinit
        self.case = Case()
        XMLinit(self.case)


    def tearDown(self):
        """
        This method is executed after all "check" methods.
        """
        del self.case


    def checkLagrangianInstantiation(self):
        """
        Check whether the LagrangianModel class could be instantiated
        """
        model = None
        model = LagrangianModel(self.case)
        
        assert model != None, 'Could not instantiate LagrangianModel'


    def checkLagrangianModelsList(self):
        """
        Check whether the LagrangianModelsList could be get
        """
        mdl = LagrangianModel(self.case)
        self.case.xmlGetNode('turbulence')['model'] = 'LES_Smagorinsky'
        assert mdl.lagrangianModelsList() == ('off',),\
               'Could not use the lagrangianModelsList method'


def suite():
    testSuite = unittest.makeSuite(LagrangianTestCase, "check")
    return testSuite

    
def runTest():
    print "LagrangianTestCase to be completed..."
    runner = unittest.TextTestRunner()
    runner.run(suite())


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


syntax highlighted by Code2HTML, v. 0.9.1