-
Notifications
You must be signed in to change notification settings - Fork 21
/
multiprocessingTest.py
70 lines (55 loc) · 2.85 KB
/
multiprocessingTest.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
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# This is an EXUDYN example
#
# Details: Example to show how to use parallel computation of EXUDYN models;
# Using multiprocessing, several instances of EXUDYN are executed (graphics DISABLED!!!)
# This represents a very simple possibility to run parallel simulations;
# More advanced parameter variation is available in exudyn.processing.ParameterVariation(...)
#
# Author: Johannes Gerstmayr
# Date: 2020-11-12
#
# 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.itemInterface import *
from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
import exudyn.graphics as graphics #only import if it does not conflict
import numpy as np
from multiprocessing import Pool
#function, which creates and runs model; executed in parallel!
def TestExudyn(x):
#create an environment for mini example
SC = exu.SystemContainer()
mbs = SC.AddSystem()
oGround=mbs.AddObject(ObjectGround(referencePosition= [0,0,0]))
nGround = mbs.AddNode(NodePointGround(referenceCoordinates=[0,0,0]))
testError=1 #set default error, if failed
exu.Print("start mini example for class ObjectMass1D")
node = mbs.AddNode(Node1D(referenceCoordinates = [0],
initialCoordinates=[0.],
initialVelocities=[1*x]))
mass = mbs.AddObject(Mass1D(nodeNumber = node, physicsMass=1))
#assemble and solve system for default parameters
mbs.Assemble()
#mbs.SolveDynamic(exu.SimulationSettings())
h=1e-6
tEnd = 10
simulationSettings = exu.SimulationSettings()
simulationSettings.timeIntegration.numberOfSteps = int(tEnd/h)
simulationSettings.timeIntegration.endTime = tEnd
simulationSettings.solutionSettings.coordinatesSolutionFileName = "coordinatesSolution"+str(int(x))+".txt"
simulationSettings.solutionSettings.writeSolutionToFile = True #no concurrent writing to files ...!
#exu.StartRenderer() #don't do this in parallelization: will crash
mbs.SolveDynamic(simulationSettings)
#exu.StopRenderer() #don't do this in parallelization: will crash
#check result, get current mass position at local position [0,0,0]
result = mbs.GetObjectOutputBody(mass, exu.OutputVariableType.Position, [0,0,0])[0]
print("result ",x, "=",result)
return result
#final x-coordinate of position shall be 2
if __name__ == '__main__':
vInput = np.linspace(1,10,20) #start 20 tasks in parallel ...
with Pool(len(vInput)) as p:
print(p.map(TestExudyn, vInput))