我已经运行以下代码来模拟2D网格中圆柱体周围的流动:
from fipy import CellVariable, FaceVariable, Grid2D, DiffusionTerm, ImplicitSourceTerm, PeriodicGrid2DTopBottom, DistanceVariable, Viewer
from fipy.tools import numerix
L = 1.0
N = 50
dL = L / N
viscosity = 1
U = 1.
#0.8 for pressure and 0.5 for velocity are typical relaxation values for SIMPLE
pressureRelaxation = 0.8
velocityRelaxation = 0.5
if __name__ == '__main__':
sweeps = 500
else:
sweeps = 5
mesh = PeriodicGrid2DTopBottom(nx=N, ny=N, dx=dL, dy=dL)
pressure = CellVariable(mesh=mesh, name='pressure')
pressureCorrection = CellVariable(mesh=mesh)
xVelocity = CellVariable(mesh=mesh, name='X velocity')
yVelocity = CellVariable(mesh=mesh, name='Y velocity')
velocity = FaceVariable(mesh=mesh, rank=1)
pfi=3000.
lfi=0.01
x, y = mesh.cellCenters
var1 = DistanceVariable(name='distance to center', mesh=mesh, value=numerix.sqrt((x-N*dL/2.)**2+(y-N*dL/2.)**2))
rad=0.1
pi_fi= CellVariable(mesh=mesh, value=0.,name='Fluid-interface energy map')
pi_fi.setValue(pfi*numerix.exp(-1.*(var1-rad)/lfi), where=(var1 > rad) )
pi_fi.setValue(pfi, where=(var1 <= rad))
xVelocityEq = DiffusionTerm(coeff=viscosity) - pressure.grad.dot([1., 0.]) - ImplicitSourceTerm(pi_fi)
yVelocityEq = DiffusionTerm(coeff=viscosity) - pressure.grad.dot([0., 1.]) - ImplicitSourceTerm(pi_fi)
ap = CellVariable(mesh=mesh, value=1.)
coeff = 1./ ap.arithmeticFaceValue*mesh._faceAreas * mesh._cellDistances
pressureCorrectionEq = DiffusionTerm(coeff=coeff) - velocity.divergence
from fipy.variables.faceGradVariable import _FaceGradVariable
volume = CellVariable(mesh=mesh, value=mesh.cellVolumes, name='Volume')
contrvolume=volume.arithmeticFaceValue
xVelocity.constrain(U, mesh.facesLeft | mesh.facesRight)
yVelocity.constrain(0., mesh.facesLeft | mesh.facesRight)
X, Y = mesh.faceCenters
pressureCorrection.constrain(0., mesh.facesLeft & (Y < dL))
if __name__ == '__main__':
viewer = Viewer(vars=(pressure, xVelocity, yVelocity, velocity),
xmin=0., xmax=1., ymin=0., ymax=1., colorbar=True)
from builtins import range
for sweep in range(sweeps):
## solve the Stokes equations to get starred values
xVelocityEq.cacheMatrix()
xres = xVelocityEq.sweep(var=xVelocity,
underRelaxation=velocityRelaxation)
xmat = xVelocityEq.matrix
yres = yVelocityEq.sweep(var=yVelocity,
underRelaxation=velocityRelaxation)
## update the ap coefficient from the matrix diagonal
ap[:] = -numerix.asarray(xmat.takeDiagonal())
## update the face velocities based on starred values with the
## Rhie-Chow correction.
## cell pressure gradient
presgrad = pressure.grad
## face pressure gradient
facepresgrad = _FaceGradVariable(pressure)
velocity[0] = xVelocity.arithmeticFaceValue \
+ contrvolume / ap.arithmeticFaceValue * \
(presgrad[0].arithmeticFaceValue-facepresgrad[0])
velocity[1] = yVelocity.arithmeticFaceValue \
+ contrvolume / ap.arithmeticFaceValue * \
(presgrad[1].arithmeticFaceValue-facepresgrad[1])
velocity[..., mesh.exteriorFaces.value] = 0.
velocity[0, mesh.facesLeft.value] = U
velocity[0, mesh.facesRight.value] = U
## solve the pressure correction equation
pressureCorrectionEq.cacheRHSvector()
## left bottom point must remain at pressure 0, so no correction
pres = pressureCorrectionEq.sweep(var=pressureCorrection)
rhs = pressureCorrectionEq.RHSvector
## update the pressure using the corrected value
pressure.setValue(pressure + pressureRelaxation * pressureCorrection )
## update the velocity using the corrected pressure
xVelocity.setValue(xVelocity - pressureCorrection.grad[0] / \
ap * mesh.cellVolumes)
yVelocity.setValue(yVelocity - pressureCorrection.grad[1] / \
ap * mesh.cellVolumes)
if __name__ == '__main__':
if sweep%10 == 0:
print('sweep:', sweep, ', x residual:', xres, \
', y residual', yres, \
', p residual:', pres, \
', continuity:', max(abs(rhs)))
viewer.plot()
print(pressure.globalValue[..., 510])
print(xVelocity.globalValue[..., 510])
print(yVelocity.globalValue[..., 510])
这应该用周期顶部/底部周期条件和网格中心的圆柱体来解决Navier-stokes方程(这就是为什么我的方程中有一个隐式源项)。速度在左边界和右边界等于一,并且平行于x轴。该示例大致对应于通过许多等距分布的圆柱形障碍物的流动。当我没有并行计算运行它时,压力和速度曲线看起来还不错。为单元格510打印的值为 2.788(压力),1.104(x速度)和-0.289(yvelocity)。
但是,当我以并行模式运行(例如使用2个处理器)时,配置文件看起来很奇怪。对于速度曲线,y = 0.2和y = 0.8之间的曲线与y = 0和y = 1之间的顺序计算得出的曲线大致相似。压力曲线却大不相同。为单元格510打印的值是 -3.163(压力),1.209(x速度)和-0.044(yvelocity)。
要使用 Grid2D 代替 PeriodicGrid2DTopBottom ,我添加了额外的边界条件,我相信在本示例中这将等同于使用周期性网格。然后,新的BC是:
xVelocity.constrain(U, mesh.facesLeft | mesh.facesRight)
xVelocity.faceGrad.constrain(mesh.faceNormals * 0., where=mesh.facesBottom | mesh.facesTop)
yVelocity.constrain(0., mesh.exteriorFaces)
这样做,通过在两个处理器上依次或并行运行,我得到相同的输出: 2.784(压力),1.104(x速度)和-0.290(yvelocity)。
使用周期性网格时边界条件是否未指定? (我想这将为同一问题解释两种不同的解决方案)还是并行计算和周期性网格由于某种原因而无法相处?