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.
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.
This compares favorably to the course notes:
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.
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
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
*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
finishExtract & 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?
Deletethank 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
DeleteOne 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.
DeleteHello all,
ReplyDeleteDoes 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
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.
DeleteHi Jason,
ReplyDeleteI'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
My guess is your loads are applied differently with quadratic elements. Perhaps look into how the mid-side nodes constraints are applied.
DeleteBest,
Jason
Hi Jason,
ReplyDeleteThanks 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
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
DeleteBest,
Jason
Hi Jason,
ReplyDeleteI'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?
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.
ReplyDeleteThanks 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?
DeleteTry 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.
DeleteHi Jason,
ReplyDeleteI have been attempting to perform a transient analysis in MATLAB using a stiffness matrix and a mass matrix extracted for modal analysis, both of which are sized 2800x2800. However, I've noticed that about 70% of the displacement values among the 2800 continually appear as zeros. This issue persists even when conducting a static analysis. Could this result be due to a discrepancy between the node ID and the Degrees of Freedom (DOF) in the stiffness matrix?
When I exported NOD2BCS for reordering, it progressed in a manner such as [1,1] = 1, [2,1] = 2, ..., [2820,1] = 2820, excluding only the fixed nodes without any mixing in the order. Could you explain what the left and right sides of NOD2BCS represent? How should I go about reordering a stiffness matrix of this size?