Commit 12acd910 authored by Louis Beauloye's avatar Louis Beauloye
Browse files

[Ctypes] results in modules

parent 86d21b79
......@@ -108,7 +108,7 @@ mbs_data.mbs_reset_data()
mbs_data.set_qa(mbs_data.joint_id["R2_pendulum"])
mbs_equil = Robotran.MbsEquil(mbs_data)
mbs_equil.add_extra_variable_um("mylink","L0")
mbs_equil.replace_extra_variable(mbs_data.Qa,mbs_data.joint_id["R2_pendulum"],1)
mbs_equil.replace_extra_variable(mbs_data.Qa,mbs_data.joint_id["R2_pendulum"],1,"Qa")
mbs_equil.set_options(senstol = 1e-6, compute_uxd = 0, resfilename = "equil6")
mbs_equil.set_options(grad_lpk = 1)
mbs_equil.run()
......@@ -124,7 +124,7 @@ mbs_equil = Robotran.MbsEquil(mbs_data)
mbs_data.qd[mbs_data.joint_id["T1_cart"]] = 5
mbs_equil.set_options(senstol = 1e-6, compute_uxd = 0, resfilename = "equil7")
mbs_equil.set_options(verbose = 1, grad_lpk = 1, mode = 2)
mbs_equil.add_exchange(mbs_data.Qa,mbs_data.joint_id["T1_cart"], mbs_data.joint_id["T1_cart"])
mbs_equil.add_exchange(mbs_data.Qa,mbs_data.joint_id["T1_cart"], mbs_data.joint_id["T1_cart"],"Qa")
mbs_equil.run()
mbs_equil.print_equil()
mbs_data.unset_qa(mbs_data.joint_id["T1_cart"])
......@@ -1012,6 +1012,8 @@ class MbsData(object):
param : options of the dirdyn module (see set_options in dirdyn module)
out : MbsResults instance
"""
if (not self.DonePart) and self.Ncons:
mbs_part = MbsPart(self)
......@@ -1019,11 +1021,13 @@ class MbsData(object):
mbs_part.run()
mbs_dirdyn = MbsDirdyn(self)
mbs_dirdyn.set_options(**kwargs)
mbs_dirdyn.run()
results = mbs_dirdyn.run()
else:
mbs_dirdyn = MbsDirdyn(self)
mbs_dirdyn.set_options(**kwargs)
mbs_dirdyn.run()
results = mbs_dirdyn.run()
return results
def exe_part(self, **kwargs):
""" Run a partitioning module with the specified options
......
......@@ -455,6 +455,8 @@ class MbsDirdyn(object):
# Anassing required symbolic functions
self.__unassign_symbolic_fct__()
self.mbs.__unassign_symb_fct__()
return self.results
def set_options(self, **kwargs):
"""
......
......@@ -117,6 +117,10 @@ class MbsEquil(object):
# addition of equilibrium variables
self.addition_list = []
# storing result
self.store_results = True
self.results = MbsResultEquil()
# Exposing some memory
if __DEBUG__ : print( "DEBUG>> Exposing MbsEquil fields")
......@@ -383,6 +387,10 @@ class MbsEquil(object):
self._compute_exchange()
self._compute_extra_variable()
libmodules.mbs_run_equil(self.mbs_equil_ptr, self.mbs.mbs_data_ptr)
if self.store_results:
self.load_results()
# Anassign user functions
self.__unassign_user_fct__()
self.mbs.__unassign_user_fct__()
......@@ -391,9 +399,46 @@ class MbsEquil(object):
self.__unassign_symbolic_fct__()
self.mbs.__unassign_symb_fct__()
return self.results
print(">>EQUIL>> MbsData structure contains the results of the equilibrium")
def add_exchange(self, ptr, index, replaced_var_id):
def load_results(self):
mode = self.get_options("mode")
if mode == 1:
self.results.type = "static"
elif mode == 2:
self.results.type = "quasistatic"
elif mode == 3:
self.results.type = "dynamic"
else:
self.results.type = "not an existing mode"
self.results.nquch = self.get_options("nquch")
self.results.nxe = self.get_options("nxe")
self.results.nx = self.nx
self.results.x = np.copy(np.ctypeslib.as_array(self.mbs_equil_ptr.contents.x, (self.nx,)))
for i, val in enumerate(self.results.x):
self.results.x_name.append("q" + str(i+1))
if self.mbs.Nux and self.get_options("compute_uxd"):
for i in range(self.mbs.Nux):
self.results.x_name[self.nx-self.mbs.Nux+i] = "ux" + str(i+1)
if self.results.nxe:
for i, val in enumerate(self.addition_list):
self.results.x_name[self.nx-self.results.nxe+i] = self.addition_list[i][1]
if self.results.nquch:
for i, val in enumerate(self.exchange_list):
self.results.x_name[self.exchange_list[i][1]-1] = self.exchange_list[i][2]
if self.success == 1:
self.results.success = "PASSED"
elif self.success == 0:
self.results.success = "NONE"
elif self.success == -1:
self.results.success = "FAILED"
def add_exchange(self, ptr, index, replaced_var_id, ptr_name = "??"):
""" Add a new equilibrium variable that replaces the specified
default one.
......@@ -413,7 +458,9 @@ class MbsEquil(object):
Index in ptr of the variable to be exchanged
replaced_var_id : int
Index of the default variable that will be replaced
ptr_name : str
(optionnal) Name of the variable to be exchanged
"""
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()")
......@@ -425,15 +472,15 @@ class MbsEquil(object):
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]]
self.exchange_list = [[address, replaced_var_id, ptr_name]]
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])
self.exchange_list.append([address, replaced_var_id, ptr_name])
def add_extra_variable(self, ptr, index):
def add_extra_variable(self, ptr, index, ptr_name = "??"):
""" Add a new equilibrium variable
Extra variables are stored and will be set in the memory of the
......@@ -450,6 +497,8 @@ class MbsEquil(object):
Array containing the extra variable to add to the equilibrium variables
index : int
Index in ptr of the extra variable to add to the equilibrium variables
ptr_name : str
(optionnal) Name of the extra variable
"""
if self.get_options("nxe"):
......@@ -462,13 +511,13 @@ class MbsEquil(object):
print(">>EQUIL>> extra exchange : Warning ! Generalized coordinates have to be used with caution as extra variables")
if len(self.addition_list) == 0:
self.addition_list = [address]
self.addition_list = [[address, ptr_name]]
else:
for element in self.addition_list:
if element == address:
if element[0] == address:
print(">>EQUIL>> variable exchange : Extra variable has already been added")
else:
self.addition_list.append(address)
self.addition_list.append([address, ptr_name])
def add_extra_variable_um(self, user_model, param):
""" Add a user model as new equilibrium variable
......@@ -496,13 +545,13 @@ class MbsEquil(object):
return
address = self.mbs.user_model[user_model].__getarray__(param)[0].ctypes.data
if len(self.addition_list) == 0:
self.addition_list = [address]
self.addition_list = [[address, user_model+ "." + param]]
else:
for element in self.addtion_list:
if element == address:
if element[0] == address:
print(">>EQUIL>> variable exchange : Extra variable has already been added")
else:
self.addition_list.append(address)
self.addition_list.append([address, user_model+ "." + param])
def add_exchange_um(self, user_model, param, replaced_var_id):
""" Add a user model as a new equilibrium variable that replaces
......@@ -533,13 +582,13 @@ class MbsEquil(object):
return
address = self.mbs.user_model[user_model].__getarray__(param)[0].ctypes.data
if len(self.exchange_list) == 0:
self.exchange_list = [[address, replaced_var_id]]
self.exchange_list = [[address, replaced_var_id, user_model+ "." + param]]
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])
self.exchange_list.append([address, replaced_var_id, user_model+ "." + param])
def _compute_exchange(self):
......@@ -566,11 +615,11 @@ class MbsEquil(object):
libmodules.mbs_equil_addition(self.mbs_equil_ptr.contents.options)
if self.get_options("nxe") == len(self.addition_list) and len(self.addition_list) > 0:
for i, element in enumerate(self.addition_list):
libmodules.mbs_equil_add_variable(self.mbs_equil_ptr.contents.options, element,i+1)
libmodules.mbs_equil_add_variable(self.mbs_equil_ptr.contents.options, element[0],i+1)
elif self.get_options("nxe") != len(self.addition_list):
print(">>EQUIL>> extra variable : nxe (" +str(self.get_options("nxe"))+ ") different from the length of the extra variable list ("+ str(len(self.addition_list))+")")
def replace_exchange(self, ptr, index, id_exchanged):
def replace_exchange(self, ptr, index, id_exchanged, ptr_name = "??"):
""" Replace an existing exchanged variable with another one
This function can be called between two 'MbsEquil::run()' as it
......@@ -587,6 +636,8 @@ class MbsEquil(object):
id_exchanged : int
Index in the list of exchanged variable of the variable to be
replaced
ptr_name : str
(optionnal) Name of the variable to be exchanged
"""
if ptr.ctypes.data == self.mbs.q.ctypes.data:
......@@ -596,8 +647,9 @@ class MbsEquil(object):
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
self.exchange_list[id_exchanged-1][2] = ptr_name
def replace_extra_variable(self, ptr, index, id_extra):
def replace_extra_variable(self, ptr, index, id_extra, ptr_name = "??"):
""" Replace an existing extra variable with another one
This function can be called between two 'MbsEquil::run()' as it
......@@ -622,8 +674,8 @@ class MbsEquil(object):
if id_extra > len(self.addition_list) or id_extra <=0 :
print(">>EQUIL>> extra exchange : There is no extra variable at this index :" + str(id_extra))
else:
self.addition_list[id_extra-1] = ptr.ctypes.data + ctypes.sizeof(ctypes.c_double)*index
self.addition_list[id_extra-1][0] = ptr.ctypes.data + ctypes.sizeof(ctypes.c_double)*index
self.addition_list[id_extra-1][1] = ptr_name
def replace_extra_variable_um(self, user_model, param, id_extra):
""" Replace an existing extra variable with a user model
......@@ -651,7 +703,7 @@ class MbsEquil(object):
print(">>EQUIL>> extra exchange : There is no extra variable at this index :" + str(id_extra))
else:
self.addition_list[id_extra-1][0] = self.mbs.user_model[user_model].__getarray__(param)[0].ctypes.data
self.addition_list[id_extra-1][1] = user_model+ "." + param
def replace_exchange_um(self, user_model, param, id_exchanged):
""" Replace an existing exchanged variable with a user model
......@@ -679,7 +731,7 @@ class MbsEquil(object):
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
self.exchange_list[id_exchanged-1][2] = user_model+ "." + param
def print_equil(self):
......@@ -843,6 +895,8 @@ class MbsEquil(object):
self.mbs_equil_ptr.contents.options.contents.max_save_user = value
elif key == "buffersize":
self.mbs_equil_ptr.contents.options.contents.buffersize = value
elif key == "store_results":
self.store_results = value
else:
print(">>EQUIL> The option "+key+ " is not defined in this module")
......@@ -927,6 +981,8 @@ class MbsEquil(object):
options.append(self.mbs_equil_ptr.contents.options.contents.max_save_user)
elif key == "buffersize":
options.append(self.mbs_equil_ptr.contents.options.contents.buffersize)
elif key == "store_results":
options.append(self.store_results)
else:
print(">>EQUIL>> The option "+key+ " is not defined in this module")
......@@ -952,4 +1008,24 @@ class MbsEquil(object):
@property
def mbs_name(self):
return self.mbs.mbs_name
\ No newline at end of file
@property
def nx(self):
return self.mbs_equil_ptr.contents.nx
@property
def success(self):
return self.mbs_equil_ptr.contents.success
class MbsResultEquil(object):
def __init__(self):
self.type = None
self.nquch = None
self.nxe = None
self.nx = None
self.x = None
self.x_name = []
self.success = None
\ No newline at end of file
......@@ -92,7 +92,7 @@ class MbsModal(object):
self.mbs_invdyna = None
# Storing Results
# self.results = MbsResult(self.mbs)
self.results = MbsResultModal()
self.store_results = False
# Exposing some memory
......@@ -323,6 +323,8 @@ class MbsModal(object):
self.mbs.__assign_symb_fct__()
libmodules.mbs_run_modal(self.mbs_modal_ptr, self.mbs.mbs_data_ptr)
if self.store_results:
self.load_results()
# Anassign user functions
self.__unassign_user_fct__()
......@@ -332,6 +334,20 @@ class MbsModal(object):
self.__unassign_symbolic_fct__()
self.mbs.__unassign_symb_fct__()
return self.results
def load_results(self):
self.results.eval_a = np.ctypeslib.as_array(self.mbs_modal_ptr.contents.eval_a, (self.nx,))
self.results.eval_b = np.ctypeslib.as_array(self.mbs_modal_ptr.contents.eval_b, (self.nx,))
self.results.evec_a = np.ctypeslib.as_array(self.mbs_modal_ptr.contents.evec_a[0], (self.nx,self.nx))
self.results.evec_b = np.ctypeslib.as_array(self.mbs_modal_ptr.contents.evec_b[0], (self.nx,self.nx))
self.results.evec_r = np.ctypeslib.as_array(self.mbs_modal_ptr.contents.evec_r[0], (self.nx,self.nx))
self.results.evec_phi = np.ctypeslib.as_array(self.mbs_modal_ptr.contents.evec_phi[0], (self.nx,self.nx))
def set_options(self, **kwargs):
"""
......@@ -544,4 +560,28 @@ class MbsModal(object):
if len(options) == 1:
return options[0]
return tuple(options)
\ No newline at end of file
return tuple(options)
#==========================================================================
# Defining properties
#==========================================================================
@property
def nx(self):
return self.mbs_modal_ptr.contents.nx
class MbsResultModal(object):
def __init__(self):
self.eval_a = None
self.eval_b = None
self.evec_a = None
self.evec_b = None
self.evec_r = None
self.evec_phi = None
\ No newline at end of file
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