Commit 65ec95c9 authored by Louis Beauloye's avatar Louis Beauloye
Browse files

[Ctypes] variable exchange equil module

parent a781f2ed
......@@ -152,6 +152,11 @@ libmodules.mbs_equil_exchange.restypes = None
libmodules.mbs_print_equil.argtypes = [ctypes.POINTER(MbsEquil_c)]
libmodules.mbs_print_equil.restypes = None
libmodules.mbs_equil_set_variable.argtypes = [ctypes.POINTER(MbsEquilOptions_c),
ctypes.c_void_p,
ctypes.c_int, ctypes.c_int]
libmodules.mbs_equil_set_variable.restypes = None
libmodules.mbs_new_modal.argtypes =[ctypes.POINTER(MbsData_c)]
libmodules.mbs_new_modal.restype = ctypes.POINTER(MbsModal_c)
......
......@@ -373,6 +373,7 @@ class MbsData(object):
self.__frc = np.ctypeslib.as_array(self.mbs_data_ptr.contents.frc[0], (3+1, self.njoint+1))
self.__trq = np.ctypeslib.as_array(self.mbs_data_ptr.contents.trq[0], (3+1, self.njoint+1))
self.__Qq = np.ctypeslib.as_array(self.mbs_data_ptr.contents.Qq, (self.njoint+1,))
self.__Qa = np.ctypeslib.as_array(self.mbs_data_ptr.contents.Qa, (self.njoint+1,))
# Constraints datas
self.nrod = 0
......@@ -1137,6 +1138,10 @@ class MbsData(object):
def trq(self):
return self.__trq
@property
def Qa(self):
return self.__Qa
@property
def Qq(self):
return self.__Qq
......
......@@ -111,6 +111,8 @@ class MbsEquil(object):
self.mbs_cons_jdqd = None
self.mbs_invdyna = None
# exchange of equilibrium variables
self.exchange_list = []
# Exposing some memory
if __DEBUG__ : print( "DEBUG>> Exposing MbsEquil fields")
......@@ -360,7 +362,7 @@ class MbsEquil(object):
self.__assign_symbolic_fct__()
self.mbs.__assign_symb_fct__()
self._compute_exchange()
libmodules.mbs_run_equil(self.mbs_equil_ptr, self.mbs.mbs_data_ptr)
# Anassign user functions
......@@ -372,10 +374,113 @@ class MbsEquil(object):
self.mbs.__unassign_symb_fct__()
print(">>EQUIL>> MbsData structure contains the results of the equilibrium")
def exchange(self):
libmodules.mbs_equil_exchange(self.mbs_equil_ptr.contents.options)
def add_exchange(self, ptr, index, replaced_var_id):
""" Add a new equilibrium variable that replaces the specified
default one.
Exchanged variables are stored and will be set in the memory of the
external library when 'MbsEquil::run()' is called. Between two run()
the equilibrium can be modified thanks to replace().
However as the number of equilibrium variables is locked in the
external memory after the first call of run(), this function can not
be called between two run().
Parameters
----------
ptr : ndarray
Array containing the variable to be exchanged
index : int
Index in ptr of the variable to be exchanged
replaced_var_id : int
Index of the default variable that will be replaced
"""
if self.get_options("nquch"):
print("as the number of equilibrium variables is locked in the external memory after the first call of run(), this function can not be called between two run()")
if index > np.size(ptr)-1:
print(">>EQUIL>> variable exchange : Index out of bound")
return
address = ptr.ctypes.data + ctypes.sizeof(ctypes.c_double)*index
if ptr.ctypes.data == self.mbs.q.ctypes.data:
print(">>EQUIL>> variable exchange : Warning ! Generalized coordinates have to be used with caution as exchange variables")
if len(self.exchange_list) == 0:
self.exchange_list = [[address, replaced_var_id]]
else:
for element in self.exchange_list:
if element[0] == address:
print(">>EQUIL>> variable exchange : Exchange variable is already an equilibrium variable")
else:
self.exchange_list.append([address, replaced_var_id])
def add_exchange_um(self, user_model, param, replaced_var_id):
""" Add a user model as a new equilibrium variable that replaces
the specified default one
Parameters
----------
user_model : str
Name of the user model to be exchanged
param : str
Parameter of the user model to be exchanged
replaced_var_id : int
Index of the default variable that will be replaced
"""
if self.mbs.user_model[user_model]._type[param] != 1 and self.mbs.user_model[user_model]._type[param] != 7:
print(">>EQUIL>> variable exchange : This function does not handle this type of user model (only float and int)")
return
address = self.mbs.user_model[user_model].__getarray__(param)[0].ctypes.data
print(address)
if len(self.exchange_list) == 0:
self.exchange_list = [[address, replaced_var_id]]
else:
for element in self.exchange_list:
if element[0] == address:
print(">>EQUIL>> variable exchange : Exchange variable is already an equilibrium variable")
else:
self.exchange_list.append([address, replaced_var_id])
def _compute_exchange(self):
if self.get_options("nquch") == 0 and len(self.exchange_list) > 0:
self.mbs_equil_ptr.contents.options.contents.nquch = len(self.exchange_list)
libmodules.mbs_equil_exchange(self.mbs_equil_ptr.contents.options)
for i, element in enumerate(self.exchange_list):
libmodules.mbs_equil_set_variable(self.mbs_equil_ptr.contents.options, element[0],i+1, element[1])
if self.get_options("nquch") == len(self.exchange_list) and len(self.exchange_list) > 0:
for i, element in enumerate(self.exchange_list):
libmodules.mbs_equil_set_variable(self.mbs_equil_ptr.contents.options, element[0],i+1, element[1])
elif self.get_options("nquch") != len(self.exchange_list):
print("Crash")
def replace_exchange(self, ptr, index, id_exchanged):
if ptr.ctypes.data == self.mbs.q.ctypes.data:
print(">>EQUIL>> variable exchange : Warning generalized coordinates have to be used with caution as exchange variables")
if id_exchanged > len(self.exchange_list) or id_exchanged <=0 :
print(">>EQUIL>> variable exchange : There is no exchange variable at this index :" + str(id_exchanged))
else:
self.exchange_list[id_exchanged-1][0] = ptr.ctypes.data + ctypes.sizeof(ctypes.c_double)*index
def replace_exchange_um(self, user_model, param, id_exchanged):
if self.mbs.user_model[user_model]._type[param] != 1 and self.mbs.user_model[user_model]._type[param] != 7:
print(">>EQUIL>> variable exchange : This function does not handle this type of user model (only float and int)")
return
if id_exchanged > len(self.exchange_list) or id_exchanged <=0 :
print(">>EQUIL>> variable exchange : There is no exchange variable at this index :" + str(id_exchanged))
else:
self.exchange_list[id_exchanged-1][0] = self.mbs.user_model[user_model].__getarray__(param)[0].ctypes.data
def print_equil(self):
......@@ -622,7 +727,7 @@ class MbsEquil(object):
elif key == "clearmbsglobal":
options.append(self.mbs_equil_ptr.contents.options.contents.clearmbsglobal)
elif key == "nquch":
print(">>EQUIL>> Option " + key + " not yet implemented in python")
options.append(self.mbs_equil_ptr.contents.options.contents.nquch)
elif key == "quch":
print(">>EQUIL>> Option " + key + " not yet implemented in python")
elif key == "xch_ptr":
......
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