generalContactCylinderTrigsTest.py
You can view and download this file on Github: generalContactCylinderTrigsTest.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN example
3#
4# Details: model with GeneralContact of a cylinder modelled by triangles rolling on a bed of spherical markers
5#
6# Author: Johannes Gerstmayr
7# Date: 2024-03-16
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 * #includes itemInterface and rigidBodyUtilities
15import exudyn.graphics as graphics
16import numpy as np
17
18testIsActive = exu.sys.get('testIsActive', False)
19
20SC = exu.SystemContainer()
21mbs = SC.AddSystem()
22
23#create an environment for mini example
24
25gravity = [0,0,-9.81]
26
27p0 = np.array([0,0,0])
28planeL = 0.3
29xFact = 5
30nPlane = 6 #spheres along x and y
31rMarker = 0.02 #radius of spheres for contact points
32
33rCyl = 0.25
34tCyl = 0.25
35
36sRad = 0.02
37k = 2e4
38d = 0.0005*k
39stepSize = 1e-3
40
41frictionCoeff = 0.1
42ss = 10
43
44markerList = []
45radiusList = []
46gDataList = []
47
48
49gContact = mbs.AddGeneralContact()
50# gContact.verboseMode = 1
51gContact.SetFrictionPairings(frictionCoeff*np.eye(1))
52gContact.SetSearchTreeCellSize(numberOfCells=[ss,ss,ss])
53# gContact.SetSearchTreeBox(pMin=np.array([-0.5*planeL,-0.5*planeL,-0.1]),
54# pMax=np.array([0.5*planeL,0.5*planeL,0.1]))
55#print('treesize=',ssx*ssx*ssy)
56#gContact.sphereSphereContact = False #does not influence results (only performance)!
57
58
59#%% ground
60gFloor = graphics.CheckerBoard(p0+[planeL*xFact*0.5,0,0],size=planeL*xFact, size2=planeL, nTiles=8)
61oGround=mbs.AddObject(ObjectGround(referencePosition= [0,0,0],
62 visualization=VObjectGround(graphicsData=[gFloor])))
63
64
65# nGround = mbs.AddNode(NodePointGround(referenceCoordinates=[0,0,0] ))
66# mGround = mbs.AddMarker(MarkerNodeRigid(nodeNumber=nGround))
67
68gCyl = graphics.Cylinder(pAxis=[0,-0.5*tCyl,0],vAxis=[0,tCyl,0], radius=rCyl,
69 color=[0.3,0.3,0.3,1],
70 alternatingColor=graphics.color.lightgrey,nTiles=64)
71
72
73#rigid body containing sphere markers:
74omegaY = 10
75bCyl=mbs.CreateRigidBody(referencePosition=[0,0,rCyl*1.0],
76 initialVelocity=[omegaY*rCyl,0,0],
77 #initialAngularVelocity=[0,omegaY,0],
78 # referenceRotationMatrix=RotationMatrixX(0.1),
79 inertia=InertiaCylinder(200, tCyl, rCyl, 1),
80 gravity = gravity,
81 nodeType = exu.NodeType.RotationRotationVector,
82 graphicsDataList=[gCyl],
83 )
84mCyl = mbs.AddMarker(MarkerBodyRigid(bodyNumber=bCyl, localPosition=[0,0,0]))
85sPos = mbs.AddSensor(SensorBody(bodyNumber=bCyl, storeInternal=True,
86 outputVariableType=exu.OutputVariableType.Position))
87sRot = mbs.AddSensor(SensorBody(bodyNumber=bCyl, storeInternal=True,
88 outputVariableType=exu.OutputVariableType.Rotation))
89sVel = mbs.AddSensor(SensorBody(bodyNumber=bCyl, storeInternal=True,
90 outputVariableType=exu.OutputVariableType.Velocity))
91sOmega = mbs.AddSensor(SensorBody(bodyNumber=bCyl, storeInternal=True,
92 outputVariableType=exu.OutputVariableType.AngularVelocity))
93
94[meshPoints, meshTrigs] = graphics.ToPointsAndTrigs(gCyl)
95
96
97gContact.AddTrianglesRigidBodyBased(rigidBodyMarkerIndex=mCyl,
98 contactStiffness=k, contactDamping=d, frictionMaterialIndex=0,
99 pointList=meshPoints, triangleList=meshTrigs)
100
101
102
103for ix in range(nPlane*xFact+1):
104 for iy in range(nPlane+1):
105 x = (ix/nPlane-0.0)*planeL
106 y = (iy/nPlane-0.5)*planeL
107 z = -rMarker
108
109 m = mbs.AddMarker(MarkerBodyRigid(bodyNumber=oGround, localPosition=[x,y,z]))
110 gContact.AddSphereWithMarker(m, radius=rMarker, contactStiffness=k, contactDamping=d,
111 frictionMaterialIndex=0)
112
113
114
115
116
117mbs.Assemble()
118
119items=gContact.GetItemsInBox(pMin=[-4,-4,0], pMax=[4,4,20])
120# print('n spheres=',len(items['MarkerBasedSpheres']))
121
122
123tEnd = 1
124simulationSettings = exu.SimulationSettings()
125simulationSettings.solution.file.write = False
126simulationSettings.timeIntegration.verboseMode = 1
127#simulationSettings.parallel.numberOfThreads = 4
128
129SC.visualizationSettings.general.graphicsUpdateInterval=0.02
130SC.visualizationSettings.view0.scene.drawCoordinateSystem=True
131SC.visualizationSettings.loads.show=False
132SC.visualizationSettings.bodies.show=True
133SC.visualizationSettings.markers.show=False
134
135SC.visualizationSettings.nodes.show=True
136SC.visualizationSettings.nodes.drawNodesAsPoint = False
137SC.visualizationSettings.nodes.defaultSize = 0 #must not be -1, otherwise uses autocomputed size
138SC.visualizationSettings.nodes.tiling = 4
139
140SC.visualizationSettings.view0.window.renderWindowSize=[1900,1200]
141SC.visualizationSettings.openGL.multiSampling = 4
142#improved OpenGL rendering
143
144SC.visualizationSettings.contact.showSpheres = True
145SC.visualizationSettings.contact.tilingSpheres = 4
146
147
148if not testIsActive:
149 SC.renderer.Start()
150 SC.renderer.RestoreSavedState()
151 # SC.renderer.DoIdleTasks()
152
153
154simulationSettings.timeIntegration.numberOfSteps = int(tEnd/stepSize)
155simulationSettings.timeIntegration.endTime = tEnd
156mbs.SolveDynamic(simulationSettings, solverType=exu.DynamicSolverType.ExplicitEuler)
157
158if not testIsActive:
159 #SC.renderer.DoIdleTasks()
160 SC.renderer.Stop() #safely close rendering window!
161
162#%%+++++++++++++++++++
163q = mbs.GetSensorValues(sPos)
164q += mbs.GetSensorValues(sVel)
165q += mbs.GetSensorValues(sOmega)
166q += mbs.GetSensorValues(sRot)
167#print('q=', q)
168
169u = np.linalg.norm(q)
170exu.Print('solution of generalContactCylinderTest =',u)
171
172exu.sys['testResult'] = u