Python Script Tutorial: Using Kratos Solvers
In this tutorial, we will solve a structural problem using the example input file from Python Script Tutorial: Reading ModelPart From Input File
We provide an inital script using the concepts introduced in the previous sections of this tutorial
from KratosMultiphysics import * from KratosMultiphysics.SolidMechanicsApplication import * structure_model_part = ModelPart("StructurePart")
Reading the ModelPart
Instead of manually adding solution step variables, we will ask the solver to add all variables it requires.
rotation_dof=False import solid_mechanics_main_solver solid_mechanics_main_solver.AddVariables(structure_model_part,rotation_dof)
The code for the solver we are going to use can be seen here.
Now we continue as usual, reading the model part file and initializing GiD output
structure_model_part.SetBufferSize(1) model_part_io_structure = ModelPartIO("example") model_part_io_structure.ReadModelPart(structure_model_part) #Creating GidIO gid_mode = GiDPostMode.GiD_PostBinary # or GiDPostMode.GiD_PostAscii use_multi_file = MultiFileFlag.MultipleFiles # or MultiFileFlag.SingleFile deformed_mesh_flag = WriteDeformedMeshFlag.WriteDeformed # or WriteDeformedMeshFlag.WriteUndeformed write_conditions = WriteConditionsFlag.WriteElementsOnly # or WriteConditionsFlag.WriteConditions gid_io = GidIO("test",gid_mode,use_multi_file,deformed_mesh_flag, write_conditions) gid_io.InitializeMesh( 0.0 ) gid_io.WriteMesh( structure_model_part.GetMesh() ) gid_io.FinalizeMesh()
Initializing a Solver
As we are going to solve a structural problem, we also need to define the constitutive law for the structure material. Remembering that all elements in our example.mdpa file had Property 1 assigned, we add a constitutive law to Property 1:
Now we let the solver to define the required degrees of freedom in the system. It will create DISPLACEMENT Dofs on all nodes
# Add DOFs used by the solver to model part import solid_mechanics_main_solver rotation_dof = False solid_mechanics_main_solver.AddDofs(structure_model_part,"Mechanical",rotation_dof)
At this point, we create a solver object, which will manage the solution process. Domain size is the number of spatial dimensions (2 or 3).
# Construct a solver object domain_size = 2 echo_level = 0 solver = solid_mechanics_main_solver.SolidMechanicsSolver(structure_model_part,domain_size,echo_level)
If we want to modify some solver parameters, this is the moment to do it. Each solver defines its own parameters, which can be set between constructing the solver object and calling its Initalize() method.
# modify default solver parameters here solver.ReformDofSetAtEachStep = True
In this case, we are forcing the solver to re-shape the system matrix at each time step. This would be essential if we wanted to modify the mesh connectivity during the solution process. Now we must set the type of solution that we want:
# modify default solver parameters here solver_type="StaticSolver" #other choices: DynamicSolver, RelaxedDynamicSolver problem_type="Mechanical" linesearchflag=False solver.SetProblemType(problem_type,solver_type,linesearchflag)
Once the choice of parameters is made, the solver object can be initalized:
# Initialize the solver (using our custom parameters) load_restart=False solver.Initialize(load_restart)
And the solver can be used to solve the problem:
# Use the solver to solve the problem solver.Solve()
Finally, we can print the results for our computation in a GiD post-process file
# Print results to GiD time = 0.0 gid_io.InitializeResults(time,structure_model_part.GetMesh()) gid_io.WriteNodalResults(DISPLACEMENT,structure_model_part.Nodes,time,0) gid_io.FinalizeResults()
Next Tutorial : Python Script Tutorial: Main Solution
Previous Tutorial : Python Script Tutorial: ModelPart Elements and Conditions