Py.Cafe

joreilly86/

beam-bending-shear-analysis-solara

Beam Bending and Shear Force Analysis in Solara

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import solara
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path

@solara.component
def Logo():
    image_url = "https://flocode.b-cdn.net/Branding/Image_server/header_logo_dark.png"
    return solara.Image(
        image=image_url,
        width="130px",  # This will maintain the aspect ratio
        classes=["logo"]  # You can add custom CSS classes if needed
    )

@solara.component
def BeamCalculator():
    length, set_length = solara.use_state(12.0)
    point_load, set_point_load = solara.use_state(5.0)
    point_load_location, set_point_load_location = solara.use_state(6.0)
    udl_start, set_udl_start = solara.use_state(2.0)
    udl_end, set_udl_end = solara.use_state(10.0)
    udl_magnitude, set_udl_magnitude = solara.use_state(3.0)

    def calculate_bending_moment(x):
        moments = np.zeros_like(x)
        L = length
        a = point_load_location
        P = point_load
        w = udl_magnitude
        start = udl_start
        end = udl_end

        # Point Load Bending Moment Calculation
        for i, xi in enumerate(x):
            if xi <= a:
                moments[i] += P * xi * (L - a) / L
            else:
                moments[i] += P * a * (L - xi) / L

        # UDL Bending Moment Calculation
        for i, xi in enumerate(x):
            if xi < start:
                # Before UDL starts
                R1 = w * (end - start) * (L - (start + end) / 2) / L
                moments[i] += R1 * xi
            elif start <= xi <= end:
                # Within UDL
                R1 = w * (end - start) * (L - (start + end) / 2) / L
                moments[i] += R1 * xi - w * (xi - start)**2 / 2
            else:
                # After UDL ends
                R1 = w * (end - start) * (L - (start + end) / 2) / L
                R2 = w * (end - start) - R1
                moments[i] += R1 * xi - w * (end - start) * (xi - (start + end) / 2)

        return -moments  # Negate to show moments below the beam

    def calculate_shear_force(x):
        shear = np.zeros_like(x)
        L = length
        a = point_load_location
        P = point_load
        w = udl_magnitude
        start = udl_start
        end = udl_end

        # Point Load Shear Force Calculation
        R1 = P * (L - a) / L
        for i, xi in enumerate(x):
            if xi < a:
                shear[i] += R1
            elif xi > a:
                shear[i] += R1 - P

        # UDL Shear Force Calculation
        R1_udl = w * (end - start) * (L - (start + end) / 2) / L
        for i, xi in enumerate(x):
            if xi < start:
                shear[i] += R1_udl
            elif start <= xi <= end:
                shear[i] += R1_udl - w * (xi - start)
            else:
                shear[i] += R1_udl - w * (end - start)

        return shear

    def plot_load_diagram():
        fig, ax = plt.subplots(figsize=(10, 2))
        ax.set_xlim(0, length)
        ax.set_ylim(-2, 2)
        ax.set_yticks([])
        ax.set_title("Load Diagram")
        ax.set_xlabel("Distance along beam (m)")

        # Plot beam
        ax.plot([0, length], [0, 0], color='black', linewidth=2)

        # Plot point load
        if point_load != 0:
            arrow_direction = -1 if point_load > 0 else 1
            ax.arrow(point_load_location, 0, 0, arrow_direction, head_width=0.3, head_length=0.3, fc='red', ec='red')
            ax.text(point_load_location, arrow_direction * 1.7, f'{point_load} kN', color='red', ha='center')

        # Plot UDL
        if udl_magnitude != 0:
            ax.plot([udl_start, udl_end], [0.5, 0.5], color='blue', linewidth=10, alpha=0.3)
            ax.text((udl_start + udl_end) / 2, 1, f'{udl_magnitude} kN/m', color='blue', ha='center')

        # Legend
        handles = [
            plt.Line2D([0], [0], color='red', lw=2, label='Point Load'),
            plt.Line2D([0], [0], color='blue', lw=2, alpha=0.3, label='Uniform Distributed Load')
        ]
        ax.legend(handles=handles)

        return fig

    def plot_bending_moment():
        x = np.linspace(0, length, 100)
        y = calculate_bending_moment(x)

        fig, ax = plt.subplots(figsize=(10, 6))
        ax.plot(x, y, label="Bending Moment", color='green')
        ax.fill_between(x, y, 0, alpha=0.3, color='green')  # Add shading
        ax.set_title("Beam Bending Moment Diagram")
        ax.set_xlabel("Distance along beam (m)")
        ax.set_ylabel("Bending Moment (kNm)")
        ax.grid(True)
        ax.set_xlim(0, length)
        ax.set_ylim(min(y.min() * 1.1, 0), max(0, y.max() * 1.1))
        ax.legend()
        return fig

    def plot_shear_force():
        x = np.linspace(0, length, 100)
        y = calculate_shear_force(x)

        fig, ax = plt.subplots(figsize=(10, 6))
        ax.plot(x, y, label="Shear Force", color='red')
        ax.fill_between(x, y, 0, alpha=0.3, color='red')  # Add shading
        ax.set_title("Beam Shear Force Diagram")
        ax.set_xlabel("Distance along beam (m)")
        ax.set_ylabel("Shear Force (kN)")
        ax.grid(True)
        ax.set_xlim(0, length)
        ax.set_ylim(min(y.min() * 1.1, 0), max(y.max() * 1.1, 0))
        ax.legend()
        return fig

    x = np.linspace(0, length, 100)
    max_moment = np.max(np.abs(calculate_bending_moment(x)))
    max_shear = np.max(np.abs(calculate_shear_force(x)))

    return solara.Column(children=[
        solara.Row(children=[Logo()], justify="start"),
        solara.Card(children=[
            solara.Markdown("""
                # Engineering Dashboard Example in Solara ☀
                ## Beam Bending Moment and Shear Force Calculator 
                This application calculates the bending moment and shear force diagrams for a simply supported beam with specified point load and uniform distributed load (UDL). 
                You can adjust the beam length, point load magnitude and location, and UDL start, end, and magnitude using the controls.
                
                This simple example is part of an engineering dashboard tutorial. To learn more, check out this [introductory article to engineering dashboards](https://open.substack.com/pub/flocode/p/engineering-dashboards-01-solara?r=tbs50&utm_campaign=post&utm_medium=web) from the [Flocode Newsletter](https://flocode.substack.com).
                
                Everything you need to build something like this is available through the docs on [solara.dev]()
            """)
        ]),
        solara.Columns([4, 8], children=[
            solara.Column(children=[
                solara.Card(children=[
                    solara.Markdown("## Controls"),
                    solara.SliderFloat("Beam Length (m)", value=length, min=1, max=20, step=0.1, on_value=set_length),
                    solara.InputFloat("Point Load (kN)", value=point_load, on_value=set_point_load),
                    solara.InputFloat("Point Load Location (m)", value=point_load_location, on_value=set_point_load_location),
                    solara.InputFloat("UDL Start (m)", value=udl_start, on_value=set_udl_start),
                    solara.InputFloat("UDL End (m)", value=udl_end, on_value=set_udl_end),
                    solara.InputFloat("UDL Magnitude (kN/m)", value=udl_magnitude, on_value=set_udl_magnitude),
                ]),
            ]),
            solara.Column(children=[
                solara.Card(children=[
                    solara.Markdown("### Load Diagram"),
                    solara.FigureMatplotlib(plot_load_diagram())
                ]),
                solara.Info(f"Maximum Bending Moment: {max_moment:.2f} kNm"),
                solara.Info(f"Maximum Shear Force: {max_shear:.2f} kN"),
                
                solara.Card(children=[
                    solara.Markdown("### Bending Moment Diagram"),
                    solara.FigureMatplotlib(plot_bending_moment())
                ]),
                solara.Card(children=[
                    solara.Markdown("### Shear Force Diagram"),
                    solara.FigureMatplotlib(plot_shear_force())
                ]),
            ])
        ])
    ], style={"padding": "30px"})

@solara.component
def Page():
    return solara.Column(children=[
        BeamCalculator()
    ])

@solara.component
def Layout(children):
    return children[0]

solara.lab.theme.dark = True

solara.render(Page())