2013/3/20 Jonathan Guyer <[email protected]>: > > On Mar 20, 2013, at 4:51 PM, Serbulent UNSAL wrote: > ... > 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.
Yes they will give same result > >> 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() > No itertools.chain is clearly fastest method. It can be seen on http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python and I test it against flatten with the code: http://pastie.org/7094117 gives the result (as second ); Numpy flatten() method consumes with numpy ndarray: 141.177155972 Numpy flatten() method consumes with 2d list: 140.103730917 itertools chain() method consumes with 2d list: 14.1506669521 > 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? > > It comes from an external source actually from a 2d cellular automata system. As I mention "I think an array in a CellVariable is 1d array (as in the phi variable) and I should convert it from 2d to 1d." But I try to figure out how FiPy can accept 2d source with 1d Cell variable try to initialize CellVariable with a 2d matrix ends with "ValueError: array is too big" error for me. >> 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) > Thanks for suggestion I'll use it. And one more question on this problem. How can I create a contraint on CellVariable phi that it can't get a value below zero. Because on a diffusion problem it is meaningless that a call has negative fluid concentration. I need something like; phi = CellVariable(name="solution variable", mesh=mesh, value = 1., minimumValue = 0. ) Best Regards, Serbulent _______________________________________________ fipy mailing list [email protected] http://www.ctcms.nist.gov/fipy [ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ]
