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.StructuralApplication import * structure_model_part = ModelPart("StructurePart") structure_model_part.SetBufferSize(1)
Reading the ModelPart
Instead of manually adding solution step variables, we will ask the solver to add all variables it requires.
import structural_solver_static structural_solver_static.AddVariables(structure_model_part)
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
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 structural_solver_static.AddDofs(structure_model_part)
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 solver = structural_solver_static.StaticStructuralSolver(structure_model_part,domain_size)
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. Once the choice of parameters is made, the solver object can be initalized:
# Initialize the solver (using our custom parameters) solver.Initialize()
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