Пример #1
0
def run(nx):
    mesh = UnitSquareMesh(nx, nx)
    n = FacetNormal(mesh)
    h = CellSize(mesh)

    VFS = VectorFunctionSpace(mesh, "DG", 1)
    FS = FunctionSpace(mesh, "DG", 1)
    
    ic = project(Expression((mms.u(),mms.v()), degree=5), VFS)
    
    u = project(ic, VFS)

    f = project(Expression((mms.f_u(),mms.f_v()), degree=5), VFS)
    
    nu = project(Expression(mms.nu(), degree=5), FS)

    uFile = File("u.pvd")
    nuFile = File("nu.pvd")
    uFile << u
    nuFile << nu 

    i, j, k, l = indices(4)
    p, q, r, s = indices(4)

    v = TestFunction(VFS)

    # PARTIAL STRESS FORM
    # A = as_tensor( 
    #     [ [ [ [ 2*nu, 0.0 ], [0.0 , nu  ] ],
    #         [ [ 0.0 , nu  ], [0.0 , 0.0 ] ] ],
    #       [ [ [ 0.0 , 0.0 ], [nu  , 0.0 ] ],
    #         [ [ nu  , 0.0 ], [0.0 , 2*nu] ] ] ]
    #     )
    # TENSOR FORM
    A = as_tensor( 
        [ [ [ [ 1.0, 0.0 ], [0.0, 0.0] ],
            [ [ 0.0, 1.0 ], [0.0, 0.0] ] ],
          [ [ [ 0.0, 0.0 ], [1.0, 0.0] ],
            [ [ 0.0, 0.0 ], [0.0, 1.0] ] ] ]
        )

    un = jump(u)[i]*n("+")[j]
    un_ij = as_tensor(un, (i,j))
    vn = jump(v)[i]*n("+")[j]
    vn_ij = as_tensor(vn, (i,j))

    un_b = (u-ic)[i]*n[j]
    un_bij = as_tensor(un_b, (i,j))
    vn_b = v[i]*n[j]
    vn_bij = as_tensor(vn_b, (i,j))
    
    F_z = 0
    F_u = 0
    for i_ in range(2):
    
        F = (-
            ( 
                grad(u)[i_,j]*grad(v)[i_,j]*dx 
                )+ 
            (
                avg(grad(v)[i_,j])*un_ij[i_,j]*dS + 
                avg(grad(u)[i_,j])*vn_ij[i_,j]*dS + 
                grad(v)[i_,j]*un_bij[i_,j]*ds + 
                grad(u)[i_,j]*vn_bij[i_,j]*ds
                )+
            (
                un_ij[i_,j]*vn_ij[i_,j]*dS +
                un_bij[i_,j]*vn_bij[i_,j]*ds
                )-
            (
                v[i_]*u[i_]*dx 
                )-
            (
                f[i_]*v[i_]*dx
                )
            )
         
    print u.vector().array()   
    solve(F == 0, u)

    uFile << u
    
    Eu = errornorm(u, ic, norm_type="L2", degree_rise=3)
    print Eu

    return Eu
