예제 #1
0
파일: mesh.py 프로젝트: qsnake/libqsnake
    def export_mesh(self, lib="hermes2d"):
        """
        Exports the mesh in various FE solver formats.

        lib == "hermes2d" ... returns the hermes2d Mesh

        Currently only hermes2d is implemented.

        Example:

        >>> m = Mesh([[0.0,1.0],[1.0,1.0],[1.0,0.0],[0.0,0.0],],[[1,0,2],[2,0,3],],[[3,2,1],[2,1,2],[1,0,3],[0,3,4],],[])
        >>> h = m.export_mesh()
        >>> h
        <hermes2d._hermes2d.Mesh object at 0x7f07284721c8>

        """
        if lib == "hermes2d":
            from hermes2d import Mesh
            m = Mesh()
            nodes = self._nodes
            elements = [list(e)+[0] for e in self._elements]
            boundaries = self._boundaries
            curves = self._curves
            m.create(nodes, elements, boundaries, curves)
            return m
        else:
            raise NotImplementedError("unknown library")
예제 #2
0
def test_mesh_create1():
    mesh = Mesh()
    mesh.create([
           [0, 0],
           [1, 0],
           [1, 1],
           [0, 1],
       ], [
           [2, 3, 0, 1, 0],
       ], [
           [0, 1, 1],
           [1, 2, 1],
           [2, 3, 1],
           [3, 0, 1],
       ], [])
    assert compare(mesh.nodes, [[0, 0], [1, 0], [1, 1], [0, 1]])
    assert mesh.elements_markers == [[2, 3, 0, 1, 0]]
    assert mesh.elements == [[2, 3, 0, 1]]
    # not yet implemented:
    #assert mesh.boundaries == [[0, 1, 1], [1, 2, 1], [2, 3, 1], [3, 0, 1]]
    #assert mesh.nurbs is None
    mesh.refine_all_elements()
    assert compare(mesh.nodes, [(0, 0), (1, 0), (1, 1), (0, 1),
        (1, 0.5), (0.5, 0), (0, 0.5), (0.5, 1), (0.5, 0.5)])
    assert mesh.elements == [[2, 7, 8, 4], [7, 3, 6, 8], [8, 6, 0, 5],
            [4, 8, 5, 1]]
    mesh.refine_all_elements()
    assert mesh.nodes_dict == {0: (0.0, 0.0), 1: (1.0, 0.0), 2: (1.0, 1.0), 3:
            (0.0, 1.0), 4: (1.0, 0.5), 5: (0.5, 0.0), 6: (0.0, 0.5), 7: (0.5,
                1.0), 8: (0.5, 0.5), 9: (0.5, 0.75), 10: (0.25, 1.0), 12:
            (0.75, 1.0), 13: (0.25, 0.5), 14: (0.0, 0.75), 15: (0.5, 0.25), 16:
            (0.25, 0.0), 17: (0.0, 0.25), 18: (0.75, 0.25), 19: (1.0, 0.25),
            20: (0.75, 0.0), 21: (0.75, 0.5), 22: (1.0, 0.75), 23: (0.75,
                0.75), 36: (0.25, 0.75), 47: (0.25, 0.25)}
    assert mesh.elements == [[2, 12, 23, 22], [12, 7, 9, 23], [23, 9, 8, 21],
            [22, 23, 21, 4], [7, 10, 36, 9], [10, 3, 14, 36], [36, 14, 6, 13],
            [9, 36, 13, 8], [8, 13, 47, 15], [13, 6, 17, 47], [47, 17, 0, 16],
            [15, 47, 16, 5], [4, 21, 18, 19], [21, 8, 15, 18], [18, 15, 5, 20],
            [19, 18, 20, 1]]
예제 #3
0
def poisson_solver(mesh_tuple):
    """
    Poisson solver.

    mesh_tuple ... a tuple of (nodes, elements, boundary, nurbs)
    """
    set_verbose(False)
    mesh = Mesh()
    mesh.create(*mesh_tuple)
    mesh.refine_element(0)
    shapeset = H1Shapeset()
    pss = PrecalcShapeset(shapeset)

    # create an H1 space
    space = H1Space(mesh, shapeset)
    space.set_uniform_order(5)
    space.assign_dofs()

    # initialize the discrete problem
    wf = WeakForm(1)
    set_forms(wf)

    solver = DummySolver()
    sys = LinSystem(wf, solver)
    sys.set_spaces(space)
    sys.set_pss(pss)

    # assemble the stiffness matrix and solve the system
    sys.assemble()
    A = sys.get_matrix()
    b = sys.get_rhs()
    from scipy.sparse.linalg import cg
    x, res = cg(A, b)
    sln = Solution()
    sln.set_fe_solution(space, pss, x)
    return sln
