Exemplo n.º 1
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)
Exemplo n.º 2
0
def setup_poisson(mesh,
                  alpha=1,
                  beta=0,
                  f=1,
                  diri=".*",
                  order=1,
                  fes_opts=dict(),
                  blf_opts=dict(),
                  lf_opts=dict()):
    V = ngs.H1(mesh, order=order, dirichlet=diri, **fes_opts)
    u, v = V.TnT()
    a = ngs.BilinearForm(V, **blf_opts)
    a += ngs.SymbolicBFI(alpha * ngs.grad(u) * ngs.grad(v))
    if beta != 0:
        a += ngs.SymbolicBFI(beta * u * v)
    lf = ngs.LinearForm(V)
    lf += ngs.SymbolicLFI(f * v)
    return V, a, lf
Exemplo n.º 3
0
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)

    bvp = ngs.BVP(bf=a, lf=f, gf=gfu, pre=c)

    while True:
        fes.Update()
        gfu.Update()
        a.Assemble()
Exemplo n.º 4
0
    def __init__(self, domain, g, codomain=None):
        codomain = codomain or domain
        self.g = g

        self.fes_domain = domain.fes
        self.fes_codomain = codomain.fes

        self.fes_in = ngs.H1(self.fes_codomain.mesh, order=1)
        self.gfu_in = ngs.GridFunction(self.fes_in)

        # grid functions for later use
        self.gfu = ngs.GridFunction(
            self.fes_codomain)  # solution, return value of _eval
        # self.gfu_bdr=ngs.GridFunction(self.fes_codomain) #grid function holding boundary values, g/sigma=du/dn

        self.gfu_bilinearform = ngs.GridFunction(
            self.fes_domain
        )  # grid function for defining integrator (bilinearform)
        self.gfu_bilinearform_codomain = ngs.GridFunction(
            self.fes_codomain
        )  # grid function for defining integrator of bilinearform

        self.gfu_linearform_domain = ngs.GridFunction(
            self.fes_codomain)  # grid function for defining linearform
        self.gfu_linearform_codomain = ngs.GridFunction(self.fes_domain)

        self.gfu_deriv_toret = ngs.GridFunction(
            self.fes_codomain)  # grid function: return value of derivative

        self.gfu_adj = ngs.GridFunction(
            self.fes_domain)  # grid function for inner computation in adjoint
        self.gfu_adj_toret = ngs.GridFunction(
            self.fes_domain)  # grid function: return value of adjoint

        self.gfu_b = ngs.GridFunction(
            self.fes_codomain)  # grid function for defining the boundary term

        u = self.fes_codomain.TrialFunction()  # symbolic object
        v = self.fes_codomain.TestFunction()  # symbolic object

        # Define Bilinearform, will be assembled later
        self.a = ngs.BilinearForm(self.fes_codomain, symmetric=True)
        self.a += ngs.SymbolicBFI(-ngs.grad(u) * ngs.grad(v) +
                                  u * v * self.gfu_bilinearform_codomain)

        # Interaction with Trace
        self.fes_bdr = ngs.H1(
            self.fes_codomain.mesh,
            order=self.fes_codomain.globalorder,
            definedon=self.fes_codomain.mesh.Boundaries("cyc"))
        self.gfu_getbdr = ngs.GridFunction(self.fes_bdr)
        self.gfu_setbdr = ngs.GridFunction(self.fes_codomain)

        # Boundary term
        self.b = ngs.LinearForm(self.fes_codomain)
        self.b += ngs.SymbolicLFI(
            -self.gfu_b * v.Trace(),
            definedon=self.fes_codomain.mesh.Boundaries("cyc"))

        # Linearform (only appears in derivative)
        self.f_deriv = ngs.LinearForm(self.fes_codomain)
        self.f_deriv += ngs.SymbolicLFI(-self.gfu_linearform_codomain *
                                        self.gfu * v)

        super().__init__(domain, codomain)
