Пример #1
0
def test_apply_restrictions():
    cell = triangle
    V0 = FiniteElement("DG", cell, 0)
    V1 = FiniteElement("Lagrange", cell, 1)
    V2 = FiniteElement("Lagrange", cell, 2)
    f0 = Coefficient(V0)
    f = Coefficient(V1)
    g = Coefficient(V2)
    n = FacetNormal(cell)
    x = SpatialCoordinate(cell)

    assert raises(UFLException, lambda: apply_restrictions(f0))
    assert raises(UFLException, lambda: apply_restrictions(grad(f)))
    assert raises(UFLException, lambda: apply_restrictions(n))

    # Continuous function gets default restriction if none
    # provided otherwise the user choice is respected
    assert apply_restrictions(f) == f('+')
    assert apply_restrictions(f('-')) == f('-')
    assert apply_restrictions(f('+')) == f('+')

    # Propagation to terminals
    assert apply_restrictions((f + f0)('+')) == f('+') + f0('+')

    # Propagation stops at grad
    assert apply_restrictions(grad(f)('-')) == grad(f)('-')
    assert apply_restrictions((grad(f)**2)('+')) == grad(f)('+')**2
    assert apply_restrictions((grad(f) + grad(g))('-')) == (grad(f)('-') + grad(g)('-'))

    # x is the same from both sides but computed from one of them
    assert apply_default_restrictions(x) == x('+')

    # n on a linear mesh is opposite pointing from the other side
    assert apply_restrictions(n('+')) == n('+')
    assert renumber_indices(apply_restrictions(n('-'))) == renumber_indices(as_tensor(-1*n('+')[i], i))
def check_single_function_pullback(g, mappings):
    expected = mappings[g]
    actual = apply_single_function_pullbacks(g)
    rexp = renumber_indices(expected)
    ract = renumber_indices(actual)
    if not rexp == ract:
        print()
        print("In check_single_function_pullback:")
        print("input:")
        print(repr(g))
        print("expected:")
        print(str(rexp))
        print("actual:")
        print(str(ract))
        print("signatures:")
        print((expected**2 * dx).signature())
        print((actual**2 * dx).signature())
        print()
    assert ract == rexp
Пример #3
0
def check_single_function_pullback(g, mappings):
    expected = mappings[g]
    actual = apply_single_function_pullbacks(g)
    rexp = renumber_indices(expected)
    ract = renumber_indices(actual)
    if not rexp == ract:
        print()
        print("In check_single_function_pullback:")
        print("input:")
        print(repr(g))
        print("expected:")
        print(str(rexp))
        print("actual:")
        print(str(ract))
        print("signatures:")
        print((expected**2*dx).signature())
        print((actual**2*dx).signature())
        print()
    assert ract == rexp
Пример #4
0
def check_single_function_pullback(g, mappings):
    expected = mappings[g]
    actual = apply_single_function_pullbacks(ReferenceValue(g), g.ufl_element())
    assert expected.ufl_shape == actual.ufl_shape
    for idx in numpy.ndindex(actual.ufl_shape):
        rexp = renumber_indices(expected[idx])
        ract = renumber_indices(actual[idx])
        if not rexp == ract:
            print()
            print("In check_single_function_pullback:")
            print("input:")
            print(repr(g))
            print("expected:")
            print(str(rexp))
            print("actual:")
            print(str(ract))
            print("signatures:")
            print((expected**2*dx).signature())
            print((actual**2*dx).signature())
            print()
        assert ract == rexp
Пример #5
0
def test_apply_restrictions():
    cell = triangle
    V0 = FiniteElement("DG", cell, 0)
    V1 = FiniteElement("Lagrange", cell, 1)
    V2 = FiniteElement("Lagrange", cell, 2)
    f0 = Coefficient(V0)
    f = Coefficient(V1)
    g = Coefficient(V2)
    n = FacetNormal(cell)
    x = SpatialCoordinate(cell)

    assert raises(UFLException, lambda: apply_restrictions(f0))
    assert raises(UFLException, lambda: apply_restrictions(grad(f)))
    assert raises(UFLException, lambda: apply_restrictions(n))

    # Continuous function gets default restriction if none
    # provided otherwise the user choice is respected
    assert apply_restrictions(f) == f('+')
    assert apply_restrictions(f('-')) == f('-')
    assert apply_restrictions(f('+')) == f('+')

    # Propagation to terminals
    assert apply_restrictions((f + f0)('+')) == f('+') + f0('+')

    # Propagation stops at grad
    assert apply_restrictions(grad(f)('-')) == grad(f)('-')
    assert apply_restrictions((grad(f)**2)('+')) == grad(f)('+')**2
    assert apply_restrictions(
        (grad(f) + grad(g))('-')) == (grad(f)('-') + grad(g)('-'))

    # x is the same from both sides but computed from one of them
    assert apply_default_restrictions(x) == x('+')

    # n on a linear mesh is opposite pointing from the other side
    assert apply_restrictions(n('+')) == n('+')
    assert renumber_indices(apply_restrictions(n('-'))) == renumber_indices(
        as_tensor(-1 * n('+')[i], i))
