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, graphicsDataUtilities and rigidBodyUtilities
 46import numpy as np
 47SC = exu.SystemContainer()
 48mbs = SC.AddSystem() #create a MainSystem 'mbs' to work with
 49
 50#graphics data for checkerboard background (not required):
 51gGround0 = GraphicsDataCheckerBoard(point=[3,0,-2], normal=[0,0,1], size=10)
 52#add ground object and background graphics; visualization is optional
 53oGround = mbs.CreateGround(graphicsDataList=[gGround0])
 54
 55#create a cube with length L (X-direction), height H (Y) and width W (Z)
 56L=1
 57H=0.2
 58W=0.1
 59#for visualization of the cube, we define a graphics object in the following
 60graphicsCube = GraphicsDataOrthoCubePoint(centerPoint = [0,0,0], #note that centerPoint is in the local coordinate system; IN MOST CASES THIS MUST BE AT [0,0,0]
 61                                          size=[L,H,W], color=color4orange)
 62#SUMMARIZING: graphicsCube usually should have centerPoint=[0,0,0] if used in the CreateRigidBody
 63#define the inertia of this cube using InertiaCuboid with density and cube dimensions; computes internally mass, COM, and inertia tensor:
 64inertiaCube = InertiaCuboid(density=5000, sideLengths=[L,H,W])
 65
 66#create simple rigid body
 67#note that graphics is always attached to reference point of body, which is by default the COM
 68#graphicsCube is added to reference point of the rigid body, here it is equal to the center of mass (COM):
 69b0 = mbs.CreateRigidBody(inertia = inertiaCube,
 70                         referencePosition = [0.5*L,0,0], #reference position x/y/z of COM
 71                         referenceRotationMatrix=RotationVector2RotationMatrix([0,0,pi*0.5]),
 72                         initialAngularVelocity=[2,0,0],
 73                         initialVelocity=[0,4,0],
 74                         gravity = [0,-9.81,0],
 75                         graphicsDataList = [graphicsCube])
 76
 77#add an load with user function:
 78def UFforce(mbs, t, loadVector):
 79    #define time-dependent function:
 80    return (10+5*np.sin(t*10*2*pi))*np.array(loadVector)
 81
 82#add an load with 10N in x-direction to rigid body at marker position
 83#add user function to modify load in time
 84mbs.CreateForce(bodyNumber=b0,
 85                localPosition=[-0.5*L,0,0],
 86                loadVector=[10,0,0],
 87                loadVectorUserFunction=UFforce)
 88
 89#add torque to rigid body at left end
 90mbs.CreateTorque(bodyNumber=b0, localPosition=[0.5,0,0],
 91                loadVector=[0,1,0]) #torque of 1N around y-axis
 92
 93#create a rigid distance between local position of bodies (or ground) or between nodes
 94mbs.CreateDistanceConstraint(bodyOrNodeList=[oGround, b0],
 95                             localPosition0 = [ 0. ,0,0],
 96                             localPosition1 = [-0.5,0,0],
 97                             distance=None, #automatically computed
 98                             drawSize=0.06)
 99
100#geometrical parameters of two further bodies
101a=1
102b=2
103xOff = 1 #offset in x-direction for first body
104yOff =-0.5 #offset in y-direction of first body
105
106#create a second graphics object
107graphicsCube2 = GraphicsDataOrthoCubePoint(centerPoint = [0,0,0],
108                                          size=[a,b,0.1], color=color4blue)
109inertiaCube2 = InertiaCuboid(density=5000, sideLengths=[a,b,0.1])
110
111#create another rigid body with other dimensions
112b1 = mbs.CreateRigidBody(inertia = inertiaCube2,
113                          referencePosition = [xOff+0.5*a,yOff-0.5*b,0], #reference position of body [X,Y,Z]
114                          gravity = [0,-9.81,0],
115                          graphicsDataList = [graphicsCube2])
116
117#create another rigid body with same dimensions as b1
118b2 = mbs.CreateRigidBody(inertia = inertiaCube2,
119                          referencePosition = [xOff+0.5*a+a,yOff-0.5*b-b,0], #reference position of body [X,Y,Z]
120                          gravity = [0,-9.81,0],
121                          graphicsDataList = [graphicsCube2])
122
123#create revolute joint with following args:
124    # name: name string for joint; markers get Marker0:name and Marker1:name
125    # bodyNumbers: a list of object numbers for body0 and body1; must be rigid body or ground object
126    # 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
127    # 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
128    # show: if True, connector visualization is drawn
129    # axisRadius: radius of axis for connector graphical representation
130    # axisLength: length of axis for connector graphical representation
131    # color: color of connector
132#CreateRevoluteJoint returns list [oJoint, mBody0, mBody1], containing the joint object number, and the two rigid body markers on body0/1 for the joint
133#(global reference) position of joint must be related to local size of rigid bodies
134mbs.CreateRevoluteJoint(bodyNumbers=[oGround, b1], position=[xOff,yOff,0], axis=[0,0,1], #rotation along global z-axis
135                        useGlobalFrame=True, axisRadius=0.02, axisLength=0.14)
136
137mbs.CreateRevoluteJoint(bodyNumbers=[b1, b2], position=[xOff+a,yOff-b,0], axis=[0,0,1], #rotation along global z-axis
138                        useGlobalFrame=True, axisRadius=0.02, axisLength=0.14)
139
140#create prismatic joint with following args:
141    # name: name string for joint; markers get Marker0:name and Marker1:name
142    # bodyNumbers: a list of object numbers for body0 and body1; must be rigid body or ground object
143    # 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
144    # axis: a 3D vector as list or np.array containing the global translation axis of the joint in reference configuration
145    # useGlobalFrame: if False, the point and axis vectors are defined in the local coordinate system of body0
146    # show: if True, connector visualization is drawn
147    # axisRadius: radius of axis for connector graphical representation
148    # axisLength: length of axis for connector graphical representation
149    # color: color of connector
150#returns list [oJoint, mBody0, mBody1], containing the joint object number, and the two rigid body markers on body0/1 for the joint
151# mbs.CreatePrismaticJoint(bodyNumbers=[oGround, b1], position=[-0.5,0,0], axis=[1,0,0], #can move in global x-direction
152#                          useGlobalFrame=True, axisRadius=0.02, axisLength=1)
153
154
155#prepare mbs for simulation:
156mbs.Assemble()
157#some simulation parameters:
158simulationSettings = exu.SimulationSettings() #takes currently set values or default values
159simulationSettings.timeIntegration.numberOfSteps = 1000
160simulationSettings.timeIntegration.endTime = 5
161
162#for redundant constraints, the following two settings:
163simulationSettings.linearSolverSettings.ignoreSingularJacobian=True
164simulationSettings.linearSolverType = exu.LinearSolverType.EigenDense #use EigenSparse for larger systems alternatively
165
166mbs.SolveDynamic(simulationSettings)
167
168#visualize results after simulation:
169mbs.SolutionViewer()