Package PyFoam :: Package LogAnalysis :: Module FoamLogAnalyzer
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.LogAnalysis.FoamLogAnalyzer

  1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/LogAnalysis/FoamLogAnalyzer.py 5635 2009-09-23T23:28:11.134879Z bgschaid  $  
  2  """Analyze OpenFOAM logs""" 
  3   
  4  from TimeLineAnalyzer import TimeLineAnalyzer 
  5  from PyFoam.Basics.LineReader import LineReader 
  6  from PyFoam.Error import error 
  7   
8 -class FoamLogAnalyzer(object):
9 """Base class for all analyzers 10 11 Administrates and calls a number of LogLineAnlayzers for each 12 line""" 13
14 - def __init__(self,progress=False):
15 """ 16 @param progress: Print time progress on console? 17 """ 18 self.analyzers={} 19 self.time="" 20 self.oDir="" 21 self.line=LineReader() 22 self.timeListeners=[] 23 self.timeTriggers=[] 24 25 tm=TimeLineAnalyzer(progress=progress) 26 self.addAnalyzer("Time",tm) 27 tm.addListener(self.setTime)
28
29 - def tearDown(self):
30 """Remove reference to self in children (hoping to remove 31 circular dependencies)""" 32 33 for a in self.analyzers.values(): 34 a.tearDown() 35 a.setParent(None)
36
37 - def setTime(self,time):
38 """Sets the time and alert all the LineAnalyzers that the time has changed 39 @param time: the new value of the time 40 """ 41 if time!=self.time: 42 self.time=time 43 for listener in self.timeListeners: 44 listener.timeChanged() 45 for nm in self.analyzers: 46 self.analyzers[nm].timeChanged() 47 self.checkTriggers()
48
49 - def addTimeListener(self,listener):
50 """@param listener: An object that is notified when the time changes. Has to 51 implement a timeChanged method""" 52 if not 'timeChanged' in dir(listener): 53 error("Error. Object has no timeChanged-method:"+str(listener)) 54 else: 55 self.timeListeners.append(listener)
56
57 - def listAnalyzers(self):
58 """@returns: A list with the names of the Analyzers""" 59 return self.analyzers.keys()
60
61 - def hasAnalyzer(self,name):
62 """Is this LogLineAnalyzer name there""" 63 return self.analyzers.has_key(name)
64
65 - def getAnalyzer(self,name):
66 """Get the LogLineAnalyzer name""" 67 if self.analyzers.has_key(name): 68 return self.analyzers[name] 69 else: 70 return None
71
72 - def addAnalyzer(self,name,obj):
73 """Adds an analyzer 74 75 obj - A LogLineAnalyzer 76 name - the name of the analyzer""" 77 78 obj.setParent(self) 79 self.analyzers[name]=obj
80
81 - def analyzeLine(self,line):
82 """Calls all the anlyzers for a line""" 83 for nm in self.analyzers: 84 self.analyzers[nm].doAnalysis(line)
85
86 - def analyze(self,fh):
87 """Analyzes a file (one line at a time) 88 89 fh - handle of the file""" 90 while(self.line.read(fh)): 91 self.analyzeLine(self.line.line)
92
93 - def goOn(self):
94 """Checks with all the analyzers 95 96 If one analyzer returns False it returns False""" 97 result=True 98 99 for nm in self.analyzers: 100 # print nm,self.analyzers[nm].goOn() 101 result=result and self.analyzers[nm].goOn() 102 103 return result
104
105 - def getTime(self):
106 """Gets the current time""" 107 return str(self.time)
108
109 - def setDirectory(self,d):
110 """Sets the output directory for all the analyzers""" 111 self.oDir=d 112 for nm in self.analyzers: 113 self.analyzers[nm].setDirectory(self.oDir)
114
115 - def getDirectory(self):
116 """Gets the output directory""" 117 return self.oDir
118
119 - def addTrigger(self,time,func,once=True,until=None):
120 """Adds a trigger function that is to be called as soon as 121 the simulation time exceeds a certain value 122 @param time: the time at which the function should be triggered 123 @param func: the trigger function 124 @param once: Should this function be called once or at every time-step 125 @param until: The time until which the trigger should be called""" 126 127 data={} 128 data["time"]=float(time) 129 data["func"]=func 130 if until!=None: 131 data["until"]=float(until) 132 once=False 133 data["once"]=once 134 135 self.timeTriggers.append(data)
136
137 - def checkTriggers(self):
138 """Check for and execute the triggered functions""" 139 140 remove=[] 141 for i in range(len(self.timeTriggers)): 142 t=self.timeTriggers[i] 143 if t["time"]<=self.time: 144 t["func"]() 145 if t["once"]: 146 remove.append(i) 147 elif "until" in t: 148 if t["until"]<=self.time: 149 remove.append(i) 150 151 remove.reverse() 152 153 for i in remove: 154 self.timeTriggers.pop(i)
155