fipy: 2D Laplace equation: boundary conditions not taken

Viewed 68
from fipy import CellVariable, Grid2D, DiffusionTerm
nx = 20         # grid size on coordinate axes
dx = 1          # grid spacing
ny = nx; dy = dx; L = dx*nx
mesh = Grid2D(dx=dx, dy=dy, nx=nx, ny=ny)
phi = CellVariable(name = "phi", mesh = mesh, value = 0.5)
eq = (0. == DiffusionTerm( coeff=1., var=phi))
valueTopLeft = 0.; valueBottomRight = 1.
phi.constrain(valueTopLeft, where = mesh.facesLeft)
phi.constrain(valueBottomRight, where = mesh.facesRight)
eq.solve(var=phi)
print (phi.value[:])

What's wrong with this code? Code produces zero solution, which is not correct.

2 Answers

There is a bug in FiPy if you specify the grid spacing as an integer. Changing to dx = 1. results in

[0.025 0.075 0.125 0.175 0.225 0.275 0.325 0.375 0.425 0.475 0.525 0.575
 0.625 0.675 0.725 0.775 0.825 0.875 0.925 0.975 0.025 0.075 0.125 0.175
 0.225 0.275 0.325 0.375 0.425 0.475 0.525 0.575 0.625 0.675 0.725 0.775
 0.825 0.875 0.925 0.975 0.025 0.075 0.125 0.175 0.225 0.275 0.325 0.375
 0.425 0.475 0.525 0.575 0.625 0.675 0.725 0.775 0.825 0.875 0.925 0.975
 0.025 0.075 0.125 0.175 0.225 0.275 0.325 0.375 0.425 0.475 0.525 0.575
 0.625 0.675 0.725 0.775 0.825 0.875 0.925 0.975 0.025 0.075 0.125 0.175
 0.225 0.275 0.325 0.375 0.425 0.475 0.525 0.575 0.625 0.675 0.725 0.775
 0.825 0.875 0.925 0.975 0.025 0.075 0.125 0.175 0.225 0.275 0.325 0.375
 0.425 0.475 0.525 0.575 0.625 0.675 0.725 0.775 0.825 0.875 0.925 0.975
 0.025 0.075 0.125 0.175 0.225 0.275 0.325 0.375 0.425 0.475 0.525 0.575
 0.625 0.675 0.725 0.775 0.825 0.875 0.925 0.975 0.025 0.075 0.125 0.175
 0.225 0.275 0.325 0.375 0.425 0.475 0.525 0.575 0.625 0.675 0.725 0.775
 0.825 0.875 0.925 0.975 0.025 0.075 0.125 0.175 0.225 0.275 0.325 0.375
 0.425 0.475 0.525 0.575 0.625 0.675 0.725 0.775 0.825 0.875 0.925 0.975
 0.025 0.075 0.125 0.175 0.225 0.275 0.325 0.375 0.425 0.475 0.525 0.575
 0.625 0.675 0.725 0.775 0.825 0.875 0.925 0.975 0.025 0.075 0.125 0.175
 0.225 0.275 0.325 0.375 0.425 0.475 0.525 0.575 0.625 0.675 0.725 0.775
 0.825 0.875 0.925 0.975 0.025 0.075 0.125 0.175 0.225 0.275 0.325 0.375
 0.425 0.475 0.525 0.575 0.625 0.675 0.725 0.775 0.825 0.875 0.925 0.975
 0.025 0.075 0.125 0.175 0.225 0.275 0.325 0.375 0.425 0.475 0.525 0.575
 0.625 0.675 0.725 0.775 0.825 0.875 0.925 0.975 0.025 0.075 0.125 0.175
 0.225 0.275 0.325 0.375 0.425 0.475 0.525 0.575 0.625 0.675 0.725 0.775
 0.825 0.875 0.925 0.975 0.025 0.075 0.125 0.175 0.225 0.275 0.325 0.375
 0.425 0.475 0.525 0.575 0.625 0.675 0.725 0.775 0.825 0.875 0.925 0.975
 0.025 0.075 0.125 0.175 0.225 0.275 0.325 0.375 0.425 0.475 0.525 0.575
 0.625 0.675 0.725 0.775 0.825 0.875 0.925 0.975 0.025 0.075 0.125 0.175
 0.225 0.275 0.325 0.375 0.425 0.475 0.525 0.575 0.625 0.675 0.725 0.775
 0.825 0.875 0.925 0.975 0.025 0.075 0.125 0.175 0.225 0.275 0.325 0.375
 0.425 0.475 0.525 0.575 0.625 0.675 0.725 0.775 0.825 0.875 0.925 0.975
 0.025 0.075 0.125 0.175 0.225 0.275 0.325 0.375 0.425 0.475 0.525 0.575
 0.625 0.675 0.725 0.775 0.825 0.875 0.925 0.975 0.025 0.075 0.125 0.175
 0.225 0.275 0.325 0.375 0.425 0.475 0.525 0.575 0.625 0.675 0.725 0.775
 0.825 0.875 0.925 0.975]

Thanks a lot. I should have found the problem myself. Now I am ready for more fancy things!

Related