mbs_dirdyn.py 51.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- coding: utf-8 -*-
"""
-------------------------------
(c) Universite catholique de Louvain, 2019

Creation : 2019 by O. Lantsoght
Last update : 2019

version MBsysC v1.11.2

-------------------------------
Portable Python interface to MBsysC using Ctypes.

Define the class MbsDirdyn based on the MbsDirdyn structure of MBsysC.
This class has the functions required to manipulate the direct dynamic module.
This include setting the options, running an (or multiple) analysis and freeing
the memory.
"""

import os
import imp
22
import ctypes
23
24
25
26
27
28
29
30

import numpy as np

# importing MbsysPy classes

# importing libraries
from ..mbsysc_loader.loadlibs import libmodules

31
32
33
# importing utilities function
from ..mbs_utilities import callback_undefined

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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_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 mbs_cons_hJ_wrap
from ..mbsysc_loader.callback import mbs_cons_jdqd_wrap
from ..mbsysc_loader.callback import mbs_invdyna_wrap
from ..mbsysc_loader.callback import mbs_dirdyna_wrap


#==============================================================================
# Global parameter of the current module
#==============================================================================
Louis Beauloye's avatar
Louis Beauloye committed
50
__DEBUG__ = True
51
52
53
54
55
56
57
58
__MODULE_DIR__ = os.path.dirname(os.path.abspath(__file__))


#==============================================================================
# Defining Python MbsDirdyn class
#==============================================================================

class MbsDirdyn(object):
59
60
61
62
63
64
65
    """
    Class of the direct dynamic module
    
    Attributes
    ----------
    dt : double
        Current integration step size.
66
67
    mbs : MbsData
        Instance of MbsData related to the analysis.
68
    results : MbsResult
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        Instance of MbsResult containing the results of the direct dynamics analysis.
    symbolic_path : str
        Path to the folder containing the symbolic functions (python modules) 
        to be loaded.
    tsim : double
        Current simulation time.
    user_path : str
        Path to the folder containing the user functions (python modules) to be loaded.
    
    Examples
    --------
    >>> mbs_data = MBsysPy.MbsData("../dataR/ExampleProject.mbs")
    >>> mbs_dirdyn = MBsysPy.MbsDirdyn(mbs_data)
    >>> mbs_dirdyn.set_options(t0 = 5, tf = 10)
    >>> mbs_dirdyn.get_options("t0", "tf")
        (5.0, 10.0)
    >>> results = mbs_dirdyn.run()
    
87
    """
88
    def __init__(self, mbs, user_path=None, symbolic_path=None):
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
        """
        Create an instance of the MbsDirdyn class for the provided MbsData instance.

        Parameters
        ----------
        mbs : MbsData
            Instance of MbsData related to the analysis.
        user_path : str or None, optionnal
            The path to the folder containing the user functions. If not provided 
            ('None') the path is retrieved from the MbsData instance 'mbs'.
            default is None
        symbolic_path : str or None, optionnal
            The path to the folder containing the symbolic functions. If not 
            provided ('None') the path is retrieved from the MbsData instance 'mbs'.
            default is None
            
        Returns
        -------
            A MbsDirdyn instance.
        """
            
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
        if __DEBUG__ : print( "DEBUG>>  Creating MbsDirdyn struct for "+mbs.mbs_name+"' MBS.")
        self.mbs_dirdyn_ptr = libmodules.mbs_new_dirdyn(mbs.mbs_data_ptr)
        if __DEBUG__ : print( "DEBUG>>  MbsDirdyn structure loaded")
        self.mbs = mbs

        #Test
        if __DEBUG__ :
            print( "DEBUG>>  MbsDirdyn created.")
            print( "     >>  options->tf (5.0): ", self.mbs_dirdyn_ptr.contents.options.contents.tf)
            print( "     >>  options->dt_max (1e-3): ", self.mbs_dirdyn_ptr.contents.options.contents.dt_max)
            print( "     >>  mbs_aux->NRerr (-): ", self.mbs_dirdyn_ptr.contents.mbs_aux.contents.NRerr)
            print( "     >>  mbs_aux->nquc (-): ", self.mbs_dirdyn_ptr.contents.mbs_aux.contents.nquc)
            print( "     >>  mbs_aux->M[1:3][1:3]: ", 
                  self.mbs_dirdyn_ptr.contents.mbs_aux.contents.M[1][1], 
                  self.mbs_dirdyn_ptr.contents.mbs_aux.contents.M[1][2], 
                  self.mbs_dirdyn_ptr.contents.mbs_aux.contents.M[2][1], 
                  self.mbs_dirdyn_ptr.contents.mbs_aux.contents.M[2][2])
127
        # Path to user function used by partitionning module
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
        self.user_path = self.mbs.user_path
        if not (user_path==None):
            project_path  = (self.mbs.project_path).decode("utf-8")
            user_path = os.path.join(project_path, user_path)
            # Error handeling
            if not os.path.isdir(user_path):
                print ('The user function directory for direct dynamic module does not exist: "'+ user_path+'"')
                print ('The current root folder is: "'+os.getcwd()+'"')
                print ('The following directory is used instead: "'+self.user_path+'".')
            else:
                self.user_path = user_path
        # Path to user function used by partitionning modue
        self.symbolic_path = self.mbs.symbolic_path
        if not (user_path==None):
            project_path  = (self.mbs.project_path).decode("utf-8")
            symbolic_path = os.path.join(project_path, symbolic_path)
            # Error handeling
            if not os.path.isdir(symbolic_path):
                print ('Thesymbolic function directory for direct dynamic module does not exist: "'+ symbolic_path+'"')
                print ('The current root folder is: "'+os.getcwd()+'"')
                print ('The following directory is used instead: "'+self.symbolic_path+'".')
            else:
                self.symbolic_path = symbolic_path
        
