FiPy Setting Initial Grid Values

294 Views Asked by At

I am a usecase whereby I need to transpose values from a 2D Numpy array to a 2D FiPy grid which will then be used to solve the system.

My code is as follows:

for x in range(0, size[0]):
            for y in range(0, size[1]):
                source.setValue(tmpSource.getGrid()[x][y], where=(X < x + 1) & (X > x) & (Y > y) & (Y < y + 1))
                sink.setValue(tmpSink.getGrid()[x][y], where=(X < x + 1) & (X > x) & (Y > y) & (Y < y + 1))

Where tmpSource and tmpSink are said Numpy arrays.

While this approach works, it is extremely slow to run. Does anybody have any advice on alternative approaches that could make it faster?

Thanks!

1

There are 1 best solutions below

1
On BEST ANSWER

You say "transpose", but your example seems to be a simple transfer of data, without swapping axes or ranges of data. Moving values from a 2D numpy array into a FiPy CellVariable defined on a Grid2D should be as simple as flattening the array:

source.value = tmpSource.getGrid().flatten()
sink.value = tmpSink.getGrid().flatten()