1

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, the cell-temperatures (in degree Celsius) are stored in vector (in the future in a matrix) T_Cell. 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 TC correspond to their row-index, so that accessing the thermal conductivity for example for a cell temperature of T_Cell=5 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 T_Cell? For example with the arrays of TC and T_Cell as shown above:

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

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 Arg-vector (in the future: 3d-array) and to check if a cell is 0. Like:

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?
And to avoid further confusion due to missing information: a is also an array of the same size/shape as Arg:

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

(And the values are not necessarily 1!)

Thanks for your help in advance!

4
  • 2
    Boolean masking is the usual way of applying a calculation conditionally. A[mask]=x1, A[~mask]=x2, or sometimes np.where(condition, x1, x2). Commented May 30, 2016 at 18:02
  • To make this question more inviting, you need to add a working example, something we can copy-n-paste and play with. Trying get all the details right from a word description is too much work. Commented May 30, 2016 at 20:52
  • sum(Arg==0) == 0 gives you a True if there's no 0s. Or, if you want to replace all the 0-values by ones: Arg[Arg==0]=1 Commented May 31, 2016 at 12:07
  • Thanks for your replies! @Swier: Yeah, but that doesn't help me with solving my equation. If there is a 0 in Arg, I still have to use Solution1 for the rest of the values in Arg and Solution2 for the 0s. @hpaulj: I'm going to try this right now! Commented May 31, 2016 at 12:11

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer! Unluckily the actual equation to solve is a little bit more complicated. I added a simplified minimalistic equation to my question which shows the basic structure. Sorry for having done that before!
@Scotty1- You can first make an array results = numpy.zeros(len(Arg)) and then fill it in two steps: results[Arg==0] = Equation1(Arg[Arg==0]) and then results[Arg!=0] = Equation2(Arg[Arg!=0])
Thanks alot Swier, that did it! I just tried it with hpaulj's solution with np.where before and that works as well... Just two more questions: If I switch to 2d-arrays (or 3d), which solution will work better and which one is faster?
I've been using boolean masking in matlab before but I didn't manage to do this in python... Looks like I still have to get used to python... :-/ Is there a way to also upvote hpaulj's answer?
numpy.where and mask arrays should be roughly equally fast. Though if your datasets become very large you might want to try looking into scipy and/or pandas and maybe pre-compile your script, but that's a whole other question. And to the top left of his comment should be a small arrow with which you can upvote.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.