Scotty1- - 7 months ago 24

Python Question

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

`TC`

`T_Cell`

`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

`TC`

`T_Cell=5`

`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

`T_Cell`

`TC`

`T_Cell`

`TC_Cell = TC[T_Cell, 1]`

So that the result for TC_Cell looks like:

`TC_Cell = array([[620],`

[606],

[606],

[598],

[598],

[590],

[574],

[569],

[569]])

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).*

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

`Arg`

`0`

`a=1`

c=2

d=3

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?

`a`

`Arg`

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

(And the values are not necessarily

`1`

Thanks for your help in advance!

Answer

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
print(Solution)
```

returns:

```
[[ 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.