Salome HOME
sauve0206
[tools/eficas.git] / generator / generator_TELEMAC.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2013   EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 """Ce module contient le plugin generateur de fichier au format  Code_Carmel3D pour EFICAS.
21 """
22
23 import traceback
24 import types,string,re,os
25 from Extensions.i18n import tr
26 from generator_python import PythonGenerator
27
28 extensions=('.comm',)
29 try :
30   from aideAuxConvertisseurs import listeSupprime, DicoAglomere, DicoEficasToCas
31   from enumDicoTelemac       import DicoEnumCasEnInverse
32 except :
33   pass
34
35
36 def entryPoint():
37    """
38       Retourne les informations necessaires pour le chargeur de plugins
39       Ces informations sont retournees dans un dictionnaire
40    """
41    return {
42         # Le nom du plugin
43           'name' : 'TELEMAC',
44         # La factory pour creer une instance du plugin
45           'factory' : TELEMACGenerator,
46           }
47
48
49 class TELEMACGenerator(PythonGenerator):
50    """
51       Ce generateur parcourt un objet de type JDC et produit
52       un texte au format eficas et 
53       un texte au format dictionnaire
54
55    """
56
57 #----------------------------------------------------------------------------------------
58    def gener(self,obj,format='brut',config=None):
59        
60       self.initDico()
61       #print self.texteDico
62       
63       # Cette instruction genere le contenu du fichier de commandes (persistance)
64       self.text=PythonGenerator.gener(self,obj,format)
65       return self.text
66
67
68 #----------------------------------------------------------------------------------------
69 # initialisations
70 #----------------------------------------------------------------------------------------
71    
72    def initDico(self) :
73  
74       self.PE=False
75       self.FE=False
76       self.VE=False
77       self.textPE = 'PRESCRIBED ELEVATIONS :'
78       self.textFE = 'PRESCRIBED FLOWRATES :'
79       self.textVE = 'PRESCRIBED VELOCITY :'
80       self.texteDico = ""
81  
82
83
84
85 #----------------------------------------------------------------------------------------
86 # ecriture
87 #----------------------------------------------------------------------------------------
88
89    def writeDefault(self,fn) :
90        self.texteDico+='\n&ETA\n'
91        fileDico = fn[:fn.rfind(".")] + '.py'
92        f = open( str(fileDico), 'wb')
93        f.write( self.texteDico )
94        f.close()
95
96 #----------------------------------------------------------------------------------------
97 #  analyse de chaque noeud de l'arbre 
98 #----------------------------------------------------------------------------------------
99
100    def generPROC_ETAPE(self,obj):
101         self.texteDico += '/------------------------------------------------------/\n'
102         self.texteDico += '/\t\t\t'+obj.nom +'\n'
103         self.texteDico += '/------------------------------------------------------/\n'
104         s=PythonGenerator.generPROC_ETAPE(self,obj)
105         #print obj
106         #print obj.nom
107         if obj.nom in TELEMACGenerator.__dict__.keys() : apply(TELEMACGenerator.__dict__[obj.nom],(self,obj))
108         
109         return s
110
111    def generMCSIMP(self,obj) :
112         """recuperation de l objet MCSIMP"""
113         s=PythonGenerator.generMCSIMP(self,obj)
114         if obj.nom == "Title" :
115             #print s
116             print str(obj.valeur)
117             #print repr(obj.valeur)
118
119        
120         # Attention pas sur --> ds certains cas non traite par MCFACT ?
121         # a reflechir avec Yoann 
122         if hasattr(obj.definition,'defaut') :
123            if obj.definition.defaut == obj.valeur : return s
124
125         nomMajuscule=obj.nom.upper()
126         nom=nomMajuscule.replace('_',' ') 
127         if nom in listeSupprime or s == "" : return s
128
129         sTelemac=s[0:-1]
130         if not( type(obj.valeur) in (types.TupleType,types.ListType) ):
131            if obj.nom in DicoEnumCasEnInverse.keys():  
132              try : sTelemac=str(DicoEnumCasEnInverse[obj.nom][obj.valeur])
133              except : print "generMCSIMP Pb valeur avec ", obj.nom, obj.valeur
134         if type(obj.valeur) in (types.TupleType,types.ListType) :
135            if obj.nom in DicoEnumCasEnInverse.keys():  
136              sT = "'"
137              for v in obj.valeur:
138                try : sT +=str(DicoEnumCasEnInverse[obj.nom][v]) +";"
139                except : print "generMCSIMP Pb Tuple avec ", obj.nom, v, obj.valeur
140              sTelemac=sT[0:-1]+"'"
141
142         if nom in DicoEficasToCas.keys() : nom=DicoEficasToCas[nom]
143         self.texteDico+=nom+ ":" + str(sTelemac) + "\n"
144         return s
145
146    def generMCFACT(self,obj):
147       """
148       """
149       s=PythonGenerator.generMCFACT(self,obj)
150       if obj.nom in TELEMACGenerator.__dict__.keys() : apply(TELEMACGenerator.__dict__[obj.nom],(self,obj))
151  
152       return s
153
154   
155    def Liquid_Boundaries(self,obj):
156       #print obj.nom , dir(obj)
157       if 'Type_Condition' in  obj.liste_mc_presents() :
158           objForme=obj.get_child('Type_Condition')
159           valForme=objForme.valeur
160           if valForme == None : return
161
162           nomBloc='b_'+valForme.split(" ")[1] 
163           if nomBloc in  obj.liste_mc_presents() :
164              objBloc=obj.get_child(nomBloc)
165              objValeur=objBloc.get_child(objBloc.liste_mc_presents()[0])
166              valeur=objValeur.valeur
167              if valeur== None : valeur="0."
168           if valForme == 'Prescribed Elevations' :
169               self.PE=True
170               self.textPE += str(valeur) +"; "
171           else : self.textPE += "0.; "
172           if valForme == 'Prescribed Flowrates' :
173               self.FE=True
174               self.textFE += str(valeur) +"; "
175           else : self.textFE += "0.; "
176           if valForme == 'Prescribed Velocity'  :
177               self.VE=True
178               self.textVE += str(valeur) +"; "
179           else : self.textVE += "0.; "
180       #print self.textPE, self.textFE,self.textVE
181
182    def BOUNDARY_CONDITIONS(self,obj):
183        if self.FE :  self.texteDico += self.textFE[0:-1]+'\n' 
184        if self.VE :  self.texteDico += self.textVE[0:-1]+'\n' 
185        if self.PE :  self.texteDico += self.textPE[0:-1]+'\n' 
186
187    def Validation(self,obj):
188        self.texteDico += "VALIDATION : True \n"
189  
190    def Date_De_L_Origine_Des_Temps (self,obj):
191        an=obj.get_child('Year').valeur
192        mois=obj.get_child('Month').valeur
193        jour=obj.get_child('Day').valeur
194        #print an, mois, jour
195        self.texteDico += "ORIGINAL DATE OF TIME  :"+ str(an)+ " ,"+str(mois)+ "," +str(jour)+ "\n"
196
197    def Original_Hour_Of_Time (self,obj):
198        hh=obj.get_child('Hour').valeur
199        mm=obj.get_child('Minute').valeur
200        ss=obj.get_child('Second').valeur
201        #print hh, mm, ss
202        self.texteDico += "ORIGINAL HOUR OF TIME :"+str(hh)+" ,"+str(mm)+ ","+str(ss)+"\n"
203
204    def Type_Of_Advection(self,obj):
205        listeAdvection=[1,5,1,1]
206        listeSupg=[2,2,2,2]
207        listeUpwind=[1.,1.,1.,1.]
208        self.listeMCAdvection=[]
209        self.chercheChildren(obj)
210        dicoSuf={ 'U_And_V' : 0, 'H' : 1, 'K_And_Epsilon' : 2, 'Tracers' : 3}
211        for c in  self.listeMCAdvection:
212            #print c.nom
213            if c.nom[0:18] == 'Type_Of_Advection_' and c.valeur!=None:
214               suf=c.nom[18:]
215               index=dicoSuf[suf]
216               #print c.valeur
217               #print DicoEnumCasEnInverse['Type_Of_Advection']
218               listeAdvection[index]=DicoEnumCasEnInverse['Type_Of_Advection'][c.valeur]
219            if c.nom[0:13] == 'Supg_Option_' and c.valeur!=None:
220               suf=c.nom[13:]
221               index=dicoSuf[suf]
222               listeAdvection[index]=DicoEnumCasEnInverse['Supg_Option'][c.valeur]
223            if c.nom[0:23] == 'Upwind_Coefficients_Of_' and c.valeur!=None:
224               suf=c.nom[23:]
225               index=dicoSuf[suf]
226               listeUpwind[index]=c.valeur
227        self.texteDico += "TYPE OF ADVECTION = "+ str(listeAdvection) + "\n"
228        self.texteDico += "SUPG OPTION = "+ str(listeSupg) + "\n"
229        self.texteDico += "UPWIND COEFFICIENTS = "+ str(listeUpwind) + "\n"
230        
231    def chercheChildren(self,obj):
232        for c in obj.liste_mc_presents():
233            objc=obj.get_child(c)
234            if hasattr(objc,'liste_mc_presents') and objc.liste_mc_presents() != [] : self.chercheChildren(objc)
235            else : self.listeMCAdvection.append(objc)
236
237       
238