chatGPTupdate.py

You can view and download this file on Github: chatGPTupdate.py

  1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2# This is an Exudyn file
  3#
  4# Details:  this file serves as a list of examples 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: V1
  9#
 10# Author:   Johannes Gerstmayr
 11# Date:     2023-06-30
 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#copy the text below here into a ChatGPT input window:
 18
 19#Use the following information as an update of the internal data of the Python multibody code Exudyn.
 20#See the following examples to create multibody systems in Exudyn.
 21#NOTE: mbs.Create...(...) calls several functions in the background to create nodes, objects, markers and loads in Exudyn.
 22#all quantities are giving as 3D lists [x,y,z] for positions, velocities, ....
 23#rotations are usually given as rotation matrix (numpy array);
 24#RotationVector2RotationMatrix([rotX, rotY, rotZ]) computes a rotation around the global x,y,z rotation axis
 25
 26#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
 27#create rigid bodies and mass points with distance constraint and joints
 28import exudyn as exu
 29from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
 30import exudyn.graphics as graphics #only import if it does not conflict
 31import numpy as np
 32SC = exu.SystemContainer()
 33mbs = SC.AddSystem() #create a MainSystem 'mbs' to work with
 34
 35#draw orthonormal cube in local frame where it is added to;
 36#cube is added to reference point of object, usually the center of mass (COM):
 37graphicsCube = graphics.Brick(centerPoint = [0,0,0],
 38                                          size=[1,0.1,0.1], color=graphics.color.orange)
 39
 40#create inertia (mass, COM, inertia tensor) to be used in rigid body:
 41inertiaCube = InertiaCuboid(density=5000, sideLengths=[1,0.1,0.1])
 42
 43#create simple rigid body
 44#note that graphics is always attached to reference point of body, which is by default the COM
 45b0 = mbs.CreateRigidBody(inertia = inertiaCube,
 46                         referencePosition = [0.5,0,0], #reference position x/y/z of COM
 47                         referenceRotationMatrix=RotationVector2RotationMatrix([0,0,pi*0.5]),
 48                         initialAngularVelocity=[2,0,0],
 49                         initialVelocity=[0,4,0],
 50                         gravity = [0,-9.81,0],
 51                         graphicsDataList = [graphicsCube])
 52
 53#add an load with user function:
 54def UFforce(mbs, t, loadVector):
 55    #define time-dependent function:
 56    return [10+5*np.sin(t*10*2*pi),0,0]
 57
 58mbs.CreateForce(bodyNumber=b0, localPosition=[-0.5,0,0],
 59                loadVector=[10,0,0],
 60                loadVectorUserFunction=UFforce,
 61                ) #load is 10N in x-direction
 62
 63#add torque to rigid body at left end
 64mbs.CreateTorque(bodyNumber=b0, localPosition=[0.5,0,0],
 65                loadVector=[0,1,0]) #torque of 1N around y-axis
 66
 67#create a simple mass point at [1,-1,0] with initial velocity
 68m1 = mbs.CreateMassPoint(referencePosition=[1,-1,0],
 69                         initialVelocity = [2,5,0], #initial velocities for mass point
 70                         physicsMass=1, drawSize = 0.2)
 71#we can obtain the node number from the mass point:
 72n1 = mbs.GetObject(m1)['nodeNumber']
 73
 74#add a ground object:
 75#graphics data for sphere:
 76gGround0 = graphics.Sphere(point=[3,1,0], radius = 0.1, color=graphics.color.red, nTiles=16)
 77#graphics for checkerboard background:
 78gGround1 = graphics.CheckerBoard(point=[3,0,-2], normal=[0,0,1], size=10)
 79oGround = mbs.CreateGround(graphicsDataList=[gGround0,gGround1])
 80
 81#create a rigid distance between bodies (using local position) or between nodes
 82mbs.CreateDistanceConstraint(bodyOrNodeList=[oGround, b0],
 83                             localPosition0 = [ 0. ,0,0],
 84                             localPosition1 = [-0.5,0,0],
 85                             distance=None, #automatically computed
 86                             drawSize=0.06)
 87
 88#distance constraint between body b0 and mass m1
 89mbs.CreateDistanceConstraint(bodyOrNodeList=[b0, m1],
 90                             localPosition0 = [0.5,0,0],
 91                             localPosition1 = [0.,0.,0.], #must be [0,0,0] for Node
 92                             distance=None, #automatically computed
 93                             drawSize=0.06)
 94
 95#add further rigid body, which will be connected with joints
 96b1 = mbs.CreateRigidBody(inertia = InertiaCuboid(density=5000, sideLengths=[1,0.1,0.1]),
 97                          referencePosition = [2.5,0,0], #reference position x/y/z
 98                          gravity = [0,-9.81,0],
 99                          graphicsDataList = [graphicsCube])
