Example #1
0
def test_calls_2d_3():

    domain = Square()

    V = FunctionSpace('V', domain)

    x, y = domain.coordinates

    pn = Field('pn', V)
    wn = Field('wn', V)

    dp = TestFunction(V, name='dp')
    dw = TestFunction(V, name='dw')
    tau = TestFunction(V, name='tau')
    sigma = TestFunction(V, name='sigma')

    Re = Constant('Re', real=True)
    dt = Constant('dt', real=True)
    alpha = Constant('alpha', real=True)

    l1 = LinearForm(tau,
                    bracket(pn, wn) * tau - 1. / Re * dot(grad(tau), grad(wn)))

    # ...
    l = LinearForm((tau, sigma), dt * l1(tau))

    print(evaluate(l, verbose=True))
Example #2
0
def test_equation_2d_2():

    domain = Square()

    V = ScalarFunctionSpace('V', domain)

    x, y = domain.coordinates

    pn, wn = [element_of(V, name=i) for i in ['pn', 'wn']]

    dp = element_of(V, name='dp')
    dw = element_of(V, name='dw')
    tau = element_of(V, name='tau')
    sigma = element_of(V, name='sigma')

    Re = Constant('Re', real=True)
    dt = Constant('dt', real=True)
    alpha = Constant('alpha', real=True)

    int_0 = lambda expr: integral(domain, expr)

    s = BilinearForm((tau, sigma), int_0(dot(grad(tau), grad(sigma))))
    m = BilinearForm((tau, sigma), int_0(tau * sigma))
    b1 = BilinearForm((tau, dw), int_0(bracket(pn, dw) * tau))
    b2 = BilinearForm((tau, dp), int_0(bracket(dp, wn) * tau))

    l1 = LinearForm(
        tau, int_0(bracket(pn, wn) * tau - 1. / Re * dot(grad(tau), grad(wn))))

    expr = m(tau, dw) - alpha * dt * b1(tau, dw) - dt * b2(
        tau, dp) - (alpha * dt / Re) * s(tau, dw)
    a = BilinearForm(((tau, sigma), (dp, dw)), int_0(expr))

    l = LinearForm((tau, sigma), dt * l1(tau))

    bc = [EssentialBC(dp, 0, domain.boundary)]
    bc += [EssentialBC(dw, 0, domain.boundary)]
    equation = Equation(a, l, tests=[tau, sigma], trials=[dp, dw], bc=bc)
Example #3
0
def test_calculus_2d_1():
    domain = Domain('Omega', dim=2)

    V = ScalarFunctionSpace('V', domain)
    W = VectorFunctionSpace('W', domain)

    alpha, beta, gamma = [Constant(i) for i in ['alpha', 'beta', 'gamma']]

    f, g, h = elements_of(V, names='f, g, h')
    F, G, H = elements_of(W, names='F, G, H')

    # ... scalar gradient properties
    assert (grad(f + g) == grad(f) + grad(g))
    assert (grad(alpha * h) == alpha * grad(h))
    assert (grad(alpha * f + beta * g) == alpha * grad(f) + beta * grad(g))

    assert (grad(f * g) == f * grad(g) + g * grad(f))
    assert (grad(f / g) == -f * grad(g) / g**2 + grad(f) / g)

    assert (expand(grad(f * g * h)) == f * g * grad(h) + f * h * grad(g) +
            g * h * grad(f))
    # ...

    # ... vector gradient properties
    assert (grad(F + G) == grad(F) + grad(G))
    assert (grad(alpha * H) == alpha * grad(H))
    assert (grad(alpha * F + beta * G) == alpha * grad(F) + beta * grad(G))

    #assert( grad(dot(F,G)) == convect(F, G) + convect(G, F) + cross(F, curl(G)) - cross(curl(F), G) )
    # ...

    # ... curl properties
    assert (curl(f + g) == curl(f) + curl(g))
    assert (curl(alpha * h) == alpha * curl(h))
    assert (curl(alpha * f + beta * g) == alpha * curl(f) + beta * curl(g))
    # ...

    # ... laplace properties
    assert (laplace(f + g) == laplace(f) + laplace(g))
    assert (laplace(alpha * h) == alpha * laplace(h))
    assert (laplace(alpha * f + beta * g) == alpha * laplace(f) +
            beta * laplace(g))
    # ...

    # ... divergence properties
    assert (div(F + G) == div(F) + div(G))
    assert (div(alpha * H) == alpha * div(H))
    assert (div(alpha * F + beta * G) == alpha * div(F) + beta * div(G))

    #assert( div(cross(F,G)) == -dot(F, curl(G)) + dot(G, curl(F)) )
    # ...

    # ... rot properties
    assert (rot(F + G) == rot(F) + rot(G))
    assert (rot(alpha * H) == alpha * rot(H))
    assert (rot(alpha * F + beta * G) == alpha * rot(F) + beta * rot(G))
    # ...

    # ... Poisson's bracket properties
    assert bracket(alpha * f, g) == alpha * bracket(f, g)
    assert bracket(f, alpha * g) == alpha * bracket(f, g)
    assert bracket(f + h, g) == bracket(f, g) + bracket(h, g)
    assert bracket(f, g + h) == bracket(f, g) + bracket(f, h)
    assert bracket(f, f) == 0
    assert bracket(f, g) == -bracket(g, f)
    assert bracket(f, g * h) == g * bracket(f, h) + bracket(f, g) * h
