Exemple #1
0
def poisson_solver(rho, prec=0.1):
    """
    Solves the Poisson equation \Nabla^2\phi = \rho.

    prec ... the precision of the solution in percents

    Returns the solution.
    """
    mesh = Mesh()
    mesh.load("square.mesh")
    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_poisson(wf, rho)
    solver = DummySolver()

    # assemble the stiffness matrix and solve the system
    for i in range(10):
        sys = LinSystem(wf, solver)
        sys.set_spaces(space)
        sys.set_pss(pss)

        sln = Solution()
        print "poisson: assembly coarse"
        sys.assemble()
        print "poisson: done"
        sys.solve_system(sln)

        rp = RefSystem(sys)
        rsln = Solution()
        print "poisson: assembly reference"
        rp.assemble()
        print "poisson: done"
        rp.solve_system(rsln)

        hp = H1OrthoHP(space)
        error = hp.calc_error(sln, rsln) * 100
        print "iteration: %d, error: %f" % (i, error)
        if error < prec:
            print "Error less than %f%%, we are done." % prec
            break
        hp.adapt(0.3)
        space.assign_dofs()
    return sln
Exemple #2
0
def test_example_12():
    from hermes2d.examples.c12 import set_bc, set_forms
    from hermes2d.examples import get_example_mesh

    #  The following parameters can be changed:
    P_INIT = 1  # Initial polynomial degree of all mesh elements.
    THRESHOLD = 0.6  # 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.
    ADAPT_TYPE = 0  # Type of automatic adaptivity:
    # ADAPT_TYPE = 0 ... adaptive hp-FEM (default),
    # ADAPT_TYPE = 1 ... adaptive h-FEM,
    # ADAPT_TYPE = 2 ... adaptive p-FEM.
    ISO_ONLY = False  # Isotropic refinement flag (concerns quadrilateral elements only).
    # ISO_ONLY = false ... anisotropic refinement of quad elements
    # is allowed (default),
    # ISO_ONLY = true ... only isotropic refinements of quad elements
    # are allowed.
    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.
    ERR_STOP = 0.01  # Stopping criterion for adaptivity (rel. error tolerance between the
    # fine mesh and coarse mesh solution in percent).
    NDOF_STOP = 40000  # Adaptivity process stops when the number of degrees of freedom grows
    # over this limit. This is to prevent h-adaptivity to go on forever.

    # Load the mesh
    mesh = Mesh()
    mesh.load(get_example_mesh())
    # mesh.load("hermes2d/examples/12.mesh")

    # Initialize the shapeset and the cache
    shapeset = H1Shapeset()
    pss = PrecalcShapeset(shapeset)

    # Create finite element space
    space = H1Space(mesh, shapeset)
    set_bc(space)
    space.set_uniform_order(P_INIT)

    # Enumerate basis functions
    space.assign_dofs()

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

    # Matrix solver
    solver = DummySolver()

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

    # Solve the coarse mesh problem
    ls = LinSystem(wf, solver)
    ls.set_spaces(space)
    ls.set_pss(pss)

    ls.assemble()
    ls.solve_system(sln_coarse)

    # Solve the fine mesh problem
    rs = RefSystem(ls)
    rs.assemble()
    rs.solve_system(sln_fine)

    # Calculate element errors and total error estimate
    hp = H1OrthoHP(space)
    err_est = hp.calc_error(sln_coarse, sln_fine) * 100
