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

Source Code for Module PyFoam.RunDictionary.SolutionDirectory

  1  """Working with a solution directory""" 
  2   
  3  from PyFoam.Basics.Utilities import Utilities 
  4  from PyFoam.Basics.BasicFile import BasicFile 
  5   
  6  from os import listdir,path,mkdir 
  7  import os 
  8   
9 -class SolutionDirectory(Utilities):
10 """Represents a solution directory 11 12 In the solution directory subdirectories whose names are numbers 13 are assumed to be solutions for a specific time-step 14 15 A sub-directory (called the Archive) is created to which solution 16 data is copied""" 17
18 - def __init__(self,name,archive="ArchiveDir"):
19 """@param name: Name of the solution directory 20 @param archive: name of the directory where the lastToArchive-method 21 should copy files, if None no archive is created""" 22 self.name=name 23 self.archive=None 24 if archive!=None: 25 self.archive=path.join(name,archive) 26 if not path.exists(self.archive): 27 mkdir(self.archive) 28 29 self.backups=[] 30 31 self.reread() 32 33 self.essential=[self.systemDir(),self.constantDir(),self.initialDir()]
34
35 - def addToClone(self,name):
36 """add directory to the list that is needed to clone this case 37 @param name: name of the subdirectory (the case directory is prepended)""" 38 self.essential.append(path.join(self.name,name))
39
40 - def cloneCase(self,name):
41 """create a clone of this case directory. Remove the target directory, if it already exists 42 43 @param name: Name of the new case directory 44 @rtype: L{SolutionDirectory} or correct subclass 45 @return: The target directory""" 46 47 if path.exists(name): 48 self.execute("rm -r "+name) 49 mkdir(name) 50 for d in self.essential: 51 self.execute("cp -r "+d+" "+name) 52 53 return self.__class__(name)
54
55 - def reread(self):
56 """Rescan the directory for the time directories""" 57 self.times=[] 58 self.first=None 59 self.last=None 60 61 for f in listdir(self.name): 62 try: 63 val=float(f) 64 self.times.append(f) 65 if self.first==None: 66 self.first=f 67 else: 68 if float(f)<float(self.first): 69 self.first=f 70 if self.last==None: 71 self.last=f 72 else: 73 if float(f)>float(self.last): 74 self.last=f 75 76 except ValueError: 77 pass 78 79 self.times.sort(self.sorttimes)
80
81 - def sorttimes(self,x,y):
82 """Sort function for the solution files""" 83 if(float(x)==float(y)): 84 return 0 85 elif float(x)<float(y): 86 return -1 87 else: 88 return 1
89 90
91 - def getTimes(self):
92 """ @return: List of all the available times""" 93 self.reread() 94 return self.times
95
96 - def addBackup(self,pth):
97 """add file to list of files that are to be copied to the 98 archive""" 99 self.backups.append(path.join(self.name,pth))
100
101 - def getLast(self):
102 """@return: the last time for which a solution exists 103 @rtype: str""" 104 self.reread() 105 return self.last
106
107 - def lastToArchive(self,name):
108 """copy the last solution (plus the backup-files to the 109 archive) 110 111 @param name: name of the sub-directory in the archive""" 112 if self.archive==None: 113 print "Warning: nor Archive-directory" 114 return 115 116 self.reread() 117 fname=path.join(self.archive,name) 118 if path.exists(fname): 119 self.execute("rm -r "+fname) 120 mkdir(fname) 121 self.execute("cp -r "+path.join(self.name,self.last)+" "+fname) 122 for f in self.backups: 123 self.execute("cp -r "+f+" "+fname)
124
125 - def clearResults(self,after=None):
126 """remove all time-directories after a certain time. If not time ist 127 set the initial time is used""" 128 129 self.reread() 130 131 if after==None: 132 time=float(self.first) 133 else: 134 time=float(after) 135 136 for f in self.times: 137 if float(f)>time: 138 self.execute("rm -r "+path.join(self.name,f))
139
140 - def initialDir(self):
141 """@return: the name of the first time-directory (==initial 142 conditions 143 @rtype: str""" 144 return path.join(self.name,self.first)
145
146 - def latestDir(self):
147 """@return: the name of the first last-directory (==simulation 148 results) 149 @rtype: str""" 150 return path.join(self.name,self.getLast())
151
152 - def constantDir(self):
153 """@return: the name of the C{constant}-directory 154 @rtype: str""" 155 return path.join(self.name,"constant")
156
157 - def systemDir(self):
158 """@return: the name of the C{system}-directory 159 @rtype: str""" 160 return path.join(self.name,"system")
161
162 - def controlDict(self):
163 """@return: the name of the C{controlDict} 164 @rtype: str""" 165 return path.join(self.systemDir(),"controlDict")
166
167 - def polyMeshDir(self):
168 """@return: the name of the C{polyMesh} 169 @rtype: str""" 170 return path.join(self.constantDir(),"polyMesh")
171
172 - def blockMesh(self):
173 """@return: the name of the C{blockMeshDict} if it exists. Returns 174 an empty string if it doesn't 175 @rtype: str""" 176 p=path.join(self.polyMeshDir(),"blockMeshDict") 177 if path.exists(p): 178 return p 179 else: 180 return ""
181
182 - def makeFile(self,name):
183 """create a file in the solution directory and return a 184 corresponding BasicFile-object 185 186 @param name: Name of the file 187 @rtype: L{BasicFile}""" 188 return BasicFile(path.join(self.name,name))
189
190 -class ChemkinSolutionDirectory(SolutionDirectory):
191 """Solution directory with a directory for the Chemkin-files""" 192 193 chemkinName = "chemkin" 194
195 - def __init__(self,name,archive="ArchiveDir"):
196 SolutionDirectory.__init__(self,name,archive=archive) 197 198 self.addToClone(self.chemkinName)
199
200 - def chemkinDir(self):
201 """@rtype: str 202 @return: The directory with the Chemkin-Files""" 203 204 return path.join(self.name,self.chemkinName)
205