genericODE2test.py

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

  1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2# This is an EXUDYN example
  3#
  4# Details:  Test model for GenericODE2
  5#
  6# Author:   Johannes Gerstmayr
  7# Date:     2020-03-09
  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 *
 15
 16import numpy as np
 17
 18useGraphics = True #without test
 19#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 20#you can erase the following lines and all exudynTestGlobals related operations if this is not intended to be used as TestModel:
 21try: #only if called from test suite
 22    from modelUnitTests import exudynTestGlobals #for globally storing test results
 23    useGraphics = exudynTestGlobals.useGraphics
 24except:
 25    class ExudynTestGlobals:
 26        pass
 27    exudynTestGlobals = ExudynTestGlobals()
 28#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 29
 30SC = exu.SystemContainer()
 31mbs = SC.AddSystem()
 32
 33
 34#linear spring-damper
 35L=0.5
 36mass = 1.6
 37k = 4000
 38omega0 = 50 # sqrt(4000/1.6)
 39dRel = 0.05
 40d = dRel * 2 * 80 #80=sqrt(1.6*4000)
 41u0=-0.08
 42v0=1
 43f = 80
 44#static result = f/k = 0.01;
 45x0 = f/k
 46#(exact) dynamic result with u0 and v0: (see bottom of file)
 47
 48#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 49#node for mass point:
 50n1=mbs.AddNode(Point(referenceCoordinates = [L,0,0], initialCoordinates = [u0,0,0], initialVelocities= [v0,0,0]))
 51
 52#add mass points and ground object:
 53objectGround = mbs.AddObject(ObjectGround(referencePosition = [0,0,0]))
 54massPoint1 = mbs.AddObject(MassPoint(physicsMass = mass, nodeNumber = n1))
 55
 56#marker for constraint / springDamper
 57groundMarker = mbs.AddMarker(MarkerBodyPosition(bodyNumber = objectGround, localPosition= [0, 0, 0]))
 58bodyMarker = mbs.AddMarker(MarkerBodyPosition(bodyNumber = massPoint1, localPosition= [0, 0, 0]))
 59
 60mbs.AddObject(CartesianSpringDamper(markerNumbers = [groundMarker, bodyMarker], stiffness = [k,k,k], damping = [d,0,0], offset = [L,0,0]))
 61
 62#add loads:
 63mbs.AddLoad(Force(markerNumber = bodyMarker, loadVector = [f, 0, 0]))
 64
 65
 66#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 67#node for mass point:
 68
 69n2=mbs.AddNode(Point(referenceCoordinates = [2*L,0,0], initialCoordinates = [u0,f/k*0.999,0], initialVelocities= [v0,0,0]))
 70
 71M=np.diag([mass,mass,mass])
 72#exu.Print("M =",M)
 73K=np.diag([k,k,k])
 74#exu.Print("K =",K)
 75D=np.diag([d,0,d])
 76#exu.Print("D =",D)
 77fv=np.array([f,f,0])
 78#exu.Print("fv =",fv)
 79
 80fdyn=np.array([0,0,10])
 81
 82def Sweep(t, t1, f0, f1):
 83    k = (f1-f0)/t1
 84    return np.sin(2*np.pi*(f0+k*0.5*t)*t) #take care of factor 0.5 in k*0.5*t, in order to obtain correct frequencies!!!
 85
 86def UFgenericODE2(mbs, t, itemIndex, q, q_t):
 87    #f = np.sin(t*2*np.pi*10)*fdyn
 88    f = Sweep(t,10,1,100)*fdyn
 89    return f
 90    #exu.Print("t =", t, ", f =", f)
 91
 92def UFmassGenericODE2(mbs, t, itemIndex, q, q_t):
 93    return 1*M
 94
 95mbs.AddObject(ObjectGenericODE2(nodeNumbers = [n2], massMatrix=M, stiffnessMatrix=K, dampingMatrix=D, forceVector=fv,
 96                                forceUserFunction=UFgenericODE2, massMatrixUserFunction=UFmassGenericODE2))
 97
 98#exu.Print(mbs)
 99mbs.Assemble()
100
101simulationSettings = exu.SimulationSettings()
102
103tEnd = 1
104steps = 2000
105simulationSettings.timeIntegration.numberOfSteps = steps
106simulationSettings.timeIntegration.endTime = tEnd
107simulationSettings.solutionSettings.solutionWritePeriod = tEnd/steps
108simulationSettings.timeIntegration.verboseMode = 1
109#simulationSettings.solutionSettings.solutionWritePeriod = tEnd/steps
110
111simulationSettings.timeIntegration.generalizedAlpha.spectralRadius = 1 #SHOULD work with 0.9 as well
112
113mbs.SolveDynamic(simulationSettings)
114
115u1 = mbs.GetNodeOutput(n1, exu.OutputVariableType.Coordinates)
116#exu.Print("u1 =", u1)
117u2 = mbs.GetNodeOutput(n2, exu.OutputVariableType.Coordinates)
118#exu.Print("u2 =", u2)
119
120u=NormL2(u1) + NormL2(u2)
121exu.Print('solution of genericODE2test=',u)
122
123exudynTestGlobals.testError = u - (0.03604546349898683) #2020-04-22: 0.03604546349898683
124exudynTestGlobals.testResult = u