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 *
 15import numpy as np
 16
 17useGraphics = True #without test
 18#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 19#you can erase the following lines and all exudynTestGlobals related operations if this is not intended to be used as TestModel:
 20try: #only if called from test suite
 21    from modelUnitTests import exudynTestGlobals #for globally storing test results
 22    useGraphics = exudynTestGlobals.useGraphics
 23except:
 24    class ExudynTestGlobals:
 25        pass
 26    exudynTestGlobals = ExudynTestGlobals()
 27#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 28
 29SC = exu.SystemContainer()
 30mbs = SC.AddSystem()
 31
 32#create an environment for mini example
 33
 34gravity = [0,0,-9.81]
 35
 36p0 = np.array([0,0,0])
 37planeL = 0.3
 38xFact = 5
 39nPlane = 6 #spheres along x and y
 40rMarker = 0.02 #radius of spheres for contact points
 41
 42rCyl = 0.25
 43tCyl = 0.25
 44
 45sRad = 0.02
 46k = 2e4
 47d = 0.0005*k
 48stepSize = 1e-3
 49
 50frictionCoeff = 0.1
 51ss = 10
 52
 53markerList = []
 54radiusList = []
 55gDataList = []
 56
 57
 58gContact = mbs.AddGeneralContact()
 59# gContact.verboseMode = 1
 60gContact.SetFrictionPairings(frictionCoeff*np.eye(1))
 61gContact.SetSearchTreeCellSize(numberOfCells=[ss,ss,ss])
 62# gContact.SetSearchTreeBox(pMin=np.array([-0.5*planeL,-0.5*planeL,-0.1]),
 63#                           pMax=np.array([0.5*planeL,0.5*planeL,0.1]))
 64#print('treesize=',ssx*ssx*ssy)
 65#gContact.sphereSphereContact = False #does not influence results (only performance)!
 66
 67
 68#%% ground
 69gFloor = GraphicsDataCheckerBoard(p0+[planeL*xFact*0.5,0,0],size=planeL*xFact, size2=planeL, nTiles=8)
 70oGround=mbs.AddObject(ObjectGround(referencePosition= [0,0,0],
 71                                    visualization=VObjectGround(graphicsData=[gFloor])))
 72
 73
 74# nGround = mbs.AddNode(NodePointGround(referenceCoordinates=[0,0,0] ))
 75# mGround = mbs.AddMarker(MarkerNodeRigid(nodeNumber=nGround))
 76
 77gCyl = GraphicsDataCylinder(pAxis=[0,-0.5*tCyl,0],vAxis=[0,tCyl,0], radius=rCyl,
 78                                       color=[0.3,0.3,0.3,1],
 79                                       alternatingColor=color4lightgrey,nTiles=64)
 80
 81
 82#rigid body containing sphere markers:
 83omegaY = 10
 84bCyl=mbs.CreateRigidBody(referencePosition=[0,0,rCyl*1.0],
 85                    initialVelocity=[omegaY*rCyl,0,0],
 86                    #initialAngularVelocity=[0,omegaY,0],
 87                    # referenceRotationMatrix=RotationMatrixX(0.1),
 88                    inertia=InertiaCylinder(200, tCyl, rCyl, 1),
 89                    gravity = gravity,
 90                    nodeType = exu.NodeType.RotationRotationVector,
 91                    graphicsDataList=[gCyl],
 92                    )
 93mCyl = mbs.AddMarker(MarkerBodyRigid(bodyNumber=bCyl, localPosition=[0,0,0]))
 94sPos = mbs.AddSensor(SensorBody(bodyNumber=bCyl, storeInternal=True,
 95                                outputVariableType=exu.OutputVariableType.Position))
 96sRot = mbs.AddSensor(SensorBody(bodyNumber=bCyl, storeInternal=True,
 97                                outputVariableType=exu.OutputVariableType.Rotation))
 98sVel = mbs.AddSensor(SensorBody(bodyNumber=bCyl, storeInternal=True,
 99                                outputVariableType=exu.OutputVariableType.Velocity))
