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, MeshGroupsFlag, WriteDeformedMeshFlag, WriteConditionsFlag ) where the names of each input file and the output file may be given separately, and
- GidIO( Filename, GiD_PostMode, MeshGroupsFlag, WriteDeformedMeshFlag, WriteConditionsFlag ), where the file names are determined automatically according to the given problem name
The following flags have to be specified in the constructor of the GiDIO instance: 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.
MeshGroupsFlag states whether GidIO should use mesh groups or not. If the flag is set to UseMeshGroups, the meshes are put into groups and labelled with a certain name. Each set of results lateron refers to a specific mesh group. If instead the flag is set to UseNoMeshGroups, the meshes are not labelled. In this case, the user needs to write either one mesh for all results or one mesh for each time step. If ASCII output is used, each different mesh needs to be stored in a separate file.
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)
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, MeshGroupsFlag.UseMeshGroups, WriteDeformedMeshFlag.WriteUndeformed, WriteConditionsFlag.WriteElementsOnly )
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(), WriteDeformedMeshFlag.WriteDeformed ) gid_io.WriteMesh( model_part.GetMesh(), WriteDeformedMeshFlag.WriteUndeformed, WriteConditionsFlag.WriteConditions ) gid_it.CloseMeshGroup()
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) gid_io.FinalizeResults( MeshGroupsFlag.UseMeshGroups )