コード例 #1
0
ファイル: wave.py プロジェクト: 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)
コード例 #2
0
    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,
                                nx=3,
コード例 #3
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
     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()
コード例 #4
0
    u = ngs.GridFunction(fem_space)

    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)
コード例 #5
0
ファイル: wave.py プロジェクト: 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)