mbs_solvekin.c 22.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
 * @file mbs_solvekin.c
 *
 * This file implements the  functions of the
 * solvekin module in C.
 *
 * Creation date: 15/12/2018
 * @author Olivier Lantsoght
 *
 * (c) Universite catholique de Louvain
 */

#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#include "lut.h"
#include "MBSfun.h"
#include "set_output.h"
#include "mbs_message.h"
#include "mbs_solvekin.h"
#include "mbs_solvekin_struct.h"
#include "mbs_project_interface.h"


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

MbsSolvekin* mbs_new_solvekin(MbsData* mbs_data)
{
    MbsAux* mbs_aux;

    // Initialize the local data struct
    mbs_aux = initMbsAux(mbs_data);

    return mbs_new_solvekin_aux(mbs_data, mbs_aux);
}

MbsSolvekin* mbs_new_solvekin_aux(MbsData* mbs_data, MbsAux* mbs_aux)
{
    MbsSolvekin *solvekin;
    MbsSolvekinOptions* opts;

    // Initialize the direct dynamic structure
    solvekin = (MbsSolvekin*)malloc(sizeof(MbsSolvekin));

    // keep the pointer to the auxiliary data structure
    solvekin->mbs_aux = mbs_aux;

    // Initialize the options struct with default options
    opts = (MbsSolvekinOptions*)malloc(sizeof(MbsSolvekinOptions));
    solvekin->options = opts;
    opts->motion = oneshot;
    opts->trajectoryqname = NULL;
    opts->trajectoryqdname = NULL;
    opts->trajectoryqddname = NULL;
    opts->t0 = 0.0;
    opts->tf = 5.0;
    opts->dt = 1e-3;
    opts->save2file = 1;
    opts->save_anim = 1;
    opts->framerate = 1000;
    opts->saveperiod = 1;
    opts->max_save_user = 12;
    opts->buffersize = -1;
    opts->resfilename = NULL;
    opts->respath = NULL;
    opts->animpath = NULL;

    return solvekin;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

void mbs_delete_solvekin(MbsSolvekin* solvekin, MbsData* mbs_data)
{
    free(solvekin->options);
    freeMbsAux(solvekin->mbs_aux, mbs_data);

    if (solvekin->q != NULL){
        // Cleaning LUT
        mbs_lut_res_free(solvekin->q);
        mbs_lut_res_free(solvekin->qd);
        mbs_lut_res_free(solvekin->qdd);
    }
    free(solvekin);
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

void mbs_run_solvekin(MbsSolvekin* sk, MbsData* mbs_data)
{
    // 1. Initialize the simulation
    // - - - - - - - - - - - - - -
    mbs_solvekin_init(sk, mbs_data);


    // 2. Run the simulation
    // - - - - - - - - - - -
    mbs_solvekin_loop(sk, mbs_data);


    // 3. Finish the simulation
    // - - - - - - - - - - - -
    mbs_solvekin_finish(sk, mbs_data);
}

void mbs_solvekin_save(MbsSolvekin* sk, MbsData *mbs_data, double t){

    int i, err =0;

    for (i=0 ; i < sk->bufferNb ; i++){
        err = mbs_buffer_save(sk->buffers[i], t, sk->savedArrays[i]);
    }
    
    if (err < 0)
    {
        mbs_msg(">>SOLVEKIN>> ***** mbs_solvekin_save : impossible to save the files *****\n");
        mbs_msg(">>SOLVEKIN>> ***** during buffer save *****\n");
        mbs_msg(">>SOLVEKIN>>\n");
        mbs_error_msg("[%d] in mbs_fct_solvekin !! \n", -500 + err);
    }

122
    err = mbs_growing_buffer_save(sk->user_buffer, t);
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
   
    if (err < 0)
    {
        mbs_msg(">>SOLVEKIN>> ***** mbs_solvekin_save : impossible to save the files *****\n");
        mbs_msg(">>SOLVEKIN>> ***** during growing buffer save *****\n");
        mbs_msg(">>SOLVEKIN>>\n");
        mbs_error_msg("[%d] in mbs_fct_solvekin !! \n", -500 + err);
    }
}

void mbs_solvekin_init(MbsSolvekin* sk, MbsData* mbs_data)
{
    int i;

    // INITIALIZATION
    // - - - - - - - -

    // Relevance of the analysis
    if (!(mbs_data->Ncons > 0)){
        mbs_msg(">>SOLVEKIN>>> This analysis is irrelevant in absence of constraints.\n");
        mbs_error_msg("[%d] in mbs_solvekin_init !! \n", -500);
    }
    if ( mbs_data->nqv == 0){
        mbs_msg(">>SOLVEKIN>>> This analysis is irrelevant in absence of dependant variable.\n");
147
        mbs_error_msg("[%d] in mbs_solvekin_init !! \n", -500);
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    }
    // base value, all cases
    sk->tindex = 0;
    // Motion identification
    if (sk->options->motion==oneshot){
        sk->trajectorytype = 0;
        sk->tsim = mbs_data->tsim;
        sk->options->buffersize=1;
        sk->q = NULL;
        sk->qd = NULL;
        sk->qdd = NULL;
    }
    if (sk->options->motion==trajectory){
        if (sk->options->trajectoryqname == NULL || sk->options->trajectoryqdname == NULL || sk->options->trajectoryqddname == NULL){
            if (mbs_data->nqu!=0){
                mbs_msg(">>SOLVEKIN>>> No trajectory (q, qd or qdd) file provided for independant joints.\n");
164
                mbs_error_msg("[%d] in mbs_solvekin_init !! \n", -509);
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
            }
            sk->trajectorytype = 1;
            sk->q = NULL;
            sk->qd = NULL;
            sk->qdd = NULL;

            mbs_data->t0   = sk->options->t0;
            mbs_data->tsim = sk->options->t0;
            mbs_data->tf   = sk->options->tf;
            mbs_data->dt0  = sk->options->dt;
            sk->tsim = sk->options->t0;
            if (mbs_data->dt0 <= 0.0){
                mbs_msg(">>SOLVEKIN>>> time step has to be stricly positive (>0) for a fully driven kinematic analysis.\n");
                mbs_error_msg("[%d] in mbs_solvekin_init !! \n", -500);
            }
            if (mbs_data->t0 >= mbs_data->tf){
                mbs_msg(">>SOLVEKIN>>> initial time has to be stricly lower (<) than final time.\n");
                mbs_error_msg("[%d] in mbs_solvekin_init !! \n", -500);
            }
        }
        else{
            sk->q = mbs_lut_res_load_file(sk->options->trajectoryqname);
            sk->qd = mbs_lut_res_load_file(sk->options->trajectoryqdname);
            sk->qdd = mbs_lut_res_load_file(sk->options->trajectoryqddname);
189

190
191
192
193
194
195
196
            if (sk->q->nq == mbs_data->njoint){
                sk->trajectorytype = 2;
            }
            else if (sk->q->nq == mbs_data->nqu){
                sk->trajectorytype = 3;
            }
            else{
Unknown's avatar
Unknown committed
197
                mbs_msg(">>SOLVEKIN>>> Provided trajectories number of variable (%d) does not match neither:\n", sk->q->nq);
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
                mbs_msg("          >>> The number of total joint ([%d]);\n", mbs_data->njoint);
                mbs_msg("          >>> The number of independant joint ([%d]);\n", mbs_data->nqu);
                mbs_error_msg("[%d] in mbs_solvekin_init !! \n", -500);

            }
            if (sk->q->nq != sk->qd->nq || sk->q->nq != sk->qdd->nq){
                mbs_msg(">>SOLVEKIN>>> Number of coordinates is not consistent between trajectories file.\n");
                mbs_error_msg("[%d] in mbs_solvekin_init !! \n", -500);
            }
            if (sk->options->dt <= 0){
                // using time vector of the input file
                mbs_data->t0   = sk->q->tq[0][0];
                mbs_data->tsim = sk->q->tq[0][0];
                mbs_data->tf   = sk->q->tq[sk->q->nt-1][0];
                mbs_data->dt0  = 0.0;
                sk->tsim = sk->options->t0;
            }
            else{
                mbs_data->t0   = sk->options->t0;
                mbs_data->tsim = sk->options->t0;
                mbs_data->tf   = sk->options->tf;
                mbs_data->dt0  = sk->options->dt;
                sk->tsim = sk->options->t0;
221
222
223
224
                if (mbs_data->t0 >= mbs_data->tf){
                    mbs_msg(">>SOLVEKIN>>> initial time has to be stricly lower (<) than final time.\n");
                    mbs_error_msg("[%d] in mbs_solvekin_init !! \n", -500);
                }
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
            }

        }
    }

    // user initialization
    if(sk->options->save2file)
    {
        // 'auto_output' structure initialized, needed for add_output_vector
        init_set_output(sk->options->max_save_user);
    }
    // Not yet implemented
    //user_solvekin_init(mbs_data, sk);

    if(sk->options->save2file)
    {
        char *resfilename, *respath, *animpath;
        int bufId, bufSize, njoint, Nux, Nlink, nqc;
        char* f;
        char* f_anim;

        char **fnameSuffix;
        int *bufferIDs;
        int *bufElemNb;

        njoint = mbs_data->njoint;
        Nux = mbs_data->Nux;
        Nlink = mbs_data->Nlink;
        nqc = mbs_data->nqc;

        // allocate buffer at needed size
        bufSize = 3 + get_output_vector_nb();
        fnameSuffix = (char**) malloc(bufSize*sizeof(char*));
        bufferIDs = (int*) malloc(bufSize*sizeof(int));
        bufElemNb = (int*) malloc(bufSize*sizeof(int));
        
        // set the filename if not specified
        if (sk->options->resfilename == NULL) {
            resfilename = "solvekin";
        }
        else {
            resfilename = sk->options->resfilename;
        }

        // set the path to result files if not specified
        if (sk->options->respath == NULL)
        {
            respath = (char*)malloc(sizeof(char)*(strlen(mbs_data->project_path) + 100));
            strcpy(respath, mbs_data->project_path);
            strcat(respath, "/resultsR");
        }
        else
        {
            respath = strdup(sk->options->respath);
        }

        // set the path to anim file if not specified
        if (sk->options->animpath == NULL)
        {
            animpath = (char*)malloc(sizeof(char)*(strlen(mbs_data->project_path) + 100));
            strcpy(animpath, mbs_data->project_path);
            strcat(animpath, "/animationR");
        }
        else
        {
            animpath = strdup(sk->options->animpath);
        }

        // generic buffer properties
        fnameSuffix[0] = "q";    bufferIDs[0] = BUFFER_Q;    bufElemNb[0]=njoint;
        fnameSuffix[1] = "qd";   bufferIDs[1] = BUFFER_QD;   bufElemNb[1]=njoint;
        fnameSuffix[2] = "qdd";  bufferIDs[2] = BUFFER_QDD;  bufElemNb[2]=njoint;
        sk->bufferNb=3;

        // buffer properties for the user specified vectors
        if (get_output_vector_nb()){
            int id = sk->bufferNb;
            for (i=0; i<get_output_vector_nb() ; i++){
                fnameSuffix[id] = get_output_vector_label(i);    bufferIDs[id] = BUFFER_OTHER+i;    bufElemNb[id]=get_output_vector_size(i);
                id +=1;
                sk->bufferNb += 1;
            }

        }

        // set the buffer size if not specified
        if (sk->options->buffersize<1){
            if (mbs_data->dt0 != 0){
                sk->options->buffersize = (int) ( (sk->options->tf - sk->options->t0)/
                                                  (mbs_data->dt0 * sk->options->saveperiod) + 1 );
            }
            else{
                sk->options->buffersize = sk->q->nt-1;
            }
        }
        
        // initialize the buffers for saving results
        sk->buffers = (MbsBuffer**)malloc(sk->bufferNb*sizeof(MbsBuffer*));

        f      = (char*)malloc(sizeof(char)*(strlen(respath) +strlen(resfilename)+30));
        f_anim = (char*)malloc(sizeof(char)*(strlen(animpath)+strlen(resfilename)+30));

        for(bufId=0 ; bufId < sk->bufferNb; bufId++){
            sprintf(f,      "%s/%s_%s.res",  respath,  resfilename, fnameSuffix[bufId]);
            sprintf(f_anim, "%s/%s_%s.anim", animpath, resfilename, fnameSuffix[bufId]);

            sk->buffers[bufId] = mbs_new_buffer(f, f_anim, bufElemNb[bufId], sk->options->buffersize, bufferIDs[bufId], sk->options->save_anim, 0, 1./(double)sk->options->framerate);
        }

        // growing buffer for user variables
        char * path_growing;
        path_growing = (char*) malloc((strlen(respath)+strlen(resfilename)+10) * sizeof(char));
        sprintf(path_growing, "%s/%s", respath, resfilename);
        sk->user_buffer = mbs_new_growing_buffer(sk->options->max_save_user, sk->options->buffersize, path_growing);

        // binding buffers to data that must be saved
        sk->savedArrays = (double**) malloc(sk->bufferNb*sizeof(double*));
        bufId = 0;  sk->savedArrays[bufId] = mbs_data->q+1;
        bufId++  ;  sk->savedArrays[bufId] = mbs_data->qd+1;
        bufId++  ;  sk->savedArrays[bufId] = mbs_data->qdd+1;

        if(get_output_vector_nb()){
            for (i=0; i<get_output_vector_nb() ; i++){
                bufId++  ;  sk->savedArrays[bufId] = get_output_vector_ptr(i);
            }
        }
        // release memory
        free(f_anim);
        free(f);
        free(respath);
        free(animpath);
        free(path_growing);
        free(fnameSuffix);
        free(bufferIDs);
        free(bufElemNb);
    }
    else{
        reset_flag_output();
    }
}

void mbs_solvekin_loop(MbsSolvekin* sk, MbsData* mbs_data)
{
    int err = 0, err2=0, i;
    if (!sk->trajectorytype){
        // Oneshot case, just call the mbs_fct_solvekin
        err = mbs_fct_solvekin(mbs_data, sk);

        if (err < 0) // Error management
        {
            mbs_msg(">>SOLVEKIN>>\n");
            mbs_msg(">>SOLVEKIN>>  Error during inverse kinematic analysis at time %g for oneshot case !\n", mbs_data->tsim);
            mbs_error_msg("[%d] in mbs_solvekin_loop !! \n", -500 + err);
        }

        // Check contents of vectors
        for (i=1; i<=mbs_data->njoint;i++){
            if (isnan(mbs_data->q[i])) {
                mbs_msg(">>SOLVEKIN>> q[%d] is Nan at time %g s ! \n", i, mbs_data->tsim );
                err2 = mbs_solvekin_write_buffers(sk);
                if (err2<0){
                    mbs_msg(">>SOLVEKIN>>\n");
387
                    mbs_msg(">>SOLVEKIN>>  Error [%d] while saving results to disk !\n", -500 + err2);
388
389
390
391
392
393
394
395
396
                    mbs_msg(">>SOLVEKIN>>\n");
                }
                mbs_error_msg("[%d] in mbs_solvekin_loop !! \n", -570);
            }
            if (isnan(mbs_data->qd[i])) {
                mbs_msg(">>SOLVEKIN>> qd[%d] is Nan at time %g s ! \n", i, mbs_data->tsim );
                err2 = mbs_solvekin_write_buffers(sk);
                if (err2<0){
                    mbs_msg(">>SOLVEKIN>>\n");
397
                    mbs_msg(">>SOLVEKIN>>  Error [%d] while saving results to disk !\n", -500 + err2);
398
399
400
401
402
403
404
405
406
                    mbs_msg(">>SOLVEKIN>>\n");
                }
                mbs_error_msg("[%d] in mbs_solvekin_loop !! \n", -570);
            }
            if (isnan(mbs_data->qdd[i])) {
                mbs_msg(">>SOLVEKIN>> qdd[%d] is Nan at time %g s ! \n", i, mbs_data->tsim );
                err2 = mbs_solvekin_write_buffers(sk);
                if (err2<0){
                    mbs_msg(">>SOLVEKIN>>\n");
407
                    mbs_msg(">>SOLVEKIN>>  Error [%d] while saving results to disk !\n", -500 + err2);
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
                    mbs_msg(">>SOLVEKIN>>\n");
                }
                mbs_error_msg("[%d] in mbs_solvekin_loop !! \n", -570);
            }
        }

        // adding to buffer
        mbs_solvekin_save(sk, mbs_data, mbs_data->tsim);
    }
    else{
        // Time loop, time updated at the end of loop
        while (mbs_data->tsim <= mbs_data->tf){
            if (mbs_data->flag_stop){
                // stop the simulation if 'flag_stop' set to 1
                break;
            }

            // Computation of q, qd, qdd
            err = mbs_fct_solvekin(mbs_data, sk);

            if (err < 0) // Error management
            {
                mbs_msg(">>SOLVEKIN>>\n");
                mbs_msg(">>SOLVEKIN>>  Error during inverse kinematic analysis at time %g for trajectory case !\n", mbs_data->tsim);
                err2 = mbs_solvekin_write_buffers(sk);
                if (err2<0){
                    mbs_msg(">>SOLVEKIN>>\n");
435
                    mbs_msg(">>SOLVEKIN>>  Error [%d] while saving results to disk !\n", -500 + err2);
436
437
438
439
440
441
442
443
444
445
446
447
                    mbs_msg(">>SOLVEKIN>>\n");
                }
                mbs_error_msg("[%d] in mbs_solvekin_loop !! \n", -500 + err);
            }

            // Check contents of vectors
            for (i=1; i<=mbs_data->njoint;i++){
                if (isnan(mbs_data->q[i])) {
                    mbs_msg(">>SOLVEKIN>> q[%d] is Nan at time %g s ! \n", i, mbs_data->tsim );
                    err2 = mbs_solvekin_write_buffers(sk);
                    if (err2<0){
                        mbs_msg(">>SOLVEKIN>>\n");
448
                        mbs_msg(">>SOLVEKIN>>  Error [%d] while saving results to disk !\n", -500 + err2);
449
450
451
452
453
454
455
456
457
                        mbs_msg(">>SOLVEKIN>>\n");
                    }
                    mbs_error_msg("[%d] in mbs_solvekin_loop !! \n", -570);
                }
                if (isnan(mbs_data->qd[i])) {
                    mbs_msg(">>SOLVEKIN>> qd[%d] is Nan at time %g s ! \n", i, mbs_data->tsim );
                    err2 = mbs_solvekin_write_buffers(sk);
                    if (err2<0){
                        mbs_msg(">>SOLVEKIN>>\n");
458
                        mbs_msg(">>SOLVEKIN>>  Error [%d] while saving results to disk !\n", -500 + err2);
459
460
461
462
463
464
465
466
467
                        mbs_msg(">>SOLVEKIN>>\n");
                    }
                    mbs_error_msg("[%d] in mbs_solvekin_loop !! \n", -570);
                }
                if (isnan(mbs_data->qdd[i])) {
                    mbs_msg(">>SOLVEKIN>> qdd[%d] is Nan at time %g s ! \n", i, mbs_data->tsim );
                    err2 = mbs_solvekin_write_buffers(sk);
                    if (err2<0){
                        mbs_msg(">>SOLVEKIN>>\n");
468
                        mbs_msg(">>SOLVEKIN>>  Error [%d] while saving results to disk !\n", -500 + err2);
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
                        mbs_msg(">>SOLVEKIN>>\n");
                    }
                    mbs_error_msg("[%d] in mbs_solvekin_loop !! \n", -570);
                }
            }

            // adding to buffer
            mbs_solvekin_save(sk, mbs_data, mbs_data->tsim);

            // Updating time
            if(mbs_data->dt0){
                // Time range specified
                mbs_data->tsim += mbs_data->dt0;
            }
            else{
                if(sk->tindex <sk->q->nt-1){
                    // Time vector from input trajectory
                    sk->tindex ++;
                    mbs_data->tsim = sk->q->tq[sk->tindex][0];}
                else{
                    break;
                }
            }
        }
    }
}

void mbs_solvekin_finish(MbsSolvekin* sk, MbsData* mbs_data){
    int i;
498
    int err = 0; // error number
499
500
501
502
503
504
505
506
507
508
509

    // ENDING SIMULATION
    // - - - - - - - -

    // user finalization
    //user_solvekin_finish(mbs_data, sk);

    // Buffers cleaning
    if(sk->options->save2file)
    {
        // write the buffer (in case they were not saved automatically)
510
511
512
513
514
        err = mbs_solvekin_write_buffers(sk);

        if (err < 0) {
            mbs_msg(">>SOLVEKIN>>\n");
            mbs_msg(">>SOLVEKIN>> in mbs_solvekin_finish : Error [%d] while saving results to disk !\n", -500 + err);
515
            mbs_error_msg(">>SOLVEKIN>>\n");
516
517
        }

518
519
        // free memory used by buffers
        for(i=0 ; i < sk->bufferNb; i++){
520
           mbs_delete_buffer(sk->buffers[i]);
521
522
523
524
        }
        free(sk->buffers);

        // user inputs final gestion
525
526
527
528
529
530
531
        err = mbs_growing_buffer_write(sk->user_buffer);

        if (err < 0) {
            mbs_msg(">>SOLVEKIN>>\n");
            mbs_msg(">>SOLVEKIN>> in mbs_solvekin_finish : Error [%d] while saving results to disk !\n", -500 + err);
            mbs_error_msg(">>SOLVEKIN>>\n");
        }
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558

        mbs_delete_growing_buffer(sk->user_buffer);

        // release memory for the 'auto_output' structure
        free_set_output();
        reset_flag_output();
        free(sk->savedArrays);
    }
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


int mbs_solvekin_write_buffers(MbsSolvekin* sk){
    int i, err =0;
    for (i=0 ; i < sk->bufferNb ; i++){
        err = mbs_buffer_write(sk->buffers[i]);
        if (err < 0) { return err; }
    }   
    return err;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


int mbs_fct_solvekin(MbsData *s, MbsSolvekin *sk)
{
559
    int err = 0;
560
    int err1=0, err2=0, err3=0;
561
    int i, k, index, nL, nK;
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
    double term;
    // Allocating output vector for Look Up Table
    // TODO : Storing and allocated in sk
    double *lutq=NULL;
    double *lutqd=NULL;
    double *lutqdd=NULL;
    if (sk->trajectorytype == 2){
        lutq = get_dvec_1(s->njoint);
        lutqd = get_dvec_1(s->njoint);
        lutqdd = get_dvec_1(s->njoint);
    }
    else if (sk->trajectorytype == 3){
        lutq = get_dvec_1(s->nqu);
        lutqd = get_dvec_1(s->nqu);
        lutqdd = get_dvec_1(s->nqu);
    }

    // Computation of driven variable and independant joints
    if (s->nqc && sk->trajectorytype!=2){
        // if there is driven and if they are not defined in trajectory file
        user_DrivenJoints(s, s->tsim);
    }
    else if(sk->trajectorytype==2) {
        // Also compute independant joint
586
587
588
        err1 = mbs_lut_res_interp(sk->q, s->tsim, lutq);
        err2 = mbs_lut_res_interp(sk->qd, s->tsim, lutqd);
        err3 = mbs_lut_res_interp(sk->qdd, s->tsim, lutqdd);
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
        for(i=1; i<=s->nqu; i++){
            index = s->qu[i];
            s->q[index] = lutq[index];
            s->qd[index] = lutqd[index];
            s->qdd[index] = lutqdd[index];
        }
        if(s->nqc){
            for(i=1; i<=s->nqc; i++){
                index = s->qc[i];
                s->q[index] = lutq[index];
                s->qd[index] = lutqd[index];
                s->qdd[index] = lutqdd[index];
            }
        }
    }
    if (sk->trajectorytype==3){
605
606
607
        err1 = mbs_lut_res_interp(sk->q, s->tsim, lutq);
        err2 = mbs_lut_res_interp(sk->qd, s->tsim, lutqd);
        err3 = mbs_lut_res_interp(sk->qdd, s->tsim, lutqdd);
608
609
610
611
612
613
614
        for(i=1; i<=s->nqu; i++){
            index = s->qu[i];
            s->q[index] = lutq[index];
            s->qd[index] = lutqd[index];
            s->qdd[index] = lutqdd[index];
        }
    }
615
616
617
618
    if (err1<0 || err2<0 || err3<0){
        mbs_msg("\t >SOLVEKIN> Impossible to interpolate trajectory\n");
        return -1;
    }
619
620
621

    // Solving constraints, Ncons checked in initialization
    // Geometric resolution
622
623
    err = mbs_close_geo(s, sk->mbs_aux);
    if (err<0)  // Error management
624
    {
625
626
        mbs_msg("\t \t in >SOLVEKIN> \n");
        return err;
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
    }

    // Kinematic resolution
    mbs_close_kin(s, sk->mbs_aux);

    // Acceleration resolution
    mbs_calc_jdqd(s, sk->mbs_aux);
    // qdd_v = Bvuc*qdd_uc + bp
    nL = s->nqv;
    nK = sk->mbs_aux->nquc;
    for (i = 1; i <= nL; i++)
    {
        term = 0.0;
        for (k = 1; k <= nK; k++)
        {
            term += sk->mbs_aux->Bvuc[i][k] * s->qdd[sk->mbs_aux->iquc[k]];
        }
        s->qdd[s->qv[i]] = term + sk->mbs_aux->bp[i];
    }

    // Freeing
    if(lutq!=NULL){
        free_dvec_1(lutq);
        free_dvec_1(lutqd);
        free_dvec_1(lutqdd);
    }
653
    return err;
654
}