How to use Python

From KratosWiki
Revision as of 18:09, 26 November 2007 by (Talk)
Jump to: navigation, search

The easiest way to describe the basic python interface is by providing a number of examples of loops of different type.

Let's start with a loop over all of the nodes in a model_part, which prints the ID and the coordinates for all of the nodes

  for node in model_part.Nodes:
      print node.Id , " ", node.X, " ",node.Y

This is very basic but already provides an idea of the interface.

Let's now enrich the example to print the same informations but exclusively on the nodes with positive abscissa

  for node in model_part.Nodes:
      if(node.X > 0.0): ##printing the ID of all of the nodes with positive X
          print node.Id , " ", node.X, " ",node.Y           

An important feature is the possibility of the interface to create easily lists of nodes, which can be used to restrict the loops to some areas in the model. For example a list of all of the nodes with positive ascissa can be obtained as

  new_list = [] #here we create an empty list
  for node in model_part.Nodes:
      if(node.X > 0.0):
           new_list.append(node) #here we add to the new list the node

at this point we filled the "new list" with pointer to the nodes we wanted to identify. The number of nodes in the new list can be known by printing

  print len(new_list) 

which is equivalent to the "size" operator in C++

let's assume now that we want to set the TEMPERATURE to the value of 100.0 on the nodes in "new_list". This is obtained immediately by typing for it in new_list:


in doing this we introduced the operator "SetSolutionStepValue" which provides us the possibility to write on the Nodal "solution step database"

The command above should be interpreted as: for the iterator "it" assign to the variable TEMPERATURE at the current step (the current step is identified by 0) the value of 100.0

Personal tools