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

[Ctypes] doc

parent 670a2c61
......@@ -243,6 +243,33 @@ class MbsData(object):
Zd : ndarray
Numpy array containing the current values of the speed (spreading)
between of the points of a link.
Example
-------
>>> mbs_data = robotran.MbsData("../dataR/UExampleProject.mbs")
>>> mbs_data.user_model["MyUserModel"]["MyScalar"] = 3.0
>>> print(mbs_data.user_model["MyUserModel"]["MyScalar"])
3.0
>>> #get a copy of the scalar
>>> a = mbs_data.user_model["MyUserModel"]["MyScalar"]
>>> a = 2
>>> print(mbs_data.user_model["MyUserModel"]["MyScalar"])
3.0
>>> print(mbs_data.user_model["MyUserModel"]["MyVector"])
array([1.0, 3.0])
>>> mbs_data.user_model["MyUserModel"]["MyVector"] = [1.0, 2.0]
>>> print(mbs_data.user_model["MyUserModel"]["MyVector"])
array([1.0, 2.0])
>>> b = mbs_data.user_model["MyUserModel"]["MyVector"]
>>> b[1] = 10.0
>>> print(mbs_data.user_model["MyUserModel"]["MyVector"])
array([1.0, 10.0])
>>> b = np.array([2.0, 4.0])
>>> print(mbs_data.user_model["MyUserModel"]["MyVector"])
array([1.0, 10.0])
"""
def __init__(self, name, user_path=None, symbolic_path=None):
......@@ -390,7 +417,7 @@ class MbsData(object):
self.__generate_id__()
#UserModel
self.user_model = _StrictDict()
self.user_model = _UserModelDict()
self.__load_user_model__()
......@@ -412,11 +439,14 @@ class MbsData(object):
def __load_user_fct__(self, user_path=None):
"""
Load all user function where arguments are in MbsData instance.
Load all user functions where arguments are in MbsData instance.
Only load the user functions in which the arguments are not
dependent of another module instance (ie. MbsPart, MbsDirdyn...).
The other user function will be loaded when a module is created.
The other user functions will be loaded when a module is created.
The functions will be assigned to the MbsData instance when
the 'run' function in other modules is called and unassigned
at the end.
The loader user functions are :
- user_derivatives (from derivatives.py)
......@@ -499,12 +529,15 @@ class MbsData(object):
def __load_symb_fct__(self, symb_path=None):
"""
Load all symbolic function if all args are in MbsData instance.
Load all symbolic functions if all args are in MbsData instance.
Only load the symbolic functions in which the arguments are not
dependent of another module instance (ie. MbsPart, MbsDirdyn...).
The other symbolic function will be loaded when a module is
The other symbolic functions will be loaded when a module is
instancied.
The functions will be assigned to the MbsData instance when
the 'run' function in other modules is called and unassigned
at the end.
The loader symbolic functions are :
- mbs_accelred (from mbs_accelred_MBSNAME.py)
......@@ -594,15 +627,14 @@ class MbsData(object):
def __assign_user_fct__(self):
"""
Assign all user function where arguments are in MbsData instance.
Only assign the user functions in which the arguments are not
dependent of another module instance (ie. MbsPart, MbsDirdyn...).
The other user function will be assigned when a module is created.
Assign all user functions if all args are in MbsData instance.
The functions will be assigned to the MbsData instance when
the 'run' function in other modules is called and unassigned
at the end.
Assign and wrap python user functions in which the arguments are
not dependent of another module instance (ie. MbsPart, MbsDirdyn...).
Store the functions in the MbsData instance and assign the pointer
of functions in the C structure.
The other user functions will be assigned when a module is
instancied.
"""
data_c_ptr=self.mbs_data_ptr.contents
......@@ -635,22 +667,15 @@ class MbsData(object):
def __assign_symb_fct__(self):
"""
Assign all symbolic function if all args are in MbsData instance.
Assign all symbolic functions if all args are in MbsData instance.
Only assign the symbolic functions in which the arguments are not
dependent of another module instance (ie. MbsPart, MbsDirdyn...).
The other symbolic function will be assigned when a module is
Assign and wrap python symbolic functions in which the arguments are
not dependent of another module instance (ie. MbsPart, MbsDirdyn...).
Store the functions in the MbsData instance and assign the pointer
of functions in the C structure.
The other user functions will be assigned when a module is
instancied.
Assign and wrap python user functions in which the arguments are not
dependent of another module instance (ie. MbsPart, MbsDirdyn...).
Store the functions in the MbsData instance
and assign the pointer of functions in the C structure
The functions will be assigned to the MbsData instance when
the 'run' function in other modules is called and unassigned
at the end.
"""
data_c_ptr=self.mbs_data_ptr.contents
......@@ -780,7 +805,7 @@ class MbsData(object):
for i in range(infos_c_ptr.user_models.contents.n_user_model):
user_model_list = infos_c_ptr.user_models.contents.user_model_list[i].contents
name = user_model_list.name.decode('utf-8')
self.user_model[name] = _StrictDict()
self.user_model[name] = _UserModelDict()
self.user_model[name]._parent_key = name
self.user_model[name]._locked = False
self.user_model[name]._type = dict()
......@@ -1205,18 +1230,27 @@ class MbsData(object):
return ctypes.string_at(self.mbs_data_ptr.contents.mbs_name).decode("utf-8")
class _StrictDict(dict):
class _UserModelDict(dict):
"""
Class defined for user models, inherites from dict
setitem and getitem function were updated
"""
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
self._locked = True
"flag that locks the addition of a new item"
self._parent_key = None
self._type = None
"key of the parent dict, None if not"
self._type = None
"type of the parameter, None if not"
def __setitem__(self, key, value):
if self._locked == True:
# if it is a parameter
if not (self._type is None):
if self._type[key] == 1:
if type(value) is float:
......
......@@ -47,7 +47,7 @@ from ..mbsysc_loader.callback import mbs_dirdyna_wrap
#==============================================================================
# Global parameter of the current module
#==============================================================================
__DEBUG__ = False
__DEBUG__ = True
__MODULE_DIR__ = os.path.dirname(os.path.abspath(__file__))
......@@ -130,7 +130,7 @@ class MbsDirdyn(object):
# Storing Results
self.results = MbsResult(self.mbs)
self.get_results = True
self.store_results = True
# Exposing some memory
if __DEBUG__ : print( "DEBUG>> Exposing MbsDirdyn fields")
......@@ -277,7 +277,9 @@ class MbsDirdyn(object):
def __assign_user_fct__(self):
"""
Assign user function where some args depend on MbsDirdyn module.
Assign and wrap python user function where some args depend
on MbsDirdyn module. Store the functions in the MbsData instance
and assign the pointer of functions in the C structure
"""
# cons_hJ
self.ptrs_to_user_fcts["user_cons_hJ"] = user_cons_hJ_wrap(lambda h,Jac,mbs,tsim : self.user_cons_hJ(self._huserc, self._Juserc, self.mbs, tsim))
......@@ -303,7 +305,9 @@ class MbsDirdyn(object):
def __assign_symbolic_fct__(self):
"""
Assign symbolic function where some args depend on MbsDirdyn module.
Assign and wrap python symbolic function where some args depend
on MbsDirdyn module. Store the functions in the MbsData instance
and assign the pointer of functions in the C structure
"""
# mbs_cons_hJ
......@@ -377,7 +381,24 @@ class MbsDirdyn(object):
if __DEBUG__ : print("DEBUG>> MbsDirdyn pointer deleted")
def run(self):
"""
Run a direct dynamics analysis
Options can be setted with set_options
Options can be retrieved with get_options
Results are stored in the field results (if store_results == True)
Examples
--------
>>> mbs_data = robotran.MbsData("../dataR/ExampleProject.mbs")
>>> mbs_dirdyn = robotran.MbsDirdyn(mbs_data)
>>> mbs_dirdyn.set_options(t0 = 5, tf = 10)
>>> mbs_dirdyn.get_options("t0", "tf")
(5.0, 10.0)
>>> mbs_dirdyn.run()
"""
# Assing required user functions
self.__assign_user_fct__()
self.mbs.__assign_user_fct__()
......@@ -387,7 +408,7 @@ class MbsDirdyn(object):
self.mbs.__assign_symb_fct__()
if not self.get_results:
if not self.store_results:
libmodules.mbs_run_dirdyn(self.mbs_dirdyn_ptr, self.mbs.mbs_data_ptr)
else:
#save2file forced to 1 because if buffers don't have the complete results, results are loaded from files
......@@ -560,7 +581,7 @@ class MbsDirdyn(object):
n_freeze : int
number of time step when the jacobian is freezed (computed once at the start of the n_freeze time steps)
default = 0
get_results : boolean
store_results : boolean
1 to save a copy of the results from the buffers
default = 1
......@@ -658,8 +679,8 @@ class MbsDirdyn(object):
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 == "get_results":
self.get_results = value
elif key == "store_results":
self.store_results = value
else:
print(">>DIRDYN>> The option "+key+ " is not defined in this module")
......@@ -760,8 +781,8 @@ class MbsDirdyn(object):
options.append(self.mbs_dirdyn_ptr.contents.options.contents.dt_max)
elif key == "n_freeze":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.n_freeze)
elif key == "get_results":
options.append(self.get_results)
elif key == "store_results":
options.append(self.store_results)
else:
print(">>DIRDYN>> The option "+key+ " is not defined in this module")
......
......@@ -173,7 +173,9 @@ class MbsPart(object):
def __assign_user_fct__(self):
"""
Assign user function where some args depend on MbsPart module.
Assign and wrap python user function where some args depend
on MbsPart module. Store the functions in the MbsData instance
and assign the pointer of functions in the C structure
"""
......@@ -184,7 +186,9 @@ class MbsPart(object):
def __assign_symbolic_fct__(self):
"""
Assign symbolic function where some args depend on MbsPart module.
Assign and wrap python symbolic function where some args depend
on MbsPart module. Store the functions in the MbsData instance
and assign the pointer of functions in the C structure
"""
self.ptrs_to_symb_fcts["mbs_cons_hJ"] = mbs_cons_hJ_wrap(lambda h,Jac,mbs,tsim : self.__callback_mbs_cons_hJ(self.mbs_cons_hJ,h,Jac))
......@@ -232,6 +236,23 @@ class MbsPart(object):
return
def run(self):
"""
Run a coordinate partitioning
Options can be setted with set_options
Options can be retrieved with get_options
Examples
--------
>>> mbs_data = robotran.MbsData("../dataR/ExampleProject.mbs")
>>> mbs_part = robotran.MbsPart(mbs_data)
>>> mbs_part.set_options(rowperm = 1, verbose = 0)
>>> mbs_part.get_options("rowperm", "verbose")
(1, 0)
>>> mbs_part.run()
"""
# Assing required user functions
self.__assign_user_fct__()
self.mbs.__assign_user_fct__()
......
Supports Markdown
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