How to use Python
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
which is equivalent to the "size" operator in C++. It is now possible to iterate on the "new_list" exactly as on the original, by simply writing
for it in new_list: it.(do something) ...
Accessing to the Nodal Data Base
The Python interface provides full access to the nodal and elemental database. To make an example let's assume that we want to set the variable 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 node pointed by iterator "it" assign to the variable TEMPERATURE at the current step (the current step is identified by 0) the value of 100.0.
In a similar way it is possible to access to the value that are already stored in the database, by using the operator "GetSolutionStepValue". to make an example the vector of DISPLACEMENTs on the nodes at at the current time step can be printed on all of the nodes in the model part by doing
for iii in model_part.Nodes: print iii.GetSolutionStepValue(DISPLACEMENT)
The possibility exists to access to the values of a variable in the past ... at least as long as the time step of interest is still in the buffer (normally the buffer includes 1 or 2 steps in the past). To make an example
for iii in model_part.Nodes: print iii.GetSolutionStepValue(DISPLACEMENT,1) # prints the DISPLACEMENT at 1 step in the past print iii.GetSolutionStepValue(DISPLACEMENT,2) # prints DISPLACEMENTS 2 corresponding to 2 steps in the past
The user should note that "GetSolutionStepValue(DISPLACEMENT,0)" is identical to "GetSolutionStepValue(DISPLACEMENT)"