rigidBodyCOMtest.py
You can view and download this file on Github: rigidBodyCOMtest.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN example
3#
4# Details: Test rigid body formulation for different center of mass (COM)
5#
6# Author: Johannes Gerstmayr
7# Date: 2020-04-22
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.utilities import *
15from exudyn.FEM import *
16
17import numpy as np
18
19useGraphics = True #without test
20#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21#you can erase the following lines and all exudynTestGlobals related operations if this is not intended to be used as TestModel:
22try: #only if called from test suite
23 from modelUnitTests import exudynTestGlobals #for globally storing test results
24 useGraphics = exudynTestGlobals.useGraphics
25except:
26 class ExudynTestGlobals:
27 pass
28 exudynTestGlobals = ExudynTestGlobals()
29#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
31SC = exu.SystemContainer()
32mbs = SC.AddSystem()
33
34nBodies = 2
35color = [0.1,0.1,0.8,1]
36s = 0.1 #width of cube
37sx = 3*s #length of cube/body
38cPosZ = 0. #offset of constraint in z-direction
39zz = sx * (nBodies+1)*2 #max size of background
40
41background0 = GraphicsDataRectangle(-zz,-zz,zz,2.5*sx,color)
42oGround=mbs.AddObject(ObjectGround(referencePosition= [0,0,0],
43 visualization=VObjectGround(graphicsData= [background0])))
44
45m=25
46inertia=np.array([[10,1,2],
47 [ 1,7,3],
48 [ 2,3,6]])
49
50nodeList=[]
51objectList=[]
52for case in range(2):
53 nRB=-1
54 if case == 0:
55 com=[0,0,0]
56 else:
57 #com=[0.4,0.6,1.3]
58 com=[0.4,0.22,-0.35]
59 zOff = 0.5*case*0
60
61 RBinertia = RigidBodyInertia(mass=m, inertiaTensor=inertia)
62 #exu.Print("RBinertia orig =", RBinertia)
63 RBinertia = RBinertia.Translated(com) #this includes the correct terms in inertia
64
65 if NormL2(RBinertia.com) != 0 and i==1:
66 exu.Print("AddRigidBody COM=", RBinertia.com)
67 exu.Print("inertia6D=", RBinertia.GetInertia6D())
68 #exu.Print("RBinertia trans=", RBinertia)
69 #exu.Print("inertia6D=", RBinertia.GetInertia6D())
70 #exu.Print("inertia.com=", RBinertia.com)
71 oRBlast = oGround
72
73 #create a chain of bodies:
74 for i in range(nBodies):
75 omega0 = [0,0,0] #arbitrary initial angular velocity
76
77 #Rotxyz:
78 #ep0 = [0,0,0]
79 #ep_t0 = [0,0,0]
80
81 p0 = VSub([i*2*sx+sx,0.,zOff],com) #reference position
82 v0 = [0.,0.,0.] #initial translational velocity
83
84 color=[0.8,0.1,0.1,1]
85 if case==0:
86 color=[0.1,0.1,0.8,1]
87
88 oGraphics = GraphicsDataOrthoCubeLines(-sx+com[0],-s+com[1],-s+com[2], sx+com[0],s+com[1],s+com[2], color)
89 d=0.02
90 oGraphicsCOM = GraphicsDataOrthoCubeLines(-d+com[0],-d+com[1],-d+com[2], d+com[0],d+com[1],d+com[2], [0.1,0.8,0.1,1])
91
92 rDict = mbs.CreateRigidBody(inertia=RBinertia,
93 referencePosition=p0,
94 initialVelocity=v0,initialAngularVelocity=omega0,
95 gravity=[0.,-9.81,0.],
96 graphicsDataList=[oGraphics,oGraphicsCOM],returnDict=True)
97 oRB = rDict['bodyNumber']
98 nRB = rDict['nodeNumber']
99
100 val=0
101 if i==0: val=1
102 mbs.CreateGenericJoint(bodyNumbers=[oRB, oRBlast], position=VAdd([-sx,0.,0],com),
103 constrainedAxes=[1,1,1, val,val,0], useGlobalFrame=False)
104
105 #for next chain body
106 oRBlast = oRB
107
108 sCoords=mbs.AddSensor(SensorNode(nodeNumber=nRB, storeInternal=True,#fileName="solution/sensor"+str(case)+".txt",
109 outputVariableType=exu.OutputVariableType.Coordinates))
110 nodeList += [nRB]
111 objectList += [oRB]
112
113mbs.Assemble()
114#exu.Print(mbs)
115
116simulationSettings = exu.SimulationSettings() #takes currently set values or default values
117
118fact = 100
119simulationSettings.timeIntegration.numberOfSteps = 1*fact
120simulationSettings.timeIntegration.endTime = 0.01*fact
121simulationSettings.solutionSettings.solutionWritePeriod = simulationSettings.timeIntegration.endTime/1000
122simulationSettings.timeIntegration.verboseMode = 1
123simulationSettings.solutionSettings.writeSolutionToFile = False
124
125simulationSettings.timeIntegration.newton.useModifiedNewton = True
126simulationSettings.timeIntegration.generalizedAlpha.useIndex2Constraints = True
127simulationSettings.timeIntegration.generalizedAlpha.useNewmark = True
128simulationSettings.timeIntegration.generalizedAlpha.spectralRadius = 0.6 #0.6 works well
129
130simulationSettings.solutionSettings.solutionInformation = "rigid body tests"
131SC.visualizationSettings.nodes.defaultSize = 0.025
132SC.visualizationSettings.nodes.drawNodesAsPoint = False
133SC.visualizationSettings.nodes.showBasis = True
134
135#simulationSettings.displayComputationTime = True
136#simulationSettings.displayStatistics = True
137
138
139if useGraphics:
140 exu.StartRenderer()
141 mbs.WaitForUserToContinue()
142
143mbs.SolveDynamic(simulationSettings)
144
145
146
147p0=mbs.GetObjectOutputBody(objectList[0], exu.OutputVariableType.Displacement, mbs.GetObject(objectList[0])['physicsCenterOfMass'])
148#exu.Print("p0=", p0)
149p1=mbs.GetObjectOutputBody(objectList[1], exu.OutputVariableType.Displacement, mbs.GetObject(objectList[1])['physicsCenterOfMass'])
150#exu.Print("p1=", p1)
151
152#exu.Print("p0-p1=", p0-p1)
153#convergence of two formulations (difference due to time integration):
154#h=0.001: p0-p1= [ 2.89037808e-06 -4.38559926e-07 4.83240595e-07] #similar results for Rxyz parameterization
155#h=0.0001: p0-p1= [ 2.88781241e-08 -4.40013365e-09 5.24721844e-09]
156#h=0.00001:p0-p1= [ 2.64592348e-10 -5.90557048e-11 4.66975986e-10]
157
158#+++++++++++++++++++++++++++++++++++++++++++++
159u=NormL2(p0) + NormL2(p1)
160exu.Print('solution of rigidBodyCOMtest=',u)
161
162exudynTestGlobals.testError = u - (3.409431467726293) #2020-04-22: 3.409431467726293
163exudynTestGlobals.testResult = u
164
165
166if useGraphics:
167 SC.WaitForRenderEngineStopFlag()
168 exu.StopRenderer() #safely close rendering window!