Skip to content

Commit

Permalink
Compatibilty problems (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
loumalouomega committed May 28, 2018
1 parent 45bd4eb commit 73e6c64
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,15 +43,15 @@ 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
name_out_file = self.settings["model_import_settings"]["input_filename"].GetString()+".out"
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)
Expand All @@ -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
Expand Down Expand Up @@ -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():
Expand All @@ -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"):
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

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")

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"):
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit 73e6c64

Please sign in to comment.