Пример #2
0
def run(nx):
    mesh = UnitSquareMesh(nx, nx)
    n = FacetNormal(mesh)
    h = CellSize(mesh)

    FS = FunctionSpace(mesh, "DG", 1)
    VFS = VectorFunctionSpace(mesh, "DG", 1)
    W = MixedFunctionSpace([FS, VFS, FS, VFS])
    w = Function(W)
    
    w_ic = project((Expression(
                (
                    mms.u(),
                    '0.0','0.0',
                    mms.v(),
                    '0.0','0.0'
                    )
                , W.ufl_element())), W)
    
    f = project((Expression(
                (
                    mms.f_u(),
                    '0.0','0.0',
                    mms.f_v(),
                    '0.0','0.0',
                    )
                , W.ufl_element())), W)
    
    test = TestFunction(W)
    trial = TrialFunction(W)
    L = 0; a = 0
    for i in range(len(w_ic)):
        a += inner(test[i], trial[i])*dx
        L += inner(test[i], w_ic[i])*dx
    solve(a == L, w)

    u_0, z_0, u_1, z_1 = w.split()
    u0File = File("u0.pvd")
    z0File = File("z0.pvd")
    u1File = File("u1.pvd")
    z1File = File("z1.pvd")
    u0File << u_0
    z0File << z_0
    u1File << u_1 
    z1File << z_1

    (v0, q0, v1, q1) = TestFunctions(W)
    (u0, z0, u1, z1) = (w[0], as_vector((w[1], w[2])), w[3], as_vector((w[4], w[5])))
    
    def F_z(z, u, q):
        return (inner(z, q)*dx - 
                inner(q, grad(u))*dx +
                inner(jump(u)*n("+"), avg(q))*dS
                )
    
    def F_u(z, u, u_b, f_, v):
        return (- inner(z, grad(v))*dx + 
                  inner(jump(v)*n("+"), avg(z))*dS +
                  (u-u_b)*v*ds -
                  v*u*dx -
                  f_*v*dx
                  )
    
    Fz = F_z(z = z0, u = u0, q = q0) + \
        F_z(z = z1, u = u1, q = q1)
    Fu = F_u(z = z0, u = u0, u_b = Expression(mms.u()), f_ = f[0], v = v0) + \
        F_u(z = z1, u = u1, u_b = Expression(mms.v()), f_ = f[3], v = v1) 
    F = Fz + Fu

    solve(F == 0, w)
    
    u_0, z_0, u_1, z_1 = w.split()
    u0File << u_0 
    z0File << z_0
    u1File << u_1 
    z1File << z_1
    
    FE = FunctionSpace(mesh, "DG", 3)
    S_u = project(Expression(mms.u(), degree=5), FE)
    S_v = project(Expression(mms.v(), degree=5), FE)

    Eu = errornorm(u_0, S_u, norm_type="L2", degree_rise=3)
    Ev = errornorm(u_1, S_v, norm_type="L2", degree_rise=3)

    return Eu, Ev
