From 4d4ed882935f16f6b1a22aec103f822cd9a8efaf Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Tue, 23 Apr 2024 17:54:50 +0200 Subject: [PATCH 01/16] Alternative implementation --- kratos/includes/define.h | 35 +++++++++++++++++------- kratos/includes/kratos_application.h | 26 +++++++++++++++++- kratos/includes/registry.h | 13 +++++++++ kratos/sources/kratos_application.cpp | 38 ++++++++++++++++++++++++++- kratos/sources/registry.cpp | 11 ++++++++ 5 files changed, 111 insertions(+), 12 deletions(-) diff --git a/kratos/includes/define.h b/kratos/includes/define.h index 92d3c887d1c0..1374b331b75d 100644 --- a/kratos/includes/define.h +++ b/kratos/includes/define.h @@ -703,36 +703,51 @@ catch(...) { Block KRATOS_THROW_ERROR(std::runtime_error, "Unknown error", MoreI #ifdef KRATOS_REGISTER_ELEMENT #undef KRATOS_REGISTER_ELEMENT #endif -#define KRATOS_REGISTER_ELEMENT(name, reference) \ - KratosComponents::Add(name, reference); \ +#define KRATOS_REGISTER_ELEMENT(name, reference) \ + KratosComponents::Add(name, reference); \ + if(!Registry::HasItem("elements."+Registry::GetCurrentSource()+"."+name)){ \ + Registry::AddItem("elements."+Registry::GetCurrentSource()+"."+name); \ + } \ Serializer::Register(name, reference); #ifdef KRATOS_REGISTER_CONDITION #undef KRATOS_REGISTER_CONDITION #endif -#define KRATOS_REGISTER_CONDITION(name, reference) \ - KratosComponents::Add(name, reference); \ +#define KRATOS_REGISTER_CONDITION(name, reference) \ + KratosComponents::Add(name, reference); \ + if(!Registry::HasItem("conditions."+Registry::GetCurrentSource()+"."+name)){ \ + Registry::AddItem("conditions."+Registry::GetCurrentSource()+"."+name); \ + } \ Serializer::Register(name, reference); #ifdef KRATOS_REGISTER_CONSTRAINT #undef KRATOS_REGISTER_CONSTRAINT #endif -#define KRATOS_REGISTER_CONSTRAINT(name, reference) \ - KratosComponents::Add(name, reference); \ +#define KRATOS_REGISTER_CONSTRAINT(name, reference) \ + KratosComponents::Add(name, reference); \ + if(!Registry::HasItem("constraints."+Registry::GetCurrentSource()+"."+name)){ \ + Registry::AddItem("constraints."+Registry::GetCurrentSource()+"."+name); \ + } \ Serializer::Register(name, reference); #ifdef KRATOS_REGISTER_MODELER #undef KRATOS_REGISTER_MODELER #endif -#define KRATOS_REGISTER_MODELER(name, reference) \ - KratosComponents::Add(name, reference); \ +#define KRATOS_REGISTER_MODELER(name, reference) \ + KratosComponents::Add(name, reference); \ + if(!Registry::HasItem("modelers."+Registry::GetCurrentSource()+"."+name)){ \ + Registry::AddItem("modelers."+Registry::GetCurrentSource()+"."+name); \ + } \ Serializer::Register(name, reference); #ifdef KRATOS_REGISTER_CONSTITUTIVE_LAW #undef KRATOS_REGISTER_CONSTITUTIVE_LAW #endif -#define KRATOS_REGISTER_CONSTITUTIVE_LAW(name, reference) \ - KratosComponents::Add(name, reference); \ +#define KRATOS_REGISTER_CONSTITUTIVE_LAW(name, reference) \ + KratosComponents::Add(name, reference); \ + if(!Registry::HasItem("constitutive_laws."+Registry::GetCurrentSource()+"."+name)){ \ + Registry::AddItem("constitutive_laws."+Registry::GetCurrentSource()+"."+name); \ + } \ Serializer::Register(name, reference); #define KRATOS_DEPRECATED [[deprecated]] diff --git a/kratos/includes/kratos_application.h b/kratos/includes/kratos_application.h index 7cc7004c339b..ad00598e641e 100644 --- a/kratos/includes/kratos_application.h +++ b/kratos/includes/kratos_application.h @@ -128,7 +128,12 @@ class KRATOS_API(KRATOS_CORE) KratosApplication { mpModelers(rOther.mpModelers) {} /// Destructor. - virtual ~KratosApplication() {} + virtual ~KratosApplication() + { + // This must be commented until tests have been fixed. + DeregisterCommonComponents(); + DeregisterApplication(); + } ///@} ///@name Operations @@ -141,6 +146,25 @@ class KRATOS_API(KRATOS_CORE) KratosApplication { void RegisterKratosCore(); + /** + * @brief This method is used to unregister common components of the application. + * @details This method is used to unregister common components of the application. + * The list of unregistered components are the ones exposed in the common KratosComponents interface: + * - Geometries + * - Elements + * - Conditions + * - MasterSlaveConstraints + * - Modelers + * - ConstitutiveLaws + */ + void DeregisterCommonComponents(); + + /** + * @brief This method is used to unregister specific application components. + * @details This method is used to unregister specific application components. + */ + virtual void DeregisterApplication(); + /////////////////////////////////////////////////////////////////// void RegisterVariables(); // This contains the whole list of common variables in the Kratos Core void RegisterDeprecatedVariables(); //TODO: remove, this variables should not be there diff --git a/kratos/includes/registry.h b/kratos/includes/registry.h index 111d604ddc6c..fc190b3f0357 100644 --- a/kratos/includes/registry.h +++ b/kratos/includes/registry.h @@ -151,6 +151,18 @@ class KRATOS_API(KRATOS_CORE) Registry final static void RemoveItem(std::string const& ItemName); + /** Sets the current source of the registry + * This function is used to keep track of which application is adding items to the registry + * @param rCurrentSource The current source of the registry + */ + static void SetCurrentSource(std::string const & rCurrentSource); + + /** Gets the current source of the registry + * This function is used to keep track of which application is adding items to the registry + * @param return The current source of the registry + */ + static std::string GetCurrentSource(); + ///@} ///@name Inquiry ///@{ @@ -189,6 +201,7 @@ class KRATOS_API(KRATOS_CORE) Registry final ///@{ static RegistryItem* mspRootRegistryItem; + static std::string mCurrentSource; ///@} ///@name Member Variables diff --git a/kratos/sources/kratos_application.cpp b/kratos/sources/kratos_application.cpp index 3797b4000999..ee140a80f3c6 100644 --- a/kratos/sources/kratos_application.cpp +++ b/kratos/sources/kratos_application.cpp @@ -116,7 +116,16 @@ KratosApplication::KratosApplication(const std::string& ApplicationName) mpConditions(KratosComponents::pGetComponents()), mpModelers(KratosComponents::pGetComponents()), mpRegisteredObjects(&(Serializer::GetRegisteredObjects())), - mpRegisteredObjectsName(&(Serializer::GetRegisteredObjectsName())) {} + mpRegisteredObjectsName(&(Serializer::GetRegisteredObjectsName())) { + + Registry::SetCurrentSource(mApplicationName); + + for (auto component : {"geometries", "elements", "conditions", "constraints", "modelers", "constitutive_laws"}) { + if (!Registry::HasItem(std::string(component))) { + Registry::AddItem(std::string(component)+"."+mApplicationName); + } + } + } void KratosApplication::RegisterKratosCore() { @@ -295,4 +304,31 @@ void KratosApplication::RegisterKratosCore() { // Register ConstitutiveLaw BaseClass KRATOS_REGISTER_CONSTITUTIVE_LAW("ConstitutiveLaw", mConstitutiveLaw); } + +void KratosApplication::DeregisterCommonComponents() +{ + KRATOS_INFO("") << "Deregistering " << mApplicationName << std::endl; + + auto deregister_detail = [&](std::string const & rComponentName, auto remove_detail) { + auto path = std::string(rComponentName)+"."+mApplicationName; + std::cout << "Deregistering " << path << " components" << std::endl; + for (auto & key : Registry::GetItem(path)) { + std::cout << "\t" << key.first << std::endl; + remove_detail(key.first); + } + }; + + deregister_detail("geometries", [](std::string const & key){ KratosComponents>::Remove(key);}); + deregister_detail("elements", [](std::string const & key){ KratosComponents::Remove(key);}); + deregister_detail("conditions", [](std::string const & key){ KratosComponents::Remove(key);}); + deregister_detail("constraints", [](std::string const & key){ KratosComponents::Remove(key);}); + deregister_detail("modelers", [](std::string const & key){ KratosComponents::Remove(key);}); + deregister_detail("constitutive_laws", [](std::string const & key){ KratosComponents::Remove(key);}); +} + +void KratosApplication::DeregisterApplication() { + // DeregisterLinearSolvers(); + // DeregisterPreconditioners(); +} + } // namespace Kratos. diff --git a/kratos/sources/registry.cpp b/kratos/sources/registry.cpp index 17487dbecefd..f442c9a0a06c 100644 --- a/kratos/sources/registry.cpp +++ b/kratos/sources/registry.cpp @@ -28,6 +28,7 @@ namespace } RegistryItem* Registry::mspRootRegistryItem = nullptr; + std::string Registry::mCurrentSource = "all"; RegistryItem& Registry::GetItem(std::string const& rItemFullName) { @@ -80,6 +81,16 @@ namespace } } + void Registry::SetCurrentSource(std::string const & rCurrentSource) + { + mCurrentSource = rCurrentSource; + } + + std::string Registry::GetCurrentSource() + { + return mCurrentSource; + } + std::size_t Registry::size() { return mspRootRegistryItem->size(); From fac137a8b0d8b729049d0377e5acf03c6b4f80dd Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Fri, 26 Apr 2024 11:42:43 +0200 Subject: [PATCH 02/16] Missing geometries --- kratos/includes/define.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kratos/includes/define.h b/kratos/includes/define.h index 1374b331b75d..83f8b21a1710 100644 --- a/kratos/includes/define.h +++ b/kratos/includes/define.h @@ -698,6 +698,9 @@ catch(...) { Block KRATOS_THROW_ERROR(std::runtime_error, "Unknown error", MoreI #endif #define KRATOS_REGISTER_GEOMETRY(name, reference) \ KratosComponents>::Add(name, reference); \ + if(!Registry::HasItem("geometries."+Registry::GetCurrentSource()+"."+name)){ \ + Registry::AddItem("geometries."+Registry::GetCurrentSource()+"."+name); \ + } \ Serializer::Register(name, reference); #ifdef KRATOS_REGISTER_ELEMENT From a9a29d36cd97f6a8fb39d48aea432e4890fadebc Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Wed, 8 May 2024 16:17:02 +0200 Subject: [PATCH 03/16] Change proposal based on comments --- kratos/includes/define.h | 38 ++++++++++++++++++--------- kratos/sources/kratos_application.cpp | 9 ++++++- kratos/sources/registry.cpp | 29 ++++++++++++++++++-- 3 files changed, 60 insertions(+), 16 deletions(-) diff --git a/kratos/includes/define.h b/kratos/includes/define.h index 83f8b21a1710..abf561b6f04d 100644 --- a/kratos/includes/define.h +++ b/kratos/includes/define.h @@ -696,10 +696,12 @@ catch(...) { Block KRATOS_THROW_ERROR(std::runtime_error, "Unknown error", MoreI #ifdef KRATOS_REGISTER_GEOMETRY #undef KRATOS_REGISTER_GEOMETRY #endif -#define KRATOS_REGISTER_GEOMETRY(name, reference) \ - KratosComponents>::Add(name, reference); \ - if(!Registry::HasItem("geometries."+Registry::GetCurrentSource()+"."+name)){ \ - Registry::AddItem("geometries."+Registry::GetCurrentSource()+"."+name); \ +#define KRATOS_REGISTER_GEOMETRY(name, reference) \ + KratosComponents>::Add(name, reference); \ + if(!Registry::HasItem("geometries."+Registry::GetCurrentSource()+"."+name) && \ + !Registry::HasItem("components."+std::string(name))){ \ + Registry::AddItem("geometries."+Registry::GetCurrentSource()+"."+name); \ + Registry::AddItem("components."+std::string(name)); \ } \ Serializer::Register(name, reference); @@ -708,8 +710,10 @@ catch(...) { Block KRATOS_THROW_ERROR(std::runtime_error, "Unknown error", MoreI #endif #define KRATOS_REGISTER_ELEMENT(name, reference) \ KratosComponents::Add(name, reference); \ - if(!Registry::HasItem("elements."+Registry::GetCurrentSource()+"."+name)){ \ - Registry::AddItem("elements."+Registry::GetCurrentSource()+"."+name); \ + if(!Registry::HasItem("elements."+Registry::GetCurrentSource()+"."+name) && \ + !Registry::HasItem("components."+std::string(name))){ \ + Registry::AddItem("elements."+Registry::GetCurrentSource()+"."+name); \ + Registry::AddItem("components."+std::string(name)); \ } \ Serializer::Register(name, reference); @@ -718,8 +722,10 @@ catch(...) { Block KRATOS_THROW_ERROR(std::runtime_error, "Unknown error", MoreI #endif #define KRATOS_REGISTER_CONDITION(name, reference) \ KratosComponents::Add(name, reference); \ - if(!Registry::HasItem("conditions."+Registry::GetCurrentSource()+"."+name)){ \ + if(!Registry::HasItem("conditions."+Registry::GetCurrentSource()+"."+name) && \ + !Registry::HasItem("components."+std::string(name))){ \ Registry::AddItem("conditions."+Registry::GetCurrentSource()+"."+name); \ + Registry::AddItem("components."+std::string(name)); \ } \ Serializer::Register(name, reference); @@ -728,8 +734,10 @@ catch(...) { Block KRATOS_THROW_ERROR(std::runtime_error, "Unknown error", MoreI #endif #define KRATOS_REGISTER_CONSTRAINT(name, reference) \ KratosComponents::Add(name, reference); \ - if(!Registry::HasItem("constraints."+Registry::GetCurrentSource()+"."+name)){ \ - Registry::AddItem("constraints."+Registry::GetCurrentSource()+"."+name); \ + if(!Registry::HasItem("constraints."+Registry::GetCurrentSource()+"."+name) && \ + !Registry::HasItem("components."+std::string(name))){ \ + Registry::AddItem("constraints."+Registry::GetCurrentSource()+"."+name); \ + Registry::AddItem("components."+std::string(name)); \ } \ Serializer::Register(name, reference); @@ -738,8 +746,10 @@ catch(...) { Block KRATOS_THROW_ERROR(std::runtime_error, "Unknown error", MoreI #endif #define KRATOS_REGISTER_MODELER(name, reference) \ KratosComponents::Add(name, reference); \ - if(!Registry::HasItem("modelers."+Registry::GetCurrentSource()+"."+name)){ \ - Registry::AddItem("modelers."+Registry::GetCurrentSource()+"."+name); \ + if(!Registry::HasItem("modelers."+Registry::GetCurrentSource()+"."+name) && \ + !Registry::HasItem("components."+std::string(name))){ \ + Registry::AddItem("modelers."+Registry::GetCurrentSource()+"."+name); \ + Registry::AddItem("components."+std::string(name)); \ } \ Serializer::Register(name, reference); @@ -748,8 +758,10 @@ catch(...) { Block KRATOS_THROW_ERROR(std::runtime_error, "Unknown error", MoreI #endif #define KRATOS_REGISTER_CONSTITUTIVE_LAW(name, reference) \ KratosComponents::Add(name, reference); \ - if(!Registry::HasItem("constitutive_laws."+Registry::GetCurrentSource()+"."+name)){ \ - Registry::AddItem("constitutive_laws."+Registry::GetCurrentSource()+"."+name); \ + if(!Registry::HasItem("constitutive_laws."+Registry::GetCurrentSource()+"."+name) && \ + !Registry::HasItem("components."+std::string(name))){ \ + Registry::AddItem("constitutive_laws."+Registry::GetCurrentSource()+"."+name); \ + Registry::AddItem("components."+std::string(name)); \ } \ Serializer::Register(name, reference); diff --git a/kratos/sources/kratos_application.cpp b/kratos/sources/kratos_application.cpp index ee140a80f3c6..70e535bfbfdd 100644 --- a/kratos/sources/kratos_application.cpp +++ b/kratos/sources/kratos_application.cpp @@ -311,10 +311,17 @@ void KratosApplication::DeregisterCommonComponents() auto deregister_detail = [&](std::string const & rComponentName, auto remove_detail) { auto path = std::string(rComponentName)+"."+mApplicationName; - std::cout << "Deregistering " << path << " components" << std::endl; for (auto & key : Registry::GetItem(path)) { std::cout << "\t" << key.first << std::endl; + + // Remove from components remove_detail(key.first); + + // Remove from registry component typed list + Registry::RemoveItem(path); + + // Remove from registry general component list + Registry::RemoveItem("components."+key.first); } }; diff --git a/kratos/sources/registry.cpp b/kratos/sources/registry.cpp index f442c9a0a06c..d7eff1cbcb51 100644 --- a/kratos/sources/registry.cpp +++ b/kratos/sources/registry.cpp @@ -83,12 +83,37 @@ namespace void Registry::SetCurrentSource(std::string const & rCurrentSource) { - mCurrentSource = rCurrentSource; + // If context key not present, create it + if(!Registry::HasItem("CurrentContext")){ + Registry::AddItem("CurrentContext"); + } + + auto & entry = Registry::GetItem("CurrentContext"); + + if (entry.HasItem("value")) { + entry.RemoveItem("value"); + } + + entry.AddItem("value", rCurrentSource); } std::string Registry::GetCurrentSource() { - return mCurrentSource; + using namespace std::literals; + + // If context key not present, create it + if(!Registry::HasItem("CurrentContext")){ + Registry::AddItem("CurrentContext"); + } + + auto & entry = Registry::GetItem("CurrentContext"); + + // If no context set, set the default one + if (!entry.HasItem("value")) { + entry.AddItem("value", std::string("KratosMultiphysics")); + } + + return Registry::GetItem("CurrentContext.value").GetValue(); } std::size_t Registry::size() From 9694b6a6d3f6939b975762a4f4d2bab73f210f97 Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Thu, 9 May 2024 16:39:36 +0200 Subject: [PATCH 04/16] Fixed loop over invalidated iterators --- kratos/sources/kratos_application.cpp | 30 ++++++++++++++++++++------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/kratos/sources/kratos_application.cpp b/kratos/sources/kratos_application.cpp index 70e535bfbfdd..42b35acbbfe0 100644 --- a/kratos/sources/kratos_application.cpp +++ b/kratos/sources/kratos_application.cpp @@ -311,17 +311,31 @@ void KratosApplication::DeregisterCommonComponents() auto deregister_detail = [&](std::string const & rComponentName, auto remove_detail) { auto path = std::string(rComponentName)+"."+mApplicationName; - for (auto & key : Registry::GetItem(path)) { - std::cout << "\t" << key.first << std::endl; - - // Remove from components - remove_detail(key.first); - // Remove from registry component typed list - Registry::RemoveItem(path); + // Generate a temporal list with all the keys to avoid invalidating the iterator (Convert this intro a transform range when C++20 is available) + std::vector keys; + std::transform(Registry::GetItem(path).cbegin(), Registry::GetItem(path).cend(), std::back_inserter(keys), [](auto & key){return std::string(key.first);}); + + for (auto & key : keys) { + auto cmpt_key = "components."+key; + auto type_key = path+"."+key; + + // Remove from KratosComponents + remove_detail(key); // Remove from registry general component list - Registry::RemoveItem("components."+key.first); + if(Registry::HasItem(cmpt_key)) { + Registry::RemoveItem(cmpt_key); + } else { + KRATOS_ERROR << "Trying ro remove: " << cmpt_key << " which was not found in registry" << std::endl; + } + + // Remove from registry component typed list + if(Registry::HasItem(type_key)) { + Registry::RemoveItem(type_key); + } else { + KRATOS_ERROR << "Trying ro remove: " << type_key << " which was not found in registry" << std::endl; + } } }; From ecd6e0bc688cba4e6b706b95ca8fef85241ea8e7 Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Thu, 9 May 2024 16:40:45 +0200 Subject: [PATCH 05/16] Removing mCurrentSource --- kratos/includes/registry.h | 1 - kratos/sources/registry.cpp | 1 - 2 files changed, 2 deletions(-) diff --git a/kratos/includes/registry.h b/kratos/includes/registry.h index fc190b3f0357..2201940c8aaf 100644 --- a/kratos/includes/registry.h +++ b/kratos/includes/registry.h @@ -201,7 +201,6 @@ class KRATOS_API(KRATOS_CORE) Registry final ///@{ static RegistryItem* mspRootRegistryItem; - static std::string mCurrentSource; ///@} ///@name Member Variables diff --git a/kratos/sources/registry.cpp b/kratos/sources/registry.cpp index d7eff1cbcb51..36bddab0d2c8 100644 --- a/kratos/sources/registry.cpp +++ b/kratos/sources/registry.cpp @@ -28,7 +28,6 @@ namespace } RegistryItem* Registry::mspRootRegistryItem = nullptr; - std::string Registry::mCurrentSource = "all"; RegistryItem& Registry::GetItem(std::string const& rItemFullName) { From c609e2c5e71ec6115e8a5d0a96ec5c667725f3b5 Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Fri, 10 May 2024 10:49:28 +0200 Subject: [PATCH 06/16] Previnting the de-register of non existing keys --- kratos/sources/kratos_application.cpp | 51 +++++++++++++++------------ 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/kratos/sources/kratos_application.cpp b/kratos/sources/kratos_application.cpp index 42b35acbbfe0..28c9320fd2e4 100644 --- a/kratos/sources/kratos_application.cpp +++ b/kratos/sources/kratos_application.cpp @@ -312,30 +312,37 @@ void KratosApplication::DeregisterCommonComponents() auto deregister_detail = [&](std::string const & rComponentName, auto remove_detail) { auto path = std::string(rComponentName)+"."+mApplicationName; - // Generate a temporal list with all the keys to avoid invalidating the iterator (Convert this intro a transform range when C++20 is available) - std::vector keys; - std::transform(Registry::GetItem(path).cbegin(), Registry::GetItem(path).cend(), std::back_inserter(keys), [](auto & key){return std::string(key.first);}); - - for (auto & key : keys) { - auto cmpt_key = "components."+key; - auto type_key = path+"."+key; - - // Remove from KratosComponents - remove_detail(key); - - // Remove from registry general component list - if(Registry::HasItem(cmpt_key)) { - Registry::RemoveItem(cmpt_key); - } else { - KRATOS_ERROR << "Trying ro remove: " << cmpt_key << " which was not found in registry" << std::endl; + // Remove only if the application has this type of components registered + if (Registry::HasItem(path)) { + + // Generate a temporal list with all the keys to avoid invalidating the iterator (Convert this intro a transform range when C++20 is available) + std::vector keys; + std::transform(Registry::GetItem(path).cbegin(), Registry::GetItem(path).cend(), std::back_inserter(keys), [](auto & key){return std::string(key.first);}); + + for (auto & key : keys) { + auto cmpt_key = "components."+key; + auto type_key = path+"."+key; + + // Remove from KratosComponents + remove_detail(key); + + // Remove from registry general component list + if (Registry::HasItem(cmpt_key)) { + Registry::RemoveItem(cmpt_key); + } else { + KRATOS_ERROR << "Trying ro remove: " << cmpt_key << " which was not found in registry" << std::endl; + } + + // Remove from registry component typed list + if (Registry::HasItem(type_key)) { + Registry::RemoveItem(type_key); + } else { + KRATOS_ERROR << "Trying ro remove: " << type_key << " which was not found in registry" << std::endl; + } } - // Remove from registry component typed list - if(Registry::HasItem(type_key)) { - Registry::RemoveItem(type_key); - } else { - KRATOS_ERROR << "Trying ro remove: " << type_key << " which was not found in registry" << std::endl; - } + // Finally, remove the entry all together + Registry::RemoveItem(path); } }; From 117b3bfb27e30da315bf6929efb92c004c82177a Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Fri, 10 May 2024 15:13:44 +0200 Subject: [PATCH 07/16] fixing possible auto deduction failure in lambda --- kratos/sources/kratos_application.cpp | 116 +++++++++++++++++--------- 1 file changed, 77 insertions(+), 39 deletions(-) diff --git a/kratos/sources/kratos_application.cpp b/kratos/sources/kratos_application.cpp index 28c9320fd2e4..39a30f677221 100644 --- a/kratos/sources/kratos_application.cpp +++ b/kratos/sources/kratos_application.cpp @@ -305,53 +305,91 @@ void KratosApplication::RegisterKratosCore() { KRATOS_REGISTER_CONSTITUTIVE_LAW("ConstitutiveLaw", mConstitutiveLaw); } -void KratosApplication::DeregisterCommonComponents() -{ - KRATOS_INFO("") << "Deregistering " << mApplicationName << std::endl; - - auto deregister_detail = [&](std::string const & rComponentName, auto remove_detail) { - auto path = std::string(rComponentName)+"."+mApplicationName; +template +void KratosApplication::DeregisterComponent(std::string const & rComponentName, TDeregisterComponentFType && remove_detail) { + auto path = std::string(rComponentName)+"."+mApplicationName; - // Remove only if the application has this type of components registered - if (Registry::HasItem(path)) { + // Remove only if the application has this type of components registered + if (Registry::HasItem(path)) { - // Generate a temporal list with all the keys to avoid invalidating the iterator (Convert this intro a transform range when C++20 is available) - std::vector keys; - std::transform(Registry::GetItem(path).cbegin(), Registry::GetItem(path).cend(), std::back_inserter(keys), [](auto & key){return std::string(key.first);}); + // Generate a temporal list with all the keys to avoid invalidating the iterator (Convert this intro a transform range when C++20 is available) + std::vector keys; + std::transform(Registry::GetItem(path).cbegin(), Registry::GetItem(path).cend(), std::back_inserter(keys), [](auto & key){return std::string(key.first);}); - for (auto & key : keys) { - auto cmpt_key = "components."+key; - auto type_key = path+"."+key; + for (auto & key : keys) { + auto cmpt_key = "components."+key; + auto type_key = path+"."+key; - // Remove from KratosComponents - remove_detail(key); + // Remove from KratosComponents + remove_detail(key); - // Remove from registry general component list - if (Registry::HasItem(cmpt_key)) { - Registry::RemoveItem(cmpt_key); - } else { - KRATOS_ERROR << "Trying ro remove: " << cmpt_key << " which was not found in registry" << std::endl; - } - - // Remove from registry component typed list - if (Registry::HasItem(type_key)) { - Registry::RemoveItem(type_key); - } else { - KRATOS_ERROR << "Trying ro remove: " << type_key << " which was not found in registry" << std::endl; - } + // Remove from registry general component list + if (Registry::HasItem(cmpt_key)) { + Registry::RemoveItem(cmpt_key); + } else { + KRATOS_ERROR << "Trying ro remove: " << cmpt_key << " which was not found in registry" << std::endl; } - // Finally, remove the entry all together - Registry::RemoveItem(path); + // Remove from registry component typed list + if (Registry::HasItem(type_key)) { + Registry::RemoveItem(type_key); + } else { + KRATOS_ERROR << "Trying ro remove: " << type_key << " which was not found in registry" << std::endl; + } } - }; - - deregister_detail("geometries", [](std::string const & key){ KratosComponents>::Remove(key);}); - deregister_detail("elements", [](std::string const & key){ KratosComponents::Remove(key);}); - deregister_detail("conditions", [](std::string const & key){ KratosComponents::Remove(key);}); - deregister_detail("constraints", [](std::string const & key){ KratosComponents::Remove(key);}); - deregister_detail("modelers", [](std::string const & key){ KratosComponents::Remove(key);}); - deregister_detail("constitutive_laws", [](std::string const & key){ KratosComponents::Remove(key);}); + + // Finally, remove the entry all together + Registry::RemoveItem(path); + } +} + +void KratosApplication::DeregisterCommonComponents() +{ + KRATOS_INFO("") << "Deregistering " << mApplicationName << std::endl; + + // auto deregister_detail = [&](std::string const & rComponentName, auto remove_detail) { + // auto path = std::string(rComponentName)+"."+mApplicationName; + + // // Remove only if the application has this type of components registered + // if (Registry::HasItem(path)) { + + // // Generate a temporal list with all the keys to avoid invalidating the iterator (Convert this intro a transform range when C++20 is available) + // std::vector keys; + // std::transform(Registry::GetItem(path).cbegin(), Registry::GetItem(path).cend(), std::back_inserter(keys), [](auto & key){return std::string(key.first);}); + + // for (auto & key : keys) { + // auto cmpt_key = "components."+key; + // auto type_key = path+"."+key; + + // // Remove from KratosComponents + // remove_detail(key); + + // // Remove from registry general component list + // if (Registry::HasItem(cmpt_key)) { + // Registry::RemoveItem(cmpt_key); + // } else { + // KRATOS_ERROR << "Trying ro remove: " << cmpt_key << " which was not found in registry" << std::endl; + // } + + // // Remove from registry component typed list + // if (Registry::HasItem(type_key)) { + // Registry::RemoveItem(type_key); + // } else { + // KRATOS_ERROR << "Trying ro remove: " << type_key << " which was not found in registry" << std::endl; + // } + // } + + // // Finally, remove the entry all together + // Registry::RemoveItem(path); + // } + // }; + + DeregisterComponent("geometries", [](std::string const & key){ KratosComponents>::Remove(key);}); + DeregisterComponent("elements", [](std::string const & key){ KratosComponents::Remove(key);}); + DeregisterComponent("conditions", [](std::string const & key){ KratosComponents::Remove(key);}); + DeregisterComponent("constraints", [](std::string const & key){ KratosComponents::Remove(key);}); + DeregisterComponent("modelers", [](std::string const & key){ KratosComponents::Remove(key);}); + DeregisterComponent("constitutive_laws", [](std::string const & key){ KratosComponents::Remove(key);}); } void KratosApplication::DeregisterApplication() { From 8d77e960312e262cd7a24178b62460b25b35c2e1 Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Fri, 10 May 2024 15:16:05 +0200 Subject: [PATCH 08/16] Missing def --- kratos/includes/kratos_application.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kratos/includes/kratos_application.h b/kratos/includes/kratos_application.h index ad00598e641e..5bfb367103b3 100644 --- a/kratos/includes/kratos_application.h +++ b/kratos/includes/kratos_application.h @@ -146,6 +146,9 @@ class KRATOS_API(KRATOS_CORE) KratosApplication { void RegisterKratosCore(); + template + void DeregisterComponent(std::string const & rComponentName, TDeregisterComponentFType && remove_detail); + /** * @brief This method is used to unregister common components of the application. * @details This method is used to unregister common components of the application. From 21aae0f1f2b76750099b977b81b6b7b08d6c85b4 Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Fri, 10 May 2024 15:40:07 +0200 Subject: [PATCH 09/16] Trying more simple approach --- kratos/includes/kratos_application.h | 4 ++-- kratos/sources/kratos_application.cpp | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/kratos/includes/kratos_application.h b/kratos/includes/kratos_application.h index 5bfb367103b3..ad2cb5e02aa9 100644 --- a/kratos/includes/kratos_application.h +++ b/kratos/includes/kratos_application.h @@ -146,8 +146,8 @@ class KRATOS_API(KRATOS_CORE) KratosApplication { void RegisterKratosCore(); - template - void DeregisterComponent(std::string const & rComponentName, TDeregisterComponentFType && remove_detail); + template + void DeregisterComponent(std::string const & rComponentName); /** * @brief This method is used to unregister common components of the application. diff --git a/kratos/sources/kratos_application.cpp b/kratos/sources/kratos_application.cpp index 39a30f677221..9fd4cd509654 100644 --- a/kratos/sources/kratos_application.cpp +++ b/kratos/sources/kratos_application.cpp @@ -305,8 +305,8 @@ void KratosApplication::RegisterKratosCore() { KRATOS_REGISTER_CONSTITUTIVE_LAW("ConstitutiveLaw", mConstitutiveLaw); } -template -void KratosApplication::DeregisterComponent(std::string const & rComponentName, TDeregisterComponentFType && remove_detail) { +template +void KratosApplication::DeregisterComponent(std::string const & rComponentName) { auto path = std::string(rComponentName)+"."+mApplicationName; // Remove only if the application has this type of components registered @@ -321,7 +321,7 @@ void KratosApplication::DeregisterComponent(std::string const & rComponentName, auto type_key = path+"."+key; // Remove from KratosComponents - remove_detail(key); + KratosComponents::Remove(key); // Remove from registry general component list if (Registry::HasItem(cmpt_key)) { @@ -384,12 +384,12 @@ void KratosApplication::DeregisterCommonComponents() // } // }; - DeregisterComponent("geometries", [](std::string const & key){ KratosComponents>::Remove(key);}); - DeregisterComponent("elements", [](std::string const & key){ KratosComponents::Remove(key);}); - DeregisterComponent("conditions", [](std::string const & key){ KratosComponents::Remove(key);}); - DeregisterComponent("constraints", [](std::string const & key){ KratosComponents::Remove(key);}); - DeregisterComponent("modelers", [](std::string const & key){ KratosComponents::Remove(key);}); - DeregisterComponent("constitutive_laws", [](std::string const & key){ KratosComponents::Remove(key);}); + DeregisterComponent>("geometries"); + DeregisterComponent("elements"); + DeregisterComponent("conditions"); + DeregisterComponent("constraints"); + DeregisterComponent("modelers"); + DeregisterComponent("constitutive_laws"); } void KratosApplication::DeregisterApplication() { From 02df3ac73dd62941f0644f3eda730476609b6a66 Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Wed, 15 May 2024 17:19:18 +0200 Subject: [PATCH 10/16] Implementing current context as a key extension to avoid regression in gcc 8.3 (centos) --- kratos/sources/kratos_application.cpp | 37 --------------------------- kratos/sources/registry.cpp | 21 ++++++++++----- 2 files changed, 15 insertions(+), 43 deletions(-) diff --git a/kratos/sources/kratos_application.cpp b/kratos/sources/kratos_application.cpp index 9fd4cd509654..ace8d7823769 100644 --- a/kratos/sources/kratos_application.cpp +++ b/kratos/sources/kratos_application.cpp @@ -347,43 +347,6 @@ void KratosApplication::DeregisterCommonComponents() { KRATOS_INFO("") << "Deregistering " << mApplicationName << std::endl; - // auto deregister_detail = [&](std::string const & rComponentName, auto remove_detail) { - // auto path = std::string(rComponentName)+"."+mApplicationName; - - // // Remove only if the application has this type of components registered - // if (Registry::HasItem(path)) { - - // // Generate a temporal list with all the keys to avoid invalidating the iterator (Convert this intro a transform range when C++20 is available) - // std::vector keys; - // std::transform(Registry::GetItem(path).cbegin(), Registry::GetItem(path).cend(), std::back_inserter(keys), [](auto & key){return std::string(key.first);}); - - // for (auto & key : keys) { - // auto cmpt_key = "components."+key; - // auto type_key = path+"."+key; - - // // Remove from KratosComponents - // remove_detail(key); - - // // Remove from registry general component list - // if (Registry::HasItem(cmpt_key)) { - // Registry::RemoveItem(cmpt_key); - // } else { - // KRATOS_ERROR << "Trying ro remove: " << cmpt_key << " which was not found in registry" << std::endl; - // } - - // // Remove from registry component typed list - // if (Registry::HasItem(type_key)) { - // Registry::RemoveItem(type_key); - // } else { - // KRATOS_ERROR << "Trying ro remove: " << type_key << " which was not found in registry" << std::endl; - // } - // } - - // // Finally, remove the entry all together - // Registry::RemoveItem(path); - // } - // }; - DeregisterComponent>("geometries"); DeregisterComponent("elements"); DeregisterComponent("conditions"); diff --git a/kratos/sources/registry.cpp b/kratos/sources/registry.cpp index 36bddab0d2c8..697d00a233b4 100644 --- a/kratos/sources/registry.cpp +++ b/kratos/sources/registry.cpp @@ -89,11 +89,15 @@ namespace auto & entry = Registry::GetItem("CurrentContext"); - if (entry.HasItem("value")) { - entry.RemoveItem("value"); + // Generate a temporal list with all the keys to avoid invalidating the iterator (Convert this intro a transform range when C++20 is available) + std::vector keys; + std::transform(entry.cbegin(), entry.cend(), std::back_inserter(keys), [](auto & key){return std::string(key.first);}); + + for (auto sub_key: keys) { + entry.RemoveItem(sub_key); } - entry.AddItem("value", rCurrentSource); + Registry::AddItem("CurrentContext."+rCurrentSource); } std::string Registry::GetCurrentSource() @@ -108,11 +112,16 @@ namespace auto & entry = Registry::GetItem("CurrentContext"); // If no context set, set the default one - if (!entry.HasItem("value")) { - entry.AddItem("value", std::string("KratosMultiphysics")); + if(entry.size() == 0){ + Registry::AddItem("CurrentContext.KratosMultiphysics"); + } + + // If more than one context set, throw an error + if(entry.size() > 1){ + KRATOS_ERROR << "More than one context set in the registry" << std::endl; } - return Registry::GetItem("CurrentContext.value").GetValue(); + return entry.begin()->first; } std::size_t Registry::size() From b637f0dea2ef9359102e89465115268bec9f8e11 Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Tue, 21 May 2024 12:35:29 +0200 Subject: [PATCH 11/16] typo --- kratos/sources/kratos_application.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kratos/sources/kratos_application.cpp b/kratos/sources/kratos_application.cpp index ace8d7823769..756d186c6018 100644 --- a/kratos/sources/kratos_application.cpp +++ b/kratos/sources/kratos_application.cpp @@ -312,7 +312,7 @@ void KratosApplication::DeregisterComponent(std::string const & rComponentName) // Remove only if the application has this type of components registered if (Registry::HasItem(path)) { - // Generate a temporal list with all the keys to avoid invalidating the iterator (Convert this intro a transform range when C++20 is available) + // Generate a temporal list with all the keys to avoid invalidating the iterator (Convert this into a transform range when C++20 is available) std::vector keys; std::transform(Registry::GetItem(path).cbegin(), Registry::GetItem(path).cend(), std::back_inserter(keys), [](auto & key){return std::string(key.first);}); From 0fc4296ffec3ed6133ef7978751a8c03b6e800e1 Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Tue, 21 May 2024 12:36:16 +0200 Subject: [PATCH 12/16] =?UTF-8?q?Prevent=20std::bad=5Fany=20cast=20due=20t?= =?UTF-8?q?o=20string=5Fview=20to=20invalid=20string=20reference=20in=20th?= =?UTF-8?q?e=20registry=20key=C3=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kratos/sources/registry.cpp | 36 ++++++++---------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/kratos/sources/registry.cpp b/kratos/sources/registry.cpp index 697d00a233b4..2f064d0dcead 100644 --- a/kratos/sources/registry.cpp +++ b/kratos/sources/registry.cpp @@ -83,45 +83,25 @@ namespace void Registry::SetCurrentSource(std::string const & rCurrentSource) { // If context key not present, create it - if(!Registry::HasItem("CurrentContext")){ - Registry::AddItem("CurrentContext"); + if (Registry::HasItem("CurrentContext")){ + Registry::RemoveItem("CurrentContext"); } - auto & entry = Registry::GetItem("CurrentContext"); + // It is needed to create a std::string explicitly copying the '"CurrentContext"+rCurrentSource' to avoid casting problems + // involing std::any_cast to a reference type which key references a string that may not be alive when invoked. + std::string context_key = std::string("CurrentContext" + rCurrentSource); - // Generate a temporal list with all the keys to avoid invalidating the iterator (Convert this intro a transform range when C++20 is available) - std::vector keys; - std::transform(entry.cbegin(), entry.cend(), std::back_inserter(keys), [](auto & key){return std::string(key.first);}); - - for (auto sub_key: keys) { - entry.RemoveItem(sub_key); - } - - Registry::AddItem("CurrentContext."+rCurrentSource); + Registry::AddItem(context_key); } std::string Registry::GetCurrentSource() { - using namespace std::literals; - // If context key not present, create it - if(!Registry::HasItem("CurrentContext")){ - Registry::AddItem("CurrentContext"); - } - - auto & entry = Registry::GetItem("CurrentContext"); - - // If no context set, set the default one - if(entry.size() == 0){ + if (!Registry::HasItem("CurrentContext")){ Registry::AddItem("CurrentContext.KratosMultiphysics"); } - // If more than one context set, throw an error - if(entry.size() > 1){ - KRATOS_ERROR << "More than one context set in the registry" << std::endl; - } - - return entry.begin()->first; + return Registry::GetItem("CurrentContext").begin()->first; } std::size_t Registry::size() From 79f8840a03b9bce4cddaab3702652dc208386b3d Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Tue, 21 May 2024 14:10:04 +0200 Subject: [PATCH 13/16] Disable deregister until tests (Failures in previous commit were expected) --- kratos/includes/kratos_application.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kratos/includes/kratos_application.h b/kratos/includes/kratos_application.h index ad2cb5e02aa9..ee8d2e26f9b5 100644 --- a/kratos/includes/kratos_application.h +++ b/kratos/includes/kratos_application.h @@ -131,8 +131,8 @@ class KRATOS_API(KRATOS_CORE) KratosApplication { virtual ~KratosApplication() { // This must be commented until tests have been fixed. - DeregisterCommonComponents(); - DeregisterApplication(); + // DeregisterCommonComponents(); + // DeregisterApplication(); } ///@} From aaaacdbeb47d1ad4c291b5208f2b229fa8080791 Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Tue, 21 May 2024 15:23:42 +0200 Subject: [PATCH 14/16] Missing . --- kratos/sources/registry.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kratos/sources/registry.cpp b/kratos/sources/registry.cpp index 2f064d0dcead..96bf2491fc6c 100644 --- a/kratos/sources/registry.cpp +++ b/kratos/sources/registry.cpp @@ -89,7 +89,7 @@ namespace // It is needed to create a std::string explicitly copying the '"CurrentContext"+rCurrentSource' to avoid casting problems // involing std::any_cast to a reference type which key references a string that may not be alive when invoked. - std::string context_key = std::string("CurrentContext" + rCurrentSource); + std::string context_key = std::string("CurrentContext." + rCurrentSource); Registry::AddItem(context_key); } From 6b7fbe95984aeacd11d7ebaf8b8c636f066f70cd Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Wed, 22 May 2024 10:34:14 +0200 Subject: [PATCH 15/16] Probably a bug in gcc --- kratos/includes/registry_item.h | 15 ++++++++++++++- kratos/sources/registry_item.cpp | 8 ++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/kratos/includes/registry_item.h b/kratos/includes/registry_item.h index f13de15f9999..5154bfeb0e9e 100644 --- a/kratos/includes/registry_item.h +++ b/kratos/includes/registry_item.h @@ -18,7 +18,12 @@ #include #include #include -#include + +#if defined (__GNUC__) && __GNUC__ <= 8 && __GNUC_MINOR__ <= 3 + #include +#else + #include +#endif // External includes @@ -243,7 +248,11 @@ class KRATOS_API(KRATOS_CORE) RegistryItem { KRATOS_TRY +#if defined (__GNUC__) && __GNUC__ <= 8 && __GNUC_MINOR__ <= 3 + return *(boost::any_cast>(mpValue)); +#else return *(std::any_cast>(mpValue)); +#endif KRATOS_CATCH(""); } @@ -253,7 +262,11 @@ class KRATOS_API(KRATOS_CORE) RegistryItem { KRATOS_TRY +#if defined (__GNUC__) && __GNUC__ <= 8 && __GNUC_MINOR__ <= 3 + return *std::dynamic_pointer_cast(boost::any_cast>(mpValue)); +#else return *std::dynamic_pointer_cast(std::any_cast>(mpValue)); +#endif KRATOS_CATCH(""); } diff --git a/kratos/sources/registry_item.cpp b/kratos/sources/registry_item.cpp index 6b312ebe7177..ad82fbc3d4e3 100644 --- a/kratos/sources/registry_item.cpp +++ b/kratos/sources/registry_item.cpp @@ -23,13 +23,21 @@ namespace Kratos RegistryItem::SubRegistryItemType& RegistryItem::GetSubRegistryItemMap() { KRATOS_ERROR_IF(HasValue()) << "Item " << Name() << " has value and cannot be iterated." << std::endl; +#if defined (__GNUC__) && __GNUC__ <= 8 && __GNUC_MINOR__ <= 3 + return *(boost::any_cast(mpValue)); +#else return *(std::any_cast(mpValue)); +#endif } RegistryItem::SubRegistryItemType& RegistryItem::GetSubRegistryItemMap() const { KRATOS_ERROR_IF(HasValue()) << "Item " << Name() << " has value and cannot be iterated." << std::endl; +#if defined (__GNUC__) && __GNUC__ <= 8 && __GNUC_MINOR__ <= 3 + return *(boost::any_cast(mpValue)); +#else return *(std::any_cast(mpValue)); +#endif } RegistryItem::SubRegistryItemType::iterator RegistryItem::begin() From 701be9bcfc8fa073f0889889003ff01e6986a6a0 Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Wed, 22 May 2024 10:35:38 +0200 Subject: [PATCH 16/16] Missing --- kratos/includes/registry_item.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kratos/includes/registry_item.h b/kratos/includes/registry_item.h index 5154bfeb0e9e..ad1eab81bdd3 100644 --- a/kratos/includes/registry_item.h +++ b/kratos/includes/registry_item.h @@ -306,7 +306,11 @@ class KRATOS_API(KRATOS_CORE) RegistryItem ///@{ std::string mName; +#if defined (__GNUC__) && __GNUC__ <= 8 && __GNUC_MINOR__ <= 3 + boost::any mpValue; +#else std::any mpValue; +#endif std::string (RegistryItem::*mGetValueStringMethod)() const; ///@}