Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Base python solver #2068

Merged
merged 20 commits into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,22 @@ 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("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")
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.")

Expand Down Expand Up @@ -134,6 +149,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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,22 @@ 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("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("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("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,
project_parameters["structure_solver_settings"])
if (KratosMPI.mpi.rank == 0) : print("* Structure solver constructed.")

Expand Down
11 changes: 8 additions & 3 deletions applications/FSIapplication/tests/FSI_problem_emulator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ 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("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")
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()
Expand Down Expand Up @@ -64,7 +81,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()

Expand All @@ -89,29 +106,16 @@ 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.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()

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.'''
Expand All @@ -130,21 +134,11 @@ 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()

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

## 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)
Expand Down Expand Up @@ -185,15 +179,13 @@ 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()

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- ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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("""
{
Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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("""
{
Expand All @@ -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 ####
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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("""
{
Expand All @@ -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
Expand Down
Loading