Esempio n. 1
0
    def ngsolve_vector_array_factory(length, dim, seed):

        if dim not in NGSOLVE_spaces:
            mesh = ngmsh.Mesh(dim=1)
            if dim > 0:
                pids = []
                for i in range(dim + 1):
                    pids.append(
                        mesh.Add(ngmsh.MeshPoint(ngmsh.Pnt(i / dim, 0, 0))))
                for i in range(dim):
                    mesh.Add(ngmsh.Element1D([pids[i], pids[i + 1]], index=1))

            NGSOLVE_spaces[dim] = NGSolveVectorSpace(
                ngs.L2(ngs.Mesh(mesh), order=0))

        U = NGSOLVE_spaces[dim].zeros(length)
        np.random.seed(seed)
        for v, a in zip(U._list, np.random.random((length, dim))):
            v.to_numpy()[:] = a
        if np.random.randint(2):
            UU = NGSOLVE_spaces[dim].zeros(length)
            for v, a in zip(UU._list, np.random.random((length, dim))):
                v.real_part.to_numpy()[:] = a
            for u, uu in zip(U._list, UU._list):
                u.imag_part = uu.real_part
        return U
Esempio n. 2
0
            def add_seg(i, j, os):
                base = p + i * d + j * deta
                p1 = base
                p2 = base + os
                ngmesh.Add(ngmsh.Element1D([points[p1], points[p2]], index=facenr))

                return
Esempio n. 3
0
def test_polynomial_ET_Segm(domain, alpha):
    order = alpha
    m = ngm.Mesh()
    m.dim = 1
    nel = 1
    
    pnums = []
    for i in range(0, nel+1):
        pnums.append (m.Add (ngm.MeshPoint (ngm.Pnt(i/nel, 0, 0))))
    
    for i in range(0,nel):
        m.Add (ngm.Element1D ([pnums[i],pnums[i+1]], index=1))
    
    m.Add (ngm.Element0D (pnums[0], index=1))
    m.Add (ngm.Element0D (pnums[nel], index=2))
    mesh = Mesh(m)
    
    x_ast = 0.78522
    levelset = x_ast-x
    referencevals = {POS:pow(x_ast, alpha+1)/(alpha+1), NEG:(1-pow(x_ast, alpha+1))/(alpha+1), IF: pow(x_ast, alpha)}
    V = H1(mesh, order=1)
    lset_approx = GridFunction(V)
    #InterpolateToP1(levelset,lset_approx)
    lset_approx.Set(levelset)
    
    f = x**alpha
    
    integral = Integrate(levelset_domain = { "levelset" : lset_approx, "domain_type" : domain},
                             cf=f, mesh=mesh, order = order)
    print("Result of Integration Key ",domain," : ", integral)
    error = abs(integral - referencevals[domain])
    print("Error: ", error)
    
    assert error < 5e-15*(order+1)*(order+1)
Esempio n. 4
0
 def _create_ngsolve_space(dim):
     if dim not in _NGSOLVE_spaces:
         mesh = ngmsh.Mesh(dim=1)
         if dim > 0:
             pids = []
             for i in range(dim + 1):
                 pids.append(mesh.Add(ngmsh.MeshPoint(ngmsh.Pnt(i / dim, 0, 0))))
             for i in range(dim):
                 mesh.Add(ngmsh.Element1D([pids[i], pids[i + 1]], index=1))
         _NGSOLVE_spaces[dim] = NGSolveVectorSpace(ngs.L2(ngs.Mesh(mesh), order=0))
     return _NGSOLVE_spaces[dim]
Esempio n. 5
0
def test_convection1d_dg():
    m = meshing.Mesh()
    m.dim = 1
    nel = 20
    pnums = []
    for i in range(0, nel + 1):
        pnums.append(m.Add(meshing.MeshPoint(Pnt(i / nel, 0, 0))))

    for i in range(0, nel):
        m.Add(meshing.Element1D([pnums[i], pnums[i + 1]], index=1))

    m.Add(meshing.Element0D(pnums[0], index=1))
    m.Add(meshing.Element0D(pnums[nel], index=2))
    m.AddPointIdentification(pnums[0],
                             pnums[nel],
                             identnr=1,
                             type=meshing.IdentificationType.PERIODIC)

    mesh = Mesh(m)

    fes = L2(mesh, order=4)

    u = fes.TrialFunction()
    v = fes.TestFunction()

    b = CoefficientFunction(1)
    bn = b * specialcf.normal(1)

    a = BilinearForm(fes)
    a += SymbolicBFI(-u * b * grad(v))
    a += SymbolicBFI(bn * IfPos(bn, u, u.Other()) * v, element_boundary=True)

    u = GridFunction(fes)
    pos = 0.5
    u0 = exp(-100 * (x - pos) * (x - pos))
    u.Set(u0)

    w = u.vec.CreateVector()

    t = 0
    tau = 1e-4
    tend = 1

    with TaskManager():
        while t < tend - tau / 2:
            a.Apply(u.vec, w)
            fes.SolveM(rho=CoefficientFunction(1), vec=w)
            u.vec.data -= tau * w
            t += tau

    l2error = sqrt(Integrate((u - u0) * (u - u0), mesh))
    print(l2error)
    assert l2error < 1e-2
