def __init__(self,XMINS,XMAXS,nCs, zero_v_across_bdry, vol_preserve,warp_around=None, cpa_calcs=None): # XMINS = [-x for x in XMAXS] # DEBUG if vol_preserve: raise ValueError("Doesn't make sense to use it for the 1D case") self.warp_around=warp_around super(CpaSpace,self).__init__(XMINS,XMAXS,nCs, zero_v_across_bdry, vol_preserve=vol_preserve, warp_around=warp_around, zero_vals=[], cpa_calcs=cpa_calcs) nCx=int(nCs[0]) nC = nCx # of cells # self.nC=nC # self.nCx=nCx # tessellation = Tessellation(nCx=nCx,XMINS=self.XMINS,XMAXS=self.XMAXS) self.tessellation=tessellation # cells_multiidx,cells_verts=create_cells(nCx,self.XMINS,self.XMAXS) try: # raise FileDoesNotExistError('fake error') subspace=Pkl.load(self.filename_subspace,verbose=1) B=subspace['B'] nConstraints=subspace['nConstraints'] nEdges=subspace['nEdges'] constraintMat=subspace['constraintMat'] # cells_verts =np.asarray(cells_verts) except FileDoesNotExistError: print "Failed loading, so compute from scrartch" verts1,H,nEdges,nConstraints = self.tessellation.create_verts_and_H() # verts1,H,nEdges,nConstraints = create_verts_and_H(nC,cells_multiidx,cells_verts, # dim_domain=self.dim_domain) # raise ValueError(tessellation.cells_verts_homo_coo) # cells_verts=np.asarray(cells_verts) L = create_cont_constraint_mat(H,verts1,nEdges,nConstraints,nC,dim_domain=self.dim_domain) if zero_v_across_bdry[0]: Lbdry = self.tessellation.create_constraint_mat_bdry( # XMINS,XMAXS, # cells_verts, nC, # dim_domain=self.dim_domain, zero_v_across_bdry=self.zero_v_across_bdry) L = np.vstack([L,Lbdry]) nConstraints += Lbdry.shape[0] if self.warp_around[0]: Lwa = create_constraint_mat_warp_around(cells_verts, nC,dim_domain=self.dim_domain) L = np.vstack([L,Lwa]) nConstraints += Lwa.shape[0] if vol_preserve: Lvol = create_constraint_mat_preserve_vol(nC,dim_domain=self.dim_domain) L = np.vstack([L,Lvol]) nConstraints += Lvol.shape[0] try: B=null(L) except: print '-'*30 print self.filename_subspace print '-'*30 raise constraintMat=sparse.csr_matrix(L) Pkl.dump(self.filename_subspace,{'B':B, 'nConstraints':nConstraints, 'nEdges':nEdges, 'constraintMat':constraintMat}, override=True) super(CpaSpace,self).__finish_init__( tessellation=tessellation, constraintMat=constraintMat, nConstraints=nConstraints, nInterfaces=nEdges,B=B,zero_vals=[]) if self.local_stuff is None: raise ValueError("WTF?")
def __init__(self,N,XMINS,XMAXS,nCs, zero_v_across_bdry=None, vol_preserve=False,warp_around=None, zero_vals=[], cpa_calcs=None, tess='II', valid_outside=None, only_local=False): if type(N)!=int: raise TypeError(type(N)) if tess !='II': raise ValueError(tess) self.dim_domain=N self.dim_range=N self.nHomoCoo = self.dim_domain+1 self.lengthAvee = self.dim_domain * self.nHomoCoo self.Ashape = self.dim_domain,self.nHomoCoo cont_constraints_are_separable=False debug_cont_constraints_are_separable=False if zero_v_across_bdry is None: zero_v_across_bdry = [False]*N super(CpaSpace,self).__init__(XMINS,XMAXS,nCs, zero_v_across_bdry, vol_preserve=vol_preserve, warp_around=warp_around, zero_vals=zero_vals, cpa_calcs=cpa_calcs, tess=tess, valid_outside=valid_outside, only_local=only_local) tessellation = Tessellation(nCs,self.nC,self.XMINS,self.XMAXS,tess=tess, dim_domain=self.dim_domain, dim_range=self.dim_range) self.tessellation=tessellation try: # raise FileDoesNotExistError("FAKE NAME") subspace=Pkl.load(self.filename_subspace,verbose=1) B=subspace['B'] nConstraints=subspace['nConstraints'] nSides=subspace['nSides'] constraintMat=subspace['constraintMat'] except FileDoesNotExistError: nC = self.nC if tess == 'II': verts,H,nSides,nConstraints=self.tessellation.create_verts_and_H( dim_range=self.dim_range,valid_outside=valid_outside) # verts,H,nSides,nConstraints = create_verts_and_H(N,self.nC, # cells_multiidx, cells_verts, # dim_domain=self.dim_domain, # dim_range=self.dim_range) else: raise ValueError(tess) L = create_cont_constraint_mat(N,H,verts,nSides,nConstraints, nC,dim_domain=self.dim_domain, dim_range=self.dim_range,tess=tess) if len(zero_vals): Lzerovals = create_constraint_mat_zerovals(nC,dim_domain=self.dim_domain, dim_range=self.dim_range, zero_vals=zero_vals) L = np.vstack([L,Lzerovals]) nConstraints += Lzerovals.shape[0] if any(zero_v_across_bdry): # raise NotImplementedError # Lbdry = create_constraint_mat_bdry(XMINS,XMAXS, cells_verts, nC, # dim_domain=self.dim_domain, # zero_v_across_bdry=self.zero_v_across_bdry) # L = np.vstack([L,Lbdry]) # nConstraints += Lbdry.shape[0] # #raise ValueError("I am not sure this is still supported") Lbdry = create_constraint_mat_bdry(XMINS,XMAXS, cells_verts, nC, dim_domain=self.dim_domain, zero_v_across_bdry=self.zero_v_across_bdry) L = np.vstack([L,Lbdry]) nConstraints += Lbdry.shape[0] if any(self.warp_around): raise NotImplementedError Lwa = create_constraint_mat_warp_around(cells_verts, nC,dim_domain=self.dim_domain) L = np.vstack([L,Lwa]) nConstraints += Lwa.shape[0] if vol_preserve: Lvol = create_constraint_mat_preserve_vol(nC,dim_domain=self.dim_domain) L = np.vstack([L,Lvol]) nConstraints += Lvol.shape[0] # ipshell('hi') # 1/0 try: B=null(L,verbose=1) # if any(self.zero_v_across_bdry) or vol_preserve: # B=null(L) # else: # def pick(i): # return L[i::N].reshape(L.shape[0]/N,N+1,N,nC)[:,:,i,:].reshape(L.shape[0]/N,-1) # Bs = [null(pick(i)) for i in range(N)] # ipshell('hi') # 2/0 except: print '----------------------' print self.filename_subspace print '---------------------' raise constraintMat=sparse.csr_matrix(L) Pkl.dump(self.filename_subspace,{'B':B, 'nConstraints':nConstraints, 'nSides':nSides, 'constraintMat':constraintMat}, override=True) super(CpaSpace,self).__finish_init__(tessellation=tessellation, constraintMat=constraintMat, nConstraints=nConstraints, nInterfaces=nSides, B=B,zero_vals=zero_vals) self.cont_constraints_are_separable=cont_constraints_are_separable self.x_dense = self._calcs.x_dense self.x_dense_grid = self._calcs.x_dense_grid self.x_dense_img = self._calcs.x_dense_img self.x_dense_grid_img = self._calcs.x_dense_grid_img if self.x_dense is not None: self.grid_shape = self.x_dense_grid[0].shape else: 1/0
def __init__(self, N, XMINS, XMAXS, nCs, zero_v_across_bdry=None, vol_preserve=False, warp_around=None, zero_vals=[], cpa_calcs=None, tess='II', valid_outside=None, only_local=False): if type(N) != int: raise TypeError(type(N)) if tess != 'II': raise ValueError(tess) self.dim_domain = N self.dim_range = N self.nHomoCoo = self.dim_domain + 1 self.lengthAvee = self.dim_domain * self.nHomoCoo self.Ashape = self.dim_domain, self.nHomoCoo cont_constraints_are_separable = False debug_cont_constraints_are_separable = False if zero_v_across_bdry is None: zero_v_across_bdry = [False] * N super(CpaSpace, self).__init__(XMINS, XMAXS, nCs, zero_v_across_bdry, vol_preserve=vol_preserve, warp_around=warp_around, zero_vals=zero_vals, cpa_calcs=cpa_calcs, tess=tess, valid_outside=valid_outside, only_local=only_local) tessellation = Tessellation(nCs, self.nC, self.XMINS, self.XMAXS, tess=tess, dim_domain=self.dim_domain, dim_range=self.dim_range) self.tessellation = tessellation try: # raise FileDoesNotExistError("FAKE NAME") subspace = Pkl.load(self.filename_subspace, verbose=1) B = subspace['B'] nConstraints = subspace['nConstraints'] nSides = subspace['nSides'] constraintMat = subspace['constraintMat'] except FileDoesNotExistError: nC = self.nC if tess == 'II': verts, H, nSides, nConstraints = self.tessellation.create_verts_and_H( dim_range=self.dim_range, valid_outside=valid_outside) # verts,H,nSides,nConstraints = create_verts_and_H(N,self.nC, # cells_multiidx, cells_verts, # dim_domain=self.dim_domain, # dim_range=self.dim_range) else: raise ValueError(tess) L = create_cont_constraint_mat(N, H, verts, nSides, nConstraints, nC, dim_domain=self.dim_domain, dim_range=self.dim_range, tess=tess) if len(zero_vals): Lzerovals = create_constraint_mat_zerovals( nC, dim_domain=self.dim_domain, dim_range=self.dim_range, zero_vals=zero_vals) L = np.vstack([L, Lzerovals]) nConstraints += Lzerovals.shape[0] if any(zero_v_across_bdry): # raise NotImplementedError # Lbdry = create_constraint_mat_bdry(XMINS,XMAXS, cells_verts, nC, # dim_domain=self.dim_domain, # zero_v_across_bdry=self.zero_v_across_bdry) # L = np.vstack([L,Lbdry]) # nConstraints += Lbdry.shape[0] # #raise ValueError("I am not sure this is still supported") Lbdry = create_constraint_mat_bdry( XMINS, XMAXS, cells_verts, nC, dim_domain=self.dim_domain, zero_v_across_bdry=self.zero_v_across_bdry) L = np.vstack([L, Lbdry]) nConstraints += Lbdry.shape[0] if any(self.warp_around): raise NotImplementedError Lwa = create_constraint_mat_warp_around( cells_verts, nC, dim_domain=self.dim_domain) L = np.vstack([L, Lwa]) nConstraints += Lwa.shape[0] if vol_preserve: Lvol = create_constraint_mat_preserve_vol( nC, dim_domain=self.dim_domain) L = np.vstack([L, Lvol]) nConstraints += Lvol.shape[0] # ipshell('hi') # 1/0 try: B = null(L, verbose=1) # if any(self.zero_v_across_bdry) or vol_preserve: # B=null(L) # else: # def pick(i): # return L[i::N].reshape(L.shape[0]/N,N+1,N,nC)[:,:,i,:].reshape(L.shape[0]/N,-1) # Bs = [null(pick(i)) for i in range(N)] # ipshell('hi') # 2/0 except: print '----------------------' print self.filename_subspace print '---------------------' raise constraintMat = sparse.csr_matrix(L) Pkl.dump(self.filename_subspace, { 'B': B, 'nConstraints': nConstraints, 'nSides': nSides, 'constraintMat': constraintMat }, override=True) super(CpaSpace, self).__finish_init__(tessellation=tessellation, constraintMat=constraintMat, nConstraints=nConstraints, nInterfaces=nSides, B=B, zero_vals=zero_vals) self.cont_constraints_are_separable = cont_constraints_are_separable self.x_dense = self._calcs.x_dense self.x_dense_grid = self._calcs.x_dense_grid self.x_dense_img = self._calcs.x_dense_img self.x_dense_grid_img = self._calcs.x_dense_grid_img if self.x_dense is not None: self.grid_shape = self.x_dense_grid[0].shape else: 1 / 0
def __init__(self,XMINS,XMAXS,nCs, zero_v_across_bdry=[False,False,False], vol_preserve=False,warp_around=None, zero_vals=[],cpa_calcs=None, tess='II', valid_outside=None, only_local=False, cont_constraints_are_separable=None): if cont_constraints_are_separable is None: raise ObsoleteError(""" Expected True/False value for cont_constraints_are_separable; got None instead""") if tess == 'II' and valid_outside is not None: print "tess='II' --> ignoring the value of valid_outside" if tess == 'I': if valid_outside is None: raise ValueError("tess='I' so you must pass valid_outside=True/False" ) self.valid_outside=valid_outside nCx,nCy,nCz=map(int,nCs) debug_cont_constraints_are_separable=False if cont_constraints_are_separable: print 'Check if can actually use separable continuity:' if any(zero_v_across_bdry): cont_constraints_are_separable=False print 'any(zero_v_across_bdry) is True' if vol_preserve: cont_constraints_are_separable=False print 'vol_preserve is True' if nCx!=nCy or nCx!=nCz: cont_constraints_are_separable=False print 'nCx!=nCy or nCx!=nCz' if XMINS[0]!=XMINS[1] or XMINS[0]!=XMINS[2]: cont_constraints_are_separable=False print 'XMINS[0]!=XMINS[1] or XMINS[0]!=XMINS[2]' if XMAXS[0]!=XMAXS[1] or XMAXS[0]!=XMAXS[2]: cont_constraints_are_separable=False print 'XMAXS[0]!=XMAXS[1] or XMAXS[0]!=XMAXS[2]' if not cont_constraints_are_separable: debug_cont_constraints_are_separable=False print 'so I could not use separable continuity.' else: print '\nWill use separable continuity.\n' super(CpaSpace,self).__init__(XMINS,XMAXS,nCs, zero_v_across_bdry, vol_preserve=vol_preserve, warp_around=warp_around, zero_vals=zero_vals,cpa_calcs=cpa_calcs, tess=tess, valid_outside=valid_outside, only_local=only_local, cont_constraints_are_separable=cont_constraints_are_separable) tessellation = Tessellation(nCx,nCy,nCz,self.nC,self.XMINS,self.XMAXS,tess=tess) self.tessellation=tessellation try: # FilesDirs.raise_if_file_does_not_exist('Fake Name') subspace=Pkl.load(self.filename_subspace,verbose=1) B=subspace['B'] nConstraints=subspace['nConstraints'] nSides=subspace['nSides'] constraintMat=subspace['constraintMat'] cont_constraints_are_separable=subspace['cont_constraints_are_separable'] except FileDoesNotExistError: nC = self.nC v1,v2,v3,v4,H,nSides,nConstraints = self.tessellation.create_verts_and_H( dim_range=self.dim_range,valid_outside=valid_outside) if cont_constraints_are_separable == False or debug_cont_constraints_are_separable: L = create_cont_constraint_mat(H,v1,v2,v3,v4,nSides,nConstraints, nC,dim_domain=self.dim_domain, dim_range=self.dim_range,tess=tess) if cont_constraints_are_separable: Lx = create_cont_constraint_mat_separable(H,v1,v2,v3,v4,nSides,nConstraints, nC,dim_domain=self.dim_domain, dim_range=self.dim_range,tess=tess) if len(zero_vals): Lzerovals = create_constraint_mat_zerovals(nC,dim_domain=self.dim_domain, dim_range=self.dim_range, zero_vals=zero_vals) L = np.vstack([L,Lzerovals]) nConstraints += Lzerovals.shape[0] if any(zero_v_across_bdry): if cont_constraints_are_separable == False or debug_cont_constraints_are_separable: Lbdry = self.tessellation.create_constraint_mat_bdry( zero_v_across_bdry=self.zero_v_across_bdry) L = np.vstack([L,Lbdry]) if cont_constraints_are_separable: Lb = self.tessellation.create_constraint_mat_bdry_separable( zero_v_across_bdry=self.zero_v_across_bdry) raise NotImplementedError(zero_v_across_bdry, cont_constraints_are_separable) nConstraints += Lbdry.shape[0] if any(self.warp_around): raise NotImplementedError Lwa = create_constraint_mat_warp_around(cells_verts, nC,dim_domain=self.dim_domain) L = np.vstack([L,Lwa]) nConstraints += Lwa.shape[0] if vol_preserve: Lvol = create_constraint_mat_preserve_vol(nC,dim_domain=self.dim_domain) L = np.vstack([L,Lvol]) nConstraints += Lvol.shape[0] if not cont_constraints_are_separable: B=null(L) else: # to solve a nuch smaller SVD and to get a sparser basis if vol_preserve or any(zero_v_across_bdry): raise NotImplementedError B1=null(Lx) # B1.shape is (nC*nHomoCoo)x dim_null_space if debug_cont_constraints_are_separable: B=null(L) if B1.shape[0]!=B.shape[0]/3: raise ValueError(B1.shape,B.shape) if float(B1.shape[1])*self.dim_range != B.shape[1]: raise ValueError(B1.shape,B.shape) _B = np.zeros((B1.shape[0]*3,B1.shape[1]*self.dim_range),B1.dtype) for j in range(B1.shape[1]): Avees = B1[:,j] # length=self.nC*self.nHomoCoo arr=Avees.reshape(self.nC,self.nHomoCoo) for k in range(self.dim_range): arr2=np.hstack([arr if m==k else np.zeros_like(arr) for m in range(self.dim_range)]) arr3=arr2.reshape(self.nC,self.lengthAvee) arr4=arr3.flatten() _B[:,j+k*B1.shape[1]]=arr4 B=_B # # 2/0 if cont_constraints_are_separable: L=Lx constraintMat=sparse.csr_matrix(L) Pkl.dump(self.filename_subspace,{'B':B,'cont_constraints_are_separable':cont_constraints_are_separable, 'nConstraints':nConstraints, 'nSides':nSides, 'constraintMat':constraintMat}, override=True) # Since B encodes the null space of, it follows that # np.allclose(L.dot(B),0)==True super(CpaSpace,self).__finish_init__(tessellation=tessellation, constraintMat=constraintMat, nConstraints=nConstraints, nInterfaces=nSides, B=B,zero_vals=zero_vals) self.cont_constraints_are_separable=cont_constraints_are_separable self.x_dense = self._calcs.x_dense self.x_dense_grid = self._calcs.x_dense_grid self.x_dense_img = self._calcs.x_dense_img self.x_dense_grid_img = self._calcs.x_dense_grid_img if self.x_dense is not None: self.grid_shape = self.x_dense_grid[0].shape else: 1/0 verts=self.tessellation.cells_verts_homo_coo for i in range(0,self.nC): for j in range(0,i): verts1=verts[i] verts2=verts[j] shared=[] for v1 in verts1: for v2 in verts2: if (v1==v2).all(): shared.append(v1) shared = np.asarray(shared).T if len(shared)==0: continue # theta =self.get_zeros_theta() for m in range(self.d): # theta[j]=1 Avees=self.get_zeros_PA() Avees[:]=self.B[:,m] # self.theta2Avees(Avees=Avees,theta=theta) As=self.Avees2As(Avees=Avees) Ai=As[i] Aj=As[j] #Ai.dot(shared) is 3 x 3 = dim x #verts_per_side # At the moment, the problem is w/ the last entry of the 4 vert (100,100,0,1) if not np.allclose((Ai-Aj).dot(shared),0): ipshell('FAILED ALL CLOSE TEST') raise ValueError
def __init__(self, XMINS, XMAXS, nCs, zero_v_across_bdry, vol_preserve, warp_around=None, cpa_calcs=None): # XMINS = [-x for x in XMAXS] # DEBUG if vol_preserve: raise ValueError("Doesn't make sense to use it for the 1D case") self.warp_around = warp_around super(CpaSpace, self).__init__(XMINS, XMAXS, nCs, zero_v_across_bdry, vol_preserve=vol_preserve, warp_around=warp_around, zero_vals=[], cpa_calcs=cpa_calcs) nCx = int(nCs[0]) nC = nCx # of cells # self.nC=nC # self.nCx=nCx # tessellation = Tessellation(nCx=nCx, XMINS=self.XMINS, XMAXS=self.XMAXS) self.tessellation = tessellation # cells_multiidx,cells_verts=create_cells(nCx,self.XMINS,self.XMAXS) try: # raise FileDoesNotExistError('fake error') subspace = Pkl.load(self.filename_subspace, verbose=1) B = subspace['B'] nConstraints = subspace['nConstraints'] nEdges = subspace['nEdges'] constraintMat = subspace['constraintMat'] # cells_verts =np.asarray(cells_verts) except FileDoesNotExistError: print "Failed loading, so compute from scrartch" verts1, H, nEdges, nConstraints = self.tessellation.create_verts_and_H( ) # verts1,H,nEdges,nConstraints = create_verts_and_H(nC,cells_multiidx,cells_verts, # dim_domain=self.dim_domain) # raise ValueError(tessellation.cells_verts_homo_coo) # cells_verts=np.asarray(cells_verts) L = create_cont_constraint_mat(H, verts1, nEdges, nConstraints, nC, dim_domain=self.dim_domain) if zero_v_across_bdry[0]: Lbdry = self.tessellation.create_constraint_mat_bdry( # XMINS,XMAXS, # cells_verts, nC, # dim_domain=self.dim_domain, zero_v_across_bdry=self.zero_v_across_bdry) L = np.vstack([L, Lbdry]) nConstraints += Lbdry.shape[0] if self.warp_around[0]: Lwa = create_constraint_mat_warp_around( cells_verts, nC, dim_domain=self.dim_domain) L = np.vstack([L, Lwa]) nConstraints += Lwa.shape[0] if vol_preserve: Lvol = create_constraint_mat_preserve_vol( nC, dim_domain=self.dim_domain) L = np.vstack([L, Lvol]) nConstraints += Lvol.shape[0] try: B = null(L) except: print '-' * 30 print self.filename_subspace print '-' * 30 raise constraintMat = sparse.csr_matrix(L) Pkl.dump(self.filename_subspace, { 'B': B, 'nConstraints': nConstraints, 'nEdges': nEdges, 'constraintMat': constraintMat }, override=True) super(CpaSpace, self).__finish_init__(tessellation=tessellation, constraintMat=constraintMat, nConstraints=nConstraints, nInterfaces=nEdges, B=B, zero_vals=[]) if self.local_stuff is None: raise ValueError("WTF?")