How to use the GidIO
This is a tutotial on how to use the Kratos GiD interface (GidIO).
The HowTo describes the measures to be taken in order to read input from input files using the GidIO interface and how to write results that can be post-processed in GiD.
Generating an instance of GidIO
The GidIO interface can only be used, if an instance of the GidIO class has been created. This GidIO instance knows the path of the current problem.gid directory and automatically opens the respective input and output files in the problem.gid directory.
For the instantiation of the GidIO interface there are two Constructors available:
- GidIO( NodeDatafilename, PropertiesDatafilename, ElementDatafilename, ConditionDatafilename, InitialValueDatafilename, ResultFilename, GiD_PostMode ) where the names of each input file and the output file may be given separately, and
- GidIO( Filename, GiD_PostMode ), where the file names are determined automatically according to the given problem name
The flag 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.
In the Python script of the problem to be solved with Kratos, the GidIO instance can be, for example, initialised by the following command:
gid_io = GidIO("problem_name",GiDPostMode.GiD_PostBinary)
the resulting Python object can then be used to read the input and write the output of the current problem.
Once a ModelPart object has been created, the actual content of the model can be read from the input files by means of the GidIO. The following command creates a ModelPart object and reads all the input files and automatically creates the respective model data:
model_part = ModelPart("Name") gid_io.ReadModelPart(model_part)
After this, the ModelPart is ready to use.
Writing output that can be post-processed by GiD is written by means of the gidpost library. The GidIO interface provides several functions that allow for a convenient usage of this library in order to easily generate suitable output files. The following sections describe the different features available in the GidIO interface.
For the GiD post-processor, different meshes can be grouped. This allows for the generation of heterogeneous meshes (i.e. meshes consisting of different geometries) and a flexible assignment of results to several meshes (e.g. if more than one, but not all time steps use the same mesh). In this case, the GidIO interface needs to generate so-called mesh groups.
GidIO provides two functions for writing a mesh:
- WriteNodeMesh( mesh, dimension, WriteDeformedMeshFlag ) where mesh is a Kratos::Mesh instance, dimension denotes the dimension of the node mesh and WriteDeformedMeshFlag denotes whether the mesh should be written in deformed or undeformed state
- WriteMesh( mesh, WriteDeformedMeshFlag [, WriteConditionsFlag] ) where WriteConditionsFlag denotes whether only the elements of also the conditions should be written.
If mesh groups shall be used, calling the WriteNodeMesh and/or WriteMesh functions have to be enclosed by a call of OpenMeshGroup( time ) and CloseMeshGroup in order to generate and finalise the mesh group:
gid_io.OpenMeshGroup( time )
gid_io.WriteNodeMesh( model_part.GetMesh(), 3, WriteDeformedMeshFlag.WriteDeformed )
gid_io.WriteMesh( model_part.GetMesh(), WriteDeformedMeshFlag.WriteUndeformed, WriteConditionsFlag.WriteConditions )
Note, that the WriteConditionsFlag has WriteElementsOnly as a standard value and may be omitted. In this case, the conditions are not written.
Writing nodal results
Nodal results are written by WriteNodalResults( variable, nodes, time, solution_step_index). Here, variable is a Kratos variable (e.g. DISPLACEMENT) that may be of types double, array_1d<double,3>, Vector or Matrix. nodes is the list of nodes the results shall be printed on, usually obtained from ModelPart.Nodes. time is the current solution step, whereas solution_step_index is the variables buffer index (0 for the current solution step, 1 for the previous one, etc.).
Writing results on integration points
Results on integration points are written by PrintOnGaussPoints( variable, model_part, time ), where variable is once again the Kratos variable like in WriteNodalResults. model_part is the current ModelPart instance. time denotes the current solution step.
Writing results must be enclosed by InitializeResults( time, mesh, MeshGroupsFlag, WriteConditionsFlag ) and FinalizeResults( MeshGroupsFlag ). See an example code below:
gid_io.InitializeResults( time, model_part.GetMesh(), MeshGroupsFlag.UseMeshGroups, WriteConditionsFlag.WriteConditions )
gid_io.WriteNodalResults( DISPLACEMENT, model_part.Nodes, time, 0 )
gid_io.PrintOnGaussPoints( PK2_STRESS_TENSOR, model_part, time)
self.gid_io.FinalizeResults( MeshGroupsFlag.UseMeshGroups )