itemParameterRenamesTest.py
You can view and download this file on Github: itemParameterRenamesTest.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN example
3#
4# Details: The item parameters renamed in Exudyn 1.13 (#2814) - the prefix physics dropped, sphereRadius, the
5# friction forces of CoordinateSpringDamperExt, factor1, rollingViscousFriction, useIntrinsicFormulation,
6# useClassicalFormulation: every old name, as exudyn.types lists them, still works with a DeprecationWarning -
7# given in the dictionary of AddObject, read and written with GetObjectParameter/SetObjectParameter - and
8# reaches the new parameter; a parameter that must be given is given under its old name as well.
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
18import exudyn.types as types
19import exudyn.itemInterface as eii
20import numpy as np
21import re
22import warnings
23
24testIsActive = exu.sys.get('testIsActive', False)
25
26SC = exu.SystemContainer()
27mbs = SC.AddSystem()
28
29def Other(value):
30 """a value different from the given one, of the same kind"""
31 if isinstance(value, bool):
32 return not value
33 if isinstance(value, int):
34 return value + 1
35 if isinstance(value, float):
36 return value + 0.5
37 if value is None: #a list of 3x3 matrices, empty by default (ANCFThinPlate)
38 return 2. * np.eye(3) #one matrix, as a numpy array
39 if len(value) == 0: #a vector, empty by default (ANCFThinPlate.thickness)
40 return np.array([0.1, 0.1, 0.1, 0.1])
41 return np.array(value, dtype=float) + 0.25
42
43def Equal(a, b):
44 return np.allclose(np.array(a, dtype=float), np.array(b, dtype=float))
45
46errors = 0
47nRenames = 0
48with warnings.catch_warnings():
49 warnings.simplefilter('ignore', DeprecationWarning)
50 for name in types.ItemNames():
51 for (old, new) in types.ItemInfo(name).get('deprecatedParameters', {}).items():
52 nRenames += 1
53 data = dict(getattr(eii, name)())
54 value = Other(data[new])
55 del data[new]
56 data[old] = value #only the old name
57 for attempt in range(5): #the other parameters that must be given, given
58 try:
59 item = mbs.AddObject(data)
60 break
61 except Exception as exception:
62 missing = re.search(r'parameter \w+\.(\w+) must be given', str(exception))
63 if missing is None:
64 raise
65 data[missing.group(1)] = 1.
66 if not Equal(mbs.GetObjectParameter(item, new), value):
67 errors += 1
68 exu.Print('itemParameterRenamesTest:', name + '.' + old, 'given in the dictionary does not reach', new)
69 if not Equal(mbs.GetObjectParameter(item, old), value):
70 errors += 1
71 exu.Print('itemParameterRenamesTest:', name + '.' + old, 'is not read from', new)
72 value = Other(value)
73 mbs.SetObjectParameter(item, old, value)
74 if not Equal(mbs.GetObjectParameter(item, new), value):
75 errors += 1
76 exu.Print('itemParameterRenamesTest:', name + '.' + old, 'is not written to', new)
77
78exu.Print('itemParameterRenamesTest:', nRenames, 'old names, errors', errors)
79u = nRenames + errors
80exu.Print('solution of itemParameterRenamesTest=', u)
81
82exu.sys['testResult'] = u