Skip to main content

Export Stiffness Matrix from Ansys

It is sometimes useful to extract the mass and stiffness matrix from Ansys.

    *SMAT,MatK,D,IMPORT,FULL,file.full,STIFF
    *PRINT,matk,matk,txt

The above script uses APDL Math to get the job done. The ordering of the matrix is unfortunately not concurrently exported. To verify the sequencing is as expected, we will work to replicate a truss example in the Finite Element Trusses course notes by Bob Greenlee.
Figure 1: Truss Problem Setup

Model Creation
Script to create model:
/prep7
!! Creates Model to reflect course notes
! Properties
et,1,1 
mp, ex, 1, 29.5e6
r, 1, 1

! Geometry
n,1 $ n,2, 40 $ n,3, 40, 30 $ n,4, 0, 30
e,1,2 $ e,2,3 $ e,1,3 $ e,3,4

! Boundary Conditions
d,1,ux,0 $ d,1,uy,0
d,2,uy,0
d,4,ux,0 $ d,4,uy,0
f,2,fx,20e3
f,3,fy,-25e3

! solves
/solu
eqslv, sparse
wrfull, 1
solve
finish
save

Extract & Export Stiffness Matrix
With the file.full file of the FEM model created, let's now export the stiffness matrix to both a text file and MMF file format.

! Gets Stiffness Matrix
*SMAT, MatK, D, import, full, file.full, stiff
*SMAT, Nod2Bcs, D, import, full, file.full,NOD2BCS
*print, MatK, matk.txt ! Exports Stiffness to Text File
*export, MatK, mmf, matkMMF.txt ! Exports Stiffness as MMF format

Here's the MatK matrix printed out. Note it is is both sparse and assumed symmetric.
Figure 2: Stiffness Matrix

This compares favorably to the course notes:
Figure 3: Course Notes Values

DOF Ordering
Despite having 4 nodes with UX and UY (total of 8 DOF), there are only 3 DOF here because the boundary condition equations were removed leaving DOF # 3, 5 and 6. In this case the ordering was in sequence but the Degree of Freedom Ordering Documentation states that the ordering may be optimized for solving.

The node numbering we use in APDL is User Ordering. To convert that to Internal Ordering that removes unused node number and optimized reordering, we use the FORWARD nodal vector mapping. Here's my interpretation of mapping the 4 sequential nodes into Internal Ordering.

*VEC,MapForward,I,IMPORT,FULL,file.full,FORWARD
*dim, Xint,, MAPFORWARD_ROWDIM*MAPFORWARD_NUMDOF
cto=0
*do, EUnodeNumber, 1, MAPFORWARD_ROWDIM
   *do, dofdir, 1, MAPFORWARD_NUMDOF
      j = MapForward(EUnodeNumber)
      cto=cto+1
      Xint(cto) = (j-1)*MAPFORWARD_NUMDOF+dofdir
   *enddo
*enddo

Next, another mapping vector was multiplied to get BCS Ordering  from Internal Ordering.
*VEC, Xxint, d, import, apdl, Xint ! from previous equivalence
*MULT, Nod2Bcs,, Xxint,, Xbcs    ! X of BCS ordering
*PRINT, Xbcs  ! Indexing of BCS (see output window)

That gets us the Xbcs ordering (Figure 4) which reflects the DOF subscript in Figure 3.
Figure 4: Xbcs index

Solving for Displacements
To continue solving the FE problem in APDL Math, the equations are solved with results identical to the course notes:
*VEC,vRHS,D,IMPORT,FULL,file.full,RHS
*LSENGINE,BCS,MyBcsSolver,MatK
*LSFACTOR,MyBcsSolver
*DMAT,VecX,D,COPY,vRHS
*LSBAC,MyBcsSolver,vRHS,VecX 
*PRINT, VecX

Figure 5: Solved Displacement

Conclusions 
APDL Math allows us to manipulate the internal matrix and vectors quite effectively once some familiarity was established. Depending on the task at hand, it may be worth while doing the calculation inside Ansys with APDL Math instead of exporting and calculating it externally in Matlab/Octave or Python etc.

Speaking of Python, I came across pyansys by Alex Kaszynski which exports the K and M matrix and it's corresponding node and degree of freedom. Unfortunately my attempt had an error of ValueError. If anyone else is able to get it to work, please let me know in the comments section. Thanks!

Addendum 
APDL script of the above project: WriteStiffness.inp

Comments

  1. pyansys seems to be very limited at this stage but I do realize it's future potential. It can only deal with the following element types- '45', '95', '185', '186', '92', '187'

    ReplyDelete
    Replies
    1. Thanks Abhijeet! That's good to know. Could you share a link that lists it's limitation?


      Cheers,
      Jason

      Delete
    2. I was going through the pyansys python code listed at github.

      Delete
    3. Thanks for the pointer. Here is the link if others are interested:
      https://github.com/akaszynski/pyansys/blob/61790110ed14a69403457bc6ae26b04de55424b0/pyansys/archive_reader.py

      Delete

Post a Comment