예제 #4
0
파일: 22.py 프로젝트: solin/hermes2d
)
from hermes2d.examples.c22 import set_bc, set_forms

threshold = 0.3
strategy = 0
h_only = False
error_tol = 1
interactive_plotting = False  # should the plot be interactively updated
# during the calculation? (slower)
show_mesh = True
show_graph = True

set_verbose(False)

mesh = Mesh()
mesh.create([[0, 0], [1, 0], [1, 1], [0, 1]], [[2, 3, 0, 1, 0]], [[0, 1, 1], [1, 2, 1], [2, 3, 1], [3, 0, 1]], [])

mesh.refine_all_elements()

shapeset = H1Shapeset()
pss = PrecalcShapeset(shapeset)

space = H1Space(mesh, shapeset)
set_bc(space)
space.set_uniform_order(1)

wf = WeakForm(1)
set_forms(wf)

sln = Solution()
rsln = Solution()
예제 #5
0
def test_mesh_create2():
    m = Mesh()
    m.create([
            [0, -1],
            [1, -1],
            [-1, 0],
            [0, 0],
            [1, 0],
            [-1, 1],
            [0, 1],
            [0.707106781, 0.707106781],
        ], [
            [0, 1, 4, 3, 0],
            [3, 4, 7, 0],
            [3, 7, 6, 0],
            [2, 3, 6, 5, 0],
        ], [
            [0, 1, 1],
            [1, 4, 2],
            [3, 0, 4],
            [4, 7, 2],
            [7, 6, 2],
            [2, 3, 4],
            [6, 5, 2],
            [5, 2, 3],
        ], [
            [4, 7, 45],
            [7, 6, 45],
        ])
    assert compare(m.nodes, [
            [0, -1],
            [1, -1],
            [-1, 0],
            [0, 0],
            [1, 0],
            [-1, 1],
            [0, 1],
            [0.707106781, 0.707106781],
        ], eps=1e-4)
    assert m.elements_markers == [
            [0, 1, 4, 3, 0],
            [3, 4, 7, 0],
            [3, 7, 6, 0],
            [2, 3, 6, 5, 0],
        ]
    assert m.elements == [
            [0, 1, 4, 3],
            [3, 4, 7],
            [3, 7, 6],
            [2, 3, 6, 5],
        ]
    # This is not yet implemented:
    #assert m.boundaries == [
    #        [0, 1, 1],
    #        [1, 4, 2],
    #        [3, 0, 4],
    #        [4, 7, 2],
    #        [7, 6, 2],
    #        [2, 3, 4],
    #        [6, 5, 2],
    #        [5, 2, 3],
    #    ]
    #assert mesh.nurbs == ...
    m.refine_all_elements()
    assert m.elements == [
            [0, 11, 20, 19], [11, 1, 9, 20], [20, 9, 4, 8], [19, 20, 8, 3],
            [3, 8, 34], [8, 4, 33], [34, 33, 7], [33, 34, 8], [3, 34, 10],
            [34, 7, 12], [10, 12, 6], [12, 10, 34], [2, 18, 16, 15],
            [18, 3, 10, 16], [16, 10, 6, 17], [15, 16, 17, 5]
            ]
