OpenSees Cloud

OpenSees AMI

Getting the Number of Nodal DOFs

Original Post - 25 Nov 2024 - Michael H. Scott

Visit Structural Analysis Is Simple on Substack.


Do you ever get tired of typing a lot of zeros, e.g., to apply a 20 kip load in the X-direction at node 18 on a 3D frame model?

ops.timeSeries('Linear',1)
ops.pattern('Plain',1,1)
ops.load(18,20,0,0,0,0,0)

I know what you’re thinking. Did I put five zeros after the 20 kip load or only four? Well, to tell you the truth, in all this excitement, I’ve kinda lost track myself. (Riffing off of Dirty Harry).

Well, there’s a better way using the oft-overlooked and mostly unknown function getNDF(), which returns the number of nodal DOFs currently stored in the model builder.

First create a Python list of all zeros, then insert the 20 kip load in the list’s first entry (for DOF 1) and pass that list (de-referenced) to the load command.

NDF = ops.getNDF()[0] # Returns a list

ops.timeSeries('Linear',1)
ops.pattern('Plain',1,1)
P = [0]*NDF # All zeros
P[0] = 20 # DOF 1
ops.load(18,*P)

Defining boundary conditions is another useful application. Instead of typing a lot of zeros and ones, define lists for specific boundary conditions like pinned or fixed, then use getNDF along with its companion function getNDM, which returns the number of model dimensions.

NDM = ops.getNDM()[0]
NDF = ops.getNDF()[0]

pinned = [0]*NDF
pinned[:NDM] = [1]*NDM
fixed = [1]*NDF

ops.node(1,0,0); ops.fix(1,*fixed)
ops.node(2,100,0); ops.fix(2,*pinned)

Using variables in this manner cuts down on mistakes when defining 3D models that can easily go between standard frame elements (NDF=6) and warping elements (NDF=7). There’s definitely other cases where lists of arbitrary length mitigate errors in defining a model.