def u_exact(self, t): """ Routine to compute the exact solution at time t Args: t: current time Returns: exact solution """ sigma_0 = 0.1 k = 7.0 * 2.0 * np.pi x_0 = 0.75 x_1 = 0.25 me = mesh(self.nvars) #me.values[0,:] = 0.5*u_initial(self.mesh - (self.cadv + self.cs)*t) + 0.5*u_initial(self.mesh - (self.cadv - self.cs)*t) #me.values[1,:] = 0.5*u_initial(self.mesh - (self.cadv + self.cs)*t) - 0.5*u_initial(self.mesh - (self.cadv - self.cs)*t) me.values[0, :] = np.exp( -np.square(self.mesh - x_0 - self.cs * t) / (sigma_0 * sigma_0)) + np.exp( -np.square(self.mesh - x_1 - self.cs * t) / (sigma_0 * sigma_0)) * np.cos( k * (self.mesh - self.cs * t) / sigma_0) me.values[1, :] = me.values[0, :] return me
def prolong(self, G): """ Prolongation implementation Args: G: the coarse level data (easier to access than via the coarse attribute) """ if isinstance(G, mesh): F = mesh(self.fine_prob.init, val=0.0) tmpG = np.fft.rfft(G.values) tmpF = np.zeros(self.fine_prob.init // 2 + 1, dtype=np.complex128) halfG = int(self.coarse_prob.init / 2) tmpF[0:halfG] = tmpG[0:halfG] tmpF[-1] = tmpG[-1] F.values[:] = np.fft.irfft(tmpF) * self.ratio elif isinstance(G, rhs_imex_mesh): F = rhs_imex_mesh(G) tmpG_impl = np.fft.rfft(G.impl.values) tmpF_impl = np.zeros(self.fine_prob.init // 2 + 1, dtype=np.complex128) halfG = int(self.coarse_prob.init / 2) tmpF_impl[0:halfG] = tmpG_impl[0:halfG] tmpF_impl[-1] = tmpG_impl[-1] F.impl.values[:] = np.fft.irfft(tmpF_impl) * self.ratio tmpG_expl = np.fft.rfft(G.expl.values) tmpF_expl = np.zeros(self.fine_prob.init // 2 + 1, dtype=np.complex128) halfG = int(self.coarse_prob.init / 2) tmpF_expl[0:halfG] = tmpG_expl[0:halfG] tmpF_expl[-1] = tmpG_expl[-1] F.expl.values[:] = np.fft.irfft(tmpF_expl) * self.ratio else: raise TransferError('Unknown data type, got %s' % type(G)) return F
def prolong(self, G): """ Prolongation implementation Args: G: the coarse level data (easier to access than via the coarse attribute) """ if isinstance(G, mesh): F = mesh(self.fine_prob.init) tmpG = self.fft_object_coarse(G.values) / ( self.coarse_prob.init[0] * self.coarse_prob.init[1]) tmpF = np.zeros(self.fine_prob.init, dtype=np.complex128) halfG = int(self.coarse_prob.init[0] / 2) tmpF[0:halfG, 0:halfG] = tmpG[0:halfG, 0:halfG] tmpF[self.fine_prob.init[0] - halfG:, 0:halfG] = tmpG[halfG:, 0:halfG] tmpF[0:halfG, self.fine_prob.init[0] - halfG:] = tmpG[0:halfG, halfG:] tmpF[self.fine_prob.init[0] - halfG:, self.fine_prob.init[0] - halfG:] = tmpG[halfG:, halfG:] F.values[:] = np.real( self.ifft_object_fine(tmpF, normalise_idft=False)) elif isinstance(G, rhs_imex_mesh): F = rhs_imex_mesh(G) tmpG_impl = self.fft_object_coarse(G.impl.values) / ( self.coarse_prob.init[0] * self.coarse_prob.init[1]) tmpF_impl = np.zeros(self.fine_prob.init, dtype=np.complex128) halfG = int(self.coarse_prob.init[0] / 2) tmpF_impl[0:halfG, 0:halfG] = tmpG_impl[0:halfG, 0:halfG] tmpF_impl[self.fine_prob.init[0] - halfG:, 0:halfG] = tmpG_impl[halfG:, 0:halfG] tmpF_impl[0:halfG, self.fine_prob.init[0] - halfG:] = tmpG_impl[0:halfG, halfG:] tmpF_impl[self.fine_prob.init[0] - halfG:, self.fine_prob.init[0] - halfG:] = tmpG_impl[halfG:, halfG:] F.impl.values[:] = np.real( self.ifft_object_fine(tmpF_impl, normalise_idft=False)) tmpG_expl = self.fft_object_coarse(G.expl.values) / ( self.coarse_prob.init[0] * self.coarse_prob.init[1]) tmpF_expl = np.zeros(self.fine_prob.init, dtype=np.complex128) halfG = int(self.coarse_prob.init[0] / 2) tmpF_expl[0:halfG, 0:halfG] = tmpG_expl[0:halfG, 0:halfG] tmpF_expl[self.fine_prob.init[0] - halfG:, 0:halfG] = tmpG_expl[halfG:, 0:halfG] tmpF_expl[0:halfG, self.fine_prob.init[0] - halfG:] = tmpG_expl[0:halfG, halfG:] tmpF_expl[self.fine_prob.init[0] - halfG:, self.fine_prob.init[0] - halfG:] = tmpG_expl[halfG:, halfG:] F.expl.values[:] = np.real( self.ifft_object_fine(tmpF_expl, normalise_idft=False)) else: raise TransferError('Unknown data type, got %s' % type(G)) return F
def mesh_test(oloops, iloops, N): maxtime = 0.0 mintime = 1E99 meantime = 0.0 maxb = 0.0 for i in range(oloops): t0 = time.time() a = mesh(init=N, val=1.0 * i) b = mesh(a) maxb = inner_loop(iloops, mesh, maxb, a, b) t1 = time.time() maxtime = max(maxtime, t1 - t0) mintime = min(mintime, t1 - t0) meantime += t1 - t0 meantime /= oloops print(maxb) print(maxtime, mintime, meantime)
def check_datatypes_mesh(init): import pySDC.implementations.datatype_classes.mesh as m m1 = m.mesh(init) m2 = m.mesh(m1) m1.values[:] = 1.0 m2.values[:] = 2.0 m3 = m1 + m2 m4 = m1 - m2 m5 = 0.1 * m1 m6 = m1 m7 = abs(m1) m8 = m.mesh(m1) assert isinstance(m3, type(m1)) assert isinstance(m4, type(m1)) assert isinstance(m5, type(m1)) assert isinstance(m6, type(m1)) assert isinstance(m7, float) assert m2 is not m1 assert m3 is not m1 assert m4 is not m1 assert m5 is not m1 assert m6 is m1 assert np.shape(m3.values) == np.shape(m1.values) assert np.shape(m4.values) == np.shape(m1.values) assert np.shape(m5.values) == np.shape(m1.values) assert np.all(m1.values == 1.0) assert np.all(m2.values == 2.0) assert np.all(m3.values == 3.0) assert np.all(m4.values == -1.0) assert np.all(m5.values == 0.1) assert np.all(m8.values == 1.0) assert m7 >= 0
def prolong(self, G): """ Prolongation implementation Args: G: the coarse level data (easier to access than via the coarse attribute) """ if isinstance(G, mesh): F = mesh(G) elif isinstance(G, rhs_imex_mesh): F = rhs_imex_mesh(G) else: raise TransferError('Unknown data type, got %s' % type(G)) return F
def restrict(self, F): """ Restriction implementation Args: F: the fine level data (easier to access than via the fine attribute) """ if isinstance(F, mesh): G = mesh(F) elif isinstance(F, rhs_imex_mesh): G = rhs_imex_mesh(F) else: raise TransferError('Unknown data type, got %s' % type(F)) return G
def restrict(self, F): """ Restriction implementation Args: F: the fine level data (easier to access than via the fine attribute) """ if isinstance(F, mesh): G = mesh(self.coarse_prob.init, val=0.0) G.values = F.values[::self.ratio] elif isinstance(F, rhs_imex_mesh): G = rhs_imex_mesh(self.coarse_prob.init, val=0.0) G.impl.values = F.impl.values[::self.ratio] G.expl.values = F.expl.values[::self.ratio] else: raise TransferError('Unknown data type, got %s' % type(F)) return G
def __eval_fexpl(self, u, t): """ Helper routine to evaluate the explicit part of the RHS Args: u: current values (not used here) t: current time Returns: explicit part of RHS """ b = np.concatenate((u.values[0, :], u.values[1, :])) sol = self.Dx.dot(b) fexpl = mesh(self.nvars) fexpl.values[0, :], fexpl.values[1, :] = np.split(sol, 2) return fexpl
def __eval_fimpl(self, u, t): """ Helper routine to evaluate the implicit part of the RHS Args: u: current values t: current time (not used here) Returns: implicit part of RHS """ b = np.concatenate((u.values[0, :], u.values[1, :])) sol = self.A.dot(b) fimpl = mesh(self.nvars, val=0) fimpl.values[0, :], fimpl.values[1, :] = np.split(sol, 2) return fimpl
def solve_system(self, rhs, factor, u0, t): """ Simple linear solver for (I-dtA)u = rhs Args: rhs: right-hand side for the nonlinear system factor: abbrev. for the node-to-node stepsize (or any other factor required) u0: initial guess for the iterative solver (not used here so far) t: current time (e.g. for time-dependent BCs) Returns: solution as mesh """ M = self.Id - factor * self.A b = np.concatenate((rhs.values[0, :], rhs.values[1, :])) sol = LA.spsolve(M, b) me = mesh(self.nvars) me.values[0, :], me.values[1, :] = np.split(sol, 2) return me
t1 = time.perf_counter() print(res, t1-t0) # res = 0 # t0 = time.perf_counter() # m = mytype((nvars, nvars), val=2.0) # for i in range(nruns): # o = mytype(m) # o.values[:] = 4.0 # res = max(res, abs(m + o)) # t1 = time.perf_counter() # print(res, t1-t0) res = 0 t0 = time.perf_counter() m = mesh(init=(nvars, nvars), val=2.0) for i in range(nruns): o = mesh(init=m) o.values[:] = 4.0 n = mesh(init=m) for j in range(i): n += 0.1 * j * o res = max(res, abs(n)) t1 = time.perf_counter() print(res, t1-t0) m = mytype((nvars, nvars), val=2.0) n = mytype((nvars, nvars), val=2.0) m[:] = -1 n[:] = 2.9 # print(n)