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

Source Code for Module PyFoam.RunDictionary.SolutionFile

  1  #  ICE Revision: $Id: SolutionFile.py 7832 2007-08-28 13:07:26Z bgschaid $  
  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   
10 -class SolutionFile(FileBasis):
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
20 - def __init__(self,directory,name):
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
27 - def dimensionPattern(self):
28 """pattern for the dimension string""" 29 return re.compile("^dimensions +\[(.+)\]\s*;")
30
31 - def internalPatternUniform(self):
32 """pattern for internal fields""" 33 return re.compile("^internalField +uniform +(.+);")
34
35 - def internalPattern(self):
36 """pattern for internal fields""" 37 return re.compile("^internalField +nonuniform .+[0-9]\((.+)\);")
38
39 - def internalPatternGeneral(self):
40 """general pattern for internal fields""" 41 return re.compile("^internalField +(non|)uniform +(.+);")
42
43 - def valuePattern(self):
44 """pattern for values""" 45 return re.compile("value +uniform +(.+);")
46
47 - def stopPattern(self):
48 """pattern that ends a boundary""" 49 return re.compile("^\b*}")
50
51 - def readBoundary(self,name):
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
71 - def replaceBoundary(self,name,newval):
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
104 - def readInternal(self):
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
121 - def readDimension(self):
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
138 - def getDimensionString(self):
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
159 - def readInternalUniform(self):
160 """read the value of the internal field""" 161 exp=self.internalPatternUniform() 162 erg="" 163 164 l=LineReader() 165 self.openFile() 166 167 while l.read(self.fh): 168 m=exp.match(l.line) 169 if m!=None: 170 erg=m.group(1) 171 break 172 173 self.closeFile() 174 return erg
175
176 - def replaceInternal(self,newval):
177 """overwrite the value of the internal field 178 179 newval - the new value""" 180 exp=self.internalPatternGeneral() 181 182 l=LineReader() 183 self.openFile() 184 185 fh,fn=self.makeTemp() 186 187 m=self.goMatch(l,exp,out=fh) 188 189 if m!=None: 190 fh.write(self.removedString+l.line+"\n") 191 fh.write("internalField uniform "+str(newval)+"; "+self.addedString+"\n") 192 else: 193 fh.write(l.line+"\n") 194 195 self.copyRest(l,fh) 196 197 self.closeFile() 198 fh.close() 199 os.rename(fn,self.name)
200