How to work with nodes and elements in python

From KratosWiki
Revision as of 21:20, 18 March 2009 by Amirbcn (Talk | contribs)
Jump to: navigation, search

When you work with python, in many applications you may need to access the nodes and elements data or you may want to apply some boundary conditions directly from python. In these cases you need to use some simple commands in your run file of python which we present them as below:

Contents

Nodes

Applying boundary conditions

To apply a boundary condition on some nodes, you should first detect your desirable nodes in the model and then save them in an array. We name this array as Dirirchlet_nodes and we first search all the nodes to detect some of them as our desirable nodes to apply a Dirichlet boundary condition. Dirichlet_nodes array will help us to access our desirable nodes faster, without searching all the nodes again. For example, we can add the following lines in our python run file, which we name it as Run.py:

Run.py:

Dirichlet_nodes = []  # Defining an array to store the nodes

for node in model_part.Nodes:

    if(node.Y > 100):
       node.Fix(DISPLACEMENT_X)
       node.Fix(DISPLACEMENT_Y)
       Dirichlet_nodes.append(node)


In this example we select the nodes whose Y coordinates are more than 100. To access the information of the nodes` coordinate, you can easily use the optional iterator node and then use the components of .X,.Y or .Z to access the X,Y and Z coordinates of each node respectively. model_part.Nodes is the interface to access the nodes information in python. For the selected nodes in this example, we want to fix the value of DISPLACEMENT_X and DISPLACEMENT_X with command .Fix and then we store each node in Dirichlet_nodes array with command .append. After fixing the nodes, we can apply any values for DISPLACEMENT_X and DISPLACEMENT_Y with the following commands:

for i in range(0,len(Dirichlet_nodes)):
    Dirichlet_nodes[i].SetSolutionStepValue(DISPLACEMENT_X,0,1) 
    Dirichlet_nodes[i].SetSolutionStepValue(DISPLACEMENT_Y,0,-1)

where we apply the values of 1 and -1 for DISPLACEMENT_X and DISPLACEMENT_Y respectively. In this way we just need to make some iterations through the loop with the length of len(Dirichlet_nodes) instead of the number of total nodes and it makes the implementation of the boundary condition faster specially when we have to apply this condition several times in our application, i.e. transient problems. To make the nodes free from previous boundary conditions we can use command .Free as

for i in range(0,len(Dirichlet_nodes)):
    Dirichlet_nodes[i].Free(DISPLACEMENT_X) 
    Dirichlet_nodes[i].Free(DISPLACEMENT_Y)

Elements

Applying boundary conditions

To be continued ............

Personal tools
Categories