Scotty1- Scotty1- - 1 year ago 62
Python Question

Array indexing by array-value and array-value dependant operation

I'm developing a finite-volume heat-transfer model in python with numpy. I've got a number of cells stacked vertically (for the next year a transfer to a 3d-model is planned) with each having a different temperature.

*To calculate the heat transfer between the cells, I need the thermal conductivity depending on the cell-temperature.
The thermal conductivity with its corresponding temperature (in degree Celsius) is stored in matrix
, the cell-temperatures (in degree Celsius) are stored in vector (in the future in a matrix)
. These arrays look like:

TC = numpy.array([[0,569],[1,574],[2,582],[3,590],[4,598],[5,606],[6,613],[7,620]])
T_Cell = numpy.array([[7],[5],[5],[4],[4],[3],[1],[0],[0]])

The temperatures in
correspond to their row-index, so that accessing the thermal conductivity for example for a cell temperature of
can be done by indexing with the temperature (the value stored in T_Cell). Here shown for the cell with Index 2:

TC_Cell = TC[numpy.round(T_Cell[2]),1]

Resulting in
TC_Cell = 606
Is there an array-operation which allows me to get the cell's thermal conductivity (depending only on the cell's temperature) in an array with the same shape as
? For example with the arrays of
as shown above:

TC_Cell = TC[T_Cell, 1]

So that the result for TC_Cell looks like:

TC_Cell = array([[620],

Interpolation is NOT needed as I already interpolated the values in TC to a satisfying degree (not shown here to keep it clean, values in the array are also simplified and physically not correct).*

I really don't know why, but suddenly it is working exactly like shown in my example... Maybe I had a typo somewhere in my code... :-/ Though my second question remains unresolved.

My second question is:
I've got a differential equation with a solution which changes depending if one argument is zero or non-zero. This argument is depending on the cell, so it might be

Arg = numpy.array([[0.12],[0.9],[0],[0],[0.2]])

Currently my way to decide which solution to use is to run a for-loop over the
-vector (in the future: 3d-array) and to check if a cell is
. Like:

for cell in range(numpy.size(Arg, 0)):
if Arg[cell, 0] != 0:
# Solution1:
Solution[cell] = (a / Arg[cell] + c) * numpy.e**(Arg[cell] * d) - (a / Arg[cell])
elif Arg[cell, 0] == 0:
# Solution2:
Solution[cell] = a * d + c

With the result:

Solution = array([[ 6.47773728],
[ 45.18138759],
[ 5. ],
[ 5. ],
[ 7.7548316 ]])

Is there an array operation with which I can avoid using the for-loop?

And to avoid further confusion due to missing information:
is also an array of the same size/shape as

a = numpy.array([[1],[1],[1],[1],[1]])

(And the values are not necessarily

Thanks for your help in advance!

Answer Source

Try this one:

[a,b,c] = [1,2,3]
Arg = numpy.array([[0.12],[0.9],[0],[0],[0.2]])
Solution = Arg
Solution[Solution ==0] = 1
Solution = Solution * a * b * c


[[ 0.72]
 [ 5.4 ]
 [ 6.  ]
 [ 6.  ]
 [ 1.2 ]]

Instead of trying to leave the 0 values in Arg out of the multipliation, just change them to 1, which is neutral in multiplication, and thus has the same effect as avoiding multiplication.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download