Effect of a and b in van der Waals isotherms (reduced variables)¶
Code: #11C-000
File: apps/van_der_waals/effect_of_a_and_b_reduces.ipynb
The aim of this notebook is to visualize the effect of a and b parameters on van der Waals’ isotherms (reduced variables).
Interface¶
The main interface (main_block_11C_000
) is divided in two HBox: top_block_11C_000
and bottom_block_11C_000
. top_block_11C_000
contains of 5 bqplot Figures: fig_11C_001
, fig_11C_002
, fig_11C_003
, fig_11C_004
and fig_11C_005
.
[1]:
from IPython.display import Image
Image(filename='../../static/images/apps/11C-000_1.png')
[1]:
CSS¶
A custom css
file is used to improve the interface of this application. It can be found here.
[2]:
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>"))
display(HTML("<style>.widget-label { display: contents !important; }</style>"))
display(HTML("<style>.slider-container { margin: 12px !important; }</style>"))
Packages¶
[3]:
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_relative_isotherms_params
[4]:
def get_relative_isotherms_params(alpha, beta, v_range, T_range):
"""This function calculates the theoretical p(v, T) plane
(in reduced coordinates) according to van der Waals
equation of state from a given range of volumes
and tenperatures and taking into account the values
of alpha and beta.
Args:
alpha: The value of the a parameter.\n
alpha: The value of the b parameter.\n
v_range: An array containing the values of v
(in reduced coordinates)for which the isotherms must be
calculated.\n
T_range: An array containing the values of T
(in reduced coordinates)for which the isotherms must be
calculated.\n
Returns:
isotherms: A list consisted of numpy arrays containing the
pressures of each isotherm.
"""
isotherms = []
for T in T_range:
p_R = []
for v in v_range:
val = (8.0/3.0*T/(v - beta) - alpha/v**2)
p_R = np.append(p_R, val)
isotherms.append(p_R)
return isotherms
Main interface¶
[ ]:
alpha_initial = 3.0 #0.0 < alpha < 3.0
beta_initial = 0.33 #0.0 < beta < 0.33
a, b = 5.536, 0.03049 #L^2 bar/mol^2, L/mol
T_values = [0.95, 1.0, 1.2]
v_values = np.linspace(0.4, 5.0, 500)
colors = ['#0079c4','#f09205','#21c400']
p_values = get_relative_isotherms_params(
alpha_initial,
beta_initial,
v_values,
T_values
)
#######################################
#######CREATE THE FIGURES##############
#######################################
fig_11C_001 = bq.Figure(
title='p vs v (fixed T, reduced variables)',
marks=[],
axes=[],
animation_duration=0,
legend_location='top-right',
background_style= {'fill': 'white', 'stroke': 'black'},
fig_margin=dict(top=70, bottom=60, left=80, right=30),
toolbar = True,
layout = widgets.Layout(
width='100%',
height='500px'
)
)
fig_11C_002 = bq.Figure(
title='',
marks=[],
axes=[],
animation_duration=0,
legend_location='top-right',
background_style= {'fill': 'white', 'stroke': 'black'},
fig_margin=dict(top=30, bottom=60, left=25, right=10),
toolbar = True,
layout = widgets.Layout(
width='90%',
height='40%'
)
)
fig_11C_003 = bq.Figure(
title='',
marks=[],
axes=[],
animation_duration=0,
legend_location='top-right',
background_style= {'fill': 'white', 'stroke': 'black'},
fig_margin=dict(top=10, bottom=60, left=25, right=10),
toolbar = True,
layout = widgets.Layout(
width='90%',
height='40%'
)
)
fig_11C_004 = bq.Figure(
title='',
marks=[],
axes=[],
animation_duration=0,
legend_location='top-right',
background_style= {'fill': 'white', 'stroke': 'black'},
fig_margin=dict(top=10, bottom=60, left=25, right=10),
toolbar = True,
layout = widgets.Layout(
width='90%',
height='40%'
)
)
fig_11C_005 = bq.Figure(
title='p vs v (fixed T, reduced variables)',
marks=[],
axes=[],
animation_duration=0,
legend_location='top-right',
background_style= {'fill': 'white', 'stroke': 'black'},
fig_margin=dict(top=70, bottom=60, left=80, right=30),
toolbar = True,
layout = widgets.Layout(
width='100%',
height='500px'
)
)
scale_x = bqs.LinearScale(min = 0.4, max = 5.0)
scale_y = bqs.LinearScale(min = 0, max = 2.0)
axis_x = bqa.Axis(
scale=scale_x,
tick_format='.2f',
tick_style={'font-size': '15px'},
tick_values = [0.5, 2.5, 5.0],
grid_lines = 'none',
grid_color = '#8e8e8e',
label='v',
label_location='middle',
label_style={'stroke': 'black', 'default-size': 35},
label_offset='50px'
)
axis_y = bqa.Axis(
scale=scale_y,
tick_format='.1f',
tick_style={'font-size': '15px'},
tick_values = [0, 1, 2],
grid_lines = 'none',
grid_color = '#8e8e8e',
orientation='vertical',
label='p',
label_location='middle',
label_style={'stroke': 'red', 'default_size': 35},
label_offset='50px'
)
axis_x_no_ticks = bqa.Axis(
scale=scale_x,
tick_format='.2f',
tick_style={'font-size': '15px'},
num_ticks=0,
grid_lines = 'none',
grid_color = '#8e8e8e',
label='v',
label_location='middle',
label_style={'stroke': 'black', 'default-size': 35},
label_offset='15px'
)
axis_y_no_ticks = bqa.Axis(
scale=scale_y,
tick_format='.0f',
tick_style={'font-size': '15px'},
num_ticks=0,
grid_lines = 'none',
grid_color = '#8e8e8e',
orientation='vertical',
label='p',
label_location='middle',
label_style={'stroke': 'red', 'default_size': 35},
label_offset='15px'
)
fig_11C_001.axes = [axis_x, axis_y]
fig_11C_002.axes = [axis_x_no_ticks, axis_y_no_ticks]
fig_11C_003.axes = [axis_x_no_ticks, axis_y_no_ticks]
fig_11C_004.axes = [axis_x_no_ticks, axis_y_no_ticks]
fig_11C_005.axes = [axis_x, axis_y]
#######################################
##############MARKS####################
#######################################
x_values = [v_values for i in range(len(p_values))]
y_values = []
color_values = []
label_values = []
for i in range(len(p_values)):
y_values.append(p_values[i])
color_values.append(colors[i])
label_values.append(str(T_values[i]))
new_state = bqm.Lines(
x = x_values,
y = y_values,
scales = {'x': scale_x, 'y': scale_y},
opacities = [1.0 for elem in p_values],
visible = True,
colors = color_values,
labels = label_values,
)
old_state = bqm.Lines(
x = x_values,
y = y_values,
scales = {'x': scale_x, 'y': scale_y},
opacities = [1.0 for elem in p_values],
visible = True,
colors = color_values,
labels = label_values,
)
current_state = bqm.Lines(
x = x_values[0],
y = y_values[0],
scales = {'x': scale_x, 'y': scale_y},
opacities = [1.0 for elem in p_values],
visible = True,
colors = color_values,
labels = label_values,
)
alpha_line = bqm.Lines(
x = x_values[0],
y = y_values[0],
scales = {'x': scale_x, 'y': scale_y},
opacities = [1.0 for elem in p_values],
visible = True,
colors = color_values,
labels = label_values,
)
beta_line = bqm.Lines(
x = x_values[0],
y = y_values[0],
scales = {'x': scale_x, 'y': scale_y},
opacities = [1.0 for elem in p_values],
visible = True,
colors = color_values,
labels = label_values,
)
ideal_isotherms = get_relative_isotherms_params(
0,
0,
v_values,
T_values
)
ideal_line = bqm.Lines(
x = x_values,
y = ideal_isotherms,
scales = {'x': scale_x, 'y': scale_y},
opacities = [1.0 for elem in p_values],
visible = True,
colors = color_values,
labels = label_values,
)
unique_isotherm = bqm.Lines(
x = x_values[0],
y = y_values[0],
scales = {'x': scale_x, 'y': scale_y},
opacities = [0.6],
visible = True,
colors = ['#c90000'],
labels = [label_values[0]],
stroke_width = 5
)
fig_11C_001.marks = [old_state]
fig_11C_002.marks = [current_state]
fig_11C_003.marks = [alpha_line]
fig_11C_004.marks = [beta_line]
fig_11C_005.marks = [ideal_line, unique_isotherm]
alpha_slider_11C_003 = widgets.FloatSlider(
min=0.0,
max=5.0,
step=0.01,
value=alpha_initial,
description=r"\( \alpha \)",
disabled=False,
continuous_update=True,
orientation='horizontal',
readout=True,
layout=widgets.Layout(width='90%')
)
alpha_slider_11C_003.observe(update_isotherms, 'value')
beta_slider_11C_004 = widgets.FloatSlider(
min=0.0,
max=0.66,
step=0.001,
value=beta_initial,
description=r"\( \beta \)",
disabled=False,
continuous_update=True,
orientation='horizontal',
readout=True,
layout=widgets.Layout(width='90%')
)
beta_slider_11C_004.observe(update_isotherms, 'value')
return_button = widgets.Button(
description='Reset',
disabled=False,
button_style='',
tooltip='Return to the original state',
)
return_button.on_click(restart)
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_11C_001_button = widgets.Button(
description='Export',
disabled=False,
button_style='',
tooltip='',
)
prepare_export_fig_11C_001_button.on_click(prepare_export)
prepare_export_fig_11C_005_button = widgets.Button(
description='Export',
disabled=False,
button_style='',
tooltip='',
)
prepare_export_fig_11C_005_button.on_click(prepare_export)
top_block_11C_000 = widgets.HBox(
[],
layout=widgets.Layout(
width='100%',
align_self='center'
)
)
top_block_11C_000.children = [
widgets.VBox([
fig_11C_001,
prepare_export_fig_11C_001_button
],
layout=widgets.Layout(
width='33%',
align_items='center'
)
),
widgets.VBox([
fig_11C_002,
fig_11C_003,
alpha_slider_11C_003
],
layout=widgets.Layout(
width='16%',
height='500px',
align_items='center',
margin='40px 0 0 0'
)
),
widgets.VBox([
fig_11C_002,
fig_11C_004,
beta_slider_11C_004
],
layout=widgets.Layout(
width='16%',
height='500px',
align_items='center',
margin='40px 0 0 0'
)
),
widgets.VBox([
fig_11C_005,
prepare_export_fig_11C_005_button
],
layout=widgets.Layout(
width='33%',
align_items='center'
)
),
]
bottom_block_11C_000 = widgets.HBox(
[],
layout=widgets.Layout(
width='100%',
height='60px',
align_self='center'
)
)
bottom_block_11C_000.children = [
widgets.VBox([
widgets.HTMLMath(
value=r"\( (p_r + \frac{\alpha}{v_r^2})(v_r - \beta) = \frac{8}{3} T_r \)"
)],
layout=widgets.Layout(
width='33%',
align_items='center'
)
),
widgets.VBox(
[return_button],
layout=widgets.Layout(
width='33%',
align_items='center'
)
),
widgets.VBox([
widgets.HTMLMath(
value=r"\( p_r v_r = R T_r \)"
)],
layout=widgets.Layout(
width='33%',
align_items='center'
)
)
]
main_block_11C_000 = widgets.VBox(
[],
layout=widgets.Layout(
width='100%',
align_items='center'
)
)
main_block_11C_000.children = [
change_view_button,
top_block_11C_000,
bottom_block_11C_000
]
figures = [
fig_11C_001,
fig_11C_002,
fig_11C_003,
fig_11C_004,
fig_11C_005,
]
main_block_11C_000