Exemple #3
0
def test_example_11():
    from hermes2d.examples.c11 import set_bc, set_wf_forms, set_hp_forms

    # The following parameters can be changed: In particular, compare hp- and
    # h-adaptivity via the ADAPT_TYPE option, and compare the multi-mesh vs. single-mesh
    # using the MULTI parameter.
    P_INIT = 1  # Initial polynomial degree of all mesh elements.
    MULTI = True  # MULTI = true  ... use multi-mesh,
    # MULTI = false ... use single-mesh.
    # Note: In the single mesh option, the meshes are
    # forced to be geometrically the same but the
    # polynomial degrees can still vary.
    SAME_ORDERS = True  # SAME_ORDERS = true ... when single-mesh is used,
    # this forces the meshes for all components to be
    # identical, including the polynomial degrees of
    # corresponding elements. When multi-mesh is used,
    # this parameter is ignored.
    THRESHOLD = 0.3  # This is a quantitative parameter of the adapt(...) function and
    # it has different meanings for various adaptive strategies (see below).
    STRATEGY = 1  # 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.
    ADAPT_TYPE = 0  # Type of automatic adaptivity:
    # ADAPT_TYPE = 0 ... adaptive hp-FEM (default),
    # ADAPT_TYPE = 1 ... adaptive h-FEM,
    # ADAPT_TYPE = 2 ... adaptive p-FEM.
    ISO_ONLY = False  # Isotropic refinement flag (concerns quadrilateral elements only).
    # ISO_ONLY = false ... anisotropic refinement of quad elements
    # is allowed (default),
    # ISO_ONLY = true ... only isotropic refinements of quad elements
    # are allowed.
    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.
    MAX_ORDER = 10  # Maximum allowed element degree
    ERR_STOP = 0.5  # Stopping criterion for adaptivity (rel. error tolerance between the
    # fine mesh and coarse mesh solution in percent).
    NDOF_STOP = 40000  # Adaptivity process stops when the number of degrees of freedom grows over
    # this limit. This is mainly to prevent h-adaptivity to go on forever.

    # Problem constants
    E = 200e9  # Young modulus for steel: 200 GPa
    nu = 0.3  # Poisson ratio
    lamda = (E * nu) / ((1 + nu) * (1 - 2 * nu))
    mu = E / (2 * (1 + nu))

    # Load the mesh
    xmesh = Mesh()
    ymesh = Mesh()
    xmesh.load(get_bracket_mesh())

    # initial mesh refinements
    xmesh.refine_element(1)
    xmesh.refine_element(4)

    # Create initial mesh for the vertical displacement component,
    # identical to the mesh for the horizontal displacement
    # (bracket.mesh becomes a master mesh)
    ymesh.copy(xmesh)

    # Initialize the shapeset and the cache
    shapeset = H1Shapeset()
    xpss = PrecalcShapeset(shapeset)
    ypss = PrecalcShapeset(shapeset)

    # Create the x displacement space
    xdisp = H1Space(xmesh, shapeset)
    set_bc(xdisp)
    xdisp.set_uniform_order(P_INIT)

    # Create the x displacement space
    ydisp = H1Space(ymesh, shapeset)
    set_bc(ydisp)
    ydisp.set_uniform_order(P_INIT)

    # Enumerate basis functions
    ndofs = xdisp.assign_dofs()
    ydisp.assign_dofs(ndofs)

    # Initialize the weak formulation
    wf = WeakForm(2)
    set_wf_forms(wf)

    # Matrix solver
    solver = DummySolver()

    # adaptivity loop
    it = 1
    done = False
    cpu = 0.0

    x_sln_coarse = Solution()
    y_sln_coarse = Solution()

    x_sln_fine = Solution()
    y_sln_fine = Solution()

    # Calculating the number of degrees of freedom
    ndofs = xdisp.assign_dofs()
    ndofs += ydisp.assign_dofs(ndofs)

    # Solve the coarse mesh problem
    ls = LinSystem(wf, solver)
    ls.set_spaces(xdisp, ydisp)
    ls.set_pss(xpss, ypss)
    ls.assemble()
    ls.solve_system(x_sln_coarse, y_sln_coarse)

    # View the solution -- this can be slow; for illustration only
    stress_coarse = VonMisesFilter(x_sln_coarse, y_sln_coarse, mu, lamda)

    # Solve the fine mesh problem
    rs = RefSystem(ls)
    rs.assemble()
    rs.solve_system(x_sln_fine, y_sln_fine)

    # Calculate element errors and total error estimate
    hp = H1OrthoHP(xdisp, ydisp)
    set_hp_forms(hp)
    err_est = hp.calc_error_2(x_sln_coarse, y_sln_coarse, x_sln_fine, y_sln_fine) * 100

    # Show the fine solution - this is the final result
    stress_fine = VonMisesFilter(x_sln_fine, y_sln_fine, mu, lamda)
