Showing posts with label equation of state. Show all posts
Showing posts with label equation of state. Show all posts

Equation of state

Energy engineers frequently calculate the equation of state for gases. The Python example below is an example of calculating the value of an unknown variable using the ideal equation of state and the Van der waals equation of state.

Ideal equation of state

      P = 0.082*T/V

Van Der Waals equation of state

      P = (0.082 * T) / (V - b) - a / V^2

      a = (27 * 0.082^2 * Tc^2) / (64 * Pc)
      b = (0.082 * Tc) / (8 * Pc)


Python code of equation of state 


Run Python code below : https://www.mycompiler.io/view/6RK7hWqB76n

import numpy as np

def eos(p, v, t, pc, tc, EOS, PVT):
 
    delta = 99999; 
    delta_result = 99999; 
 
    if EOS == "Ideal":

        if (PVT == 'P'):
            p = 0.082 * t / v
            result = p
        elif PVT == 'V':
            v = 0.082 * t / p
            result = v
        elif PVT == 'T':
            t = p * v / 0.082
            result = t

    elif EOS == "VanderWaals":

        a = 27*(pow(0.082,2)*pow(tc,2))/(64*pc)
        b = 0.082*tc/(8*pc)

        if PVT == 'P':
            p = (0.082*t)/(v-b)-a/pow(v,2)
            result = p
     
        elif PVT == 'V':
            v = 0.082 * t / p
            v_start = v*1-(v*0.5)
            v_end = v*1+(v*0.5)
            v_interval = v*1/2000

            Iteration = np.arange(v_start, v_end, v_interval)

            for v_temp in Iteration:
             
                p1 = p
                p2 = (0.082*t)/(v_temp-b)-a/pow(v_temp,2)
              
                delta = abs(p1 - p2)

                if (delta < delta_result):
                    delta_result = delta
                    v_result = v_temp                
          
            v_result = round(v_result*1000)/1000
            result = v_result

        elif PVT == 'T':
            t = (p*1+a/pow(v,2))*(v-b)/0.082
            result = t
    else:
        pass

    return result

P = 10          # Pressure (P, atm) = 10
V = 2.403       # Volume/mole (V, L/gmol) = 2.403
T = 298.15      # Temperature (T, K) = 298.15
Pc = 45.80      # Critical Pressure (Pc, atm) = 45.80
Tc = 190.7      # Critical Temperature (Tc, K) = 190.7

print(eos(P, V, T, Pc, Tc, 'Ideal', 'P'))
print(eos(P, V, T, Pc, Tc, 'Ideal', 'V'))
print(eos(P, V, T, Pc, Tc, 'Ideal', 'T'))

print(eos(P, V, T, Pc, Tc, 'VanderWaals', 'P'))
print(eos(P, V, T, Pc, Tc, 'VanderWaals', 'V'))
print(eos(P, V, T, Pc, Tc, 'VanderWaals', 'T'))

When run the code, you will receive the following results.

10.174
2.445
293.049
9.968
2.395
299.072