Пример #3
0
def run(nx):
    mesh = UnitSquareMesh(nx, nx)
    n = FacetNormal(mesh)
    h = CellSize(mesh)

    FS = FunctionSpace(mesh, "DG", 1)
    W = MixedFunctionSpace([FS, FS, FS, FS, FS, FS])
    w = Function(W)

    w_ic = project((Expression(
        (mms.u(), mms.v(), '0.0', '0.0', '0.0', '0.0'), W.ufl_element())), W)

    f = project((Expression((
        mms.f_u(),
        mms.f_v(),
        '0.0',
        '0.0',
        '0.0',
        '0.0',
    ), W.ufl_element())), W)

    test = TestFunction(W)
    trial = TrialFunction(W)
    L = 0
    a = 0
    for i in range(len(w_ic)):
        a += inner(test[i], trial[i]) * dx
        L += inner(test[i], w_ic[i]) * dx
    solve(a == L, w)

    nu = project(Expression(mms.nu()), FS)

    u_0, u_1, z_00, z_01, z_10, z_11 = w.split()
    u0File = File("u0.pvd")
    u1File = File("u1.pvd")
    nuFile = File("nu.pvd")
    z00File = File("z00.pvd")
    z01File = File("z01.pvd")
    z10File = File("z10.pvd")
    z11File = File("z11.pvd")
    u0File << u_0
    u1File << u_1
    nuFile << nu
    z00File << z_00
    z01File << z_01
    z10File << z_10
    z11File << z_11

    i, j, k, l = indices(4)
    p, q, r, s = indices(4)

    (v, q) = (as_vector(
        (test[0], test[1])), as_tensor(
            ((test[2], test[3]), (test[4], test[5]))))
    (u, z) = (as_vector((w[0], w[1])), as_tensor(((w[2], w[3]), (w[4], w[5]))))

    # PARTIAL STRESS FORM
    A = as_tensor([[[[2 * nu, 0.0], [0.0, nu]], [[0.0, nu], [0.0, 0.0]]],
                   [[[0.0, 0.0], [nu, 0.0]], [[nu, 0.0], [0.0, 2 * nu]]]])
    # TENSOR FORM
    # A = as_tensor(
    #     [ [ [ [ 1.0, 0.0 ], [0.0, 0.0] ],
    #         [ [ 0.0, 1.0 ], [0.0, 0.0] ] ],
    #       [ [ [ 0.0, 0.0 ], [1.0, 0.0] ],
    #         [ [ 0.0, 0.0 ], [0.0, 1.0] ] ] ]
    #     )

    u = as_vector((w_ic[0], w_ic[1]))

    un = jump(u)[i] * n("+")[j]
    un_ij = as_tensor(un, (i, j))
    vn = jump(v)[i] * n("+")[j]
    vn_ij = as_tensor(vn, (i, j))

    u_b = as_vector((w_ic[0], w_ic[1]))
    un_b = (u - u_b)[i] * n[j]
    un_bij = as_tensor(un_b, (i, j))
    vn_b = v[i] * n[j]
    vn_bij = as_tensor(vn_b, (i, j))

    F_z = 0
    F_u = 0
    for i_ in range(2):

        F_z += A[i_,j,r,s]*z[i_,j]*q[r,s]*dx - \
            q[i_,j]*grad(u)[i_,j]*dx + \
            un_ij[i_,j]*avg(q[i_,j])*dS + \
            un_bij[i_,j]*q[i_,j]*ds

        F_u = v[i_] * u[i_] * dx - v[i_] * u[i_] * dx

        # F_u += - z[i_,j]*grad(v)[i_,j]*dx + \
        #     vn_ij[i_,j]*avg(z[i_,j])*dS + \
        #     vn_bij[i_,j]*z[i_,j]*ds - \
        #     v[i_]*u[i_]*dx - \
        #     f[i_]*v[i_]*dx

    F = F_z + F_u

    solve(F == 0, w)

    u_0, u_1, z_00, z_01, z_10, z_11 = w.split()
    u0File << u_0
    u1File << u_1
    z00File << z_00
    z01File << z_01
    z10File << z_10
    z11File << z_11

    FE = FunctionSpace(mesh, "DG", 3)
    S_u = project(Expression(mms.u(), degree=5), FE)
    S_v = project(Expression(mms.v(), degree=5), FE)

    Eu = errornorm(u_0, S_u, norm_type="L2", degree_rise=3)
    Ev = errornorm(u_1, S_v, norm_type="L2", degree_rise=3)

    return Eu, Ev
