FEM mesh: Create mesh groups in Python (exposing C++ functions to Python)

About the development of the FEM module/workbench.

Moderator: bernd

joha2
Posts: 243
Joined: Tue Oct 11, 2016 9:48 pm

FEM mesh: Create mesh groups in Python (exposing C++ functions to Python)

Postby joha2 » Sat Apr 20, 2019 11:29 am

From some discussion in https://forum.freecadweb.org/viewtopic. ... 10#p302498 the following point is to be achieved:

bernd:

to be able to create group data by python the C++ methods for creating groups need to be exposed to python. This needs to be done in C++. It is not that difficault to do this. There are lots of examples around how to do such new Python methods. If you are interested I could post some links which will help you. But for this I would need a computer, but I am on the way ... Furthermore it makes sense to make a new topic for this since it is some own topic.

joha2:

If I have a little time, I will try to implement this. In the meantime if you have to opportunity, I would like to have the links you have about this stuff, since the C++ code is quite complicated.

Thanks and best wishes
Johannes
User avatar
bernd
Posts: 8468
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: FEM mesh: Create mesh groups in Python (exposing C++ functions to Python)

Postby bernd » Sat Apr 20, 2019 6:55 pm

As soon as I have a computer I post some more informations ...
User avatar
bernd
Posts: 8468
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: FEM mesh: Create mesh groups in Python (exposing C++ functions to Python)

Postby bernd » Mon Apr 22, 2019 7:41 pm

the commits which added some group reading methods ...

git commit 084e163
git commit efb87dc1

some group code ...

https://github.com/FreeCAD/FreeCAD/blob ... #L211-L249 and https://github.com/FreeCAD/FreeCAD/blob ... L960-L1032

first of all we should define which defs and what def names we would like to implement ...
joha2
Posts: 243
Joined: Tue Oct 11, 2016 9:48 pm

Re: FEM mesh: Create mesh groups in Python (exposing C++ functions to Python)

Postby joha2 » Tue Apr 23, 2019 6:48 am

bernd wrote:
Mon Apr 22, 2019 7:41 pm
the commits which added some group reading methods ...

git commit 084e163
git commit efb87dc1

some group code ...

https://github.com/FreeCAD/FreeCAD/blob ... #L211-L249 and https://github.com/FreeCAD/FreeCAD/blob ... L960-L1032
Thanks for the code examples! In particular we should have a look at the copy code for groups, maybe this provides some hints on how to proceed.
bernd wrote:
Mon Apr 22, 2019 7:41 pm
first of all we should define which defs and what def names we would like to implement ...
Do you refer to the function names which should be exposed to Python? If so, I would suggest something like addGroup('name') [the group number is simply incremented) and addGroupElements(group_number, element_list) [a counterpart to getGroupElements] where the element_list may only consist of elements which already exist in the mesh structure (they were created by addNode, addEdge, ... and so on.)

Hope this helps on a first sight :-)

Best wishes
Johannes
joha2
Posts: 243
Joined: Tue Oct 11, 2016 9:48 pm

Re: FEM mesh: Create mesh groups in Python (exposing C++ functions to Python)

Postby joha2 » Tue Jul 09, 2019 6:03 am

bernd wrote:
Mon Apr 22, 2019 7:41 pm
ping pong
:mrgreen: Any news here? If we have implemented addGroup and the former mentioned functions, maybe we can think of to define boundary conditions also based on groups and not only on geometry. From this the flexibility of FEM is increased and also compliance calculations (these simulations shown in another thread removing elements to maintain a mechanism or functionality) could be done, couldn't they?

Best wishes
Johannes
joha2
Posts: 243
Joined: Tue Oct 11, 2016 9:48 pm

Re: FEM mesh: Create mesh groups in Python (exposing C++ functions to Python)

Postby joha2 » Sat Jul 13, 2019 11:53 am

User avatar
bernd
Posts: 8468
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: FEM mesh: Create mesh groups in Python (exposing C++ functions to Python)

Postby bernd » Sat Jul 13, 2019 1:09 pm

