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 from ParsedParameterFile import ParsedParameterFile
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 (and never
18 will handle more because the ParsedParameterFile-class does a
19 much better job)"""
20
22 """ @param directory: name of the directory containing the solutions
23 for a specific time
24 @param name: name of the field."""
25
26 FileBasis.__init__(self,path.abspath(path.join(directory,name)))
27
29 """pattern for the dimension string"""
30 return re.compile("^dimensions +\[(.+)\]\s*;")
31
35
37 """pattern for internal fields"""
38 return re.compile("^internalField +nonuniform .+[0-9]\((.+)\);")
39
41 """general pattern for internal fields"""
42 return re.compile("^internalField +(non|)uniform +(.+);")
43
45 """pattern for values"""
46 return re.compile("value +uniform +(.+);")
47
49 """pattern that ends a boundary"""
50 return re.compile("^\b*}")
51
53 """read the value at a boundary
54
55 name - the name of the boundary patch"""
56 exp=self.valuePattern()
57 erg=""
58
59 l=LineReader()
60 self.openFile()
61
62 self.goTo(l,"boundaryField")
63 self.goTo(l,name)
64
65 m=self.goMatch(l,exp)
66 if m!=None:
67 erg=m.group(1)
68
69 self.closeFile()
70 return erg
71
73 """write the value at a boundary
74
75 @param name: the name of the boundary patch
76 @param newval: the new value"""
77 exp=self.valuePattern()
78
79 l=LineReader()
80 self.openFile()
81
82 fh,fn=self.makeTemp()
83
84 self.goTo(l,"boundaryField",out=fh,echoLast=True)
85 self.goTo(l,name,out=fh,echoLast=True)
86
87 m=self.goMatch(l,exp,out=fh,stop=self.stopPattern())
88
89 if m!=None:
90 if type(m)==str:
91 fh.write("value uniform "+str(newval)+"; "+self.addedString+"\n")
92 fh.write(l.line+"\n")
93 else:
94 fh.write(self.removedString+l.line+"\n")
95 fh.write("value uniform "+str(newval)+"; "+self.addedString+"\n")
96 else:
97 fh.write(l.line+"\n")
98
99 self.copyRest(l,fh)
100
101 self.closeFile()
102 fh.close()
103 os.rename(fn,self.realName())
104
106 """read the value of the internal field"""
107 exp=self.internalPattern()
108 erg=""
109
110 l=LineReader()
111 self.openFile()
112
113 while l.read(self.fh):
114 m=exp.match(l.line)
115 if m!=None:
116 erg=m.group(1)
117 break
118
119 self.closeFile()
120 return erg
121
123 """read the dimension of the field"""
124 exp=self.dimensionPattern()
125 erg=""
126
127 l=LineReader()
128 self.openFile()
129
130 while l.read(self.fh):
131 m=exp.match(l.line)
132 if m!=None:
133 erg=m.group(1)
134 break
135
136 self.closeFile()
137 return erg
138
140 """builds a dimension string from the dimension information in the file"""
141 dim=self.readDimension()
142 units=["kg","m","s","K","mol","A","cd"]
143 dims=dim.split()
144
145 result=""
146
147 for i in range(len(dims)):
148 if float(dims[i])==1.:
149 result+=" "+units[i]
150 elif float(dims[i])!=0.:
151 result+=" "+units[i]+"^"+dims[i]
152
153 if result=="":
154 result="1"
155 else:
156 result=result[1:]
157
158 return result
159
176
201
202 - def getContent(self,listLengthUnparsed=None):
203 """Returns the parsed content of the file"""
204 return ParsedParameterFile(self.name,listLengthUnparsed=listLengthUnparsed)
205