Package PyFoam :: Package RunDictionary :: Module SolutionFile
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.RunDictionary.SolutionFile

  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   
11 -class SolutionFile(FileBasis):
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
19 - def __init__(self,directory,name):
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
34 - def openFile(self):
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
42 - def makeTemp(self):
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
53 - def dimensionPattern(self):
54 """pattern for the dimension string""" 55 return re.compile("^dimensions +\[(.+)\]\s*;")
56
57 - def internalPatternUniform(self):
58 """pattern for internal fields""" 59 return re.compile("^internalField +uniform +(.+);")
60
61 - def internalPattern(self):
62 """pattern for internal fields""" 63 return re.compile("^internalField +nonuniform .+[0-9]\((.+)\);")
64
65 - def valuePattern(self):
66 """pattern for values""" 67 return re.compile("value +uniform +(.+);")
68
69 - def stopPattern(self):
70 """pattern that ends a boundary""" 71 return re.compile("^\b*}")
72
73 - def readBoundary(self,name):
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
93 - def replaceBoundary(self,name,newval):
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
126 - def readInternal(self):
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
143 - def readDimension(self):
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
160 - def getDimensionString(self):
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
181 - def readInternalUniform(self):
182 """read the value of the internal field""" 183 exp=self.internalPatternUniform() 184 erg="" 185 186 l=LineReader() 187 self.openFile() 188 189 while l.read(self.fh): 190 m=exp.match(l.line) 191 if m!=None: 192 erg=m.group(1) 193 break 194 195 self.closeFile() 196 return erg
197
198 - def replaceInternal(self,newval):
199 """overwrite the value of the internal field 200 201 newval - the new value""" 202 exp=self.internalPattern() 203 204 l=LineReader() 205 self.openFile() 206 207 fh,fn=self.makeTemp() 208 209 m=self.goMatch(l,exp,out=fh) 210 211 if m!=None: 212 fh.write(self.removedString+l.line+"\n") 213 fh.write("internalField uniform "+str(newval)+"; "+self.addedString+"\n") 214 else: 215 fh.write(l.line+"\n") 216 217 self.copyRest(l,fh) 218 219 self.closeFile() 220 fh.close() 221 os.rename(fn,self.name)
222