152
        # dictionary containing the pointers to avoid garbage collecting
153
154
        self.ptrs_to_user_fcts = dict()
        self.ptrs_to_symb_fcts = dict()
155
156
157
158
159
160
161
162
163
164
165
166
        
        # Storing project function pointer
        self.user_cons_hJ = None
        self.user_cons_jdqd = None
        self.user_dirdyn_init = None
        self.user_dirdyn_loop = None
        self.user_dirdyn_finish = None
        self.mbs_cons_hJ = None
        self.mbs_cons_jdqd = None
        self.mbs_invdyna = None
        self.mbs_dirdyna = None
        
167
168
        # Storing Results
        self.results = MbsResult(self.mbs)
Louis Beauloye's avatar
Louis Beauloye committed
169
        self.store_results = True
170
        
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
        # Exposing some memory
        if __DEBUG__ : print( "DEBUG>>  Exposing MbsDirdyn fields")
        # Constraints
        self._h = self._Jac = self._jdqd = None
        if self.mbs.Ncons>0:
            self._h = np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.mbs_aux.contents.h, (self.mbs.Ncons+1,))
            self._Jac = np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.mbs_aux.contents.Jac[0], (self.mbs.Ncons+1,self.mbs.njoint+1))
            self._jdqd = np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.mbs_aux.contents.jdqd, (self.mbs.Ncons+1,))
        self._huserc = self._Juserc = self._jdqduserc = None
        if self.mbs.Nuserc>0:
            self._huserc = np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.mbs_aux.contents.huserc, (self.mbs.Nuserc+1,))
            self._Juserc = np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.mbs_aux.contents.Juserc[0], (self.mbs.Nuserc+1,self.mbs.njoint+1))
            self._jdqduserc = np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.mbs_aux.contents.jdqduserc, (self.mbs.Nuserc+1,))
        # Fields from equation of motion
        self._M = np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.mbs_aux.contents.M[0], (self.mbs.njoint+1,self.mbs.njoint+1))
        self._c = np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.mbs_aux.contents.c, (self.mbs.njoint+1,))
        self._F = np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.mbs_aux.contents.F, (self.mbs.njoint+1,))
        
        # Loading user function
190
191
192
        if self.mbs.opt_load_c < 2:
            if __DEBUG__ : print( "DEBUG>>  Loading user functions")
            self.__load_user_fct__(self.user_path)
193
        # Loading symbolic function
194
195
196
        if self.mbs.opt_load_c < 1:
            if __DEBUG__ : print( "DEBUG>>  Loading symbolic functions")
            self.__load_symbolic_fct__(self.symbolic_path)
197
198
199
200
201
202
203
204
205
        

    def __str__(self):
        if __DEBUG__ : print( "DEBUG>>  start of __str")
        
        return "MbsDirdyn instance has nothing to be printed from C library!"
    
    def __load_user_fct__(self, user_path):
        """ 
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
        Load user function where some args depend on MbsDirdyn module.
        
        Load the user functions in which some of the arguments are  
        dependent of MbsDirdyn module instance. The functions will be 
        assigned to the MbsData instance when the 'run' functions is called 
        and unassigned at the end.
        
        The loader user functions are :
         - user_cons_hJ (from cons_hJ.py)
         - user_cons_jdqd (from cons_hJ.py)
         - user_dirdyn_init (from user_dirdyn.py)
         - user_dirdyn_loop (from user_dirdyn.py)
         - user_dirdyn_finish (from user_dirdyn.py)

        Parameters
        ----------
        user_path : str
            The path to the folder containing the user functions.
224
        """
225
226
227
228
        
        template_path = os.path.join(__MODULE_DIR__,'../templates/user')
        
        
229
230
231
        # cons_hJ
        user_file = "cons_hJ.py"
        path = os.path.abspath(os.path.join(user_path, user_file))
232
        if not os.path.isfile(path):
233
            if __DEBUG__ : print( "DEBUG>>  file '"+user_file+"' not found in folder '"+os.path.dirname(path))
234
235
236
            path = os.path.abspath(os.path.join(template_path, user_file))
        module = imp.load_source(user_file[:-3], path)
        self.user_cons_hJ = module.user_cons_hJ
237
238
239
240
        
        # cons_jdqd
        user_file = "cons_jdqd.py"
        path = os.path.abspath(os.path.join(user_path, user_file))
241
        if not os.path.isfile(path):
242
            if __DEBUG__ : print( "DEBUG>>  file '"+user_file+"' not found in folder '"+os.path.dirname(path))
243
244
245
            path = os.path.abspath(os.path.join(template_path, user_file))
        module = imp.load_source(user_file[:-3], path)
        self.user_cons_jdqd = module.user_cons_jdqd
246
        
247
248
249
        # user_dirdyn
        user_file = "user_dirdyn.py"
        path      = os.path.abspath(os.path.join(user_path, user_file))
250
        if not os.path.isfile(path):
251
            print ("file '"+user_file+"' not found in folder '"+os.path.dirname(path))
252
253
254
255
256
257
258
            path = os.path.abspath(os.path.join(template_path, user_file))
        else:
            if __DEBUG__ : print( "DEBUG>>  loading file '"+user_file+"' in folder '"+os.path.dirname(path))
        module = imp.load_source(user_file[:-3], path)
        self.user_dirdyn_init   = module.user_dirdyn_init
        self.user_dirdyn_loop   = module.user_dirdyn_loop
        self.user_dirdyn_finish = module.user_dirdyn_finish
