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

About the development of the FEM module/workbench.

Moderator: bernd

User avatar
bernd
Posts: 8488
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:19 am

joha2 wrote:
Thu Sep 19, 2019 4:46 pm
... is there any useful documentation how to extend the functionality of FemMesh or other C++ classes? ...
no, just the code

joha2 wrote:
Thu Sep 19, 2019 4:46 pm
... I had to put it all together by recycling other functions from the code, which is maybe not the best way of development :-) :mrgreen: ...
it is exactly the same way I have done all my contributions to FreeCAD.
User avatar
bernd
Posts: 8488
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 4:17 am

py3 fix ... https://github.com/berndhahnebach/FreeC ... /femgroups



python only example ...

Code: Select all

import Fem
from femexamples.meshes.mesh_canticcx_tetra10 import create_nodes, create_elements

fm = Fem.FemMesh()
control = create_nodes(fm)
print(control)
control = create_elements(fm)
print(control)

# informations
fm
fm.GroupCount

fm.addGroup("MyNodeGroup", "Node")
# should return or print the group ID, similar if a node is added to a mesh
# there should be the possibility to set a group ID
fm.GroupCount
fm.Groups
fm.getGroupElementType(0)
fm.getGroupElements(0)

fm.addGroupElements(0, [1,2,3,4,49,64,88,100,102,188,189,190,191])
fm.getGroupElements(0)

import ObjectsFem
doc = App.ActiveDocument
femmesh_obj = doc.addObject("Fem::FemMeshObject", "mymesh")
femmesh_obj.FemMesh = fm
doc.recompute()

doc.mymesh.ViewObject.HighlightedNodes = []
doc.mymesh.ViewObject.DisplayMode = "Faces & Wireframe"
doc.mymesh.ViewObject.HighlightedNodes = list(fm.getGroupElements(0))
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 » Fri Sep 20, 2019 8:10 am

bernd wrote:
Fri Sep 20, 2019 4:17 am
py3 fix ... https://github.com/berndhahnebach/FreeC ... /femgroups



python only example ...

Code: Select all

import Fem
from femexamples.meshes.mesh_canticcx_tetra10 import create_nodes, create_elements

fm = Fem.FemMesh()
control = create_nodes(fm)
print(control)
control = create_elements(fm)
print(control)

# informations
fm
fm.GroupCount

fm.addGroup("MyNodeGroup", "Node")
# should return or print the group ID, similar if a node is added to a mesh
# there should be the possibility to set a group ID
fm.GroupCount
fm.Groups
fm.getGroupElementType(0)
fm.getGroupElements(0)

fm.addGroupElements(0, [1,2,3,4,49,64,88,100,102,188,189,190,191])
fm.getGroupElements(0)

import ObjectsFem
doc = App.ActiveDocument
femmesh_obj = doc.addObject("Fem::FemMeshObject", "mymesh")
femmesh_obj.FemMesh = fm
doc.recompute()

doc.mymesh.ViewObject.HighlightedNodes = []
doc.mymesh.ViewObject.DisplayMode = "Faces & Wireframe"
doc.mymesh.ViewObject.HighlightedNodes = list(fm.getGroupElements(0))
Hey @bernd!

Thanks for the fast answers -- was a little late yesterday, wasn't it? :mrgreen: :D
How do you suggest to proceed with putting these functions into FemMesh.cpp? There it makes obviously no sense to use Python data types to access the arguments. So I would do the following:

In FemMesh.cpp:

Code: Select all

void addGroupElements(int id, size_t *elementids)
{
	// Smesh C++ code goes here
}
In FemMeshPy.cpp:

Code: Select all

PyObject* FemMeshPy::addGroupElements(PyObject *args)
{
	// parsing the Python object goes here
	FemMesh::addGroupElements(id, elementids);
}
Is this the separation, you intended? I would further try to define a delGroup function in FemMesh.cpp. The 'intersection' and 'union' functions I would postpone until we have a useful usecase. How do we expose these functions to the GUI? Does it make sense to add new buttons to the mesh section of the FEM workbench? I think modifying the dialog for mesh group creation is not that good, since the mechanism is real different.

The unit test you proposed above, I realized how it works, but at the end of the day, there has to be some assertion. Do we have to check whether the group elements gotten from getGroupElements are the same?
User avatar
bernd
Posts: 8488
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 Sep 30, 2019 4:01 pm

joha2 wrote:
Fri Sep 20, 2019 8:10 am
... was a little late yesterday, wasn't it? :mrgreen: :D
actually it was not late but early instead. post time is not local time but greenwich time AFAIK For me it was a quater past 5.


joha2 wrote:
Fri Sep 20, 2019 8:10 am
How do you suggest to proceed with putting these functions into FemMesh.cpp? There it makes obviously no sense to use Python data types to access the arguments. So I would do the following:

In FemMesh.cpp:

Code: Select all

void addGroupElements(int id, size_t *elementids)
{
	// Smesh C++ code goes here
}
In FemMeshPy.cpp:

Code: Select all

PyObject* FemMeshPy::addGroupElements(PyObject *args)
{
	// parsing the Python object goes here
	FemMesh::addGroupElements(id, elementids);
}
Is this the separation, you intended?
exactly. With this one could use the methods from C++ too.