예제 #6
0
    def calc(threshold=0.3,
             strategy=0,
             h_only=False,
             error_tol=1,
             interactive_plotting=False,
             show_mesh=False,
             show_graph=True):
        mesh = Mesh()
        mesh.create([
            [0, 0],
            [1, 0],
            [1, 1],
            [0, 1],
        ], [
            [2, 3, 0, 1, 0],
        ], [
            [0, 1, 1],
            [1, 2, 1],
            [2, 3, 1],
            [3, 0, 1],
        ], [])

        mesh.refine_all_elements()

        shapeset = H1Shapeset()
        pss = PrecalcShapeset(shapeset)

        space = H1Space(mesh, shapeset)
        set_bc(space)
        space.set_uniform_order(1)

        wf = WeakForm(1)
        set_forms(wf)

        sln = Solution()
        rsln = Solution()
        solver = DummySolver()

        selector = H1ProjBasedSelector(CandList.HP_ANISO, 1.0, -1, shapeset)

        view = ScalarView("Solution")
        iter = 0
        graph = []
        while 1:
            space.assign_dofs()

            sys = LinSystem(wf, solver)
            sys.set_spaces(space)
            sys.set_pss(pss)
            sys.assemble()
            sys.solve_system(sln)
            dofs = sys.get_matrix().shape[0]
            if interactive_plotting:
                view.show(sln,
                          lib=lib,
                          notebook=True,
                          filename="a%02d.png" % iter)

            rsys = RefSystem(sys)
            rsys.assemble()

            rsys.solve_system(rsln)

            hp = H1Adapt([space])
            hp.set_solutions([sln], [rsln])
            err_est = hp.calc_error() * 100

            err_est = hp.calc_error(sln, rsln) * 100
            print "iter=%02d, err_est=%5.2f%%, DOFS=%d" % (iter, err_est, dofs)
            graph.append([dofs, err_est])
            if err_est < error_tol:
                break
            hp.adapt(selector, threshold, strategy)
            iter += 1

        if not interactive_plotting:
            view.show(sln, lib=lib, notebook=True)

        if show_mesh:
            mview = MeshView("Mesh")
            mview.show(mesh, lib="mpl", notebook=True, filename="b.png")

        if show_graph:
            from numpy import array
            graph = array(graph)
            import pylab
            pylab.clf()
            pylab.plot(graph[:, 0], graph[:, 1], "ko", label="error estimate")
            pylab.plot(graph[:, 0], graph[:, 1], "k-")
            pylab.title("Error Convergence for the Inner Layer Problem")
            pylab.legend()
            pylab.xlabel("Degrees of Freedom")
            pylab.ylabel("Error [%]")
            pylab.yscale("log")
            pylab.grid()
            pylab.savefig("graph.png")
예제 #7
0
파일: 22.py 프로젝트: xj361685640/hermes-1
                            # over this limit. This is to prevent h-adaptivity to go on forever.

H2DRS_DEFAULT_ORDER = -1 # A default order. Used to indicate an unkonwn order or a maximum support order

# Problem parameters.
SLOPE = 60           # Slope of the layer.

# Load the mesh
mesh = Mesh()
mesh.create([
        [0, 0],
        [1, 0],
        [1, 1],
        [0, 1],
    ], [
        [2, 3, 0, 1, 0],
    ], [
        [0, 1, 1],
        [1, 2, 1],
        [2, 3, 1],
        [3, 0, 1],
    ], [])

# Perform initial mesh refinements
mesh.refine_all_elements()

# Create an H1 space with default shapeset
space = H1Space(mesh, P_INIT)
set_bc(space)

