On Fri, Nov 30, 2012 at 6:33 AM, A. A. Eftekhari <[email protected]>wrote:

> Dear all,
>
>
> n = 5
> mesh = Grid2D(nx = n, ny = n)
> k = rand(n,n)
> phi = CellVariable(mesh)
> phi.value = k ????
>

In the example above, one can just create a flat array

   k = rand(mesh.numberOfCells)

No need for a 2D array and mostly there is never any need to use a 2D
array. If you did for some reason have a 2D array, then you would have to
interpolate it to the grid since you have no idea about the ordering of a
FiPy mesh. There is some internal ordering for FiPy's native grid classes
only, but it isn't a good idea to rely on that. Any general FiPy mesh may
have any ordering. A GmshGrid could have any ordering for example.

Given that, to interpolate from 2D grid data to any general FiPy mesh the
following works.


    import numpy as np
    import fipy as fp
    import scipy.interpolate

    nx = 5
    ny = 10
    mesh = fp.Grid2D(nx=nx, ny=ny)

    ## Generate grid data (could be any nx, ny, dx, dy, independent of the
mesh).
    k = np.random.rand(mesh.nx, mesh.ny)
    x = np.arange(mesh.nx) * mesh.dx + mesh.dx / 2. + mesh.origin[0]
    y = np.arange(mesh.ny) * mesh.dy + mesh.dy / 2. + mesh.origin[1]
    xx, yy = np.meshgrid(x, y)
    points = np.array((xx.flatten(), yy.flatten())).swapaxes(0,1)
    values = np.array(k.flatten())

    ## Interpolate grid data to the fipy mesh (don't have to use nearest
neighbour, only using it in this case because the data points happen to be
aligned).
    phi = fp.CellVariable(mesh=mesh)
    phi[:] = scipy.interpolate.NearestNDInterpolator(points,
values)((mesh.x, mesh.y))

Cheers

-- 
Daniel Wheeler
_______________________________________________
fipy mailing list
[email protected]
http://www.ctcms.nist.gov/fipy
  [ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ]

Reply via email to