joha2 wrote:
Fri Sep 20, 2019 8:10 am
I would further try to define a delGroup function in FemMesh.cpp. The 'intersection' and 'union' functions I would postpone until we have a useful usecase.
great


joha2 wrote:
Fri Sep 20, 2019 8:10 am
How do we expose these functions to the GUI? Does it make sense to add new buttons to the mesh section of the FEM workbench? I think modifying the dialog for mesh group creation is not that good, since the mechanism is real different.
I do not know yet. I am the step by step guy. We have a tool to select nodes from a mesh. This could get some love. Furthermore a toggle button in Mesh group obj is thinkabel. Use mesher or use internal search for groups. Than activate netgen for the group tool too.


joha2 wrote:
Fri Sep 20, 2019 8:10 am
The unit test you proposed above, I realized how it works, but at the end of the day, there has to be some assertion. Do we have to check whether the group elements gotten from getGroupElements are the same?
that was not a unit test. Just some code to have a test at all, to play with the new methods. It could be extended to a unit test the way you proposed it.

cheers bernd
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 Oct 05, 2019 7:59 am

bernd wrote:
Mon Sep 30, 2019 4:01 pm
*SCHNIPP* *SCHNAPP*
exactly. With this one could use the methods from C++ too.
*SCHNIPP* *SCHNAPP*
cheers bernd
Hey bernd,

I ask myself: How to proceed on the development. Should I continue to add changes to the pull requested code from my fem_mesh_groups branch into your femdev branch?

Best wishes
Johannes
User avatar
bernd
Posts: 8488
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 Oct 05, 2019 5:04 pm

what ever fits best for you. Just add them to your group dev branch and they will show on the PR.

Before we will merge everything into main FreeCAD we will squash eyerything into one or two commits anyway.

As you know I like a clean commit history. It is much easier to show newbees how to do something if one has a clean commit to show as an example.

cheers bernd

BTW.
I will continue to maintain a brach which will be regularly rebased on master. With this we will not have any difficaulty at the end with mergong into master.
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 Oct 05, 2019 5:33 pm

bernd wrote:
Sat Oct 05, 2019 5:04 pm
what ever fits best for you. ...
I will continue to maintain a brach which will be regularly rebased on master. With this we will not have any difficaulty at the end with mergong into master.
Ah OK. I already thought like this, but I wanted to be sure. :mrgreen: I've provided the splitting of C++ and Python, that makes the code more usable - as you mentioned -, just a few minutes ago. Please have a look at it. Now the addGroup function returns the group ids. The customized group ID functionality is implemented, but it is ignored by Salome (although you are able to provide an ID to AddGroup).

Code: Select all

>>> fm.addGroup("mynodes", "Node")
0
>>> fm.addGroup("mynodes2", "Node", 4)
1
So maybe this comes in the future. If you are OK with the functions, I can continue to implement the delGroup command, so that we have a complete set of group management functions.

Edit: I've added the removeGroup function (name due to SMESH compatibility), and here's a short demo of how it works in the FreeCAD console (starting point is a FemMesh for a cube without groups):

Code: Select all

>>> fm.removeGroup(0)
False
>>> fm.removeGroup(1)
False
>>> fm.addGroup("mynodegroup", "Node")
0
>>> fm.addGroupElements(0, [200, 201, 202])
>>> fm.GroupCount
1L
>>> fm.Groups
(0,)
>>> fm.addGroup("myallgroup", "All")
1
>>> fm.removeGroup(0)
True
>>> fm.removeGroup(1)
True
>>> fm.GroupCount
0L
>>> fm.Groups
()
https://github.com/berndhahnebach/FreeCAD_bhb/pull/57

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 » Sun Oct 06, 2019 9:35 am

@bernd I know I asked that already, but I cannot find where we discussed this, whether on github or here in the forum: How to implement a useful unit test for the functions?

I already found Mod/Fem/femtest/App/test_mesh.py

There I would implement a new method into the TestMeshCommon class, namely test_mesh_groups_python().

Is there anything else to do?

Best wishes
Johannes
User avatar
bernd
Posts: 8488
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 Oct 14, 2019 5:37 pm

joha2 wrote:
Sun Oct 06, 2019 9:35 am
@bernd I know I asked that already, but I cannot find where we discussed this, whether on github or here in the forum: How to implement a useful unit test for the functions?

I already found Mod/Fem/femtest/App/test_mesh.py

There I would implement a new method into the TestMeshCommon class, namely test_mesh_groups_python().

Is there anything else to do?
May be it would be even better to add a new class called FemMeshGroup here https://github.com/FreeCAD/FreeCAD/blob ... sh.py#L221 There we would add some methods. There is not a reading test ATM. This would needed first, because to proof the writing we would need to read the groups. All tests threre are Pythontests, means it should be somehow test_mesh_groups_reading and test_mesh_groups_writing or even more methods. For every python group method we have one unit test method. with this we can later exactly find which was wrong. Means just one assert per method.
User avatar
bernd
Posts: 8488
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 Oct 14, 2019 5:38 pm

every class needs setUp and tearDown method. They you could copy.