Ideal Gas Laws : Coded using Python

Real gases behave in a complex way under different conditions of state variables namely Pressure, Volume and Temperature.  Chemists have first developed laws of has behavior using ideal conditions.  

After the ideal laws, different scientists like Van Der Waal developed equations to incorporate the electrical forces between atoms and also space occupied by atoms.

In this code, we used both these laws to calculate the pressure exerted by Argon gas due to given weight, volume and temperature. 

First part of this code was to pull out unit conversions from scipy library constants to calculate value of gas constant R in different formats. 

After this, we wrote three functions to calculate gas pressure by ideal gas law and using van der Waal equations. The required a,b values of different gases were stored in a dictionary and extracted when needed by using .get function.

We also wrote a function, which tells us whether the calculated pressures are more or less as compared to a given value. 

The code is given below:






from scipy.constants import bar,R, deci, atmosphere,torr, calorie, liter
print(f"1 calorie:{calorie:.4f} Joules")
print(f"1 Torr: {torr:.2f} pascal")
print(f"1 decimeter:{deci:.1f} meter")
print(f"1 Atmosphere: {atmosphere:.1f} pascal")
print(f"1 Bar : {bar:.1f} pascals")
print(f"1 dm3: {liter:.4f} m3")

"""van der waal coeff. to be used
for volume in liter and pressure
in bar"""

vanderwaal_values={
    "nh3":{"a":4.225,"b":0.03713},
    "ar":{"a":1.355,"b":0.03201},
    "br2":{"a":9.75,"b":0.0591},
    "he":{"a":0.0346,"b":0.0238}
}
    




liter_1_m3=1/liter
#print(liter_1_m3)
print()
atm_in_1_pascal=1/atmosphere
print(f"Atmospheres in a pascal:{atm_in_1_pascal:.4e}")
#si unit of R
#joule/degk/mole 

print( )

print("R values in different units")

R_lit_atm_degk_mole=R*liter_1_m3*atm_in_1_pascal
print(f"R(l.atm/degk/mole): {R_lit_atm_degk_mole:.4f}")

bar_in_1_ps=1/bar

R_in_lit_bar_degk_mole=R*liter_1_m3*bar_in_1_ps
print(f"R(l*bar/degk/mole):{R_in_lit_bar_degk_mole:.4f}")
cal_in_joule=1/calorie
r_in_cal_degk_mole=R*cal_in_joule
print(f"R(cal/degk/mole):{r_in_cal_degk_mole:.4f}")

torr_in_1_ps=1/torr
r_in_lit_torr_degk_mole=R*liter_1_m3*torr_in_1_ps
print(f"R(lxtorr/degk/mole):{r_in_lit_torr_degk_mole:.3f}")


def ideal_gas(r_v,mole,vol=None,tk=None, p=None):
    if vol is not None and tk is not None:
        return mole* r_v* tk/vol
    if vol is not None and p is not None:
        return p*vol/(mole*r_v)
    if tk is not None and p is not None:
        return mole* r_v* tk/p
      
def ask_actual_p(asked_p,actual_p):
    if actual_p>asked_p:
         return "Pressure exerted is more than asked"
    else:   
         return "Pressure exerted is less or equal to than asked"
         
        
def pressure(name,R_v,vol,tk,mole):
        a_c=vanderwaal_values.get(name.lower())['a']
        b_c=vanderwaal_values.get(name.lower())['b']
        if a_c and b_c:
           return  ((mole*R_v*tk)/(vol-mole*b_c))-((mole**2*a_c)/vol**2)
 



t_k=lambda c:c+273.15


#prob
print()
print(f"{'Argon Gas Calculations':^35}")
print("First treated as an ideal gas")
gas="Argon"
at_wt=39.948
wt_argon=25 #gm
vol_ar=1.5 #dm3 or lit
temp_c=30
question_pr=2 #bar
R_val=R_in_lit_bar_degk_mole
#print(R_val)
mole_ar=wt_argon/at_wt
print(f"Moles of {gas}: {mole_ar:.4f}")


pr_bar=ideal_gas(R_val,mole_ar,vol_ar, t_k(temp_c),None)
print(f"Pressure: {pr_bar:.2f} Bar")
 

#van der waal gas
symbol="Ar"
prs_vander=pressure(symbol,R_val,vol_ar,t_k(temp_c),mole_ar)
print(f"Pressure as per Van der Waal: {prs_vander:.2f} bars")

act_ps={
    "ideal":pr_bar,
    "vander":prs_vander
}

for p, v_p in act_ps.items():
    msg=ask_actual_p(question_pr,v_p)
    print(f"{p}:{msg}")
    
    

Comments

Popular posts from this blog

Carbonyl Sulphide COS

Applications of Elemental Properties

Dwarf Planets