Пример #4
0
def run(nx):
    mesh = UnitSquareMesh(nx, nx)
    n = FacetNormal(mesh)
    h = CellSize(mesh)

    FS = FunctionSpace(mesh, "DG", 1)
    W = MixedFunctionSpace([FS, FS, FS, FS, FS, FS])
    w = Function(W)
    
    w_ic = project((Expression(
                (
                    mms.u(),
                    mms.v(),
                    '0.0',
                    '0.0',
                    '0.0',
                    '0.0'
                    )
                , W.ufl_element())), W)
    
    f = project((Expression(
                (
                    mms.f_u(),
                    mms.f_v(),
                    '0.0',
                    '0.0',
                    '0.0',
                    '0.0',
                    )
                , W.ufl_element())), W)
    
    test = TestFunction(W)
    trial = TrialFunction(W)
    L = 0; a = 0
    for i in range(len(w_ic)):
        a += inner(test[i], trial[i])*dx
        L += inner(test[i], w_ic[i])*dx
    solve(a == L, w)

    nu = project(Expression(mms.nu()), FS)

    u_0, u_1, z_00, z_01, z_10, z_11 = w.split()
    u0File = File("u0.pvd")
    u1File = File("u1.pvd")
    nuFile = File("nu.pvd")
    z00File = File("z00.pvd")
    z01File = File("z01.pvd")
    z10File = File("z10.pvd")
    z11File = File("z11.pvd")
    u0File << u_0
    u1File << u_1 
    nuFile << nu 
    z00File << z_00
    z01File << z_01
    z10File << z_10
    z11File << z_11

    i, j, k, l = indices(4)
    p, q, r, s = indices(4)

    (v, q) = (as_vector((test[0], test[1])), 
              as_tensor(((test[2], test[3]),(test[4], test[5]))))
    (u, z) = (as_vector((w[0], w[1])), 
              as_tensor(((w[2], w[3]),(w[4], w[5]))))

    # PARTIAL STRESS FORM
    A = as_tensor( 
        [ [ [ [ 2*nu, 0.0 ], [0.0 , nu  ] ],
            [ [ 0.0 , nu  ], [0.0 , 0.0 ] ] ],
          [ [ [ 0.0 , 0.0 ], [nu  , 0.0 ] ],
            [ [ nu  , 0.0 ], [0.0 , 2*nu] ] ] ]
        )
    # TENSOR FORM
    # A = as_tensor( 
    #     [ [ [ [ 1.0, 0.0 ], [0.0, 0.0] ],
    #         [ [ 0.0, 1.0 ], [0.0, 0.0] ] ],
    #       [ [ [ 0.0, 0.0 ], [1.0, 0.0] ],
    #         [ [ 0.0, 0.0 ], [0.0, 1.0] ] ] ]
    #     )

    u = as_vector((w_ic[0],w_ic[1]))

    un = jump(u)[i]*n("+")[j]
    un_ij = as_tensor(un, (i,j))
    vn = jump(v)[i]*n("+")[j]
    vn_ij = as_tensor(vn, (i,j))

    u_b = as_vector((w_ic[0],w_ic[1]))
    un_b = (u-u_b)[i]*n[j]
    un_bij = as_tensor(un_b, (i,j))
    vn_b = v[i]*n[j]
    vn_bij = as_tensor(vn_b, (i,j))
    
    F_z = 0
    F_u = 0
    for i_ in range(2):

        F_z += A[i_,j,r,s]*z[i_,j]*q[r,s]*dx - \
            q[i_,j]*grad(u)[i_,j]*dx + \
            un_ij[i_,j]*avg(q[i_,j])*dS + \
            un_bij[i_,j]*q[i_,j]*ds

        F_u = v[i_]*u[i_]*dx - v[i_]*u[i_]*dx

        # F_u += - z[i_,j]*grad(v)[i_,j]*dx + \
        #     vn_ij[i_,j]*avg(z[i_,j])*dS + \
        #     vn_bij[i_,j]*z[i_,j]*ds - \
        #     v[i_]*u[i_]*dx - \
        #     f[i_]*v[i_]*dx
    
    F = F_z + F_u

    solve(F == 0, w)
    
    u_0, u_1, z_00, z_01, z_10, z_11 = w.split()
    u0File << u_0
    u1File << u_1 
    z00File << z_00
    z01File << z_01
    z10File << z_10
    z11File << z_11
    
    FE = FunctionSpace(mesh, "DG", 3)
    S_u = project(Expression(mms.u(), degree=5), FE)
    S_v = project(Expression(mms.v(), degree=5), FE)

    Eu = errornorm(u_0, S_u, norm_type="L2", degree_rise=3)
    Ev = errornorm(u_1, S_v, norm_type="L2", degree_rise=3)

    return Eu, Ev
