-
Notifications
You must be signed in to change notification settings - Fork 247
Tutorial: Creating the Python Solver file
Alejandro Cornejo Velázquez edited this page Jan 8, 2018
·
19 revisions
The porpouse of this file is to act as an interface between your problem's script (also in python) file and the Kratos functions. We will create a class and functions already customized for our type of problem, so that the calls required in the .py from the problem become simpler. The file can be in the /python_scripts
folder.
from __future__ import print_function, absolute_import, division #makes KratosMultiphysics backward compatible with python 2.6 and 2.7
# importing the Kratos Library
import KratosMultiphysics
import KratosMultiphysics.MyLaplacianApplication as Poisson
# Check that KratosMultiphysics was imported in the main script
KratosMultiphysics.CheckForPreviousImport()
def CreateSolver(model_part, custom_settings):
return PureDiffusionSolver(model_part, custom_settings)
class PureDiffusionSolver(object):
def __init__(self, model_part, custom_settings): # Constructor of the class
self.model_part = model_part
self.domain_size = self.model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE]
## Settings string in json format
default_settings = KratosMultiphysics.Parameters("""
{
"model_import_settings" : {
"input_type" : "mdpa",
"input_filename" : "unknown_name"
},
"echo_level" : 0,
"buffer_size" : 2,
"relative_tolerance" : 1e-6,
"absolute_tolerance" : 1e-9,
"maximum_iterations" : 20,
"compute_reactions" : false,
"reform_dofs_at_each_step" : false,
"calculate_norm_dx" : true,
"move_mesh_flag" : false,
"linear_solver_settings" : {
"solver_type" : "SkylineLUFactorizationSolver"
}
}""")
"""
## Overwrite the default settings with user-provided parameters
self.settings = custom_settings
self.settings.ValidateAndAssignDefaults(default_settings)
## Construct the linear solver
import linear_solver_factory
self.linear_solver = linear_solver_factory.ConstructSolver(self.settings["linear_solver_settings"])
"""
# Assign default parameters ADDED
self.settings = default_settings
## Construct the linear solver
import linear_solver_factory
self.linear_solver = linear_solver_factory.ConstructSolver(self.settings["linear_solver_settings"])
def AddVariables(self):
self.model_part.AddNodalSolutionStepVariable(KratosMultiphysics.TEMPERATURE);
self.model_part.AddNodalSolutionStepVariable(Poisson.POINT_HEAT_SOURCE);
def AddDofs(self):
for node in self.model_part.Nodes:
node.AddDof(KratosMultiphysics.TEMPERATURE);
print ("variables for the Poisson solver added correctly")
def GetMinimumBufferSize(self):
return 1
def ImportModelPart(self):
## Model part reading
if(self.settings["model_import_settings"]["input_type"].GetString() == "mdpa"):
## Here it would be the place to import restart data if required
KratosMultiphysics.ModelPartIO(self.settings["model_import_settings"]["input_filename"].GetString()).ReadModelPart(self.model_part)
else:
raise Exception("Other input options are not yet implemented")
## Set the buffer size
self.model_part.SetBufferSize( self.settings["buffer_size"].GetInt() )
if(self.GetMinimumBufferSize() > self.model_part.GetBufferSize() ):
self.model_part.SetBufferSize(self.GetMinimumBufferSize())
def Initialize(self):
# Creating the solution strategy
self.conv_criteria = KratosMultiphysics.DisplacementCriteria(self.settings["relative_tolerance"].GetDouble(),
self.settings["absolute_tolerance"].GetDouble())
(self.conv_criteria).SetEchoLevel(self.settings["echo_level"].GetInt())
self.time_scheme = KratosMultiphysics.ResidualBasedIncrementalUpdateStaticScheme()
builder_and_solver = KratosMultiphysics.ResidualBasedBlockBuilderAndSolver(self.linear_solver)
self.solver = KratosMultiphysics.ResidualBasedLinearStrategy(self.model_part,
self.time_scheme,
self.linear_solver,
self.settings["compute_reactions"].GetBool(),
self.settings["reform_dofs_at_each_step"].GetBool(),
self.settings["calculate_norm_dx"].GetBool(),
self.settings["move_mesh_flag"].GetBool())
(self.solver).SetEchoLevel(self.settings["echo_level"].GetInt())
(self.solver).Check()
(self.solver).Initialize()
print ("Pure diffusion solver initialization finished")
def Solve(self):
# Solve equations on mesh
(self.solver).Solve()
- Getting Kratos (Last compiled Release)
- Compiling Kratos
- Running an example from GiD
- Kratos input files and I/O
- Data management
- Solving strategies
- Manipulating solution values
- Multiphysics
- Video tutorials
- Style Guide
- Authorship of Kratos files
- Configure .gitignore
- How to configure clang-format
- How to use smart pointer in Kratos
- How to define adjoint elements and response functions
- Visibility and Exposure
- Namespaces and Static Classes
Kratos structure
Conventions
Solvers
Debugging, profiling and testing
- Compiling Kratos in debug mode
- Debugging Kratos using GDB
- Cross-debugging Kratos under Windows
- Debugging Kratos C++ under Windows
- Checking memory usage with Valgind
- Profiling Kratos with MAQAO
- Creating unitary tests
- Using ThreadSanitizer to detect OMP data race bugs
- Debugging Memory with ASAN
HOW TOs
- How to create applications
- Python Tutorials
- Kratos For Dummies (I)
- List of classes and variables accessible via python
- How to use Logger
- How to Create a New Application using cmake
- How to write a JSON configuration file
- How to Access DataBase
- How to use quaternions in Kratos
- How to do Mapping between nonmatching meshes
- How to use Clang-Tidy to automatically correct code
- How to use the Constitutive Law class
- How to use Serialization
- How to use GlobalPointerCommunicator
- How to use PointerMapCommunicator
- How to use the Geometry
- How to use processes for BCs
- How to use Parallel Utilities in futureproofing the code
- Porting to Pybind11 (LEGACY CODE)
- Porting to AMatrix
- How to use Cotire
- Applications: Python-modules
- How to run multiple cases using PyCOMPSs
- How to apply a function to a list of variables
- How to use Kratos Native sparse linear algebra
Utilities
Kratos API
Kratos Structural Mechanics API