switchingConstraintsPendulum.py
You can view and download this file on Github: switchingConstraintsPendulum.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN example
3#
4# Details: Rigid pendulum with tip mass
5# The functionality mbs.SetPreStepUserFunction is used to deactivate a constraint after a given time
6# Shows how to load solution to animate an existing solution
7#
8# Author: Johannes Gerstmayr
9# Date: 2019-11-22
10#
11# 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.
12#
13#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
15import exudyn as exu
16from exudyn.itemInterface import *
17from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
18import exudyn.graphics as graphics #only import if it does not conflict
19
20SC = exu.SystemContainer()
21mbs = SC.AddSystem()
22
23#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24#rigid pendulum with initial velocities
25#constraints and joints are deactivated during simulation
26
27rect = [-2.5,-1.5,0.5,1.5] #xmin,ymin,xmax,ymax
28background = {'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
29oGround=mbs.AddObject(ObjectGround(referencePosition= [0,0,0], visualization=VObjectGround(graphicsData= [background])))
30#nGround=mbs.AddNode(NodePointGround(referencePosition= [0,0,0]))
31a = 0.5 #x-dim of pendulum
32b = 0.05 #y-dim of pendulum
33massRigid = 12
34mass = 2 #of additional mass
35inertiaRigid = massRigid/12*(2*a)**2
36
37omega0 = 4
38
39graphics2 = {'type':'Line', 'color':[0.1,0.1,0.8,1], 'data':[-a,-b,0, a,-b,0, a,b,0, -a,b,0, -a,-b,0]} #background
40nRigid = mbs.AddNode(Rigid2D(referenceCoordinates=[-0.5,0,0], initialVelocities=[0,omega0*a,omega0]));
41oRigid = mbs.AddObject(RigidBody2D(physicsMass=massRigid, physicsInertia=inertiaRigid,nodeNumber=nRigid,visualization=VObjectRigidBody2D(graphicsData= [graphics2])))
42
43mR1 = mbs.AddMarker(MarkerBodyPosition(bodyNumber=oRigid, localPosition=[-0.5,0.,0.])) #support point
44mG0 = mbs.AddMarker(MarkerBodyPosition(bodyNumber=oGround, localPosition=[-0.5-a,0.,0.]))
45oRJoint = mbs.AddObject(RevoluteJoint2D(markerNumbers=[mG0,mR1],activeConnector=True))
46
47#mass point is attached with coordinate constraints:
48mCoordR0 = mbs.AddMarker(MarkerNodeCoordinate(nodeNumber=nRigid, coordinate=0))
49mCoordR1 = mbs.AddMarker(MarkerNodeCoordinate(nodeNumber=nRigid, coordinate=1))
50
51#additional mass point attached to COM of rigid body:
52nMass = mbs.AddNode(Point2D(referenceCoordinates=[-0.5,0], initialVelocities=[0,omega0*a]));
53oMass = mbs.AddObject(MassPoint2D(physicsMass=mass, nodeNumber=nMass) )
54
55mCoordM0 = mbs.AddMarker(MarkerNodeCoordinate(nodeNumber=nMass, coordinate=0))
56mCoordM1 = mbs.AddMarker(MarkerNodeCoordinate(nodeNumber=nMass, coordinate=1))
57
58oConstraint0 = mbs.AddObject(CoordinateConstraint(markerNumbers=[mCoordM0, mCoordR0],activeConnector=True))
59oConstraint1 = mbs.AddObject(CoordinateConstraint(markerNumbers=[mCoordM1, mCoordR1],activeConnector=True))
60
61mbs.Assemble()
62
63simulationSettings = exu.SimulationSettings() #takes currently set values or default values
64simulationSettings.timeIntegration.numberOfSteps = 4000
65simulationSettings.timeIntegration.endTime = 2
66simulationSettings.timeIntegration.newton.relativeTolerance = 1e-8
67simulationSettings.timeIntegration.newton.absoluteTolerance = 1e-6
68simulationSettings.timeIntegration.verboseMode = 1
69
70def UFswitchConnector(mbs, t):
71 if t > 0.3:
72 mbs.SetObjectParameter(oRJoint, 'activeConnector', False)
73 if t > 0.1:
74 mbs.SetObjectParameter(oConstraint0, 'activeConnector', False)
75 mbs.SetObjectParameter(oConstraint1, 'activeConnector', False)
76 return True #True, means that everything is alright, False=stop simulation
77
78mbs.SetPreStepUserFunction(UFswitchConnector)
79
80simulationSettings.timeIntegration.newton.useModifiedNewton = False
81simulationSettings.timeIntegration.newton.numericalDifferentiation.minimumCoordinateSize = 1
82simulationSettings.timeIntegration.generalizedAlpha.useNewmark = True
83simulationSettings.timeIntegration.generalizedAlpha.useIndex2Constraints = True
84simulationSettings.solutionSettings.solutionInformation = "Rigid pendulum with switching constraints"
85simulationSettings.displayStatistics = True
86
87SC.visualizationSettings.openGL.multiSampling = 1
88
89exu.StartRenderer()
90
91mbs.SolveDynamic(simulationSettings)
92print('end time =',mbs.systemData.GetTime()) #time after time integration ...
93#print('solution =',mbs.systemData.GetODE2Coordinates()) #solution coordinates after time integration ...
94
95
96SC.WaitForRenderEngineStopFlag()
97exu.StopRenderer() #safely close rendering window!
98
99mbs.SolutionViewer()