Python Script Tutorial: Writing Output File
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") structure_model_part.AddNodalSolutionStepVariable(DISPLACEMENT) structure_model_part.AddNodalSolutionStepVariable(FORCE) model_part_io_structure = ModelPartIO("path/to/file/example") model_part_io_structure.ReadModelPart(structure_model_part)
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)
For writing a mesh first we have to initialize it passing a double number as mesh name 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() ) gid_io.FinalizeMesh()
Writing Nodal Data
Writing Elemental Data
Next Tutorial : Python Script Tutorial: ModelPart Nodes and Nodal Data
Previous Tutorial : Python Script Tutorial: Reading ModelPart From Input File
All Tutorials : Kratos Tutorials