SimAcademy Webinar Series
Python Scripting in Adams View
By Kent West & Maziar Rostamian
MSC Technical Support
Page 1
Copyright© 2016 [Link] Corporation
Agenda
• Python in Adams – Why?
• Python Introduction
• Adams Python API
• Example – joint_rename
• Example – save_version
• Example – FE_part creation
2
Copyright© 2016 [Link] Corporation
Python in Adams – Why?
• Python is an alternative to the existing cmd language
• This is ‘phase 1’ for ‘model building’ functionality
• Next phases focus on post-processing, automotive-specific
functionality and more.
• Why Python?
– Excellent community support
– Powerful, efficient language
– Many, many libraries available..
Copyright© 2016 [Link] Corporation
Agenda
• Python in Adams – Why?
• Python Introduction
• Adams Python API
• Example – joint_rename
• Example – save_version
• Example – FE_part creation
4
Copyright© 2016 [Link] Corporation
Basic Data Types in Python
• Review the Help documentation:
– See the ‘Overview of the Interface’ section in the Adams View Help guides
– Visit [Link]
Copyright© 2016 [Link] Corporation
List Overview
>>> aList = ['one', 'two', 'three', 'four']
>>> len(aList)
4
>>> [Link]('five')
>>> print aList
['one', 'two', 'three', 'four', 'five']
>>> print(aList[1], aList[-1], aList[2:])
('two', 'five', ['three', 'four', 'five'])
>>> newList = aList + ['ten', 'eleven']
>>> for anItem in newList:
... print anItem
Copyright© 2016 [Link] Corporation
Dictionary Overview
>>> dict = { 'mar1':11, 'mar2':22, 'mar3':33 }
>>> print([Link](), [Link]())
(['mar1', 'mar2', 'mar3'], [11, 22, 33])
>>> dict['newkey'] = 44
>>> 'mar1' in dict
True
>>> 22 in [Link]()
True
Copyright© 2016 [Link] Corporation
Dictionaries: Looping
>>> mod = [Link]()
>>> allParts = [Link]
>>> for partName in allParts:
... print partName,
...
ground cylinder lid collar disc plunger pivot_bottom top handle
connector spout
>>> for partName, partObject in [Link]():
... if 'i' in partName:
... print partName, partObject
...
cylinder <[Link] object at 0x0000000025A8CDD8>
lid <[Link] object at 0x0000000025A8CDA0>
disc <[Link] object at 0x0000000025AA51D0>
Copyright© 2016 [Link] Corporation
More List Operations:
Function Name Description
[Link](x) Remove the first item from the list whose value is x. It
is an error if there is no such item
[Link]([i]) Remove the item at the given position in the list, and
return it
[Link](x) Return the number of times x appears in the list.
[Link]() Reverse the elements of the list, in place.
[Link](cmp=None, Sort the items of the list in place (the arguments can
key=None, be used for sort customization, see sorted() for their
reverse=False) explanation).
9
Copyright© 2016 [Link] Corporation
List Example: Sorting
>>> markers = [ ("mar1", 33), ("mar2", 22), ("mar4", 1)]
>>> [Link]()
>>> print markers
[('mar4', 1), ('mar2', 22), ('mar1', 33)]
>>> [Link]()
>>> print markers
[('mar1', 33), ('mar2', 22), ('mar4', 1)]
>>> def get_2nd_element(some_list):
... return some_list[1]
...
>>> sorted(markers, key=get_2nd_element)
[('mar4', 1), ('mar2', 22), ('mar1', 33)]
10
Copyright© 2016 [Link] Corporation
Functions - Simple
>>> def add(a, b):
... """ Add two numbers """
... return a + b
...
>>> add(5, 20)
25
>>> help(add)
Help on function add in module __main__:
add(a, b)
Add two numbers
11
Copyright© 2016 [Link] Corporation
Functions - Parameters
>>> def new_part_name(prefix = None):
... """ Make a nice name.. """
... if prefix is None:
... prefix = "part"
... return prefix + "_new“
>>> new_part_name()
'part_new'
>>> new_part_name('base')
'base_new'
12
Copyright© 2016 [Link] Corporation
Functions – Interesting Return Values
def compute_location(dist, angle):
""" Compute end point of vector.."""
import math
return dist * [Link](angle), dist * [Link](angle)
>>> compute_location(100, 0.5)
(87.75825618903727, 47.942553860420304)
>>> loc = compute_location(100, 0.5)
>>> print loc
(87.75825618903727, 47.942553860420304)
>>> x, y = compute_location(100, 0.5)
>>> print x, y
87.758256189 47.9425538604
13
Copyright© 2016 [Link] Corporation
Functions – Organizing, Simple Method:
Use execfile() to read in a file containing all function definitions:
>>> dir()
['Adams', '__builtins__', '__doc__', '__name__',
'__package__', 'evaluate_exp', 'execute_cmd', 'mdi', 'pm',
'sys', 'x']
>>> execfile('marker_functions.py')
>>> dir()
['Adams', '__builtins__', '__doc__', '__name__',
'__package__', 'add', 'compute_location', 'evaluate_exp',
'execute_cmd', 'mdi', 'mid_point', 'new_part_name', 'pm',
'shift_markers', 'sys', 'x']
Copyright© 2016 [Link] Corporation
Functions – Organizing, Pythonic Method:
Arrange functions, constants, classes into a module structure on disk:
>>> from ProjectNoel import marker_functions
>>> dir()
['Adams', '__builtins__', '__doc__', '__name__',
'__package__', 'evaluate_exp', 'execute_cmd',
'marker_functions', 'mdi', 'os', 'pm', 'sys']
>>> dir(marker_functions)
['__builtins__', '__doc__', '__file__', '__name__',
'__package__', 'add', 'compute_location', 'mid_point',
'new_part_name', 'shift_markers']
Copyright© 2016 [Link] Corporation
Agenda
• Python in Adams – Why?
• Python Introduction
• Adams Python API
– Creating Modeling Elements
– Get/Set Properties
– Finding Elements
• Example – joint_rename
• Example – save_version
• Example – FE_part creation
16
Copyright© 2016 [Link] Corporation
Adams Python API - Overview:
Copyright© 2016 [Link] Corporation
Adams Python API – Getting Started
• Importing Python Commands
– Import Python Commands version of the model using File > Import
– Input Python commands in the Command Window by switching to python:
• Common Python commands for all models:
– Import the Adams library that includes all Adams related commands:
import Adams # unnecessary in Adams View environment
– Define some model default values such as units:
[Link]='mm'
– Define the ‘model’ object:
m = [Link](name='Model_Part')
Copyright© 2016 [Link] Corporation
Adams Python API – Create Markers, Design Variables
• Creating markers:
– Create parts first:
p1 = [Link](name='Part_1')
p2 = [Link](name='Part_2')
– Create markers on the parts:
# marker command default location is the global origin
mar1 = [Link](name='Marker_1')
mar2 = [Link](name='Marker_2', location =
[500,500,0])
• Creating design variables:
# an integer DV
v1 = [Link] (name = "DV_1", value =
10)
# an object DV, referring to part_1 (p1)
v4 = [Link] (name = "DV_4", value =
p1)
Copyright© 2016 [Link] Corporation
Adams Python API – Joints, Geometry
• Creating joints:
– A translational joint:
j1 = [Link](name = 'Joint_1',
i_marker_name = '.Constraints.Part_1.Marker_1', j_marker_name
= '.Constraints.Part_2.Marker_2')
– A ConVel joint:
j2 = [Link](name = 'Joint_2',
i_marker_name = '.Constraints.Part_1.Marker_1', j_marker_name
= '.Constraints.Part_2.Marker_2')
• Creating geometries:
– A block:
g1 = [Link](name='Box_1', corner_marker =
mar1, x = 250, y = 250, z = 250)
– An Ellipse:
g2 = [Link](name='Ellipse_1',
center_marker=mar1, start_angle=60, end_angle=270,
major_radius=1000, minor_radius=500)
Copyright© 2016 [Link] Corporation
Adams Python API - Forces
• Creating forces:
– Gravity:
g1 = [Link](name='ACCGRAV_1',
xyz_component_gravity = [0,-9810,0])
– A VFORCE:
# Force VFORCE, we need a floating marker as well:
Fmar1 = [Link](name="FMarker_1")
# Create VFORCE
f1 = [Link](name = "VForce_1",
i_marker_name = '.Forces.Part_1.Marker_1',
j_floating_marker_name = '.Forces.Part_2.FMarker_1',
ref_marker_name = '.Forces.Part_1.Marker_1')
– An SFORCE:
f2 = [Link](name = "SForce_1",
i_marker_name = '.Forces.Part_1.Marker_1', j_marker_name =
'.Forces.Part_2.Marker_2')
Copyright© 2016 [Link] Corporation
Adams Python API – FE_PART:
• Creating FE_PARTs:
– First, create material properties:
mat = [Link](name='steel', youngs_modulus =
2.07E+07, poissons_ratio = 0.29, density = 7.801E-06)
– Next, create the FE_PART components:
# Create the FE_PART object:
fep=[Link](name = 'FE_PART1', i_location=mi,
j_location=mj, material_type=mat)
# Create a rectangular section:
sec=[Link]()
sec.rect_base=20.0
sec.rect_height=30.0
[Link](sec)
# add nodes on the FE_Part
[Link](0.2, section_label=sec)
[Link](0.5, section_label=sec)
[Link](0.8, section_label=sec)
Copyright© 2016 [Link] Corporation
Adams Python API – Get/Set Properties:
>>> myPart = [Link](".manual_pump.cylinder")
• To find property list: use Documentation or dir():
>>> dir(myPart)
['DBKey', 'DesignPoints', 'DesignVariables', 'FloatingMarkers',
'Geometries', 'Markers', … , ‘mass‘, 'parent', 'planar', 'plane', 'properties',
'relative_to', 'setProperties', 'vm', 'vm_name', 'vx', 'vy', 'vz', 'wm',
'wm_name', 'wx', 'wy', 'wz']
>>> [Link]
0.11641093038060639
>>> [Link] = 1.234
Copyright© 2016 [Link] Corporation
Adams Python API – Finding Elements:
• Find by name:
>>> myPart = [Link](".manual_pump.cylinder")
>>> print([Link], [Link], [Link])
('cylinder', 1.2, <[Link] object at 0x000000001C9315F8>)
• To find all elements:
>>> mod = [Link]()
>>> print([Link](), )
(['FixedJoint_ground_to_cylinder',
'FixedJoint_cylinder_to_lid', 'FixedJoint_lid_to_collar',
'FixedJoint_spout_to_cylinder)
>>> for jntName in [Link]():
... print jntName,
...
Copyright© 2016 [Link] Corporation