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

Source Code for Module PyFoam.Applications.CaseReporter

  1  #  ICE Revision: $Id$  
  2  """ 
  3  Application class that implements pyFoamSamplePlot.py 
  4  """ 
  5   
  6  import sys,string 
  7   
  8  from PyFoamApplication import PyFoamApplication 
  9  from PyFoam.RunDictionary.SampleDirectory import SampleDirectory 
 10   
 11  from PyFoam.Error import error 
 12   
13 -class SamplePlot(PyFoamApplication):
14 - def __init__(self,args=None):
15 description=""" 16 Reads data from the sample-dictionary and generates appropriate 17 gnuplot-commands 18 """ 19 20 PyFoamApplication.__init__(self, 21 args=args, 22 description=description, 23 usage="%prog [options] <casedir>", 24 nr=1, 25 interspersed=True)
26 27 modeChoices=["separate","timesInOne","fieldsInOne","complete"] 28
29 - def addOptions(self):
30 self.parser.add_option("--line", 31 action="append", 32 default=None, 33 dest="line", 34 help="Thesample line from which data is plotted (can be used more than once)") 35 self.parser.add_option("--field", 36 action="append", 37 default=None, 38 dest="field", 39 help="The fields that are plotted (can be used more than once). If none are specified all found fields are used") 40 self.parser.add_option("--time", 41 action="append", 42 default=None, 43 dest="time", 44 help="The times that are plotted (can be used more than once). If none are specified all found times are used") 45 self.parser.add_option("--min-time", 46 action="store", 47 type="float", 48 default=None, 49 dest="minTime", 50 help="The smallest time that should be used") 51 self.parser.add_option("--max-time", 52 action="store", 53 type="float", 54 default=None, 55 dest="maxTime", 56 help="The biggest time that should be used") 57 self.parser.add_option("--mode", 58 type="choice", 59 default="separate", 60 dest="mode", 61 action="store", 62 choices=self.modeChoices, 63 help="What kind of plots are generated: a) separate for every time and field b) all times of a field in one plot c) all fields of a time in one plot d) all lines in one plot. (Names: "+string.join(self.modeChoices,", ")+") Default: %default") 64 self.parser.add_option("--directory-name", 65 action="store", 66 default="samples", 67 dest="dirName", 68 help="Alternate name for the directory with the samples (Default: %default)") 69 self.parser.add_option("--unscaled", 70 action="store_false", 71 dest="scaled", 72 default=True, 73 help="Don't scale a value to the same range for all plots") 74 self.parser.add_option("--info", 75 action="store_true", 76 dest="info", 77 default=False, 78 help="Print info about the sampled data and exit") 79 self.parser.add_option("--style", 80 action="store", 81 default="lines", 82 dest="style", 83 help="Gnuplot-style for the data (Default: %default)")
84
85 - def run(self):
86 samples=SampleDirectory(self.parser.getArgs()[0],dirName=self.opts.dirName) 87 88 lines=samples.lines() 89 times=samples.times 90 values=samples.values() 91 92 if self.opts.info: 93 print "Times : ",samples.times 94 print "Lines : ",samples.lines() 95 print "Values: ",samples.values() 96 sys.exit(0) 97 98 if self.opts.line==None: 99 error("At least one line has to be specified. Found were",samples.lines()) 100 else: 101 102 for l in self.opts.line: 103 if l not in lines: 104 error("The line",l,"does not exist in",lines) 105 106 if self.opts.maxTime or self.opts.minTime: 107 if self.opts.time: 108 error("Times",self.opts.time,"and range [",self.opts.minTime,",",self.opts.maxTime,"] set: contradiction") 109 self.opts.time=[] 110 if self.opts.maxTime==None: 111 self.opts.maxTime= 1e20 112 if self.opts.minTime==None: 113 self.opts.minTime=-1e20 114 115 for t in times: 116 if float(t)<=self.opts.maxTime and float(t)>=self.opts.minTime: 117 self.opts.time.append(t) 118 119 if len(self.opts.time)==0: 120 error("No times in range [",self.opts.minTime,",",self.opts.maxTime,"] found: ",times) 121 122 plots=[] 123 124 if self.opts.mode=="separate": 125 if self.opts.time==None: 126 self.opts.time=samples.times 127 if self.opts.field==None: 128 self.opts.field=samples.values() 129 for t in self.opts.time: 130 for f in self.opts.field: 131 plots.append(samples.getData(line=self.opts.line, 132 value=[f], 133 time=[t])) 134 elif self.opts.mode=="timesInOne": 135 if self.opts.field==None: 136 self.opts.field=samples.values() 137 for f in self.opts.field: 138 plots.append(samples.getData(line=self.opts.line, 139 value=[f], 140 time=self.opts.time)) 141 elif self.opts.mode=="fieldsInOne": 142 if self.opts.time==None: 143 self.opts.time=samples.times 144 for t in self.opts.time: 145 plots.append(samples.getData(line=self.opts.line, 146 value=self.opts.field, 147 time=[t])) 148 elif self.opts.mode=="complete": 149 plots.append(samples.getData(line=self.opts.line, 150 value=self.opts.field, 151 time=self.opts.time)) 152 153 if self.opts.scaled: 154 vRange=None 155 for p in plots: 156 for d in p: 157 mi,ma=d.range() 158 if vRange==None: 159 vRange=mi,ma 160 else: 161 vRange=min(vRange[0],mi),max(vRange[1],ma) 162 163 result="set term png\n" 164 165 for p in plots: 166 if len(p)<1: 167 continue 168 169 name=self.opts.dirName 170 title=None 171 tIndex=times.index(p[0].time()) 172 173 name+="_"+string.join(self.opts.line,"_") 174 175 if self.opts.mode=="separate": 176 name+="_%s_%04d" % (p[0].name,tIndex) 177 title="%s at t=%f" % (p[0].name,float(p[0].time())) 178 elif self.opts.mode=="timesInOne": 179 if self.opts.time!=None: 180 name+="_"+string.join(self.opts.time,"_") 181 name+="_%s" % p[0].name 182 title="%s" % p[0].name 183 elif self.opts.mode=="fieldsInOne": 184 if self.opts.field!=None: 185 name+="_"+string.join(self.opts.field,"_") 186 name+="_%04d" % tIndex 187 title="t=%f" % float(p[0].time()) 188 elif self.opts.mode=="complete": 189 pass 190 191 name+=".png" 192 result+='set output "%s"\n' % name 193 if title!=None: 194 result+='set title "%s"\n' % title 195 196 result+="plot " 197 if self.opts.scaled: 198 result+="[][%f:%f] " % vRange 199 200 first=True 201 202 for d in p: 203 if first: 204 first=False 205 else: 206 result+=", " 207 208 result+='"%s" using 1:%d ' % (d.file,d.index+1) 209 210 title=None 211 if self.opts.mode=="separate": 212 title="" 213 elif self.opts.mode=="timesInOne": 214 title="t=%f" % float(d.time()) 215 elif self.opts.mode=="fieldsInOne": 216 title="%s" % d.name 217 elif self.opts.mode=="complete": 218 title="%s at t=%f" % (d.name,float(d.time())) 219 220 if len(self.opts.line)>1: 221 title+=" on %s" % d.line() 222 223 if title=="": 224 result+="notitle " 225 else: 226 result+='title "%s" ' % title 227 228 result+="with %s " % self.opts.style 229 230 result+="\n" 231 232 print result
233