d4c94a22a9735ad6e0e8e95bde40c80e6b907a18
[scilab.git] / scilab / modules / scicos / sci_gateway / cpp / sci_scicosim.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2014 - Scilab Enterprises - Paul Bignier
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15
16 #include <string>
17 #include <cstdio>
18 #include <cwchar>
19 #include <cstring>
20
21 #include "gw_scicos.hxx"
22
23 #include "internal.hxx"
24 #include "types.hxx"
25 #include "context.hxx"
26 #include "double.hxx"
27 #include "function.hxx"
28 #include "macro.hxx"
29 #include "list.hxx"
30 #include "tlist.hxx"
31 #include "string.hxx"
32 #include "int.hxx"
33
34 #include "il_state.hxx"
35 #include "il_sim.hxx"
36
37 extern "C"
38 {
39 #include "machine.h" /* C2F */
40 #include "import.h"
41 #include "sci_malloc.h"
42 #include "scicos.h" /* funnum2() */
43 #include "scicos-def.h"
44 #include "charEncoding.h"
45
46 #include "localization.h"
47 #include "Scierror.h"
48
49     COSIM_struct C2F(cosim);
50     /*--------------------------------------------------------------------------*/
51     // Variables defined in scicos.c
52     extern COSERR_struct coserr;
53     extern CURBLK_struct C2F(curblk);
54     /*--------------------------------------------------------------------------*/
55 }
56
57 #ifdef _MSC_VER
58 #define snprintf _snprintf
59 #endif
60
61 // anonymous namespace for helper functions
62 namespace
63 {
64 void delete_strings(char** l_sim_lab, int len)
65 {
66     for (int i = 0; i < len; i++)
67     {
68         FREE(l_sim_lab[i]);
69     }
70     delete[] l_sim_lab;
71 }
72
73 } // namespace
74
75
76 /*--------------------------------------------------------------------------*/
77 /* intsicosimc scicosim interface routine.
78 *
79 * [state,t] = scicosim(state,tcur,tf,sim,str,tol)
80 *
81 * rhs 1 state : Tlist
82 *        - 1  : state(1)     : !xcs  x  z  oz iz  tevts  evtspt  pointi  outtb  !
83 *        - 2  : state.x      : column vector of real
84 *        - 3  : state.z      : column vector of real
85 *        - 4  : state.oz     : list of scilab object
86 *        - 5  : state.iz     : column vector of real (empty object with flag "finish")
87 *        - 6  : state.tevts  : column vector of real
88 *        - 7  : state.evtspt : column vector of real
89 *        - 8  : state.pointi : real scalar
90 *        - 9  : state.outtb  : list of scilab object
91 * rhs 2 tcur  : real scalar
92 * rhs 3 tf    : real scalar
93 * rhs 4 sim   : Tlist
94 *        - 1  : sim(1) : !scs    funs    xptr    zptr    ozptr   zcptr   inpptr
95 *                         outptr inplnk  outlnk  rpar    rpptr   ipar    ipptr
96 *                         opar   opptr   clkptr  ordptr  execlk  ordclk  cord
97 *                         oord   zord    critev  nb      ztyp    nblk    ndcblk
98 *                         subscr funtyp  iord    labels  modptr  uids !
99 *        - 2  : sim.funs   : list of strings and/or scilab function
100 *        - 3  : sim.xptr   : column vector of real
101 *        - 4  : sim.zptr   : column vector of real
102 *        - 5  : sim.ozptr  : column vector of real
103 *        - 6  : sim.zcptr  : column vector of real
104 *        - 7  : sim.inpptr : column vector of real
105 *        - 8  : sim.outptr : column vector of real
106 *        - 9  : sim.inplnk : column vector of real
107 *        - 10 : sim.outlnk : column vector of real
108 *        - 11 : sim.rpar   : column vector of real
109 *        - 12 : sim.rpptr  : column vector of real
110 *        - 13 : sim.ipar   : column vector of real
111 *        - 14 : sim.ipptr  : column vector of real
112 *        - 15 : sim.opar   : list of scilab object
113 *        - 16 : sim.opptr  : column vector of real
114 *        - 17 : sim.clkptr : column vector of real
115 *        - 18 : sim.ordptr : column vector of real
116 *        - 19 : sim.execlk : matrix of real
117 *        - 20 : sim.ordclk : matrix of real
118 *        - 21 : sim.cord   : matrix of real
119 *        - 22 : sim.oord   : matrix of real
120 *        - 23 : sim.zord   : column vector ? of real
121 *        - 24 : sim.critev : column vector of real
122 *        - 25 : sim.nb     : real scalar
123 *        - 26 : sim.ztyp   : column vector of real
124 *        - 27 : sim.nblk   : real scalar
125 *        - 28 : sim.ndcblk : real scalar
126 *        - 29 : sim.subscr : column vector of real
127 *        - 30 : sim.funtyp : column vector of real
128 *        - 31 : sim.iord   : column vector of real
129 *        - 32 : sim.labels : column vector of strings
130 *        - 33 : sim.modptr : column vector of real
131 *        - 34 : sim.uids : column vector of strings
132 *
133 * rhs 5 str   : string flag : 'start','run','finish','linear'
134 * rhs 6 tol   : real vector of size (7,1) minimum (4,1)
135 *               [atol rtol ttol [deltat realtimescale solver hmax]]'
136 *
137 * 16/03/06, A.Layec : Rewritten from original fortran
138 *                     source code intsscicos in intcos.f.
139 *
140 * 29/03/06, Alan    : Improvement in accordance to c_pass2
141 *                     (int32 parameters).
142 *
143 * 31/05/06, Alan    : Add global variable int *il_state_save
144 *                     and int *il_sim_save in intcscicos.h to store
145 *                     stack address of list %cpr.state and %cpr.sim
146 *                     (to use with get/setscicosvars).
147 *
148 * 14/06/06, Alan    : Save common intersci before calling scicos
149 *                     (to disable scilab crash with scifunc.f).
150 *
151 * 13/11/06, Alan    : Get back to double parameters for sim and state
152 *                     (for better compatibility with scilab-4.x families).
153 *                     Remove il_sim_save global variable.
154 *
155 * 15/12/06, Alan    : Warnings compilation removed.
156 *                     This can crash scilab/scicos.
157 *                     Please report.
158 *
159 * xx/02/07, Alan    : Add opar/odstate : scilab lists of arbitrary object
160 *                     parameters/states.
161 *
162 * 08/12/14, Paul    : Rewrite to C++.
163 *
164 */
165 /*--------------------------------------------------------------------------*/
166
167 static const std::string funname = "scicosim";
168
169 types::Function::ReturnValue sci_scicosim(types::typed_list &in, int _iRetCount, types::typed_list &out)
170 {
171     /************************************
172     * Variables and constants definition
173     ************************************/
174     double simpar[7];
175
176     BOOL allocatedError = FALSE;
177     const int MAX_ERROR_LEN = 512;
178
179     if (in.size() != 6)
180     {
181         Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), funname.data(), 6);
182         return types::Function::Error;
183     }
184
185     if (_iRetCount > 2)
186     {
187         Scierror(78, _("%s: Wrong number of output argument(s): %d to %d expected.\n"), funname.data(), 1, 2);
188         return types::Function::Error;
189     }
190
191     /****************
192     * state (rhs 1)
193     ****************/
194     if (in[0]->isTList() == false)
195     {
196         Scierror(999, _("%s: Wrong type for input argument #%d : A tlist expected.\n"), funname.data(), 1);
197         return types::Function::Error;
198     }
199     types::TList* il_state_input = in[0]->getAs<types::TList>();
200     types::TList* il_state = new types::TList();
201
202     if (il_state_input->getSize() < 9)
203     {
204         Scierror(999, _("%s: Wrong size for input argument #%d : %d elements expected.\n"), funname.data(), 1, 9);
205         il_state->DecreaseRef();
206         il_state->killMe();
207         return types::Function::Error;
208     }
209
210     // Make a copy of 'il_state' in a global variabe
211     set_il_state(il_state_input);
212
213     types::String* header_state = il_state_input->get(0)->getAs<types::String>();
214     il_state->append(header_state->clone());
215
216     /*2 : state.x      */
217     if (il_state_input->get(1)->isDouble() == false)
218     {
219         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 2, 1);
220         il_state->DecreaseRef();
221         il_state->killMe();
222         return types::Function::Error;
223     }
224     types::Double* il_state_x_input = il_state_input->get(1)->getAs<types::Double>();
225     il_state->append(il_state_x_input->clone());
226     types::Double* il_state_x = il_state->get(1)->getAs<types::Double>();
227     double* l_state_x = il_state_x->get();
228
229     /*3 : state.z      */
230     if (il_state_input->get(2)->isDouble() == false)
231     {
232         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 3, 1);
233         il_state->DecreaseRef();
234         il_state->killMe();
235         return types::Function::Error;
236     }
237     types::Double* il_state_z_input = il_state_input->get(2)->getAs<types::Double>();
238     il_state->append(il_state_z_input->clone());
239     types::Double* il_state_z = il_state->get(2)->getAs<types::Double>();
240     double* l_state_z = il_state_z->get();
241
242     /*4 : state.oz     */
243     types::List* il_state_oz_input = il_state_input->get(3)->getAs<types::List>();
244     types::List* il_state_oz = new types::List();
245     for (int i = 0; i < il_state_oz_input->getSize(); ++i)
246     {
247         types::InternalType* l_state_oz_input = il_state_oz_input->get(i);
248         il_state_oz->append(l_state_oz_input->clone());
249     }
250     il_state->append(il_state_oz);
251     int noz = il_state_oz->getSize(); // 'nlnk' is the dimension of the list 'state.oz'
252
253     /*5 : state.iz     */
254     if (il_state_input->get(4)->isDouble() == false)
255     {
256         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 5, 1);
257         return types::Function::Error;
258     }
259     types::Double* il_state_iz_input = il_state_input->get(4)->getAs<types::Double>();
260     il_state->append(il_state_iz_input->clone());
261     types::Double* il_state_iz = il_state->get(4)->getAs<types::Double>();
262     void** l_state_iz = (void**) il_state_iz->get();
263     int m1e5 = il_state_iz->getRows();
264
265     /*6 : state.tevts  */
266     if (il_state_input->get(5)->isDouble() == false)
267     {
268         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 6, 1);
269         il_state->DecreaseRef();
270         il_state->killMe();
271         return types::Function::Error;
272     }
273     types::Double* il_state_tevts_input = il_state_input->get(5)->getAs<types::Double>();
274     il_state->append(il_state_tevts_input->clone());
275     types::Double* il_state_tevts = il_state->get(5)->getAs<types::Double>();
276     double* l_state_tevts = il_state_tevts->get();
277     int m1e6 = il_state_tevts->getRows();
278
279     /*7 : state.evtspt */
280     if (il_state_input->get(6)->isDouble() == false)
281     {
282         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 7, 1);
283         il_state->DecreaseRef();
284         il_state->killMe();
285         return types::Function::Error;
286     }
287     types::Double* il_state_evtspt_input = il_state_input->get(6)->getAs<types::Double>();
288     il_state->append(il_state_evtspt_input->clone());
289     types::Double* il_state_evtspt = il_state->get(6)->getAs<types::Double>();
290     il_state_evtspt->convertToInteger();
291     int* l_state_evtspt = (int*) il_state_evtspt->get();
292     int m1e7 = il_state_evtspt->getRows();
293
294     /*8 : state.pointi */
295     if (il_state_input->get(7)->isDouble() == false)
296     {
297         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 8, 1);
298         il_state->DecreaseRef();
299         il_state->killMe();
300         return types::Function::Error;
301     }
302     types::Double* il_pointi_input = il_state_input->get(7)->getAs<types::Double>();
303     il_state->append(il_pointi_input->clone());
304     types::Double* il_pointi = il_state->get(7)->getAs<types::Double>();
305     il_pointi->convertToInteger();
306     int* l_pointi = (int*) il_pointi->get();
307
308     /*9 : state.outtb  */
309     if (il_state_input->get(8)->isList() == false)
310     {
311         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A list expected.\n"), funname.data(), 9, 1);
312         il_state->DecreaseRef();
313         il_state->killMe();
314         return types::Function::Error;
315     }
316     types::List* il_state_outtb_input = il_state_input->get(8)->getAs<types::List>();
317     types::List* il_state_outtb = new types::List();
318     for (int i = 0; i < il_state_outtb_input->getSize(); ++i)
319     {
320         types::InternalType* l_state_outtb_input = il_state_outtb_input->get(i);
321         il_state_outtb->append(l_state_outtb_input->clone());
322     }
323     il_state->append(il_state_outtb);
324     int nlnk = il_state_outtb->getSize(); // 'nlnk' is the dimension of the list 'state.outtb'
325
326     /***************
327     * tcur (rhs 2)
328     ***************/
329     if (in[1]->isDouble() == false)
330     {
331         Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 2);
332         il_state->DecreaseRef();
333         il_state->killMe();
334         return types::Function::Error;
335     }
336     types::Double* il_tcur_input = in[1]->getAs<types::Double>();
337     types::Double* il_tcur = il_tcur_input->clone()->getAs<types::Double>();
338     if (il_tcur->isScalar() == false)
339     {
340         Scierror(999, _("%s: Wrong size for input argument #%d : A scalar expected.\n"), funname.data(), 2);
341         il_state->DecreaseRef();
342         il_state->killMe();
343         il_tcur->DecreaseRef();
344         il_tcur->killMe();
345         return types::Function::Error;
346     }
347     double* l_tcur = il_tcur->get();
348
349     /*************
350     * tf (rhs 3)
351     *************/
352     if (in[2]->isDouble() == false)
353     {
354         Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 3);
355         il_state->DecreaseRef();
356         il_state->killMe();
357         il_tcur->DecreaseRef();
358         il_tcur->killMe();
359         return types::Function::Error;
360     }
361     types::Double* il_tf = in[2]->getAs<types::Double>();
362     if (il_tf->isScalar() == false)
363     {
364         Scierror(999, _("%s: Wrong size for input argument #%d : A scalar expected.\n"), funname.data(), 3);
365         il_state->DecreaseRef();
366         il_state->killMe();
367         il_tcur->DecreaseRef();
368         il_tcur->killMe();
369         return types::Function::Error;
370     }
371     double* l_tf = il_tf->get();
372
373     /*************
374     * sim (rhs 4)
375     *************/
376     if (in[3]->isTList() == false)
377     {
378         Scierror(999, _("%s: Wrong type for input argument #%d : A tlist expected.\n"), funname.data(), 4);
379         il_state->DecreaseRef();
380         il_state->killMe();
381         il_tcur->DecreaseRef();
382         il_tcur->killMe();
383         return types::Function::Error;
384     }
385     types::TList* il_sim_input = in[3]->getAs<types::TList>();
386     types::TList* il_sim = new types::TList();
387
388     // Make a copy of 'il_sim' in a global variabe
389     set_il_sim(il_sim_input);
390
391     if (il_sim_input->getSize() < 34)
392     {
393         Scierror(999, _("%s: Wrong size for input argument #%d : %d elements expected.\n"), funname.data(), 4, 34);
394         il_state->DecreaseRef();
395         il_state->killMe();
396         il_tcur->DecreaseRef();
397         il_tcur->killMe();
398         il_sim->DecreaseRef();
399         il_sim->killMe();
400         return types::Function::Error;
401     }
402
403     types::String* header_sim = il_sim_input->get(0)->getAs<types::String>();
404     il_sim->append(header_sim->clone());
405
406     /*2  : sim.funs*/
407     if (il_sim_input->get(1)->isList() == false)
408     {
409         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A list expected.\n"), funname.data(), 2, 4);
410         il_state->DecreaseRef();
411         il_state->killMe();
412         il_tcur->DecreaseRef();
413         il_tcur->killMe();
414         il_sim->DecreaseRef();
415         il_sim->killMe();
416         return types::Function::Error;
417     }
418     types::List* il_sim_fun_input = il_sim_input->get(1)->getAs<types::List>();
419     types::List* il_sim_fun = new types::List();
420     for (int i = 0; i < il_sim_fun_input->getSize(); ++i)
421     {
422         types::InternalType* l_sim_fun_input = il_sim_fun_input->get(i);
423         il_sim_fun->append(l_sim_fun_input->clone());
424     }
425     il_sim->append(il_sim_fun);
426     int nblk = il_sim_fun->getSize(); // 'nblk' is the dimension of the list 'sim.funs'
427
428     /*3  : sim.xptr   */
429     if (il_sim_input->get(2)->isDouble() == false)
430     {
431         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 3, 4);
432         il_state->DecreaseRef();
433         il_state->killMe();
434         il_tcur->DecreaseRef();
435         il_tcur->killMe();
436         il_sim->DecreaseRef();
437         il_sim->killMe();
438         return types::Function::Error;
439     }
440     types::Double* il_sim_xptr_input = il_sim_input->get(2)->getAs<types::Double>();
441     il_sim->append(il_sim_xptr_input->clone());
442     types::Double* il_sim_xptr = il_sim->get(2)->getAs<types::Double>();
443     il_sim_xptr->convertToInteger();
444     int* l_sim_xptr = (int*) il_sim_xptr->get();
445     int m_xptr = il_sim_xptr->getRows();
446
447     /*4  : sim.zptr   */
448     if (il_sim_input->get(3)->isDouble() == false)
449     {
450         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 4, 4);
451         il_state->DecreaseRef();
452         il_state->killMe();
453         il_tcur->DecreaseRef();
454         il_tcur->killMe();
455         il_sim->DecreaseRef();
456         il_sim->killMe();
457         return types::Function::Error;
458     }
459     types::Double* il_sim_zptr_input = il_sim_input->get(3)->getAs<types::Double>();
460     il_sim->append(il_sim_zptr_input->clone());
461     types::Double* il_sim_zptr = il_sim->get(3)->getAs<types::Double>();
462     il_sim_zptr->convertToInteger();
463     int* l_sim_zptr = (int*) il_sim_zptr->get();
464     int m_zptr = il_sim_zptr->getRows();
465
466     /*5  : sim.ozptr   */
467     if (il_sim_input->get(4)->isDouble() == false)
468     {
469         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 5, 4);
470         il_state->DecreaseRef();
471         il_state->killMe();
472         il_tcur->DecreaseRef();
473         il_tcur->killMe();
474         il_sim->DecreaseRef();
475         il_sim->killMe();
476         return types::Function::Error;
477     }
478     types::Double* il_sim_ozptr_input = il_sim_input->get(4)->getAs<types::Double>();
479     il_sim->append(il_sim_ozptr_input->clone());
480     types::Double* il_sim_ozptr = il_sim->get(4)->getAs<types::Double>();
481     il_sim_ozptr->convertToInteger();
482     int* l_sim_ozptr = (int*) il_sim_ozptr->get();
483     int m_ozptr = il_sim_ozptr->getRows();
484
485     /*6  : sim.zcptr  */
486     if (il_sim_input->get(5)->isDouble() == false)
487     {
488         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 6, 4);
489         il_state->DecreaseRef();
490         il_state->killMe();
491         il_tcur->DecreaseRef();
492         il_tcur->killMe();
493         il_sim->DecreaseRef();
494         il_sim->killMe();
495         return types::Function::Error;
496     }
497     types::Double* il_sim_zcptr_input = il_sim_input->get(5)->getAs<types::Double>();
498     il_sim->append(il_sim_zcptr_input->clone());
499     types::Double* il_sim_zcptr = il_sim->get(5)->getAs<types::Double>();
500     il_sim_zcptr->convertToInteger();
501     int* l_sim_zcptr = (int*) il_sim_zcptr->get();
502     int m_zcptr = il_sim_zcptr->getRows();
503
504     /*7  : sim.inpptr */
505     if (il_sim_input->get(6)->isDouble() == false)
506     {
507         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 7, 4);
508         il_state->DecreaseRef();
509         il_state->killMe();
510         il_tcur->DecreaseRef();
511         il_tcur->killMe();
512         il_sim->DecreaseRef();
513         il_sim->killMe();
514         return types::Function::Error;
515     }
516     types::Double* il_sim_inpptr_input = il_sim_input->get(6)->getAs<types::Double>();
517     il_sim->append(il_sim_inpptr_input->clone());
518     types::Double* il_sim_inpptr = il_sim->get(6)->getAs<types::Double>();
519     il_sim_inpptr->convertToInteger();
520     int* l_sim_inpptr = (int*) il_sim_inpptr->get();
521     int m_inpptr = il_sim_inpptr->getRows();
522
523     /*8  : sim.outptr */
524     if (il_sim_input->get(7)->isDouble() == false)
525     {
526         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 8, 4);
527         il_state->DecreaseRef();
528         il_state->killMe();
529         il_tcur->DecreaseRef();
530         il_tcur->killMe();
531         il_sim->DecreaseRef();
532         il_sim->killMe();
533         return types::Function::Error;
534     }
535     types::Double* il_sim_outpptr_input = il_sim_input->get(7)->getAs<types::Double>();
536     il_sim->append(il_sim_outpptr_input->clone());
537     types::Double* il_sim_outptr = il_sim->get(7)->getAs<types::Double>();
538     il_sim_outptr->convertToInteger();
539     int* l_sim_outptr = (int*) il_sim_outptr->get();
540     int m_outptr = il_sim_outptr->getRows();
541
542     /*9  : sim.inplnk */
543     if (il_sim_input->get(8)->isDouble() == false)
544     {
545         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 9, 4);
546         il_state->DecreaseRef();
547         il_state->killMe();
548         il_tcur->DecreaseRef();
549         il_tcur->killMe();
550         il_sim->DecreaseRef();
551         il_sim->killMe();
552         return types::Function::Error;
553     }
554     types::Double* il_sim_inplnk_input = il_sim_input->get(8)->getAs<types::Double>();
555     il_sim->append(il_sim_inplnk_input->clone());
556     types::Double* il_sim_inplnk = il_sim->get(8)->getAs<types::Double>();
557     il_sim_inplnk->convertToInteger();
558     int* l_sim_inplnk = (int*) il_sim_inplnk->get();
559
560     /*10  : sim.outlnk */
561     if (il_sim_input->get(9)->isDouble() == false)
562     {
563         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 10, 4);
564         il_state->DecreaseRef();
565         il_state->killMe();
566         il_tcur->DecreaseRef();
567         il_tcur->killMe();
568         il_sim->DecreaseRef();
569         il_sim->killMe();
570         return types::Function::Error;
571     }
572     types::Double* il_sim_outlnk_input = il_sim_input->get(9)->getAs<types::Double>();
573     il_sim->append(il_sim_outlnk_input->clone());
574     types::Double* il_sim_outlnk = il_sim->get(9)->getAs<types::Double>();
575     il_sim_outlnk->convertToInteger();
576     int* l_sim_outlnk = (int*) il_sim_outlnk->get();
577
578     /*11 : sim.rpar   */
579     if (il_sim_input->get(10)->isDouble() == false)
580     {
581         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 11, 4);
582         il_state->DecreaseRef();
583         il_state->killMe();
584         il_tcur->DecreaseRef();
585         il_tcur->killMe();
586         il_sim->DecreaseRef();
587         il_sim->killMe();
588         return types::Function::Error;
589     }
590     types::Double* il_sim_rpar_input = il_sim_input->get(10)->getAs<types::Double>();
591     il_sim->append(il_sim_rpar_input->clone());
592     types::Double* il_sim_rpar = il_sim->get(10)->getAs<types::Double>();
593     double* l_sim_rpar = il_sim_rpar->get();
594
595     /*12 : sim.rpptr  */
596     if (il_sim_input->get(11)->isDouble() == false)
597     {
598         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 12, 4);
599         il_state->DecreaseRef();
600         il_state->killMe();
601         il_tcur->DecreaseRef();
602         il_tcur->killMe();
603         il_sim->DecreaseRef();
604         il_sim->killMe();
605         return types::Function::Error;
606     }
607     types::Double* il_sim_rpptr_input = il_sim_input->get(11)->getAs<types::Double>();
608     il_sim->append(il_sim_rpptr_input->clone());
609     types::Double* il_sim_rpptr = il_sim->get(11)->getAs<types::Double>();
610     il_sim_rpptr->convertToInteger();
611     int* l_sim_rpptr = (int*) il_sim_rpptr->get();
612     int m_rpptr = il_sim_rpptr->getRows();
613
614     /*13 : sim.ipar   */
615     if (il_sim_input->get(12)->isDouble() == false)
616     {
617         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 13, 4);
618         il_state->DecreaseRef();
619         il_state->killMe();
620         il_tcur->DecreaseRef();
621         il_tcur->killMe();
622         il_sim->DecreaseRef();
623         il_sim->killMe();
624         return types::Function::Error;
625     }
626     types::Double* il_sim_ipar_input = il_sim_input->get(12)->getAs<types::Double>();
627     il_sim->append(il_sim_ipar_input->clone());
628     types::Double* il_sim_ipar = il_sim->get(12)->getAs<types::Double>();
629     il_sim_ipar->convertToInteger();
630     int* l_sim_ipar = (int*) il_sim_ipar->get();
631
632     /*14 : sim.ipptr  */
633     if (il_sim_input->get(13)->isDouble() == false)
634     {
635         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 14, 4);
636         il_state->DecreaseRef();
637         il_state->killMe();
638         il_tcur->DecreaseRef();
639         il_tcur->killMe();
640         il_sim->DecreaseRef();
641         il_sim->killMe();
642         return types::Function::Error;
643     }
644     types::Double* il_sim_ipptr_input = il_sim_input->get(13)->getAs<types::Double>();
645     il_sim->append(il_sim_ipptr_input->clone());
646     types::Double* il_sim_ipptr = il_sim->get(13)->getAs<types::Double>();
647     il_sim_ipptr->convertToInteger();
648     int* l_sim_ipptr = (int*) il_sim_ipptr->get();
649     int m_ipptr = il_sim_ipptr->getRows();
650
651     /*15 : sim.opar   */
652     if (il_sim_input->get(14)->isList() == false)
653     {
654         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A list expected.\n"), funname.data(), 15, 4);
655         il_state->DecreaseRef();
656         il_state->killMe();
657         il_tcur->DecreaseRef();
658         il_tcur->killMe();
659         il_sim->DecreaseRef();
660         il_sim->killMe();
661         return types::Function::Error;
662     }
663     types::List* il_sim_opar_input = il_sim_input->get(14)->getAs<types::List>();
664     types::List* il_sim_opar = new types::List();
665     for (int i = 0; i < il_sim_opar_input->getSize(); ++i)
666     {
667         types::InternalType* l_sim_opar_input = il_sim_opar_input->get(i);
668         il_sim_opar->append(l_sim_opar_input->clone());
669     }
670     il_sim->append(il_sim_opar);
671     int nopar = il_sim_opar->getSize(); // 'nopar' is the dimension of the list 'sim.opar'
672
673     /*16 : sim.opptr  */
674     if (il_sim_input->get(15)->isDouble() == false)
675     {
676         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 16, 4);
677         il_state->DecreaseRef();
678         il_state->killMe();
679         il_tcur->DecreaseRef();
680         il_tcur->killMe();
681         il_sim->DecreaseRef();
682         il_sim->killMe();
683         return types::Function::Error;
684     }
685     types::Double* il_sim_opptr_input = il_sim_input->get(15)->getAs<types::Double>();
686     il_sim->append(il_sim_opptr_input->clone());
687     types::Double* il_sim_opptr = il_sim->get(15)->getAs<types::Double>();
688     il_sim_opptr->convertToInteger();
689     int* l_sim_opptr = (int*) il_sim_opptr->get();
690     int m_opptr = il_sim_opptr->getRows();
691
692     /*17 : sim.clkptr */
693     if (il_sim_input->get(16)->isDouble() == false)
694     {
695         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 17, 4);
696         il_state->DecreaseRef();
697         il_state->killMe();
698         il_tcur->DecreaseRef();
699         il_tcur->killMe();
700         il_sim->DecreaseRef();
701         il_sim->killMe();
702         return types::Function::Error;
703     }
704     types::Double* il_sim_clkptr_input = il_sim_input->get(16)->getAs<types::Double>();
705     il_sim->append(il_sim_clkptr_input->clone());
706     types::Double* il_sim_clkptr = il_sim->get(16)->getAs<types::Double>();
707     il_sim_clkptr->convertToInteger();
708     int* l_sim_clkptr = (int*) il_sim_clkptr->get();
709     int m_clkptr = il_sim_clkptr->getRows();
710
711     /*18 : sim.ordptr */
712     if (il_sim_input->get(17)->isDouble() == false)
713     {
714         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 18, 4);
715         il_state->DecreaseRef();
716         il_state->killMe();
717         il_tcur->DecreaseRef();
718         il_tcur->killMe();
719         il_sim->DecreaseRef();
720         il_sim->killMe();
721         return types::Function::Error;
722     }
723     types::Double* il_sim_ordptr_input = il_sim_input->get(17)->getAs<types::Double>();
724     il_sim->append(il_sim_ordptr_input->clone());
725     types::Double* il_sim_ordptr = il_sim->get(17)->getAs<types::Double>();
726     il_sim_ordptr->convertToInteger();
727     int* l_sim_ordptr = (int*) il_sim_ordptr->get();
728     int m_ordptr = il_sim_ordptr->getRows();
729
730     /*19 : sim.execlk */
731     if (il_sim_input->get(18)->isDouble() == false)
732     {
733         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 19, 4);
734         il_state->DecreaseRef();
735         il_state->killMe();
736         il_tcur->DecreaseRef();
737         il_tcur->killMe();
738         il_sim->DecreaseRef();
739         il_sim->killMe();
740         return types::Function::Error;
741     }
742     types::Double* il_sim_execlk_input = il_sim_input->get(18)->getAs<types::Double>();
743     il_sim->append(il_sim_execlk_input->clone());
744
745     /*20 : sim.ordclk */
746     if (il_sim_input->get(19)->isDouble() == false)
747     {
748         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 20, 4);
749         il_state->DecreaseRef();
750         il_state->killMe();
751         il_tcur->DecreaseRef();
752         il_tcur->killMe();
753         il_sim->DecreaseRef();
754         il_sim->killMe();
755         return types::Function::Error;
756     }
757     types::Double* il_sim_ordclk_input = il_sim_input->get(19)->getAs<types::Double>();
758     il_sim->append(il_sim_ordclk_input->clone());
759     types::Double* il_sim_ordclk = il_sim->get(19)->getAs<types::Double>();
760     il_sim_ordclk->convertToInteger();
761     int* l_sim_ordclk = (int*) il_sim_ordclk->get();
762     int m_ordclk = il_sim_ordclk->getRows();
763     int n_ordclk = il_sim_ordclk->getCols();
764
765     /*21 : sim.cord   */
766     if (il_sim_input->get(20)->isDouble() == false)
767     {
768         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 21, 4);
769         il_state->DecreaseRef();
770         il_state->killMe();
771         il_tcur->DecreaseRef();
772         il_tcur->killMe();
773         il_sim->DecreaseRef();
774         il_sim->killMe();
775         return types::Function::Error;
776     }
777     types::Double* il_sim_cord_input = il_sim_input->get(20)->getAs<types::Double>();
778     il_sim->append(il_sim_cord_input->clone());
779     types::Double* il_sim_cord = il_sim->get(20)->getAs<types::Double>();
780     il_sim_cord->convertToInteger();
781     int* l_sim_cord = (int*) il_sim_cord->get();
782     int m_cord = il_sim_cord->getRows();
783     int n_cord = il_sim_cord->getCols();
784
785     /*22 : sim.oord   */
786     if (il_sim_input->get(21)->isDouble() == false)
787     {
788         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 22, 4);
789         il_state->DecreaseRef();
790         il_state->killMe();
791         il_tcur->DecreaseRef();
792         il_tcur->killMe();
793         il_sim->DecreaseRef();
794         il_sim->killMe();
795         return types::Function::Error;
796     }
797     types::Double* il_sim_oord_input = il_sim_input->get(21)->getAs<types::Double>();
798     il_sim->append(il_sim_oord_input->clone());
799     types::Double* il_sim_oord = il_sim->get(21)->getAs<types::Double>();
800     il_sim_oord->convertToInteger();
801     int* l_sim_oord = (int*) il_sim_oord->get();
802     int m_oord = il_sim_oord->getRows();
803     int n_oord = il_sim_oord->getCols();
804
805     /*23 : sim.zord   */
806     if (il_sim_input->get(22)->isDouble() == false)
807     {
808         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 23, 4);
809         il_state->DecreaseRef();
810         il_state->killMe();
811         il_tcur->DecreaseRef();
812         il_tcur->killMe();
813         il_sim->DecreaseRef();
814         il_sim->killMe();
815         return types::Function::Error;
816     }
817     types::Double* il_sim_zord_input = il_sim_input->get(22)->getAs<types::Double>();
818     il_sim->append(il_sim_zord_input->clone());
819     types::Double* il_sim_zord = il_sim->get(22)->getAs<types::Double>();
820     il_sim_zord->convertToInteger();
821     int* l_sim_zord = (int*) il_sim_zord->get();
822     int m_zord = il_sim_zord->getRows();
823     int n_zord = il_sim_zord->getCols();
824
825     /*24 : sim.critev */
826     if (il_sim_input->get(23)->isDouble() == false)
827     {
828         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 24, 4);
829         il_state->DecreaseRef();
830         il_state->killMe();
831         il_tcur->DecreaseRef();
832         il_tcur->killMe();
833         il_sim->DecreaseRef();
834         il_sim->killMe();
835         return types::Function::Error;
836     }
837     types::Double* il_sim_critev_input = il_sim_input->get(23)->getAs<types::Double>();
838     il_sim->append(il_sim_critev_input->clone());
839     types::Double* il_sim_critev = il_sim->get(23)->getAs<types::Double>();
840     il_sim_critev->convertToInteger();
841     int* l_sim_critev = (int*) il_sim_critev->get();
842
843     /*25 : sim.nb     */
844     if (il_sim_input->get(24)->isDouble() == false)
845     {
846         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 25, 4);
847         il_state->DecreaseRef();
848         il_state->killMe();
849         il_tcur->DecreaseRef();
850         il_tcur->killMe();
851         il_sim->DecreaseRef();
852         il_sim->killMe();
853         return types::Function::Error;
854     }
855     types::Double* il_sim_nb_input = il_sim_input->get(24)->getAs<types::Double>();
856     il_sim->append(il_sim_nb_input->clone());
857     types::Double* il_sim_nb = il_sim->get(24)->getAs<types::Double>();
858     double* l_sim_nb = il_sim_nb->get();
859
860     if (static_cast<int>(l_sim_nb[0]) != nblk) // Value of 'nb' must be equal to 'nblk'
861     {
862         Scierror(42, _("%s : Incompatible sim.nb RHS parameter.\n"), funname.data());
863         il_state->DecreaseRef();
864         il_state->killMe();
865         il_tcur->DecreaseRef();
866         il_tcur->killMe();
867         il_sim->DecreaseRef();
868         il_sim->killMe();
869         return types::Function::Error;
870     }
871
872     /*26 : sim.ztyp   */
873     if (il_sim_input->get(25)->isDouble() == false)
874     {
875         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 26, 4);
876         il_state->DecreaseRef();
877         il_state->killMe();
878         il_tcur->DecreaseRef();
879         il_tcur->killMe();
880         il_sim->DecreaseRef();
881         il_sim->killMe();
882         return types::Function::Error;
883     }
884     types::Double* il_sim_ztyp_input = il_sim_input->get(25)->getAs<types::Double>();
885     il_sim->append(il_sim_ztyp_input->clone());
886     types::Double* il_sim_ztyp = il_sim->get(25)->getAs<types::Double>();
887     il_sim_ztyp->convertToInteger();
888     int* l_sim_ztyp = (int*) il_sim_ztyp->get();
889
890     /*27 : sim.nblk   */
891     if (il_sim_input->get(26)->isDouble() == false)
892     {
893         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 27, 4);
894         il_state->DecreaseRef();
895         il_state->killMe();
896         il_tcur->DecreaseRef();
897         il_tcur->killMe();
898         il_sim->DecreaseRef();
899         il_sim->killMe();
900         return types::Function::Error;
901     }
902     types::Double* il_sim_nblk_input = il_sim_input->get(26)->getAs<types::Double>();
903     il_sim->append(il_sim_nblk_input->clone());
904
905     /*28 : sim.ndcblk */
906     if (il_sim_input->get(27)->isDouble() == false)
907     {
908         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 28, 4);
909         il_state->DecreaseRef();
910         il_state->killMe();
911         il_tcur->DecreaseRef();
912         il_tcur->killMe();
913         il_sim->DecreaseRef();
914         il_sim->killMe();
915         return types::Function::Error;
916     }
917     types::Double* il_sim_ndcblk_input = il_sim_input->get(27)->getAs<types::Double>();
918     il_sim->append(il_sim_ndcblk_input->clone());
919
920     /*29 : sim.subscr */
921     if (il_sim_input->get(28)->isDouble() == false)
922     {
923         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 29, 4);
924         il_state->DecreaseRef();
925         il_state->killMe();
926         il_tcur->DecreaseRef();
927         il_tcur->killMe();
928         il_sim->DecreaseRef();
929         il_sim->killMe();
930         return types::Function::Error;
931     }
932     types::Double* il_sim_subscr_input = il_sim_input->get(28)->getAs<types::Double>();
933     il_sim->append(il_sim_subscr_input->clone());
934     types::Double* il_sim_subscr = il_sim->get(28)->getAs<types::Double>();
935     il_sim_subscr->convertToInteger();
936     int* l_sim_subscr = (int*) il_sim_subscr->get();
937     int m_subscr = il_sim_subscr->getRows();
938
939     /*30 : sim.funtyp */
940     if (il_sim_input->get(29)->isDouble() == false)
941     {
942         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 30, 4);
943         il_state->DecreaseRef();
944         il_state->killMe();
945         il_tcur->DecreaseRef();
946         il_tcur->killMe();
947         il_sim->DecreaseRef();
948         il_sim->killMe();
949         return types::Function::Error;
950     }
951     types::Double* il_sim_funtyp_input = il_sim_input->get(29)->getAs<types::Double>();
952     il_sim->append(il_sim_funtyp_input->clone());
953     types::Double* il_sim_funtyp = il_sim->get(29)->getAs<types::Double>();
954     il_sim_funtyp->convertToInteger();
955     int* l_sim_funtyp = (int*) il_sim_funtyp->get();
956
957     /*31 : sim.iord   */
958     if (il_sim_input->get(30)->isDouble() == false)
959     {
960         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 31, 4);
961         il_state->DecreaseRef();
962         il_state->killMe();
963         il_tcur->DecreaseRef();
964         il_tcur->killMe();
965         il_sim->DecreaseRef();
966         il_sim->killMe();
967         return types::Function::Error;
968     }
969     types::Double* il_sim_iord_input = il_sim_input->get(30)->getAs<types::Double>();
970     il_sim->append(il_sim_iord_input->clone());
971     types::Double* il_sim_iord = il_sim->get(30)->getAs<types::Double>();
972     il_sim_iord->convertToInteger();
973     int* l_sim_iord = (int*) il_sim_iord->get();
974     int m_iord = il_sim_iord->getRows();
975     int n_iord = il_sim_iord->getCols();
976
977     /*32 : sim.labels */
978     if (il_sim_input->get(31)->isString() == false)
979     {
980         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A string matrix expected.\n"), funname.data(), 32, 4);
981         il_state->DecreaseRef();
982         il_state->killMe();
983         il_tcur->DecreaseRef();
984         il_tcur->killMe();
985         il_sim->DecreaseRef();
986         il_sim->killMe();
987         return types::Function::Error;
988     }
989     types::String* il_sim_lab_input = il_sim_input->get(31)->getAs<types::String>();
990     il_sim->append(il_sim_lab_input->clone());
991     types::String* il_sim_lab = il_sim->get(31)->getAs<types::String>();
992     int* il_sim_labptr;
993     try
994     {
995         il_sim_labptr = new int[il_sim_lab->getSize()];
996     }
997     catch (const std::bad_alloc& /*e*/)
998     {
999         Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1000         il_state->DecreaseRef();
1001         il_state->killMe();
1002         il_tcur->DecreaseRef();
1003         il_tcur->killMe();
1004         il_sim->DecreaseRef();
1005         il_sim->killMe();
1006         return types::Function::Error;
1007     }
1008     char** l_sim_lab;
1009     try
1010     {
1011         l_sim_lab = new char*[il_sim_lab->getSize()];
1012     }
1013     catch (const std::bad_alloc& /*e*/)
1014     {
1015         Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1016         il_state->DecreaseRef();
1017         il_state->killMe();
1018         il_tcur->DecreaseRef();
1019         il_tcur->killMe();
1020         delete[] il_sim_labptr;
1021         il_sim->DecreaseRef();
1022         il_sim->killMe();
1023         return types::Function::Error;
1024     }
1025     for (int i = 0; i < il_sim_lab->getSize(); ++i)
1026     {
1027         l_sim_lab[i] = wide_string_to_UTF8(il_sim_lab->get(i));
1028         il_sim_labptr[i] = static_cast<int>(strlen(l_sim_lab[i]));
1029     }
1030
1031     /*33 : sim.modptr */
1032     if (il_sim_input->get(32)->isDouble() == false)
1033     {
1034         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 33, 4);
1035         il_state->DecreaseRef();
1036         il_state->killMe();
1037         il_tcur->DecreaseRef();
1038         il_tcur->killMe();
1039         delete[] il_sim_labptr;
1040         delete_strings(l_sim_lab, il_sim_lab->getSize());
1041         il_sim->DecreaseRef();
1042         il_sim->killMe();
1043         return types::Function::Error;
1044     }
1045     types::Double* il_sim_modptr_input = il_sim_input->get(32)->getAs<types::Double>();
1046     il_sim->append(il_sim_modptr_input->clone());
1047     types::Double* il_sim_modptr = il_sim->get(32)->getAs<types::Double>();
1048     il_sim_modptr->convertToInteger();
1049     int* l_sim_modptr = (int*) il_sim_modptr->get();
1050
1051     /*34 : sim.uids */
1052     if (il_sim_input->get(33)->isString() == false)
1053     {
1054         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A string matrix expected.\n"), funname.data(), 34, 4);
1055         il_state->DecreaseRef();
1056         il_state->killMe();
1057         il_tcur->DecreaseRef();
1058         il_tcur->killMe();
1059         delete[] il_sim_labptr;
1060         delete_strings(l_sim_lab, il_sim_lab->getSize());
1061         il_sim->DecreaseRef();
1062         il_sim->killMe();
1063         return types::Function::Error;
1064     }
1065     types::String* il_sim_uid_input = il_sim_input->get(33)->getAs<types::String>();
1066     il_sim->append(il_sim_uid_input->clone());
1067     types::String* il_sim_uid = il_sim->get(33)->getAs<types::String>();
1068     int* il_sim_uidptr;
1069     try
1070     {
1071         il_sim_uidptr = new int[il_sim_uid->getSize()];
1072     }
1073     catch (const std::bad_alloc& /*e*/)
1074     {
1075         Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1076         il_state->DecreaseRef();
1077         il_state->killMe();
1078         il_tcur->DecreaseRef();
1079         il_tcur->killMe();
1080         delete[] il_sim_labptr;
1081         delete_strings(l_sim_lab, il_sim_lab->getSize());
1082         il_sim->DecreaseRef();
1083         il_sim->killMe();
1084         return types::Function::Error;
1085     }
1086     char** l_sim_uid;
1087     try
1088     {
1089         l_sim_uid = new char*[il_sim_uid->getSize()];
1090     }
1091     catch (const std::bad_alloc& /*e*/)
1092     {
1093         Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1094         il_state->DecreaseRef();
1095         il_state->killMe();
1096         il_tcur->DecreaseRef();
1097         il_tcur->killMe();
1098         delete[] il_sim_labptr;
1099         delete_strings(l_sim_lab, il_sim_lab->getSize());
1100         delete[] il_sim_uidptr;
1101         il_sim->DecreaseRef();
1102         il_sim->killMe();
1103         return types::Function::Error;
1104     }
1105     for (int i = 0; i < il_sim_uid->getSize(); ++i)
1106     {
1107         l_sim_uid[i] = wide_string_to_UTF8(il_sim_uid->get(i));
1108         il_sim_uidptr[i] = static_cast<int>(strlen(l_sim_uid[i]));
1109     }
1110
1111     /*************
1112     * str (rhs 5)
1113     *************/
1114     if (in[4]->isString() == false)
1115     {
1116         Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 5);
1117         il_state->DecreaseRef();
1118         il_state->killMe();
1119         il_tcur->DecreaseRef();
1120         il_tcur->killMe();
1121         delete[] il_sim_labptr;
1122         delete_strings(l_sim_lab, il_sim_lab->getSize());
1123         delete[] il_sim_uidptr;
1124         delete_strings(l_sim_uid, il_sim_uid->getSize());
1125         il_sim->DecreaseRef();
1126         il_sim->killMe();
1127         return types::Function::Error;
1128     }
1129     types::String* il_str = in[4]->getAs<types::String>();
1130     if (il_str->isScalar() == false)
1131     {
1132         Scierror(999, _("%s: Wrong size for input argument #%d : A scalar expected.\n"), funname.data(), 5);
1133         il_state->DecreaseRef();
1134         il_state->killMe();
1135         il_tcur->DecreaseRef();
1136         il_tcur->killMe();
1137         delete[] il_sim_labptr;
1138         delete_strings(l_sim_lab, il_sim_lab->getSize());
1139         delete[] il_sim_uidptr;
1140         delete_strings(l_sim_uid, il_sim_uid->getSize());
1141         il_sim->DecreaseRef();
1142         il_sim->killMe();
1143         return types::Function::Error;
1144     }
1145
1146     const std::wstring start  (L"start");
1147     const std::wstring run    (L"run");
1148     const std::wstring finish (L"finish");
1149     const std::wstring linear (L"linear");
1150     const std::wstring Kinsol (L"Kinsol");
1151     int flag;
1152     if (il_str->get(0) == start)
1153     {
1154         flag = 1;
1155     }
1156     else if (il_str->get(0) == run)
1157     {
1158         flag = 2;
1159     }
1160     else if (il_str->get(0) == finish)
1161     {
1162         flag = 3;
1163     }
1164     else if (il_str->get(0) == linear)
1165     {
1166         flag = 4;
1167     }
1168     else if (il_str->get(0) == Kinsol)
1169     {
1170         flag = 5;
1171     }
1172     else
1173     {
1174         Scierror(44, _("%s: Wrong value for input argument #%d : ""%s"", ""%s"" ""%s"" ""%s"" or ""%s"" expected.\n"), funname.data(), 5,
1175                  "start", "run", "finish", "linear", "Kinsol");
1176         il_state->DecreaseRef();
1177         il_state->killMe();
1178         il_tcur->DecreaseRef();
1179         il_tcur->killMe();
1180         delete[] il_sim_labptr;
1181         delete_strings(l_sim_lab, il_sim_lab->getSize());
1182         delete[] il_sim_uidptr;
1183         delete_strings(l_sim_uid, il_sim_uid->getSize());
1184         il_sim->DecreaseRef();
1185         il_sim->killMe();
1186         return types::Function::Error;
1187     }
1188
1189     /*************
1190     * tol (rhs 6)
1191     *************/
1192     if (in[5]->isDouble() == false)
1193     {
1194         Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 6);
1195         il_state->DecreaseRef();
1196         il_state->killMe();
1197         il_tcur->DecreaseRef();
1198         il_tcur->killMe();
1199         delete[] il_sim_labptr;
1200         delete_strings(l_sim_lab, il_sim_lab->getSize());
1201         delete[] il_sim_uidptr;
1202         delete_strings(l_sim_uid, il_sim_uid->getSize());
1203         il_sim->DecreaseRef();
1204         il_sim->killMe();
1205         return types::Function::Error;
1206     }
1207     types::Double* il_tol = in[5]->getAs<types::Double>();
1208     int m6 = il_tol->getSize();
1209     if (m6 < 4 || m6 > 7) // Check if 'tol' has 4 to 7 elements
1210     {
1211         Scierror(999, _("%s: Wrong size for input argument #%d : %d to %d elements expected.\n"), funname.data(), 6, 4, 7);
1212         il_state->DecreaseRef();
1213         il_state->killMe();
1214         il_tcur->DecreaseRef();
1215         il_tcur->killMe();
1216         delete[] il_sim_labptr;
1217         delete_strings(l_sim_lab, il_sim_lab->getSize());
1218         delete[] il_sim_uidptr;
1219         delete_strings(l_sim_uid, il_sim_uid->getSize());
1220         il_sim->DecreaseRef();
1221         il_sim->killMe();
1222         return types::Function::Error;
1223     }
1224
1225     /******************
1226     * Set 'simpar' array
1227     ******************/
1228     for (int i = 0; i < m6; ++i)
1229     {
1230         simpar[i] = il_tol->get(i);
1231     }
1232     for (int i = m6; i < 7; ++i)
1233     {
1234         simpar[i] = 0;
1235     }
1236
1237     /******************************
1238     * Cross variable size checking
1239     ******************************/
1240     int err_check = 0;
1241     if (m1e6 != m1e7)
1242     {
1243         err_check = 1;    // tevts vs evtspt
1244     }
1245     else if (m_xptr != m_zptr)
1246     {
1247         err_check = 2;    // xptr vs zptr
1248     }
1249     else if (m_xptr != m_ozptr)
1250     {
1251         err_check = 3;    // xptr vs ozptr
1252     }
1253     else if (m_xptr != m_zcptr)
1254     {
1255         err_check = 4;    // xptr vs zcptr
1256     }
1257     else if (m_xptr != m_inpptr)
1258     {
1259         err_check = 5;    // xptr vs inpptr
1260     }
1261     else if (m_xptr != m_outptr)
1262     {
1263         err_check = 6;    // xptr vs outptr
1264     }
1265     else if (m_xptr != m_rpptr)
1266     {
1267         err_check = 7;    // xptr vs rpptr
1268     }
1269     else if (m_xptr != m_ipptr)
1270     {
1271         err_check = 8;    // xptr vs ipptr
1272     }
1273     else if (m_xptr != m_opptr)
1274     {
1275         err_check = 8;    // xptr vs opptr
1276     }
1277     else if (m_xptr != m_clkptr)
1278     {
1279         err_check = 10;    // xptr vs clkptr
1280     }
1281     else if ((n_ordclk != 2) & (m_ordclk != 0))
1282     {
1283         err_check = 11;    // sim.ordclk
1284     }
1285     else if ((n_cord != 2) & (m_cord != 0))
1286     {
1287         err_check = 12;    // sim.cord
1288     }
1289     else if ((n_oord != 2) & (m_oord != 0))
1290     {
1291         err_check = 13;    // sim.oord
1292     }
1293     else if ((n_zord != 2) & (m_zord != 0))
1294     {
1295         err_check = 14;    // sim.zord
1296     }
1297     else if ((n_iord != 2) & (m_iord != 0))
1298     {
1299         err_check = 15;    // sim.iord
1300     }
1301     if (err_check != 0)
1302     {
1303         // Please write an error table here
1304         Scierror(42, _("%s : error in cross variable size checking : %d\n"), funname.data(), err_check);
1305         il_state->DecreaseRef();
1306         il_state->killMe();
1307         il_tcur->DecreaseRef();
1308         il_tcur->killMe();
1309         delete[] il_sim_labptr;
1310         delete_strings(l_sim_lab, il_sim_lab->getSize());
1311         delete[] il_sim_uidptr;
1312         delete_strings(l_sim_uid, il_sim_uid->getSize());
1313         il_sim->DecreaseRef();
1314         il_sim->killMe();
1315         return types::Function::Error;
1316     }
1317
1318     /*******************************
1319     * Set function table for blocks
1320     *******************************/
1321     // Define new variable 'lfunpt'
1322     void** lfunpt;
1323     try
1324     {
1325         lfunpt = new void*[nblk];
1326     }
1327     catch (const std::bad_alloc& /*e*/)
1328     {
1329         Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1330         il_state->DecreaseRef();
1331         il_state->killMe();
1332         il_tcur->DecreaseRef();
1333         il_tcur->killMe();
1334         delete[] il_sim_labptr;
1335         delete_strings(l_sim_lab, il_sim_lab->getSize());
1336         delete[] il_sim_uidptr;
1337         delete_strings(l_sim_uid, il_sim_uid->getSize());
1338         il_sim->DecreaseRef();
1339         il_sim->killMe();
1340         return types::Function::Error;
1341     }
1342
1343     // For each block
1344     for (int i = 0; i < nblk; ++i) // For each block
1345     {
1346         types::InternalType* pIT = il_sim_fun->get(i);
1347         // Block is defined by a Scilab function
1348         if (pIT->isCallable())
1349         {
1350             lfunpt[i] = (void*)pIT;
1351             // Keep 'l_sim_funtyp' negative for Scilab macros
1352             if (l_sim_funtyp[i] > 0)
1353             {
1354                 l_sim_funtyp[i] *= -1;
1355             }
1356         }
1357         // Block is defined by a function described by a string
1358         else if (pIT->isString())
1359         {
1360             types::String* funStr = pIT->getAs<types::String>();
1361             if (funStr->isScalar() == false)
1362             {
1363                 Scierror(999, _("%s: Wrong size for element #%d of input argument #%d : A scalar expected.\n"), funname.data(), i + 1, 4);
1364                 il_state->DecreaseRef();
1365                 il_state->killMe();
1366                 il_tcur->DecreaseRef();
1367                 il_tcur->killMe();
1368                 delete[] il_sim_labptr;
1369                 delete_strings(l_sim_lab, il_sim_lab->getSize());
1370                 delete[] il_sim_uidptr;
1371                 delete_strings(l_sim_uid, il_sim_uid->getSize());
1372                 delete[] lfunpt;
1373                 il_sim->DecreaseRef();
1374                 il_sim->killMe();
1375                 return types::Function::Error;
1376             }
1377
1378             wchar_t* w_str = funStr->get(0);
1379             char* c_str = wide_string_to_UTF8(w_str);
1380             if (strcmp(c_str, "ifthel") == 0)
1381             {
1382                 l_sim_funtyp[i] = 11; // Magic value for "if-then-else" block
1383             }
1384             else if (strcmp(c_str, "eselect") == 0)
1385             {
1386                 l_sim_funtyp[i] = 12; // Magic value for "eselect" block
1387             }
1388             else
1389             {
1390                 void *f = funnum2(c_str); // Search associated function number of function name
1391                 // Block is defined by a C or Fortran function
1392                 if (f != nullptr)
1393                 {
1394                     // C interface from "tabsim" defined in blocks.h
1395                     lfunpt[i] = f;
1396                     if (l_sim_funtyp[i] < 0)
1397                     {
1398                         // Keep 'l_sim_funtyp' positive for Fortran functions
1399                         l_sim_funtyp[i] *= -1;
1400                     }
1401                 }
1402                 // Block is defined by a predefined scilab function
1403                 else
1404                 {
1405                     ConfigVariable::EntryPointStr* pEP = ConfigVariable::getEntryPoint(w_str);
1406                     if (pEP)
1407                     {
1408                         //linked functions
1409                         lfunpt[i] = (void*)pEP->functionPtr;
1410                     }
1411                     else
1412                     {
1413                         types::InternalType* pMacro = symbol::Context::getInstance()->get(symbol::Symbol(w_str));
1414                         if (pMacro && pMacro->isCallable())
1415                         {
1416                             //macros
1417                             lfunpt[i] = (void*)pMacro;
1418                             l_sim_funtyp[i] *= -1;
1419                         }
1420                         else
1421                         {
1422                             Scierror(888, _("%s : unknown block : %s\n"), funname.data(), c_str);
1423                             il_state->DecreaseRef();
1424                             il_state->killMe();
1425                             il_tcur->DecreaseRef();
1426                             il_tcur->killMe();
1427                             delete[] il_sim_labptr;
1428                             delete_strings(l_sim_lab, il_sim_lab->getSize());
1429                             delete[] il_sim_uidptr;
1430                             delete_strings(l_sim_uid, il_sim_uid->getSize());
1431                             delete[] lfunpt;
1432                             FREE(c_str);
1433                             il_sim->DecreaseRef();
1434                             il_sim->killMe();
1435                             return types::Function::Error;
1436                         }
1437                     }
1438                 }
1439             }
1440             FREE(c_str);
1441         }
1442         else
1443         {
1444             Scierror(999, _("%s: Wrong type for element #%d of input argument #%d : string or macro expected.\n"), funname.data(), i + 1, 4);
1445             il_state->DecreaseRef();
1446             il_state->killMe();
1447             il_tcur->DecreaseRef();
1448             il_tcur->killMe();
1449             delete[] il_sim_labptr;
1450             delete_strings(l_sim_lab, il_sim_lab->getSize());
1451             delete[] il_sim_uidptr;
1452             delete_strings(l_sim_uid, il_sim_uid->getSize());
1453             delete[] lfunpt;
1454             il_sim->DecreaseRef();
1455             il_sim->killMe();
1456             return types::Function::Error;
1457         }
1458     }
1459
1460     /**********************
1461     * Set oz, ozsz, oztyp
1462     **********************/
1463     void** oz = nullptr;
1464     int* ozsz = nullptr;
1465     int* oztyp = nullptr;
1466     if (noz > 0)
1467     {
1468         // Allocation of 'oz'
1469         try
1470         {
1471             oz = new void*[noz];
1472         }
1473         catch (const std::bad_alloc& /*e*/)
1474         {
1475             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1476             il_state->DecreaseRef();
1477             il_state->killMe();
1478             il_tcur->DecreaseRef();
1479             il_tcur->killMe();
1480             delete[] il_sim_labptr;
1481             delete_strings(l_sim_lab, il_sim_lab->getSize());
1482             delete[] il_sim_uidptr;
1483             delete_strings(l_sim_uid, il_sim_uid->getSize());
1484             delete[] lfunpt;
1485             il_sim->DecreaseRef();
1486             il_sim->killMe();
1487             return types::Function::Error;
1488         }
1489         // Allocation of 'ozsz'
1490         try
1491         {
1492             ozsz = new int[2 * noz];
1493         }
1494         catch (const std::bad_alloc& /*e*/)
1495         {
1496             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1497             il_state->DecreaseRef();
1498             il_state->killMe();
1499             il_tcur->DecreaseRef();
1500             il_tcur->killMe();
1501             delete[] il_sim_labptr;
1502             delete_strings(l_sim_lab, il_sim_lab->getSize());
1503             delete[] il_sim_uidptr;
1504             delete_strings(l_sim_uid, il_sim_uid->getSize());
1505             delete[] lfunpt;
1506             delete[] oz;
1507             il_sim->DecreaseRef();
1508             il_sim->killMe();
1509             return types::Function::Error;
1510         }
1511         // Allocation of 'oztyp'
1512         try
1513         {
1514             oztyp = new int[noz];
1515         }
1516         catch (const std::bad_alloc& /*e*/)
1517         {
1518             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1519             il_state->DecreaseRef();
1520             il_state->killMe();
1521             il_tcur->DecreaseRef();
1522             il_tcur->killMe();
1523             delete[] il_sim_labptr;
1524             delete_strings(l_sim_lab, il_sim_lab->getSize());
1525             delete[] il_sim_uidptr;
1526             delete_strings(l_sim_uid, il_sim_uid->getSize());
1527             delete[] lfunpt;
1528             delete[] oz;
1529             delete[] ozsz;
1530             il_sim->DecreaseRef();
1531             il_sim->killMe();
1532             return types::Function::Error;
1533         }
1534
1535         // Set vectors of 'oz'
1536         for (int j = 0; j < noz; ++j)
1537         {
1538             int subtype = il_state_oz->get(j)->getType();
1539
1540             switch (subtype) // Store type and address
1541             {
1542                 case types::InternalType::ScilabDouble :
1543                 {
1544                     types::Double* ozDouble = il_state_oz->get(j)->getAs<types::Double>();
1545                     if (ozDouble->isComplex() == false)
1546                     {
1547                         oztyp[j] = SCSREAL_N; // Double real matrix
1548                         oz[j] = (SCSREAL_COP *) ozDouble->get();
1549                     }
1550                     else
1551                     {
1552                         oztyp[j] = SCSCOMPLEX_N; // Double complex matrix
1553                         oz[j] = (SCSCOMPLEX_COP *) ozDouble->get();
1554                     }
1555                     ozsz[j] = ozDouble->getRows();
1556                     ozsz[j + noz] = ozDouble->getCols();
1557                     break;
1558                 }
1559                 case types::InternalType::ScilabInt8 :
1560                 {
1561                     types::Int8* ozInt8 = il_state_oz->get(j)->getAs<types::Int8>();
1562                     oztyp[j] = SCSINT8_N; // int8
1563                     ozsz[j] = ozInt8->getRows();
1564                     ozsz[j + noz] = ozInt8->getCols();
1565                     oz[j] = (SCSINT8_COP *) ozInt8->get();
1566                     break;
1567                 }
1568                 case types::InternalType::ScilabInt16 :
1569                 {
1570                     types::Int16* ozInt16 = il_state_oz->get(j)->getAs<types::Int16>();
1571                     oztyp[j] = SCSINT16_N; // int16
1572                     ozsz[j] = ozInt16->getRows();
1573                     ozsz[j + noz] = ozInt16->getCols();
1574                     oz[j] = (SCSINT16_COP *) ozInt16->get();
1575                     break;
1576                 }
1577                 case types::InternalType::ScilabInt32 :
1578                 {
1579                     types::Int32* ozInt32 = il_state_oz->get(j)->getAs<types::Int32>();
1580                     oztyp[j] = SCSINT32_N; // int32
1581                     ozsz[j] = ozInt32->getRows();
1582                     ozsz[j + noz] = ozInt32->getCols();
1583                     oz[j] = (SCSINT32_COP *) ozInt32->get();
1584                     break;
1585                 }
1586                 case types::InternalType::ScilabUInt8 :
1587                 {
1588                     types::UInt8* ozUInt8 = il_state_oz->get(j)->getAs<types::UInt8>();
1589                     oztyp[j] = SCSUINT8_N; // uint8
1590                     ozsz[j] = ozUInt8->getRows();
1591                     ozsz[j + noz] = ozUInt8->getCols();
1592                     oz[j] = (SCSUINT8_COP *) ozUInt8->get();
1593                     break;
1594                 }
1595                 case types::InternalType::ScilabUInt16 :
1596                 {
1597                     types::UInt16* ozUInt16 = il_state_oz->get(j)->getAs<types::UInt16>();
1598                     oztyp[j] = SCSUINT16_N; // uint16
1599                     ozsz[j] = ozUInt16->getRows();
1600                     ozsz[j + noz] = ozUInt16->getCols();
1601                     oz[j] = (SCSUINT16_COP *) ozUInt16->get();
1602                     break;
1603                 }
1604                 case types::InternalType::ScilabUInt32 :
1605                 {
1606                     types::UInt32* ozUInt32 = il_state_oz->get(j)->getAs<types::UInt32>();
1607                     oztyp[j] = SCSUINT32_N; // uint32
1608                     ozsz[j] = ozUInt32->getRows();
1609                     ozsz[j + noz] = ozUInt32->getCols();
1610                     oz[j] = (SCSUINT32_COP *) ozUInt32->get();
1611                     break;
1612                 }
1613                 default :
1614                 {
1615                     oztyp[j] = SCSUNKNOW_N;
1616                     oz[j] = il_sim_opar->get(j);
1617                     ozsz[j] = 0; // rows
1618                     ozsz[j + nopar] = 0; // cols
1619                     break;
1620                 }
1621             }
1622         }
1623     }
1624
1625     /****************************
1626     * Set opar, oparsz, opartyp
1627     ****************************/
1628     void** opar = nullptr;
1629     int* oparsz = nullptr;
1630     int* opartyp = nullptr;
1631     if (nopar > 0)
1632     {
1633         // Allocation of 'opar'
1634         try
1635         {
1636             opar = new void*[nopar];
1637         }
1638         catch (const std::bad_alloc& /*e*/)
1639         {
1640             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1641             il_state->DecreaseRef();
1642             il_state->killMe();
1643             il_tcur->DecreaseRef();
1644             il_tcur->killMe();
1645             delete[] il_sim_labptr;
1646             delete_strings(l_sim_lab, il_sim_lab->getSize());
1647             delete[] il_sim_uidptr;
1648             delete_strings(l_sim_uid, il_sim_uid->getSize());
1649             delete[] lfunpt;
1650             delete[] oz;
1651             delete[] ozsz;
1652             delete[] oztyp;
1653             il_sim->DecreaseRef();
1654             il_sim->killMe();
1655             return types::Function::Error;
1656         }
1657         // Allocation of 'oparsz'
1658         try
1659         {
1660             oparsz = new int[2 * nopar];
1661         }
1662         catch (const std::bad_alloc& /*e*/)
1663         {
1664             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1665             il_state->DecreaseRef();
1666             il_state->killMe();
1667             il_tcur->DecreaseRef();
1668             il_tcur->killMe();
1669             delete[] il_sim_labptr;
1670             delete_strings(l_sim_lab, il_sim_lab->getSize());
1671             delete[] il_sim_uidptr;
1672             delete_strings(l_sim_uid, il_sim_uid->getSize());
1673             delete[] lfunpt;
1674             delete[] oz;
1675             delete[] ozsz;
1676             delete[] oztyp;
1677             delete[] opar;
1678             il_sim->DecreaseRef();
1679             il_sim->killMe();
1680             return types::Function::Error;
1681         }
1682         // Allocation of 'opartyp'
1683         try
1684         {
1685             opartyp = new int[nopar];
1686         }
1687         catch (const std::bad_alloc& /*e*/)
1688         {
1689             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1690             il_state->DecreaseRef();
1691             il_state->killMe();
1692             il_tcur->DecreaseRef();
1693             il_tcur->killMe();
1694             delete[] il_sim_labptr;
1695             delete_strings(l_sim_lab, il_sim_lab->getSize());
1696             delete[] il_sim_uidptr;
1697             delete_strings(l_sim_uid, il_sim_uid->getSize());
1698             delete[] lfunpt;
1699             delete[] oz;
1700             delete[] ozsz;
1701             delete[] oztyp;
1702             delete[] opar;
1703             delete[] oparsz;
1704             il_sim->DecreaseRef();
1705             il_sim->killMe();
1706             return types::Function::Error;
1707         }
1708
1709         // Set vectors of 'opar'
1710         for (int j = 0; j < nopar; ++j)
1711         {
1712             int subtype = il_sim_opar->get(j)->getType();
1713
1714             switch (subtype) // Store type and address
1715             {
1716                 case types::InternalType::ScilabDouble :
1717                 {
1718                     types::Double* oparDouble = il_sim_opar->get(j)->getAs<types::Double>();
1719                     if (oparDouble->isComplex() == false)
1720                     {
1721                         opartyp[j] = SCSREAL_N; // Double real matrix
1722                         opar[j] = (SCSREAL_COP *) oparDouble->get();
1723                     }
1724                     else
1725                     {
1726                         opartyp[j] = SCSCOMPLEX_N; // Double complex matrix
1727                         // Allocate a long array in order to make the real and complex parts contiguous (oparDouble->get() and oparDouble->getImg())
1728                         try
1729                         {
1730                             opar[j] = new double[2 * oparDouble->getSize()];
1731                         }
1732                         catch (const std::bad_alloc& /*e*/)
1733                         {
1734                             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1735                             il_state->DecreaseRef();
1736                             il_state->killMe();
1737                             il_tcur->DecreaseRef();
1738                             il_tcur->killMe();
1739                             delete[] il_sim_labptr;
1740                             delete_strings(l_sim_lab, il_sim_lab->getSize());
1741                             delete[] il_sim_uidptr;
1742                             delete_strings(l_sim_uid, il_sim_uid->getSize());
1743                             delete[] lfunpt;
1744                             delete[] oz;
1745                             delete[] ozsz;
1746                             delete[] oztyp;
1747                             delete[] opar;
1748                             delete[] oparsz;
1749                             delete[] opartyp;
1750                             il_sim->DecreaseRef();
1751                             il_sim->killMe();
1752                             return types::Function::Error;
1753                         }
1754                         memcpy(opar[j], oparDouble->get(), oparDouble->getSize() * sizeof(double)); // Real part
1755                         memcpy((double*)(opar[j]) + oparDouble->getSize(), oparDouble->getImg(), oparDouble->getSize() * sizeof(double)); // Complex part
1756                         // FIXME: delete oparDouble because we copied it instead of using it?
1757                     }
1758                     oparsz[j] = oparDouble->getRows();
1759                     oparsz[j + nopar] = oparDouble->getCols();
1760                     break;
1761                 }
1762                 case types::InternalType::ScilabInt8 :
1763                 {
1764                     types::Int8* oparInt8 = il_sim_opar->get(j)->getAs<types::Int8>();
1765                     opartyp[j] = SCSINT8_N; // int8
1766                     oparsz[j] = oparInt8->getRows();
1767                     oparsz[j + nopar] = oparInt8->getCols();
1768                     opar[j] = (SCSINT8_COP *) oparInt8->get();
1769                     break;
1770                 }
1771                 case types::InternalType::ScilabInt16:
1772                 {
1773                     types::Int16 *oparInt16 =
1774                         il_sim_opar->get(j)->getAs<types::Int16>();
1775                     opartyp[j] = SCSINT16_N; // int16
1776                     oparsz[j] = oparInt16->getRows();
1777                     oparsz[j + nopar] = oparInt16->getCols();
1778                     opar[j] = (SCSINT16_COP *)oparInt16->get();
1779                     break;
1780                 }
1781                 case types::InternalType::ScilabInt32 :
1782                 {
1783                     types::Int32* oparInt32 = il_sim_opar->get(j)->getAs<types::Int32>();
1784                     opartyp[j] = SCSINT32_N; // int32
1785                     oparsz[j] = oparInt32->getRows();
1786                     oparsz[j + nopar] = oparInt32->getCols();
1787                     opar[j] = (SCSINT32_COP *) oparInt32->get();
1788                     break;
1789                 }
1790                 case types::InternalType::ScilabUInt8 :
1791                 {
1792                     types::UInt8* oparUInt8 = il_sim_opar->get(j)->getAs<types::UInt8>();
1793                     opartyp[j] = SCSUINT8_N; // uint8
1794                     oparsz[j] = oparUInt8->getRows();
1795                     oparsz[j + nopar] = oparUInt8->getCols();
1796                     opar[j] = (SCSUINT8_COP *) oparUInt8->get();
1797                     break;
1798                 }
1799                 case types::InternalType::ScilabUInt16 :
1800                 {
1801                     types::UInt16* oparUInt16 = il_sim_opar->get(j)->getAs<types::UInt16>();
1802                     opartyp[j] = SCSUINT16_N; // uint16
1803                     oparsz[j] = oparUInt16->getRows();
1804                     oparsz[j + nopar] = oparUInt16->getCols();
1805                     opar[j] = (SCSUINT16_COP *)oparUInt16->get();
1806                     break;
1807                 }
1808                 case types::InternalType::ScilabUInt32 :
1809                 {
1810                     types::UInt32* oparUInt32 = il_sim_opar->get(j)->getAs<types::UInt32>();
1811                     opartyp[j] = SCSUINT32_N; // uint32
1812                     oparsz[j] = oparUInt32->getRows();
1813                     oparsz[j + nopar] = oparUInt32->getCols();
1814                     opar[j] = (SCSUINT32_COP *) oparUInt32->get();
1815                     break;
1816                 }
1817                 default:
1818                 {
1819                     opartyp[j] = SCSUNKNOW_N;
1820                     opar[j] = il_sim_opar->get(j);
1821                     oparsz[j] = 0; // rows
1822                     oparsz[j + nopar] = 0; // cols
1823                     break;
1824                 }
1825             }
1826         }
1827     }
1828
1829     // Declaration of 'outtb_elem'
1830     outtb_el* outtb_elem = nullptr;
1831     int nelem = 0;
1832
1833     /*******************************
1834     * Set outtbptr, outtbsz, outtbtyp
1835     *******************************/
1836     void** outtbptr = nullptr;
1837     int* outtbsz = nullptr;
1838     int* outtbtyp = nullptr;
1839     if (nlnk > 0)
1840     {
1841         // Allocation of 'outtbptr'
1842         try
1843         {
1844             outtbptr = new void*[nlnk];
1845         }
1846         catch (const std::bad_alloc& /*e*/)
1847         {
1848             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1849             il_state->DecreaseRef();
1850             il_state->killMe();
1851             il_tcur->DecreaseRef();
1852             il_tcur->killMe();
1853             delete[] il_sim_labptr;
1854             delete_strings(l_sim_lab, il_sim_lab->getSize());
1855             delete[] il_sim_uidptr;
1856             delete_strings(l_sim_uid, il_sim_uid->getSize());
1857             delete[] lfunpt;
1858             delete[] oz;
1859             delete[] ozsz;
1860             delete[] oztyp;
1861             delete[] opar;
1862             delete[] oparsz;
1863             delete[] opartyp;
1864             il_sim->DecreaseRef();
1865             il_sim->killMe();
1866             return types::Function::Error;
1867         }
1868         // Allocation of 'outtbptrsz'
1869         try
1870         {
1871             outtbsz = new int[2 * nlnk];
1872         }
1873         catch (const std::bad_alloc& /*e*/)
1874         {
1875             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1876             il_state->DecreaseRef();
1877             il_state->killMe();
1878             il_tcur->DecreaseRef();
1879             il_tcur->killMe();
1880             delete[] il_sim_labptr;
1881             delete_strings(l_sim_lab, il_sim_lab->getSize());
1882             delete[] il_sim_uidptr;
1883             delete_strings(l_sim_uid, il_sim_uid->getSize());
1884             delete[] lfunpt;
1885             delete[] oz;
1886             delete[] ozsz;
1887             delete[] oztyp;
1888             delete[] opar;
1889             delete[] oparsz;
1890             delete[] opartyp;
1891             delete[] outtbptr;
1892             il_sim->DecreaseRef();
1893             il_sim->killMe();
1894             return types::Function::Error;
1895         }
1896         // Allocation of 'outtbtyp'
1897         try
1898         {
1899             outtbtyp = new int[nlnk];
1900         }
1901         catch (const std::bad_alloc& /*e*/)
1902         {
1903             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1904             il_state->DecreaseRef();
1905             il_state->killMe();
1906             il_tcur->DecreaseRef();
1907             il_tcur->killMe();
1908             delete[] il_sim_labptr;
1909             delete_strings(l_sim_lab, il_sim_lab->getSize());
1910             delete[] il_sim_uidptr;
1911             delete_strings(l_sim_uid, il_sim_uid->getSize());
1912             delete[] lfunpt;
1913             delete[] oz;
1914             delete[] ozsz;
1915             delete[] oztyp;
1916             delete[] opar;
1917             delete[] oparsz;
1918             delete[] opartyp;
1919             delete[] outtbptr;
1920             delete[] outtbsz;
1921             il_sim->DecreaseRef();
1922             il_sim->killMe();
1923             return types::Function::Error;
1924         }
1925
1926         // Set vectors of 'outtbptr'
1927         for (int j = 0; j < nlnk; ++j)
1928         {
1929             int subtype = il_state_outtb->get(j)->getType();
1930
1931             switch (subtype) // Store type and address
1932             {
1933                 case types::InternalType::ScilabDouble :
1934                 {
1935                     types::Double* outtbDouble = il_state_outtb->get(j)->getAs<types::Double>();
1936                     if (outtbDouble->isComplex() == false)
1937                     {
1938                         outtbtyp[j] = SCSREAL_N; // Double real matrix
1939                         outtbptr[j] = (SCSREAL_COP *) outtbDouble->get();
1940                     }
1941                     else
1942                     {
1943                         outtbtyp[j] = SCSCOMPLEX_N; // Double complex matrix
1944                         // Allocate a long array in order to make the real and complex parts contiguous (outtbDouble->get() and outtbDouble->getImg())
1945                         try
1946                         {
1947                             outtbptr[j] = new double[2 * outtbDouble->getSize()];
1948                         }
1949                         catch (const std::bad_alloc& /*e*/)
1950                         {
1951                             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1952                             il_state->DecreaseRef();
1953                             il_state->killMe();
1954                             il_tcur->DecreaseRef();
1955                             il_tcur->killMe();
1956                             delete[] il_sim_labptr;
1957                             delete_strings(l_sim_lab, il_sim_lab->getSize());
1958                             delete[] il_sim_uidptr;
1959                             delete_strings(l_sim_uid, il_sim_uid->getSize());
1960                             delete[] lfunpt;
1961                             delete[] oz;
1962                             delete[] ozsz;
1963                             delete[] oztyp;
1964                             delete[] opar;
1965                             delete[] oparsz;
1966                             delete[] opartyp;
1967                             delete[] outtbptr;
1968                             delete[] outtbsz;
1969                             il_sim->DecreaseRef();
1970                             il_sim->killMe();
1971                             return types::Function::Error;
1972                         }
1973                         memcpy(outtbptr[j], outtbDouble->get(), outtbDouble->getSize() * sizeof(double)); // Real part
1974                         memcpy((double*)(outtbptr[j]) + outtbDouble->getSize(), outtbDouble->getImg(), outtbDouble->getSize() * sizeof(double)); // Complex part
1975                         // FIXME: delete outtbDouble because we copied it instead of using it?
1976                     }
1977                     outtbsz[j] = outtbDouble->getRows();
1978                     outtbsz[j + nlnk] = outtbDouble->getCols();
1979                     break;
1980                 }
1981                 case types::InternalType::ScilabInt8 :
1982                 {
1983                     types::Int8* outtbInt8 = il_state_outtb->get(j)->getAs<types::Int8>();
1984                     outtbtyp[j] = SCSINT8_N; // int8
1985                     outtbsz[j] = outtbInt8->getRows();
1986                     outtbsz[j + nlnk] = outtbInt8->getCols();
1987                     outtbptr[j] = (SCSINT8_COP *) outtbInt8->get();
1988                     break;
1989                 }
1990                 case types::InternalType::ScilabInt16 :
1991                 {
1992                     types::Int16* outtbInt16 = il_state_outtb->get(j)->getAs<types::Int16>();
1993                     outtbtyp[j] = SCSINT16_N; // int16
1994                     outtbsz[j] = outtbInt16->getRows();
1995                     outtbsz[j + nlnk] = outtbInt16->getCols();
1996                     outtbptr[j] = (SCSINT16_COP *) outtbInt16->get();
1997                     break;
1998                 }
1999                 case types::InternalType::ScilabInt32 :
2000                 {
2001                     types::Int32* outtbInt32 = il_state_outtb->get(j)->getAs<types::Int32>();
2002                     outtbtyp[j] = SCSINT32_N; // int32
2003                     outtbsz[j] = outtbInt32->getRows();
2004                     outtbsz[j + nlnk] = outtbInt32->getCols();
2005                     outtbptr[j] = (SCSINT32_COP *) outtbInt32->get();
2006                     break;
2007                 }
2008                 case types::InternalType::ScilabUInt8 :
2009                 {
2010                     types::UInt8* outtbUInt8 = il_state_outtb->get(j)->getAs<types::UInt8>();
2011                     outtbtyp[j] = SCSUINT8_N; // uint8
2012                     outtbsz[j] = outtbUInt8->getRows();
2013                     outtbsz[j + nlnk] = outtbUInt8->getCols();
2014                     outtbptr[j] = (SCSUINT8_COP *) outtbUInt8->get();
2015                     break;
2016                 }
2017                 case types::InternalType::ScilabUInt16 :
2018                 {
2019                     types::UInt16* outtbUInt16 = il_state_outtb->get(j)->getAs<types::UInt16>();
2020                     outtbtyp[j] = SCSUINT16_N; // uint16
2021                     outtbsz[j] = outtbUInt16->getRows();
2022                     outtbsz[j + nlnk] = outtbUInt16->getCols();
2023                     outtbptr[j] = (SCSUINT16_COP *) outtbUInt16->get();
2024                     break;
2025                 }
2026                 case types::InternalType::ScilabUInt32 :
2027                 {
2028                     types::UInt32* outtbUInt32 = il_state_outtb->get(j)->getAs<types::UInt32>();
2029                     outtbtyp[j] = SCSUINT32_N; // uint32
2030                     outtbsz[j] = outtbUInt32->getRows();
2031                     outtbsz[j + nlnk] = outtbUInt32->getCols();
2032                     outtbptr[j] = (SCSUINT32_COP *) outtbUInt32->get();
2033                     break;
2034                 }
2035                 default :
2036                 {
2037                     Scierror(999, _("%s: Wrong type for element #%d of element #%d of argument #%d : A matrix expected.\n"), funname.data(), j + 1, 9, 1);
2038                     il_state->DecreaseRef();
2039                     il_state->killMe();
2040                     il_tcur->DecreaseRef();
2041                     il_tcur->killMe();
2042                     delete[] il_sim_labptr;
2043                     delete_strings(l_sim_lab, il_sim_lab->getSize());
2044                     delete[] il_sim_uidptr;
2045                     delete_strings(l_sim_uid, il_sim_uid->getSize());
2046                     delete[] lfunpt;
2047                     delete[] outtbptr;
2048                     delete[] outtbtyp;
2049                     delete[] outtbsz;
2050                     delete[] opar;
2051                     delete[] oparsz;
2052                     delete[] opartyp;
2053                     delete[] oz;
2054                     delete[] ozsz;
2055                     delete[] oztyp;
2056                     FREE(outtb_elem);
2057                     il_sim->DecreaseRef();
2058                     il_sim->killMe();
2059                     return types::Function::Error;
2060                 }
2061             }
2062
2063             // Store 'lnk' and 'pos' in 'outtb_elem'
2064             int k = nelem;
2065             nelem += outtbsz[j] * outtbsz[j + nlnk];
2066             if ((outtb_elem = (outtb_el *) REALLOC(outtb_elem, nelem * sizeof(outtb_el))) == nullptr)
2067             {
2068                 Scierror(999, _("%s : No more free memory.\n"), funname.data());
2069                 il_state->DecreaseRef();
2070                 il_state->killMe();
2071                 il_tcur->DecreaseRef();
2072                 il_tcur->killMe();
2073                 delete[] il_sim_labptr;
2074                 delete_strings(l_sim_lab, il_sim_lab->getSize());
2075                 delete[] il_sim_uidptr;
2076                 delete_strings(l_sim_uid, il_sim_uid->getSize());
2077                 delete[] lfunpt;
2078                 delete[] outtbptr;
2079                 delete[] outtbtyp;
2080                 delete[] outtbsz;
2081                 delete[] opar;
2082                 delete[] oparsz;
2083                 delete[] opartyp;
2084                 delete[] oz;
2085                 delete[] ozsz;
2086                 delete[] oztyp;
2087                 FREE(outtb_elem);
2088                 il_sim->DecreaseRef();
2089                 il_sim->killMe();
2090                 return types::Function::Error;
2091             }
2092             for (int i = 0; i < outtbsz[j]*outtbsz[j + nlnk]; ++i)
2093             {
2094                 outtb_elem[k + i].lnk = j;
2095                 outtb_elem[k + i].pos = i;
2096             }
2097         }
2098     }
2099
2100     /************************
2101     * Call scicos simulator
2102     ************************/
2103     /* Syntax :
2104     * int C2F(scicos)(double *x_in, int *xptr_in, double *z__,
2105     *                 void **work,int *zptr,int *modptr_in,
2106     *                 void **oz,int *ozsz,int *oztyp,int *ozptr,
2107     *                 char **iz,int *izptr,double *t0_in,
2108     *                 double *tf_in,double *tevts_in,int *evtspt_in,
2109     *                 int *nevts,int *pointi_in,void **outtbptr_in,
2110     *                 int *outtbsz_in,int *outtbtyp_in,
2111     *                 outtb_el *outtb_elem_in,int *nelem1,int *nlnk1,
2112     *                 int *funptr,int *funtyp_in,int *inpptr_in,
2113     *                 int *outptr_in, int *inplnk_in,int *outlnk_in,
2114     *                 double *rpar,int *rpptr,int *ipar,int *ipptr,
2115     *                 void **opar,int *oparsz,int *opartyp,int *opptr,
2116     *                 int *clkptr_in,int *ordptr_in,int *nordptr1,
2117     *                 int *ordclk_in,int *cord_in,int *ncord1,
2118     *                 int *iord_in,int *niord1,int *oord_in,
2119     *                 int *noord1,int *zord_in,int *nzord1,
2120     *                 int *critev_in,int *nblk1,int *ztyp,
2121     *                 int *zcptr_in,int *subscr,int *nsubs,
2122     *                 double *simpar,int *flag__,int *ierr_out)
2123     */
2124
2125     int ierr = 0;
2126     C2F(scicos)(l_state_x, l_sim_xptr, l_state_z,
2127                 l_state_iz, l_sim_zptr, l_sim_modptr,
2128                 oz, ozsz, oztyp, l_sim_ozptr,
2129                 l_sim_lab, il_sim_labptr, l_sim_uid, il_sim_uidptr, l_tcur,
2130                 l_tf, l_state_tevts, l_state_evtspt,
2131                 &m1e5, l_pointi, outtbptr,
2132                 outtbsz, outtbtyp,
2133                 outtb_elem, &nelem, &nlnk,
2134                 lfunpt, l_sim_funtyp, l_sim_inpptr,
2135                 l_sim_outptr, l_sim_inplnk, l_sim_outlnk,
2136                 l_sim_rpar, l_sim_rpptr, l_sim_ipar, l_sim_ipptr,
2137                 opar, oparsz, opartyp, l_sim_opptr,
2138                 l_sim_clkptr, l_sim_ordptr, &m_ordptr,
2139                 l_sim_ordclk, l_sim_cord, &m_cord,
2140                 l_sim_iord, &m_iord, l_sim_oord,
2141                 &m_oord, l_sim_zord, &m_zord,
2142                 l_sim_critev, &nblk, l_sim_ztyp,
2143                 l_sim_zcptr, l_sim_subscr, &m_subscr,
2144                 simpar, &flag, &ierr);
2145
2146     /**********************
2147     * Free allocated arrays
2148     **********************/
2149     delete[] outtbptr;
2150     delete[] outtbtyp;
2151     delete[] outtbsz;
2152     delete[] opar;
2153     delete[] oparsz;
2154     delete[] opartyp;
2155     delete[] oz;
2156     delete[] ozsz;
2157     delete[] oztyp;
2158     delete[] lfunpt;
2159     FREE(outtb_elem);
2160
2161     /*************************************
2162     * Switch to appropriate message error
2163     *************************************/
2164     if (ierr > 0)
2165     {
2166         char* error;
2167         switch (ierr)
2168         {
2169             case 1  :
2170                 error = _("scheduling problem");
2171                 C2F(curblk).kfun = 0;
2172                 break;
2173
2174             case 2  :
2175                 error = _("input to zero-crossing stuck on zero");
2176                 C2F(curblk).kfun = 0;
2177                 break;
2178
2179             case 3  :
2180                 error = _("event conflict");
2181                 C2F(curblk).kfun = 0;
2182                 break;
2183
2184             case 4  :
2185                 error = _("algebraic loop detected");
2186                 C2F(curblk).kfun = 0;
2187                 break;
2188
2189             case 5  :
2190                 error = _("cannot allocate memory");
2191                 C2F(curblk).kfun = 0;
2192                 break;
2193
2194             case 6  :
2195                 try
2196                 {
2197                     error = new char[MAX_ERROR_LEN];
2198                     allocatedError = TRUE;
2199                     snprintf(error, MAX_ERROR_LEN, _("the block %d has been called with input out of its domain"), C2F(curblk).kfun);
2200                 }
2201                 catch (const std::bad_alloc& /*e*/)
2202                 {
2203                     error = _("cannot allocate memory");
2204                 }
2205                 break;
2206
2207             case 7  :
2208                 error = _("singularity in a block");
2209                 break;
2210
2211             case 8  :
2212                 error = _("block produces an internal error");
2213                 break;
2214
2215             case 10  :
2216                 error = coserr.buf;
2217                 break;
2218
2219             case 20  :
2220                 error = _("initial conditions not converging");
2221                 break;
2222
2223             case 21  :
2224                 try
2225                 {
2226                     error = new char[MAX_ERROR_LEN];
2227                     allocatedError = TRUE;
2228                     snprintf(error, MAX_ERROR_LEN, _("cannot allocate memory in block=%d"), C2F(curblk).kfun);
2229                 }
2230                 catch (const std::bad_alloc& /*e*/)
2231                 {
2232                     error = _("cannot allocate memory");
2233                 }
2234                 break;
2235
2236             case 22  :
2237                 error = _("sliding mode condition, cannot integrate");
2238                 break;
2239
2240             case 23  :
2241                 error = _("Cannot find the initial mode, maybe there is a sliding mode condition");
2242                 break;
2243             case 24:
2244                 error = _("You have changed a parameter in your model, but the "
2245                           "model has been compiled to use an XML file containing "
2246                           "initial values and parameters. So you should either "
2247                           "recompile your Scicos diagram or [re]launch the "
2248                           "initialization interface to regenerate the XML file  "
2249                           "with new parameters.");
2250                 break;
2251
2252             case 25  :
2253                 error = _("Undefined data type.");
2254                 break;
2255             case 26  :
2256                 error = _("The number of parameter provided by Scicos blocks is different from what expected by the code generated by the Modelica compiler. You might have relaxed a parameter using FIXED property (i.e., fixed=false) in a Modelica model. This will be corrected in the next version");
2257                 break;
2258             // In this case, you need to turn off the parameter embedded code generation mode by setting 'Modelica_ParEmb=%f' in the Scilab command window, and recompile the Scicos diagram
2259
2260             default  :
2261                 if (ierr >= 1000)
2262                 {
2263                     error = _("unknown or erroneous block");
2264                 }
2265                 else if (ierr >= 201 && ierr <= 416) // Sundials error messages, stored in coserr.buf
2266                 {
2267                     error = coserr.buf;
2268                 }
2269                 else if (ierr >= 100)
2270                 {
2271                     int istate = -(ierr - 100);
2272                     try
2273                     {
2274                         error = new char[MAX_ERROR_LEN];
2275                         allocatedError = TRUE;
2276                         snprintf(error, MAX_ERROR_LEN, _("integration problem istate=%d"), istate);
2277                     }
2278                     catch (const std::bad_alloc& /*e*/)
2279                     {
2280                         error = _("cannot allocate memory");
2281                     }
2282                     C2F(curblk).kfun = 0;
2283                 }
2284                 else
2285                 {
2286                     error = _("scicos unexpected error, please report...");
2287                     C2F(curblk).kfun = 0;
2288                 }
2289                 break;
2290         }
2291
2292         Scierror(888, "%s\n", error);
2293         il_state->DecreaseRef();
2294         il_state->killMe();
2295         il_tcur->DecreaseRef();
2296         il_tcur->killMe();
2297         delete[] il_sim_labptr;
2298         delete_strings(l_sim_lab, il_sim_lab->getSize());
2299         delete[] il_sim_uidptr;
2300         delete_strings(l_sim_uid, il_sim_uid->getSize());
2301         if (allocatedError)
2302         {
2303             delete[] error;
2304         }
2305         il_sim->DecreaseRef();
2306         il_sim->killMe();
2307         return types::Function::Error;
2308     }
2309
2310     /*********************
2311     * Return Lhs variables
2312     *********************/
2313     // Convert integer parameters of 'il_state' into double parameters
2314     il_state_evtspt->convertFromInteger();
2315     il_pointi->convertFromInteger();
2316
2317     out.push_back(il_state);
2318     out.push_back(il_tcur);
2319
2320     // End
2321     delete[] il_sim_labptr;
2322     delete_strings(l_sim_lab, il_sim_lab->getSize());
2323     delete[] il_sim_uidptr;
2324     delete_strings(l_sim_uid, il_sim_uid->getSize());
2325     il_sim->DecreaseRef();
2326     il_sim->killMe();
2327     return types::Function::OK;
2328 }
2329