-
Notifications
You must be signed in to change notification settings - Fork 11
/
Evaluator.py
executable file
·251 lines (223 loc) · 10.3 KB
/
Evaluator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python
## Program: PyNS
## Module: Evaluator.py
## Language: Python
## Date: $Date: 2012/09/04 10:21:12 $
## Version: $Revision: 0.4.2 $
## Copyright (c) Simone Manini, Luca Antiga. All rights reserved.
## See LICENCE file for details.
## This software is distributed WITHOUT ANY WARRANTY; without even
## the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE. See the above copyright notices for more information.
## Developed with support from the EC FP7/2007-2013: ARCH, Project n. 224390
import re
from math import *
from datetime import *
class Evaluator(object):
'''
This class evaluates xml expression using regular expressions.
This class provides the following methods:
SetSimulationContext : a method for setting simulation context.
SetNetworkGraph: a method for setting NetworkGraph input.
SetNetworkMesh : a method for setting NetworkMesh.
SetInfo: a method for setting info dictionary ({'DofMap':self.DofMap}...)
SetAbscissa: a method for setting abscissa value.
GetElement: a method for finding element from its edge and specified position (abscissa).
GetEdge: a method for returning corresponding edge or superedge.
GetVariableComponents: a method for splitting expression into variables.
Evaluate: the main method of the class, it evaluates expression and returns result.
'''
def __init__(self):
'''
Constructor
'''
self.variableRe = re.compile('\$.*?[\}\]]')
self.parameterRe = re.compile('\$.*?[\{\[]')
self.elementRe = re.compile('\[.*?\]')
self.edgeRe = re.compile('\{.*?\}')
self.abscissa = None
self.NetworkMesh = None
self.NetworkGraph = None
self.SimulationContext = None
self.Info = None
self.ExpressionCache = {}
self.rhsCache = {}
def SetSimulationContext(self, context):
'''
Setting SimulationContext
'''
self.SimulationContext = context
def SetNetworkMesh(self, networkMesh):
'''
Setting NetworkMesh
'''
self.NetworkMesh = networkMesh
def SetNetworkGraph(self, networkGraph):
'''
Setting NetworkMesh
'''
self.NetworkGraph = networkGraph
def SetInfo(self,info):
'''
Setting info dictionary
'''
self.Info = info
def SetAbscissa(self,abscissa):
'''
Setting Abscissa Value
'''
self.abscissa = abscissa
def GetElement(self, edge, abscissa = None):
'''
This method returns the element from its edge and
specified position (abscissa)
'''
if abscissa is None:
abscissa = 0.0
if self.abscissa is None:
pass
else:
abscissa = self.abscissa
try:
ed = self.NetworkGraph.Edges[self.NetworkGraph.EdgeNamesToIds[edge]]
for el in self.NetworkMesh.GraphEdgeToMesh[ed]:
if abscissa <= el.values()[0][1] and abscissa >= el.values()[0][0]:
return self.NetworkMesh.ElementIdsToElements[str(el.keys()[0])]
except KeyError:
node = self.NetworkGraph.Nodes[self.NetworkGraph.NodeNamesToIds[edge]]
return self.NetworkMesh.ElementIdsToElements[self.NetworkMesh.GraphNodeToMesh[node]]
def GetEdge(self, edge, abscissa = None):
'''
This method returns corresponding edge or superedge
'''
if edge in self.NetworkGraph.EdgeNamesToIds:
if abscissa is not None:
self.NetworkGraph.Edges[self.NetworkGraph.EdgeNamesToIds[edge]].edgeAbscissa = abscissa
else:
abscissa = None
self.NetworkGraph.Edges[self.NetworkGraph.EdgeNamesToIds[edge]].edgeAbscissa = abscissa
return self.NetworkGraph.Edges[self.NetworkGraph.EdgeNamesToIds[edge]]
elif edge in self.NetworkGraph.SuperEdgeNamesToIds:
return self.NetworkGraph.SuperEdges[self.NetworkGraph.SuperEdgeNamesToIds[edge]]
else:
return None
def GetVariableComponents(self,variable):
'''
This method split expression into variables.
'''
parameter = self.parameterRe.findall(variable)[0][1:-1]
try:
element = self.elementRe.findall(variable)[0][1:-1]
abscissa = 0.0
timeIndex = 0
if len(element.split(',')) > 1:
splitElement = element.split(',')[1:]
try:
abscissa = float(splitElement[0])
timeIndex= int(splitElement[0])
except:
el = splitElement[0]
par = el.split('=')[0].strip()
if par == 's':
abscissa = float(el.split('=')[1])
elif par == 't':
timeIndex = int(el.split('=')[1])
if len(splitElement) > 2:
try:
timeIndex = int(splitElement[1])
except:
el = splitElement[1]
par = el.split('=')[0].strip()
if par == 's':
abscissa = float(el.split('=')[1])
elif par == 't':
timeIndex = int(el.split('=')[1])
element = element.split(',')[0]
edge = None
except:
edge = self.edgeRe.findall(variable)[0][1:-1]
element = None
abscissa = None
timeIndex = 0
if len(edge.split(',')) > 1:
abscissa = float(edge.split(',')[1])
edge = edge.split(',')[0]
return parameter, element, abscissa, timeIndex, edge
def Evaluate(self,expression):
'''
Evaluate(expr,{'DofMap':self.DofMap}...)
This method evaluates provided expression and returns result.
'''
info = self.Info
if expression in self.ExpressionCache:
elEvals = self.ExpressionCache[expression]['elEvals']
lhsEvalDict = self.ExpressionCache[expression]['lhsEvalDict']
lhsExpr = self.ExpressionCache[expression]['lhsExpr']
try:
lhsElement = lhsEvalDict['lhsElement']
lhsAbscissa = lhsEvalDict['lhsAbscissa']
except KeyError:
lhsEdge = lhsEvalDict['lhsEdge']
for elEvalDict in elEvals:
try:
rhsElement = elEvalDict['rhsElement']
rhsAbscissa = elEvalDict['rhsAbscissa']
rhsTimeIndex = elEvalDict['rhsTimeIndex']
except KeyError:
rhsEdge = elEvalDict['rhsEdge']
exec(elEvalDict['elEval'])
exec(lhsEvalDict['lhsEval'])
exec(lhsExpr)
return
splitExpression = expression.split('=')
lhs = splitExpression[0]
rhs = splitExpression[1]
lhsVariable = self.variableRe.findall(lhs)[0]
lhsParameter, lhsElement, lhsAbscissa, lhsTimeIndex, lhsEdge = self.GetVariableComponents(lhsVariable)
rhsVariables = self.variableRe.findall(rhs)
elCount = 0
elEvals = []
if lhsEdge is None:
if self.rhsCache.has_key(lhsElement):
self.rhsCache[lhsElement].append(rhs)
else:
self.rhsCache[lhsElement] = [rhs]
if len(self.rhsCache[lhsElement]) == 2:
if self.rhsCache.has_key(lhsElement) and rhs == self.rhsCache[lhsElement][0]:
rhs = self.rhsCache[lhsElement][1]
for rhsVariable in rhsVariables:
rhsParameter, rhsElement, rhsAbscissa, rhsTimeIndex, rhsEdge = self.GetVariableComponents(rhsVariable)
if rhsEdge is not None:
elEvals.append({'elEval': 'edge%d = self.GetEdge(rhsEdge,rhsAbscissa)' % elCount, 'rhsEdge':rhsEdge, 'rhsAbscissa':rhsAbscissa})
rhs = self.variableRe.sub('edge%d.Get%s(rhsAbscissa)' % (elCount,rhsParameter),rhs,1)
else:
if rhsElement == '':
rhsParameter = self.SimulationContext.Context[rhsParameter]
rhs = self.variableRe.sub('%s' % (rhsParameter),rhs,1)
else:
elEvals.append({'elEval': 'el%d = self.GetElement(rhsElement,rhsAbscissa)' % elCount, 'rhsElement':rhsElement, 'rhsAbscissa':rhsAbscissa, 'rhsTimeIndex':rhsTimeIndex})
rhs = self.variableRe.sub('el%d.Get%s(info,%d)' % (elCount,rhsParameter,rhsTimeIndex),rhs,1)
elCount += 1
if lhsEdge is None:
self.rhsCache[lhsElement].append(rhs)
if lhsEdge is not None:
lhsExpr = self.variableRe.sub('lhsEdge.Set%s(%s)' % (lhsParameter,rhs),lhs,1)
lhsEvalDict = {'lhsEval':'lhsEdge = self.GetEdge(lhsEdge,lhsAbscissa)', 'lhsEdge':lhsEdge, 'lhsAbscissa':lhsAbscissa}
else:
if lhsElement == '':
lhsExpr = self.variableRe.sub('self.SimulationContext.Context[%s]=%s' % ("'"+lhsParameter+"'",rhs),lhs,1)
lhsEvalDict = {'lhsEval':'', 'lhsElement':None, 'lhsAbscissa':None}
else:
lhsExpr = self.variableRe.sub('lhsEl.Set%s(%s, info)' % (lhsParameter,rhs),lhs,1)
lhsEvalDict = {'lhsEval':'lhsEl = self.GetElement(lhsElement,lhsAbscissa)', 'lhsElement':lhsElement, 'lhsAbscissa':lhsAbscissa}
for elEvalDict in elEvals:
try:
rhsEdge = elEvalDict['rhsEdge']
rhsAbscissa = elEvalDict['rhsAbscissa']
except KeyError:
rhsElement = elEvalDict['rhsElement']
rhsAbscissa = elEvalDict['rhsAbscissa']
exec(elEvalDict['elEval'])
exec(lhsEvalDict['lhsEval'])
exec(lhsExpr)
self.ExpressionCache[expression] = {'elEvals':elEvals, 'lhsEvalDict':lhsEvalDict, 'lhsExpr':lhsExpr}