1 #@ AJOUT OpenturnsSolver Macro
2 # -*- coding: iso-8859-1 -*-
6 Ce module contient le generateur XML pour Openturns
12 # Dictionnaires de conversion des valeurs lues dans EFICAS
13 # en valeurs reconnues par Open TURNS
14 # Les clefs 'None' sont les valeurs par defaut
16 VariableTypeByName = {
17 "in" : openturns.WrapperDataVariableType.IN,
18 "out" : openturns.WrapperDataVariableType.OUT,
19 None : openturns.WrapperDataVariableType.IN,
23 "in" : openturns.WrapperDataFileType.IN,
24 "out" : openturns.WrapperDataFileType.OUT,
25 None : openturns.WrapperDataFileType.IN,
28 SymbolProvidedByName = {
29 "no" : openturns.WrapperSymbolProvided.NO,
30 "yes" : openturns.WrapperSymbolProvided.YES,
31 None : openturns.WrapperSymbolProvided.NO,
34 WrapperStateByName = {
35 "shared" : openturns.WrapperState.SHARED,
36 "specific" : openturns.WrapperState.SPECIFIC,
37 None : openturns.WrapperState.SPECIFIC,
41 "static-link" : openturns.WrapperMode.STATICLINK,
42 "dynamic-link" : openturns.WrapperMode.DYNAMICLINK,
43 "fork" : openturns.WrapperMode.FORK,
44 None : openturns.WrapperMode.FORK,
47 WrapperDataTransferByName = {
48 "files" : openturns.WrapperDataTransfer.FILES,
49 "pipe" : openturns.WrapperDataTransfer.PIPE,
50 "arguments" : openturns.WrapperDataTransfer.ARGUMENTS,
51 "socket" : openturns.WrapperDataTransfer.SOCKET,
52 "corba" : openturns.WrapperDataTransfer.CORBA,
53 None : openturns.WrapperDataTransfer.FILES,
56 #==========================
57 # La classe de creation XML
58 #==========================
63 Generation du fichier XML
65 def __init__ (self, appli, DictMCVal, DictVariables ) :
66 self.DictMCVal = DictMCVal
67 self.DictVariables = DictVariables
72 Pilotage general de la creation du fichier XML
74 data = openturns.WrapperData()
75 data.setLibraryPath( self.GetMCVal('WrapperPath','') )
76 data.setVariableList( self.VariableList() )
77 data.setFunctionDescription( self.FunctionDefinition() )
78 data.setGradientDescription( self.GradientDefinition() )
79 data.setHessianDescription( self.HessianDefinition() )
80 data.setFileList( self.FileList() )
81 data.setParameters( self.Parameters() )
82 data.setFrameworkData( self.FrameworkData() )
84 wrapper=openturns.WrapperFile()
85 wrapper.setWrapperData( data )
90 class __variable_ordering:
91 def __init__ (self, dictVar) :
92 self.dictVar = dictVar
94 def __call__(self, a, b):
95 return self.dictVar[a]['numOrdre'] - self.dictVar[b]['numOrdre']
97 def VariableList (self) :
99 Ecrit la liste des variables
101 varList = openturns.WrapperDataVariableCollection()
102 for var in sorted( self.DictVariables.keys(), self.__variable_ordering( self.DictVariables ) ) :
103 varList.add( self.Variable( var, self.DictVariables[var] ) )
106 def Variable (self, var, dictVar) :
108 Ecrit le parametrage d une variable
110 variable = openturns.WrapperDataVariable()
112 if dictVar[ 'Type' ] in VariableTypeByName.keys() :
113 variable.type_ = VariableTypeByName[ dictVar[ 'Type' ] ]
114 if dictVar.has_key('Comment') : variable.comment_ = dictVar[ 'Comment' ]
115 if dictVar.has_key('Unit') : variable.unit_ = dictVar[ 'Unit' ]
116 if dictVar.has_key('Regexp') : variable.regexp_ = dictVar[ 'Regexp' ]
117 if dictVar.has_key('Format') : variable.format_ = dictVar[ 'Format' ]
120 def FunctionDefinition (self) :
122 Ecrit la description de la Fonction
124 func = openturns.WrapperFunctionDescription()
125 func.name_ = self.GetMCVal( 'FunctionName', '' )
126 if (len(func.name_) != 0) : func.provided_ = SymbolProvidedByName[ 'yes' ]
129 def GradientDefinition (self) :
131 Ecrit la description du Gradient
133 grad = openturns.WrapperFunctionDescription()
134 grad.name_ = self.GetMCVal( 'GradientName', '' )
135 if (len(grad.name_) != 0) : grad.provided_ = SymbolProvidedByName[ 'yes' ]
138 def HessianDefinition (self) :
140 Ecrit la description de la Hessienne
142 hess = openturns.WrapperFunctionDescription()
143 hess.name_ = self.GetMCVal( 'HessianName', '' )
144 if (len(hess.name_) != 0) : hess.provided_ = SymbolProvidedByName[ 'yes' ]
149 def FileList (self) :
151 Ecrit la liste des fichiers
153 fileList = openturns.WrapperDataFileCollection()
154 for dictFile in self.GetMCVal('Files', []) :
155 fileList.add( self.File( dictFile ) )
158 def File (self, dictFile ) :
160 Ecrit le parametrage d un fichier
162 fich = openturns.WrapperDataFile()
163 fich.id_ = dictFile[ 'Id' ]
164 if dictFile[ 'Type' ] in FileTypeByName.keys() :
165 fich.type_ = FileTypeByName[ dictFile[ 'Type' ] ]
166 if dictFile.has_key('Name') : fich.name_ = dictFile[ 'Name' ]
167 if dictFile.has_key('Path') : fich.path_ = dictFile[ 'Path' ]
168 if dictFile.has_key('Subst') :
170 fich.subst_ = string.join( dictFile[ 'Subst' ], ',' )
173 def Parameters (self) :
175 Ecrit les parametres de couplage au code externe
177 parameters = openturns.WrapperParameter()
178 parameters.mode_ = WrapperModeByName[ self.GetMCVal('WrapCouplingMode') ]
179 if (parameters.mode_ == openturns.WrapperMode.FORK ):
180 parameters.command_ = self.GetMCVal('Command')
181 userPrefix = self.GetMCVal('UserPrefix', None)
182 if userPrefix != None : parameters.userPrefix_ = userPrefix
183 parameters.state_ = WrapperStateByName[ self.GetMCVal('State') ]
184 parameters.in_ = WrapperDataTransferByName[ self.GetMCVal('InDataTransfer') ]
185 parameters.out_ = WrapperDataTransferByName[ self.GetMCVal('OutDataTransfer') ]
188 def FrameworkData (self) :
190 Ecrit les donnees liees a l utilisation d un framework englobant
192 framework = openturns.WrapperFrameworkData()
193 # framework.studycase_ = "12:23:34"
194 # framework.componentname_ = self.GetMCVal('SolverComponentName', 'UNDEFINED')
195 CN = self.GetMCVal('SolverComponentName', 'UNDEFINED')
197 framework.componentname_ = CN
201 # ---------------------------------------------------------------------------------
204 def GetTag (self, tag) :
206 Recupere la chaine associee au tag dans la table dictTagsXML.
207 Leve une exception si le tag n est pas trouve
209 if ( dictTagsXML.has_key(tag) ) :
210 return dictTagsXML[tag]
212 raise KeyError, "Tag '%s' is undefined. This is an internal bug. Report bug to developers" % tag
215 def GetMCVal (self, MC, default = None, mandatory = False) :
217 Recupere la chaine associee au MC dans la table DictMCVal.
218 Leve une exception si le MC n est pas trouve et mandatory vaut True
220 if ( self.DictMCVal.has_key(MC) and self.DictMCVal[MC] != None ) :
221 return self.DictMCVal[MC]
224 raise KeyError, "Keyword '%s' is mandatory" % MC