Python Script Tutorial: Writing Output File

From KratosWiki
Jump to: navigation, search

In this tutorial the procedure for writing the mesh and data from a ModelPart to output files will be described briefly. More information can be found here



First of all we need to create a python file with following code to import the Kratos, create a ModelPart and read it from input as described in the previous tutorial :

from KratosMultiphysics import *
from KratosMultiphysics.StructuralApplication import *

structure_model_part = ModelPart("StructurePart")

model_part_io_structure = ModelPartIO("path/to/file/example")

Creating GidIO

The GidIO is in charge of writing files in GiD format. So we need to create an instance of this class for writing output files for GiD. The constructor of the GidIO takes following parameters:

  • GiD_PostMode determines whether the output should be written in plain text (GiD_PostAscii) or binary (Gid_PostBinary) file format. The ASCII format is only recommended for debugging, where the user needs to manually access and read the output data. In general, the binary format should be preferred as it yields much smaller output files and can be read and written much faster.
  • MultiFileFlag states whether GidIO should write all results in one file or use different files. If the flag is set to SingleFile, all meshes are written into one file. Note that this is only possible in Binary mode. If instead the flag is set to MultipleFiles, a new file (or, in ASCII mode set of mesh and result files) is generated for each step. The names for the files are chosen automatically according to the number given in InitializeMesh.
  • WriteDeformedMeshFlag specifies whether the meshes are written in deformed or undeformed configuration. WriteDeformedMesh will cause the meshes to be written in deformed state and WriteUndeformedMesh causes the meshes to be written in their original configuration respectively.
  • WriteConditionsFlag is used to switch between the option to write only the elements of each mesh (option WriteElementsOnly) or to write additionally the Condition objects in the current model part (option WriteConditions)

To create a GidIO we can call the constructor passing above arguments as follow:

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

Writing Mesh

For writing a mesh first we have to initialize it passing a double number as mesh number to the InitializeMesh method:

gid_io.InitializeMesh( 0.0 )

After initializing we can write the Mesh stored in ModelPart to the IO using WriteMesh( mesh ) method:

gid_io.WriteMesh( structure_model_part.GetMesh() ) 

It is necessary to call FinalizeMesh(). So the entire process is as follow:

gid_io.InitializeMesh( 0.0 )
gid_io.WriteMesh( structure_model_part.GetMesh() ) 

Writing Nodal Data

Similar to writing the mesh, before start writing the results we have to initialize a result using InitializeResults method:

gid_io.InitializeResults( mesh_number, mesh )

where mesh_number is the same number given to InitializeMesh method. here is an example:

gid_io.InitializeResults( 0.0, structure_model_part.GetMesh() )

Now we can write the nodal displacement and temperature as follow:

gid_io.WriteNodalResults( DISPLACEMENT, structure_model_part.Nodes, time, 0 )
gid_io.WriteNodalResults( TEMPERATURE, structure_model_part.Nodes, time, 0 )

It can be seen that the second argument is nodes stored in the model_part. The third one is the time step and the forth one is the history step. It is important to mention that this function writes only the historical data and uses the last argument to reach the solution step asked for in the history. Note that the function is overloaded for different type of results. In this way the displacement vector and temperature is written in the same way.

After writing all the results we have to close the results by calling the FinalizeResults method:


Writing Elemental Data

For writing the elemental data we can use the PrintOnGaussPoints method after initializing the results as follow:

 gid_io.PrintOnGaussPoints( PK2_STRESS_TENSOR, structure_model_part, time)

we can also combine the nodal and elemental writing:

     gid_io.InitializeResults( time, structure_model_part.GetMesh() )
     gid_io.WriteNodalResults( DISPLACEMENT, structure_model_part.Nodes, time, 0 )
     gid_io.WriteNodalResults( TEMPERATURE, structure_model_part.Nodes, time, 0 )
     gid_io.PrintOnGaussPoints( PK2_STRESS_TENSOR, structure_model_part, time)

Next Tutorial : Python Script Tutorial: ModelPart Nodes and Nodal Data

Previous Tutorial : Python Script Tutorial: Reading ModelPart From Input File

All Tutorials : Kratos Tutorials

Personal tools