On Mar 20, 2013, at 4:51 PM, Serbulent UNSAL wrote:

> First I'm sorry for confusion. I should share both versions (slow one
> and mine) but only share last version. But before that explaining
> problem domain might be helpful for smoothing confusion away.
> 
> I'm working on tumor modelling. The code I sent is the part which
> calculates oxygen diffusion in a tissue with 4 boundaries (they
> represent blood veins).
> 
> Oxygen diffusion calculated on this module but based on each cells
> oxygen consumption. The function "myFunc" will call lots of
> complicated functions that calculates oxygen consumption for each
> living cell. But for the sake of simplicity in this phase I assume
> each cell has a static consumption. This consumption is represented by
> source term.

Thank you for the explanation.

> At the end I'll have a consumption matrix for each time step like; "at
> time step t cell at (x,y) has 0.00001 mol oxygen consumption"  and so
> on.

Why does it need to be in a matrix? If the calculation is based on your 
coordinates, then let the shape of the coordinate dictate the shape of the 
result, i.e., fn(x, y, t) will automatically be the same shape as x and y.

> So at first i think that I should pass x and y to my function at each
> time step but it makes the code slower so I decided calculate
> cordinates with "nx" and "dx" parameters in the future.
> 
> itertools.chain -> list business was used for setting source matrix
> the source term because I think an array in a CellVariable is 1d array
> (as in the phi variable) and I should convert it from 2d to 1d.

Much more expediently done with 

    return tmp.flatten()

but also probably not necessary. If the array can be flattened satisfactorily, 
then FiPy will likely accept the 2d array without complaint. I still wonder why 
it needs to be 2d in the first place, though, or is it coming from some 
external source (e.g., an experimental image) that is 2d by nature?


> The first code which I mentioned as slow and forget to add is here:
> http://pastie.org/6642897

This code is slow because you are looping over each cell center in Python with:

for pt_ind in range(nx*ny):
        (x, y) = (X[pt_ind], Y[pt_ind])  

You should not ever do this. Instead, write a vectorized expression over all x 
and y, like Dan Wheeler posted yesterday:

  eqn = someTerms + mesh.x * mesh.y * t 

or, for your case, 

  eqn = someTerms + myFunc(mesh.x, mesh.y, t)

If, for some reason, you need to recalculate and assign the source in your 
solve loop, then you still wouldn't use a loop. Just write

  for step in range(steps):
         
      source.value = myFunc(mesh.x, mesh.y, t)


_______________________________________________
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