Skip to content

Commit

Permalink
Merge pull request #2897 from KratosMultiphysics/core/missing-remove-…
Browse files Browse the repository at this point in the history
…constraint

[Core] Missing remove method for constraint in model part
  • Loading branch information
Vicente Mataix Ferrándiz authored Sep 24, 2018
2 parents 658cafb + 262086e commit e1c54e7
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 40 deletions.
63 changes: 41 additions & 22 deletions kratos/includes/model_part.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,19 +442,19 @@ class KRATOS_API(KRATOS_CORE) ModelPart : public DataValueContainer, public Flag
*/
void RemoveNodeFromAllLevels(NodeType::Pointer pThisNode, IndexType ThisIndex = 0);

/** erases all nodes identified by "identifier_flag" by removing the pointer.
/** erases all nodes identified by "IdentifierFlag" by removing the pointer.
* Pointers are erased from this level downwards
* nodes will be automatically destructured
* when no pointer is left to them
*/
void RemoveNodes(Flags identifier_flag = TO_ERASE);
void RemoveNodes(Flags IdentifierFlag = TO_ERASE);

/** erases all nodes identified by "identifier_flag" by removing the pointer.
/** erases all nodes identified by "IdentifierFlag" by removing the pointer.
* Pointers will be erase from all levels
* nodes will be automatically destructured
* when no pointer is left to them
*/
void RemoveNodesFromAllLevels(Flags identifier_flag = TO_ERASE);
void RemoveNodesFromAllLevels(Flags IdentifierFlag = TO_ERASE);

/** this function gives back the "root" model part, that is the model_part that has no father */
ModelPart& GetRootModelPart();
Expand Down Expand Up @@ -708,8 +708,9 @@ class KRATOS_API(KRATOS_CORE) ModelPart : public DataValueContainer, public Flag
KRATOS_CATCH("")
}

/** Creates a new master-slave constraint in the current modelpart.
* //TODO: replace these 3 functions by one that perfectly forwards arguments, then just define these 3 interfaces on the pybind side
/**
* @brief Creates a new master-slave constraint in the current modelpart.
* @todo Replace these 3 functions by one that perfectly forwards arguments, then just define these 3 interfaces on the pybind side
*/
MasterSlaveConstraint::Pointer CreateNewMasterSlaveConstraint(const std::string& ConstraintName,
IndexType Id,
Expand Down Expand Up @@ -739,22 +740,40 @@ class KRATOS_API(KRATOS_CORE) ModelPart : public DataValueContainer, public Flag
double Constant,
IndexType ThisIndex = 0);

/** Remove the master-slave constraint with given Id from mesh with ThisIndex in this modelpart and all its subs.
*/
/**
* @brief Remove the master-slave constraint with given Id from mesh with ThisIndex in this modelpart and all its subs.
*/
void RemoveMasterSlaveConstraint(IndexType MasterSlaveConstraintId, IndexType ThisIndex = 0);

/** Remove given master-slave constraint from mesh with ThisIndex in this modelpart and all its subs.
*/
/**
* @brief Remove given master-slave constraint from mesh with ThisIndex in this modelpart and all its subs.
*/
void RemoveMasterSlaveConstraint(MasterSlaveConstraintType& ThisMasterSlaveConstraint, IndexType ThisIndex = 0);

/** Remove the master-slave constraint with given Id from mesh with ThisIndex in parents, itself and children.
*/
/**
* @brief Remove the master-slave constraint with given Id from mesh with ThisIndex in parents, itself and children.
*/
void RemoveMasterSlaveConstraintFromAllLevels(IndexType MasterSlaveConstraintId, IndexType ThisIndex = 0);

/** Remove given master-slave constraint from mesh with ThisIndex in parents, itself and children.
*/
/**
* @brief Remove given master-slave constraint from mesh with ThisIndex in parents, itself and children.
*/
void RemoveMasterSlaveConstraintFromAllLevels(MasterSlaveConstraintType& ThisMasterSlaveConstraint, IndexType ThisIndex = 0);

/**
* @brief It erases all constraints identified by "IdentifierFlag" by removing the pointer.
* @details Pointers are erased from this level downwards nodes will be automatically destructured when no pointer is left to them
* @param IdentifierFlag The flag that identifies the constraints to remove
*/
void RemoveMasterSlaveConstraints(Flags IdentifierFlag = TO_ERASE);