Пример #6
0
def uflSignature(form,*args):
    import ufl
    from ufl.algorithms.renumbering import renumber_indices
    sig = ''
    hashList  = [str(arg) for arg in args if not isinstance(arg,ufl.core.expr.Expr)]
    #   the following fails in the ufl algorithm in the rentrant corner problem:
    #   phi = atan_2(x[1], x[0]) + conditional(x[1] < 0, 2*math.pi, 0)
    #   create.function("ufl",gridview=grid,name="tmp",order=1,ufl=phi)
    # hashList += [str(renumber_indices(arg)) for arg in args if isinstance(arg,ufl.core.expr.Expr)]
    for arg in args:
        if isinstance(arg,ufl.core.expr.Expr):
            try:
                hashList += [str(renumber_indices(arg))]
            except ValueError:  # I don't think this should happen but it does :-<
                hashList += [str(arg)]

    if form is not None:
        hashList.append(form.signature())
    hashList = [h.split(" at ")[0] for h in hashList]
    return hashIt( sorted(hashList) )
Пример #7
0
    def compare(self, f, value):
        debug = 0
        if debug:
            print(('f', f))
        g = expand_derivatives(f)
        if debug:
            print(('g', g))
        gv = g(self.x, self.mapping)
        assert abs(gv - value) < 1e-7

        g = expand_indices(g)
        if debug:
            print(('g', g))
        gv = g(self.x, self.mapping)
        assert abs(gv - value) < 1e-7

        g = renumber_indices(g)
        if debug:
            print(('g', g))
        gv = g(self.x, self.mapping)
        assert abs(gv - value) < 1e-7
Пример #8
0
    def compare(self, f, value):
        debug = 0
        if debug:
            print(('f', f))
        g = expand_derivatives(f)
        if debug:
            print(('g', g))
        gv = g(self.x, self.mapping)
        assert abs(gv - value) < 1e-7

        g = expand_indices(g)
        if debug:
            print(('g', g))
        gv = g(self.x, self.mapping)
        assert abs(gv - value) < 1e-7

        g = renumber_indices(g)
        if debug:
            print(('g', g))
        gv = g(self.x, self.mapping)
        assert abs(gv - value) < 1e-7
Пример #9
0
def test_inverse1(A1):
    expected = as_tensor(((1.0/A1[0, 0],),))  # reshaped into 1x1 tensor
    assert inverse_expr(A1) == renumber_indices(expected)
Пример #10
0
def test_change_to_reference_grad():
    cell = triangle
    domain = Mesh(cell)
    U = FunctionSpace(domain, FiniteElement("CG", cell, 1))
    V = FunctionSpace(domain, VectorElement("CG", cell, 1))
    u = Coefficient(U)
    v = Coefficient(V)
    Jinv = JacobianInverse(domain)
    i, j, k = indices(3)
    q, r, s = indices(3)
    t, = indices(1)

    # Single grad change on a scalar function
    expr = grad(u)
    actual = change_to_reference_grad(expr)
    expected = as_tensor(Jinv[k, i] * ReferenceGrad(u)[k], (i, ))
    assert renumber_indices(actual) == renumber_indices(expected)

    # Single grad change on a vector valued function
    expr = grad(v)
    actual = change_to_reference_grad(expr)
    expected = as_tensor(Jinv[k, j] * ReferenceGrad(v)[i, k], (i, j))
    assert renumber_indices(actual) == renumber_indices(expected)

    # Multiple grads should work fine for affine domains:
    expr = grad(grad(u))
    actual = change_to_reference_grad(expr)
    expected = as_tensor(
        Jinv[s, j] * (Jinv[r, i] * ReferenceGrad(ReferenceGrad(u))[r, s]),
        (i, j))
    assert renumber_indices(actual) == renumber_indices(expected)

    expr = grad(grad(grad(u)))
    actual = change_to_reference_grad(expr)
    expected = as_tensor(
        Jinv[s, k] *
        (Jinv[r, j] *
         (Jinv[q, i] *
          ReferenceGrad(ReferenceGrad(ReferenceGrad(u)))[q, r, s])), (i, j, k))
    assert renumber_indices(actual) == renumber_indices(expected)

    # Multiple grads on a vector valued function
    expr = grad(grad(v))
    actual = change_to_reference_grad(expr)
    expected = as_tensor(
        Jinv[s, j] * (Jinv[r, i] * ReferenceGrad(ReferenceGrad(v))[t, r, s]),
        (t, i, j))
    assert renumber_indices(actual) == renumber_indices(expected)

    expr = grad(grad(grad(v)))
    actual = change_to_reference_grad(expr)
    expected = as_tensor(
        Jinv[s, k] *
        (Jinv[r, j] *
         (Jinv[q, i] *
          ReferenceGrad(ReferenceGrad(ReferenceGrad(v)))[t, q, r, s])),
        (t, i, j, k))
    assert renumber_indices(actual) == renumber_indices(expected)
