def pickGaussPoints(self): varlist = self.varlist orderlist = [] for var in varlist: orderlist.append(range(var.order)) #tensor product of runs - not the best solve self.runords = list(allcombos(*orderlist))
def makeCoeffs(N,indexset,verbose=False): NI=len(indexset) c=np.zeros(NI) iset=indexset[:] zerone=[0,1] sets=[] for n in range(N): sets.append(zerone) #instead of a list, I want an iterator jiter=allcombos(*sets) #for j,entry in enumerate(jset): #jset[j]=np.array(entry) if verbose: print '\n\nmaking coeffs...' while True: try: jx = np.array(jiter.next()) for i,ix in enumerate(iset): ix=np.array(ix) comb = tuple(jx+ix) if comb in iset: c[i]+=(-1)**sum(jx) except StopIteration: break return c
def tensorGrid(N,m,varlist,idx): #print '\n',idx quadptlists=[] quadwtlists=[] for n in range(N): mn = m[n] #print 'mn:',mn var=varlist[varlist.keys()[n]] var.setQuadrature(mn) quadptlists.append(var.pts) quadwtlists.append(var.wts) quadpts = list(allcombos(*quadptlists)) quadwts = list(allcombos(*quadwtlists)) for k,wtset in enumerate(quadwts): quadwts[k]=np.product(wtset) #for i in range(len(quadpts)): # print quadpts[i],quadwts[i] #print 'wts,pts',quadwts,quadpts return quadpts,quadwts
def TotalDegree(orderlist, maxorder): ''' Given [[a,b],[c,d]], returns only possible cominations where for combination (p1,p2), p1+p2<=maxorder ''' start = list(allcombos(*orderlist)) end = [] tossed = 0 for entry in start: if np.sum(entry) <= maxorder: end.append(entry) else: tossed += 1 print 'Discarded', tossed, 'indices from TP' return end
def TotalDegree(orderlist,maxorder): ''' Given [[a,b],[c,d]], returns only possible cominations where for combination (p1,p2), p1+p2<=maxorder ''' start = list(allcombos(*orderlist)) end=[] tossed=0 for entry in start: if np.sum(entry)<=maxorder: end.append(entry) else: tossed+=1 print 'Discarded',tossed,'indices from TP' return end
def TotalDegree(orderlist, maxorder, impwts): ''' Given [[a,b],[c,d]], returns only possible cominations where for combination (p1,p2), p1+p2<=maxorder ''' print ' ...level:', maxorder start = list(allcombos(*orderlist)) end = [] tossed = 0 for entry in start: vals = np.array(entry) * np.array(impwts) if np.sum(vals) <= sum(impwts) / float(len(vals)) * maxorder: end.append(entry) else: tossed += 1 # print 'Discarded index',entry print ' ...discarded', tossed, 'indices from TP' return end
def HyperbolicCross(orderlist, maxorder): ''' Given [[a,b],[c,d]], returns only possible cominations where for combination (p1,p2), p1*p2<=maxorder ''' start = list(allcombos(*orderlist)) end = [] tossed = 0 for entry in start: tot = 1 for e in entry: tot *= e + 1 if tot <= maxorder + 1: end.append(entry) else: tossed += 1 print 'Discarded', tossed, 'indices from TP' return end
def TotalDegree(orderlist,maxorder,impwts): ''' Given [[a,b],[c,d]], returns only possible cominations where for combination (p1,p2), p1+p2<=maxorder ''' print ' ...level:',maxorder start = list(allcombos(*orderlist)) end=[] tossed=0 for entry in start: vals=np.array(entry)*np.array(impwts) if np.sum(vals)<=sum(impwts)/float(len(vals))*maxorder: end.append(entry) else: tossed+=1 # print 'Discarded index',entry print ' ...discarded',tossed,'indices from TP' return end
def HyperbolicCross(orderlist,maxorder): ''' Given [[a,b],[c,d]], returns only possible cominations where for combination (p1,p2), p1*p2<=maxorder ''' start = list(allcombos(*orderlist)) end=[] tossed=0 for entry in start: tot=1 for e in entry: tot*=e+1 if tot<=maxorder+1: end.append(entry) else: tossed+=1 print 'Discarded',tossed,'indices from TP' return end
def oldmakeCoeffs(N,indexset,verbose=True): NI=len(indexset) c=np.zeros(NI) #set up index set as iset iset=indexset[:] #iset=np.array(indexset) #for i,entry in enumerate(iset): # iset[i]=np.array(entry) #print 'index set:',iset #set up potential js as jset zerone=[0,1] sets=[] for n in range(N): sets.append(zerone) jset=list(allcombos(*sets)) #jset=np.array(jset) for j,entry in enumerate(jset): jset[j]=np.array(entry) if verbose: print '\n\nmaking coeffs...' #TODO this is really slow. for i,ix in enumerate(iset): #print ' i:',i,ix ix=np.array(ix) for j,jx in enumerate(jset): #jx=np.array(jx) #print ' j:',j,jx comb = tuple(jx+ix) #print ' i,j,ix+jx',ix,jx,comb,comb in iset, if comb in iset: #print ' adding in',(-1)**sum(jx) c[i]+=(-1)**sum(jx) #else: print ' not adding.' #print 'final c[%i]:' %i,c[i] #for j in range(i+1,NI): # d = indexset[j]-indexset[i] # print ' d:',d # if d.all()>=0 and d.all()<=1: # c[i]+=(-1)**sum(d) #print ' c:',c[i] #print 'c:' #for i,cof in enumerate(c): # print i,cof return c
def loadBackends(self): ''' Sets up post-processing Input: none Output: none ''' backendTypes = ['ROM']#self.input_file('Backend/active','').split(' ') self.BEoutFile = self.case+'.SC' #self.input_file('Backend/outFile','BE.out') self.backends={} for beType in backendTypes: if beType == 'ROM': #TODO bad full tensor product, so fix this orderlist=[] for var in self.varDict.values(): orderlist.append(range(var.expOrd)) tensorprod=list(allcombos(*orderlist)) backend = be.ROM(tensorprod,self.BEoutFile) self.backends['ROM']=backend
def loadSampler(self): #TODO this is wrong! #first need to build index set indexranges=[] for var in self.varDict.values(): # wrong place for this var.setQuadrature(self.input_file) var.setExpansion(self.input_file) indexranges.append(range(var.expOrd)) maxorder = np.max(np.max(indexranges)) iset=self.input_file('Sampler/SC/indexSet','dud') if iset=='dud': print 'Index set not specified; using tensor product.' iset='TP' self.indexSet = IndexSets.chooseSet(indexranges,iset,maxorder) #TODO do I need the max for each var or just the overall max? print '...%i expansion moments used...' %len(self.indexSet) #now make quadrature set multfac = self.input_file('Sampler/SC/quadFactor',0) if multfac==0: print '...Quadrature multiplication factor not specified.' print ' Using 2*(max degree) for quadrature set size.' multfac = 2 self.quadSet=[] for i,indx in enumerate(self.indexSet): ranges=[] for n in indx: ranges.append(range(n*multfac+1)) newEntries=list(allcombos(*ranges)) for ent in newEntries: if ent not in self.quadSet: self.quadSet.append(ent) print '...%i quadrature points used...' %len(self.quadSet) #now stash the quadrature points and weights # - get the max for each dimension listedOrds=zip(*self.quadSet) for v,var in enumerate(self.varDict.values()): maxOrd=max(listedOrds[v])+1 var.setQuadrature(maxOrd) #now load the sampler self.sampler = spr.StochasticPoly(self.varDict,\ self.input_file,\ self.quadSet)
def loadSampler(self): #TODO this is wrong! #first need to build index set indexranges = [] for var in self.varDict.values(): # wrong place for this var.setQuadrature(self.input_file) var.setExpansion(self.input_file) indexranges.append(range(var.expOrd)) maxorder = np.max(np.max(indexranges)) iset = self.input_file('Sampler/SC/indexSet', 'dud') if iset == 'dud': print 'Index set not specified; using tensor product.' iset = 'TP' self.indexSet = IndexSets.chooseSet(indexranges, iset, maxorder) #TODO do I need the max for each var or just the overall max? print '...%i expansion moments used...' % len(self.indexSet) #now make quadrature set multfac = self.input_file('Sampler/SC/quadFactor', 0) if multfac == 0: print '...Quadrature multiplication factor not specified.' print ' Using 2*(max degree) for quadrature set size.' multfac = 2 self.quadSet = [] for i, indx in enumerate(self.indexSet): ranges = [] for n in indx: ranges.append(range(n * multfac + 1)) newEntries = list(allcombos(*ranges)) for ent in newEntries: if ent not in self.quadSet: self.quadSet.append(ent) print '...%i quadrature points used...' % len(self.quadSet) #now stash the quadrature points and weights # - get the max for each dimension listedOrds = zip(*self.quadSet) for v, var in enumerate(self.varDict.values()): maxOrd = max(listedOrds[v]) + 1 var.setQuadrature(maxOrd) #now load the sampler self.sampler = spr.StochasticPoly(self.varDict,\ self.input_file,\ self.quadSet)
def loadSampler(self): indexranges=[] for var in self.varDict.values(): var.setExpansion(self.input_file) indexranges.append(range(var.expOrd)) maxorder = np.max(np.max(indexranges)) iset=self.input_file('Sampler/SC/indexSet','dud') if iset=='dud': print 'Index set not specified; using tensor product.' iset='TP' self.indexSet = IndexSets.chooseSet(indexranges,iset,maxorder) print '...%i expansion moments used...' %len(self.indexSet) #get multiplication factor for quad order = m*exp order multfac = self.input_file('Sampler/SC/quadFactor',0) if multfac==0: print '...Quadrature multiplication factor not specified.' print ' Using 2*(max degree) for quadrature set size.' multfac = 2 #Make quad set to run #TODO this is TP; need reduced (sparse) system self.quadSet=[] listSets=zip(*self.indexSet) ranges=[] for v,var in enumerate(self.varDict.values()): maxOrd = max(listSets[v])+1 quadOrd = maxOrd*multfac+1 var.setQuadrature(quadOrd) ranges.append(range(quadOrd)) self.quadSet=list(allcombos(*ranges)) print '...%i quadrature points used...' %len(self.quadSet) #now stash the quadrature points and weights # - get the max for each dimension listedOrds=zip(*self.quadSet) for v,var in enumerate(self.varDict.values()): maxOrd=max(listedOrds[v])+1 var.setQuadrature(maxOrd) #now load the sampler self.sampler = spr.StochasticPoly(self.varDict,\ self.input_file,\ self.quadSet)
def loadSampler(self): indexranges=[] for var in self.varDict.values(): var.setExpansion(self.input_file) indexranges.append(range(var.expOrd)) maxorder = np.max(np.max(indexranges)) iset=self.input_file('Sampler/SC/indexSet','dud') if iset=='dud': print 'Index set not specified; using tensor product.' iset='TP' self.indexSet = IndexSets.chooseSet(indexranges,iset,maxorder) print '...%i expansion moments used...' %len(self.indexSet) #get multiplication factor for quad order = m*exp order multfac = self.input_file('Sampler/SC/quadFactor',0) if multfac==0: print '...Quadrature multiplication factor not specified.' print ' Using 2*(max degree) for quadrature set size.' multfac = 2 #Make quad set to run #TODO this is TP; need reduced (sparse) system self.quadSet=[] listSets=zip(*self.indexSet) ranges=[] for v,var in enumerate(self.varDict.values()): maxOrd = max(listedOrds[v])+1 quadOrd = maxOrd*multFac+1 var.setQuadrature(quadOrd) ranges.append(range(quadOrd)) self.quadSet=list(allcombos(*ranges)) print '...%i quadrature points used...' %len(self.quadSet) #now stash the quadrature points and weights # - get the max for each dimension listedOrds=zip(*self.quadSet) for v,var in enumerate(self.varDict.values()): maxOrd=max(listedOrds[v])+1 var.setQuadrature(maxOrd) #now load the sampler self.sampler = spr.StochasticPoly(self.varDict,\ self.input_file,\ self.quadSet)
def parMakeCoeffs(self,nump,N,indexset,verbose=False): procs=[] done=False allStarted=False NI=len(indexset) c=np.zeros(NI) iset=indexset[:] jiter=allcombos([0,1],repeat=N) numdone=0 numtodo = 2**N if verbose: print '\n\nmaking coeffs...' while not done: for p,proc in enumerate(procs): if not proc.is_alive(): proc.join() while not self.coefq.empty(): n,cofs = self.coefq.get() numdone+=n c+=cofs del procs[p] print ' ...finished jx %i/%i (%i pct)...\r'%(numdone,numtodo,100*float(numdone)/float(numtodo)), if allStarted and len(procs)==0: done=True break while len(procs)<nump and not allStarted: try: jxs=[] for i in range(500): jxs.append( np.array(jiter.next() )) procs.append(multiprocessing.Process(target=self.makeCoeffBatch,args=[jxs,NI,iset])) procs[-1].start() except StopIteration: allStarted=True if len(jxs)>0: procs.append(multiprocessing.Process(target=self.makeCoeffBatch,args=[jxs,NI,iset])) procs[-1].start() break return c
def HyperbolicCross(orderlist, maxorder, impwts): ''' Given [[a,b],[c,d]], returns only possible cominations where for combination (p1,p2), p1*p2<=maxorder ''' #TODO importance weighting #print 'starting HC index generation...' #print len(orderlist),len(orderlist[0]) #print orderlist start = list(allcombos(*orderlist)) target = (maxorder + 1)**(sum(impwts) / float(len(impwts))) #print 'target:',target end = [] tossed = 0 for entry in start: tot = 1 for e, val in enumerate(entry): tot *= (val + 1)**impwts[e] if tot <= target: end.append(entry) else: tossed += 1 print ' ...discarded', tossed, 'indices from TP' return end
def HyperbolicCross(orderlist,maxorder,impwts): ''' Given [[a,b],[c,d]], returns only possible cominations where for combination (p1,p2), p1*p2<=maxorder ''' #TODO importance weighting #print 'starting HC index generation...' #print len(orderlist),len(orderlist[0]) #print orderlist start = list(allcombos(*orderlist)) target = (maxorder+1)**(sum(impwts)/float(len(impwts))) #print 'target:',target end=[] tossed=0 for entry in start: tot=1 for e,val in enumerate(entry): tot*=(val+1)**impwts[e] if tot<=target: end.append(entry) else: tossed+=1 print ' ...discarded',tossed,'indices from TP' return end
def TensorProduct(orderlist, maxorder=0): ''' Given [[a,b],[c,d]], returns all possible cominations: [[a,c],[a,d],[b,c],[b,d]] ''' return list(allcombos(*orderlist))
def TensorProduct(orderlist,maxorder=0): ''' Given [[a,b],[c,d]], returns all possible cominations: [[a,c],[a,d],[b,c],[b,d]] ''' return list(allcombos(*orderlist))