Page MenuHomec4science

hidden_code_nb_geo.py
No OneTemporary

File Metadata

Created
Sat, Apr 27, 20:16

hidden_code_nb_geo.py

import math
import numpy as np
from bokeh.layouts import layout, column
from bokeh.models import Div, CustomJS, Slider, Spacer
from bokeh.models.tickers import SingleIntervalTicker
from bokeh.plotting import figure, show, ColumnDataSource
from bokeh.io import output_notebook
from cienpy import simplebeam as sb
from cienpy import rectangular_section as beam_section
from cienpy import models
output_notebook()
def main_code(L, h, b, A, Iy, Iz):
########################################################
# Initialisation
########################################################
# constants for the visualisation
MAX_B = 3*b
MAX_H = 3*h
# store the values in a specific format
data_beam = dict(
x=[0, L],
y=[0, 0]
)
initial_position = L
data = dict( # stores every useful single variable
L=[L],
b=[b],
h=[h],
A=[A],
Iy=[Iy],
Iz=[Iz],
x=[initial_position],
y=[0]
)
source_beam = ColumnDataSource(data_beam)
source = ColumnDataSource(data)
########################################################
# Define figures, widgets and renderers
########################################################
FIG_H_B = 200 # height figure beam
FIG_B_B = 700 # width figure beam
FIG_H_SEC = 600 # height figure section
paddingx = 0.2*L
options = dict(
toolbar_location=None
)
fig_beam = figure(**options,
x_axis_label="Position [m]",
plot_height=200,
plot_width=700,
x_range=(0-paddingx, L+paddingx),
y_range=(-1, 1),
title="Simple supported beam"
)
fig_beam.xgrid.grid_line_alpha = 0
fig_beam.ygrid.grid_line_alpha = 0
fig_beam.yaxis.visible = False
fig_section = figure(**options,
x_axis_label="Width b [mm]",
y_axis_label="Height h [mm]",
plot_height=int(MAX_H*0.9),
plot_width=int(MAX_B),
match_aspect=True,
title="Cross-section of the beam"
)
fig_section.rect([0], [0], width=MAX_B, height=MAX_H, alpha=0, fill_alpha=0) # transparent rect
fig_section.axis.ticker = SingleIntervalTicker(interval=50, num_minor_ticks=5)
# beam
beam = fig_beam.line('x', 'y', source=source_beam, line_width=6, color='black')
b_supp = 0.3
supp_opt = dict(
fill_color='white',
line_color='black'
)
support_l = fig_beam.patch([0, b_supp/2, -b_supp/2],
[0, -b_supp/2*math.sqrt(3), -b_supp/2*math.sqrt(3)],
**supp_opt)
support_r = fig_beam.circle([L], [-b_supp/2], radius=b_supp/2, **supp_opt)
# section
section = fig_section.rect([0], [0], width=b, height=h, fill_color='white',
color='black', line_width=3, hatch_pattern='/', hatch_color='black')
# show mechanical parameters
div_text = lambda h, b, L, A, Iy, Iz : f"""<p style='font-size:16px'><b>Geometrical and mechanical parameters</b></p>
h = {round(h)} mm<br>
b = {round(b)} mm<br>
L = {round(L)} m<br>
A = {"{:.2e}".format(A)} mm<sup>2</sup><br>
Iy = {"{:.2e}".format(Iy)} mm<sup>4</sup><br>
Iz = {"{:.2e}".format(Iz)} mm<sup>4</sup>"""
div = Div(
text=div_text(h, b, L, A, Iy, Iz),
width=300,
height=300
)
# change geometry
slider_b = Slider(
title="Change the width b [mm]",
start=10,
end=MAX_B,
step=10,
value=b
)
slider_h = Slider(
title="Change the height h [mm]",
start=20,
end=MAX_H,
step=20,
value=h
)
slider_L = Slider(
title="Change the length L [m]",
start=1,
end=L+1,
step=0.1,
value=L
)
# reference system
axis_arrow_length = 0.8
axis_arrow_scale = 100
x_axis_arrow = models.force_vector(fig_beam, axis_arrow_length, L, L+axis_arrow_length, 0, 0, 'gray')
x_axis_label = fig_beam.text(x=[L+axis_arrow_length/2], y=[0.1], text=["x axis"], text_color='gray', text_baseline='bottom')
models.force_vector(fig_section, axis_arrow_length*10, 0, 0, 0, axis_arrow_length*axis_arrow_scale*1.6, 'gray') # y axis
fig_section.text(x=[0], y=[axis_arrow_length*axis_arrow_scale*1.7], text=["y axis"], text_color='gray', text_baseline='middle', angle=math.pi/2)
models.force_vector(fig_section, axis_arrow_length*10, 0, -axis_arrow_length*axis_arrow_scale, 0, 0, 'gray') # z axis
fig_section.text(x=[-axis_arrow_length*axis_arrow_scale*1.1], y=[0], text=["z axis"], text_color='gray', text_align='right', text_baseline='middle')
########################################################
# Configurethe logics
########################################################
code_slider = lambda var, L_code, b_code, h_code, x_axis_code="" : f"""
// retrieve data used
const data_b = s_b.data
const data_s = s_s.data
// retrieve the var from the object that uses callback
const f = cb_obj.value // value of the slider
// compute the parameters and dimensions
const L = Math.round({L_code}*10)/10
const b = Math.round({b_code})
const h = Math.round({h_code})
const A = b*h
const Iy = Math.round(Math.pow(h, 3)*b/12)
const Iz = Math.round(Math.pow(b, 3)*h/12)
div.text = "<p style='font-size:16px'><b>Geometrical and mechanical parameters:</b></p> h = "+h+
" mm<br> b = "+b+
" mm<br> L = "+L+
" m<br> A = "+A.toExponential()+
" mm<sup>2</sup><br> Iy = "+Iy.toExponential()+
" mm<sup>4</sup><br> Iz = "+Iz.toExponential()+
" mm<sup>4</sup>"
// change the plot of the section
section.glyph.width = b
section.glyph.height = h
// change the plot of the beam
data_b['x'][1] = L
support_r.glyph.x = L
{x_axis_code}
// apply the changes
data_s['{var}'] = f
data_s['A'] = A
data_s['Iy'] = Iy
data_s['Iz'] = Iz
s_s.change.emit()
s_b.change.emit()
"""
x_axis_code = f"""
// change the position of the x axis arrow and text
x_axis_arrow.x_start = L
x_axis_arrow.x_end = L+{axis_arrow_length}
x_axis_label.glyph.x = L+{axis_arrow_length}/2
"""
# logic for slider_L
update_L = CustomJS(args=dict(s_b=source_beam, s_s=source, div=div, section=section, support_r=support_r,
x_axis_arrow=x_axis_arrow, x_axis_label=x_axis_label),
code=code_slider("L", "f", "data_s['b']", "data_s['h']", x_axis_code))
slider_L.js_on_change('value', update_L)
# logic for slider_b
update_b = CustomJS(args=dict(s_b=source_beam, s_s=source, div=div, section=section, support_r=support_r),
code=code_slider("b", "data_s['L']", "f", "data_s['h']"))
slider_b.js_on_change('value', update_b)
# logic for slider_h
update_h = CustomJS(args=dict(s_b=source_beam, s_s=source, div=div, section=section, support_r=support_r),
code=code_slider("h", "data_s['L']", "data_s['b']", "f"))
slider_h.js_on_change('value', update_h)
########################################################
# Build the layout
########################################################
layout1 = layout([
[fig_beam],
[fig_section, Spacer(width=20), column([Spacer(height=50), slider_L, slider_b, slider_h, Spacer(height=50), div])]
])
show(layout1)

Event Timeline