259
260
261
262
        return
        
    def __load_symbolic_fct__(self, symbolic_path):
        """ 
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
        Load symbolic function where some args depend on MbsDirdyn module.
        
        Load the symb functions in which some of the arguments are  
        dependent of MbsDirdyn module instance. The functions will be 
        assigned to the MbsData instance when the 'run' functions is called 
        and unassigned at the end.
        
        The loader user functions are :
         - cons_hJ (from mbs_cons_hJ_MBSNAME.py)
         - cons_jdqd (from mbs_cons_jdqd_MBSNAME.py)
         - dirdyna (from mbs_dirdyna_MBSNAME.py)
         - invdyna (from mbs_invdyna_MBSNAME.py)

        Parameters
        ----------
        symbolic_path : str
            The path to the folder containing the symbolic functions.
280
281
        """
        mbs_name = self.mbs.mbs_name
282
        template_path = os.path.join(__MODULE_DIR__,'../templates/symbolic')
283
284
285
        # mbs_cons_hJ
        symb_file = "mbs_cons_hJ_"+mbs_name+".py"
        path = os.path.abspath(os.path.join(symbolic_path, symb_file))
286
        if not os.path.isfile(path):
287
            if __DEBUG__ : print( "DEBUG>>  file '"+symb_file+"' not found in folder '"+os.path.dirname(path))
288
289
290
291
292
            symb_file = "mbs_cons_hJ_PRJ.py"
            path = os.path.abspath(os.path.join(template_path, symb_file))
        module = imp.load_source(symb_file[:-3], path)
        self.mbs_cons_hJ = module.cons_hJ
        
293
294
295
296
        
        # mbs_cons_jdqd
        symb_file = "mbs_cons_jdqd_"+mbs_name+".py"
        path = os.path.abspath(os.path.join(symbolic_path, symb_file))
297
        if not os.path.isfile(path):
298
            if __DEBUG__ : print( "DEBUG>>  file '"+symb_file+"' not found in folder '"+os.path.dirname(path))
299
300
301
302
            symb_file = "mbs_cons_jdqd_PRJ.py"
            path = os.path.abspath(os.path.join(template_path, symb_file))
        module = imp.load_source(symb_file[:-3], path)
        self.mbs_cons_jdqd = module.cons_jdqd
303
304
305
306
        
        # dirdyna
        symb_file = "mbs_dirdyna_"+mbs_name+".py"
        path = os.path.abspath(os.path.join(symbolic_path, symb_file))
307
        if not os.path.isfile(path):
308
            if __DEBUG__ : print( "DEBUG>>  file '"+symb_file+"' not found in folder '"+os.path.dirname(path))
309
310
311
312
            symb_file = "mbs_dirdyna_PRJ.py"
            path = os.path.abspath(os.path.join(template_path, symb_file))
        module = imp.load_source(symb_file[:-3], path)
        self.mbs_dirdyna = module.dirdyna
313
       
314
315
        return
    
316
317
    def __assign_user_fct__(self):
        """ 
318
319
320
        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
321
322
323
        """
        # 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))
324
        self.mbs.user_cons_hJ = self.user_cons_hJ
325
        self.mbs.mbs_data_ptr.contents.user_cons_hJ = self.ptrs_to_user_fcts["user_cons_hJ"]
326
327
328
        
        # cons_jdqd
        self.ptrs_to_user_fcts["user_cons_jdqd"] = user_cons_jdqd_wrap(lambda jdqd,mbs,tsim : self.user_cons_jdqd(self._jdqduserc, self.mbs))
329
        self.mbs.user_cons_jdqd = self.user_cons_jdqd
330
        self.mbs.mbs_data_ptr.contents.user_cons_jdqd = self.ptrs_to_user_fcts["user_cons_jdqd"]
331
332
333
334
335

        # user_dirdyn
        self.ptrs_to_user_fcts["user_dirdyn_init"] = user_dirdyn_init_wrap(lambda mbs, dd : self.mbs.user_dirdyn_init(self.mbs, self))
        self.ptrs_to_user_fcts["user_dirdyn_loop"] = user_dirdyn_loop_wrap(lambda mbs, dd : self.mbs.user_dirdyn_loop(self.mbs, self))
        self.ptrs_to_user_fcts["user_dirdyn_finish"] = user_dirdyn_finish_wrap(lambda mbs, dd : self.mbs.user_dirdyn_finish(self.mbs, self))
336
        self.mbs.user_dirdyn_init = self.user_dirdyn_init
337
        self.mbs.mbs_data_ptr.contents.user_dirdyn_init = self.ptrs_to_user_fcts["user_dirdyn_init"]
338
        self.mbs.user_dirdyn_loop = self.user_dirdyn_loop
339
        self.mbs.mbs_data_ptr.contents.user_dirdyn_loop = self.ptrs_to_user_fcts["user_dirdyn_loop"]
340
        self.mbs.user_dirdyn_finish = self.user_dirdyn_finish
341
        self.mbs.mbs_data_ptr.contents.user_dirdyn_finish = self.ptrs_to_user_fcts["user_dirdyn_finish"]
342
343
344
345
        return
        
    def __assign_symbolic_fct__(self):
        """ 
346
347
348
        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
349
350
351
352
        """
        
        # mbs_cons_hJ
        self.ptrs_to_symb_fcts["mbs_cons_hJ"] = mbs_cons_hJ_wrap(lambda h,Jac,mbs,tsim : self.mbs_cons_hJ(self._h,self._Jac, self.mbs))
353
        self.mbs.mbs_cons_hJ = self.mbs_cons_hJ
354
        self.mbs.mbs_data_ptr.contents.mbs_cons_hJ = self.ptrs_to_symb_fcts["mbs_cons_hJ"]
355
356
357
        
        # mbs_cons_jdqd
        self.ptrs_to_symb_fcts["mbs_cons_jdqd"] = mbs_cons_jdqd_wrap(lambda jdqd,mbs,tsim : self.mbs_cons_jdqd(self._jdqd, self.mbs))
358
        self.mbs.mbs_cons_jdqd = self.mbs_cons_jdqd
