Package PyFoam :: Package Applications :: Module PVSnapshot
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Applications.PVSnapshot

  1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/Applications/PVSnapshot.py 7660 2012-01-07T16:44:40.128256Z bgschaid  $  
  2  """ 
  3  Class that implements pyFoamPVSnapshot 
  4  """ 
  5   
  6  from optparse import OptionGroup 
  7   
  8  from PyFoamApplication import PyFoamApplication 
  9   
 10  from CommonSelectTimesteps import CommonSelectTimesteps 
 11   
 12  from PyFoam.RunDictionary.SolutionDirectory import SolutionDirectory 
 13  from PyFoam.Paraview.ServermanagerWrapper import ServermanagerWrapper as SM 
 14  from PyFoam.Paraview.StateFile import StateFile 
 15  from PyFoam.Paraview import version as PVVersion 
 16   
 17  from PyFoam.FoamInformation import foamVersion 
 18   
 19  from os import path,unlink 
 20  import sys,string 
 21   
22 -class PVSnapshot(PyFoamApplication, 23 CommonSelectTimesteps ):
24 - def __init__(self,args=None):
25 description="""\ 26 Generates snapshots of an OpenFOAM-case and a predefined 27 paraview-State-File using the PV3FoamReader that comes with OpenFOAM. 28 29 The state-file can be generated using a different case (the script 30 adjusts it before using) but the original case has to have a similar 31 structure to the current one. Also exactly one PV3Reader has to be 32 used in the state-file (this requirement is fullfilled if the 33 StateFile was generated using paraFoam) 34 35 In TextSources the string "%(casename)s" gets replaced by the 36 casename. Additional replacements can be specified 37 """ 38 CommonSelectTimesteps.__init__(self) 39 40 PyFoamApplication.__init__(self, 41 args=args, 42 description=description, 43 usage="%prog [options] <case>", 44 interspersed=True, 45 nr=1)
46 47 typeTable={"png":"vtkPNGWriter", 48 "jpg":"vtkJPEGWriter"} 49
50 - def addOptions(self):
51 CommonSelectTimesteps.addOptions(self,defaultUnique=False) 52 53 paraview=OptionGroup(self.parser, 54 "Paraview specifications", 55 "Options concerning paraview") 56 paraview.add_option("--state-file", 57 dest="state", 58 default=None, 59 help="The pvsm-file that should be used. If none is specified the file 'default.pvsm' in the case-directory is used") 60 paraview.add_option("--maginfication", 61 dest="magnification", 62 default=1, 63 type="int", 64 help="Magnification factor of the picture (integer). Default: %default") 65 paraview.add_option("--type", 66 dest="type", 67 type="choice", 68 choices=self.typeTable.keys(), 69 default="png", 70 help="The type of the bitmap-file. Possibilities are "+string.join(self.typeTable.keys(),", ")+". Default: %default") 71 paraview.add_option("--no-progress", 72 dest="progress", 73 action="store_false", 74 default=True, 75 help="Paraview will not print the progress of the filters") 76 self.parser.add_option_group(paraview) 77 78 filename=OptionGroup(self.parser, 79 "Filename specifications", 80 "The names of the resulting files") 81 filename.add_option("--file-prefix", 82 dest="prefix", 83 default="Snapshot", 84 help="Start of the filename for the bitmap-files") 85 filename.add_option("--no-casename", 86 dest="casename", 87 action="store_false", 88 default=True, 89 help="Do not add the casename to the filename") 90 filename.add_option("--no-timename", 91 dest="timename", 92 action="store_false", 93 default=True, 94 help="Do not append the string 't=<time>' to the filename") 95 self.parser.add_option_group(filename) 96 97 replace=OptionGroup(self.parser, 98 "Replacements etc", 99 "Manipuations of the statefile") 100 replace.add_option("--replacements", 101 dest="replacements", 102 default="{}", 103 help="Dictionary with replacement strings. Default: %default") 104 replace.add_option("--casename-key", 105 dest="casenameKey", 106 default="casename", 107 help="Key with which the caename should be replaced. Default: %default")
108
109 - def run(self):
110 if foamVersion()>=(1,6): 111 self.warning("This utilitiy currently does not work with OF>=1.6 because the API in Paraview>=3.6 has changed. But we'll try") 112 113 case=path.abspath(self.parser.getArgs()[0]) 114 short=path.basename(case) 115 116 if self.opts.state==None: 117 self.opts.state=path.join(case,"default.pvsm") 118 119 if not path.exists(self.opts.state): 120 self.error("The state file",self.opts.state,"does not exist") 121 122 timeString="" 123 124 if self.opts.casename: 125 timeString+="_"+short 126 timeString+="_%(nr)05d" 127 if self.opts.timename: 128 timeString+="_t=%(t)s" 129 timeString+="."+self.opts.type 130 131 sol=SolutionDirectory(case, 132 paraviewLink=False, 133 archive=None) 134 135 times=self.processTimestepOptions(sol) 136 if len(times)<1: 137 self.warning("Can't continue without time-steps") 138 return 139 140 dataFile=path.join(case,short+".OpenFOAM") 141 createdDataFile=False 142 if not path.exists(dataFile): 143 createdDataFile=True 144 f=open(dataFile,"w") 145 f.close() 146 147 sf=StateFile(self.opts.state) 148 sf.setCase(dataFile) 149 150 values=eval(self.opts.replacements) 151 values[self.opts.casenameKey]=short 152 sf.rewriteTexts(values) 153 newState=sf.writeTemp() 154 155 sm=SM(requiredReader=sf.readerType()) 156 if not self.opts.progress: 157 sm.ToggleProgressPrinting() 158 159 # print dir(sm.module()) 160 sm.LoadState(newState) 161 views=sm.GetRenderViews() 162 163 if len(views)>1: 164 self.warning("More than 1 view in state-file. Generating multiple series") 165 timeString="_View%(view)02d"+timeString 166 timeString=self.opts.prefix+timeString 167 168 for view in views: 169 view.UseOffscreenRenderingForScreenshots=True 170 171 for i,t in enumerate(times): 172 print "Snapshot ",i," for t=",t 173 for j,view in enumerate(views): 174 view.ViewTime=float(t) 175 fn = timeString % {'nr':i,'t':t,'view':j} 176 if PVVersion()<(3,6): 177 view.WriteImage(fn, 178 self.typeTable[self.opts.type], 179 self.opts.magnification) 180 else: 181 from paraview.simple import SetActiveView,Render,WriteImage 182 SetActiveView(view) 183 Render() 184 # This always produces segmentation fauklts for me 185 WriteImage(fn, 186 view, 187 # Writer=self.typeTable[self.opts.type], 188 Magnification=self.opts.magnification) 189 190 if createdDataFile: 191 self.warning("Removing pseudo-data-file",dataFile) 192 unlink(dataFile) 193 194 del sm
195