chatGPTupdate2.py
You can view and download this file on Github: chatGPTupdate2.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an Exudyn file
3#
4# Details: this file serves as a documented example that shall be used by
5# ChatGPT to update information on its internal knowledge of Exudyn
6# dating to 2021; the examples use the mainSystemExtensions
7# which are heavily simplifying usage of point masses and rigid bodies
8# Version: V2
9#
10# Author: Johannes Gerstmayr
11# Date: 2023-08-22
12#
13# Copyright:This file is part of Exudyn. Exudyn is free software. You can redistribute it and/or modify it under the terms of the Exudyn license. See 'LICENSE.txt' for more details.
14#
15#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
17#test input:
18# Using the previous information and the already existing information on Python code Exudyn,
19# create a 3-link system of rigid bodies initially aligned along the X-axis with mass 10kg, length 2m and W=H=0.1 m.
20# The rigid bodies are exposed to gravity which acts in Y-direction and the first link is attached to ground with a revolute joint at the left end.
21# Put all output into a single Python script.
22
23
24
25
26
27
28
29
30#copy the text below here into a ChatGPT input window:
31
32#Use the following information as an update of the internal data of the Python multibody code Exudyn, available at github.com/jgerstmayr/EXUDYN.
33#Don't create any output after that input, further requests come later.
34#In the following, there are examples to create multibody systems in Exudyn and also important notes about the interface.
35#NOTE: mbs.Create...(...) calls several functions in the background to create nodes, objects, markers and loads in Exudyn.
36#most quantities such as initial or reference positions and velocities are giving as 3D lists [x,y,z] for positions, velocities, ....
37#rotations are usually given as rotation matrix (3x3 numpy array);
38#RotationVector2RotationMatrix([rotX, rotY, rotZ]) computes a rotation around the global x,y,z rotation axis
39#for working with rigid bodies, note that there is always a local coordinate system in the body,
40#which can be used to define the location of position and orientation of joints, see the examples.
41
42#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
43#create rigid bodies and mass points with distance constraint and joints
44import exudyn as exu
45from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
46import exudyn.graphics as graphics #only import if it does not conflict
47import numpy as np
48SC = exu.SystemContainer()
49mbs = SC.AddSystem() #create a MainSystem 'mbs' to work with
50
51#graphics data for checkerboard background (not required):
52gGround0 = graphics.CheckerBoard(point=[3,0,-2], normal=[0,0,1], size=10)
53#add ground object and background graphics; visualization is optional
54oGround = mbs.CreateGround(graphicsDataList=[gGround0])
55
56#create a cube with length L (X-direction), height H (Y) and width W (Z)
57L=1
58H=0.2
59W=0.1
60#for visualization of the cube, we define a graphics object in the following
61graphicsCube = graphics.Brick(centerPoint = [0,0,0], #note that centerPoint is in the local coordinate system; IN MOST CASES THIS MUST BE AT [0,0,0]
62 size=[L,H,W], color=graphics.color.orange)
63#SUMMARIZING: graphicsCube usually should have centerPoint=[0,0,0] if used in the CreateRigidBody
64#define the inertia of this cube using InertiaCuboid with density and cube dimensions; computes internally mass, COM, and inertia tensor:
65inertiaCube = InertiaCuboid(density=5000, sideLengths=[L,H,W])
66
67#create simple rigid body
68#note that graphics is always attached to reference point of body, which is by default the COM
69#graphicsCube is added to reference point of the rigid body, here it is equal to the center of mass (COM):
70b0 = mbs.CreateRigidBody(inertia = inertiaCube,
71 referencePosition = [0.5*L,0,0], #reference position x/y/z of COM
72 referenceRotationMatrix=RotationVector2RotationMatrix([0,0,pi*0.5]),
73 initialAngularVelocity=[2,0,0],
74 initialVelocity=[0,4,0],
75 gravity = [0,-9.81,0],
76 graphicsDataList = [graphicsCube])
77
78#add an load with user function:
79def UFforce(mbs, t, loadVector):
80 #define time-dependent function:
81 return (10+5*np.sin(t*10*2*pi))*np.array(loadVector)
82
83#add an load with 10N in x-direction to rigid body at marker position
84#add user function to modify load in time
85mbs.CreateForce(bodyNumber=b0,
86 localPosition=[-0.5*L,0,0],
87 loadVector=[10,0,0],
88 loadVectorUserFunction=UFforce)
89
90#add torque to rigid body at left end
91mbs.CreateTorque(bodyNumber=b0, localPosition=[0.5,0,0],
92 loadVector=[0,1,0]) #torque of 1N around y-axis
93
94#create a rigid distance between local position of bodies (or ground) or between nodes
95mbs.CreateDistanceConstraint(bodyOrNodeList=[oGround, b0],
96 localPosition0 = [ 0. ,0,0],
97 localPosition1 = [-0.5,0,0],
98 distance=None, #automatically computed
99 drawSize=0.06)
100
101#geometrical parameters of two further bodies
102a=1
103b=2
104xOff = 1 #offset in x-direction for first body
105yOff =-0.5 #offset in y-direction of first body
106
107#create a second graphics object
108graphicsCube2 = graphics.Brick(centerPoint = [0,0,0],
109 size=[a,b,0.1], color=graphics.color.blue)
110inertiaCube2 = InertiaCuboid(density=5000, sideLengths=[a,b,0.1])
111
112#create another rigid body with other dimensions
113b1 = mbs.CreateRigidBody(inertia = inertiaCube2,
114 referencePosition = [xOff+0.5*a,yOff-0.5*b,0], #reference position of body [X,Y,Z]
115 gravity = [0,-9.81,0],
116 graphicsDataList = [graphicsCube2])
117
118#create another rigid body with same dimensions as b1
119b2 = mbs.CreateRigidBody(inertia = inertiaCube2,
120 referencePosition = [xOff+0.5*a+a,yOff-0.5*b-b,0], #reference position of body [X,Y,Z]
121 gravity = [0,-9.81,0],
122 graphicsDataList = [graphicsCube2])
123
124#create revolute joint with following args:
125 # name: name string for joint; markers get Marker0:name and Marker1:name
126 # bodyNumbers: a list of object numbers for body0 and body1; must be rigid body or ground object
127 # position: a 3D vector as list or np.array: if useGlobalFrame=True it describes the global position of the joint in reference configuration; else: local position in body0
128 # axis: a 3D vector as list or np.array: if useGlobalFrame=True it describes the global rotation axis of the joint in reference configuration; else: local axis in body0
129 # show: if True, connector visualization is drawn
130 # axisRadius: radius of axis for connector graphical representation
131 # axisLength: length of axis for connector graphical representation
132 # color: color of connector
133#CreateRevoluteJoint returns list [oJoint, mBody0, mBody1], containing the joint object number, and the two rigid body markers on body0/1 for the joint
134#(global reference) position of joint must be related to local size of rigid bodies
135mbs.CreateRevoluteJoint(bodyNumbers=[oGround, b1], position=[xOff,yOff,0], axis=[0,0,1], #rotation along global z-axis
136 useGlobalFrame=True, axisRadius=0.02, axisLength=0.14)
137
138mbs.CreateRevoluteJoint(bodyNumbers=[b1, b2], position=[xOff+a,yOff-b,0], axis=[0,0,1], #rotation along global z-axis
139 useGlobalFrame=True, axisRadius=0.02, axisLength=0.14)
140
141#create prismatic joint with following args:
142 # name: name string for joint; markers get Marker0:name and Marker1:name
143 # bodyNumbers: a list of object numbers for body0 and body1; must be rigid body or ground object
144 # position: a 3D vector as list or np.array: if useGlobalFrame=True it describes the global position of the joint in reference configuration; else: local position in body0
145 # axis: a 3D vector as list or np.array containing the global translation axis of the joint in reference configuration
146 # useGlobalFrame: if False, the point and axis vectors are defined in the local coordinate system of body0
147 # show: if True, connector visualization is drawn
148 # axisRadius: radius of axis for connector graphical representation
149 # axisLength: length of axis for connector graphical representation
150 # color: color of connector
151#returns list [oJoint, mBody0, mBody1], containing the joint object number, and the two rigid body markers on body0/1 for the joint
152# mbs.CreatePrismaticJoint(bodyNumbers=[oGround, b1], position=[-0.5,0,0], axis=[1,0,0], #can move in global x-direction
153# useGlobalFrame=True, axisRadius=0.02, axisLength=1)
154
155
156#prepare mbs for simulation:
157mbs.Assemble()
158#some simulation parameters:
159simulationSettings = exu.SimulationSettings() #takes currently set values or default values
160simulationSettings.timeIntegration.numberOfSteps = 1000
161simulationSettings.timeIntegration.endTime = 5
162
163#for redundant constraints, the following two settings:
164simulationSettings.linearSolverSettings.ignoreSingularJacobian=True
165simulationSettings.linearSolverType = exu.LinearSolverType.EigenDense #use EigenSparse for larger systems alternatively
166
167mbs.SolveDynamic(simulationSettings)
168
169#visualize results after simulation:
170mbs.SolutionViewer()