Display principal stresses
Moderator: bernd
Forum rules
and Helpful information for the FEM forum
and Helpful information for the FEM forum
Display principal stresses
I want to add a feature to the FEM workbench that displays the principal stresses of a part. The goal is to have double-headed arrows at each node (or at every x-th node) that show the direction of the selected principal stress (similar to the example in the attachment).
As far as I can tell, fem doesn't calculate or save the direction vectors for principal stresses anywhere. So my plan is to add VectorLists in
src/Mod/Fem/PyObjects/_FemResultMechanical.py and calculate and fill them in src/Mod/Fem/importToolsFem.py
Can anyone who is more familiar with the code tell me if this is a reasonable approach?
Next problem would be to write code that acutally displays the arrows.
I think possible locations would be ViewProviderFemMesh or somewhere in PostPipeline (not sure yet where and how exactly). I guess PostPipeline would be more appropriate?
Any help is much appreciated!
As far as I can tell, fem doesn't calculate or save the direction vectors for principal stresses anywhere. So my plan is to add VectorLists in
src/Mod/Fem/PyObjects/_FemResultMechanical.py and calculate and fill them in src/Mod/Fem/importToolsFem.py
Can anyone who is more familiar with the code tell me if this is a reasonable approach?
Next problem would be to write code that acutally displays the arrows.
I think possible locations would be ViewProviderFemMesh or somewhere in PostPipeline (not sure yet where and how exactly). I guess PostPipeline would be more appropriate?
Any help is much appreciated!
- Attachments
-
- p2.png (199.08 KiB) Viewed 4931 times
Re: Display principal stresses
Hello,
I'm not entirely sure about the normal result object, but if you create a Pipeline from the result it holds the principal stresses as vector field. In the Pipeline you can than visualize individual components of the vector field as well as the magnitude. So all information is there.
Now it would be very easy to add a new filter for the postprocessing pipeline which displays vectors as arrows, as this is natively supported by VTK. You can play around with this stuff in paraview to get a feeling of the possibilities, there it is called glyph:
See pictures
To make a new filter have a look at the existing ones and copy lots of stuff, see
https://github.com/FreeCAD/FreeCAD/blob ... stFilter.h
https://github.com/FreeCAD/FreeCAD/blob ... Filter.cpp
I'm not entirely sure how glyph works from vtk site, there you need to do some investigation. This sounds like a good start (and has a good explanation):
https://www.vtk.org/doc/nightly/html/cl ... ilter.html
About visualisation, the 3d Plotting is all done automatically by the FilterViewprovider after vtk filter generated the arrows. All you need to do is to setup a task panel that works for your filter in:
https://github.com/FreeCAD/FreeCAD/blob ... stFilter.h
https://github.com/FreeCAD/FreeCAD/blob ... Filter.cpp
If you need any help feel free to ask, I love to see the post processing to be extended and gladly help out!
I'm not entirely sure about the normal result object, but if you create a Pipeline from the result it holds the principal stresses as vector field. In the Pipeline you can than visualize individual components of the vector field as well as the magnitude. So all information is there.
Now it would be very easy to add a new filter for the postprocessing pipeline which displays vectors as arrows, as this is natively supported by VTK. You can play around with this stuff in paraview to get a feeling of the possibilities, there it is called glyph:
See pictures
To make a new filter have a look at the existing ones and copy lots of stuff, see
https://github.com/FreeCAD/FreeCAD/blob ... stFilter.h
https://github.com/FreeCAD/FreeCAD/blob ... Filter.cpp
I'm not entirely sure how glyph works from vtk site, there you need to do some investigation. This sounds like a good start (and has a good explanation):
https://www.vtk.org/doc/nightly/html/cl ... ilter.html
About visualisation, the 3d Plotting is all done automatically by the FilterViewprovider after vtk filter generated the arrows. All you need to do is to setup a task panel that works for your filter in:
https://github.com/FreeCAD/FreeCAD/blob ... stFilter.h
https://github.com/FreeCAD/FreeCAD/blob ... Filter.cpp
If you need any help feel free to ask, I love to see the post processing to be extended and gladly help out!
Re: Display principal stresses
Thanks for the informative reply ickby!
I dug around in the Pipeline/VTK source, yet still can't find anything related to the principal stress vectors. Only the magnitudes keep showing up. Can you please point me to the vectorfields?
Also thanks for the insight into the inner workings of Pipeline! I'm looking into it right now.
I dug around in the Pipeline/VTK source, yet still can't find anything related to the principal stress vectors. Only the magnitudes keep showing up. Can you please point me to the vectorfields?

