1
2 """ Working with solutions """
3
4 import re,os
5 from os import path
6
7 from PyFoam.Basics.LineReader import LineReader
8 from FileBasis import FileBasis
9
11 """ Solution data file
12
13 Represents a file with the solution data for one
14 OpenFOAM-field at one point of time
15
16 Currently this can only handle uniform field values (and never
17 will handle more because the ParsedParameterFile-class does a
18 much better job)"""
19
21 """ @param directory: name of the directory containing the solutions
22 for a specific time
23 @param name: name of the field."""
24
25 FileBasis.__init__(self,path.abspath(path.join(directory,name)))
26
28 """pattern for the dimension string"""
29 return re.compile("^dimensions +\[(.+)\]\s*;")
30
32 """pattern for internal fields"""
33 return re.compile("^internalField +uniform +(.+);")
34
36 """pattern for internal fields"""
37 return re.compile("^internalField +nonuniform .+[0-9]\((.+)\);")
38
40 """general pattern for internal fields"""
41 return re.compile("^internalField +(non|)uniform +(.+);")
42
44 """pattern for values"""
45 return re.compile("value +uniform +(.+);")
46
48 """pattern that ends a boundary"""
49 return re.compile("^\b*}")
50
52 """read the value at a boundary
53
54 name - the name of the boundary patch"""
55 exp=self.valuePattern()
56 erg=""
57
58 l=LineReader()
59 self.openFile()
60
61 self.goTo(l,"boundaryField")
62 self.goTo(l,name)
63
64 m=self.goMatch(l,exp)
65 if m!=None:
66 erg=m.group(1)
67
68 self.closeFile()
69 return erg
70
72 """write the value at a boundary
73
74 @param name: the name of the boundary patch
75 @param newval: the new value"""
76 exp=self.valuePattern()
77
78 l=LineReader()
79 self.openFile()
80
81 fh,fn=self.makeTemp()
82
83 self.goTo(l,"boundaryField",out=fh,echoLast=True)
84 self.goTo(l,name,out=fh,echoLast=True)
85
86 m=self.goMatch(l,exp,out=fh,stop=self.stopPattern())
87
88 if m!=None:
89 if type(m)==str:
90 fh.write("value uniform "+str(newval)+"; "+self.addedString+"\n")
91 fh.write(l.line+"\n")
92 else:
93 fh.write(self.removedString+l.line+"\n")
94 fh.write("value uniform "+str(newval)+"; "+self.addedString+"\n")
95 else:
96 fh.write(l.line+"\n")
97
98 self.copyRest(l,fh)
99
100 self.closeFile()
101 fh.close()
102 os.rename(fn,self.name)
103
105 """read the value of the internal field"""
106 exp=self.internalPattern()
107 erg=""
108
109 l=LineReader()
110 self.openFile()
111
112 while l.read(self.fh):
113 m=exp.match(l.line)
114 if m!=None:
115 erg=m.group(1)
116 break
117
118 self.closeFile()
119 return erg
120
122 """read the dimension of the field"""
123 exp=self.dimensionPattern()
124 erg=""
125
126 l=LineReader()
127 self.openFile()
128
129 while l.read(self.fh):
130 m=exp.match(l.line)
131 if m!=None:
132 erg=m.group(1)
133 break
134
135 self.closeFile()
136 return erg
137
139 """builds a dimension string from the dimension information in the file"""
140 dim=self.readDimension()
141 units=["kg","m","s","K","mol","A","cd"]
142 dims=dim.split()
143
144 result=""
145
146 for i in range(len(dims)):
147 if float(dims[i])==1.:
148 result+=" "+units[i]
149 elif float(dims[i])!=0.:
150 result+=" "+units[i]+"^"+dims[i]
151
152 if result=="":
153 result="1"
154 else:
155 result=result[1:]
156
157 return result
158
175
200