Exemple #4
0
ls.set_spaces(space)

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

while (not done):
    print("\n---- Adaptivity step %d ---------------------------------------------\n" % (it+1))
    it += 1

    # 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)
    else:
        ls.project_global(sln_fine, sln_coarse)
        
    # View the solution and mesh
    sview.show(sln_coarse);
    mesh.plot(space=space)

    # Calculate error estimate wrt. fine mesh solution
    hp = H1Adapt(ls)
Exemple #5
0
    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="mayavi", filename="a%02d.png" % iter)
        if show_mesh:
            mview.show(mesh, lib="mpl", method="orders", filename="b%02d.png" % iter)

    rsys = RefSystem(sys)
    rsys.assemble()

    rsys.solve_system(rsln)

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

if not interactive_plotting:
    view.show(sln, lib="mayavi")
    if show_mesh:
        mview = MeshView("Mesh")
        mview.show(mesh, lib="mpl", method="orders")
Exemple #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")
Exemple #7
0
ls = LinSystem(wf)
ls.set_spaces(space)

# Adaptivity loop
iter = 0
done =  False
print "Calculating..."
sln_coarse = Solution()
sln_fine = Solution()

while (not done):
    
    # 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)
    else:
        ls.project_global(sln_fine, sln_coarse)
    
    # View the solution and mesh
    sview.show(sln_coarse);
    mesh.plot(space=space)
        
    # Calculate error estimate wrt. fine mesh solution
    hp = H1Adapt(ls)
Exemple #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
Exemple #9
0
def test_example_11():
    from hermes2d.examples.c11 import set_bc, set_wf_forms, set_hp_forms

    SOLVE_ON_COARSE_MESH = True  # If true, coarse mesh FE problem is solved in every adaptivity step.
    P_INIT_U = 2  # Initial polynomial degree for u
    P_INIT_V = 2  # Initial polynomial degree for v
    INIT_REF_BDY = 3  # Number of initial boundary refinements
    MULTI = True  # MULTI = true  ... use multi-mesh,
    # MULTI = false ... use single-mesh.
    # Note: In the single mesh option, the meshes are
    # forced to be geometrically the same but the
    # polynomial degrees can still vary.
    THRESHOLD = 0.3  # This is a quantitative parameter of the adapt(...) function and
    # it has different meanings for various adaptive strategies (see below).
    STRATEGY = 1  # 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 = 1  # Default value is 1.0. This parameter influences the selection of
    # cancidates in hp-adaptivity. See get_optimal_refinement() for details.
    MAX_ORDER = 10  # Maximum allowed element degree
    ERR_STOP = 0.5  # 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 mainly 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

    # Load the mesh
    umesh = Mesh()
    vmesh = Mesh()
    umesh.load(get_bracket_mesh())
    if MULTI == False:
        umesh.refine_towards_boundary(1, INIT_REF_BDY)

    # Create initial mesh (master mesh).
    vmesh.copy(umesh)

    # Initial mesh refinements in the vmesh towards the boundary
    if MULTI == True:
        vmesh.refine_towards_boundary(1, INIT_REF_BDY)

    # Create the x displacement space
    uspace = H1Space(umesh, P_INIT_U)
    vspace = H1Space(vmesh, P_INIT_V)

    # Initialize the weak formulation
    wf = WeakForm(2)
    set_wf_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(uspace, vspace)

    u_sln_coarse = Solution()
    v_sln_coarse = Solution()
    u_sln_fine = Solution()
    v_sln_fine = Solution()

    # Assemble and Solve the fine mesh problem
    rs = RefSystem(ls)
    rs.assemble()
    rs.solve_system(u_sln_fine, v_sln_fine, lib="scipy")

    # 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(u_sln_coarse, v_sln_coarse, lib="scipy")

    # Calculate element errors and total error estimate
    hp = H1Adapt(ls)
    hp.set_solutions([u_sln_coarse, v_sln_coarse], [u_sln_fine, v_sln_fine])
    set_hp_forms(hp)
    err_est = hp.calc_error() * 100