359
        self.mbs.mbs_data_ptr.contents.mbs_cons_jdqd = self.ptrs_to_symb_fcts["mbs_cons_jdqd"]
360
361
362
        
        # dirdyna
        self.ptrs_to_symb_fcts["mbs_dirdyna"] = mbs_dirdyna_wrap(lambda M,c,mbs,tsim : self.mbs_dirdyna(self._M, self._c, self.mbs, tsim))
363
        self.mbs.mbs_dirdyna = self.mbs_dirdyna
364
        self.mbs.mbs_data_ptr.contents.mbs_dirdyna = self.ptrs_to_symb_fcts["mbs_dirdyna"]
365
366
367
368
369
370
371
372
373
374
375
376
         
        return
    
     # Callback function for function with advanced arguments
    def __callback_mbs_cons_hJ(self, fun, h, Jac):
        if __DEBUG__ : print( "DEBUG>>  callback_mbs_cons_hJ")
        __h = np.ctypeslib.as_array(h, (self.mbs.Ncons+1,))
        __Jac = np.ctypeslib.as_array(Jac[0], (self.mbs.Ncons+1,self.mbs.njoint+1))
        fun(__h,__Jac,self.mbs)
    
    
    def __unassign_user_fct__(self):
377
        """ Unassign user function where some args depend on MbsDirdyn module.  """
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
        
        self.ptrs_to_user_fcts = {}
        
        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"))
        self.mbs.user_cons_jdqd = None
        self.mbs.mbs_data_ptr.contents.user_cons_jdqd = user_cons_jdqd_wrap(lambda h,Jac,mbs,tsim : callback_undefined("user_cons_jdqd"))
        self.mbs.user_dirdyn_init = None
        self.mbs.mbs_data_ptr.contents.user_dirdyn_init = user_dirdyn_init_wrap(lambda mbs, dd : callback_undefined("user_dirdyn_init"))
        self.mbs.user_dirdyn_loop = None
        self.mbs.mbs_data_ptr.contents.user_dirdyn_loop = user_dirdyn_loop_wrap(lambda mbs, dd : callback_undefined("user_dirdyn_loop"))
        self.mbs.user_dirdyn_finish = None
        self.mbs.mbs_data_ptr.contents.user_dirdyn_finish = user_dirdyn_finish_wrap(lambda mbs, dd : callback_undefined("user_dirdyn_finish"))
        
    def __unassign_symbolic_fct__(self):
        
394
        """ Unassign symbolic function where some args depend on MbsDirdyn module. """
395
        
396
        self.ptrs_to_symb_fcts = {}
397
        
398
399
400
401
402
403
404
405
406
407
408
409
        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"))
        self.mbs.mbs_cons_jdqd = None
        self.mbs.mbs_data_ptr.contents.mbs_cons_jdqd = mbs_cons_jdqd_wrap(lambda jdqd,mbs,tsim : callback_undefined("mbs_cons_jdqd"))
        self.mbs.mbs_dirdyna = None
        self.mbs.mbs_data_ptr.contents.mbs_dirdyna = mbs_dirdyna_wrap(lambda M,c,mbs,tsim : callback_undefined("mbs_dirdyna"))
        
    def __del__(self):
        libmodules.mbs_delete_dirdyn(self.mbs_dirdyn_ptr, self.mbs.mbs_data_ptr)
        if __DEBUG__ : print("DEBUG>>  MbsDirdyn pointer deleted")
        
    def run(self):
Louis Beauloye's avatar
Louis Beauloye committed
410
        """
411
412
413
414
415
416
417
        Run a direct dynamics analysis
        
        Options can be setted with the function 'set_options()' 
        Options can be retrieved with the function 'get_options()'
        
        Results are stored in the field 'results' if the options 'store_results'
        is set to True.
Louis Beauloye's avatar
Louis Beauloye committed
418
        
419
420
421
        Returns
        -------
            The MbsResults containing the results of the analysis.
422
        """     
423
        # Assing required user functions
424
425
426
        if self.mbs.opt_load_c < 2:
            self.__assign_user_fct__()
            self.mbs.__assign_user_fct__()
427
428
        
        # Assing required symbolic functions
429
430
431
        if self.mbs.opt_load_c < 1:
            self.__assign_symbolic_fct__()
            self.mbs.__assign_symb_fct__()
432
433
        
        
Louis Beauloye's avatar
Louis Beauloye committed
434
        if not self.store_results:   
435
436
437
438
439
440
441
442
443
444
445
            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
            self.set_options(save2file = 1)
            libmodules.mbs_dirdyn_init(self.mbs_dirdyn_ptr, self.mbs.mbs_data_ptr)
            libmodules.mbs_dirdyn_loop(self.mbs_dirdyn_ptr, self.mbs.mbs_data_ptr)
       
            # Results (buffer) memory is kept BUT FILES WILL BE WRITTEN LATER
            if self.get_options("save2file"):
                #c_mbs_dirdyn_write_buffers(self.mbs_dirdyn_ptr)
                size1 = self.mbs_dirdyn_ptr.contents.buffers[0].contents.index
446
447
                if size1 == 0:
                    size1 = self.mbs_dirdyn_ptr.contents.buffers[0].contents.size
448
449
450
451
452
453
454
455
                size2 = self.mbs_dirdyn_ptr.contents.buffers[0].contents.nx+1
                
                # array are initialized to the time pointer so as to start index of joints at 1 (we have to ensure contiguity between t and x in buffers ! ! !)    
                self.results.q = np.copy(np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.buffers[0].contents.tx ,(size1, size2)))
                # get time array from the q buffer
                self.results.t = self.results.q[:,0]
                if not self.results.t[0] == self.get_options("t0"):
                    print("The beginning of the integration is not available in the buffer.\n The complete results have to be loaded from files.")