Exemplo n.º 5
0
    def __init__(self, domain, g, codomain=None):
        codomain = codomain or domain
        self.g = g
        # self.pts=pts

        # Define mesh and finite element space
        # geo=SplineGeometry()
        # geo.AddCircle((0,0), 1, bc="circle")
        # ngmesh = geo.GenerateMesh()
        # ngmesh.Save('ngmesh')
        #        self.mesh=MakeQuadMesh(10)
        # self.mesh=Mesh(ngmesh)

        self.fes_domain = domain.fes
        self.fes_codomain = codomain.fes

        # Variables for setting of boundary values later
        # self.ind=[v.point in pts for v in self.mesh.vertices]
        self.pts = [v.point for v in self.fes_codomain.mesh.vertices]
        self.ind = [np.linalg.norm(np.array(p)) > 0.95 for p in self.pts]
        self.pts_bdr = np.array(self.pts)[self.ind]

        self.fes_in = ngs.H1(self.fes_codomain.mesh, order=1)
        self.gfu_in = ngs.GridFunction(self.fes_in)

        # grid functions for later use
        self.gfu = ngs.GridFunction(
            self.fes_codomain)  # solution, return value of _eval
        self.gfu_bdr = ngs.GridFunction(
            self.fes_codomain
        )  # grid function holding boundary values, g/sigma=du/dn

        self.gfu_integrator = ngs.GridFunction(
            self.fes_domain
        )  # grid function for defining integrator (bilinearform)
        self.gfu_integrator_codomain = ngs.GridFunction(self.fes_codomain)
        self.gfu_rhs = ngs.GridFunction(
            self.fes_codomain
        )  # grid function for defining right hand side (linearform), f

        self.gfu_inner_domain = ngs.GridFunction(
            self.fes_domain
        )  # grid function for reading in values in derivative
        self.gfu_inner = ngs.GridFunction(
            self.fes_codomain
        )  # grid function for inner computation in derivative and adjoint
        self.gfu_deriv = ngs.GridFunction(
            self.fes_codomain)  # gridd function return value of derivative
        self.gfu_toret = ngs.GridFunction(
            self.fes_domain
        )  # grid function for returning values in adjoint and derivative

        self.gfu_dir = ngs.GridFunction(
            self.fes_domain
        )  # grid function for solving the dirichlet problem in adjoint
        self.gfu_error = ngs.GridFunction(
            self.fes_codomain
        )  # grid function used in _target to compute the error in forward computation
        self.gfu_tar = ngs.GridFunction(
            self.fes_codomain
        )  # grid function used in _target, holding the arguments
        self.gfu_adjtoret = ngs.GridFunction(self.fes_domain)

        self.Number = ngs.NumberSpace(self.fes_codomain.mesh)
        r, s = self.Number.TnT()

        u = self.fes_codomain.TrialFunction()  # symbolic object
        v = self.fes_codomain.TestFunction()  # symbolic object

        # Define Bilinearform, will be assembled later
        self.a = ngs.BilinearForm(self.fes_codomain, symmetric=True)
        self.a += ngs.SymbolicBFI(
            ngs.grad(u) * ngs.grad(v) * self.gfu_integrator_codomain)

        ########new
        self.a += ngs.SymbolicBFI(
            u * s + v * r, definedon=self.fes_codomain.mesh.Boundaries("cyc"))
        self.fes1 = ngs.H1(self.fes_codomain.mesh,
                           order=2,
                           definedon=self.fes_codomain.mesh.Boundaries("cyc"))
        self.gfu_getbdr = ngs.GridFunction(self.fes1)
        self.gfu_setbdr = ngs.GridFunction(self.fes_codomain)

        # Define Linearform, will be assembled later
        self.f = ngs.LinearForm(self.fes_codomain)
        self.f += ngs.SymbolicLFI(self.gfu_rhs * v)

        self.r = self.f.vec.CreateVector()

        self.b = ngs.LinearForm(self.fes_codomain)
        self.gfu_b = ngs.GridFunction(self.fes_codomain)
        self.b += ngs.SymbolicLFI(
            self.gfu_b * v.Trace(),
            definedon=self.fes_codomain.mesh.Boundaries("cyc"))

        self.f_deriv = ngs.LinearForm(self.fes_codomain)
        self.f_deriv += ngs.SymbolicLFI(self.gfu_rhs * ngs.grad(self.gfu) *
                                        ngs.grad(v))

        #        self.b2=LinearForm(self.fes)
        #        self.b2+=SymbolicLFI(div(v*grad(self.gfu))

        super().__init__(domain, codomain)