100sOmega = mbs.AddSensor(SensorBody(bodyNumber=bCyl, storeInternal=True,
101                                outputVariableType=exu.OutputVariableType.AngularVelocity))
102
103[meshPoints, meshTrigs] = GraphicsData2PointsAndTrigs(gCyl)
104
105
106gContact.AddTrianglesRigidBodyBased(rigidBodyMarkerIndex=mCyl,
107                                    contactStiffness=k, contactDamping=d, frictionMaterialIndex=0,
108    pointList=meshPoints,  triangleList=meshTrigs)
109
110
111
112for ix in range(nPlane*xFact+1):
113    for iy in range(nPlane+1):
114        x = (ix/nPlane-0.0)*planeL
115        y = (iy/nPlane-0.5)*planeL
116        z = -rMarker
117
118        m = mbs.AddMarker(MarkerBodyRigid(bodyNumber=oGround, localPosition=[x,y,z]))
119        gContact.AddSphereWithMarker(m, radius=rMarker, contactStiffness=k, contactDamping=d,
120                                     frictionMaterialIndex=0)
121
122
123
124
125
126mbs.Assemble()
127
128items=gContact.GetItemsInBox(pMin=[-4,-4,0], pMax=[4,4,20])
129# print('n spheres=',len(items['MarkerBasedSpheres']))
130
131
132tEnd = 1
133#tEnd = h*100
134simulationSettings = exu.SimulationSettings()
135# simulationSettings.linearSolverType = exu.LinearSolverType.EigenSparse
136#simulationSettings.solutionSettings.writeSolutionToFile = True
137simulationSettings.solutionSettings.writeSolutionToFile = False
138# simulationSettings.displayComputationTime = True
139#simulationSettings.displayStatistics = True
140simulationSettings.timeIntegration.verboseMode = 1
141#simulationSettings.parallel.numberOfThreads = 8
142#simulationSettings.timeIntegration.simulateInRealtime = True
143
144SC.visualizationSettings.general.graphicsUpdateInterval=0.02
145SC.visualizationSettings.general.drawCoordinateSystem=True
146SC.visualizationSettings.loads.show=False
147SC.visualizationSettings.bodies.show=True
148SC.visualizationSettings.markers.show=False
149
150SC.visualizationSettings.nodes.show=True
151SC.visualizationSettings.nodes.drawNodesAsPoint = False
152SC.visualizationSettings.nodes.defaultSize = 0 #must not be -1, otherwise uses autocomputed size
153SC.visualizationSettings.nodes.tiling = 4
154
155SC.visualizationSettings.window.renderWindowSize=[1900,1200]
156SC.visualizationSettings.openGL.multiSampling = 4
157#improved OpenGL rendering
158
159SC.visualizationSettings.contact.showSpheres = True
160SC.visualizationSettings.contact.tilingSpheres = 4
161
162
163if useGraphics:
164    exu.StartRenderer()
165    if 'renderState' in exu.sys:
166        SC.SetRenderState(exu.sys['renderState'])
167    # mbs.WaitForUserToContinue()
168
169
170simulationSettings.timeIntegration.numberOfSteps = int(tEnd/stepSize)
171simulationSettings.timeIntegration.endTime = tEnd
172mbs.SolveDynamic(simulationSettings, solverType=exu.DynamicSolverType.ExplicitEuler)
173
174if useGraphics:
175    #SC.WaitForRenderEngineStopFlag()
176    exu.StopRenderer() #safely close rendering window!
177
178#%%+++++++++++++++++++
179q = mbs.GetSensorValues(sPos)
180q += mbs.GetSensorValues(sVel)
181q += mbs.GetSensorValues(sOmega)
182q += mbs.GetSensorValues(sRot)
183#print('q=', q)
184
185u = NormL2(q)
186exu.Print('solution of generalContactCylinderTest =',u)
187
188exudynTestGlobals.testError = u - (5.486908430912642)
189exudynTestGlobals.testResult = u