-
Notifications
You must be signed in to change notification settings - Fork 247
Python Script Tutorial: Nodes and Nodal Data
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.
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 *
import KratosMultiphysics.FluidDynamicsApplication
this_model = Model()
fluid_model_part = this_model.CreateModelPart("FluidPart")
fluid_model_part.AddNodalSolutionStepVariable(VELOCITY)
fluid_model_part.AddNodalSolutionStepVariable(PRESSURE)
fluid_model_part.AddNodalSolutionStepVariable(TEMPERATURE)
fluid_model_part_io = ModelPartIO("path/to/file/example")
fluid_model_part_io.ReadModelPart(fluid_model_part)
fluid_model_part.SetBufferSize(3)
The nodes stored in the ModelPart can be accessed using the Nodes parameter:
model_part_nodes = fluid_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 fluid_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 fluid_model_part.Nodes:
print(node.Id)
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 fluid_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 fluid_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)
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_velocity = node.GetSolutionStepValue(VELOCITY) # node's velocity at the current time step
We can write the velocities of all the nodes:
for node in fluid_model_part.Nodes:
node_velocity = node.GetSolutionStepValue(VELOCITY) # node's velocity at the current time step
print(node_velocity)
you can also get a value for n time step ago, where n is the buffer size:
node_previous_velocity = node.GetSolutionStepValue(VELOCITY, 1) # node's velocity at 1 time step ago
node_earlier_velocity = node.GetSolutionStepValue(VELOCITY, 2) # node's velocity at 2 time step ago
For getting the previous time step velocities of all the nodes:
for node in fluid_model_part.Nodes:
print(node.GetSolutionStepValue(VELOCITY, 1)) # node's velocity 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 ModelPart
. This is obtained immediately by typing
for node in fluid_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 ModelPart Elements and Conditions
Prev Writing Output File
- Getting Kratos (Last compiled Release)
- Compiling Kratos
- Running an example from GiD
- Kratos input files and I/O
- Data management
- Solving strategies
- Manipulating solution values
- Multiphysics
- Video tutorials
- Style Guide
- Authorship of Kratos files
- Configure .gitignore
- How to configure clang-format
- How to use smart pointer in Kratos
- How to define adjoint elements and response functions
- Visibility and Exposure
- Namespaces and Static Classes
Kratos structure
Conventions
Solvers
Debugging, profiling and testing
- Compiling Kratos in debug mode
- Debugging Kratos using GDB
- Cross-debugging Kratos under Windows
- Debugging Kratos C++ under Windows
- Checking memory usage with Valgind
- Profiling Kratos with MAQAO
- Creating unitary tests
- Using ThreadSanitizer to detect OMP data race bugs
- Debugging Memory with ASAN
HOW TOs
- How to create applications
- Python Tutorials
- Kratos For Dummies (I)
- List of classes and variables accessible via python
- How to use Logger
- How to Create a New Application using cmake
- How to write a JSON configuration file
- How to Access DataBase
- How to use quaternions in Kratos
- How to do Mapping between nonmatching meshes
- How to use Clang-Tidy to automatically correct code
- How to use the Constitutive Law class
- How to use Serialization
- How to use GlobalPointerCommunicator
- How to use PointerMapCommunicator
- How to use the Geometry
- How to use processes for BCs
- How to use Parallel Utilities in futureproofing the code
- Porting to Pybind11 (LEGACY CODE)
- Porting to AMatrix
- How to use Cotire
- Applications: Python-modules
- How to run multiple cases using PyCOMPSs
- How to apply a function to a list of variables
- How to use Kratos Native sparse linear algebra
Utilities
Kratos API
Kratos Structural Mechanics API