rigid3Dexample.py
You can view and download this file on Github: rigid3Dexample.py
1#%%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN example
3#
4# Details: A chain of 3D rigid bodies is simulated rigid bodies are connected via spring-dampers (no joints!)
5#
6# Author: Johannes Gerstmayr
7# Date: 2019-11-15
8#
9# 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.
10#
11#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12
13import exudyn as exu
14from exudyn.itemInterface import *
15from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
16import exudyn.graphics as graphics #only import if it does not conflict
17
18SC = exu.SystemContainer()
19mbs = SC.AddSystem()
20
21print('EXUDYN version='+exu.GetVersionString())
22
23#%%+++++++++++++++++++++++++++++++++++
24#background
25#rect = [-0.1,-0.1,0.1,0.1] #xmin,ymin,xmax,ymax
26#background0 = {'type':'Line', 'color':[0.1,0.1,0.8,1], 'data':[rect[0],rect[1],0, rect[2],rect[1],0, rect[2],rect[3],0, rect[0],rect[3],0, rect[0],rect[1],0]} #background
27color = [0.1,0.1,0.8,1]
28zz = 2*3 #max size
29s = 0.1 #size of cube
30sx = 3*s #x-size
31cPosZ = 0.1 #offset of constraint in z-direction
32
33#create background, in order to have according zoom all
34# background0 = GraphicsDataRectangle(-zz,-2*zz,zz,zz,graphics.color.white)
35background0 = graphics.CheckerBoard(point=[0,-0.5*zz,-0.25*zz],size=6*zz)
36oGround=mbs.CreateGround(referencePosition= [0,0,0],
37 graphicsDataList = [background0])
38mPosLast = mbs.AddMarker(MarkerBodyPosition(bodyNumber = oGround,
39 localPosition=[-2*sx,0,cPosZ]))
40
41#%%+++++++++++++++++++++++++++++++++++
42#create a chain of bodies:
43for i in range(20):
44 #print("Build Object", i)
45 f = 0 #factor for initial velocities
46 omega0 = [0,50.*f,20*f] #arbitrary initial angular velocity
47 ep0 = eulerParameters0 #no rotation
48
49 ep_t0 = AngularVelocity2EulerParameters_t(omega0, ep0)
50
51 p0 = [-sx+i*2*sx,0.,0] #reference position
52 v0 = [0.2*f,0.,0.] #initial translational velocity
53
54 nRB = mbs.AddNode(NodeRigidBodyEP(referenceCoordinates=p0+ep0,
55 initialVelocities=v0+list(ep_t0)))
56 oGraphicsLines = GraphicsDataOrthoCubeLines(-0.9*sx,-s,-s, 0.9*sx,s,s, color=graphics.color.black)
57 oGraphics = graphics.Brick(size=[1.8*sx, 2*s, 2*s], color= graphics.color.dodgerblue)
58 oGraphicsJoint = graphics.Sphere(point=[-sx,0,cPosZ], radius = 0.6*s, color=graphics.color.darkgrey, nTiles=24)
59 oRB = mbs.AddObject(ObjectRigidBody(physicsMass=2,
60 physicsInertia=[6,1,6,0,0,0],
61 nodeNumber=nRB,
62 visualization=VObjectRigidBody(graphicsData=[oGraphics, oGraphicsJoint, oGraphicsLines])))
63
64 mMassRB = mbs.AddMarker(MarkerBodyMass(bodyNumber = oRB))
65 mbs.AddLoad(Gravity(markerNumber = mMassRB, loadVector=[0.,-9.81,0.])) #gravity in negative z-direction
66
67 k = 1e7
68 d=0.01*k
69 mPos = mbs.AddMarker(MarkerBodyPosition(bodyNumber = oRB, localPosition = [-sx,0.,cPosZ]))
70 mbs.AddObject(ObjectConnectorCartesianSpringDamper(markerNumbers = [mPosLast, mPos],
71 stiffness=[k,k,k], damping=[d,d,d])) #gravity in negative z-direction
72 mPosLast = mbs.AddMarker(MarkerBodyPosition(bodyNumber = oRB, localPosition = [sx,0.,cPosZ]))
73
74#%%+++++++++++++++++++++++++++++++++++
75mbs.Assemble()
76print(mbs)
77
78simulationSettings = exu.SimulationSettings() #takes currently set values or default values
79
80fact = 20000 #10000
81simulationSettings.timeIntegration.numberOfSteps = 1*fact
82simulationSettings.timeIntegration.endTime = 0.001*fact*0.5*4
83simulationSettings.solutionSettings.solutionWritePeriod = simulationSettings.timeIntegration.endTime/fact*10
84simulationSettings.timeIntegration.verboseMode = 1
85
86simulationSettings.timeIntegration.newton.useModifiedNewton = True
87simulationSettings.timeIntegration.generalizedAlpha.spectralRadius = 0.6 #0.6 works well
88simulationSettings.linearSolverType = exu.LinearSolverType.EigenSparse
89
90simulationSettings.solutionSettings.solutionInformation = "rigid body tests"
91SC.visualizationSettings.nodes.defaultSize = 0.05
92SC.visualizationSettings.openGL.multiSampling = 4
93SC.visualizationSettings.openGL.lineWidth = 2
94
95# uncomment following line for shadow:
96# SC.visualizationSettings.openGL.shadow = 0.5
97# SC.visualizationSettings.openGL.light0position = [4,4,10,0]
98
99exu.StartRenderer()
100mbs.WaitForUserToContinue()
101
102mbs.SolveDynamic(simulationSettings)
103
104SC.WaitForRenderEngineStopFlag()
105exu.StopRenderer() #safely close rendering window!