#004 - SciPy in Structural and Civil Engineering, Part 2/3: Optimization and Interpolation
Leveraging SciPy for Design Optimization in Structural Components
In the world of structural and civil engineering, precision and efficiency are paramount. SciPy, a Python package, excels (no pun intended) in both aspects. This post delves into its practical applications, focusing on how it aids professional engineers.
You can check out the supporting code examples on the flocode Github repo.
The other posts in this series:
SciPy in Structural and Civil Engineering, Part 1/3: Numerical Integration
SciPy in Structural and Civil Engineering, Part 3/3: Structural Dynamics
SciPy is a powerful, open-source Python package used for scientific and technical computing. It provides many efficient methods for tasks such as numerical integration, interpolation, optimization, linear algebra, and more. SciPy builds upon the NumPy package, enabling it to handle arrays and matrices of large size, making it an exceptional tool for various scientific fields. In this article, we will explore how SciPy is used in the field of structural and civil engineering, with a specific focus on optimization, basically refining solutions based on our objectives.
A few key distinctions between the SciPy and NumPy packages.
NumPy
Written in C for speed
Focuses on basic mathematical and numeric calculations
Primarily uses array data structures
Offers basic operations like sorting, shaping, and indexing
Fundamental for basic calculations in Data Science
SciPy
Built on top of NumPy
Provides advanced features, especially in linear algebra and optimization
Utilizes NumPy arrays and introduces additional data structures
Includes extensive functions for scientific computing
Offers more advanced features useful in Data Science
Also, check out this handy SciPy cheat sheet from DataCamp.
Optimization
Optimization involves finding the best solution to a problem by minimizing or maximizing an objective function. In structural engineering, this could relate to optimizing the design of components like beams, concrete slab depth or plate thicknesses.
Engineers can utilize SciPy's optimization module to find the optimal dimensions or configurations of components, ensuring designs are both efficient and reliable. The spo.minimize
function employs various algorithms to achieve this optimization goal.
This process is crucial when designing complex structures like turbine blades or dams, where finding the best configuration can have a significant impact on performance and safety.
The concept of optimization using SciPy in Python is similar in principle to "Goal Seek" in Excel, but it extends to a broader range of applications and offers more advanced optimization techniques.
In both cases:
Objective Function: You define an objective function that represents the quantity you want to optimize (minimize or maximize). In Excel's "Goal Seek," this is typically a cell value, while in SciPy, it can be any mathematical function representing an engineering objective, such as cost minimization or performance maximization.
Initial Guess: You provide an initial guess or estimate of the input variables (parameters) that will produce the desired outcome in the objective function.
Optimization Process: Both Excel's "Goal Seek" and SciPy's optimization methods iteratively adjust the input variables to minimize or maximize the objective function while respecting constraints. They aim to find the optimal solution.
However, there are significant differences:
Complexity and Flexibility: SciPy offers a wide range of optimization algorithms suitable for various scenarios, from simple linear optimization to complex nonlinear optimization with constraints. Excel's "Goal Seek" is more user-friendly for basic tasks but may not handle advanced optimization problems.
Customization: With SciPy, you have more control over the optimization process, including specifying constraints and selecting optimization methods tailored to your specific engineering problems.
Scripting and Automation: SciPy's optimization can be scripted and automated, making it suitable for batch processing and integrating with other Python-based tools and workflows.
In summary, while both Excel's "Goal Seek" and SciPy's optimization serve the purpose of finding optimal solutions, SciPy provides more advanced capabilities and flexibility for engineers and scientists dealing with complex and customized optimization tasks in Python.
Optimization Example: Beam Deflection
Let’s consider
Let's consider optimizing the deflection of a simply supported beam subjected to a point load P at its midpoint. The formula for maximum deflection δ_max​ is given by:
Objective
Minimize δmax​ by altering I, subject to a constraint that I should not go below a certain value I_min​.
In this example, the minimize
function uses the SLSQP (Sequential Least Squares Quadratic Programming) algorithm by default, which is suitable for a constrained optimization problem like this one.
In simple terms, it works in steps:
Approximates the objective function and constraints using quadratic forms.
Solves these approximations to find an improved point.
Updates the approximations based on the new point.
Repeats the steps until it finds the best solution within the given tolerances.
It's useful for problems where you want to find the optimal value while respecting certain rules or limits.
Here's a Python code snippet using SciPy's minimize
function:
from scipy.optimize import minimize
# Constants, we define these
P = 10 # kN
L = 5 # meters
E = 200_000 # kN/m^2
I_min = 1e-6 # m^4, This is the minimum allowable Moment on Inertia
# Objective function: Max deflection to be minimized
def max_deflection(I):
return (P * L ** 3) / (48 * E * I)
# Constraint: I should be >= I_min
constraints = ({'type': 'ineq', 'fun': lambda I: I - I_min})
# Initial guess
I_initial = 2e-6 # m^4
# Optimization
result = minimize(max_deflection, I_initial, constraints=constraints)
# Extract optimized value of I
I_optimized = result.x[0]
print(f"Optimized Moment of Inertia: {I_optimized:.3e} m^4")
Run the Python script and your output is:
Optimized Moment of Inertia: 3.155e+04 m^4
Interpolation
For irregular data points, SciPy's interp1d
function creates smooth curves, which are useful in designing efficient water distribution systems. This interpolation capability ensures that engineers can work with irregularly spaced data efficiently.
In the example below, we will investigate the use of SciPy's interp1d
function for linear interpolation of a set of data points. The sample data points represent the relationship between x_data
and y_data
. We want to estimate values of y
at new points x_new
within the range of our sample data.
Here's how it works:
Import necessary libraries (
numpy
,scipy.interpolate
, andmatplotlib.pyplot
).Define sample data points
(x_data, y_data)
that represent a known relationship betweenx
andy
.Create an interpolation function
f
usinginterp1d
that models this relationship.Utilize the function
f
to estimatey
values (y_new
) at new pointsx_new
within the sample data range.Visualize the original data points and the interpolated curve using
matplotlib
.
By following these steps, you get an interpolated curve that approximates the values of y
between the known data points, allowing for more precise estimates within that range.
from scipy.interpolate import interp1d
import numpy as np
import matplotlib.pyplot as plt
# Sample data points
x_data = np.array([0, 1, 2, 3, 4])
y_data = np.array([0, 1, 4, 9, 16])
# Create interpolation function
f = interp1d(x_data, y_data)
# Interpolate at new points
x_new = np.linspace(0, 4, 100)
y_new = f(x_new)
# Plotting
plt.scatter(x_data, y_data, label='Data Points')
plt.plot(x_new, y_new, label='Interpolated Curve')
plt.legend()
plt.show()
This code interpolates between the given data points (x_data, y_data)
and evaluates the interpolated values at 100 equally-spaced points (x_new
) between 0 and 4. The result is plotted for visualization.
Closing
Optimization and interpolation are invaluable tools in the engineering toolkit. Optimization algorithms like those available in SciPy enable engineers to fine-tune system parameters, reduce material costs, and enhance safety measures. Interpolation, on the other hand, allows engineers to make informed estimates when faced with incomplete or irregular data, thus bridging the gap between known data points. These capabilities are crucial in fields requiring high levels of accuracy and efficiency, such as structural, civil, and mechanical engineering.
To get the most out of SciPy, engineers should start by familiarizing themselves with the official documentation. Follow this up by working on small, focused projects or exercises that directly relate to your field. Over time, integrate these techniques into larger workflows, gradually unlocking the package's full potential. The key is consistent practice and application, allowing you to harness the advanced capabilities that SciPy offers for complex engineering tasks.
Flocode courses will be delving more deeply into how we can use SciPy for other more involved engineering tasks.
See you in the next post!
James 🌊