#023 - Python Libraries for Civil/Structural Engineering
A curated listed of useful Python libraries specifically for civil and structural engineering applications.
Hello again; today is a list of all the actively maintained Python libraries I know of for civil and structural engineering.
The list is not exhaustive, so if you have suggestions, please add them in the comments.
The list is at the end of the article if you can’t wait!
First, I want to give a brief overview of Python libraries.
What are Python Libraries?
Python libraries are collections of pre-written code that extend the language's capabilities. They provide ready-to-use functions and tools for a vast range of tasks, from scientific computing and data analysis to machine learning, web development and, in our case, civil and structural engineering. Using libraries, engineers can save time, streamline their workflows, and leverage well-tested code to tackle complex engineering problems more efficiently.
Often, the term ‘library’ is used interchangeably with ‘package’, but that’s not totally accurate. A library is a collection of code aimed at solving specific problems, whereas a package is a structured collection of Python modules and libraries organized to facilitate easy usage and implementation. A package is a bigger, broader concept and can contain libraries. Is this a big deal? No.
The modularity of Python libraries allows engineers to integrate specific functionalities into their projects without the overhead of unnecessary features. This modularity ensures that engineers can construct tailored solutions, picking and choosing libraries that best fit their needs.
They also allow you to build tools and solve problems in new, novel ways.
These libraries are accessible through repositories like PyPI (Python Package Index), where you can find and install them using package managers like pip. Websites like GitHub also host many of these libraries, often accompanied by community-contributed documentation and examples.
I like Poetry as a package manager. I’ve been using it for the last 6 months or so, and it’s made managing virtual environments and dependency version control much easier.
A lot of people like Anaconda or miniconda. Both are great alternatives for library/package management.
The open-source nature of these libraries fosters a collaborative environment where a global community of developers continually contributes to their improvement and expansion. This community-driven development model accelerates innovation and ensures that the libraries are constantly updated with the latest advancements and best practices.
"Open source is a development methodology; free software is a social movement." - Richard Stallman
Engineers using Python libraries benefit from a collective wealth of knowledge, enhancing the efficiency and reliability of their own solutions.
It’s the opposite of a black box.
An open and transparent box that can be passed around the world for everyone to conduct their own personal QA/QC protocols. If you don’t like the box, redesign it and submit your idea through a pull request.
The library maintainer/creator may accept your proposed change, and now everyone can benefit from your outstanding box design skills. 🎉👏
I use the same libraries for many of my Python engineering projects: NumPy, Pandas, Matplotlib and Tabulate. These libraries alone provide so much flexibility and technical reach.
I’ll briefly cover just a couple to give you a sense of a Python library. Flocode’s courses will cover these libraries in much greater detail throughout our example projects, but you can always find out more from the documentation for each library linked below.
NumPy: The Foundation of Numerical Computing in Python
NumPy, an acronym for Numerical Python, is a fundamental library for scientific computing in Python. NumPy is the go-to library for efficient numerical computation. It forms the foundational layer for many other scientific libraries, including Pandas, SciPy and Matplotlib. NumPy supports large, multi-dimensional arrays and matrices, along with a comprehensive collection of high-level mathematical functions to operate on these arrays. Its extensive functionality makes it indispensable for tasks requiring numerical calculations, from simple arithmetic to geometry to complex matrix operations.
Core Features of NumPy
At the heart of NumPy are its arrays, which are more efficient and powerful than Python's built-in list data structure, especially for numerical operations. These arrays are central to NumPy's capabilities, enabling efficient storage and manipulation of large datasets. NumPy arrays facilitate advanced matrix operations and linear algebra functions, which are everywhere in any finite element modelling.
Another significant feature of NumPy is vectorization. Vectorization allows for executing operations on entire arrays instead of looping over individual elements, drastically improving computational efficiency. This approach minimizes the overhead of Python loops and leverages optimized C and Fortran libraries under the hood, leading to faster execution times. Combining these features – efficient array handling and vectorized computations – makes NumPy a powerhouse for numerical computing.
Microsoft Excel, the juggernaut of the engineering industry for the last 30 years, relies heavily on cell-by-cell calculations. NumPy uses vectorization to operate on entire data arrays with a single command.
It's important to clarify that you could conceptually replicate vectorized operations in Excel, but it would involve convoluted formulas or nested functions across multiple cells.
Applications of NumPy in Engineering
There are so many use cases for NumPy in engineering but a quick look at structural applications includes:
Representing Structural Elements: NumPy arrays conveniently store material properties (modulus of elasticity, Poisson's ratio), beam cross-sectional dimensions, and nodal coordinates.
Loads and Boundary Conditions: Vectors and matrices represent applied forces, moments, and displacement constraints.
Stiffness Matrices: NumPy's linear algebra capabilities are crucial in assembling and manipulating stiffness matrices for structural systems.
Solving for Displacements, Stresses, and Strains: NumPy's efficient solvers handle systems of linear equations to obtain structural responses.
Post-processing: NumPy aids in calculating stresses at various cross-sections and visualizing deformed shapes.
Here’s a quick example using NumPy to perform the calculation across multiple points, y, along the height of the beam when checking for bending stress.
We'll calculate the bending stress using the formula:
Where:
σ is the bending stress,
M is the moment at a cross-section due to the applied load (for simplicity, we'll assume a value),
y is the distance from the neutral axis (we'll consider an array of values to represent different points across the height of the beam),
I is the moment of inertia of the beam's cross-section (we'll assume a value).
‘y’ is vectorized as an array to represent positions above and below the beam’s neutral axis, calculates the bending stress at these points using NumPy's efficient array operations, and prints the stress distribution.
Output is:
Bending Stress (N/mm^2) at different y positions (mm):
At y = -150 mm: -2.5000 N/mm^2
At y = -100 mm: -1.6667 N/mm^2
At y = -50 mm: -0.8333 N/mm^2
At y = 0 mm: 0.0000 N/mm^2
At y = 50 mm: 0.8333 N/mm^2
At y = 100 mm: 1.6667 N/mm^2
At y = 150 mm: 2.5000 N/mm^2
Think of how you would do the same thing in Excel; there would be quite a few cells involved in addition to dragging formulas around.
Matplotlib: Visualizing Data in Engineering
Matplotlib is Python's premier data visualization library, enabling engineers to create high-quality graphs and plots. This versatile tool simplifies producing static, animated, or interactive visualizations. Matplotlib offers a wide range of plot types and integrates seamlessly with NumPy and Pandas, making it a cornerstone of the Python data science ecosystem.
Seaborn, another fantastic visualization library and one of my favourites, is built on Matplotlib.
Matplotlib Key Features
Diverse Plot Types: Matplotlib supports line plots, scatter plots, bar charts, histograms, 3D plots, and more, catering to various engineering visualization needs.
Customization: Engineers can tailor virtually every plot aspect, from colours and line styles to axis labels and annotations. This allows for the creation of precise and informative visualizations.
Integration: Matplotlib works seamlessly with NumPy for data manipulation and Pandas for data handling, streamlining visualization within engineering workflows.
Subplots: Easily create multi-panel figures for comparing different aspects of your data, enhancing analysis and interpretation.
Matplotlib Engineering Applications
Matplotlib's ability to translate complex data into clear visuals makes it invaluable in engineering contexts. Here are some common uses:
Visualizing Analysis Results: Plot pressure diagrams, bending/shear plots, rating curves, structural deflections, and more.
Understanding Trends: Explore relationships within data, aiding analysis and decision-making.
Communicating Insights: Share findings with colleagues or stakeholders using clear and informative plots, simplifying the communication of complex engineering concepts.
Exploring the Extensive Library Ecosystem in Python
Python's vast library ecosystem offers engineers a wealth of pre-built tools to accelerate their work. Understanding how libraries and packages are organized is a fundamental skill that will unlock their full potential within your engineering projects. It opens the doors to:
Efficient Problem Solving: Knowing how to import the right components from a library allows you to leverage its functions and classes directly, saving you time and effort from reinventing solutions.
Streamlined Workflows: Grasping the relationship between different modules within a library helps you build modular, reusable code for complex engineering tasks.
Effective Collaboration: A shared understanding of libraries and packages fosters clear communication and collaboration within engineering teams.
Flocode's Approach
Flocode's curriculum dives deep into the concepts of libraries and packages. You'll learn how to:
Install and manage essential libraries for your projects.
Import specific modules and their contents.
Navigate the internal structure of complex libraries.
Write your own custom modules and packages for long-term reusability.
The Python Ecosystem
There are over 137,000 Python libraries out there.
Python Libraries for Civil and Structural Engineers
I've categorized the libraries into several key areas to organize the list by topic and provide structure for readers. We can update this as we move forward.
It is important to note that I have not used all of these libraries, but this will serve as a working list moving forward. I’ve added a coveted ♥ of approval for the libraries I use the most, for whatever that’s worth.
Numerical and Scientific Computing
NumPy: Advanced mathematical functions, array operations. numpy.org ♥
SciPy: Scientific and technical computing. scipy.org ♥
SymPy: Symbolic mathematics. sympy.org
Jupyter Notebook: It's not a library but interactive computing. jupyter.org ♥
Data Manipulation and Visualization
Pandas: Data manipulation and analysis. pandas.pydata.org ♥
XLwings: Control Excel from Python. xlwings.org ♥
Pyexcel: Spreadsheet manipulation. Pyexcel Docs
Matplotlib: 2D plotting library. matplotlib.org ♥
Seaborn: Statistical data visualization. seaborn.pydata.org ♥
Plotly: Interactive plotting. plotly.com ♥
Bokeh: Interactive visualization. bokeh.org
Dash: Web applications. Dash by plotly.com
PyVista: 3D plotting and mesh analysis. pyvista.org
Structural Analysis
Pynite: Simple finite element analysis. Pynite docs ♥
PyAnsys: Many Python packages for using Ansys. PyAnsys Docs ♥
OpenSees: Finite element tasks. OpenSees Docs ♥
PlaneSections: Finite element beam bending. PlaneSections
sectionproperties: Cross-section analysis. sectionproperties Docs
comtypes: CSI SAP2000 and Etabs manipulation. PyPI - comtypes ♥
Anastruct: 2D frame analysis. GitHub
PyCBA: Fast linear elastic analysis of general beam configurations. PyPI - PyCBA
PyAbaqus: Control and scripting for Abaqus. PyAbaqus Docs
IndeterminateBeam: Indeterminate beam solver. GitHub
StruPy: Structural engineering design. Bitbucket
3D Modelling and CAD
Compas: A python framework with many tools for computational design, including Blender, Grasshopper, Rhino and more. compas.dev
Blender API: Excellent tool for controlling and extracting data. Blender API Docs
BlenderBIM IFC API: Manipulate and control BlenderBIM. BenderBIM_shell Docs
pyRevit: Rapid prototyping API for Revit. pyRevit
pyautocad: COM for controlling Autocad. pyautocad Docs
rhinoscriptsyntax: Scripting engine for Rhino. GitHub
FreeCAD API: Scripting and extending FreeCAD capabilities. FreeCAD Docs
Geotechnical Engineering
Groundhog: Geotechnical calculation library. Groundhog Docs ♥
pySlope: Slope stability analysis. GitHub
PyAnchor: Soil anchor design. Github
FoundationDesign: Foundation analysis and design. GitHub
LiquPy: Liquefaction analysis using Python. Github
Geotecha: Tools for geotechnical engineering analysis. PyPI - Geotecha
ObsPy: Python framework for processing seismological data. GitHub - ObsPy
Hydrotechnical Engineering
fluids: Fluid dynamics library. PyPI - fluids ♥
ChannelFlowlib: Open Channel flow solver. GitHub - ChannelFlowlib ♥
HecPy: HEC-RAS hydraulic modeling tools. PyPI - HecPy.
Hydrostats: Hydrological data analysis and statistics. PyPI - Hydrostats.
PyHSPF: Hydrological Simulation Program Fortran (HSPF) modeling. PyPI - PyHSPF.
Hydroengine: Hydrological and environmental data analysis tools. GitHub - Hydroengine.
Geographic Information Systems (GIS)
PyQGIS: The Python API for QGIS. PyQGIS Developer Cookbook
GeoPandas: Extends pandas for spatial data operations. GeoPandas
Shapely: Manipulates and analyzes planar geometric objects. PyPI - Shapely
Folium: Creates interactive maps with Python, integrating Leaflet.js. PyPI - Folium
Unit and Calculation Tools
forallpeople: Python SI units library. GitHub
Handcalcs: Python calculations into rendered LaTeX. GitHub ♥
Tabulate: Pretty-print tabular data. PyPI - Tabulate ♥
Machine Learning
TensorFlow: Machine learning and neural networks. tensorflow.org
Scikit-learn: Machine learning algorithms. scikit-learn.org ♥
Keras: High-level neural networks API. keras.io
PyTorch: Machine learning library. pytorch.org
OpenCV: Computer vision and image processing. opencv.org
Web Development and API Tools
Solara: Python web framework for reactive web apps. solara.dev ♥
Streamlit: Create apps and dashboards easily. streamlit.io ♥
Flask: Lightweight web development framework. palletsprojects.com ♥
Django: High-level Python web framework. djangoproject.com
FastAPI: Create web APIs with Python. fastapi.tiangolo.com ♥
Litestar: Lightweight API builder. litestar.dev
Others
Beautiful Soup: HTML and XML parsing. Beautiful Soup Docs ♥
SQLAlchemy: Database toolkit. sqlalchemy.org
PySpark: Big data processing. spark.apache.org
Pydantic: Data validation. Pydantic Docs ♥
PyTest: Testing framework. pytest.org
Flocode Community Suggestions
rhino3dmpy: Geometry manipulation for Rhino 3D. GitHub
Pint: A very useful unit conversion tool. Pint Docs
ak_sap: A Python wrapper to control SAP2000 FE models. GitHub (One to watch 👀)
PyTekla: A thin Python wrapper around the .NET Tekla API. PyTekla Docs
PyAutoGUI: Automate interactions with other applications. PyAutoGUI Docs
For those of you who persevered this far with unwavering focus, you are rewarded with this link to my Notion Database of these libraries; feel free to bookmark or duplicate it for your own use. 👍
Don’t see a library that should be here? What am I missing? Something glaringly obvious, I’m sure. Let me know; I’d like everyone’s help in building this list.
I’m working hard on the flocode courses; you can expect some new info soon on the Flocode Beta Program.
Sign up if you’re not already on the waitlist. I’m looking forward to finally speaking to some of you!
A new podcast episode is coming soon with Tim Rawling, co-founder at CalcTree, a new engineering calculation platform with many cool features, including Python capability. We had a great discussion and covered a lot of ground, so stand by for that.
See you in the next one!
James 🌊