def test_l2_h1_1(): eps = 1e-12 eps_low = 1e-10 func = lambda x: sin(x) mesh1 = Mesh1D((0, pi), (20, )) mesh2 = Mesh1D((0, pi / 2, pi), (20, 20)) mesh3 = Mesh1D((0, pi / 2), (20, )) f = Function(func, mesh1) g = Function(func, mesh2) h = Function(func, mesh3) assert abs(f.l2_norm(method="Fekete") - sqrt(pi / 2)) < eps assert abs(g.l2_norm(method="Fekete") - sqrt(pi / 2)) < eps assert abs(h.l2_norm(method="Fekete") - sqrt(pi / 4)) < eps assert abs(f.l2_norm(method="FE") - sqrt(pi / 2)) < eps assert abs(g.l2_norm(method="FE") - sqrt(pi / 2)) < eps assert abs(h.l2_norm(method="FE") - sqrt(pi / 4)) < eps assert abs(f.h1_norm() - sqrt(pi)) < eps_low assert abs(g.h1_norm() - sqrt(pi)) < eps_low assert abs(h.h1_norm() - sqrt(pi / 2)) < eps_low func = lambda x: cos(x) mesh1 = Mesh1D((0, pi / 4, pi / 2, 3 * pi / 4, pi), (20, 20, 20, 20)) f = Function(func, mesh1) assert abs(f.l2_norm(method="Fekete") - sqrt(pi / 2)) < eps assert abs(f.l2_norm(method="FE") - sqrt(pi / 2)) < eps assert abs(f.h1_norm() - sqrt(pi)) < eps
def test3(): eps = 1e-12 func = lambda x: x**2 f = Function(func, Mesh1D((-5, -4, 3, 10), (1, 5, 1))) for x in [-4, -3, -2, -1, 0, 0.01, 1e-5, 1, 2, 3]: assert abs(f(x) - func(x)) < eps func = lambda x: x**3 f = Function(func, Mesh1D((-5, -4, 3, 10), (1, 5, 1))) for x in [-4, -3, -2, -1, 0, 0.01, 1e-5, 1, 2, 3]: assert abs(f(x) - func(x)) < eps func = lambda x: x**4 f = Function(func, Mesh1D((-5, -4, 3, 10), (1, 5, 1))) for x in [-4, -3, -2, -1, 0, 0.01, 1e-5, 1, 2, 3]: assert abs(f(x) - func(x)) < eps func = lambda x: x**5 f = Function(func, Mesh1D((-5, -4, 3, 10), (1, 5, 1))) for x in [-4, -3, -2, -1, 0, 0.01, 1e-5, 1, 2, 3]: assert abs(f(x) - func(x)) < eps func = lambda x: x**6 f = Function(func, Mesh1D((-5, -4, 3, 10), (1, 5, 1))) x = -1 assert abs(f(x) - func(x)) > 61.9 x = 0 assert abs(f(x) - func(x)) > 61.9 x = 1 assert abs(f(x) - func(x)) > 61.6 x = 2 assert abs(f(x) - func(x)) > 28.9
def test4(): eps = 1e-12 func = lambda x: x**2 orig_mesh = Mesh1D((-5, -4, 3, 10), (1, 5, 1)) mesh1 = Mesh1D((-5, -4, 3, 10), (1, 1, 1)) f = Function(func, orig_mesh) g = f.project_onto(mesh1) h = Function(func, mesh1) assert g == Function(func, mesh1) assert h == h.project_onto(orig_mesh)
def test_l2_h1_proj6(): """ Tests exact projections. Slightly more complicated example. """ f_exact = lambda x: sin(x)*exp(x) f_exact_l2 = lambda x: -e*cos(1)/4 + e*sin(1)/4 + exp(-1)*sin(1)/4 + 3*x*(e*sin(1)/2 - exp(-1)*sin(1)/2 - cos(1)*exp(-1))/2 + cos(1)*exp(-1)/4 pts = [-1, -0.5, 0, 0.5, 1] orders = [20]*(len(pts)-1) m = Mesh1D(pts, orders) f = Function(f_exact, m) pts = [-1, 1] orders = [1]*(len(pts)-1) m = Mesh1D(pts, orders) f_proj_l2 = Function(f_exact_l2, m) assert (f.project_onto(m, proj_type="L2") - f_proj_l2).l2_norm() < 0.03
def test_l2_h1_2(): eps = 1e-9 func = lambda x: log(x) mesh1 = Mesh1D((1, 1.5, 2, 2.5, e), (20, 20, 20, 20)) f = Function(func, mesh1) l2_norm_exact = sqrt(e - 2) h1_norm_exact = sqrt(e - 1 - exp(-1)) assert abs(f.l2_norm(method="Fekete") - l2_norm_exact) < eps assert abs(f.l2_norm(method="FE") - l2_norm_exact) < eps assert abs(f.h1_norm() - h1_norm_exact) < eps
def test5(): eps = 1e-12 func = lambda x: x**2 mesh1 = Mesh1D((-5, -4, 3, 10), (2, 5, 2)) mesh2 = Mesh1D((-5, -4, 3, 10), (2, 2, 2)) mesh3 = Mesh1D((-5, -4, 3, 10), (2, 2, 1)) mesh4 = Mesh1D((-5, 10), (2, )) mesh5 = Mesh1D((-5, 10), (3, )) mesh6 = Mesh1D((-5, 10), (1, )) f = Function(func, mesh1) g = Function(func, mesh2) h = Function(func, mesh3) l = Function(func, mesh4) assert f == g assert g == f assert f == l assert g == l assert f != h assert h != f assert g != h assert h != g assert f == Function(lambda x: x**2, mesh1) assert f != Function(lambda x: x**3, mesh1) assert f == Function(lambda x: x**2, mesh2) assert f == Function(lambda x: x**2, mesh4) assert f == Function(lambda x: x**2, mesh5) assert f != Function(lambda x: x**2, mesh6)
def test_l2_h1_proj5(): """ Tests exact projections. The exact results were generated using: from sympy import sin, cos, integrate, var, pi, exp, log, E, S var("x") f = exp(x) S(1)/2 * integrate(f, (x, -1, 1)) + S(3)/2*x*integrate(x*f, (x, -1, 1)) """ f_exact = lambda x: exp(x) f_exact_l2 = lambda x: e / 2 - exp(-1) / 2 + 3 * x * exp(-1) # TODO: The constant term here has to be checked: f_exact_h1 = lambda x: +3 * e / 8 + 3 * exp(-1) / 8 + 3 * x * (e + exp(-1) ) / 8 pts = [-1, -0.5, 0, 0.5, 1] orders = [20] * (len(pts) - 1) m = Mesh1D(pts, orders) f = Function(f_exact, m) pts = [-1, 1] orders = [1] * (len(pts) - 1) m = Mesh1D(pts, orders) f_proj_l2_exact = Function(f_exact_l2, m) f_proj_l2 = f.project_onto(m, proj_type="L2") f_proj_h1_exact = Function(f_exact_h1, m) f_proj_h1 = f.project_onto(m, proj_type="H1") eps_l2 = 1e-3 eps_h1 = 0.03 assert (f_proj_l2 - f_proj_l2_exact).l2_norm() < eps_l2 assert (f_proj_h1 - f_proj_h1_exact).l2_norm() < eps_h1 # Make sure that if we exchange the L2 and H1 solutions, then the test # fails: assert (f_proj_l2 - f_proj_h1_exact).l2_norm() > max(eps_l2, eps_h1) assert (f_proj_h1 - f_proj_l2_exact).l2_norm() > max(eps_l2, eps_h1)
def test2(): eps = 1e-12 func = lambda x: x**2 f = Function(func, Mesh1D((-5, -4, 3, 10), (2, 5, 2))) for x in [ -5, -4.5, -4, -3, -2, -1, 0, 0.01, 1e-5, 1, 2, 3, 4, 5, 6, 7, 10 ]: assert abs(f(x) - func(x)) < eps f = Function(func, Mesh1D((-5, -4, 3, 10), (1, 5, 2))) for x in [-5, -4, -3, -2, -1, 0, 0.01, 1e-5, 1, 2, 3, 4, 5, 6, 7, 10]: assert abs(f(x) - func(x)) < eps x = -4.9 assert abs(f(x) - func(x)) > 0.08 x = -4.5 assert abs(f(x) - func(x)) > 0.24 f = Function(func, Mesh1D((-5, -4, 3, 10), (1, 5, 1))) for x in [-5, -4, -3, -2, -1, 0, 0.01, 1e-5, 1, 2, 3, 10]: assert abs(f(x) - func(x)) < eps x = -4.9 assert abs(f(x) - func(x)) > 0.08 x = -4.5 assert abs(f(x) - func(x)) > 0.24 x = 4 assert abs(f(x) - func(x)) > 5.9 x = 5 assert abs(f(x) - func(x)) > 9.9 x = 6 assert abs(f(x) - func(x)) > 11.9 x = 7 assert abs(f(x) - func(x)) > 11.9 x = 8 assert abs(f(x) - func(x)) > 9.9 x = 9 assert abs(f(x) - func(x)) > 5.9
def test_l2_h1_proj4(): """ Tests conversion to FE basis. """ pts = arange(0, 2 * pi, 0.4) orders = [2] * (len(pts) - 1) m = Mesh1D(pts, orders) f = Function(lambda x: sin(x), m) assert f.project_onto(m, proj_type="Fekete") == f assert f.project_onto(m, proj_type="L2") == f assert f.project_onto(m, proj_type="H1") == f orders = [3] * (len(pts) - 1) m = Mesh1D(pts, orders) assert f.project_onto(m, proj_type="Fekete") == f assert f.project_onto(m, proj_type="L2") == f assert f.project_onto(m, proj_type="H1") == f orders = [4] * (len(pts) - 1) m = Mesh1D(pts, orders) assert f.project_onto(m, proj_type="Fekete") == f assert f.project_onto(m, proj_type="L2") == f assert f.project_onto(m, proj_type="H1") == f pts = arange(0, 2 * pi, 3) orders = [2] * (len(pts) - 1) m = Mesh1D(pts, orders) pts = array(list(arange(0, pts[-1], 0.1)) + [pts[-1]]) orders = [6] * (len(pts) - 1) f_exact = Function(lambda x: sin(x), Mesh1D(pts, orders)) sol_l2 = f_exact.project_onto(m, proj_type="L2") sol_h1 = f_exact.project_onto(m, proj_type="H1") assert (sol_l2 - f_exact).l2_norm() < 0.07 assert (sol_h1 - f_exact).l2_norm() < 0.07
def test8(): eps = 1e-12 func = lambda x: x**2 mesh1 = Mesh1D((-5, -4, 3, 10), (2, 5, 2)) mesh2 = Mesh1D((-5, -4, 3, 10), (2, 2, 2)) mesh3 = Mesh1D((-5, -4, 3, 10), (2, 2, 1)) mesh4 = Mesh1D((-5, 10), (2, )) mesh5 = Mesh1D((-5, 10), (3, )) mesh6 = Mesh1D((-5, 10), (1, )) f = Function(func, mesh1) g = Function(func, mesh2) h = Function(func, mesh3) l = Function(func, mesh4) zero = Function(lambda x: 0., Mesh1D((-5, 10), (1, ))) assert zero.l2_norm() < eps assert Function(lambda x: 0., mesh1) == zero assert Function(lambda x: 0., mesh2) == zero assert Function(lambda x: 0., mesh3) == zero assert Function(lambda x: 0., mesh4) == zero assert Function(lambda x: 0., mesh5) == zero assert Function(lambda x: 0., mesh6) == zero assert f - g == zero assert (f - g).l2_norm() < eps assert g - f == zero assert (g - f).l2_norm() < eps assert f - l == zero assert (f - l).l2_norm() < eps assert g - l == zero assert (g - l).l2_norm() < eps assert f - h != zero assert (f - h).l2_norm() > eps assert h - f != zero assert (h - f).l2_norm() > eps assert g - h != zero assert (g - h).l2_norm() > eps assert h - g != zero assert (h - g).l2_norm() > eps assert f - Function(lambda x: x**2, mesh1) == zero assert f - Function(lambda x: x**3, mesh1) != zero assert f - Function(lambda x: x**2, mesh2) == zero assert f - Function(lambda x: x**2, mesh4) == zero assert f - Function(lambda x: x**2, mesh5) == zero assert f - Function(lambda x: x**2, mesh6) != zero
def test_l2_h1_proj3(): """ Tests conversion to FE basis. """ pts = arange(0, 2 * pi, 0.1) orders = [2] * (len(pts) - 1) m = Mesh(pts, orders) f = Function(lambda x: sin(x), Mesh1D(pts, orders)) n_dof = m.assign_dofs() A = CSCMatrix(n_dof) rhs = AVector(n_dof) assemble_projection_matrix_rhs(m, A, rhs, f, projection_type="L2") x = solve(A.to_scipy_csc().todense(), rhs.to_numpy()) sol_l2 = FESolution(m, x).to_discrete_function() A = CSCMatrix(n_dof) assemble_projection_matrix_rhs(m, A, rhs, f, projection_type="H1") x = solve(A.to_scipy_csc().todense(), rhs.to_numpy()) sol_h1 = FESolution(m, x).to_discrete_function() assert sol_l2 == f assert sol_h1 == f
def test_l2_h1_proj2(): """ Tests the correctness of the projections. """ pts = arange(0, 2 * pi, 3) orders = [4] * (len(pts) - 1) m = Mesh(pts, orders) pts = array(list(arange(0, pts[-1], 0.1)) + [pts[-1]]) orders = [6] * (len(pts) - 1) f_exact = Function(lambda x: sin(x), Mesh1D(pts, orders)) n_dof = m.assign_dofs() A = CSCMatrix(n_dof) rhs = AVector(n_dof) assemble_projection_matrix_rhs(m, A, rhs, f_sin, projection_type="L2") x = solve(A.to_scipy_csc().todense(), rhs.to_numpy()) sol_l2 = FESolution(m, x).to_discrete_function() A = CSCMatrix(n_dof) assemble_projection_matrix_rhs(m, A, rhs, f_sin, projection_type="H1") x = solve(A.to_scipy_csc().todense(), rhs.to_numpy()) sol_h1 = FESolution(m, x).to_discrete_function() assert (sol_l2 - f_exact).l2_norm() < 0.002 assert (sol_h1 - f_exact).l2_norm() < 0.002
def test6(): mesh1 = Mesh1D((-5, -4, 3, 10), (2, 5, 2)) mesh2 = Mesh1D((-5, -4, 3, 10), (2, 2, 2)) mesh3 = Mesh1D((-5, -4, 3, 10), (2, 2, 1)) mesh4 = Mesh1D((-5, 10), (2, )) mesh5 = Mesh1D((-5, 10), (3, )) mesh6 = Mesh1D((-5, 10), (1, )) mesh7 = Mesh1D((-5, 10), (1, )) mesh8 = Mesh1D((-5, 0, 10), (1, 4)) assert mesh1 == mesh1 assert not (mesh1 != mesh1) assert mesh1 != mesh2 assert mesh1 != mesh3 assert mesh1 != mesh4 assert mesh1 != mesh5 assert mesh1 != mesh6 assert mesh6 == mesh7 assert mesh1.union(mesh1) == mesh1 assert mesh1.union(mesh2) == mesh1 assert mesh2.union(mesh1) == mesh1 assert mesh1.union(mesh3) == mesh1 assert mesh3.union(mesh1) == mesh1 assert mesh1.union(mesh4) == mesh1 assert mesh4.union(mesh1) == mesh1 assert mesh1.union(mesh5) == Mesh1D((-5, -4, 3, 10), (3, 5, 3)) assert mesh5.union(mesh1) == Mesh1D((-5, -4, 3, 10), (3, 5, 3)) assert mesh1.union(mesh6) == mesh1 assert mesh6.union(mesh1) == mesh1 assert mesh1.union(mesh8) == Mesh1D((-5, -4, 0, 3, 10), (2, 5, 5, 4)) assert mesh8.union(mesh1) == Mesh1D((-5, -4, 0, 3, 10), (2, 5, 5, 4))
def test_power(): eps = 1e-12 func = lambda x: x mesh1 = Mesh1D((0, 1), (1, )) mesh2 = Mesh1D((0, 1), (2, )) mesh3 = Mesh1D((0, 1), (3, )) f = Function(func, mesh1) assert abs(f.l2_norm() - sqrt(1. / 3)) < eps assert f**2 != Function(lambda x: x**2, mesh1) assert f**2 == Function(lambda x: x**2, mesh2) assert f**2 == Function(lambda x: x**2, mesh3) func = lambda x: x mesh1 = Mesh1D((5, 6), (1, )) mesh2 = Mesh1D((5, 6), (2, )) mesh3 = Mesh1D((5, 6), (3, )) f = Function(func, mesh1) assert f**2 != Function(lambda x: x**2, mesh1) assert f**2 == Function(lambda x: x**2, mesh2) assert f**2 == Function(lambda x: x**2, mesh3) func = lambda x: x**3 + x mesh1 = Mesh1D((5, 6), (3, )) mesh2 = Mesh1D((5, 6), (5, )) mesh3 = Mesh1D((5, 6), (6, )) mesh4 = Mesh1D((5, 6), (9, )) f = Function(func, mesh1) assert f**2 != Function(lambda x: x**6 + 2 * x**4 + x**2, mesh1) assert f**2 != Function(lambda x: x**6 + 2 * x**4 + x**2, mesh2) assert f**2 == Function(lambda x: x**6 + 2 * x**4 + x**2, mesh3) assert f**3 == Function(lambda x: x**3 + 3 * x**5 + 3 * x**7 + x**9, mesh4) func = lambda x: x**3 + x mesh1 = Mesh1D((5, 5.1, 6), (3, 3)) mesh2 = Mesh1D((5, 5.1, 6), (5, 5)) mesh3 = Mesh1D((5, 5.1, 6), (6, 6)) mesh4 = Mesh1D((5, 5.1, 6), (9, 9)) mesh5 = Mesh1D((5, 5.1, 6), (9, 7)) mesh6 = Mesh1D((5, 5.1, 6), (6, 5)) mesh7 = Mesh1D((5, 6), (6, )) mesh8 = Mesh1D((5, 6), (9, )) f = Function(func, mesh1) assert f**2 != Function(lambda x: x**6 + 2 * x**4 + x**2, mesh1) assert f**2 != Function(lambda x: x**6 + 2 * x**4 + x**2, mesh2) assert f**2 != Function(lambda x: x**6 + 2 * x**4 + x**2, mesh6) assert f**2 == Function(lambda x: x**6 + 2 * x**4 + x**2, mesh3) assert f**3 == Function(lambda x: x**3 + 3 * x**5 + 3 * x**7 + x**9, mesh4) assert f**3 != Function(lambda x: x**3 + 3 * x**5 + 3 * x**7 + x**9, mesh5) assert f**2 == Function(lambda x: x**6 + 2 * x**4 + x**2, mesh7) assert f**3 == Function(lambda x: x**3 + 3 * x**5 + 3 * x**7 + x**9, mesh8)
def test1(): m = Mesh1D((-5, -4, 3, 10), (1, 5, 1))
def adapt_mesh(mesh, eigs, l=0, Z=1, adapt_type="hp", eqn_type="R"): """ Adapts the mesh using the adaptivity type 'adapt_type'. Returns a new instance of the H1D mesh. adapt_type .... one of: h, hp, p, uniform-p, romanowski """ if adapt_type == "romanowski": m = refine_mesh_romanowski(mesh, eigs) pts, orders = m.get_mesh_data() return Mesh(pts, orders) elif adapt_type == "uniform-p": pts, orders = mesh.get_mesh_data() orders = array(orders) + 1 return Mesh(pts, orders) elif adapt_type in ["h", "p", "hp"]: NORM = 1 # 1 ... H1; 0 ... L2; THRESHOLD = 0.7 mesh_ref = mesh.reference_refinement() print "Fine mesh created (%d DOF)." % mesh_ref.get_n_dof() N_dof, energies, eigs_ref = solve_schroedinger(mesh_ref, l=l, Z=Z, eqn_type=eqn_type, eig_num=len(eigs)) flip_vectors(mesh, eigs, mesh_ref, eigs_ref) print " Done." sols = [] sols_ref = [] print "Normalizing solutions..." for i in range(len(eigs)): e = (eigs[i]).copy() coarse_h1_norm = FESolution(mesh, e).h1_norm() e /= coarse_h1_norm sols.append(e) e = (eigs_ref[i]).copy() reference_h1_norm = FESolution(mesh_ref, e).h1_norm() e /= reference_h1_norm sols_ref.append(e) #print "H1 norms:" #print "coarse (%d):" % i, coarse_h1_norm #print "reference (%d):" % i, reference_h1_norm print " Done." meshes = [] mesh_orig = mesh.copy() mesh_orig.assign_dofs() errors = [] for sol, sol_ref in zip(sols, sols_ref): mesh = mesh_orig.copy() mesh.assign_dofs() mesh_ref = mesh.reference_refinement() mesh_ref.assign_dofs() mesh.copy_vector_to_mesh(sol, 0) mesh_ref.copy_vector_to_mesh(sol_ref, 0) err_est_total, err_est_array = calc_error_estimate( NORM, mesh, mesh_ref) ref_sol_norm = calc_solution_norm(NORM, mesh_ref) err_est_rel = err_est_total / ref_sol_norm print "Relative error (est) = %g %%\n" % (100. * err_est_rel) errors.append(err_est_rel) # TODO: adapt using all the vectors: # 0 ... hp, 1 ... h, 2 ... p if adapt_type == "hp": ADAPT_TYPE = 0 elif adapt_type == "h": ADAPT_TYPE = 1 elif adapt_type == "p": ADAPT_TYPE = 2 else: raise ValueError("Unkown adapt_type") adapt(NORM, ADAPT_TYPE, THRESHOLD, err_est_array, mesh, mesh_ref) meshes.append(mesh) pts, orders = mesh_orig.get_mesh_data() mesh = Mesh1D(pts, orders) for m in meshes: pts, orders = m.get_mesh_data() m = Mesh1D(pts, orders) mesh = mesh.union(m) pts, orders = mesh.get_mesh_data() mesh = Mesh(pts, orders) return mesh else: raise ValueError("Unknown adapt_type")
def test7(): mesh1 = Mesh1D((-5, -4, 3, 10), (2, 5, 2)) mesh2 = Mesh1D((-5, -4, 3, 10), (2, 2, 2)) mesh3 = Mesh1D((-5, -4, 3, 10), (2, 2, 1)) mesh4 = Mesh1D((-5, 10), (2, )) mesh5 = Mesh1D((-5, 10), (3, )) mesh6 = Mesh1D((-5, 10), (1, )) mesh8 = Mesh1D((-5, 0, 10), (1, 4)) assert mesh1.restrict_to_interval(-5, 10) == mesh1 assert mesh1.restrict_to_interval(-4.5, 10) == Mesh1D((-4.5, -4, 3, 10), (2, 5, 2)) assert mesh1.restrict_to_interval(-4, 10) != mesh1 assert mesh1.restrict_to_interval(-4, 10) == Mesh1D((-4, 3, 10), (5, 2)) assert mesh1.restrict_to_interval(-3.5, 10) == Mesh1D((-3.5, 3, 10), (5, 2)) assert mesh1.restrict_to_interval(3, 10) == Mesh1D((3, 10), (2, )) assert mesh1.restrict_to_interval(3.5, 10) == Mesh1D((3.5, 10), (2, )) assert mesh2.restrict_to_interval(-5, 10) == mesh2 assert mesh2.restrict_to_interval(-4.5, 10) == Mesh1D((-4.5, -4, 3, 10), (2, 2, 2)) assert mesh2.restrict_to_interval(-4, 10) != mesh2 assert mesh2.restrict_to_interval(-4, 10) == Mesh1D((-4, 3, 10), (2, 2)) assert mesh2.restrict_to_interval(-3.5, 10) == Mesh1D((-3.5, 3, 10), (2, 2)) assert mesh2.restrict_to_interval(3, 10) == Mesh1D((3, 10), (2, )) assert mesh2.restrict_to_interval(3.5, 10) == Mesh1D((3.5, 10), (2, )) assert mesh3.restrict_to_interval(-5, 10) == mesh3 assert mesh3.restrict_to_interval(-4.5, 10) == Mesh1D((-4.5, -4, 3, 10), (2, 2, 1)) assert mesh3.restrict_to_interval(-4, 10) != mesh3 assert mesh3.restrict_to_interval(-4, 10) == Mesh1D((-4, 3, 10), (2, 1)) assert mesh3.restrict_to_interval(-3.5, 10) == Mesh1D((-3.5, 3, 10), (2, 1)) assert mesh3.restrict_to_interval(3, 10) == Mesh1D((3, 10), (1, )) assert mesh3.restrict_to_interval(3.5, 10) == Mesh1D((3.5, 10), (1, ))