fileHeaderThreadsTest.py

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

 1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2# This is an EXUDYN example
 3#
 4# Details:  The version line of the solution and sensor file headers names the number of threads of the
 5#           computation (#1450): "#Exudyn version = ...; FLOAT64; 2 thread(s)". A mass point is solved with
 6#           parallel.numberOfThreads = 2 and with 1; the result counts the threads read back from both files.
 7#
 8# Author:   Johannes Gerstmayr
 9# Date:     2026-10-07
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
17
18testIsActive = exu.sys.get('testIsActive', False)
19
20SC = exu.SystemContainer()
21mbs = SC.AddSystem()
22oMass = mbs.CreateMassPoint(referencePosition=[0,0,0], mass=1, gravity=[0,-9.81,0])
23mbs.AddSensor(SensorBody(bodyNumber=oMass, storeInternal=False, fileName='solution/fileHeaderThreadsSensor.txt',
24                         outputVariableType=exu.OutputVariableType.Position))
25mbs.Assemble()
26
27def ThreadsInHeader(fileName):
28    """the number at the end of the version line of a written file"""
29    with open(OutputFilePath(fileName), 'r') as file:
30        for line in file:
31            if line.startswith('#Exudyn version = '):
32                return int(line.split(';')[-1].split()[0]) #'... FLOAT64; 2 thread(s)'
33    return 0
34
35result = 0
36for nThreads in [2, 1]:
37    simulationSettings = exu.SimulationSettings()
38    simulationSettings.timeIntegration.numberOfSteps = 10
39    simulationSettings.timeIntegration.endTime = 0.1
40    simulationSettings.timeIntegration.verboseMode = 0
41    simulationSettings.parallel.numberOfThreads = nThreads
42    simulationSettings.solution.file.name = 'solution/fileHeaderThreadsSolution.txt'
43    mbs.SolveDynamic(simulationSettings)
44
45    solutionThreads = ThreadsInHeader('solution/fileHeaderThreadsSolution.txt')
46    sensorThreads = ThreadsInHeader('solution/fileHeaderThreadsSensor.txt')
47    exu.Print('requested', nThreads, 'threads; solution file:', solutionThreads, ', sensor file:', sensorThreads)
48    result += 10*nThreads*(solutionThreads == nThreads) + nThreads*(sensorThreads == nThreads)
49
50exu.Print('solution of fileHeaderThreadsTest=', result) #33 if both files name the threads in both runs
51exu.sys['testResult'] = result