1 """ Working with solutions """
2
3 import re,os
4 import gzip
5 from os import path
6 from tempfile import mktemp
7
8 from PyFoam.Basics.LineReader import LineReader
9 from FileBasis import FileBasis
10
12 """ Solution data file
13
14 Represents a file with the solution data for one
15 OpenFOAM-field at one point of time
16
17 Currently this can only handle uniform field values"""
18
20 """ @param directory: name of the directory containing the solutions
21 for a specific time
22 @param name: name of the field. If the field is zipped the .gz is
23 appended"""
24
25 FileBasis.__init__(self,path.abspath(path.join(directory,name)))
26 if path.exists(self.name):
27 self.zipped=False
28 else:
29 self.zipped=True
30 self.name+=".gz"
31
32 self.fh=None
33
35 """opens the file (if zipped, the file is written zipped)"""
36
37 if self.zipped:
38 self.fh=gzip.open(self.name,"r")
39 else:
40 self.fh=open(self.name,"r")
41
43 """creates a temporary file (if the original is zipped, this
44 is zipped too"""
45 fn=mktemp(dir=path.dirname(self.name))
46 if self.zipped:
47 fh=gzip.open(fn,"w")
48 else:
49 fh=open(fn,"w")
50
51 return fh,fn
52
54 """pattern for the dimension string"""
55 return re.compile("^dimensions +\[(.+)\]\s*;")
56
58 """pattern for internal fields"""
59 return re.compile("^internalField +uniform +(.+);")
60
62 """pattern for internal fields"""
63 return re.compile("^internalField +nonuniform .+[0-9]\((.+)\);")
64
66 """pattern for values"""
67 return re.compile("value +uniform +(.+);")
68
70 """pattern that ends a boundary"""
71 return re.compile("^\b*}")
72
74 """read the value at a boundary
75
76 name - the name of the boundary patch"""
77 exp=self.valuePattern()
78 erg=""
79
80 l=LineReader()
81 self.openFile()
82
83 self.goTo(l,"boundaryField")
84 self.goTo(l,name)
85
86 m=self.goMatch(l,exp)
87 if m!=None:
88 erg=m.group(1)
89
90 self.closeFile()
91 return erg
92
94 """write the value at a boundary
95
96 @param name: the name of the boundary patch
97 @param newval: the new value"""
98 exp=self.valuePattern()
99
100 l=LineReader()
101 self.openFile()
102
103 fh,fn=self.makeTemp()
104
105 self.goTo(l,"boundaryField",out=fh,echoLast=True)
106 self.goTo(l,name,out=fh,echoLast=True)
107
108 m=self.goMatch(l,exp,out=fh,stop=self.stopPattern())
109
110 if m!=None:
111 if type(m)==str:
112 fh.write("value uniform "+str(newval)+"; "+self.addedString+"\n")
113 fh.write(l.line+"\n")
114 else:
115 fh.write(self.removedString+l.line+"\n")
116 fh.write("value uniform "+str(newval)+"; "+self.addedString+"\n")
117 else:
118 fh.write(l.line+"\n")
119
120 self.copyRest(l,fh)
121
122 self.closeFile()
123 fh.close()
124 os.rename(fn,self.name)
125
127 """read the value of the internal field"""
128 exp=self.internalPattern()
129 erg=""
130
131 l=LineReader()
132 self.openFile()
133
134 while l.read(self.fh):
135 m=exp.match(l.line)
136 if m!=None:
137 erg=m.group(1)
138 break
139
140 self.closeFile()
141 return erg
142
144 """read the dimension of the field"""
145 exp=self.dimensionPattern()
146 erg=""
147
148 l=LineReader()
149 self.openFile()
150
151 while l.read(self.fh):
152 m=exp.match(l.line)
153 if m!=None:
154 erg=m.group(1)
155 break
156
157 self.closeFile()
158 return erg
159
161 """builds a dimension string from the dimension information in the file"""
162 dim=self.readDimension()
163 units=["kg","m","s","K","mol","A","cd"]
164 dims=dim.split()
165
166 result=""
167
168 for i in range(len(dims)):
169 if float(dims[i])==1.:
170 result+=" "+units[i]
171 elif float(dims[i])!=0.:
172 result+=" "+units[i]+"^"+dims[i]
173
174 if result=="":
175 result="1"
176 else:
177 result=result[1:]
178
179 return result
180
197
222