Python Interoperability Module

Slides:



Advertisements
Similar presentations
Chapter Modules CSC1310 Fall Modules Modules Modules are the highest level program organization unit, usually correspond to source files and.
Advertisements

CIS 4004: Web Based Information Technology Spring 2013
Word Lesson 8 Increasing Efficiency Using Word
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Creating a Console Application with Visual Studio
Structure of program You must start with a module import# You must then encapsulate any while loop in a main function at the start of the program Then.
UFCEKG-20-2 Data, Schemas & Applications Lecture 4 Server Side Scripting & PHP.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
A First Program Using C#
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
Computing IV Visual C Introduction with OpenCV Example Xinwen Fu.
Introduction to Programming Peggy Batchelor.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Engineering\CADD Systems Office CADD Manager's Series Customizing the Interface.
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Transition to Java Programming with Alice and Java First Edition.
® IBM Software Group © 2006 IBM Corporation Rational Asset Manager v7.2 Using Scripting Tutorial for using command line and scripting using Ant Tasks Carlos.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
ASP.NET Programming with C# and SQL Server First Edition
June 17, 2009 Office 2007 Tips & Tricks.
User-Written Functions
Python’s Modules Noah Black.
Add-In for Pre-Processing in SolidWorks
CS1022 Computer Programming & Principles
Topics Introduction Hardware and Software How Computers Store Data
Classes and objects You can define many objects of the same class:
WORKSHOP 6 USING THE ASCII CONDUIT
Play Framework: Introduction
Java Programming: Guided Learning with Early Objects
Java Primer 1: Types, Classes and Operators
PYTHON: AN INTRODUCTION
3.01 Apply Controls Associated With Visual Studio Form
Why exception handling in C++?
Computer Programming I
Topic: Functions – Part 2
3.01 Apply Controls Associated With Visual Studio Form
Functions.
Copyright © 2003 Pearson Education, Inc.
CMPE 152: Compiler Design ANTLR 4 and C++
Eclipse 20-Sep-18.
Social Media And Global Computing Introduction to Visual Studio
Introduction to Python
Python Mr. Husch.
Python Primer 2: Functions and Control Flow
Topics Introduction to File Input and Output
Loaders and Linkers: Features
Understanding the Visual IDE
Lecture Set 3 Introduction to Visual Basic Concepts
Introduction to .NET By : Mr. V. D. Panchal Content :
CS 100: Roadmap to Computing
Topics Introduction Hardware and Software How Computers Store Data
Add-In for pre-processing in SolidWorks
Electronic Field Study Advanced User Training
Cmdlets “Command-lets”
Tonga Institute of Higher Education
Introduction to Object-Oriented Programming in Alice
CS 1111 Introduction to Programming Spring 2019
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Python Modules.
Matlab Basics Tutorial
Topics Introduction to File Input and Output
Review of Previous Lesson
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
The beginning of media computation Followed by a demo
SPL – PS1 Introduction to C++.
Presentation transcript:

Python Interoperability Module Chrono::Python Python Interoperability Module

Chrono::Python Use Python in Chrono? Yes! The PYTHON module is a wrapper to Chrono classes and functions How to build it: Install Python (suggested v.3.3 or later), 64 bit if you compiled Chrono in 64 bit Install the SWIG tool Enable PYTHON module in Chrono CMake Set directories to Python libs etc. in the Cmake Build Chrono – generating the PYTHON module might require few minutes… The PYTHON module of Chrono contains The ChronoEngine_pyparser.dll library for parsing Python from the C++ Chrono side The Chrono::PyEngine .pyd modules for calling the API from the Python side

Chrono::Python

a) Call Python from the C++ side

a) Call Python from the C++ side: Use the ChronoEngine_pyparser.dll library Create a python engine for parsing: ChPythonEngine my_python; // figure out what version of Python is run under the hood my_python.Run("import sys"); GetLog() << "Python version run by Chrono:\n"; my_python.Run("print (sys.version)"); Execute Python instructions: my_python.Run("a =8.6"); my_python.Run("b =4"); my_python.Run("c ='blabla' "); my_python.Run("print('In:Python - A computation:', a/2)");

