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

WIP function loader outside modules

parent 7b12b996
# -*- coding: utf-8 -*-
"""Portable Python interface to MBsysC using Ctypes.
MBsysC which is a scientific tool dedicated to the analysis of rigid multibody
systems.
"""
__author__ = "Robotran team"
# importing utilities function
from .mbs_utilities import callback_undefined
# importing wrapping function
from ._mbsysc_loader.callback import user_cons_hJ_wrap
from ._mbsysc_loader.callback import user_cons_jdqd_wrap
from ._mbsysc_loader.callback import user_Derivative_wrap
from ._mbsysc_loader.callback import user_DrivenJoints_wrap
from ._mbsysc_loader.callback import user_ExtForces_wrap
from ._mbsysc_loader.callback import user_JointForces_wrap
from ._mbsysc_loader.callback import user_LinkForces_wrap
from ._mbsysc_loader.callback import user_Link3DForces_wrap
from ._mbsysc_loader.callback import user_dirdyn_init_wrap
from ._mbsysc_loader.callback import user_dirdyn_loop_wrap
from ._mbsysc_loader.callback import user_dirdyn_finish_wrap
from ._mbsysc_loader.callback import user_equil_init_wrap
from ._mbsysc_loader.callback import user_equil_loop_wrap
from ._mbsysc_loader.callback import user_equil_finish_wrap
from ._mbsysc_loader.callback import user_equil_fxe_wrap
def assign_user_fct_to_None(module, functions=None):
"""
Parameters
----------
module : TYPE
DESCRIPTION.
Returns
-------
None.
"""
if functions is not None:
for fun in functions:
if fun == "cons_hJ":
module.user_cons_hJ = None
elif fun == "cons_jdqd":
module.user_cons_jdqd = None
elif fun == "derivative":
module.user_derivative = None
elif fun == "DrivenJoints":
module.user_DrivenJoints = None
elif fun == "ExtForces":
module.user_ExtForces = None
elif fun == "JointForces":
module.user_JointForces = None
elif fun == "LinkForces":
module.user_LinkForces = None
elif fun == "Link3DForces":
module.user_Link3DForces = None
elif fun == "dirdyn_init":
module.user_dirdyn_init = None
elif fun == "dirdyn_loop":
module.user_dirdyn_loop = None
elif fun == "dirdyn_finish":
module.user_dirdyn_finish = None
elif fun == "equil_init":
module.user_equil_init = None
elif fun == "equil_loop":
module.user_dequil_loop = None
elif fun == "equil_finish":
module.user_equil_finish = None
else:
print(fun + " is not an existing user function")
else:
# User function that only relies on MbsData structure
module.user_cons_hJ = None
module.user_cons_jdqd = None
module.user_derivative = None
module.user_DrivenJoints = None
module.user_ExtForces = None
module.user_JointForces = None
module.user_LinkForces = None
module.user_Link3DForces = None
# User function also related to dirdyn, equil...
module.user_dirdyn_init = None
module.user_dirdyn_loop = None
module.user_dirdyn_finish = None
def iassign_symb_fct_to_None(module, functions=None):
"""
Parameters
----------
module : TYPE
DESCRIPTION.
Returns
-------
None.
"""
if functions is not None:
for fun in functions:
if fun == "accelred":
module.mbs_accelred = None
elif fun == "cons_hJ":
module.mbs_cons_hJ = None
elif fun == "cons_jdqd":
module.mbs_cons_jdqd = None
elif fun == "dirdyna":
module.mbs_dirdyna = None
elif fun == "extforces":
module.mbs_extforces = None
elif fun == "gensensor":
module.mbs_gensensor = None
elif fun == "invdyna":
module.mbs_invdyna = None
elif fun == "link":
module.mbs_link = None
elif fun == "link3D":
module.mbs_link3D = None
elif fun == "sensor":
module.mbs_sensor = None
else:
print(fun + " is not an existing symbolic function")
else:
module.mbs_accelred = None
module.mbs_cons_hJ = None
module.mbs_cons_jdqd = None
module.mbs_dirdyna = None
module.mbs_extforces = None
module.mbs_gensensor = None
module.mbs_invdyna = None
module.mbs_link = None
module.mbs_link3D = None
module.mbs_sensor = None
def __unassign_user_fct_2__(module, functions=None):
"""
Unassign all user function if all args are in MbsData instance.
Only unassign the user functions in which the arguments are not
dependent of another module instance (ie. MbsPart, MbsDirdyn...).
"""
print("unassign 2")
# Not necessary but if not present, dictionary must be filled in the
# load function and not the assign function.
module.ptrs_to_user_fcts.clear()
__assign_user_to_undefined__(module, functions)
assign_user_fct_to_None(module)
def __assign_user_to_undefined__(module, functions=None):
if functions is not None:
for fun in functions:
if fun == "cons_hJ":
module.mbs.mbs_data_ptr.contents.user_cons_hJ = user_cons_hJ_wrap(lambda h, Jac, mbs, tsim: callback_undefined("user_cons_hJ"))
elif fun == "cons_jdqd":
module.mbs.mbs_data_ptr.contents.user_cons_jdqd = user_cons_jdqd_wrap(lambda h, Jac, mbs, tsim: callback_undefined("user_cons_jdqd"))
elif fun == "derivative":
module.mbs_data_ptr.contents.user_Derivative = user_Derivative_wrap(lambda mbs: callback_undefined("user_Derivative"))
elif fun == "DrivenJoints":
module.mbs_data_ptr.contents.user_DrivenJoints = user_DrivenJoints_wrap(lambda mbs, t: callback_undefined("user_DrivenJoints"))
elif fun == "ExtForces":
module.mbs_data_ptr.contents.user_ExtForces = user_ExtForces_wrap(lambda PxF, RxF, VxF, OMxF, AxF, OMPxF, mbs, tsim, ixF: callback_undefined("user_ExtForces"))
elif fun == "JointForces":
module.mbs_data_ptr.contents.user_JointForces = user_JointForces_wrap(lambda mbs, t: callback_undefined("user_JointForces"))
elif fun == "LinkForces":
module.mbs_data_ptr.contents.user_LinkForces = user_LinkForces_wrap(lambda Z, Zd, mbs, tsim, ilnk: callback_undefined("user_LinkForces"))
elif fun == "Link3DForces":
module.mbs_data_ptr.contents.user_Link3DForces = user_Link3DForces_wrap(lambda PxF, RxF, VxF, OMxF, AxF, OMPxF, mbs, tsim, ixF: callback_undefined("user_Link3DForces"))
elif fun == "dirdyn_init":
module.mbs.mbs_data_ptr.contents.user_dirdyn_init = user_dirdyn_init_wrap(lambda mbs, dd: callback_undefined("user_dirdyn_init"))
elif fun == "dirdyn_loop":
module.mbs.mbs_data_ptr.contents.user_dirdyn_loop = user_dirdyn_loop_wrap(lambda mbs, dd: callback_undefined("user_dirdyn_loop"))
elif fun == "dirdyn_finish":
module.mbs.mbs_data_ptr.contents.user_dirdyn_finish = user_dirdyn_finish_wrap(lambda mbs, dd: callback_undefined("user_dirdyn_finish"))
elif fun == "equil_init":
module.mbs.mbs_data_ptr.contents.user_equil_init = user_equil_init_wrap(lambda mbs, eq: callback_undefined("user_equil_init"))
elif fun == "equil_loop":
module.mbs.mbs_data_ptr.contents.user_equil_loop = user_equil_loop_wrap(lambda mbs, eq: callback_undefined("user_equil_loop"))
elif fun == "equil_finish":
module.mbs.mbs_data_ptr.contents.user_equil_finish = user_equil_finish_wrap(lambda mbs, eq: callback_undefined("user_equil_finish"))
elif fun == "equil_fxe":
module.mbs.mbs_data_ptr.contents.user_equil_fxe = user_equil_fxe_wrap(lambda mbs, f: callback_undefined("user_equil_fxe"))
else:
print(fun + " is not an existing user function")
else:
module.mbs_data_ptr.contents.user_Derivative = user_Derivative_wrap(lambda mbs: callback_undefined("user_Derivative"))
module.mbs_data_ptr.contents.user_DrivenJoints = user_DrivenJoints_wrap(lambda mbs, t: callback_undefined("user_DrivenJoints"))
module.mbs_data_ptr.contents.user_ExtForces = user_ExtForces_wrap(lambda PxF, RxF, VxF, OMxF, AxF, OMPxF, mbs, tsim, ixF: callback_undefined("user_ExtForces"))
module.mbs_data_ptr.contents.user_JointForces = user_JointForces_wrap(lambda mbs, t: callback_undefined("user_JointForces"))
module.mbs_data_ptr.contents.user_LinkForces = user_LinkForces_wrap(lambda Z, Zd, mbs, tsim, ilnk: callback_undefined("user_LinkForces"))
module.mbs_data_ptr.contents.user_Link3DForces = user_Link3DForces_wrap(lambda PxF, RxF, VxF, OMxF, AxF, OMPxF, mbs, tsim, ixF: callback_undefined("user_Link3DForces"))
......@@ -13,14 +13,14 @@ Copyright 2019 Universite Catholique de Louvain
# Packages loading
#==============================================================================
import sys, time
sys.path.insert(1,"./../../../..") # Relative path for this example script
sys.path.insert(1,"./../../../../../build/python") # Relative path for this example script
import MBsysPy as robotran
#==============================================================================
# Project loading
#==============================================================================
print("\n>>PYTHON>> Loading Project!")
mbs_data = robotran.MbsData("../dataR/Bodies_and_Joints.mbs",
mbs_data = robotran.MbsData("../dataR/Bodies_and_Joints.mbs",
prj_functions_c=None, prj_lib_path="..")
print(mbs_data)
......
......@@ -43,6 +43,11 @@ from .._mbsysc_loader.loadlibs import libloadXML
# importing utilities function
from ..mbs_utilities import callback_undefined
# importing function handler
from .._mbs_function_handler import init_user_fct
from .._mbs_function_handler import init_symb_fct
from .._mbs_function_handler import __unassign_user_fct_2__
# importing wrapping function
from .._mbsysc_loader.callback import user_Derivative_wrap
from .._mbsysc_loader.callback import user_DrivenJoints_wrap
......@@ -392,30 +397,32 @@ class MbsData(object):
# Storing project function pointer
# User function that only relies on MbsData structure
self.user_cons_hJ = None
self.user_cons_jdqd = None
self.user_derivative = None
self.user_DrivenJoints = None
self.user_ExtForces = None
self.user_JointForces = None
self.user_LinkForces = None
self.user_Link3DForces = None
# self.user_cons_hJ = None
# self.user_cons_jdqd = None
# self.user_derivative = None
# self.user_DrivenJoints = None
# self.user_ExtForces = None
# self.user_JointForces = None
# self.user_LinkForces = None
# self.user_Link3DForces = None
init_user_fct(self)
# User function also related to dirdyn, equil...
self.user_dirdyn_init = None
self.user_dirdyn_loop = None
self.user_dirdyn_finish = None
# self.user_dirdyn_init = None
# self.user_dirdyn_loop = None
# self.user_dirdyn_finish = None
# Symbolic functions
self.mbs_accelred = None
self.mbs_cons_hJ = None
self.mbs_cons_jdqd = None
self.mbs_dirdyna = None
self.mbs_extforces = None
self.mbs_gensensor = None
self.mbs_invdyna = None
self.mbs_link = None
self.mbs_link3D = None
self.mbs_sensor = None
# self.mbs_accelred = None
# self.mbs_cons_hJ = None
# self.mbs_cons_jdqd = None
# self.mbs_dirdyna = None
# self.mbs_extforces = None
# self.mbs_gensensor = None
# self.mbs_invdyna = None
# self.mbs_link = None
# self.mbs_link3D = None
# self.mbs_sensor = None
init_symb_fct(self)
# pointers dict to avoid garbage collecting
self.ptrs_to_user_fcts = dict()
......@@ -862,16 +869,17 @@ class MbsData(object):
Only unassign the user functions in which the arguments are not
dependent of another module instance (ie. MbsPart, MbsDirdyn...).
"""
# Not necessary but if not present, dictionary must be filled in the
# load function and not the assign function.
self.ptrs_to_user_fcts = {}
self.mbs_data_ptr.contents.user_Derivative = user_Derivative_wrap(lambda mbs: callback_undefined("user_Derivative"))
self.mbs_data_ptr.contents.user_DrivenJoints = user_DrivenJoints_wrap(lambda mbs, t: callback_undefined("user_DrivenJoints"))
self.mbs_data_ptr.contents.user_ExtForces = user_ExtForces_wrap(lambda PxF, RxF, VxF, OMxF, AxF, OMPxF, mbs, tsim, ixF: callback_undefined("user_ExtForces"))
self.mbs_data_ptr.contents.user_JointForces = user_JointForces_wrap(lambda mbs, t: callback_undefined("user_JointForces"))
self.mbs_data_ptr.contents.user_LinkForces = user_LinkForces_wrap(lambda Z, Zd, mbs, tsim, ilnk: callback_undefined("user_LinkForces"))
self.mbs_data_ptr.contents.user_Link3DForces = user_Link3DForces_wrap(lambda PxF, RxF, VxF, OMxF, AxF, OMPxF, mbs, tsim, ixF: callback_undefined("user_Link3DForces"))
__unassign_user_fct_2__(self)
# # Not necessary but if not present, dictionary must be filled in the
# # load function and not the assign function.
# self.ptrs_to_user_fcts = {}
# self.mbs_data_ptr.contents.user_Derivative = user_Derivative_wrap(lambda mbs: callback_undefined("user_Derivative"))
# self.mbs_data_ptr.contents.user_DrivenJoints = user_DrivenJoints_wrap(lambda mbs, t: callback_undefined("user_DrivenJoints"))
# self.mbs_data_ptr.contents.user_ExtForces = user_ExtForces_wrap(lambda PxF, RxF, VxF, OMxF, AxF, OMPxF, mbs, tsim, ixF: callback_undefined("user_ExtForces"))
# self.mbs_data_ptr.contents.user_JointForces = user_JointForces_wrap(lambda mbs, t: callback_undefined("user_JointForces"))
# self.mbs_data_ptr.contents.user_LinkForces = user_LinkForces_wrap(lambda Z, Zd, mbs, tsim, ilnk: callback_undefined("user_LinkForces"))
# self.mbs_data_ptr.contents.user_Link3DForces = user_Link3DForces_wrap(lambda PxF, RxF, VxF, OMxF, AxF, OMPxF, mbs, tsim, ixF: callback_undefined("user_Link3DForces"))
def __unassign_symb_fct__(self):
"""
......
......@@ -390,7 +390,7 @@ class MbsDirdyn(object):
def __unassign_user_fct__(self):
"""Unassign user function where some args depend on MbsDirdyn module."""
self.ptrs_to_user_fcts = {}
self.ptrs_to_user_fcts.clear()
self.mbs.user_cons_hJ = None
self.mbs.mbs_data_ptr.contents.user_cons_hJ = user_cons_hJ_wrap(lambda h, Jac, mbs, tsim: callback_undefined("user_cons_hJ"))
......@@ -405,7 +405,7 @@ class MbsDirdyn(object):
def __unassign_symbolic_fct__(self):
"""Unassign symbolic function where some args depend on MbsDirdyn module."""
self.ptrs_to_symb_fcts = {}
self.ptrs_to_symb_fcts.clear()
self.mbs.mbs_cons_hJ = None
self.mbs.mbs_data_ptr.contents.mbs_cons_hJ = mbs_cons_hJ_wrap(lambda h, Jac, mbs, tsim: callback_undefined("mbs_cons_hJ"))
......
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