def test_surf_approx(n, f, true=None): '''Surface integral (for benchmark problem)''' omega = UnitCubeMesh(n, n, 2 * n) A = np.array([0.5, 0.5, 0.0]) B = np.array([0.5, 0.5, 1.0]) gamma = StraightLineMesh(A, B, 2 * n) V3 = FunctionSpace(omega, 'CG', 1) V1 = FunctionSpace(gamma, 'CG', 1) f3 = interpolate(f, V3) shape = SquareRim(P=lambda x: np.array([0.25, 0.25, x[2]]), degree=10) Pi = average_matrix(V3, V1, shape=shape) x = Pi * f3.vector() f1 = Function(V1, x) if true is None: true = f error = sqrt(abs(assemble(inner(true - f1, true - f1) * dx))) return gamma.hmin(), error, f1
def test_id(n, f, align=True): '''Surface integral (for benchmark problem)''' omega = UnitCubeMesh(n, n, 2 * n) A = np.array([0.5, 0.5, 0.0]) B = np.array([0.5, 0.5, 1.0]) lmbda = StraightLineMesh(A, B, 2 * n if align else n) # The curve # Coupling surface walls = [ 'near((x[0]-0.25)*(x[0]-0.75), 0) && ((0.25-tol) < x[1]) && ((0.75+tol) > x[1])', 'near((x[1]-0.25)*(x[1]-0.75), 0) && ((0.25-tol) < x[0]) && ((0.75+tol) > x[0])' ] walls = ['( %s )' % w for w in walls] chi = CompiledSubDomain(' || '.join(walls), tol=1E-10) surfaces = MeshFunction('size_t', omega, 2, 0) chi.mark(surfaces, 1) gamma = EmbeddedMesh(surfaces, 1) V3 = FunctionSpace(omega, 'CG', 1) V2 = FunctionSpace(gamma, 'CG', 1) V1 = FunctionSpace(lmbda, 'CG', 1) # What we want to extend f1 = interpolate(f, V1) # To 2d E = uniform_extension_matrix(V1, V2) x = E * f1.vector() f2 = Function(V2, x) # Extend to 3d by harmonic extension u, v = TrialFunction(V3), TestFunction(V3) a, L = inner(grad(u), grad(v)) * dx, inner(Constant(0), v) * dx bcs = [DirichletBC(V3, f, 'on_boundary'), DirichletBC(V3, f, surfaces, 1)] A, b = assemble_system(a, L, bcs) f3 = Function(V3) solver = KrylovSolver('cg', 'hypre_amg') solver.parameters['relative_tolerance'] = 1E-13 solver.solve(A, f3.vector(), b) # Come back shape = SquareRim(P=lambda x: np.array([0.25, 0.25, x[2]]), degree=10) Pi = average_matrix(V3, V1, shape=shape) x = Pi * f3.vector() f10 = Function(V1, x) error = sqrt(abs(assemble(inner(f10 - f, f10 - f) * dx))) return gamma.hmin(), error, f10
def poisson_1d(n, u_true, f): '''1d part of benchmark''' A, B = np.array([0.5, 0.5, 0]), np.array([0.5, 0.5, 1]) mesh = StraightLineMesh(A, B, n) V = FunctionSpace(mesh, 'CG', 1) bc = DirichletBC(V, u_true, 'on_boundary') u, v = TrialFunction(V), TestFunction(V) a = inner(grad(u), grad(v)) * dx L = inner(f, v) * dx A, b = assemble_system(a, L, bc) solver = KrylovSolver('cg', 'amg') solver.parameters['relative_tolerance'] = 1E-13 uh = Function(V) solver.solve(A, uh.vector(), b) return mesh.hmin(), errornorm(u_true, uh, 'H1'), uh
def test_Pi(n, f3, f1, Pi_f3): '''(Pi u_3d, u_1d)_Gamma.''' # True here is the reduced one omega = UnitCubeMesh(n, n, 2*n) A = np.array([0.5, 0.5, 0.0]) B = np.array([0.5, 0.5, 1.0]) gamma = StraightLineMesh(A, B, 2*n) V3 = FunctionSpace(omega, 'CG', 1) V1 = FunctionSpace(gamma, 'CG', 1) u3 = TrialFunction(V3) v1 = TestFunction(V1) shape = SquareRim(P=lambda x: np.array([0.25, 0.25, x[2]]), degree=10) dx_ = Measure('dx', domain=gamma) a = inner(Average(u3, gamma, shape=shape), v1)*dx_ A = ii_assemble(a) # Now check action f3h = interpolate(f3, V3) x = A*f3h.vector() f1h = interpolate(f1, V1) Pi_f = Function(V1, x) result = f1h.vector().inner(x) true = assemble(inner(Pi_f3, f1)*dx_) error = abs(true - result) return gamma.hmin(), error, Pi_f
def test_ET(n, f3, f1): '''(E u_1d, T u_3d)_Gamma.''' # True here is the reduced one omega = UnitCubeMesh(n, n, 2 * n) # Coupling surface walls = [ 'near((x[0]-0.25)*(x[0]-0.75), 0) && ((0.25-tol) < x[1]) && ((0.75+tol) > x[1])', 'near((x[1]-0.25)*(x[1]-0.75), 0) && ((0.25-tol) < x[0]) && ((0.75+tol) > x[0])' ] walls = ['( %s )' % w for w in walls] chi = CompiledSubDomain(' || '.join(walls), tol=1E-10) surfaces = MeshFunction('size_t', omega, 2, 0) chi.mark(surfaces, 1) gamma = EmbeddedMesh(surfaces, 1) A = np.array([0.5, 0.5, 0.0]) B = np.array([0.5, 0.5, 1.0]) lmbda = StraightLineMesh(A, B, 2 * n) # The curve V3 = FunctionSpace(omega, 'CG', 1) V2 = FunctionSpace(gamma, 'CG', 2) V1 = FunctionSpace(lmbda, 'CG', 1) u1 = TrialFunction(V1) v3 = TestFunction(V3) dx_ = Measure('dx', domain=gamma) a = inner(Extension(u1, gamma, 'uniform'), Trace(v3, gamma)) * dx_ A = ii_assemble(a) # Now check action f1h = interpolate(f1, V1) x = A * f1h.vector() f3h = interpolate(f3, V3) result = f3h.vector().inner(x) true = assemble(inner(f3, f1) * dx_) error = abs(true - result) return gamma.hmin(), error, f3h
def test_ext(n, f, true=None): '''Surface integral (for benchmark problem)''' omega = UnitCubeMesh(n, n, 2 * n) A = np.array([0.5, 0.5, 0.0]) B = np.array([0.5, 0.5, 1.0]) lmbda = StraightLineMesh(A, B, 2 * n) # The curve # Coupling surface walls = [ 'near((x[0]-0.25)*(x[0]-0.75), 0) && ((0.25-tol) < x[1]) && ((0.75+tol) > x[1])', 'near((x[1]-0.25)*(x[1]-0.75), 0) && ((0.25-tol) < x[0]) && ((0.75+tol) > x[0])' ] walls = ['( %s )' % w for w in walls] chi = CompiledSubDomain(' || '.join(walls), tol=1E-10) surfaces = MeshFunction('size_t', omega, 2, 0) chi.mark(surfaces, 1) gamma = EmbeddedMesh(surfaces, 1) V3 = FunctionSpace(omega, 'CG', 1) V2 = FunctionSpace(gamma, 'CG', 1) V1 = FunctionSpace(lmbda, 'CG', 1) # What we want to extend f1 = interpolate(f, V1) E = uniform_extension_matrix(V1, V2) x = E * f1.vector() f2 = Function(V2, x) if true is None: true = f error = sqrt(abs(assemble(inner(true - f2, true - f2) * dx))) return gamma.hmin(), error, f2
# I check here assembly of coupling integrals that are needed in Federica's # LM formulation from dolfin import * from weak_bcs.burman.generation import StraightLineMesh import numpy as np from xii import * H, n = 4, 10 # Vasculature mesh_1d = StraightLineMesh(np.array([0.5, 0.5, 0]), np.array([0.5, 0.5, H]), 3 * n) # Just to make it finer # This is a model background mesh where all the cells would be intersected # by a line. FIXME: how to compute this mesh = BoxMesh(Point(0, 0, 0), Point(1, 1, H), 1, 1, n) # Now we have a bulk unknown, the 1d unknown and the multiplier V3 = FunctionSpace(mesh, 'CG', 1) # This V1 = FunctionSpace(mesh_1d, 'CG', 1) Q = FunctionSpace(mesh, 'DG', 0) # and this would be different u3, u1 = list(map(TrialFunction, (V3, V1))) q = TestFunction(Q) # 3d->1d avg_shape = Circle(lambda x: 0.05, degree=10) Pi_u = Average(u3, mesh_1d, avg_shape) # To aveluate the multiplier on the line we use trace (Average of None shape) Tq = Average(q, mesh_1d, None) # The coupling is still on the line