[Python] Why does the divergence value at the left and right boundaries show up as 1 instead...

Discussão em 'Python' iniciado por Stack, Abril 23, 2025.

  1. Stack

    Stack Membro Participativo

    I have tried two different ways of calculating a vector's divergence using Fipy: one using the gradient of its components and the other using face divergence. For simplicity, I have used a vector such that it has a constant x-component and a y-component is zero. In either case, the value of divergence at the left and right edge is different than the expected value. Please, help me understand and fix this. Below is the code for the check

    import matplotlib.pyplot as plt
    from fipy import Grid2D, CellVariable, FaceVariable

    # Define the mesh
    nx, ny = 100, 100 # Number of cells
    dx, dy = 0.1, 0.1 # Cell size
    mesh = Grid2D(nx=nx, ny=ny, dx=dx, dy=dy)

    # Get cell center coordinates
    x, y = mesh.cellCenters

    # Define constant field variables
    C = 2 # Constant gradient of ux along x axis
    D = 0 # Constant uy
    ux = CellVariable(name="ux", mesh=mesh, value=C*x)
    uy = CellVariable(name="uy", mesh=mesh, value=D)

    # Define the vector field
    uvec = (ux, uy)

    # Compute divergence using gradient components
    div_u = CellVariable(name="div(u)", mesh=mesh, value=(ux.grad[0] + uy.grad[1]))

    # Compute divergence using face variable
    ux_face = ux.arithmeticFaceValue
    uy_face = uy.arithmeticFaceValue
    u = FaceVariable(mesh=mesh, value=(ux_face, uy_face))
    facediv_u = CellVariable(name="face_div(u)", mesh=mesh, value=u.divergence)

    print(div_u, "gradcomponents")
    print(facediv_u, "facedivergence")


    which gives the following result;

    [1. 2. 2. ... 2. 2. 1.] gradcomponents
    [1. 2. 2. ... 2. 2. 1.] facedivergence

    Continue reading...

Compartilhe esta Página