Esempio n. 6
0
def MakeMesh1D():
    a = 0
    b = 1
    nel = 10
    m = msh.Mesh()
    m.dim = 1
    pnums = []

    for i in range(nel + 1):
        pnums.append(m.Add(msh.MeshPoint(msh.Pnt(a + (b - a) * i / nel, 0,
                                                 0))))

    for i in range(2):
        m.Add(msh.Element1D([pnums[i], pnums[i + 1]], index=1))
    for i in range(2, 5):
        m.Add(msh.Element1D([pnums[i], pnums[i + 1]], index=3))
    for i in range(5, 7):
        m.Add(msh.Element1D([pnums[i], pnums[i + 1]], index=1))
    for i in range(7, nel):
        m.Add(msh.Element1D([pnums[i], pnums[i + 1]], index=2))

    m.SetMaterial(1, 'base')
    m.SetMaterial(2, 'chip')
    m.SetMaterial(3, 'top')

    # add points
    m.Add(msh.Element0D(pnums[0], index=1))
    m.Add(msh.Element0D(pnums[2], index=2))
    m.Add(msh.Element0D(pnums[5], index=2))
    m.Add(msh.Element0D(pnums[7], index=2))
    m.Add(msh.Element0D(pnums[nel], index=2))

    # set boundary condition names
    m.SetBCName(0, 'bottom')
    m.SetBCName(1, 'other')

    m.Save('temp.vol')
    return m
Esempio n. 7
0
def test_1dlaplace():
    m = meshing.Mesh()
    m.dim = 1

    nel = 20

    pnums = []
    for i in range(0, nel + 1):
        pnums.append(m.Add(meshing.MeshPoint(meshing.Pnt(i / nel, 0, 0))))

    for i in range(0, nel):
        m.Add(meshing.Element1D([pnums[i], pnums[i + 1]], index=1))

    m.Add(meshing.Element0D(pnums[0], index=1))
    m.Add(meshing.Element0D(pnums[nel], index=2))

    mesh = Mesh(m)

    order = 10

    fes = H1(mesh, order=order, dirichlet=[1, 2])

    u = fes.TrialFunction()
    v = fes.TestFunction()

    n = specialcf.normal(mesh.dim)
    h = specialcf.mesh_size
    ugf = GridFunction(fes)
    uex = GridFunction(fes)
    uex.Set(sin(pi * x))

    a = BilinearForm(fes)
    a += SymbolicBFI(grad(u) * grad(v))

    l = LinearForm(fes)
    l += SymbolicLFI(pi * pi * uex * v)

    l.Assemble()
    a.Assemble()
    invmat = a.mat.Inverse(fes.FreeDofs())
    ugf.vec.data = invmat * l.vec
    error = sqrt(Integrate((ugf - uex) * (ugf - uex), mesh))
    assert error <= 1e-14
Esempio n. 8
0
m = ngm.Mesh()
m.dim = 1

n_elems = 1000
x_min = -50
x_max = 50
length = x_max - x_min
pnums = []
xs = []
for i in range(0, n_elems + 1):
    pnt_x = x_min + length * i / n_elems
    xs.append(pnt_x)
    pnums.append(m.Add(ngm.MeshPoint(ngm.Pnt(pnt_x, 0, 0))))

for i in range(0, n_elems):
    m.Add(ngm.Element1D([pnums[i], pnums[i + 1]], index=1))

m.SetMaterial(1, 'material')

m.Add(ngm.Element0D(pnums[0], index=1))
m.Add(ngm.Element0D(pnums[n_elems], index=2))

## NGSolve
mesh = ngs.Mesh(m)
fes = ngs.H1(mesh, order=1, dirichlet=[1, 2], complex=True)

u = fes.TrialFunction()
v = fes.TestFunction()

