appendSystemTest.py

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

 1#%%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2# This is an EXUDYN example
 3#
 4# Details:  SystemContainer.AppendSystem with a system the container did not create (#2842): a copy of a
 5#           MainSystem, which belongs to Python, and the system of another container. The container keeps an
 6#           appended system alive and does not delete it - neither when the Python name is deleted first, nor
 7#           with SC.Reset(), nor when the container goes; the system of another container gets that container
 8#           back. Before #2842, each of these deleted the system twice and ended in an access violation.
 9#
10# Author:   Johannes Gerstmayr
11# Date:     2026-10-04
12#
13# 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.
14#
15#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
17import exudyn as exu
18from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
19import copy
20
21testIsActive = exu.sys.get('testIsActive', False)
22
23
24def MassOnSpring(mbs):
25    """a mass point on a spring under a force; returns the sensor of its position"""
26    oGround = mbs.CreateGround()
27    oMass = mbs.CreateMassPoint(referencePosition=[1, 0, 0], mass=2)
28    mbs.CreateSpringDamper(itemNumbers=[oGround, oMass], stiffness=100, damping=1)
29    mbs.CreateForce(itemNumber=oMass, loadVector=[5, 0, 0])
30    return mbs.AddSensor(SensorBody(bodyNumber=oMass, storeInternal=True,
31                                    outputVariableType=exu.OutputVariableType.Position))
32
33
34def Solve(mbs, sensor):
35    mbs.Assemble()
36    simulationSettings = exu.SimulationSettings()
37    simulationSettings.timeIntegration.numberOfSteps = 100
38    simulationSettings.timeIntegration.endTime = 0.5
39    simulationSettings.solution.file.write = False
40    mbs.SolveDynamic(simulationSettings)
41    return mbs.GetSensorValues(sensor)[0]
42
43
44result = 0
45
46#a copy, which belongs to Python, appended and then forgotten by Python: the container keeps it alive
47SC = exu.SystemContainer()
48mbs = SC.AddSystem()
49sensor = MassOnSpring(mbs)
50mbsCopy = copy.copy(mbs)
51index = SC.AppendSystem(mbsCopy)
52exu.Print('appended copy as system', index, 'of', SC.NumberOfSystems())
53result += index                                     #1
54del mbsCopy
55result += Solve(SC.GetSystem(1), sensor)
56del mbs
57del SC                                              #the container goes; the copy is deleted once, by Python
58
59#SC.Reset() keeps an appended system: it still works afterwards
60SC = exu.SystemContainer()
61mbs = SC.AddSystem()
62sensor = MassOnSpring(mbs)
63mbsCopy = copy.copy(mbs)
64SC.AppendSystem(mbsCopy)
65SC.Reset()
66result += SC.NumberOfSystems()                      #0
67SC2 = exu.SystemContainer()
68SC2.AppendSystem(mbsCopy)
69result += Solve(mbsCopy, sensor)
70del SC2, mbsCopy, SC
71
72#the system of another container: it is shown by the second one and returns to the first one
73SC = exu.SystemContainer()
74mbs = SC.AddSystem()
75sensor = MassOnSpring(mbs)
76SC2 = exu.SystemContainer()
77SC2.AppendSystem(mbs)
78result += SC2.NumberOfSystems()                     #1
79del SC2
80result += SC.NumberOfSystems()                      #1
81result += Solve(mbs, sensor)
82
83exu.Print('appendSystemTest result=', result)
84exu.sys['testResult'] = result