Search code examples
fipy

Fipy Get values from a mesh region and not by point coordinates


I am trying to get values from a region. I know that there is a method to call the values by introducing the coordinates:

var([(x1, x2, x3) #xvalues
     (y1, y2, y3)]) #yvalues

But, for instance, it is confusing to how many points and which coordinates they have if I previously set the values by using the 'where' attr in var.setValue (which accepts regions).

var.setValue(20., where=(Y <= y1) & ((X <= x1) & (X > x2)))

Is there any method to get the values by "querying" a region as in the var.setValue(where='...')?

Thank you.


Solution

  • If it's possible to construct a mask for a region from the cell coordinates then that mask can be used to extract the values. There is no special function in FiPy to do this, but we can use a boolean mask to index into the variable. For example,

    from fipy import CellVariable, Grid2D
    
    mesh = Grid2D(nx=4, ny=4)
    var = CellVariable(mesh=mesh)
    mask = (mesh.y <= 2) & (mesh.x <= 3) & (mesh.x > 1)
    var.setValue(20., where=mask)
    print('x:', mesh.x[mask.value])
    print('y:', mesh.y[mask.value])
    print('val:', var[mask.value])
    print(var.mesh)
    print(var[mask.value].mesh)
    

    gives

    x: [1.5 2.5 1.5 2.5]
    y: [0.5 0.5 1.5 1.5]
    val: [20. 20. 20. 20.]
    UniformGrid2D(dx=1.0, nx=4, dy=1.0, ny=4)
    Traceback (most recent call last):
      File "tmp.py", line 11, in <module>
        print(var[mask.value].mesh)
    AttributeError: 'unOp' object has no attribute 'mesh'
    

    This above gets you the sub-region of values along with the corresponding coordinates. However, there is no way in FiPy to extract a sub-region variable that also maintains its associated mesh data.