Series: How to code for structural engineering - Part 1
Getting set up for structural engineering programming
In this first tutorial, we’ll code a simple calculation to teach you the basics of structural engineering programming in Python.
It will be a command line program that calculates the linear mass density (or mass per length) of a steel reinforcement bar using the bar’s diameter.
You’ll be able to replace this calculation with your own more advanced structural engineering calculations once we’ve completed the course.
We assume no prior programming knowledge for this tutorial, and it’s written with the expectation that you’ll code along on your own computer.
If you have some programming knowledge, we challenge you to try to build the program yourself!
Note: We’ll be using SI units in these tutorials, but we’ll note where units should be converted.
Command line programs are great place to to start learning programming, because they allow you to focus on the business logic of the application.
The business logic is the value a program is going to deliver. In structural engineering, the typical business logic for a program is to perform structural analysis calculations. You can think of the business logic as a specialised calculator, like the formulas you put into the cells of a spreadsheet.
Note: In future tutorials, we’ll learn how to add an interface to the command line program to develop standalone desktop applications.
Overview
When I first started programming, all code looked like this to me:
I’ve been programming for about 6 years and some code still looks like this to me.
It’s very easy to get overwhelmed when starting to learn how to code. It’s a complex and enormous field of study.
The key to avoiding getting overwhelmed is to learn in the smallest possible steps. You’ll be surprised at how quickly it all adds up and how much a small set of programming skills can add to your value as a structural engineer.
We are going to build this first program using two technologies: PyCharm and the Python programming language:
PyCharm is an integrated development environment (IDE). It’s a program that will help us write code. It handles a lot of the complex configuration setup. Some of the most complex problems in programming are related to configuration so PyCharm saves us a lot of effort!
Python is currently the most popular programming language in the world. It’s a very simple language that can also scale to handle complex problems. It’s become the language of choice for machine learning and is emerging as the language of choice amongst structural engineers:
Step 1: Install Python v3.9.5
To install Python, hop over to the official Python website and download the installer we need.
Note: We’ll be using Python version 3.9.5 for this tutorial. I selected this as it was the latest version available when I wrote it. The tutorial will work fine with Python 3.10, just note that some references might be different. Technology evolves very fast!
Once Python has downloaded, you can open the installer and complete the installation.
You can confirm that the installation is working by opening up a command line interface or CLI. Any CLI will work but if you are new to programming and using Windows, you can use the Command Prompt program which you can find in the start bar. If you are using a Mac, you’ll need to look for the Terminal program.
Once your CLI has opened, type the following command:
python3 --version
Click enter to run the command. You should see 3.9.5
appear on the next line; indicating the version of Python we just installed.
If you can see the above, congratulations! You now have Python installed on your machine! 🎉
Note: If you are having trouble at any point in the tutorial, feel free to reply to this email or write a comment on our web post. I’m happy to help out!
Step 2: Install PyCharm
Next, we’ll need to hop over to the PyCharm page and download the community version.
Once PyCharm has downloaded, run through the installer similar to our Python installation. If you are using a work computer you may need permission from your employer before installing this software.
If you have any trouble, you can follow this instruction video for help.
Step 3: Set up a PyCharm project
When you open PyCharm after it has installed, you should see the welcome screen below. Click on the New Project Button.
We’ll name name our new project BarLinearMassCalculator
with no spaces. The project name is the text after the last forward slash:
Make sure your Python Interpreter settings match what I have above.
Once the project has been created, this page will automatically open:
On the left side is the file directory. The BarLinearMassCalculator
name is the folder that contains the file we will write our code in.
On the right is the contents of the main.py
file. Right now there is a sample script but this is where we will write our Python code.
Note: To help contextualise those who are completely new to programming. Writing code is the same as writing in a simple
.txt
file or word.doc
file.
Let’s delete all the placeholder code and get to writing our very first line of code.
Traditionally the first program software developers write in a new language is the hello world program. It’s a very simple program that allows you to become familiar with a new environment. To write the hello world program in python, write print(“Hello World!)
in our main.py
file. To run the program, click the green play button in the toolbar, as shown below.
Congratulations! You just created your first Python program! 🥳
So what exactly happened when you ran the program?
At a high level, Python executes the series of commands you’ve written from the top of the main.py
file to the bottom. In the above example, we only added a single command: print()
command. This is a special built-in command that allows us to output some text. It’s useful to start with this command because it allows us to inspect the sequential execution. Write out and run the following code:
And that’s all the generic programming knowledge we’ll need!
In Part 2 of our series, we will start to write our first structural engineering program.
How did we go in this part of the series? Let us know below!