Exemple #10
0
def test_example_10():
    from hermes2d.examples.c10 import set_bc, set_forms
    from hermes2d.examples import get_motor_mesh

    # The following parameters can be changed:
    SOLVE_ON_COARSE_MESH = True  # If true, coarse mesh FE problem is solved in every adaptivity step
    P_INIT = 2  # Initial polynomial degree of all mesh elements.
    THRESHOLD = 0.2  # This is a quantitative parameter of the adapt(...) function and
    # it has different meanings for various adaptive strategies (see below).

    STRATEGY = 1  # 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_H  # Predefined list of element refinement candidates.
    # Possible values are are attributes of the class CandList:
    # H2D_P_ISO, H2D_P_ANISO, H2D_H_ISO, H2D_H_ANISO, H2D_HP_ISO, H2D_HP_ANISO_H, H2D_HP_ANISO_P, H2D_HP_ANISO
    # See User Documentation 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.

    ERR_STOP = 1.0  # Stopping criterion for adaptivity (rel. error tolerance between the
    # fine mesh and coarse mesh solution in percent).
    CONV_EXP = 1.0
    # Default value is 1.0. This parameter influences the selection of
    # cancidates in hp-adaptivity. See get_optimal_refinement() for details.
    # 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

    # Load the mesh
    mesh = Mesh()
    mesh.load(get_motor_mesh())

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

    # Initialize the discrete problem
    wf = WeakForm()
    set_forms(wf)

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

    # Initialize the linear system.
    ls = LinSystem(wf)
    ls.set_spaces(space)

    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 element errors and total error estimate
    hp = H1Adapt(ls)
    hp.set_solutions([sln_coarse], [sln_fine])
    err_est = hp.calc_error() * 100
Exemple #11
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
Exemple #12
0
def test_example_11():
    from hermes2d.examples.c11 import set_bc, set_wf_forms, set_hp_forms

    SOLVE_ON_COARSE_MESH = True  # If true, coarse mesh FE problem is solved in every adaptivity step.
    P_INIT_U = 2             # Initial polynomial degree for u
    P_INIT_V = 2             # Initial polynomial degree for v
    INIT_REF_BDY = 3         # Number of initial boundary refinements
    MULTI = True             # MULTI = true  ... use multi-mesh,
                                # MULTI = false ... use single-mesh.
                                # Note: In the single mesh option, the meshes are
                                # forced to be geometrically the same but the
                                # polynomial degrees can still vary.
    THRESHOLD = 0.3          # This is a quantitative parameter of the adapt(...) function and
                                     # it has different meanings for various adaptive strategies (see below).
    STRATEGY = 1             # 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 = 1             # Default value is 1.0. This parameter influences the selection of
                                # cancidates in hp-adaptivity. See get_optimal_refinement() for details.
    MAX_ORDER = 10           # Maximum allowed element degree
    ERR_STOP = 0.5           # 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 mainly 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

    # Load the mesh
    umesh = Mesh()
    vmesh = Mesh()
    umesh.load(get_bracket_mesh())
    if MULTI == False:
        umesh.refine_towards_boundary(1, INIT_REF_BDY)
        
    # Create initial mesh (master mesh).
    vmesh.copy(umesh)

    # Initial mesh refinements in the vmesh towards the boundary
    if MULTI == True:
        vmesh.refine_towards_boundary(1, INIT_REF_BDY)

    # Create the x displacement space
    uspace = H1Space(umesh, P_INIT_U)
    vspace = H1Space(vmesh, P_INIT_V)

    # Initialize the weak formulation
    wf = WeakForm(2)
    set_wf_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(uspace, vspace)

    u_sln_coarse = Solution()
    v_sln_coarse = Solution()
    u_sln_fine = Solution()
    v_sln_fine = Solution()
    
    # Assemble and Solve the fine mesh problem
    rs = RefSystem(ls)
    rs.assemble()
    rs.solve_system(u_sln_fine, v_sln_fine, lib="scipy")

    # 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(u_sln_coarse, v_sln_coarse, lib="scipy")

    # Calculate element errors and total error estimate
    hp = H1Adapt(ls)
    hp.set_solutions([u_sln_coarse, v_sln_coarse], [u_sln_fine, v_sln_fine]);
    set_hp_forms(hp)
    err_est = hp.calc_error() * 100
