Beispiel #1
0
def my_test(n, interface, fname):

    t0  = time.clock()
    alg = InterfaceMesh2d(interface, interface.box, n)
    ppoint, pcell, pcellLocation = alg.run()
    t1 = time.clock()

    pmesh = PolygonMesh(ppoint, pcell, pcellLocation)
    a = pmesh.angle()
    write_vtk_mesh(pmesh, fname)

    return t1 - t0, np.max(a), (n+1)*(n+1)
Beispiel #2
0
 def init_mesh(self, n=4, meshtype='tri', h=0.1):
     """ generate the initial mesh
     """
     from meshpy.triangle import build
     domain = self.domain(domaintype='meshpy')
     mesh = build(domain, max_volume=h**2)
     node = np.array(mesh.points, dtype=np.float64)
     cell = np.array(mesh.elements, dtype=np.int_)
     if meshtype == 'tri':
         return TriangleMesh(node, cell)
     elif meshtype == 'polygon':
         mesh = TriangleMeshWithInfinityNode(TriangleMesh(node, cell))
         pnode, pcell, pcellLocation = mesh.to_polygonmesh()
         return PolygonMesh(pnode, pcell, pcellLocation)
Beispiel #3
0
    def loadMatlabMesh(self, filename=None):
        # filename = self.filename if filename is None else filename
        # mfile = self.mfile if filename is None else loadmat(filename)
        mfile = loadmat(filename)
        pnode = mfile['node']
        pelem = mfile['elem']
        Nelem = pelem.shape[0]
        pcellLocation = np.zeros((Nelem + 1,), dtype=np.int)
        pcell = np.zeros((0,), dtype=np.int)

        for n in range(Nelem):
            lcell = np.squeeze(pelem[n][0]) - 1  # let the index from 0.
            lNedge = lcell.shape[0]
            pcellLocation[n + 1] = pcellLocation[n] + lNedge
            pcell = np.concatenate([pcell, lcell])

        mesh = PolygonMesh(pnode, pcell, pcellLocation)
        return mesh
Beispiel #4
0
import sys

import numpy as np

from fealpy.mesh.TriangleMesh import TriangleMeshWithInfinityPoint
from fealpy.mesh import rectangledomainmesh
from fealpy.mesh.PolygonMesh import PolygonMesh

import matplotlib.pyplot as plt

n = 2
box = [0, 1, 0, 1]
mesh = rectangledomainmesh(box, nx=n, ny=n, meshtype='tri')
nmesh = TriangleMeshWithInfinityPoint(mesh)
ppoint, pcell, pcellLocation = nmesh.to_polygonmesh()
pmesh = PolygonMesh(ppoint, pcell, pcellLocation)
Beispiel #5
0
for i in range(NNC):
    idx0 = np.arange(newCellLocation2[i],
                     newCellLocation2[i] + location[i, 0] + 1)
    idx1 = np.arange(initLocation[i], initLocation[i] + location[i, 0] + 1)
    newCell2[idx0] = cell[idx1]
    idx0 = np.arange(newCellLocation2[i] + location[i, 0] + 3,
                     newCellLocation2[i] + NV2[i])
    idx1 = np.arange(initLocation[i] + location[i, 1] + 1,
                     initLocation[i] + NV0[i])
    newCell2[idx0] = cell[idx1]

np.repeat(isInterfaceCell, NV)
NV3 = NV[~isInterfaceCell]
newCell3 = cell[np.repeat(~isInterfaceCell, NV)]
newCellLocation3 = np.zeros(NV3.shape[0] + 1, dtype=np.int)
newCellLocation3[1:] = np.cumsum(NV3)

cell = np.concatenate((newCell3, newCell1, newCell2))
cellLocation = np.concatenate(
    (newCellLocation3[0:-1], newCellLocation3[-1] + newCellLocation1[0:-1],
     newCellLocation3[-1] + newCellLocation1[-1] + newCellLocation2),
    axis=0)
point = np.append(point, cutPoint, axis=0)
polymesh1 = PolygonMesh(point, cell, cellLocation)

fig = plt.figure()
axes = fig.gca()
polymesh1.add_plot(axes)
#polymesh.find_edge(axes, index=isCutEdge0, color='y')
plt.show()
Beispiel #6
0
from fealpy.mesh.PolygonMesh import PolygonMesh

node = np.array([(0, 0), (1, 0), (2, 0), (0, 1), (1, 1), (2, 1), (0, 2),
                 (1, 2), (2, 2)],
                dtype=np.float)
