Ejemplo n.º 1
0
 def __init__(self, pde, mesh, p):
     self.space = ScaledMonomialSpace2d(mesh,p)
     self.mesh = mesh
     self.pde = pde 
     self.cellbarycenter = mesh.entity_barycenter('cell')
     self.p = p
     self.cellmeasure = mesh.entity_measure('cell')
Ejemplo n.º 2
0
    def __init__(self):
        self.domain = [0, 50, 0, 50]
        self.mesh = MeshFactory().regular(self.domain, n=50)
        self.timeline = UniformTimeLine(0, 1, 100)
        self.space0 = RaviartThomasFiniteElementSpace2d(self.mesh, p=0)
        self.space1 = ScaledMonomialSpace2d(self.mesh, p=1)  # 线性间断有限元空间

        self.vh = self.space0.function()  # 速度
        self.ph = self.space0.smspace.function()  # 压力
        self.ch = self.space1.function(dim=3)  # 三个组分的摩尔密度
        self.options = {
            'viscosity': 1.0,
            'permeability': 1.0,
            'temperature': 397,
            'pressure': 50,
            'porosity': 0.2,
            'injecttion_rate': 0.1,
            'compressibility': (0.001, 0.001, 0.001),
            'pmv': (1.0, 1.0, 1.0),
            'dt': self.timeline.dt
        }

        c = self.options['viscosity'] / self.options['permeability']
        self.A = c * self.space0.mass_matrix()
        self.B = self.space0.div_matrix()

        phi = self.options['porosity']
        self.M = phi / dt * self.space0.smspace.mass_matrix()  #
        self.MC = self.space1.cell_mass_matrix() / dt
Ejemplo n.º 3
0
    def __init__(self,
                 vdata,
                 cdata,
                 mesh,
                 timeline,
                 p=0,
                 options={'rdir': '/home/why/result'}):

        self.options = options
        self.vdata = vdata
        self.cdata = cdata
        self.mesh = mesh
        self.timeline = timeline
        self.uspace = RaviartThomasFiniteElementSpace2d(mesh, p=p)
        self.cspace = ScaledMonomialSpace2d(mesh, p=p + 1)

        self.uh = self.uspace.function()  # 速度场自由度数组
        self.ph = self.uspace.smspace.function()  # 压力场自由度数组
        self.ch = self.cspace.function()  # 浓度场自由度数组

        # 初始浓度场设为 1
        ldof = self.cspace.number_of_local_dofs()
        self.ch[0::ldof] = 1.0

        self.M = self.cspace.cell_mass_matrix()
        self.H = inv(self.M)
        self.set_init_velocity_field()  # 计算初始的速度场和压力场

        # vtk 文件输出
        node, cell, cellType, NC = self.mesh.to_vtk()
        self.points = vtk.vtkPoints()
        self.points.SetData(vnp.numpy_to_vtk(node))
        self.cells = vtk.vtkCellArray()
        self.cells.SetCells(NC, vnp.numpy_to_vtkIdTypeArray(cell))
        self.cellType = cellType
