1
2 """Analyze OpenFOAM logs"""
3
4 from TimeLineAnalyzer import TimeLineAnalyzer
5 from PyFoam.Basics.LineReader import LineReader
6 from PyFoam.Error import error
7
9 """Base class for all analyzers
10
11 Administrates and calls a number of LogLineAnlayzers for each
12 line"""
13
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
30 """Remove reference to self in children (hoping to remove
31 circular dependencies)"""
32
33 for a in self.analyzers.values():
34 a.setParent(None)
35
37 """Sets the time and alert all the LineAnalyzers that the time has changed
38 @param time: the new value of the time
39 """
40 if time!=self.time:
41 self.time=time
42 for listener in self.timeListeners:
43 listener.timeChanged()
44 for nm in self.analyzers:
45 self.analyzers[nm].timeChanged()
46 self.checkTriggers()
47
49 """@param listener: An object that is notified when the time changes. Has to
50 implement a timeChanged method"""
51 if not 'timeChanged' in dir(listener):
52 error("Error. Object has no timeChanged-method:"+str(listener))
53 else:
54 self.timeListeners.append(listener)
55
57 """@returns: A list with the names of the Analyzers"""
58 return self.analyzers.keys()
59
61 """Get the LogLineAnalyzer name"""
62 if self.analyzers.has_key(name):
63 return self.analyzers[name]
64 else:
65 return None
66
68 """Adds an analyzer
69
70 obj - A LogLineAnalyzer
71 name - the name of the analyzer"""
72
73 obj.setParent(self)
74 self.analyzers[name]=obj
75
77 """Calls all the anlyzers for a line"""
78 for nm in self.analyzers:
79 self.analyzers[nm].doAnalysis(line)
80
82 """Analyzes a file (one line at a time)
83
84 fh - handle of the file"""
85 while(self.line.read(fh)):
86 self.analyzeLine(self.line.line)
87
89 """Checks with all the analyzers
90
91 If one analyzer returns False it returns False"""
92 result=True
93
94 for nm in self.analyzers:
95
96 result=result and self.analyzers[nm].goOn()
97
98 return result
99
101 """Gets the current time"""
102 return str(self.time)
103
105 """Sets the output directory for all the analyzers"""
106 self.oDir=d
107 for nm in self.analyzers:
108 self.analyzers[nm].setDirectory(self.oDir)
109
111 """Gets the output directory"""
112 return self.oDir
113
114 - def addTrigger(self,time,func,once=True,until=None):
115 """Adds a trigger function that is to be called as soon as
116 the simulation time exceeds a certain value
117 @param time: the time at which the function should be triggered
118 @param func: the trigger function
119 @param once: Should this function be called once or at every time-step
120 @param until: The time until which the trigger should be called"""
121
122 data={}
123 data["time"]=float(time)
124 data["func"]=func
125 if until!=None:
126 data["until"]=float(until)
127 once=False
128 data["once"]=once
129
130 self.timeTriggers.append(data)
131
133 """Check for and execute the triggered functions"""
134
135 remove=[]
136 for i in range(len(self.timeTriggers)):
137 t=self.timeTriggers[i]
138 if t["time"]<=self.time:
139 t["func"]()
140 if t["once"]:
141 remove.append(i)
142 elif "until" in t:
143 if t["until"]<=self.time:
144 remove.append(i)
145
146 remove.reverse()
147
148 for i in remove:
149 self.timeTriggers.pop(i)
150