pendulum.py

You can view and download this file on Github: pendulum.py

  1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2# This is an EXUDYN example
  3#
  4# Details:  Mathematical pendulum with constraint or spring-damper;
  5#           Remark: uses old style definition of items
  6#
  7# Author:   Johannes Gerstmayr
  8# Date:     2019-08-15
  9#
 10# 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.
 11#
 12#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 13
 14import exudyn as exu
 15
 16import time #for sleep()
 17SC = exu.SystemContainer()
 18mbs = SC.AddSystem()
 19
 20nBodies = 2
 21
 22L = 0.8 #distance
 23
 24
 25#phi = -0.7853981633974483 #-np.pi/8*2
 26n1=mbs.AddNode({'nodeType': 'Point',
 27                'referenceCoordinates': [L,0,0],
 28                'initialCoordinates':   [0,0,0]})
 29                #'initialCoordinates':   [-0.23431457505076192, -0.565685424949238, 0]})
 30                #'initialCoordinates':   [-(L-L*np.cos(phi)),L*np.sin(phi),0]})
 31
 32Lo=L*0.3 #graphics
 33mass = 2.5
 34g = 9.81
 35graphics1 = {'type':'Line', 'color':[0.1,0.1,0.8,1], 'data':[-(L+Lo),-(L+Lo),0, (L+Lo),-(L+Lo),0, (L+Lo),Lo,0, -(L+Lo),Lo,0, -(L+Lo),-(L+Lo), 0]} #background
 36
 37#add mass points and ground object:
 38mbs.AddObject({'objectType': 'Ground', 'referencePosition': [0,0,0], 'VgraphicsData': [graphics1]})
 39mbs.AddObject({'objectType': 'MassPoint', 'physicsMass': mass, 'nodeNumber': n1, 'VdrawSize':0.05*L})
 40
 41#add markers (needed for connectors and loads):
 42for i in range(nBodies): mbs.AddMarker({'markerType': 'BodyPosition',  'bodyNumber': i,  'localPosition': [0.0, 0.0, 0.0], 'bodyFixed': False})
 43
 44k = 4000 #spring stiffness
 45d = 200  #damping coefficient
 46
 47useConstraint = False
 48if useConstraint:
 49    mbs.AddObject({'objectType': 'ConnectorDistance',
 50                   'distance': L,
 51                   'markerNumbers': [0,1],
 52                   'drawSize': 0.01,
 53                   'color': [1,0,0,1]})
 54else:
 55    mbs.AddObject({'objectType': 'ConnectorSpringDamper',
 56                   'stiffness': k,
 57                   'damping': d,
 58                   'force': 0,
 59                   'referenceLength':L,
 60                   'markerNumbers': [0,1],
 61                   'drawSize': 0.05})
 62#add loads:
 63#mbs.AddLoad({'loadType': 'ForceVector',  'markerNumber': nBodies-1,  'loadVector': [0, 20, 0]}) #gives 20N tension in Distance constraint
 64mbs.AddLoad({'loadType': 'ForceVector',  'markerNumber': nBodies-1,  'loadVector': [0, -mass*g, 0]}) #gives approx. 0.001 deformation in x-direction
 65
 66print(mbs)
 67
 68mbs.Assemble()
 69exu.StartRenderer()
 70
 71#time.sleep(10)
 72
 73simulationSettings = exu.SimulationSettings()
 74
 75computeDynamic = True
 76if computeDynamic:
 77    f = 4400000
 78    simulationSettings.timeIntegration.numberOfSteps = 1*f
 79    simulationSettings.timeIntegration.endTime = 0.005*f
 80    simulationSettings.solutionSettings.writeSolutionToFile = True
 81    simulationSettings.solutionSettings.solutionWritePeriod = simulationSettings.timeIntegration.endTime/5000
 82    simulationSettings.displayComputationTime = True
 83    simulationSettings.timeIntegration.verboseMode = 1
 84
 85    simulationSettings.timeIntegration.newton.relativeTolerance = 1e-8
 86    simulationSettings.timeIntegration.newton.absoluteTolerance = 1e-10
 87    simulationSettings.timeIntegration.newton.useModifiedNewton = False
 88    simulationSettings.timeIntegration.newton.numericalDifferentiation.minimumCoordinateSize = 1
 89    simulationSettings.timeIntegration.newton.numericalDifferentiation.relativeEpsilon = 6.055454452393343e-06 #eps^(1/3)
 90    # simulationSettings.timeIntegration.generalizedAlpha.useIndex2Constraints = True
 91    simulationSettings.displayStatistics = True
 92
 93    SC.visualizationSettings.nodes.showNumbers = True
 94    SC.visualizationSettings.bodies.showNumbers = True
 95    #mbs.visualizationSettings.connectors.showNumbers = True
 96    SC.visualizationSettings.nodes.defaultSize = 0.05
 97
 98    exu.InfoStat()
 99    mbs.SolveDynamic(simulationSettings,
100                     solverType=exu.DynamicSolverType.TrapezoidalIndex2)
101    exu.InfoStat()
102
103else:
104    simulationSettings.solutionSettings.coordinatesSolutionFileName = "staticSolution.txt"
105    simulationSettings.solutionSettings.appendToFile = False
106    simulationSettings.staticSolver.newton.numericalDifferentiation.relativeEpsilon = 1e-4
107    #simulationSettings.staticSolver.newton.relativeTolerance = 1e-6
108    simulationSettings.staticSolver.newton.absoluteTolerance = 1e-1
109    simulationSettings.staticSolver.verboseMode = 2
110    #simulationSettings.staticSolver.newton.maxIterations = 5
111
112    SC.visualizationSettings.nodes.showNumbers = True
113    SC.visualizationSettings.bodies.showNumbers = True
114    SC.visualizationSettings.connectors.showNumbers = True
115    SC.visualizationSettings.nodes.defaultSize = 0.05
116
117    mbs.SolveStatic(simulationSettings)
118
119#time.sleep(0.5)
120
121SC.WaitForRenderEngineStopFlag()
122exu.StopRenderer() #safely close rendering window!