# Initialize the weak formulation
예제 #8
0
def test_example_22():
    from hermes2d.examples.c22 import set_bc, set_forms

    #  The following parameters can be changed:
    SOLVE_ON_COARSE_MESH = True  # if true, coarse mesh FE problem is solved in every adaptivity step
    INIT_REF_NUM = 1  # Number of initial uniform mesh refinements
    P_INIT = 2  # Initial polynomial degree of all mesh elements.
    THRESHOLD = 0.3  # This is a quantitative parameter of the adapt(...) function and
    # it has different meanings for various adaptive strategies (see below).
    STRATEGY = 0  # Adaptive strategy:
    # STRATEGY = 0 ... refine elements until sqrt(THRESHOLD) times total
    #   error is processed. If more elements have similar errors, refine
    #   all to keep the mesh symmetric.
    # STRATEGY = 1 ... refine all elements whose error is larger
    #   than THRESHOLD times maximum element error.
    # STRATEGY = 2 ... refine all elements whose error is larger
    #   than THRESHOLD.
    # More adaptive strategies can be created in adapt_ortho_h1.cpp.
    CAND_LIST = CandList.H2D_HP_ANISO  # Predefined list of element refinement candidates.
    # Possible values are are attributes of the class CandList:
    # P_ISO, P_ANISO, H_ISO, H_ANISO, HP_ISO, HP_ANISO_H, HP_ANISO_P, HP_ANISO
    # See the Sphinx tutorial (http://hpfem.org/hermes2d/doc/src/tutorial-2.html#adaptive-h-fem-and-hp-fem) for details.
    MESH_REGULARITY = -1  # Maximum allowed level of hanging nodes:
    # MESH_REGULARITY = -1 ... arbitrary level hangning nodes (default),
    # MESH_REGULARITY = 1 ... at most one-level hanging nodes,
    # MESH_REGULARITY = 2 ... at most two-level hanging nodes, etc.
    # Note that regular meshes are not supported, this is due to
    # their notoriously bad performance.
    CONV_EXP = 0.5
    ERR_STOP = 0.1  # Stopping criterion for adaptivity (rel. error tolerance between the
    # fine mesh and coarse mesh solution in percent).
    NDOF_STOP = 60000  # Adaptivity process stops when the number of degrees of freedom grows
    # over this limit. This is to prevent h-adaptivity to go on forever.

    H2DRS_DEFAULT_ORDER = -1  # A default order. Used to indicate an unkonwn order or a maximum support order

    # Problem parameters.
    SLOPE = 60  # Slope of the layer.

    # Load the mesh
    mesh = Mesh()
    mesh.create([
        [0, 0],
        [1, 0],
        [1, 1],
        [0, 1],
    ], [
        [2, 3, 0, 1, 0],
    ], [
        [0, 1, 1],
        [1, 2, 1],
        [2, 3, 1],
        [3, 0, 1],
    ], [])

    # Perform initial mesh refinements
    mesh.refine_all_elements()

    # Create an H1 space with default shapeset
    space = H1Space(mesh, P_INIT)
    set_bc(space)

    # Initialize the weak formulation
    wf = WeakForm()
    set_forms(wf)

    # Initialize refinement selector
    selector = H1ProjBasedSelector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER)

    # Initialize the coarse mesh problem
    ls = LinSystem(wf)
    ls.set_spaces(space)

    # Adaptivity loop
    iter = 0
    done = False
    sln_coarse = Solution()
    sln_fine = Solution()

    # Assemble and solve the fine mesh problem
    rs = RefSystem(ls)
    rs.assemble()
    rs.solve_system(sln_fine)

    # Either solve on coarse mesh or project the fine mesh solution
    # on the coarse mesh.
    if SOLVE_ON_COARSE_MESH:
        ls.assemble()
        ls.solve_system(sln_coarse)

    # Calculate error estimate wrt. fine mesh solution
    hp = H1Adapt(ls)
    hp.set_solutions([sln_coarse], [sln_fine])
    err_est = hp.calc_error() * 100
예제 #9
0
def test_example_22():
    from hermes2d.examples.c22 import set_bc, set_forms

    #  The following parameters can be changed:
    SOLVE_ON_COARSE_MESH = True   # if true, coarse mesh FE problem is solved in every adaptivity step
    INIT_REF_NUM = 1               # Number of initial uniform mesh refinements
    P_INIT = 2              # Initial polynomial degree of all mesh elements.
    THRESHOLD = 0.3         # This is a quantitative parameter of the adapt(...) function and
                            # it has different meanings for various adaptive strategies (see below).
    STRATEGY = 0            # Adaptive strategy:
                                # STRATEGY = 0 ... refine elements until sqrt(THRESHOLD) times total
                                #   error is processed. If more elements have similar errors, refine
                                #   all to keep the mesh symmetric.
                                # STRATEGY = 1 ... refine all elements whose error is larger
                                #   than THRESHOLD times maximum element error.
                                # STRATEGY = 2 ... refine all elements whose error is larger
                                #   than THRESHOLD.
                                # More adaptive strategies can be created in adapt_ortho_h1.cpp.
    CAND_LIST = CandList.H2D_HP_ANISO  # Predefined list of element refinement candidates.
                            # Possible values are are attributes of the class CandList:
                            # P_ISO, P_ANISO, H_ISO, H_ANISO, HP_ISO, HP_ANISO_H, HP_ANISO_P, HP_ANISO
                            # See the Sphinx tutorial (http://hpfem.org/hermes2d/doc/src/tutorial-2.html#adaptive-h-fem-and-hp-fem) for details.
    MESH_REGULARITY = -1    # Maximum allowed level of hanging nodes:
                                # MESH_REGULARITY = -1 ... arbitrary level hangning nodes (default),
                                # MESH_REGULARITY = 1 ... at most one-level hanging nodes,
                                # MESH_REGULARITY = 2 ... at most two-level hanging nodes, etc.
                                # Note that regular meshes are not supported, this is due to
                                # their notoriously bad performance.
    CONV_EXP = 0.5
    ERR_STOP = 0.1         # Stopping criterion for adaptivity (rel. error tolerance between the
                                # fine mesh and coarse mesh solution in percent).
    NDOF_STOP = 60000       # Adaptivity process stops when the number of degrees of freedom grows
                                # over this limit. This is to prevent h-adaptivity to go on forever.

    H2DRS_DEFAULT_ORDER = -1 # A default order. Used to indicate an unkonwn order or a maximum support order

    # Problem parameters.
    SLOPE = 60           # Slope of the layer.

    # Load the mesh
    mesh = Mesh()
    mesh.create([
            [0, 0],
            [1, 0],
            [1, 1],
            [0, 1],
        ], [
            [2, 3, 0, 1, 0],
        ], [
            [0, 1, 1],
            [1, 2, 1],
            [2, 3, 1],
            [3, 0, 1],
        ], [])

    # Perform initial mesh refinements
    mesh.refine_all_elements()

    # Create an H1 space with default shapeset
    space = H1Space(mesh, P_INIT)
    set_bc(space)

    # Initialize the weak formulation
    wf = WeakForm()
    set_forms(wf)

    # Initialize refinement selector
    selector = H1ProjBasedSelector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER)

    # Initialize the coarse mesh problem
    ls = LinSystem(wf)
    ls.set_spaces(space)

    # Adaptivity loop
    iter = 0
    done =  False
    sln_coarse = Solution()
    sln_fine = Solution()
      
    # Assemble and solve the fine mesh problem
    rs = RefSystem(ls)
    rs.assemble()
    rs.solve_system(sln_fine)
    
    # Either solve on coarse mesh or project the fine mesh solution 
    # on the coarse mesh.   
    if SOLVE_ON_COARSE_MESH:
        ls.assemble()
        ls.solve_system(sln_coarse)

    # Calculate error estimate wrt. fine mesh solution
    hp = H1Adapt(ls)
    hp.set_solutions([sln_coarse], [sln_fine])
    err_est = hp.calc_error() * 100
