예제 #1
0
def test_pickle_surface_fespaces():
    import netgen.meshing as meshing
    import netgen.csg as csg
    geo = csg.CSGeometry()
    bottom = csg.Plane(csg.Pnt(0, 0, 0), csg.Vec(0, 0, 1))
    surface = csg.SplineSurface(bottom)
    pts = [(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 0, 0)]
    geopts = [surface.AddPoint(*p) for p in pts]
    for p1, p2, bc in [(0, 1, "left"), (1, 2, "top"), (2, 3, "right"),
                       (3, 0, "bottom")]:
        surface.AddSegment(geopts[p1], geopts[p2], bc)
    geo.AddSplineSurface(surface)
    mesh = Mesh(
        geo.GenerateMesh(maxh=0.3,
                         perfstepsend=meshing.MeshingStep.MESHSURFACE))

    spaces = [
        HDivDivSurface(mesh, order=3, dirichlet=[1, 2, 3, 4]),
        FacetSurface(mesh, order=3, dirichlet=[1, 2, 3, 4]),
        HDivSurface(mesh, order=3, dirichlet=[1, 2, 3, 4])
    ]

    for space in spaces:
        with io.BytesIO() as f:
            pickler = pickle.Pickler(f)
            pickler.dump(space)
            data = f.getvalue()

        with io.BytesIO(data) as f:
            unpickler = pickle.Unpickler(f)
            space2 = unpickler.load()

        assert space.ndof == space2.ndof
예제 #2
0
def test_SurfaceGetFE(quads=False):
    # 2d surface in 3d tests
    import netgen.meshing as meshing
    import netgen.csg as csg
    geo = csg.CSGeometry()
    bottom = csg.Plane(csg.Pnt(0, 0, 0), csg.Vec(0, 0, 1))
    surface = csg.SplineSurface(bottom)
    pts = [(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 0, 0)]
    geopts = [surface.AddPoint(*p) for p in pts]
    for p1, p2, bc in [(0, 1, "left"), (1, 2, "top"), (2, 3, "right"),
                       (3, 0, "bottom")]:
        surface.AddSegment(geopts[p1], geopts[p2], bc)
    geo.AddSplineSurface(surface)

    mesh = Mesh(
        geo.GenerateMesh(perfstepsend=meshing.MeshingStep.MESHSURFACE,
                         quad=quads))

    for spacename in surfacespaces.keys():
        if quads and spaces2d[spacename]["quad"]:
            for order in surfacespaces[spacename]["order"]:
                space = FESpace(type=spacename, mesh=mesh, order=order)
                for vb in surfacespaces[spacename]["vorb"]:
                    for el in space.Elements(vb):
                        assert space.GetFE(el).ndof == len(
                            space.GetDofNrs(el)), [spacename, vb, order]
    return
예제 #3
0
def gen_3dbeam(maxh, nref, comm, lens=[10, 1, 1]):
    b = csg.OrthoBrick(csg.Pnt(-1, 0, 0), csg.Pnt(lens[0], lens[1],
                                                  lens[2])).bc("other")
    p = csg.Plane(csg.Pnt(0, 0, 0), csg.Vec(-1, 0, 0)).bc("left")
    geo = csg.CSGeometry()
    geo.Add(b * p)
    return gen_ref_mesh(geo, maxh, nref, comm)
예제 #4
0
 def __init__(self, zero, normal, *, eps=csg_eps):
     value = numpy.array(zero).dot(numpy.array(normal))
     super().__init__(
         netgen_csg.Plane(netgen_csg.Pnt(*zero), netgen_csg.Vec(*normal)),
         csg_boundaries=[
             dolfin.CompiledSubDomain(
                 'on_boundary && near(x[0]*n0 + x[1]*n1 + x[2]*n2, value, eps)',
                 n0=normal[0],
                 n1=normal[1],
                 n2=normal[2],
                 value=value,
                 eps=eps)
         ])
예제 #5
0
def cube_geo():
    origin = csg.Pnt(0, 0, 0)

    side = 1
    box = csg.OrthoBrick(origin, csg.Pnt(side, 2 * side,
                                         side)).bc('cube_outer')

    normal_vec = csg.Vec(0, 1, 0)
    topplane = csg.Plane(csg.Pnt(0, 1, 0), normal_vec).bc('dirichlet')

    cube = (box * topplane).mat('cube_mat')

    cube_geom = csg.CSGeometry()
    cube_geom.Add(cube)
    return cube_geom
