Package parasol :: Module Plots_Cache'
[hide private]
[frames] | no frames]

Source Code for Module parasol.Plots_Cache'

  1   
  2  # CONSTRAINT  VIOLATIONS are detected in ParametricSoln.violatesResultConstraint() 
  3  #  when the Cached result variables are placed back in ParametricSoln.resultVarDict 
  4  #    (and possibly ParametricSoln.desVarDict if desVar is a control variable 
  5  #     for a minmax or feasible pair) 
  6   
7 -class Plots_Cache(object):
8 - def __init__(self, PS, maxCache=100000):
9 10 self.maxCache = maxCache 11 self.Nresults = 0 12 self.PS = PS 13 self.resultDict = {} #: has tuple of result vars, indexed by desVar values 14 15 self.desVarL = [] #: list of desVar objects (dv) that will index into resultDict 16 # desVarL omits desVars that are control variables, (minmax or feasible) 17 18 self.resultVarL = [] #: list of result var objects (rv) including design control vars 19 # resultVarL includes desVars that are control variables, (minmax or feasible) 20 21 for key,dv in self.PS.desVarDict.items(): 22 if PS.hasMinMaxControlVar(key) or PS.hasFeasibleControlVar(key): 23 # control varaibles in minmax or feasible change like result vars 24 self.resultVarL.append(dv) 25 else: 26 self.desVarL.append(dv) 27 28 for key,rv in self.PS.resultVarDict.items(): 29 self.resultVarL.append(rv) 30 31 print ' ----------------------------------- ' 32 for dv in self.desVarL: 33 print ' DESIGN===>',dv.name,'in Plots_Cache desVar List' 34 for rv in self.resultVarL: 35 print ' RESULT===>',rv.name,'in Plots_Cache resultVar List' 36 print ' ----------------------------------- '
37
38 - def saveParasolState(self):
39 '''Save design variables values in dictionary, dvSavedD, so the state can 40 be restored at a later time by restoreParasolState 41 ''' 42 self.dvSavedD = {} 43 for dv in self.desVarL: 44 self.dvSavedD[dv.name] = dv.val
45
46 - def restoreParasolState(self):
47 '''Restore design variables values from dvSavedD and call 48 ParametricSoln model evaluate(), this is done to restore the last call 49 to saveParasolState 50 ''' 51 for dv in self.desVarL: 52 dv.val = self.dvSavedD[dv.name] 53 self.PS.evaluate()
54
55 - def getResults(self, dump=0):
56 ''' 57 Either retrieve results from cache, or rerun model to get result vars 58 Assume that the desVars have been set in PS to desired values 59 ''' 60 61 dvL = [] 62 for dv in self.desVarL: 63 dvL.append( dv.val ) 64 dvTuple = tuple( dvL ) # tuple of design values is index into resultDict 65 66 if self.resultDict.has_key(dvTuple): 67 # if already in cache, retrieve and place in resultVars "val" attribute 68 rvL = self.resultDict[dvTuple] 69 for i,rv in enumerate( self.resultVarL ): 70 rv.val = rvL[i] 71 #print rv.name,'=',rv.val 72 #print 'M',dvTuple,rvL 73 else: 74 self.PS.evaluate() 75 76 rvL = [] 77 for rv in self.resultVarL: # may contain desVar control vars 78 rvL.append( rv.val ) 79 80 # if maxCache not exceeded, then store result 81 if self.Nresults < self.maxCache: 82 self.Nresults += 1 83 self.resultDict[dvTuple] = rvL 84 #print 'E',dvTuple,rvL 85 86 if dump: 87 keyL = self.resultDict.keys() 88 keyL.sort() 89 for key in keyL: 90 print key,self.resultDict[key]
91
92 - def setUpForFuncCall(self, dvNameL=None, outNameL=None):
93 '''set up so that the ParametricSoln model can be called as a simple function 94 by Carpet plot routine 95 96 for example: 97 setUpForFuncCall( dvL=['a','b'], outL=['x','y']) 98 99 would set up for: 100 x, y = self.funcCall( a, b ) 101 ''' 102 103 self.funcCallDesVarNameL = dvNameL 104 self.funcCAllOutVarNameL = outNameL 105 106 self.violationCoordL = [] # lists all result points (plot points) of violations 107 self.violationDescD = {} # dictionary hold descriptions of violations encountered
108 109
110 - def funcCall(self, *dvL):
111 '''Treats ParametricSoln object like a function call as defined in setUpForFuncCall 112 (only used by Carpet plot routine) 113 ''' 114 #print 'dvL=',dvL,' self.funcCallDesVarNameL=',self.funcCallDesVarNameL 115 116 for i,dvName in enumerate(self.funcCallDesVarNameL): 117 self.PS.setDesignVar( dvName, dvL[i] ) 118 119 self.getResults() 120 121 resultL = [] 122 for outName in self.funcCAllOutVarNameL: 123 if self.PS.hasResultVar(outName): 124 resultL.append( self.PS.getResultVar(outName) ) 125 else: 126 resultL.append( self.PS.getDesignVar(outName) ) 127 128 # check for constraint violations 129 vioList = self.PS.violatesResultConstraint() 130 if len(vioList)>0: 131 self.violationCoordL.append( resultL ) 132 for viol in vioList: 133 self.violationDescD[viol] = viol # only 1 entry per violation description 134 135 return resultL # can be combination of result variables and design variables
136 137
138 - def getViolationXYLists(self):
139 if len(self.violationCoordL)==0: 140 return [],[] 141 142 jj=range(len(self.violationCoordL[0])) 143 144 rl = [[li[j] for li in self.violationCoordL] for j in jj] # a list of lists 145 if len(rl)==1: 146 rl=rl[0] #convert list of 1 list to a list 147 return rl
148