# 40. Building the primary stiffness matrix

Questions and discussion for this lecture live here. Fire away by hitting Reply below Hey Sean, i’m struggeling to understand the indeces step (11’ in the video) i think my issue that i’m not visualizing what we are trying to do and I’m tripping on why we are accessing 0,1 by 0 , 2 not by 1,2 in python. i’m also not getting the arrangment for kp[ia:ib+1, ia:ib+1] = kp[ia:ib+1, ia:ib+1] +k11 on the logic of the arrangement i think this also goes back that i’m not able to visualise it

Hey @MarioSamaan - the good news is that once you’ve fully understood this, you’ve basically unlocked the core idea behind the direct stiffness method!

So there’s two parts to understanding what we’re doing here when we programmatically build the primary stiffness matrix:

1. You need to review lecture 21 where I explain how the element stiffness matrices are ‘plugged into’ the primary stiffness matrix for the structure.
2. Once you’ve understood that - it’s just a matter of understanding how we perform this exact same process in Python. This boils down to understanding how indexing works in Numpy/Python.

What catches most people out with Python indexing is:

• indexing starts at 0 (not 1)
• to index a range of values we need to include ‘one past’ the location we want.

So, for example, if I have an array of 5 numbers

``````array = [1,3,7,4,9] #Some array of five numbers
``````

Now, say I want to select the first 3 elements as a subset, I would index as follows:

``````array[0:3]
``````

This selects elements 0,1,2.

Once you understand this concept, you can start to index into a 2D array (like we do in the lecture). If we want to select a subsection of a 2D array (rather than a 1D array in the example above), we index into the rows and columns - think about it as selecting the relevant rows and relevant columns for your selection - in doing so, you’re extracting all of the elements at the intersection of the rows and columns. It may help you to draw this on a sheet of paper.

Let me know if this hasn’t helped and you’re still confused - we’ll tease it out a little more.

S

Hey Sean,

Thank you for clarifying the Python indexing. I revisited Lecture 20, which enhanced my grasp of the Primary stiffness matrix assembly process. However, I’m still encountering difficulty understanding how we’re identifying the rows and columns of `kp` in the expression: ```
kp[ia:ib+1, ia:ib+1] = kp[ia:ib+1, ia:ib+1] + k11

``````

also in the above example you sat [0:3] should yield  0,1,2 this refer to the sequence and this hould yield 1,3,7 if i understand you correctly``````

Think of the process like a breadcrumb trail, i.e. one piece of info leads us to the next, and then the next, so…

In the `addElement` function we take in the node numbers for the element, i.e. the node numbers at each end of the element, `node_i` and `node_j`.

We use these to identify the horizontal and vertical degrees of freedom at each node:

``````ia = 2*node_i - 2 #index of horizontal DoF at node_i
ib = 2*node_i - 1 #index of vertical DoF at node_i
ja = 2*node_j - 2 #index of horizontal DoF at node_j
jb = 2*node_j - 1 #index of vertical DoF at node_j
``````

Note that I’m calculating the index of each degree of freedom, so DoF 1 and 2 would occupy indices (or locations) 0 and 1 in a python array/matrix…remember indexing starts at 0 in Python.

So, by calculating `ia`, `ib`, `ja` and `jb`, I’m actually calculating the location or address within the primary stiffness matrix of the degrees of freedom for the member in question.

The best way to really get this clear in your mind is to write the code for `addElement` function out on paper, but put numbers in for `node_i` and `node_j`. Then just follow the logic and it should all become alot clearer - especially when you layer it on top of revisiting lecture 20.

S

1 Like

thanks it is clear now i had to write the full matrix down to get it i think it was not clear to me that we are identifying arrays within the matrix and once we arrive to the right array using we run our function and assemble it.

1 Like