From 7335fd4f86f9a56897c46ffeb876e3d7a20fe27d Mon Sep 17 00:00:00 2001 From: philbucher Date: Mon, 14 May 2018 18:03:59 +0200 Subject: [PATCH 01/17] Adding first version of solver --- kratos/python_scripts/python_solver.py | 94 ++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 kratos/python_scripts/python_solver.py diff --git a/kratos/python_scripts/python_solver.py b/kratos/python_scripts/python_solver.py new file mode 100644 index 000000000000..8fc4c6bad7bc --- /dev/null +++ b/kratos/python_scripts/python_solver.py @@ -0,0 +1,94 @@ +from __future__ import print_function, absolute_import, division # makes KratosMultiphysics backward compatible with python 2.6 and 2.7 + +# Importing Kratos +import KratosMultiphysics + +class PythonSolver(object): + """The base class for the Python Solvers in the applications + Changes to this BaseClass have to be discussed first! + """ + def __init__(self, model_part, solver_settings): + """The constructor of the PythonSolver-Object. + + It is intended to be called from the constructor + of deriving classes: + super(DerivedSolver, self).__init__(solver_settings) + + Keyword arguments: + self -- It signifies an instance of a class. + model_part -- The ModelPart to be used + solver_settings -- The solver settings used + """ + if (type(model_part) != KratosMultiphysics.ModelPart): + raise Exception("Input is expected to be provided as a Kratos Model object") + + if (type(solver_settings) != KratosMultiphysics.Parameters): + raise Exception("Input is expected to be provided as a Kratos Parameters object") + + self.main_model_part = model_part + self.solver_settings = solver_settings + + def AddVariables(self): + pass + + def AddDofs(self): + pass + + def GetMinimumBufferSize(self): + pass + + def ReadModelPart(self): + # TODO replace the functions in the solvers in Fluid and Structure + KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Reading model part.") + problem_path = os.getcwd() + input_filename = self.settings["model_import_settings"]["input_filename"].GetString() + if self.is_restarted(): + self.get_restart_utility().LoadRestart() + elif(self.settings["model_import_settings"]["input_type"].GetString() == "mdpa"): + # Import model part from mdpa file. + KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Reading model part from file: " + os.path.join(problem_path, input_filename) + ".mdpa") + KratosMultiphysics.ModelPartIO(input_filename).ReadModelPart(self.main_model_part) + KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Finished reading model part from mdpa file.") + else: + raise Exception("Other model part input options are not yet implemented.") + KratosMultiphysics.Logger.PrintInfo("ModelPart", self.main_model_part) + KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]:: ", "Finished reading model part.") + + def PrepareModelPartForSolver(self): + pass + + def ExportModelPart(self): + name_out_file = self.settings["model_import_settings"]["input_filename"].GetString()+".out" + file = open(name_out_file + ".mdpa","w") + file.close() + KratosMultiphysics.ModelPartIO(name_out_file, KratosMultiphysics.IO.WRITE).WriteModelPart(self.main_model_part) + + def AdvanceInTime(self): + pass + + def ComputeDeltaTime(self): + pass + + def Initialize(self): + pass + + def Predict(self): + pass + + def InitializeSolutionStep(self): + pass + + def FinalizeSolutionStep(self): + pass + + def SolveSolutionStep(self): + pass + + def Check(self): + pass + + def Clear(self): + pass + + def GetComputingModelPart(self): + return self.main_model_part.GetSubModelPart(self.settings["computing_model_part_name"].GetString()) From 8c58792f5845fc612ccae080cc1c2cf1abada4eb Mon Sep 17 00:00:00 2001 From: philbucher Date: Mon, 14 May 2018 18:04:20 +0200 Subject: [PATCH 02/17] Applying some of the changes to the StructMech solver --- .../structural_mechanics_solver.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py index eed40af6a56f..51e3c7725567 100755 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py @@ -9,6 +9,9 @@ # Import applications import KratosMultiphysics.StructuralMechanicsApplication as StructuralMechanicsApplication +# Importing the base class +from python_solver import PythonSolver + # Other imports import os @@ -17,7 +20,7 @@ def CreateSolver(main_model_part, custom_settings): return MechanicalSolver(main_model_part, custom_settings) -class MechanicalSolver(object): +class MechanicalSolver(PythonSolver): """The base class for structural mechanics solvers. This class provides functions for importing and exporting models, @@ -45,6 +48,8 @@ class MechanicalSolver(object): main_model_part -- the model part used to construct the solver. """ def __init__(self, main_model_part, custom_settings): + super(MechanicalSolver, self).__init__(main_model_part, custom_settings) + default_settings = KratosMultiphysics.Parameters(""" { "echo_level": 0, @@ -113,8 +118,6 @@ def __init__(self, main_model_part, custom_settings): self.settings = custom_settings self.settings.ValidateAndAssignDefaults(default_settings) - #TODO: shall obtain the computing_model_part from the MODEL once the object is implemented - self.main_model_part = main_model_part self.print_on_rank_zero("::[MechanicalSolver]:: ", "Construction finished") # Set if the analysis is restarted @@ -209,12 +212,6 @@ def PrepareModelPartForSolver(self): self._set_and_fill_buffer() KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "ModelPart prepared for Solver.") - def ExportModelPart(self): - name_out_file = self.settings["model_import_settings"]["input_filename"].GetString()+".out" - file = open(name_out_file + ".mdpa","w") - file.close() - KratosMultiphysics.ModelPartIO(name_out_file, KratosMultiphysics.IO.WRITE).WriteModelPart(self.main_model_part) - def Initialize(self): """Perform initialization after adding nodal variables and dofs to the main model part. """ self.print_on_rank_zero("::[MechanicalSolver]:: ", "Initializing ...") @@ -235,9 +232,6 @@ def Initialize(self): self.Check() self.print_on_rank_zero("::[MechanicalSolver]:: ", "Finished initialization.") - def GetComputingModelPart(self): - return self.main_model_part.GetSubModelPart(self.settings["computing_model_part_name"].GetString()) - def GetOutputVariables(self): pass From 16a02cfe077ceed8b44d73ecdcb49dd48fe01df5 Mon Sep 17 00:00:00 2001 From: philbucher Date: Wed, 16 May 2018 13:48:02 +0200 Subject: [PATCH 03/17] WIP --- .../structural_mechanics_solver.py | 34 ++++---- kratos/python_scripts/python_solver.py | 78 ++++++++++++++++--- 2 files changed, 84 insertions(+), 28 deletions(-) diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py index 51e3c7725567..42156e7fcd3c 100755 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py @@ -171,23 +171,23 @@ def AddDofs(self): KratosMultiphysics.VariableUtils().AddDof(KratosMultiphysics.PRESSURE, KratosMultiphysics.PRESSURE_REACTION,self.main_model_part) self.print_on_rank_zero("::[MechanicalSolver]:: ", "DOF's ADDED") - def ImportModelPart(self): - """ Legacy function, use ReadModelPart and PrepareModelPartForSolver instead """ - KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "Importing model part.") - problem_path = os.getcwd() - input_filename = self.settings["model_import_settings"]["input_filename"].GetString() - if self.is_restarted(): - self.get_restart_utility().LoadRestart() - elif(self.settings["model_import_settings"]["input_type"].GetString() == "mdpa"): - # Import model part from mdpa file. - KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "Reading model part from file: " + os.path.join(problem_path, input_filename) + ".mdpa") - KratosMultiphysics.ModelPartIO(input_filename).ReadModelPart(self.main_model_part) - KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "Finished reading model part from mdpa file.") - self.PrepareModelPartForSolver() - else: - raise Exception("Other model part input options are not yet implemented.") - KratosMultiphysics.Logger.PrintInfo("ModelPart", self.main_model_part) - KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]:: ", "Finished importing model part.") + # def ImportModelPart(self): + # """ Legacy function, use ReadModelPart and PrepareModelPartForSolver instead """ + # KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "Importing model part.") + # problem_path = os.getcwd() + # input_filename = self.settings["model_import_settings"]["input_filename"].GetString() + # if self.is_restarted(): + # self.get_restart_utility().LoadRestart() + # elif(self.settings["model_import_settings"]["input_type"].GetString() == "mdpa"): + # # Import model part from mdpa file. + # KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "Reading model part from file: " + os.path.join(problem_path, input_filename) + ".mdpa") + # KratosMultiphysics.ModelPartIO(input_filename).ReadModelPart(self.main_model_part) + # KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "Finished reading model part from mdpa file.") + # self.PrepareModelPartForSolver() + # else: + # raise Exception("Other model part input options are not yet implemented.") + # KratosMultiphysics.Logger.PrintInfo("ModelPart", self.main_model_part) + # KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]:: ", "Finished importing model part.") def ReadModelPart(self): KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "Reading model part.") diff --git a/kratos/python_scripts/python_solver.py b/kratos/python_scripts/python_solver.py index 8fc4c6bad7bc..bc5b8f468533 100644 --- a/kratos/python_scripts/python_solver.py +++ b/kratos/python_scripts/python_solver.py @@ -29,22 +29,26 @@ def __init__(self, model_part, solver_settings): self.solver_settings = solver_settings def AddVariables(self): + """This function add the Variables needed by this PythonSolver to the the ModelPart + It has to be called BEFORE the ModelPart is read! + """ pass def AddDofs(self): - pass - - def GetMinimumBufferSize(self): + """This function add the Dofs needed by this PythonSolver to the the ModelPart + It has to be called AFTER the ModelPart is read! + """ pass def ReadModelPart(self): - # TODO replace the functions in the solvers in Fluid and Structure + """This function reads the ModelPart + """ KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Reading model part.") problem_path = os.getcwd() - input_filename = self.settings["model_import_settings"]["input_filename"].GetString() + input_filename = self.solver_settings["model_import_settings"]["input_filename"].GetString() if self.is_restarted(): self.get_restart_utility().LoadRestart() - elif(self.settings["model_import_settings"]["input_type"].GetString() == "mdpa"): + elif(self.solver_settings["model_import_settings"]["input_type"].GetString() == "mdpa"): # Import model part from mdpa file. KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Reading model part from file: " + os.path.join(problem_path, input_filename) + ".mdpa") KratosMultiphysics.ModelPartIO(input_filename).ReadModelPart(self.main_model_part) @@ -54,41 +58,93 @@ def ReadModelPart(self): KratosMultiphysics.Logger.PrintInfo("ModelPart", self.main_model_part) KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]:: ", "Finished reading model part.") - def PrepareModelPartForSolver(self): + def PrepareModelPart(self): + """This function prepares the ModelPart for being used by the PythonSolver + """ pass + def GetMinimumBufferSize(self): + """This function returns the minimum buffer size needed for this PythonSolver + """ + raise Exception('Please implement "GetMinimumBufferSize" in your derived solver') + + def ImportModelPart(self): + warning_msg = 'Using "ImportModelPart" is deprecated and will be removed in the future!\n' + warning_msg = 'Use "ReadModelPart" + "PrepareModelPart" instead' + KratosMultiphysics.Logger.PrintWarning("::[PythonSolver]::", warning_msg) + self.ReadModelPart() + self.PrepareModelPart() + def ExportModelPart(self): - name_out_file = self.settings["model_import_settings"]["input_filename"].GetString()+".out" + """This function exports the ModelPart to and mdpa-file + """ + name_out_file = self.solver_settings["model_import_settings"]["input_filename"].GetString()+".out" file = open(name_out_file + ".mdpa","w") file.close() KratosMultiphysics.ModelPartIO(name_out_file, KratosMultiphysics.IO.WRITE).WriteModelPart(self.main_model_part) def AdvanceInTime(self): + """This function advances the PythonSolver in time + Usage: It is designed to be called once per solution step, before performing the solution + """ pass - def ComputeDeltaTime(self): + def Initialize(self): + """This function initializes the PythonSolver + Usage: It is designed to be called ONCE, BEFORE the execution of the solution-loop + """ pass - def Initialize(self): + def Finalize(self): + """This function finalizes the PythonSolver + Usage: It is designed to be called ONCE, AFTER the execution of the solution-loop + """ pass def Predict(self): + """This function performs all the required operations that should be executed + (for each step) ONCE, AFTER initializing the solution step. + """ pass def InitializeSolutionStep(self): + """This function performs all the required operations that should be executed + (for each step) BEFORE solving the solution step. + """ pass def FinalizeSolutionStep(self): + """This function performs all the required operations that should be executed + (for each step) AFTER solving the solution step. + """ pass def SolveSolutionStep(self): + """This function solves the current step. + It can be called multiple times within one solution step + """ pass def Check(self): + """This function checks the PythonSolver + """ pass + def Solve(self): + warning_msg = 'Using "Solve" is deprecated and will be removed in the future!\n' + warning_msg += 'Use the separate calls to "Initialize", "InitializeSolutionStep", "Predict", ' + warning_msg += '"SolveSolutionStep" and "FinalizeSolutionStep"' + KratosMultiphysics.Logger.PrintWarning("::[PythonSolver]::", warning_msg) + self.Initialize() + self.Predict() + self.InitializeSolutionStep() + self.SolveSolutionStep() + self.FinalizeSolutionStep() + def Clear(self): + """This function clears the PythonSolver + """ pass def GetComputingModelPart(self): - return self.main_model_part.GetSubModelPart(self.settings["computing_model_part_name"].GetString()) + return self.main_model_part.GetSubModelPart(self.solver_settings["computing_model_part_name"].GetString()) From 5d05fe4c6374e4b0cd3cbf64cfa9bf21a5744aa1 Mon Sep 17 00:00:00 2001 From: philbucher Date: Wed, 16 May 2018 16:46:15 +0200 Subject: [PATCH 04/17] Properly integrated Restart --- kratos/python_scripts/python_solver.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/kratos/python_scripts/python_solver.py b/kratos/python_scripts/python_solver.py index bc5b8f468533..7d9f77238be4 100644 --- a/kratos/python_scripts/python_solver.py +++ b/kratos/python_scripts/python_solver.py @@ -46,13 +46,18 @@ def ReadModelPart(self): KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Reading model part.") problem_path = os.getcwd() input_filename = self.solver_settings["model_import_settings"]["input_filename"].GetString() - if self.is_restarted(): - self.get_restart_utility().LoadRestart() - elif(self.solver_settings["model_import_settings"]["input_type"].GetString() == "mdpa"): + input_type = self.solver_settings["model_import_settings"]["input_type"].GetString() + + if (input_type == "mdpa"): # Import model part from mdpa file. KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Reading model part from file: " + os.path.join(problem_path, input_filename) + ".mdpa") KratosMultiphysics.ModelPartIO(input_filename).ReadModelPart(self.main_model_part) KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Finished reading model part from mdpa file.") + elif (input_type == "rest"): + KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Loading model part from restart file.") + from restart_utility import RestartUtility + RestartUtility(self.main_model_part, self._GetRestartSettings()).LoadRestart() + KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Finished loading model part from restart file.") else: raise Exception("Other model part input options are not yet implemented.") KratosMultiphysics.Logger.PrintInfo("ModelPart", self.main_model_part) @@ -148,3 +153,12 @@ def Clear(self): def GetComputingModelPart(self): return self.main_model_part.GetSubModelPart(self.solver_settings["computing_model_part_name"].GetString()) + + + def _GetRestartSettings(self): + restart_settings = KratosMultiphysics.Parameters("""{}""") + restart_settings.AddValue("input_filename", self.solver_settings["model_import_settings"]["input_filename"]) + if self.solver_settings.Has("echo_level") + restart_settings.AddValue("echo_level", self.solver_settings["echo_level"]) + + return restart_settings \ No newline at end of file From d0322420077744d485fc5f14d51fa0bc81b97f86 Mon Sep 17 00:00:00 2001 From: philbucher Date: Wed, 16 May 2018 18:25:54 +0200 Subject: [PATCH 05/17] Adapting Structural Solver --- .../structural_mechanics_solver.py | 37 ------------------- .../tests/restart_tests.py | 11 +++--- kratos/python_scripts/python_solver.py | 11 ++++-- 3 files changed, 14 insertions(+), 45 deletions(-) diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py index 42156e7fcd3c..577d927d7353 100755 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py @@ -12,9 +12,6 @@ # Importing the base class from python_solver import PythonSolver -# Other imports -import os - def CreateSolver(main_model_part, custom_settings): return MechanicalSolver(main_model_part, custom_settings) @@ -171,40 +168,6 @@ def AddDofs(self): KratosMultiphysics.VariableUtils().AddDof(KratosMultiphysics.PRESSURE, KratosMultiphysics.PRESSURE_REACTION,self.main_model_part) self.print_on_rank_zero("::[MechanicalSolver]:: ", "DOF's ADDED") - # def ImportModelPart(self): - # """ Legacy function, use ReadModelPart and PrepareModelPartForSolver instead """ - # KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "Importing model part.") - # problem_path = os.getcwd() - # input_filename = self.settings["model_import_settings"]["input_filename"].GetString() - # if self.is_restarted(): - # self.get_restart_utility().LoadRestart() - # elif(self.settings["model_import_settings"]["input_type"].GetString() == "mdpa"): - # # Import model part from mdpa file. - # KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "Reading model part from file: " + os.path.join(problem_path, input_filename) + ".mdpa") - # KratosMultiphysics.ModelPartIO(input_filename).ReadModelPart(self.main_model_part) - # KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "Finished reading model part from mdpa file.") - # self.PrepareModelPartForSolver() - # else: - # raise Exception("Other model part input options are not yet implemented.") - # KratosMultiphysics.Logger.PrintInfo("ModelPart", self.main_model_part) - # KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]:: ", "Finished importing model part.") - - def ReadModelPart(self): - KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "Reading model part.") - problem_path = os.getcwd() - input_filename = self.settings["model_import_settings"]["input_filename"].GetString() - if self.is_restarted(): - self.get_restart_utility().LoadRestart() - elif(self.settings["model_import_settings"]["input_type"].GetString() == "mdpa"): - # Import model part from mdpa file. - KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "Reading model part from file: " + os.path.join(problem_path, input_filename) + ".mdpa") - KratosMultiphysics.ModelPartIO(input_filename).ReadModelPart(self.main_model_part) - KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "Finished reading model part from mdpa file.") - else: - raise Exception("Other model part input options are not yet implemented.") - KratosMultiphysics.Logger.PrintInfo("ModelPart", self.main_model_part) - KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]:: ", "Finished reading model part.") - def PrepareModelPartForSolver(self): if not self.is_restarted(): # Check and prepare computing model part and import constitutive laws. diff --git a/applications/StructuralMechanicsApplication/tests/restart_tests.py b/applications/StructuralMechanicsApplication/tests/restart_tests.py index 7534fe3c4108..1ac782c15fc9 100644 --- a/applications/StructuralMechanicsApplication/tests/restart_tests.py +++ b/applications/StructuralMechanicsApplication/tests/restart_tests.py @@ -69,13 +69,14 @@ def setUp(self): save_restart_parameters = KratosMultiphysics.Parameters("""{ "save_restart" : true }""") - load_restart_parameters = KratosMultiphysics.Parameters("""{ - "load_restart" : true, - "restart_load_file_label" : "3.0" - }""") self.project_parameters_save["solver_settings"].AddValue("restart_settings", save_restart_parameters) - self.project_parameters_load["solver_settings"].AddValue("restart_settings", load_restart_parameters) + load_mp_import_settings = self.project_parameters_load["solver_settings"]["model_import_settings"] + load_mp_import_settings.AddEmptyValue("restart_load_file_label") + load_mp_import_settings["input_type"].SetString("rest") + load_mp_import_settings["restart_load_file_label"].SetString("3.0") + + # print(self.project_parameters_load.PrettyPrintJsonString()) def test_execution(self): # Within this location context: diff --git a/kratos/python_scripts/python_solver.py b/kratos/python_scripts/python_solver.py index 7d9f77238be4..e001ea0b956b 100644 --- a/kratos/python_scripts/python_solver.py +++ b/kratos/python_scripts/python_solver.py @@ -3,6 +3,9 @@ # Importing Kratos import KratosMultiphysics +# Other imports +import os + class PythonSolver(object): """The base class for the Python Solvers in the applications Changes to this BaseClass have to be discussed first! @@ -156,9 +159,11 @@ def GetComputingModelPart(self): def _GetRestartSettings(self): - restart_settings = KratosMultiphysics.Parameters("""{}""") - restart_settings.AddValue("input_filename", self.solver_settings["model_import_settings"]["input_filename"]) - if self.solver_settings.Has("echo_level") + restart_settings = self.settings["model_import_settings"].Clone() + restart_settings.RemoveValue("input_type") + if not restart_settings.Has("restart_load_file_label"): + raise Exception('"restart_load_file_label" must be specified when starting from a restart-file!') + if self.solver_settings.Has("echo_level"): restart_settings.AddValue("echo_level", self.solver_settings["echo_level"]) return restart_settings \ No newline at end of file From f1f890ebb44f1f118ac671af9f1b8978ac773d63 Mon Sep 17 00:00:00 2001 From: philbucher Date: Wed, 16 May 2018 18:51:36 +0200 Subject: [PATCH 06/17] Final Fixes in StructuralMechanics --- .../structural_mechanics_analysis.py | 2 +- .../structural_mechanics_solver.py | 2 +- .../trilinos_structural_mechanics_solver.py | 22 +++---------------- 3 files changed, 5 insertions(+), 21 deletions(-) diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py index 0bba83211a91..b7fcc227e450 100644 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py @@ -130,7 +130,7 @@ def _SetUpGiDOutput(self): def _ExecuteInitialize(self): """ Initializing the Analysis """ ## ModelPart is being prepared to be used by the solver - self.solver.PrepareModelPartForSolver() + self.solver.PrepareModelPart() ## Adds the Dofs if they don't exist self.solver.AddDofs() diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py index 577d927d7353..a35aad1b5061 100755 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py @@ -168,7 +168,7 @@ def AddDofs(self): KratosMultiphysics.VariableUtils().AddDof(KratosMultiphysics.PRESSURE, KratosMultiphysics.PRESSURE_REACTION,self.main_model_part) self.print_on_rank_zero("::[MechanicalSolver]:: ", "DOF's ADDED") - def PrepareModelPartForSolver(self): + def PrepareModelPart(self): if not self.is_restarted(): # Check and prepare computing model part and import constitutive laws. self._execute_after_reading() diff --git a/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_solver.py b/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_solver.py index 443168c8d42e..50f815e3f45e 100644 --- a/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_solver.py @@ -42,22 +42,6 @@ def AddVariables(self): self.main_model_part.AddNodalSolutionStepVariable(KratosMultiphysics.PARTITION_INDEX) self.print_on_rank_zero("::[TrilinosMechanicalSolver]:: ", "Variables ADDED") - def ImportModelPart(self): - self.print_on_rank_zero("::[TrilinosMechanicalSolver]:: ", "Importing model part.") - if self.is_restarted(): - self.get_restart_utility().LoadRestart() - elif(self.settings["model_import_settings"]["input_type"].GetString() == "mdpa"): - # Construct the Trilinos import model part utility. - import trilinos_import_model_part_utility - self.trilinos_model_part_importer = trilinos_import_model_part_utility.TrilinosImportModelPartUtility(self.main_model_part, self.settings) - # Execute the Metis partitioning and reading. - self.trilinos_model_part_importer.ExecutePartitioningAndReading() - # Call the base class PrepareModelPartForSolver - super(TrilinosMechanicalSolver, self).PrepareModelPartForSolver() - else: - raise Exception("Other model part input options are not yet implemented.") - self.print_on_rank_zero("::[TrilinosMechanicalSolver]:: ", "Finished importing model part.") - def ReadModelPart(self): self.print_on_rank_zero("::[TrilinosMechanicalSolver]:: ", "Reading model part.") if self.is_restarted(): @@ -70,11 +54,11 @@ def ReadModelPart(self): self.trilinos_model_part_importer.ExecutePartitioningAndReading() else: raise Exception("Other model part input options are not yet implemented.") - self.print_on_rank_zero("::[TrilinosMechanicalSolver]:: ", "Finished importing model part.") + self.print_on_rank_zero("::[TrilinosMechanicalSolver]:: ", "Finished reading model part.") - def PrepareModelPartForSolver(self): + def PrepareModel(self): if not self.is_restarted(): - super(TrilinosMechanicalSolver, self).PrepareModelPartForSolver() + super(TrilinosMechanicalSolver, self).PrepareModelPart() # Construct the communicators self.trilinos_model_part_importer.CreateCommunicators() KratosMultiphysics.Logger.PrintInfo("::[TrilinosMechanicalSolver]::", "ModelPart prepared for Solver.") From 08e41df5733774dded8eb37f5c94fe8e5cb05ecc Mon Sep 17 00:00:00 2001 From: philbucher Date: Wed, 16 May 2018 20:10:08 +0200 Subject: [PATCH 07/17] More fixes for consistency --- .../structural_mechanics_solver.py | 7 ++--- kratos/python_scripts/python_solver.py | 26 +++++++++++-------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py index a35aad1b5061..2b45fb7ac90f 100755 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py @@ -57,7 +57,6 @@ def __init__(self, main_model_part, custom_settings): "input_filename": "unknown_name" }, "restart_settings" : { - "load_restart" : false, "save_restart" : false }, "computing_model_part_name" : "computing_domain", @@ -112,15 +111,13 @@ def __init__(self, main_model_part, custom_settings): self.print_warning_on_rank_zero("Time integration method", warning) # Overwrite the default settings with user-provided parameters. - self.settings = custom_settings self.settings.ValidateAndAssignDefaults(default_settings) self.print_on_rank_zero("::[MechanicalSolver]:: ", "Construction finished") # Set if the analysis is restarted - if self.settings["restart_settings"].Has("load_restart"): - load_restart = self.settings["restart_settings"]["load_restart"].GetBool() - self.main_model_part.ProcessInfo[KratosMultiphysics.IS_RESTARTED] = load_restart + if self.settings["model_import_settings"]["input_type"].GetString() == "rest": + self.main_model_part.ProcessInfo[KratosMultiphysics.IS_RESTARTED] = True else: self.main_model_part.ProcessInfo[KratosMultiphysics.IS_RESTARTED] = False diff --git a/kratos/python_scripts/python_solver.py b/kratos/python_scripts/python_solver.py index e001ea0b956b..ce39e5860db1 100644 --- a/kratos/python_scripts/python_solver.py +++ b/kratos/python_scripts/python_solver.py @@ -10,26 +10,26 @@ class PythonSolver(object): """The base class for the Python Solvers in the applications Changes to this BaseClass have to be discussed first! """ - def __init__(self, model_part, solver_settings): + def __init__(self, model_part, settings): """The constructor of the PythonSolver-Object. It is intended to be called from the constructor of deriving classes: - super(DerivedSolver, self).__init__(solver_settings) + super(DerivedSolver, self).__init__(settings) Keyword arguments: self -- It signifies an instance of a class. model_part -- The ModelPart to be used - solver_settings -- The solver settings used + settings -- The solver settings used """ if (type(model_part) != KratosMultiphysics.ModelPart): raise Exception("Input is expected to be provided as a Kratos Model object") - if (type(solver_settings) != KratosMultiphysics.Parameters): + if (type(settings) != KratosMultiphysics.Parameters): raise Exception("Input is expected to be provided as a Kratos Parameters object") self.main_model_part = model_part - self.solver_settings = solver_settings + self.settings = settings def AddVariables(self): """This function add the Variables needed by this PythonSolver to the the ModelPart @@ -48,13 +48,17 @@ def ReadModelPart(self): """ KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Reading model part.") problem_path = os.getcwd() - input_filename = self.solver_settings["model_import_settings"]["input_filename"].GetString() - input_type = self.solver_settings["model_import_settings"]["input_type"].GetString() + model_import_settings = self.settings["model_import_settings"] + input_filename = model_import_settings["input_filename"].GetString() + input_type = model_import_settings["input_type"].GetString() if (input_type == "mdpa"): # Import model part from mdpa file. KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Reading model part from file: " + os.path.join(problem_path, input_filename) + ".mdpa") KratosMultiphysics.ModelPartIO(input_filename).ReadModelPart(self.main_model_part) + if (model_import_settings.Has("reorder") and model_import_settings["reorder"].GetBool()): + tmp = KratosMultiphysics.Parameters("{}") + KratosMultiphysics.ReorderAndOptimizeModelPartProcess(self.main_model_part, tmp).Execute() KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Finished reading model part from mdpa file.") elif (input_type == "rest"): KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Loading model part from restart file.") @@ -86,7 +90,7 @@ def ImportModelPart(self): def ExportModelPart(self): """This function exports the ModelPart to and mdpa-file """ - name_out_file = self.solver_settings["model_import_settings"]["input_filename"].GetString()+".out" + name_out_file = self.settings["model_import_settings"]["input_filename"].GetString()+".out" file = open(name_out_file + ".mdpa","w") file.close() KratosMultiphysics.ModelPartIO(name_out_file, KratosMultiphysics.IO.WRITE).WriteModelPart(self.main_model_part) @@ -155,7 +159,7 @@ def Clear(self): pass def GetComputingModelPart(self): - return self.main_model_part.GetSubModelPart(self.solver_settings["computing_model_part_name"].GetString()) + return self.main_model_part.GetSubModelPart(self.settings["computing_model_part_name"].GetString()) def _GetRestartSettings(self): @@ -163,7 +167,7 @@ def _GetRestartSettings(self): restart_settings.RemoveValue("input_type") if not restart_settings.Has("restart_load_file_label"): raise Exception('"restart_load_file_label" must be specified when starting from a restart-file!') - if self.solver_settings.Has("echo_level"): - restart_settings.AddValue("echo_level", self.solver_settings["echo_level"]) + if self.settings.Has("echo_level"): + restart_settings.AddValue("echo_level", self.settings["echo_level"]) return restart_settings \ No newline at end of file From 5b7bf4808e7cb15f626678d05932217e7140c24b Mon Sep 17 00:00:00 2001 From: philbucher Date: Wed, 16 May 2018 20:19:30 +0200 Subject: [PATCH 08/17] removing comment --- .../StructuralMechanicsApplication/tests/restart_tests.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/applications/StructuralMechanicsApplication/tests/restart_tests.py b/applications/StructuralMechanicsApplication/tests/restart_tests.py index 1ac782c15fc9..de964a65760d 100644 --- a/applications/StructuralMechanicsApplication/tests/restart_tests.py +++ b/applications/StructuralMechanicsApplication/tests/restart_tests.py @@ -76,8 +76,6 @@ def setUp(self): load_mp_import_settings["input_type"].SetString("rest") load_mp_import_settings["restart_load_file_label"].SetString("3.0") - # print(self.project_parameters_load.PrettyPrintJsonString()) - def test_execution(self): # Within this location context: with controlledExecutionScope(os.path.dirname(os.path.realpath(__file__))): From fb8dab01de37fd4d830cbb021fc5dbb019816fa4 Mon Sep 17 00:00:00 2001 From: philbucher Date: Sat, 19 May 2018 17:57:08 +0200 Subject: [PATCH 09/17] codacy suggestion --- kratos/python_scripts/python_solver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kratos/python_scripts/python_solver.py b/kratos/python_scripts/python_solver.py index ce39e5860db1..c0e17f3fdcca 100644 --- a/kratos/python_scripts/python_solver.py +++ b/kratos/python_scripts/python_solver.py @@ -95,7 +95,7 @@ def ExportModelPart(self): file.close() KratosMultiphysics.ModelPartIO(name_out_file, KratosMultiphysics.IO.WRITE).WriteModelPart(self.main_model_part) - def AdvanceInTime(self): + def AdvanceInTime(self, current_time): """This function advances the PythonSolver in time Usage: It is designed to be called once per solution step, before performing the solution """ From 636ab02628f97b65ede12b2985962466638fbf1b Mon Sep 17 00:00:00 2001 From: philbucher Date: Thu, 24 May 2018 12:02:13 +0200 Subject: [PATCH 10/17] minor --- kratos/python_scripts/python_solver.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/kratos/python_scripts/python_solver.py b/kratos/python_scripts/python_solver.py index c0e17f3fdcca..51307e6daa71 100644 --- a/kratos/python_scripts/python_solver.py +++ b/kratos/python_scripts/python_solver.py @@ -31,6 +31,8 @@ def __init__(self, model_part, settings): self.main_model_part = model_part self.settings = settings + self.echo_level = self.settings["echo_level"].GetInt() + def AddVariables(self): """This function add the Variables needed by this PythonSolver to the the ModelPart It has to be called BEFORE the ModelPart is read! @@ -142,6 +144,11 @@ def Check(self): """ pass + def Clear(self): + """This function clears the PythonSolver + """ + pass + def Solve(self): warning_msg = 'Using "Solve" is deprecated and will be removed in the future!\n' warning_msg += 'Use the separate calls to "Initialize", "InitializeSolutionStep", "Predict", ' @@ -153,13 +160,8 @@ def Solve(self): self.SolveSolutionStep() self.FinalizeSolutionStep() - def Clear(self): - """This function clears the PythonSolver - """ - pass - def GetComputingModelPart(self): - return self.main_model_part.GetSubModelPart(self.settings["computing_model_part_name"].GetString()) + raise Exception("This function has to be implemented in the derived class") def _GetRestartSettings(self): From 0b50e995b9f732c56d7f98e0389834d348bec374 Mon Sep 17 00:00:00 2001 From: philbucher Date: Fri, 25 May 2018 15:55:00 +0200 Subject: [PATCH 11/17] Passing the Model to the solver insted of ModelPart --- .../python_solvers_wrapper_structural.py | 8 +-- .../structural_mechanics_analysis.py | 13 +--- ...tural_mechanics_explicit_dynamic_solver.py | 8 +-- ...ural_mechanics_harmonic_analysis_solver.py | 8 +-- ...tural_mechanics_implicit_dynamic_solver.py | 8 +-- .../structural_mechanics_solver.py | 37 ++++++++-- .../structural_mechanics_static_solver.py | 8 +-- ...tural_mechanics_implicit_dynamic_solver.py | 12 ++-- .../trilinos_structural_mechanics_solver.py | 8 +-- ...inos_structural_mechanics_static_solver.py | 8 +-- kratos/python_scripts/python_solver.py | 72 +++++++++---------- 11 files changed, 105 insertions(+), 85 deletions(-) diff --git a/applications/StructuralMechanicsApplication/python_scripts/python_solvers_wrapper_structural.py b/applications/StructuralMechanicsApplication/python_scripts/python_solvers_wrapper_structural.py index 318afa3ee54c..adb3a8f0a7af 100644 --- a/applications/StructuralMechanicsApplication/python_scripts/python_solvers_wrapper_structural.py +++ b/applications/StructuralMechanicsApplication/python_scripts/python_solvers_wrapper_structural.py @@ -3,10 +3,10 @@ import KratosMultiphysics -def CreateSolver(main_model_part, custom_settings): +def CreateSolver(model, custom_settings): - if (type(main_model_part) != KratosMultiphysics.ModelPart): - raise Exception("input is expected to be provided as a Kratos ModelPart object") + if (type(model) != KratosMultiphysics.Model): + raise Exception("input is expected to be provided as a Kratos Model object") if (type(custom_settings) != KratosMultiphysics.Parameters): raise Exception("input is expected to be provided as a Kratos Parameters object") @@ -69,6 +69,6 @@ def CreateSolver(main_model_part, custom_settings): custom_settings["solver_settings"].RemoveValue("time_integration_method") # does not throw even if the value is not existing solver_module = __import__(solver_module_name) - solver = solver_module.CreateSolver(main_model_part, custom_settings["solver_settings"]) + solver = solver_module.CreateSolver(model, custom_settings["solver_settings"]) return solver diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py index 9fe07cc5a35d..0c1f6c36769f 100644 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py @@ -104,7 +104,7 @@ def _CreateSolver(self, external_model_part=None): ## Solver construction import python_solvers_wrapper_structural - self.solver = python_solvers_wrapper_structural.CreateSolver(self.main_model_part, self.project_parameters) + self.solver = python_solvers_wrapper_structural.CreateSolver(self.model, self.project_parameters) ## Adds the necessary variables to the model_part only if they don't exist self.solver.AddVariables() @@ -139,12 +139,6 @@ def _ExecuteInitialize(self): if not self.using_external_model_part: self.model.AddModelPart(self.main_model_part) - ## Print model_part and properties - if self.is_printing_rank and self.echo_level > 1: - KratosMultiphysics.Logger.PrintInfo("ModelPart", self.main_model_part) - for properties in self.main_model_part.Properties: - KratosMultiphysics.Logger.PrintInfo("Property " + str(properties.Id), properties) - def _SetUpListOfProcesses(self): from process_factory import KratosProcessFactory factory = KratosProcessFactory(self.model) @@ -189,11 +183,10 @@ def _ExecuteBeforeSolutionLoop(self): start_time = self.project_parameters["problem_data"]["start_time"].GetDouble() self.end_time = self.project_parameters["problem_data"]["end_time"].GetDouble() - if self.main_model_part.ProcessInfo[KratosMultiphysics.IS_RESTARTED] is True: - self.time = self.main_model_part.ProcessInfo[KratosMultiphysics.TIME] + if self.solver.GetComputingModelPart().ProcessInfo[KratosMultiphysics.IS_RESTARTED] is True: + self.time = self.solver.GetComputingModelPart().ProcessInfo[KratosMultiphysics.TIME] else: self.time = start_time - self.main_model_part.ProcessInfo[KratosMultiphysics.STEP] = 0 if self.is_printing_rank: KratosMultiphysics.Logger.PrintInfo(self._GetSimulationName(), "Analysis -START- ") diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_explicit_dynamic_solver.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_explicit_dynamic_solver.py index 0369d65c1d67..bc7fc1fd11b7 100644 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_explicit_dynamic_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_explicit_dynamic_solver.py @@ -16,8 +16,8 @@ -def CreateSolver(main_model_part, custom_settings): - return ExplicitMechanicalSolver(main_model_part, custom_settings) +def CreateSolver(model, custom_settings): + return ExplicitMechanicalSolver(model, custom_settings) class ExplicitMechanicalSolver(structural_mechanics_solver.MechanicalSolver): """The structural mechanics explicit dynamic solver. @@ -29,7 +29,7 @@ class ExplicitMechanicalSolver(structural_mechanics_solver.MechanicalSolver): See structural_mechanics_solver.py for more information. """ - def __init__(self, main_model_part, custom_settings): + def __init__(self, model, custom_settings): # Set defaults and validate custom settings. self.dynamic_settings = KratosMultiphysics.Parameters(""" { @@ -44,7 +44,7 @@ def __init__(self, main_model_part, custom_settings): # Construct the base solver. - super(ExplicitMechanicalSolver, self).__init__(main_model_part, custom_settings) + super(ExplicitMechanicalSolver, self).__init__(model, custom_settings) self.print_on_rank_zero("::[ExplicitMechanicalSolver]:: Construction finished") def AddVariables(self): diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_harmonic_analysis_solver.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_harmonic_analysis_solver.py index 372c838a9e30..b662e9ef49ef 100644 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_harmonic_analysis_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_harmonic_analysis_solver.py @@ -13,8 +13,8 @@ import structural_mechanics_solver -def CreateSolver(main_model_part, custom_settings): - return HarmonicAnalysisSolver(main_model_part, custom_settings) +def CreateSolver(model, custom_settings): + return HarmonicAnalysisSolver(model, custom_settings) class HarmonicAnalysisSolver(structural_mechanics_solver.MechanicalSolver): @@ -28,7 +28,7 @@ class HarmonicAnalysisSolver(structural_mechanics_solver.MechanicalSolver): See structural_mechanics_solver.py for more information. """ - def __init__(self, main_model_part, custom_settings): + def __init__(self, model, custom_settings): # Set defaults and validate custom settings. self.harmonic_analysis_settings = KratosMultiphysics.Parameters(""" { @@ -42,7 +42,7 @@ def __init__(self, main_model_part, custom_settings): # Validate the remaining settings in the base class. # Construct the base solver. - super(HarmonicAnalysisSolver, self).__init__(main_model_part, custom_settings) + super(HarmonicAnalysisSolver, self).__init__(model, custom_settings) self.print_on_rank_zero("::[HarmonicAnalysisSolver]:: ", "Construction finished") #### Private functions #### diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_implicit_dynamic_solver.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_implicit_dynamic_solver.py index c601a7c22643..0de36dd4a926 100644 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_implicit_dynamic_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_implicit_dynamic_solver.py @@ -13,8 +13,8 @@ import structural_mechanics_solver -def CreateSolver(main_model_part, custom_settings): - return ImplicitMechanicalSolver(main_model_part, custom_settings) +def CreateSolver(model, custom_settings): + return ImplicitMechanicalSolver(model, custom_settings) class ImplicitMechanicalSolver(structural_mechanics_solver.MechanicalSolver): @@ -28,7 +28,7 @@ class ImplicitMechanicalSolver(structural_mechanics_solver.MechanicalSolver): See structural_mechanics_solver.py for more information. """ - def __init__(self, main_model_part, custom_settings): + def __init__(self, model, custom_settings): # Set defaults and validate custom settings. self.dynamic_settings = KratosMultiphysics.Parameters(""" { @@ -42,7 +42,7 @@ def __init__(self, main_model_part, custom_settings): # Validate the remaining settings in the base class. # Construct the base solver. - super(ImplicitMechanicalSolver, self).__init__(main_model_part, custom_settings) + super(ImplicitMechanicalSolver, self).__init__(model, custom_settings) self.print_on_rank_zero("::[ImplicitMechanicalSolver]:: ", "Construction finished") # Setting minimum buffer diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py index 2b45fb7ac90f..c83ab4d721b4 100755 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py @@ -13,8 +13,8 @@ from python_solver import PythonSolver -def CreateSolver(main_model_part, custom_settings): - return MechanicalSolver(main_model_part, custom_settings) +def CreateSolver(model, custom_settings): + return MechanicalSolver(model, custom_settings) class MechanicalSolver(PythonSolver): @@ -42,13 +42,14 @@ class MechanicalSolver(PythonSolver): Public member variables: settings -- Kratos parameters containing solver settings. - main_model_part -- the model part used to construct the solver. + model -- the model containing the modelpart used to construct the solver. """ - def __init__(self, main_model_part, custom_settings): - super(MechanicalSolver, self).__init__(main_model_part, custom_settings) + def __init__(self, model, custom_settings): + super(MechanicalSolver, self).__init__(model, custom_settings) default_settings = KratosMultiphysics.Parameters(""" { + "model_part_name" : "PLEASE_SPECIFY_NAME", "echo_level": 0, "buffer_size": 2, "analysis_type": "non_linear", @@ -112,6 +113,13 @@ def __init__(self, main_model_part, custom_settings): # Overwrite the default settings with user-provided parameters. self.settings.ValidateAndAssignDefaults(default_settings) + model_part_name = self.settings["model_part_name"].GetString() + + # This will be changed once the Model is fully supported! + if self.model.HasModelPart(model_part_name): + self.main_model_part = self.model[model_part_name] + else: + self.main_model_part = KratosMultiphysics.ModelPart(model_part_name) self.print_on_rank_zero("::[MechanicalSolver]:: ", "Construction finished") @@ -165,11 +173,21 @@ def AddDofs(self): KratosMultiphysics.VariableUtils().AddDof(KratosMultiphysics.PRESSURE, KratosMultiphysics.PRESSURE_REACTION,self.main_model_part) self.print_on_rank_zero("::[MechanicalSolver]:: ", "DOF's ADDED") + def ReadModelPart(self): + """This function reads the ModelPart + """ + self._ImportModelPart(self.main_model_part, self.settings["model_import_settings"]) + def PrepareModelPart(self): if not self.is_restarted(): # Check and prepare computing model part and import constitutive laws. self._execute_after_reading() self._set_and_fill_buffer() + + # This will be removed once the Model is fully supported! + if not self.model.HasModelPart(self.main_model_part.Name): + self.model.AddModelPart(self.main_model_part) + KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "ModelPart prepared for Solver.") def Initialize(self): @@ -237,6 +255,15 @@ def SetDeltaTime(self, dt): # This is a TEMPORARY function until the solver can compute dt! self.delta_time = dt + def GetComputingModelPart(self): + return self.main_model_part.GetSubModelPart(self.settings["computing_model_part_name"].GetString()) + + def ExportModelPart(self): + name_out_file = self.settings["model_import_settings"]["input_filename"].GetString()+".out" + file = open(name_out_file + ".mdpa","w") + file.close() + KratosMultiphysics.ModelPartIO(name_out_file, KratosMultiphysics.IO.WRITE).WriteModelPart(self.main_model_part) + def SetEchoLevel(self, level): self.get_mechanical_solution_strategy().SetEchoLevel(level) diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_static_solver.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_static_solver.py index 0e89f18f0f1c..da986fb3fd88 100644 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_static_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_static_solver.py @@ -13,8 +13,8 @@ import structural_mechanics_solver -def CreateSolver(main_model_part, custom_settings): - return StaticMechanicalSolver(main_model_part, custom_settings) +def CreateSolver(model, custom_settings): + return StaticMechanicalSolver(model, custom_settings) class StaticMechanicalSolver(structural_mechanics_solver.MechanicalSolver): @@ -29,7 +29,7 @@ class StaticMechanicalSolver(structural_mechanics_solver.MechanicalSolver): See structural_mechanics_solver.py for more information. """ - def __init__(self, main_model_part, custom_settings): + def __init__(self, model, custom_settings): # Set defaults and validate custom settings. static_settings = KratosMultiphysics.Parameters(""" { @@ -53,7 +53,7 @@ def __init__(self, main_model_part, custom_settings): # Validate the remaining settings in the base class. # Construct the base solver. - super(StaticMechanicalSolver, self).__init__(main_model_part, custom_settings) + super(StaticMechanicalSolver, self).__init__(model, custom_settings) self.print_on_rank_zero("::[StaticMechanicalSolver]:: ", "Construction finished") def Initialize(self): diff --git a/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_implicit_dynamic_solver.py b/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_implicit_dynamic_solver.py index b37adabbe564..4f6b531c3bf8 100644 --- a/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_implicit_dynamic_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_implicit_dynamic_solver.py @@ -15,8 +15,8 @@ import trilinos_structural_mechanics_solver -def CreateSolver(main_model_part, custom_settings): - return TrilinosImplicitMechanicalSolver(main_model_part, custom_settings) +def CreateSolver(model, custom_settings): + return TrilinosImplicitMechanicalSolver(model, custom_settings) class TrilinosImplicitMechanicalSolver(trilinos_structural_mechanics_solver.TrilinosMechanicalSolver): @@ -29,7 +29,7 @@ class TrilinosImplicitMechanicalSolver(trilinos_structural_mechanics_solver.Tril structural_mechanics_solver.py trilinos_structural_mechanics_solver.py """ - def __init__(self, main_model_part, custom_settings): + def __init__(self, model, custom_settings): # Set defaults and validate custom settings. self.dynamic_settings = KratosMultiphysics.Parameters(""" { @@ -41,15 +41,15 @@ def __init__(self, main_model_part, custom_settings): """) self.validate_and_transfer_matching_settings(custom_settings, self.dynamic_settings) # Validate the remaining settings in the base class. - + # Construct the base solver. - super(TrilinosImplicitMechanicalSolver, self).__init__(main_model_part, custom_settings) + super(TrilinosImplicitMechanicalSolver, self).__init__(model, custom_settings) def AddVariables(self): super(TrilinosImplicitMechanicalSolver, self).AddVariables() self._add_dynamic_variables() self.print_on_rank_zero("::[TrilinosImplicitMechanicalSolver]:: Variables ADDED") - + def AddDofs(self): super(TrilinosImplicitMechanicalSolver, self).AddDofs() self._add_dynamic_dofs() diff --git a/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_solver.py b/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_solver.py index 50f815e3f45e..65548fb1b8d6 100644 --- a/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_solver.py @@ -15,8 +15,8 @@ import structural_mechanics_solver -def CreateSolver(main_model_part, custom_settings): - return TrilinosMechanicalSolver(main_model_part, custom_settings) +def CreateSolver(model, custom_settings): + return TrilinosMechanicalSolver(model, custom_settings) class TrilinosMechanicalSolver(structural_mechanics_solver.MechanicalSolver): @@ -24,7 +24,7 @@ class TrilinosMechanicalSolver(structural_mechanics_solver.MechanicalSolver): See structural_mechanics_solver.py for more information. """ - def __init__(self, main_model_part, custom_settings): + def __init__(self, model, custom_settings): if not custom_settings.Has("linear_solver_settings"): # Override defaults in the base class. linear_solver_settings = KratosMultiphysics.Parameters("""{ "solver_type" : "AmesosSolver", @@ -33,7 +33,7 @@ def __init__(self, main_model_part, custom_settings): custom_settings.AddValue("linear_solver_settings", linear_solver_settings) # Construct the base solver. - super(TrilinosMechanicalSolver, self).__init__(main_model_part, custom_settings) + super(TrilinosMechanicalSolver, self).__init__(model, custom_settings) self.print_on_rank_zero("::[TrilinosMechanicalSolver]:: ", "Construction finished") def AddVariables(self): diff --git a/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_static_solver.py b/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_static_solver.py index deb459622fa6..1e053ece665f 100644 --- a/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_static_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/trilinos_structural_mechanics_static_solver.py @@ -15,8 +15,8 @@ import trilinos_structural_mechanics_solver -def CreateSolver(main_model_part, custom_settings): - return TrilinosStaticMechanicalSolver(main_model_part, custom_settings) +def CreateSolver(model, custom_settings): + return TrilinosStaticMechanicalSolver(model, custom_settings) class TrilinosStaticMechanicalSolver(trilinos_structural_mechanics_solver.TrilinosMechanicalSolver): @@ -26,9 +26,9 @@ class TrilinosStaticMechanicalSolver(trilinos_structural_mechanics_solver.Trilin structural_mechanics_solver.py trilinos_structural_mechanics_solver.py """ - def __init__(self, main_model_part, custom_settings): + def __init__(self, model, custom_settings): # Construct the base solver. - super(TrilinosStaticMechanicalSolver, self).__init__(main_model_part, custom_settings) + super(TrilinosStaticMechanicalSolver, self).__init__(model, custom_settings) self.print_on_rank_zero("::[TrilinosStaticMechanicalSolver]:: ", "Construction finished") def _create_solution_scheme(self): diff --git a/kratos/python_scripts/python_solver.py b/kratos/python_scripts/python_solver.py index 51307e6daa71..66a016f469cd 100644 --- a/kratos/python_scripts/python_solver.py +++ b/kratos/python_scripts/python_solver.py @@ -10,7 +10,7 @@ class PythonSolver(object): """The base class for the Python Solvers in the applications Changes to this BaseClass have to be discussed first! """ - def __init__(self, model_part, settings): + def __init__(self, model, settings): """The constructor of the PythonSolver-Object. It is intended to be called from the constructor @@ -19,16 +19,16 @@ def __init__(self, model_part, settings): Keyword arguments: self -- It signifies an instance of a class. - model_part -- The ModelPart to be used + model -- The Model to be used settings -- The solver settings used """ - if (type(model_part) != KratosMultiphysics.ModelPart): + if (type(model) != KratosMultiphysics.Model): raise Exception("Input is expected to be provided as a Kratos Model object") if (type(settings) != KratosMultiphysics.Parameters): raise Exception("Input is expected to be provided as a Kratos Parameters object") - self.main_model_part = model_part + self.model = model self.settings = settings self.echo_level = self.settings["echo_level"].GetInt() @@ -48,29 +48,7 @@ def AddDofs(self): def ReadModelPart(self): """This function reads the ModelPart """ - KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Reading model part.") - problem_path = os.getcwd() - model_import_settings = self.settings["model_import_settings"] - input_filename = model_import_settings["input_filename"].GetString() - input_type = model_import_settings["input_type"].GetString() - - if (input_type == "mdpa"): - # Import model part from mdpa file. - KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Reading model part from file: " + os.path.join(problem_path, input_filename) + ".mdpa") - KratosMultiphysics.ModelPartIO(input_filename).ReadModelPart(self.main_model_part) - if (model_import_settings.Has("reorder") and model_import_settings["reorder"].GetBool()): - tmp = KratosMultiphysics.Parameters("{}") - KratosMultiphysics.ReorderAndOptimizeModelPartProcess(self.main_model_part, tmp).Execute() - KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Finished reading model part from mdpa file.") - elif (input_type == "rest"): - KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Loading model part from restart file.") - from restart_utility import RestartUtility - RestartUtility(self.main_model_part, self._GetRestartSettings()).LoadRestart() - KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Finished loading model part from restart file.") - else: - raise Exception("Other model part input options are not yet implemented.") - KratosMultiphysics.Logger.PrintInfo("ModelPart", self.main_model_part) - KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]:: ", "Finished reading model part.") + raise Exception("This function has to be implemented in the derived class") def PrepareModelPart(self): """This function prepares the ModelPart for being used by the PythonSolver @@ -92,10 +70,7 @@ def ImportModelPart(self): def ExportModelPart(self): """This function exports the ModelPart to and mdpa-file """ - name_out_file = self.settings["model_import_settings"]["input_filename"].GetString()+".out" - file = open(name_out_file + ".mdpa","w") - file.close() - KratosMultiphysics.ModelPartIO(name_out_file, KratosMultiphysics.IO.WRITE).WriteModelPart(self.main_model_part) + raise Exception("This function has to be implemented in the derived class") def AdvanceInTime(self, current_time): """This function advances the PythonSolver in time @@ -140,7 +115,7 @@ def SolveSolutionStep(self): pass def Check(self): - """This function checks the PythonSolver + """This function checks the PythonSolver. It usually calls the "Check" function of a solving strategy """ pass @@ -163,13 +138,38 @@ def Solve(self): def GetComputingModelPart(self): raise Exception("This function has to be implemented in the derived class") + def _ImportModelPart(self, model_part, model_part_import_settings): + """This function imports the ModelPart + """ + KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Reading model part.") + problem_path = os.getcwd() + input_filename = model_part_import_settings["input_filename"].GetString() + input_type = model_part_import_settings["input_type"].GetString() + + if (input_type == "mdpa"): + # Import model part from mdpa file. + KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Reading model part from file: " + os.path.join(problem_path, input_filename) + ".mdpa") + KratosMultiphysics.ModelPartIO(input_filename).ReadModelPart(model_part) + if (model_part_import_settings.Has("reorder") and model_part_import_settings["reorder"].GetBool()): + tmp = KratosMultiphysics.Parameters("{}") + KratosMultiphysics.ReorderAndOptimizeModelPartProcess(model_part, tmp).Execute() + KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Finished reading model part from mdpa file.") + elif (input_type == "rest"): + KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Loading model part from restart file.") + from restart_utility import RestartUtility + RestartUtility(model_part, self._GetRestartSettings(model_part_import_settings)).LoadRestart() + KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]::", "Finished loading model part from restart file.") + else: + raise Exception("Other model part input options are not yet implemented.") + KratosMultiphysics.Logger.PrintInfo("ModelPart", model_part) + KratosMultiphysics.Logger.PrintInfo("::[PythonSolver]:: ", "Finished reading model part.") - def _GetRestartSettings(self): - restart_settings = self.settings["model_import_settings"].Clone() + def _GetRestartSettings(self, model_part_import_settings): + restart_settings = model_part_import_settings.Clone() restart_settings.RemoveValue("input_type") if not restart_settings.Has("restart_load_file_label"): raise Exception('"restart_load_file_label" must be specified when starting from a restart-file!') - if self.settings.Has("echo_level"): - restart_settings.AddValue("echo_level", self.settings["echo_level"]) + if model_part_import_settings.Has("echo_level"): + restart_settings.AddValue("echo_level", model_part_import_settings["echo_level"]) return restart_settings \ No newline at end of file From 05b1c4545585b1b8f8e7e3f2f9802b336fd5fc91 Mon Sep 17 00:00:00 2001 From: philbucher Date: Mon, 28 May 2018 12:48:41 +0200 Subject: [PATCH 12/17] ReadModelPart => ImportModelPart --- kratos/python_scripts/python_solver.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/kratos/python_scripts/python_solver.py b/kratos/python_scripts/python_solver.py index 66a016f469cd..1b90ca9eceff 100644 --- a/kratos/python_scripts/python_solver.py +++ b/kratos/python_scripts/python_solver.py @@ -45,7 +45,7 @@ def AddDofs(self): """ pass - def ReadModelPart(self): + def ImportModelPart(self): """This function reads the ModelPart """ raise Exception("This function has to be implemented in the derived class") @@ -60,13 +60,6 @@ def GetMinimumBufferSize(self): """ raise Exception('Please implement "GetMinimumBufferSize" in your derived solver') - def ImportModelPart(self): - warning_msg = 'Using "ImportModelPart" is deprecated and will be removed in the future!\n' - warning_msg = 'Use "ReadModelPart" + "PrepareModelPart" instead' - KratosMultiphysics.Logger.PrintWarning("::[PythonSolver]::", warning_msg) - self.ReadModelPart() - self.PrepareModelPart() - def ExportModelPart(self): """This function exports the ModelPart to and mdpa-file """ From b4a8ef1dd447b7cbd93a628c08698664616245a2 Mon Sep 17 00:00:00 2001 From: philbucher Date: Mon, 28 May 2018 12:52:48 +0200 Subject: [PATCH 13/17] Changes to fully support passing the Model to the solver and in preparation og #2135 --- .../structural_mechanics_analysis.py | 34 ++++++++----------- .../structural_mechanics_solver.py | 27 +++++++++++---- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py index 0c1f6c36769f..26434fe6a4a1 100644 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py @@ -31,6 +31,17 @@ class StructuralMechanicsAnalysis(AnalysisStage): def __init__(self, model, project_parameters): super(StructuralMechanicsAnalysis, self).__init__(model, project_parameters) + solver_settings = project_parameters["solver_settings"] + if not solver_settings.Has("domain_size"): + KratosMultiphysics.Logger.PrintInfo("StructuralMechanicsAnalysis", "Using the old way to pass the domain_size, this will be removed!") + solver_settings.AddEmptyValue("domain_size") + solver_settings["domain_size"].SetInt(project_parameters["problem_data"]["domain_size"].GetInt()) + + if not solver_settings.Has("model_part_name"): + KratosMultiphysics.Logger.PrintInfo("StructuralMechanicsAnalysis", "Using the old way to pass the model_part_name, this will be removed!") + solver_settings.AddEmptyValue("model_part_name") + solver_settings["model_part_name"].SetString(project_parameters["problem_data"]["model_part_name"].GetString()) + ## Get echo level and parallel type self.echo_level = self.project_parameters["problem_data"]["echo_level"].GetInt() self.parallel_type = self.project_parameters["problem_data"]["parallel_type"].GetString() @@ -64,7 +75,7 @@ def InitializeSolutionStep(self): super(StructuralMechanicsAnalysis, self).InitializeSolutionStep() if self.is_printing_rank: - KratosMultiphysics.Logger.PrintInfo(self._GetSimulationName(), "STEP: ", self.main_model_part.ProcessInfo[KratosMultiphysics.STEP]) + KratosMultiphysics.Logger.PrintInfo(self._GetSimulationName(), "STEP: ", self.solver.GetComputingModelPart().ProcessInfo[KratosMultiphysics.STEP]) KratosMultiphysics.Logger.PrintInfo(self._GetSimulationName(), "TIME: ", self.time) sys.stdout.flush() @@ -89,19 +100,8 @@ def Finalize(self): #### Internal functions #### - def _CreateSolver(self, external_model_part=None): + def _CreateSolver(self): """ Create the Solver (and create and import the ModelPart if it is not alread in the model) """ - ## Structure model part definition - main_model_part_name = self.project_parameters["problem_data"]["model_part_name"].GetString() - if self.model.HasModelPart(main_model_part_name): - self.main_model_part = self.model[main_model_part_name] - self.using_external_model_part = True - else: - self.main_model_part = KratosMultiphysics.ModelPart(main_model_part_name) - self.main_model_part.ProcessInfo.SetValue(KratosMultiphysics.DOMAIN_SIZE, - self.project_parameters["problem_data"]["domain_size"].GetInt()) - self.using_external_model_part = False - ## Solver construction import python_solvers_wrapper_structural self.solver = python_solvers_wrapper_structural.CreateSolver(self.model, self.project_parameters) @@ -109,9 +109,7 @@ def _CreateSolver(self, external_model_part=None): ## Adds the necessary variables to the model_part only if they don't exist self.solver.AddVariables() - if not self.using_external_model_part: - ## Read the model - note that SetBufferSize is done here - self.solver.ReadModelPart() # TODO move to global instance + self.solver.ImportModelPart() # TODO move to global instance def _SetUpGiDOutput(self): '''Initialize self.output as a GiD output instance.''' @@ -135,10 +133,6 @@ def _ExecuteInitialize(self): ## Adds the Dofs if they don't exist self.solver.AddDofs() - ## Add the Modelpart to the Model if it is not already there - if not self.using_external_model_part: - self.model.AddModelPart(self.main_model_part) - def _SetUpListOfProcesses(self): from process_factory import KratosProcessFactory factory = KratosProcessFactory(self.model) diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py index c83ab4d721b4..7945176df476 100755 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py @@ -41,15 +41,16 @@ class MechanicalSolver(PythonSolver): Only the member variables listed below should be accessed directly. Public member variables: - settings -- Kratos parameters containing solver settings. model -- the model containing the modelpart used to construct the solver. + settings -- Kratos parameters containing solver settings. """ def __init__(self, model, custom_settings): super(MechanicalSolver, self).__init__(model, custom_settings) default_settings = KratosMultiphysics.Parameters(""" { - "model_part_name" : "PLEASE_SPECIFY_NAME", + "model_part_name" : "", + "domain_size" : -1, "echo_level": 0, "buffer_size": 2, "analysis_type": "non_linear", @@ -115,11 +116,20 @@ def __init__(self, model, custom_settings): self.settings.ValidateAndAssignDefaults(default_settings) model_part_name = self.settings["model_part_name"].GetString() + if model_part_name == "": + raise Exception('Please specify a model_part name!') + # This will be changed once the Model is fully supported! if self.model.HasModelPart(model_part_name): self.main_model_part = self.model[model_part_name] + self.solver_imports_model_part = False else: - self.main_model_part = KratosMultiphysics.ModelPart(model_part_name) + self.main_model_part = KratosMultiphysics.ModelPart(model_part_name) # Model.CreateodelPart() + domain_size = self.settings["domain_size"].GetInt() + if domain_size < 0: + raise Exception('Please specify a "domain_size" >= 0!') + self.main_model_part.ProcessInfo.SetValue(KratosMultiphysics.DOMAIN_SIZE, domain_size) + self.solver_imports_model_part = True self.print_on_rank_zero("::[MechanicalSolver]:: ", "Construction finished") @@ -173,10 +183,11 @@ def AddDofs(self): KratosMultiphysics.VariableUtils().AddDof(KratosMultiphysics.PRESSURE, KratosMultiphysics.PRESSURE_REACTION,self.main_model_part) self.print_on_rank_zero("::[MechanicalSolver]:: ", "DOF's ADDED") - def ReadModelPart(self): - """This function reads the ModelPart + def ImportModelPart(self): + """This function imports the ModelPart """ - self._ImportModelPart(self.main_model_part, self.settings["model_import_settings"]) + if self.solver_imports_model_part: + self._ImportModelPart(self.main_model_part, self.settings["model_import_settings"]) def PrepareModelPart(self): if not self.is_restarted(): @@ -184,10 +195,12 @@ def PrepareModelPart(self): self._execute_after_reading() self._set_and_fill_buffer() - # This will be removed once the Model is fully supported! + # This will be removed once the Model is fully supported! => It wont e necessary anymore if not self.model.HasModelPart(self.main_model_part.Name): self.model.AddModelPart(self.main_model_part) + print(self.model) + KratosMultiphysics.Logger.PrintInfo("::[MechanicalSolver]::", "ModelPart prepared for Solver.") def Initialize(self): From 71ef4de6cfb785c1eb57eaaec9a5c45310da485f Mon Sep 17 00:00:00 2001 From: philbucher Date: Mon, 28 May 2018 13:05:14 +0200 Subject: [PATCH 14/17] FSI: Adapting Emulator Test --- .../FSIapplication/tests/FSI_problem_emulator_test.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/applications/FSIapplication/tests/FSI_problem_emulator_test.py b/applications/FSIapplication/tests/FSI_problem_emulator_test.py index b1a5278ba214..dfc192f1cc0a 100644 --- a/applications/FSIapplication/tests/FSI_problem_emulator_test.py +++ b/applications/FSIapplication/tests/FSI_problem_emulator_test.py @@ -74,6 +74,8 @@ def RunTestCase(self): "parallel_type" : "OpenMP" }, "solver_settings" : { + "model_part_name" : "Structure", + "domain_size" : 2, "solver_type" : "Dynamic", "echo_level" : 0, "analysis_type" : "linear", @@ -107,17 +109,20 @@ def RunTestCase(self): with WorkFolderScope(self.work_folder): - self.structure_main_model_part = ModelPart("Structure") - self.structure_main_model_part.ProcessInfo.SetValue(DOMAIN_SIZE, 2) + self.model = Model() # Construct the structure solver import python_solvers_wrapper_structural - self.structure_solver = python_solvers_wrapper_structural.CreateSolver(self.structure_main_model_part, StructureSolverSettings) + self.structure_solver = python_solvers_wrapper_structural.CreateSolver(self.model, StructureSolverSettings) self.structure_solver.AddVariables() self.structure_solver.ImportModelPart() + self.structure_solver.PrepareModelPart() + + self.structure_main_model_part = self.model["Structure"] + self.structure_solver.AddDofs() self.SetStructureBoundaryConditions() From dd8a542d463451792a26966d9fb68a22f0f0c6a9 Mon Sep 17 00:00:00 2001 From: philbucher Date: Mon, 28 May 2018 13:10:38 +0200 Subject: [PATCH 15/17] FSI: Adapting Solvers --- .../python_scripts/partitioned_fsi_base_solver.py | 13 ++++++++++++- .../trilinos_partitioned_fsi_base_solver.py | 12 +++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/applications/FSIapplication/python_scripts/partitioned_fsi_base_solver.py b/applications/FSIapplication/python_scripts/partitioned_fsi_base_solver.py index 82b3f0283c90..7f1972cf8535 100755 --- a/applications/FSIapplication/python_scripts/partitioned_fsi_base_solver.py +++ b/applications/FSIapplication/python_scripts/partitioned_fsi_base_solver.py @@ -76,7 +76,17 @@ def __init__(self, structure_main_model_part, fluid_main_model_part, project_par coupling_utility_parameters = self.settings["coupling_solver_settings"]["coupling_strategy"] # Construct the structure solver - self.structure_solver = python_solvers_wrapper_structural.CreateSolver(self.structure_main_model_part, + self.structure_model = KratosMultiphysics.Model() + structural_solver_settings = project_parameters["structure_solver_settings"]["solver_settings"] + if not structural_solver_settings.Has("domain_size"): + KratosMultiphysics.Logger.PrintInfo("PartitionedFSIBaseSolver", "Using the old way to pass the domain_size, this will be removed!") + structural_solver_settings.AddEmptyValue("domain_size") + structural_solver_settings["domain_size"].SetInt(project_parameters["structure_solver_settings"]["problem_data"]["domain_size"].GetInt()) + if not structural_solver_settings.Has("model_part_name"): + KratosMultiphysics.Logger.PrintInfo("PartitionedFSIBaseSolver", "Using the old way to pass the model_part_name, this will be removed!") + structural_solver_settings.AddEmptyValue("model_part_name") + structural_solver_settings["model_part_name"].SetString(project_parameters["structure_solver_settings"]["problem_data"]["model_part_name"].GetString()) + self.structure_solver = python_solvers_wrapper_structural.CreateSolver(self.structure_model, project_parameters["structure_solver_settings"]) print("* Structure solver constructed.") @@ -134,6 +144,7 @@ def AddVariables(self): def ImportModelPart(self): # Import structure model part self.structure_solver.ImportModelPart() + self.structure_solver.PrepareModelPart() # Import fluid model part self.fluid_solver.ImportModelPart() diff --git a/applications/FSIapplication/python_scripts/trilinos_partitioned_fsi_base_solver.py b/applications/FSIapplication/python_scripts/trilinos_partitioned_fsi_base_solver.py index 20a984b80b74..a4f2af3e0bc5 100644 --- a/applications/FSIapplication/python_scripts/trilinos_partitioned_fsi_base_solver.py +++ b/applications/FSIapplication/python_scripts/trilinos_partitioned_fsi_base_solver.py @@ -84,7 +84,17 @@ def __init__(self, structure_main_model_part, fluid_main_model_part, project_par self.structure_interface_submodelpart_name = self.settings["coupling_solver_settings"]["structure_interfaces_list"][0].GetString() # Construct the structure solver - self.structure_solver = python_solvers_wrapper_structural.CreateSolver(self.structure_main_model_part, + self.structure_model = KratosMultiphysics.Model() + structural_solver_settings = project_parameters["structure_solver_settings"]["solver_settings"] + if not structural_solver_settings.Has("domain_size"): + KratosMultiphysics.Logger.PrintInfo("PartitionedFSIBaseSolver", "Using the old way to pass the domain_size, this will be removed!") + structural_solver_settings.AddEmptyValue("domain_size") + structural_solver_settings["domain_size"].SetInt(project_parameters["structure_solver_settings"]["problem_data"]["domain_size"].GetInt()) + if not structural_solver_settings.Has("model_part_name"): + KratosMultiphysics.Logger.PrintInfo("PartitionedFSIBaseSolver", "Using the old way to pass the model_part_name, this will be removed!") + structural_solver_settings.AddEmptyValue("model_part_name") + structural_solver_settings["model_part_name"].SetString(project_parameters["structure_solver_settings"]["problem_data"]["model_part_name"].GetString()) + self.structure_solver = python_solvers_wrapper_structural.CreateSolver(self.structure_model, project_parameters["structure_solver_settings"]) if (KratosMPI.mpi.rank == 0) : print("* Structure solver constructed.") From 44675e58567ff3c50c876791ccf41be59af165f8 Mon Sep 17 00:00:00 2001 From: philbucher Date: Mon, 28 May 2018 13:19:12 +0200 Subject: [PATCH 16/17] Contact: Adapting Analysis and solver_wrapper --- .../contact_structural_mechanics_analysis.py | 17 ++--------------- ...python_solvers_wrapper_contact_structural.py | 8 ++++---- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/applications/ContactStructuralMechanicsApplication/python_scripts/contact_structural_mechanics_analysis.py b/applications/ContactStructuralMechanicsApplication/python_scripts/contact_structural_mechanics_analysis.py index cb45e8bbad7c..9446e67eabf9 100644 --- a/applications/ContactStructuralMechanicsApplication/python_scripts/contact_structural_mechanics_analysis.py +++ b/applications/ContactStructuralMechanicsApplication/python_scripts/contact_structural_mechanics_analysis.py @@ -47,27 +47,14 @@ def _CreateSolver(self, external_model_part=None): if (self.echo_level == 0): KM.Logger.GetDefaultOutput().SetSeverity(KM.Logger.Severity.WARNING) - ## Structure model part definition - main_model_part_name = self.project_parameters["problem_data"]["model_part_name"].GetString() - if self.model.HasModelPart(main_model_part_name): - self.main_model_part = self.model[main_model_part_name] - self.using_external_model_part = True - else: - self.main_model_part = KratosMultiphysics.ModelPart(main_model_part_name) - self.main_model_part.ProcessInfo.SetValue(KratosMultiphysics.DOMAIN_SIZE, - self.project_parameters["problem_data"]["domain_size"].GetInt()) - self.using_external_model_part = False - ## Solver construction import python_solvers_wrapper_contact_structural - self.solver = python_solvers_wrapper_contact_structural.CreateSolver(self.main_model_part, self.project_parameters) + self.solver = python_solvers_wrapper_contact_structural.CreateSolver(self.model, self.project_parameters) ## Adds the necessary variables to the model_part only if they don't exist self.solver.AddVariables() - if not self.using_external_model_part: - ## Read the model - note that SetBufferSize is done here - self.solver.ReadModelPart() # TODO move to global instance + self.solver.ImportModelPart() # TODO move to global instance def _SetUpListOfProcesses(self): """ Set up the list of processes """ diff --git a/applications/ContactStructuralMechanicsApplication/python_scripts/python_solvers_wrapper_contact_structural.py b/applications/ContactStructuralMechanicsApplication/python_scripts/python_solvers_wrapper_contact_structural.py index 0a2facb13b54..ddbe0cd23db9 100644 --- a/applications/ContactStructuralMechanicsApplication/python_scripts/python_solvers_wrapper_contact_structural.py +++ b/applications/ContactStructuralMechanicsApplication/python_scripts/python_solvers_wrapper_contact_structural.py @@ -2,10 +2,10 @@ import KratosMultiphysics -def CreateSolver(main_model_part, custom_settings): +def CreateSolver(model, custom_settings): - if (type(main_model_part) != KratosMultiphysics.ModelPart): - raise Exception("input is expected to be provided as a Kratos ModelPart object") + if (type(model) != KratosMultiphysics.Model): + raise Exception("input is expected to be provided as a Kratos Model object") if (type(custom_settings) != KratosMultiphysics.Parameters): raise Exception("input is expected to be provided as a Kratos Parameters object") @@ -43,6 +43,6 @@ def CreateSolver(main_model_part, custom_settings): custom_settings["solver_settings"].RemoveValue("time_integration_method") # does not throw even if the value is not existing solver_module = __import__(solver_module_name) - solver = solver_module.CreateSolver(main_model_part, custom_settings["solver_settings"]) + solver = solver_module.CreateSolver(model, custom_settings["solver_settings"]) return solver From e97f752798f5499928599e912a6380304a53102c Mon Sep 17 00:00:00 2001 From: philbucher Date: Mon, 28 May 2018 13:36:53 +0200 Subject: [PATCH 17/17] Maving the time settings to solver as preparation of #2135 --- .../python_scripts/partitioned_fsi_base_solver.py | 5 +++++ .../trilinos_partitioned_fsi_base_solver.py | 9 +++++++-- .../python_scripts/structural_mechanics_analysis.py | 7 ++++++- .../python_scripts/structural_mechanics_solver.py | 7 ++----- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/applications/FSIapplication/python_scripts/partitioned_fsi_base_solver.py b/applications/FSIapplication/python_scripts/partitioned_fsi_base_solver.py index 7f1972cf8535..fdf8e3e0960e 100755 --- a/applications/FSIapplication/python_scripts/partitioned_fsi_base_solver.py +++ b/applications/FSIapplication/python_scripts/partitioned_fsi_base_solver.py @@ -78,6 +78,11 @@ def __init__(self, structure_main_model_part, fluid_main_model_part, project_par # Construct the structure solver self.structure_model = KratosMultiphysics.Model() structural_solver_settings = project_parameters["structure_solver_settings"]["solver_settings"] + if not structural_solver_settings.Has("time_stepping"): + KratosMultiphysics.Logger.PrintInfo("PartitionedFSIBaseSolver", "Using the old way to pass the time_step, this will be removed!") + time_stepping_params = KratosMultiphysics.Parameters("{}") + time_stepping_params.AddValue("time_step", project_parameters["structure_solver_settings"]["problem_data"]["time_step"]) + structural_solver_settings.AddValue("time_stepping", time_stepping_params) if not structural_solver_settings.Has("domain_size"): KratosMultiphysics.Logger.PrintInfo("PartitionedFSIBaseSolver", "Using the old way to pass the domain_size, this will be removed!") structural_solver_settings.AddEmptyValue("domain_size") diff --git a/applications/FSIapplication/python_scripts/trilinos_partitioned_fsi_base_solver.py b/applications/FSIapplication/python_scripts/trilinos_partitioned_fsi_base_solver.py index a4f2af3e0bc5..16c86a1bbcaf 100644 --- a/applications/FSIapplication/python_scripts/trilinos_partitioned_fsi_base_solver.py +++ b/applications/FSIapplication/python_scripts/trilinos_partitioned_fsi_base_solver.py @@ -86,12 +86,17 @@ def __init__(self, structure_main_model_part, fluid_main_model_part, project_par # Construct the structure solver self.structure_model = KratosMultiphysics.Model() structural_solver_settings = project_parameters["structure_solver_settings"]["solver_settings"] + if not structural_solver_settings.Has("time_stepping"): + KratosMultiphysics.Logger.PrintInfo("TrilinosPartitionedFSIBaseSolver", "Using the old way to pass the time_step, this will be removed!") + time_stepping_params = KratosMultiphysics.Parameters("{}") + time_stepping_params.AddValue("time_step", project_parameters["problem_data"]["time_step"]) + solver_settings.AddValue("time_stepping", time_stepping_params) if not structural_solver_settings.Has("domain_size"): - KratosMultiphysics.Logger.PrintInfo("PartitionedFSIBaseSolver", "Using the old way to pass the domain_size, this will be removed!") + KratosMultiphysics.Logger.PrintInfo("TrilinosPartitionedFSIBaseSolver", "Using the old way to pass the domain_size, this will be removed!") structural_solver_settings.AddEmptyValue("domain_size") structural_solver_settings["domain_size"].SetInt(project_parameters["structure_solver_settings"]["problem_data"]["domain_size"].GetInt()) if not structural_solver_settings.Has("model_part_name"): - KratosMultiphysics.Logger.PrintInfo("PartitionedFSIBaseSolver", "Using the old way to pass the model_part_name, this will be removed!") + KratosMultiphysics.Logger.PrintInfo("TrilinosPartitionedFSIBaseSolver", "Using the old way to pass the model_part_name, this will be removed!") structural_solver_settings.AddEmptyValue("model_part_name") structural_solver_settings["model_part_name"].SetString(project_parameters["structure_solver_settings"]["problem_data"]["model_part_name"].GetString()) self.structure_solver = python_solvers_wrapper_structural.CreateSolver(self.structure_model, diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py index 26434fe6a4a1..1b0ecfc3d6ba 100644 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py @@ -32,6 +32,12 @@ def __init__(self, model, project_parameters): super(StructuralMechanicsAnalysis, self).__init__(model, project_parameters) solver_settings = project_parameters["solver_settings"] + if not solver_settings.Has("time_stepping"): + KratosMultiphysics.Logger.PrintInfo("StructuralMechanicsAnalysis", "Using the old way to pass the time_step, this will be removed!") + time_stepping_params = KratosMultiphysics.Parameters("{}") + time_stepping_params.AddValue("time_step", project_parameters["problem_data"]["time_step"]) + solver_settings.AddValue("time_stepping", time_stepping_params) + if not solver_settings.Has("domain_size"): KratosMultiphysics.Logger.PrintInfo("StructuralMechanicsAnalysis", "Using the old way to pass the domain_size, this will be removed!") solver_settings.AddEmptyValue("domain_size") @@ -173,7 +179,6 @@ def _ExecuteBeforeSolutionLoop(self): f.close() ## Stepping and time settings - self.solver.SetDeltaTime(self.project_parameters["problem_data"]["time_step"].GetDouble()) start_time = self.project_parameters["problem_data"]["start_time"].GetDouble() self.end_time = self.project_parameters["problem_data"]["end_time"].GetDouble() diff --git a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py index 7945176df476..307611c07dad 100755 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_solver.py @@ -65,6 +65,7 @@ def __init__(self, model, custom_settings): "material_import_settings" :{ "materials_filename": "" }, + "time_stepping" : { }, "rotation_dofs": false, "pressure_dofs": false, "reform_dofs_at_each_step": false, @@ -262,11 +263,7 @@ def AdvanceInTime(self, current_time): return new_time def ComputeDeltaTime(self): - return self.delta_time - - def SetDeltaTime(self, dt): - # This is a TEMPORARY function until the solver can compute dt! - self.delta_time = dt + return self.settings["time_stepping"]["time_step"].GetDouble() def GetComputingModelPart(self): return self.main_model_part.GetSubModelPart(self.settings["computing_model_part_name"].GetString())