Skip to content

Commit

Permalink
FEM: modernize C++: use range-based for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Aug 14, 2023
1 parent 664a080 commit 009f9f2
Show file tree
Hide file tree
Showing 37 changed files with 460 additions and 534 deletions.
113 changes: 53 additions & 60 deletions src/Mod/Fem/App/FemMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ void FemMesh::copyMeshData(const FemMesh& mesh)
SMESHDS_Group* newGroupDS = dynamic_cast<SMESHDS_Group*>(newGroupObj->GetGroupDS());
if (newGroupDS) {
SMDS_MeshGroup& smdsGroup = ((SMESHDS_Group*)newGroupDS)->SMDSGroup();
for (unsigned i = 0; i < groupElems.size(); ++i)
smdsGroup.Add(groupElems[i]);
for (auto it : groupElems)
smdsGroup.Add(it);
}
}
}
Expand Down Expand Up @@ -808,8 +808,8 @@ std::map<int, int> FemMesh::getccxVolumesByFace(const TopoDS_Face &face) const
std::map<int, std::vector<int> >::iterator it = elem_order.find(num_of_nodes);
if (it != elem_order.end()) {
const std::vector<int>& order = it->second;
for (std::vector<int>::const_iterator jt = order.begin(); jt != order.end(); ++jt) {
int vid = vol->GetNode(*jt)->GetID();
for (int jt : order) {
int vid = vol->GetNode(jt)->GetID();
apair.second.push_back(vid);
}
}
Expand Down Expand Up @@ -890,8 +890,7 @@ std::set<int> FemMesh::getNodesBySolid(const TopoDS_Solid &solid) const
}