## Potentials
### Potential barrier
Esempio n. 9
0
def get_Netgen_nonconformal(N, scale, offset, dim=2):
    """
    Generate a structured quadrilateral/hexahedral NGSolve mesh over a
    prescribed square/cubic domain.

    Args:
        N (list): Number of mesh elements in each direction (N+1 nodes).
        scale (list): Extent of the meshed domain in each direction ([-2,2]
                      square -> scale=[4,4]).
        offset (list): Centers the meshed domain in each direction ([-2,2]
                       square -> offset=[2,2]).
        dim (int): Dimension of the domain (must be 2 or 3).

    Returns:
        mesh (Netgen mesh): Structured quadrilateral/hexahedral Netgen mesh.
    """

    # Construct a Netgen mesh.
    ngmesh = ngmsh.Mesh()

    if dim == 2:
        ngmesh.SetGeometry(unit_square)
        ngmesh.dim = 2

        # Set evenly spaced mesh nodes.
        points = []
        for i in range(N[1] + 1):
            for j in range(N[0] + 1):
                x = -offset[0] + scale[0] * j / N[0]
                y = -offset[1] + scale[1] * i / N[1]
                z = 0
                points.append(ngmesh.Add(ngmsh.MeshPoint(ngmsh.Pnt(x, y, z))))

        # TODO: Should the user be able to set their own BC names?
        idx_dom = ngmesh.AddRegion('dom', dim=2)
        idx_bottom = ngmesh.AddRegion('bottom', dim=1)
        idx_right = ngmesh.AddRegion('right', dim=1)
        idx_top = ngmesh.AddRegion('top', dim=1)
        idx_left = ngmesh.AddRegion('left', dim=1)

        # Generate mesh faces.
        for i in range(N[1]):
            for j in range(N[0]):
                p1 = i * (N[0] + 1) + j
                p2 = i * (N[0] + 1) + j + 1
                p3 = i * (N[0] + 1) + j + 2 + N[0]
                p4 = i * (N[0] + 1) + j + 1 + N[0]
                ngmesh.Add(ngmsh.Element2D(idx_dom, [points[p1], points[p2], points[p3], points[p4]]))

        # Assign each edge of the domain to the same boundary.
        for i in range(N[1]):
            ngmesh.Add(ngmsh.Element1D([points[N[0] + i * (N[0] + 1)], points[N[0] + (i + 1) * (N[0] + 1)]], index=idx_right))
            ngmesh.Add(ngmsh.Element1D([points[(i + 1) * (N[0] + 1)], points[i * (N[0] + 1)]], index=idx_left))

        for i in range(N[0]):
            ngmesh.Add(ngmsh.Element1D([points[i], points[i + 1]], index=idx_bottom))
            ngmesh.Add(ngmsh.Element1D([points[1 + i + N[1] * (N[0] + 1)], points[i + N[1] * (N[0] + 1)]], index=idx_top))

    elif dim == 3:
        ngmesh.dim = 3

        p1 = (0, 0, 0)
        p2 = (1, 1, 1)
        cube = ngcsg.OrthoBrick(ngcsg.Pnt(p1[0], p1[1], p1[2]), ngcsg.Pnt(p2[0], p2[1], p2[2])).bc(1)
        geo = ngcsg.CSGeometry()
        geo.Add(cube)
        ngmesh.SetGeometry(geo)

        # Set evenly spaced mesh nodes.
        points = []
        for i in range(N[0] + 1):
            for j in range(N[1] + 1):
                for k in range(N[2] + 1):
                    x = -offset[0] + scale[0] * i / N[0]
                    y = -offset[1] + scale[1] * j / N[1]
                    z = -offset[2] + scale[2] * k / N[2]
                    points.append(ngmesh.Add(ngmsh.MeshPoint(ngmsh.Pnt(x, y, z))))

        # Generate mesh cells.
        for i in range(N[0]):
            for j in range(N[1]):
                for k in range(N[2]):
                    base = i * (N[1] + 1) * (N[2] + 1) + j * (N[2] + 1) + k
                    baseup = base + (N[1] + 1) * (N[2] + 1)
                    p1 = base
                    p2 = base + 1
                    p3 = base + (N[2] + 1) + 1
                    p4 = base + (N[2] + 1)
                    p5 = baseup
                    p6 = baseup + 1
                    p7 = baseup + (N[2] + 1) + 1
                    p8 = baseup + (N[2] + 1)
                    idx = 1
                    ngmesh.Add(ngmsh.Element3D(idx,
                                             [points[p1], points[p2], points[p3], points[p4], points[p5], points[p6],
                                              points[p7], points[p8]]))

        def add_bc(p, d, N, deta, neta, facenr):

            def add_seg(i, j, os):
                base = p + i * d + j * deta
                p1 = base
                p2 = base + os
                ngmesh.Add(ngmsh.Element1D([points[p1], points[p2]], index=facenr))

                return

            for i in range(N):
                for j in [0, neta]:
                    add_seg(i, j, d)

            for i in [0, N]:
                for j in range(neta):
                    add_seg(i, j, deta)

            for i in range(N):
                for j in range(neta):
                    base = p + i * d + j * deta
                    p1 = base
                    p2 = base + d
                    p3 = base + d + deta
                    p4 = base + deta
                    ngmesh.Add(ngmsh.Element2D(facenr, [points[p1], points[p2], points[p3], points[p4]]))

            return

        # Order is important!
        ngmesh.Add(ngmsh.FaceDescriptor(surfnr=4, domin=1, bc=1))
        ngmesh.Add(ngmsh.FaceDescriptor(surfnr=2, domin=1, bc=2))
        ngmesh.Add(ngmsh.FaceDescriptor(surfnr=5, domin=1, bc=3))
        ngmesh.Add(ngmsh.FaceDescriptor(surfnr=3, domin=1, bc=4))
        ngmesh.Add(ngmsh.FaceDescriptor(surfnr=0, domin=1, bc=5))
        ngmesh.Add(ngmsh.FaceDescriptor(surfnr=1, domin=1, bc=6))

        # Assign each exterior face of the domain to its respective boundary.
        add_bc(0, 1, N[2], N[2] + 1, N[1], 1)
        add_bc(0, (N[1] + 1) * (N[2] + 1), N[0], 1, N[2], 2)
        add_bc((N[0] + 1) * (N[1] + 1) * (N[2] + 1) - 1, -(N[2] + 1), N[1], -1, N[2], 3)
        add_bc((N[0] + 1) * (N[1] + 1) * (N[2] + 1) - 1, -1, N[2], -(N[1] + 1) * (N[2] + 1), N[0], 4)
        add_bc(0, N[2] + 1, N[1], (N[1] + 1) * (N[2] + 1), N[0], 5)
        add_bc((N[0] + 1) * (N[1] + 1) * (N[2] + 1) - 1, -(N[1] + 1) * (N[2] + 1), N[0], -(N[2] + 1), N[1], 6)

        # TODO: Should the user be able to specify their own bc names?
        bc_names = {0: 'back', 1: 'left', 2: 'front', 3: 'right', 4: 'bottom', 5: 'top'}
        for key, val in bc_names.items():
            ngmesh.SetBCName(key, val)

    else:
        raise ValueError('Only works with 2D or 3D meshes.')

    return ngmesh
