My variable C0 is defined in pint.UnitRegistry units mol/L. I need to use it in a function, but in order for Python to not call me on inconsistent units for the general function, I have to define a new variable in the function with units, as follows.
import pint
u = pint.UnitRegistry()
C0 = [10**(-3),10**(-6),0] *u.mol/u.L
def r(c,t):
C = c * u.mol/u.L #Python expects c to be dimensionless
return ν * k[3]*C0[1]*C[0] / ((k[2]+k[3])/k[1] + C[0]) #k and ν are arrays for the problem I'm working on.
I want to add an if statement so that I don't end up with C in r(C0,t) being in units of mol^2/L^2. What I have is
def r(c,t):
if c.dimensionless == True:
C = c * u.mol/u.L
else:
C = c
return ν * k[3]*C0[1]*C[0] / ((k[2]+k[3])/k[1] + C[0])
But when I run this with C0, it tells me that C0 has no attribute called dimensionless. How should I edit my if statement so it will work to check any kind of input for units?
Traceback:
File [Redacted], line 32, in <module>
Ct = odeint(r,C0,t)
File "C:\Users\Spencer\Anaconda3\lib\site-packages\scipy\integrate\odepack.py", line 233, in odeint
int(bool(tfirst)))
File [Redacted], line 24, in r
if c.dimensionless == True:
AttributeError: 'numpy.ndarray' object has no attribute 'dimensionless'```
Aucun commentaire:
Enregistrer un commentaire