Example #4
0
def test_zero_derivative():

    assert grad(1) == 0  # native int
    assert grad(2.3) == 0  # native float
    assert grad(4 + 5j) == 0  # native complex
    assert grad(Integer(1)) == 0  # sympy Integer
    assert grad(Float(2.3)) == 0  # sympy Float
    assert grad(Rational(6, 7)) == 0  # sympy Rational
    assert grad(Constant('a')) == 0  # sympde Constant

    assert laplace(1) == 0  # native int
    assert laplace(2.3) == 0  # native float
    assert laplace(4 + 5j) == 0  # native complex
    assert laplace(Integer(1)) == 0  # sympy Integer
    assert laplace(Float(2.3)) == 0  # sympy Float
    assert laplace(Rational(6, 7)) == 0  # sympy Rational
    assert laplace(Constant('a')) == 0  # sympde Constant

    assert hessian(1) == 0  # native int
    assert hessian(2.3) == 0  # native float
    assert hessian(4 + 5j) == 0  # native complex
    assert hessian(Integer(1)) == 0  # sympy Integer
    assert hessian(Float(2.3)) == 0  # sympy Float
    assert hessian(Rational(6, 7)) == 0  # sympy Rational
    assert hessian(Constant('a')) == 0  # sympde Constant

    # 2D convection of constant scalar field
    domain = Domain('Omega', dim=2)
    W = VectorFunctionSpace('W', domain)
    F = element_of(W, name='F')

    assert convect(F, 1) == 0  # native int
    assert convect(F, 2.3) == 0  # native float
    assert convect(F, 4 + 5j) == 0  # native complex
    assert convect(F, Integer(1)) == 0  # sympy Integer
    assert convect(F, Float(2.3)) == 0  # sympy Float
    assert convect(F, Rational(6, 7)) == 0  # sympy Rational
    assert convect(F, Constant('a')) == 0  # sympde Constant

    # 3D convection of constant scalar field
    domain = Domain('Omega', dim=3)
    Z = VectorFunctionSpace('Z', domain)
    G = element_of(Z, name='G')

    assert convect(G, 1) == 0  # native int
    assert convect(G, 2.3) == 0  # native float
    assert convect(G, 4 + 5j) == 0  # native complex
    assert convect(G, Integer(1)) == 0  # sympy Integer
    assert convect(G, Float(2.3)) == 0  # sympy Float
    assert convect(G, Rational(6, 7)) == 0  # sympy Rational
    assert convect(G, Constant('a')) == 0  # sympde Constant

    # Poisson's bracket in 2D
    domain = Domain('Omega', dim=2)
    V = ScalarFunctionSpace('V', domain)
    u = element_of(V, name='V')

    assert bracket(u, 1) == 0  # native int
    assert bracket(u, 2.3) == 0  # native float
    assert bracket(u, 4 + 5j) == 0  # native complex
    assert bracket(u, Integer(1)) == 0  # sympy Integer
    assert bracket(u, Float(2.3)) == 0  # sympy Float
    assert bracket(u, Rational(6, 7)) == 0  # sympy Rational
    assert bracket(u, Constant('a')) == 0  # sympde Constant

    assert bracket(1, u) == 0  # native int
    assert bracket(2.3, u) == 0  # native float
    assert bracket(4 + 5j, u) == 0  # native complex
    assert bracket(Integer(1), u) == 0  # sympy Integer
    assert bracket(Float(2.3), u) == 0  # sympy Float
    assert bracket(Rational(6, 7), u) == 0  # sympy Rational
    assert bracket(Constant('a'), u) == 0  # sympde Constant