Esempio n. 10
0
def CartSquare(N, t_steps, xscale=1, xshift=0, tscale=1):
    ngmesh = ngm.Mesh()
    ngmesh.SetGeometry(unit_square)
    ngmesh.dim = 2
    pnums = []
    for j in range(t_steps + 1):
        for i in range(N + 1):
            pnums.append(
                ngmesh.Add(
                    ngm.MeshPoint(
                        ngm.Pnt((i / N) * xscale - xshift,
                                (j / t_steps) * tscale, 0))))

    foo = ngm.FaceDescriptor(surfnr=1, domin=1, bc=1)
    ngmesh.Add(foo)
    ngmesh.SetMaterial(1, "mat")
    for j in range(t_steps):
        for i in range(N):
            ngmesh.Add(
                ngm.Element2D(1, [
                    pnums[i + j * (N + 1)], pnums[i + 1 + j * (N + 1)],
                    pnums[i + 1 + (j + 1) * (N + 1)], pnums[i + (j + 1) *
                                                            (N + 1)]
                ]))

    fde = ngm.FaceDescriptor(surfnr=1, domin=1, bc=1)
    fde.bcname = "bottom"
    fdid = ngmesh.Add(fde)
    for i in range(N):
        ngmesh.Add(ngm.Element1D([pnums[i], pnums[i + 1]], index=1))

    fde = ngm.FaceDescriptor(surfnr=2, domin=1, bc=2)
    fde.bcname = "top"
    fdid = ngmesh.Add(fde)
    for i in range(N):
        ngmesh.Add(
            ngm.Element1D([
                pnums[i + t_steps * (N + 1)], pnums[i + 1 + t_steps * (N + 1)]
            ],
                          index=2))

    fde = ngm.FaceDescriptor(surfnr=3, domin=1, bc=3)
    fde.bcname = "dirichlet"
    fdid = ngmesh.Add(fde)
    for i in range(t_steps):
        ngmesh.Add(
            ngm.Element1D(
                [pnums[N + i * (N + 1)], pnums[N + (i + 1) * (N + 1)]],
                index=3))
        ngmesh.Add(
            ngm.Element1D(
                [pnums[0 + i * (N + 1)], pnums[0 + (i + 1) * (N + 1)]],
                index=3))

    ngmesh.SetBCName(0, "bottom")
    ngmesh.SetBCName(1, "top")
    ngmesh.SetBCName(2, "dirichlet")

    mesh = Mesh(ngmesh)
    # print("boundaries" + str(mesh.GetBoundaries()))
    return mesh