예제 #6
0
def hinges_3d(N=4, touch=True):
    geo = csg.CSGeometry()
    plane_left = csg.Plane(csg.Pnt(0, 0, 0), csg.Vec(-1, 0, 0)).bc("left")
    plane_right = csg.Plane(csg.Pnt(1, 0, 0), csg.Vec(1, 0, 0)).bc("outer")
    plane_bot = csg.Plane(csg.Pnt(0, 0, 0), csg.Vec(0, -1, 0)).bc("outer")
    plane_top = csg.Plane(csg.Pnt(0, 1, 0), csg.Vec(0, 1, 0)).bc("outer")
    plane_back = csg.Plane(csg.Pnt(0, 0, 0), csg.Vec(0, 0, -1)).bc("outer")
    plane_front = csg.Plane(csg.Pnt(0, 0, 1), csg.Vec(0, 0, 1)).bc("outer")

    box = csg.OrthoBrick(csg.Pnt(-0.1, -0.1, -0.1),
                         csg.Pnt(1, 1, 1)).mat("mat_a").bc("outer")

    # N hinges, so N+1 boxes, or N+2 if we do not want to touch diri
    h = 1 / (N + 1)

    hinges = list()
    if touch:
        h0 = csg.OrthoBrick(csg.Pnt(-0.1, -0.1, -0.1),
                            csg.Pnt(h, h, 1.1)).mat("mat_b").bc("inner")
        hinges.append(h0)

    rmin = 1
    rmax = N
    hinges = hinges + [
        csg.OrthoBrick(csg.Pnt(k * h, k * h, -0.1),
                       csg.Pnt((k + 1) * h,
                               (k + 1) * h, 1.1)).mat("mat_b").bc("inner")
        for k in range(rmin, rmax)
    ]

    hl = csg.OrthoBrick(csg.Pnt(N * h, N * h, -0.1),
                        csg.Pnt(1.1, 1.1, 1.1)).mat("mat_b").bc("inner")
    hinges.append(hl)

    bmh = box - hinges[0]
    hs = hinges[0]
    for h in hinges[1:]:
        hs = hs + h
        bmh = bmh - h

    geo.Add(bmh * plane_left * plane_right * plane_bot * plane_top *
            plane_back * plane_front)
    geo.Add(hs * plane_left * plane_right * plane_bot * plane_top *
            plane_back * plane_front)

    return geo
예제 #7
0
파일: STL.py 프로젝트: wlgjs622/OpenPNM
    def save(cls,
             network,
             phases=[],
             filename='',
             maxsize='auto',
             fileformat='STL Format',
             logger_level=0):
        r"""
        Saves (transient/steady-state) data from the given objects into the
        specified file.

        Parameters
        ----------
        network : OpenPNM Network Object.
            The network containing the desired data.

        phases : list of OpenPNM Phase Objects (place holder, default is none).

        filename : string (optional).
            The name of the file containing the data to export.

        maxsize : a float or a string "auto" (optional).
            The maximum size of the mesh elements allowed. "auto" corresponds
            to an automatic determination based on pores and throats sizes. Any
            float value will be used as a maximum size. Small values result in
            finner meshes, but slower mesh calculations.

        fileformat : string (optional).
            Default is "STL Format" which corresponds to STL format. Other
            formats such as Gmsh and Fluent are supported (see ngsolve.org).

        logger_level : integer between 0 and 7 (optional).
            Default is 0. The logger level set in netgen package.

        Notes
        -----
        This method only saves the geometry of the network, not any of the
        pore-scale models or other attributes.  To save an actual OpenPNM
        Project use the ``Workspace`` object.

        """
        try:
            import netgen.csg as csg
        except ModuleNotFoundError:
            logger.error('Module "netgen.csg" not found.')
        try:
            from netgen.meshing import SetMessageImportance as log
            log(logger_level)
        except ModuleNotFoundError:
            logger.warning('Module "netgen.meshing" not found. ' +
                           'The "logger_level" ignored.')

        project, network, phases = cls._parse_args(network=network,
                                                   phases=phases)
        network = network[0]

        if filename == '':
            filename = project.name
        path = cls._parse_filename(filename=filename, ext='stl')
        # Path is a pathlib object, so slice it up as needed
        fname_stl = path.name

        # correct connections where 'pore.diameter' = 'throat.diameter'
        dt = network['throat.diameter'].copy()
        dp = network['pore.diameter'][network['throat.conns']]
        dt[dp[:, 0] == dt] *= 0.99
        dt[dp[:, 1] == dt] *= 0.99

        scale = max(network['pore.diameter'].max(), dt.max(),
                    network['throat.length'].max())
        if maxsize == 'auto':
            maxsize = min(network['pore.diameter'].min(), dt.min(),
                          network['throat.length'].min())
        geo = csg.CSGeometry()

        # define pores
        geometry = csg.Sphere(
            csg.Pnt(network['pore.coords'][0, 0] / scale,
                    network['pore.coords'][0, 1] / scale,
                    network['pore.coords'][0, 2] / scale),
            network['pore.diameter'][0] / scale / 2)
        for p in range(1, network.Np):
            pore = csg.Sphere(
                csg.Pnt(network['pore.coords'][p, 0] / scale,
                        network['pore.coords'][p, 1] / scale,
                        network['pore.coords'][p, 2] / scale),
                network['pore.diameter'][p] / scale / 2)
            geometry += pore

        # define throats
        for t in range(network.Nt):
            A = network['throat.endpoints.tail'][t, :] / scale
            B = network['throat.endpoints.head'][t, :] / scale
            V = (B - A) / _np.linalg.norm(B - A)
            plane1 = csg.Plane(csg.Pnt(A[0], A[1], A[2]),
                               csg.Vec(-V[0], -V[1], -V[2]))
            plane2 = csg.Plane(csg.Pnt(B[0], B[1], B[2]),
                               csg.Vec(V[0], V[1], V[2]))
            cylinder = csg.Cylinder(csg.Pnt(A[0], A[1], A[2]),
                                    csg.Pnt(B[0], B[1], B[2]),
                                    dt[t] / scale / 2)
            throat = cylinder * plane1 * plane2
            geometry += throat

        # add pore and throats to geometry, build mesh, rescale, and export
        geo.Add(geometry)
        mesh = geo.GenerateMesh(maxh=maxsize / scale)
        mesh.Scale(scale)
        mesh.Export(filename=fname_stl, format=fileformat)