/**
* @brief It erases all constraints identified by "IdentifierFlag" by removing the pointer.
* @details Pointers will be erase from all levels nodes will be automatically destructured when no pointer is left to them
* @param IdentifierFlag The flag that identifies the constraints to remove
*/
void RemoveMasterSlaveConstraintsFromAllLevels(Flags IdentifierFlag = TO_ERASE);

/** Returns the MasterSlaveConstraint::Pointer corresponding to it's identifier */
MasterSlaveConstraintType::Pointer pGetMasterSlaveConstraint(IndexType ConstraintId, IndexType ThisIndex = 0);

Expand Down Expand Up @@ -1009,19 +1028,19 @@ class KRATOS_API(KRATOS_CORE) ModelPart : public DataValueContainer, public Flag
*/
void RemoveElementFromAllLevels(ElementType::Pointer pThisElement, IndexType ThisIndex = 0);

/** erases all elements identified by "identifier_flag" by removing the pointer.
/** erases all elements identified by "IdentifierFlag" by removing the pointer.
* Pointers are erased from this level downwards
* nodes will be automatically destructured
* when no pointer is left to them
*/
void RemoveElements(Flags identifier_flag = TO_ERASE);
void RemoveElements(Flags IdentifierFlag = TO_ERASE);

/** erases all elements identified by "identifier_flag" by removing the pointer.
/** erases all elements identified by "IdentifierFlag" by removing the pointer.
* Pointers will be erase from all levels
* nodes will be automatically destructured
* when no pointer is left to them
*/
void RemoveElementsFromAllLevels(Flags identifier_flag = TO_ERASE);
void RemoveElementsFromAllLevels(Flags IdentifierFlag = TO_ERASE);

