0

Here in my code, I need to use quad to compute J-ij and Jp_ij as following:

import numpy as np
from scipy.integrate import quad

xData = {'xCa': np.array([0.08177462, 0.07454374, 0.06799069, 0.0612067 , 0.05375614,
   0.04635545, 0.03826685, 0.03011176, 0.02174885, 0.01294261]), \
     'xH': np.array([0., 0.00880058, 0.01676501, 0.02492618, 0.0339801 ,
   0.04296084, 0.05264879, 0.06251044, 0.07260724, 0.08314678]), \
     'xCl': np.array([0.08177462, 0.08334432, 0.08475571, 0.08613288, 0.08773624,
   0.08931629, 0.09091563, 0.0926222 , 0.09435609, 0.09608939])}

Ax = 2.921
cations = {'Ca' : 2, 'H' : 1}
anions = {'Cl' : 1}

ionicDic = dict()
for dd in (cations, anions):
    ionicDic.update(dd)  
ionic_dic = {f'x{i}': ionicDic[i] for i in ionicDic}
arr1 = np.array(list(xData.values()))
arr2 = np.array([ionic_dic[k] for k in xData])
ionicArray = (np.sum(np.transpose(arr1) * arr2 ** 2, axis=1) / 2) ** 0.5

def xij(a, b):
    x_ij = 6 * a * b * Ax * ionicArray
    return x_ij

def J_ij(c):
    int_fun = lambda t : (np.log(t) / (c * t / (np.log(t)))) * \
    (1 - np.exp(c * t / (np.log(t))))
    integral_part = quad(int_fun, 0, 1, limlst=1000)[0]
    Jij = (c / 4.0) - 1 + integral_part
    return Jij

def Jp_ij(d):
    int_fun_p = lambda t: (t / (d * t / (np.log(t))) ** 2) * \
            (1 - (1 - (d * t / (np.log(t)))) * np.exp(d * t np.log(t))))
    integral_part_p = quad(int_fun_p, 0, 1, limlst=1000)[0]
    Jpij = (1 / 4.0) + 1 + integral_part_p
    return Jpij

AB = ['J_Ca_H', 'P', 'JP_Ca_H']
for k in AB:
    if (k.split('_'))[0] == 'J':
        i = (k.split('_'))[1]
        j = (k.split('_'))[2]
        n = list(xij(ionicDic[i], ionicDic[j]))
        m = []
        for num in n:
            m.append(J_ij(num))
        xData.update({f'J_{i}_{j}': m})
        print(xData)

    elif (k.split('_'))[0] == 'JP':
        ip = (k.split('_'))[1]
        jp = (k.split('_'))[2]
        np = list(xij(ionicDic[ip], ionicDic[jp]))
        m = []
        for ii in np:
        
            m.append(Jp_ij(ii))
        xData.update({f'JP_{ip}_{jp}': mp})

    else:
        print(f'No higher order term for the {k} item to calculate!!')
        continue

J_ij works well while the Jp_ij gives error:

>>> retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit
>>> return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
>>> int_fun_p = lambda t: (t / (d * t / (np.log(t))) ** 2) * (1 - (1 - (d * t / (np.log(t)))) * np.exp(d * t / (np.log(t))))
AttributeError: 'list' object has no attribute 'log'

I got totally mixed up because if I pass (Jp_ij(15.84)),as the first value that must be passed in it, it works but in for loop it raise an error. What should I do to fix error??

Here My problem was being careless with the Numpy name (np) as name of a list in for loop. Something kind of like Why does code like `str = str(...)` cause a TypeError, but only the second time? where str is passed as a name to a variable.

0 Answers0