def mapNtoDD_left_s0(lo, ld, g, c, s0=()): no = lo.n nd = ld.n Kpd = ly.layerpotSD(s=ld) Kpo = ly.layerpotSD(s=lo) Kpd[np.diag_indices(nd)] = Kpd[np.diag_indices(nd)] + 0.5 * c Kpo[np.diag_indices(no)] = Kpo[np.diag_indices(no)] + 0.5 Kd2o = ly.layerpotSD(s=ld, t=lo) Ko2d = ly.layerpotSD(s=lo, t=ld) row1 = np.concatenate((Kpo.T, Kd2o.T)).T row2 = np.concatenate((Ko2d.T, Kpd.T)).T # row1 = lo.SL.T.dot(row1) Ks = np.concatenate((row1, row2)) if g.ndim == 1: gz = np.concatenate((lo.SL.T.dot(g), np.zeros(nd))) elif g.ndim == 2: nt = g.shape[1] gz = np.concatenate((lo.SL.T.dot(g), np.zeros((nd, nt)))) if setups.mapNtoD_left == False: if g.ndim == 1: gz = np.concatenate((g, np.zeros(nd))) elif g.ndim == 2: nt = g.shape[1] gz = np.concatenate((g, np.zeros((nd, nt)))) gz[0] = 0 Ks[0, :no] = lo.s0 Ks[0, no:] = 0 phi = linalg.lstsq(Ks, gz)[0] # (lu, piv) = linalg.lu_factor(Ks) # phi = linalg.lu_solve((lu, piv), gz) return phi
def mapNtoDD0(lo, ld, g, c, s0): no = lo.n nd = ld.n # --- matrix: shape = (lo.n + ld.n, lo.n + ld.n) --- Kpd = ly.layerpotSD(s=ld) Kpo = ly.layerpotSD(s=lo) Kpd[np.diag_indices(nd)] = Kpd[np.diag_indices(nd)] + 0.5 * c Kpo[np.diag_indices(no)] = Kpo[np.diag_indices(no)] + 0.5 Kd2o = ly.layerpotSD(s=ld, t=lo) Ko2d = ly.layerpotSD(s=lo, t=ld) row1 = np.concatenate((Kpo.T, Kd2o.T)).T row2 = np.concatenate((Ko2d.T, Kpd.T)).T Ks = np.concatenate((row1, row2)) if verbose: print(dbgg.CYAN, end='') print(' condition num = %.5e' % numpy.linalg.cond(np.array(Ks, float))) print(' determninant= ', numpy.linalg.det(np.array(Ks, float))) print(dbgg.RESET, end='') # --- rhs: [g, 0] with shape = (lo.n + ld.n) --- if g.ndim == 1: gz = np.concatenate((g, np.zeros(nd))) elif g.ndim == 2: nt = g.shape[1] gz = np.concatenate((g, np.zeros((nd, nt)))) # --- linear system resolution : () * phi = g --- phi = linalg.lstsq(Ks, gz)[0] return phi
def mapNtoDD_correctedinfirst(lo, ld, g, c, s0): no = lo.n nd = ld.n Kpd = ly.layerpotSD(s=ld) Kpo = ly.layerpotSD(s=lo) Kpd[np.diag_indices(nd)] = Kpd[np.diag_indices(nd)] + 0.5 * c Kpo[np.diag_indices(no)] = Kpo[np.diag_indices(no)] + 0.5 Kd2o = ly.layerpotSD(s=ld, t=lo) Ko2d = ly.layerpotSD(s=lo, t=ld) row1 = np.concatenate((Kpo.T, Kd2o.T)).T row2 = np.concatenate((Ko2d.T, Kpd.T)).T Ks = np.concatenate((row1, row2)) if verbose: print('mapNtoD condition num = %.5e' % numpy.linalg.cond(np.array(Ks, float))) print('mapNtoD determninant= ', numpy.linalg.det(np.array(Ks, float))) if g.ndim == 1: gz = np.concatenate((g, np.zeros(nd))) elif g.ndim == 2: nt = g.shape[1] gz = np.concatenate((g, np.zeros((nd, nt)))) mean_reduced = lo.w[1:].dot(gz[1:no]) / lo.w[0] gz[0] = -mean_reduced phi = linalg.lstsq(Ks, gz)[0] # (lu, piv) = linalg.lu_factor(Ks) # phi = linalg.lu_solve((lu, piv), gz) return phi
def mapNtoD(lo, ld, g, c, s0=()): print("!! not revisioned !!") # --------------------------------------------------- # --- building matrix --- no = lo.n nd = ld.n Kpd = ly.layerpotSD(s=ld) Kpo = ly.layerpotSD(s=lo) Kpd[np.diag_indices(nd)] = Kpd[np.diag_indices(nd)] + 0.5 * c Kpo[np.diag_indices(no)] = Kpo[np.diag_indices(no)] + 0.5 Kd2o = ly.layerpotSD(s=ld, t=lo) Ko2d = ly.layerpotSD(s=lo, t=ld) # --------------------------------------------------- if s0 == (): s0 = np.ones(no) S = linf.gramschmidt(s0=s0) Kpo = Kpo.dot(S) Ko2d = Ko2d.dot(S) row1 = np.concatenate((Kpo.T, Kd2o.T)).T row2 = np.concatenate((Ko2d.T, Kpd.T)).T Ks = np.concatenate((lo.SL.T.dot(row1), row2)) # Ks = np.concatenate(( row1, row2 )) Ks1 = Ks[1::, 1::] if verbose: print(' not true matrix (to fix)') print(dbgg.CYAN, end='') print(' condition num = %.5e' % numpy.linalg.cond(np.array(Ks, float))) print(' determinant = %.5e' % numpy.linalg.det(np.array(Ks, float))) print(dbgg.RESET, end='') (lu, piv) = linalg.lu_factor(Ks1) if g.ndim == 1: gs = np.concatenate((lo.SL.T.dot(g), np.zeros(nd))) # gs = np.concatenate(( g, np.zeros(nd) )) gs1 = gs[1::] phi1 = linalg.lu_solve((lu, piv), gs1) if verbose: print(' residual = ', numpy.linalg.norm(Ks1.dot(phi1) - gs1)) print(' residual2 = ', numpy.linalg.norm(row2[:, 1::].dot(phi1) - gs1[-nd::])) phi = np.concatenate(([0], phi1)) elif g.ndim == 2: nt = g.shape[1] gs = np.concatenate((lo.SL.T.dot(g), np.zeros((nd, nt)))) # gs = np.concatenate(( g, np.zeros((nd, nt)) )) gs1 = gs[1::] # gs2t = gs2.T # phi2 = np.empty((nt, no + nd - 1)) # for k in range(nt): # phi2[k] = linalg.lu_solve((lu, piv), gs2t[k]) # time.sleep(0.001) # phi2 = phi2.T phi1 = linalg.lu_solve((lu, piv), gs1) phi = np.concatenate((np.zeros((1, nt)), phi1)) else: print('Error dimensions for gs1 in mapNtoD') return np.concatenate((S.dot(phi[0:no]), phi[no::]))
def __init__(self, pieces=[], more=0): self.pc = pieces self.s = pieces self.n = sum([len(pk.x) for pk in self.pc]) self.x = np.array([z for p in pieces for z in p.x]) self.nx = np.array([nx for p in pieces for nx in p.nx]) self.speed = np.array([sp for p in pieces for sp in p.speed]) * len(pieces) self.kappa = np.array([k for p in pieces for k in p.kappa]) self.w = np.array([w for p in pieces for w in p.w]) self.condc = np.array([c for p in pieces for c in p.condc]) # else: # no itial position # self.n = sum([len(pk.x) for pk in self.pc]) - len(pieces) # self.x = np.array([z for p in pieces for z in p.x]) # self.nx = np.array([nx for p in pieces for nx in p.nx]) # self.speed = np.array([sp for p in pieces for sp in p.speed]) # self.kappa = np.array([k for p in pieces for k in p.kappa]) # self.w = np.array([w for p in pieces for w in p.w]) # # miscellaneous if more: print("# Boundary.__init__ : more = 1") K = ly.layerpotSD(s=self) nu, s0 = linf.eigmaxpower(K) self.s0 = s0 self.S = linf.gramschmidt(s0=s0) # self.Sw = linf.gramschmidtw(s=self, s0=s0) self.SL = linf.gramschmidt(s0=self.w) # self.B = linf.gramschmidtw(s=self, s0=np.ones(self.n)) self.B = linf.gramschmidt(s0=np.ones(self.n)) self.BX, self.BXinv = get_Basis(self.n) self.BY, self.BYinv = get_Basis(self.n)
def mapNtoD0_left_s0(l, g, s0=()): ''' This function computes interior laplace problem with neumann data - l: segment or boundary of the region where there are data - g: neumann data (Kp + 0.5 I) * phi = g ''' n = l.n # --- matrix --- Kp = ly.layerpotSD(s=l) Kp[np.diag_indices(n)] = Kp[np.diag_indices(n)] + 0.5 if verbose: print(dbgg.CYAN, end='') print(' condition num = %.5e' % numpy.linalg.cond(np.array(Kp, float))) print(' determinant = %.5e' % numpy.linalg.det(np.array(Kp, float))) print(dbgg.RESET, end='') Kps = l.SL.T.dot(Kp) # --- rhs --- gs = l.SL.T.dot(g) # --- correction s0 in first line --- gs[0] = 0 Kps[0, :] = l.s0 # potrebbe essere anche l.w ? vedi vecchio func_left in vecchio script # --- linear system resolution : (Kp + 0.5 I) * phi = g --- phi = linalg.lstsq(Kps, gs)[0] if verbose and gs.ndim == 1: print(' residual', max(abs(np.array(Kps.dot(phi) - gs)))) return phi
def mapNtoD0_left(l, g, s0=()): ''' This function computes interior laplace problem with neumann data - l: segment or boundary of the region where there are data - g: neumann data (Kp + 0.5 I) * phi = g ''' n = l.n # --- matrix --- Kp = ly.layerpotSD(s=l) Kp[np.diag_indices(n)] = Kp[np.diag_indices(n)] + 0.5 if verbose: print(dbgg.CYAN, end='') print(' condition num = %.5e' % numpy.linalg.cond(np.array(Kp, float))) print(' determinant = %.5e' % numpy.linalg.det(np.array(Kp, float))) print(dbgg.RESET, end='') Kps = l.SL.T.dot(Kp) # --- rhs --- gs = l.SL.T.dot(g) # --- linear system resolution : (Kp + 0.5 I) * phi = g --- phi = linalg.lstsq(Kps, gs)[0] print(" g ndim =", gs.shape) if verbose and gs.ndim == 1: print(' residual', max(abs(np.array(Kps.dot(phi) - gs)))) return phi
def mapNtoD00(l, g, s0=()): ''' This function computes interior laplace problem with neumann data - l: segment or boundary of the region where there are data - g: neumann data (Kp + 0.5 I) * phi = g ''' n = l.n # --- matrix --- Kp = ly.layerpotSD(s=l) Kp[np.diag_indices(n)] = Kp[np.diag_indices(n)] + 0.5 if verbose: print(dbgg.CYAN, end='') print(' condition num = %.5e' % numpy.linalg.cond(np.array(Kp, float))) print(' determinant = %.5e' % numpy.linalg.det(np.array(Kp, float))) print(dbgg.RESET, end='') # --- rhs --- # --- linear system resolution : (Kp + 0.5 I) * phi = g --- phi = linalg.lstsq(Kp, g)[0] # (lu, piv) = linalg.lu_factor(Kp) # phi = linalg.lu_solve((lu, piv), g) return phi
def mapNtoD0(l, g, s0=()): print("!! not revisioned !!") print(g.shape) # --------------------------------------------------- # --- building matrix --- n = l.n Kp = ly.layerpotSD(s=l) Kp[np.diag_indices(n)] = Kp[np.diag_indices(n)] + 0.5 # --------------------------------------------------- if verbose: print(dbgg.CYAN, end='') print(' condition num = %.5e' % numpy.linalg.cond(np.array(Kp, float))) print(' determinant = %.5e' % numpy.linalg.det(np.array(Kp, float))) print(dbgg.RESET, end='') if s0 == (): s0 = np.ones(n) S = l.S # Kps = S.T.dot(Kp.dot(S)) Kps = l.SL.T.dot(Kp.dot(S)) Kps1 = Kps[1::, 1::] # gs = S.T.dot(g) gs = l.SL.T.dot(g) gs1 = gs[1::] # phi2 = linalg.solve(Kps2, gs2) (lu, piv) = linalg.lu_factor(Kps1) if gs1.ndim == 1: phi1 = linalg.lu_solve((lu, piv), gs1) # phi1 = linalg.lstsq(Kps1, gs1)[0] phi = np.concatenate(([0], phi1)) if verbose: print('residual = ', numpy.linalg.norm(Kps1.dot(phi1) - gs1)) elif gs1.ndim == 2: nt = g.shape[1] # gs2t = gs2.T # phi2 = np.empty((len(gs2t), n - 1 )) # for k in range(len(gs2t)): # phi2[k] = linalg.lu_solve((lu, piv), gs2t[k]) # time.sleep(0.001) # phi2 = phi2.T phi1 = linalg.lu_solve((lu, piv), gs1) # phi1 = linalg.lstsq(Kps1, gs1)[0] phi = np.concatenate((np.zeros((1, nt)), phi1)) else: print(' Error: dimensions for gs in mapNtoD0') # phi2 = scipy.sparse.linalg.cg(Kps2, gs2)[0] # phi = linalg.solve(Kps, gs) # check error return S.dot(phi)
def computeLLdiff(ld, so, T, c): if T == (): T = so.BX allpsi0 = dpb.mapNtoD0(so, T, so.s0) Lo = ly.layerpotSD(s=so, t=ld) rhsdiff = Lo.dot(allpsi0) allpsi = dpb.mapNtoDdiff(so, ld, rhsdiff, c, so.s0) Lo = ly.layerpotS(s=so) Ld = ly.layerpotS(s=ld, t=so) L = Lo.dot(allpsi[0:so.n]) + Ld.dot(allpsi[so.n::]) # means = sum(np.diagflat(so.w).dot(L)) / sum(so.w) # correct? strange sum by rows means = np.ones(so.n).dot(np.diagflat(so.w).dot(L)) / sum(so.w) L = L - np.array([means for k in range(so.n)]) return L
def computeU(allpsi, ld, so, sb): kerS = ly.layerpotS(s=ld, t=so) kerSD = ly.layerpotSD(s=ld, t=so) U_psi = kerS.dot(allpsi) U_psi_nu = kerSD.dot(allpsi) U_psi = U_psi.T.dot(np.diag(so.w)) U_psi_nu = U_psi_nu.T.dot(np.diag(so.w)) U = ly.layerpotS(s=so, t=sb) U_nu = ly.layerpotD(s=so, t=sb) U = U + U_psi U_nu = U_nu + U_psi_nu return (U, U_nu)
def computeR(allpsi, ld, so, sb, sv=(), testset=1): if sv == (): sv = sb nso, nsb = so.n, sb.n U, U_nu = computeU(allpsi, ld, so, sb) testset = 1 if testset == 1 or testset == 3: V1 = ly.layerpotS(s=sv, t=so) V1_nu = ly.layerpotSD(s=sv, t=so) R1 = U.dot(V1_nu) - U_nu.dot(V1) R = R1 if testset == 2 or testset == 3: V2 = ly.layerpotD(s=sv, t=so) V2_nu = ly.layerpotDD(s=sv, t=so) R2 = U.dot(V2_nu) - U_nu.dot(V2) R = R2 if testset == 3: R = np.concatenate((R1.T, R2.T)).T if verbose: print(' R.shape = ', R.shape) return (R, U, U_nu)
def __init__(self, n, Z_args=((), (), (), ()), f_inargs=((), ()), quad='ps', aff=(0, 1), sign=1, more=1, h=2.0): ''' This constructor initializes these fields - x: (complex = R^2) curve points - dx: (complex = R^2) curve derivative = tangent unit vect * speed - speed: (complex = R^2) curve speed Quadrature - 'p': periodic - 'ps': periodic shifted, meaning all points are shifted by half step - 'gf': graded full (with initial point) ''' # in vecchie versioni c'era una diversa ipotesi di costruttore (vedi parametri) Z, Zp, Zpp, args = Z_args f, inargs = f_inargs if f != () and f != (): (Z, Zp, Zpp, args) = f(*inargs) # ------------------------------------------------------------------- # ------------------- Quadrature: t --------------------------------- if quad == 'p' or quad == 'ps': self.t = np.array([float(k) / n for k in range(n)], float) if quad == 'ps': self.t = self.t + 1. / 2 / n elif quad == 'gp' or quad == 'g' or quad == 'gf': temp_s = np.array([float(k) / n for k in range(n)], float) if quad == 'gf': pass else: # no initial point with w=0 temp_s = temp_s[1:] n = n - 1 # self.t = graded.w(2.0 * pi * temp_s) / 2 / pi self.a = graded.wp(2.0 * pi * temp_s) else: self.t = np.array([float(k) / (n - 1) for k in range(n)], float) # ------------------------------------------------------------------- # trapezoidal nodes self.n = n self.x = np.array([Z(t, *args, aff=aff) for t in self.t], np.cfloat) self.dx = np.array([Zp(t, *args, aff=aff) for t in self.t], np.cfloat) self.speed = np.array(abs(self.dx), float) self.nx = np.array(-1j * self.dx / self.speed, np.cfloat) * sign self.ddx = np.array([Zpp(t, *args, aff=aff) for t in self.t], np.cfloat) # s.kappa = -real(conj(-1i*dZdt).*s.Zpp(s.t)) ./ s.speed.^3; %curvature self.kappa = -np.real(np.conj(-1j * self.dx) * self.ddx) / ( self.speed**3) # signed curvature # ------------------------------------------------------------------- # ------------------- Quadrature: w --------------------------------- if quad == 'p' or quad == 'ps': self.w = np.array([sp / n for sp in self.speed], float) elif quad == 'gp' or quad == 'g' or quad == 'gf': self.speed = self.speed * self.a # self.w = np.array([sp / (n) for sp in self.speed], float) if quad == 'gp' or quad == 'g': self.w = np.array([sp / (n + 1) for sp in self.speed], float) print('graded mesh in segment, n = ', n, ' w.size =', self.w.size) elif quad == 'gf': self.w = np.array([sp / n for sp in self.speed], float) print('graded FULL mesh in segment, n = ', n, ' w.size =', self.w.size) # self.w = np.array([sp / n for sp in self.speed], float) * self.a else: self.w = np.array([sp / (n - 1) for sp in self.speed], float) self.w[0] = self.w[0] * 0.5 self.w[-1] = self.w[-1] * 0.5 # ------------------------------------------------------------------- # ------------------------------------------------------------------- # miscellaneous if more: K = ly.layerpotSD(s=self) nu, s0 = linf.eigmaxpower( K ) # eigenvector with max. modulus eigenvalues: it's density which makes constant simple layer self.s0 = s0 self.S = linf.gramschmidt(s0=s0) # # self.Sw = linf.gramschmidtw(s=self, s0=s0) self.SL = linf.gramschmidt(s0=self.w) # # self.B = linf.gramschmidtw(s=self, s0=np.ones(self.n)) # self.B = linf.gramschmidt(s0=np.ones(self.n)) self.BT = get_basis_trigonometric(self.n) self.BX, self.BXinv = get_Basis(self.n) self.BY, self.BYinv = get_Basis(self.n) # ------------------------------------------------------------------- condc = const_from_conductivity(h) self.condc = np.array([condc for t in self.t], np.cfloat) # ---- save properties ---------------------------------------------- self.name = f_inargs[0] self.params = f_inargs[1] self.aff = aff return # __init__