cell = np.array([1, 4, 0, 3, 0, 4, 1, 2, 5, 4, 3, 4, 7, 6, 4, 5, 8, 7],
                dtype=np.int)
cellLocation = np.array([0, 3, 6, 10, 14, 18], dtype=np.int)

node = np.array([(0, 0), (1, 0), (2, 0), (0, 1), (1, 1), (2, 1), (0, 2),
                 (1, 2), (2, 2)],
                dtype=np.float)
cell = np.array([0, 3, 4, 1, 1, 4, 5, 2, 3, 6, 7, 4, 4, 7, 8, 5], dtype=np.int)
cellLocation = np.array([0, 4, 8, 12, 16], dtype=np.int)

pmesh = PolygonMesh(node, cell, cellLocation)
area = pmesh.cell_area()

d = pmesh.angle()
print(d)
print(pmesh.node)
print(pmesh.ds.cell)
print(pmesh.ds.edge)
print(pmesh.ds.edge2cell)
print(area)
print(pmesh.ds.cell_to_cell().toarray())
print(pmesh.ds.cell_to_node().toarray())
print(pmesh.ds.cell_to_edge().toarray())

fig = plt.figure()
axes = fig.gca()
Beispiel #7
0
from fealpy.mesh.curve import CircleCurve
from fealpy.mesh.curve import Curve1
from fealpy.mesh.curve import Curve2
from fealpy.mesh.curve import Curve3

import time

n = 40

interface = Curve1(a=6)
#interface = Curve2()
#interface = Curve3()

t0 = time.clock()
alg = InterfaceMesh2d(interface, interface.box, n)
ppoint, pcell, pcellLocation = alg.run()
t1 = time.clock()
pointMarker = alg.point_marker()
pmesh = PolygonMesh(ppoint, pcell, pcellLocation)
a = pmesh.angle()
print(a.max())
write_vtk_mesh(pmesh, 'test.vtk')

fig = plt.figure()
axes = fig.gca()
pmesh.add_plot(axes, cellcolor=[0.5, 0.9, 0.45])
pmesh.find_point(axes, point=pmesh.point[pointMarker], markersize=30)
plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
plt.savefig('sixfolds.pdf')
plt.show()
Beispiel #8
0
pfix = np.array([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]],
                dtype=np.float)
fd = lambda p: drectangle(p, box)

pmesh = distmesh2d(fd, h, box, pfix, meshtype='polygon')
area = pmesh.entity_measure('cell')

node = pmesh.entity('node')
t = Delaunay(node)
tmesh = TriangleMesh(node, t.simplices.copy())

area = tmesh.entity_measure('cell')
tmesh.delete_cell(area < 1e-8)
area = tmesh.entity_measure('cell')
print(len(tmesh.node))
pmesh = PolygonMesh.from_mesh(tmesh)
print(pmesh.ds.cell)
fig = plt.figure()
axes = fig.gca()
pmesh.add_plot(axes)
#mesh.find_node(axes, showindex=True)
plt.show()

Ndof = np.zeros((maxit, ), dtype=np.int)
errorType = [
    '$|| u - \Pi^\\nabla u_h||_0$ with p=1',
    '$||\\nabla u - \\nabla \Pi^\\nabla u_h||_0$ with p=1',
]

p = 1
Beispiel #9
0
p = 1 # degree of the vem space
box = [0, 1, 0, 1] # domain 
n = 10 # initial 
maxit = 5  
error = np.zeros((maxit,), dtype=np.float)
Ndof = np.zeros((maxit,), dtype=np.int)

model = CosCosData()
model = PolynomialData()
model = ExpData()
for i in range(maxit):
    # Mesh 
    mesh0 = rectangledomainmesh(box, nx=n, ny=n, meshtype='tri') 
    mesh1 = TriangleMeshWithInfinityPoint(mesh0)
    point, cell, cellLocation = mesh1.to_polygonmesh()
    mesh = PolygonMesh(point, cell, cellLocation)

    # VEM Space
    V = VirtualElementSpace2d(mesh, p) 

    uh = FiniteElementFunction(V)

    Ndof[i] = V.number_of_global_dofs() 

    vem = PoissonVEMModel(model, V)
    BC = DirichletBC(V, model.dirichlet)

    solve(vem, uh, dirichlet=BC, solver='direct')

    # error 
    uI = V.interpolation(model.solution)
