# Take scalar product g_hat = Function(T) g_hat = inner(v, gj, output_array=g_hat) # Assemble matrices. if by_parts: mats = inner(grad(v), grad(u)) mats += [inner(v, alpha * u)] else: mats = inner(v, -div(grad(u)) + alpha * u) # Solve u_hat = Function(T) Sol1 = SolverGeneric1ND(mats) u_hat = Sol1(g_hat, u_hat) # Transform back to real space. uj = u_hat.backward() uq = Array(T, buffer=ue) print('Error =', np.linalg.norm(uj - uq)) if 'pytest' not in os.environ: # Postprocess # Refine for a nicer plot. Refine simply pads Functions with zeros, which # gives more quadrature points. u_hat has NxM quadrature points, refine # using any higher number. u_hat2 = u_hat.refine([N * 3, M * 3]) ur = u_hat2.backward(kind='uniform') from mayavi import mlab
mats = inner(grad(v), grad(u)) mats += [inner(v, alpha*u)] # case m=0 if comm.Get_rank() == 0: mats0 = inner(grad(v0), grad(u0)) mats0 += [inner(v0, alpha*u0)] else: mats = inner(v, -div(grad(u))+alpha*u) # case m=0 if comm.Get_rank() == 0: mats0 = inner(v0, -div(grad(u0))+alpha*u0) # Solve # case m > 0 u_hat = Function(T) Sol1 = SolverGeneric1ND(mats) u_hat = Sol1(f_hat, u_hat) # case m = 0 u0_hat = Function(T0) if comm.Get_rank() == 0: Sol0 = SolverGeneric1ND(mats0) u0_hat = Sol0(f0_hat, u0_hat) comm.Bcast(u0_hat, root=0) # Transform back to real space. Broadcast 1D solution sl = T.local_slice(False) uj = u_hat.backward() + u0_hat.backward()[:, sl[1]] uq = Array(T, buffer=ue) print('Error =', np.linalg.norm(uj-uq)) assert np.linalg.norm(uj-uq) < 1e-8