Ejemplo n.º 4
0
    def stokes_equation_test(self, p=2, maxit=4):
        from scipy.sparse import bmat
        from fealpy.pde.Stokes_Model_2d import CosSinData
        from fealpy.mesh.simple_mesh_generator import triangle
        h = 0.4
        pde = CosSinData()
        error = np.zeros((maxit, ), dtype=np.float)
        for i in range(maxit):
            mesh = pde.init_mesh(n=i + 2, meshtype='poly')

            if 0:
                fig = plt.figure()
                axes = fig.gca()
                mesh.add_plot(axes)
                plt.show()

            uspace = DivFreeNonConformingVirtualElementSpace2d(mesh, p)
            pspace = ScaledMonomialSpace2d(mesh, p - 1)

            isBdDof = uspace.boundary_dof()

            udof = uspace.number_of_global_dofs()
            pdof = pspace.number_of_global_dofs()

            uh = uspace.function()
            ph = pspace.function()
            uspace.set_dirichlet_bc(uh, pde.dirichlet)

            A = uspace.matrix_A()
            P = uspace.matrix_P()
            F = uspace.source_vector(pde.source)

            AA = bmat([[A, P.T], [P, None]], format='csr')
            FF = np.block([F, np.zeros(pdof, dtype=uspace.ftype)])
            x = np.block([uh.T.flat, ph])
            isBdDof = np.block(
                [isBdDof, isBdDof,
                 np.zeros(pdof, dtype=np.bool)])

            gdof = 2 * udof + pdof
            FF -= AA @ x
            bdIdx = np.zeros(gdof, dtype=np.int)
            bdIdx[isBdDof] = 1
            Tbd = spdiags(bdIdx, 0, gdof, gdof)
            T = spdiags(1 - bdIdx, 0, gdof, gdof)
            AA = T @ AA @ T + Tbd
            FF[isBdDof] = x[isBdDof]
            x[:] = spsolve(AA, FF)
            uh[:, 0] = x[:udof]
            uh[:, 1] = x[udof:2 * udof]
            ph[:] = x[2 * udof:]

            up = uspace.project_to_smspace(uh)
            integralalg = uspace.integralalg
            error[i] = integralalg.L2_error(pde.velocity, up)
            h /= 2

        print(error)
        print(error[0:-1] / error[1:])
Ejemplo n.º 5
0
    def one_cell_test_0(self, p=2):
        from fealpy.pde.stokes_model_2d import StokesModelData_7
        pde = StokesModelData_7()
        node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)], dtype=np.float)
        cell = np.array([0, 1, 2, 3], dtype=np.int)
        cellLocation = np.array([0, 4], dtype=np.int)
        mesh = PolygonMesh(node, cell, cellLocation)

        uspace = DivFreeNonConformingVirtualElementSpace2d(mesh, p)
        pspace = ScaledMonomialSpace2d(mesh, p - 1)
        ldof = pspace.number_of_local_dofs()

        isBdDof = uspace.boundary_dof()

        udof = uspace.number_of_global_dofs()
        pdof = pspace.number_of_global_dofs()

        uh = uspace.function()
        ph = pspace.function()
        uspace.set_dirichlet_bc(uh, pde.dirichlet)

        A = uspace.matrix_A()
        P = uspace.matrix_P()
        C = uspace.CM[:, 0, :ldof].reshape(-1)
        F = uspace.source_vector(pde.source)

        AA = bmat([[A, P.T, None], [P, None, C[:, None]], [None, C, None]],
                  format='csr')
        FF = np.block([F, np.zeros(pdof + 1, dtype=uspace.ftype)])
        x = np.block([uh, ph, np.zeros((1, ), dtype=uspace.ftype)])
        isBdDof = np.r_['0', isBdDof, np.zeros(pdof + 1, dtype=np.bool)]
        gdof = udof + pdof + 1

        FF -= AA @ x
        bdIdx = np.zeros(gdof, dtype=np.int)
        bdIdx[isBdDof] = 1
        Tbd = spdiags(bdIdx, 0, gdof, gdof)
        T = spdiags(1 - bdIdx, 0, gdof, gdof)
        AA = T @ AA @ T + Tbd
        FF[isBdDof] = x[isBdDof]
        x[:] = spsolve(AA, FF)
        uh[:] = x[:udof]
        ph[:] = x[udof:-1]
        print('ph:', ph)

        print('uh:', uh)
        up = uspace.project_to_smspace(uh)
        print('up:', up)
        integralalg = uspace.integralalg
        error = integralalg.L2_error(pde.velocity, up)
        print(error)

        uv = uspace.project(pde.velocity)
        print('uproject:', uv)
        up = uspace.project_to_smspace(uv)
        print('up', up)

        error = integralalg.L2_error(pde.velocity, up)
        print(error)
