Introduction to Python

Learn the basics of Python interoperation with Chrono::Engine, using PyChrono.

  • import the PyChrono module
  • use basic classes: vectors, matrices, etc.
  • inherit classes
1 #------------------------------------------------------------------------------
2 # Name: pychrono example
3 # Purpose:
4 #
5 # Author: Alessandro Tasora
6 #
7 # Created: 1/01/2019
8 # Copyright: (c) ProjectChrono 2019
9 #------------------------------------------------------------------------------
10 
11 print ("First tutorial for PyChrono: vectors, matrices etc.");
12 
13 
14 # Load the Chrono::Engine core module!
15 import pychrono as chrono
16 try:
17  import numpy as np
18  from numpy import linalg as LA
19 except ImportError:
20  print("You need NumPyto run this demo!")
21 
22 
23 # Test logging
24 chrono.GetLog().Bar()
25 chrono.GetLog() << "result is: " << 11+1.5 << "\n"
26 chrono.GetLog().Bar()
27 
28 
29 # Test vectors
30 my_vect1 = chrono.ChVectorD()
31 my_vect1.x=5
32 my_vect1.y=2
33 my_vect1.z=3
34 my_vect2 = chrono.ChVectorD(3,4,5)
35 my_vect4 = my_vect1*10 + my_vect2
36 my_len = my_vect4.Length()
37 print ('vect sum =', my_vect1 + my_vect2)
38 print ('vect cross =', my_vect1 % my_vect2)
39 print ('vect dot =', my_vect1 ^ my_vect2)
40 
41 # Test quaternions
42 my_quat = chrono.ChQuaternionD(1,2,3,4)
43 my_qconjugate = ~my_quat
44 print ('quat. conjugate =', my_qconjugate)
45 print ('quat. dot product=', my_qconjugate ^ my_quat)
46 print ('quat. product=', my_qconjugate % my_quat)
47 
48 # Test matrices and NumPy interoperability
49 mlist = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]
50 ma = chrono.ChMatrixDynamicD()
51 ma.SetMatr(mlist) # Create a Matrix from a list. Size is adjusted automatically.
52 npmat = np.asarray(ma.GetMatr()) # Create a 2D npy array from the list extracted from ChMatrixDynamic
53 w, v = LA.eig(npmat) # get eigenvalues and eigenvectors using numpy
54 mb = chrono.ChMatrixDynamicD(4,4)
55 prod = v * npmat # you can perform linear algebra operations with numpy and then feed results into a ChMatrixDynamicD using SetMatr
56 mb.SetMatr(v.tolist()) # create a ChMatrixDynamicD from the numpy eigenvectors
57 mr = chrono.ChMatrix33D()
58 mr.SetMatr([[1,2,3], [4,5,6], [7,8,9]])
59 print (mr*my_vect1);
60 
61 
62 # Test frames -
63 # create a frame representing a translation and a rotation
64 # of 20 degrees on X axis
65 my_frame = chrono.ChFrameD(my_vect2, chrono.Q_from_AngAxis(20*chrono.CH_C_DEG_TO_RAD, chrono.ChVectorD(1,0,0)))
66 my_vect5 = my_vect1 >> my_frame
67 
68 # Print the class hierarchy of a chrono class
69 import inspect
70 inspect.getmro(chrono.ChStreamOutAsciiFile)
71 
72 
73 
74 # Use the ChFunction classes
75 my_funct = chrono.ChFunction_Sine(0,0.5,3)
76 print ('function f(0.2)=', my_funct.Get_y(0.2) )
77 
78 
79 # Inherit from the ChFunction, from the Python side,
80 # (do not forget the __init__ constructor)
81 
82 class MySquareFunct (chrono.ChFunction):
83  def __init__(self):
84  chrono.ChFunction.__init__(self)
85  def Get_y(self,x):
86  return x*x
87 
88 
89 my_funct2 = MySquareFunct()
90 print ('function f(2) =', my_funct2.Get_y(3) )
91 print ('function df/dx=', my_funct2.Get_y_dx(3) )
92 
93 
94 
95 
96 
97 
98 
99