Example #1
0
    def __init__(self, show_gui, max_ndof=50000):
        super().__init__(show_gui)

        # init protected
        self._pde_string = "-laplacian(u(x)) = 2*(pi**2)*sin(pi*x)*sin(pi*y)"
        self._ngs_ex = ngs.sin(np.pi * ngs.x) * ngs.sin(np.pi * ngs.y)

        # init public
        self.max_ndof = max_ndof
 def __init__(self, show_gui, max_ndof=50000):
     super().__init__(show_gui)
     
     # init protected
     self._pde_string = """laplacian(u(x)) = 2*np.exp(-2  (x^2 + y^2))*(2 * np.sin(-2 (x^2 + y^2)) + 2 (1-8 * x^2) np.cos(-2 * (x^2 + y^2))) + \
                2*np.exp(-2  (x^2 + y^2))*(2 * np.sin(-2  (x^2 + y^2)) + 2  (1-8 * y^2) np.cos(-2 * (x^2 + y^2))) + \
                2*np.exp(-1  (x^2 + y^2))*(1 * np.sin(-1  (x^2 + y^2)) + 1  (1-4 * x^2) np.cos(-1 * (x^2 + y^2))) + \
                2*np.exp(-1  (x^2 + y^2))*(1 * np.sin(-1  (x^2 + y^2)) + 1  (1-4 * y^2) np.cos(-1 * (x^2 + y^2))) + \
                2*np.exp(-0.1(x^2 + y^2))*(0.1*np.sin(-0.1(x^2 + y^2)) + 0.1(1-0.4*x^2) np.cos(-0.1*(x^2 + y^2))) + \
                2*np.exp(-0.1(x^2 + y^2))*(0.1*np.sin(-0.1(x^2 + y^2)) + 0.1(1-0.4*y^2) np.cos(-0.1*(x^2 + y^2)))  """
     
     self._ngs_ex = ngs.exp(-2  * ((ngs.x)**2 + (ngs.y)**2))*ngs.sin(2  * ((ngs.x)**2 + (ngs.y)**2)) + \
                    ngs.exp(-1  * ((ngs.x)**2 + (ngs.y)**2))*ngs.sin(1  * ((ngs.x)**2 + (ngs.y)**2)) + \
                    ngs.exp(-0.1* ((ngs.x)**2 + (ngs.y)**2))*ngs.sin(0.1* ((ngs.x)**2 + (ngs.y)**2))
     
     # init public
     self.max_ndof = max_ndof
Example #3
0
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s %(levelname)s %(name)-40s :: %(message)s')

meshsize_domain = 10
meshsize_codomain = 10

mesh = MakeQuadMesh(meshsize_domain)
fes_domain = ngs.L2(mesh, order=2)
domain = NgsSpace(fes_domain)

mesh = MakeQuadMesh(meshsize_codomain)
fes_codomain = ngs.H1(mesh, order=3, dirichlet="left|top|right|bottom")
codomain = NgsSpace(fes_codomain)

rhs = 10 * ngs.sin(ngs.x) * ngs.sin(ngs.y)
op = Coefficient(domain,
                 rhs,
                 codomain=codomain,
                 bc_left=0,
                 bc_right=0,
                 bc_bottom=0,
                 bc_top=0,
                 diffusion=False,
                 reaction=True,
                 dim=2)

exact_solution_coeff = ngs.x + 1
gfu_exact_solution = ngs.GridFunction(op.fes_domain)
gfu_exact_solution.Set(exact_solution_coeff)
exact_solution = gfu_exact_solution.vec.FV().NumPy()
Example #4
0
File: wave.py Project: jayggg/DPG
        h0 = 0.25      # coarsest mesh size
        p = 1          # polynomial degree

        if example[3:] == 'triangular':
            mesh = ngs.Mesh(unit_square.GenerateMesh(maxh=h0))
        elif example[3:] == 'rectangular':
            mesh = ngs.Mesh(unit_square.GenerateMesh(maxh=h0,
                                                     quad_dominated=True))

        q_zero = 'bottom'  # mesh boundary parts where q = 0,
        mu_zero = 'bottom|right|left'         # where mu = 0.

        x = ngs.x
        t = ngs.y
        cwave = 1
        f = 2*pi*pi*sin(pi*x)*cos(2*pi*t) + pi*pi*sin(pi*x)*sin(pi*t)*sin(pi*t)
        F = CoefficientFunction((0, f))
        exactu = CoefficientFunction((pi*cos(pi*x)*sin(pi*t)*sin(pi*t),
                                      pi*sin(pi*x)*sin(2*pi*t)))

        hs = []
        er = []
        for l in range(maxr):
            h = h0 * 2**(-l)
            hs.append(h)
            e, *rest = solvewavedirect(mesh, p, F, q_zero, mu_zero, cwave, exactu)
            # e, *rest = solvewave(mesh, p, F, q_zero, mu_zero, cwave, exactu)
            er.append(e)
            mesh.Refine()

        print_rates(er, hs)
