Engineering Notes

Automation & code Structural analysis N.º 02

Automating a beam with Python, from scratch

Series: Automating structural calcs with Python · Part 1 of 6

Abstract. We have all computed a simply supported beam by hand. In this first part we move it to Python, line by line, and see what a calculator cannot do: compute twenty beams in one second.

At university we all solved the same beam many times: simply supported, a distributed load and maybe a point load. Formula, calculator, result. At the office it is similar, but with twenty different beams, and every time a load or a span changes you redo everything.

That is where programming helps. Python does not know more engineering than you: it is a calculator that remembers what you taught it and repeats it without getting tired or slipping on the third decimal.

This note is the first of a series that goes from simple to complex. Today we will do four things:

  1. recall the beam formulas;
  2. write them in Python, line by line;
  3. compute one beam;
  4. compute several beams at once, which is where you save time.

You do not need to know how to program. If you have never used Python, at the end I explain how to run the script.

wPLEI
The beam in this note: simply supported, distributed load w over the whole span L and point load P at midspan.

The usual formulas

For a simply supported beam of span LL, with a distributed load ww along the whole span and a point load PP at midspan, the maximum moment is at midspan:

M=wL28+PL4M = \frac{w L^2}{8} + \frac{P L}{4}

Maximum shear is at the supports:

V=wL2+P2V = \frac{w L}{2} + \frac{P}{2}

And midspan deflection, which depends on the section stiffness EIEI:

δ=5 wL4384 EI+PL348 EI\delta = \frac{5\, w L^4}{384\, EI} + \frac{P L^3}{48\, EI}

Notice the exponents: moment grows with L2L^2, but deflection grows with L4L^4. We will come back to that.

The same formulas in Python

In Python, a function is a block that takes data and returns results. It is how you “teach” it a calculation:

def viga_simple(L, w, P, EI):
    """Returns maximum moment, maximum shear and midspan deflection."""
    M = w * L**2 / 8 + P * L / 4                                  # kN·m
    V = w * L / 2 + P / 2                                         # kN
    flecha = 5 * w * L**4 / (384 * EI) + P * L**3 / (48 * EI)     # m
    return M, V, flecha

Read it like a spreadsheet:

  • def viga_simple(L, w, P, EI): says “this function needs span, distributed load, point load and stiffness”.
  • L**2 is L2L^2. The double asterisk is the power.
  • Anything after # is a comment: Python ignores it, but it reminds you of the units.
  • return hands back the three results.

A tip that saved me many mistakes: pick one unit system and write it at the top of the file. Here everything is in kN and m. Moment comes out in kN·m and deflection in meters, with no hidden conversions.

One beam

Now the data. A 30 × 60 cm concrete beam, with 20 kN/m distributed and 30 kN at midspan:

w = 20.0              # distributed load, kN/m
P = 30.0              # point load at midspan, kN
b, h = 0.30, 0.60     # section, m
E = 25_000_000        # modulus of elasticity, kPa (25,000 MPa)
I = b * h**3 / 12     # moment of inertia, m⁴
EI = E * I            # kN·m²

M, V, f = viga_simple(6.0, w, P, EI)
print(f"M = {M:.0f} kN·m,  V = {V:.0f} kN,  deflection = {f*1000:.1f} mm")

Python answers:

M = 135 kN·m,  V = 75 kN,  deflection = 3.5 mm

Check it on a calculator: 20⋅62/8+30⋅6/4=90+45=13520 \cdot 6^2/8 + 30 \cdot 6/4 = 90 + 45 = 135 kN·m. Always check the first run by hand. If it matches, you can trust the script for the rest.

Many beams: what a calculator cannot do

Here is the payoff. With a for loop, Python repeats the calculation for each span in a list:

for L in [4, 5, 6, 7, 8]:
    M, V, f = viga_simple(L, w, P, EI)
    print(f"L = {L} m:  M = {M:.0f} kN·m,  deflection = {f*1000:.1f} mm")
Span (m)MM (kN·m)VV (kN)Deflection (mm)LL/deflection
470550.8≈ 5,000
5100651.8≈ 2,800
6135753.5≈ 1,700
7175856.2≈ 1,100
82209510.3≈ 780

Look at the table. If the span doubles, from 4 to 8 m, moment triples, but deflection grows 13 times. That is the L4L^4 in the formula, now with real numbers. By hand that pattern is hard to see; with five rows in a table it jumps out.

An honest warning: deflection here uses the uncracked section inertia. A real concrete beam cracks and keeps deforming over time, so the real deflection is several times larger. We will get to that later in the series.

How to run the script

  1. Install Python from python.org (tick “Add Python to PATH” in the installer).
  2. Download this note’s script, below.
  3. Open a terminal in the file’s folder and type python viga-01.py.

Change the data (load, section, list of spans) and run it again. That is how you learn: by touching it.

Next

In part 2 we will draw moment and shear diagrams for any load combination, not just the load at midspan.

Automating is not knowing more formulas. It is writing the one you know well, once, and letting the machine repeat it.

Download the script for this note ↓ viga-01.py

About the author

Yordan Rocio Maldonado

Structural engineer, seismic and bridge specialist. CIP 215845

Educational, reference-only content. Opinions are my own and do not represent any employer. On a real project, the engineer of record and the governing code decide.

← Back to notes