8 logger
= logging
.getLogger()
10 def qualityKind(prop
):
11 #Check if the fluid is subcooled,superheated or supercritic, and return it
12 for x
in prop
.project
.elemList
:
14 for y
in prop
.project
.elemList
[x
].properties
:
15 if prop
== prop
.project
.elemList
[x
].properties
[y
]:
16 elem
= prop
.project
.elemList
[x
]
17 fluid
= elem
.properties
['fluid'].value
18 temperature
= elem
.properties
['temperature'].value
19 pressure
= elem
.properties
['pressure'].value
21 tsat
= fluid
.Tsat_p(pressure
)
22 if temperature
< tcrit
:
23 if temperature
< tsat
:
25 elif temperature
> tsat
:
32 class Property(notif
.Notifier
):
33 ''' Property has a value and a label. Element contains Property in its properties dictionnary.'''
34 def __init__(self
,project
,label
,value
= None):
35 notif
.Notifier
.__init
__(self
)
36 self
._isImposed
= False
40 self
._theyDependOnMe
= set()
42 self
._wantNotif
= set() #Take element which want to be notify when these property change, and wich have an update() method
43 self
.__project
= project
47 return self
._isImposed
50 def isImposed(self
, val
):
64 def labelKey(self
, val
):
78 return self
._wantNotif
84 if self
._isImposed
== True:
86 elif self
._isTell
== True:
91 class Variable(Property
):
92 ''' Variable are Property that can be tell (only)'''
93 def __init__(self
,project
,label
,value
=None):
94 Property
.__init
__(self
,project
,label
,value
)
95 self
._iDependOnThem
= set()
97 def addDependence(self
, on
):
98 self
._iDependOnThem |
= on
100 x
._theyDependOnMe
.add(self
)
102 def clearDependence(self
):
103 for x
in self
._iDependOnThem
:
104 assert self
in x
._theyDependOnMe
105 x
._theyDependOnMe
.remove(self
)#Remove from the dependance so nobody else depend on me
106 self
._iDependOnThem
.clear() # Clear my dependance so I depend on nobody
109 """ Empty the value and do notification"""
110 ''' You have to check if it's not impose '''
115 theydependedOnMe
=self
._theyDependOnMe
.copy()
116 self
.clearDependence() # first
117 for x
in theydependedOnMe
: # second : order important
119 assert self
._iDependOnThem
== set()
120 assert self
._theyDependOnMe
== set()
121 self
.project
.todoList |
= self
.wantNotif
124 def impose(self
, val
):
125 ''' Impose a value and do notification '''
127 assert self
._isTell
== False
128 self
._isImposed
= False
130 if val
== '': # relax
131 self
._isImposed
= False
132 self
.relax() #force the value out
133 logger
.info('relax %s',self
.label
)
135 self
._isImposed
= True
136 self
._value
= float('NaN')
137 logger
.info('impose %s with a value of %s',self
.label
,self
._value
)
139 self
._isImposed
= True
141 logger
.info('impose %s with a value of %s',self
.label
,self
._value
)
142 self
.project
.doNotify()
144 def tell(self
, val
, becauseOf
):
145 ''' Property can tell another property. It spreads the value troughout the cycle.
146 Creates dependance and notification '''
149 if val
== self
._value
:
150 if not self
._isImposed
:
151 self
.addDependence(becauseOf
)
154 assert self
._iDependOnThem
== set()
156 elif self
._value
== None :
157 assert self
._iDependOnThem
== set()
158 assert self
._theyDependOnMe
== set()
159 self
.relax() #Just for notifications
161 self
.addDependence(becauseOf
)
163 if type(self
) != FluidProperty
:
164 logger
.info('tell %s with a value of %s',self
.label
,val
)
166 logger
.info('tell %s with a value of %s',self
.label
,val
.name
)
169 logger
.info('tell %s with a value of %s \n but already have %s',self
.label
,val
,self
._value
)
170 return False # Property was told or impose and someone try to impose another value
172 class FloatVariableProperty(Variable
):
173 ''' Variable who's value is a float'''
174 def __init__(self
,project
,label
,value
=None):
175 Variable
.__init
__(self
,project
,label
,value
)
180 if self
._value
!= None:
181 return float(self
._value
)
186 if self
._value
== None :
187 self
._display
= wx
.propgrid
.StringProperty(self
.label
)
190 self
._display
= wx
.propgrid
.StringProperty(self
.label
,value
= str('%0.2f'%float
(self
._value
)))
193 class StringVariableProperty(Variable
):
194 ''' Variable who's value is a string'''
195 def __init__(self
,project
,label
,value
=None):
196 Variable
.__init
__(self
,project
,label
,value
)
201 if self
._value
== None :
202 self
._display
= wx
.propgrid
.StringProperty(self
.label
)
205 self
._display
= wx
.propgrid
.StringProperty(self
.label
,value
= str(self
._value
))
209 class FixedProperty(Property
):
210 ''' Properties that can not be tell by the programm '''
211 def __init__(self
,project
,label
):
212 Property
.__init
__(self
,project
,label
)
214 def impose(self
, val
):
215 self
.isImposed
= True
217 self
.project
.doNotify()
220 class FloatFixedProperty(FixedProperty
):
221 ''' Float properties that can not be tell '''
222 def __init__(self
,project
,label
):
223 FixedProperty
.__init
__(self
,project
,label
)
227 if self
._value
!= None:
228 return float(self
._value
)
233 if self
._value
== None :
234 self
._display
= wx
.propgrid
.StringProperty(self
.label
)
237 self
._display
= wx
.propgrid
.StringProperty(self
.label
,value
= str(self
._value
))
241 class StringFixedProperty(FixedProperty
):
242 ''' String properties that can not be tell'''
243 def __init__(self
,project
,label
):
244 FixedProperty
.__init
__(self
,project
,label
)
248 if self
._value
!= None:
249 return str(self
._value
)
258 if self
._value
== None :
259 self
._display
= wx
.propgrid
.StringProperty(self
.label
)
262 self
._display
= wx
.propgrid
.StringProperty(self
.label
,value
= str(self
._value
))
266 class TellProperty(Variable
):
267 ''' All the properties that only can be tell but can't be imposed'''
268 def __init__(self
,project
,label
,value
= None):
269 Variable
.__init
__(self
,project
,label
,value
)
271 def impose(self
, val
):
274 class FloatTellProperty(TellProperty
):
275 ''' Float properties that can be tell '''
276 def __init__(self
,project
,label
,value
=None):
277 TellProperty
.__init
__(self
,project
,label
,value
)
282 if self
._value
== None :
283 self
._display
= wx
.propgrid
.StringProperty(self
.label
)
286 self
._display
= wx
.propgrid
.StringProperty(self
.label
,value
= str('%0.5f'%float
(self
._value
)))
290 class BoolProperty(Variable
):
291 ''' Boolean properties '''
292 def __init__(self
,project
,label
,value
=None):
293 Variable
.__init
__(self
,project
,label
,value
)
305 if self
._value
!= None :
306 return wx
.propgrid
.BoolProperty(self
.label
,value
= self
.value
)
308 return wx
.propgrid
.BoolProperty(self
.label
)
311 class FluidProperty(Variable
):
312 ''' Fluid properties '''
313 def __init__(self
,project
,label
,value
=None):
314 Variable
.__init
__(self
,project
,label
,value
=fluid
.Fluid(project
,''))
322 arrayId
= range(len(self
.project
.app
.fluidList
))
323 fluidname
= self
.value
.name
324 num
= self
.project
.app
.fluidListName
.index(fluidname
)
325 return wx
.propgrid
.EnumProperty('fluid','fluid',self
.project
.app
.fluidListName
,arrayId
,num
)
330 self
._value
= self
.project
.app
.fluidList
['']
332 theydependedOnMe
=self
._theyDependOnMe
.copy()
333 self
.clearDependence() # first
334 for x
in theydependedOnMe
: # second : order important
336 assert self
._iDependOnThem
== set()
337 assert self
._theyDependOnMe
== set()
338 self
.project
.todoList |
= self
.wantNotif
341 def impose(self
, val
):
343 assert self
._isTell
== False
345 if type(val
) == fluid
.Fluid
: # relax
347 self
._isImposed
= False
349 logger
.info('relax %s',self
.label
)
351 self
._isImposed
= False
353 self
._isImposed
= True
355 logger
.info('impose %s with a value of %s',self
.label
,self
._value
.name
)
356 self
.project
.doNotify()
358 def tell(self
, val
, becauseOf
):
361 if val
== self
._value
:
362 if not self
._isImposed
:
363 self
.addDependence(becauseOf
)
366 assert self
._iDependOnThem
== set()
368 elif type(self
._value
) == fluid
.Fluid
:
369 assert self
._iDependOnThem
== set()
370 assert self
._theyDependOnMe
== set()
371 self
.relax() #Just for notifications
373 self
.addDependence(becauseOf
)
375 logger
.info('tell %s with a value of %s',self
.label
,val
.name
)
378 logger
.info('tell %s with a value of %s \n but already have %s',self
.label
,val
.name
,self
._value
.name
)
379 return False # Property was told or impose and someone try to impose another value
383 class QualityProperty(Variable
):
384 ''' Quality properties '''
385 def __init__(self
,project
,label
,value
= None):
386 Variable
.__init
__(self
,project
,label
,value
)
390 if self
._value
!= None:
391 return float(self
._value
)
397 if self
._value
== None :
398 self
._display
= wx
.propgrid
.StringProperty(self
.label
)
399 elif not math
.isnan(float(self
._value
)):
400 self
._display
= wx
.propgrid
.StringProperty(self
.label
,value
= str(self
._value
))
401 elif math
.isnan(float(self
._value
)):
402 labelVal
= qualityKind(self
)
403 self
._display
= wx
.propgrid
.StringProperty(self
.label
,value
= labelVal
)
407 def tell(self
, val
, becauseOf
):
408 ''' Property can tell another property. It spreads the value troughout the cycle.
409 Creates dependance and notification '''
412 if val
== self
._value
:
413 if not self
._isImposed
:
414 self
.addDependence(becauseOf
)
417 assert self
._iDependOnThem
== set()
419 elif self
._value
== None :
420 assert self
._iDependOnThem
== set()
421 assert self
._theyDependOnMe
== set()
422 self
.relax() #Just for notifications
424 self
.addDependence(becauseOf
)
426 logger
.info('tell %s with a value of %s',self
.label
,val
)
428 elif val
!= None and self
._value
!= None:
429 if math
.isnan(float(val
)) and math
.isnan(float(self
._value
)):
430 if not self
._isImposed
:
431 self
.addDependence(becauseOf
)
434 assert self
._iDependOnThem
== set()
437 logger
.info('tell %s with a value of %s \n but already have %s',self
.label
,val
,self
._value
)
438 return False # Property was told or impose and someone try to impose another value