Beispiel #10
0
def tri_to_polygon(mesh):
    nmesh = TriangleMeshWithInfinityPoint(mesh)
    ppoint, pcell, pcellLocation = nmesh.to_polygonmesh()
    return PolygonMesh(ppoint, pcell, pcellLocation)
Beispiel #11
0
p = 2        #int(sys.argv[2]) # 有限元空间的次数
maxit =4     #int(sys.argv[4]) # 迭代加密的次数
pde = PDE()  # 创建 pde 模型
beta = 200
alpha = -1

# 误差类型与误差存储数组
errorType = ['$|| u - u_h||_0$', '$||\\nabla u - \\nabla u_h||_0$']
errorMatrix = np.zeros((len(errorType), maxit), dtype=np.float)
# 自由度数组
NDof = np.zeros(maxit, dtype=np.int)

for i in range(maxit):
    print("The {}-th computation:".format(i))
    mesh = pde.init_mesh(n=i+3, meshtype='tri') 
    mesh = PolygonMesh.from_mesh(mesh)
    # mesh.add_plot(plt, cellcolor='w')
    # mesh = pde.init_mesh(n=2**(i+3), meshtype='poly') 
    # mesh.add_plot(plt, cellcolor='w')
    space = ScaledMonomialSpace2d(mesh,p)
    NDof[i] = space.number_of_global_dofs()
    
    fem = IPDGModel(pde, mesh, p) # 创建 Poisson 有限元模型
    fem.solve(beta,alpha) # 求解
    uh = fem.uh
    errorMatrix[0, i] = space.integralalg.L2_error(pde.solution, uh)
    errorMatrix[1, i] = space.integralalg.L2_error(pde.gradient, uh.grad_value)
    
# 显示误差
show_error_table(NDof, errorType, errorMatrix)
# 可视化误差收敛阶
Beispiel #12
0
import sys
import matplotlib.pyplot as plt

from fealpy.mesh.simple_mesh_generator import unitcircledomainmesh
from fealpy.mesh.TriangleMesh import TriangleMeshWithInfinityPoint
from fealpy.mesh.PolygonMesh import PolygonMesh

h = 0.1
mesh0 = unitcircledomainmesh(h)

mesh1 = TriangleMeshWithInfinityPoint(mesh0)

point, cell, cellLocation = mesh1.to_polygonmesh()

pmesh = PolygonMesh(point, cell, cellLocation)

f0 = plt.figure()
axes0 = f0.gca()
mesh0.add_plot(axes0)

f1 = plt.figure()
axes1 = f1.gca()
pmesh.add_plot(axes1)
plt.show()
Beispiel #13
0
import numpy as np

import matplotlib.pyplot as plt
from fealpy.mesh.simple_mesh_generator import rectangledomainmesh
from fealpy.mesh.TriangleMesh import TriangleMeshWithInfinityPoint
from fealpy.mesh.PolygonMesh import PolygonMesh

# Generate mesh
box = [-1, 1, -1, 1]
mesh = rectangledomainmesh(box, nx=10, ny=10, meshtype='tri')
mesht = TriangleMeshWithInfinityPoint(mesh)
ppoint, pcell, pcellLocation = mesht.to_polygonmesh()
pmesh = PolygonMesh(ppoint, pcell, pcellLocation)

# Virtual  element space

fig = plt.figure()
axes = fig.gca()
pmesh.add_plot(axes)
plt.show()
Beispiel #14
0
# @E-mail: [email protected]
# @Site:
# @Time: Nov 01, 2020
# ---

from scipy.io import loadmat
import numpy as np
from fealpy.mesh.PolygonMesh import PolygonMesh
from ShowCls import ShowCls

m = loadmat('../Meshfiles/Dmesh_nonconvex_[0,1]x[0,1]_4.mat')
pnode = m['node']
pelem = m['elem']
Nelem = pelem.shape[0]
pcellLocation = np.zeros((Nelem + 1, ), dtype=np.int)
pcell = np.zeros((0, ), dtype=np.int)

for n in range(Nelem):
    lcell = np.squeeze(pelem[n][0]) - 1  # let the index from 0.
    lNedge = lcell.shape[0]
    pcellLocation[n + 1] = pcellLocation[n] + lNedge
    pcell = np.concatenate([pcell, lcell])
    # print('pcell = ', pcell)

mesh = PolygonMesh(pnode, pcell, pcellLocation)
sc = ShowCls(1, mesh)
sc.showMesh()

# ---
print("End of this file")