Exemplo n.º 6
0
    def __init__(self,
                 domain,
                 rhs,
                 bc_left=None,
                 bc_right=None,
                 bc_top=None,
                 bc_bottom=None,
                 codomain=None,
                 diffusion=True,
                 reaction=False,
                 dim=1):
        assert dim in (1, 2)
        assert diffusion or reaction

        codomain = codomain or domain
        self.rhs = rhs

        self.diffusion = diffusion
        self.reaction = reaction
        self.dim = domain.fes.mesh.dim

        bc_left = bc_left or 0
        bc_right = bc_right or 0
        bc_top = bc_top or 0
        bc_bottom = bc_bottom or 0

        # Define mesh and finite element space
        self.fes_domain = domain.fes
        # self.mesh=self.fes.mesh

        self.fes_codomain = codomain.fes
        #        if dim==1:
        #            self.mesh = Make1DMesh(meshsize)
        #            self.fes = H1(self.mesh, order=2, dirichlet="left|right")
        #        elif dim==2:
        #            self.mesh = MakeQuadMesh(meshsize)
        #            self.fes = H1(self.mesh, order=2, dirichlet="left|top|right|bottom")

        # grid functions for later use
        self.gfu = ngs.GridFunction(
            self.fes_codomain)  # solution, return value of _eval
        self.gfu_bdr = ngs.GridFunction(
            self.fes_codomain)  # grid function holding boundary values

        self.gfu_integrator = ngs.GridFunction(
            self.fes_domain
        )  # grid function for defining integrator (bilinearform)
        self.gfu_integrator_codomain = ngs.GridFunction(self.fes_codomain)
        self.gfu_rhs = ngs.GridFunction(
            self.fes_codomain
        )  # grid function for defining right hand side (Linearform)

        self.gfu_inner_domain = ngs.GridFunction(
            self.fes_domain
        )  # grid function for reading in values in derivative
        self.gfu_inner = ngs.GridFunction(
            self.fes_codomain
        )  # grid function for inner computation in derivative and adjoint
        self.gfu_deriv = ngs.GridFunction(
            self.fes_codomain)  # return value of derivative
        self.gfu_toret = ngs.GridFunction(
            self.fes_domain
        )  # grid function for returning values in adjoint and derivative

        u = self.fes_codomain.TrialFunction()  # symbolic object
        v = self.fes_codomain.TestFunction()  # symbolic object

        # Define Bilinearform, will be assembled later
        self.a = ngs.BilinearForm(self.fes_codomain, symmetric=True)
        if self.diffusion:
            self.a += ngs.SymbolicBFI(
                ngs.grad(u) * ngs.grad(v) * self.gfu_integrator_codomain)
        elif self.reaction:
            self.a += ngs.SymbolicBFI(
                ngs.grad(u) * ngs.grad(v) +
                u * v * self.gfu_integrator_codomain)

        # Define Linearform, will be assembled later
        self.f = ngs.LinearForm(self.fes_codomain)
        self.f += ngs.SymbolicLFI(self.gfu_rhs * v)

        if diffusion:
            self.f_deriv = ngs.LinearForm(self.fes_codomain)
            self.f_deriv += ngs.SymbolicLFI(-self.gfu_rhs *
                                            ngs.grad(self.gfu) * ngs.grad(v))

        # Precompute Boundary values and boundary valued corrected rhs
        if self.dim == 1:
            self.gfu_bdr.Set(
                [bc_left, bc_right],
                definedon=self.fes_codomain.mesh.Boundaries("left|right"))
        elif self.dim == 2:
            self.gfu_bdr.Set([bc_left, bc_top, bc_right, bc_bottom],
                             definedon=self.fes_codomain.mesh.Boundaries(
                                 "left|top|right|bottom"))
        self.r = self.f.vec.CreateVector()

        super().__init__(domain, codomain)
