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

Source Code for Module PyFoam.Basics.OutFileCollection

  1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/Basics/OutFileCollection.py 5789 2009-11-06T12:32:54.086322Z bgschaid  $  
  2  """Collections of output files""" 
  3   
  4  from os import path 
  5   
  6  from OutputFile import OutputFile 
  7   
  8  from PyFoam import configuration as conf 
  9   
10 -class OutFileCollection(object):
11 """Collection of output files 12 13 The files are stored in a common directory and are created on 14 first access 15 16 Each file can be identified by a unique name. If a file is 17 accessed a second time at the same simulation-time a file with the 18 ending _2 is created (incrementing with each access)""" 19 20 maxOpenFiles=10 21
22 - def __init__(self, 23 basename, 24 titles=[], 25 singleFile=False):
26 """ 27 @param basename: name of the base directory 28 @param titles: names of the data columns 29 @param singleFile: don't split into multiple files if more than one 30 datum is insert per time-step 31 """ 32 self.files={} 33 self.lastTime="" 34 self.called={} 35 self.basename=basename 36 self.setTitles(titles) 37 self.singleFile=singleFile 38 self.openList=[]
39 40 # def __del__(self): 41 # print "\n Deleting this OutputFile\n" 42
43 - def setTitles(self,titles):
44 """ 45 Sets the titles anew 46 47 @param titles: the new titles 48 """ 49 self.titles=titles 50 for f in self.files.items(): 51 f.setTitles(titles)
52
53 - def checkTime(self,time):
54 """check whether the time has changed""" 55 if time!=self.lastTime: 56 self.lastTime=time 57 self.called={}
58
59 - def getFile(self,name):
60 """get a OutputFile-object""" 61 62 if not self.files.has_key(name): 63 fullname=path.join(self.basename,name) 64 self.files[name]=OutputFile(fullname,titles=self.titles,parent=self) 65 66 return self.files[name]
67
68 - def addToOpenList(self,name):
69 """Adds a file to the list of open files. Closes another 70 file if limit is reached""" 71 try: 72 ind=self.openList.index(name) 73 self.openList=self.openList[:ind]+self.openList[ind+1:] 74 except ValueError: 75 if len(self.openList)>=OutFileCollection.maxOpenFiles: 76 old=self.files[self.openList[0]] 77 self.openList=self.openList[1:] 78 # print "Closing",old.name 79 # assert old.handle!=None 80 old.close(temporary=True) 81 # assert old.handle==None 82 83 self.openList.append(name)
84
85 - def removeFromOpenList(self,name):
86 """Adds a file to the list of open files. Closes another 87 file if limit is reached""" 88 try: 89 ind=self.openList.index(name) 90 self.openList=self.openList[:ind]+self.openList[ind+1:] 91 except ValueError: 92 pass
93
94 - def prevCalls(self,name):
95 """checks whether the name was used previously at that time-step""" 96 if self.called.has_key(name): 97 return self.called[name] 98 else: 99 return 0
100
101 - def incrementCalls(self,name):
102 """increments the access counter for name""" 103 self.called[name]=1+self.prevCalls(name)
104
105 - def write(self,name,time,data):
106 """writes data to file 107 108 name - name of the file 109 time - simulation time 110 data - tuple with the data""" 111 self.checkTime(time) 112 113 fname=name 114 self.incrementCalls(name) 115 116 if self.prevCalls(name)>1 and not self.singleFile: 117 fname+="_"+str(self.prevCalls(name)) 118 119 f=self.getFile(fname) 120 121 try: 122 f.write(time,data) 123 except IOError,e: 124 print self.openList 125 print len(self.files) 126 print self.files 127 print "Open:", 128 cnt=0 129 for f in self.files: 130 if self.files[f].handle!=None: 131 print f, 132 cnt+=1 133 print 134 print "Actually open",cnt,"of",len(self.files) 135 raise e
136
137 - def close(self):
138 """Force all files to be closed""" 139 140 for f in self.files: 141 self.files[f].close()
142 143 OutFileCollection.maxOpenFiles=int(conf().get("OutfileCollection","maximumOpenFiles")) 144