Пример #11
0
def xtest_pseudo_inverse21(A21):
    expected = todo
    assert renumber_indices(inverse_expr(A21)) == renumber_indices(expected)
Пример #12
0
def xtest_inverse3(A3):
    expected = todo
    assert inverse_expr(A3) == renumber_indices(expected)
Пример #13
0
def test_grad_ruleset():
    cell = triangle
    d = cell.geometric_dimension()

    V0 = FiniteElement("DG", cell, 0)
    V1 = FiniteElement("Lagrange", cell, 1)
    V2 = FiniteElement("Lagrange", cell, 2)
    W0 = VectorElement("DG", cell, 0)
    W1 = VectorElement("Lagrange", cell, 1)
    W2 = VectorElement("Lagrange", cell, 2)

    # Literals
    one = as_ufl(1)
    two = as_ufl(2.0)
    I = Identity(d)
    literals = [one, two, I]

    # Geometry
    x = SpatialCoordinate(cell)
    n = FacetNormal(cell)
    volume = CellVolume(cell)
    geometry = [x, n, volume]

    # Arguments
    u0 = TestFunction(V0)
    u1 = TestFunction(V1)
    arguments = [u0, u1]

    # Coefficients
    r = Constant(cell)
    vr = VectorConstant(cell)
    f0 = Coefficient(V0)
    f1 = Coefficient(V1)
    f2 = Coefficient(V2)
    vf0 = Coefficient(W0)
    vf1 = Coefficient(W1)
    vf2 = Coefficient(W2)
    coefficients = [f0, f1, vf0, vf1]

    # Expressions
    e0 = f0 + f1
    e1 = u0 * (f1/3 - f0**2)
    e2 = exp(sin(cos(tan(ln(x[0])))))
    expressions = [e0, e1, e2]

    # Variables
    v0 = variable(one)
    v1 = variable(f1)
    v2 = variable(f0*f1)
    variables = [v0, v1, v2]

    rules = GradRuleset(d)

    # Literals
    assert rules(one) == zero((d,))
    assert rules(two) == zero((d,))
    assert rules(I) == zero((d, d, d))

    # Assumed piecewise constant geometry
    for g in [n, volume]:
        assert rules(g) == zero(g.ufl_shape + (d,))

    # Non-constant geometry
    assert rules(x) == I

    # Arguments
    for u in arguments:
        assert rules(u) == grad(u)

    # Piecewise constant coefficients (Constant)
    assert rules(r) == zero((d,))
    assert rules(vr) == zero((d, d))
    assert rules(grad(r)) == zero((d, d))
    assert rules(grad(vr)) == zero((d, d, d))

    # Piecewise constant coefficients (DG0)
    assert rules(f0) == zero((d,))
    assert rules(vf0) == zero((d, d))
    assert rules(grad(f0)) == zero((d, d))
    assert rules(grad(vf0)) == zero((d, d, d))

    # Piecewise linear coefficients
    assert rules(f1) == grad(f1)
    assert rules(vf1) == grad(vf1)
    # assert rules(grad(f1)) == zero((d,d)) # TODO: Use degree to make this work
    # assert rules(grad(vf1)) == zero((d,d,d))

    # Piecewise quadratic coefficients
    assert rules(grad(f2)) == grad(grad(f2))
    assert rules(grad(vf2)) == grad(grad(vf2))

    # Indexed coefficients
    assert renumber_indices(apply_derivatives(grad(vf2[0]))) == renumber_indices(grad(vf2)[0, :])
    assert renumber_indices(apply_derivatives(grad(vf2[1])[0])) == renumber_indices(grad(vf2)[1, 0])

    # Grad of gradually more complex expressions
    assert apply_derivatives(grad(2*f0)) == zero((d,))
    assert renumber_indices(apply_derivatives(grad(2*f1))) == renumber_indices(2*grad(f1))
    assert renumber_indices(apply_derivatives(grad(sin(f1)))) == renumber_indices(cos(f1) * grad(f1))
    assert renumber_indices(apply_derivatives(grad(cos(f1)))) == renumber_indices(-sin(f1) * grad(f1))