456
457
                    filename = ctypes.string_at(self.mbs_dirdyn_ptr.contents.buffers[0].contents.filename).decode('utf-8')
                    self.results.load_results_from_file(filename, module = 6)
458
459
460
461
462
                # get qd and qdd buffer
                self.results.qd = np.copy(np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.buffers[1].contents.tx ,(size1, size2)))
                self.results.qdd = np.copy(np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.buffers[2].contents.tx ,(size1, size2)))
                size2 = self.mbs_dirdyn_ptr.contents.buffers[3].contents.nx+1
                self.results.Qq = np.copy(np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.buffers[3].contents.tx ,(size1, size2)))
463
                buffer_id = 4
464
                if self.mbs.Nux:
465
466
467
468
469
470
471
472
473
474
475
476
477
                    size2 = self.mbs_dirdyn_ptr.contents.buffers[buffer_id].contents.nx+1
                    self.results.ux = np.copy(np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.buffers[buffer_id].contents.tx ,(size1, size2)))
                    self.results.uxd = np.copy(np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.buffers[buffer_id+1].contents.tx ,(size1, size2)))
                    buffer_id = buffer_id + 2
                if self.mbs.Nlink:
                    size2 = self.mbs_dirdyn_ptr.contents.buffers[buffer_id].contents.nx+1
                    self.results.Z = np.copy(np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.buffers[buffer_id].contents.tx ,(size1, size2)))
                    self.results.Zd = np.copy(np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.buffers[buffer_id+1].contents.tx ,(size1, size2)))
                    self.results.Fl = np.copy(np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.buffers[buffer_id+2].contents.tx ,(size1, size2)))
                    buffer_id = buffer_id + 3
                if self.mbs.nqc:
                    size2 = self.mbs_dirdyn_ptr.contents.buffers[buffer_id].contents.nx+1
                    self.results.Qc = np.copy(np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.buffers[buffer_id].contents.tx ,(size1, size2)))
478
479
480
481
482
                    
                if self.mbs_dirdyn_ptr.contents.user_buffer.contents.nx:
                    size = self.mbs_dirdyn_ptr.contents.user_buffer.contents.index
                    nbOutput = self.mbs_dirdyn_ptr.contents.user_buffer.contents.nx
                    user_out = np.copy(np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.user_buffer.contents.X[0],(nbOutput, size)))
483
                    for i in range(nbOutput):
484
485
486
487
488
                        name = self.mbs_dirdyn_ptr.contents.user_buffer.contents.names[i]
                        name = name.decode('utf-8')
                        self.results.outputs[name] = user_out[i,:]
            
            libmodules.mbs_dirdyn_finish(self.mbs_dirdyn_ptr, self.mbs.mbs_data_ptr)
489
490
        
        # Anassign user functions
491
492
493
        if self.mbs.opt_load_c < 2:
            self.__unassign_user_fct__()
            self.mbs.__unassign_user_fct__()
494
495
        
        # Anassing required symbolic functions
496
497
498
        if self.mbs.opt_load_c < 1:
            self.__unassign_symbolic_fct__()
            self.mbs.__unassign_symb_fct__()
Louis Beauloye's avatar
Louis Beauloye committed
499
500
        
        return self.results