Пример #5
0
def run(nx):
    mesh = UnitSquareMesh(nx, nx)
    n = FacetNormal(mesh)
    h = CellSize(mesh)

    VFS = VectorFunctionSpace(mesh, "DG", 1)
    FS = FunctionSpace(mesh, "DG", 1)

    ic = project(Expression((mms.u(), mms.v()), degree=5), VFS)

    u = project(ic, VFS)

    f = project(Expression((mms.f_u(), mms.f_v()), degree=5), VFS)

    nu = project(Expression(mms.nu(), degree=5), FS)

    uFile = File("u.pvd")
    nuFile = File("nu.pvd")
    uFile << u
    nuFile << nu

    i, j, k, l = indices(4)
    p, q, r, s = indices(4)

    v = TestFunction(VFS)

    # PARTIAL STRESS FORM
    # A = as_tensor(
    #     [ [ [ [ 2*nu, 0.0 ], [0.0 , nu  ] ],
    #         [ [ 0.0 , nu  ], [0.0 , 0.0 ] ] ],
    #       [ [ [ 0.0 , 0.0 ], [nu  , 0.0 ] ],
    #         [ [ nu  , 0.0 ], [0.0 , 2*nu] ] ] ]
    #     )
    # TENSOR FORM
    A = as_tensor([[[[1.0, 0.0], [0.0, 0.0]], [[0.0, 1.0], [0.0, 0.0]]],
                   [[[0.0, 0.0], [1.0, 0.0]], [[0.0, 0.0], [0.0, 1.0]]]])

    un = jump(u)[i] * n("+")[j]
    un_ij = as_tensor(un, (i, j))
    vn = jump(v)[i] * n("+")[j]
    vn_ij = as_tensor(vn, (i, j))

    un_b = (u - ic)[i] * n[j]
    un_bij = as_tensor(un_b, (i, j))
    vn_b = v[i] * n[j]
    vn_bij = as_tensor(vn_b, (i, j))

    F_z = 0
    F_u = 0
    for i_ in range(2):

        F = (-(grad(u)[i_, j] * grad(v)[i_, j] * dx) +
             (avg(grad(v)[i_, j]) * un_ij[i_, j] * dS + avg(grad(u)[i_, j]) *
              vn_ij[i_, j] * dS + grad(v)[i_, j] * un_bij[i_, j] * ds +
              grad(u)[i_, j] * vn_bij[i_, j] * ds) +
             (un_ij[i_, j] * vn_ij[i_, j] * dS +
              un_bij[i_, j] * vn_bij[i_, j] * ds) - (v[i_] * u[i_] * dx) -
             (f[i_] * v[i_] * dx))

    print u.vector().array()
    solve(F == 0, u)

    uFile << u

    Eu = errornorm(u, ic, norm_type="L2", degree_rise=3)
    print Eu

    return Eu
