Following a recent comment this post looks at the code for setting up the stiffness matrix for a 3D frame, and how to step through any selected part of the code.
The general procedure for setting up a 3D stiffness matrix, and rotating it to the global coordinate system is given at the link above. The 3DFrame spreadsheet (including full open-source code) can be downloaded from:
The main program for the 3D analysis is the sub-routine p44, found in the mFrameP44 module in the Visual Basic Editor (VBE), which can be accessed by:
- From the spreadsheet, press Alt-F11, or click the Visual Basic icon on the Developer tab*
- Double-click the mFrameP44 module in the Project Explorer window.
- Click the right-hand dropdown box above the editor window, and select P44
* If the Developer tab is not visible, go to File-Options-Customize Ribbon, and select it as one of the main tabs.
The code for setting up the global stiffness matrix is:
' !---------------------global stiffness matrix assembly------------------------ elements_2: ' Range("kga").ClearContents For iel = 1 To Nels For i = 1 To nod Num(i) = g_num(i, iel) For j = 1 To NDim Coord(i, j) = g_coord(j, Num(i)) Next j Next i ReDim km(ndof, ndof) i = BeamConER(iel, 6) If i = 0 Then ReDim R0(1 To 3, 1 To 3) Call rigid_jointed3(km, Prop, Gamma, Etype, iel, Coord, R0) Else For j = 1 To 12 SpringA(1, j) = BeamHinge(i, j + 1) Next j Call spring_jointed3(km, Prop, Gamma, Etype, iel, Coord, R0, SpringA) End If For i = 1 To ndof g(i) = g_g(i, iel) Next i Call formkv(kv, km, g, neq, iel, g_coord, Num) If iel = Range("kmbeam").Value2 Then Range("km") = km Next iel ' elements_2
This code loops through each beam element in the frame and calls either the rigid_jointed3 or spring-jointed3 routines for beams with fixed or spring end conditions respectively. The 12×12 matrix returned for each beam is then added to the global stiffness matrix with the formkv routine.
To step through the code in the p44 routine simply make sure that the cursor is somewhere within the body of the routine and press F8, or Debug-Step Into. To run the routine to a selected point either:
- Set a breakpoint at the required location by clicking in the left hand margin, then press F5 or Run-Run or
- Select the line to run to, then press Ctrl-F8 or Debug-Run to cursor.
There is also a separate function, rigid_jointed3TT, that can be called from the spreadsheet for a specified beam, and returns the beam 12×12 stiffness matrix to the spreadsheet as an array function. The instructions below show how to step through this function line by line.
To step through a VBA function it must either be called from a sub-routine, or initiated from the spreadsheet. To do the latter:
- Create a breakpoint somewhere within the VBA code by clicking in the left-hand margin.
- Select a cell in the spreadsheet that calls the chosen function. The screenshots below use cell C35 in the Matrix examples spreadsheet.
- Press F2 (edit), then either enter for a function that returns a single value, or in this case Ctrl-Shift Enter to return an array. The function will then run to the selected breakpoint:
You can now step through the code one line at a time by pressing the F8 key (or Debug-Step Into). Note that the Locals Window, under the main Editor window displays the status and value of all variables, and you can also see the value of a variable by simply placing the cursor over the variable name anywhere in the code:
Procedures for stepping through the code are then the same as for a sub-routine. To run to a selected line press Ctrl-F8 or right click and Run to cursor:
At the end of the function it will either return the end result (the km2 array), or one of the four intermediate arrays, depending on the value of the Out argument:
The Debug menu provides further options, for example allowing control to jump directly to a specified point, or for called routines to be run in a single step.
For more details of VBA debugging options see Chip Pearson’s Debugging VBA.