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