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

Source Code for Module PyFoam.LogAnalysis.FoamLogAnalyzer

  1  #  ICE Revision: $Id: FoamLogAnalyzer.py 8292 2007-12-12 15:22:00Z 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 getAnalyzer(self,name):
62 """Get the LogLineAnalyzer name""" 63 if self.analyzers.has_key(name): 64 return self.analyzers[name] 65 else: 66 return None
67
68 - def addAnalyzer(self,name,obj):
69 """Adds an analyzer 70 71 obj - A LogLineAnalyzer 72 name - the name of the analyzer""" 73 74 obj.setParent(self) 75 self.analyzers[name]=obj
76
77 - def analyzeLine(self,line):
78 """Calls all the anlyzers for a line""" 79 for nm in self.analyzers: 80 self.analyzers[nm].doAnalysis(line)
81
82 - def analyze(self,fh):
83 """Analyzes a file (one line at a time) 84 85 fh - handle of the file""" 86 while(self.line.read(fh)): 87 self.analyzeLine(self.line.line)
88
89 - def goOn(self):
90 """Checks with all the analyzers 91 92 If one analyzer returns False it returns False""" 93 result=True 94 95 for nm in self.analyzers: 96 # print nm,self.analyzers[nm].goOn() 97 result=result and self.analyzers[nm].goOn() 98 99 return result
100
101 - def getTime(self):
102 """Gets the current time""" 103 return str(self.time)
104
105 - def setDirectory(self,d):
106 """Sets the output directory for all the analyzers""" 107 self.oDir=d 108 for nm in self.analyzers: 109 self.analyzers[nm].setDirectory(self.oDir)
110
111 - def getDirectory(self):
112 """Gets the output directory""" 113 return self.oDir
114
115 - def addTrigger(self,time,func,once=True,until=None):
116 """Adds a trigger function that is to be called as soon as 117 the simulation time exceeds a certain value 118 @param time: the time at which the function should be triggered 119 @param func: the trigger function 120 @param once: Should this function be called once or at every time-step 121 @param until: The time until which the trigger should be called""" 122 123 data={} 124 data["time"]=float(time) 125 data["func"]=func 126 if until!=None: 127 data["until"]=float(until) 128 once=False 129 data["once"]=once 130 131 self.timeTriggers.append(data)
132
133 - def checkTriggers(self):
134 """Check for and execute the triggered functions""" 135 136 remove=[] 137 for i in range(len(self.timeTriggers)): 138 t=self.timeTriggers[i] 139 if t["time"]<=self.time: 140 t["func"]() 141 if t["once"]: 142 remove.append(i) 143 elif "until" in t: 144 if t["until"]<=self.time: 145 remove.append(i) 146 147 remove.reverse() 148 149 for i in remove: 150 self.timeTriggers.pop(i)
151