springDamperTutorialNew.py
You can view and download this file on Github: springDamperTutorialNew.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN example
3#
4# Details: This is the file for the EXUDYN first tutorial example showing a simple masspoint a SpringDamper
5#
6# Author: Johannes Gerstmayr
7# Date: 2023-05-15
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
13
14import exudyn as exu
15from exudyn.utilities import *
16
17import numpy as np #for postprocessing
18
19SC = exu.SystemContainer()
20mbs = SC.AddSystem()
21
22print('EXUDYN version='+exu.GetVersionString())
23
24L=0.5
25mass = 1.6 #mass in kg
26spring = 4000 #stiffness of spring-damper in N/m
27damper = 8 #damping constant in N/(m/s)
28
29u0=-0.08 #initial displacement
30v0=1 #initial velocity
31f =80 #force on mass
32x0=f/spring #static displacement
33
34print('resonance frequency = '+str(np.sqrt(spring/mass)))
35print('static displacement = '+str(x0))
36
37oMass = mbs.CreateMassPoint(referencePosition=[L,0,0],
38 initialDisplacement = [u0,0,0],
39 initialVelocity= [v0,0,0],
40 physicsMass=mass) #force created via gravity
41
42oGround = mbs.AddObject(ObjectGround())
43
44#create spring damper with reference length computed from reference positions (=L)
45oSD = mbs.CreateSpringDamper(bodyOrNodeList=[oMass, oGround],
46 stiffness = spring, damping = damper)
47
48#add load via marker:
49bodyMarker = mbs.AddMarker(MarkerBodyPosition(bodyNumber=oMass))
50mbs.AddLoad(LoadForceVector(markerNumber = bodyMarker, loadVector = [f,0,0]))
51
52
53#add sensor:
54sForce = mbs.AddSensor(SensorObject(objectNumber=oSD, storeInternal=True,
55 outputVariableType=exu.OutputVariableType.ForceLocal))
56sDisp = mbs.AddSensor(SensorBody(bodyNumber=oMass, storeInternal=True,
57 outputVariableType=exu.OutputVariableType.Displacement))
58
59print(mbs)
60mbs.Assemble()
61
62tEnd = 1 #end time of simulation
63h = 0.001 #step size; leads to 1000 steps
64
65simulationSettings = exu.SimulationSettings()
66simulationSettings.solutionSettings.solutionWritePeriod = 5e-3 #output interval general
67simulationSettings.solutionSettings.sensorsWritePeriod = 5e-3 #output interval of sensors
68simulationSettings.timeIntegration.numberOfSteps = int(tEnd/h) #must be integer
69simulationSettings.timeIntegration.endTime = tEnd
70
71#add some drawing parameters for this example
72SC.visualizationSettings.nodes.drawNodesAsPoint=False
73SC.visualizationSettings.nodes.defaultSize=0.1
74
75exu.StartRenderer() #start graphics visualization
76mbs.WaitForUserToContinue() #wait for pressing SPACE bar to continue
77
78#start solver:
79mbs.SolveDynamic(simulationSettings,
80 solverType=exu.DynamicSolverType.TrapezoidalIndex2)
81
82SC.WaitForRenderEngineStopFlag()#wait for pressing 'Q' to quit
83exu.StopRenderer() #safely close rendering window!
84
85#evaluate final (=current) output values
86u = mbs.GetNodeOutput(0, exu.OutputVariableType.Position) #Node 0 is first node
87print('displacement=',u)
88
89#+++++++++++++++++++++++++++++++++++++++++++++++++++++
90#compute exact solution:
91
92omega0 = np.sqrt(spring/mass) #eigen frequency of undamped system
93dRel = damper/(2*np.sqrt(spring*mass)) #dimensionless damping
94omega = omega0*np.sqrt(1-dRel**2) #eigen frequency of damped system
95C1 = u0-x0 #static solution needs to be considered!
96C2 = (v0+omega0*dRel*C1) / omega #C1, C2 are coeffs for solution
97steps = int(tEnd/h)
98
99refSol = np.zeros((steps+1,2))
100for i in range(0,steps+1):
101 t = tEnd*i/steps
102 refSol[i,0] = t
103 refSol[i,1] = np.exp(-omega0*dRel*t)*(C1*np.cos(omega*t) + C2*np.sin(omega*t))+x0
104
105#use PlotSensor functionality to plot data:
106mbs.PlotSensor(sensorNumbers=[refSol], components=[0], labels='displacement (m); exact solution',
107 colorCodeOffset=2, closeAll=True) #color code offset to have same colors as in original example
108mbs.PlotSensor(sensorNumbers=[sDisp], components=[0], labels='displacement (m); numerical solution',
109 colorCodeOffset=0, newFigure=False)
110
111mbs.PlotSensor(sensorNumbers=[sForce], labels='force (kN)',
112 colorCodeOffset=1, factors=[1e-3], newFigure=False)