예제 #10
0
파일: demos.py 프로젝트: hpfem/hermes2d
    def calc(
        threshold=0.3,
        strategy=0,
        h_only=False,
        error_tol=1,
        interactive_plotting=False,
        show_mesh=False,
        show_graph=True,
    ):
        mesh = Mesh()
        mesh.create(
            [[0, 0], [1, 0], [1, 1], [0, 1]], [[2, 3, 0, 1, 0]], [[0, 1, 1], [1, 2, 1], [2, 3, 1], [3, 0, 1]], []
        )

        mesh.refine_all_elements()

        shapeset = H1Shapeset()
        pss = PrecalcShapeset(shapeset)

        space = H1Space(mesh, shapeset)
        set_bc(space)
        space.set_uniform_order(1)

        wf = WeakForm(1)
        set_forms(wf)

        sln = Solution()
        rsln = Solution()
        solver = DummySolver()

        selector = H1ProjBasedSelector(CandList.HP_ANISO, 1.0, -1, shapeset)

        view = ScalarView("Solution")
        iter = 0
        graph = []
        while 1:
            space.assign_dofs()

            sys = LinSystem(wf, solver)
            sys.set_spaces(space)
            sys.set_pss(pss)
            sys.assemble()
            sys.solve_system(sln)
            dofs = sys.get_matrix().shape[0]
            if interactive_plotting:
                view.show(sln, lib=lib, notebook=True, filename="a%02d.png" % iter)

            rsys = RefSystem(sys)
            rsys.assemble()

            rsys.solve_system(rsln)

            hp = H1Adapt([space])
            hp.set_solutions([sln], [rsln])
            err_est = hp.calc_error() * 100

            err_est = hp.calc_error(sln, rsln) * 100
            print "iter=%02d, err_est=%5.2f%%, DOFS=%d" % (iter, err_est, dofs)
            graph.append([dofs, err_est])
            if err_est < error_tol:
                break
            hp.adapt(selector, threshold, strategy)
            iter += 1

        if not interactive_plotting:
            view.show(sln, lib=lib, notebook=True)

        if show_mesh:
            mview = MeshView("Mesh")
            mview.show(mesh, lib="mpl", notebook=True, filename="b.png")

        if show_graph:
            from numpy import array

            graph = array(graph)
            import pylab

            pylab.clf()
            pylab.plot(graph[:, 0], graph[:, 1], "ko", label="error estimate")
            pylab.plot(graph[:, 0], graph[:, 1], "k-")
            pylab.title("Error Convergence for the Inner Layer Problem")
            pylab.legend()
            pylab.xlabel("Degrees of Freedom")
            pylab.ylabel("Error [%]")
            pylab.yscale("log")
            pylab.grid()
            pylab.savefig("graph.png")
예제 #11
0
 def get_mesh(self):
     from hermes2d import Mesh
     m = Mesh()
     m.create(self._nodes, self._elements, self._boundaries, self._curves)
     return m