Erase Selected Elements in a Finite Element Mesh

About the development of the FEM module/workbench.

Moderator: bernd

Post Reply
mac_the_bike
Posts: 36
Joined: Sun Jun 30, 2019 12:56 pm

Erase Selected Elements in a Finite Element Mesh

Post by mac_the_bike »

If this is not in the correct Forum, can you tell me which one is should be in.
I have written some code, for the FEM WB, that enables the user to erase elements from the display. The majority of the code is written in c++ with some processing in python. The code is based on "Mesh->Nodes Set" in FreeCAD version 20, the source of which I downloaded in May, 2022.

The first picture shows the full mesh, which is based on the "Fem.FCStd" example, and the second shows a cross-section.
The attachments are:
1 - a different FCStd file which contains examples of the output of this new function, as described below
2 - my version of "femmesh2mesh.py"

This function can be used:
  • to look at the internal mesh to check on the mesh density
  • to look at parts of the model that are obscured by other parts
  • to look at the variation of results through the thickness
The new functionality will be in the FEM WB, under Mesh->Erase Elements.

Open "erase.FCStd". Because of file attachment size limitations, this FCStd file is not the same as the one shown in the first 2 attachments. In the "Labels and Attributes" list, we have:

FEMMeshGmsh - the original finite element mesh, FEM
ResultMesh - the latest "ResultMesh"
ResultPipeLine - based on the original "ResultMesh"
ResultPipeLine001 - based on the latest "ResultMesh"
ElementsSet001 - a list of kept element numbers in "NewFemMesh", used internally
NewFemMesh - the new FEM, with elements erased from "FEMMeshGmsh"
ElementsSet001 - a list of kept element numbers in "NewResultMesh", used internally
StartResultMesh - a copy of the original "ResultMesh"
NewResultMesh - the new "ResultMesh", with elements erased from "StartResultMesh", a copy of this is now the new "ResultMesh"
Mesh - the Mesh of the "StartResultMesh" FEM
Mesh2Fem - the FEM mesh from "Mesh"
ElementsSet002 - a list of kept element numbers in "NewResultMesh001", used internally
NewFemMesh001 - the new FEM, with elements erased from "Mesh2Fem"

"Mesh2Fem" has been created with an additional function I have added to "femmesh2mesh.py", which now generates an FEM from the mesh that the code originally generated.

To change the "ResultMesh", use:

Code: Select all

name = "StartResultMesh"
c = FreeCADGui.ActiveDocument.getObject(name)
FreeCAD.ActiveDocument.ResultMesh.FemMesh = c.Object.FemMesh
Gui.ActiveDocument.getObject('ResultMesh').BackfaceCulling = False
You can't do this with a mesh that contains the word "Fem".

What now?

Does this look like a useful addition to FreeCAD?
If it is, then how do I go about incorporating it?
Attachments
femmesh2mesh.py
(9.34 KiB) Downloaded 6 times
erase.FCStd
(569.12 KiB) Downloaded 7 times
erase_1.jpg
erase_1.jpg (173.33 KiB) Viewed 785 times
erase_2.jpg
erase_2.jpg (124.07 KiB) Viewed 785 times
Jee-Bee
Veteran
Posts: 2536
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: Erase Selected Elements in a Finite Element Mesh

Post by Jee-Bee »

Is the idea of erasing elements something that has a visual goal(read improved version of clip face). Or is the idea to remove elements so you don't calculate them enymore
NewJoker
Veteran
Posts: 1333
Joined: Sun Oct 11, 2020 7:49 pm

Re: Erase Selected Elements in a Finite Element Mesh

Post by NewJoker »

mac_the_bike wrote: Wed Aug 10, 2022 11:48 am Does this look like a useful addition to FreeCAD?
If it is, then how do I go about incorporating it?
Yes, hiding individual elements from the display can be very useful. Professional postprocessors (such as Abaqus/Viewer or ParaView) offer such tools and they are commonly used.

mac_the_bike wrote: Wed Aug 10, 2022 11:48 am The code is based on "Mesh->Nodes Set"
Maybe on this occassion you could also add a tool for element set creation ? It would be much appreciated.

mac_the_bike wrote: Wed Aug 10, 2022 11:48 am how do I go about incorporating it?
You already have the code so you could create a Pull Request on GitHub. Maybe @bernd can help you with that.
mac_the_bike
Posts: 36
Joined: Sun Jun 30, 2019 12:56 pm

Re: Erase Selected Elements in a Finite Element Mesh

Post by mac_the_bike »

Is the idea of erasing elements something that has a visual goal(read improved version of clip face). Or is the idea to remove elements so you don't calculate them enymore
This is purely a visual thing, so I am not removing elements from the analysis.
In "Mesh->Nodes Set" a rubber-band polygon is generated and those nodes inside/outside are chosen. I use this method but with the CofG of the elements being inside/outside the polygon.
Maybe on this occassion you could also add a tool for element set creation ? It would be much appreciated.
What data would you require in the element set?
NewJoker
Veteran
Posts: 1333
Joined: Sun Oct 11, 2020 7:49 pm

Re: Erase Selected Elements in a Finite Element Mesh

Post by NewJoker »

