Exemplo n.º 1
0
def assemble_with_mixed_types(
    mesh: Mesh,
    diffusions: List[Coefficient],
    convections: List[Optional[Coefficient]],
    reactions: List[Optional[Coefficient]],
):
    for diffusion in diffusions:
        for convection in convections:
            for reaction in reactions:
                eafe_assemble(mesh, diffusion, convection, reaction)
Exemplo n.º 2
0
def assert_solves(
    mesh: Mesh,
    diffusion: Coefficient,
    convection: Optional[Coefficient],
    reaction: Optional[Coefficient],
    source: Coefficient,
    exact: Coefficient,
    l2_tol: Optional[float] = 1.0e-8,
    h1_tol: Optional[float] = 1.0e-6,
):
    eafe_matrix = eafe_assemble(mesh, diffusion, convection, reaction)

    pw_linears = FunctionSpace(mesh, "Lagrange", 1)
    test_function = TestFunction(pw_linears)
    rhs_vector = assemble(source * test_function * dx)

    bc = DirichletBC(pw_linears, exact, lambda _, on_bndry: on_bndry)
    bc.apply(eafe_matrix, rhs_vector)

    solution = Function(pw_linears)
    solver = LUSolver(eafe_matrix, "default")
    solver.parameters["symmetric"] = False
    solver.solve(solution.vector(), rhs_vector)

    l2_err: float = errornorm(exact, solution, "l2", 3)
    assert l2_err <= l2_tol, f"L2 error too large: {l2_err} > {l2_tol}"

    h1_err: float = errornorm(exact, solution, "H1", 3)
    assert h1_err <= h1_tol, f"H1 error too large: {h1_err} > {h1_tol}"
Exemplo n.º 3
0
def run():
    def boundary(x, on_boundary):
        return on_boundary

    diffusivity = 1.0E-2

    def diffusion_expression(x):
        return diffusivity

    def convection_expression(x):
        return np.array([x[1], -x[0], 0.0])

    exact_solution = Expression(
        "sin(2 * DOLFIN_PI * x[0]) * cos(2 * DOLFIN_PI * x[1])", degree=4)

    right_side_expression = Expression(
        "8 * DOLFIN_PI * DOLFIN_PI * diffusivity\
        * sin(2 * DOLFIN_PI * x[0]) * cos(2 * DOLFIN_PI * x[1])\
        - 2 * DOLFIN_PI * x[1] * cos(2 * DOLFIN_PI * x[0])\
        * cos(2 * DOLFIN_PI * x[1])\
        - 2 * DOLFIN_PI * x[0] * sin(2 * DOLFIN_PI * x[0])\
        * sin(2 * DOLFIN_PI * x[1])",
        degree=2,
        diffusivity=diffusivity)

    granularity = 8
    mesh = UnitCubeMesh(granularity, granularity, granularity)

    continuous_pw_linear_space = FunctionSpace(mesh, "Lagrange", 1)
    test_function = TestFunction(continuous_pw_linear_space)
    linear_functional = right_side_expression * test_function * dx

    stiffness_matrix = pyeafe.eafe_assemble(mesh, diffusion_expression,
                                            convection_expression)
    rhs_vector = assemble(linear_functional)

    bc = DirichletBC(continuous_pw_linear_space, exact_solution, boundary)
    bc.apply(stiffness_matrix, rhs_vector)

    solution = Function(continuous_pw_linear_space)
    solver = LUSolver(stiffness_matrix, "default")
    solver.parameters["symmetric"] = False
    solver.solve(solution.vector(), rhs_vector)

    if (errornorm(exact_solution, solution, 'l2', 3) > 2.12E-1):
        print "L2 error increased! Solver failed"
        exit(1)

    if (errornorm(exact_solution, solution, 'H1', 3) > 2.33E-0):
        print "H1 error increased! Solver failed"
        exit(1)

    print "L2 error = ", errornorm(exact_solution, solution, 'l2', 3)
    print "H1 error = ", errornorm(exact_solution, solution, 'H1', 3)
    print "Success!"
Exemplo n.º 4
0
def test_assemble_raises_for_invalid_convection():
    with pytest.raises(TypeError):
        eafe_assemble(MESH, DIFFUSION, 5.0)

    with pytest.raises(ValueError):
        eafe_assemble(MESH, DIFFUSION, Expression("1.", degree=1))

    with pytest.raises(ValueError):
        eafe_assemble(MESH, DIFFUSION, Expression(("1."), degree=1))
