# fealpy网格生成

## 构造性方法

import sys
import numpy as np
import matplotlib.pyplot as plt
from fealpy.mesh import TriangleMesh

node = np.array([
[0.0, 0.0],
[1.0, 0.0],
[1.0, 1.0],
[0.0, 1.0]], dtype=np.float) # (NN, 2)

cell = np.array([[1, 2, 0], [3, 0, 2]], dtype=np.int) # (NC, 3)

mesh = TriangleMesh(node, cell)
node = mesh.entity('node')
edge = mesh.entity('edge')
cell = mesh.entity('cell')

fig = plt.figure()
axes = fig.gca()
mesh.find_node(axes, showindex=True)
mesh.find_edge(axes, showindex=True)
mesh.find_cell(axes, showindex=True)
plt.show()

Fealpy 中构造了一个网格加密的函数 mesh.uniform_refine(n) ， 其核心想法便是把边的中点连接起来, n 指的是加密的次数， 比如：

mesh.uniform_refine(n=1)
fig = plt.figure()
axes = fig.gca()
mesh.find_node(axes, showindex=True)
mesh.find_edge(axes, showindex=True)
mesh.find_cell(axes, showindex=True)
plt.show()

## 正方形网格

import sys
import numpy as np
import matplotlib.pyplot as plt

node = np.array([
[0.0, 0.0],
[1.0, 0.0],
[1.0, 1.0],
[0.0, 1.0]], dtype=np.float)

cell = np.array([[0,1, 2,3]], dtype=np.int)

mesh =QuadrangleMesh(node, cell)

## 利用 Mesh Factory 生成网格

from fealpy.mesh import MeshFactory as MF
from matplotlib import pyplot as plt

box = [0, 1, 0, 1]
mesh = MF.boxmesh2d(box, nx=2, ny=5, meshtype='tri')  #quad、poly

fig = plt.figure()
axes = fig.gca()
mesh.find_node(axes, showindex=True)
mesh.find_edge(axes, showindex=True)
mesh.find_cell(axes, showindex=True)
plt.show()

## 非结构三角网格

from fealpy.mesh import MeshFactory as MF
from matplotlib import pyplot as plt

box = [0, 1, 0, 1]
mesh = MF.triangle(box, h=0.2, meshtype='tri')

fig = plt.figure()
axes = fig.gca()
mesh.find_node(axes, showindex=True)
mesh.find_edge(axes, showindex=True)
mesh.find_cell(axes, showindex=True)
plt.show()

## 鱼骨型网格

from fealpy.mesh import MeshFactory as MF
from matplotlib import pyplot as plt

box = [0, 1, 0, 1]
mesh = MF.special_boxmesh2d(box, n=4, meshtype='fishbone')

fig = plt.figure()
axes = fig.gca()
mesh.find_node(axes, showindex=True)
mesh.find_edge(axes, showindex=True)
mesh.find_cell(axes, showindex=True)
plt.show()

## 米字型网格

from fealpy.mesh import MeshFactory as MF
from matplotlib import pyplot as plt

box = [0, 1, 0, 1]
mesh = MF.special_boxmesh2d(box, n=4, meshtype='rice')

fig = plt.figure()
axes = fig.gca()
mesh.find_node(axes, showindex=True)
mesh.find_edge(axes, showindex=True)
mesh.find_cell(axes, showindex=True)
plt.show()

## 交叉型网格

from fealpy.mesh import MeshFactory as MF
from matplotlib import pyplot as plt

box = [0, 1, 0, 1]
mesh = MF.special_boxmesh2d(box, n=3, meshtype='cross')

fig = plt.figure()
axes = fig.gca()
mesh.find_node(axes, showindex=True)
mesh.find_edge(axes, showindex=True)
mesh.find_cell(axes, showindex=True)
plt.show()

## 非一致网格

from fealpy.mesh import MeshFactory as MF
from matplotlib import pyplot as plt

box = [0, 10, 0, 10]
mesh = MF.special_boxmesh2d(box, n=3, meshtype='nonuniform')

fig = plt.figure()
axes = fig.gca()
mesh.find_node(axes, showindex=True)
mesh.find_edge(axes, showindex=True)
mesh.find_cell(axes, showindex=True)
plt.show()

## 圆上的网格

from fealpy.mesh import MeshFactory as MF
from matplotlib import pyplot as plt

box = [0, 10, 0, 10]
mesh = MF.unitcirclemesh(h=0.4, meshtype='tri')
fig = plt.figure()
axes = fig.gca()
mesh.find_node(axes, showindex=True)
mesh.find_edge(axes, showindex=True)
mesh.find_cell(axes, showindex=True)
plt.show()

## L-shape 网格

from fealpy.mesh import MeshFactory as MF
from matplotlib import pyplot as plt

box = [0, 10, 0, 10]
mesh = MF.lshape_mesh(n=1)
fig = plt.figure()
axes = fig.gca()
mesh.find_node(axes, showindex=True)
mesh.find_edge(axes, showindex=True)
mesh.find_cell(axes, showindex=True)
plt.show()

## 四面体六面体网格

from fealpy.mesh import MeshFactory as MF
from matplotlib import pyplot as plt
box = [0, 1, 0, 1,0,1]
mesh = MF.boxmesh3d(box, nx=2, ny=2, nz=2, meshtype='tet') #hex
fig = plt.figure()
axes = fig.gca(projection='3d')
plt.show()
from fealpy.mesh import MeshFactory as MF
mesh.add_plot(axes)