Example #5
0
    Draw(mesh)
    input("x-periodic quad mesh -- press any key to continue -- ")

    mesh = MakeStructured2DMesh(quads=False,
                                nx=16,
                                ny=4,
                                periodic_x=False,
                                periodic_y=True)
    Draw(mesh)
    input("y-periodic non-uniform trig mesh -- press any key to continue -- ")

    mesh = MakeStructured2DMesh(quads=True,
                                nx=32,
                                ny=16,
                                mapping=lambda x, y:
                                ((1.1 + sin(pi * (y - 0.5))) * sin(pi * x),
                                 (1.1 + sin(pi * (y - 0.5))) * cos(pi * x)))
    Draw(mesh)
    input(
        "structured quad half circle with a whole -- press any key to continue -- "
    )

    mesh = MakeHexMesh(nx=8)
    Draw(mesh)
    input("simple cube mesh -- press any key to continue -- ")

    mesh = MakeHexMesh(nx=8, periodic_x=True, periodic_y=True, periodic_z=True)
    Draw(mesh)
    input("periodic cube mesh -- press any key to continue -- ")

    mesh = MakeStructured3DMesh(hexes=False,
Example #6
0
    u.vec.FV().NumPy()[:] = np.array(soln_fem.real, dtype=np.float_)

    #u_im=dolfin.Function(fenics_space)
    #u_im.vector()[:]=np.array(soln_fem.imag,dtype=np.float_)

    bem_soln = bempp.api.GridFunction(bc_space, coefficients=soln_bem)

    ns.append(fem_size)

    actual0 = bempp.api.GridFunction(bc_space, fun=zero)

    errs.append((actual0 - bem_soln).l2_norm())

    actual_u = ngs.CoefficientFunction((ngs.cos(k * ngs.z), 0, 0))
    actual_curl_u = ngs.CoefficientFunction((0, -k * ngs.sin(k * ngs.z), 0))

    actual_sol = np.concatenate([soln_fem, soln_bem])

    from math import sqrt
    u_H1 = sqrt(
        abs(
            ngs.Integrate((actual_u - u) * (actual_u - u) +
                          (actual_curl_u - u.Deriv())**2,
                          mesh,
                          order=10)))
    err2s.append(u_H1)

    print("ns: ", ns)
    print("Hcurl error:", err2s)
    print("BEM error", errs)
Example #7
0
    format='%(asctime)s %(levelname)s %(name)-40s :: %(message)s')

geo = SplineGeometry()
geo.AddCircle((0, 0), r=1, bc="cyc", maxh=0.2)
mesh = ngs.Mesh(geo.GenerateMesh())

fes_domain = ngs.H1(mesh, order=2)
domain = NgsSpace(fes_domain)

fes_codomain = ngs.H1(mesh, order=2)
codomain = NgsSpace(fes_codomain)

g = ngs.x**2 * ngs.y
op = ReactionBoundary(domain, g, codomain=codomain)

exact_solution_coeff = ngs.sin(ngs.y) + 2
gfu_exact_solution = ngs.GridFunction(op.fes_domain)
gfu_exact_solution.Set(exact_solution_coeff)
exact_solution = gfu_exact_solution.vec.FV().NumPy()
exact_data = op(exact_solution)

