Series: How to code for structural engineering - Part 2
Writing your first structural engineering program
In this second tutorial, we are going to begin writing the code for our first structural engineering program. If you missed Part 1 of the series, you can find it here.
To begin, we’ll need to use our structural engineering knowledge. We need to know the density of steel used for reinforcement. The density of steel will typically depend on the concrete standard and unit system you are using. I’m working in Australia and will be using the AS3600 concrete standard with SI units, which specifies that the density of steel for reinforcement is 7850 kg/m^3.
If you are working with a different standard and unit system, I encourage you to use the density of steel you typically use.
Step 1: Storing required data
To start off, we need to store the density of steel data in our program. To do this, we assign (or initialise) the density of steel to a variable in Python as below. Note we’ll need to clear the code we wrote in Part 1 and start fresh.
Variables in Python are named using snake_case. This means you should use lower case characters and join your names with underscores. No spaces are allowed.
Note: In the above code I write a comment. Comments are lines of text that are ignored when you run your program. If you want to write a comments you need to start the line with a hash (#).
I highly recommend that when you are first learning to code that you write a comment above each line of code. It should state what you want to achieve in the following line of code below. This will help you break down your steps and make writing each line of code easier.
When moving from structural engineering mathematics to Python programming, the variable assignment syntax above can seem familiar but there are a number differences that are worth noting.
For now we’ll note just one and come back to the others as we go:
the order of the statement above is not reversible. The left hand side of the equals sign must always be the variable (
steel_density
), and the right hand must always be the value (7850
).
Once we’ve initialised our steel_density
variable, we can confirm that the value we assigned has been stored by printing it to the output:
Printing out variables is a simple and intuitive testing or debugging method when you are first learning to program. It allows you to check that your code is working as expected or understand why it’s not.
Note: Testing code is an important part of writing code that is highly relevant to structural engineering programming. If you are relying on the programs you write to perform structural calculations you need to be confident that your code is bug free. Testing can provide that confidence. We’ll come back to testing in a later series.
We need just one other variable for our program: the bar_diameter
. Unlike our steel_density
variable this is an input into our program, meaning that we expect it to change. For now we’ll hard-code a value to our variable and revisit this in the next tutorial. I’m going to select 20mm which is a standard reinforcement bar size in Australia. I’ll add the variable above our steel_density
variable.
You may have noticed that, unlike the “hello world!“
text we wrote in our first program, we are not encasing the number values we assign to our variable in quotes: (“hello world!“ vs 20 and 7850)
. This brings us to the second important note relating to our variable assignment syntax:
Python treats text and numbers differently. Text is encased in
“double”
or‘single’
quotes. In programming we refer to text wrapped in quotes as a string. Numbers on the other hand are not wrapped in quotes. Integer numbers are referred to as Ints and numbers with decimal places are referred to as Floats. Strings, Ints and Floats are all examples of data types. This distinction allows Python to understand that we want to perform calculations on variables that have been assigned an Int or Float, but not those that have been assigned a String.
Understanding data types is a complex part of learning to code. It becomes easier to understand when we focus on practical implementation. Fortunately the next step in building our program requires us to execute a calculation. We need to know the cross-sectional area of our steel reinforcement bar.
Step 2: Writing our first calculations
While we could complete this calculation in a single expression, we’ll write it out in multiple steps.
First, we need to calculate the radius from the diameter we’ve input. Then we’ll use the area of a circle ( A= πr^2 ) to calculate the cross sectional area of our reinforcement bar.
Note: If you are programming structural engineering calculations, no matter your level of experience, you should always try to split out your calculations into as many simple steps as possible. This is because:
it allows us to test our code much more easily than having complex mathematical steps
it improves our ability to debug the code by making it easier to isolate the error and
it improves the readability of our code, making it easier for other engineers to review.
With that in mind, let’s write our first calculation in Python.
We can do this by creating a new variable called bar_radius
and initialising it with a value equal to bar_diameter
divided by 2.
Python supports an exhaustive range of mathematical functions. To find the correct symbol for the mathematical operation you’d like to perform, I’d recommend googling it.
Note: Googling for answers to software development questions is an essential skill for programmers. For those interested in more advanced mathematical operations for structural engineering, like numerical methods and calculus, I’ll cover this in a future series.
Next, we’ll need to calculate the cross_sectional_area
of the bar. For this we’ll need to find the Python symbol for the exponent or power operator and the constant Pi.
If you haven’t already seen it in the gif, I challenge you to try googling for the correct answer! Bonus points if you find out how to write Pi, which is a little tricker.
In Python, Pi (π) and other mathematical constants are stored in the math module. To use a module you first need to import it with the special import
keyword. To access a constant from the module, you write the module name (math
) followed by a dot (.
) then the constant you need (pi
). The math module is essential for structural engineering software developers.
We are now getting close to the last step in our calculation, but you might have noticed we are working in different units. Our density is in kg/m^3 but our bar diameter is in mm.
Step 3: Converting units (optional)
We could change our input to ensure our program only considers a single unit, but in Australia we typically specify reinforcement bar sizes in mm. So let’s instead convert the units within our program. If you don’t require a conversion with the units you are using you can skip this line of code!
Note that we are performing the conversion on the bar_dimeter
rather than the cross_sectional_area.
I recommend ensuring your conversation steps are performed on base units rather than derived units, for the same reason as writing out your code in the simplest steps: it’s easier to test and debug.
Step 4: Bringing it together
We can now perform the final step in our calculation. We can multiply the steel_density
(kg/m^3) by the cross_sectional_area
(m^2) to find the bar_linear_mass
(kg/m)(linear mass density)
.
We can now print the result out and run the program!
You can check if the program is working correctly with a hand calculation, or using this lookup table for the linear mass available here.
You can further test the calculation on different reinforcement bar sizes, but you’d have to change the value assigned the bar_diameter
parameter each time. This means constantly updating raw code, which exposes the possibility of accidentally editing a calculation step.
This isn’t great for structural engineers who aren’t now super-star coders like yourself who want to use your program. There’s also a risk that other engineers may change your code. So in part 3 we are going to remove our hard-code value and develop our full command line program.
How did we go in this part of the series? Let us know below!