performanceMultiThreadingNG.py
You can view and download this file on Github: performanceMultiThreadingNG.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN example
3#
4# Details: Test performance of unconstrained bodies
5# Uses NGsolve multi-threading tools
6#
7# Author: Johannes Gerstmayr
8# Date: 2020-09-15
9#
10# 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.
11#
12#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
14import exudyn as exu
15from exudyn.itemInterface import *
16from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
17import exudyn.graphics as graphics #only import if it does not conflict
18from exudyn.graphicsDataUtilities import *
19
20
21#import numpy as np
22
23SC = exu.SystemContainer()
24mbs = SC.AddSystem()
25
26print("version=", exu.GetVersionString())
27
28#nBodiesList = [10,20,50,100,200,500,1000,2000,5000,10000]
29nBodiesList = [20,40,100,400,1000,5000]
30#nBodiesList = [10,100,1000,10000]
31results = []
32
33for nBodies in nBodiesList:
34
35 mbs.Reset()
36
37 a=0.03
38 gBody = graphics.BrickXYZ(-a, -a, -a, a, a, a, color=graphics.color.steelblue)
39
40 m = 1 #mass
41 J=[1,2,3,0,0,0] #inertia
42 omega0 = [0.01,0.03,0]
43
44 useEulerParameters = False
45
46 if useEulerParameters:
47 ep_t0 = list(AngularVelocity2EulerParameters_t(omega0, eulerParameters0))
48 for i in range(nBodies):
49 n = mbs.AddNode(RigidEP(referenceCoordinates=[0.1*i,0,0]+list(eulerParameters0),
50 initialVelocities=[0,0,0]+ ep_t0))
51 o = mbs.AddObject(RigidBody(physicsMass=m, physicsInertia=J, nodeNumber=n,
52 visualization=VObjectRigidBody(graphicsData=[gBody])))
53 else:
54 for i in range(nBodies):
55 n = mbs.AddNode(RigidRxyz(referenceCoordinates=[0.1*i,0,0]+[0]*3,
56 initialVelocities=[0,0,0]+ omega0))
57 o = mbs.AddObject(RigidBody(physicsMass=m, physicsInertia=J, nodeNumber=n,
58 visualization=VObjectRigidBody(graphicsData=[gBody])))
59
60 mbs.Assemble()
61
62 simulationSettings = exu.SimulationSettings() #takes currently set values or default values
63
64 tEnd = 1*1000/nBodies
65 h=0.0005 #no visual differences for step sizes smaller than 0.0005
66
67 simulationSettings.timeIntegration.numberOfSteps = int(tEnd/h)
68 simulationSettings.timeIntegration.endTime = tEnd
69 simulationSettings.solutionSettings.writeSolutionToFile = False
70
71 #simulationSettings.solutionSettings.solutionWritePeriod = 0.01
72 #simulationSettings.solutionSettings.sensorsWritePeriod = h*4
73 simulationSettings.timeIntegration.verboseMode = 1
74 simulationSettings.timeIntegration.verboseModeFile = 0
75
76 simulationSettings.timeIntegration.newton.useModifiedNewton = True
77
78 simulationSettings.timeIntegration.generalizedAlpha.useIndex2Constraints = True
79 simulationSettings.timeIntegration.generalizedAlpha.useNewmark = True
80 simulationSettings.timeIntegration.generalizedAlpha.spectralRadius = 0.5#0.5
81 simulationSettings.timeIntegration.generalizedAlpha.computeInitialAccelerations=True
82
83 simulationSettings.linearSolverType=exu.LinearSolverType.EigenSparse
84 simulationSettings.displayComputationTime = True
85 #simulationSettings.displayStatistics = True
86
87 SC.visualizationSettings.nodes.show = True
88 SC.visualizationSettings.nodes.drawNodesAsPoint = False
89 SC.visualizationSettings.nodes.showBasis = True
90 SC.visualizationSettings.nodes.basisSize = 0.015
91
92 #exu.StartRenderer()
93 #mbs.WaitForUserToContinue()
94
95 threadsList = [1,2,3,4,6,8,10,12]#,14,16]#,20,24]
96 #threadsList = [1,4,10]#,14,16]#,20,24]
97 tTotalList = []
98 tODE2RHSlist = []
99 tMassMatrixList = []
100 tNewtonIncrementList = []
101
102 for nThreads in threadsList:
103 simulationSettings.parallel.numberOfThreads = nThreads
104 #print("=====================================================")
105 print("compute with", nThreads, " threads")
106 #print("=======================================")
107 solver = exu.MainSolverImplicitSecondOrder()
108 solver.SolveSystem(mbs, simulationSettings)
109
110 #tNewtonIncrement = solver.timer.newtonIncrement
111
112 tTotalList += [solver.timer.total]
113 tODE2RHSlist += [solver.timer.ODE2RHS]
114 tMassMatrixList += [solver.timer.massMatrix]
115 tNewtonIncrementList += [solver.timer.newtonIncrement]
116
117 print("=====================================================")
118 print("nBodies =",nBodies)
119 print("thread count =",threadsList)
120 print("CPU times =",tTotalList)
121 print("ODE2RHS times =",tODE2RHSlist)
122 print("massMatrix times =",tMassMatrixList)
123 print("newtonIncrement t=",tNewtonIncrementList)
124
125 results += [{'nBodies':nBodies,
126 'threads':threadsList,
127 'CPU time':tTotalList,
128 'RHS CPU time':tODE2RHSlist,
129 'mass matrix CPU time':tMassMatrixList,
130 'newton increment CPU time':tNewtonIncrementList,
131 }]
132
133 #SC.WaitForRenderEngineStopFlag()
134 #exu.StopRenderer() #safely close rendering window!
135
136print("results=",results)