ElementIterator ElementsBegin(IndexType ThisIndex = 0)
{
Expand Down Expand Up @@ -1187,19 +1206,19 @@ class KRATOS_API(KRATOS_CORE) ModelPart : public DataValueContainer, public Flag
*/
void RemoveConditionFromAllLevels(ConditionType::Pointer pThisCondition, IndexType ThisIndex = 0);

/** erases all elements identified by "identifier_flag" by removing the pointer.
/** erases all elements identified by "IdentifierFlag" by removing the pointer.
* Pointers are erased from this level downwards
* nodes will be automatically destructured
* when no pointer is left to them
*/
void RemoveConditions(Flags identifier_flag = TO_ERASE);
void RemoveConditions(Flags IdentifierFlag = TO_ERASE);

/** erases all elements identified by "identifier_flag" by removing the pointer.
/** erases all elements identified by "IdentifierFlag" by removing the pointer.
* Pointers will be erase from all levels
* nodes will be automatically destructured
* when no pointer is left to them
*/
void RemoveConditionsFromAllLevels(Flags identifier_flag = TO_ERASE);
void RemoveConditionsFromAllLevels(Flags IdentifierFlag = TO_ERASE);

ConditionIterator ConditionsBegin(IndexType ThisIndex = 0)
{
Expand Down
2 changes: 2 additions & 0 deletions kratos/python/add_model_part_to_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,8 @@ void AddModelPartToPython(pybind11::module& m)
.def("RemoveMasterSlaveConstraint", ModelPartRemoveMasterSlaveConstraint2)
.def("RemoveMasterSlaveConstraintFromAllLevels", ModelPartRemoveMasterSlaveConstraintFromAllLevels1)
.def("RemoveMasterSlaveConstraintFromAllLevels", ModelPartRemoveMasterSlaveConstraintFromAllLevels2)
.def("RemoveMasterSlaveConstraints", &ModelPart::RemoveMasterSlaveConstraints)
.def("RemoveMasterSlaveConstraintsFromAllLevels", &ModelPart::RemoveMasterSlaveConstraintsFromAllLevels)
.def("AddMasterSlaveConstraint", ModelPartAddMasterSlaveConstraint)
.def("AddMasterSlaveConstraints", AddMasterSlaveConstraintsByIds)
.def("CreateNewMasterSlaveConstraint",CreateNewMasterSlaveConstraint1, return_value_policy::reference_internal)
Expand Down
80 changes: 62 additions & 18 deletions kratos/sources/model_part.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ void ModelPart::RemoveNodeFromAllLevels(ModelPart::NodeType::Pointer pThisNode,
RemoveNode(pThisNode, ThisIndex);
}

void ModelPart::RemoveNodes(Flags identifier_flag)
void ModelPart::RemoveNodes(Flags IdentifierFlag)
{
// This method is optimized to free the memory
//loop over all the meshes
Expand All @@ -462,7 +462,7 @@ void ModelPart::RemoveNodes(Flags identifier_flag)
{
ModelPart::NodesContainerType::iterator i_node = i_mesh->NodesBegin() + i;

if( i_node->IsNot(identifier_flag) )
if( i_node->IsNot(IdentifierFlag) )
erase_count++;
}

Expand All @@ -473,20 +473,20 @@ void ModelPart::RemoveNodes(Flags identifier_flag)

for(ModelPart::NodesContainerType::iterator i_node = temp_nodes_container.begin() ; i_node != temp_nodes_container.end() ; i_node++)
{
if( i_node->IsNot(identifier_flag) )
if( i_node->IsNot(IdentifierFlag) )
(i_mesh->Nodes()).push_back(std::move(*(i_node.base())));
}
}

//now recursively remove the nodes in the submodelparts
for (SubModelPartIterator i_sub_model_part = SubModelPartsBegin(); i_sub_model_part != SubModelPartsEnd(); i_sub_model_part++)
i_sub_model_part->RemoveNodes(identifier_flag);
i_sub_model_part->RemoveNodes(IdentifierFlag);
}

void ModelPart::RemoveNodesFromAllLevels(Flags identifier_flag)
void ModelPart::RemoveNodesFromAllLevels(Flags IdentifierFlag)
{
ModelPart& root_model_part = GetRootModelPart();
root_model_part.RemoveNodes(identifier_flag);
root_model_part.RemoveNodes(IdentifierFlag);
}

ModelPart& ModelPart::GetRootModelPart()
Expand Down Expand Up @@ -812,7 +812,7 @@ void ModelPart::RemoveElementFromAllLevels(ModelPart::ElementType::Pointer pThis
RemoveElement(pThisElement, ThisIndex);
}

void ModelPart::RemoveElements(Flags identifier_flag)
void ModelPart::RemoveElements(Flags IdentifierFlag)
{
// This method is optimized to free the memory
//loop over all the meshes
Expand All @@ -827,7 +827,7 @@ void ModelPart::RemoveElements(Flags identifier_flag)
{
auto i_elem = i_mesh->ElementsBegin() + i;

if( i_elem->IsNot(identifier_flag) )
if( i_elem->IsNot(IdentifierFlag) )
erase_count++;
}

Expand All @@ -838,20 +838,20 @@ void ModelPart::RemoveElements(Flags identifier_flag)

for(ModelPart::ElementsContainerType::iterator i_elem = temp_elements_container.begin() ; i_elem != temp_elements_container.end() ; i_elem++)
{
if( i_elem->IsNot(identifier_flag) )
if( i_elem->IsNot(IdentifierFlag) )
(i_mesh->Elements()).push_back(std::move(*(i_elem.base())));
}
}

//now recursively remove the elements in the submodelparts
for (SubModelPartIterator i_sub_model_part = SubModelPartsBegin(); i_sub_model_part != SubModelPartsEnd(); i_sub_model_part++)
i_sub_model_part->RemoveElements(identifier_flag);
i_sub_model_part->RemoveElements(IdentifierFlag);
}

void ModelPart::RemoveElementsFromAllLevels(Flags identifier_flag)
void ModelPart::RemoveElementsFromAllLevels(Flags IdentifierFlag)
{
ModelPart& root_model_part = GetRootModelPart();
root_model_part.RemoveElements(identifier_flag);
root_model_part.RemoveElements(IdentifierFlag);
}


Expand Down Expand Up @@ -1119,6 +1119,50 @@ void ModelPart::RemoveMasterSlaveConstraintFromAllLevels(ModelPart::MasterSlaveC
RemoveMasterSlaveConstraint(ThisMasterSlaveConstraint, ThisIndex);
}

