Beispiel #1
0
def Solve(a, f, c, ms=100, tol=1e-12, nocb=True):
    ngs.ngsglobals.msg_level = 5
    gfu = ngs.GridFunction(a.space)
    with ngs.TaskManager():
        a.Assemble()
        f.Assemble()
        ngs.ngsglobals.msg_level = 1
        c.Test()
        cb = None if a.space.mesh.comm.rank != 0 or nocb else lambda k, x: print(
            "it =", k, ", err =", x)
        cg = ngs.krylovspace.CGSolver(mat=a.mat,
                                      pre=c,
                                      callback=cb,
                                      maxsteps=ms,
                                      tol=tol)
        ngs.mpi_world.Barrier()
        ts = ngs.mpi_world.WTime()
        cg.Solve(sol=gfu.vec, rhs=f.vec)
        ngs.mpi_world.Barrier()
        ts = ngs.mpi_world.WTime() - ts
        if ngs.mpi_world.rank == 0:
            print("---")
            print("multi-dim ", a.space.dim)
            print("(vectorial) ndof ", a.space.ndofglobal)
            print("(scalar) ndof ", a.space.dim * a.space.ndofglobal)
            print("used nits = ", cg.iterations)
            print("(vec) dofs / (sec * np) = ",
                  a.space.ndofglobal / (ts * max(ngs.mpi_world.size - 1, 1)))
            print("(scal) dofs / (sec * np) = ",
                  (a.space.ndofglobal * a.space.dim) /
                  (ts * max(ngs.mpi_world.size - 1, 1)))
            print("---")
    assert cg.errors[-1] < tol * cg.errors[0]
    assert cg.iterations < ms
    return gfu
Beispiel #2
0
def test_h1_real():
    """Test h1 amg for real example."""
    with ngs.TaskManager():
        mesh = ngs.Mesh(unit_square.GenerateMesh(maxh=0.2))

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

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

        # rhs
        f = ngs.LinearForm(fes)
        f += ngs.SymbolicLFI(v)
        f.Assemble()

        # lhs
        a = ngs.BilinearForm(fes, symmetric=True)
        a += ngs.SymbolicBFI(grad(u) * grad(v))

        c = ngs.Preconditioner(a, 'h1amg2')
        a.Assemble()

        solver = ngs.CGSolver(mat=a.mat, pre=c.mat)

        gfu = ngs.GridFunction(fes)
        gfu.vec.data = solver * f.vec

    assert_greater(solver.GetSteps(), 0)
    assert_less_equal(solver.GetSteps(), 4)
Beispiel #3
0
 def apply_inverse(self, V, mu=None, least_squares=False, default_solver=''):
     assert V in self.range
     if least_squares:
         raise NotImplementedError
     solver = self.solver_options.get('inverse', default_solver) if self.solver_options else default_solver
     R = self.source.zeros(len(V))
     with ngs.TaskManager():
         inv = self.matrix.Inverse(self.source.V.FreeDofs(), inverse=solver)
         for r, v in zip(R._list, V._list):
             r.impl.vec.data = inv * v.impl.vec
     return R
Beispiel #4
0
def test_parallel():
    mesh = ngs.Mesh(unit_square.GenerateMesh(maxh=0.2))
    fes = ngs.L2(mesh, order=5, complex=True)
    g1 = ngs.GridFunction(fes)
    g2 = ngs.GridFunction(fes)

    for order in range(10):
        functions = [(f(ngs.x + 1j * ngs.y, order)) for f in fs_ng]

        for f in functions:
            g1.Set(f)
            with ngs.TaskManager():
                g2.Set(f)

            error = ngs.Integrate(g1 - g2, mesh)
            assert error == 0j
Beispiel #5
0
"""Solve simple laplace equation on a square."""
# pylint: disable=no-member

from ctypes import CDLL

import ngsolve as ngs
from ngsolve import grad
from netgen.geom2d import unit_square

CDLL('libh1amg.so')

