systemContainerLifetimeTest.py

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

 1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2# This is an EXUDYN example
 3#
 4# Details:  A MainSystem keeps its SystemContainer alive, whether it came from SC.AddSystem() or from
 5#           SC.GetSystem(i) (#2851): a function that builds a model in a container of its own and returns
 6#           only the system leaves a system that works - before, the system of GetSystem pointed into a
 7#           container that had been deleted.
 8#
 9# Author:   Johannes Gerstmayr
10# Date:     2026-10-05
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
17import gc
18import weakref
19
20testIsActive = exu.sys.get('testIsActive', False)
21
22def BuildModel(useGetSystem):
23    """a model in a container of its own; only the system and a weak reference to the container are returned"""
24    SC = exu.SystemContainer()
25    mbs = SC.AddSystem()
26    mbs.CreateGround()
27    mbs.CreateMassPoint(referencePosition=[1,0,0], mass=1, gravity=[0,-9.81,0])
28    if useGetSystem:
29        mbs = SC.GetSystem(0)
30    return (mbs, weakref.ref(SC))
31
32u = 0
33for useGetSystem in [False, True]:
34    (mbs, container) = BuildModel(useGetSystem)
35    gc.collect()
36    mbs.Assemble()
37    simulationSettings = exu.SimulationSettings()
38    simulationSettings.solution.file.write = False
39    simulationSettings.timeIntegration.verboseMode = 0
40    mbs.SolveDynamic(simulationSettings)
41    alive = container() is not None
42    exu.Print('GetSystem' if useGetSystem else 'AddSystem', ': container alive', alive, ', objects',
43              mbs.systemData.NumberOfObjects())
44    u += alive + mbs.systemData.NumberOfObjects() + mbs.systemData.GetODE2Coordinates()[1]
45
46exu.Print('solution of systemContainerLifetimeTest=', u)
47
48exu.sys['testResult'] = u