Exemplo n.º 7
0
    cf_n0 = ngs.CoefficientFunction([init_concentr[mat] for mat in mesh.GetMaterials()])
    gfu.components[0].Set(cf_n0)

    ## Poisson's equation for initial potential
    # An extra space is needed, due to different dirichlet conditions for the initial potential
    initial_potential_space = ngs.H1(mesh, order=1, dirichlet='particle|anode')
    phi = initial_potential_space.TrialFunction()
    psi = initial_potential_space.TestFunction()

    a_pot = ngs.BilinearForm(initial_potential_space)
    a_pot += ngs.SymbolicBFI(grad(phi) * grad(psi))
    a_pot.Assemble()

    f_pot = ngs.LinearForm(initial_potential_space)
    # permittivity seems to be missing, but gives too high value if included
    f_pot += ngs.SymbolicLFI(cf_valence * cf_n0 * F * psi)
    f_pot.Assemble()

    gf_phi = ngs.GridFunction(initial_potential_space)
    gf_phi.Set(ngs.CoefficientFunction(cathode_init_pot), definedon=mesh.Materials('particle'))
    ngs.Draw(gf_phi)
    res = f_pot.vec.CreateVector()
    res.data = f_pot.vec - a_pot.mat * gf_phi.vec
    gf_phi.vec.data += a_pot.mat.Inverse(initial_potential_space.FreeDofs()) * res
    gfu.components[1].vec.data = gf_phi.vec

    # Visualization
    ngs.Draw(gfu.components[1])
    input()
    ngs.Draw(1/normalization_concentration * gfu.components[0], mesh, name='nconcentration')
    visoptions.autoscale = '0'
Exemplo n.º 8
0
    V = ngs.H1(mesh, order=1)
    print(V.ndof)

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

    mass = ngs.BilinearForm(V)
    mass += ngs.SymbolicBFI(u * v)
    mass.Assemble()

    a = ngs.BilinearForm(V)
    a += ngs.SymbolicBFI(diffusivity * grad(u) * grad(v))
    a.Assemble()

    f = ngs.LinearForm(V)
    f += ngs.SymbolicLFI(discharge_current_density / F * v.Trace(), ngs.BND,
                         definedon=mesh.Boundaries('anode'))
    f.Assemble()

    # Initial conditions
    gfu = ngs.GridFunction(V)
    gfu.Set(ngs.CoefficientFunction(initial_concentration))

    # Visualization
    ngs.Draw(gfu)
    print('0s')
    input()

    # Time stepping
    timestep = 1
    t = 0
Exemplo n.º 9
0
        ngmesh = cube_geo().GenerateMesh(maxh=0.1)
        ngmesh.Save('cube.vol')
        mesh = ngs.Mesh('cube.vol')

    print(mesh.GetMaterials())
    print(mesh.GetBoundaries())

    fes = ngs.H1(mesh, dirichlet='dirichlet', order=1)
    print('Dofs:', fes.ndof)

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

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

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

    c = h1amg.H1AMG(a)

    gfu = ngs.GridFunction(fes)
    bvp = ngs.BVP(bf=a, lf=f, gf=gfu, pre=c)

    while True:
        fes.Update()
        gfu.Update()
        a.Assemble()