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

Exporting mass matrix would be similar:
    *SMAT, MatM, D, import, full, file.full, MASS

The above script uses APDL Math to get the job done. (Please see previous post for another example). 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:
!! Creates Model to reflect course notes
! Properties
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,4,ux,0 $ d,4,uy,0

! solves
eqslv, sparse
wrfull, 1

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.

*do, EUnodeNumber, 1, MAPFORWARD_ROWDIM
   *do, dofdir, 1, MAPFORWARD_NUMDOF
      j = MapForward(EUnodeNumber)
      Xint(cto) = (j-1)*MAPFORWARD_NUMDOF+dofdir

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:

Figure 5: Solved Displacement

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!

APDL script of the above project: WriteStiffness.inp
Post on creating a user defined ansys superelement *sub: link 


  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'

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


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

    3. Thanks for the pointer. Here is the link if others are interested:

  2. Hi Jason,

    Do you have an example how APDL math can be use for parameter sensitivity study of a model ?

    1. Here is a not-so-good example for modal sensitivity:

  3. Hi Jason,

    I got an error when I want to extract the mass matrix on the same way (it says that mass is not existing) do we need to do something more ?
    Thank you

    1. You first need create the *.full where the matrix is (WRFULL). The above example here would not have mass matrix as no density was previously defined for LINK1 elements.

    2. Okay it works now, thanks again !

  4. Hi dear Jason
    I've defined density and I tried to extract mass matrix but the mass and stiffness matrix both was looks exactly like each other. Please write the mass matrix code.
    Thank you 💐

    1. These commands should work:
      *SMAT, MatM, D, import, full, file.full, MASS
      *PRINT, MatM, MatM, txt

    2. Thank you so much ,That worked.
      I have another question. I'm modeling something and after solving i extract stiffness and mass matrix and then i'm trying to model another thing but i can't extract the stiffness and mass matrix of the new model, when i run the commands of mass and stiffness matrix, ANSYS saves the previous model's mass and stiffness matrix for me!
      what should i do?

    3. Perhaps look at Multiframe restart ( Inserting your script after the first and last FINISH command in example 5.1 yields different stiffness matrix file.

    4. For the link .It shows' The page you requested is unavailable and will not be restored. For documentation please contact your software provider.'

  5. hello sir,
    There is an error when i try to run the code, it says:
    *SMA Command : Fails to open the file file.full.
    *PRI Command : Matrix/Vector MATK cannot be found
    *EXP Command : Matrix/Vector MATK cannot be found

    1. It sounds like Ansys was not able to locate file.full. Make sure you have it in the working directory before *SMAT is issued.

    2. could you please explain more? I have the same problem and I know its because I'm using the Ansys 19 version but I just can't worked out codes to creat the full file.

    3. It should work as well for Ansys 19+. The WRFULL,1 should create the *.full file.
      If you have multiple *.full files, perhaps look at DMPOPTION to combine them into one.

  6. It seems it get stuck at
    *SMAT, MatK, D, import, full, Writestiffness1.full, stiff.
    I copy the command in the inp file and paste it in mechanical APDL. However, it shows
    Import Data From File Writestiffness1.full (Format=FULL) and never finish.
    Do you have any idea why this happens?

    1. Hmm... I'm not sure why. Does running the WriteStiffness.inp from this post work for you? If so, try modifying the script incrementally to troubleshoot.

    2. After further thought... it could be due to use of distributed solver with multiple FULL files. If so, please take a look at DMPOPTION to ask solver to combine all full files and possibly COMBINE to combine the files after the fact.

  7. Hi Jason,

    Thank you very much for your wonderful post. I am very new in APDL Math. I have a very basic question.

    The exported mass and stiffness matrix only contain the active (unconstrained) DOFs, that is [1 2 3] in this example. Xbcs tells us what original DOFs those active DOFs correspond to, that is [3 5 6 ] in this example.

    If I would like to extend the matrices to the original full DOFs, what I need to do is to locate the extracted mass and stiffness terms to the matrix of full DOFs and set the values corresponding to the constrained DOFs as zeros. Is this correct?

    I would like to extract the mass and stiffness matrices of each substructure and analyze their influence to the matrices of whole structure. So I guess it would be better for me to have the matrix of the full DOFs.

    Thank you very much.



  8. By original DOF I mean the user DOF in APDL


    1. Hi ZZ,

      From the example above, the user DOF is [3 5 6] which corresponds to the q3, q5 & q6 DOF in the diagram. Other degrees of freedom are constrained, thus required in the matrix. The stiffness matrix [1 2 3] thus maps to the user DOF of [3 5 6].

      To have the 'original full' DOFs, you could try having no constraints. You can then constrain and apply loads accordingly directly on the matrix.

      Kind regards,

    2. Hi dear all, im working now to extract mass and striffness Matrix from Ansys APDL to MATLAB, i have building a wing structure with ribs and ailerons... How Can i do for extracting mass and striffness Matrix like this example plz?

    3. Please try going through the example in this post, it should help you answer your question.

  9. hello sir,I have a question:I just creat one element,and export the gloabal stiffness matrix and the element stiffness matrix ,but the two matices are not the same,do you know the reason?

    1. If you have only one element, I don't know how to select the 'element' vs 'global' as they appears to be the same to me.

    2. thank you very much for your reply !To have the 'original full' DOFs, I try having no constraints,and export the global matrix from .full file.Meanwhile,Iexport the element matrix from the .emat file with the code:
      but the two obtained matrices are different,is there anything wrong?

    3. I've not tried emat so I don't know what's the difference. Perhaps the element coordinate system might be different?

    4. I've not tried emat so I don't know. Perhaps it element coordinate system is different to global coordinate system?


Post a Comment