"""
@author:       Ole Havik Bjoerkedal
@copyright:    Ole Havik Bjoerkedal
@contact:      ole.h.bjorkedal@ntnu.no
@organization: Department of Chemical Engineering, NTNU, Norway
@since:        2018-01-19
@purpose:      Calculating and visualizing equations of state for gases
               Ideal Gas and van der Waal
               Support material for Exercise 3 in TKP4120
@requires:     Python 3
               matplotlib
"""
import numpy as np
import matplotlib.pyplot as plt

R = 8.314    # J/mol K

def IdealGasPressure(V, T=298.0, n=1.0):
    """
    @parameters:
    -------------
    V = Volume [m3]
    T = Temperature [K], default value is 298 K (25 C)
    n = Amount of gas [mol], default value is 1 mol

    @returns:
    -------------
    Ideal pressure [Pa]     
    """
    P = (n*R*T)/V
    return P

def VanDerWaalPressure(V, T=298.0, n=1.0, a=1.0913, b=6.27E-5):
    """
    @parameters:
    -------------
    V = Volume [m3]
    T = Temperature [K], default value is 298 K (25 C)
    n = Amount of gas [mol], default value is 1 mol
    a = van der Waal parameter [J m3 mol-2]
    b = van der Waal parameter [m3 mol-1]
    @returns:
    -------------
    van der Waal pressure [Pa]     
    """
    Vm = V/n
    P = (R*T)/(Vm - b) - (a)/(Vm**2)

    return P

#----------------------------------------------------------------------
#-----------------------------BEREGNINGER------------------------------
#----------------------------------------------------------------------

# Calculations for Ex. b:
#ideal1 = 
#vdW1   = 

# Calculations for Ex. c:
#ideal2 = 
#vdW2   = 

# Importing volume- and pressure -data for propane from text file
volume, realPressure = np.loadtxt('propaneData_2.txt', comments='#', unpack=True)

idealPressure = [IdealGasPressure(V) for V in volume]
vdwPressure = [VanDerWaalPressure(V) for V in volume]

#----------------------------------------------------------------------
#-----------------------------PLOTTING---------------------------------
#----------------------------------------------------------------------


width  = 20.0    # Width of figure [inch].
height = 20.0    # Heigh of figure [inch]. 

# Defining the figure size
plt.figure(figsize=(width,height))

# Plotting three graphs on a logarithmic x-scale
plt.semilogx(volume, idealPressure, color='b', linestyle='-', lw=0.5, label='Ideal Gas')
plt.semilogx(volume, vdwPressure, color='r', linestyle='-', lw=0.5, label='van der Waal Gas')
plt.semilogx(volume, realPressure, color='g',linestyle='None', lw=0.5, marker='x', label='Real Gas')

# Defining axis-labels
plt.xlabel('Volume [m3]', fontsize=11)
plt.ylabel('Pressure [Pa]', fontsize=11)

#plt.xlim([0.0,0.06])

# Specifying location for legend
plt.legend(loc='upper right')

# Saving the figure as a file in the current working directory
plt.savefig('pressure.pdf')

# Display the figure
plt.show()