Ejemplo n.º 6
0
 def index2_test(self, p=2):
     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)
     cellLocation = np.array([0, 4], dtype=np.int)
     mesh = PolygonMesh(node, cell, cellLocation)
     space = ScaledMonomialSpace2d(mesh, 3)
     idx = space.index2(p=p)
     print("p=", p, "\n", idx)
Ejemplo n.º 7
0
    def edge_mass_matrix_test(self, p=2):

        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)
        cellLocation = np.array([0, 4], dtype=np.int)
        mesh = PolygonMesh(node, cell, cellLocation)
        space = ScaledMonomialSpace2d(mesh, 3)
        print("new: p=", p, "\n", space.edge_mass_matrix(p=p))
        print("old: p=", p, "\n", space.edge_mass_matrix_1(p=p))
Ejemplo n.º 8
0
    def __init__(self, model):
        self.model = model
        self.mesh = model.space_mesh()
        self.timeline = model.time_mesh(n=3650)
        self.uspace = RaviartThomasFiniteElementSpace2d(self.mesh, p=0)
        self.cspace = ScaledMonomialSpace2d(self.mesh, p=1)  # 线性间断有限元空间

        self.uh = self.uspace.function()  # 速度
        self.ph = model.init_pressure(self.uspace.smspace)  # 初始压力

        # 三个组分的摩尔密度, 三个组分一起计算
        self.ch = model.init_molar_density(self.cspace)

        # TODO:初始化三种物质的浓度
        # 1 muPa*s = 1e-11 bar*s = 1e-11*24*3600 bar*d = 8.64e-07 bar*d
        # 1 md = 9.869 233e-16 m^2
        self.options = {
            'viscosity':
            1.0,  # 粘性系数 1 muPa*s = 1e-6 Pa*s, 1 cP = 10^{−3} Pa⋅s = 1 mPa⋅s
            'permeability': 1.0,  # 1 md 渗透率, 1 md = 9.869233e-16 m^2
            'temperature': 397,  # 初始温度 K
            'pressure': 50,  # bar 初始压力
            'porosity': 0.2,  # 孔隙度
            'injecttion_rate': 0.1,  # 注入速率
            'compressibility': 0.001,  #压缩率
            'pmv': (1.0, 1.0, 1.0),  # 偏摩尔体积
            'rdir': '/home/why/result/test/',
            'step': 1
        }
        self.CM = self.cspace.cell_mass_matrix()
        self.H = inv(self.CM)

        c = 8.64 / 9.869233 * 1e+9
        self.M = c * self.uspace.mass_matrix()
        self.B = -self.uspace.div_matrix()

        dt = self.timeline.dt
        c = self.options['porosity'] * self.options['compressibility'] / dt
        self.D = c * self.uspace.smspace.mass_matrix()

        # 压力边界条件
        self.F0 = -self.uspace.set_neumann_bc(model.pressure_bc,
                                              threshold=model.is_pressure_bc)

        # vtk 文件输出
        node, cell, cellType, NC = self.mesh.to_vtk()
        self.points = vtk.vtkPoints()
        self.points.SetData(vnp.numpy_to_vtk(node))
        self.cells = vtk.vtkCellArray()
        self.cells.SetCells(NC, vnp.numpy_to_vtkIdTypeArray(cell))
        self.cellType = cellType
Ejemplo n.º 9
0
 def project_test(self, p=2, m=0):
     if m == 0:
         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)
         cellLocation = np.array([0, 4], dtype=np.int)
         mesh = PolygonMesh(node, cell, cellLocation)
     elif m == 1:
         pde = CosCosData()
         qtree = pde.init_mesh(n=4, meshtype='quadtree')
         mesh = qtree.to_polygonmesh()
     points = np.array([[(0.5, 0.5), (0.6, 0.6)]], dtype=np.float)
     space = ScaledMonomialSpace2d(mesh, p)
     gphi = space.grad_basis(points)
     print(gphi)
