# Principal Stresses in Elasticity

In structural engineering we are often interested in minimum and maximum principal stress. In NGSolve we are able to calculate the eigenstress by something like (assuming stresses to be a 3x3 matrix valued coefficient function):

``eig_stresses = stresses.eig()``

Then the first 9 entries of correspond to the stress eigenvectors and

`eig_stresses, eig_stresses, eig_stresses` are the eigenvalues. However, it seemed to be that the order of the eigenvalues is not fixed. Is there an efficient procedure to order them into maximal, middle and minimal principal values?
In 2D I could use

``max_princ = IfPos(eig_stresses-eig_stresses,eig_stresses,eig_stresses)``

but in 3D this is very inefficient due to the need of multiple comparisons.

Best regards,
Nils

Hi Nils,

using staggered IfPos commands the comparisons can be written relatively compact for the 3D case:

``````max_princ = IfPos(eig_stresses-eig_stresses,
IfPos(eig_stresses-eig_stresses,eig_stresses,eig_stresses),
IfPos(eig_stresses-eig_stresses,eig_stresses,eig_stresses))

min_princ = IfPos(eig_stresses-eig_stresses,
IfPos(eig_stresses-eig_stresses,eig_stresses,eig_stresses),
IfPos(eig_stresses-eig_stresses,eig_stresses,eig_stresses))

middle_princ = eig_stresses+eig_stresses+eig_stresses - max_princ - min_princ``````

Best,
Michael

Your solution works but for larger 3D models it is very slow.

It would be great if there is a solution which works faster and is implemented in the ngsolve environment. E.g. having .eig() already sorted or another option method .sortedEig()

Did you try to compile ‘max_princ’, eg. via

``max_princ.Compile()``

or the actual expression in which max_princ is used (‘Compile’ works recursively)?

If this is not done, the full Eigensystem will be re-evaluated multiple times with the same outcome, which is quite inefficient.

Thank you very much Matthias.
I did not set the .Compile() in the Draw function, now it’s much faster.