How to work with nodes and elements in python

From KratosWiki
(Difference between revisions)
Jump to: navigation, search
(Applying boundary conditions)
(Getting value from integration points)
Line 33: Line 33:
 
=== Elements ===
 
=== Elements ===
  
==== Getting value from integration points====
+
==== Getting a value from integration points====
  
 
The typical example of working with integration points is to get stress or strain values from these points for an element. For the first step, you should get the current process information and put it in a value such as proc_info:
 
The typical example of working with integration points is to get stress or strain values from these points for an element. For the first step, you should get the current process information and put it in a value such as proc_info:
Line 40: Line 40:
  
 
    
 
    
 +
then you can use the following loop to get a value such as Stress from integration point of each element in the model:
 +
 +
 +
StressVector = [] # Defining an array to store stress components 
 +
 +
for element in model_part.Elements:
 +
    StressVector = element.GetValuesOnIntegrationPoints(STRESS, proc_info)
 +
 +
 +
where StressVector is the array to store stress components. The size of this vector depends on the output vector that you define in your element
  
  
  
 
To be continued ............
 
To be continued ............

Revision as of 21:40, 18 March 2009

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

Getting a value from integration points

The typical example of working with integration points is to get stress or strain values from these points for an element. For the first step, you should get the current process information and put it in a value such as proc_info:

proc_info = model_part.ProcessInfo 


then you can use the following loop to get a value such as Stress from integration point of each element in the model:


StressVector = [] # Defining an array to store stress components  
for element in model_part.Elements:
    StressVector = element.GetValuesOnIntegrationPoints(STRESS, proc_info)

where StressVector is the array to store stress components. The size of this vector depends on the output vector that you define in your element


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

Personal tools
Categories