On May 26, 2011, at 2:11 AM, Fangohr H. wrote:

> here is my next query: at this point, I would like to modify the value of phi 
> at a particular position r=(x,y).
> 
> To do this, I want to find the mesh cell in which the position r is located. 
> Once I have this, for example through the 'index' of that cell, then I can 
> (for example) read the value via
> 
>>>> tmp=phi.getValue()
> 
> modify it by dphi
> 
>>>> tmp[index] += dphi
> 
> and write it back
> 
>>>> phi.setValue(tmp)
> 
> if I understand things correctly.
> 
> My question is: is there a function that gives me the index for a given mesh 
> and position r?
> 
> If there are better ways to change the value of phi in one cell, then I'd be 
> interested to hear about those of course.


Generally, the easiest way to do this in FiPy is not with an index, but with a 
mask:

  >>> phi.setValue(phi + dphi, where=[False, False, False, True, False, False, 
...])

This probably doesn't seem very easy, because we make a point of not promising 
what index corresponds to what position, so how would you know which value to 
set True? 

You can use the cell coordinates to describe just about any configuration, 
though:

  >>> x, y = mesh.getCellCenters()
  >>> phi.setValue(phi + dphi, where=(abs(x - x0) < dx) & (abs(y - y0) < dy))

Often, you could get away with

  >>> phi.setValue(phi + dphi, where=(x == x0) & (y == y0))

but x, y are the floating point coordinates of the cell centers, and so there's 
no guarantee that they exactly match the values of x0, y0. You can either use 
the comparisons as above, or use numerix.isclose().

This syntax allows you to describe all sorts of geometries. E.g.,

  >>> phi.setValue(phi + dphi, where=((x - x0)**2 + (y - y0)**2 < r0**2) & (x < 
x0))

will change the value in a left hemicircle.

Reply via email to