501
502
503
    
    def set_options(self, **kwargs):
        """
504
        Set the specified options for Dirdyn module.
505
506
507
508
        
        Parameters
        ----------
        t0 : float
509
510
            Initial time of the simulation.
            default is 0.0
511
        tf : float
512
513
            Final time of the simulation
            default is 5.0
514
        dt0 : float
515
516
517
            Initial value of the integration step size. For fixed-step integrator
            (ie. RK4, Euler explicit...) this is the time step for the whole simulation.
            default is 0.001
518
519
        save2file : int
            Determine whether results are written to files on disk (in resultsR folder):  
520
521
522
             - 1: results are saved
             - 0: results are not saved 
            default is 1
523
        resfilename : str
524
525
            The keyword used for determining the name of result files.
            default is 'dirdyn'
526
        respath : str
527
528
529
            Path in which result file are saved. This is the full path or the
            relative path. The folder must exist. 
            default is the 'resultsR/' folder of the project
530
        animpath : str
531
532
533
            Path in which animation file is saved. This is the full path or the
            relative path. The folder must exist. 
            default is the 'animationR/' folder of the project
534
        save_anim : int
535
536
537
            1 to save the animation file, if 'save2file' is set to 1. Set to 0 
            to not save the animation file.
            default is 1
538
        save_visu : int 
539
540
541
542
            Unused options as realtime is deactivated is MBsysPy.
            1 to save the visualizazion file (as it appears in 'user_realtime_visu.c'),
            if 'save2file' is set to 1. Set to O to not save the visualization.
            default is 0
543
        framerate : int
544
545
            number of frame per second for the animation file.
            default is 1000
546
        saveperiod : int
547
548
            The number of time steps between two buffer records.
            default is 1 (every time step are recorded)
549
        max_save_user : int
550
551
            The maximal number of user variables saved.
            default is 12
552
        buffersize : int 
553
554
555
556
557
            The number of time step that can be recorded in the buffer. Results 
            are written to disk when the buffer is full. Writing to disk is slow.
            If set to -1, it computes the buffer size for saving results only once
            at the end according to dt0, t0 and tf.
            default is -1
558
        realtime : int
559
560
            Unused options as realtime is deactivated is MBsysPy.
            1 to activate to real-time features, 0 to deactivate them.
561
562
            default = 0
        accelred : int
563
564
            1 to use accelred, 0 otherwise.
            default is 0
565
        flag_compute_Qc : int
566
567
568
569
570
            If 1 it computes the forces/torques applied in each driven joints in
            order to follow the specified trajectory. Otherwhise the forces/torques
            are only computed in the joints specified in the option 'compute_Qc'.
            Setting the option to 0 speeds up the computation.
            default is 1
Louis Beauloye's avatar
Louis Beauloye committed
571
        compute_all_uxd : int
572
573
574
            If 1 the user derivative are computed during the analysis. Otherwhise
            If set to 0, they are not computed.
            default is 1
575
        compute_Qc : numpy.ndarray
576
577
578
579
580
581
582
583
            If option "flag_compute_Qc' is set to 0, the vector allows to select
            the (driven) joints on which the force/torque required to follow the 
            trajectory will be computed.
            The shape of the array is '(mbs.njoint+1)', for example with 
            'compute_Qc = numpy.array([mbs.njoint 1 0 0 1 0])', we will compute
            Qc(1) and Qc(4) only, on a mbs made of 5 joints.
            compute_Qc[0] is always mbs.njoint
            default is an array full of zero (no forces computation)
584
        integrator : str
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
            Set integrator to use, available value: 
             - "RK4": explicit Runge–Kutta method
                      fixed stepsize
             - "Dopri5": explicit Runge–Kutta method of order (4)5
                         adaptative stepsize
             - "Rosenbrock": implicit fourth-order Rosenbrock
                             adaptative stepsize
                             for stiff problems
             - "EulerEx": Euler explicit method
                          fixed stepsize
             - "Eulaire": Modified Euler explicit method
                          fixed stepsize
             - "EulerIm": Euler implicit method
                          fixed stepsize
             - "Bader": Semi-implicit midpoint rule method
                        adaptative stepsize
             - "WMethods": implicit W Methods
            default is "RK4"
603
        verbose : int 
604
605
606
            Set to 1 to get messages related to adaptive stepsize integrator. To
            disable message set to 0.
            default is 1
607
        flag_stop_stiff : int 
608
609
610
            For adaptive stepsize integrator, set at 1 to stop integration if the
            systems become too stiff. Set it at 0 to continue integration.
            default is 0
611
        flag_precise_dynamics : int
612
613
614
615
616
617
618
619
620
621
622
623
624
            Flag to set which values are saved in the output files. This only
            changes the value save to the output files not the simulation results.
            
            If set at 1, the direct dynamics (constraint solving, computation of
            the generalized acceleration) will be done at the beginning of each
            time step of integration. The output files will be exact, but the 
            computation will be a little slower.
            If set at 0, the output file will contains the value (constraint 
            solution anf the generalized acceleration) of the last computation
            of the integrator. The values may be the one used for an internal step
            of the integrator, but the computation is a little faster.

            default is 1
625
        flag_waypoint : int
626
627
628
629
            If set to 1, the integrator will be forced to give a solution at the
            specified time interval ('delta_t_wp'). If set to 0, the solution
            will be given at every computed time-step.
            default is 0
630
        flag_solout_wp : int
631
632
633
634
635
            Only used if 'flag_waypoint' is 1. In that case if set to 1, the 
            integration results will only contains the value at the specified 
            time interval ('delta_t_wp'). Otherwhise the solution will contain 
            all the timesteps and the waypoints.
            default is 0
636
        delta_t_wp : float
637
638
639
            Time interval between two waypoints [s], unused if 'flag_waypoint' 
            is set to 0.
            default is 1.0e-3
640
        nmax : int
641
642
            maximal number of steps for adaptative stepsize integrators.
            default is 1e9
643
        toler : float 
644
645
            mixed error tolerances for some adaptative stepsize integrators.
            default is 1.0e-3
646
        rtoler : float
647
648
            relative error tolerances for some adaptative stepsize integrators.
            default is 1.0e-3
649
        atoler : float
650
651
            absolute error tolerances for some adaptative stepsize integrators.
            default is 1.0e-6
652
        dt_max : float
653
654
            maximal time step [s] for some adaptative stepsize integrators.
            default is 1.0e-3
655
        n_freeze : int 
656
657
658
            number of time step when the jacobian is freezed (computed once at 
            the start of the n_freeze time steps) for implicit integrators.
            default is 0
Louis Beauloye's avatar
Louis Beauloye committed
659
        show_failed_closure : int 
660
661
662
            If set to 1, an animation of the Newton-Raphson procedure is generated 
            if the closure fails.
            default is 0
Louis Beauloye's avatar
Louis Beauloye committed
663
        store_results : boolean
664
665
            If set to 1, a copy of the results is done.
            default is 1
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
        """
        
        for key, value in kwargs.items():
            if key == "t0":
                self.mbs_dirdyn_ptr.contents.options.contents.t0 = value
            elif key == "tf":
                self.mbs_dirdyn_ptr.contents.options.contents.tf = value
            elif key == "dt0":
                self.mbs_dirdyn_ptr.contents.options.contents.dt0 = value
            elif key == "save2file":
                self.mbs_dirdyn_ptr.contents.options.contents.save2file = value
            elif key == "resfilename":
                self.mbs_dirdyn_ptr.contents.options.contents.resfilename = value.encode('utf-8')
            elif key == "respath":
                self.mbs_dirdyn_ptr.contents.options.contents.respath = value.encode('utf-8')
            elif key == "animpath":
                self.mbs_dirdyn_ptr.contents.options.contents.animpath = value.encode('utf-8')
            elif key == "save_anim":
                self.mbs_dirdyn_ptr.contents.options.contents.save_anim = value
            elif key == "save_visu":
                self.mbs_dirdyn_ptr.contents.options.contents.save_visu = value
            elif key == "framerate":
                self.mbs_dirdyn_ptr.contents.options.contents.framerate = value
            elif key == "saveperiod":
                self.mbs_dirdyn_ptr.contents.options.contents.saveperiod= value
            elif key == "max_save_user":
                self.mbs_dirdyn_ptr.contents.options.contents.max_save_user = value
            elif key == "buffersize":
                self.mbs_dirdyn_ptr.contents.options.contents.buffersize = value
            elif key == "realtime":
                self.mbs_dirdyn_ptr.contents.options.contents.realtime = value
            elif key == "accelred":
                self.mbs_dirdyn_ptr.contents.options.contents.accelred = value
            elif key == "flag_compute_Qc":
                self.mbs_dirdyn_ptr.contents.options.contents.flag_compute_Qc = value
