1
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
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
41
42
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
58
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
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
79
80 old.close(temporary=True)
81
82
83 self.openList.append(name)
84
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
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
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
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