Commit a17530a1 authored by Louis Beauloye's avatar Louis Beauloye
Browse files

type conversion in MbsDirdyn ok

parent d48094dd
......@@ -680,117 +680,183 @@ class MbsDirdyn(object):
If set to 1, an animation of the Newton-Raphson procedure is generated
if the closure fails.
default is 0
store_results: boolean
store_results: int
If set to 1, a copy of the results is done.
default is 1
"""
for key, value in kwargs.items():
if key == "t0":
self.mbs_dirdyn_ptr.contents.options.contents.t0 = value
elif key == "tf":
self.mbs_dirdyn_ptr.contents.options.contents.tf = value
elif key == "dt0":
self.mbs_dirdyn_ptr.contents.options.contents.dt0 = value
elif key == "save2file":
self.mbs_dirdyn_ptr.contents.options.contents.save2file = value
elif key == "resfilename":
self.mbs_dirdyn_ptr.contents.options.contents.resfilename = str_to_bytes(value)
elif key == "respath":
self.mbs_dirdyn_ptr.contents.options.contents.respath = str_to_bytes(value)
elif key == "animpath":
self.mbs_dirdyn_ptr.contents.options.contents.animpath = str_to_bytes(value)
elif key == "save_anim":
self.mbs_dirdyn_ptr.contents.options.contents.save_anim = value
elif key == "save_visu":
self.mbs_dirdyn_ptr.contents.options.contents.save_visu = value
elif key == "framerate":
self.mbs_dirdyn_ptr.contents.options.contents.framerate = value
elif key == "saveperiod":
self.mbs_dirdyn_ptr.contents.options.contents.saveperiod = value
elif key == "max_save_user":
self.mbs_dirdyn_ptr.contents.options.contents.max_save_user = value
elif key == "buffersize":
self.mbs_dirdyn_ptr.contents.options.contents.buffersize = value
elif key == "realtime":
if value != 0:
mbs_warning('Realtime option is not working with pre-compiled'
'libraries(as included in Pip package)!')
if self.mbs.opt_load_c < 2:
mbs_error('Realtime simulation requires to provide a C-compiled'
'libraries of the user functions!')
self.mbs_dirdyn_ptr.contents.options.contents.realtime = value
elif key == "accelred":
self.mbs_dirdyn_ptr.contents.options.contents.accelred = value
elif key == "flag_compute_Qc":
self.mbs_dirdyn_ptr.contents.options.contents.flag_compute_Qc = value
elif key == "compute_all_uxd":
self.mbs_dirdyn_ptr.contents.options.contents.compute_all_uxd = value
elif key == "compute_Qc":
if(type(value) is np.ndarray) or(type(value) is list):
if np.size(value) == self.mbs.mbs_data_ptr.contents.njoint + 1:
for i, val in enumerate(value[1:]):
self.mbs_dirdyn_ptr.contents.options.contents.compute_Qc[i + 1] = val
else:
print(">>DIRDYN>> The size of compute_Qc is not consistent with njoint")
def __realtime__(mbs_dirdyn, value):
"""Cast value into integer and set it to the option 'realtime' in C-memory.
Parameters
----------
mbs_dirdyn : MbsDirdyn
Instance of MbsDirdyn.
value : int
Value of the realtime option.
See doc of set_options() for more information about realtime
Returns
-------
None.
"""
if value != 0:
mbs_warning('Realtime option is not working with pre-compiled '
'libraries(as included in Pip package)!')
if mbs_dirdyn.mbs.opt_load_c < 2:
mbs_error('Realtime simulation requires to provide a C-compiled '
'libraries of the user functions!')
mbs_dirdyn.mbs_dirdyn_ptr.contents.options.contents.realtime = int(value)
def __int_2_integrator__(value):
"""Set value into the C-memory as integrator option.
Parameters
----------
value : int or str
Value corresponding to an integrator type.
See doc of set_options() for more information about integrator
Raises
------
ValueError
If value does not correspond to a valid integrator.
TypeError
If value does not have the right type.
Returns
-------
int
Integer from 0 to 8 corresponding to an integrator.
"""
if(type(value) is int):
if (value >= 0 and value <= 8):
return value
else:
print(">>DIRDYN>> The specified value for compute_Qc is not a list or a np.array")
elif key == "integrator":
if(type(value) is int) and(value >= 0 and value <= 8):
self.mbs_dirdyn_ptr.contents.options.contents.integrator = value
elif type(value) is str:
if value == "RK4":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 0
elif value == "Dopri5":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 1
elif value == "Rosenbrock":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 2
elif value == "EulerEx":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 3
elif value == "Eulaire":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 4
elif value == "EulerIm":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 5
elif value == "Bader":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 6
elif value == "WMethods":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 7
elif value == "Custom":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 8
else:
print(">>DIRDYN>> " + value + " is not an integrator")
raise ValueError
elif type(value) is str:
if value == "RK4":
return 0
elif value == "Dopri5":
return 1
elif value == "Rosenbrock":
return 2
elif value == "EulerEx":
return 3
elif value == "Eulaire":
return 4
elif value == "EulerIm":
return 5
elif value == "Bader":
return 6
elif value == "WMethods":
return 7
elif value == "Custom":
return 8
else:
print(">>DIRDYN>> " + value + " is not an integrator")
elif key == "verbose":
self.mbs_dirdyn_ptr.contents.options.contents.verbose = value
elif key == "flag_stop_stiff":
self.mbs_dirdyn_ptr.contents.options.contents.flag_stop_stiff = value
elif key == "flag_precise_dynamics":
self.mbs_dirdyn_ptr.contents.options.contents.flag_precise_dynamics = value
elif key == "flag_waypoint":
self.mbs_dirdyn_ptr.contents.options.contents.flag_waypoint = value
elif key == "flag_solout_wp":
self.mbs_dirdyn_ptr.contents.options.contents.flag_solout_wp = value
elif key == "delta_t_wp":
self.mbs_dirdyn_ptr.contents.options.contents.delta_t_wp = value
elif key == "nmax":
self.mbs_dirdyn_ptr.contents.options.contents.nmax = value
elif key == "toler":
self.mbs_dirdyn_ptr.contents.options.contents.toler = value
elif key == "rtoler":
self.mbs_dirdyn_ptr.contents.options.contents.rtoler = value
elif key == "atoler":
self.mbs_dirdyn_ptr.contents.options.contents.atoler = value
elif key == "dt_max":
self.mbs_dirdyn_ptr.contents.options.contents.dt_max = value
elif key == "n_freeze":
self.mbs_dirdyn_ptr.contents.options.contents.n_freeze = value
elif key == "show_failed_closure":
self.mbs_dirdyn_ptr.contents.options.contents.show_failed_closure = value
elif key == "store_results":
self.store_results = value
raise ValueError
else:
print(">>DIRDYN>> The option " + key + " is not defined in this module")
raise TypeError
def __compute_Qc__(mbs_dirdyn, value):
"""Set value into the C-memory as compute_Qc option.
Parameters
----------
mbs_dirdyn : MbsDirdyn
Instance of MbsDirdyn.
value : list or numpy.ndarray
Value of compute_Qc option.
See doc of set_options() for more information about compute_Qc
Raises
------
ValueError
If value does not have the right size.
TypeError
If value is not a list or a numpy.ndarray.
Returns
-------
None.
"""
if(type(value) is np.ndarray) or (type(value) is list):
if np.size(value) == mbs_dirdyn.mbs.mbs_data_ptr.contents.njoint + 1:
for i, val in enumerate(value[1:]):
mbs_dirdyn.mbs_dirdyn_ptr.contents.options.contents.compute_Qc[i + 1] = val
else:
raise ValueError
else:
raise TypeError
options = {'t0': {'convert': float, 'c_name': 't0'},
'tf': {'convert': float, 'c_name': 'tf'},
'dt0': {'convert': float, 'c_name': 'dt0'},
'save2file': {'convert': int, 'c_name': 'save2file'},
'resfilename': {'convert': str_to_bytes, 'c_name': 'resfilename'},
'respath': {'convert': str_to_bytes, 'c_name': 'respath'},
'animpath': {'convert': str_to_bytes, 'c_name': 'animpath'},
'save_anim': {'convert': int, 'c_name': 'save_anim'},
'save_visu': {'convert': int, 'c_name': 'save_visu'},
'framerate': {'convert': int, 'c_name': 'framerate'},
'saveperiod': {'convert': int, 'c_name': 'saveperiod'},
'max_save_user': {'convert': int, 'c_name': 'max_save_user'},
'buffersize': {'convert': int, 'c_name': 'buffersize'},
'realtime': {'convert': int, 'c_name': 'realtime'},
'accelred': {'convert': int, 'c_name': 'accelred'},
'flag_compute_Qc': {'convert': int, 'c_name': 'flag_compute_Qc'},
'compute_all_uxd': {'convert': int, 'c_name': 'compute_all_uxd'},
'compute_Qc': {'convert': 'list or numpy.ndarray', 'c_name': 'compute_Qc'},
'integrator': {'convert': __int_2_integrator__, 'c_name': 'integrator'},
'verbose': {'convert': int, 'c_name': 'verbose'},
'flag_stop_stiff': {'convert': int, 'c_name': 'flag_stop_stiff'},
'flag_precise_dynamics': {'convert': int, 'c_name': 'flag_precise_dynamics'},
'flag_waypoint': {'convert': int, 'c_name': 'flag_waypoint'},
'flag_solout_wp': {'convert': int, 'c_name': 'flag_solout_wp'},
'delta_t_wp': {'convert': float, 'c_name': 'delta_t_wp'},
'nmax': {'convert': int, 'c_name': 'nmax'},
'toler': {'convert': float, 'c_name': 'toler'},
'rtoler': {'convert': float, 'c_name': 'rtoler'},
'atoler': {'convert': float, 'c_name': 'atoler'},
'dt_max': {'convert': float, 'c_name': 'dt_max'},
'n_freeze': {'convert': int, 'c_name': 'n_freeze'},
'show_failed_closure': {'convert': int, 'c_name': 'show_failed_closure'},
'store_results': {'convert': int, 'c_name': 'store_results'},
}
for key, value in kwargs.items():
if key not in options:
raise TypeError("{:} is an invalid option name.".format(key))
try:
c_name = options[key]['c_name']
if key == 'realtime':
__realtime__(self, value)
elif key == 'compute_Qc':
__compute_Qc__(self, value)
elif key == 'store_results':
self.store_results = int(value)
else:
setattr(self.mbs_dirdyn_ptr.contents.options.contents, c_name, options[key]['convert'](value))
except ValueError as err:
# if wrong value in integrator or compute_QC
if key == 'integrator':
raise ValueError('>>DIRDYN>> {:} is not a valid integrator'.format(value)).with_traceback(err.__traceback__) from None
elif key == 'compute_Qc':
raise ValueError('>>DIRDYN>> The size of compute_Qc is not consistent with njoint').with_traceback(err.__traceback__) from None
# if error during the cast
else:
raise TypeError("{:} is {:}, can not be casted from {:}.".format(key, options[key]['convert'], type(value))).with_traceback(err.__traceback__)
except AttributeError as err:
# if error during cast of strings (i.e in str_to_bytes)
raise TypeError("{:} is str, {:} can not be casted.".format(key, type(value))).with_traceback(err.__traceback__)
except TypeError as err:
# if wrong type in integrator or compute_QC
raise TypeError("{:} is {:}, can not be casted from {:}.".format(key, options[key]['convert'], type(value))).with_traceback(err.__traceback__) from None
def get_options(self, *args):
"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment