Display principal stresses

About the development of the FEM module/workbench.

Moderator: bernd

amstuff
Posts: 11
Joined: Wed Sep 27, 2017 1:15 am

Display principal stresses

Postby amstuff » 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).

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
p2.png (199.08 KiB) Viewed 1660 times
ickby
Posts: 2879
Joined: Wed Oct 05, 2011 7:36 am

Re: Display principal stresses

Postby ickby » Wed Sep 27, 2017 5:55 am

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!
amstuff
Posts: 11
Joined: Wed Sep 27, 2017 1:15 am

Re: Display principal stresses

Postby amstuff » Wed Sep 27, 2017 9:28 am

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.
ickby
Posts: 2879
Joined: Wed Oct 05, 2011 7:36 am

Re: Display principal stresses

Postby ickby » Wed Sep 27, 2017 10:13 am

I dug around in the Pipeline/VTK source, yet still can't find anything related to the principal stress vectors
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:
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.
User avatar
bernd
Posts: 7711
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Display principal stresses

Postby bernd » Wed Sep 27, 2017 4:22 pm

amstuff 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).
would be a great improvement


amstuff 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?
yes, this is how it works at the moment with all other results


amstuff wrote:
Wed Sep 27, 2017 3:53 am
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?
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
amstuff
Posts: 11
Joined: Wed Sep 27, 2017 1:15 am

Re: Display principal stresses

Postby amstuff » Wed Oct 11, 2017 12:50 am

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)

Code: Select all

sigma = np.array([[i[0], i[3], i[4]],
                  [i[3], i[1], i[5]],
                  [i[4], i[5], i[2]]])
current code of calculate_von_mises(i)

Code: Select all

s11 = i[0]
s22 = i[1]
s33 = i[2]
s12 = i[3]
s23 = i[4]
s31 = i[5]
Am i wrong?

Lastly, thanks ickby and bernd for your advice! It was very helpful and saved me a lot of time.
Attachments
freecad.png
freecad.png (153.68 KiB) Viewed 1483 times
makkemal
Posts: 382
Joined: Wed Apr 29, 2015 12:41 pm

Re: Display principal stresses

Postby makkemal » Wed Oct 11, 2017 4:42 pm

Very useful feature. I am testing a bit it seems to work fine
ickby
Posts: 2879
Joined: Wed Oct 05, 2011 7:36 am

Re: Display principal stresses

Postby ickby » Fri Oct 13, 2017 6:33 am

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!

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

Code: Select all

2. I have no idea how to set filter parameters such as the active vectors, scaling, color etc.
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?

Code: Select all

3. Data is not passed through the filter.
This I also don't get.
User avatar
bernd
Posts: 7711
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Display principal stresses

Postby bernd » Fri Oct 13, 2017 4:27 pm

amstuff wrote:
Wed Oct 11, 2017 12:50 am
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)

Am i wrong?
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
amstuff
Posts: 11
Joined: Wed Sep 27, 2017 1:15 am

Re: Display principal stresses

Postby amstuff » Sun Oct 15, 2017 11:26 pm

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.
Thanks! this seems to be a good idea. I'll look into it.
ickby wrote:
Fri Oct 13, 2017 6:33 am

Code: Select all

2. I have no idea how to set filter parameters such as the active vectors, scaling, color etc.
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?
actually, nevermind :) What i was missing was some convenience methods that other vtk filters provide (eg "SetInputArrayToProcess", "SetScaleFactor") but since all data is present in the vtkPolyData, i can reimplement this by passing parameters to the callback.

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.
ickby wrote:
Fri Oct 13, 2017 6:33 am

Code: Select all

3. Data is not passed through the filter.
This I also don't get.
Right now, the filter breaks the pipeline. If you would add another filter behind it, that filter won't see any data.
bernd wrote:
Fri Oct 13, 2017 4:27 pm
Since this in an independent change it would be good to put this in a separate commit before all your great add ons.
I split my commit into smaller chunks: relevant commit