'How can I eliminate the error 'float' object is not subscriptable?
I am trying to use ρ_mix object inside the function def odes(x, V) but this message is appeared - 'float' object is not subscriptable. ρ_mix was created by the first value of an array of different sized-objects. The size of ρ_mix is (50,). The full error message is:
Traceback (most recent call last):
File ~\OneDrive - uowm.gr\Desktop\draft\untitled0.py:92 in <module> sol = odeint(odes, x0, V)
File ~\anaconda3\lib\site-packages\scipy\integrate\odepack.py:241 in odeint output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
File ~\OneDrive - uowm.gr\Desktop\draft\untitled0.py:74 in odes d_mix = np.array([i[0] for i in gas_new(T, 101325)])
File ~\OneDrive - uowm.gr\Desktop\draft\untitled0.py:74 in <listcomp> d_mix = np.array([i[0] for i in gas_new(T, 101325)])
TypeError: 'float' object is not subscriptable
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import cantera as ct
T = np.linspace(873.15, 1023.15, 50)
def gas(T, P): # Kelvin, Pascal
gas = ct.Solution('gri30.xml','gri30_mix')
molar_ratio = {'CH4': 0.3, 'CO2':0.2, 'H2O':0.0, 'H2':0.0, 'CO':0.0, 'Ar': 0.5}
gas.TPX = T, P, molar_ratio
feed = gas['CH4', 'CO2', 'H2O', 'H2', 'CO', 'Ar']
ρ_mix = feed.density # kg/m3
cp_mix_mass = feed.cp_mass # J/Kg/K
Dm_mole = feed.mix_diff_coeffs_mole # m2/s mixture-averaged diffusion coefficients
λg = feed.thermal_conductivity # J/s/m/K average gas thermal conductivity
μ = feed.viscosity # Pa.s
Ci = feed.concentrations*1e03 # mol/m3
return np.array([ρ_mix, cp_mix_mass, Dm_mole, λg, μ, Ci], dtype=object)
gas_new = np.vectorize(gas)
ρ_mix = [i[0] for i in gas_new(T, 101325)]
def odes (x, V):
# assign each ODE (ordinary differential equation) to a vector element
# states (2): molar rate mol/L for 3 components (1*3) | temperature (K) (1)
Fa = x[0]
Fb = x[1]
Fc = x[2]
T = x[3]
# equations used
k1a, k2a = k(T)[0], k(T)[1]
ρ_mix = [i[0] for i in gas_new(T, 101325)]
Ft = Fa + Fb + Fc
Ca = Cto*(Fa/Ft)*(To/T)*(P/Po)
Cb = Cto*(Fb/Ft)*(To/T)*(P/Po)
Cc = Cto*(Fc/Ft)*(To/T)*(P/Po)
r1a = -k1a*Ca*ρ_mix
r2a = -k2a*Ca**2*ρ_mix
# define each ODE
dFadV = r1a + r2a
dFbdV = -r1a
dFcdV = -r2a/2
dTdV = (Ua*(Ta-T)+(-r1a)*(-dH_r1A)+(-r2a)*(-dH_r2A))/(Cpa*Fa+Cpb*Cpb+Cpc*Fc)
return np.array([dFadV, dFbdV, dFcdV, dTdV], dtype='float64')
x0 = np.array([100, 0.1, 0.1, 450], dtype='float64')
V = np.linspace(0, 1, 10000)
sol = odeint(odes, x0, V)
Fa, Fb, Fc, T = sol.T
Solution 1:[1]
T = x[3]
# equations used
k1a, k2a = k(T)[0], k(T)[1]
?_mix = [i[0] for i in gas_new(T, 101325)]
Is this T here an array just like this T = np.linspace(873.15, 1023.15, 50)? Because as far as I can see x[3] would be simply be 450, not a linspace so there is nothing to be vectorised with gas_new
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | matszwecja |
