From 001cf85f1b4b8e4d363b0274f2edf8b424bb43d0 Mon Sep 17 00:00:00 2001 From: Riccardo Rossi Date: Mon, 7 May 2018 19:29:34 +0200 Subject: [PATCH] making the scope of the model local, while still registering it in the kernel --- kratos/containers/model.cpp | 20 +++++++++++++++++++- kratos/containers/model.h | 14 ++++++++++++-- kratos/includes/kernel.h | 3 ++- kratos/python/add_kernel_to_python.cpp | 2 +- kratos/python/add_model_part_to_python.cpp | 2 +- kratos/python/add_model_to_python.cpp | 7 ++++--- kratos/sources/kernel.cpp | 12 +++++++++--- kratos/tests/test_model.py | 4 +++- kratos/tests/test_model_part.py | 15 +++++++++++++++ 9 files changed, 66 insertions(+), 13 deletions(-) diff --git a/kratos/containers/model.cpp b/kratos/containers/model.cpp index 33fcb824a0a5..cb433ba93add 100644 --- a/kratos/containers/model.cpp +++ b/kratos/containers/model.cpp @@ -20,6 +20,7 @@ // Project includes #include "includes/define.h" #include "containers/model.h" +#include "includes/kernel.h" #include #include #include @@ -27,12 +28,29 @@ namespace Kratos { + Model::Model() + { + Model*& pregistered_model = Kernel::GetModel(); + if(pregistered_model != nullptr) + KRATOS_ERROR << "trying to create a new Model, however one is already existing" << std::endl; + pregistered_model = &(*this); + }; + + /// Destructor. + Model::~Model() + { + Model*& pregistered_model = Kernel::GetModel(); + pregistered_model = nullptr; + }; ModelPart& Model::CreateModelPart( const std::string ModelPartName ) { KRATOS_TRY - std::cout << "within CreateModelPart address of Model is " << &(*this) << std::endl; + if(Kernel::GetModel() == nullptr) + KRATOS_ERROR << "trying to create a new ModelPart however a Model has not yet been registered in the kernel. Please ensure that the Model is allocated before calling the model part constructor. This is achieved by simply adding something like model = Model(). note however that this function can be called just once" << std::endl; + +// std::cout << "within CreateModelPart address of Model is " << &(*this) << std::endl; auto search = mRootModelPartMap.find(ModelPartName); if( search == mRootModelPartMap.end()) diff --git a/kratos/containers/model.h b/kratos/containers/model.h index 4e659b369d94..9ea66661559c 100644 --- a/kratos/containers/model.h +++ b/kratos/containers/model.h @@ -69,10 +69,20 @@ namespace Kratos ///@{ /// Default constructor. - Model(){}; + Model(); +// { +// Model*& pregistered_model = Kernel::GetModel(); +// if(pregistered_model != nullptr) +// KRATOS_ERROR << "trying to create a new Model, however one is already existing" << std::endl; +// pregistered_model = &(*this); +// }; /// Destructor. - virtual ~Model(){}; + virtual ~Model(); +// { +// Model*& pregistered_model = Kernel::GetModel(); +// pregistered_model = nullptr; +// }; Model & operator=(const Model&) = delete; Model(const Model&) = delete; diff --git a/kratos/includes/kernel.h b/kratos/includes/kernel.h index 51d40c50251b..4de06e6822ac 100644 --- a/kratos/includes/kernel.h +++ b/kratos/includes/kernel.h @@ -127,7 +127,8 @@ class KRATOS_API(KRATOS_CORE) Kernel { static std::unordered_set& GetApplicationsList(); - static Model& GetModel(); +// static Model& GetModel(); + static Model*& GetModel(); ///@} protected: diff --git a/kratos/python/add_kernel_to_python.cpp b/kratos/python/add_kernel_to_python.cpp index 904cb3c1db7b..7bbc1b270d91 100644 --- a/kratos/python/add_kernel_to_python.cpp +++ b/kratos/python/add_kernel_to_python.cpp @@ -150,7 +150,7 @@ void AddKernelToPython(pybind11::module& m) { self.Initialize(); /*RegisterInPythonApplicationVariables(App);*/ }) //&Kernel::InitializeApplication) //.def(""A,&Kernel::Initialize) - .def("GetModel", [](Kernel& self) -> Model& { return self.GetModel();}, return_value_policy::reference_internal) + .def("GetModel", [](Kernel& self) -> Model& { return *(self.GetModel());}, return_value_policy::reference_internal) .def("IsImported", &Kernel::IsImported) .def("HasFlag", HasFlag) .def("GetFlag", GetFlag) diff --git a/kratos/python/add_model_part_to_python.cpp b/kratos/python/add_model_part_to_python.cpp index 26b6f6a03b0f..87b0cb176e3d 100644 --- a/kratos/python/add_model_part_to_python.cpp +++ b/kratos/python/add_model_part_to_python.cpp @@ -746,7 +746,7 @@ void AddModelPartToPython(pybind11::module& m) ; //defining the "constructor" - m.def("ModelPart", [](std::string const& name) -> ModelPart& { return Kernel::GetModel().CreateModelPart(name);}, return_value_policy::reference); + m.def("ModelPart", [](std::string const& name) -> ModelPart& { return Kernel::GetModel()->CreateModelPart(name);}, return_value_policy::reference); } diff --git a/kratos/python/add_model_to_python.cpp b/kratos/python/add_model_to_python.cpp index b68e9026721b..b25a8ac9affb 100644 --- a/kratos/python/add_model_to_python.cpp +++ b/kratos/python/add_model_to_python.cpp @@ -31,10 +31,11 @@ using namespace pybind11; void AddModelToPython(pybind11::module& m) { - m.def("Model", &Kernel::GetModel, return_value_policy::reference); +// m.def("Model", &Kernel::GetModel, return_value_policy::reference); - //NOTE: we call this class "ModelInterface" instead of "Model" since the cosntructor is emulated as a standalone function which gets it from the kernel - class_(m,"ModelInterface") +// // // // // //NOTE: we call this class "ModelInterface" instead of "Model" since the cosntructor is emulated as a standalone function which gets it from the kernel + class_(m,"Model") + .def(init<>()) .def("AddModelPart", &Model::AddModelPart) .def("GetModelPart", &Model::GetModelPart, return_value_policy::reference_internal) .def("HasModelPart", &Model::HasModelPart) diff --git a/kratos/sources/kernel.cpp b/kratos/sources/kernel.cpp index 9bb8d8330910..aefcf740b842 100644 --- a/kratos/sources/kernel.cpp +++ b/kratos/sources/kernel.cpp @@ -37,10 +37,16 @@ std::unordered_set &Kernel::GetApplicationsList() { return application_list; } -Model& Kernel::GetModel() +// Model& Kernel::GetModel() +// { +// static Model smodel; +// return smodel; +// } + +Model*& Kernel::GetModel() { - static Model smodel; - return smodel; + static Model* spmodel; + return spmodel; } bool Kernel::IsImported(std::string ApplicationName) const { diff --git a/kratos/tests/test_model.py b/kratos/tests/test_model.py index f6060247e74a..3ec26a3b8947 100644 --- a/kratos/tests/test_model.py +++ b/kratos/tests/test_model.py @@ -8,12 +8,14 @@ class TestModel(KratosUnittest.TestCase): def test_model(self): + model = KratosMultiphysics.Model() #this should go before the creation of any modelpart + model_part = KratosMultiphysics.ModelPart("Main") model_part.CreateSubModelPart("Inlets") model_part.CreateSubModelPart("Temp") outlet = model_part.CreateSubModelPart("Outlet") - model = KratosMultiphysics.Model() + #model.AddModelPart(model_part) print("----------------") diff --git a/kratos/tests/test_model_part.py b/kratos/tests/test_model_part.py index d3924a9f3797..8846ea159e35 100644 --- a/kratos/tests/test_model_part.py +++ b/kratos/tests/test_model_part.py @@ -12,6 +12,8 @@ def setUp(self): self.assertRaisesRegex = self.assertRaisesRegexp def test_model_part_sub_model_parts(self): + model = Model() #this should go before the creation of any modelpart + model_part = ModelPart("Main") self.assertEqual(model_part.NumberOfSubModelParts(), 0) @@ -65,6 +67,8 @@ def test_model_part_sub_model_parts(self): #print (model_part) def test_variables_list(self): + model = Model() #this should go before the creation of any modelpart + model_part = ModelPart("Main") self.assertEqual(model_part.GetNodalSolutionStepDataSize(), 0) @@ -98,6 +102,7 @@ def test_variables_list(self): def test_model_part_nodes(self): + model = Model() #this should go before the creation of any modelpart model_part = ModelPart("Main") self.assertEqual(model_part.NumberOfNodes(), 0) @@ -190,6 +195,7 @@ def test_model_part_nodes(self): self.assertEqual(model_part.NumberOfNodes(), 7) def test_model_part_tables(self): + model = Model() #this should go before the creation of any modelpart model_part = ModelPart("Main") self.assertEqual(model_part.NumberOfTables(), 0) @@ -212,6 +218,7 @@ def test_model_part_tables(self): #self.assertEqual(model_part.NumberOfTables(), 0) def test_model_part_properties(self): + model = Model() #this should go before the creation of any modelpart model_part = ModelPart("Main") self.assertEqual(model_part.NumberOfProperties(), 0) @@ -287,6 +294,7 @@ def test_model_part_properties(self): self.assertEqual(model_part.NumberOfProperties(), 7) def test_model_part_elements(self): + model = Model() #this should go before the creation of any modelpart model_part = ModelPart("Main") self.assertEqual(model_part.NumberOfElements(), 0) @@ -380,6 +388,7 @@ def test_model_part_elements(self): self.assertEqual(model_part.NumberOfElements(), 7) def test_model_part_conditions(self): + model = Model() #this should go before the creation of any modelpart model_part = ModelPart("Main") self.assertEqual(model_part.NumberOfConditions(), 0) @@ -472,6 +481,7 @@ def test_model_part_conditions(self): self.assertEqual(model_part.NumberOfConditions(), 7) def test_modelpart_variables_list(self): + model = Model() #this should go before the creation of any modelpart model_part = ModelPart("Main") model_part.AddNodalSolutionStepVariable(VELOCITY) @@ -482,6 +492,7 @@ def test_modelpart_variables_list(self): self.assertTrue(model_part.Nodes[1].SolutionStepsDataHas(VELOCITY)) def test_modelpart_buffersize(self): + model = Model() #this should go before the creation of any modelpart model_part = ModelPart("Main") model_part.SetBufferSize(3) @@ -490,6 +501,7 @@ def test_modelpart_buffersize(self): self.assertEqual(model_part.GetBufferSize(), submodel.GetBufferSize() ) def test_add_node(self): + model = Model() #this should go before the creation of any modelpart model_part1 = ModelPart("Main") sub1 = model_part1.CreateSubModelPart("sub1") sub2 = model_part1.CreateSubModelPart("sub2") @@ -536,6 +548,7 @@ def test_add_node(self): self.assertFalse( n5.Id in sub2.Nodes ) def test_add_condition(self): + model = Model() #this should go before the creation of any modelpart model_part1 = ModelPart("Main") sub1 = model_part1.CreateSubModelPart("sub1") sub2 = model_part1.CreateSubModelPart("sub2") @@ -585,6 +598,7 @@ def test_add_condition(self): def test_add_element(self): + model = Model() #this should go before the creation of any modelpart model_part1 = ModelPart("Main") sub1 = model_part1.CreateSubModelPart("sub1") sub2 = model_part1.CreateSubModelPart("sub2") @@ -633,6 +647,7 @@ def test_add_element(self): self.assertFalse( e5.Id in sub2.Elements ) def test_model_part_iterators(self): + model = Model() #this should go before the creation of any modelpart model_part1 = ModelPart("Main") sub1 = model_part1.CreateSubModelPart("sub1") sub2 = model_part1.CreateSubModelPart("sub2")