multiprocessingTest.py

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

 1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2# This is an EXUDYN example
 3#
 4# Details:  Example to show how to use parallel computation of EXUDYN models;
 5#           Using multiprocessing, several instances of EXUDYN are executed (graphics DISABLED!!!)
 6#           This represents a very simple possibility to run parallel simulations;
 7#           More advanced parameter variation is available in exudyn.processing.ParameterVariation(...)
 8#
 9# Author:   Johannes Gerstmayr
10# Date:     2020-11-12
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.
13#
14#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
16
17import exudyn as exu
18from exudyn.itemInterface import *
19from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
20import exudyn.graphics as graphics #only import if it does not conflict
21
22import numpy as np
23from multiprocessing import Pool
24
25#function, which creates and runs model; executed in parallel!
26def TestExudyn(x):
27
28    #create an environment for mini example
29    SC = exu.SystemContainer()
30    mbs = SC.AddSystem()
31
32    oGround=mbs.AddObject(ObjectGround(referencePosition= [0,0,0]))
33    nGround = mbs.AddNode(NodePointGround(referenceCoordinates=[0,0,0]))
34
35    testError=1 #set default error, if failed
36    exu.Print("start mini example for class ObjectMass1D")
37
38    node = mbs.AddNode(Node1D(referenceCoordinates = [0],
39                              initialCoordinates=[0.],
40                              initialVelocities=[1*x]))
41    mass = mbs.AddObject(Mass1D(nodeNumber = node, physicsMass=1))
42
43    #assemble and solve system for default parameters
44    mbs.Assemble()
45    #mbs.SolveDynamic(exu.SimulationSettings())
46
47    h=1e-6
48    tEnd = 10
49    simulationSettings = exu.SimulationSettings()
50    simulationSettings.timeIntegration.numberOfSteps = int(tEnd/h)
51    simulationSettings.timeIntegration.endTime = tEnd
52    simulationSettings.solutionSettings.coordinatesSolutionFileName = "coordinatesSolution"+str(int(x))+".txt"
53    simulationSettings.solutionSettings.writeSolutionToFile = True #no concurrent writing to files ...!
54    #exu.StartRenderer() #don't do this in parallelization: will crash
55    mbs.SolveDynamic(simulationSettings)
56    #exu.StopRenderer() #don't do this in parallelization: will crash
57
58    #check result, get current mass position at local position [0,0,0]
59    result = mbs.GetObjectOutputBody(mass, exu.OutputVariableType.Position, [0,0,0])[0]
60    print("result ",x, "=",result)
61    return result
62    #final x-coordinate of position shall be 2
63
64if __name__ == '__main__':
65    vInput = np.linspace(1,10,20) #start 20 tasks in parallel ...
66    with Pool(len(vInput)) as p:
67        print(p.map(TestExudyn, vInput))