Mathematical analysis of the effect of a and b parameters¶
Code: #119-000
File: apps/van_der_waals/parameters_analysis.ipynb
The aim of this notebook is to visualize the effect of a and b parameters on van der Waals’ isotherms.
Interface¶
The main interface (main_block_119_000
) is divided in two HBox: top_block_119_000
and bottom_block_119_000
. top_block_119_000
contains of 2 bqplot Figures: fig_119_001
and fig_119_002
.
[1]:
from IPython.display import Image
Image(filename='../../static/images/apps/119-000_1.png')
[1]:
The sliders a_slider
and b_slider
update the values of \(a\) and \(b\) which updates the isotherms of fig_119_001
and fig_119_002
.
[2]:
Image(filename='../../static/images/apps/119-000_2.png')
[2]:
CSS¶
A custom css
file is used to improve the interface of this application. It can be found here.
[1]:
from IPython.display import HTML
display(HTML("<head><link rel='stylesheet' type='text/css' href='./../../static/custom.css'></head>"))
display(HTML("<style>.container { width:100% !important; }</style>"))
Packages¶
[2]:
from bqplot import *
import bqplot as bq
import bqplot.marks as bqm
import bqplot.scales as bqs
import bqplot.axes as bqa
import ipywidgets as widgets
import urllib.parse
import webbrowser
import sys
Physical functions¶
This are the functions that have a physical meaning:
get_absolute_isotherms
calculate_critic
bar_to_atm
[3]:
def get_absolute_isotherms(a, b, v_values, T_values):
"""This function calculates the theoretical p(v, T) plane
(in absolute coordinates) according to van der Waals
equation of state from a given range of volumes
and tenperatures.
Args:
a: Term related with the attraction between particles in
L^2 bar/mol^2.\n
b: Term related with the volume that is occupied by one
mole of the molecules in L/mol.\n
v_values: An array containing the values of v
for which the isotherms must be calculated.\n
T_values: An array containing the values of T for which
the isotherms must be calculated.\n
Returns:
isotherms: A list consisted of numpy arrays containing the
pressures of each isotherm.
"""
isotherms = []
R = 0.082 * 1.01325 #bar L mol^-1 K^-1
for T in T_values:
isot = []
for v in v_values:
p = R*T/(v - b) - (a/v**2)
isot = np.append(isot, p)
isotherms.append(isot)
return isotherms
[4]:
def calculate_critic(a, b):
"""
This function calculates the critic point
(p_c, v_c, T_c) from given a and b parameters of
the Van der Waals equation of state for real gases.
:math:`(P + a \\frac{n^2}{V^2})(V - nb) = nRT`
:math:`p_c = \\frac{a}{27 b^2}`
:math:`v_c = 3b`
:math:`T_c = \\frac{8a}{27 b R}`
Args:
a: Term related with the attraction between particles in
L^2 bar/mol^2.\n
b: Term related with the volume that is occupied by one
mole of the molecules in L/mol.\n
Returns:
p_c: Critical pressure in bar.\n
v_c: Critical volume in L/mol.\n
T_c: Critical tenperature in K.\n
"""
if b == 0.0:
return None
k_B = 1.3806488e-23 #m^2 kg s^-2 K^-1
N_A = 6.02214129e23
R = 0.082 * 1.01325 #bar L mol^-1 K^-1
p_c = a/27.0/(b**2)
v_c = 3.0*b
T_c = 8.0*a/27.0/b/R
return p_c, v_c, T_c
[5]:
def bar_to_atm(p_values):
"""This function changes the pressures of an array
form bars to atm.
Args:
p_values: List consisted of pressures in bars.\n
Returns:
p_values: List consisted of pressures in atm.\n
"""
p_values = np.array(p_values) * 0.9869
return p_values
Main interface¶
[ ]:
#In this program we are going to use water's parameters
a = 5.536 #L^2 bar mol^-2
b = 0.03049 #L mol^-1
colors = ['#0079c4','#f09205','#21c400', '#850082']
p_c, v_c, T_c = calculate_critic(a, b)
p_c = p_c * 0.9869 #unit change from bar to atm
v_values = np.linspace(-5, 5, 3000) #L/mol
T_values = [0.9*T_c, 1.0*T_c, 1.1*T_c]
p_values = get_absolute_isotherms(a, b, v_values, T_values)
p_values = bar_to_atm(p_values)
#####################
######TOP BLOCK######
#####################
top_block_119_000 = widgets.VBox(
[],
layout=widgets.Layout(
align_items='center',
)
)
scale_x_119_001 = bqs.LinearScale(min = -0.5, max = 0.5)
scale_y_119_001 = bqs.LinearScale(min = -300, max = 300)
axis_x_119_001 = bqa.Axis(
scale=scale_x_119_001,
tick_format='.1f',
tick_style={'font-size': '15px'},
num_ticks=5,
grid_lines = 'none',
grid_color = '#8e8e8e',
label='v (L/mol)',
label_location='middle',
label_style={'stroke': 'black', 'default-size': 35},
label_offset='50px'
)
axis_y_119_001 = bqa.Axis(
scale=scale_y_119_001,
tick_format='.0f',
tick_style={'font-size': '15px'},
num_ticks=4,
grid_lines = 'none',
grid_color = '#8e8e8e',
orientation='vertical',
label='p (atm)',
label_location='middle',
label_style={'stroke': 'red', 'default_size': 35},
label_offset='50px'
)
fig_119_001 = Figure(
title='',
marks=[],
axes=[axis_x_119_001, axis_y_119_001],
animation_duration=0,
legend_location='top-right',
background_style= {'fill': 'white', 'stroke': 'black'},
min_aspect_ratio=1.0,
fig_margin=dict(top=10, bottom=60, left=80, right=30),
toolbar = True,
layout=widgets.Layout(
height='350px',
)
)
marks = [
bqm.Lines(
x = [v_values for elem in p_values],
y = p_values,
scales = {'x': scale_x_119_001, 'y': scale_y_119_001},
opacities = [1.0],
visible = True,
colors = colors,
)
]
fig_119_001.marks = marks
tb_119_001 = Toolbar(figure=fig_119_001, layout=widgets.Layout(align_self='center'))
fig_119_002 = Figure(
title='',
marks=[],
axes=[axis_x_119_001, axis_y_119_001],
animation_duration=0,
legend_location='top-right',
background_style= {'fill': 'white', 'stroke': 'black'},
min_aspect_ratio=1.0,
fig_margin=dict(top=10, bottom=60, left=80, right=30),
toolbar = True,
layout=widgets.Layout(
height='350px',
)
)
fig_119_002.marks = marks
change_view_button = widgets.ToggleButton(
value=False,
description='Presentation mode (OFF)',
disabled=False,
button_style='',
tooltip='',
icon='desktop',
layout=widgets.Layout(
width='initial',
align_self='center'
)
)
change_view_button.observe(change_view, 'value')
prepare_export_fig_119_001_button = widgets.Button(
description='Export',
disabled=False,
button_style='',
tooltip='',
layout=widgets.Layout(
align_self='center'
)
)
prepare_export_fig_119_001_button.on_click(prepare_export)
prepare_export_fig_119_002_button = widgets.Button(
description='Export',
disabled=False,
button_style='',
tooltip='',
layout=widgets.Layout(
align_self='center'
)
)
prepare_export_fig_119_002_button.on_click(prepare_export)
top_block_119_000.children = [
change_view_button,
tb_119_001,
widgets.HBox([
widgets.VBox([
prepare_export_fig_119_001_button,
fig_119_001
]),
widgets.VBox([
prepare_export_fig_119_002_button,
fig_119_002
])
])
]
#####################
######BOTTOM BLOCK###
#####################
bottom_block_119_000 = widgets.VBox(
[],
layout=widgets.Layout(
align_items='center',
width='100%',
margin='30px 0 0 0'
)
)
a_slider = widgets.FloatSlider(
min=0,
max=34.0,
step=0.001,
value=a,
description='a',
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
layout=widgets.Layout(width='90%'),
)
a_slider.observe(update_isotherms, 'value')
b_slider = widgets.FloatSlider(
min=0,
max=0.1735,
step=0.0001,
value=b,
description='b',
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
layout=widgets.Layout(width='90%'),
)
b_slider.observe(update_isotherms, 'value')
bottom_block_119_000.children = [
widgets.HBox([
a_slider,
widgets.HTMLMath(
value=r"\( \frac{L^2 bar}{mol^2} \)",
layout=widgets.Layout(height='60px')
)],
layout=widgets.Layout(
width='50%',
height='100%'
)
),
widgets.HBox([
b_slider,
widgets.HTMLMath(
value=r"\( \frac{L}{mol} \)",
layout=widgets.Layout(height='60px')
)],
layout=widgets.Layout(
width='50%',
height='100%'
)
)
]
#####################
####MAIN BLOCK#####
#####################
main_block_119_000 = widgets.VBox(
[],
layout=widgets.Layout(align_items='center')
)
main_block_119_000.children = [
top_block_119_000,
bottom_block_119_000
]
figures = [
fig_119_001,
fig_119_002
]
main_block_119_000