Example #5
0
def test_linear_form_2d_1():

    domain = Domain('Omega', dim=2)
    B1 = Boundary(r'\Gamma_1', domain)

    x, y = domain.coordinates

    kappa = Constant('kappa', is_real=True)
    mu = Constant('mu', is_real=True)
    nn = NormalVector('nn')

    V = ScalarFunctionSpace('V', domain)
    W = VectorFunctionSpace('W', domain)

    u, u1, u2 = [element_of(V, name=i) for i in ['u', 'u1', 'u2']]
    v, v1, v2 = [element_of(V, name=i) for i in ['v', 'v1', 'v2']]
    # ...
    int_0 = lambda expr: integral(domain, expr)
    int_1 = lambda expr: integral(B1, expr)

    l = LinearForm(v, int_0(x * y * v))

    assert (l.domain == domain.interior)
    assert (l(v1) == int_0(x * y * v1))
    # ...

    # ...
    g = Tuple(x**2, y**2)
    l = LinearForm(v, int_1(v * dot(g, nn)))
    print(l)

    assert (l.domain == B1)
    assert (l(v1) == int_1(v1 * trace_1(g, B1)))
    # ...

    # ...
    g = Tuple(x**2, y**2)
    l = LinearForm(v, int_1(v * dot(g, nn)) + int_0(x * y * v))

    assert (len(l.domain.args) == 2)
    for i in l.domain.args:
        assert (i in [domain.interior, B1])

    assert (l(v1) == int_1(v1 * trace_1(g, B1)) + int_0(x * y * v1))
    # ...

    # ...
    l1 = LinearForm(v1, int_0(x * y * v1))
    l = LinearForm(v, l1(v))

    assert (l.domain == domain.interior)
    assert (l(u1) == int_0(x * y * u1))
    # ...

    # ...
    g = Tuple(x, y)
    l1 = LinearForm(u1, int_0(x * y * u1))
    l2 = LinearForm(u2, int_0(dot(grad(u2), g)))

    l = LinearForm(v, l1(v) + l2(v))

    assert (l.domain == domain.interior)
    assert (l(v1) == int_0(x * y * v1) + int_0(dot(grad(v1), g)))
    # ...

    # ...
    pn, wn = [element_of(V, name=i) for i in ['pn', 'wn']]

    tau = element_of(V, name='tau')
    sigma = element_of(V, name='sigma')

    Re = Constant('Re', real=True)
    dt = Constant('dt', real=True)
    alpha = Constant('alpha', real=True)

    l1 = LinearForm(
        tau, int_0(bracket(pn, wn) * tau - 1. / Re * dot(grad(tau), grad(wn))))

    l = LinearForm((tau, sigma), dt * l1(tau))

    assert (l.domain == domain.interior)
    assert (l(u1,
              u2).expand() == int_0(-1.0 * dt * dot(grad(u1), grad(wn)) / Re) +
            int_0(dt * u1 * bracket(pn, wn)))