From KratosWiki
Jump to: navigation, search


Kratos Course 2013

Before we start

Usually, the Python interpreter can be called from command line. To execute the commands in a file, lets say, we can use the instruction


Exceptionally, for this course, we are using a non-standard python installation so, for today, download the following file [1] and copy it to your problem’s folder (for example, inside cylinder.gid)

To run a python script, on a command window, go to the model’s folder and type


First, we will create a new Python file to work on. To do this, we create a copyuse the default script ( to use as a starting point:


Open on a text editor.

A first Python example

The following function defines a parabolic velocity profile, with different values depending on the Y coordinate (we are assuming that the Y coordinates vary from 0 to h)

 def get_boundary_velocity(y,u_max,h):
     return (1-y/h)*(y/h)*u_max

we can use it as an initial condition by assigning it to all nodes in the model (correct the value of h if your model has a different size)

 u_max = 0.01
 h = 2.0
 for node in fluid_model_part.Nodes:
     u_node = get_boundary_velocity(node.Y,u_max,h)

Paste the last two pieces of code into (just before the while solution loop is a fine place) and run your script.

Alternatively, we could use it as an inlet boundary condition by asigning it only to the leftmost nodes (I’m assuming here that the inlet boundary is defined by x=0)

 u_max = 0.01
 h = 2.0
 for node in fluid_model_part.Nodes:
     if node.X == 0.0:
         u_node = get_boundary_velocity(node.Y,u_max,h)

A similar technique can be used to print information about particular nodes:

 id_to_track = 50
 for node in fluid_model_part.Nodes:
     if node.Id == id_to_track:
         this_node = node

With this, we have a pointer to a node we are interested in. Later, in the solution loop, we can use it to display information of interest

 print "Pressure on node 50 is",node.GetSolutionStepValue(PRESSURE,0)

Older stuff

Required components How to refine an existing triangular or tetrahedral mesh

Kratos Structure

Personal tools