Salome HOME
cht version
[tools/eficas.git] / generator / generator_mapVP.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2021   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 """
21    Ce module contient le plugin generateur de fichier au format
22    CARMEL3D pour EFICAS.
23
24 """
25 from __future__ import print_function
26 from __future__ import absolute_import
27 try :
28     from builtins import str
29 except : pass
30
31 import traceback
32 import types,re,os
33 import Accas
34
35 from .generator_python import PythonGenerator
36
37 listeCalParName = ('Time' , 'Temperature', 'DoseRate', 'Thickness')        #'calculation_parameter_names'
38
39
40 def entryPoint():
41     """
42        Retourne les informations necessaires pour le chargeur de plugins
43
44        Ces informations sont retournees dans un dictionnaire
45     """
46     return {
47          # Le nom du plugin
48            'name' : 'MAPVp',
49          # La factory pour creer une instance du plugin
50            'factory' : MapGenerator,
51            }
52
53
54 class MapGenerator(PythonGenerator):
55     """
56        Ce generateur parcourt un objet de type JDC et produit
57        un texte au format eficas et
58        un texte au format py
59
60     """
61
62     def gener(self,obj,format='brut',config=None,appliEficas=None):
63         self.appliEficas=appliEficas
64         self.cata=self.appliEficas.readercata.cata
65         self.initDico()
66         self.text=PythonGenerator.gener(self,obj,format)
67         if obj.isValid() :self.genereTexte(obj)
68         return self.text
69
70     def initDico(self) :
71         self.texteInput = ""
72         self.dictParam={}
73         self.dictValeur={}
74         self.listeEquations=[]
75         self.typeEtude = ""
76
77
78     def genereTexte(self,obj) :
79         print ('genereTexte', self.typeEtude)
80         if self.typeEtude == "Calculation" : self.genereCalculation()
81
82
83     def generPROC_ETAPE(self,obj):
84         s=PythonGenerator.generPROC_ETAPE(self,obj)
85         if obj.nom == "Calculation_for_Mechanistic" : print ('hjkhjkh');self.typeEtude="Calculation"
86         return s
87
88     def genereCalculation(self) :
89         '''
90         Prepare le contenu du fichier de parametres python
91         '''
92         self.texteInput = ""
93         self.texteInput += self.genereCsv()
94         self.texteInput += self.genereCalculationParams()
95         self.texteInput += self.txtNomCst
96         self.texteInput += self.txtVal
97         self.texteInput += self.txtValAct
98         self.texteInput += self.txtNomCstNA
99         self.texteInput += self.txtValNA
100         self.texteInput += self.txtInitName
101         self.texteInput += self.txtInitVal
102         self.texteInput += self.genereEquations()
103         print (self.texteInput)
104
105     def writeDefault(self, fn):
106         # normalement c_solver_polymer_kinetics_myStudy.input ou myStudy est le nom de l etude
107         fileInput = fn[:fn.rfind(".")] + '.input'
108         f = open( str(fileInput), 'wb')
109         f.write( self.texteInput )
110         f.close()
111
112
113     def genereCalculationParams(self) :
114         txtNom  = "calculation_parameter_names = [ "
115         txtVal = "calculation_parameters = [ "
116         for param in ('Time' , 'Temperature', 'DoseRate', 'Thickness')  :
117             if param in self.dictValeur.keys() :
118                 txtNom  += "'"+param +  "', "
119                 txtVal += str(self.dictValeur[param]) + ", "
120         # on enleve les dernieres , et on ferme
121         txtNom = txtNom[0:-2]
122         txtNom += "]\n"
123         txtVal = txtVal[0:-2]
124         txtVal += "]\n"
125         txt = txtNom + txtVal
126         return txt
127
128     def genereEquations(self) :
129         txt="equation =["
130         index=0
131         TechnicalUse = self.dictValeur['TechnicalUse']
132         ModelName    = self.dictValeur['ModelName']
133         for param in  self.listInitialParameters:
134             print ('*******************************************')
135             print (' je  traite ', param , 'index : ', index)
136             trouveParam=False
137
138             if index != 0 : txtParam = 'Dy[j*5 + '+str(index)+ '] = '
139             else :          txtParam = 'Dy[j*5] = '
140
141             for equation in  self.listeEquations :
142                 if param in (self.cata.dicoEquations[TechnicalUse][ModelName]['equa_diff'][equation].keys()):
143                     print ('____________ trouve : ', param , 'in ', equation, ' ',  self.cata.dicoEquations[TechnicalUse][ModelName]['equa_diff'][equation][param])
144                     trouveParam = True
145                     if self.cata.dicoEquations[TechnicalUse][ModelName]['equa_diff'][equation][param][0] == '-' :
146                         txtParam += ' ' + self.cata.dicoEquations[TechnicalUse][ModelName]['equa_diff'][equation][param]
147                     else :
148                         if index != 0 :
149                             txtParam += ' + ' + self.cata.dicoEquations[TechnicalUse][ModelName]['equa_diff'][equation][param]
150                         else :
151                             txtParam +=  self.cata.dicoEquations[TechnicalUse][ModelName]['equa_diff'][equation][param]
152                     print ('         txtParam   intermediaire ', txtParam)
153
154             if trouveParam :
155                 txtParam = txtParam + ", "
156                 txt += txtParam
157                 index = index+1
158             print (txtParam)
159             print ('fin param', param, 'trouve ', trouveParam, '___________________________')
160             print ('*************************************************')
161             print (' ')
162         print ('_________________fin for')
163         txt=txt[0:-3]
164         txt+="]\n"
165         return txt
166
167     def genereCsv(self) :
168         txt =  'study_name = ' +self.dictValeur['SimulationName'] +  "\n"
169         txt += 'csv_output_file_name = ' + self.dictValeur['OutPutFolder'] + '/c_solver_stiff_ode_1d_' + self.dictValeur['SimulationName']+ '.csv\n'
170         return txt
171
172
173     def generMCList(self,obj):
174         s=PythonGenerator.generMCList(self,obj)
175         if obj.nom == 'ConstantesArrhenius' :
176             self.txtNomCst  = "Arrhenius_Name = [ "
177             self.txtVal     = "Arrhenius_A = [ "
178             self.txtValAct  = "Arrhenius_Ea = [ "
179             for objFils in obj.data:
180                 for mc in objFils.mcListe :
181                     self.txtNomCst +=  "'" + mc.nom + "', "
182                     self.txtVal    +=  str(mc.valeur[0]) + ", "
183                     self.txtValAct +=  str(mc.valeur[1]) + ", "
184             self.txtNomCst = self.txtNomCst[0:-2]
185             self.txtVal    = self.txtVal[0:-2]
186             self.txtValAct = self.txtValAct [0:-2]
187             self.txtNomCst += ']\n'
188             self.txtVal    += ']\n'
189             self.txtValAct += ']\n'
190
191         if obj.nom == 'ConstantesNonArrhenius' :
192             self.txtNomCstNA  = "non_Arrhenius_coefs_names = [ "
193             self.txtValNA     = "non_Arrhenius_coefs = [ "
194             for objFils in obj.data:
195                 for mc in objFils.mcListe :
196                     self.txtNomCstNA +=  "'" + mc.nom + "', "
197                     self.txtValNA    +=  str(mc.valeur) + ", "
198             self.txtNomCstNA  = self.txtNomCstNA[0:-2]
199             self.txtValNA     = self.txtValNA[0:-2]
200             self.txtNomCstNA += ']\n'
201             self.txtValNA    += ']\n'
202
203         if obj.nom == 'InitialParameters' :
204             self.listInitialParameters =[]
205             self.txtInitName  = "initial_Value_names = [ "
206             self.txtInitVal   = "initial_Values = [ "
207             for objFils in obj.data:
208                 for mc in objFils.mcListe :
209                     self.txtInitName +=  "'" + mc.nom + "', "
210                     self.txtInitVal  +=  str(mc.valeur) + ", "
211                     self.listInitialParameters.append(mc.nom)
212             self.txtInitName  = self.txtInitName[0:-2]
213             self.txtInitVal   = self.txtInitVal[0:-2]
214             self.txtInitName += ']\n'
215             self.txtInitVal  += ']\n'
216
217         if obj.nom in( 'initiation','propagation','termination','stabilization') :
218             for o in obj :
219                 for mc  in o.mcListe :
220                     nom=mc.nom.replace (' ','').replace ('+','_').replace ('-','_').replace ('>','_').replace('(','').replace(')','').replace('*','').replace('[','').replace(']','')
221                     nom=obj.nom+'_'+nom
222                     self.listeEquations.append(nom )
223         return s
224
225     def generMCSIMP(self,obj) :
226         """
227         Convertit un objet MCSIMP en texte python
228         Remplit le dictionnaire des MCSIMP
229         """
230         s=PythonGenerator.generMCSIMP(self,obj)
231         if obj.nom=='Consigne' : return s
232
233         if obj.getGenealogie()[0][-6:-1]=="_PARA":
234             self.dictParam[obj.nom]=obj.valeur
235         else :
236             self.dictValeur[obj.nom]=obj.valeur
237         return s