Louis Beauloye's avatar
Louis Beauloye committed
701
702
            elif key == "compute_all_uxd":
                self.mbs_dirdyn_ptr.contents.options.contents.compute_all_uxd = value
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
            elif key == "compute_Qc":
                if (type(value) is np.ndarray) or (type(value) is list):
                    if  np.size(value) == self.mbs.mbs_data_ptr.contents.njoint + 1:
                        for i, val in enumerate(value[1:]):
                            self.mbs_dirdyn_ptr.contents.options.contents.compute_Qc[i+1] = val
                    else:
                        print(">>DIRDYN>> The size of compute_Qc is not consistent with njoint")
                else:
                    print(">>DIRDYN>> The specified value for compute_Qc is not a list or a np.array")
            elif key == "integrator":
                if (type(value) is int) and (value>=0 and value<=8):
                    self.mbs_dirdyn_ptr.contents.options.contents.integrator = value
                elif type(value) is str:
                    if value == "RK4":
                        self.mbs_dirdyn_ptr.contents.options.contents.integrator = 0
                    elif value == "Dopri5":
                        self.mbs_dirdyn_ptr.contents.options.contents.integrator = 1
                    elif value == "Rosenbrock":
                        self.mbs_dirdyn_ptr.contents.options.contents.integrator = 2
                    elif value == "EulerEx":
                        self.mbs_dirdyn_ptr.contents.options.contents.integrator = 3
                    elif value == "Eulaire":
                        self.mbs_dirdyn_ptr.contents.options.contents.integrator = 4
                    elif value == "EulerIm":
                        self.mbs_dirdyn_ptr.contents.options.contents.integrator = 5
                    elif value == "Bader":
                        self.mbs_dirdyn_ptr.contents.options.contents.integrator = 6
                    elif value == "WMethods":
                        self.mbs_dirdyn_ptr.contents.options.contents.integrator = 7
                    elif value == "Custom":
                        self.mbs_dirdyn_ptr.contents.options.contents.integrator = 8
                    else:
                        print(">>DIRDYN>>  " + value + " is not an integrator")
                else:
                    print(">>DIRDYN>>  " + value + " is not an integrator")
            elif key == "verbose":
                self.mbs_dirdyn_ptr.contents.options.contents.verbose = value
            elif key == "flag_stop_stiff":
                self.mbs_dirdyn_ptr.contents.options.contents.flag_stop_stiff = value
            elif key == "flag_precise_dynamics":
                self.mbs_dirdyn_ptr.contents.options.contents.flag_precise_dynamics = value
            elif key == "flag_waypoint":
                self.mbs_dirdyn_ptr.contents.options.contents.flag_waypoint = value
            elif key == "flag_solout_wp":
                self.mbs_dirdyn_ptr.contents.options.contents.flag_solout_wp= value
            elif key == "delta_t_wp":
                self.mbs_dirdyn_ptr.contents.options.contents.delta_t_wp = value
            elif key == "nmax":
                self.mbs_dirdyn_ptr.contents.options.contents.nmax = value
            elif key == "toler":
                self.mbs_dirdyn_ptr.contents.options.contents.toler = value
            elif key == "rtoler":
                self.mbs_dirdyn_ptr.contents.options.contents.rtoler = value
            elif key == "atoler":
                self.mbs_dirdyn_ptr.contents.options.contents.atoler = value
            elif key == "dt_max":
                self.mbs_dirdyn_ptr.contents.options.contents.dt_max = value
            elif key == "n_freeze":
                self.mbs_dirdyn_ptr.contents.options.contents.n_freeze = value
Louis Beauloye's avatar
Louis Beauloye committed
762
763
            elif key == "show_failed_closure":
                self.mbs_dirdyn_ptr.contents.options.contents.show_failed_closure = value
Louis Beauloye's avatar
Louis Beauloye committed
764
765
            elif key == "store_results":
                self.store_results = value
766
767
768
769
770
771
772
773
774
775
            else:
                print(">>DIRDYN>> The option "+key+ " is not defined in this module")
                
        
    def get_options(self, *args):
        """
        Get the specified options for Dirdyn module
        
        Parameters
        ----------
776
        The different options specifed in the documentation of 'set_options()'
777
778
779
        
        Returns
        -------
780
        The value of the different options specifed in the documentation of 'set_options()'
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
        """
        options = []
        for key in args:
            if key == "t0":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.t0)
            elif key == "tf":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.tf)
            elif key == "dt0":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.dt0)
            elif key == "save2file":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.save2file)
            elif key == "resfilename":
                options.append(ctypes.string_at(self.mbs_dirdyn_ptr.contents.options.contents.resfilename).decode("utf-8"))
            elif key == "respath":
                options.append(ctypes.string_at(self.mbs_dirdyn_ptr.contents.options.contents.respath).decode("utf-8"))
            elif key == "animpath":
                options.append(ctypes.string_at(self.mbs_dirdyn_ptr.contents.options.contents.animpath).decode("utf-8"))
            elif key == "save_anim":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.save_anim)
            elif key == "save_visu":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.save_visu)
            elif key == "framerate":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.framerate)
            elif key == "saveperiod":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.saveperiod)
            elif key == "max_save_user":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.max_save_user)
            elif key == "buffersize":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.buffersize)
            elif key == "realtime":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.realtime)
            elif key == "accelred":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.accelred)
            elif key == "flag_compute_Qc":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.flag_compute_Qc)