Exemplo n.º 5
0
def run():
    logging.getLogger('FFC').setLevel(logging.WARNING)

    granularity = 8
    mesh = UnitSquareMesh(granularity, granularity)

    def diffusion(x):
        return diffusivity

    def convection(x):
        return np.array([x[1], -x[0]])

    eafe = pyeafe.eafe_assemble(mesh, diffusion, convection)
    try:
        compute_error(eafe, mesh)
    except error:
        print("error when executing with python defined coefficients")
        print(error)
        exit(1)

    diffusion_expression = Expression("diffusivity",
                                      diffusivity=diffusivity, degree=2)
    convection_expression = Expression(("x[1]", "-x[0]"), degree=2)
    eafe_expression = pyeafe.eafe_assemble(
        mesh,
        diffusion_expression,
        convection_expression
    )
    try:
        compute_error(eafe_expression, mesh)
    except error:
        print("error when executing with python defined coefficients")
        print(error)
        exit(1)

    cg = FunctionSpace(mesh, "CG", 1)
    bdm = FunctionSpace(mesh, "BDM", 1)
    diffusion_interpolant = interpolate(diffusion_expression, cg)
    convection_interpolant = interpolate(convection_expression, bdm)
    eafe_function = pyeafe.eafe_assemble(
        mesh,
        diffusion_interpolant,
        convection_interpolant
    )
    try:
        compute_error(eafe_function, mesh)
    except error:
        print("error when executing with python defined coefficients")
        print(error)
        exit(1)

    eafe_constant = pyeafe.eafe_assemble(mesh, diffusivity, np.zeros(2))
    try:
        diff_expr = Expression("diff", degree=2, diff=diffusivity)
        zero_expr = Expression("zero", degree=2, zero=0.0)
        err = compute_error(eafe_constant, mesh, diff_expr, zero_expr)
    except error:
        print("error when executing with constant coefficients")
        print(error)
        exit(1)

    print "Errors for various PDE coefficient representations are acceptable"
    print "Success!"
Exemplo n.º 6
0
    * sin(2 * DOLFIN_PI * x[0]) * cos(2 * DOLFIN_PI * x[1])\
    - 2 * DOLFIN_PI * x[1] * cos(2 * DOLFIN_PI * x[0])\
    * cos(2 * DOLFIN_PI * x[1])\
    - 2 * DOLFIN_PI * x[0] * sin(2 * DOLFIN_PI * x[0])\
    * sin(2 * DOLFIN_PI * x[1])",
                                   degree=2,
                                   diffusivity=diffusivity)

granularity = 8
mesh = UnitSquareMesh(granularity, granularity)

continuous_pw_linear_space = FunctionSpace(mesh, "Lagrange", 1)
test_function = TestFunction(continuous_pw_linear_space)
linear_functional = right_side_expression * test_function * dx

stiffness_matrix = pyeafe.eafe_assemble(mesh, diffusion_expression,
                                        convection_expression)
rhs_vector = assemble(linear_functional)

exact_solution = Expression(
    "sin(2 * DOLFIN_PI * x[0]) * cos(2 * DOLFIN_PI * x[1])", degree=4)
bc = DirichletBC(continuous_pw_linear_space, exact_solution, boundary)
bc.apply(stiffness_matrix, rhs_vector)

solution = Function(continuous_pw_linear_space)
solver = LUSolver(stiffness_matrix, "default")
solver.parameters["symmetric"] = False
solver.solve(solution.vector(), rhs_vector)

if (errornorm(exact_solution, solution, 'l2', 3) > 2.12E-1):
    print "L2 error increased! Solver failed"
    exit(1)
Exemplo n.º 7
0
def test_assemble_raises_for_invalid_reaction():
    with pytest.raises(TypeError):
        eafe_assemble(MESH, DIFFUSION, CONVECTION, 5.0)

    with pytest.raises(TypeError):
        eafe_assemble(MESH, DIFFUSION, reaction=5.0)
Exemplo n.º 8
0
def test_assemble_raises_for_invalid_diffusion():
    with pytest.raises(TypeError):
        eafe_assemble(MESH, None)

    with pytest.raises(TypeError):
        eafe_assemble(MESH, 1.0)
Exemplo n.º 9
0
def test_assemble_raises_for_invalid_mesh():
    with pytest.raises(TypeError):
        eafe_assemble(None, DIFFUSION)