Search code examples
pythonscipytypeerrorcomplex-numbers

Problem running Scipy modified Bessel function k0


I am trying to run a function that contains modified Bessel functions k0 and k1 that I imported from scipy.special. This is what I am trying to run :

freqM2 = 1.932274 /86400 #cps
rw = 10*1e-3 
rc = rw #[m]
omegaM2 = 2*np.pi*freqM2

leakage = np.arange(1e-11,1e-3, 1e-6)
BKu = 40*1000000000

eo = 2.5*1e-7 * 0.045

def leaky_model(leakage, S, T, omega, BKu, rc, rw, eo, rho=1000, g=9.81):
    
    # specific leakage = Kprime/bprime
    i= 1j
    beta = ((leakage/T) + i*omega*S/T )**(1/2)
    
    upsilon = 1 + (rc/rw)**2 * (i*omega*rw / (2*T*beta)) * k0(beta*rw)/k1(beta*rw)
    
    hwo = i*omega*S/((i*omega*S + leakage)*upsilon) * (BKu*eo / rho*g)
    
    A = np.abs(hwo/((BKu*eo / rho*g)))
    eta = phase(hwo/(BKu*eo / rho*g))
    
    return A, eta


A1, eta1 = leaky_model(leakage, S=1e-2, T =1e-4, omega=omegaM2, BKu=BKu, rc=rc, rw=rw, eo=eo, rho=1000, g=9.81)

However, when I try to run the code, I get the following error :

TypeError                                 Traceback (most recent call last)
<ipython-input-26-79ffbddffb54> in <module>
----> 1 A1, eta1 = leaky_model(leakage, S=1e-2, T =1e-4, omega=omegaM2, BKu=BKu, rc=rc, rw=rw, eo=eo, rho=1000, g=9.81)

<ipython-input-25-ccd69a11a928> in leaky_model(leakage, S, T, omega, BKu, rc, rw, eo, rho, g)
      5     beta = ((leakage/T) + i*omega*S/T )**(1/2)
      6 
----> 7     upsilon = 1 + (rc/rw)**2 * (i*omega*rw / (2*T*beta)) * k0(beta*rw)/k1(beta*rw)
      8 
      9     hwo = i*omega*S/((i*omega*S + leakage)*upsilon) * (BKu*eo / rho*g)

TypeError: ufunc 'k0' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

I understand that the problem is the use of a complex number in the modified Bessel function, but how can I do this then ? If that is relevant, I am using Jupyter Notebook with the last update of Anaconda.


Solution

  • The function scipy.special.kv(v, z) accepts complex z. So instead of k0(z) and k1(z), use kv(0, z) and kv(1, z).