Пример #6
0
def run(nx):
    mesh = UnitSquareMesh(nx, nx)
    n = FacetNormal(mesh)
    h = CellSize(mesh)

    FS = FunctionSpace(mesh, "DG", 1)
    W = MixedFunctionSpace([FS, FS, FS, FS, FS, FS])
    w = Function(W)

    w_ic = project((Expression(
        (mms.u(), mms.v(), '0.0', '0.0', '0.0', '0.0'), W.ufl_element())), W)

    f = project((Expression((
        mms.f_u(),
        mms.f_v(),
        '0.0',
        '0.0',
        '0.0',
        '0.0',
    ), W.ufl_element())), W)

    test = TestFunction(W)
    trial = TrialFunction(W)
    L = 0
    a = 0
    for i in range(len(w_ic)):
        a += inner(test[i], trial[i]) * dx
        L += inner(test[i], w_ic[i]) * dx
    solve(a == L, w)

    u_0, u_1, z_00, z_01, z_10, z_11 = w.split()
    u0File = File("u0.pvd")
    u1File = File("u1.pvd")
    z00File = File("z00.pvd")
    z01File = File("z01.pvd")
    z10File = File("z10.pvd")
    z11File = File("z11.pvd")
    u0File << u_0
    u1File << u_1
    z00File << z_00
    z01File << z_01
    z10File << z_10
    z11File << z_11

    i, j, k, l = indices(4)
    p, q, r, s = indices(4)

    (v, q) = (as_vector(
        (test[0], test[1])), as_tensor(
            ((test[2], test[3]), (test[4], test[5]))))
    (u, z) = (as_vector((w[0], w[1])), as_tensor(((w[2], w[3]), (w[4], w[5]))))

    # int0ij = z[:,j]*q[:,j]
    # int0 = as_tensor(int0ij, (i,j))

    # int1 = q[i,j]*grad(u)[i,j]
    # int1i = as_tensor(int1, (i,j))

    un = jump(u)[i] * n("+")[j]
    un_ij = as_tensor(un, (i, j))
    vn = jump(v)[i] * n("+")[j]
    vn_ij = as_tensor(vn, (i, j))

    # int_2 = int2_0ij[i,j]*avg(q)[i,j]
    # int2i = as_vector(int2, (i))

    F_z = 0
    F_u = 0
    for i_ in range(2):
        F_z += z[i_,j]*q[i_,j]*dx - \
            q[i_,j]*grad(u)[i_,j]*dx + \
            un_ij[i_,j]*avg(q)[i_,j]*dS
        F_u += - z[i_,j]*grad(v)[i_,j]*dx + \
            vn_ij[i_,j]*avg(z)[i_,j]*dS + \
            (u[i_]-w_ic[i_])*v[i_]*ds - \
            v[i_]*u[i_]*dx - \
            f[i_]*v[i_]*dx

    # def F_z(z, u, q):
    #     return (inner(z, q)*dx -
    #             inner(q, grad(u))*dx +
    #             inner(jump(u)*n("+"), avg(q))*dS
    #             )

    # def F_u(z, u, u_b, f_, v):
    #     i,j = indices(2)

    #     if z[2] == 0:
    #         A0 = as_matrix([[2.0,0.0],[0.0,1.0]])
    #         A1 = as_matrix([[1.0,0.0],[0.0,0.0]])
    #     if z[2] == 1:
    #         A0 = as_matrix([[0.0,0.0],[0.0,1.0]])
    #         A1 = as_matrix([[1.0,0.0],[0.0,2.0]])

    #     Az0 = A0*z[0]
    #     # Az0 = as_vector(Az0i, i)
    #     Az1 = A1*z[1]
    #     # Az1 = as_vector(Az1i, i)

    #     return (- (inner(Az0,grad(v))*dx#  +
    #                # inner(Az1,grad(v))*dx
    #                ) +
    #               (inner(jump(v)*n("+"), avg(Az0))*dS#  +
    #                # inner(jump(v)*n("+"), avg(Az1))*dS
    #                ) +
    #               (u-u_b)*v*ds -
    #               v*u*dx -
    #               f_*v*dx
    #               )

    # Fz = F_z(z = z0, u = u0, q = q0) + \
    #     F_z(z = z1, u = u1, q = q1)
    # Fu = F_u(z = [z0,z1,0], u = u0, u_b = Expression(mms.u()), f_ = f[0], v = v0) + \
    #     F_u(z = [z0,z1,1], u = u1, u_b = Expression(mms.v()), f_ = f[3], v = v1)

    F = F_z + F_u

    solve(F == 0, w)

    u_0, u_1, z_00, z_01, z_10, z_11 = w.split()
    u0File << u_0
    u1File << u_1
    z00File << z_00
    z01File << z_01
    z10File << z_10
    z11File << z_11

    FE = FunctionSpace(mesh, "DG", 3)
    S_u = project(Expression(mms.u(), degree=5), FE)
    S_v = project(Expression(mms.v(), degree=5), FE)

    Eu = errornorm(u_0, S_u, norm_type="L2", degree_rise=3)
    Ev = errornorm(u_1, S_v, norm_type="L2", degree_rise=3)

    return Eu, Ev
