ChronoPyEngine:Demo python1

From ChronoWiki

Jump to: navigation, search

Before executing this tutorial, be sure that your PyChrono::Engine is installed.

This tutorial uses Python as a language, so you can type it into PyScripter or IDLE or similar Python developing environments.

The original demo_python1.py file is available in the source directory of the Chrono::Engine SDK.

This should be considered as the first lesson about PyChrono::Engine, because it shows how to import the PyChrono::Engine module, how to create its objects, how to manipulate some simple but fundamental classes about 3D vectors, matrices, etc. Read the comments - they are self explainatory.


# Load the Chrono::Engine unit!!!
import ChronoEngine_PYTHON_core as chrono


# Test logging
chrono.GetLog() << "result is: " << 11+1.5 << "\n"


# Test vectors
my_vect1 = chrono.ChVectorD()
my_vect1.x=5
my_vect1.y=2
my_vect1.z=3
my_vect2 = chrono.ChVectorD(3,4,5)
my_vect4 = my_vect1*10 + my_vect2
my_len = my_vect4.Length()
print ('vect sum   =', my_vect1 + my_vect2)
print ('vect cross =', my_vect1 % my_vect2)
print ('vect dot   =', my_vect1 ^ my_vect2)

# Test quaternions
my_quat = chrono.ChQuaternionD(1,2,3,4)
my_qconjugate = ~my_quat
print ('quat. conjugate  =', my_qconjugate)
print ('quat. dot product=', my_qconjugate ^ my_quat)
print ('quat. product=',     my_qconjugate % my_quat)

# Test matrices
ma = chrono.ChMatrixDynamicD(4,4)
ma.FillDiag(-2)
mb = chrono.ChMatrixDynamicD(4,4)
mb.FillElem(10)
mc = (ma-mb)*0.1;   # operator overloading of +,-,* is supported
print (mc);
mr = chrono.ChMatrix33D()
mr.FillDiag(20)
print  (mr*my_vect1);


# Test frames -
#  create a frame representing a translation and a rotation
#  of 20 degrees on X axis
my_frame = chrono.ChFrameD(my_vect2, chrono.Q_from_AngAxis(20*chrono.CH_C_DEG_TO_RAD, chrono.ChVectorD(1,0,0)))
my_vect5 = my_vect1 >> my_frame

# Print the class hierarchy of a chrono class
import inspect
inspect.getmro(chrono.ChStreamOutAsciiFile)

# Use matrices
my_matr = chrono.ChMatrixDynamicD(6,4)
my_matr[1,2]=4
my_matrb = chrono.ChMatrixDynamicD(4,6)
my_matrb.FillRandom(-2,2)
print ( my_matrb * 4 )


# Use the ChFunction classes
my_funct = chrono.ChFunction_Sine(0,0.5,3)
print ('function f(0.2)=', my_funct.Get_y(0.2) )

Note that most of the PyChrono::Engine classes cannot be inherited from Python, except for few of them that support cross polymorphism thank to the director feature of SWIG. This is the case of the ChFunction class, that can be inherited and specialized with additional classes on the Python side, for example:

# Inherit from the ChFunction, from the Python side,
# (do not forget the __init__ constructor)

class MySquareFunct (chrono.ChFunction):
    def __init__(self):
         chrono.ChFunction.__init__(self)
    def Get_y(self,x):
         return x*x

my_funct2 = MySquareFunct()
print ('function f(2) =', my_funct2.Get_y(2) )
print ('function df/dx=', my_funct2.Get_y_dx(2) )
Personal tools