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

Source Code for Module PyFoam.RunDictionary.SolutionFile

  1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/RunDictionary/SolutionFile.py 2962 2008-03-31T17:30:07.010870Z 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  from ParsedParameterFile import ParsedParameterFile 
 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 (and never 18 will handle more because the ParsedParameterFile-class does a 19 much better job)""" 20
21 - def __init__(self,directory,name):
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
28 - def dimensionPattern(self):
29 """pattern for the dimension string""" 30 return re.compile("^dimensions +\[(.+)\]\s*;")
31
32 - def internalPatternUniform(self):
33 """pattern for internal fields""" 34 return re.compile("^internalField +uniform +(.+);")
35
36 - def internalPattern(self):
37 """pattern for internal fields""" 38 return re.compile("^internalField +nonuniform .+[0-9]\((.+)\);")
39
40 - def internalPatternGeneral(self):
41 """general pattern for internal fields""" 42 return re.compile("^internalField +(non|)uniform +(.+);")
43
44 - def valuePattern(self):
45 """pattern for values""" 46 return re.compile("value +uniform +(.+);")
47
48 - def stopPattern(self):
49 """pattern that ends a boundary""" 50 return re.compile("^\b*}")
51
52 - def readBoundary(self,name):
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
72 - def replaceBoundary(self,name,newval):
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
105 - def readInternal(self):
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
122 - def readDimension(self):
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
139 - def getDimensionString(self):
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
160 - def readInternalUniform(self):
161 """read the value of the internal field""" 162 exp=self.internalPatternUniform() 163 erg="" 164 165 l=LineReader() 166 self.openFile() 167 168 while l.read(self.fh): 169 m=exp.match(l.line) 170 if m!=None: 171 erg=m.group(1) 172 break 173 174 self.closeFile() 175 return erg
176
177 - def replaceInternal(self,newval):
178 """overwrite the value of the internal field 179 180 newval - the new value""" 181 exp=self.internalPatternGeneral() 182 183 l=LineReader() 184 self.openFile() 185 186 fh,fn=self.makeTemp() 187 188 m=self.goMatch(l,exp,out=fh) 189 190 if m!=None: 191 fh.write(self.removedString+l.line+"\n") 192 fh.write("internalField uniform "+str(newval)+"; "+self.addedString+"\n") 193 else: 194 fh.write(l.line+"\n") 195 196 self.copyRest(l,fh) 197 198 self.closeFile() 199 fh.close() 200 os.rename(fn,self.realName())
201
202 - def getContent(self,listLengthUnparsed=None):
203 """Returns the parsed content of the file""" 204 return ParsedParameterFile(self.name,listLengthUnparsed=listLengthUnparsed)
205