Пример #7
0
def run(nx):
    mesh = UnitSquareMesh(nx, nx)
    n = FacetNormal(mesh)
    h = CellSize(mesh)

    FS = FunctionSpace(mesh, "DG", 1)
    W = MixedFunctionSpace([FS, FS, FS, FS, FS, FS])
    w = Function(W)

    w_ic = project((Expression((mms.u(), mms.v(), "0.0", "0.0", "0.0", "0.0"), W.ufl_element())), W)

    f = project((Expression((mms.f_u(), mms.f_v(), "0.0", "0.0", "0.0", "0.0"), W.ufl_element())), W)

    test = TestFunction(W)
    trial = TrialFunction(W)
    L = 0
    a = 0
    for i in range(len(w_ic)):
        a += inner(test[i], trial[i]) * dx
        L += inner(test[i], w_ic[i]) * dx
    solve(a == L, w)

    u_0, u_1, z_00, z_01, z_10, z_11 = w.split()
    u0File = File("u0.pvd")
    u1File = File("u1.pvd")
    z00File = File("z00.pvd")
    z01File = File("z01.pvd")
    z10File = File("z10.pvd")
    z11File = File("z11.pvd")
    u0File << u_0
    u1File << u_1
    z00File << z_00
    z01File << z_01
    z10File << z_10
    z11File << z_11

    i, j, k, l = indices(4)
    p, q, r, s = indices(4)

    (v, q) = (as_vector((test[0], test[1])), as_tensor(((test[2], test[3]), (test[4], test[5]))))
    (u, z) = (as_vector((w[0], w[1])), as_tensor(((w[2], w[3]), (w[4], w[5]))))

    # int0ij = z[:,j]*q[:,j]
    # int0 = as_tensor(int0ij, (i,j))

    # int1 = q[i,j]*grad(u)[i,j]
    # int1i = as_tensor(int1, (i,j))

    un = jump(u)[i] * n("+")[j]
    un_ij = as_tensor(un, (i, j))
    vn = jump(v)[i] * n("+")[j]
    vn_ij = as_tensor(vn, (i, j))

    # int_2 = int2_0ij[i,j]*avg(q)[i,j]
    # int2i = as_vector(int2, (i))

    F_z = 0
    F_u = 0
    for i_ in range(2):
        F_z += z[i_, j] * q[i_, j] * dx - q[i_, j] * grad(u)[i_, j] * dx + un_ij[i_, j] * avg(q)[i_, j] * dS
        F_u += (
            -z[i_, j] * grad(v)[i_, j] * dx
            + vn_ij[i_, j] * avg(z)[i_, j] * dS
            + (u[i_] - w_ic[i_]) * v[i_] * ds
            - v[i_] * u[i_] * dx
            - f[i_] * v[i_] * dx
        )

    # def F_z(z, u, q):
    #     return (inner(z, q)*dx -
    #             inner(q, grad(u))*dx +
    #             inner(jump(u)*n("+"), avg(q))*dS
    #             )

    # def F_u(z, u, u_b, f_, v):
    #     i,j = indices(2)

    #     if z[2] == 0:
    #         A0 = as_matrix([[2.0,0.0],[0.0,1.0]])
    #         A1 = as_matrix([[1.0,0.0],[0.0,0.0]])
    #     if z[2] == 1:
    #         A0 = as_matrix([[0.0,0.0],[0.0,1.0]])
    #         A1 = as_matrix([[1.0,0.0],[0.0,2.0]])

    #     Az0 = A0*z[0]
    #     # Az0 = as_vector(Az0i, i)
    #     Az1 = A1*z[1]
    #     # Az1 = as_vector(Az1i, i)

    #     return (- (inner(Az0,grad(v))*dx#  +
    #                # inner(Az1,grad(v))*dx
    #                ) +
    #               (inner(jump(v)*n("+"), avg(Az0))*dS#  +
    #                # inner(jump(v)*n("+"), avg(Az1))*dS
    #                ) +
    #               (u-u_b)*v*ds -
    #               v*u*dx -
    #               f_*v*dx
    #               )

    # Fz = F_z(z = z0, u = u0, q = q0) + \
    #     F_z(z = z1, u = u1, q = q1)
    # Fu = F_u(z = [z0,z1,0], u = u0, u_b = Expression(mms.u()), f_ = f[0], v = v0) + \
    #     F_u(z = [z0,z1,1], u = u1, u_b = Expression(mms.v()), f_ = f[3], v = v1)

    F = F_z + F_u

    solve(F == 0, w)

    u_0, u_1, z_00, z_01, z_10, z_11 = w.split()
    u0File << u_0
    u1File << u_1
    z00File << z_00
    z01File << z_01
    z10File << z_10
    z11File << z_11

    FE = FunctionSpace(mesh, "DG", 3)
    S_u = project(Expression(mms.u(), degree=5), FE)
    S_v = project(Expression(mms.v(), degree=5), FE)

    Eu = errornorm(u_0, S_u, norm_type="L2", degree_rise=3)
    Ev = errornorm(u_1, S_v, norm_type="L2", degree_rise=3)

    return Eu, Ev