Louis Beauloye's avatar
Louis Beauloye committed
816
817
            elif key == "compute_all_uxd":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.compute_all_uxd)
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
            elif key == "compute_Qc":
                compute_Qc_py = np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.options.contents.compute_Qc, (self.mbs.mbs_data_ptr.contents.njoint+1,))
                options.append(compute_Qc_py)
            elif key == "integrator":
                if self.mbs_dirdyn_ptr.contents.options.contents.integrator == 0:
                    options.append("RK4")
                elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 1:
                    options.append("Dopri5")
                elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 2:
                    options.append("Rosenbrock")
                elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 3:
                    options.append("EulerEx")    
                elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 4:
                    options.append("Eulaire") 
                elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 5:
                    options.append("EulerIm")  
                elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 6:
                    options.append("Bader")
                elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 7:
                    options.append("WMethods")   
                elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 8:
                    options.append("Custom")   
            elif key == "verbose":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.verbose)
            elif key == "flag_stop_stiff":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.flag_stop_stiff)
            elif key == "flag_precise_dynamics":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.flag_precise_dynamics)
            elif key == "flag_waypoint":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.flag_waypoint)
            elif key == "flag_solout_wp":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.flag_solout_wp)
            elif key == "delta_t_wp":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.delta_t_wp)
            elif key == "nmax":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.nmax)
            elif key == "toler":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.toler)
            elif key == "rtoler":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.rtoler)
            elif key == "atoler":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.atoler)
            elif key == "dt_max":
                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)
Louis Beauloye's avatar
Louis Beauloye committed
864
865
            elif key == "show_failed_closure":
                options.append(self.mbs_dirdyn_ptr.contents.options.contents.show_failed_closure)
Louis Beauloye's avatar
Louis Beauloye committed
866
867
            elif key == "store_results":
                options.append(self.store_results)
868
869
            else:
                print(">>DIRDYN>> The option "+key+ " is not defined in this module")
870
871
872
873
874
                
        if len(options) == 0:
            return
        if len(options) == 1:
            return options[0]
875
876
            
        return tuple(options)
877
    
878
                
879
880
881
882
883
884
885
886
887
888
889
890
891
    #==========================================================================
    # Defining properties
    #==========================================================================
    @property
    def mbs_filename(self):
        return self.mbs.mbs_filename
    
    @property
    def project_path(self):
        return self.mbs.project_path
    
    @property
    def mbs_name(self):
892
        return self.mbs.mbs_name
893
    
894
895
896
897
898
899
900
901
    @property
    def tsim(self):
        return self.mbs_dirdyn_ptr.contents.tsim
    
    @property
    def dt(self):
        return self.mbs_dirdyn_ptr.contents.dt
    
902
903
    
class MbsResult(object):
904
905
906
    """
    Class containing results.
    
907
908
    The user-specified vector are not available.
    
909
910
911
912
913
914
915
916
917
918
919
920
    Attributes
    ----------
    q : ndarray
        Numpy array containing the current values of the generalized 
        coordinates.
    qd : ndarray
        Numpy array containing the current values of the generalized 
        velocities.
    qdd : ndarray
        Numpy array containing the current values of the generalized 
        accelerations.
    Qq : ndarray
921
922
923
924
925
926
927
928
        Numpy array containing the values of the joint forces.
    Qc : ndarray
        Numpy array containing the value of joint force introduced in driven 
        joints to respect the user function
    qa : ndarray of int
        Numpy array of integers containing the indices of actuated 
        articulations (only for inverse dynamic). Those articulations are 
        controlled by an actuator.
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
    t : ndarray
        Numpy array containing the values of the time vector
    ux : ndarray
        Numpy array containing the values of the user variables.
    uxd : ndarray
        Numpy array containing the values of the time derivative of the user 
        variables.
    Fl : ndarray
        Numpy array containing the current values of the forces between of 
        the points of a link.
    Z : ndarray
        Numpy array containing the current values of the distances between of 
        the points of a link.
    Zd : ndarray
        Numpy array containing the current values of the speed (spreading) 
        between of the points of a link.
    outputs : dict
        Dict containing the names and the values of the user outputs
    mbs : MbsData
        instance of MbsData from where results come from
    """
950
951
        
    def __init__(self,mbs):
952
953
954
        """
            
        """
955
956
957
958
959
960
961
        self.q=[]
        self.qd=[]
        self.qdd=[]
        self.Fl=[]
        self.Z=[]
        self.Zd=[]
        self.Qq=[]
962
        self.Qa=[]
963
964
965
        self.t=[]
        self.ux=[]
        self.uxd=[]
966
        self.Qc=[]
967
968
969
        self.outputs={}
        self.mbs=mbs
    
970
    def load_results_from_file(self, filename, result_path="resultsR", module=0):
971
        """
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
        Load the results from the files into a MbsResult instance.
        
        This function is called if the buffers don not contain the full simulation.
        
        Parameters
        ----------
        filename : str
            The resfilename containing the results to load. The required suffix
            (ie. "_q.res") will be added automatically
        result_path : str, optional
            The relative path of the result folder from the project folder.
            default is "resultsR"
        module : int
            The module in which this function is called. In some modules, some 
            results files don't exist. Module ids corresponds to the 
            'MbsData.process' value. If set to 0 we try to load all files
            default is 0
989
        """
990
991
        
        project_path = self.mbs.project_path
992
993
        baseFileName = os.path.basename(filename)    
        baseFileName = baseFileName[:-6]
994
        result_path = os.path.join(project_path, result_path)
995
996
997
998
999
1000
        # Error handeling
        if not os.path.isdir(result_path):
            if __DEBUG__ : print ('DEBUG>> The result directory does not exist: "'+ result_path+'"')
            
        # Generalized coordinates
        CurFile = baseFileName+'_q.res'