FEM: add methods to to edit mesh groups:

- add addGroup, addGroupElements, removeGroup to C++ mesh class
- expose the methods to Python
- add a unit test class
- update test commands file
- add test to fem test app module
This commit is contained in:
joha2
2020-05-22 11:46:38 +02:00
committed by Bernd Hahnebach
parent 39d520f672
commit bdfd0b10a7
7 changed files with 401 additions and 3 deletions

View File

@@ -529,3 +529,174 @@ class TestMeshEleTetra10(unittest.TestCase):
femmesh_outfile,
file_extension
)
# ************************************************************************************************
# ************************************************************************************************
# TODO: add elements to group with another type. Should be empty at the end.
class TestMeshGroups(unittest.TestCase):
fcc_print("import TestMeshGroups")
# ********************************************************************************************
def setUp(
self
):
# setUp is executed before every test
# new document
self.document = FreeCAD.newDocument(self.__class__.__name__)
# ********************************************************************************************
def tearDown(
self
):
# tearDown is executed after every test
FreeCAD.closeDocument(self.document.Name)
# ********************************************************************************************
def test_00print(
self
):
# since method name starts with 00 this will be run first
# this test just prints a line with stars
fcc_print("\n{0}\n{1} run FEM TestMeshGroups tests {2}\n{0}".format(
100 * "*",
10 * "*",
57 * "*"
))
# ********************************************************************************************
def test_add_groups(self):
"""
Create different groups with different names. Check whether the
ids are correct, the names are correct, and whether the GroupCount is
correct.
"""
from femexamples.meshes.mesh_canticcx_tetra10 import create_elements
from femexamples.meshes.mesh_canticcx_tetra10 import create_nodes
fm = Fem.FemMesh()
control = create_nodes(fm)
if not control:
fcc_print("failed to create nodes")
control = create_elements(fm)
if not control:
fcc_print("failed to create elements")
# information
# fcc_print(fm)
expected_dict = {}
expected_dict["ids"] = []
expected_dict["names"] = [
"MyNodeGroup",
"MyEdgeGroup",
"MyVolumeGroup",
"My0DElementGroup",
"MyBallGroup"
]
expected_dict["types"] = [
"Node",
"Edge",
"Volume",
"0DElement",
"Ball"
]
expected_dict["count"] = fm.GroupCount + 5
result_dict = {}
mygrpids = []
for (name, typ) in zip(expected_dict["names"], expected_dict["types"]):
mygrpids.append(fm.addGroup(name, typ))
expected_dict["ids"] = sorted(tuple(mygrpids))
# fcc_print("expected dict")
# fcc_print(expected_dict)
result_dict["count"] = fm.GroupCount
result_dict["ids"] = sorted(fm.Groups)
result_dict["types"] = list([fm.getGroupElementType(g)
for g in fm.Groups])
result_dict["names"] = list([fm.getGroupName(g) for g in fm.Groups])
# fcc_print("result dict")
# fcc_print(result_dict)
self.assertEqual(
expected_dict,
result_dict,
msg="expected: {0}\n\nresult: {1}\n\n differ".format(expected_dict, result_dict)
)
def test_delete_groups(self):
"""
Adds a number of groups to FemMesh and deletes them
afterwards. Checks whether GroupCount is OK
"""
from femexamples.meshes.mesh_canticcx_tetra10 import create_elements
from femexamples.meshes.mesh_canticcx_tetra10 import create_nodes
fm = Fem.FemMesh()
control = create_nodes(fm)
if not control:
fcc_print("failed to create nodes")
control = create_elements(fm)
if not control:
fcc_print("failed to create elements")
# information
# fcc_print(fm)
old_group_count = fm.GroupCount
myids = []
for i in range(1000):
myids.append(fm.addGroup("group" + str(i), "Node"))
for grpid in myids:
fm.removeGroup(grpid)
new_group_count = fm.GroupCount
self.assertEqual(
old_group_count,
new_group_count,
msg=(
"GroupCount before and after adding and deleting groups differ: {0} != {1}"
.format(old_group_count, new_group_count)
)
)
def test_add_group_elements(self):
"""
Add a node group, add elements to it. Verify that elements added
and elements in getGroupElements are the same.
"""
from femexamples.meshes.mesh_canticcx_tetra10 import create_elements
from femexamples.meshes.mesh_canticcx_tetra10 import create_nodes
fm = Fem.FemMesh()
control = create_nodes(fm)
if not control:
fcc_print("failed to create nodes")
control = create_elements(fm)
if not control:
fcc_print("failed to create elements")
# information
# fcc_print(fm)
elements_to_be_added = [1, 2, 3, 4, 49, 64, 88, 100, 102, 188, 189, 190, 191]
myid = fm.addGroup("mynodegroup", "Node")
# fcc_print(fm.getGroupElements(myid))
fm.addGroupElements(myid, elements_to_be_added)
elements_returned = list(fm.getGroupElements(myid)) # returns tuple
# fcc_print(elements_returned)
self.assertEqual(
elements_to_be_added,
elements_returned,
msg=(
"elements to be added {0} and elements returned {1} differ".
format(elements_to_be_added, elements_returned)
)
)

View File

@@ -26,6 +26,7 @@ make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_femimport.TestObjectExistance
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_material.TestMaterialUnits
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_mesh.TestMeshCommon
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_mesh.TestMeshEleTetra10
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_mesh.TestMeshGroups
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_object.TestObjectCreate
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_object.TestObjectType
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_open.TestObjectOpen
@@ -61,6 +62,9 @@ make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_mesh.TestMeshEleTetra10.test_t
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_mesh.TestMeshEleTetra10.test_tetra10_vkt
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_mesh.TestMeshEleTetra10.test_tetra10_yml
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_mesh.TestMeshEleTetra10.test_tetra10_z88
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_mesh.TestMeshGroups.test_add_groups
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_mesh.TestMeshGroups.test_delete_groups
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_mesh.TestMeshGroups.test_add_group_elements
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_object.TestObjectCreate.test_femobjects_make
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_object.TestObjectType.test_femobjects_type
make -j 4 && ./bin/FreeCADCmd -t femtest.app.test_object.TestObjectType.test_femobjects_isoftype
@@ -215,6 +219,21 @@ unittest.TextTestRunner().run(unittest.TestLoader().loadTestsFromName(
'femtest.app.test_mesh.TestMeshEleTetra10.test_tetra10_z88'
))
import unittest
unittest.TextTestRunner().run(unittest.TestLoader().loadTestsFromName(
'femtest.app.test_mesh.TestMeshGroups.test_add_groups'
))
import unittest
unittest.TextTestRunner().run(unittest.TestLoader().loadTestsFromName(
'femtest.app.test_mesh.TestMeshGroups.test_delete_groups'
))
import unittest
unittest.TextTestRunner().run(unittest.TestLoader().loadTestsFromName(
'femtest.app.test_mesh.TestMeshGroups.test_add_group_elements'
))
import unittest
unittest.TextTestRunner().run(unittest.TestLoader().loadTestsFromName(
'femtest.app.test_object.TestObjectCreate.test_femobjects_make'