Пример #14
0
def test_inverse0(A0):
    expected = 1.0/A0  # stays scalar
    assert inverse_expr(A0) == renumber_indices(expected)
Пример #15
0
def test_pseudo_determinant32(A32):
    i = Index()
    c = cross_expr(A32[:, 0], A32[:, 1])
    assert renumber_indices(determinant_expr(A32)) == renumber_indices(sqrt(c[i]*c[i]))
Пример #16
0
def test_pseudo_determinant31(A31):
    i = Index()
    assert renumber_indices(determinant_expr(A31)) == renumber_indices(sqrt((A31[i, 0]*A31[i, 0])))
Пример #17
0
def xtest_pseudo_inverse32(A32):
    expected = todo
    assert renumber_indices(inverse_expr(A32)) == renumber_indices(expected)
Пример #18
0
def test_grad_ruleset():
    cell = triangle
    d = cell.geometric_dimension()

    V0 = FiniteElement("DG", cell, 0)
    V1 = FiniteElement("Lagrange", cell, 1)
    V2 = FiniteElement("Lagrange", cell, 2)
    W0 = VectorElement("DG", cell, 0)
    W1 = VectorElement("Lagrange", cell, 1)
    W2 = VectorElement("Lagrange", cell, 2)

    # Literals
    one = as_ufl(1)
    two = as_ufl(2.0)
    I = Identity(d)
    literals = [one, two, I]

    # Geometry
    x = SpatialCoordinate(cell)
    n = FacetNormal(cell)
    volume = CellVolume(cell)
    geometry = [x, n, volume]

    # Arguments
    u0 = TestFunction(V0)
    u1 = TestFunction(V1)
    arguments = [u0, u1]

    # Coefficients
    r = Constant(cell)
    vr = VectorConstant(cell)
    f0 = Coefficient(V0)
    f1 = Coefficient(V1)
    f2 = Coefficient(V2)
    vf0 = Coefficient(W0)
    vf1 = Coefficient(W1)
    vf2 = Coefficient(W2)
    coefficients = [f0, f1, vf0, vf1]

    # Expressions
    e0 = f0 + f1
    e1 = u0 * (f1 / 3 - f0**2)
    e2 = exp(sin(cos(tan(ln(x[0])))))
    expressions = [e0, e1, e2]

    # Variables
    v0 = variable(one)
    v1 = variable(f1)
    v2 = variable(f0 * f1)
    variables = [v0, v1, v2]

    rules = GradRuleset(d)

    # Literals
    assert rules(one) == zero((d, ))
    assert rules(two) == zero((d, ))
    assert rules(I) == zero((d, d, d))

    # Assumed piecewise constant geometry
    for g in [n, volume]:
        assert rules(g) == zero(g.ufl_shape + (d, ))

    # Non-constant geometry
    assert rules(x) == I

    # Arguments
    for u in arguments:
        assert rules(u) == grad(u)

    # Piecewise constant coefficients (Constant)
    assert rules(r) == zero((d, ))
    assert rules(vr) == zero((d, d))
    assert rules(grad(r)) == zero((d, d))
    assert rules(grad(vr)) == zero((d, d, d))

    # Piecewise constant coefficients (DG0)
    assert rules(f0) == zero((d, ))
    assert rules(vf0) == zero((d, d))
    assert rules(grad(f0)) == zero((d, d))
    assert rules(grad(vf0)) == zero((d, d, d))

    # Piecewise linear coefficients
    assert rules(f1) == grad(f1)
    assert rules(vf1) == grad(vf1)
    # assert rules(grad(f1)) == zero((d,d)) # TODO: Use degree to make this work
    # assert rules(grad(vf1)) == zero((d,d,d))

    # Piecewise quadratic coefficients
    assert rules(grad(f2)) == grad(grad(f2))
    assert rules(grad(vf2)) == grad(grad(vf2))

    # Indexed coefficients
    assert renumber_indices(apply_derivatives(grad(
        vf2[0]))) == renumber_indices(grad(vf2)[0, :])
    assert renumber_indices(apply_derivatives(grad(
        vf2[1])[0])) == renumber_indices(grad(vf2)[1, 0])

    # Grad of gradually more complex expressions
    assert apply_derivatives(grad(2 * f0)) == zero((d, ))
    assert renumber_indices(apply_derivatives(grad(
        2 * f1))) == renumber_indices(2 * grad(f1))
    assert renumber_indices(apply_derivatives(grad(
        sin(f1)))) == renumber_indices(cos(f1) * grad(f1))
    assert renumber_indices(apply_derivatives(grad(
        cos(f1)))) == renumber_indices(-sin(f1) * grad(f1))