rollingCoinTest.py
You can view and download this file on Github: rollingCoinTest.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN example
3#
4# Details: Rolling coin example;
5# examine example of Rill, Schaeffer, Grundlagen und Methodik der Mehrkörpersimulation, 2010, page 59
6# Note that in comparison to the literature, we use the local x-axis for the local axis of the coin, z is the normal to the plane
7# mass and inertia do not influence the results, as long as mass and inertia of a infinitely small ring are used
8# gravity is set to [0,0,-9.81m/s^2] and the radius is 0.01m
9#
10# Author: Johannes Gerstmayr
11# Date: 2020-06-19
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
17import exudyn as exu
18from exudyn.utilities import *
19
20import numpy as np
21
22useGraphics = True #without test
23#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24#you can erase the following lines and all exudynTestGlobals related operations if this is not intended to be used as TestModel:
25try: #only if called from test suite
26 from modelUnitTests import exudynTestGlobals #for globally storing test results
27 useGraphics = exudynTestGlobals.useGraphics
28except:
29 class ExudynTestGlobals:
30 pass
31 exudynTestGlobals = ExudynTestGlobals()
32#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
34SC = exu.SystemContainer()
35mbs = SC.AddSystem()
36
37phi0 = 1./180.*np.pi#initial nick angle of disc, 1 degree
38g = [0,0,-9.81] #gravity in m/s^2
39m = 1 #mass in kg
40r = 0.01 #radius of disc in m
41w = 0.001 #width of disc in m, just for drawing
42p0 = [r*np.sin(phi0),0,r*np.cos(phi0)] #origin of disc center point at reference, such that initial contact point is at [0,0,0]
43initialRotation = RotationMatrixY(phi0)
44
45omega0 = [0,0,1800/180*np.pi] #initial angular velocity around z-axis
46v0 = Skew(omega0) @ initialRotation @ [0,0,r] #initial angular velocity of center point
47#v0 = [0,0,0] #initial translational velocity
48#print("v0=",v0)#," = ", [0,10*np.pi*r*np.sin(phi0),0])
49
50#inertia for infinitely small ring:
51inertiaRing = RigidBodyInertia(mass=1, inertiaTensor= np.diag([0.5*m*r**2, 0.25*m*r**2, 0.25*m*r**2]))
52#print(inertiaRing)
53
54#additional graphics for visualization of rotation:
55graphicsBody = GraphicsDataOrthoCubePoint(centerPoint=[0,0,0],size=[w*1.1,0.7*r,0.7*r], color=color4lightred)
56
57[n0,b0]=AddRigidBody(mainSys = mbs,
58 inertia = inertiaRing,
59 nodeType = str(exu.NodeType.RotationEulerParameters),
60 position = p0,
61 rotationMatrix = initialRotation, #np.diag([1,1,1]),
62 angularVelocity = omega0,
63 velocity=v0,
64 gravity = g,
65 graphicsDataList = [graphicsBody])
66
67#ground body and marker
68gGround = GraphicsDataOrthoCubePoint(centerPoint=[0,0,-0.001],size=[0.12,0.12,0.002], color=color4lightgrey)
69oGround = mbs.AddObject(ObjectGround(visualization=VObjectGround(graphicsData=[gGround])))
70markerGround = mbs.AddMarker(MarkerBodyRigid(bodyNumber=oGround, localPosition=[0,0,0]))
71
72#markers for rigid body:
73markerBody0J0 = mbs.AddMarker(MarkerBodyRigid(bodyNumber=b0, localPosition=[0,0,0]))
74
75#rolling disc:
76oRolling=mbs.AddObject(ObjectJointRollingDisc(markerNumbers=[markerGround, markerBody0J0],
77 constrainedAxes=[1,1,1], discRadius=r,
78 visualization=VObjectJointRollingDisc(discWidth=w,color=color4blue)))
79
80
81#sensor for trace of contact point:
82if useGraphics:
83 sTrail=mbs.AddSensor(SensorObject(objectNumber=oRolling, storeInternal=True,#fileName='solution/rollingDiscTrail.txt',
84 outputVariableType = exu.OutputVariableType.Position))
85
86 sVel=mbs.AddSensor(SensorObject(objectNumber=oRolling, storeInternal=True,#fileName='solution/rollingDiscTrailVel.txt',
87 outputVariableType = exu.OutputVariableType.Velocity))
88
89
90
91mbs.Assemble()
92
93simulationSettings = exu.SimulationSettings() #takes currently set values or default values
94
95tEnd = 0.5
96if useGraphics:
97 tEnd = 0.5
98
99h=0.0005 #no visual differences for step sizes smaller than 0.0005
100
101simulationSettings.timeIntegration.numberOfSteps = int(tEnd/h)
102simulationSettings.timeIntegration.endTime = tEnd
103#simulationSettings.solutionSettings.solutionWritePeriod = 0.01
104simulationSettings.solutionSettings.sensorsWritePeriod = 0.0005
105#simulationSettings.timeIntegration.verboseMode = 1
106simulationSettings.solutionSettings.writeSolutionToFile = False
107
108simulationSettings.timeIntegration.generalizedAlpha.useIndex2Constraints = True
109simulationSettings.timeIntegration.generalizedAlpha.useNewmark = True
110simulationSettings.timeIntegration.generalizedAlpha.spectralRadius = 0.5
111simulationSettings.timeIntegration.generalizedAlpha.computeInitialAccelerations=True
112
113
114SC.visualizationSettings.nodes.show = True
115SC.visualizationSettings.nodes.drawNodesAsPoint = False
116SC.visualizationSettings.nodes.showBasis = True
117SC.visualizationSettings.nodes.basisSize = 0.015
118
119if useGraphics:
120 exu.StartRenderer()
121 mbs.WaitForUserToContinue()
122
123mbs.SolveDynamic(simulationSettings)
124
125p0=mbs.GetObjectOutput(oRolling, exu.OutputVariableType.Position)
126exu.Print('solution of rollingCoinTest=',p0[0]) #use x-coordinate
127
128exudynTestGlobals.testError = p0[0] - (0.002004099927340136) #2020-06-20: 0.002004099927340136; 2020-06-19: 0.002004099760845168 #4s looks visually similar to Rill, but not exactly ...
129exudynTestGlobals.testResult = p0[0]
130
131
132if useGraphics:
133 SC.WaitForRenderEngineStopFlag()
134 exu.StopRenderer() #safely close rendering window!
135
136 ##++++++++++++++++++++++++++++++++++++++++++++++q+++++++
137 #plot results
138 if True:
139
140
141 mbs.PlotSensor(sTrail, componentsX=[0],components=[1], closeAll=True, title='wheel trail')
142
143
144 # import matplotlib.pyplot as plt
145 # import matplotlib.ticker as ticker
146
147 # if True:
148 # data = np.loadtxt('solution/rollingDiscTrail.txt', comments='#', delimiter=',')
149 # plt.plot(data[:,1], data[:,2], 'r-',label='contact point trail') #x/y coordinates of trail
150 # else:
151 # #show trail velocity computed numerically and from sensor:
152 # data = np.loadtxt('solution/rollingDiscTrail.txt', comments='#', delimiter=',')
153
154 # nData = len(data)
155 # vVec = np.zeros((nData,2))
156 # dt = data[1,0]-data[0,0]
157 # for i in range(nData-1):
158 # vVec[i+1,0:2] = 1/dt*(data[i+1,1:3]-data[i,1:3])
159
160 # plt.plot(data[:,0], vVec[:,0], 'r-',label='contact point vel x')
161 # plt.plot(data[:,0], vVec[:,1], 'k-',label='contact point vel y')
162 # plt.plot(data[:,0], (vVec[:,0]**2+vVec[:,1]**2)**0.5, 'g-',label='|contact point vel|')
163
164 # trailVel = np.loadtxt('solution/rollingDiscTrailVel.txt', comments='#', delimiter=',')
165 # plt.plot(data[:,0], trailVel[:,1], 'r--',label='trail vel x')
166 # plt.plot(data[:,0], trailVel[:,2], 'k--',label='trail vel y')
167 # plt.plot(data[:,0], trailVel[:,3], 'y--',label='trail vel z')
168 # plt.plot(data[:,0], (trailVel[:,1]**2+trailVel[:,2]**2)**0.5, 'b--',label='|trail vel|')
169
170 # ax=plt.gca() # get current axes
171 # ax.grid(True, 'major', 'both')
172 # ax.xaxis.set_major_locator(ticker.MaxNLocator(10)) #use maximum of 8 ticks on y-axis
173 # ax.yaxis.set_major_locator(ticker.MaxNLocator(10)) #use maximum of 8 ticks on y-axis
174 # plt.tight_layout()
175 # plt.legend()
176 # plt.show()