inspectTest.py
You can view and download this file on Github: inspectTest.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN example
3#
4# Details: mbs.Inspect(itemIndex, what) asks an item what it provides and requests (#2203): what is a
5# member of exu.InspectType, the answer a list of the enumeration members Exudyn exports
6# (OutputVariableType, ObjectType, NodeType, MarkerType, AccessFunctionType); with what=None
7# the answer is a dict of all that apply to the item. Shown on a mass point on a spring-damper,
8# a rigid body, a load and a sensor:
9# (1) the output variables of a body, a connector, a node and a marker - the potential energy of
10# a spring-damper only as long as no user function defines its force;
11# (2) the types of an object, a node and a marker;
12# (3) what a connector and a load request of their markers, what an object requests of its
13# nodes, what a node marker requests of its node - requirements, each a list of
14# alternatives of which the node provides one -, and the access functions of a body,
15# which decide the markers it takes;
16# (4) what Inspect refuses: a plain int instead of a typed index, and a what that does not
17# apply to the item.
18#
19# Author: Johannes Gerstmayr
20# Date: 2026-10-01
21#
22# 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.
23#
24#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
25
26import exudyn as exu
27from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
28
29testIsActive = exu.sys.get('testIsActive', False)
30
31SC = exu.SystemContainer()
32mbs = SC.AddSystem()
33I = exu.InspectType
34
35oGround = mbs.AddObject(ObjectGround())
36nMass = mbs.AddNode(NodePoint(referenceCoordinates=[1, 0, 0]))
37oMass = mbs.AddObject(MassPoint(nodeNumber=nMass, mass=2))
38mGround = mbs.AddMarker(MarkerBodyPosition(bodyNumber=oGround))
39mMass = mbs.AddMarker(MarkerNodePosition(nodeNumber=nMass))
40oSpring = mbs.AddObject(SpringDamper(markerNumbers=[mGround, mMass], referenceLength=1, stiffness=100))
41lForce = mbs.AddLoad(LoadForceVector(markerNumber=mMass, loadVector=[1, 0, 0]))
42sSpring = mbs.AddSensor(SensorObject(objectNumber=oSpring, outputVariableType=exu.OutputVariableType.Force, storeInternal=True))
43oRigid = mbs.CreateRigidBody(referencePosition=[0, 1, 0], inertia=InertiaCuboid(density=1000, sideLengths=[0.2, 0.1, 0.1]))
44mbs.Assemble()
45
46def Names(answer):
47 """the names of the members, for printing"""
48 return [member.name for member in answer]
49
50#(1) output variables; the spring-damper lists PotentialEnergy, and no longer does with a force user function
51outputVariables = {name: mbs.Inspect(item, I.OutputVariables) for (name, item) in
52 [('mass point', oMass), ('spring-damper', oSpring), ('node', nMass), ('marker', mMass)]}
53for (name, answer) in outputVariables.items():
54 exu.Print('output variables of the', name + ':', Names(answer))
55mbs.SetObjectParameter(oSpring, 'springForceUserFunction', lambda mbs, t, itemNumber, u, v, k, d, f: k*u)
56withUserFunction = mbs.Inspect(oSpring, I.OutputVariables)
57exu.Print('with a springForceUserFunction, PotentialEnergy is listed:', exu.OutputVariableType.PotentialEnergy in withUserFunction)
58mbs.SetObjectParameter(oSpring, 'springForceUserFunction', 0)
59
60#(2) the types, as single flags
61exu.Print('object types of the rigid body:', Names(mbs.Inspect(oRigid, I.ObjectType)))
62exu.Print('node types of the mass point node:', Names(mbs.Inspect(nMass, I.NodeType)))
63exu.Print('marker types of the node marker:', Names(mbs.Inspect(mMass, I.MarkerType)))
64
65#(3) what is requested, per marker or per node, and the access functions; and everything of one item
66exu.Print('the spring-damper requests per marker:', [Names(perMarker) for perMarker in mbs.Inspect(oSpring, I.RequestedMarkerTypes)])
67exu.Print('the load requests per marker:', [Names(perMarker) for perMarker in mbs.Inspect(lForce, I.RequestedMarkerTypes)])
68exu.Print('the mass point requests per node:', [Names(perNode) for perNode in mbs.Inspect(oMass, I.RequestedNodeTypes)])
69markerRequests = mbs.Inspect(mMass, I.RequestedNodeTypes)[0] #one node; [[Position, Position2D]]
70exu.Print('the node marker requests of its node one of:', [Names(alternatives) for alternatives in markerRequests])
71exu.Print('access functions of the rigid body:', Names(mbs.Inspect(oRigid, I.AccessFunctions)))
72everything = mbs.Inspect(oSpring)
73exu.Print('everything of the spring-damper:', {what.name: answer for (what, answer) in everything.items()})
74exu.Print('a sensor has nothing to inspect:', mbs.Inspect(sSpring))
75
76#(4) refused, with the reason
77refused = 0
78for (itemIndex, what) in [(1, I.OutputVariables), (oSpring, I.AccessFunctions)]:
79 try:
80 mbs.Inspect(itemIndex, what)
81 except (TypeError, ValueError) as error:
82 exu.Print('refused:', str(error).split(' [Python file')[0])
83 refused += 1
84
85testResult = (sum(len(answer) for answer in outputVariables.values()) + len(withUserFunction) + len(everything)
86 + len(mbs.Inspect(oRigid, I.AccessFunctions)) + sum(len(alternatives) for alternatives in markerRequests) + refused)
87exu.Print('solution of inspectTest=', testResult)
88exu.sys['testResult'] = testResult