a) Call Python from the C++ side: // TEST - fetch a value from a python variable (in __main__ namespace) GetLog() << "\n\n Chrono::PyEngine Test 3.\n"; double mfval; my_python.GetFloat("a", mfval); GetLog() << "In:C++ - Passed float variable 'a' from Python, a=" << mfval << "\n"; // TEST - set a value into a python variable (in __main__ namespace) my_python.SetFloat("d", 123.5); my_python.Run("print('In:Python - Passed variable d from c++, d=', d)"); // In the previous examples we didn't have any syntax errors. // In general, it is wise to enclose Python commands in a try-catch block // because errors are handled with exceptions: try { my_python.Run("a= this_itGoInG_TO_giVe_ErroRs!()"); } catch (ChException myerror) { GetLog() << "Ok, Python parsing error caught as expected.\n";

a) Call Python from the C++ side: Load a mechanical system in a .py file: // load a mechanical system, previously saved to disk from SolidWorks add-in ChSystem my_system; try { my_python.ImportSolidWorksSystem(GetChronoDataFile("solid_works/swiss_escapement").c_str(), my_system); // note, don't type the .py suffic in filename.. my_system.ShowHierarchy(GetLog()); } How to generate the .py models?  See the Chrono::SolidWorks add-in

b) Use Chrono from the Python side

b) Use Chrono from the Python side: Have you built Chrono::PyEngine? (see build instructions on the web site) Ok, now you can call Chrono API functions from a Python command line! Suggested: use PyScripter or similar IDEs for editing/running Python programs  Hint: look at .py examples in chrono\src\demos\python

b) Use Chrono from the Python side: Important!!! All Python programs must import Chrono::PyEngine Python modules using the import statement: import ChronoEngine_python_core as chrono Chrono classes will be accessed via the chrono.xxyyyzzz Python namespace If you use additional modules, for example, add also import ChronoEngine_python_postprocess as postprocess import ChronoEngine_python_irrlicht as chronoirr

b) Use Chrono from the Python side: Let's create a 3D vector object: my_vect1 = chrono.ChVectorD() Modify the properties of that vector object; this is done using the . dot operator: my_vect1.x =5 my_vect1.y =2 my_vect1.z =3 Some classes have build parameters, for example another vector can be built by passing the 3 coordinates for quick initialization: my_vect2 = chrono.ChVectorD(3,4,5) Most operator-overloading features that are available in C++ for the Chrono::Engine vectors and matrices are also available in Python, for example: my_vect4 = my_vect1*10 + my_vect2

b) Use Chrono from the Python side: Member functions of an object can be called using the . dot operator, like in C++: my_len = my_vect4.Length() print ('vector length =', my_len) You can use most of the classes that you would use in C++, for example let's play with quaternions and matrices: 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) ma = chrono.ChMatrixDynamicD(4,4) ma.FillDiag(-2) mb = chrono.ChMatrixDynamicD(4,4) mb.FillElem(10) mc = (ma-mb)*0.1; print (mc); mr = chrono.ChMatrix33D(); ...

b) Use Chrono from the Python side: Differences respect to the C++ API: Not all C++ classes/functions are wrapped in Python Templated classes are instanced with type ‘double’ by appending ‘D’ at the name: PYTHON C++ chrono.ChVectorD ChVector<double> chrono.ChQuaternionD ChQuaternion<double> chrono.ChMatrix33D ChMatrix33<double> chrono.ChMatrixNMD ChMatrixNM<double>

b) Use Chrono from the Python side: Differences respect to the C++ API: Shared pointers are handled automatically: C++: std::shared_ptr<ChLinkLockRevolute> my_link_BC(new ChLinkLockRevolute); PYTHON: my_link_BC = chrono.ChLinkLockRevolute() Upcasting is automatic, like in C++, but downcasting? There are no dynamic_cast…. But we added some helper functions called CastToChClassNameShared() : C++: myvis = std::dynamic_pointer_cast<ChVisualization>(myasset); PYTHON: myvis = chrono.CastToChVisualizationShared(myasset)