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

Source Code for Module PyFoam.Basics.OutFileCollection

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