#pragma omp parallel for schedule(dynamic)
for (size_t i = 0; i < nodes.size(); ++i) {
const SMDS_MeshNode* aNode = nodes[i];
for (auto aNode : nodes) {
double xyz[3];
aNode->GetXYZ(xyz);
Base::Vector3d vec(xyz[0], xyz[1], xyz[2]);
Expand Down Expand Up @@ -942,8 +941,7 @@ std::set<int> FemMesh::getNodesByFace(const TopoDS_Face &face) const
}

#pragma omp parallel for schedule(dynamic)
for (size_t i = 0; i < nodes.size(); ++i) {
const SMDS_MeshNode* aNode = nodes[i];
for (auto aNode : nodes) {
double xyz[3];
aNode->GetXYZ(xyz);
Base::Vector3d vec(xyz[0], xyz[1], xyz[2]);
Expand Down Expand Up @@ -992,8 +990,7 @@ std::set<int> FemMesh::getNodesByEdge(const TopoDS_Edge &edge) const
}

#pragma omp parallel for schedule(dynamic)
for (size_t i = 0; i < nodes.size(); ++i) {
const SMDS_MeshNode* aNode = nodes[i];
for (auto aNode : nodes) {
double xyz[3];
aNode->GetXYZ(xyz);
Base::Vector3d vec(xyz[0], xyz[1], xyz[2]);
Expand Down Expand Up @@ -1041,8 +1038,7 @@ std::set<int> FemMesh::getNodesByVertex(const TopoDS_Vertex &vertex) const
}

#pragma omp parallel for schedule(dynamic)
for (size_t i = 0; i < nodes.size(); ++i) {
const SMDS_MeshNode* aNode = nodes[i];
for (auto aNode : nodes) {
double xyz[3];
aNode->GetXYZ(xyz);
Base::Vector3d vec(xyz[0], xyz[1], xyz[2]);
Expand Down Expand Up @@ -2077,8 +2073,8 @@ void FemMesh::writeABAQUS(const std::string &Filename, int elemParam, bool group
std::map<int, std::string>::iterator it = volTypeMap.find(numNodes);
if (it != volTypeMap.end()) {
const std::vector<int>& order = elemOrderMap[it->second];
for (std::vector<int>::const_iterator jt = order.begin(); jt != order.end(); ++jt)
apair.second.push_back(aVol->GetNode(*jt)->GetID());
for (int jt : order)
apair.second.push_back(aVol->GetNode(jt)->GetID());
elementsMapVol[it->second].insert(apair);
}
}
Expand All @@ -2098,25 +2094,25 @@ void FemMesh::writeABAQUS(const std::string &Filename, int elemParam, bool group
std::map<int, std::string>::iterator it = faceTypeMap.find(numNodes);
if (it != faceTypeMap.end()) {
const std::vector<int>& order = elemOrderMap[it->second];
for (std::vector<int>::const_iterator jt = order.begin(); jt != order.end(); ++jt)
apair.second.push_back(aFace->GetNode(*jt)->GetID());
for (int jt : order)
apair.second.push_back(aFace->GetNode(jt)->GetID());
elementsMapFac[it->second].insert(apair);
}
}
}
if (elemParam == 2) {
// we're going to fill the elementsMapFac with the facesOnly
std::set<int> facesOnly = getFacesOnly();
for (std::set<int>::iterator itfa = facesOnly.begin(); itfa != facesOnly.end(); ++itfa) {
for (int itfa : facesOnly) {
std::pair<int, std::vector<int> > apair;
apair.first = *itfa;
const SMDS_MeshElement* aFace = myMesh->GetMeshDS()->FindElement(*itfa);
apair.first = itfa;
const SMDS_MeshElement* aFace = myMesh->GetMeshDS()->FindElement(itfa);
int numNodes = aFace->NbNodes();
std::map<int, std::string>::iterator it = faceTypeMap.find(numNodes);
if (it != faceTypeMap.end()) {
const std::vector<int>& order = elemOrderMap[it->second];
for (std::vector<int>::const_iterator jt = order.begin(); jt != order.end(); ++jt)
apair.second.push_back(aFace->GetNode(*jt)->GetID());
for (int jt : order)
apair.second.push_back(aFace->GetNode(jt)->GetID());
elementsMapFac[it->second].insert(apair);
}
}
Expand All @@ -2137,25 +2133,25 @@ void FemMesh::writeABAQUS(const std::string &Filename, int elemParam, bool group
std::map<int, std::string>::iterator it = edgeTypeMap.find(numNodes);
if (it != edgeTypeMap.end()) {
const std::vector<int>& order = elemOrderMap[it->second];
for (std::vector<int>::const_iterator jt = order.begin(); jt != order.end(); ++jt)
apair.second.push_back(aEdge->GetNode(*jt)->GetID());
for (int jt : order)
apair.second.push_back(aEdge->GetNode(jt)->GetID());
elementsMapEdg[it->second].insert(apair);
}
}
}
if (elemParam == 2) {
// we're going to fill the elementsMapEdg with the edgesOnly
std::set<int> edgesOnly = getEdgesOnly();
for (std::set<int>::iterator ited = edgesOnly.begin(); ited != edgesOnly.end(); ++ited) {
for (int ited : edgesOnly) {
std::pair<int, std::vector<int> > apair;
apair.first = *ited;
const SMDS_MeshElement* aEdge = myMesh->GetMeshDS()->FindElement(*ited);
apair.first = ited;
const SMDS_MeshElement* aEdge = myMesh->GetMeshDS()->FindElement(ited);
int numNodes = aEdge->NbNodes();
std::map<int, std::string>::iterator it = edgeTypeMap.find(numNodes);
if (it != edgeTypeMap.end()) {
const std::vector<int>& order = elemOrderMap[it->second];
for (std::vector<int>::const_iterator jt = order.begin(); jt != order.end(); ++jt)
apair.second.push_back(aEdge->GetNode(*jt)->GetID());
for (int jt : order)
apair.second.push_back(aEdge->GetNode(jt)->GetID());
elementsMapEdg[it->second].insert(apair);
}
}
Expand Down Expand Up @@ -2197,28 +2193,27 @@ void FemMesh::writeABAQUS(const std::string &Filename, int elemParam, bool group
anABAQUS_Output << "*Node, NSET=Nall" << std::endl;
// This way we get sorted output.
// See http://forum.freecad.org/viewtopic.php?f=18&t=12646&start=40#p103004
for (VertexMap::iterator it = vertexMap.begin(); it != vertexMap.end(); ++it) {
anABAQUS_Output << it->first << ", "
<< it->second.x << ", "
<< it->second.y << ", "
<< it->second.z << std::endl;
for (const auto& it : vertexMap) {
anABAQUS_Output << it.first << ", "
<< it.second.x << ", "
<< it.second.y << ", "
<< it.second.z << std::endl;
}
anABAQUS_Output << std::endl << std::endl;;


// write volumes to file
std::string elsetname;
if (!elementsMapVol.empty()) {
for (ElementsMap::iterator it = elementsMapVol.begin(); it != elementsMapVol.end(); ++it) {
for (const auto& it : elementsMapVol) {
anABAQUS_Output << "** Volume elements" << std::endl;
anABAQUS_Output << "*Element, TYPE=" << it->first << ", ELSET=Evolumes" << std::endl;
for (NodesMap::iterator jt = it->second.begin(); jt != it->second.end(); ++jt) {
anABAQUS_Output << jt->first;
anABAQUS_Output << "*Element, TYPE=" << it.first << ", ELSET=Evolumes" << std::endl;
for (const auto& jt : it.second) {
anABAQUS_Output << jt.first;
// Calculix allows max 16 entries in one line, a hexa20 has more !
int ct = 0; // counter
bool first_line = true;
for (std::vector<int>::iterator kt = jt->second.begin(); kt != jt->second.end();
++kt, ++ct) {
for (auto kt = jt.second.begin(); kt != jt.second.end(); ++kt, ++ct) {
if (ct < 15) {
anABAQUS_Output << ", " << *kt;
}
Expand All @@ -2239,14 +2234,13 @@ void FemMesh::writeABAQUS(const std::string &Filename, int elemParam, bool group

// write faces to file
if (!elementsMapFac.empty()) {
for (ElementsMap::iterator it = elementsMapFac.begin(); it != elementsMapFac.end(); ++it) {
for (const auto& it : elementsMapFac) {
anABAQUS_Output << "** Face elements" << std::endl;
anABAQUS_Output << "*Element, TYPE=" << it->first << ", ELSET=Efaces" << std::endl;
for (NodesMap::iterator jt = it->second.begin(); jt != it->second.end(); ++jt) {
anABAQUS_Output << jt->first;
for (std::vector<int>::iterator kt = jt->second.begin(); kt != jt->second.end();
++kt) {
anABAQUS_Output << ", " << *kt;
anABAQUS_Output << "*Element, TYPE=" << it.first << ", ELSET=Efaces" << std::endl;
for (const auto& jt : it.second) {
anABAQUS_Output << jt.first;
for (int kt : jt.second) {
anABAQUS_Output << ", " << kt;
}
anABAQUS_Output << std::endl;
}
Expand All @@ -2260,14 +2254,13 @@ void FemMesh::writeABAQUS(const std::string &Filename, int elemParam, bool group

// write edges to file
if (!elementsMapEdg.empty()) {
for (ElementsMap::iterator it = elementsMapEdg.begin(); it != elementsMapEdg.end(); ++it) {
for (const auto& it : elementsMapEdg) {
anABAQUS_Output << "** Edge elements" << std::endl;
anABAQUS_Output << "*Element, TYPE=" << it->first << ", ELSET=Eedges" << std::endl;
for (NodesMap::iterator jt = it->second.begin(); jt != it->second.end(); ++jt) {
anABAQUS_Output << jt->first;
for (std::vector<int>::iterator kt = jt->second.begin(); kt != jt->second.end();
++kt) {
anABAQUS_Output << ", " << *kt;
anABAQUS_Output << "*Element, TYPE=" << it.first << ", ELSET=Eedges" << std::endl;
for (const auto& jt : it.second) {
anABAQUS_Output << jt.first;
for (int kt : jt.second) {
anABAQUS_Output << ", " << kt;
}
anABAQUS_Output << std::endl;
}
Expand All @@ -2293,12 +2286,12 @@ void FemMesh::writeABAQUS(const std::string &Filename, int elemParam, bool group
anABAQUS_Output << std::endl << "** Group data" << std::endl;

std::list<int> groupIDs = myMesh->GetGroupIds();
for (std::list<int>::iterator it = groupIDs.begin(); it != groupIDs.end(); ++it) {
for (int it : groupIDs) {

// get and write group info and group definition
// TODO group element type code has duplicate code of
// PyObject* FemMeshPy::getGroupElementType()
SMDSAbs_ElementType aElementType = myMesh->GetGroup(*it)->GetGroupDS()->GetType();
SMDSAbs_ElementType aElementType = myMesh->GetGroup(it)->GetGroupDS()->GetType();
const char* groupElementType = "";
switch(aElementType) {
case SMDSAbs_All : groupElementType = "All"; break;
Expand All @@ -2310,8 +2303,8 @@ void FemMesh::writeABAQUS(const std::string &Filename, int elemParam, bool group
case SMDSAbs_Ball : groupElementType = "Ball"; break;
default : groupElementType = "Unknown"; break;
}
const char* groupName = myMesh->GetGroup(*it)->GetName();
anABAQUS_Output << "** GroupID: " << (*it) << " --> GroupName: " << groupName
const char* groupName = myMesh->GetGroup(it)->GetName();
anABAQUS_Output << "** GroupID: " << (it) << " --> GroupName: " << groupName
<< " --> GroupElementType: " << groupElementType << std::endl;

if (aElementType == SMDSAbs_Node) {
Expand All @@ -2323,13 +2316,13 @@ void FemMesh::writeABAQUS(const std::string &Filename, int elemParam, bool group

// get and write group elements
std::set<int> ids;
SMDS_ElemIteratorPtr aElemIter = myMesh->GetGroup(*it)->GetGroupDS()->GetElements();
SMDS_ElemIteratorPtr aElemIter = myMesh->GetGroup(it)->GetGroupDS()->GetElements();
while (aElemIter->more()) {
const SMDS_MeshElement* aElement = aElemIter->next();
ids.insert(aElement->GetID());
}
for (std::set<int>::iterator it = ids.begin(); it != ids.end(); ++it) {
anABAQUS_Output << *it << std::endl;
for (int it : ids) {
anABAQUS_Output << it << std::endl;
}

// write newline after each group
Expand Down
Loading

0 comments on commit 009f9f2

Please sign in to comment.