HyperWorks Solvers

Python-based API

Python-based API

Previous topic Next topic No expanding text in this topic  

Python-based API

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

There are five key reasons why Altair is providing a Python interface for MotionSolve:

Make MotionSolve easier to learn and use
Allow you to build models that are described in terms of design variables. New model variations can be generated simply be redefining some or all of the design variables. Such descriptions are well suited for design exploration and optimization.
Allow you to customize MotionSolve to do what you want
Perform design sensitivity analysis and optimization
Allow you to have fun “playing” with MotionSolve

Why Python?

Python is easy to learn and use. Its syntax is very simple. Python very closely resembles the English language. Due to its simplicity and consistency, Python has become a very popular programming language.
Python is very popular in the scientific community because it is well suited for numerical calculations. Python has been around for over 20 years, so many utilities and libraries are available for everyone to use. The most well-known of these are available at https://pypi.python.org,
Python is free. There are many publicly available web sites and resources dedicated to teaching Python.
Python is robust and has proven to be relatively error free. Python can scale to solve complex problems.
Python is flexible. It is extremely well suited to rapid development and prototyping.

A simpler and more powerful MotionSolve

The MotionSolve interface is presented to you as a set of classes with attributes and methods. Each attribute is associated with a “property” -- a type definition that allows attributes to be initialized and validated.

With the XML approach, you execute MotionSolve model “statements” or “commands” in an XML file. The equivalent in this approach is that you instantiate objects, modify them and apply methods on them. Some classes of objects are “pre-defined or built-in” and you could define others.

In this approach, MotionSolve models are compact and they are human readable. Standard Python syntax is used for MotionSolve objects and extensions. You do not need to learn a new syntax or language. Python syntax is very simple and English-like.

The MotionSolve Python interface provides the same language for MotionSolve Statements and Commands. In fact the distinction between them vanishes – it’s just that some actions are performed prior to running the solver and others while it is executing. MotionSolve allows only a subset of the model attributes to be modified at run time. The interface is aware of this and will only let you do what is permitted.

The Python interface also performs extensive checks on the model to ensure that it is proper before allowing an analysis to be done. Incorrect or illegal models are flagged as early as possible so you know the root cause of the errors and can fix these easily.

Unlike the XML format that relies on object IDs to cross reference entities, the Python interface allows you to directly cross reference objects. This is more natural. You no longer need to manage IDs any more. They are internally generated as needed. Ids are still supported -- you can continue to use these if you want to.

Finally, the messages emanating from MotionSolve have been improved so that they are more understandable and they are always associated with objects that you have created. This becomes especially important when you create and use higher-level or composite objects. Instantiating such an object will result in the creation of numerous lower-level objects. So a proper handling of messages becomes very important.

Examples

A few simple examples introducing the MotionSolve-Python interface are presented here.  In the examples in this document:

“>>>” is the prefix used by Python
The text after “>>>”, in bold, is typed by you
Software response is shown as highlighted text

Launching Python, importing the MotionSolve Python Interface library

C:\Users\rajivr\Desktop\lugre> python

Python 2.7 (r27:82500, Apr  5 2012, 14:38:31) [MSC v.1600 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> from msolve import *

>>> import math

>>>

Creating modeling elements while building a model

Define the Units for a model

define_units_python

Define a Marker that lies midway between two points with its z-axis pointing to the second point

 

A  = Point (20,30,44) #  20, 30,  44

B  = Point (40,200,0) #  40, 200, 0

qploc  = (A+B)/2

M = Marker (body=p3, qp=qploc, zp=B, label="Coupler-link cm")

Accessing attribute values

The dot operator (.) is used to “get” or access class attribute values. These values can be used in other python expressions. The examples below illustrate.

>>> kmmsN.mass

KILOGRAM’

 

>>> kmmsN.force

NEWTON’

 

>>> A.x

20.0

 

>>> B.z

0.0

 

>>> M.body

p3

 

>>> M.qp

Point (30.0, 115.0, 22.0)

 

>>> M.zp.x

40.0

 

>>> M.label

‘Coupler-link cm’

Modifying attribute values

>>> kmmsN.mass

‘KILOGRAM’

 

>>> kmmsN.mass = “MEGAGRAM”

>>> kmmsN.mass

‘MEGAGRAM’