The goal with the following code is to plug the function "ddW" into odeint to find W for a given X (later on). I'm using the print function to make sure functions run).
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint
import pint
u = pint.UnitRegistry()
R = 8.31446261815324 * u.J/(u.mol*u.K)
Rgas = R.to (u.atm*u.L/(u.mol*u.K))
P0 = 10 * u.atm
T = 400 * u.K
v0 = 2 * u.L/u.min
α = 0.02 * u.kg**-1
k = 1.4 * u.L**2/(u.mol*u.kg*u.min)
FA0 = 0.5*P0*v0/(Rgas*T) #Assuming I.G.
def ddW(param,w): #param: [X,P]
X = param[0]
P = u.Quantity(param[1]).magnitude * u.atm
W = u.Quantity(w).magnitude * u.kg
d = np.zeros(2)
d[0] = k*FA0/(v0**2*P0) * P*W*(1-X) #dX/dW
d[1] = -α/2 *P0**2 * (1-X)/P #dP/dW
return d
param0 = [0,P0]
Wrange = np.linspace(0,100) *u.kg
#PBR = odeint(ddW,param0,Wrange)
#plt.plot(Wrange,PBR)
print(ddW(param0,0))
Traceback (most recent call last):
File "<ipython-input-1-83022bc3b5da>", line 1, in <module>
runfile(REDACTED, wdir=REDACTED)
File "C:\Users\Spencer\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\Spencer\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "REDACTED", line 43, in <module>
print(ddW(param0,0))
File "REDACTED", line 33, in ddW
d[1] = -α/2 *P0**2 * (1-X)/P #dP/dW
ValueError: setting an array element with a sequence.
I previously had the same error message for what is now line 32 (defining d[0]) until I defined W in line 29. What is bothering me is that none of the inputs for d[1] (as far as I can tell) have size >1, so it should fit.
Assuming your fix for line 32 (defining d[0]) was to replace w
with W
where you took the magnitude of w
, you need to do the same for the quantity you are trying to put into d[1].
The right side of =
for line 32 is a dimensionless Quantity
object, whereas the right side of =
for line 33 is a Quantity
object with dimension standard_atmosphere / kilogram
which can't be put into array d
as is because it needs to be magnitude only so it can be converted to type float.
Try this if you want to keep your array d
as type float
:
val = -a/2 *P0**2 * (1-X)/P #dP/dW
d[1] = val.magnitude