Пример #8
0
def run(nx):
    mesh = UnitSquareMesh(nx, nx)
    n = FacetNormal(mesh)
    h = CellSize(mesh)

    FS = FunctionSpace(mesh, "DG", 1)
    VFS = VectorFunctionSpace(mesh, "DG", 1)
    W = MixedFunctionSpace([FS, VFS, FS, VFS])
    w = Function(W)

    w_ic = project((Expression(
        (mms.u(), '0.0', '0.0', mms.v(), '0.0', '0.0'), W.ufl_element())), W)

    f = project((Expression((
        mms.f_u(),
        '0.0',
        '0.0',
        mms.f_v(),
        '0.0',
        '0.0',
    ), W.ufl_element())), W)

    test = TestFunction(W)
    trial = TrialFunction(W)
    L = 0
    a = 0
    for i in range(len(w_ic)):
        a += inner(test[i], trial[i]) * dx
        L += inner(test[i], w_ic[i]) * dx
    solve(a == L, w)

    u_0, z_0, u_1, z_1 = w.split()
    u0File = File("u0.pvd")
    z0File = File("z0.pvd")
    u1File = File("u1.pvd")
    z1File = File("z1.pvd")
    u0File << u_0
    z0File << z_0
    u1File << u_1
    z1File << z_1

    (v0, q0, v1, q1) = TestFunctions(W)
    (u0, z0, u1, z1) = (w[0], as_vector(
        (w[1], w[2])), w[3], as_vector((w[4], w[5])))

    def F_z(z, u, q):
        return (inner(z, q) * dx - inner(q, grad(u)) * dx +
                inner(jump(u) * n("+"), avg(q)) * dS)

    def F_u(z, u, u_b, f_, v):
        return (-inner(z, grad(v)) * dx +
                inner(jump(v) * n("+"), avg(z)) * dS + (u - u_b) * v * ds -
                v * u * dx - f_ * v * dx)

    Fz = F_z(z = z0, u = u0, q = q0) + \
        F_z(z = z1, u = u1, q = q1)
    Fu = F_u(z = z0, u = u0, u_b = Expression(mms.u()), f_ = f[0], v = v0) + \
        F_u(z = z1, u = u1, u_b = Expression(mms.v()), f_ = f[3], v = v1)
    F = Fz + Fu

    solve(F == 0, w)

    u_0, z_0, u_1, z_1 = w.split()
    u0File << u_0
    z0File << z_0
    u1File << u_1
    z1File << z_1

    FE = FunctionSpace(mesh, "DG", 3)
    S_u = project(Expression(mms.u(), degree=5), FE)
    S_v = project(Expression(mms.v(), degree=5), FE)

    Eu = errornorm(u_0, S_u, norm_type="L2", degree_rise=3)
    Ev = errornorm(u_1, S_v, norm_type="L2", degree_rise=3)

    return Eu, Ev