-
Notifications
You must be signed in to change notification settings - Fork 21
/
objectGenericODE2Test.py
281 lines (212 loc) · 12.4 KB
/
objectGenericODE2Test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# This is an EXUDYN example
#
# Details: Test for ObjectGenericODE2 with python user function for linear and rotor dynamics
# This test represents a FEM model of a rotor, which has elastic supports and rotation is locked
# We compute eigenmodes, compute the linear response as well as the response of the rotor with gyroscopic terms
#
# Author: Johannes Gerstmayr
# Date: 2020-05-13
#
# 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.
#
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import exudyn as exu
from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
import exudyn.graphics as graphics #only import if it does not conflict
from exudyn.FEM import *
useGraphics = True #without test
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#you can erase the following lines and all exudynTestGlobals related operations if this is not intended to be used as TestModel:
try: #only if called from test suite
from modelUnitTests import exudynTestGlobals #for globally storing test results
useGraphics = exudynTestGlobals.useGraphics
except:
class ExudynTestGlobals:
pass
exudynTestGlobals = ExudynTestGlobals()
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SC = exu.SystemContainer()
mbs = SC.AddSystem()
import numpy as np
accumulatedError = 0
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#Use FEMinterface to import FEM model and create FFRFreducedOrder object
fem = FEMinterface()
inputFileName = 'testData/rotorDiscTest' #runTestSuite.py is at another directory
#useGraphics = False
nodes=fem.ImportFromAbaqusInputFile(inputFileName+'.inp', typeName='Instance', name='rotor-1')
nNodes = len(nodes)
nODE2 = nNodes*3 #total number of ODE2 coordinates in FEM system; size of M and K
fem.ReadMassMatrixFromAbaqus(inputFileName+'MASS1.mtx')
fem.ReadStiffnessMatrixFromAbaqus(inputFileName+'STIF1.mtx')
fem.ScaleStiffnessMatrix(1e-2) #for larger deformations, stiffness is reduced to 1%
#+++++++++++ add elastic supports to fem ==> compute correct eigen frequencies
pLeft = [0,0,0]
pRight = [0,0,0.5]
nLeft = fem.GetNodeAtPoint(pLeft)
nRight = fem.GetNodeAtPoint(pRight)
kJoint = 2e8 #joint stiffness
dJoint = kJoint*0.01 #joint damping
fem.AddElasticSupportAtNode(nLeft, springStiffness=[kJoint,kJoint,kJoint])
fem.AddElasticSupportAtNode(nRight, springStiffness=[kJoint,kJoint,kJoint])
#+++++++++++ compute eigenmodes for comparison
nModes = 8
fem.ComputeEigenmodes(nModes, excludeRigidBodyModes = 6, useSparseSolver = False)
exu.Print("eigen freq.=", fem.GetEigenFrequenciesHz()[0:6+nModes].round(4)) #mode 0 is rigid body mode (free rotation)!
exu.Print("eigen freq. first mode =", fem.GetEigenFrequenciesHz()[1]) #mode1 with supports: 57.6317863976366Hz; free-free mode6: sparse: 104.63701326020315, dense: 104.63701326063597
accumulatedError += 1e-2*(fem.GetEigenFrequenciesHz()[1]/57.63178639764625 - 1.) #check mode (with supports); this is subject to small variations between 32 and 64bit! ==>*1e-2
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#create generic object for rotor:
forwardWhirl = True #test: True; switch this flag to turn on rotordynamics effects
backwardWhirl = False #test: False; switch this flag to turn on rotordynamics effects
excitationSign = 1
if backwardWhirl: excitationSign = -1
forceVector = [0,-1.,0]*nNodes #static force vector, optional; add some force in y-direction; could also use node mass to compute force due to weight
fUnbalance = 2000 #fictive force, not depending on frequency
nForce = fem.GetNodeAtPoint([0,0,0.15])#position where unbalance (excitation) force acts
exu.Print("excitation node=", nForce)
fRotX = np.array([0]*nODE2)
fRotX[nForce*3+0] = fUnbalance
fRotY = np.array([0]*nODE2)
fRotY[nForce*3+1] = fUnbalance
#sweep parameters:
t1 = 5 #end time of sweep (t0=0)
f0 = 50 #start frequency
f1 = 250 #terminal frequency
omega1=np.pi*2.*f1
G = fem.GetGyroscopicMatrix(rotationAxis=2, sparse=False) #gyroscopic matrix for rotordynamics effects
useSparseG=True #speed up computation, using sparse matrix in user function
if useSparseG:
from scipy.sparse import csr_matrix
G=csr_matrix(G) #convert to sparse matrix
def UFforce(mbs, t, itemIndex, q, q_t):
#print("UFforce")
omega = 2.*np.pi*FrequencySweep(t, t1, f0,f1)
fact = omega/omega1
force = (fact*SweepCos(t, t1, f0, f1))*fRotY + (excitationSign*fact*SweepSin(t, t1, f0, f1))*fRotX #excitationSign: '+' = forward whirl, '-' = backward whirl
if forwardWhirl or backwardWhirl:
#omegaQ_t = omega * np.array(q_t)
force -= G @ (omega * np.array(q_t)) #negative sign: because term belongs to left-hand-side!!!
#force -= omega*(G @q_t) #negative sign: because term belongs to left-hand-side!!!
return force
#add nodes:
nodeList = [] #create node list
for node in fem.GetNodePositionsAsArray():
nodeList += [mbs.AddNode(NodePoint(referenceCoordinates=node))]
#now add generic body built from FEM model with mass and stiffness matrix (optional damping could be added):
oGenericODE2 = mbs.AddObject(ObjectGenericODE2(nodeNumbers = nodeList,
massMatrix=fem.GetMassMatrix(sparse=False),
stiffnessMatrix=fem.GetStiffnessMatrix(sparse=False),
forceVector=forceVector, forceUserFunction=UFforce,
visualization=VObjectGenericODE2(triangleMesh = fem.GetSurfaceTriangles(),
color=graphics.color.lightred)))
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#add markers and joints
nodeDrawSize = 0.0025 #for joint drawing
nMid = fem.GetNodeAtPoint([0,0,0.25])
nTopMid = fem.GetNodeAtPoint([0., 0.05, 0.25]) #lock rotation of body
exu.Print("nMid=",nMid)
exu.Print("nTopMid=",nTopMid)
nGround = mbs.AddNode(NodePointGround(referenceCoordinates=[0,0,0], visualization = VNodePointGround(show=False))) #ground node for coordinate constraint
mGroundCoordinate = mbs.AddMarker(MarkerNodeCoordinate(nodeNumber = nGround, coordinate=0)) #Ground node ==> no action
#add constraint to avoid rotation of body
mTopRight = mbs.AddMarker(MarkerNodeCoordinate(nodeNumber = nTopMid, coordinate=0)) #x-coordinate of node at y-max
mbs.AddObject(CoordinateConstraint(markerNumbers=[mGroundCoordinate, mTopRight]))
oGround = mbs.AddObject(ObjectGround(referencePosition= [0,0,0]))
mGroundPosLeft = mbs.AddMarker(MarkerBodyPosition(bodyNumber=oGround, localPosition=pLeft))
mGroundPosRight = mbs.AddMarker(MarkerBodyPosition(bodyNumber=oGround, localPosition=pRight))
#++++++++++++++++++++++++++++++++++++++++++
#find nodes at left and right surface:
nodeListLeft = fem.GetNodesInPlane(pLeft, [0,0,1])
nodeListRight = fem.GetNodesInPlane(pRight, [0,0,1])
lenLeft = len(nodeListLeft)
lenRight = len(nodeListRight)
weightsLeft = np.array((1./lenLeft)*np.ones(lenLeft))
weightsRight = np.array((1./lenRight)*np.ones(lenRight))
addDampers = True
if addDampers:
for i in range(3):
mLeft = mbs.AddMarker(MarkerNodeCoordinate(nodeNumber = nLeft, coordinate=i))
mRight = mbs.AddMarker(MarkerNodeCoordinate(nodeNumber = nRight, coordinate=i))
mbs.AddObject(CoordinateSpringDamper(markerNumbers=[mGroundCoordinate,mLeft],
stiffness=0, damping=dJoint))
if i != 2: #exclude double constraint in z-direction (axis)
mbs.AddObject(CoordinateSpringDamper(markerNumbers=[mGroundCoordinate,mRight],
stiffness=0, damping=dJoint))
addJoint = False #set this flag, if not adding supports to stiffness matrix in fem
if addJoint:
useSpringDamper = True
mLeft = mbs.AddMarker(MarkerSuperElementPosition(bodyNumber=oGenericODE2,
meshNodeNumbers=np.array(nodeListLeft), #these are the meshNodeNumbers
weightingFactors=weightsLeft))
mRight = mbs.AddMarker(MarkerSuperElementPosition(bodyNumber=oGenericODE2,
meshNodeNumbers=np.array(nodeListRight), #these are the meshNodeNumbers
weightingFactors=weightsRight))
oSJleft = mbs.AddObject(CartesianSpringDamper(markerNumbers=[mLeft, mGroundPosLeft],
stiffness=[kJoint,kJoint,kJoint], damping=[dJoint,dJoint,dJoint]))
oSJright = mbs.AddObject(CartesianSpringDamper(markerNumbers=[mRight,mGroundPosRight],
stiffness=[kJoint,kJoint,0], damping=[dJoint,dJoint,0]))
fileDir = 'solution/'
sDisp=mbs.AddSensor(SensorSuperElement(bodyNumber=oGenericODE2, meshNodeNumber=nMid, #meshnode number!
storeInternal=True,#fileName=fileDir+'nMidDisplacementLinearTest.txt',
outputVariableType = exu.OutputVariableType.Displacement))
mbs.Assemble()
simulationSettings = exu.SimulationSettings()
SC.visualizationSettings.nodes.defaultSize = nodeDrawSize
SC.visualizationSettings.nodes.drawNodesAsPoint = False
SC.visualizationSettings.connectors.defaultSize = 2*nodeDrawSize
SC.visualizationSettings.bodies.show = True
#SC.visualizationSettings.connectors.show = False
SC.visualizationSettings.bodies.deformationScaleFactor = 10 #use this factor to scale the deformation of modes
if SC.visualizationSettings.bodies.deformationScaleFactor !=1:
SC.visualizationSettings.nodes.show = False #nodes are not scaled
SC.visualizationSettings.openGL.showFaceEdges = True
SC.visualizationSettings.openGL.showFaces = True
#SC.visualizationSettings.sensors.show = True
#SC.visualizationSettings.sensors.drawSimplified = False
SC.visualizationSettings.sensors.defaultSize = 0.01
#SC.visualizationSettings.markers.drawSimplified = False
#SC.visualizationSettings.markers.show = True
SC.visualizationSettings.markers.defaultSize = 0.01
SC.visualizationSettings.loads.drawSimplified = False
SC.visualizationSettings.contour.outputVariable = exu.OutputVariableType.Displacement
SC.visualizationSettings.contour.outputVariableComponent = 1 #y-component
simulationSettings.solutionSettings.solutionInformation = "ObjectGenericODE2 test"
simulationSettings.solutionSettings.writeSolutionToFile=False
h=1e-3
tEnd = 0.05
#if useGraphics:
# tEnd = 0.1
simulationSettings.timeIntegration.numberOfSteps = int(tEnd/h)
simulationSettings.timeIntegration.endTime = tEnd
simulationSettings.solutionSettings.solutionWritePeriod = h
simulationSettings.timeIntegration.verboseMode = 1
#simulationSettings.timeIntegration.verboseModeFile = 3
simulationSettings.timeIntegration.newton.useModifiedNewton = True
simulationSettings.solutionSettings.sensorsWritePeriod = h
simulationSettings.timeIntegration.generalizedAlpha.spectralRadius = 0.5 #SHOULD work with 0.9 as well
simulationSettings.displayStatistics = False
simulationSettings.displayComputationTime = False
#create animation:
#simulationSettings.solutionSettings.recordImagesInterval = 0.0002
#SC.visualizationSettings.exportImages.saveImageFileName = "animation/frame"
#useGraphics = True
if useGraphics:
exu.StartRenderer()
if 'lastRenderState' in vars():
SC.SetRenderState(lastRenderState) #load last model view
#mbs.WaitForUserToContinue() #press space to continue
mbs.SolveDynamic(simulationSettings)
if useGraphics:
SC.WaitForRenderEngineStopFlag()
exu.StopRenderer() #safely close rendering window!
lastRenderState = SC.GetRenderState() #store model view for next simulation
accumulatedError += mbs.GetNodeOutput(nMid,exu.OutputVariableType.Position)[0] #take x-coordinate of position
exu.Print('solution of ObjectGenericODE2=',accumulatedError)
exudynTestGlobals.testError = accumulatedError - (-2.2737401292182432e-05) #2020-05-18: -2.2737401292182432e-05
exudynTestGlobals.testResult = accumulatedError
##++++++++++++++++++++++++++++++++++++++++++++++q+++++++
#plot results
if useGraphics:
mbs.PlotSensor(sDisp, components=1, closeAll=True, labels=['uMid,linear'])