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