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:
/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

/aux2
combine, full  ! combines all file*.full into file.full
finish

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
Post on creating a user defined ansys superelement *sub: link 

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
  2. Hi Jason,

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

    ReplyDelete
    Replies
    1. Here is a not-so-good example for modal sensitivity: https://www.ansystips.com/2017/06/apdl-math-example.html.

      Delete
  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

    ReplyDelete
    Replies
    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.

      Delete
    2. Okay it works now, thanks again !

      Delete
  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 💐

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

      Delete
    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?

      Delete
    3. Perhaps look at Multiframe restart (https://www.sharcnet.ca/Software/Ansys/16.2.3/en-us/help/ans_bas/Hlp_G_BAS3_12.html#BASmultrestmap52199). Inserting your script after the first and last FINISH command in example 5.1 yields different stiffness matrix file.

      Delete
    4. For the link https://www.sharcnet.ca/Software/Ansys/16.2.3/en-us/help/ans_bas/Hlp_G_BAS3_12.html#BASmultrestmap52199 .It shows' The page you requested is unavailable and will not be restored. For documentation please contact your software provider.'

      Delete
  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

    ReplyDelete
    Replies
    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.

      Delete
    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.

      Delete
    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.

      Delete
  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?

    ReplyDelete
    Replies
    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.

      Delete
    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.

      Delete
  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.

    Best,

    ZZ

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

    ZZ

    ReplyDelete
    Replies
    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,
      Jason

      Delete
    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?

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

      Delete
  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?

    ReplyDelete
    Replies
    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.

      Delete
    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:
      *dmat,element_k,D,import,emat,jobname.emat,stiff,elemnuber
      *print,element_k,out.txt
      but the two obtained matrices are different,is there anything wrong?

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

      Delete
    4. thank you very much ,dear Jason,in fact I want to export the element stiffness matrix k and the element displacement vector u ,and calculate the strain energy by u'ku,but I don't know the stiffness matrix is matching the displacement results I exported or not,I can export the element stiffness matrix from .emat file and by the code:/output,elemstiff,txt /debug,-1,,,1 ,but the obtained elemant stiffness matrices are all different,I don't know the differnce between the two method

      Delete
    5. One suggestion would be to build a simple model with a few elements and known hand calculation solution. That way, you could try the different methods to see what is correct.

      Delete
  10. Hello all,

    Does anyone know how can i get the mass matrix from ANSYS before it is reordered internally?

    I would like to get the matrix entries corresponding to the nodes numbers, i.e, for a solid 3d element, the first three entries of the mass matrix corresponding to element 1 and so on.

    Thank you very much,

    Pedro

    ReplyDelete
    Replies
    1. The few cases I done seems to show it's originally in the right order but that may be a fluke. Not sure how else to do it.

      Delete
  11. Hi Jason,

    I'm trying to export a stiffness matrix for some substructure using method you described and then to solve equation system U = K\F in Matlab. For linear elements SOLID185 is working fine and it gives almost same results as for a full model in Ansys. But for quadratic elements SOLID186 the solution in Matlab provides huge differencies comparing to the full model in Ansys. Do you know why could this happen or how to use the stiffness matrix with quadratic element functions correctly?

    Best regards,
    Serhii

    ReplyDelete
    Replies
    1. My guess is your loads are applied differently with quadratic elements. Perhaps look into how the mid-side nodes constraints are applied.

      Best,
      Jason

      Delete
  12. Hi Jason,

    Thanks for all your post. Could you please again upload on your blog this documentation ''Degree of Freedom Ordering Documentation'', as right now I'm not able to access it.

    Best Regards

    ReplyDelete
    Replies
    1. Unfortunately sharcnet.ca no longer hosts the help files. You will have to look in your Ansys Manual under: // ANSYS Parametric Design Language Guide // 4. APDL Math // 4.4. Degree of Freedom Ordering

      Best,
      Jason

      Delete
  13. Hi Jason,

    I'm trying to export the mass and stiffness matrices for a beam and the exported matrices are not diagonal as I would expect. I'm not sure what format the original mass and stiffness matrices for beam elements come as but I would like to export them in the lumped matrix format and consistent matrix format considering only 2 or three degrees of freedom per node and I can use calculations to validate. Can you help with this please?

    ReplyDelete
  14. It's unfortunately not very clear in the documentation in my view. I'm not sure if this is helpful but maybe you could tease out the identity of each dof by constraining one at a time (D command), then export the mass/stiffness to see which dof is missing. Doing it repeatedly might get you what you want unless the values are identical (symmetry). Maybe a non-symmetrical area moment of inertia might help. Sorry, I don't have a great answer here.

    ReplyDelete
    Replies
    1. Thanks for your reply. I have managed to extract the consistent mass and stiffness matrices for a beam 188 element using the substructure analysis on Mechanical APDL however, I find some issues with the accuracy of the matrices which affects the results I get when I try to use the matrices for modal analysis. Do you have an idea what could be the cause please?

      Delete
    2. Try doing the eigenvalue analysis in both Ansys and using APDL math (e.g. https://www.ansystips.com/2017/06/apdl-math-example.html). They should hopefully match since the matrices should should be identical.

      Delete

Post a Comment

Popular posts from this blog

ANSYS User Defined Results

There is an abundant of options in ANSYS classic when one wishes to post process results. ANSYS workbench default pull down menu post processing options are more limited but they can still be accessed via the User Defined Results. One way not commonly used but can come in handy is as follows: Zeroth: Under Analysis Settings, there is "Output Controls" where you can toggle to "Yes" what you would like to save before the solution starts. This is like OUTRES in APDL. Output Controls First: After solving the model, click on Solution in the tree to highlight it. Solution Second: Click on Worksheet in the toolbar. Worksheet Third: In the worksheet, you will see list of results that are saved. Right click on it to create the User Defined Results. Create User Defined Results So here we have it. You could of look up the different expressions in the help document but I find this method of accessing the results convenient.  Example: Aspec

ANSYS APDL Syntax Highlighting editor

Notepad++ with APDL User Defined Language The editor of my choice is Notepad++  with the available User Defined Language Files for APDL . You can install it without administrative privileges via the zip file. The best part of it is, it's FREE! After installing Notepad++, go to "Language>Define Your Language..." then "Import" the XML file downloaded from the above link. Remember to restart Notepad++ so that the language changes will take into effect. Opening up any *.inp or *.ans files should automatically switch highlighting to APDL. I made some minor edits. Here's my XML file: LINK . I also heard Sublime Text and  Ultraedit  has more advance features but they aren't (totally) free.