Exemple #13
0
def test_example_10():
    from hermes2d.examples.c10 import set_bc, set_forms
    from hermes2d.examples import get_motor_mesh
    
    # The following parameters can be changed:
    SOLVE_ON_COARSE_MESH = True   # If true, coarse mesh FE problem is solved in every adaptivity step
    P_INIT = 2              # Initial polynomial degree of all mesh elements.
    THRESHOLD = 0.2         # This is a quantitative parameter of the adapt(...) function and
                            # it has different meanings for various adaptive strategies (see below).

    STRATEGY = 1            # 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_H  # Predefined list of element refinement candidates.
                            # Possible values are are attributes of the class CandList:
                            # H2D_P_ISO, H2D_P_ANISO, H2D_H_ISO, H2D_H_ANISO, H2D_HP_ISO, H2D_HP_ANISO_H, H2D_HP_ANISO_P, H2D_HP_ANISO
                            # See User Documentation 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.

    ERR_STOP = 1.0          # Stopping criterion for adaptivity (rel. error tolerance between the
                            # fine mesh and coarse mesh solution in percent).
    CONV_EXP = 1.0;         # Default value is 1.0. This parameter influences the selection of
                            # cancidates in hp-adaptivity. See get_optimal_refinement() for details.
                            # 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

    # Load the mesh
    mesh = Mesh()
    mesh.load(get_motor_mesh())

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

    # Initialize the discrete problem
    wf = WeakForm()
    set_forms(wf)

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

    # Initialize the linear system.
    ls = LinSystem(wf)
    ls.set_spaces(space)

    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 element errors and total error estimate
    hp = H1Adapt(ls)
    hp.set_solutions([sln_coarse], [sln_fine])
    err_est = hp.calc_error() * 100
Exemple #14
0
    ls.assemble()
    ls.solve_system(x_sln_coarse, y_sln_coarse, lib="scipy")

    # View the solution -- this can be slow; for illustration only
    stress_coarse = VonMisesFilter(x_sln_coarse, y_sln_coarse, mu, lamda)
    #sview.set_min_max_range(0, 3e4)
    sview.show(stress_coarse, lib='mayavi')
    #xoview.show(xdisp, lib='mayavi')
    #yoview.show(ydisp, lib='mayavi')
    xomview.show(xmesh, space=xdisp, lib="mpl", method="orders", notebook=False)
    yomview.show(ymesh, space=ydisp, lib="mpl", method="orders", notebook=False)

    # Solve the fine mesh problem
    rs = RefSystem(ls)
    rs.assemble()
    rs.solve_system(x_sln_fine, y_sln_fine, lib="scipy")

    # Calculate element errors and total error estimate
    hp = H1OrthoHP(xdisp, ydisp)
    set_hp_forms(hp)
    err_est = hp.calc_error_2(x_sln_coarse, y_sln_coarse, x_sln_fine, y_sln_fine) * 100

    print("Error estimate: %s" % err_est)

# If err_est too large, adapt the mesh
    if err_est < ERR_STOP:
        done = True
    else:
        hp.adapt(THRESHOLD, STRATEGY, ADAPT_TYPE)#, ISO_ONLY, MESH_REGULARITY, MAX_ORDER, SAME_ORDERS)
        ndofs = xdisp.assign_dofs()
        ndofs += ydisp.assign_dofs(ndofs)
Exemple #15
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")
Exemple #16
0
it = 1
done = False
u_sln_coarse = Solution()
v_sln_coarse = Solution()
u_sln_fine = Solution()
v_sln_fine = Solution()

while(not done):

    print ("\n---- Adaptivity step %d ---------------------------------------------\n" % it)
    it += 1
    
    # Assemble and Solve the fine mesh problem
    rs = RefSystem(ls)
    rs.assemble()
    rs.solve_system(u_sln_fine, v_sln_fine, lib="scipy")

    # 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(u_sln_coarse, v_sln_coarse, lib="scipy")
    else:
        ls.project_global()

    # View the solution and meshes
    uview.show(u_sln_coarse)
    vview.show(v_sln_coarse)
    umesh.plot(space=uspace)
    vmesh.plot(space=vspace)