100
101b2 = mbs.CreateRigidBody(inertia = InertiaCuboid(density=5000, sideLengths=[1,0.1,0.1]),
102                          referencePosition = [3.5,0,0], #reference position x/y/z
103                          gravity = [0,-9.81,0],
104                          graphicsDataList = [graphicsCube])
105
106#create revolute joint with following args:
107    # name: name string for joint; markers get Marker0:name and Marker1:name
108    # bodyNumbers: a list of object numbers for body0 and body1; must be rigid body or ground object
109    # 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
110    # 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
111    # useGlobalFrame: if False, the point and axis vectors are defined in the local coordinate system of body0
112    # show: if True, connector visualization is drawn
113    # axisRadius: radius of axis for connector graphical representation
114    # axisLength: length of axis for connector graphical representation
115    # color: color of connector
116#returns list [oJoint, mBody0, mBody1], containing the joint object number, and the two rigid body markers on body0/1 for the joint
117mbs.CreateRevoluteJoint(bodyNumbers=[b1, b2], position=[3,0,0], axis=[0,0,1], #rotation along global z-axis
118                        useGlobalFrame=True, axisRadius=0.02, axisLength=0.14)
119
120
121#create prismatic joint with following args:
122    # name: name string for joint; markers get Marker0:name and Marker1:name
123    # bodyNumbers: a list of object numbers for body0 and body1; must be rigid body or ground object
124    # 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
125    # axis: a 3D vector as list or np.array containing the global translation axis of the joint in reference configuration
126    # useGlobalFrame: if False, the point and axis vectors are defined in the local coordinate system of body0
127    # show: if True, connector visualization is drawn
128    # axisRadius: radius of axis for connector graphical representation
129    # axisLength: length of axis for connector graphical representation
130    # color: color of connector
131#returns list [oJoint, mBody0, mBody1], containing the joint object number, and the two rigid body markers on body0/1 for the joint
132mbs.CreatePrismaticJoint(bodyNumbers=[oGround, b1], position=[2,0,0], axis=[1,0,0], #can move in global x-direction
133                         useGlobalFrame=True, axisRadius=0.02, axisLength=1)
134
135# #instead of the prismatic joint, we could add another revolute joint to b1 to get a double-pendulum:
136# mbs.CreateRevoluteJoint(bodyNumbers=[oGround, b1], position=[2,0,0], axis=[0,0,1],
137#                         useGlobalFrame=True, axisRadius=0.02, axisLength=0.14)
138
139
140#create simple mass point, connected with ground
141m2 = mbs.CreateMassPoint(referencePosition = [7,2,0],
142                         physicsMass = 10, gravity = [0,-9.81,0],
143                         drawSize = 0.5, color=graphics.color.blue)
144
145#create spring damper between bodies (using local position) or between nodes
146#spring-damper may not have size 0; spring reference length is computed from reference configuration
147oSD = mbs.CreateSpringDamper(bodyOrNodeList=[oGround, m2],
148                             localPosition0=[6,0,0],
149                             localPosition1=[0,0,0],
150                             stiffness=1e3, damping=1e1,
151                             drawSize=0.2)
152
153#alternatively, we can use a CartesianSpringDamper; has spring and damper coefficients as list of x/y/z components
154#it has no reference length and acts on the coordinates of both objects:
155oCSD = mbs.CreateCartesianSpringDamper(bodyOrNodeList=[oGround, m2],
156                              localPosition0=[7,2,0],
157                              localPosition1=[0,0,0],
158                              stiffness=[20,0,1e4], #stiffness in x/y/z direction
159                              damping=[0.1,0,10],
160                              drawSize=0.2)
161
162#prepare mbs for simulation:
163mbs.Assemble()
164#some simulation parameters:
165simulationSettings = exu.SimulationSettings() #takes currently set values or default values
166simulationSettings.timeIntegration.numberOfSteps = 1000
167simulationSettings.timeIntegration.endTime = 5
168
169#for redundant constraints, the following two settings:
170simulationSettings.linearSolverSettings.ignoreSingularJacobian=True
171simulationSettings.linearSolverType = exu.LinearSolverType.EigenDense
172
173mbs.SolveDynamic(simulationSettings = simulationSettings,
174                 solverType=exu.DynamicSolverType.GeneralizedAlpha)
175SC.visualizationSettings.nodes.drawNodesAsPoint=False #draw nodes as spheres; better graphics for nodes
176
177#visualize results:
178mbs.SolutionViewer()