Salome HOME
b082e82bff0ee6b0e02df682313617a395dceeb3
[tools/eficas.git] / convert / convert_TELEMAC.py
1 # Copyright (C) 2007-2013   EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19 import re, string
20 from Extensions.i18n import tr
21 from Accas.A_BLOC import BLOC
22 from Accas import *
23
24                                                                                         
25 from convert_python import PythonParser
26
27 pattern_comment_slash   = re.compile(r"^\s*/")
28 pattern_eta   = re.compile(r".*&ETA.*")
29 pattern_fin   = re.compile(r".*&FIN.*")
30 pattern_blanc = re.compile(r"^ *$")
31 pattern_OUI   = re.compile(r"^ *OUI *")
32 pattern_oui   = re.compile(r"^ *oui *")
33 pattern_NON   = re.compile(r"^ *NON *")
34 pattern_non   = re.compile(r"^ *non *")
35
36 from aideAuxConvertisseurs import DicoEficasToCas, ListeSupprimeCasToEficas
37 from aideAuxConvertisseurs import ListeCalculCasToEficas, DicoAvecMajuscules
38 from enumDicoTelemac       import DicoEnumCasEn
39
40 from Extensions import localisation
41
42 from determine import monEnvQT5
43
44
45
46 def entryPoint():
47    """
48    Return a dictionary containing the description needed to load the plugin
49    """
50    return {
51           'name' : 'TELEMAC',
52           'factory' : TELEMACParser
53           }
54
55 class TELEMACParser(PythonParser):
56    """
57    This converter works like PythonParser, except that it also initializes all
58    model variables to None in order to avoid Python syntax errors when loading
59    a file with a different or inexistent definition of variables.
60    """
61
62    def convert(self, outformat, appli=None):
63       self.dicoInverseFrancais=appli.readercata.dicoInverseFrancais
64       self.dicoAnglaisFrancais=appli.readercata.dicoAnglaisFrancais
65       self.dicoFrancaisAnglais=appli.readercata.dicoFrancaisAnglais
66       self.dicoMC=appli.readercata.dicoMC
67       self.Ordre_Des_Commandes=appli.readercata.Ordre_Des_Commandes
68    
69
70       #print self.dicoInverseFrancais
71       #text = PythonParser.convert(self, outformat, appli)
72       
73       text=""
74       l_lignes = string.split(self.text,'\n')
75       self.dictSimp={}
76       for ligne in l_lignes :
77           if pattern_comment_slash.match(ligne) : continue
78           if pattern_eta.match(ligne) : continue
79           if pattern_fin.match(ligne) : continue
80           if pattern_blanc.match(ligne) : continue
81           ligne=re.sub('\t',' ',ligne)
82           ligne=re.sub("'",' ',ligne)
83           ligne=re.sub(":",'=',ligne)
84           if ligne.count('=') != 1 :
85               print "pb avec la ligne " , ligne
86               continue 
87
88           motsInLigne=string.split(ligne,' ')
89           listeMotsSimp=()
90           simp=""
91           for mot in motsInLigne:
92               if mot == ""   : continue
93               if mot == "="  :
94                  simp=simp[0:-1]
95                  while simp[-1] == " " : simp=simp[0:-1]
96                  if simp.find('-') > 0 : simp=self.redecoupeSimp(simp)
97                  break
98
99               mot=mot.replace('_','__')
100               simp=simp+mot[0].upper() +mot[1:].lower()+'_'
101           valeur=ligne.split('=')[1]
102           self.dictSimp[simp]=valeur
103
104       
105       
106       #print dictSimp
107       #print self.dicoInverseFrancais
108
109       dicoParMC={}
110       #print ListeCalculCasToEficas
111
112       if 'Titre' not in self.dictSimp.keys():
113           import os
114           self.dictSimp['Titre']=os.path.basename(self.filename)
115       
116       for simp in self.dictSimp.keys():
117           if simp in ListeSupprimeCasToEficas: continue
118           if simp in TELEMACParser.__dict__.keys() : apply(TELEMACParser.__dict__[simp],(self,))
119
120       for simp in self.dictSimp.keys():
121           if simp not in self.dicoInverseFrancais.keys() : 
122              print "************"
123              print "pb avec ", simp,'------'
124              print "************"
125              continue
126           listeGenea=self.dicoInverseFrancais[simp]
127           listeGeneaReverse=[]
128           for (u,v) in listeGenea : 
129               if isinstance(v,BLOC): continue
130               listeGeneaReverse.append(u)
131           listeGeneaReverse.reverse()
132           dicoTravail=dicoParMC
133           i=0
134           #print (listeGeneaReverse[0:-1])
135           while i < len(listeGeneaReverse[0:-1]) : 
136             mot=listeGeneaReverse[i]
137             i=i+1
138             if mot not in dicoTravail.keys(): dicoTravail[mot]={}
139             dicoTravail=dicoTravail[mot]
140           dicoTravail[simp]=self.dictSimp[simp]
141         
142       self.textePy=""
143       #print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
144       #print dicoParMC
145       #print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
146       listeMC=self.tri(dicoParMC.keys())
147       for k in listeMC :
148           print "----------- traitement de " , k
149           self.textePy += self.dicoFrancaisAnglais[k] + "("
150           self.traiteMC(dicoParMC[k])
151           self.textePy += ");\n"
152           print "----------- " 
153            
154               
155       print self.textePy
156       return self.textePy 
157
158    def traiteMC(self,dico) :
159        for k in dico.keys() :
160            valeur= dico[k]
161            if k not in self.dicoMC.keys() : kA=self.dicoFrancaisAnglais[k] 
162            else : kA=k
163            obj=self.dicoMC[kA]
164            if isinstance(obj,FACT):   self.generFACT(obj,kA,valeur)
165            elif isinstance(obj,BLOC): self.generBLOC(obj,kA,valeur)
166            elif isinstance(obj,SIMP): self.generSIMP(obj,kA,valeur)
167            else : print "%%%%%%%%%%%\n", "pb generation pour", k, obj, "\n%%%%%%%%%%%"
168
169            #print "_____________"
170
171    def generFACT(self,obj,nom,valeur):
172        if nom in TELEMACParser.__dict__.keys() : 
173           apply(TELEMACParser.__dict__[nom],(self,))
174           return
175        self.textePy +=  nom + "=_F( "
176        self.traiteMC(valeur)
177        self.textePy += '),\n'
178
179
180    def generBLOC(self,obj,nom,valeur):
181        print "BLOC "
182        print nom
183
184    def generSIMP(self,obj,nom,valeur):
185        if nom in ("Prescribed_Flowrates", "Prescribed_Velocities", "Prescribed_Elevations" ): return
186        if obj.max==1 : 
187           if 'TXM' in obj.type :
188               valeur=str(valeur)
189               while valeur[-1] == " " : valeur=valeur[0:-1]
190               while valeur[0]  == " " : valeur=valeur[1:]
191               valeur=valeur[0].upper()+valeur[1:].lower()
192               valeur=tr(valeur)
193           if 'Fichier' in obj.type or 'TXM' in obj.type or 'Repertoire' in obj.type :
194               valeur=str(valeur)
195               while valeur[-1] == " " : valeur=valeur[0:-1]
196               while valeur[0]  == " " : valeur=valeur[1:]
197               self.textePy += nom + "= '" + str(valeur) +"' ,"
198               return
199           if bool in obj.type :
200             if pattern_OUI.match(valeur) or  pattern_oui.match(valeur) : self.textePy += nom + "= True,"
201             if pattern_NON.match(valeur) or  pattern_non.match(valeur) : self.textePy += nom + "= False,"
202             return
203           if nom in DicoEnumCasEn.keys():
204             valeur=DicoEnumCasEn[nom][valeur]
205           self.textePy += nom + "=" + str(valeur) +","
206
207
208
209    def tri(self, listeIn):
210       if len(listeIn) == 1 : return listeIn
211       if self.Ordre_Des_Commandes == None : return listeIn
212       #print self.Ordre_Des_Commandes
213       listeOut=[listeIn[0],]
214       for kF in listeIn[1:]:
215           k=str(self.dicoFrancaisAnglais[kF])
216           ordreK=self.Ordre_Des_Commandes.index(k)
217           i=0
218           while i < len(listeOut):
219              ordreI=self.Ordre_Des_Commandes.index(self.dicoFrancaisAnglais[listeOut[i]])
220              if ordreK < ordreI : break
221              i=i+1
222           listeOut.insert(i,kF)
223       return listeOut
224
225    def Processeurs_Paralleles(self):
226       #YOANN
227       if self.dictSimp["Processeurs_Paralleles"] == 0 : del  self.dictSimp["Processeurs_Paralleles"]
228       else : self.dictSimp["Parallel_Computation"]="Parallel"
229  
230       
231    def Option_De_Supg(self):
232        print "ds Option_De_Supg"
233        return
234
235    def Forme_De_La_Convection(self):
236        print "ds Forme_De_La_Convection"
237        return
238
239    def redecoupeSimp(self,simp): 
240       # replace('-','_')  uniquement dans les identifiants
241       while simp.find('-') > 0 : 
242         ind=simp.find('-')
243         if ind==len(simp)-1 : break
244         simp=simp[0:ind]+'_'+simp[ind+1].upper()+simp[ind+2:]
245       return simp
246
247
248
249    def Liquid_Boundaries(self):
250        texte_Boundaries="Liquid_Boundaries=( "
251        premier=0
252        if 'Prescribed_Elevations' in self.dictSimp.keys(): 
253            valeurs=self.dictSimp["Prescribed_Elevations"].split(";")
254        elif 'Cotes_Imposees' in self.dictSimp.keys(): 
255            valeurs=self.dictSimp["Cotes_Imposees"].split(";")
256        else : valeurs=()
257        for e in range(len(valeurs)):
258           if valeurs[e] == "" or valeurs[e] == "\n" : continue
259           if eval(valeurs[e],{})==0 : continue
260           if not premier : premier=1
261           texte_Boundaries += "_F(Type_Condition = 'Prescribed Elevations',\n"
262           texte_Boundaries += "Prescribed_Elevations = " + str(valeurs[e]) + "),\n"
263                
264        if 'Prescribed_Flowrates' in self.dictSimp.keys(): 
265           valeurs=self.dictSimp["Prescribed_Flowrates"].split(";")
266        elif 'Debits_Imposes' in self.dictSimp.keys(): 
267           valeurs=self.dictSimp["Debits_Imposes"].split(";")
268        else : valeurs=()
269        for e in range(len(valeurs)):
270           if valeurs[e] == "" or valeurs[e] == "\n" : continue
271           if eval(valeurs[e],{})==0 : continue
272           if not premier : premier=1
273           texte_Boundaries += "_F(Type_Condition = 'Prescribed Flowrates',\n"
274           texte_Boundaries += "Prescribed_Flowrates = " + str(valeurs[e]) + "),\n"
275                
276        if 'Prescribed_Velocity' in self.dictSimp.keys(): 
277            valeurs=self.dictSimp["Prescribed_Velocity"].split(";")
278        elif 'Vitesses_Imposees' in self.dictSimp.keys(): 
279            valeurs=self.dictSimp["Vitesses_Imposees"].split(";")
280        else : valeurs=()
281        for e in range(len(valeurs)):
282           if valeurs[e] == "" or valeurs[e] == "\n" : continue
283           if eval(valeurs[e],{})==0 : continue
284           if not premier : premier=1
285           texte_Boundaries += "_F(Type_Condition = 'Prescribed Velocity',\n"
286           texte_Boundaries += "Prescribed_Velocity = " + str(valeurs[e]) + "),\n"
287        if premier :  texte_Boundaries +="),\n"
288        else : texte_Boundaries="" ; print "pb texte_Boundaries "
289        self.textePy += texte_Boundaries
290