wow! I will have a look! :D
joha2
Posts: 243
Joined: Tue Oct 11, 2016 9:48 pm

Re: FEM mesh: Create mesh groups in Python (exposing C++ functions to Python)

Postby joha2 » Wed Jul 17, 2019 6:38 am

bernd wrote:
Sat Jul 13, 2019 1:09 pm
wow! I will have a look! :D
:mrgreen: Hehe I knew that this would make you happy :D
Just some further short questions about groups:

  • Would it make sense to also add deleting functionality? Like a delGroup function?
  • Would it make sense to add also the whole bunch of manipulating group functions? I.e. intersect etc?
  • I realized that at several places in the code a conversion from/to elementtype into string takes place. Where would a general function doing this belong to? This is obviously nothing which should only appear in the Python implementation of FemMesh, neither in the C++ interface of FemMesh? Is there a more general file where such service functions belong to?
  • Correct me if I am wrong: The implementation of the groups at the moment works via: define them in the mesh GUI, mesh via GMSH (use PhysicalGroups), reimport the GMSH-created UNV-file and add the groups in the UNV-import from Salome. I did not really understood it from inspecting the code. Can you explain that further?
Thanks for your help!

Best wishes
Johannes
joha2
Posts: 243
Joined: Tue Oct 11, 2016 9:48 pm

Re: FEM mesh: Create mesh groups in Python (exposing C++ functions to Python)

Postby joha2 » Thu Sep 19, 2019 4:46 pm

bernd wrote:
Sat Jul 13, 2019 1:09 pm
wow! I will have a look! :D
Hey @bernd!

I will continue the discussion from github here. Maybe some of the questions are also answered in the posts above.

File location of the methods: I was not sure where to put them in, and since we wanted to expose some C++ functionality to the Python code, I put them into the ...Py.cpp file. But yes, your suggestion seems to be very good, to put the functions into the C++ file where they could also be used from C++ and just use the functions in the ...Py.cpp file as wrappers around the C++ functions. Btw. is there any useful documentation how to extend the functionality of FemMesh or other C++ classes? I had to put it all together by recycling other functions from the code, which is maybe not the best way of development :-) :mrgreen:

My only unit test was to check whether it works with some examples in FreeCAD, but nothing more. Maybe we could use this as a baseline for the unit test. :-)

Best wishes and welcome back
Johannes

Edit: Sorry I forgot: I have no idea what '0DElement' and 'Ball' groups are. I thought that is FreeCAD specific stuff, but apparently not :mrgreen:
User avatar
bernd
Posts: 8468
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: FEM mesh: Create mesh groups in Python (exposing C++ functions to Python)

Postby bernd » Fri Sep 20, 2019 3:18 am

  • Would it make sense to also add deleting functionality? Like a delGroup function?
yes

  • Would it make sense to add also the whole bunch of manipulating group functions? I.e. intersect etc?
I have never needed sucht method, thus I do not know

  • I realized that at several places in the code a conversion from/to elementtype into string takes place. Where would a general function doing this belong to? This is obviously nothing which should only appear in the Python implementation of FemMesh, neither in the C++ interface of FemMesh? Is there a more general file where such service functions belong to?
do you meand these ones: https://github.com/FreeCAD/FreeCAD/blob ... 1649-L1658 for sure this would be better from code point of view. Why not in the file FemMesh.cpp? It belongs to the mesh.

  • Correct me if I am wrong: The implementation of the groups at the moment works via: define them in the mesh GUI, mesh via GMSH (use PhysicalGroups), reimport the GMSH-created UNV-file and add the groups in the UNV-import from Salome. I did not really understood it from inspecting the code. Can you explain that further?

- add a gmsh mesh object
- add a mesh group object and define physical groups (based on shapes or elements of shapes)
- when writing the geo file for gmsh the physical from mesh group objects are written and gmsh is triggered to write the groups to unv
- the unv reader supports reading mesh groups, thus we have groups
. thus groups only work with a gmsh mesh object