From 73e6c64afbf5c07e027fd558e6e3c25a2f0d59aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Mon, 28 May 2018 12:57:22 +0200 Subject: [PATCH] Compatibilty problems (WIP) --- .../python_scripts/fluid_dynamics_analysis.py | 14 +++-- .../navier_stokes_base_solver.py | 56 ++++++++++++++++--- .../navier_stokes_solver_vmsmonolithic.py | 34 +++++++---- .../python_solvers_wrapper_fluid.py | 10 ++-- .../structural_mechanics_analysis.py | 3 +- 5 files changed, 87 insertions(+), 30 deletions(-) diff --git a/applications/FluidDynamicsApplication/python_scripts/fluid_dynamics_analysis.py b/applications/FluidDynamicsApplication/python_scripts/fluid_dynamics_analysis.py index f132ffa208df..c5d47be34193 100644 --- a/applications/FluidDynamicsApplication/python_scripts/fluid_dynamics_analysis.py +++ b/applications/FluidDynamicsApplication/python_scripts/fluid_dynamics_analysis.py @@ -15,10 +15,14 @@ class FluidDynamicsAnalysis(AnalysisStage): def __init__(self,model,parameters): # Create the ModelPart # Note that this in temporary and will be done through the model in the future - model_part_name = parameters["problem_data"]["model_part_name"].GetString() - self.main_model_part = Kratos.ModelPart(model_part_name) - self.main_model_part.ProcessInfo.SetValue(KratosMultiphysics.DOMAIN_SIZE, - parameters["problem_data"]["domain_size"].GetInt()) + main_model_part_name = parameters["problem_data"]["model_part_name"].GetString() + if model.HasModelPart(main_model_part_name): + self.main_model_part = model[main_model_part_name] + else: + self.main_model_part = KratosMultiphysics.ModelPart(main_model_part_name) + self.main_model_part.ProcessInfo.SetValue(KratosMultiphysics.DOMAIN_SIZE, + parameters["problem_data"]["domain_size"].GetInt()) + model.AddModelPart(self.main_model_part) super(FluidDynamicsAnalysis,self).__init__(model,parameters) @@ -29,7 +33,7 @@ def __init__(self,model,parameters): def _CreateSolver(self): import python_solvers_wrapper_fluid - return python_solvers_wrapper_fluid.CreateSolver(self.main_model_part, self.project_parameters) + return python_solvers_wrapper_fluid.CreateSolver(self.model, self.project_parameters) def Initialize(self): # This function is temporary until restart saving is handled through process diff --git a/applications/FluidDynamicsApplication/python_scripts/navier_stokes_base_solver.py b/applications/FluidDynamicsApplication/python_scripts/navier_stokes_base_solver.py index d3489571c142..c7082a16f7ed 100755 --- a/applications/FluidDynamicsApplication/python_scripts/navier_stokes_base_solver.py +++ b/applications/FluidDynamicsApplication/python_scripts/navier_stokes_base_solver.py @@ -10,12 +10,16 @@ # Import applications import KratosMultiphysics.FluidDynamicsApplication as KratosCFD -def CreateSolver(main_model_part, custom_settings): - return NavierStokesBaseSolver(main_model_part, custom_settings) +# Importing the base class +from python_solver import PythonSolver -class NavierStokesBaseSolver(object): +def CreateSolver(model, custom_settings): + return NavierStokesBaseSolver(model, custom_settings) - def __init__(self, main_model_part, custom_settings): +class NavierStokesBaseSolver(PythonSolver): + + def __init__(self, model, custom_settings): + super(NavierStokesBaseSolver, self).__init__(model, custom_settings) ## Set the element and condition names for the replace settings self.element_name = None self.condition_name = None @@ -39,7 +43,7 @@ def ImportModelPart(self): self._set_buffer_size() if self._IsPrintingRank(): - KratosMultiphysics.Logger.PrintInfo("NavierStokesBaseSolver", "Model reading finished.") + KratosMultiphysics.Logger.PrintInfo("::[NavierStokesBaseSolver]:: ", "Model reading finished.") def ExportModelPart(self): ## Model part writing @@ -47,7 +51,7 @@ def ExportModelPart(self): KratosMultiphysics.ModelPartIO(name_out_file, KratosMultiphysics.IO.WRITE).WriteModelPart(self.main_model_part) if self._IsPrintingRank(): - KratosMultiphysics.Logger.PrintInfo("NavierStokesBaseSolver", "Model export finished.") + KratosMultiphysics.Logger.PrintInfo("::[NavierStokesBaseSolver]:: ", "Model export finished.") def AddDofs(self): KratosMultiphysics.VariableUtils().AddDof(KratosMultiphysics.VELOCITY_X, KratosMultiphysics.REACTION_X,self.main_model_part) @@ -56,7 +60,27 @@ def AddDofs(self): KratosMultiphysics.VariableUtils().AddDof(KratosMultiphysics.PRESSURE, KratosMultiphysics.REACTION_WATER_PRESSURE,self.main_model_part) if self._IsPrintingRank(): - KratosMultiphysics.Logger.PrintInfo("NavierStokesBaseSolver", "Fluid solver DOFs added correctly.") + KratosMultiphysics.Logger.PrintInfo("::[NavierStokesBaseSolver]:: ", "Fluid solver DOFs added correctly.") + + 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(): + ## Replace default elements and conditions + self._replace_elements_and_conditions() + ## Executes the check and prepare model process + self._execute_check_and_prepare() + ## Set buffer size + self._set_buffer_size() + + # 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("::[NavierStokesBaseSolver]:: ", "ModelPart prepared for Solver.") def AdaptMesh(self): pass @@ -118,7 +142,7 @@ def SolveSolutionStep(self): if not is_converged and self._IsPrintingRank(): msg = "Navier-Stokes solver did not converge for iteration " + str(self.main_model_part.ProcessInfo[KratosMultiphysics.STEP]) + "\n" msg += "corresponding to time " + str(self.main_model_part.ProcessInfo[KratosMultiphysics.TIME]) + "\n" - KratosMultiphysics.Logger.PrintWarning("NavierStokesBaseSolver",msg) + KratosMultiphysics.Logger.PrintWarning("::[NavierStokesBaseSolver]:: ",msg) def FinalizeSolutionStep(self): if self._TimeBufferIsInitialized(): @@ -130,6 +154,21 @@ def Solve(self): self.SolveSolutionStep() self.FinalizeSolutionStep() + def is_restarted(self): + # this function avoids the long call to ProcessInfo and is also safer + # in case the detection of a restart is changed later + return self.main_model_part.ProcessInfo[KratosMultiphysics.IS_RESTARTED] + + def print_on_rank_zero(self, *args): + # This function will be overridden in the trilinos-solvers + KratosMultiphysics.Logger.PrintInfo(" ".join(map(str,args))) + + def print_warning_on_rank_zero(self, *args): + # This function will be overridden in the trilinos-solvers + KratosMultiphysics.Logger.PrintWarning(" ".join(map(str,args))) + + #### Private functions #### + def _model_part_reading(self): ## Model part reading if(self.settings["model_import_settings"]["input_type"].GetString() == "mdpa"): @@ -151,7 +190,6 @@ def _execute_check_and_prepare(self): import check_and_prepare_model_process_fluid check_and_prepare_model_process_fluid.CheckAndPrepareModelProcess(self.main_model_part, prepare_model_part_settings).Execute() - def _set_buffer_size(self): current_buffer_size = self.main_model_part.GetBufferSize() if self.min_buffer_size > current_buffer_size: diff --git a/applications/FluidDynamicsApplication/python_scripts/navier_stokes_solver_vmsmonolithic.py b/applications/FluidDynamicsApplication/python_scripts/navier_stokes_solver_vmsmonolithic.py index 700e608fd9f6..c3b4b39074e4 100755 --- a/applications/FluidDynamicsApplication/python_scripts/navier_stokes_solver_vmsmonolithic.py +++ b/applications/FluidDynamicsApplication/python_scripts/navier_stokes_solver_vmsmonolithic.py @@ -101,22 +101,21 @@ def _SetUpFIC(self,settings): self.process_data[KratosCFD.FIC_BETA] = settings["beta"].GetDouble() -def CreateSolver(main_model_part, custom_settings): - return NavierStokesSolverMonolithic(main_model_part, custom_settings) +def CreateSolver(model, custom_settings): + return NavierStokesSolverMonolithic(model, custom_settings) class NavierStokesSolverMonolithic(navier_stokes_base_solver.NavierStokesBaseSolver): - def __init__(self, main_model_part, custom_settings): - + def __init__(self, model, custom_settings): + super(NavierStokesSolverMonolithic, self).__init__(model, custom_settings) + # There is only a single rank in OpenMP, we always print self._is_printing_rank = True - #TODO: shall obtain the compute_model_part from the MODEL once the object is implemented - self.main_model_part = main_model_part - ##settings string in json format default_settings = KratosMultiphysics.Parameters(""" { + "model_part_name" : "PLEASE_SPECIFY_NAME", "solver_type": "navier_stokes_solver_vmsmonolithic", "model_import_settings": { "input_type": "mdpa", @@ -181,6 +180,20 @@ def __init__(self, main_model_part, custom_settings): self.settings = custom_settings 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) + + # Set if the analysis is restarted + 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 + self.stabilization = StabilizedFormulation(self.settings["stabilization"]) self.element_name = self.stabilization.element_name self.condition_name = self.stabilization.condition_name @@ -200,8 +213,7 @@ def __init__(self, main_model_part, custom_settings): import linear_solver_factory self.linear_solver = linear_solver_factory.ConstructSolver(self.settings["linear_solver_settings"]) - KratosMultiphysics.Logger.PrintInfo("NavierStokesSolverMonolithic", "Construction of NavierStokesSolverMonolithic finished.") - + self.print_on_rank_zero("::[NavierStokesSolverMonolithic]:: ", "Construction of NavierStokesSolverMonolithic finished.") def AddVariables(self): ## Add base class variables @@ -227,7 +239,7 @@ def AddVariables(self): if self.settings["consider_periodic_conditions"].GetBool() == True: self.main_model_part.AddNodalSolutionStepVariable(KratosCFD.PATCH_INDEX) - KratosMultiphysics.Logger.PrintInfo("NavierStokesSolverMonolithic", "Fluid solver variables added correctly.") + self.print_on_rank_zero("::[NavierStokesSolverMonolithic]:: ", "Fluid solver variables added correctly.") def ImportModelPart(self): @@ -296,7 +308,7 @@ def Initialize(self): self.solver.Check() - KratosMultiphysics.Logger.PrintInfo("NavierStokesSolverMonolithic", "Solver initialization finished.") + self.print_on_rank_zero("::[NavierStokesSolverMonolithic]:: ", "Solver initialization finished.") def _set_physical_properties(self): diff --git a/applications/FluidDynamicsApplication/python_scripts/python_solvers_wrapper_fluid.py b/applications/FluidDynamicsApplication/python_scripts/python_solvers_wrapper_fluid.py index 500b2b5a4793..aedb17f4359b 100644 --- a/applications/FluidDynamicsApplication/python_scripts/python_solvers_wrapper_fluid.py +++ b/applications/FluidDynamicsApplication/python_scripts/python_solvers_wrapper_fluid.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") @@ -13,6 +13,8 @@ def CreateSolver(main_model_part, custom_settings): parallelism = custom_settings["problem_data"]["parallel_type"].GetString() solver_type = custom_settings["solver_settings"]["solver_type"].GetString() + custom_settings["solver_settings"].AddValue("model_part_name", custom_settings["problem_data"]["model_part_name"]) + # Solvers for OpenMP parallelism if (parallelism == "OpenMP"): if (solver_type == "Monolithic"): @@ -53,6 +55,6 @@ def CreateSolver(main_model_part, custom_settings): raise Exception("parallelism is neither OpenMP nor MPI") 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 426b72004fc7..8d422c7edb81 100644 --- a/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py +++ b/applications/StructuralMechanicsApplication/python_scripts/structural_mechanics_analysis.py @@ -43,6 +43,7 @@ def __init__(self, model, project_parameters): self.main_model_part = KratosMultiphysics.ModelPart(main_model_part_name) self.main_model_part.ProcessInfo.SetValue(KratosMultiphysics.DOMAIN_SIZE, project_parameters["problem_data"]["domain_size"].GetInt()) + model.AddModelPart(self.main_model_part) super(StructuralMechanicsAnalysis, self).__init__(model, project_parameters) @@ -64,7 +65,7 @@ def _CreateSolver(self): """ Create the Solver (and create and import the ModelPart if it is not alread in the model) """ ## Solver construction import python_solvers_wrapper_structural - return python_solvers_wrapper_structural.CreateSolver(self.main_model_part, self.project_parameters) + return python_solvers_wrapper_structural.CreateSolver(self.model, self.project_parameters) def _CreateProcesses(self, parameter_name, initialization_order): """Create a list of Processes