Python Forum
ValueError: The truth value of an array with more than one element is ambiguous.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: The truth value of an array with more than one element is ambiguous.
#1
I am working on a simple code, for calculating Re number. The problem is that I want to check every number in Re vector, although I do not know how to do it.
The error I am getting is: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The code:
import numpy as np
from CoolProp.CoolProp import PropsSI
from numpy import*

v = [1,2,3,4,5]
Tz = 42 + 273
v = 4e-2 # [m]
s = 2e-2 # [m]
#------------------------------------------------------------------------------------

npv = np.array(v)

A = v*s
ob = 2*(s + v)
Dh = (4*A)/ ob
roZ = PropsSI('D', 'P', 101325, 'T', Tz, 'Air')
din_vis= PropsSI('V', 'P', 101325, 'T', Tz, 'Air')
kin_vis = din_vis/roZ

Re= (npv *Dh )/ kin_vis
Pr = PropsSI('PRANDTL', 'P', 101325, 'T', Tz, 'Air')
print ('Re:',Re)
print ('Pr:',Pr)

if Re < 2300:
    print('Laminar flow, Re = ', Re)
elif Re > 10000 and Pr >= 0.6 and Pr <= 160:
    print('Turbolent flow, Re = ', Re)
    print('Pr = ', Pr)
else:
    print(' Re = ', Re)
    print('Pr = ', Pr)
Any help is much appreciated.
Reply
#2
I am not a profi in hydrodynamics, but ... (hope that helps):

import numpy as np
from CoolProp.CoolProp import PropsSI
# from numpy import* # Remove this line, numpy is already imported as np!
 
v = [1, 2, 3, 4, 5] # be pep8 compliant
Tz = 42 + 273
v = 4e-2 # [m]
s = 2e-2 # [m]
#------------------------------------------------------------------------------------
 
npv = np.array(v, dtype=np.float64)  # don't forget to specify the type of your data; 
 
A = npv * s # pep8, use npv instead of v; npv is a numpy array, that could be multiplied on a constant
# A is an array now

ob = 2 * (s + npv) # don't forget spaces; v -> npv
Dh = (4 * A) / ob # elementwise division of vectors A and ob (hope `ob` doesn't not include zeros)

# Probably, roZ is a scalar value... 
roZ = PropsSI('D', 'P', 101325, 'T', Tz, 'Air') 

# Probably, din_vis is a scalar value... 
din_vis= PropsSI('V', 'P', 101325, 'T', Tz, 'Air')

kin_vis = din_vis / roZ #  kin_vis = a scalar
 
Re = (npv * Dh) / kin_vis # now Re is a numpy array (length(Re) = length(v))


Pr = PropsSI('PRANDTL', 'P', 101325, 'T', Tz, 'Air') # this is (probably) a scalar value

print ('Re:', Re)
print ('Pr:', Pr)
 

# You can use for loop  here:

for r in Re:
    if r < 2300:
        print('Laminar flow, Re = ', r)
    elif r > 10000 and Pr >= 0.6 and Pr <= 160:
        print('Turbolent flow, Re = ', r)
        print('Pr = ', Pr)
    else:
        print('Re = ', r)
        print('Pr = ', Pr)

# or use numpy vectorized notation:

print('Laminar flow, Re = ', Re[Re < 2300])

if Pr >= 0.6 and Pr <= 160:
    print('Turbolent flow, Re = ', Re[Re > 10000])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  RandomForest --ValueError: setting an array element with a sequence JaneTan 0 1,733 Sep-08-2021, 02:12 AM
Last Post: JaneTan
  Data cardinality is ambiguous: x sizes: 51 y sizes: 26 sidra 0 2,323 Oct-03-2020, 11:43 AM
Last Post: sidra
  error : value of a DataFrame is ambiguous Sonata 1 2,252 Apr-24-2020, 05:40 PM
Last Post: anbu23
  ValueError: Found array with 0 samples marcellam 1 5,180 Apr-22-2020, 04:12 PM
Last Post: jefsummers
  I am trying to change the value of an element in a record array ingu 1 2,184 Jan-14-2020, 01:30 PM
Last Post: perfringo
  TensorFlow get error - array with more than one element is ambiguous vokoyo 3 5,558 Nov-07-2019, 01:12 PM
Last Post: ThomasL
  ValueError: The truth value of a Series is ambiguous ? firebird 1 4,325 Aug-01-2019, 12:33 AM
Last Post: scidam
  ValueError: could not broadcast input array from shape (75) into shape (25) route2sabya 0 6,489 Mar-14-2019, 01:14 PM
Last Post: route2sabya
  How to add an element such as an average to a multi-dimensional array? xhughesey 6 4,005 Jan-06-2019, 10:47 PM
Last Post: xhughesey
  Convert element of list to integer(Two dimentional array) zorro_phu 3 4,748 Jun-12-2018, 04:49 AM
Last Post: zorro_phu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020