diff --git a/applications/ContactStructuralMechanicsApplication/custom_master_slave_constraints/contact_master_slave_constraint.cpp b/applications/ContactStructuralMechanicsApplication/custom_master_slave_constraints/contact_master_slave_constraint.cpp index a87bec5921fa..07dc922ac478 100644 --- a/applications/ContactStructuralMechanicsApplication/custom_master_slave_constraints/contact_master_slave_constraint.cpp +++ b/applications/ContactStructuralMechanicsApplication/custom_master_slave_constraints/contact_master_slave_constraint.cpp @@ -1,7 +1,7 @@ // KRATOS ______ __ __ _____ __ __ __ // / ____/___ ____ / /_____ ______/ /_/ ___// /________ _______/ /___ ___________ _/ / -// / / / __ \/ __ \/ __/ __ `/ ___/ __/\__ \/ __/ ___/ / / / ___/ __/ / / / ___/ __ `/ / -// / /___/ /_/ / / / / /_/ /_/ / /__/ /_ ___/ / /_/ / / /_/ / /__/ /_/ /_/ / / / /_/ / / +// / / / __ \/ __ \/ __/ __ `/ ___/ __/\__ \/ __/ ___/ / / / ___/ __/ / / / ___/ __ `/ / +// / /___/ /_/ / / / / /_/ /_/ / /__/ /_ ___/ / /_/ / / /_/ / /__/ /_/ /_/ / / / /_/ / / // \____/\____/_/ /_/\__/\__,_/\___/\__//____/\__/_/ \__,_/\___/\__/\__,_/_/ \__,_/_/ MECHANICS // // License: BSD License @@ -90,7 +90,7 @@ MasterSlaveConstraint::Pointer ContactMasterSlaveConstraint::Create( const VectorType& rConstantVector ) const { - return Kratos::make_shared(Id, rMasterDofsVector, rSlaveDofsVector, rRelationMatrix, rConstantVector); + return Kratos::make_intrusive(Id, rMasterDofsVector, rSlaveDofsVector, rRelationMatrix, rConstantVector); } /***********************************************************************************/ @@ -106,7 +106,7 @@ MasterSlaveConstraint::Pointer ContactMasterSlaveConstraint::Create( const double Constant ) const { - return Kratos::make_shared(Id, rMasterNode, rMasterVariable, rSlaveNode, rSlaveVariable, Weight, Constant); + return Kratos::make_intrusive(Id, rMasterNode, rMasterVariable, rSlaveNode, rSlaveVariable, Weight, Constant); } /***********************************************************************************/ diff --git a/applications/ContactStructuralMechanicsApplication/custom_processes/mpc_contact_search_process.cpp b/applications/ContactStructuralMechanicsApplication/custom_processes/mpc_contact_search_process.cpp index 4d0fd37f0837..4a28e2d1d038 100644 --- a/applications/ContactStructuralMechanicsApplication/custom_processes/mpc_contact_search_process.cpp +++ b/applications/ContactStructuralMechanicsApplication/custom_processes/mpc_contact_search_process.cpp @@ -1,7 +1,7 @@ // KRATOS ______ __ __ _____ __ __ __ // / ____/___ ____ / /_____ ______/ /_/ ___// /________ _______/ /___ ___________ _/ / -// / / / __ \/ __ \/ __/ __ `/ ___/ __/\__ \/ __/ ___/ / / / ___/ __/ / / / ___/ __ `/ / -// / /___/ /_/ / / / / /_/ /_/ / /__/ /_ ___/ / /_/ / / /_/ / /__/ /_/ /_/ / / / /_/ / / +// / / / __ \/ __ \/ __/ __ `/ ___/ __/\__ \/ __/ ___/ / / / ___/ __/ / / / ___/ __ `/ / +// / /___/ /_/ / / / / /_/ /_/ / /__/ /_ ___/ / /_/ / / /_/ / /__/ /_/ /_/ / / / /_/ / / // \____/\____/_/ /_/\__/\__,_/\___/\__//____/\__/_/ \__,_/\___/\__/\__,_/_/ \__,_/_/ MECHANICS // // License: BSD License @@ -128,7 +128,7 @@ Condition::Pointer MPCContactSearchProcess::Ad // Creating constraint if (p_cond.get() != nullptr) { - MasterSlaveConstraint::Pointer p_new_const = Kratos::make_shared(GetMaximumConstraintsIds() + 1); + MasterSlaveConstraint::Pointer p_new_const = Kratos::make_intrusive(GetMaximumConstraintsIds() + 1); p_new_const->Set(ACTIVE); const auto& r_process_info = rComputingModelPart.GetProcessInfo(); p_new_const->Initialize(r_process_info); diff --git a/kratos/constraints/linear_master_slave_constraint.h b/kratos/constraints/linear_master_slave_constraint.h index e84c575bcec0..a8d9f48eefb7 100644 --- a/kratos/constraints/linear_master_slave_constraint.h +++ b/kratos/constraints/linear_master_slave_constraint.h @@ -202,7 +202,7 @@ class KRATOS_API(KRATOS_CORE) LinearMasterSlaveConstraint ) const override { KRATOS_TRY - return Kratos::make_shared(Id, rMasterDofsVector, rSlaveDofsVector, rRelationMatrix, rConstantVector); + return Kratos::make_intrusive(Id, rMasterDofsVector, rSlaveDofsVector, rRelationMatrix, rConstantVector); KRATOS_CATCH(""); } @@ -228,7 +228,7 @@ class KRATOS_API(KRATOS_CORE) LinearMasterSlaveConstraint ) const override { KRATOS_TRY - return Kratos::make_shared(Id, rMasterNode, rMasterVariable, rSlaveNode, rSlaveVariable, Weight, Constant); + return Kratos::make_intrusive(Id, rMasterNode, rMasterVariable, rSlaveNode, rSlaveVariable, Weight, Constant); KRATOS_CATCH(""); } @@ -241,7 +241,7 @@ class KRATOS_API(KRATOS_CORE) LinearMasterSlaveConstraint { KRATOS_TRY - MasterSlaveConstraint::Pointer p_new_const = Kratos::make_shared(*this); + MasterSlaveConstraint::Pointer p_new_const = Kratos::make_intrusive(*this); p_new_const->SetId(NewId); p_new_const->SetData(this->GetData()); p_new_const->Set(Flags(*this)); diff --git a/kratos/includes/master_slave_constraint.h b/kratos/includes/master_slave_constraint.h index 53b914ac7c7d..c6b86fecd318 100644 --- a/kratos/includes/master_slave_constraint.h +++ b/kratos/includes/master_slave_constraint.h @@ -106,7 +106,7 @@ class KRATOS_API(KRATOS_CORE) MasterSlaveConstraint typedef Kratos::Variable VariableType; /// Pointer definition of MasterSlaveConstraint - KRATOS_CLASS_POINTER_DEFINITION(MasterSlaveConstraint); + KRATOS_CLASS_INTRUSIVE_POINTER_DEFINITION(MasterSlaveConstraint); ///@} ///@name Enum's @@ -215,7 +215,7 @@ class KRATOS_API(KRATOS_CORE) MasterSlaveConstraint KRATOS_TRY KRATOS_WARNING("MasterSlaveConstraint") << " Call base class constraint Clone " << std::endl; - MasterSlaveConstraint::Pointer p_new_const = Kratos::make_shared(*this); + MasterSlaveConstraint::Pointer p_new_const = Kratos::make_intrusive(*this); p_new_const->SetId(NewId); p_new_const->SetData(this->GetData()); p_new_const->Set(Flags(*this)); @@ -520,7 +520,7 @@ class KRATOS_API(KRATOS_CORE) MasterSlaveConstraint * @brief Check if the Data exists with Has(..) methods: * @param rThisVariable The variable to be check */ - template + template bool Has(const Variable& rThisVariable) const { return mData.Has(rThisVariable); @@ -612,6 +612,28 @@ class KRATOS_API(KRATOS_CORE) MasterSlaveConstraint DataValueContainer mData; /// Pointer to the data related to this constraint + ///@} + ///@name Private operations + ///@{ + + //********************************************* + //this block is needed for refcounting + mutable std::atomic mReferenceCounter{0}; + + friend void intrusive_ptr_add_ref(const MasterSlaveConstraint* x) + { + x->mReferenceCounter.fetch_add(1, std::memory_order_relaxed); + } + + friend void intrusive_ptr_release(const MasterSlaveConstraint* x) + { + if (x->mReferenceCounter.fetch_sub(1, std::memory_order_release) == 1) { + std::atomic_thread_fence(std::memory_order_acquire); + delete x; + } + } + //********************************************* + ///@} ///@name Serialization ///@{ diff --git a/kratos/tests/cpp_tests/sources/test_constraint.cpp b/kratos/tests/cpp_tests/sources/test_constraint.cpp index 4926c27b557f..9552525bb52d 100644 --- a/kratos/tests/cpp_tests/sources/test_constraint.cpp +++ b/kratos/tests/cpp_tests/sources/test_constraint.cpp @@ -95,7 +95,7 @@ namespace Kratos { const auto& r_master_dofs = p_const->GetMasterDofsVector(); // Create empty constraint - MasterSlaveConstraint::Pointer p_new_const = Kratos::make_shared(2); + MasterSlaveConstraint::Pointer p_new_const = Kratos::make_intrusive(2); p_new_const->SetLocalSystem(matrix, vector, r_process_info); p_new_const->SetDofList(r_slave_dofs, r_master_dofs, r_process_info); diff --git a/kratos/utilities/rve_periodicity_utility.cpp b/kratos/utilities/rve_periodicity_utility.cpp index e92493b8bde2..9bae65ef1185 100644 --- a/kratos/utilities/rve_periodicity_utility.cpp +++ b/kratos/utilities/rve_periodicity_utility.cpp @@ -186,7 +186,7 @@ MasterSlaveConstraint::Pointer RVEPeriodicityUtility::GenerateConstraint( for (IndexType i = 0; i < rMasterIds.size(); ++i) master_dofs.push_back(mrModelPart.pGetNode(rMasterIds[i])->pGetDof(rVar)); - auto pconstraint = Kratos::make_shared(rConstraintId, master_dofs, slave_dofs, rRelationMatrix, rTranslationVector); + auto pconstraint = Kratos::make_intrusive(rConstraintId, master_dofs, slave_dofs, rRelationMatrix, rTranslationVector); rConstraintId++; return pconstraint; }