Sunday, August 24, 2025

#6 Civil Applications using Python

 Let’s dive deeper into civil engineering applications with Python.

I’ll give you detailed, explained code for three real-world tasks:

  1. BMD (Bending Moment Diagram) & SFD plotting

  2. Rainfall data analysis (Hydrology)

  3. Survey data processing (Coordinate method for area)


πŸ“˜ 1. Shear Force & Bending Moment Diagram (SFD & BMD)

🎯 Theory

  • A beam under loads has Shear Force (V) and Bending Moment (M) at every section.

  • Equilibrium conditions are used to compute reactions at supports.

  • For a simply supported beam with span L and a point load P at midspan:

    • Reactions: RA=RB=P/2R_A = R_B = P/2

    • Shear force is +RA+R_A till midspan, then RB-R_B.

    • Bending moment: linear, max at midspan: Mmax=PL4M_{max} = \frac{PL}{4}.

🐍 Python Code

import numpy as np
import matplotlib.pyplot as plt

# Beam parameters
L = 6   # length of beam in meters
P = 20  # point load in kN at midspan
a = L/2 # location of load

# Reactions
RA = RB = P/2

# Discretize beam
x = np.linspace(0, L, 100)

# Shear force (piecewise function)
V = np.piecewise(x, [x <= a, x > a], [RA, -RB])

# Bending moment (linear on each side)
M = np.piecewise(x, [x <= a, x > a],
                 [lambda x: RA*x,
                  lambda x: RB*(L-x)])

# Plotting
plt.figure(figsize=(8,6))

plt.subplot(2,1,1)
plt.plot(x, V, color="blue")
plt.axhline(0, color="black", linewidth=0.7)
plt.title("Shear Force Diagram (SFD)")
plt.ylabel("Shear Force (kN)")
plt.grid(True)

plt.subplot(2,1,2)
plt.plot(x, M, color="red")
plt.axhline(0, color="black", linewidth=0.7)
plt.title("Bending Moment Diagram (BMD)")
plt.xlabel("Beam Length (m)")
plt.ylabel("Moment (kNm)")
plt.grid(True)

plt.tight_layout()
plt.show()

✅ This shows the jump in shear at load point and the triangular bending moment distribution.


πŸ“˜ 2. Rainfall Data Analysis (Hydrology)

🎯 Theory

  • Rainfall data is collected from multiple stations.

  • Arithmetic mean method or Thiessen polygon method is used for average rainfall.

  • Here we’ll use pandas to process rainfall data and matplotlib to plot annual trends.

🐍 Python Code

import pandas as pd
import matplotlib.pyplot as plt

# Example rainfall data (mm) for 5 years
data = {
    "Year": [2018, 2019, 2020, 2021, 2022],
    "Station_A": [850, 920, 870, 910, 890],
    "Station_B": [800, 880, 860, 930, 910],
    "Station_C": [780, 850, 810, 870, 860]
}

df = pd.DataFrame(data)

# Compute average rainfall (Arithmetic Mean)
df["Avg_Rainfall"] = df[["Station_A","Station_B","Station_C"]].mean(axis=1)

print("Rainfall Data:\n", df)

# Plot rainfall trend
plt.figure(figsize=(7,4))
plt.plot(df["Year"], df["Station_A"], marker="o", label="Station A")
plt.plot(df["Year"], df["Station_B"], marker="o", label="Station B")
plt.plot(df["Year"], df["Station_C"], marker="o", label="Station C")
plt.plot(df["Year"], df["Avg_Rainfall"], marker="s", linestyle="--", color="black", label="Average")

plt.title("Annual Rainfall Analysis")
plt.xlabel("Year")
plt.ylabel("Rainfall (mm)")
plt.legend()
plt.grid(True)
plt.show()

✅ This produces a line chart showing rainfall variation across stations and the average.
Civil engineers use such analysis for hydrological design (dams, canals, urban drainage).


πŸ“˜ 3. Survey Data Processing (Coordinate Method for Area)

🎯 Theory

  • For a closed traverse, area can be computed using the Shoelace Formula:

A=12(xiyi+1xi+1yi)A = \frac{1}{2} \left| \sum (x_i y_{i+1} - x_{i+1} y_i) \right|
  • Commonly used in surveying and GIS.

🐍 Python Code

# Coordinates of a land parcel (survey data)
coords = [(0,0), (60,0), (80,40), (40,60), (0,40)]

# Apply Shoelace formula
area = 0
n = len(coords)
for i in range(n):
    x1, y1 = coords[i]
    x2, y2 = coords[(i+1) % n]  # next point (cyclic)
    area += (x1*y2 - x2*y1)

area = abs(area)/2
print("Surveyed Land Area =", area, "sq.m")

# Plotting the boundary
xs, ys = zip(*coords)
plt.figure(figsize=(6,6))
plt.fill(xs + (xs[0],), ys + (ys[0],), color="lightblue", alpha=0.6, edgecolor="black")
plt.scatter(xs, ys, color="red")
for i, (x,y) in enumerate(coords):
    plt.text(x+1, y+1, f"P{i+1}")
plt.title("Survey Plot (Coordinate Method)")
plt.xlabel("X (m)")
plt.ylabel("Y (m)")
plt.axis("equal")
plt.grid(True)
plt.show()

✅ This computes land area from survey coordinates and plots the site boundary.


πŸ”‘ Summary

  • BMD/SFD → Structural engineering visualization.

  • Rainfall analysis → Hydrology applications.

  • Survey data processing → Surveying/GIS applications.

No comments:

Post a Comment

#6 Civil Applications using Python

 Let’s dive deeper into civil engineering applications with Python . I’ll give you detailed, explained code for three real-world tasks: ...