How to use np.log or np.exp in GEKKO intermediate

Viewed 183

I am using gekko to solve a system of equations. As an intermediate step I am using an intermediate that plugs the MV temperature into the following function:

def riedelVP(T, const):
    '''Returns Vapor Pressure
    INPUTS
    :T - Temperature (K)
    :const - A, B, C, D, E constants for eqn
    OUTPUTS
    :Y - Pressure in Pascals'''
    # unpack constants
    a, b, c, d, e = const
    
    # plug into equation
    Y = np.exp(a+b/T+c*np.log(T) + d*T**e)
    return Y

When I do this, I get the following Error:

I have tried using T.value and T.value[0] as arguments into the function instead of T.
TypeError: loop of ufunc does not support argument 0 of type GKVariable which has no callable log method
How can I use a function with exp and log in a gekko intermediate

1 Answers

The problem in this case is that both the np.exp() function and the np.log function require an argument that is a float, while gekko variables are their own type. If you were to use gekko's methods for exponentiation and taking the logarithm in the function, it will work. To do so, use the m.exp or m.log methods. In this example, I already had a gekko model created using m = GEKKO(), then I could create the function:

def riedelVP(T, const):
    '''Returns Vapor Pressure
    INPUTS
    :T - Temperature (K)
    :const - A, B, C, D, E constants for eqn
    OUTPUTS
    :Y - Pressure in Pascals'''
    # unpack constants
    a, b, c, d, e = const
    
    # plug into equation
    Y = m.exp(a+b/T+c*m.log(T) + d*T**e)
    return Y

Use gekko's analogous methods when type errors in functions occur.

Related