mainSystemExtensionsTests.py

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

  1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2# This is an EXUDYN example
  3#
  4# Details:  Test models for mainSystemExtensions; tests for functions except PlotSensor or SolutionViewer,
  5#           which are tested already in other functions or cannot be tested with test suite;
  6#           all tests are self-contained and are included as examples for docu
  7#
  8# Author:   Johannes Gerstmayr
  9# Date:     2023-05-19
 10#
 11# 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.
 12#
 13#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 14
 15import exudyn as exu
 16from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
 17import exudyn.graphics as graphics #only import if it does not conflict
 18import numpy as np
 19
 20useGraphics = True #without test
 21#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 22#you can erase the following lines and all exudynTestGlobals related operations if this is not intended to be used as TestModel:
 23try: #only if called from test suite
 24    from modelUnitTests import exudynTestGlobals #for globally storing test results
 25    useGraphics = exudynTestGlobals.useGraphics
 26except:
 27    class ExudynTestGlobals:
 28        pass
 29    exudynTestGlobals = ExudynTestGlobals()
 30#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 31
 32testErrorTotal = 0
 33
 34#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
 35#create single mass point:
 36import exudyn as exu
 37from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
 38import exudyn.graphics as graphics #only import if it does not conflict
 39import numpy as np
 40SC = exu.SystemContainer()
 41mbs = SC.AddSystem()
 42
 43b0=mbs.CreateMassPoint(referencePosition = [0,0,0],
 44                       initialVelocity = [2,5,0],
 45                       physicsMass = 1, gravity = [0,-9.81,0],
 46                       drawSize = 0.5, color=graphics.color.blue)
 47
 48mbs.Assemble()
 49simulationSettings = exu.SimulationSettings() #takes currently set values or default values
 50simulationSettings.timeIntegration.numberOfSteps = 1000
 51simulationSettings.timeIntegration.endTime = 2
 52mbs.SolveDynamic(simulationSettings = simulationSettings)
 53
 54#mbs.SolutionViewer()
 55testError = np.linalg.norm(mbs.systemData.GetODE2Coordinates())
 56exu.Print('solution of mainSystemExtensions test MP=',testError)
 57testErrorTotal += testError
 58
 59#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
 60#create single rigid body:
 61import exudyn as exu
 62from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
 63import exudyn.graphics as graphics #only import if it does not conflict
 64import numpy as np
 65SC = exu.SystemContainer()
 66mbs = SC.AddSystem()
 67
 68b0 = mbs.CreateRigidBody(inertia = InertiaCuboid(density=5000,
 69                                                 sideLengths=[1,0.1,0.1]),
 70                         referencePosition = [1,0,0],
 71                         initialVelocity = [2,5,0],
 72                         initialAngularVelocity = [5,0.5,0.7],
 73                         gravity = [0,-9.81,0],
 74                         graphicsDataList = [graphics.Brick(size=[1,0.1,0.1],
 75                                                                      color=graphics.color.red)])
 76
 77mbs.Assemble()
 78simulationSettings = exu.SimulationSettings() #takes currently set values or default values
 79simulationSettings.timeIntegration.numberOfSteps = 1000
 80simulationSettings.timeIntegration.endTime = 2
 81
 82mbs.SolveDynamic(simulationSettings = simulationSettings)
 83
 84testError = np.linalg.norm(mbs.systemData.GetODE2Coordinates())
 85exu.Print('solution of mainSystemExtensions test RB=',testError)
 86testErrorTotal += testError
 87
 88#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
 89#create spring-damper:
 90import exudyn as exu
 91from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
 92import exudyn.graphics as graphics #only import if it does not conflict
 93import numpy as np
 94SC = exu.SystemContainer()
 95mbs = SC.AddSystem()
 96
 97b0 = mbs.CreateMassPoint(referencePosition = [2,0,0],
 98                         initialVelocity = [2,5,0],
 99                         physicsMass = 1, gravity = [0,-9.81,0],
100                         drawSize = 0.5, color=graphics.color.blue)
101
102oGround = mbs.AddObject(ObjectGround())
103#add vertical spring
104oSD = mbs.CreateSpringDamper(bodyList=[oGround, b0],
105                             localPosition0=[2,1,0],
106                             localPosition1=[0,0,0],
107                             stiffness=1e4, damping=1e2,
108                             drawSize=0.2)
109
110mbs.Assemble()
111simulationSettings = exu.SimulationSettings() #takes currently set values or default values
112simulationSettings.timeIntegration.numberOfSteps = 1000
113simulationSettings.timeIntegration.endTime = 2
114SC.visualizationSettings.nodes.drawNodesAsPoint=False
115mbs.SolveDynamic(simulationSettings = simulationSettings)
116
117#mbs.SolutionViewer()
118testError = np.linalg.norm(mbs.systemData.GetODE2Coordinates())
119exu.Print('solution of mainSystemExtensions test SD=',testError)
120testErrorTotal += testError
121
122
123#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
124#create mass point with cartesian spring damper:
125import exudyn as exu
126from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
127import exudyn.graphics as graphics #only import if it does not conflict
128import numpy as np
129SC = exu.SystemContainer()
130mbs = SC.AddSystem()
131
132b0 = mbs.CreateMassPoint(referencePosition = [7,0,0],
133                          physicsMass = 1, gravity = [0,-9.81,0],
134                          drawSize = 0.5, color=graphics.color.blue)
135
136oGround = mbs.AddObject(ObjectGround())
137
138oSD = mbs.CreateCartesianSpringDamper(bodyList=[oGround, b0],
139                              localPosition0=[7.5,1,0],
140                              localPosition1=[0,0,0],
141                              stiffness=[200,2000,0], damping=[2,20,0],
142                              drawSize=0.2)
143
144mbs.Assemble()
145simulationSettings = exu.SimulationSettings() #takes currently set values or default values
146simulationSettings.timeIntegration.numberOfSteps = 1000
147simulationSettings.timeIntegration.endTime = 2
148SC.visualizationSettings.nodes.drawNodesAsPoint=False
149
150mbs.SolveDynamic(simulationSettings = simulationSettings)
151
152# mbs.SolutionViewer()
153testError = np.linalg.norm(mbs.systemData.GetODE2Coordinates())
154exu.Print('solution of mainSystemExtensions test CSD=',testError)
155testErrorTotal += testError
156
157#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
158#create rigid body with revolute joint:
159import exudyn as exu
160from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
161import exudyn.graphics as graphics #only import if it does not conflict
162import numpy as np
163SC = exu.SystemContainer()
164mbs = SC.AddSystem()
165
166b0 = mbs.CreateRigidBody(inertia = InertiaCuboid(density=5000,
167                                                 sideLengths=[1,0.1,0.1]),
168                         referencePosition = [3,0,0],
169                         gravity = [0,-9.81,0],
170                         graphicsDataList = [graphics.Brick(size=[1,0.1,0.1],
171                                                                      color=graphics.color.steelblue)])
172oGround = mbs.AddObject(ObjectGround())
173mbs.CreateRevoluteJoint(bodyNumbers=[oGround, b0], position=[2.5,0,0], axis=[0,0,1],
174                        useGlobalFrame=True, axisRadius=0.02, axisLength=0.14)
175
176mbs.Assemble()
177simulationSettings = exu.SimulationSettings() #takes currently set values or default values
178simulationSettings.timeIntegration.numberOfSteps = 1000
179simulationSettings.timeIntegration.endTime = 2
180
181mbs.SolveDynamic(simulationSettings = simulationSettings)
182
183testError = np.linalg.norm(mbs.systemData.GetODE2Coordinates())
184exu.Print('solution of mainSystemExtensions test RJ=',testError)
185testErrorTotal += testError
186
187#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
188#create rigid body with prismatic joint:
189import exudyn as exu
190from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
191import exudyn.graphics as graphics #only import if it does not conflict
192import numpy as np
193SC = exu.SystemContainer()
194mbs = SC.AddSystem()
195
196b0 = mbs.CreateRigidBody(inertia = InertiaCuboid(density=5000,
197                                                 sideLengths=[1,0.1,0.1]),
198                         referencePosition = [4,0,0],
199                         initialVelocity = [0,4,0],
200                         gravity = [0,-9.81,0],
201                         graphicsDataList = [graphics.Brick(size=[1,0.1,0.1],
202                                                                      color=graphics.color.steelblue)])
203
204oGround = mbs.AddObject(ObjectGround())
205mbs.CreatePrismaticJoint(bodyNumbers=[oGround, b0], position=[3.5,0,0], axis=[0,1,0],
206                         useGlobalFrame=True, axisRadius=0.02, axisLength=1)
207
208mbs.Assemble()
209simulationSettings = exu.SimulationSettings() #takes currently set values or default values
210simulationSettings.timeIntegration.numberOfSteps = 1000
211simulationSettings.timeIntegration.endTime = 2
212
213mbs.SolveDynamic(simulationSettings = simulationSettings)
214
215testError = np.linalg.norm(mbs.systemData.GetODE2Coordinates())
216exu.Print('solution of mainSystemExtensions test PJ=',testError)
217testErrorTotal += testError
218
219
220#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
221#create rigid body with spherical joint:
222import exudyn as exu
223from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
224import exudyn.graphics as graphics #only import if it does not conflict
225import numpy as np
226SC = exu.SystemContainer()
227mbs = SC.AddSystem()
228
229b0 = mbs.CreateRigidBody(inertia = InertiaCuboid(density=5000,
230                                                 sideLengths=[1,0.1,0.1]),
231                         referencePosition = [5,0,0],
232                         initialAngularVelocity = [5,0,0],
233                         gravity = [0,-9.81,0],
234                         graphicsDataList = [graphics.Brick(size=[1,0.1,0.1],
235                                                                      color=graphics.color.orange)])
236oGround = mbs.AddObject(ObjectGround())
237mbs.CreateSphericalJoint(bodyNumbers=[oGround, b0], position=[5.5,0,0],
238                         useGlobalFrame=True, jointRadius=0.06)
239
240mbs.Assemble()
241simulationSettings = exu.SimulationSettings() #takes currently set values or default values
242simulationSettings.timeIntegration.numberOfSteps = 1000
243simulationSettings.timeIntegration.endTime = 2
244
245mbs.SolveDynamic(simulationSettings = simulationSettings)
246
247testError = np.linalg.norm(mbs.systemData.GetODE2Coordinates())
248testErrorTotal += testError
249exu.Print('solution of mainSystemExtensions test SJ=',testError)
250
251#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
252#create rigid body with generic joint, universal joint case with axes tilted by 0.125*pi around X:
253import exudyn as exu
254from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
255import exudyn.graphics as graphics #only import if it does not conflict
256import numpy as np
257SC = exu.SystemContainer()
258mbs = SC.AddSystem()
259
260b0 = mbs.CreateRigidBody(inertia = InertiaCuboid(density=5000,
261                                                 sideLengths=[1,0.1,0.1]),
262                         referencePosition = [6,0,0],
263                         initialAngularVelocity = [0,8,0],
264                         gravity = [0,-9.81,0],
265                         graphicsDataList = [graphics.Brick(size=[1,0.1,0.1],
266                                                                      color=graphics.color.orange)])
267oGround = mbs.AddObject(ObjectGround())
268mbs.CreateGenericJoint(bodyNumbers=[oGround, b0], position=[5.5,0,0],
269                       constrainedAxes=[1,1,1, 1,0,0],
270                       rotationMatrixAxes=RotationMatrixX(0.125*pi), #tilt axes
271                       useGlobalFrame=True, axesRadius=0.02, axesLength=0.2)
272
273#add global force:
274f0 = mbs.CreateForce(bodyNumber=b0, loadVector=[0.,20.,0.], localPosition=[0.5,0,0])
275
276#define user function for torque
277def UFtorque(mbs, t, load):
278    val = 1
279    if t < 1:
280        val = t*t
281    #print('load type=',type(load))
282    # return val*load
283    return val*np.array(load)
284
285#add torque applied in body coordinates:
286t0 = mbs.CreateTorque(bodyNumber=b0, loadVector=[0.,0.,10.], bodyFixed=True,
287                      loadVectorUserFunction=UFtorque)
288
289mbs.Assemble()
290simulationSettings = exu.SimulationSettings() #takes currently set values or default values
291simulationSettings.timeIntegration.numberOfSteps = 1000
292simulationSettings.timeIntegration.endTime = 2
293
294mbs.SolveDynamic(simulationSettings = simulationSettings)
295# mbs.SolutionViewer()
296
297testError = np.linalg.norm(mbs.systemData.GetODE2Coordinates())
298testErrorTotal += testError
299exu.Print('solution of mainSystemExtensions test GJ=',testError)
300
301#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
302#create single mass point and compute linearized system and eigenvalues:
303import exudyn as exu
304from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
305import exudyn.graphics as graphics #only import if it does not conflict
306import numpy as np
307SC = exu.SystemContainer()
308mbs = SC.AddSystem()
309
310b0 = mbs.CreateMassPoint(referencePosition = [2,0,0],
311                         initialVelocity = [2*0,5,0],
312                         physicsMass = 1, gravity = [0,-9.81,0],
313                         drawSize = 0.5, color=graphics.color.blue)
314
315oGround = mbs.AddObject(ObjectGround())
316#add vertical spring
317oSD = mbs.CreateSpringDamper(bodyList=[oGround, b0],
318                             localPosition0=[2,1,0],
319                             localPosition1=[0,0,0],
320                             stiffness=1e4, damping=1e2,
321                             drawSize=0.2)
322
323mbs.Assemble()
324[M,K,D] = mbs.ComputeLinearizedSystem()
325# exu.Print('M=\n',M,'\nK=\n',K,'\nD=\n',D) #check if K makes sense?
326
327[eigenvalues, eigenvectors] = mbs.ComputeODE2Eigenvalues()
328# exu.Print('eigenvalues=\n',eigenvalues)
329# exu.Print('eigenvectors=\n',eigenvectors)
330
331testError = 1e-3*np.linalg.norm(eigenvalues)
332exu.Print('solution of mainSystemExtensions test LinEig=',testError)
333testErrorTotal += testError
334
335#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
336#create rigid body with generic joint: compute system DOF
337import exudyn as exu
338from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
339import exudyn.graphics as graphics #only import if it does not conflict
340import numpy as np
341SC = exu.SystemContainer()
342mbs = SC.AddSystem()
343
344b0 = mbs.CreateRigidBody(inertia = InertiaCuboid(density=5000,
345                                                 sideLengths=[1,0.1,0.1]),
346                         referencePosition = [6,0,0],
347                         initialAngularVelocity = [0,8,0],
348                         gravity = [0,-9.81,0],
349                         graphicsDataList = [graphics.Brick(size=[1,0.1,0.1],
350                                                                      color=graphics.color.orange)])
351oGround = mbs.AddObject(ObjectGround())
352mbs.CreateGenericJoint(bodyNumbers=[oGround, b0], position=[5.5,0,0],
353                       constrainedAxes=[1,1,1, 1,0,0],
354                       rotationMatrixAxes=RotationMatrixX(0.125*pi), #tilt axes
355                       useGlobalFrame=True, axesRadius=0.02, axesLength=0.2)
356
357mbs.Assemble()
358res = mbs.ComputeSystemDegreeOfFreedom(verbose=0)
359
360
361testDrawSystemGraph = False
362try:
363    #all imports are part of anaconda (e.g. anaconda 5.2.0, python 3.6.5)
364    import numpy as np
365    import networkx as nx #for generating graphs and graph arrangement
366    import matplotlib.pyplot as plt #for drawing
367    testDrawSystemGraph = True
368except:
369    exu.Print("numpy, networkx and matplotlib required for DrawSystemGraph(...); skipping test")
370
371if testDrawSystemGraph:
372    mbs.DrawSystemGraph(useItemTypes=True, tightLayout=False)
373    if not useGraphics:
374        import matplotlib.pyplot as plt
375        plt.close('all')
376
377testError = np.sum(list(res.values())[0:3])
378exu.Print('solution of mainSystemExtensions test DOF=',testError)
379testErrorTotal += testError
380
381
382#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
383#create rigid body and mass point with distance constraint
384import exudyn as exu
385from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
386import exudyn.graphics as graphics #only import if it does not conflict
387import numpy as np
388SC = exu.SystemContainer()
389mbs = SC.AddSystem()
390
391b0 = mbs.CreateRigidBody(inertia = InertiaCuboid(density=5000,
392                                                  sideLengths=[1,0.1,0.1]),
393                          referencePosition = [6,0,0],
394                          gravity = [0,-9.81,0],
395                          graphicsDataList = [graphics.Brick(size=[1,0.1,0.1],
396                                                                      color=graphics.color.orange)])
397m1 = mbs.CreateMassPoint(referencePosition=[5.5,-1,0],
398                         physicsMass=1, drawSize = 0.2)
399n1 = mbs.GetObject(m1)['nodeNumber']
400
401oGround = mbs.AddObject(ObjectGround())
402mbs.CreateDistanceConstraint(bodyList=[oGround, b0],
403                             localPosition0 = [6.5,1,0],
404                             localPosition1 = [0.5,0,0],
405                             distance=None, #automatically computed
406                             drawSize=0.06)
407
408mbs.CreateDistanceConstraint(bodyOrNodeList=[b0, n1],
409                             localPosition0 = [-0.5,0,0],
410                             localPosition1 = [0.,0.,0.], #must be [0,0,0] for Node
411                             distance=None, #automatically computed
412                             drawSize=0.06)
413
414mbs.Assemble()
415simulationSettings = exu.SimulationSettings() #takes currently set values or default values
416simulationSettings.timeIntegration.numberOfSteps = 1000
417simulationSettings.timeIntegration.endTime = 2
418
419mbs.SolveDynamic(simulationSettings = simulationSettings)
420
421SC.visualizationSettings.nodes.drawNodesAsPoint=False
422#mbs.SolutionViewer()
423
424testError = np.linalg.norm(mbs.systemData.GetODE2Coordinates())
425testErrorTotal += testError
426exu.Print('solution of mainSystemExtensions test DC=',testError)
427
428#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
429
430exu.Print('solution of mainSystemExtensions TOTAL=',testErrorTotal)
431exudynTestGlobals.testError = testErrorTotal - (57.64639446941554)   #up to 2023-11-19:57.96750245606998 (added force/torque) #2023-05-19: 51.699269012604674
432exudynTestGlobals.testResult = testErrorTotal