with ngs.TaskManager():
    mesh = ngs.Mesh(unit_square.GenerateMesh(maxh=0.2))

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

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

    # rhs
    f = ngs.LinearForm(fes)
    f += ngs.SymbolicLFI(v)

    # lhs
    a = ngs.BilinearForm(fes, symmetric=True)
    a += ngs.SymbolicBFI(grad(u) * grad(v))

    c = ngs.Preconditioner(a, 'h1amg', test=True)

    gfu = ngs.GridFunction(fes)
Beispiel #6
0
    "ngs_amg_enable_redist": True,
    "ngs_amg_first_aaf": 0.025
}

gfu = ngsolve.GridFunction(V)
a.Assemble()
f.Assemble()
gfu.vec.data = a.mat.Inverse(V.FreeDofs()) * f.vec

#ngsolve.Draw(mesh, deformation = ngsolve.CoefficientFunction((gfu[0], gfu[1], gfu[2])), name="defo")
ngsolve.Draw(mesh,
             deformation=ngsolve.CoefficientFunction((gfu[0], gfu[1])),
             name="defo")
ngsolve.Draw(gfu[2], mesh, name="rot")
# for i in range(6):
#     ngsolve.Draw(gfu[i], mesh, name="comp_"+str(i))

# ngsolve.Draw(gfu, name="sol")

# # c = ngsolve.Preconditioner(a, "ngs_amg.elast3d", **pc_opts)
c = ngs_amg.elast_3d(a, **pc_opts)
pt = 0  #100 * 1024 * 1024
with ngsolve.TaskManager(pajetrace=pt):
    Solve(a, f, c, ms=40)

# if ngsolve.mpi_world.rank == 1:
#SetNumThreads(5)

#from bftester_vec import shape_test
#shape_test(mesh, maxh, V, a, c, 6)
Beispiel #7
0
    def solve(self):

        # disable garbage collector
        # --------------------------------------------------------------------#
        gc.disable()
        while (gc.isenabled()):
            time.sleep(0.1)
        # --------------------------------------------------------------------#

        # measure how much memory is used until here
        process = psutil.Process()
        memstart = process.memory_info().vms

        # starts timer
        tstart = time.time()
        if self.show_gui:
            import netgen.gui

        # create mesh with initial size 0.1
        self._mesh = ngs.Mesh(unit_square.GenerateMesh(maxh=0.1))

        #create finite element space
        self._fes = ngs.H1(self._mesh,
                           order=2,
                           dirichlet=".*",
                           autoupdate=True)

        # test and trail function
        u = self._fes.TrialFunction()
        v = self._fes.TestFunction()

        # create bilinear form and enable static condensation
        self._a = ngs.BilinearForm(self._fes, condense=True)
        self._a += ngs.grad(u) * ngs.grad(v) * ngs.dx

        # creat linear functional and apply RHS
        self._f = ngs.LinearForm(self._fes)
        self._f += (-4) * v * ngs.dx

        # preconditioner: multigrid - what prerequisits must the problem have?
        self._c = ngs.Preconditioner(self._a, "multigrid")

        # create grid function that holds the solution and set the boundary to 0
        self._gfu = ngs.GridFunction(self._fes, autoupdate=True)  # solution
        self._g = self._ngs_ex
        self._gfu.Set(self._g, definedon=self._mesh.Boundaries(".*"))

        # draw grid function in gui
        if self.show_gui:
            ngs.Draw(self._gfu)

        # create Hcurl space for flux calculation and estimate error
        self._space_flux = ngs.HDiv(self._mesh, order=2, autoupdate=True)
        self._gf_flux = ngs.GridFunction(self._space_flux,
                                         "flux",
                                         autoupdate=True)

        # TaskManager starts threads that (standard thread nr is numer of cores)
        with ngs.TaskManager():
            # this is the adaptive loop
            while self._fes.ndof < self.max_ndof:
                self._solveStep()
                self._estimateError()
                self._mesh.Refine()

        # since the adaptive loop stopped with a mesh refinement, the gfu must be
        # calculated one last time
        self._solveStep()
        if self.show_gui:
            ngs.Draw(self._gfu)

        # set measured exectution time
        self._exec_time = time.time() - tstart

        # set measured used memory
        memstop = process.memory_info().vms - memstart
        self._mem_consumption = memstop

        # enable garbage collector
        # --------------------------------------------------------------------#
        gc.enable()
        gc.collect()