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.

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

With the

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

This compares favorably to the course notes:

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

*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

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

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

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

*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

**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

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

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'

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

DeleteCheers,

Jason

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

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

Deletehttps://github.com/akaszynski/pyansys/blob/61790110ed14a69403457bc6ae26b04de55424b0/pyansys/archive_reader.py

Hi Jason,

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

Thanks

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

DeleteHi Jason,

ReplyDeleteI 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

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.

DeleteOkay it works now, thanks again !

DeleteHi dear Jason

ReplyDeleteI'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 💐

These commands should work:

Delete*SMAT, MatM, D, import, full, file.full, MASS

*PRINT, MatM, MatM, txt

Thank you so much ,That worked.

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

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.

DeleteFor 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.'

Deletehello sir,

ReplyDeleteThere 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

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

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

DeleteIt should work as well for Ansys 19+. The WRFULL,1 should create the *.full file.

DeleteIf you have multiple *.full files, perhaps look at DMPOPTION to combine them into one.

It seems it get stuck at

ReplyDelete*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?

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.

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

DeleteHi Jason,

ReplyDeleteThank 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

By original DOF I mean the user DOF in APDL

ReplyDeleteZZ

Hi ZZ,

DeleteFrom 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

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?

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

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

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

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

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

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

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

Delete