How to use Python
m (How to loop over Nodes and Elements moved to Python Basics: not a good name) |
Revision as of 11:26, 27 November 2007
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
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 our model_part. This is obtained immediately by typing for it in model_part.Nodes:
it.SetSolutionStepValue(TEMPERATURE,0,100.0)
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)"
Creating Lists
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++. 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) ...
"Coloring Nodes" to create custom lists
Creating lists of Nodes depending on their coordinates may be cumbersome or impossible on complex geometries. A more practical possibility is "coloring" nodes in the pre-processing step and using this information for creating custom_lists inside the python. This could be done with every variable, but we will assume here that "FLAG_VARIABLE" is used for the purpose. For example one may assume that in the pre-processing step
- FLAG_VARIABLE = 1 was set on the nodes on one part of the boundary
- FLAG_VARIABLE = 2 was set somewhere else
the user may want to initialize the TEMPERATURE to 10 in the area identified by FLAG_VARIABLE=1 or to TEMPERATURE=sin(time) when FLAG_VARIABLE=2
a simple way of doing this is creating two lists identifying the nodes of one type or of the other
list1 = [] list2 = [] for node in model_part.Nodes: if(node.GetSolutionStepValue(FLAG_VARIABLE) == 1): list1.append(node) elif(node.GetSolutionStepValue(FLAG_VARIABLE) == 2): list2.append(node)
at this point we can loop on the two list and apply the conditions at wish, for example
for node in list1: node.SetSolutionStepValue(TEMPERATURE,0,10.0)
and, after importing python's mathematical library
import math
the user can, at every time step, update the value of temperature on the second part
temp = math.sin(time); for node in list2: node.SetSolutionStepValue(TEMPERATURE,0,temp)