Skip to content

Commit

Permalink
Merge pull request #11854 from KratosMultiphysics/core/improve-master…
Browse files Browse the repository at this point in the history
…-slave-constraints

[Core] Improve Master-Slave Constraint Insertion
  • Loading branch information
matekelemen authored Mar 17, 2024
2 parents 358979a + a9a5da1 commit 9bc21c6
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 71 deletions.
22 changes: 17 additions & 5 deletions kratos/includes/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,11 +728,23 @@ class Mesh : public DataValueContainer, public Flags
return mpMasterSlaveConstraints->size();
}

/** Inserts a master-slave constraint in the mesh.
*/
void AddMasterSlaveConstraint(typename MasterSlaveConstraintType::Pointer pNewMasterSlaveConstraint)
{
mpMasterSlaveConstraints->insert(mpMasterSlaveConstraints->begin(), pNewMasterSlaveConstraint);
/// @brief Insert a @ref MasterSlaveConstraint.
/// @returns @a false if a constraint with identical ID already exists
/// in the mesh and the insertion did not take place, @a true
/// otherwise.
bool AddMasterSlaveConstraint(typename MasterSlaveConstraintType::Pointer pNewMasterSlaveConstraint)
{
const auto it_existing_constraint = mpMasterSlaveConstraints->find(pNewMasterSlaveConstraint->Id());
if (it_existing_constraint == mpMasterSlaveConstraints->end()) {
// PointerVectorSet::insert takes a position argument to insert the
// item at but ignores it, which makes it completely irrelevant to
// properly compute (an estimate) of the new constraint's position
// in the container => pass begin as position.
const auto it_insert_position = mpMasterSlaveConstraints->begin();
mpMasterSlaveConstraints->insert(it_insert_position, pNewMasterSlaveConstraint);
return true;
}
return false;
}

/** Returns the MasterSlaveConstraint::Pointer corresponding to it's identifier */
Expand Down
150 changes: 84 additions & 66 deletions kratos/sources/model_part.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1229,45 +1229,53 @@ void ModelPart::AddMasterSlaveConstraints(std::vector<IndexType> const& MasterSl
KRATOS_CATCH("");
}

/** Inserts an master-slave constraint in the current mesh.
*/
/// @brief Construct a new @ref MasterSlaveConstraint and insert it into the specified @ref Mesh.
/// @note The constraint is created by the root @ref ModelPart and inserted into the root mesh as well.
/// @throws if a constraint with the same ID already exists in the target mesh.
ModelPart::MasterSlaveConstraintType::Pointer ModelPart::CreateNewMasterSlaveConstraint(const std::string& ConstraintName,
IndexType Id,
ModelPart::DofsVectorType& rMasterDofsVector,
ModelPart::DofsVectorType& rSlaveDofsVector,
const ModelPart::MatrixType& RelationMatrix,
const ModelPart::VectorType& ConstantVector,
IndexType ThisIndex)
IndexType Id,
ModelPart::DofsVectorType& rMasterDofsVector,
ModelPart::DofsVectorType& rSlaveDofsVector,
const ModelPart::MatrixType& RelationMatrix,
const ModelPart::VectorType& ConstantVector,
IndexType ThisIndex)
{

KRATOS_TRY
if (IsSubModelPart())
{
ModelPart::MasterSlaveConstraintType::Pointer p_new_constraint = mpParentModelPart->CreateNewMasterSlaveConstraint(ConstraintName, Id, rMasterDofsVector,
rSlaveDofsVector,
RelationMatrix,
ConstantVector,
ThisIndex);
GetMesh(ThisIndex).AddMasterSlaveConstraint(p_new_constraint);
GetMesh(ThisIndex).MasterSlaveConstraints().Unique();

return p_new_constraint;
}
MeshType& r_mesh = GetMesh(ThisIndex);
ModelPart::MasterSlaveConstraintType::Pointer p_new_constraint;

auto existing_constraint_iterator = GetMesh(ThisIndex).MasterSlaveConstraints().find(Id);
KRATOS_ERROR_IF(existing_constraint_iterator != GetMesh(ThisIndex).MasterSlaveConstraintsEnd() )
<< "trying to construct an master-slave constraint with ID " << Id << " however a constraint with the same Id already exists";


//create the new element
ModelPart::MasterSlaveConstraintType const& r_clone_constraint = KratosComponents<MasterSlaveConstraintType>::Get(ConstraintName);
ModelPart::MasterSlaveConstraintType::Pointer p_new_constraint = r_clone_constraint.Create(Id, rMasterDofsVector,
rSlaveDofsVector,
RelationMatrix,
ConstantVector);
if (IsSubModelPart()) {
// Defer constraint construction to the root model part
p_new_constraint = mpParentModelPart->CreateNewMasterSlaveConstraint(
ConstraintName,
Id,
rMasterDofsVector,
rSlaveDofsVector,
RelationMatrix,
ConstantVector,
ThisIndex);

// Add the constraint
if (&r_mesh != &mpParentModelPart->GetMesh(ThisIndex)) {
KRATOS_ERROR_IF_NOT(r_mesh.AddMasterSlaveConstraint(p_new_constraint))
<< "trying to insert a master-slave constraint with ID "
<< Id << " but a constraint with the same ID already exists\n";
}
} else /*IsSubModelPart*/ {
// Construct the new constraint
ModelPart::MasterSlaveConstraintType const& r_registered_constraint = KratosComponents<MasterSlaveConstraintType>::Get(ConstraintName);
p_new_constraint = r_registered_constraint.Create(
Id,
rMasterDofsVector,
rSlaveDofsVector,
RelationMatrix,
ConstantVector);

GetMesh(ThisIndex).AddMasterSlaveConstraint(p_new_constraint);
GetMesh(ThisIndex).MasterSlaveConstraints().Unique();
KRATOS_ERROR_IF_NOT(r_mesh.AddMasterSlaveConstraint(p_new_constraint))
<< "trying to insert a master-slave constraint with ID "
<< Id << " but a constraint with the same ID already exists\n";
}

return p_new_constraint;
KRATOS_CATCH("")
Expand All @@ -1284,46 +1292,56 @@ ModelPart::MasterSlaveConstraintType::Pointer ModelPart::CreateNewMasterSlaveCon
const double Constant,
IndexType ThisIndex)
{
KRATOS_ERROR_IF_NOT(rMasterNode.HasDofFor(rMasterVariable))
<< "master node " << rMasterNode.Id() << " has no variable " << rMasterVariable.Name() << "\n";

KRATOS_TRY
if (rMasterNode.HasDofFor(rMasterVariable) && rSlaveNode.HasDofFor(rSlaveVariable) )
{
if (IsSubModelPart())
{
ModelPart::MasterSlaveConstraintType::Pointer p_new_constraint = mpParentModelPart->CreateNewMasterSlaveConstraint(ConstraintName, Id, rMasterNode,
rMasterVariable,
rSlaveNode,
rSlaveVariable,
Weight,
Constant,
ThisIndex);

GetMesh(ThisIndex).AddMasterSlaveConstraint(p_new_constraint);
GetMesh(ThisIndex).MasterSlaveConstraints().Unique();
return p_new_constraint;
}
KRATOS_ERROR_IF_NOT(rSlaveNode.HasDofFor(rSlaveVariable))
<< "slave node " << rSlaveNode.Id() << " has no variable " << rSlaveVariable.Name() << "\n";

KRATOS_ERROR_IF(GetMesh(ThisIndex).HasMasterSlaveConstraint(Id))
<< "trying to construct an master-slave constraint with ID " << Id << " however a constraint with the same Id already exists";
KRATOS_TRY

ModelPart::MasterSlaveConstraintType::Pointer p_new_constraint;
MeshType& r_mesh = this->GetMesh(ThisIndex);

//create the new element
if (IsSubModelPart()) {
// Defer constraint construction to the root model part
p_new_constraint = mpParentModelPart->CreateNewMasterSlaveConstraint(
ConstraintName,
Id,
rMasterNode,
rMasterVariable,
rSlaveNode,
rSlaveVariable,
Weight,
Constant,
ThisIndex);

// Insert the constraint
if (&r_mesh != &mpParentModelPart->GetMesh(ThisIndex)) {
KRATOS_ERROR_IF_NOT(r_mesh.AddMasterSlaveConstraint(p_new_constraint))
<< "trying to insert a master-slave constraint with ID "
<< Id << " but a constraint with the same ID already exists\n";
}
} else { /*IsSubModelPart*/
// Construct the new constraint
ModelPart::MasterSlaveConstraintType const& r_clone_constraint = KratosComponents<MasterSlaveConstraintType>::Get(ConstraintName);
ModelPart::MasterSlaveConstraintType::Pointer p_new_constraint = r_clone_constraint.Create(Id, rMasterNode,
rMasterVariable,
rSlaveNode,
rSlaveVariable,
Weight,
Constant);

GetMesh(ThisIndex).AddMasterSlaveConstraint(p_new_constraint);
GetMesh(ThisIndex).MasterSlaveConstraints().Unique();
return p_new_constraint;
} else
{
KRATOS_ERROR << "Master or Slave node does not have requested DOF " <<std::endl;
p_new_constraint = r_clone_constraint.Create(
Id,
rMasterNode,
rMasterVariable,
rSlaveNode,
rSlaveVariable,
Weight,
Constant);

// Insert the constraint
KRATOS_ERROR_IF_NOT(r_mesh.AddMasterSlaveConstraint(p_new_constraint))
<< "trying to insert a master-slave constraint with ID "
<< Id << " but a constraint with the same ID already exists\n";
}

return p_new_constraint;

KRATOS_CATCH("")

}
Expand Down

0 comments on commit 9bc21c6

Please sign in to comment.