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 * #includes itemInterface and rigidBodyUtilities
15import exudyn.graphics as graphics
16
17import numpy as np
18
19testIsActive = exu.sys.get('testIsActive', False)
20
21SC = exu.SystemContainer()
22mbs = SC.AddSystem()
23
24
25#linear spring-damper
26L=0.5
27mass = 1.6
28k = 4000
29omega0 = 50 # sqrt(4000/1.6)
30dRel = 0.05
31d = dRel * 2 * 80 #80=sqrt(1.6*4000)
32u0=-0.08
33v0=1
34f = 80
35#static result = f/k = 0.01;
36x0 = f/k
37#(exact) dynamic result with u0 and v0: (see bottom of file)
38
39#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
40#node for mass point:
41n1=mbs.AddNode(Point(referenceCoordinates = [L,0,0], initialCoordinates = [u0,0,0], initialVelocities= [v0,0,0]))
42
43#add mass points and ground object:
44objectGround = mbs.AddObject(ObjectGround(referencePosition = [0,0,0]))
45massPoint1 = mbs.AddObject(MassPoint(mass = mass, nodeNumber = n1))
46
47#marker for constraint / springDamper
48groundMarker = mbs.AddMarker(MarkerBodyPosition(bodyNumber = objectGround, localPosition= [0, 0, 0]))
49bodyMarker = mbs.AddMarker(MarkerBodyPosition(bodyNumber = massPoint1, localPosition= [0, 0, 0]))
50
51mbs.AddObject(CartesianSpringDamper(markerNumbers = [groundMarker, bodyMarker], stiffness = [k,k,k], damping = [d,0,0], offset = [L,0,0]))
52
53#add loads:
54mbs.AddLoad(Force(markerNumber = bodyMarker, loadVector = [f, 0, 0]))
55
56
57#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
58#node for mass point:
59
60n2=mbs.AddNode(Point(referenceCoordinates = [2*L,0,0], initialCoordinates = [u0,f/k*0.999,0], initialVelocities= [v0,0,0]))
61
62M=np.diag([mass,mass,mass])
63#exu.Print("M =",M)
64K=np.diag([k,k,k])
65#exu.Print("K =",K)
66D=np.diag([d,0,d])
67#exu.Print("D =",D)
68fv=np.array([f,f,0])
69#exu.Print("fv =",fv)
70
71fdyn=np.array([0,0,10])
72
73def Sweep(t, t1, f0, f1):
74 k = (f1-f0)/t1
75 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!!!
76
77def UFgenericODE2(mbs, t, itemIndex, q, q_t):
78 #f = np.sin(t*2*np.pi*10)*fdyn
79 f = Sweep(t,10,1,100)*fdyn
80 return f
81 #exu.Print("t =", t, ", f =", f)
82
83def UFmassGenericODE2(mbs, t, itemIndex, q, q_t):
84 return 1*M
85
86mbs.AddObject(ObjectGenericODE2(nodeNumbers = [n2], massMatrix=M, stiffnessMatrix=K, dampingMatrix=D, forceVector=fv,
87 forceUserFunction=UFgenericODE2, massMatrixUserFunction=UFmassGenericODE2))
88
89#exu.Print(mbs)
90mbs.Assemble()
91
92simulationSettings = exu.SimulationSettings()
93simulationSettings.timeIntegration.newton.useModifiedNewton = False #Just for the test; modified Newton is usually faster
94
95tEnd = 1
96steps = 2000
97simulationSettings.timeIntegration.numberOfSteps = steps
98simulationSettings.timeIntegration.endTime = tEnd
99simulationSettings.solution.file.writePeriod = tEnd/steps
100simulationSettings.timeIntegration.verboseMode = 1
101#simulationSettings.solution.file.writePeriod = tEnd/steps
102
103simulationSettings.timeIntegration.generalizedAlpha.spectralRadius = 1 #SHOULD work with 0.9 as well
104
105mbs.SolveDynamic(simulationSettings)
106
107u1 = mbs.GetNodeOutput(n1, exu.OutputVariableType.Coordinates)
108#exu.Print("u1 =", u1)
109u2 = mbs.GetNodeOutput(n2, exu.OutputVariableType.Coordinates)
110#exu.Print("u2 =", u2)
111
112u=np.linalg.norm(u1) + np.linalg.norm(u2)
113exu.Print('solution of genericODE2test=',u)
114
115exu.sys['testResult'] = u