/***********************************************************************************/
/***********************************************************************************/

void ModelPart::RemoveMasterSlaveConstraints(Flags IdentifierFlag)
{
// This method is optimized to free the memory loop over all the meshes
auto& meshes = this->GetMeshes();
for(auto it_mesh = meshes.begin() ; it_mesh != meshes.end() ; it_mesh++) {
// Count the constraints to be erase
const SizeType nconstraints = it_mesh->MasterSlaveConstraints().size();
SizeType erase_count = 0;
#pragma omp parallel for reduction(+:erase_count)
for(int i=0; i<static_cast<int>(nconstraints); ++i) {
auto it_const = it_mesh->MasterSlaveConstraintsBegin() + i;

if( it_const->IsNot(IdentifierFlag) )
erase_count++;
}

ModelPart::MasterSlaveConstraintContainerType temp_constraints_container;
temp_constraints_container.reserve(it_mesh->MasterSlaveConstraints().size() - erase_count);

temp_constraints_container.swap(it_mesh->MasterSlaveConstraints());

for(auto it_const = temp_constraints_container.begin() ; it_const != temp_constraints_container.end(); it_const++) {
if( it_const->IsNot(IdentifierFlag) )
(it_mesh->MasterSlaveConstraints()).push_back(std::move(*(it_const.base())));
}
}

// Now recursively remove the constraints in the submodelparts
for (SubModelPartIterator i_sub_model_part = SubModelPartsBegin(); i_sub_model_part != SubModelPartsEnd(); i_sub_model_part++)
i_sub_model_part->RemoveMasterSlaveConstraints(IdentifierFlag);
}

/***********************************************************************************/
/***********************************************************************************/

void ModelPart::RemoveMasterSlaveConstraintsFromAllLevels(Flags IdentifierFlag)
{
ModelPart& root_model_part = GetRootModelPart();
root_model_part.RemoveMasterSlaveConstraints(IdentifierFlag);
}

/** Returns the MasterSlaveConstraint::Pointer corresponding to it's identifier */
ModelPart::MasterSlaveConstraintType::Pointer ModelPart::pGetMasterSlaveConstraint(ModelPart::IndexType MasterSlaveConstraintId, IndexType ThisIndex)
{
Expand Down Expand Up @@ -1332,7 +1376,7 @@ void ModelPart::RemoveConditionFromAllLevels(ModelPart::ConditionType::Pointer p
RemoveCondition(pThisCondition, ThisIndex);
}

void ModelPart::RemoveConditions(Flags identifier_flag)
void ModelPart::RemoveConditions(Flags IdentifierFlag)
{
// This method is optimized to free the memory
//loop over all the meshes
Expand All @@ -1347,7 +1391,7 @@ void ModelPart::RemoveConditions(Flags identifier_flag)
{
auto i_cond = i_mesh->ConditionsBegin() + i;

if( i_cond->IsNot(identifier_flag) )
if( i_cond->IsNot(IdentifierFlag) )
erase_count++;
}

Expand All @@ -1358,20 +1402,20 @@ void ModelPart::RemoveConditions(Flags identifier_flag)

for(ModelPart::ConditionsContainerType::iterator i_cond = temp_conditions_container.begin() ; i_cond != temp_conditions_container.end() ; i_cond++)
{
if( i_cond->IsNot(identifier_flag) )
if( i_cond->IsNot(IdentifierFlag) )
(i_mesh->Conditions()).push_back(std::move(*(i_cond.base())));
}
}

//now recursively remove the conditions in the submodelparts
for (SubModelPartIterator i_sub_model_part = SubModelPartsBegin(); i_sub_model_part != SubModelPartsEnd(); i_sub_model_part++)
i_sub_model_part->RemoveConditions(identifier_flag);
i_sub_model_part->RemoveConditions(IdentifierFlag);
}

void ModelPart::RemoveConditionsFromAllLevels(Flags identifier_flag)
void ModelPart::RemoveConditionsFromAllLevels(Flags IdentifierFlag)
{
ModelPart& root_model_part = GetRootModelPart();
root_model_part.RemoveConditions(identifier_flag);
root_model_part.RemoveConditions(IdentifierFlag);
}


Expand Down

0 comments on commit e1c54e7

Please sign in to comment.