Python Script Tutorial: ModelPart Nodes and Nodal Data

From KratosWiki
Revision as of 16:23, 11 July 2016 by Cpuigbo (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

In this tutorial the access to the nodes stored in a ModelPart and their nodal data will be described. More information about nodes and nodal data can be found here.


Contents

Starting

First of all we need to create a python file with following code to import the Kratos, create a ModelPart and read it from input as described in the here :

from KratosMultiphysics import *
from KratosMultiphysics.StructuralMechanicsApplication import *

structure_model_part = ModelPart("StructurePart")

structure_model_part.AddNodalSolutionStepVariable(DISPLACEMENT)
structure_model_part.AddNodalSolutionStepVariable(FORCE)
structure_model_part.AddNodalSolutionStepVariable(TEMPERATURE )
 
model_part_io_structure = ModelPartIO("path/to/file/example")
model_part_io_structure.ReadModelPart(structure_model_part)

structure_model_part.SetBufferSize(3)

Accessing Nodes

The nodes stored in the ModelPart can be accessed using the Nodes parameter:

 model_part_nodes = structure_model_part.Nodes

Having access to the nodes make iteration over all nodes very easy. For example to print all nodes in the model part:

  for node in structure_model_part.Nodes:
      print node

Here is a loop over all of the nodes in a model part, which prints the ID for all of the nodes:

  for node in structure_model_part.Nodes:
      print node.Id

Node Coordinates

The coordinates can be accessed by X,Y,Z parameters of the node:

node_x = node.X
node_y = node.Y
node_z = node.Z

Or we can extend the previous example writing also the coordinates of all the nodes in the ModelPart:

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


This access is very useful in order to classify the nodes due to their position. For example we can extend the previous loop to write node information exclusively on the nodes with positive X

  for node in structure_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

Nodal Data

The Python interface provides full access to the nodal database. The access to the historical variables is given by GetSolutionStepValue and SetSolutionStepValue passing the variable you want:

  node_displacement = node.GetSolutionStepValue(DISPLACEMENT) # node's displacement at the current time step

We can write the displacements of all the nodes:

 for node in structure_model_part.Nodes:
   node_displacement = node.GetSolutionStepValue(DISPLACEMENT) # node's displacement at the current time step
   print node_displacement
  

you can also get a value for n time step ago, where n is the buffer size:

  node_previous_displacement = node.GetSolutionStepValue(DISPLACEMENT, 1) # node's displacement at 1 time step ago 
  node_earlier_displacement = node.GetSolutionStepValue(DISPLACEMENT, 2) # node's displacement at 2 time step ago 

For getting the previous time step displacements of all the nodes:  

for node in structure_model_part.Nodes:
   print  node.GetSolutionStepValue(DISPLACEMENT, 1) # node's displacement at 1 time step ago 

    To set the historical value for a variable in a node we can use the SetSolutionStepValue. 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 node in structure_model_part.Nodes:
    node.SetSolutionStepValue(TEMPERATURE,0,100.0)


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.



Next Tutorial : Python Script Tutorial: ModelPart Elements and Conditions

Previous Tutorial : Python Script Tutorial: Writing Output File

All Tutorials : Kratos Tutorials

Personal tools
Categories