Also thanks for the insight into the inner workings of Pipeline! I'm looking into it right now.
Re: Display principal stresses
The post processing pipeline is universal, it can work with every input. So you won't find any special field for certain quantities. The data is stored in the vtkDataObject which is hold by PropertyPostDataObject. This can hold arbitrary scalar fields, vector fields or tensor fields. In the case of mechanical simulation in freecad it holds next to other stuff the vector field "PrincipalStress". Each field has a name so you can distuinguish it. To see how to acess the data e.g. have a look here:I dug around in the Pipeline/VTK source, yet still can't find anything related to the principal stress vectors
https://github.com/FreeCAD/FreeCAD/blob ... r.cpp#L424
Now the nice thing with this system is that you implement it for a general vector field, and than it can be used for every domain, no matter which quantity it is. So electromagnetic guys, thermal etc. all can than use your arrow plots.
Re: Display principal stresses
would be a great improvementamstuff wrote: ↑Wed Sep 27, 2017 3:53 am I want to add a feature to the FEM workbench that displays the principal stresses of a part. The goal is to have double-headed arrows at each node (or at every x-th node) that show the direction of the selected principal stress (similar to the example in the attachment).
yes, this is how it works at the moment with all other resultsamstuff wrote: ↑Wed Sep 27, 2017 3:53 am As far as I can tell, fem doesn't calculate or save the direction vectors for principal stresses anywhere. So my plan is to add VectorLists in
src/Mod/Fem/PyObjects/_FemResultMechanical.py and calculate and fill them in src/Mod/Fem/importToolsFem.py
Can anyone who is more familiar with the code tell me if this is a reasonable approach?
As you pointed out already, you would have two possibilities. Either use the ViewProviderFemMesh or the VTK post pipline and you found yourself the PostPipeline is more appropriate. I really would recommend to use the PostPipeline !!! Ickby gave the most important informations in this regard already.
BTW: we gone use flake8 code formating in FEM Python code and use no tabulator at all in FEM Python and FEM C++ development. Best is to use github to show the code to others before making a PR at the master branch of FreeCAD.
cheers bernd
Re: Display principal stresses
I took a shot at implementing this and got somewhat close to what i want to achieve.
The current code is available on my github: https://github.com/AMstuff/FreeCAD/comm ... 64172ce48d
Still i am facing some problems which I hope you can help me with:
1. I have trouble passing arguments to vtkProgrammableGlyphFilter. My problem here is that the calcGlyphArrow method needs to be static and accessing other class members is not easily possible. Can you recommend an elegant solution to circumvent this problem?
2. I have no idea how to set filter parameters such as the active vectors, scaling, color etc.
3. Data is not passed through the filter.
Also, since I need to work with the principal stresses in the future, i added them to the resultmesh object: https://github.com/AMstuff/FreeCAD/comm ... 1a3643L423
Note that i changed the sigma matrix in importToolsFem.py's calculate_principal_stresses(i) as i believe it is not consistent with the order of the stresses as implemented in calculate_von_mises(i)
current code of calculate_principal_stresses(i)
current code of calculate_von_mises(i)
Am i wrong?
Lastly, thanks ickby and bernd for your advice! It was very helpful and saved me a lot of time.
The current code is available on my github: https://github.com/AMstuff/FreeCAD/comm ... 64172ce48d
Still i am facing some problems which I hope you can help me with:
1. I have trouble passing arguments to vtkProgrammableGlyphFilter. My problem here is that the calcGlyphArrow method needs to be static and accessing other class members is not easily possible. Can you recommend an elegant solution to circumvent this problem?
2. I have no idea how to set filter parameters such as the active vectors, scaling, color etc.
3. Data is not passed through the filter.
Also, since I need to work with the principal stresses in the future, i added them to the resultmesh object: https://github.com/AMstuff/FreeCAD/comm ... 1a3643L423
Note that i changed the sigma matrix in importToolsFem.py's calculate_principal_stresses(i) as i believe it is not consistent with the order of the stresses as implemented in calculate_von_mises(i)
current code of calculate_principal_stresses(i)
Code: Select all
sigma = np.array([[i[0], i[3], i[4]],
[i[3], i[1], i[5]],
[i[4], i[5], i[2]]])
Code: Select all
s11 = i[0]
s22 = i[1]
s33 = i[2]
s12 = i[3]
s23 = i[4]
s31 = i[5]
Lastly, thanks ickby and bernd for your advice! It was very helpful and saved me a lot of time.
- Attachments
-
- freecad.png (153.68 KiB) Viewed 4801 times
Re: Display principal stresses
Very useful feature. I am testing a bit it seems to work fine
Re: Display principal stresses
Good morning,
sorry for the late reply, I'm rather busy. But you made very nice progress, looking forward to play with this in freecad!
This is tricky with those plain c callbacks, and I never tried this in VTK. One mathod that may work is to utilize the argument. If I understand it correctly, the argument Arg you pass to SetGlyphMethod(callback, Arg) is the same that is passed to the callback itself. So what you can do is for example pass your "FemPostGlyphFilter" as Arg and call the non-static function of FemPostGlyphFilter from within the callback.
What do you mean exactly with this? How to set this within the programmable filter or how to set this in the GUI? Could you elaborate?
This I also don't get.
sorry for the late reply, I'm rather busy. But you made very nice progress, looking forward to play with this in freecad!
Code: Select all
I have trouble passing arguments to vtkProgrammableGlyphFilter. My problem here is that the calcGlyphArrow method needs to be static and accessing other class members is not easily possible. Can you recommend an elegant solution to circumvent this problem?
Code: Select all
2. I have no idea how to set filter parameters such as the active vectors, scaling, color etc.
Code: Select all
3. Data is not passed through the filter.
Re: Display principal stresses
Good question ... Any other some input in this regard? @makkemal: wasn't it you who added the principal stress calculation? Since this in an independent change it would be good to put this in a separate commit before all your great add ons. If later we will have a discussion about this it is much easier to find this change in commit history. I hopefully find the time on the weekend to give your branch a try.
cheers bernd
Re: Display principal stresses
Thanks! this seems to be a good idea. I'll look into it.ickby wrote: ↑Fri Oct 13, 2017 6:33 am This is tricky with those plain c callbacks, and I never tried this in VTK. One mathod that may work is to utilize the argument. If I understand it correctly, the argument Arg you pass to SetGlyphMethod(callback, Arg) is the same that is passed to the callback itself. So what you can do is for example pass your "FemPostGlyphFilter" as Arg and call the non-static function of FemPostGlyphFilter from within the callback.
actually, nevermindickby wrote: ↑Fri Oct 13, 2017 6:33 amWhat do you mean exactly with this? How to set this within the programmable filter or how to set this in the GUI? Could you elaborate?Code: Select all
2. I have no idea how to set filter parameters such as the active vectors, scaling, color etc.

What I didn't get to work though is setting the strings in the "Glyph" dropdown menu. This should be handled by execute(). I tried to copy the way this is done for the Vectors, but nothing happens.... I've never used Qt before, so am probably missing something obvious here.
Right now, the filter breaks the pipeline. If you would add another filter behind it, that filter won't see any data.ickby wrote: ↑Fri Oct 13, 2017 6:33 amThis I also don't get.Code: Select all
3. Data is not passed through the filter.
I split my commit into smaller chunks: relevant commit