Salome HOME
Pour Telemac et qques bugs
[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 #if 1:
30 try :
31    from enumDicoTelemac       import TelemacdicoEn
32    DicoEnumCasEnInverse={}
33    for motClef in TelemacdicoEn.keys():
34      d={}
35      for valTelemac in TelemacdicoEn[motClef].keys():
36         valEficas= TelemacdicoEn[motClef][valTelemac]
37         d[valEficas]=valTelemac
38      DicoEnumCasEnInverse[motClef]=d
39
40 except :
41  pass
42
43
44 def entryPoint():
45    """
46       Retourne les informations necessaires pour le chargeur de plugins
47       Ces informations sont retournees dans un dictionnaire
48    """
49    return {
50         # Le nom du plugin
51           'name' : 'TELEMAC',
52         # La factory pour creer une instance du plugin
53           'factory' : TELEMACGenerator,
54           }
55
56
57 class TELEMACGenerator(PythonGenerator):
58    """
59       Ce generateur parcourt un objet de type JDC et produit
60       un texte au format eficas et 
61       un texte au format dictionnaire
62
63    """
64
65 #----------------------------------------------------------------------------------------
66    def gener(self,obj,format='brut',config=None,appli=None,statut="Entier"):
67        
68       self.statut=statut
69       self.langue=appli.langue
70       self.initDico()
71       # Pour Simplifier les verifs d ecriture
72       if hasattr(appli,'listeTelemac') : self.listeTelemac=appli.listeTelemac
73       else : self.listeTelemac = ()
74
75       self.dicoCataToCas={}
76       self.dicoCasToCata=appli.readercata.dicoCasToCata
77       for motClef in self.dicoCasToCata.keys():
78            self.dicoCataToCas[self.dicoCasToCata[motClef]]=motClef
79
80
81
82       # Cette instruction genere le contenu du fichier de commandes (persistance)
83       self.text=PythonGenerator.gener(self,obj,format)
84       return self.text
85
86
87 #----------------------------------------------------------------------------------------
88 # initialisations
89 #----------------------------------------------------------------------------------------
90    
91    def initDico(self) :
92  
93       self.PE=False
94       self.FE=False
95       self.VE=False
96       if self.langue == "fr" :
97         self.textPE = 'COTES IMPOSEES :'
98         self.textFE = 'DEBITS IMPOSES :'
99         self.textVE = 'VITESSES IMPOSEES :'
100       else :
101         self.textPE = 'PRESCRIBED ELEVATIONS :'
102         self.textFE = 'PRESCRIBED FLOWRATES :'
103         self.textVE = 'PRESCRIBED VELOCITIES :'
104       self.nbTracers = 0
105       self.texteDico = ""
106  
107
108
109
110 #----------------------------------------------------------------------------------------
111 # ecriture de tout
112 #----------------------------------------------------------------------------------------
113
114    def writeDefault(self,fn) :
115        self.texteDico+='\n&ETA\n&FIN\n'
116        if self.statut == 'Leger' : extension = ".Lcas"
117        else                      : extension = ".cas"
118        fileDico = fn[:fn.rfind(".")] + extension 
119        f = open( str(fileDico), 'wb')
120        f.write( self.texteDico )
121        f.close()
122
123 #----------------------------------------------------------------------------------------
124 # ecriture de Leger
125 #----------------------------------------------------------------------------------------
126
127    def writeLeger(self,fn,jdc,config,appli) :
128        jdc_formate=self.gener(jdc,config=config,appli=appli,statut="Leger")
129        self.writeDefault(fn) 
130
131
132 #----------------------------------------------------------------------------------------
133 #  analyse de chaque noeud de l'arbre 
134 #----------------------------------------------------------------------------------------
135
136    def generPROC_ETAPE(self,obj):
137         self.texteDico += '/------------------------------------------------------/\n'
138         self.texteDico += '/\t\t\t'+obj.nom +'\n'
139         self.texteDico += '/------------------------------------------------------/\n'
140         s=PythonGenerator.generPROC_ETAPE(self,obj)
141         #print obj
142         #print obj.nom
143         if obj.nom in TELEMACGenerator.__dict__.keys() : apply(TELEMACGenerator.__dict__[obj.nom],(self,obj))
144         
145         return s
146
147    def generMCSIMP(self,obj) :
148         """recuperation de l objet MCSIMP"""
149         s=PythonGenerator.generMCSIMP(self,obj)
150         #if obj.nom == "Title" :
151             #print s
152          #  print str(obj.valeur)
153             #print repr(obj.valeur)
154
155        
156         # Attention pas sur --> ds certains cas non traite par MCFACT ?
157         # a reflechir avec Yoann 
158         # ajouter le statut ?
159         if self.statut == 'Leger' :
160           if hasattr(obj.definition,'defaut') and (obj.definition.defaut == obj.valeur) and (obj.nom not in self.listeTelemac) : return s
161           if hasattr(obj.definition,'defaut') and obj.definition.defaut != None and (type(obj.valeur) == types.TupleType or type(obj.valeur) == types.ListType) and (tuple(obj.definition.defaut) == tuple(obj.valeur)) and (obj.nom not in self.listeTelemac) : return s
162  
163
164         #nomMajuscule=obj.nom.upper()
165         #nom=nomMajuscule.replace('_',' ') 
166         #if nom in listeSupprime or s == "" : return s
167         if s == "" : return s
168
169       
170        
171         sTelemac=s[0:-1]
172         if not( type(obj.valeur) in (types.TupleType,types.ListType) ):
173            if obj.nom in DicoEnumCasEnInverse.keys():  
174              try : sTelemac=str(DicoEnumCasEnInverse[obj.nom][obj.valeur])
175              except : print "generMCSIMP Pb valeur avec ", obj.nom, obj.valeur
176         if type(obj.valeur) in (types.TupleType,types.ListType) :
177            #print "je passe pour", obj.nom
178            if obj.nom in DicoEnumCasEnInverse.keys():  
179              #sT = "'"
180              sT=''
181              for v in obj.valeur:
182                try : sT +=str(DicoEnumCasEnInverse[obj.nom][v]) +";"
183                except : print "generMCSIMP Pb Tuple avec ", obj.nom, v, obj.valeur
184              #sTelemac=sT[0:-1]+"'"
185              sTelemac=sT[0:-1]
186            else  :
187              sTelemac=sTelemac[0:-1]
188              if sTelemac.find("'") > 0 :
189                 sTelemac= sTelemac.replace (',',';\n    ') 
190
191
192         if self.langue=='fr' :
193            s1=str(sTelemac).replace('True','OUI')
194            s2=s1.replace('False','NON')
195         else :
196            s1=str(sTelemac).replace('True','YES')
197            s2=s1.replace('False','NO')
198         s3=s2.replace(',',';')
199         if s3 != "" and s3[0]=='(' : 
200           try : s3=s3[1:-1] # cas de liste vide
201           except : s3 = ' '
202         
203        
204         # LIQUID_BOUNDARIES
205         if obj.nom in ('PRESCRIBED_FLOWRATES','PRESCRIBED_VELOCITIES','PRESCRIBED_ELEVATIONS') :
206            return s
207
208         if obj.nom not in self.dicoCataToCas :
209            if obj.nom == 'Consigne' : return ""
210            print obj.nom , ' non traite'
211            return s
212
213         nom=self.dicoCataToCas[obj.nom]
214         if nom == "VARIABLES FOR GRAPHIC PRINTOUTS" : s3=s3.replace(';',',')
215         if nom == "VARIABLES POUR LES SORTIES GRAPHIQUES" : s3=s3.replace(';',',')
216         if s3 == "" or s3 == " " : s3 = "None"
217         ligne=nom+ " : " + s3 + "\n"
218         if len(ligne) > 72 : ligne=self.redecoupeLigne(nom,s3) 
219         self.texteDico+=ligne
220         #print "_______________________"
221         #print s
222         #print ligne
223         #print "_______________________"
224         return s
225
226    def generMCFACT(self,obj):
227       """
228       """
229       s=PythonGenerator.generMCFACT(self,obj)
230       if obj.nom in TELEMACGenerator.__dict__.keys() : apply(TELEMACGenerator.__dict__[obj.nom],(self,obj))
231  
232       return s
233
234   
235    def LIQUID_BOUNDARIES(self,obj):
236       if 'BOUNDARY_TYPE' in  obj.liste_mc_presents() :
237           objForme=obj.get_child('BOUNDARY_TYPE')
238           valForme=objForme.valeur
239           if valForme == None : return
240
241           nomBloc='b_'+valForme.split(" ")[1] 
242           if nomBloc in  obj.liste_mc_presents() :
243              objBloc=obj.get_child(nomBloc)
244              objValeur=objBloc.get_child(objBloc.liste_mc_presents()[0])
245              valeur=objValeur.valeur
246              if valeur== None : valeur="0."
247           if valForme == 'Prescribed Elevations' :
248               self.PE=True
249               self.textPE += str(valeur) +"; "
250           else : self.textPE += "0.; "
251           if valForme == 'Prescribed Flowrates' :
252               self.FE=True
253               self.textFE += str(valeur) +"; "
254           else : self.textFE += "0.; "
255           if valForme == 'Prescribed Velocity'  :
256               self.VE=True
257               self.textVE += str(valeur) +"; "
258           else : self.textVE += "0.; "
259       print self.textPE, self.textFE,self.textVE
260
261    def BOUNDARY_CONDITIONS(self,obj):
262        # sans '; '
263        if self.FE :  self.texteDico += self.textFE[0:-2]+'\n' 
264        if self.PE :  self.texteDico += self.textPE[0:-2]+'\n' 
265        if self.VE :  self.texteDico += self.textVE[0:-2]+'\n' 
266
267    def TRACERS(self,obj):
268        if self.nbTracers != 0 :  self.texteDico += 'NUMBER_OF_TRACERS : '+str(self.nbTracers) + '\n'
269  
270
271    def NAME_OF_TRACER(self,obj):
272        print dir(obj) 
273        print obj.get_genealogie_precise()
274
275    def Validation(self,obj):
276        self.texteDico += "VALIDATION : True \n"
277  
278    def Date_De_L_Origine_Des_Temps (self,obj):
279        an=obj.get_child('Year').valeur
280        mois=obj.get_child('Month').valeur
281        jour=obj.get_child('Day').valeur
282        #print an, mois, jour
283        self.texteDico += "ORIGINAL DATE OF TIME  :"+ str(an)+ " ,"+str(mois)+ "," +str(jour)+ "\n"
284
285    def Original_Hour_Of_Time (self,obj):
286        hh=obj.get_child('Hour').valeur
287        mm=obj.get_child('Minute').valeur
288        ss=obj.get_child('Second').valeur
289        #print hh, mm, ss
290        self.texteDico += "ORIGINAL HOUR OF TIME :"+str(hh)+" ,"+str(mm)+ ","+str(ss)+"\n"
291
292    def Type_Of_Advection(self,obj):
293        listeAdvection=[1,5,1,1]
294        listeSupg=[2,2,2,2]
295        listeUpwind=[1.,1.,1.,1.]
296        self.listeMCAdvection=[]
297        self.chercheChildren(obj)
298        dicoSuf={ 'U_And_V' : 0, 'H' : 1, 'K_And_Epsilon' : 2, 'Tracers' : 3}
299        for c in  self.listeMCAdvection:
300            #print c.nom
301            if c.nom[0:18] == 'Type_Of_Advection_' and c.valeur!=None:
302               suf=c.nom[18:]
303               index=dicoSuf[suf]
304               #print c.valeur
305               #print DicoEnumCasEnInverse['Type_Of_Advection']
306               listeAdvection[index]=DicoEnumCasEnInverse['Type_Of_Advection'][c.valeur]
307            if c.nom[0:13] == 'Supg_Option_' and c.valeur!=None:
308               suf=c.nom[13:]
309               index=dicoSuf[suf]
310               listeAdvection[index]=DicoEnumCasEnInverse['Supg_Option'][c.valeur]
311            if c.nom[0:23] == 'Upwind_Coefficients_Of_' and c.valeur!=None:
312               suf=c.nom[23:]
313               index=dicoSuf[suf]
314               listeUpwind[index]=c.valeur
315        self.texteDico += "TYPE OF ADVECTION = "+ str(listeAdvection) + "\n"
316        self.texteDico += "SUPG OPTION = "+ str(listeSupg) + "\n"
317        self.texteDico += "UPWIND COEFFICIENTS = "+ str(listeUpwind) + "\n"
318        
319    def chercheChildren(self,obj):
320        for c in obj.liste_mc_presents():
321            objc=obj.get_child(c)
322            if hasattr(objc,'liste_mc_presents') and objc.liste_mc_presents() != [] : self.chercheChildren(objc)
323            else : self.listeMCAdvection.append(objc)
324
325       
326  
327    def redecoupeLigne(self,nom,valeur) :
328        text=nom+ " : \n"
329        valeur=valeur
330        if valeur.find("'") > -1:
331           lval=valeur.split(";")
332           for v in lval : text+='   '+v+';'
333           text=text[0:-1]+'\n'
334        else :
335          lval=valeur.split(";")
336          ligne="   "
337          for v in lval :
338            if len(ligne) < 70 : ligne += str(v)+'; '
339            else :
340               text+= ligne+"\n"
341               ligne="   "+str(v)+'; '
342          text+= ligne[0:-2]+'\n'
343        return text