Ejemplo n.º 10
0
errorMatrix = np.zeros((3, maxit), dtype=np.float)
dof = np.zeros(maxit, dtype=np.float)

for i in range(maxit):
    #mesh = pde.init_mesh(n=i+2, meshtype='poly')
    mesh = pde.init_mesh(n=i + 2, meshtype='quad')
    mesh = PolygonMesh.from_mesh(mesh)

    NE = mesh.number_of_edges()
    NC = mesh.number_of_cells()
    idof = (p - 2) * (p - 1) // 2

    dof[i] = NC

    uspace = ReducedDivFreeNonConformingVirtualElementSpace2d(mesh, p, q=6)
    pspace = ScaledMonomialSpace2d(mesh, 0)

    isBdDof = uspace.boundary_dof()

    udof = uspace.number_of_global_dofs()
    pdof = pspace.number_of_global_dofs()

    uh = uspace.function()
    ph = pspace.function()

    A = uspace.matrix_A()
    P = uspace.matrix_P()
    F = uspace.source_vector(pde.source)

    AA = bmat([[A, P.T], [P, None]], format='csr')
    FF = np.block([F, np.zeros(pdof, dtype=uspace.ftype)])
    def stokes_equation_test(self, p=2, maxit=4, mtype=1):
        from scipy.sparse import bmat
        from fealpy.pde.Stokes_Model_2d import CosSinData, PolyY2X2Data
        from fealpy.mesh.simple_mesh_generator import triangle
        h = 0.4
        #pde = CosSinData()
        pde = PolyY2X2Data()
        error = np.zeros((maxit, ), dtype=np.float)
        for i in range(maxit):
            if mtype == 0:
                node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)],
                                dtype=np.float)
                cell = np.array([0, 1, 2, 3], dtype=np.int)
                cellLocation = np.array([0, 4], dtype=np.int)
                mesh = PolygonMesh(node, cell, cellLocation)
            else:
                mesh = pde.init_mesh(n=i + 2, meshtype='poly')

            NE = mesh.number_of_edges()
            NC = mesh.number_of_cells()
            idof = (p - 2) * (p - 1) // 2

            if True:
                fig = plt.figure()
                axes = fig.gca()
                mesh.add_plot(axes)

            uspace = ReducedDivFreeNonConformingVirtualElementSpace2d(mesh, p)
            pspace = ScaledMonomialSpace2d(mesh, 0)

            isBdDof = uspace.boundary_dof()

            udof = uspace.number_of_global_dofs()
            pdof = pspace.number_of_global_dofs()

            uh = uspace.function()
            ph = pspace.function()
            uspace.set_dirichlet_bc(uh, pde.dirichlet)

            A = uspace.matrix_A()
            P = uspace.matrix_P()
            F = uspace.source_vector(pde.source)

            AA = bmat([[A, P.T], [P, None]], format='csr')
            FF = np.block([F, np.zeros(pdof, dtype=uspace.ftype)])
            x = np.block([uh, ph])
            isBdDof = np.block(
                [isBdDof, isBdDof,
                 np.zeros(NC * idof + pdof, dtype=np.bool)])

            gdof = udof + pdof
            FF -= AA @ x
            bdIdx = np.zeros(gdof, dtype=np.int)
            bdIdx[isBdDof] = 1
            Tbd = spdiags(bdIdx, 0, gdof, gdof)
            T = spdiags(1 - bdIdx, 0, gdof, gdof)
            AA = T @ AA @ T + Tbd
            FF[isBdDof] = x[isBdDof]
            x[:] = spsolve(AA, FF)
            uh[:] = x[:udof]
            ph[:] = x[udof:]

            up = uspace.project_to_smspace(uh)
            integralalg = uspace.integralalg
            error[i] = integralalg.L2_error(pde.velocity, up)
            h /= 2

        print(error)
        print(error[0:-1] / error[1:])
        plt.show()