Package PyFoam :: Package Basics :: Module OutFileCollection
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.OutFileCollection

 1  #  ICE Revision: $Id: OutFileCollection.py 10069 2009-03-02 09:39:44Z bgschaid $  
 2  """Collections of output files""" 
 3   
 4  from os import path 
 5   
 6  from OutputFile import OutputFile 
 7   
8 -class OutFileCollection(object):
9 """Collection of output files 10 11 The files are stored in a common directory and are created on 12 first access 13 14 Each file can be identified by a unique name. If a file is 15 accessed a second time at the same simulation-time a file with the 16 ending _2 is created (incrementing with each access)""" 17
18 - def __init__(self, 19 basename, 20 titles=[], 21 singleFile=False):
22 """ 23 @param basename: name of the base directory 24 @param titles: names of the data columns 25 @param singleFile: don't split into multiple files if more than one 26 datum is insert per time-step 27 """ 28 self.files={} 29 self.lastTime="" 30 self.called={} 31 self.basename=basename 32 self.setTitles(titles) 33 self.singleFile=singleFile
34 35 # def __del__(self): 36 # print "\n Deleting this OutputFile\n" 37
38 - def setTitles(self,titles):
39 """ 40 Sets the titles anew 41 42 @param titles: the new titles 43 """ 44 self.titles=titles 45 for f in self.files.items(): 46 f.setTitles(titles)
47
48 - def checkTime(self,time):
49 """check whether the time has changed""" 50 if time!=self.lastTime: 51 self.lastTime=time 52 self.called={}
53
54 - def getFile(self,name):
55 """get a OutputFile-object""" 56 if not self.files.has_key(name): 57 fullname=path.join(self.basename,name) 58 self.files[name]=OutputFile(fullname,titles=self.titles) 59 60 return self.files[name]
61
62 - def prevCalls(self,name):
63 """checks whether the name was used previously at that time-step""" 64 if self.called.has_key(name): 65 return self.called[name] 66 else: 67 return 0
68
69 - def incrementCalls(self,name):
70 """increments the access counter for name""" 71 self.called[name]=1+self.prevCalls(name)
72
73 - def write(self,name,time,data):
74 """writes data to file 75 76 name - name of the file 77 time - simulation time 78 data - tuple with the data""" 79 self.checkTime(time) 80 81 fname=name 82 self.incrementCalls(name) 83 84 if self.prevCalls(name)>1 and not self.singleFile: 85 fname+="_"+str(self.prevCalls(name)) 86 87 f=self.getFile(fname) 88 89 f.write(time,data)
90
91 - def close(self):
92 """Force all files to be closed""" 93 94 for f in self.files: 95 self.files[f].close()
96