Salome HOME
Telemac 1eres lectures/ecriture
[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 from aideAuxConvertisseurs import listeSupprime, DicoAglomere, DicoEficasToCas
30 from enumDicoTelemac       import DicoEnumCasEnInverse
31
32
33 def entryPoint():
34    """
35       Retourne les informations necessaires pour le chargeur de plugins
36       Ces informations sont retournees dans un dictionnaire
37    """
38    return {
39         # Le nom du plugin
40           'name' : 'TELEMAC',
41         # La factory pour creer une instance du plugin
42           'factory' : TELEMACGenerator,
43           }
44
45
46 class TELEMACGenerator(PythonGenerator):
47    """
48       Ce generateur parcourt un objet de type JDC et produit
49       un texte au format eficas et 
50       un texte au format dictionnaire
51
52    """
53
54 #----------------------------------------------------------------------------------------
55    def gener(self,obj,format='brut',config=None):
56        
57       self.initDico()
58       #print self.texteDico
59       
60       # Cette instruction genere le contenu du fichier de commandes (persistance)
61       self.text=PythonGenerator.gener(self,obj,format)
62       return self.text
63
64
65 #----------------------------------------------------------------------------------------
66 # initialisations
67 #----------------------------------------------------------------------------------------
68    
69    def initDico(self) :
70  
71       self.PE=False
72       self.FE=False
73       self.VE=False
74       self.textPE = 'PRESCRIBED ELEVATIONS :'
75       self.textFE = 'PRESCRIBED FLOWRATES :'
76       self.textVE = 'PRESCRIBED VELOCITY :'
77       self.texteDico = ""
78  
79
80
81
82 #----------------------------------------------------------------------------------------
83 # ecriture
84 #----------------------------------------------------------------------------------------
85
86    def writeDefault(self,fn) :
87        self.texteDico+='\n&ETA\n'
88        fileDico = fn[:fn.rfind(".")] + '.py'
89        f = open( str(fileDico), 'wb')
90        f.write( self.texteDico )
91        f.close()
92
93 #----------------------------------------------------------------------------------------
94 #  analyse de chaque noeud de l'arbre 
95 #----------------------------------------------------------------------------------------
96
97    def generPROC_ETAPE(self,obj):
98         self.texteDico += '/------------------------------------------------------/\n'
99         self.texteDico += '/\t\t\t'+obj.nom +'\n'
100         self.texteDico += '/------------------------------------------------------/\n'
101         s=PythonGenerator.generPROC_ETAPE(self,obj)
102         #print obj
103         #print obj.nom
104         if obj.nom in TELEMACGenerator.__dict__.keys() : apply(TELEMACGenerator.__dict__[obj.nom],(self,obj))
105         
106         return s
107
108    def generMCSIMP(self,obj) :
109         """recuperation de l objet MCSIMP"""
110         s=PythonGenerator.generMCSIMP(self,obj)
111         if obj.nom == "Title" :
112             #print s
113             print str(obj.valeur)
114             #print repr(obj.valeur)
115
116        
117         # Attention pas sur --> ds certains cas non traite par MCFACT ?
118         # a reflechir avec Yoann 
119         if hasattr(obj.definition,'defaut') :
120            if obj.definition.defaut == obj.valeur : return s
121
122         nomMajuscule=obj.nom.upper()
123         nom=nomMajuscule.replace('_',' ') 
124         if nom in listeSupprime or s == "" : return s
125
126         sTelemac=s[0:-1]
127         if not( type(obj.valeur) in (types.TupleType,types.ListType) ):
128            if obj.nom in DicoEnumCasEnInverse.keys():  
129              try : sTelemac=str(DicoEnumCasEnInverse[obj.nom][obj.valeur])
130              except : print "generMCSIMP Pb valeur avec ", obj.nom, obj.valeur
131         if type(obj.valeur) in (types.TupleType,types.ListType) :
132            if obj.nom in DicoEnumCasEnInverse.keys():  
133              sT = "'"
134              for v in obj.valeur:
135                try : sT +=str(DicoEnumCasEnInverse[obj.nom][v]) +";"
136                except : print "generMCSIMP Pb Tuple avec ", obj.nom, v, obj.valeur
137              sTelemac=sT[0:-1]+"'"
138
139         if nom in DicoEficasToCas.keys() : nom=DicoEficasToCas[nom]
140         self.texteDico+=nom+ ":" + str(sTelemac) + "\n"
141         return s
142
143    def generMCFACT(self,obj):
144       """
145       """
146       s=PythonGenerator.generMCFACT(self,obj)
147       if obj.nom in TELEMACGenerator.__dict__.keys() : apply(TELEMACGenerator.__dict__[obj.nom],(self,obj))
148  
149       return s
150
151   
152    def Liquid_Boundaries(self,obj):
153       #print obj.nom , dir(obj)
154       if 'Type_Condition' in  obj.liste_mc_presents() :
155           objForme=obj.get_child('Type_Condition')
156           valForme=objForme.valeur
157           if valForme == None : return
158
159           nomBloc='b_'+valForme.split(" ")[1] 
160           if nomBloc in  obj.liste_mc_presents() :
161              objBloc=obj.get_child(nomBloc)
162              objValeur=objBloc.get_child(objBloc.liste_mc_presents()[0])
163              valeur=objValeur.valeur
164              if valeur== None : valeur="0."
165           if valForme == 'Prescribed Elevations' :
166               self.PE=True
167               self.textPE += str(valeur) +"; "
168           else : self.textPE += "0.; "
169           if valForme == 'Prescribed Flowrates' :
170               self.FE=True
171               self.textFE += str(valeur) +"; "
172           else : self.textFE += "0.; "
173           if valForme == 'Prescribed Velocity'  :
174               self.VE=True
175               self.textVE += str(valeur) +"; "
176           else : self.textVE += "0.; "
177       #print self.textPE, self.textFE,self.textVE
178
179    def BOUNDARY_CONDITIONS(self,obj):
180        if self.FE :  self.texteDico += self.textFE[0:-1]+'\n' 
181        if self.VE :  self.texteDico += self.textVE[0:-1]+'\n' 
182        if self.PE :  self.texteDico += self.textPE[0:-1]+'\n' 
183
184    def Validation(self,obj):
185        self.texteDico += "VALIDATION : True \n"
186  
187    def Date_De_L_Origine_Des_Temps (self,obj):
188        an=obj.get_child('Year').valeur
189        mois=obj.get_child('Month').valeur
190        jour=obj.get_child('Day').valeur
191        #print an, mois, jour
192        self.texteDico += "ORIGINAL DATE OF TIME  :"+ str(an)+ " ,"+str(mois)+ "," +str(jour)+ "\n"
193
194    def Original_Hour_Of_Time (self,obj):
195        hh=obj.get_child('Hour').valeur
196        mm=obj.get_child('Minute').valeur
197        ss=obj.get_child('Second').valeur
198        #print hh, mm, ss
199        self.texteDico += "ORIGINAL HOUR OF TIME :"+str(hh)+" ,"+str(mm)+ ","+str(ss)+"\n"
200
201    def Type_Of_Advection(self,obj):
202        listeAdvection=[1,5,1,1]
203        listeSupg=[2,2,2,2]
204        listeUpwind=[1.,1.,1.,1.]
205        self.listeMCAdvection=[]
206        self.chercheChildren(obj)
207        dicoSuf={ 'U_And_V' : 0, 'H' : 1, 'K_And_Epsilon' : 2, 'Tracers' : 3}
208        for c in  self.listeMCAdvection:
209            #print c.nom
210            if c.nom[0:18] == 'Type_Of_Advection_' and c.valeur!=None:
211               suf=c.nom[18:]
212               index=dicoSuf[suf]
213               #print c.valeur
214               #print DicoEnumCasEnInverse['Type_Of_Advection']
215               listeAdvection[index]=DicoEnumCasEnInverse['Type_Of_Advection'][c.valeur]
216            if c.nom[0:13] == 'Supg_Option_' and c.valeur!=None:
217               suf=c.nom[13:]
218               index=dicoSuf[suf]
219               listeAdvection[index]=DicoEnumCasEnInverse['Supg_Option'][c.valeur]
220            if c.nom[0:23] == 'Upwind_Coefficients_Of_' and c.valeur!=None:
221               suf=c.nom[23:]
222               index=dicoSuf[suf]
223               listeUpwind[index]=c.valeur
224        self.texteDico += "TYPE OF ADVECTION = "+ str(listeAdvection) + "\n"
225        self.texteDico += "SUPG OPTION = "+ str(listeSupg) + "\n"
226        self.texteDico += "UPWIND COEFFICIENTS = "+ str(listeUpwind) + "\n"
227        
228    def chercheChildren(self,obj):
229        for c in obj.liste_mc_presents():
230            objc=obj.get_child(c)
231            if hasattr(objc,'liste_mc_presents') and objc.liste_mc_presents() != [] : self.chercheChildren(objc)
232            else : self.listeMCAdvection.append(objc)
233
234       
235