fes_noise = ngs.L2(fes_codomain.mesh, order=1)
gfu_noise_order1 = ngs.GridFunction(fes_noise)
gfu_noise_order1.vec.FV().NumPy()[:] = 0.0005 * np.random.randn(fes_noise.ndof)
gfu_noise = ngs.GridFunction(fes_codomain)
gfu_noise.Set(gfu_noise_order1)
noise = op._get_boundary_values(gfu_noise)

data = exact_data + noise

init = 2
 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
     geo = geom2d.SplineGeometry()
     p1 = geo.AppendPoint (-2,-2)
     p2 = geo.AppendPoint (2,-2)
     p3 = geo.AppendPoint (2,2)
     p4 = geo.AppendPoint (-2,2)
     geo.Append (["line", p1, p2])
     geo.Append (["line", p2, p3])
     geo.Append (["line", p3, p4])
     geo.Append (["line", p4, p1])
     self._mesh = ngs.Mesh(geo.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 += - ( \
                  2*ngs.exp(-2 * (ngs.x**2 + ngs.y**2))*(2 * ngs.sin(-2 * (ngs.x**2 + ngs.y**2)) + 2 * (1-8 * ngs.x**2)*ngs.cos(-2 * (ngs.x**2 + ngs.y**2))) + \
                  2*ngs.exp(-2 * (ngs.x**2 + ngs.y**2))*(2 * ngs.sin(-2 * (ngs.x**2 + ngs.y**2)) + 2 * (1-8 * ngs.y**2)*ngs.cos(-2 * (ngs.x**2 + ngs.y**2))) + \
                  2*ngs.exp(-1 * (ngs.x**2 + ngs.y**2))*(1 * ngs.sin(-1 * (ngs.x**2 + ngs.y**2)) + 1 * (1-4 * ngs.x**2)*ngs.cos(-1 * (ngs.x**2 + ngs.y**2))) + \
                  2*ngs.exp(-1 * (ngs.x**2 + ngs.y**2))*(1 * ngs.sin(-1 * (ngs.x**2 + ngs.y**2)) + 1 * (1-4 * ngs.y**2)*ngs.cos(-1 * (ngs.x**2 + ngs.y**2))) + \
                  2*ngs.exp(-0.1*(ngs.x**2 + ngs.y**2))*(0.1*ngs.sin(-0.1*(ngs.x**2 + ngs.y**2)) + 0.1*(1-0.4*ngs.x**2)*ngs.cos(-0.1*(ngs.x**2 + ngs.y**2))) + \
                  2*ngs.exp(-0.1*(ngs.x**2 + ngs.y**2))*(0.1*ngs.sin(-0.1*(ngs.x**2 + ngs.y**2)) + 0.1*(1-0.4*ngs.y**2)*ngs.cos(-0.1*(ngs.x**2 + ngs.y**2)))
                  )*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()
Example #9
0
File: wave.py Project: prklVIP/DPG
        h0 = 0.25  # coarsest mesh size
        p = 1  # polynomial degree

        if example[3:] == 'triangular':
            mesh = ngs.Mesh(unit_square.GenerateMesh(maxh=h0))
        elif example[3:] == 'rectangular':
            mesh = ngs.Mesh(
                unit_square.GenerateMesh(maxh=h0, quad_dominated=True))

        q_zero = 'bottom'  # mesh boundary parts where q = 0,
        mu_zero = 'bottom|right|left'  # where mu = 0.

        x = ngs.x
        t = ngs.y
        cwave = 1
        f = 2 * pi * pi * sin(pi * x) * cos(2 * pi * t) + pi * pi * sin(
            pi * x) * sin(pi * t) * sin(pi * t)
        F = CoefficientFunction((0, f))
        exactu = CoefficientFunction(
            (pi * cos(pi * x) * sin(pi * t) * sin(pi * t),
             pi * sin(pi * x) * sin(2 * pi * t)))

        hs = []
        er = []
        for l in range(maxr):
            h = h0 * 2**(-l)
            hs.append(h)
            e, *rest = solvewavedirect(mesh, p, F, q_zero, mu_zero, cwave,
                                       exactu)
            # e, *rest = solvewave(mesh, p, F, q_zero, mu_zero, cwave, exactu)
            er.append(e)