binarySolutionFileTest.py

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

 1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2# This is an EXUDYN example
 3#
 4# Details:  The binary solution file stores its numbers as float or as double, as simulationSettings.solution.precision
 5#           says (< 8: float, 4 bytes; >= 8: double, 8 bytes) - the precision of the files, not of the console. A mass
 6#           on a spring is solved twice, and the solution files read back: the double file gives the final state
 7#           of the solver exactly, the float file to about 7 digits, and it is half the size.
 8#
 9# Author:   Johannes Gerstmayr
10# Date:     2026-10-04
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
16import exudyn as exu
17from exudyn.utilities import *
18import numpy as np
19import os
20
21testIsActive = exu.sys.get('testIsActive', False)
22
23SC = exu.SystemContainer()
24mbs = SC.AddSystem()
25oGround = mbs.CreateGround()
26oMass = mbs.CreateMassPoint(referencePosition=[1, 0, 0], initialVelocity=[0, 2, 0], mass=1.3)
27mbs.CreateSpringDamper(itemNumbers=[oGround, oMass], stiffness=1000, damping=0.7)
28mbs.Assemble()
29nNode = mbs.GetObject(oMass)['nodeNumber']
30
31errors = 0
32results = {}
33for precision in [6, 12]:
34    simulationSettings = exu.SimulationSettings()
35    simulationSettings.timeIntegration.numberOfSteps = 100
36    simulationSettings.timeIntegration.endTime = 0.1
37    simulationSettings.timeIntegration.newton.useModifiedNewton = False #Just for the test; modified Newton is usually faster
38    simulationSettings.solution.file.binary = True
39    simulationSettings.solution.precision = precision
40    simulationSettings.solution.file.name = 'solution/binarySolutionFileTest' + str(precision) + '.sol'
41    mbs.SolveDynamic(simulationSettings)
42
43    fileName = OutputFilePath(simulationSettings.solution.file.name) #where the solver wrote it
44    solution = LoadSolutionFile(fileName, verbose=False)
45    lastRow = solution['data'][-1, 1:4]                     #time, then the three coordinates of the node
46    exact = mbs.GetNodeOutput(nNode, exu.OutputVariableType.Coordinates)
47    results[precision] = (np.linalg.norm(lastRow - exact), os.path.getsize(fileName), lastRow)
48
49(errorFloat, sizeFloat, rowFloat) = results[6]
50(errorDouble, sizeDouble, rowDouble) = results[12]
51exu.Print('binarySolutionFileTest: error float =', errorFloat, ', double =', errorDouble, '; sizes', sizeFloat, sizeDouble)
52if not (errorDouble == 0 and 0 < errorFloat < 1e-5):
53    errors += 1
54if not sizeFloat < 0.75 * sizeDouble:
55    errors += 1
56
57u = np.sum(rowDouble) + errors
58exu.Print('solution of binarySolutionFileTest=', u)
59
60exu.sys['testResult'] = u