Commit 3dedf7b3 authored by Louis Beauloye's avatar Louis Beauloye
Browse files

[Ctypes] user model + doc

parent dcfac19f
......@@ -799,7 +799,15 @@ class MbsData(object):
def __load_user_model__(self):
"""
Load user models in 2 nested dictionaries
The first one contains the names of the user models as keys and a
second dictionary for the parameters as values.
The second one contains the names of the parameters as keys and
numpy arrays that point toward C memory as values
"""
self.user_model._locked = False
infos_c_ptr=self.mbs_infos_ptr.contents
for i in range(infos_c_ptr.user_models.contents.n_user_model):
......@@ -818,7 +826,12 @@ class MbsData(object):
size = parameter_list.n_value
UmPy = (ctypes.cast(val_ptr, ctypes.c_void_p).value==ctypes.cast(value_list, ctypes.c_void_p).value)
if UmPy :
self.user_model[name][name2] = np.ctypeslib.as_array(val_ptr, (1,size))
if self.user_model[name]._type[name2] == 5:
self.user_model[name][name2] = np.array(np.ctypeslib.as_array(val_ptr, (1,size)), dtype = np.int)
else:
self.user_model[name][name2] = np.ctypeslib.as_array(val_ptr, (1,size))
#in full python project, the C memory is always a double
else :
#not yet tested, it requires user and symbolic fonctions in C
if self.user_model[name]._type[name2] == 5 or self.user_model[name]._type[name2] == 7:
......@@ -1250,59 +1263,73 @@ class _UserModelDict(dict):
def __setitem__(self, key, value):
if self._locked == True:
# if it is a parameter
# if it is a parameter and not a parent
if not (self._type is None):
if self._type[key] == 1:
if type(value) is float:
tab = self.__getarray__(key)
tab[0][0] = value
elif type(value) is int:
tab = self.__getarray__(key)
tab[0][0] = int(value)
else :
print("UserModel "+self._parent_key+"."+key+" is a float, "+str(type(value))+" was given")
elif self._type[key] == 7:
if type(value) is int:
tab = self.__getarray__(key)
tab[0][0] = value
else :
print("UserModel "+self._parent_key+"."+key+" is an int, "+str(type(value))+" was given")
elif self._type[key] == 2 or self._type[key] == 5:
if type(value) is np.ndarray or type(value) is list:
tab = self.__getarray__(key)
if np.size(value) == np.size(tab):
tab[:] = value[:]
else:
print("UserModel "+self._parent_key+"."+key+" is an array of size "+str(np.size(tab))+", array of size "+str(np.size(value))+" was given")
if key in self.keys():
if self._type[key] == 1:
if type(value) is float:
tab = self.__getarray__(key)
tab[0][0] = value
elif type(value) is int:
tab = self.__getarray__(key)
tab[0][0] = int(value)
else :
print("UserModel "+self._parent_key+"."+key+" is a float, "+str(type(value))+" was given")
elif self._type[key] == 7:
if type(value) is int:
tab = self.__getarray__(key)
tab[0][0] = value
else :
print("UserModel "+self._parent_key+"."+key+" is an int, "+str(type(value))+" was given")
elif self._type[key] == 2 or self._type[key] == 5:
if type(value) is np.ndarray or type(value) is list:
tab = self.__getarray__(key)
if np.size(value) == np.size(tab):
tab[:] = value[:]
else:
print("UserModel "+self._parent_key+"."+key+" is an array of size "+str(np.size(tab))+", array of size "+str(np.size(value))+" was given")
else :
print("UserModel "+self._parent_key+"."+key+" is an array, "+str(type(value))+" was given")
elif self._type[key] == 3:
print("Type lut1D for a user mode is not available in python")
elif self._type[key] == 4:
print("Type lut2D for a user mode is not available in python")
elif self._type[key] == 6:
print("Type structure for a user model is not available in python")
else :
print("UserModel "+self._parent_key+"."+key+" is an array, "+str(type(value))+" was given")
elif self._type[key] == 3:
print("Type lut1D for a user mode is not available in python")
elif self._type[key] == 4:
print("Type lut2D for a user mode is not available in python")
elif self._type[key] == 6:
print("Type structure for a user model is not available in python")
else :
print("Invalid type for the user model")
print("Invalid type for the user model")
else:
print("Unknown user model "+str(self._parent_key)+"."+str(key))
else :
dict.__setitem__(self, key, value)
def __getarray__(self, key):
"""
Get the full array stored in the dict
"""
return dict.__getitem__(self, key)
def __getitem__(self, key):
"""
Get the item in the array in the dict
if scalar, return a scalar
if vector, return a vector
"""
if self._type is None:
return dict.__getitem__(self, key)
else :
if self._type[key] == 1 or self._type[key] == 7 :
return dict.__getitem__(self, key)[0][0]
else:
return dict.__getitem__(self, key)[0]
if key in self.keys():
if self._type[key] == 1 or self._type[key] == 7 :
return dict.__getitem__(self, key)[0][0]
else:
return dict.__getitem__(self, key)[0]
else:
print("Unknown user model "+str(self._parent_key)+"."+str(key))
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