mac_the_bike wrote: Thu Aug 11, 2022 11:48 am What data would you require in the element set?
Element sets are objects analogous to node sets but for elements. They are basically grouped element selections used in definitions of various features in CalculiX. Just like node sets, they can be useful when defining features that are not yet supported by FreeCAD and require modification of the input file. The current tool for node set creation may also need improvement. To be honest, I'm not sure if it works properly and if it does, it's not very intuitive. But maybe on this occasion, you could also add an element set creation tool based on the same way of selecting elements as for your tool presented here. The difference would be that they wouldn't be hidden but only added to a named group available in the input file:

Code: Select all

*ELSET, ELSET=name
list_of_elements_belonging_to_the_elset
mac_the_bike
Posts: 36
Joined: Sun Jun 30, 2019 12:56 pm

Re: Erase Selected Elements in a Finite Element Mesh

Post by mac_the_bike »

I want to keep the code doing "what is says on the tin", i.e. generating a display of selected elements. I don't want to add any options to this function.
As a by-product, the code generates, internally, an object called "ElementsSet". This contains the list of elements that are displayed together with the element type. Second and subsequent invocations create "ElementsSet001", etc.
You can't see the contents of these objects in the production version of FreeCAD.
In my development version I have:

Code: Select all

NE = Gui.ActiveDocument.getObject("ElementsSet001")
NE.Object.Elements # list of element numbers displayed

Output from the above command:
{1025, 1026, 1027, 1537, 1538, 1543, 1544, 1546, 1548, 1037, 1549, 1550, 1551, 1041, 1555, 1044, 1045, 1558, 1559, 1561, 1051, 1532, 1563, 1564, 1058, 1570, 1060, 1571, 1534, 1572, 1573, 1066, 1072, 1073, 1584, 1585, 1588, 1589, 1590, 1592, 1081, 1593, 1594, 1084, 1597, 1598, 1599, 1600, 1601, 1093, 1094, 1605, 1096, 1097, 1607, 1099, 1611, 1614, 1615, 1107, 1108, 1109, 1620, 1111, 1112, 1622, 1625, 1627, 1116, 1117, 1628, 1120, 1121, 1635, 1637, 1639, 1640, 1642, 1131, 1643, 1644, 1134 -4, 1022}

# find the erased element numbers
c = Gui.ActiveDocument.getObject("FEMMeshGmsh") # put your mesh name here
lel = list(NE.Object.Elements) # convert to a list
lel.sort() # sort
lel.pop(0) # remove first element - lel is now the list of the displayed elements
slel = set(lel) # convert to set

vv = c.Object.FemMesh.Volumes # all elements in model
svv = set(vv)  # convert to set
svv.difference(slel) # all elements that are in the model but not in "ElementsSet001"
The "-4" is the type of element, 3, 2D, or 4, 3D.
mac_the_bike
Posts: 36
Joined: Sun Jun 30, 2019 12:56 pm

Re: Erase Selected Elements in a Finite Element Mesh

Post by mac_the_bike »

Code: Select all

OS: Ubuntu 22.04 LTS (GNOME-Flashback:GNOME/gnome-flashback-metacity)
Word size of FreeCAD: 64-bit
Version: 0.21.30159 (Git)
Build type: Unknown
Branch: master
Hash: 71015d23d051229db4b86d4e81dd887826c95d79
Python 3.10.4, Qt 5.15.3, Coin 4.0.0, Vtk 7.1.1, OCC 7.5.1
Locale: English/United Kingdom (en_GB)
I have now updated my code to Version 0.21 and it works. There are:
  • 10 new files
  • 10 modified versions of existing files, most changes are small except for "src/Mod/Fem/Gui/Command.cpp" which modifies the Fem WB "Mesh" pull-down menu.
At this point I would like to incorporate this code into FreeCAD - the question is how?

Do I need a "Pull Request" and if so how do I get one? And then what do I do?

How do I incorporate the new code into FreeCAD?

How is git involved in all this?

Thanks for your future help
NewJoker
Veteran
Posts: 1333
Joined: Sun Oct 11, 2020 7:49 pm

Re: Erase Selected Elements in a Finite Element Mesh

Post by NewJoker »

mac_the_bike wrote: Sat Aug 27, 2022 11:21 am At this point I would like to incorporate this code into FreeCAD - the question is how?

Do I need a "Pull Request" and if so how do I get one? And then what do I do?

How do I incorporate the new code into FreeCAD?

How is git involved in all this?
This page on the wiki describes all the necessary steps: Source code management.

You will have to create a PR on GitHub. Then it will be reviewed by the devs before merging. You might be asked a few questions about your code so check any notifications from GitHub.

Maybe @uwestoehr can help you with this.
User avatar
johnwang
Veteran
Posts: 1128
Joined: Sun Jan 27, 2019 12:41 am

Re: Erase Selected Elements in a Finite Element Mesh

Post by johnwang »

Do you have a GitHub account? If you have, Fork freecad, make your change on your branch,then make a pull request.
Since you have C code, need other people to compile to test it.

For the py file change, you can provide a test case and procedure to test your added function. You can ask if someone could make a pull request for you if you are not good at GitHub thing.
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
User avatar
Kunda1
Veteran
Posts: 13022
Joined: Thu Jan 05, 2017 9:03 pm

Re: Erase Selected Elements in a Finite Element Mesh

Post by Kunda1 »

@mac_the_bike Feel free to ask for help with git related stuff. It's a PITA but also quite a powerful tool.
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
Post Reply