def __init__(self, operator): from scipy.sparse import csc_matrix if isinstance(operator, SparseDiscreteBoundaryOperator): mat = operator.sparse_operator elif isinstance(operator, csc_matrix): mat = operator else: raise ValueError("op must be either of type " + "SparseDiscreteBoundaryOperator or of type csc_matrix. Actual type: " + str(type(operator))) from scipy.sparse.linalg import splu self._solve_fun = None self._shape = (mat.shape[1], mat.shape[0]) self._dtype = mat.dtype if mat.shape[0] == mat.shape[1]: # Square matrix case solver = splu(mat) self._solve_fun = solver.solve elif mat.shape[0] > mat.shape[1]: # Thin matrix case mat_hermitian = mat.conjugate().transpose() solver = splu((mat_hermitian*mat).tocsc()) self._solve_fun = lambda x: solver.solve(mat_hermitian*x) else: # Thick matrix case mat_hermitian = mat.conjugate().transpose() solver = splu((mat*mat_hermitian).tocsc()) self._solve_fun = lambda x: mat_hermitian*solver.solve(x)
def comp_evcs_evls(compall=False, nevs=None, linsysstr=None, bigamat=None, bigmmatshift=None, bigmmat=None, retinstevcs=False): whichevecs = '_leftevs' if leftevecs else '_rightevs' bcstr = '_palpha{0}'.format(palpha) if bccontrol else '' levstr = linsysstr + bcstr + whichevecs try: if debug: raise IOError() evls = dou.load_npa(levstr) print 'loaded the eigenvalues of the linearized system: \n' + levstr if retinstevcs: evcs = dou.load_npa(levstr+'_instableevcs') return evls, evcs except IOError: print 'computing the eigenvalues of the linearized system: \n' + levstr if compall: A = bigamat.todense() M = bigmmat.todense() if not shiftall else bigmmatshift.todense() evls = spla.eigvals(A, M, overwrite_a=True, check_finite=False) else: if leftevecs: bigamat = bigamat.T # TODO: we assume that M is symmetric msplu = spsla.splu(bigmmatshift) def minvavec(vvec): return msplu.solve(bigamat*vvec) miaop = spsla.LinearOperator(bigamat.shape, matvec=minvavec, dtype=bigamat.dtype) shiftamm = bigamat - sigma*bigmmatshift sammsplu = spsla.splu(shiftamm) def samminvmvec(vvec): return sammsplu.solve(bigmmatshift*vvec) samminvop = spsla.LinearOperator(bigamat.shape, matvec=samminvmvec, dtype=bigamat.dtype) evls, evcs = spsla.eigs(miaop, k=nevs, sigma=sigma, OPinv=samminvop, maxiter=100, # sigma=None, which='SM', return_eigenvectors=retinstevcs) dou.save_npa(evls, levstr) if retinstevcs: instevlsindx = np.real(evls) > 0 instevcs = evcs[:, instevlsindx] dou.save_npa(instevcs, levstr+'_instableevcs') else: instevcs = None return evls, instevcs
def __init__(self, chamb, Dh, sparse_solver='scipy_slu', ext_boundary=False): # mimics the super() call in the old version params = compute_new_mesh_properties( chamb.x_aper, chamb.y_aper, Dh, ext_boundary=ext_boundary) self.Dh, self.xg, self.Nxg, self.bias_x, self.yg, self.Nyg, self.bias_y = params self.chamb = chamb [xn, yn]=np.meshgrid(self.xg,self.yg) xn=xn.T xn=xn.flatten() self.xn = xn yn=yn.T yn=yn.flatten() self.yn = yn #% xn and yn are stored such that the external index is on x self.flag_outside_n=chamb.is_outside(xn,yn) self.flag_inside_n=~(self.flag_outside_n) flag_outside_n_mat=np.reshape(self.flag_outside_n,(self.Nyg,self.Nxg),'F') flag_outside_n_mat=flag_outside_n_mat.T [gx,gy]=np.gradient(np.double(flag_outside_n_mat)) gradmod=abs(gx)+abs(gy); self.flag_border_mat=np.logical_and((gradmod>0), flag_outside_n_mat); self.flag_border_n = self.flag_border_mat.flatten() A = self.assemble_laplacian() diagonal = A.diagonal() N_full = len(diagonal) indices_non_id = np.where(diagonal!=1.)[0] N_sel = len(indices_non_id) Msel = sps.lil_matrix((N_full, N_sel)) for ii, ind in enumerate(indices_non_id): Msel[ind, ii] =1. Msel = Msel.tocsc() Asel = Msel.T*A*Msel Asel=Asel.tocsc() if sparse_solver == 'scipy_slu': print "Using scipy superlu solver..." self.luobj = spl.splu(Asel.tocsc()) elif sparse_solver == 'PyKLU': print "Using klu solver..." try: import PyKLU.klu as klu self.luobj = klu.Klu(Asel.tocsc()) except StandardError, e: print "Got exception: ", e print "Falling back on scipy superlu solver:" self.luobj = spl.splu(Asel.tocsc())
def build_sparse_solver(self): if self.sparse_solver == 'scipy_slu': print "Using scipy superlu solver..." luobj = ssl.splu(self.Asel.tocsc()) elif self.sparse_solver == 'PyKLU': print "Using klu solver..." try: import PyKLU.klu as klu luobj = klu.Klu(self.Asel.tocsc()) except StandardError, e: print "Got exception: ", e print "Falling back on scipy superlu solver:" luobj = ssl.splu(self.Asel.tocsc())
def iJ_weak_form(self): self._collect() if self._iJ_assembled: return self.iJlu tinit = time() print('==Factorization LU: J') if self._J_is == 'Sp': self.iJlu = spla.splu(self.Jw) self.tassembiJ = time() - tinit self._iJ_assembled = True print('##time sparse J=LU: {}'.format(self.tassembiJ)) return self.iJlu Jw = self.opI.weak_form() N = len(self.domains) dtype = self.dtype iJlu = bem.BlockedDiscreteOperator(N, N) for ii in range(N): Jb = Jw[ii, ii] Jd, Jn = Jb[0, 0], Jb[1, 1] js = Jd.sparse_operator js = js.astype(dtype) js = spla.splu(js) iJd = Bridge(js.shape, dot=js.solve, dtype=dtype) js = Jn.sparse_operator js = js.astype(dtype) js = spla.splu(js) iJn = Bridge(js.shape, dot=js.solve, dtype=dtype) iJloc = bem.BlockedDiscreteOperator(2, 2) iJloc[0, 0], iJloc[1, 1] = iJd, iJn iJlu[ii, ii] = iJloc self.iJlu = iJlu self.tassembiJ = time() - tinit self._iJ_assembled = True print('##time blocked J=LU: {}'.format(self.tassembiJ)) return self.iJlu
def AugYbus(baseMVA, bus, branch, xd_tr, gbus, P, Q, U0): """ Constructs augmented bus admittance matrix Ybus. @param baseMVA: power base @param bus: bus data @param branch: branch data @param xd_tr: d component of transient reactance @param gbus: generator buses @param P: load active power @param Q: load reactive power @param U0: steady-state bus voltages @return: factorised augmented bus admittance matrix @see: U{http://www.esat.kuleuven.be/electa/teaching/matdyn/} """ # Calculate bus admittance matrix Ybus, _, _ = makeYbus(baseMVA, bus, branch) # Calculate equivalent load admittance yload = (P - 1j * Q) / (abs(U0)**2) # Calculate equivalent generator admittance ygen = zeros(Ybus.shape[0]) ygen[gbus] = 1 / (1j * xd_tr) # Add equivalent load and generator admittance to Ybus matrix for i in range(Ybus.shape[0]): Ybus[i, i] = Ybus[i, i] + ygen[i] + yload[i] # Factorise return splu(Ybus)
def solve(self,ridgePen = False): xtx = self.X.T.dot(self.X) if ridgePen: penVal = self.getRidgePen() ind = range(1,xtx.shape[0]) penMat = sps.csr_matrix(([penVal]*len(ind),(ind,ind)), shape=xtx.shape) # self.beta = sla.spsolve(xtx + penMat,self.X.T.dot(self.Y)) # lu = sla.splu(xtx + penMat) # self.beta = lu.solve(self.X.T.dot(self.Y).todense()) A = xtx + penMat b = self.X.T.dot(self.Y).todense() else: # self.beta = sla.spsolve(xtx,self.X.T.dot(self.Y)) # lu = sla.splu(xtx) # self.beta = lu.solve(self.X.T.dot(self.Y).todense()) A = xtx b = self.X.T.dot(self.Y).todense() if float(A.nnz)/float(A.shape[0]*A.shape[1]) < 0.1: lu = sla.splu(A) self.beta = lu.solve(b) else: lu = sp.linalg.lu_factor(A.todense()) self.beta = sp.linalg.lu_solve(lu,b)
def getCvError(self,penVal): p = self.X.shape[1] ind = range(1,p) penMat = sps.csr_matrix(([penVal]*len(ind),(ind,ind)),shape=(p,p)) cvError = 0.0 for n in range(self.nGroups): if self.cvXtXtest[n] is None: self.cvXtXtest[n] = self.cvXtest[n].T.dot(self.cvXtest[n]) if self.cvXtYtest[n] is None: self.cvXtYtest[n] = self.cvXtest[n].T.dot(self.cvYtest[n]) if self.cvXtXtrain[n] is None: self.cvXtXtrain[n] = self.cvXtrain[n].T.dot(self.cvXtrain[n]) if self.cvXtYtrain[n] is None: self.cvXtYtrain[n] = self.cvXtrain[n].T.dot(self.cvYtrain[n]) A = self.cvXtXtrain[n] + penMat b = self.cvXtYtrain[n].todense() # print float(A.nnz)/float(A.shape[0]*A.shape[1]) if float(A.nnz)/float(A.shape[0]*A.shape[1]) < 0.1: # beta = sla.spsolve(A,b) lu = sla.splu(A) beta = lu.solve(b) else: lu = sp.linalg.lu_factor(A.todense()) beta = sp.linalg.lu_solve(lu,b) resid = (self.cvXtXtest[n] + penMat).dot(beta) - self.cvXtYtest[n] b = np.linalg.norm(resid,ord=2) cvError += b return cvError
def perform(self, node, inputs, outputs): (A,) = inputs (z,) = outputs As = spar.csc_matrix(A) Ud = spla.splu(As).U.diagonal() ld = np.sum(np.log(np.abs(Ud))) z[0] = ld
def update( ti, x): theta,thetadot,q2,qdot2 = x G1 = 1.0*g*(l1*m1*sin(theta) + m2*(q2*cos(theta) + (-l1 - l2 + q2)*sin(theta))) G2 = -1.0*sqrt(2)*g*m2*cos(theta + pi/4) C1 = 0 C2 = 1.0*m2*thetadot*(l1*thetadot + l2*thetadot - q2*thetadot - qdot2) M11 = 1.0*I1 + 1.0*I2 + 1.0*l1**2*m1 + 1.0*l1**2*m2 + 2.0*l1*l2*m2 - 2.0*l1*m2*q2 + 1.0*l2**2*m2 - 2.0*l2*m2*q2 + 1.0*m2*q2**2 M12 = 0 M21 = 0 M22 = 1.0*m2 G = np.array([ [G1], [G2], ]) C = np.array([ [C1], [C2], ]) M = csc_matrix([ [M11,M12], [M21,M22], ]) lu = sla.splu(M) x2,y2 = lu.solve() # K = 0.5*qdot2*(qdot2*(1.0*m2*sin(theta)**2 + 1.0*m2*cos(theta)**2 + 3.74939945665464e-33*m2) + thetadot*(-1.0*m2*(l1 + l2 - q2)*sin(theta)**2 - 1.0*m2*(l1 + l2 - q2)*cos(theta)**2)) + 0.5*thetadot*(qdot2*(-1.0*m2*(l1 + l2 - q2)*sin(theta)**2 - 1.0*m2*(l1 + l2 - q2)*cos(theta)**2) + thetadot*(I1 + I2 + 1.0*l1**2*m1*sin(theta)**2 + 1.0*l1**2*m1*cos(theta)**2 + 1.0*m2*(l1 + l2 - q2)**2*sin(theta)**2 + 1.0*m2*(l1 + l2 - q2)**2*cos(theta)**2)) # P = 1.0*g*l1*m1*cos(theta) - g*m2*(1.0*q2*sin(theta) + 1.0*(l1 + l2 - q2)*cos(theta)) # E.append(K-P) x2 = -1.0*m2*(-1.0*g*l2 + 2.0*g*q2 + 1.0*qdot2**2)/I2 y2 = (1.0*I2*g + 1.0*g*l2**2*m2 - 3.0*g*l2*m2*q2 + 2.0*g*m2*q2**2 - 1.0*l2*m2*qdot2**2 + 1.0*m2*q2*qdot2**2)/I2 x1,y1 = thetadot,qdot2 return [x1,x2,y1,y2]
def solve_nonlinear(self, inputs, outputs): force_vector = np.concatenate([self.options['force_vector'], np.zeros(2)]) self.K = self.assemble_CSC_K(inputs) self.lu = splu(self.K) outputs['d'] = self.lu.solve(force_vector)
def _solve_(self, L, x, b): diag = L.takeDiagonal() maxdiag = max(numerix.absolute(diag)) L = L * (1 / maxdiag) b = b * (1 / maxdiag) LU = splu(L.matrix.asformat("csc"), diag_pivot_thresh=1., relax=1, panel_size=10, permc_spec=3) error0 = numerix.sqrt(numerix.sum((L * x - b)**2)) for iteration in range(min(self.iterations, 10)): errorVector = L * x - b if (numerix.sqrt(numerix.sum(errorVector**2)) / error0) <= self.tolerance: break xError = LU.solve(errorVector) x[:] = x - xError if 'FIPY_VERBOSE_SOLVER' in os.environ: from fipy.tools.debug import PRINT PRINT('iterations: %d / %d' % (iteration+1, self.iterations)) PRINT('residual:', numerix.sqrt(numerix.sum(errorVector**2))) return x
def test_solver(self): if SOLVER == 'default': mat = spmatrix.LL((5,5), format = 'd', isSym=True) elif SOLVER == 'sparse': mat = sparse.lil_matrix((5, 5), dtype = 'd') for col in range(5): for row in range(4, col, -1): mat[row,col] = 2.5 for col in range(5): for row in range(0, col, 1): mat[row,col] = 2.5 for row in range(5): mat[row,row] = 5. rhs = np.array((1.,1.,1.,1.,1.), dtype=float) if SOLVER == 'default': spooles = solver.Spooles(mat) spooles.solve(rhs) elif SOLVER == 'sparse': csc_mat = sparse.csc_matrix(mat) LU = linalg.splu(csc_mat, permc_spec='MMD_AT_PLUS_A', diag_pivot_thresh=0) rhs = LU.solve(rhs) self.almostEqual(rhs, [0.06666667, 0.06666667, 0.06666667, 0.06666667, 0.06666667])
def keo_lu(self, psi): """Solves a system with the kinetic energy operator only via ordinary CG. """ if self._modeleval._keo is None: self._modeleval._assemble_kinetic_energy_operator() # From http://crd.lbl.gov/~xiaoye/SuperLU/faq.html#sym-problem: # SuperLU cannot take advantage of symmetry, but it can still solve the # linear system as long as you input both the lower and upper parts of # the matrix A. If off-diagonal pivoting does not occur, the U matrix # in A = L*U is equivalent to D*L'. # In many applications, matrix A may be diagonally dominant or nearly # so. In this case, pivoting on the diagonal is sufficient for # stability and is preferable for sparsity to off-diagonal pivoting. To # do this, the user can set a small (less-than-one) diagonal pivot # threshold (e.g., 0.0, 0.01, ...) and choose an (A' + A)-based column # permutation algorithm. We call this setting Symmetric Mode. To use # this (in serial SuperLU), you need to set: # # options.SymmetricMode = YES; # options.ColPerm = MMD_AT_PLUS_A; # options.DiagPivotThresh = 0.001; /* or 0.0, 0.01, etc. */ # if self._keo_lu is None: self._keo_lu = splu( self._modeleval._keo, options={"SymmetricMode": True}, permc_spec="MMD_AT_PLUS_A", # minimum deg diag_pivot_thresh=0.0, ) return self._keo_lu.solve(psi)
def get_iJlu(self): Id = self.Id Idw = Id.weak_form() mId = Idw.sparse_operator mId = mId.astype(complex) iId = spla.splu(mId) iJlu = spla.LinearOperator(iId.shape, matvec=iId.solve, dtype=complex) return iJlu
def perform(self, node, inputs, outputs): (rho,) = inputs (z, ) = outputs rW = rho * self.W A = self.I - rW Ud = spla.splu(A).U.diagonal() ld = np.asarray(np.sum(np.log(np.abs(Ud)))) z[0] = ld
def keoi(self, psi): if self._modeleval._keo is None: self._modeleval._assemble_kinetic_energy_operator() if self._keoai_lu is None: self._keoai_lu = splu( self._modeleval._keo + 1.0e-2 * sparse.identity(len(psi)) ) return self._keoai_lu.solve(psi)
def __init__(self, mesh, laplacian_stencil=laplacian_3D_7stencil): self.mesh = mesh self.mesh_inner = ~mesh.boundary_nodes() lapl_data, lapl_rows, lapl_cols = self.assemble_laplacian(mesh, laplacian_stencil) #COO A = sps.coo_matrix((lapl_data, (lapl_rows, lapl_cols)), shape=(mesh.n_nodes, mesh.n_nodes), dtype=np.float64).tocsc() self.lu_obj = spl.splu(A, permc_spec="MMD_AT_PLUS_A")
def __init__(self, M): self.M = M n = M.shape[0] self.shape = (n,n) self.fac = splu(M) self.dtype = 'd' self.mvcount = 0 self.scount = 0
def sp_factiz(A): """ Helper function to behave the same way scipy.sparse.factorized does, but for to allow for matrix right hand sides. """ from scipy.sparse.linalg import factorized, splu from scipy.sparse.linalg.dsolve import _superlu A = csc_matrix(A) M, N = A.shape #Afactsolve = factorized(A) Afactsolve = splu(A, options={'IterRefine' : 'DOUBLE', 'SymmetricMode' : True}).solve def solveit(b): """ Mainly lifted from scipy.sparse.linalg.spsolve, which doesn't allow for the factorization of A to be saved, which should happen in this usage. """ b_is_vector = (max(b.shape) == prod(b.shape)) if b_is_vector: if isspmatrix(b): b = b.toarray() b = asarray(b) b = b.squeeze() else: if isspmatrix(b): b = csc_matrix(b) if b.ndim != 2: raise ValueError("b must be either a vector or a matrix") if M != b.shape[0]: raise ValueError("matrix - rhs dimension mismatch (%s - %s)" % (A.shape, b.shape[0])) if b_is_vector: b = asarray(b, dtype=A.dtype) options = dict(ColPerm='COLAMD', IterRefine='DOUBLE', SymmetricMode=True) x = _superlu.gssv(N, A.nnz, A.data, A.indices, A.indptr, b, 1, options=options)[0] else: # Cover the case where b is also a matrix tempj = empty(M, dtype=int) x = A.__class__(b.shape) mat_flag = False if b.__class__ is matrix: mat_flag = True for j in range(b.shape[1]): if mat_flag: xj = Afactsolve(squeeze(asarray(b[:, j]))) else: xj = Afactsolve(squeeze(b[:, j].toarray())) w = where(xj != 0.0)[0] tempj.fill(j) x = x + A.__class__((xj[w], (w, tempj[:len(w)])), shape=b.shape, dtype=A.dtype) return x return solveit
def solve_nonlinear(self, inputs, outputs): num_rhs = self.options['num_rhs'] self.K = self.assemble_CSC_K(inputs) self.lu = splu(self.K) for j in range(num_rhs): force_vector = np.concatenate([self.options['force_vector'][:, j], np.zeros(2)]) outputs['d_%d' % j] = self.lu.solve(force_vector)
def lulogdet(matrix): """ compute the log determinant using a lu decomposition appropriate to input type """ if spar.issparse(matrix): LUfunction = lambda x: spla.splu(x).U.diagonal() else: LUfunction = lambda x: scla.lu_factor(x)[0].diagonal() LUdiag = LUfunction(matrix) return np.sum(np.log(np.abs(LUdiag)))
def __init__(self, verts, tris, m=10.0): self._verts = verts self._tris = tris # precompute some stuff needed later on e01 = verts[tris[:,1]] - verts[tris[:,0]] e12 = verts[tris[:,2]] - verts[tris[:,1]] e20 = verts[tris[:,0]] - verts[tris[:,2]] self._triangle_area = .5 * veclen(np.cross(e01, e12)) unit_normal = normalized(np.cross(normalized(e01), normalized(e12))) self._unit_normal_cross_e01 = np.cross(unit_normal, e01) self._unit_normal_cross_e12 = np.cross(unit_normal, e12) self._unit_normal_cross_e20 = np.cross(unit_normal, e20) # parameters for heat method h = np.mean(map(veclen, [e01, e12, e20])) t = m * h ** 2 # pre-factorize poisson systems Lc, A = compute_mesh_laplacian(verts, tris) self._factored_AtLc = splu((A - t * Lc).tocsc()).solve self._factored_L = splu(Lc.tocsc()).solve
def test_preconditioner(self): # Check that preconditioning works pc = splu(Am.tocsc()) M = LinearOperator(matvec=pc.solve, shape=A.shape, dtype=A.dtype) x0, count_0 = do_solve() x1, count_1 = do_solve(M=M) assert_(count_1 == 3) assert_(count_1 < count_0/2) assert_(allclose(x1, x0, rtol=1e-14))
def linearize(self, inputs, outputs, jacobian): num_elements = self.options['num_elements'] self.K = self.assemble_CSC_K(inputs) self.lu = splu(self.K) i_elem = np.tile(np.arange(4), 4) i_d = np.tile(i_elem, num_elements) + np.repeat(np.arange(num_elements), 16) * 2 jacobian['d', 'K_local'] = outputs['d'][i_d] jacobian['d', 'd'] = self.K.toarray()
def __init__(self, system, boundary, mortarsystem, skelmesh, sysargs, syskwargs): AA, G = system.getSystem(*sysargs, **syskwargs) BS = boundary.stiffness() # print 'BS',BS.tocsr() A = (AA + BS).tocsr() A.eliminate_zeros() BL = boundary.load(False).tocsr() MM = mortarsystem.getMass() self.M = MM.tocsr().transpose() idxs = mortarsystem.idxs self.skelidxs = MM.subrows(skelmesh.partition) print self.M.shape self.Ainv = ssl.splu(A[idxs, :][:, idxs]) self.Minv = ssl.splu(self.M[self.skelidxs, :][:, self.skelidxs] * (1 + 0j)) self.Brow = -BL[idxs, :] T = mortarsystem.getOppositeTrace().tocsr().transpose() self.Scol = -T[:, idxs].conj() # Why? self.G = G.tocsr().todense()[idxs].A.flatten() self.localtoglobal = pusu.sparseindex(idxs, np.arange(len(idxs)), A.shape[0], len(idxs)) # print 'localtoglobal', self.localtoglobal print "nnz", BL.nnz, self.Brow.nnz, A.nnz, A[idxs, :][:, idxs].nnz, T.nnz, T[:, idxs].nnz
def calculate_posterior(i): # This function calculates the posterior for u[i,:] and # sigma[i,:]. Note: this function makes use of variables which are # outside of its scope. logger.debug('evaluating the filtered solution for data set %s ...' % i) # identify observation points where we do not want to estimate the # filtered solution mask = _get_mask(x,sigma[i],fill) # number of unmasked entries K = np.sum(~mask) # build differentiation matrices L,D = build_L_and_D(tuple(mask)) # form weight matrix W = _diag(1.0/sigma[i,~mask]) # compute penalty parameter lamb = _penalty(cutoff,p,sigma[i,~mask]) # form left and right hand side of the system to solve lhs = W.T.dot(W) + L.T.dot(L)/lamb**2 rhs = W.T.dot(W).dot(u[i,~mask]) # generate LU decomposition of left-hand side lu = spla.splu(lhs) # compute the smoothed derivative of the posterior mean post_mean = np.empty((N,)) post_mean[~mask] = D.dot(lu.solve(rhs)) post_mean[mask] = np.nan # compute the posterior standard deviation. if exact: cov = D.dot(spla.inv(lhs)).dot(D.T) var = np.diag(cov.toarray()) else: # compute uncertainty through repeated random perturbations of # the data and prior vector. ivar = _IterativeVariance(post_mean[~mask]) for j in range(samples): w1 = np.random.normal(0.0,1.0,K) w2 = np.random.normal(0.0,1.0,K) # generate sample of the posterior post_sample = lu.solve(rhs + W.T.dot(w1) + L.T.dot(w2)/lamb) # differentiate the sample post_sample = D.dot(post_sample) ivar.add_sample(post_sample) var = ivar.get_variance() post_sigma = np.empty((N,)) post_sigma[~mask] = np.sqrt(var) post_sigma[mask] = np.inf logger.debug('done') return post_mean,post_sigma
def setup(self, system, sysargs, syskwargs): S,G = system.getSystem(*sysargs, **syskwargs) self.localidxs = S.subrows(self.mesh.partition) M = S.tocsr() D = S.diagonal().tocsr() self.C = (D - M)[self.localidxs, :] self.Dlu = ssl.splu(D[self.localidxs, :][:, self.localidxs]) self.n = S.shape[0] self.b = np.zeros(self.n, dtype=np.complex) self.b[self.localidxs] = self.Dlu.solve(G.tocsr().todense().A[self.localidxs].ravel()) self.dtype = M.dtype
def M_L(para,mu,Mag,cdt): n = len(Mag) V = para['V'] [L,G,H] = Estimate_mu(para,mu,Mag,cdt) LU = spla.splu(-H) log_det = np.log(np.abs(LU.U.diagonal())).sum() ML = L + np.log(2.0*np.pi)*n/2.0 - log_det/2.0 ML = ML - np.exp(-(V-5e-8)/1e-8) return ML
def FiedlerVectorSparse(temporalnet, model="SECOND", normalize=True, lanczosVecs=15, maxiter=10): """Returns the Fiedler vector of the second-order (model=SECOND) or the second-order null (model=NULL) model for a temporal network. The Fiedler vector can be used for a spectral bisectioning of the network. Note that sparse linear algebra for eigenvalue problems with small eigenvalues is problematic in terms of numerical stability. Consider using the dense version of this measure. Note also that the FiedlerVector might be scaled by a factor (-1) compared to the dense version. @param temporalnet: The temporalnetwork instance to work on @param model: either C{"SECOND"} or C{"NULL"}, where C{"SECOND"} is the the default value. @param normalize: whether (default) or not to normalize the fiedler vector. Normalization is done such that the sum of squares equals one in order to get reasonable values as entries might be positive and negative. @param lanczosVecs: number of Lanczos vectors to be used in the approximate calculation of eigenvectors and eigenvalues. This maps to the ncv parameter of scipy's underlying function eigs. @param maxiter: scaling factor for the number of iterations to be used in the approximate calculation of eigenvectors and eigenvalues. The number of iterations passed to scipy's underlying eigs function will be n*maxiter where n is the number of rows/columns of the Laplacian matrix. """ if (model is "SECOND" or "NULL") == False: raise ValueError("model must be one of \"SECOND\" or \"NULL\"") # NOTE: The transposed matrix is needed to get the "left" eigen vectors L = Laplacian(temporalnet, model) # NOTE: ncv=lanczosVecs sets additional auxiliary eigenvectors that are computed # NOTE: in order to be more confident to find the one with the largest # NOTE: magnitude, see # NOTE: https://github.com/scipy/scipy/issues/4987 maxiter = maxiter*L.get_shape()[0] w = sla.eigs( L, k=2, which="SM", ncv=lanczosVecs, return_eigenvectors=False, maxiter=maxiter ) # compute a sparse LU decomposition and solve for the eigenvector # corresponding to the second largest eigenvalue n = L.get_shape()[0] b = np.ones(n) evalue = np.sort(np.abs(w))[1] A = (L[1:n,:].tocsc()[:,1:n] - sparse.identity(n-1).multiply(evalue)) b[1:n] = A[0,:].toarray() lu = sla.splu(A) b[1:n] = lu.solve(b[1:n]) if normalize: b /= np.sqrt(np.inner(b, b)) return b
def FDPF(Vbus, Sbus, Ibus, Ybus, B1, B2, pq, pv, pqpv, tol=1e-9, max_it=100): """ Fast decoupled power flow :param Vbus: :param Sbus: :param Ibus: :param Ybus: :param B1: :param B2: :param pq: :param pv: :param pqpv: :param tol: :param max_it: :return: """ start = time.time() # pvpq = np.r_[pv, pq] # set voltage vector for the iterations voltage = Vbus.copy() Va = np.angle(voltage) Vm = np.abs(voltage) # Factorize B1 and B2 J1 = splu(B1[np.ix_(pqpv, pqpv)]) J2 = splu(B2[np.ix_(pq, pq)]) # evaluate initial mismatch Scalc = voltage * np.conj(Ybus * voltage - Ibus) mis = (Scalc - Sbus) / Vm # complex power mismatch dP = mis[pqpv].real dQ = mis[pq].imag if len(pqpv) > 0: normP = norm(dP, Inf) normQ = norm(dQ, Inf) converged = normP < tol and normQ < tol # iterate iter_ = 0 while not converged and iter_ < max_it: iter_ += 1 # ----------------------------- P iteration to update Va ---------------------- # solve voltage angles dVa = J1.solve(dP) # update voltage Va[pqpv] -= dVa voltage = Vm * exp(1j * Va) # evaluate mismatch Scalc = voltage * conj(Ybus * voltage - Ibus) mis = (Scalc - Sbus) / Vm # complex power mismatch dP = mis[pqpv].real dQ = mis[pq].imag normP = norm(dP, Inf) normQ = norm(dQ, Inf) if normP < tol and normQ < tol: converged = True else: # ----------------------------- Q iteration to update Vm ---------------------- # Solve voltage modules dVm = J2.solve(dQ) # update voltage Vm[pq] -= dVm voltage = Vm * exp(1j * Va) # evaluate mismatch Scalc = voltage * conj(Ybus * voltage - Ibus) mis = (Scalc - Sbus) / Vm # complex power mismatch dP = mis[pqpv].real dQ = mis[pq].imag normP = norm(dP, Inf) normQ = norm(dQ, Inf) if normP < tol and normQ < tol: converged = True else: converged = True iter_ = 0 F = r_[dP, dQ] # concatenate again normF = norm(F, Inf) end = time.time() elapsed = end - start return voltage, converged, normF, Scalc, iter_, elapsed
id_global = i_f + m * N_correct SuperMatrixF = np.append(SuperMatrixF, F_corrected[i_f]) print( "------------------------------------------------------------------------------" ) end_time_ord = time.time() SuperMatrixA = coo_matrix((SuperMatrixA_v, (SuperMatrixA_i, SuperMatrixA_j))) SuperMatrixF = F_corrected #---------------------------Solve----------------------------------------------# if Solver_type == "gmres": print("Using GMRES") SuperMatrixU = ssl.gmres(SuperMatrixA, SuperMatrixF)[0] if Solver_type == "SuperLU": print("Using SuperLU") SuperMatrixU_LU = ssl.splu(SuperMatrixA) SuperMatrixU = SuperMatrixU_LU.solve(SuperMatrixF.todense()) #---------------------------Carry Out Angular Integration----------------------# print("Now carrying out our angular integration") U_angular = np.zeros((N_correct, 1)) for ordinate in range(M): for dof in range(N_correct): U_angular[dof] += SuperMatrixU[ordinate * N_correct + dof] U_angular = U_angular / M #----------------------------Create Dictionary of solutions--------------------# dictsol = {} for key, val in G_Red_map.items(): dictsol[key] = U_angular[val] #---------------------------Put EBCs Back--------------------------------------# U_Final = np.zeros((N, 1)) for NodeID, Nodevalue in dictEBC.items():
start_init_time = time.time() simplesphere = SimpleSphere(L_max, S_max) domain = simplesphere.domain # Model model = equations.ActiveMatterModel(simplesphere, params) state_system = model.state_system # Matrices # Combine matrices and perform LU decompositions for constant timestep A = [] for dm, m in enumerate(simplesphere.local_m): # Backward Euler for LHS Am = model.M[dm] + dt * model.L[dm] if STORE_LU: Am = spla.splu(Am.tocsc(), permc_spec=PERMC_SPEC) A.append(Am) # Initial conditions # Add random perturbations to the velocity coefficients v = model.v rand = np.random.RandomState(seed=42 + rank) for dm, m in enumerate(simplesphere.local_m): shape = v.coeffs[dm].shape noise = rand.standard_normal(shape) phase = rand.uniform(0, 2 * np.pi, shape) v.coeffs[dm] = Amp * noise * np.exp(1j * phase) state_system.pack_coeffs() # Setup outputs file_num = 1
def _steadystate_power(L, ss_args): """ Inverse power method for steady state solving. """ ss_args['info'].pop('weight', None) if settings.debug: logger.debug('Starting iterative inverse-power method solver.') tol = ss_args['tol'] mtol = ss_args['mtol'] if mtol is None: mtol = max(0.1*tol, 1e-15) maxiter = ss_args['maxiter'] use_solver(assumeSortedIndices=True) rhoss = Qobj() sflag = issuper(L) if sflag: rhoss.dims = L.dims[0] else: rhoss.dims = [L.dims[0], 1] n = L.shape[0] # Build Liouvillian if ss_args['solver'] == 'mkl' and ss_args['method'] == 'power': has_mkl = 1 else: has_mkl = 0 L, perm, perm2, rev_perm, ss_args = _steadystate_power_liouvillian(L, ss_args, has_mkl) orig_nnz = L.nnz # start with all ones as RHS v = np.ones(n, dtype=complex) if ss_args['use_rcm']: v = v[np.ix_(perm2,)] # Do preconditioning if ss_args['solver'] == 'scipy': if ss_args['M'] is None and ss_args['use_precond'] and \ ss_args['method'] in ['power-gmres', 'power-lgmres', 'power-bicgstab']: ss_args['M'], ss_args = _iterative_precondition(L, int(np.sqrt(n)), ss_args) if ss_args['M'] is None: warnings.warn("Preconditioning failed. Continuing without.", UserWarning) ss_iters = {'iter': 0} def _iter_count(r): ss_iters['iter'] += 1 return _power_start = time.time() # Get LU factors if ss_args['method'] == 'power': if ss_args['solver'] == 'mkl': lu = mkl_splu(L, max_iter_refine=ss_args['max_iter_refine'], scaling_vectors=ss_args['scaling_vectors'], weighted_matching=ss_args['weighted_matching']) else: lu = splu(L, permc_spec=ss_args['permc_spec'], diag_pivot_thresh=ss_args['diag_pivot_thresh'], options=dict(ILU_MILU=ss_args['ILU_MILU'])) if settings.debug: L_nnz = lu.L.nnz U_nnz = lu.U.nnz logger.debug('L NNZ: %i ; U NNZ: %i' % (L_nnz, U_nnz)) logger.debug('Fill factor: %f' % ((L_nnz+U_nnz)/orig_nnz)) it = 0 while (la.norm(L * v, np.inf) > tol) and (it < maxiter): check = 0 if ss_args['method'] == 'power': v = lu.solve(v) elif ss_args['method'] == 'power-gmres': v, check = gmres(L, v, tol=mtol, atol=ss_args['matol'], M=ss_args['M'], x0=ss_args['x0'], restart=ss_args['restart'], maxiter=ss_args['maxiter'], callback=_iter_count, callback_type='legacy') elif ss_args['method'] == 'power-lgmres': v, check = lgmres(L, v, tol=mtol, atol=ss_args['matol'], M=ss_args['M'], x0=ss_args['x0'], maxiter=ss_args['maxiter'], callback=_iter_count) elif ss_args['method'] == 'power-bicgstab': v, check = bicgstab(L, v, tol=mtol, atol=ss_args['matol'], M=ss_args['M'], x0=ss_args['x0'], maxiter=ss_args['maxiter'], callback=_iter_count) else: raise Exception("Invalid iterative solver method.") if check > 0: raise Exception("{} failed to find solution in " "{} iterations.".format(ss_args['method'], check)) if check < 0: raise Exception("Breakdown in {}".format(ss_args['method'])) v = v / la.norm(v, np.inf) it += 1 if ss_args['method'] == 'power' and ss_args['solver'] == 'mkl': lu.delete() if ss_args['return_info']: ss_args['info']['max_iter_refine'] = ss_args['max_iter_refine'] ss_args['info']['scaling_vectors'] = ss_args['scaling_vectors'] ss_args['info']['weighted_matching'] = ss_args['weighted_matching'] if it >= maxiter: raise Exception('Failed to find steady state after ' + str(maxiter) + ' iterations') _power_end = time.time() ss_args['info']['solution_time'] = _power_end-_power_start ss_args['info']['iterations'] = it if ss_args['return_info']: ss_args['info']['residual_norm'] = la.norm(L*v, np.inf) if settings.debug: logger.debug('Number of iterations: %i' % it) if ss_args['use_rcm']: v = v[np.ix_(rev_perm,)] # normalise according to type of problem if sflag: trow = v[::rhoss.shape[0]+1] data = v / np.sum(trow) else: data = data / la.norm(v) data = dense2D_to_fastcsr_fmode(vec2mat(data), rhoss.shape[0], rhoss.shape[0]) rhoss.data = 0.5 * (data + data.H) rhoss.isherm = True if ss_args['return_info']: return rhoss, ss_args['info'] else: return rhoss
def solve(self): """ might be broken for superLU """ if self.USE_UMFPACK: import scikits.umfpack as um umfpack = um.UmfpackContext( ) # Use default 'di' family of UMFPACK routines. umfpack.control[um.UMFPACK_STRATEGY_SYMMETRIC] = True umfpack.control[um.UMFPACK_PRL] = 0 # not working # UGLY c = self.standard_c A = self.standard_A b = self.standard_b M, N = A.shape converged = False """ Initial solution """ x, lambda_, s = self.find_initial_solution(A, b, c) zero_m_m = sp.csc_matrix((M, M)) iter = 1 while (not converged): if iter > self.max_iter: return False, np.nan """ Affine-scaling directions: 14.41 general form """ D = sp.diags(np.sqrt(1 / s)).dot(sp.diags(np.sqrt(x))) # CORRECT D_ = -sp.diags( 1.0 / np.square(D.diagonal())) # CORRECT (octave test) diag = D_.diagonal() pos_inds = np.where(diag >= 0.0) neg_inds = np.where(diag < 0.0) new_diag = diag[:] new_diag[pos_inds] += self.dyn_reg_eps new_diag[neg_inds] -= self.dyn_reg_eps D_.setdiag(new_diag) lhs = sp.bmat([[D_, A.T], [A, zero_m_m]], format='csc') lhs_fact = None if self.USE_UMFPACK: if iter == 1: umfpack.symbolic(lhs) #umfpack.report_symbolic() umfpack.numeric(lhs) #umfpack.report_numeric() else: lhs_fact = splin.splu( lhs ) #, permc_spec='MMD_AT_PLUS_A', diag_pivot_thresh=0.0, options=dict(SymmetricMode=True)) # superlu r_b = A.dot(x) - b r_c = A.T.dot(lambda_) + s - c r_xs = x * s rhs = np.hstack([-r_c + sp.diags(1 / x).dot(r_xs), -r_b]) sol = None if self.USE_UMFPACK: sol = umfpack(um.UMFPACK_A, lhs, rhs, autoTranspose=True) else: sol = lhs_fact.solve(rhs) delta_x_aff = sol[:N] delta_lambda_aff = sol[N:N + M] delta_s_aff = -sp.diags(1 / x).dot(r_xs) - (sp.diags(1 / x).dot( sp.diags(s))).dot(delta_x_aff) """ Affine-scaling step-length: 14:32 + 14:33 """ alpha_pri_aff = 1.0 for i in range(N): if delta_x_aff[i] < 0.0 and not np.isclose( delta_x_aff[i], 0.0): # CRITICAL alpha_pri_aff = min(alpha_pri_aff, -x[i] / delta_x_aff[i]) alpha_dual_aff = 1.0 for i in range(M): if delta_s_aff[i] < 0.0 and not np.isclose( delta_x_aff[i], 0.0): # CRITICAL alpha_dual_aff = min(alpha_dual_aff, -s[i] / delta_s_aff[i]) mu = 1 / N * np.dot(x, s) mu_aff = 1 / N * np.dot(x + alpha_pri_aff * delta_x_aff, s + alpha_dual_aff * delta_s_aff) """ Centering param """ sigma = (mu_aff / mu)**3 """ Re-solve for directions """ r_xs_ = r_xs + delta_x_aff * delta_s_aff - sigma * mu # CRITICAL # TODO rhs = np.hstack([-r_c + sp.diags(1 / x).dot(r_xs_), -r_b]) sol_ = None if self.USE_UMFPACK: sol_ = umfpack(um.UMFPACK_A, lhs, rhs, autoTranspose=True) else: sol_ = lhs_fact.solve(rhs) delta_x = sol_[:N] delta_lambda = sol_[N:N + M] delta_s = -sp.diags(1 / x).dot(r_xs_) - (sp.diags(1 / x).dot( sp.diags(s))).dot(delta_x) """ Step-lengths """ eta = 0.9 alpha_pri_max = np.inf for i in range(N): if delta_x[i] < 0.0: alpha_pri_max = min(alpha_pri_max, -x[i] / delta_x[i]) alpha_dual_max = np.inf for i in range(N): if delta_s[i] < 0.0: alpha_dual_max = min(alpha_dual_max, -s[i] / delta_s[i]) alpha_pri = min(1.0, eta * alpha_pri_max) alpha_dual = min(1.0, eta * alpha_dual_max) """ Update current solution """ x += alpha_pri * delta_x lambda_ += alpha_dual * delta_lambda s += alpha_dual * delta_s if abs(np.dot(c, x) - np.dot(b, lambda_)) < 1e-5: converged = True else: iter += 1 return True, np.dot(c, x)
def __init__(self, A): LOGGER.debug('computing the LU decomposition of a %s by %s ' 'sparse matrix with %s nonzeros ' % (A.shape + (A.nnz, ))) self.factor = spla.splu(A)
def step(self, dt, state_vector, B, L, M, P, NL, LU): """Advance solver by one timestep.""" # References MX = self.MX LX = self.LX F = self.F RHS = self.RHS # Cycle and compute timesteps self.dt.rotate() self.dt[0] = dt # Compute IMEX coefficients a, b, c = self.compute_coefficients(self.dt, self._iteration) self._iteration += 1 # Update RHS components and LHS matrices MX.rotate() LX.rotate() F.rotate() MX0 = MX[0] LX0 = LX[0] F0 = F[0] a0 = a[0] b0 = b[0] if STORE_LU: update_LHS = ((a0, b0) != self._LHS_params) self._LHS_params = (a0, b0) ell_start = B.ell_min ell_end = B.ell_max m_start = B.m_min m_end = B.m_max m_size = m_end - m_start + 1 for ell in range(ell_start, ell_end + 1): ell_local = ell - ell_start P[ell_local] = a0 * M[ell_local] + b0 * L[ell_local] for m in range(m_start, m_end + 1): m_local = m - m_start index = ell_local * m_size + m_local MX0.data[index] = M[ell_local].dot(state_vector.data[index]) LX0.data[index] = L[ell_local].dot(state_vector.data[index]) F0.data[index] = NL.data[index] # Build RHS RHS.data[index] *= 0. for j in range(1, len(c)): RHS.data[index] += c[j] * F[j - 1].data[index] for j in range(1, len(a)): RHS.data[index] -= a[j] * MX[j - 1].data[index] for j in range(1, len(b)): RHS.data[index] -= b[j] * LX[j - 1].data[index] # Solve if STORE_LU: if update_LHS: LU[ell_local] = linalg.splu(P[ell_local].tocsc(), permc_spec=PERMC_SPEC) pLHS = LU[ell_local] state_vector.data[index] = pLHS.solve(RHS.data[index]) else: state_vector.data[index] = linalg.spsolve( P[ell_local], RHS.data[index], use_umfpack=USE_UMFPACK, permc_spec=PERMC_SPEC)
def solve_newton(n, m, h, col_fun, bc, jac, y, p, B, bvp_tol): """Solve the nonlinear collocation system by a Newton method. This is a simple Newton method with a backtracking line search. As advised in [1]_, an affine-invariant criterion function F = ||J^-1 r||^2 is used, where J is the Jacobian matrix at the current iteration and r is the vector or collocation residuals (values of the system lhs). The method alters between full Newton iterations and the fixed-Jacobian iterations based There are other tricks proposed in [1]_, but they are not used as they don't seem to improve anything significantly, and even break the convergence on some test problems I tried. All important parameters of the algorithm are defined inside the function. Parameters ---------- n : int Number of equations in the ODE system. m : int Number of nodes in the mesh. h : ndarray, shape (m-1,) Mesh intervals. col_fun : callable Function computing collocation residuals. bc : callable Function computing boundary condition residuals. jac : callable Function computing the Jacobian of the whole system (including collocation and boundary condition residuals). It is supposed to return csc_matrix. y : ndarray, shape (n, m) Initial guess for the function values at the mesh nodes. p : ndarray, shape (k,) Initial guess for the unknown parameters. B : ndarray with shape (n, n) or None Matrix to force the S y(a) = 0 condition for a problems with the singular term. If None, the singular term is assumed to be absent. bvp_tol : float Tolerance to which we want to solve a BVP. Returns ------- y : ndarray, shape (n, m) Final iterate for the function values at the mesh nodes. p : ndarray, shape (k,) Final iterate for the unknown parameters. singular : bool True, if the LU decomposition failed because Jacobian turned out to be singular. References ---------- .. [1] U. Ascher, R. Mattheij and R. Russell "Numerical Solution of Boundary Value Problems for Ordinary Differential Equations" """ # We know that the solution residuals at the middle points of the mesh # are connected with collocation residuals r_middle = 1.5 * col_res / h. # As our BVP solver tries to decrease relative residuals below a certain # tolerance it seems reasonable to terminated Newton iterations by # comparison of r_middle / (1 + np.abs(f_middle)) with a certain threshold, # which we choose to be 1.5 orders lower than the BVP tolerance. We rewrite # the condition as col_res < tol_r * (1 + np.abs(f_middle)), then tol_r # should be computed as follows: tol_r = 2/3 * h * 5e-2 * bvp_tol # We also need to control residuals of the boundary conditions. But it # seems that they become very small eventually as the solver progresses, # i. e. the tolerance for BC are not very important. We set it 1.5 orders # lower than the BVP tolerance as well. tol_bc = 5e-2 * bvp_tol # Maximum allowed number of Jacobian evaluation and factorization, in # other words the maximum number of full Newton iterations. A small value # is recommended in the literature. max_njev = 4 # Maximum number of iterations, considering that some of them can be # performed with the fixed Jacobian. In theory such iterations are cheap, # but it's not that simple in Python. max_iter = 8 # Minimum relative improvement of the criterion function to accept the # step (Armijo constant). sigma = 0.2 # Step size decrease factor for backtracking. tau = 0.5 # Maximum number of backtracking steps, the minimum step is then # tau ** n_trial. n_trial = 4 col_res, y_middle, f, f_middle = col_fun(y, p) bc_res = bc(y[:, 0], y[:, -1], p) res = np.hstack((col_res.ravel(order='F'), bc_res)) njev = 0 singular = False recompute_jac = True for iteration in range(max_iter): if recompute_jac: J = jac(y, p, y_middle, f, f_middle, bc_res) njev += 1 try: LU = splu(J) except RuntimeError: singular = True break step = LU.solve(res) cost = np.dot(step, step) y_step = step[:m * n].reshape((n, m), order='F') p_step = step[m * n:] alpha = 1 for trial in range(n_trial + 1): y_new = y - alpha * y_step if B is not None: y_new[:, 0] = np.dot(B, y_new[:, 0]) p_new = p - alpha * p_step col_res, y_middle, f, f_middle = col_fun(y_new, p_new) bc_res = bc(y_new[:, 0], y_new[:, -1], p_new) res = np.hstack((col_res.ravel(order='F'), bc_res)) step_new = LU.solve(res) cost_new = np.dot(step_new, step_new) if cost_new < (1 - 2 * alpha * sigma) * cost: break if trial < n_trial: alpha *= tau y = y_new p = p_new if njev == max_njev: break if (np.all(np.abs(col_res) < tol_r * (1 + np.abs(f_middle))) and np.all(bc_res < tol_bc)): break # If the full step was taken, then we are going to continue with # the same Jacobian. This is the approach of BVP_SOLVER. if alpha == 1: step = step_new cost = cost_new recompute_jac = False else: recompute_jac = True return y, p, singular
def lu(A): self.nlu += 1 return splu(A)
tstop = Tt # ===================Sampling parameters================= nts = s.nts kts = int(Mt / nts) #=====================Generate matrix======================== Lap, Q = sparse_laplacian(nx, ny) I = sp.eye(nx * ny, format='csc') A0 = Q @ (I - CFL * Lap) # preconditioner M2 = spla.splu(A0) M_x = lambda x: M2.solve(x) M = spla.LinearOperator((nv, nv), M_x) ##====================Initial condition========================= # Temp = np.zeros((nv,nts+1)) # G_arr = np.zeros((nv,nts+1)); R_arr = np.zeros((nv,nts+1)) #T = np.zeros((ny,nx)) # temperature T0 #T = np.arange(nv).reshape((ny,nx)) T0 = p.Te * np.ones((ny, nx)) y = np.reshape(T0, (nv, ), order='F') #T0/y0 # Temp[:,[0]] = y #Gv,Rv = gradients(y,y,dx) #G_arr[:,[0]] = Gv; R_arr[:,[0]] = Rv
opJ = bem.operators.boundary.sparse.identity(opV.domain, opW.dual_to_range, opW.dual_to_range) opG = bem.operators.boundary.sparse.identity(opW.domain, opV.dual_to_range, opV.dual_to_range) print('Assembling V00...') tt = time() V00 = opV00.weak_form() tt_V00 = time() - tt Dof[i, 0], Tps[i, 0] = V00.shape[0], tt_V00 tt = time() J00w = opJ00.weak_form() J00 = J00w.sparse_operator iJ00lu = spla.splu(J00) iJ00 = spla.LinearOperator(iJ00lu.shape, matvec=iJ00lu.solve) Dof[i, 1], Tps[i, 1] = V00.shape[0], time() - tt # print('Assembling W00...') # tt = time() # W00 = opW00.weak_form() # tt_W00 = time() - tt # W00_real = lambda x: -np.real(W00(x)) # Dof[i, 6], Tps[i, 6] = W00.shape[0], tt_W00 print('Assembling V...') tt = time()
def steady_state(self, max_iter_refine = 100, use_mkl = False, weighted_matching = False, series_method = False): """ Computes steady state dynamics max_iter_refine : Int Parameter for the mkl LU solver. If pardiso errors are returned this should be increased. use_mkl : Boolean Optional override default use of mkl if mkl is installed. weighted_matching : Boolean Setting this true may increase run time, but reduce stability (pardisio may not converge). """ nstates = self._N_he sup_dim = self._sup_dim n = int(np.sqrt(sup_dim)) unit_h_elems = sp.identity(nstates, format='csr') L = deepcopy(self.RHSmat)# + sp.kron(unit_h_elems, #liouvillian(H).data) b_mat = np.zeros(sup_dim*nstates, dtype=complex) b_mat[0] = 1. L = L.tolil() L[0, 0 : n**2*nstates] = 0. L = L.tocsr() if settings.has_mkl & use_mkl == True: print("Using Intel mkl solver") from qutip._mkl.spsolve import (mkl_splu, mkl_spsolve) L = L.tocsr() + \ sp.csr_matrix((np.ones(n), (np.zeros(n), [num*(n+1)for num in range(n)])), shape=(n**2*nstates, n**2*nstates)) L.sort_indices() solution = mkl_spsolve(L, b_mat, perm = None, verbose = True, \ max_iter_refine = max_iter_refine, \ scaling_vectors = True, \ weighted_matching = weighted_matching) else: if series_method == False: L = L.tocsc() + \ sp.csc_matrix((np.ones(n), (np.zeros(n), [num*(n+1)for num in range(n)])), shape=(n**2*nstates, n**2*nstates)) # Use superLU solver LU = splu(L) solution = LU.solve(b_mat) else: L = L.tocsc() + \ sp.csc_matrix((np.ones(n), (np.zeros(n), [num*(n+1)for num in range(n)])), shape=(n**2*nstates, n**2*nstates)) # Use series method L.sort_indices() solution,fidelity = lgmres(L, b_mat) dims = self.H_sys.dims data = dense2D_to_fastcsr_fmode(vec2mat(solution[:sup_dim]), n, n) data = 0.5*(data + data.H) solution = solution.reshape((nstates, self.H_sys.shape[0]**2)) return Qobj(data, dims=dims), solution
def decoupledpf(Ybus, Sbus, V0, pv, pq, ppci, options): """Solves the power flow using a fast decoupled method. Solves for bus voltages given the full system admittance matrix (for all buses), the complex bus power injection vector (for all buses), the initial vector of complex bus voltages, the FDPF matrices B prime and B double prime, and column vectors with the lists of bus indices for the swing bus, PV buses, and PQ buses, respectively. The bus voltage vector contains the set point for generator (including ref bus) buses, and the reference angle of the swing bus, as well as an initial guess for remaining magnitudes and angles. C{ppopt} is a PYPOWER options vector which can be used to set the termination tolerance, maximum number of iterations, and output options (see L{ppoption} for details). Uses default options if this parameter is not given. Returns the final complex voltages, a flag which indicates whether it converged or not, and the number of iterations performed. @see: L{runpf} @author: Ray Zimmerman (PSERC Cornell) Modified to consider voltage_depend_loads """ # old algortihm options to the new ones pp2pypower_algo = {'fdbx': 2, 'fdxb': 3} # options tol = options["tolerance_mva"] max_it = options["max_iteration"] # No use currently for numba. TODO: Check if can be applied in Bp and Bpp # numba = options["numba"] # NOTE: options["algorithm"] is either 'fdbx' or 'fdxb'. Otherwise, error algorithm = pp2pypower_algo[options["algorithm"]] voltage_depend_loads = options["voltage_depend_loads"] v_debug = options["v_debug"] baseMVA = ppci["baseMVA"] bus = ppci["bus"] branch = ppci["branch"] gen = ppci["gen"] # initialize i = 0 V = V0 Va = angle(V) Vm = abs(V) dVa, dVm = None, None if v_debug: Vm_it = Vm.copy() Va_it = Va.copy() else: Vm_it = None Va_it = None # set up indexing for updating V pvpq = r_[pv, pq] # evaluate initial mismatch P, Q = _evaluate_mis(Ybus, V, Sbus, pvpq, pq) # check tolerance converged = _check_for_convergence(P, Q, tol) # create and reduce B matrices Bp, Bpp = makeB(baseMVA, bus, real(branch), algorithm) # splu requires a CSC matrix Bp = Bp[array([pvpq]).T, pvpq].tocsc() Bpp = Bpp[array([pq]).T, pq].tocsc() # factor B matrices Bp_solver = splu(Bp) Bpp_solver = splu(Bpp) # do P and Q iterations while (not converged and i < max_it): # update iteration counter i = i + 1 # ----- do P iteration, update Va ----- dVa = -Bp_solver.solve(P) # update voltage Va[pvpq] = Va[pvpq] + dVa V = Vm * exp(1j * Va) # evalute mismatch P, Q = _evaluate_mis(Ybus, V, Sbus, pvpq, pq) # check tolerance if _check_for_convergence(P, Q, tol): converged = True break # ----- do Q iteration, update Vm ----- dVm = -Bpp_solver.solve(Q) # update voltage Vm[pq] = Vm[pq] + dVm V = Vm * exp(1j * Va) if v_debug: Vm_it = column_stack((Vm_it, Vm)) Va_it = column_stack((Va_it, Va)) if voltage_depend_loads: Sbus = makeSbus(baseMVA, bus, gen, vm=Vm) # evalute mismatch P, Q = _evaluate_mis(Ybus, V, Sbus, pvpq, pq) # check tolerance if _check_for_convergence(P, Q, tol): converged = True break # the newtonpf/newtonpf funtion returns J. We are returning Bp and Bpp return V, converged, i, Bp, Bpp, Vm_it, Va_it
def cp_R(problem_data, rho_method): ###################### ## IMPORT LIBRARIES ## ###################### #Math libraries import numpy as np from scipy.sparse import csc_matrix from scipy.sparse import csr_matrix from scipy.sparse import linalg #Timing import time #Import data from Data.read_fclib import * #Plot residuals from Solver.ADMM_iteration.Numerics.plot import * #Initial penalty parameter import Solver.Rho.Optimal #Max iterations and kind of tolerance from Solver.Tolerance.iter_totaltolerance import * #Acceleration from Solver.Acceleration.minusr import * #b = Es matrix from Data.Es_matrix import * #Projection onto second order cone from Solver.ADMM_iteration.Numerics.projection import * ##################################################### ############# TERMS / NOT A FUNCTION YET ############ ##################################################### start = time.clock() problem = hdf5_file(problem_data) M = problem.M.tocsc() f = problem.f A = csc_matrix.transpose(problem.H.tocsc()) A_T = csr_matrix.transpose(A) w = problem.w mu = problem.mu #Dimensions (normal,tangential,tangential) dim1 = 3 dim2 = np.shape(w)[0] #Problem size n = np.shape(M)[0] p = np.shape(w)[0] b = 0.1 * Es_matrix(w, mu, np.ones([ p, ])) / np.linalg.norm(Es_matrix(w, mu, np.ones([ p, ]))) ################################# ############# SET-UP ############ ################################# #Set-up of vectors v = [np.zeros([ n, ])] u = [ np.zeros([ p, ]) ] #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])] u_hat = [np.zeros([ p, ])] #u_hat[0] #in the notation of the paper this used with a underline xi = [np.zeros([ p, ])] xi_hat = [np.zeros([ p, ])] r = [np.zeros([ p, ])] #primal residual s = [np.zeros([ p, ])] #dual residual r_norm = [0] s_norm = [0] tau = [1] #over-relaxation e = [] #restart #Optimal penalty parameter rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)' rho = eval(rho_string) ######################################## ## TERMS COMMON TO ALL THE ITERATIONS ## ######################################## #Super LU factorization of M + rho * dot(M_T,M) P = M + rho * csc_matrix.dot(A_T, A) LU = linalg.splu(P) ################ ## ITERATIONS ## ################ for k in range(MAXITER): ################ ## v - update ## ################ RHS = -f + rho * csc_matrix.dot(A_T, -w - b - xi_hat[k] + u_hat[k]) v.append(LU.solve(RHS)) #v[k+1] ################ ## u - update ## ################ Av = csr_matrix.dot(A, v[k + 1]) vector = Av + xi_hat[k] + w + b u.append(projection(vector, mu, dim1, dim2)) #u[k+1] ######################## ## residuals - update ## ######################## s.append(rho * csc_matrix.dot(A_T, (u[k + 1] - u_hat[k]))) #s[k+1] r.append(Av - u[k + 1] + w + b) #r[k+1] ################# ## xi - update ## ################# xi.append(xi_hat[k] + r[k + 1]) #xi[k+1] #################### ## stop criterion ## #################### pri_evalf = np.amax( np.array([ np.linalg.norm(csr_matrix.dot(A, v[k + 1])), np.linalg.norm(u[k + 1]), np.linalg.norm(w + b) ])) eps_pri = np.sqrt(p) * ABSTOL + RELTOL * pri_evalf dual_evalf = np.linalg.norm(rho * csc_matrix.dot(A_T, xi[k + 1])) eps_dual = np.sqrt(n) * ABSTOL + RELTOL * dual_evalf r_norm.append(np.linalg.norm(r[k + 1])) s_norm.append(np.linalg.norm(s[k + 1])) if r_norm[k + 1] <= eps_pri and s_norm[k + 1] <= eps_dual: break ###################################### ## accelerated ADMM without restart ## ###################################### minusr(tau, u, u_hat, xi, xi_hat, k) #end rutine end = time.clock() #################### ## REPORTING DATA ## #################### #plotit(r,s,start,end,'With acceleration / Without restarting for '+problem_data+' for rho: '+rho_method) time = end - start return time
M = msysmat.todense() if not shiftall else mshiftsmat.todense() levs = spla.eigvals(A, M, overwrite_a=True, check_finite=False) else: levs = spsla.eigs(asysmat, M=mshiftsmat, sigma=1, k=10, which='LR', return_eigenvectors=False) dou.save_npa(levs, levstr) plt.figure(1) # plt.xlim((-20, 15)) # plt.ylim((-100, 100)) plt.plot(np.real(levs), np.imag(levs), '+') plt.show(block=False) projsys = False if projsys: mfac = spsla.splu(M) mijtl = [] for kcol in range(J.shape[0]): curcol = np.array(J.T[:, kcol].todense()) micc = np.array(mfac.solve(curcol.flatten())) mijtl.append(micc.reshape((NV, 1))) mijt = np.hstack(mijtl) si = np.linalg.inv(J*mijt) def _comp_proj(v): jtsijv = np.array(J.T*np.dot(si, J*v)).flatten() mjtsijv = mfac.solve(jtsijv) return v - mjtsijv.reshape((NV, 1)) def _comp_proj_t(v):
def Rayleigh_Benard(Rayleigh=1e6, Prandtl=1, nz=64, nx=None, aspect=2, fixed_flux=False, fixed_T=True, viscous_heating=False, restart=None, run_time=150., run_time_buoyancy=20000., run_time_iter=np.inf, data_dir='./', coeff_output=True, verbose=False): # input parameters logger.info("Ra = {}, Pr = {}".format(Rayleigh, Prandtl)) Lz = 1. Lx = aspect * Lz if nx is None: nx = int(nz * aspect) logger.info("resolution: [{}x{}]".format(nx, nz)) # Create bases and domain x_basis = de.Fourier('x', nx, interval=(0, Lx), dealias=3 / 2) z_basis = de.Chebyshev('z', nz, interval=(0, Lz), dealias=3 / 2) domain = de.Domain([x_basis, z_basis], grid_dtype=np.float64) if domain.distributor.rank == 0: import os if not os.path.exists('{:s}/'.format(data_dir)): os.mkdir('{:s}/'.format(data_dir)) if fixed_flux: T_bc_var = 'Tz' elif fixed_T: T_bc_var = 'T' # 2D Boussinesq hydrodynamics problem = de.IVP(domain, variables=['Tz', 'T', 'p', 'u', 'uz', 'w', 'wz']) problem.meta['p', T_bc_var, 'uz', 'w']['z']['dirichlet'] = True T0_z = domain.new_field() T0_z.meta['x']['constant'] = True T0_z['g'] = -1 T0 = domain.new_field() T0.meta['x']['constant'] = True T0['g'] = Lz / 2 - domain.grid(-1) problem.parameters['T0'] = T0 problem.parameters['T0_z'] = T0_z problem.parameters['P'] = (Rayleigh * Prandtl)**(-1 / 2) problem.parameters['R'] = (Rayleigh / Prandtl)**(-1 / 2) problem.parameters['F'] = F = 1 problem.parameters['Lx'] = Lx problem.parameters['Lz'] = Lz # Phi z0 = 1. + Lz problem.parameters['phi'] = phi = - 'g' * (z0 - 'z') problem.substitutions['plane_avg(A)'] = 'integ(A, "x")/Lx' problem.substitutions['vol_avg(A)'] = 'integ(A)/Lx/Lz' problem.substitutions['enstrophy'] = '(dx(w) - uz)**2' problem.substitutions['vorticity'] = '(dx(w) - uz)' problem.substitutions['u_fluc'] = '(u - plane_avg(u))' problem.substitutions['w_fluc'] = '(w - plane_avg(w))' problem.substitutions['KE'] = '(0.5*(u*u+w*w))' problem.substitutions['sigma_xz'] = '(dx(w) + uz)' problem.substitutions['sigma_xx'] = '(2*dx(u))' problem.substitutions['sigma_zz'] = '(2*wz)' if viscous_heating: problem.substitutions[ 'visc_heat'] = 'R*((sigma_xz)*(dx(w)+uz) + (sigma_xx)*dx(u) + (sigma_zz)*wz)' problem.substitutions['visc_flux_z'] = 'R*(u*sigma_xz + w*sigma_zz)' else: problem.substitutions['visc_heat'] = '0' problem.substitutions['visc_flux_z'] = '0' problem.substitutions['conv_flux_z'] = '(w*T + visc_flux_z)/P' problem.substitutions['kappa_flux_z'] = '(-Tz)' problem.add_equation("Tz - dz(T) = 0") problem.add_equation( "dt(T) - (P / phi) * (dx(dx(T * phi)) + dz(Tz + phi)) + w*T0_z = -(u*dx(T) + w*Tz) - visc_heat" ) problem.add_equation( "dt(u) - R*(dx(dx(u)) + dz(uz)) + dx(p) = -(u*dx(u) + w*uz)") problem.add_equation( "dt(w) - R*(dx(dx(w)) + dz(wz)) + dz(p) - T = -(u*dx(w) + w*wz)") problem.add_equation("uz - dz(u) = 0") problem.add_equation("wz - dz(w) = 0") problem.add_equation("dx(u) + wz = 0") problem.add_bc("left(p) = 0", condition="(nx == 0)") if fixed_flux: problem.add_bc("left(Tz) = 0") problem.add_bc("right(Tz) = 0") elif fixed_T: problem.add_bc("left(T) = 0") problem.add_bc("right(T) = 0") problem.add_bc("left(uz) = 0") problem.add_bc("right(uz) = 0") problem.add_bc("left(w) = 0") problem.add_bc("right(w) = 0", condition="(nx != 0)") # Build solver ts = de.timesteppers.RK443 cfl_safety = 1 solver = problem.build_solver(ts) logger.info('Solver built') # Checkpointing if checkpointing: checkpoint = Checkpoint(data_dir) checkpoint.set_checkpoint(solver, wall_dt=1800) # Initial conditions x = domain.grid(0) z = domain.grid(1) T = solver.state['T'] Tz = solver.state['Tz'] # Random perturbations, initialized globally for same results in parallel noise = global_noise(domain, scale=1, frac=0.25) if restart is None: # Linear background + perturbations damped at walls zb, zt = z_basis.interval pert = 1e-3 * noise * (zt - z) * (z - zb) T['g'] = pert T.differentiate('z', out=Tz) else: logger.info("restarting from {}".format(restart)) checkpoint.restart(restart, solver) # Integration parameters solver.stop_sim_time = np.inf solver.stop_wall_time = run_time * 3600. solver.stop_iteration = np.inf # Analysis analysis_tasks = [] snapshots = solver.evaluator.add_file_handler(data_dir + 'slices', sim_dt=0.1, max_writes=10) snapshots.add_task("T+T0", name='T') snapshots.add_task("T - plane_avg(T)", name="T'") snapshots.add_task("enstrophy") snapshots.add_task("vorticity") analysis_tasks.append(snapshots) if coeff_output: coeffs = solver.evaluator.add_file_handler(data_dir + 'coeffs', sim_dt=0.1, max_writes=10) coeffs.add_task("T+T0", name="T", layout='c') coeffs.add_task("T - plane_avg(T)", name="T'", layout='c') coeffs.add_task("w", layout='c') coeffs.add_task("u", layout='c') coeffs.add_task("enstrophy", layout='c') coeffs.add_task("vorticity", layout='c') analysis_tasks.append(coeffs) profiles = solver.evaluator.add_file_handler(data_dir + 'profiles', sim_dt=0.1, max_writes=10) profiles.add_task("plane_avg(T+T0)", name="T") profiles.add_task("plane_avg(T)", name="T'") profiles.add_task("plane_avg(u)", name="u") profiles.add_task("plane_avg(enstrophy)", name="enstrophy") # This may have an error: profiles.add_task("plane_avg(conv_flux_z)/plane_avg(kappa_flux_z) + 1", name="Nu") profiles.add_task("plane_avg(conv_flux_z) + plane_avg(kappa_flux_z)", name="Nu_2") analysis_tasks.append(profiles) scalar = solver.evaluator.add_file_handler(data_dir + 'scalar', sim_dt=0.1, max_writes=10) scalar.add_task("vol_avg(T)", name="IE") scalar.add_task("vol_avg(KE)", name="KE") scalar.add_task("vol_avg(T) + vol_avg(KE)", name="TE") scalar.add_task("0.5*vol_avg(u_fluc*u_fluc+w_fluc*w_fluc)", name="KE_fluc") scalar.add_task("0.5*vol_avg(u*u)", name="KE_x") scalar.add_task("0.5*vol_avg(w*w)", name="KE_z") scalar.add_task("0.5*vol_avg(u_fluc*u_fluc)", name="KE_x_fluc") scalar.add_task("0.5*vol_avg(w_fluc*w_fluc)", name="KE_z_fluc") scalar.add_task("vol_avg(plane_avg(u)**2)", name="u_avg") scalar.add_task("vol_avg((u - plane_avg(u))**2)", name="u1") scalar.add_task("vol_avg(conv_flux_z) + 1.", name="Nu") analysis_tasks.append(scalar) # CFL CFL = flow_tools.CFL(solver, initial_dt=0.1, cadence=1, safety=cfl_safety, max_change=1.5, min_change=0.5, max_dt=0.1, threshold=0.1) CFL.add_velocities(('u', 'w')) # Flow properties flow = flow_tools.GlobalFlowProperty(solver, cadence=1) flow.add_property("sqrt(u*u + w*w) / R", name='Re') first_step = True # Main loop try: logger.info('Starting loop') Re_avg = 0 while solver.ok and np.isfinite(Re_avg): dt = CFL.compute_dt() solver.step(dt) #, trim=True) Re_avg = flow.grid_average('Re') log_string = 'Iteration: {:5d}, '.format(solver.iteration) log_string += 'Time: {:8.3e}, dt: {:8.3e}, '.format( solver.sim_time, dt) log_string += 'Re: {:8.3e}/{:8.3e}'.format(Re_avg, flow.max('Re')) logger.info(log_string) if first_step: if verbose: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.spy(solver.pencils[0].L, markersize=1, markeredgewidth=0.0) fig.savefig(data_dir + "sparsity_pattern.png", dpi=1200) import scipy.sparse.linalg as sla LU = sla.splu(solver.pencils[0].LHS.tocsc(), permc_spec='NATURAL') fig = plt.figure() ax = fig.add_subplot(1, 2, 1) ax.spy(LU.L.A, markersize=1, markeredgewidth=0.0) ax = fig.add_subplot(1, 2, 2) ax.spy(LU.U.A, markersize=1, markeredgewidth=0.0) fig.savefig(data_dir + "sparsity_pattern_LU.png", dpi=1200) logger.info("{} nonzero entries in LU".format(LU.nnz)) logger.info("{} nonzero entries in LHS".format( solver.pencils[0].LHS.tocsc().nnz)) logger.info("{} fill in factor".format( LU.nnz / solver.pencils[0].LHS.tocsc().nnz)) first_step = False start_time = time.time() except: logger.error('Exception raised, triggering end of main loop.') raise finally: end_time = time.time() main_loop_time = end_time - start_time n_iter_loop = solver.iteration - 1 logger.info('Iterations: {:d}'.format(n_iter_loop)) logger.info('Sim end time: {:f}'.format(solver.sim_time)) logger.info('Run time: {:f} sec'.format(main_loop_time)) logger.info('Run time: {:f} cpu-hr'.format(main_loop_time / 60 / 60 * domain.dist.comm_cart.size)) logger.info('iter/sec: {:f} (main loop only)'.format(n_iter_loop / main_loop_time)) logger.info('beginning join operation') if checkpointing: try: final_checkpoint = Checkpoint( data_dir, checkpoint_name='final_checkpoint') final_checkpoint.set_checkpoint(solver, wall_dt=1, write_num=1, set_num=1) solver.step(dt) #clean this up in the future...works for now. logger.info(data_dir + '/final_checkpoint/') post.merge_analysis(data_dir + '/final_checkpoint/') except: print('cannot save final checkpoint') logger.info(data_dir + '/checkpoint/') post.merge_analysis(data_dir + '/checkpoint/') for task in analysis_tasks: logger.info(task.base_path) post.merge_analysis(task.base_path) logger.info(40 * "=") logger.info('Iterations: {:d}'.format(n_iter_loop)) logger.info('Sim end time: {:f}'.format(solver.sim_time)) logger.info('Run time: {:f} sec'.format(main_loop_time)) logger.info('Run time: {:f} cpu-hr'.format(main_loop_time / 60 / 60 * domain.dist.comm_cart.size)) logger.info('iter/sec: {:f} (main loop only)'.format(n_iter_loop / main_loop_time))
def newton_admm(data, cone, maxiters=100, admm_maxiters=1, gmres_maxiters=lambda i: i // 10 + 1, zerotol=1e-14, res_tol=1e-10, ridge=0, alpha=0.001, beta=0.5, verbose=0, benchmark=None): A, b, c = _unpack(data) assert (A.ndim == 2 and b.ndim == 1 and c.ndim == 1) m, n = A.shape k = n + m + 1 # preconstruct IplusQ and do an LU decomposition Q = sp.bmat([[None, A.T, c[:, None]], [-A, None, b[:, None]], [-c[None, :], -b[None, :], None]]) IplusQ = (sp.eye(Q.shape[0]) + Q).tocsc() if IplusQ.nnz / (Q.shape[0]**2) < 0.1: IplusQ_LU = sla.splu(IplusQ) solve_IplusQ = lambda v: IplusQ_LU.solve(v) else: IplusQ = IplusQ.todense() IplusQ_LU = scipy.linalg.lu_factor(IplusQ) solve_IplusQ = lambda v: scipy.linalg.lu_solve(IplusQ_LU, v) # create preconditioner linear op M_lo = sla.LinearOperator((3 * k, 3 * k), matvec=lambda v: M_matvec(v, solve_IplusQ, k)) # Initialize ADMM variables utilde, u, v = np.zeros(k), np.zeros(k), np.zeros(k) utilde[-1] = 1.0 u[-1] = 1.0 v[-1] = 1.0 Ik = sp.eye(k) In = sp.eye(n) # save benchmarks in a dictionary b = _setup_benchmark() extra_iters = 0 for iters in range(admm_maxiters): if benchmark is not None: start_time = time() # do admm step utilde = solve_IplusQ(u + v) u = _proj_onto_C(utilde - v, cone, n) v = v - utilde + u if benchmark is not None: update_benchmark(b, u, v, c, time() - start_time, zerotol, benchmark, m) if verbose and np.mod(iters, verbose) == 0: # If still running ADMM iterations, compute residuals r_utilde, r_u, r_v = _compute_r(utilde, u, v, cone, n, IplusQ) _r_utilde, _r_u, _r_v = np.linalg.norm(r_utilde), np.linalg.norm( r_u), np.linalg.norm(r_v) x, _, _ = _extract_solution(u, v, n, zerotol) objval = c.T.dot(x) print( "%d/%d: r_utilde = %E, r_u = %E, r_v = %E, obj val c^T x = %E." % (iters, admm_maxiters, _r_utilde, _r_u, _r_v, objval)) if all(r < res_tol for r in (_r_utilde, _r_u, _r_v)): print("Stopping early, residuals < {}".format(res_tol)) break for iters in range(maxiters): if benchmark is not None: start_time = time() # do newton step r_utilde, r_u, r_v = _compute_r(utilde, u, v, cone, n, IplusQ) # create linear operator for Jacobian # This code is more readable but slower, and is left here # just for the understanding of the reader. # d = np.array(utilde[-1] - v[-1] >= # 0.0).astype(np.float64).reshape(1, 1) # D = block_diag(_J_onto_Kstar((utilde - v)[n:-1], cone)[0], # arrtype=sla.LinearOperator) # D_lo = block_diag([In, D, d], arrtype=sla.LinearOperator) # J_lo = block([[IplusQ * (1 + ridge), '-I', '-I'], # [-D_lo, Ik * (1 + ridge), D_lo], # ['I', '-I', -Ik * ridge]], arrtype=sla.LinearOperator) # Instead of building it block-wise, it is more efficient to just # create the linear operator directly as follows. d = np.array(utilde[-1] - v[-1] >= 0.0).astype(np.float64).reshape( 1, 1) J_cones, cone_lengths = _J_onto_Kstar((utilde - v)[n:-1], cone) J_lo = sla.LinearOperator( (3 * k, 3 * k), matvec=lambda x: J_matvec(x, n, m, k, d, ridge, IplusQ, J_cones, cone_lengths)) # approximately solve then newton step dall, info = sla.gmres(J_lo, np.concatenate([r_utilde, r_u, r_v]), M=M_lo, tol=1e-12, maxiter=gmres_maxiters(iters) + extra_iters) dutilde, du, dv = dall[0:k], dall[k:2 * k], dall[2 * k:] # backtracking line search t = 1.0 r_all_norm = np.linalg.norm(np.concatenate([r_utilde, r_u, r_v])) while True: utilde0 = utilde - t * dutilde u0 = u - t * du v0 = v - t * dv r_utilde0, r_u0, r_v0 = _compute_r(utilde0, u0, v0, cone, n, IplusQ) if not ((np.linalg.norm(np.concatenate([r_utilde0, r_u0, r_v0])) > (1 - alpha * t) * r_all_norm) and (t >= 1e-4)): break t *= beta if t < 1e-4: extra_iters += 1 # update iterates utilde, u, v = utilde0, u0, v0 ridge *= 0.9 if benchmark is not None: update_benchmark(b, u, v, c, time() - start_time, zerotol, benchmark, m) if verbose and np.mod(iters, verbose) == 0: # If still running ADMM iterations, compute residuals _r_utilde, _r_u, _r_v = np.linalg.norm(r_utilde), np.linalg.norm( r_u), np.linalg.norm(r_v) x, _, _ = _extract_solution(u, v, n, zerotol) objval = c.T.dot(x) print( "%d/%d: r_utilde = %E, r_u = %E, r_v = %E, obj val c^T x = %E. (%d)" % (iters, maxiters, _r_utilde, _r_u, _r_v, objval, extra_iters)) if all(r < res_tol for r in (_r_utilde, _r_u, _r_v)): print("Stopping early, residuals < {}".format(res_tol)) break x, s, y = _extract_solution(u, v, n, zerotol) return { 'x': x, 's': s, 'y': y, 'info': { 'fstar': c.dot(x), 'benchmark': b } }
col_end += c Jcsc[row_start:row_end, col_start:col_end] = mat row_start, col_start = row_end, col_end mat = bem.as_matrix(Jn) mat = sp.lil_matrix(mat) r, c = mat.shape row_end += r col_end += c Jcsc[row_start:row_end, col_start:col_end] = mat row_start, col_start = row_end, col_end Jcsc = Jcsc.tocsc() print('##time convert Identity to CSC: {}'.format(time() - tt)) tt = time() iJlu = spla.splu(Jcsc) print('##time sparse J=LU: {}'.format(time() - tt)) tt = time() Ecsc = sp.lil_matrix(Xw.shape, dtype=complex) row_start, row_end = 0, 0 for r in range(len(domains)): row, col = 0, 0 col_start, col_end = 0, 0 first = True for c in range(len(domains)): if first: op = opI[r, r] row, _ = op.weak_form().shape row_end += row first = False
# Open reference image imdef = imagefile % imnums[-2] g = px.Image(imdef).Load() # Multiscale initialization of the displacement U0 = px.MultiscaleInit(m, f, g, cam, 3) m.Plot(U0, 30) #%% ============================================================================ # Classic Modified Gauss Newton =============================================== # ============================================================================== U = U0.copy() dic = px.DICEngine() H = dic.ComputeLHS(f, m, cam) H_LU = splalg.splu(H) for ik in range(0, 30): [b, res] = dic.ComputeRHS(g, m, cam, U) dU = H_LU.solve(b) U += dU err = np.linalg.norm(dU) / np.linalg.norm(U) print("Iter # %2d | disc/dyn=%2.2f %% | dU/U=%1.2e" % (ik + 1, np.std(res) / dic.dyn * 100, err)) if err < 1e-3: break #%% ============================================================================ # Post-processing ============================================================= # ============================================================================== # Visualization: Scaled deformation of the mesh m.Plot(edgecolor='#CCCCCC')
def cp_RR(problem_data, rho_method): ###################### ## IMPORT LIBRARIES ## ###################### #Math libraries import numpy as np from scipy.sparse import csc_matrix from scipy.sparse import csr_matrix from scipy.sparse import linalg #Timing import time #Import data from Data.read_fclib import * #Plot residuals from Solver.ADMM_iteration.Numerics.plot import * #Initial penalty parameter import Solver.Rho.Optimal #Max iterations and kind of tolerance from Solver.Tolerance.iter_totaltolerance import * #Acceleration from Solver.Acceleration.plusr import * #b = Es matrix from Data.Es_matrix import * #Projection onto second order cone from Solver.ADMM_iteration.Numerics.projection import * ##################################################### ############# TERMS / NOT A FUNCTION YET ############ ##################################################### start = time.clock() problem = hdf5_file(problem_data) M = problem.M.tocsc() f = problem.f A = csc_matrix.transpose(problem.H.tocsc()) A_T = csr_matrix.transpose(A) w = problem.w mu = problem.mu #Dimensions (normal,tangential,tangential) dim1 = 3 dim2 = np.shape(w)[0] #Problem size n = np.shape(M)[0] p = np.shape(w)[0] b = [ 1 / linalg.norm(A, 'fro') * Es_matrix(w, mu, np.zeros([ p, ])) / np.linalg.norm(Es_matrix(w, mu, np.ones([ p, ]))) ] ################################# ############# SET-UP ############ ################################# #Set-up of vectors v = [np.zeros([ n, ])] u = [ np.zeros([ p, ]) ] #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])] u_hat = [np.zeros([ p, ])] #u_hat[0] #in the notation of the paper this used with a underline xi = [np.zeros([ p, ])] xi_hat = [np.zeros([ p, ])] r = [np.zeros([ p, ])] #primal residual s = [np.zeros([ p, ])] #dual residual r_norm = [0] s_norm = [0] tau = [1] #over-relaxation e = [] #restart #Optimal penalty parameter rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)' rho = eval(rho_string) #Plot r_plot = [] s_plot = [] b_plot = [] u_bin_plot = [] xi_bin_plot = [] ######################################## ## TERMS COMMON TO ALL THE ITERATIONS ## ######################################## #Super LU factorization of M + rho * dot(M_T,M) P = M + rho * csc_matrix.dot(A_T, A) LU = linalg.splu(P) ################ ## ITERATIONS ## ################ for j in range(20): print j len_u = len(u) - 1 for k in range(len_u, MAXITER): ################ ## v - update ## ################ RHS = -f + rho * csc_matrix.dot(A_T, -w - b[j] - xi_hat[k] + u_hat[k]) v.append(LU.solve(RHS)) #v[k+1] ################ ## u - update ## ################ Av = csr_matrix.dot(A, v[k + 1]) vector = Av + xi_hat[k] + w + b[j] u.append(projection(vector, mu, dim1, dim2)) #u[k+1] ######################## ## residuals - update ## ######################## s.append(rho * csc_matrix.dot(A_T, (u[k + 1] - u_hat[k]))) #s[k+1] r.append(Av - u[k + 1] + w + b[j]) #r[k+1] ################# ## xi - update ## ################# xi.append(xi_hat[k] + r[k + 1]) #xi[k+1] ################################### ## accelerated ADMM with restart ## ################################### plusr(tau, u, u_hat, xi, xi_hat, k, e, rho) #################### ## stop criterion ## #################### pri_evalf = np.amax( np.array([ np.linalg.norm(csr_matrix.dot(A, v[k + 1])), np.linalg.norm(u[k + 1]), np.linalg.norm(w + b[j]) ])) eps_pri = np.sqrt(p) * ABSTOL + RELTOL * pri_evalf dual_evalf = np.linalg.norm(rho * csc_matrix.dot(A_T, xi[k + 1])) eps_dual = np.sqrt(n) * ABSTOL + RELTOL * dual_evalf r_norm.append(np.linalg.norm(r[k + 1])) s_norm.append(np.linalg.norm(s[k + 1])) if r_norm[k + 1] <= eps_pri and s_norm[k + 1] <= eps_dual: for element in range(len(u)): #Relative velocity u_proj = projection(u[element], mu, dim1, dim2) u_proj_contact = np.split(u_proj, dim2 / dim1) u_contact = np.split(u[element], dim2 / dim1) u_count = 0.0 for contact in range(dim2 / dim1): if np.array_equiv(u_contact[contact], u_proj_contact[contact]): u_count += 1.0 u_bin = 100 * u_count / (dim2 / dim1) u_bin_plot.append(u_bin) #Reaction xi_proj = projection(xi[element], 1 / mu, dim1, dim2) xi_proj_contact = np.split(xi_proj, dim2 / dim1) xi_contact = np.split(xi[element], dim2 / dim1) xi_count = 0.0 for contact in range(dim2 / dim1): if np.array_equiv(xi_contact[contact], xi_proj_contact[contact]): xi_count += 1.0 xi_bin = 100 * xi_count / (dim2 / dim1) xi_bin_plot.append(xi_bin) for element in range(len(r_norm)): r_plot.append(r_norm[element]) s_plot.append(s_norm[element]) b_plot.append(np.linalg.norm(b[j])) #print 'First contact' #print rho*xi[k+1][:3] #uy = projection(rho*xi[k+1],1/mu,dim1,dim2) #print uy[:3] #print 'Last contact' #print rho*xi[k+1][-3:] #print uy[-3:] #print u[k+1] #R = rho*xi[k+1] #N1 = csc_matrix.dot(M, v[k+1]) - csc_matrix.dot(A_T, R) + f #N2 = R - projection(R - u[k+1], 1/mu, dim1, dim2) #N1_norm = np.linalg.norm(N1) #N2_norm = np.linalg.norm(N2) #print np.sqrt( N1_norm**2 + N2_norm**2 ) break #b(s) stop criterion b.append(Es_matrix(w, mu, Av + w)) if j == 0: pass else: b_per_contact_j1 = np.split(b[j + 1], dim2 / dim1) b_per_contact_j0 = np.split(b[j], dim2 / dim1) count = 0 for i in range(dim2 / dim1): if np.linalg.norm(b_per_contact_j1[i] - b_per_contact_j0[i]) / np.linalg.norm( b_per_contact_j0[i]) > 1e-03: count += 1 if count < 1: break v = [np.zeros([ n, ])] u = [ np.zeros([ p, ]) ] #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])] u_hat = [np.zeros([ p, ])] #u_hat[0] #in the notation of the paper this used with a underline xi = [np.zeros([ p, ])] xi_hat = [np.zeros([ p, ])] r = [np.zeros([ p, ])] #primal residual s = [np.zeros([ p, ])] #dual residual r_norm = [0] s_norm = [0] tau = [1] #over-relaxation e = [] #restart end = time.clock() #################### ## REPORTING DATA ## #################### f, axarr = plt.subplots(4, sharex=True) f.suptitle('External update with cp_RR (Acary)') axarr[0].semilogy(b_plot) axarr[0].set(ylabel='||Phi(s)||') axarr[1].axhline(y=rho) axarr[1].set(ylabel='Rho') axarr[2].semilogy(r_plot, label='||r||') axarr[2].semilogy(s_plot, label='||s||') axarr[2].set(ylabel='Residuals') axarr[3].plot(u_bin_plot, label='u in K*') axarr[3].plot(xi_bin_plot, label='xi in K') axarr[3].legend() axarr[3].set(xlabel='Iteration', ylabel='Projection (%)') plt.show() #plotit(r,b,start,end,'With acceleration / With restarting for '+problem_data+' for rho: '+rho_method) time = end - start print 'Total time: ', time return time
basis = InteriorBasis(mesh, element) L = diffusivity * asm(laplace, basis) M = asm(mass, basis) dt = .01 print(f'dt = {dt} µs') theta = 0.5 # Crank–Nicolson A = M + theta * L * dt B = M - (1 - theta) * L * dt boundary = basis.find_dofs() interior = basis.complement_dofs(boundary) # transpose as splu prefers CSC backsolve = splu(condense(A, D=boundary, expand=False).T).solve u_init = (np.cos(np.pi * mesh.p[0, :] / 2 / halfwidth[0]) * np.cos(np.pi * mesh.p[1, :] / 2 / halfwidth[1])) def step(t: float, u: np.ndarray) -> Tuple[float, np.ndarray]: u_new = np.zeros_like(u) # zero Dirichlet conditions _, b1 = condense( csr_matrix(A.shape), # ignore condensed matrix B @ u, u_new, D=boundary, expand=False) u_new[interior] = backsolve(b1) return t + dt, u_new
def vp_RR_He(problem_data, rho_method): ###################### ## IMPORT LIBRARIES ## ###################### #Math libraries import numpy as np from scipy.sparse import csc_matrix from scipy.sparse import csr_matrix from scipy.sparse import linalg #Timing import time #Import data from Data.read_fclib import * #Plot residuals from Solver.ADMM_iteration.Numerics.plot import * #Initial penalty parameter import Solver.Rho.Optimal #Max iterations and kind of tolerance from Solver.Tolerance.iter_totaltolerance import * #Acceleration from Solver.Acceleration.plusr_vp import * #Varying penalty parameter from Solver.Rho.Varying.He import * #b = Es matrix from Data.Es_matrix import * #Projection onto second order cone from Solver.ADMM_iteration.Numerics.projection import * ################################## ############# REQUIRE ############ ################################## start = time.clock() problem = hdf5_file(problem_data) M = problem.M.tocsc() f = problem.f A = csc_matrix.transpose(problem.H.tocsc()) A_T = csr_matrix.transpose(A) w = problem.w mu = problem.mu #Dimensions (normal,tangential,tangential) dim1 = 3 dim2 = np.shape(w)[0] #Problem size n = np.shape(M)[0] p = np.shape(w)[0] b = [Es_matrix(w, mu, np.zeros([ p, ]))] ################################# ############# SET-UP ############ ################################# #Set-up of vectors v = [np.zeros([ n, ])] u = [ np.zeros([ p, ]) ] #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])] u_hat = [np.zeros([ p, ])] #u_hat[0] #in the notation of the paper this used with a underline xi = [np.zeros([ p, ])] xi_hat = [np.zeros([ p, ])] r = [np.zeros([ p, ])] #primal residual s = [np.zeros([ p, ])] #dual residual r_norm = [0] s_norm = [0] tau = [1] #over-relaxation e = [] #restart rho = [] #Optimal penalty parameter rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)' rh = eval(rho_string) rho.append(rh) #rho[0] #Plot rho_plot = [] r_plot = [] s_plot = [] b_plot = [] u_bin_plot = [] xi_bin_plot = [] siconos_plot = [] ################ ## ITERATIONS ## ################ for j in range(200): print j len_u = len(u) - 1 for k in range(len_u, MAXITER): #Super LU factorization of M + rho * dot(M_T,M) if rho[k] != rho[k - 1] or k == len_u: #rho[k] != rho[k-1] or P = M + rho[k] * csc_matrix.dot(A_T, A) LU = linalg.splu(P) LU_old = LU else: LU = LU_old ################ ## v - update ## ################ RHS = -f + rho[k] * csc_matrix.dot( A_T, -w - b[j] - xi_hat[k] + u_hat[k]) v.append(LU.solve(RHS)) #v[k+1] ################ ## u - update ## ################ Av = csr_matrix.dot(A, v[k + 1]) vector = Av + xi_hat[k] + w + b[j] u.append(projection(vector, mu, dim1, dim2)) #u[k+1] ######################## ## residuals - update ## ######################## s.append(rho[k] * csc_matrix.dot(A_T, (u[k + 1] - u_hat[k]))) #s[k+1] r.append(Av - u[k + 1] + w + b[j]) #r[k+1] ################# ## xi - update ## ################# ratio = rho[k - 1] / rho[ k] #update of dual scaled variable with new rho xi.append(ratio * (xi_hat[k] + r[k + 1])) #xi[k+1] ################################### ## accelerated ADMM with restart ## ################################### plusr(tau, u, u_hat, xi, xi_hat, k, e, rho, ratio) ################################ ## penalty parameter - update ## ################################ r_norm.append(np.linalg.norm(r[k + 1])) s_norm.append(np.linalg.norm(s[k + 1])) rho.append(penalty(rho[k], r_norm[k + 1], s_norm[k + 1])) #################### ## stop criterion ## #################### pri_evalf = np.amax( np.array([ np.linalg.norm(csr_matrix.dot(A, v[k + 1])), np.linalg.norm(u[k + 1]), np.linalg.norm(w + b[j]) ])) eps_pri = np.sqrt(p) * ABSTOL + RELTOL * pri_evalf dual_evalf = np.linalg.norm(rho[k] * csc_matrix.dot(A_T, xi[k + 1])) eps_dual = np.sqrt(n) * ABSTOL + RELTOL * dual_evalf R = -rho[k] * xi[k + 1] N1 = csc_matrix.dot(M, v[k + 1]) - csc_matrix.dot(A_T, R) + f N2 = u[k + 1] - projection(u[k + 1] - R, mu, dim1, dim2) N1_norm = np.linalg.norm(N1) N2_norm = np.linalg.norm(N2) siconos_plot.append(np.sqrt(N1_norm**2 + N2_norm**2)) if r_norm[k + 1] <= eps_pri and s_norm[k + 1] <= eps_dual: #if k == len_u: for element in range(len(u)): #Relative velocity u_proj = projection(u[element], mu, dim1, dim2) u_proj_contact = np.split(u_proj, dim2 / dim1) u_contact = np.split(u[element], dim2 / dim1) u_count = 0.0 for contact in range(dim2 / dim1): #if np.linalg.norm(u_contact[contact] - u_proj_contact[contact]) / np.linalg.norm(u_contact[contact]) < 1e-01: if np.allclose(u_contact[contact], u_proj_contact[contact], rtol=0.1, atol=0.0): u_count += 1.0 u_bin = 100 * u_count / (dim2 / dim1) u_bin_plot.append(u_bin) #Reaction xi_proj = projection(-1 * xi[element], 1 / mu, dim1, dim2) xi_proj_contact = np.split(xi_proj, dim2 / dim1) xi_contact = np.split(-1 * xi[element], dim2 / dim1) xi_count = 0.0 for contact in range(dim2 / dim1): #if np.linalg.norm(xi_contact[contact] - xi_proj_contact[contact]) / np.linalg.norm(xi_contact[contact]) < 1e-01: if np.allclose(xi_contact[contact], xi_proj_contact[contact], rtol=0.1, atol=0.0): xi_count += 1.0 xi_bin = 100 * xi_count / (dim2 / dim1) xi_bin_plot.append(xi_bin) for element in range(len(r_norm)): rho_plot.append(rho[element]) r_plot.append(r_norm[element]) s_plot.append(s_norm[element]) b_plot.append(np.linalg.norm(b[j])) #print 'First contact' #print -rho[k]*xi[k+1][:3] #uy = projection(-rho[k]*xi[k+1],1/mu,dim1,dim2) #print uy[:3] #print 'Last contact' #print -rho[k]*xi[k+1][-3:] #print uy[-3:] #R = -rho[k]*xi[k+1] #N1 = csc_matrix.dot(M, v[k+1]) - csc_matrix.dot(A_T, R) + f #N2 = u[k+1] - projection(u[k+1] - R, mu, dim1, dim2) #N1_norm = np.linalg.norm(N1) #N2_norm = np.linalg.norm(N2) #print np.sqrt( N1_norm**2 + N2_norm**2 ) print b_plot[-1] print b[-1][:3] print b[-1][-3:] break #end rutine #b(s) stop criterion b.append(Es_matrix(w, mu, Av + w)) if j == 0: pass else: b_per_contact_j1 = np.split(b[j + 1], dim2 / dim1) b_per_contact_j0 = np.split(b[j], dim2 / dim1) count = 0 for i in range(dim2 / dim1): if np.linalg.norm(b_per_contact_j1[i] - b_per_contact_j0[i]) / np.linalg.norm( b_per_contact_j0[i]) > 1e-03: count += 1 if count < 1: break v = [np.zeros([ n, ])] u = [ np.zeros([ p, ]) ] #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])] u_hat = [np.zeros([ p, ])] #u_hat[0] #in the notation of the paper this used with a underline xi = [np.zeros([ p, ])] xi_hat = [np.zeros([ p, ])] r = [np.zeros([ p, ])] #primal residual s = [np.zeros([ p, ])] #dual residual r_norm = [0] s_norm = [0] tau = [1] #over-relaxation e = [] #restart rho = [rho[-1]] end = time.clock() time = end - start #################### ## REPORTING DATA ## #################### f, axarr = plt.subplots(5, sharex=True) f.suptitle('External update with vp_RR_He (Di Cairano)') axarr[0].semilogy(b_plot) axarr[0].set(ylabel='||Phi(s)||') axarr[1].plot(rho_plot) axarr[1].set(ylabel='Rho') axarr[2].semilogy(r_plot, label='||r||') axarr[2].semilogy(s_plot, label='||s||') axarr[2].legend() axarr[2].set(ylabel='Residuals') axarr[3].semilogy(siconos_plot) axarr[3].set(ylabel='SICONOS error') axarr[4].plot(u_bin_plot, label='u in K*') axarr[4].plot(xi_bin_plot, label='-xi in K') axarr[4].legend() axarr[4].set(xlabel='Iteration', ylabel='Projection (%)') plt.show() print 'Total time: ', time return time
def _steadystate_direct_sparse(L, ss_args): """ Direct solver that uses scipy sparse matrices """ if settings.debug: logger.debug('Starting direct LU solver.') dims = L.dims[0] n = int(np.sqrt(L.shape[0])) b = np.zeros(n ** 2, dtype=complex) b[0] = ss_args['weight'] if ss_args['solver'] == 'mkl': has_mkl = 1 else: has_mkl = 0 ss_lu_liouv_list = _steadystate_LU_liouvillian(L, ss_args, has_mkl) L, perm, perm2, rev_perm, ss_args = ss_lu_liouv_list if np.any(perm): b = b[np.ix_(perm,)] if np.any(perm2): b = b[np.ix_(perm2,)] if ss_args['solver'] == 'scipy': ss_args['info']['permc_spec'] = ss_args['permc_spec'] ss_args['info']['drop_tol'] = ss_args['drop_tol'] ss_args['info']['diag_pivot_thresh'] = ss_args['diag_pivot_thresh'] ss_args['info']['fill_factor'] = ss_args['fill_factor'] ss_args['info']['ILU_MILU'] = ss_args['ILU_MILU'] if not ss_args['solver'] == 'mkl': # Use superLU solver orig_nnz = L.nnz _direct_start = time.time() lu = splu(L, permc_spec=ss_args['permc_spec'], diag_pivot_thresh=ss_args['diag_pivot_thresh'], options=dict(ILU_MILU=ss_args['ILU_MILU'])) v = lu.solve(b) _direct_end = time.time() ss_args['info']['solution_time'] = _direct_end - _direct_start if (settings.debug or ss_args['return_info']): L_nnz = lu.L.nnz U_nnz = lu.U.nnz ss_args['info']['l_nnz'] = L_nnz ss_args['info']['u_nnz'] = U_nnz ss_args['info']['lu_fill_factor'] = (L_nnz + U_nnz)/L.nnz if settings.debug: logger.debug('L NNZ: %i ; U NNZ: %i' % (L_nnz, U_nnz)) logger.debug('Fill factor: %f' % ((L_nnz + U_nnz)/orig_nnz)) else: # Use MKL solver if len(ss_args['info']['perm']) != 0: in_perm = np.arange(n**2, dtype=np.int32) else: in_perm = None _direct_start = time.time() v = mkl_spsolve(L, b, perm=in_perm, verbose=ss_args['verbose'], max_iter_refine=ss_args['max_iter_refine'], scaling_vectors=ss_args['scaling_vectors'], weighted_matching=ss_args['weighted_matching']) _direct_end = time.time() ss_args['info']['solution_time'] = _direct_end-_direct_start if ss_args['return_info']: ss_args['info']['residual_norm'] = la.norm(b - L*v, np.inf) ss_args['info']['max_iter_refine'] = ss_args['max_iter_refine'] ss_args['info']['scaling_vectors'] = ss_args['scaling_vectors'] ss_args['info']['weighted_matching'] = ss_args['weighted_matching'] if ss_args['use_rcm']: v = v[np.ix_(rev_perm,)] data = dense2D_to_fastcsr_fmode(vec2mat(v), n, n) data = 0.5 * (data + data.H) if ss_args['return_info']: return Qobj(data, dims=dims, isherm=True), ss_args['info'] else: return Qobj(data, dims=dims, isherm=True)
def gev_sparse(obj,A,B): r""" :param obj: object of the class ``BrakeClass`` :param A: ``A x = lamda B x`` :param B: ``A x = lamda B x`` :return: ``la`` - eigenvalues, ``v`` - eigenvectors :raises: ``SOLVER_BadInputError``, When the matrix A, B are not all in sparse format Example:: . """ LOG_LEVEL = obj.log_level logger_t = obj.logger_t logger_i = obj.logger_i evs_per_shift = obj.evs_per_shift if(sparse.issparse(A) & sparse.issparse(B)): ''' Example Implementation of the QEVP #-----Creating matrix A and B n = 2*M.shape[0] begin_create = timeit.default_timer() I = sparse.identity(n, dtype=complex, format='csr') Z = I-I A = sparse.block_diag((K,I)) B = vstack([hstack([-1*C,-1*M]),hstack([I,Z])]) end_create = timeit.default_timer() #logger_t.info("\n"+"\n"+'Creating A and B for GEV: '+"%.2f" % \ #(end_create-begin_create)+' sec') ''' A = A.tocsc() # warn('splu requires CSC matrix format', SparseEfficiencyWarning) n = A.shape[0] #Computing the LU factors of Sparse A try: begin_LU = timeit.default_timer() LUfactors = splu(A, permc_spec=None, diag_pivot_thresh=None, drop_tol=None, relax=None, panel_size=None, options={}) ''' #LUfactors = spilu(A, drop_tol=None, fill_factor=None, drop_rule=None, permc_spec=None, diag_pivot_thresh=None, relax=None, panel_size=None, options=None) ''' end_LU = timeit.default_timer() logger_t.info('SOLVER: LU Factorization of A in time : '+"%.2f" % \ (end_LU-begin_LU)+' sec') except RuntimeError: raise SOLVER_BadInputError('The matrix A is sparse but singular' ) # Operator defining the matrix vector product A*x # which is passed to eigs(python inbuilt solver) for calculating the eigenvalues def mv(x): y = B.dot(x) z = LUfactors.solve(y) return z Aoperator = LinearOperator((n,n),matvec=mv) # computation begin_eigen_solve = timeit.default_timer() la,v = eigs(Aoperator, k=evs_per_shift, M=None, sigma=None, which='LM') end_eigen_solve = timeit.default_timer() logger_t.info('SOLVER: Call to python eigs command : '+"%.2f" % \ (end_eigen_solve-begin_eigen_solve)+' sec') #Inverting the obtained eigen values for i in range(0, la.shape[0]): x1 = la[i].real x2 = la[i].imag la[i] = complex(x1,-x2)/(x1*x1+x2*x2) else: raise SOLVER_BadInputError('The matrix A, B are not all in sparse format') return la, v;
RHS.pack(u_rhs, h_rhs, c_rhs) # Setup outputs file_num = 1 if not os.path.exists(output_folder): os.mkdir(output_folder) t = 0 # Combine matrices and perform LU decompositions for constant timestep for m in range(m_start, m_end + 1): md = m - m_start Pmd = M[md] + dt * L[md] if STORE_LU: P[md] = spla.splu(Pmd.tocsc(), permc_spec=PERMC_SPEC) else: P[md] = Pmd start_time = time.time() # Main loop for i in range(n_iterations): if i % n_output == 0: state_vector.unpack(u, h, c) # for m in range(m_start,m_end+1): # md = m - m_start # (start_index,end_index,spin) = S.tensor_index(m,1) # om['c'][md] = 1j*(S.op('k-',m,1).dot(u['c'][md][start_index[0]:end_index[0]]) - S.op('k+',m,-1).dot(u['c'][md][start_index[1]:end_index[1]]))
if verbose: data_dir = './' import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.spy(solver.pencils[0].L, markersize=1, markeredgewidth=0.0) fig.savefig(data_dir + "sparsity_pattern.png", dpi=1200) import scipy.sparse.linalg as sla perm_spec_set = [ 'NATURAL', 'COLAMD', 'MMD_ATA', 'MMD_AT_plus_A' ] for perm_spec in perm_spec_set: LU = sla.splu(solver.pencils[0].LHS.tocsc(), permc_spec=perm_spec) fig = plt.figure() ax = fig.add_subplot(1, 2, 1) ax.spy(LU.L.A, markersize=1, markeredgewidth=0.0) ax = fig.add_subplot(1, 2, 2) ax.spy(LU.U.A, markersize=1, markeredgewidth=0.0) fig.savefig(data_dir + "sparsity_pattern_LU_{}.png".format(perm_spec), dpi=1200) logger.info("------- {} -------".format(perm_spec)) logger.info("{} nonzero entries in LU".format(LU.nnz)) logger.info("{} nonzero entries in LHS".format( solver.pencils[0].LHS.tocsc().nnz)) logger.info("{} fill in factor".format( LU.nnz / solver.pencils[0].LHS.tocsc().nnz))
def solve_linear(self, d_outputs, d_residuals, mode): print('----------run___solve_linear') linear_solver_ = self.options['linear_solver_'] pde_problem = self.options['pde_problem'] state_name = self.options['state_name'] state_function = pde_problem.states_dict[state_name]['function'] residual_form = pde_problem.states_dict[state_name]['residual_form'] if state_name == 'density': print('this is a variational density filter') A, _ = df.assemble_system(self.derivative_form, -residual_form) else: A, _ = df.assemble_system(self.derivative_form, -residual_form, pde_problem.bcs_list) def report(xk): frame = inspect.currentframe().f_back # print(frame.f_locals['resid']) if linear_solver_ == 'fenics_direct': rhs_ = df.Function(state_function.function_space()) dR = df.Function(state_function.function_space()) rhs_.vector().set_local(d_outputs[state_name]) if state_name != 'density': for bc in pde_problem.bcs_list: bc.apply(A) Am = df.as_backend_type(A).mat() ATm = Am.transpose() AT = df.PETScMatrix(ATm) df.solve(AT, dR.vector(), rhs_.vector()) d_residuals[state_name] = dR.vector().get_local() elif linear_solver_ == 'scipy_splu': if state_name != 'density': for bc in pde_problem.bcs_list: bc.apply(A) Am = df.as_backend_type(A).mat() ATm = Am.transpose() ATm_csr = csr_matrix(ATm.getValuesCSR()[::-1], shape=Am.size) lu = splu(ATm_csr.tocsc()) d_residuals[state_name] = lu.solve(d_outputs[state_name], trans='T') elif linear_solver_ == 'scipy_cg': if state_name != 'density': for bc in pde_problem.bcs_list: bc.apply(A) Am = df.as_backend_type(A).mat() ATm = Am.transpose() ATm_csr = csr_matrix(ATm.getValuesCSR()[::-1], shape=Am.size) # lu = splu(ATm_csr.tocsc()) b = d_outputs[state_name] x, info = splinalg.cg(ATm_csr, b, tol=1e-8, callback=report) print('the residual is:') print(info) d_residuals[state_name] = x elif linear_solver_ == 'fenics_krylov': rhs_ = df.Function(state_function.function_space()) dR = df.Function(state_function.function_space()) rhs_.vector().set_local(d_outputs[state_name]) if state_name != 'density': for bc in pde_problem.bcs_list: bc.apply(A) # for bc in pde_problem.bcs_list: # bc.apply(A) Am = df.as_backend_type(A).mat() ATm = Am.transpose() AT = df.PETScMatrix(ATm) solver = df.KrylovSolver('gmres', 'ilu') prm = solver.parameters prm["maximum_iterations"] = 1000000 prm["divergence_limit"] = 1e2 solver.solve(AT, dR.vector(), rhs_.vector()) # print('solve'+iter) d_residuals[state_name] = dR.vector().get_local() elif linear_solver_ == 'petsc_gmres_ilu': ksp = PETSc.KSP().create() ksp.setType(PETSc.KSP.Type.GMRES) ksp.setTolerances(rtol=5e-17) if state_name != 'density': for bc in pde_problem.bcs_list: bc.apply(A) Am = df.as_backend_type(A).mat() ksp.setOperators(Am) ksp.setFromOptions() pc = ksp.getPC() pc.setType("lu") size = state_function.function_space().dim() dR = PETSc.Vec().create() dR.setSizes(size) dR.setType('seq') dR.setValues(range(size), d_residuals[state_name]) dR.setUp() du = PETSc.Vec().create() du.setSizes(size) du.setType('seq') du.setValues(range(size), d_outputs[state_name]) du.setUp() if mode == 'fwd': ksp.solve(dR, du) d_outputs[state_name] = du.getValues(range(size)) else: ksp.solveTranspose(du, dR) d_residuals[state_name] = dR.getValues(range(size)) elif linear_solver_ == 'petsc_cg_ilu': ksp = PETSc.KSP().create() ksp.setType(PETSc.KSP.Type.CG) ksp.setTolerances(rtol=5e-15) if state_name != 'density': for bc in pde_problem.bcs_list: bc.apply(A) Am = df.as_backend_type(A).mat() ksp.setOperators(Am) ksp.setFromOptions() pc = ksp.getPC() pc.setType("lu") size = state_function.function_space().dim() dR = PETSc.Vec().create() dR.setSizes(size) dR.setType('seq') dR.setValues(range(size), d_residuals[state_name]) dR.setUp() du = PETSc.Vec().create() du.setSizes(size) du.setType('seq') du.setValues(range(size), d_outputs[state_name]) du.setUp() if mode == 'fwd': ksp.solve(dR, du) d_outputs[state_name] = du.getValues(range(size)) else: ksp.solveTranspose(du, dR) d_residuals[state_name] = dR.getValues(range(size)) print('----------finish_____run___solve_linear')
def logdet_sp(P): LU = spla.splu(P) logdet = np.log(np.abs(LU.U.diagonal())).sum() + np.log( np.abs(LU.L.diagonal())).sum() #logdet = np.linalg.slogdet(P.toarray())[1] return logdet
def qev_sparse(obj,M,C,K): r""" :param obj: object of the class ``BrakeClass`` :param M: Mass Matrix :param C: Damping Matrix :param K: Stiffness Matrix :return: ``la`` - eigenvalues, ``v`` - eigenvectors :raises: ``SOLVER_BadInputError``, When the matrix M,C,K are not all in sparse format Example:: . """ LOG_LEVEL = obj.log_level logger_t = obj.logger_t logger_i = obj.logger_i evs_per_shift = obj.evs_per_shift if(sparse.issparse(M) & sparse.issparse(C) & sparse.issparse(K)): K = K.tocsc() #Computing the LU factors of Sparse K try: begin_LU = timeit.default_timer() LUfactors = splu(K, permc_spec=None, diag_pivot_thresh=None, drop_tol=None, relax=None, panel_size=None, options={}) ''' LUfactors = spilu(K, permc_spec=None, diag_pivot_thresh=None, drop_tol=None, relax=None, panel_size=None, options={}) LUfactors = factorized(K) ''' end_LU = timeit.default_timer() logger_t.info("\n"+"\n"+'SOLVER: LU Factorization of sparse K using splu in time : '\ +"%.2f" % (end_LU-begin_LU)+' sec') except RuntimeError: raise SOLVER_BadInputError('The matrix K is sparse but singular') n = M.shape[0] # Operator defining the matrix vector product A*x # which is passed to eigs(python inbuilt solver) for calculating the eigenvalues def mv(x): y1 = LUfactors.solve(-C.dot(x[0:n])-M.dot(x[n:2*n])) y2 = x[0:n] y= np.hstack((y1,y2)) return y Aoperator = LinearOperator((2*n,2*n),matvec=mv) # computation begin_eigen_solve = timeit.default_timer() la,v = eigs(Aoperator, k=evs_per_shift, M=None, sigma=None, which='LM') end_eigen_solve = timeit.default_timer() logger_t.info('SOLVER: Call to python eigs command: '+"%.2f" % \ (end_eigen_solve-begin_eigen_solve)+' sec') #Inverting the obtained eigen values for i in range(0, la.shape[0]): x1 = la[i].real x2 = la[i].imag la[i] = complex(x1,-x2)/(x1*x1+x2*x2) else: raise SOLVER_BadInputError('The matrix M,C,K are not all in sparse format') return la, v;
def curl(self, ell, rank, data_in, data_out): data_dtype = data_in.dtype if STORE_LU_TRANSFORM: i_LU = ell - self.ell_min if not self.LU_curl_initialized[i_LU][rank]: logger.debug("LU_curl not initialized l={},rank={}".format( ell, rank)) self.LU_curl[i_LU][rank] = [None] * (3) N = self.N_max - self.N_min(ell - self.R_max) xim = self.xi(-1, ell) xip = self.xi(+1, ell) if ell >= 1: Cm = self.op('E', N, 0, ell - 1, data_dtype) Dm = self.op('D-', N, 0, ell, data_dtype) if STORE_LU_TRANSFORM: index = 0 if not self.LU_curl_initialized[ell][rank]: self.LU_curl[i_LU][rank][index] = spla.splu(Cm) data_out[:N + 1] = self.LU_curl[i_LU][rank][index].solve( -1j * xip * Dm.dot(data_in[(N + 1):2 * (N + 1)])) else: data_out[:N + 1] = spla.spsolve( Cm, -1j * xip * Dm.dot(data_in[(N + 1):2 * (N + 1)])) else: data_out[:N + 1] = 0. C0 = self.op('E', N, 0, ell, data_dtype) Dm = self.op('D-', N, 0, ell + 1, data_dtype) if STORE_LU_TRANSFORM: index = 1 if not self.LU_curl_initialized[ell][rank]: self.LU_curl[i_LU][rank][index] = spla.splu(C0) if ell >= 1: Dp = self.op('D+', N, 0, ell - 1, data_dtype) if STORE_LU_TRANSFORM: data_out[(N + 1):2 * (N + 1)] = self.LU_curl[i_LU][rank][index].solve( 1j * xim * Dm.dot(data_in[2 * (N + 1):]) - 1j * xip * Dp.dot(data_in[:(N + 1)])) else: data_out[(N + 1):2 * (N + 1)] = spla.spsolve( C0, 1j * xim * Dm.dot(data_in[2 * (N + 1):]) - 1j * xip * Dp.dot(data_in[:(N + 1)])) else: if STORE_LU_TRANSFORM: data_out[(N + 1):2 * (N + 1)] = self.LU_curl[i_LU][rank][index].solve( 1j * xim * Dm.dot(data_in[2 * (N + 1):])) else: data_out[(N + 1):2 * (N + 1)] = spla.spsolve( C0, 1j * xim * Dm.dot(data_in[2 * (N + 1):])) Cp = self.op('E', N, 0, ell + 1, data_dtype) Dp = self.op('D+', N, 0, ell, data_dtype) if STORE_LU_TRANSFORM: index = 2 if not self.LU_curl_initialized[ell][rank]: self.LU_curl[i_LU][rank][index] = spla.splu(Cp) if STORE_LU_TRANSFORM: data_out[2 * (N + 1):] = self.LU_curl[i_LU][rank][index].solve( 1j * xim * Dp.dot(data_in[(N + 1):2 * (N + 1)])) else: data_out[2 * (N + 1):] = spla.spsolve( Cp, 1j * xim * Dp.dot(data_in[(N + 1):2 * (N + 1)])) if STORE_LU_TRANSFORM and not self.LU_curl_initialized[i_LU][rank]: self.LU_curl_initialized[i_LU][rank] = True