compareFullModifiedNewton.py
You can view and download this file on Github: compareFullModifiedNewton.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN example
3#
4# Details: pendulum example: check difference of full and modified newton
5#
6# Author: Johannes Gerstmayr
7# Date: 2020-06-19
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 #only import if it does not conflict
16
17import numpy as np
18import matplotlib.pyplot as plt
19import matplotlib.ticker as ticker
20
21useGraphics = True #without test
22#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23#you can erase the following lines and all exudynTestGlobals related operations if this is not intended to be used as TestModel:
24try: #only if called from test suite
25 from modelUnitTests import exudynTestGlobals #for globally storing test results
26 useGraphics = exudynTestGlobals.useGraphics
27except:
28 class ExudynTestGlobals:
29 pass
30 exudynTestGlobals = ExudynTestGlobals()
31#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
33SC = exu.SystemContainer()
34mbs = SC.AddSystem()
35
36
37#%%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
38#rigid pendulum:
39rect = [-2,-2,2,2] #xmin,ymin,xmax,ymax
40background = {'type':'Line', 'color':[0.1,0.1,0.8,1], 'data':[rect[0],rect[1],0, rect[2],rect[1],0, rect[2],rect[3],0, rect[0],rect[3],0, rect[0],rect[1],0]} #background
41oGround=mbs.AddObject(ObjectGround(referencePosition= [0,0,0], visualization=VObjectGround(graphicsData= [background])))
42a = 0.5 #x-dim of pendulum
43b = 0.05 #y-dim of pendulum
44massRigid = 12
45inertiaRigid = massRigid/12*(2*a)**2
46g = 9.81 # gravity
47
48graphics2 = {'type':'Line', 'color':[0.1,0.1,0.8,1], 'data':[-a,-b,0, a,-b,0, a,b,0, -a,b,0, -a,-b,0]} #background
49omega0 = 5*0 #inconsistent initial conditions lead to integration problems!
50nRigid = mbs.AddNode(Rigid2D(referenceCoordinates=[a,0,0], initialVelocities=[0,omega0*a,omega0]));
51oRigid = mbs.AddObject(RigidBody2D(physicsMass=massRigid, physicsInertia=inertiaRigid,nodeNumber=nRigid,visualization=VObjectRigidBody2D(graphicsData= [graphics2])))
52
53mR1 = mbs.AddMarker(MarkerBodyPosition(bodyNumber=oRigid, localPosition=[-a,0.,0.])) #support point
54mR2 = mbs.AddMarker(MarkerBodyPosition(bodyNumber=oRigid, localPosition=[ a,0.,0.])) #end point
55
56mG0 = mbs.AddMarker(MarkerBodyPosition(bodyNumber=oGround, localPosition=[0,0,0.]))
57mbs.AddObject(RevoluteJoint2D(markerNumbers=[mG0,mR1]))
58
59mbs.AddLoad(Force(markerNumber = mR2, loadVector = [0, -massRigid*g, 0]))
60
61mbs.Assemble()
62#exu.Print(mbs)
63
64simulationSettings = exu.SimulationSettings() #takes currently set values or default values
65
66simulationSettings.timeIntegration.numberOfSteps = 100 #100
67simulationSettings.timeIntegration.endTime = 2
68simulationSettings.timeIntegration.newton.relativeTolerance = 1e-8
69simulationSettings.timeIntegration.newton.absoluteTolerance = 1e-4
70simulationSettings.timeIntegration.verboseMode = 1
71
72simulationSettings.timeIntegration.newton.numericalDifferentiation.minimumCoordinateSize = 1
73#simulationSettings.timeIntegration.generalizedAlpha.useNewmark = True
74#simulationSettings.timeIntegration.generalizedAlpha.useIndex2Constraints = True
75simulationSettings.timeIntegration.generalizedAlpha.spectralRadius = 0.5
76simulationSettings.displayStatistics = True
77simulationSettings.solutionSettings.solutionWritePeriod = 1e-4
78
79simulationSettings.timeIntegration.newton.useModifiedNewton = True
80simulationSettings.solutionSettings.coordinatesSolutionFileName = "solution/modifiedNewton.txt"
81mbs.SolveDynamic(simulationSettings)#, experimentalNewSolver=False)
82
83simulationSettings.timeIntegration.newton.useModifiedNewton = False
84simulationSettings.solutionSettings.coordinatesSolutionFileName = "solution/fullNewton.txt"
85mbs.SolveDynamic(simulationSettings)#, experimentalNewSolver=False)
86
87
88#%%*****************************************
89#post processing for mass point system
90
91dataM = np.loadtxt('solution/modifiedNewton.txt', comments='#', delimiter=',')
92dataF = np.loadtxt('solution/fullNewton.txt', comments='#', delimiter=',')
93
94# exu.Print("solFullNewton =",sum(abs(dataF[:,5])))
95# exu.Print("solModifiedNewton =",sum(abs(dataM[:,5])))
96
97u=sum(abs(dataM[:,5]-dataF[:,5]))
98exu.Print("compareFullModifiedNewton u=",u)
99
100exudynTestGlobals.testError = u - (0.0001583478719999567 ) #2020-12-18: 0.0001583478719999567
101exudynTestGlobals.testResult = u
102
103import os
104os.remove('solution/modifiedNewton.txt')
105os.remove('solution/fullNewton.txt')
106
107if useGraphics:
108 # plt.plot(dataM[:,0], dataM[:,3+2], 'b-') #plot column i over column 0 (time)
109 # plt.plot(dataF[:,0], dataF[:,3+2], 'r-') #plot column i over column 0 (time)
110 plt.plot(dataF[:,0], dataF[:,5]-dataM[:,5], 'r-')
111
112 ax=plt.gca() # get current axes
113 ax.grid(True, 'major', 'both')
114 ax.xaxis.set_major_locator(ticker.MaxNLocator(10)) #use maximum of 8 ticks on y-axis
115 ax.yaxis.set_major_locator(ticker.MaxNLocator(10)) #use maximum of 8 ticks on y-axis
116 plt.tight_layout()
117 plt.show()