2
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.

  • See the docs: you need to define boundary conditions on external faces, otherwise the result is Zero. – Richard Barber Feb 27 '21 at 05:57
  • The Dirichlet constraints as specified at left and right are fine. Top and bottom faces naturally get a zero flux condition. – jeguyer Feb 28 '21 at 00:14

2 Answers2

0

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]
jeguyer
  • 2,379
  • 1
  • 11
  • 15
  • If this answered your question, [please accept](https://stackoverflow.com/help/someone-answers). – jeguyer Mar 01 '21 at 13:27
-1

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