Python Script Tutorial: Using Kratos Solvers

From KratosWiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "In this tutorial, we will solve a structural problem using the example input file from Python Script Tutorial: Reading ModelPart From Input File Starting We provide an init...")
 
Line 5: Line 5:
 
We provide an inital script using the concepts introduced in the previous sections of this tutorial
 
We provide an inital script using the concepts introduced in the previous sections of this tutorial
  
from KratosMultiphysics import *
+
from KratosMultiphysics import *
from KratosMultiphysics.StructuralApplication import *
+
from KratosMultiphysics.StructuralApplication import *
 +
 +
structure_model_part = ModelPart("StructurePart")
 +
structure_model_part.SetBufferSize(1)
  
structure_model_part = ModelPart("StructurePart")
+
Reading the ModelPart
structure_model_part.SetBufferSize(1)
+
  
 +
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)
  
structure_model_part.AddNodalSolutionStepVariable(DISPLACEMENT)
+
The code for the solver we are going to use can be seen [http://kratos.cimne.upc.es/projects/kratos/repository/entry/kratos/applications/structural_application/python_scripts/structural_solver_static.py here].
structure_model_part.AddNodalSolutionStepVariable(FORCE)
+
  
model_part_io_structure = ModelPartIO("example")
+
Now we continue as usual, reading the model part file and initializing GiD output
  
model_part_io_structure.ReadModelPart(structure_model_part)
+
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)
  
# Wiki, ch 3
+
gid_io.InitializeMesh( 0.0 )
 +
gid_io.WriteMesh( structure_model_part.GetMesh() ) # I CHANGED THIS!
 +
gid_io.FinalizeMesh()
  
#Creating GidIO
+
As we are going to solve a structural problem, we also need to define the constitutive law for the structure material.
gid_mode = GiDPostMode.GiD_PostBinary    # or GiDPostMode.GiD_PostAscii
+
Remembering that all elements in our '''example.mdpa''' file had Property 1 assigned, we add a constitutive law to Property 1:
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 )
+
structure_model_part.Properties[1].SetValue(CONSTITUTIVE_LAW, Isotropic2D())
gid_io.WriteMesh( structure_model_part.GetMesh() ) # I CHANGED THIS!
+
gid_io.FinalizeMesh()
+
  
for node in structure_model_part.Nodes:
+
Now we let the solver to define the required degrees of freedom in the system. It will create DISPLACEMENT Dofs on all nodes
  node.SetSolutionStepValue(TEMPERATURE,0,100.0)
+
 
 +
# 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]]

Revision as of 19:08, 9 May 2012

In this tutorial, we will solve a structural problem using the example input file from Python Script Tutorial: Reading ModelPart From Input File

Starting

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() ) # I CHANGED THIS!
gid_io.FinalizeMesh()

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:

structure_model_part.Properties[1].SetValue(CONSTITUTIVE_LAW, Isotropic2D())

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

Personal tools
Categories