Python Script Tutorial: Reading ModelPart From Input File

From KratosWiki
Revision as of 18:03, 9 May 2012 by Jcotela (Talk | contribs)
Jump to: navigation, search

The ModelPart represents an arbitrary part of the model to be simulated and stores the mesh and additional data for it. Most of the Kratos routines take a ModelPart as their argument. So always is necessary to create and fill a ModelPart. In this tutorial we will describe how to create and fill a model part from given input file.



First of all we need to create a python file with following code to import the Kratos:

from KratosMultiphysics import *
from KratosMultiphysics.StructuralApplication import *

Creating a ModelPart

To create a ModelPart, one has to call its constructor passing the ModelPart`s name as its argument:

structure_model_part = ModelPart("StructurePart")

You can print the fluid_model_part:

>>> print structure_model_part
StructurePart model part
    Buffer Size : 1
    Number of tables : 0
    Current solution step index : 0

    Mesh 0 : 
    Number of Nodes      : 0
    Number of Properties : 0
    Number of Elements   : 0
    Number of Conditions : 0

It can be seen that the ModelPart is empty and has the buffer size equal to 1. This means that no history of the nodal solution steps variables will be saved. If we need to store the values of the nodal solution step variables in previous time steps, we must modify the buffer size before defining the historical variables:


This would store the values for two previous steps in addition to the current ones.

The next step is to define the variables we want to store as historical variables in nodes of this ModelPart as follow:


Reading ModelPart File

The input file of the Kratos has .mdpa (stand for ModelPart) and contains the properties, nodes, elements, conditions and initial values. A convenient way to create this file is to use the interface prepared for GiD pre and post processor. Here you can find more information about the input file. Here we assume that the example.mdpa input file is already created as follow:


Begin ModelPartData
//  nothing here
End ModelPartData

 Begin Properties  1
 DENSITY 2700.000000
 YOUNG_MODULUS 7000000.000000
 BODY_FORCE [3] (0.000000,0.000000,0.000000)
 THICKNESS 1.000000
 End Properties

Begin Nodes
       1        0.0        0.0         0.0                               //node number, coord x, cord y, coord z
       2        1.0        0.0         0.0                               //node number, coord x, cord y, coord z
       3        1.0        1.0         0.0                               //node number, coord x, cord y, coord z
       4        0.0        1.0         0.0                               //node number, coord x, cord y, coord z
End Nodes

Begin Elements TotalLagrangian2D3N
  1 1 1 2 4  //the first column is the property
  2 1 3 4 2
End Elements

Begin NodalData DISPLACEMENT_X          //be careful, variables are case sensitive!
1 1 100.0                // pos1 is the node, pos2 (a 1) means that the DOF is fixed, then (position 3) we write the fixed displacement (in this case, temperature)  
End NodalData

Begin NodalData DISPLACEMENT_Y          
1 1 100.0              
End NodalData

Begin NodalData FORCE_Y             
3    0    5.0             //fixing it or not does not change anything since it is not a degree of freedom, it's just info that will be used by the condition  
End NodalData

Begin Conditions PointForce2D
1 1 3     //pos1:condition ID(irrelevant) ; pos2:cond Property ( = 1 in this case) ; pos3:node to apply the condition.              
End Conditions

which represents the following 2D problem:


For reading the .mdpa file first we have to create a ModelPartIO object passing the input file path/name to its constructor:

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

Warn_icon.gif Note:

the file name is used without the .mdpa extension!

Then use this io object to read the file and store the mesh and data in ModelPart:


And printing again the ModelPart:

 >>> print structure_model_part
 StructurePart model part
     Buffer Size : 1
     Number of tables : 0
     Current solution step index : 0
     Mesh 0 : 
     Number of Nodes      : 4
     Number of Properties : 1
     Number of Elements   : 2
     Number of Conditions : 1

Setting the Buffer Size

Next Tutorial : Python Script Tutorial: Writing Output File

Previous Tutorial : Python Script Tutorial: Hello Kratos

All Tutorials : Kratos Tutorials

Personal tools