2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2014 - Scilab Enterprises - Paul Bignier
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
21 #include "gw_scicos.hxx"
23 #include "internal.hxx"
25 #include "context.hxx"
27 #include "function.hxx"
34 #include "il_state.hxx"
39 #include "machine.h" /* C2F */
41 #include "sci_malloc.h"
42 #include "scicos.h" /* funnum2() */
43 #include "scicos-def.h"
44 #include "charEncoding.h"
46 #include "localization.h"
49 COSIM_struct C2F(cosim);
50 /*--------------------------------------------------------------------------*/
51 // Variables defined in scicos.c
52 extern COSERR_struct coserr;
53 extern CURBLK_struct C2F(curblk);
54 /*--------------------------------------------------------------------------*/
58 #define snprintf _snprintf
61 /*--------------------------------------------------------------------------*/
62 /* intsicosimc scicosim interface routine.
64 * [state,t] = scicosim(state,tcur,tf,sim,str,tol)
67 * - 1 : state(1) : !xcs x z oz iz tevts evtspt pointi outtb !
68 * - 2 : state.x : column vector of real
69 * - 3 : state.z : column vector of real
70 * - 4 : state.oz : list of scilab object
71 * - 5 : state.iz : column vector of real (empty object with flag "finish")
72 * - 6 : state.tevts : column vector of real
73 * - 7 : state.evtspt : column vector of real
74 * - 8 : state.pointi : real scalar
75 * - 9 : state.outtb : list of scilab object
76 * rhs 2 tcur : real scalar
77 * rhs 3 tf : real scalar
79 * - 1 : sim(1) : !scs funs xptr zptr ozptr zcptr inpptr
80 * outptr inplnk outlnk rpar rpptr ipar ipptr
81 * opar opptr clkptr ordptr execlk ordclk cord
82 * oord zord critev nb ztyp nblk ndcblk
83 * subscr funtyp iord labels modptr uids !
84 * - 2 : sim.funs : list of strings and/or scilab function
85 * - 3 : sim.xptr : column vector of real
86 * - 4 : sim.zptr : column vector of real
87 * - 5 : sim.ozptr : column vector of real
88 * - 6 : sim.zcptr : column vector of real
89 * - 7 : sim.inpptr : column vector of real
90 * - 8 : sim.outptr : column vector of real
91 * - 9 : sim.inplnk : column vector of real
92 * - 10 : sim.outlnk : column vector of real
93 * - 11 : sim.rpar : column vector of real
94 * - 12 : sim.rpptr : column vector of real
95 * - 13 : sim.ipar : column vector of real
96 * - 14 : sim.ipptr : column vector of real
97 * - 15 : sim.opar : list of scilab object
98 * - 16 : sim.opptr : column vector of real
99 * - 17 : sim.clkptr : column vector of real
100 * - 18 : sim.ordptr : column vector of real
101 * - 19 : sim.execlk : matrix of real
102 * - 20 : sim.ordclk : matrix of real
103 * - 21 : sim.cord : matrix of real
104 * - 22 : sim.oord : matrix of real
105 * - 23 : sim.zord : column vector ? of real
106 * - 24 : sim.critev : column vector of real
107 * - 25 : sim.nb : real scalar
108 * - 26 : sim.ztyp : column vector of real
109 * - 27 : sim.nblk : real scalar
110 * - 28 : sim.ndcblk : real scalar
111 * - 29 : sim.subscr : column vector of real
112 * - 30 : sim.funtyp : column vector of real
113 * - 31 : sim.iord : column vector of real
114 * - 32 : sim.labels : column vector of strings
115 * - 33 : sim.modptr : column vector of real
116 * - 34 : sim.uids : column vector of strings
118 * rhs 5 str : string flag : 'start','run','finish','linear'
119 * rhs 6 tol : real vector of size (7,1) minimum (4,1)
120 * [atol rtol ttol [deltat realtimescale solver hmax]]'
122 * 16/03/06, A.Layec : Rewritten from original fortran
123 * source code intsscicos in intcos.f.
125 * 29/03/06, Alan : Improvement in accordance to c_pass2
126 * (int32 parameters).
128 * 31/05/06, Alan : Add global variable int *il_state_save
129 * and int *il_sim_save in intcscicos.h to store
130 * stack address of list %cpr.state and %cpr.sim
131 * (to use with get/setscicosvars).
133 * 14/06/06, Alan : Save common intersci before calling scicos
134 * (to disable scilab crash with scifunc.f).
136 * 13/11/06, Alan : Get back to double parameters for sim and state
137 * (for better compatibility with scilab-4.x families).
138 * Remove il_sim_save global variable.
140 * 15/12/06, Alan : Warnings compilation removed.
141 * This can crash scilab/scicos.
144 * xx/02/07, Alan : Add opar/odstate : scilab lists of arbitrary object
147 * 08/12/14, Paul : Rewrite to C++.
150 /*--------------------------------------------------------------------------*/
152 static const std::string funname = "scicosim";
154 types::Function::ReturnValue sci_scicosim(types::typed_list &in, int _iRetCount, types::typed_list &out)
156 /************************************
157 * Variables and constants definition
158 ************************************/
161 BOOL allocatedError = FALSE;
162 const int MAX_ERROR_LEN = 512;
166 Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), funname.data(), 6);
167 return types::Function::Error;
172 Scierror(78, _("%s: Wrong number of output argument(s): %d to %d expected.\n"), funname.data(), 1, 2);
173 return types::Function::Error;
179 if (in[0]->isTList() == false)
181 Scierror(999, _("%s: Wrong type for input argument #%d : A tlist expected.\n"), funname.data(), 1);
182 return types::Function::Error;
184 types::TList* il_state_input = in[0]->getAs<types::TList>();
185 types::TList* il_state = new types::TList();
187 if (il_state_input->getSize() < 9)
189 Scierror(999, _("%s: Wrong size for input argument #%d : %d elements expected.\n"), funname.data(), 1, 9);
190 il_state->DecreaseRef();
192 return types::Function::Error;
195 // Make a copy of 'il_state' in a global variabe
196 set_il_state(il_state_input);
198 types::String* header_state = il_state_input->get(0)->getAs<types::String>();
199 il_state->append(header_state->clone());
202 if (il_state_input->get(1)->isDouble() == false)
204 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 2, 1);
205 il_state->DecreaseRef();
207 return types::Function::Error;
209 types::Double* il_state_x_input = il_state_input->get(1)->getAs<types::Double>();
210 il_state->append(il_state_x_input->clone());
211 types::Double* il_state_x = il_state->get(1)->getAs<types::Double>();
212 double* l_state_x = il_state_x->get();
215 if (il_state_input->get(2)->isDouble() == false)
217 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 3, 1);
218 il_state->DecreaseRef();
220 return types::Function::Error;
222 types::Double* il_state_z_input = il_state_input->get(2)->getAs<types::Double>();
223 il_state->append(il_state_z_input->clone());
224 types::Double* il_state_z = il_state->get(2)->getAs<types::Double>();
225 double* l_state_z = il_state_z->get();
228 types::List* il_state_oz_input = il_state_input->get(3)->getAs<types::List>();
229 types::List* il_state_oz = new types::List();
230 for (int i = 0; i < il_state_oz_input->getSize(); ++i)
232 types::InternalType* l_state_oz_input = il_state_oz_input->get(i);
233 il_state_oz->append(l_state_oz_input->clone());
235 il_state->append(il_state_oz);
236 int noz = il_state_oz->getSize(); // 'nlnk' is the dimension of the list 'state.oz'
239 if (il_state_input->get(4)->isDouble() == false)
241 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 5, 1);
242 return types::Function::Error;
244 types::Double* il_state_iz_input = il_state_input->get(4)->getAs<types::Double>();
245 il_state->append(il_state_iz_input->clone());
246 types::Double* il_state_iz = il_state->get(4)->getAs<types::Double>();
247 void** l_state_iz = (void**) il_state_iz->get();
248 int m1e5 = il_state_iz->getRows();
251 if (il_state_input->get(5)->isDouble() == false)
253 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 6, 1);
254 il_state->DecreaseRef();
256 return types::Function::Error;
258 types::Double* il_state_tevts_input = il_state_input->get(5)->getAs<types::Double>();
259 il_state->append(il_state_tevts_input->clone());
260 types::Double* il_state_tevts = il_state->get(5)->getAs<types::Double>();
261 double* l_state_tevts = il_state_tevts->get();
262 int m1e6 = il_state_tevts->getRows();
264 /*7 : state.evtspt */
265 if (il_state_input->get(6)->isDouble() == false)
267 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 7, 1);
268 il_state->DecreaseRef();
270 return types::Function::Error;
272 types::Double* il_state_evtspt_input = il_state_input->get(6)->getAs<types::Double>();
273 il_state->append(il_state_evtspt_input->clone());
274 types::Double* il_state_evtspt = il_state->get(6)->getAs<types::Double>();
275 il_state_evtspt->convertToInteger();
276 int* l_state_evtspt = (int*) il_state_evtspt->get();
277 int m1e7 = il_state_evtspt->getRows();
279 /*8 : state.pointi */
280 if (il_state_input->get(7)->isDouble() == false)
282 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 8, 1);
283 il_state->DecreaseRef();
285 return types::Function::Error;
287 types::Double* il_pointi_input = il_state_input->get(7)->getAs<types::Double>();
288 il_state->append(il_pointi_input->clone());
289 types::Double* il_pointi = il_state->get(7)->getAs<types::Double>();
290 il_pointi->convertToInteger();
291 int* l_pointi = (int*) il_pointi->get();
294 if (il_state_input->get(8)->isList() == false)
296 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A list expected.\n"), funname.data(), 9, 1);
297 il_state->DecreaseRef();
299 return types::Function::Error;
301 types::List* il_state_outtb_input = il_state_input->get(8)->getAs<types::List>();
302 types::List* il_state_outtb = new types::List();
303 for (int i = 0; i < il_state_outtb_input->getSize(); ++i)
305 types::InternalType* l_state_outtb_input = il_state_outtb_input->get(i);
306 il_state_outtb->append(l_state_outtb_input->clone());
308 il_state->append(il_state_outtb);
309 int nlnk = il_state_outtb->getSize(); // 'nlnk' is the dimension of the list 'state.outtb'
314 if (in[1]->isDouble() == false)
316 Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 2);
317 il_state->DecreaseRef();
319 return types::Function::Error;
321 types::Double* il_tcur_input = in[1]->getAs<types::Double>();
322 types::Double* il_tcur = il_tcur_input->clone()->getAs<types::Double>();
323 if (il_tcur->isScalar() == false)
325 Scierror(999, _("%s: Wrong size for input argument #%d : A scalar expected.\n"), funname.data(), 2);
326 il_state->DecreaseRef();
328 il_tcur->DecreaseRef();
330 return types::Function::Error;
332 double* l_tcur = il_tcur->get();
337 if (in[2]->isDouble() == false)
339 Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 3);
340 il_state->DecreaseRef();
342 il_tcur->DecreaseRef();
344 return types::Function::Error;
346 types::Double* il_tf = in[2]->getAs<types::Double>();
347 if (il_tf->isScalar() == false)
349 Scierror(999, _("%s: Wrong size for input argument #%d : A scalar expected.\n"), funname.data(), 3);
350 il_state->DecreaseRef();
352 il_tcur->DecreaseRef();
354 return types::Function::Error;
356 double* l_tf = il_tf->get();
361 if (in[3]->isTList() == false)
363 Scierror(999, _("%s: Wrong type for input argument #%d : A tlist expected.\n"), funname.data(), 4);
364 il_state->DecreaseRef();
366 il_tcur->DecreaseRef();
368 return types::Function::Error;
370 types::TList* il_sim_input = in[3]->getAs<types::TList>();
371 types::TList* il_sim = new types::TList();
373 // Make a copy of 'il_sim' in a global variabe
374 set_il_sim(il_sim_input);
376 if (il_sim_input->getSize() < 34)
378 Scierror(999, _("%s: Wrong size for input argument #%d : %d elements expected.\n"), funname.data(), 4, 34);
379 il_state->DecreaseRef();
381 il_tcur->DecreaseRef();
383 il_sim->DecreaseRef();
385 return types::Function::Error;
388 types::String* header_sim = il_sim_input->get(0)->getAs<types::String>();
389 il_sim->append(header_sim->clone());
392 if (il_sim_input->get(1)->isList() == false)
394 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A list expected.\n"), funname.data(), 2, 4);
395 il_state->DecreaseRef();
397 il_tcur->DecreaseRef();
399 il_sim->DecreaseRef();
401 return types::Function::Error;
403 types::List* il_sim_fun_input = il_sim_input->get(1)->getAs<types::List>();
404 types::List* il_sim_fun = new types::List();
405 for (int i = 0; i < il_sim_fun_input->getSize(); ++i)
407 types::InternalType* l_sim_fun_input = il_sim_fun_input->get(i);
408 il_sim_fun->append(l_sim_fun_input->clone());
410 il_sim->append(il_sim_fun);
411 int nblk = il_sim_fun->getSize(); // 'nblk' is the dimension of the list 'sim.funs'
414 if (il_sim_input->get(2)->isDouble() == false)
416 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 3, 4);
417 il_state->DecreaseRef();
419 il_tcur->DecreaseRef();
421 il_sim->DecreaseRef();
423 return types::Function::Error;
425 types::Double* il_sim_xptr_input = il_sim_input->get(2)->getAs<types::Double>();
426 il_sim->append(il_sim_xptr_input->clone());
427 types::Double* il_sim_xptr = il_sim->get(2)->getAs<types::Double>();
428 il_sim_xptr->convertToInteger();
429 int* l_sim_xptr = (int*) il_sim_xptr->get();
430 int m_xptr = il_sim_xptr->getRows();
433 if (il_sim_input->get(3)->isDouble() == false)
435 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 4, 4);
436 il_state->DecreaseRef();
438 il_tcur->DecreaseRef();
440 il_sim->DecreaseRef();
442 return types::Function::Error;
444 types::Double* il_sim_zptr_input = il_sim_input->get(3)->getAs<types::Double>();
445 il_sim->append(il_sim_zptr_input->clone());
446 types::Double* il_sim_zptr = il_sim->get(3)->getAs<types::Double>();
447 il_sim_zptr->convertToInteger();
448 int* l_sim_zptr = (int*) il_sim_zptr->get();
449 int m_zptr = il_sim_zptr->getRows();
452 if (il_sim_input->get(4)->isDouble() == false)
454 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 5, 4);
455 il_state->DecreaseRef();
457 il_tcur->DecreaseRef();
459 il_sim->DecreaseRef();
461 return types::Function::Error;
463 types::Double* il_sim_ozptr_input = il_sim_input->get(4)->getAs<types::Double>();
464 il_sim->append(il_sim_ozptr_input->clone());
465 types::Double* il_sim_ozptr = il_sim->get(4)->getAs<types::Double>();
466 il_sim_ozptr->convertToInteger();
467 int* l_sim_ozptr = (int*) il_sim_ozptr->get();
468 int m_ozptr = il_sim_ozptr->getRows();
471 if (il_sim_input->get(5)->isDouble() == false)
473 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 6, 4);
474 il_state->DecreaseRef();
476 il_tcur->DecreaseRef();
478 il_sim->DecreaseRef();
480 return types::Function::Error;
482 types::Double* il_sim_zcptr_input = il_sim_input->get(5)->getAs<types::Double>();
483 il_sim->append(il_sim_zcptr_input->clone());
484 types::Double* il_sim_zcptr = il_sim->get(5)->getAs<types::Double>();
485 il_sim_zcptr->convertToInteger();
486 int* l_sim_zcptr = (int*) il_sim_zcptr->get();
487 int m_zcptr = il_sim_zcptr->getRows();
490 if (il_sim_input->get(6)->isDouble() == false)
492 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 7, 4);
493 il_state->DecreaseRef();
495 il_tcur->DecreaseRef();
497 il_sim->DecreaseRef();
499 return types::Function::Error;
501 types::Double* il_sim_inpptr_input = il_sim_input->get(6)->getAs<types::Double>();
502 il_sim->append(il_sim_inpptr_input->clone());
503 types::Double* il_sim_inpptr = il_sim->get(6)->getAs<types::Double>();
504 il_sim_inpptr->convertToInteger();
505 int* l_sim_inpptr = (int*) il_sim_inpptr->get();
506 int m_inpptr = il_sim_inpptr->getRows();
509 if (il_sim_input->get(7)->isDouble() == false)
511 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 8, 4);
512 il_state->DecreaseRef();
514 il_tcur->DecreaseRef();
516 il_sim->DecreaseRef();
518 return types::Function::Error;
520 types::Double* il_sim_outpptr_input = il_sim_input->get(7)->getAs<types::Double>();
521 il_sim->append(il_sim_outpptr_input->clone());
522 types::Double* il_sim_outptr = il_sim->get(7)->getAs<types::Double>();
523 il_sim_outptr->convertToInteger();
524 int* l_sim_outptr = (int*) il_sim_outptr->get();
525 int m_outptr = il_sim_outptr->getRows();
528 if (il_sim_input->get(8)->isDouble() == false)
530 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 9, 4);
531 il_state->DecreaseRef();
533 il_tcur->DecreaseRef();
535 il_sim->DecreaseRef();
537 return types::Function::Error;
539 types::Double* il_sim_inplnk_input = il_sim_input->get(8)->getAs<types::Double>();
540 il_sim->append(il_sim_inplnk_input->clone());
541 types::Double* il_sim_inplnk = il_sim->get(8)->getAs<types::Double>();
542 il_sim_inplnk->convertToInteger();
543 int* l_sim_inplnk = (int*) il_sim_inplnk->get();
546 if (il_sim_input->get(9)->isDouble() == false)
548 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 10, 4);
549 il_state->DecreaseRef();
551 il_tcur->DecreaseRef();
553 il_sim->DecreaseRef();
555 return types::Function::Error;
557 types::Double* il_sim_outlnk_input = il_sim_input->get(9)->getAs<types::Double>();
558 il_sim->append(il_sim_outlnk_input->clone());
559 types::Double* il_sim_outlnk = il_sim->get(9)->getAs<types::Double>();
560 il_sim_outlnk->convertToInteger();
561 int* l_sim_outlnk = (int*) il_sim_outlnk->get();
564 if (il_sim_input->get(10)->isDouble() == false)
566 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 11, 4);
567 il_state->DecreaseRef();
569 il_tcur->DecreaseRef();
571 il_sim->DecreaseRef();
573 return types::Function::Error;
575 types::Double* il_sim_rpar_input = il_sim_input->get(10)->getAs<types::Double>();
576 il_sim->append(il_sim_rpar_input->clone());
577 types::Double* il_sim_rpar = il_sim->get(10)->getAs<types::Double>();
578 double* l_sim_rpar = il_sim_rpar->get();
581 if (il_sim_input->get(11)->isDouble() == false)
583 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 12, 4);
584 il_state->DecreaseRef();
586 il_tcur->DecreaseRef();
588 il_sim->DecreaseRef();
590 return types::Function::Error;
592 types::Double* il_sim_rpptr_input = il_sim_input->get(11)->getAs<types::Double>();
593 il_sim->append(il_sim_rpptr_input->clone());
594 types::Double* il_sim_rpptr = il_sim->get(11)->getAs<types::Double>();
595 il_sim_rpptr->convertToInteger();
596 int* l_sim_rpptr = (int*) il_sim_rpptr->get();
597 int m_rpptr = il_sim_rpptr->getRows();
600 if (il_sim_input->get(12)->isDouble() == false)
602 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 13, 4);
603 il_state->DecreaseRef();
605 il_tcur->DecreaseRef();
607 il_sim->DecreaseRef();
609 return types::Function::Error;
611 types::Double* il_sim_ipar_input = il_sim_input->get(12)->getAs<types::Double>();
612 il_sim->append(il_sim_ipar_input->clone());
613 types::Double* il_sim_ipar = il_sim->get(12)->getAs<types::Double>();
614 il_sim_ipar->convertToInteger();
615 int* l_sim_ipar = (int*) il_sim_ipar->get();
618 if (il_sim_input->get(13)->isDouble() == false)
620 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 14, 4);
621 il_state->DecreaseRef();
623 il_tcur->DecreaseRef();
625 il_sim->DecreaseRef();
627 return types::Function::Error;
629 types::Double* il_sim_ipptr_input = il_sim_input->get(13)->getAs<types::Double>();
630 il_sim->append(il_sim_ipptr_input->clone());
631 types::Double* il_sim_ipptr = il_sim->get(13)->getAs<types::Double>();
632 il_sim_ipptr->convertToInteger();
633 int* l_sim_ipptr = (int*) il_sim_ipptr->get();
634 int m_ipptr = il_sim_ipptr->getRows();
637 if (il_sim_input->get(14)->isList() == false)
639 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A list expected.\n"), funname.data(), 15, 4);
640 il_state->DecreaseRef();
642 il_tcur->DecreaseRef();
644 il_sim->DecreaseRef();
646 return types::Function::Error;
648 types::List* il_sim_opar_input = il_sim_input->get(14)->getAs<types::List>();
649 types::List* il_sim_opar = new types::List();
650 for (int i = 0; i < il_sim_opar_input->getSize(); ++i)
652 types::InternalType* l_sim_opar_input = il_sim_opar_input->get(i);
653 il_sim_opar->append(l_sim_opar_input->clone());
655 il_sim->append(il_sim_opar);
656 int nopar = il_sim_opar->getSize(); // 'nopar' is the dimension of the list 'sim.opar'
659 if (il_sim_input->get(15)->isDouble() == false)
661 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 16, 4);
662 il_state->DecreaseRef();
664 il_tcur->DecreaseRef();
666 il_sim->DecreaseRef();
668 return types::Function::Error;
670 types::Double* il_sim_opptr_input = il_sim_input->get(15)->getAs<types::Double>();
671 il_sim->append(il_sim_opptr_input->clone());
672 types::Double* il_sim_opptr = il_sim->get(15)->getAs<types::Double>();
673 il_sim_opptr->convertToInteger();
674 int* l_sim_opptr = (int*) il_sim_opptr->get();
675 int m_opptr = il_sim_opptr->getRows();
678 if (il_sim_input->get(16)->isDouble() == false)
680 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 17, 4);
681 il_state->DecreaseRef();
683 il_tcur->DecreaseRef();
685 il_sim->DecreaseRef();
687 return types::Function::Error;
689 types::Double* il_sim_clkptr_input = il_sim_input->get(16)->getAs<types::Double>();
690 il_sim->append(il_sim_clkptr_input->clone());
691 types::Double* il_sim_clkptr = il_sim->get(16)->getAs<types::Double>();
692 il_sim_clkptr->convertToInteger();
693 int* l_sim_clkptr = (int*) il_sim_clkptr->get();
694 int m_clkptr = il_sim_clkptr->getRows();
697 if (il_sim_input->get(17)->isDouble() == false)
699 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 18, 4);
700 il_state->DecreaseRef();
702 il_tcur->DecreaseRef();
704 il_sim->DecreaseRef();
706 return types::Function::Error;
708 types::Double* il_sim_ordptr_input = il_sim_input->get(17)->getAs<types::Double>();
709 il_sim->append(il_sim_ordptr_input->clone());
710 types::Double* il_sim_ordptr = il_sim->get(17)->getAs<types::Double>();
711 il_sim_ordptr->convertToInteger();
712 int* l_sim_ordptr = (int*) il_sim_ordptr->get();
713 int m_ordptr = il_sim_ordptr->getRows();
716 if (il_sim_input->get(18)->isDouble() == false)
718 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 19, 4);
719 il_state->DecreaseRef();
721 il_tcur->DecreaseRef();
723 il_sim->DecreaseRef();
725 return types::Function::Error;
727 types::Double* il_sim_execlk_input = il_sim_input->get(18)->getAs<types::Double>();
728 il_sim->append(il_sim_execlk_input->clone());
731 if (il_sim_input->get(19)->isDouble() == false)
733 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 20, 4);
734 il_state->DecreaseRef();
736 il_tcur->DecreaseRef();
738 il_sim->DecreaseRef();
740 return types::Function::Error;
742 types::Double* il_sim_ordclk_input = il_sim_input->get(19)->getAs<types::Double>();
743 il_sim->append(il_sim_ordclk_input->clone());
744 types::Double* il_sim_ordclk = il_sim->get(19)->getAs<types::Double>();
745 il_sim_ordclk->convertToInteger();
746 int* l_sim_ordclk = (int*) il_sim_ordclk->get();
747 int m_ordclk = il_sim_ordclk->getRows();
748 int n_ordclk = il_sim_ordclk->getCols();
751 if (il_sim_input->get(20)->isDouble() == false)
753 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 21, 4);
754 il_state->DecreaseRef();
756 il_tcur->DecreaseRef();
758 il_sim->DecreaseRef();
760 return types::Function::Error;
762 types::Double* il_sim_cord_input = il_sim_input->get(20)->getAs<types::Double>();
763 il_sim->append(il_sim_cord_input->clone());
764 types::Double* il_sim_cord = il_sim->get(20)->getAs<types::Double>();
765 il_sim_cord->convertToInteger();
766 int* l_sim_cord = (int*) il_sim_cord->get();
767 int m_cord = il_sim_cord->getRows();
768 int n_cord = il_sim_cord->getCols();
771 if (il_sim_input->get(21)->isDouble() == false)
773 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 22, 4);
774 il_state->DecreaseRef();
776 il_tcur->DecreaseRef();
778 il_sim->DecreaseRef();
780 return types::Function::Error;
782 types::Double* il_sim_oord_input = il_sim_input->get(21)->getAs<types::Double>();
783 il_sim->append(il_sim_oord_input->clone());
784 types::Double* il_sim_oord = il_sim->get(21)->getAs<types::Double>();
785 il_sim_oord->convertToInteger();
786 int* l_sim_oord = (int*) il_sim_oord->get();
787 int m_oord = il_sim_oord->getRows();
788 int n_oord = il_sim_oord->getCols();
791 if (il_sim_input->get(22)->isDouble() == false)
793 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 23, 4);
794 il_state->DecreaseRef();
796 il_tcur->DecreaseRef();
798 il_sim->DecreaseRef();
800 return types::Function::Error;
802 types::Double* il_sim_zord_input = il_sim_input->get(22)->getAs<types::Double>();
803 il_sim->append(il_sim_zord_input->clone());
804 types::Double* il_sim_zord = il_sim->get(22)->getAs<types::Double>();
805 il_sim_zord->convertToInteger();
806 int* l_sim_zord = (int*) il_sim_zord->get();
807 int m_zord = il_sim_zord->getRows();
808 int n_zord = il_sim_zord->getCols();
811 if (il_sim_input->get(23)->isDouble() == false)
813 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 24, 4);
814 il_state->DecreaseRef();
816 il_tcur->DecreaseRef();
818 il_sim->DecreaseRef();
820 return types::Function::Error;
822 types::Double* il_sim_critev_input = il_sim_input->get(23)->getAs<types::Double>();
823 il_sim->append(il_sim_critev_input->clone());
824 types::Double* il_sim_critev = il_sim->get(23)->getAs<types::Double>();
825 il_sim_critev->convertToInteger();
826 int* l_sim_critev = (int*) il_sim_critev->get();
829 if (il_sim_input->get(24)->isDouble() == false)
831 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 25, 4);
832 il_state->DecreaseRef();
834 il_tcur->DecreaseRef();
836 il_sim->DecreaseRef();
838 return types::Function::Error;
840 types::Double* il_sim_nb_input = il_sim_input->get(24)->getAs<types::Double>();
841 il_sim->append(il_sim_nb_input->clone());
842 types::Double* il_sim_nb = il_sim->get(24)->getAs<types::Double>();
843 double* l_sim_nb = il_sim_nb->get();
845 if (static_cast<int>(l_sim_nb[0]) != nblk) // Value of 'nb' must be equal to 'nblk'
847 Scierror(42, _("%s : Incompatible sim.nb RHS parameter.\n"), funname.data());
848 il_state->DecreaseRef();
850 il_tcur->DecreaseRef();
852 il_sim->DecreaseRef();
854 return types::Function::Error;
858 if (il_sim_input->get(25)->isDouble() == false)
860 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 26, 4);
861 il_state->DecreaseRef();
863 il_tcur->DecreaseRef();
865 il_sim->DecreaseRef();
867 return types::Function::Error;
869 types::Double* il_sim_ztyp_input = il_sim_input->get(25)->getAs<types::Double>();
870 il_sim->append(il_sim_ztyp_input->clone());
871 types::Double* il_sim_ztyp = il_sim->get(25)->getAs<types::Double>();
872 il_sim_ztyp->convertToInteger();
873 int* l_sim_ztyp = (int*) il_sim_ztyp->get();
876 if (il_sim_input->get(26)->isDouble() == false)
878 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 27, 4);
879 il_state->DecreaseRef();
881 il_tcur->DecreaseRef();
883 il_sim->DecreaseRef();
885 return types::Function::Error;
887 types::Double* il_sim_nblk_input = il_sim_input->get(26)->getAs<types::Double>();
888 il_sim->append(il_sim_nblk_input->clone());
891 if (il_sim_input->get(27)->isDouble() == false)
893 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 28, 4);
894 il_state->DecreaseRef();
896 il_tcur->DecreaseRef();
898 il_sim->DecreaseRef();
900 return types::Function::Error;
902 types::Double* il_sim_ndcblk_input = il_sim_input->get(27)->getAs<types::Double>();
903 il_sim->append(il_sim_ndcblk_input->clone());
906 if (il_sim_input->get(28)->isDouble() == false)
908 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 29, 4);
909 il_state->DecreaseRef();
911 il_tcur->DecreaseRef();
913 il_sim->DecreaseRef();
915 return types::Function::Error;
917 types::Double* il_sim_subscr_input = il_sim_input->get(28)->getAs<types::Double>();
918 il_sim->append(il_sim_subscr_input->clone());
919 types::Double* il_sim_subscr = il_sim->get(28)->getAs<types::Double>();
920 il_sim_subscr->convertToInteger();
921 int* l_sim_subscr = (int*) il_sim_subscr->get();
922 int m_subscr = il_sim_subscr->getRows();
925 if (il_sim_input->get(29)->isDouble() == false)
927 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 30, 4);
928 il_state->DecreaseRef();
930 il_tcur->DecreaseRef();
932 il_sim->DecreaseRef();
934 return types::Function::Error;
936 types::Double* il_sim_funtyp_input = il_sim_input->get(29)->getAs<types::Double>();
937 il_sim->append(il_sim_funtyp_input->clone());
938 types::Double* il_sim_funtyp = il_sim->get(29)->getAs<types::Double>();
939 il_sim_funtyp->convertToInteger();
940 int* l_sim_funtyp = (int*) il_sim_funtyp->get();
943 if (il_sim_input->get(30)->isDouble() == false)
945 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 31, 4);
946 il_state->DecreaseRef();
948 il_tcur->DecreaseRef();
950 il_sim->DecreaseRef();
952 return types::Function::Error;
954 types::Double* il_sim_iord_input = il_sim_input->get(30)->getAs<types::Double>();
955 il_sim->append(il_sim_iord_input->clone());
956 types::Double* il_sim_iord = il_sim->get(30)->getAs<types::Double>();
957 il_sim_iord->convertToInteger();
958 int* l_sim_iord = (int*) il_sim_iord->get();
959 int m_iord = il_sim_iord->getRows();
960 int n_iord = il_sim_iord->getCols();
963 if (il_sim_input->get(31)->isString() == false)
965 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A string matrix expected.\n"), funname.data(), 32, 4);
966 il_state->DecreaseRef();
968 il_tcur->DecreaseRef();
970 il_sim->DecreaseRef();
972 return types::Function::Error;
974 types::String* il_sim_lab_input = il_sim_input->get(31)->getAs<types::String>();
975 il_sim->append(il_sim_lab_input->clone());
976 types::String* il_sim_lab = il_sim->get(31)->getAs<types::String>();
980 il_sim_labptr = new int[il_sim_lab->getSize()];
982 catch (const std::bad_alloc& /*e*/)
984 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
985 il_state->DecreaseRef();
987 il_tcur->DecreaseRef();
989 il_sim->DecreaseRef();
991 return types::Function::Error;
996 l_sim_lab = new char*[il_sim_lab->getSize()];
998 catch (const std::bad_alloc& /*e*/)
1000 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1001 il_state->DecreaseRef();
1003 il_tcur->DecreaseRef();
1005 il_sim->DecreaseRef();
1007 delete[] il_sim_labptr;
1008 return types::Function::Error;
1010 for (int i = 0; i < il_sim_lab->getSize(); ++i)
1012 l_sim_lab[i] = wide_string_to_UTF8(il_sim_lab->get(i));
1013 il_sim_labptr[i] = static_cast<int>(strlen(l_sim_lab[i]));
1016 /*33 : sim.modptr */
1017 if (il_sim_input->get(32)->isDouble() == false)
1019 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 33, 4);
1020 il_state->DecreaseRef();
1022 il_tcur->DecreaseRef();
1024 il_sim->DecreaseRef();
1026 delete[] il_sim_labptr;
1028 return types::Function::Error;
1030 types::Double* il_sim_modptr_input = il_sim_input->get(32)->getAs<types::Double>();
1031 il_sim->append(il_sim_modptr_input->clone());
1032 types::Double* il_sim_modptr = il_sim->get(32)->getAs<types::Double>();
1033 il_sim_modptr->convertToInteger();
1034 int* l_sim_modptr = (int*) il_sim_modptr->get();
1037 if (il_sim_input->get(33)->isString() == false)
1039 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A string matrix expected.\n"), funname.data(), 34, 4);
1040 il_state->DecreaseRef();
1042 il_tcur->DecreaseRef();
1044 il_sim->DecreaseRef();
1046 delete[] il_sim_labptr;
1048 return types::Function::Error;
1050 types::String* il_sim_uid_input = il_sim_input->get(33)->getAs<types::String>();
1051 il_sim->append(il_sim_uid_input->clone());
1052 types::String* il_sim_uid = il_sim->get(33)->getAs<types::String>();
1056 il_sim_uidptr = new int[il_sim_uid->getSize()];
1058 catch (const std::bad_alloc& /*e*/)
1060 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1061 il_state->DecreaseRef();
1063 il_tcur->DecreaseRef();
1065 il_sim->DecreaseRef();
1067 delete[] il_sim_labptr;
1069 return types::Function::Error;
1074 l_sim_uid = new char*[il_sim_uid->getSize()];
1076 catch (const std::bad_alloc& /*e*/)
1078 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1079 il_state->DecreaseRef();
1081 il_tcur->DecreaseRef();
1083 il_sim->DecreaseRef();
1085 delete[] il_sim_labptr;
1087 delete[] il_sim_uidptr;
1088 return types::Function::Error;
1090 for (int i = 0; i < il_sim_uid->getSize(); ++i)
1092 l_sim_uid[i] = wide_string_to_UTF8(il_sim_uid->get(i));
1093 il_sim_uidptr[i] = static_cast<int>(strlen(l_sim_uid[i]));
1099 if (in[4]->isString() == false)
1101 Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 5);
1102 il_state->DecreaseRef();
1104 il_tcur->DecreaseRef();
1106 il_sim->DecreaseRef();
1108 delete[] il_sim_labptr;
1110 delete[] il_sim_uidptr;
1112 return types::Function::Error;
1114 types::String* il_str = in[4]->getAs<types::String>();
1115 if (il_str->isScalar() == false)
1117 Scierror(999, _("%s: Wrong size for input argument #%d : A scalar expected.\n"), funname.data(), 5);
1118 il_state->DecreaseRef();
1120 il_tcur->DecreaseRef();
1122 il_sim->DecreaseRef();
1124 delete[] il_sim_labptr;
1126 delete[] il_sim_uidptr;
1128 return types::Function::Error;
1131 const std::wstring start (L"start");
1132 const std::wstring run (L"run");
1133 const std::wstring finish (L"finish");
1134 const std::wstring linear (L"linear");
1135 const std::wstring Kinsol (L"Kinsol");
1137 if (il_str->get(0) == start)
1141 else if (il_str->get(0) == run)
1145 else if (il_str->get(0) == finish)
1149 else if (il_str->get(0) == linear)
1153 else if (il_str->get(0) == Kinsol)
1159 Scierror(44, _("%s: Wrong value for input argument #%d : ""%s"", ""%s"" ""%s"" ""%s"" or ""%s"" expected.\n"), funname.data(), 5,
1160 "start", "run", "finish", "linear", "Kinsol");
1161 il_state->DecreaseRef();
1163 il_tcur->DecreaseRef();
1165 il_sim->DecreaseRef();
1167 delete[] il_sim_labptr;
1169 delete[] il_sim_uidptr;
1171 return types::Function::Error;
1177 if (in[5]->isDouble() == false)
1179 Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 6);
1180 il_state->DecreaseRef();
1182 il_tcur->DecreaseRef();
1184 il_sim->DecreaseRef();
1186 delete[] il_sim_labptr;
1188 delete[] il_sim_uidptr;
1190 return types::Function::Error;
1192 types::Double* il_tol = in[5]->getAs<types::Double>();
1193 int m6 = il_tol->getSize();
1194 if (m6 < 4 || m6 > 7) // Check if 'tol' has 4 to 7 elements
1196 Scierror(999, _("%s: Wrong size for input argument #%d : %d to %d elements expected.\n"), funname.data(), 6, 4, 7);
1197 il_state->DecreaseRef();
1199 il_tcur->DecreaseRef();
1201 il_sim->DecreaseRef();
1203 delete[] il_sim_labptr;
1205 delete[] il_sim_uidptr;
1207 return types::Function::Error;
1211 * Set 'simpar' array
1213 for (int i = 0; i < m6; ++i)
1215 simpar[i] = il_tol->get(i);
1217 for (int i = m6; i < 7; ++i)
1222 /******************************
1223 * Cross variable size checking
1224 ******************************/
1228 err_check = 1; // tevts vs evtspt
1230 else if (m_xptr != m_zptr)
1232 err_check = 2; // xptr vs zptr
1234 else if (m_xptr != m_ozptr)
1236 err_check = 3; // xptr vs ozptr
1238 else if (m_xptr != m_zcptr)
1240 err_check = 4; // xptr vs zcptr
1242 else if (m_xptr != m_inpptr)
1244 err_check = 5; // xptr vs inpptr
1246 else if (m_xptr != m_outptr)
1248 err_check = 6; // xptr vs outptr
1250 else if (m_xptr != m_rpptr)
1252 err_check = 7; // xptr vs rpptr
1254 else if (m_xptr != m_ipptr)
1256 err_check = 8; // xptr vs ipptr
1258 else if (m_xptr != m_opptr)
1260 err_check = 8; // xptr vs opptr
1262 else if (m_xptr != m_clkptr)
1264 err_check = 10; // xptr vs clkptr
1266 else if ((n_ordclk != 2) & (m_ordclk != 0))
1268 err_check = 11; // sim.ordclk
1270 else if ((n_cord != 2) & (m_cord != 0))
1272 err_check = 12; // sim.cord
1274 else if ((n_oord != 2) & (m_oord != 0))
1276 err_check = 13; // sim.oord
1278 else if ((n_zord != 2) & (m_zord != 0))
1280 err_check = 14; // sim.zord
1282 else if ((n_iord != 2) & (m_iord != 0))
1284 err_check = 15; // sim.iord
1288 // Please write an error table here
1289 Scierror(42, _("%s : error in cross variable size checking : %d\n"), funname.data(), err_check);
1290 il_state->DecreaseRef();
1292 il_tcur->DecreaseRef();
1294 il_sim->DecreaseRef();
1296 delete[] il_sim_labptr;
1298 delete[] il_sim_uidptr;
1300 return types::Function::Error;
1303 /*******************************
1304 * Set function table for blocks
1305 *******************************/
1306 // Define new variable 'lfunpt'
1310 lfunpt = new void*[nblk];
1312 catch (const std::bad_alloc& /*e*/)
1314 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1315 il_state->DecreaseRef();
1317 il_tcur->DecreaseRef();
1319 il_sim->DecreaseRef();
1321 delete[] il_sim_labptr;
1323 delete[] il_sim_uidptr;
1325 return types::Function::Error;
1329 for (int i = 0; i < nblk; ++i) // For each block
1331 types::InternalType* pIT = il_sim_fun->get(i);
1332 // Block is defined by a Scilab function
1333 if (pIT->isCallable())
1335 lfunpt[i] = (void*)pIT;
1336 // Keep 'l_sim_funtyp' negative for Scilab macros
1337 if (l_sim_funtyp[i] > 0)
1339 l_sim_funtyp[i] *= -1;
1342 // Block is defined by a function described by a string
1343 else if (pIT->isString())
1345 types::String* funStr = pIT->getAs<types::String>();
1346 if (funStr->isScalar() == false)
1348 Scierror(999, _("%s: Wrong size for element #%d of input argument #%d : A scalar expected.\n"), funname.data(), i + 1, 4);
1349 il_state->DecreaseRef();
1351 il_tcur->DecreaseRef();
1353 il_sim->DecreaseRef();
1355 delete[] il_sim_labptr;
1357 delete[] il_sim_uidptr;
1360 return types::Function::Error;
1363 wchar_t* w_str = funStr->get(0);
1364 char* c_str = wide_string_to_UTF8(w_str);
1365 if (strcmp(c_str, "ifthel") == 0)
1367 l_sim_funtyp[i] = 11; // Magic value for "if-then-else" block
1369 else if (strcmp(c_str, "eselect") == 0)
1371 l_sim_funtyp[i] = 12; // Magic value for "eselect" block
1375 void* f = funnum2(c_str); // Search associated function number of function name
1376 // Block is defined by a C or Fortran function
1379 // C interface from "tabsim" defined in blocks.h
1381 if (l_sim_funtyp[i] < 0)
1383 // Keep 'l_sim_funtyp' positive for Fortran functions
1384 l_sim_funtyp[i] *= -1;
1387 // Block is defined by a predefined scilab function
1390 ConfigVariable::EntryPointStr* pEP = ConfigVariable::getEntryPoint(w_str);
1394 lfunpt[i] = (void*)pEP->functionPtr;
1398 types::InternalType* pMacro = symbol::Context::getInstance()->get(symbol::Symbol(w_str));
1399 if (pMacro && pMacro->isCallable())
1402 lfunpt[i] = (void*)pMacro;
1403 l_sim_funtyp[i] *= -1;
1407 Scierror(888, _("%s : unknown block : %s\n"), funname.data(), c_str);
1408 il_state->DecreaseRef();
1410 il_tcur->DecreaseRef();
1412 il_sim->DecreaseRef();
1414 delete[] il_sim_labptr;
1416 delete[] il_sim_uidptr;
1420 return types::Function::Error;
1429 Scierror(999, _("%s: Wrong type for element #%d of input argument #%d : string or macro expected.\n"), funname.data(), i + 1, 4);
1430 il_state->DecreaseRef();
1432 il_tcur->DecreaseRef();
1434 il_sim->DecreaseRef();
1436 delete[] il_sim_labptr;
1438 delete[] il_sim_uidptr;
1441 return types::Function::Error;
1445 /**********************
1446 * Set oz, ozsz, oztyp
1447 **********************/
1448 void** oz = nullptr;
1449 int* ozsz = nullptr;
1450 int* oztyp = nullptr;
1453 // Allocation of 'oz'
1456 oz = new void*[noz];
1458 catch (const std::bad_alloc& /*e*/)
1460 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1461 il_state->DecreaseRef();
1463 il_tcur->DecreaseRef();
1465 il_sim->DecreaseRef();
1467 delete[] il_sim_labptr;
1469 delete[] il_sim_uidptr;
1472 return types::Function::Error;
1474 // Allocation of 'ozsz'
1477 ozsz = new int[2 * noz];
1479 catch (const std::bad_alloc& /*e*/)
1481 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1482 il_state->DecreaseRef();
1484 il_tcur->DecreaseRef();
1486 il_sim->DecreaseRef();
1488 delete[] il_sim_labptr;
1490 delete[] il_sim_uidptr;
1494 return types::Function::Error;
1496 // Allocation of 'oztyp'
1499 oztyp = new int[noz];
1501 catch (const std::bad_alloc& /*e*/)
1503 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1504 il_state->DecreaseRef();
1506 il_tcur->DecreaseRef();
1508 il_sim->DecreaseRef();
1510 delete[] il_sim_labptr;
1512 delete[] il_sim_uidptr;
1517 return types::Function::Error;
1520 // Set vectors of 'oz'
1521 for (int j = 0; j < noz; ++j)
1523 int subtype = il_state_oz->get(j)->getType();
1525 switch (subtype) // Store type and address
1527 case types::InternalType::ScilabDouble :
1529 types::Double* ozDouble = il_state_oz->get(j)->getAs<types::Double>();
1530 if (ozDouble->isComplex() == false)
1532 oztyp[j] = SCSREAL_N; // Double real matrix
1533 oz[j] = (SCSREAL_COP *) ozDouble->get();
1537 oztyp[j] = SCSCOMPLEX_N; // Double complex matrix
1538 oz[j] = (SCSCOMPLEX_COP *) ozDouble->get();
1540 ozsz[j] = ozDouble->getRows();
1541 ozsz[j + noz] = ozDouble->getCols();
1544 case types::InternalType::ScilabInt8 :
1546 types::Int8* ozInt8 = il_state_oz->get(j)->getAs<types::Int8>();
1547 oztyp[j] = SCSINT8_N; // int8
1548 ozsz[j] = ozInt8->getRows();
1549 ozsz[j + noz] = ozInt8->getCols();
1550 oz[j] = (SCSINT8_COP *) ozInt8->get();
1553 case types::InternalType::ScilabInt16 :
1555 types::Int16* ozInt16 = il_state_oz->get(j)->getAs<types::Int16>();
1556 oztyp[j] = SCSINT16_N; // int16
1557 ozsz[j] = ozInt16->getRows();
1558 ozsz[j + noz] = ozInt16->getCols();
1559 oz[j] = (SCSINT16_COP *) ozInt16->get();
1562 case types::InternalType::ScilabInt32 :
1564 types::Int32* ozInt32 = il_state_oz->get(j)->getAs<types::Int32>();
1565 oztyp[j] = SCSINT32_N; // int32
1566 ozsz[j] = ozInt32->getRows();
1567 ozsz[j + noz] = ozInt32->getCols();
1568 oz[j] = (SCSINT32_COP *) ozInt32->get();
1571 case types::InternalType::ScilabUInt8 :
1573 types::UInt8* ozUInt8 = il_state_oz->get(j)->getAs<types::UInt8>();
1574 oztyp[j] = SCSUINT8_N; // uint8
1575 ozsz[j] = ozUInt8->getRows();
1576 ozsz[j + noz] = ozUInt8->getCols();
1577 oz[j] = (SCSUINT8_COP *) ozUInt8->get();
1580 case types::InternalType::ScilabUInt16 :
1582 types::UInt16* ozUInt16 = il_state_oz->get(j)->getAs<types::UInt16>();
1583 oztyp[j] = SCSUINT16_N; // uint16
1584 ozsz[j] = ozUInt16->getRows();
1585 ozsz[j + noz] = ozUInt16->getCols();
1586 oz[j] = (SCSUINT16_COP *) ozUInt16->get();
1589 case types::InternalType::ScilabUInt32 :
1591 types::UInt32* ozUInt32 = il_state_oz->get(j)->getAs<types::UInt32>();
1592 oztyp[j] = SCSUINT32_N; // uint32
1593 ozsz[j] = ozUInt32->getRows();
1594 ozsz[j + noz] = ozUInt32->getCols();
1595 oz[j] = (SCSUINT32_COP *) ozUInt32->get();
1600 Scierror(999, _("%s: Wrong type for element #%d of element #%d of argument #%d : A matrix expected.\n"), funname.data(), j + 1, 4, 1);
1601 il_state->DecreaseRef();
1603 il_tcur->DecreaseRef();
1605 il_sim->DecreaseRef();
1607 delete[] il_sim_labptr;
1609 delete[] il_sim_uidptr;
1615 return types::Function::Error;
1621 /****************************
1622 * Set opar, oparsz, opartyp
1623 ****************************/
1624 void** opar = nullptr;
1625 int* oparsz = nullptr;
1626 int* opartyp = nullptr;
1629 // Allocation of 'opar'
1632 opar = new void*[nopar];
1634 catch (const std::bad_alloc& /*e*/)
1636 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1637 il_state->DecreaseRef();
1639 il_tcur->DecreaseRef();
1641 il_sim->DecreaseRef();
1643 delete[] il_sim_labptr;
1645 delete[] il_sim_uidptr;
1651 return types::Function::Error;
1653 // Allocation of 'oparsz'
1656 oparsz = new int[2 * nopar];
1658 catch (const std::bad_alloc& /*e*/)
1660 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1661 il_state->DecreaseRef();
1663 il_tcur->DecreaseRef();
1665 il_sim->DecreaseRef();
1667 delete[] il_sim_labptr;
1669 delete[] il_sim_uidptr;
1676 return types::Function::Error;
1678 // Allocation of 'opartyp'
1681 opartyp = new int[nopar];
1683 catch (const std::bad_alloc& /*e*/)
1685 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1686 il_state->DecreaseRef();
1688 il_tcur->DecreaseRef();
1690 il_sim->DecreaseRef();
1692 delete[] il_sim_labptr;
1694 delete[] il_sim_uidptr;
1702 return types::Function::Error;
1705 // Set vectors of 'opar'
1706 for (int j = 0; j < nopar; ++j)
1708 int subtype = il_sim_opar->get(j)->getType();
1710 switch (subtype) // Store type and address
1712 case types::InternalType::ScilabDouble :
1714 types::Double* oparDouble = il_sim_opar->get(j)->getAs<types::Double>();
1715 if (oparDouble->isComplex() == false)
1717 opartyp[j] = SCSREAL_N; // Double real matrix
1718 opar[j] = (SCSREAL_COP *) oparDouble->get();
1722 opartyp[j] = SCSCOMPLEX_N; // Double complex matrix
1723 // Allocate a long array in order to make the real and complex parts contiguous (oparDouble->get() and oparDouble->getImg())
1726 opar[j] = new double[2 * oparDouble->getSize()];
1728 catch (const std::bad_alloc& /*e*/)
1730 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1731 il_state->DecreaseRef();
1733 il_tcur->DecreaseRef();
1735 il_sim->DecreaseRef();
1737 delete[] il_sim_labptr;
1739 delete[] il_sim_uidptr;
1748 return types::Function::Error;
1750 memcpy(opar[j], oparDouble->get(), oparDouble->getSize() * sizeof(double)); // Real part
1751 memcpy((double*)(opar[j]) + oparDouble->getSize(), oparDouble->getImg(), oparDouble->getSize() * sizeof(double)); // Complex part
1752 // FIXME: delete oparDouble because we copied it instead of using it?
1754 oparsz[j] = oparDouble->getRows();
1755 oparsz[j + nopar] = oparDouble->getCols();
1758 case types::InternalType::ScilabInt8 :
1760 types::Int8* oparInt8 = il_sim_opar->get(j)->getAs<types::Int8>();
1761 opartyp[j] = SCSINT8_N; // int8
1762 oparsz[j] = oparInt8->getRows();
1763 oparsz[j + nopar] = oparInt8->getCols();
1764 opar[j] = (SCSINT8_COP *) oparInt8->get();
1767 case types::InternalType::ScilabInt16 :
1769 types::Int16* oparInt16 = il_sim_opar->get(j)->getAs<types::Int16>();
1770 opartyp[j] = SCSINT16_N; // int16
1771 oparsz[j] = oparInt16->getRows();
1772 oparsz[j + nopar] = oparInt16->getCols();
1773 opar[j] = (SCSINT16_COP *) oparInt16->get();
1776 case types::InternalType::ScilabInt32 :
1778 types::Int32* oparInt32 = il_sim_opar->get(j)->getAs<types::Int32>();
1779 opartyp[j] = SCSINT32_N; // int32
1780 oparsz[j] = oparInt32->getRows();
1781 oparsz[j + nopar] = oparInt32->getCols();
1782 opar[j] = (SCSINT32_COP *) oparInt32->get();
1785 case types::InternalType::ScilabUInt8 :
1787 types::UInt8* oparUInt8 = il_sim_opar->get(j)->getAs<types::UInt8>();
1788 opartyp[j] = SCSUINT8_N; // uint8
1789 oparsz[j] = oparUInt8->getRows();
1790 oparsz[j + nopar] = oparUInt8->getCols();
1791 opar[j] = (SCSUINT8_COP *) oparUInt8->get();
1794 case types::InternalType::ScilabUInt16 :
1796 types::UInt16* oparUInt16 = il_sim_opar->get(j)->getAs<types::UInt16>();
1797 opartyp[j] = SCSUINT16_N; // uint16
1798 oparsz[j] = oparUInt16->getRows();
1799 oparsz[j + nopar] = oparUInt16->getCols();
1800 opar[j] = (SCSUINT16_COP *) oparUInt16->get();
1803 case types::InternalType::ScilabUInt32 :
1805 types::UInt32* oparUInt32 = il_sim_opar->get(j)->getAs<types::UInt32>();
1806 opartyp[j] = SCSUINT32_N; // uint32
1807 oparsz[j] = oparUInt32->getRows();
1808 oparsz[j + nopar] = oparUInt32->getCols();
1809 opar[j] = (SCSUINT32_COP *) oparUInt32->get();
1814 Scierror(999, _("%s: Wrong type for element #%d of element #%d of argument #%d : A matrix expected.\n"), funname.data(), j + 1, 15, 4);
1815 il_state->DecreaseRef();
1817 il_tcur->DecreaseRef();
1819 il_sim->DecreaseRef();
1821 delete[] il_sim_labptr;
1823 delete[] il_sim_uidptr;
1832 return types::Function::Error;
1838 // Declaration of 'outtb_elem'
1839 outtb_el* outtb_elem = nullptr;
1842 /*******************************
1843 * Set outtbptr, outtbsz, outtbtyp
1844 *******************************/
1845 void** outtbptr = nullptr;
1846 int* outtbsz = nullptr;
1847 int* outtbtyp = nullptr;
1850 // Allocation of 'outtbptr'
1853 outtbptr = new void*[nlnk];
1855 catch (const std::bad_alloc& /*e*/)
1857 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1858 il_state->DecreaseRef();
1860 il_tcur->DecreaseRef();
1862 il_sim->DecreaseRef();
1864 delete[] il_sim_labptr;
1866 delete[] il_sim_uidptr;
1875 return types::Function::Error;
1877 // Allocation of 'outtbptrsz'
1880 outtbsz = new int[2 * nlnk];
1882 catch (const std::bad_alloc& /*e*/)
1884 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1885 il_state->DecreaseRef();
1887 il_tcur->DecreaseRef();
1889 il_sim->DecreaseRef();
1891 delete[] il_sim_labptr;
1893 delete[] il_sim_uidptr;
1903 return types::Function::Error;
1905 // Allocation of 'outtbtyp'
1908 outtbtyp = new int[nlnk];
1910 catch (const std::bad_alloc& /*e*/)
1912 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1913 il_state->DecreaseRef();
1915 il_tcur->DecreaseRef();
1917 il_sim->DecreaseRef();
1919 delete[] il_sim_labptr;
1921 delete[] il_sim_uidptr;
1932 return types::Function::Error;
1935 // Set vectors of 'outtbptr'
1936 for (int j = 0; j < nlnk; ++j)
1938 int subtype = il_state_outtb->get(j)->getType();
1940 switch (subtype) // Store type and address
1942 case types::InternalType::ScilabDouble :
1944 types::Double* outtbDouble = il_state_outtb->get(j)->getAs<types::Double>();
1945 if (outtbDouble->isComplex() == false)
1947 outtbtyp[j] = SCSREAL_N; // Double real matrix
1948 outtbptr[j] = (SCSREAL_COP *) outtbDouble->get();
1952 outtbtyp[j] = SCSCOMPLEX_N; // Double complex matrix
1953 // Allocate a long array in order to make the real and complex parts contiguous (outtbDouble->get() and outtbDouble->getImg())
1956 outtbptr[j] = new double[2 * outtbDouble->getSize()];
1958 catch (const std::bad_alloc& /*e*/)
1960 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1961 il_state->DecreaseRef();
1963 il_tcur->DecreaseRef();
1965 il_sim->DecreaseRef();
1967 delete[] il_sim_labptr;
1969 delete[] il_sim_uidptr;
1980 return types::Function::Error;
1982 memcpy(outtbptr[j], outtbDouble->get(), outtbDouble->getSize() * sizeof(double)); // Real part
1983 memcpy((double*)(outtbptr[j]) + outtbDouble->getSize(), outtbDouble->getImg(), outtbDouble->getSize() * sizeof(double)); // Complex part
1984 // FIXME: delete outtbDouble because we copied it instead of using it?
1986 outtbsz[j] = outtbDouble->getRows();
1987 outtbsz[j + nlnk] = outtbDouble->getCols();
1990 case types::InternalType::ScilabInt8 :
1992 types::Int8* outtbInt8 = il_state_outtb->get(j)->getAs<types::Int8>();
1993 outtbtyp[j] = SCSINT8_N; // int8
1994 outtbsz[j] = outtbInt8->getRows();
1995 outtbsz[j + nlnk] = outtbInt8->getCols();
1996 outtbptr[j] = (SCSINT8_COP *) outtbInt8->get();
1999 case types::InternalType::ScilabInt16 :
2001 types::Int16* outtbInt16 = il_state_outtb->get(j)->getAs<types::Int16>();
2002 outtbtyp[j] = SCSINT16_N; // int16
2003 outtbsz[j] = outtbInt16->getRows();
2004 outtbsz[j + nlnk] = outtbInt16->getCols();
2005 outtbptr[j] = (SCSINT16_COP *) outtbInt16->get();
2008 case types::InternalType::ScilabInt32 :
2010 types::Int32* outtbInt32 = il_state_outtb->get(j)->getAs<types::Int32>();
2011 outtbtyp[j] = SCSINT32_N; // int32
2012 outtbsz[j] = outtbInt32->getRows();
2013 outtbsz[j + nlnk] = outtbInt32->getCols();
2014 outtbptr[j] = (SCSINT32_COP *) outtbInt32->get();
2017 case types::InternalType::ScilabUInt8 :
2019 types::UInt8* outtbUInt8 = il_state_outtb->get(j)->getAs<types::UInt8>();
2020 outtbtyp[j] = SCSUINT8_N; // uint8
2021 outtbsz[j] = outtbUInt8->getRows();
2022 outtbsz[j + nlnk] = outtbUInt8->getCols();
2023 outtbptr[j] = (SCSUINT8_COP *) outtbUInt8->get();
2026 case types::InternalType::ScilabUInt16 :
2028 types::UInt16* outtbUInt16 = il_state_outtb->get(j)->getAs<types::UInt16>();
2029 outtbtyp[j] = SCSUINT16_N; // uint16
2030 outtbsz[j] = outtbUInt16->getRows();
2031 outtbsz[j + nlnk] = outtbUInt16->getCols();
2032 outtbptr[j] = (SCSUINT16_COP *) outtbUInt16->get();
2035 case types::InternalType::ScilabUInt32 :
2037 types::UInt32* outtbUInt32 = il_state_outtb->get(j)->getAs<types::UInt32>();
2038 outtbtyp[j] = SCSUINT32_N; // uint32
2039 outtbsz[j] = outtbUInt32->getRows();
2040 outtbsz[j + nlnk] = outtbUInt32->getCols();
2041 outtbptr[j] = (SCSUINT32_COP *) outtbUInt32->get();
2046 Scierror(999, _("%s: Wrong type for element #%d of element #%d of argument #%d : A matrix expected.\n"), funname.data(), j + 1, 9, 1);
2047 il_state->DecreaseRef();
2049 il_tcur->DecreaseRef();
2051 il_sim->DecreaseRef();
2053 delete[] il_sim_labptr;
2055 delete[] il_sim_uidptr;
2068 return types::Function::Error;
2072 // Store 'lnk' and 'pos' in 'outtb_elem'
2074 nelem += outtbsz[j] * outtbsz[j + nlnk];
2075 if ((outtb_elem = (outtb_el *) REALLOC(outtb_elem, nelem * sizeof(outtb_el))) == nullptr)
2077 Scierror(999, _("%s : No more free memory.\n"), funname.data());
2078 il_state->DecreaseRef();
2080 il_tcur->DecreaseRef();
2082 il_sim->DecreaseRef();
2084 delete[] il_sim_labptr;
2086 delete[] il_sim_uidptr;
2099 return types::Function::Error;
2101 for (int i = 0; i < outtbsz[j]*outtbsz[j + nlnk]; ++i)
2103 outtb_elem[k + i].lnk = j;
2104 outtb_elem[k + i].pos = i;
2109 /************************
2110 * Call scicos simulator
2111 ************************/
2113 * int C2F(scicos)(double *x_in, int *xptr_in, double *z__,
2114 * void **work,int *zptr,int *modptr_in,
2115 * void **oz,int *ozsz,int *oztyp,int *ozptr,
2116 * char **iz,int *izptr,double *t0_in,
2117 * double *tf_in,double *tevts_in,int *evtspt_in,
2118 * int *nevts,int *pointi_in,void **outtbptr_in,
2119 * int *outtbsz_in,int *outtbtyp_in,
2120 * outtb_el *outtb_elem_in,int *nelem1,int *nlnk1,
2121 * int *funptr,int *funtyp_in,int *inpptr_in,
2122 * int *outptr_in, int *inplnk_in,int *outlnk_in,
2123 * double *rpar,int *rpptr,int *ipar,int *ipptr,
2124 * void **opar,int *oparsz,int *opartyp,int *opptr,
2125 * int *clkptr_in,int *ordptr_in,int *nordptr1,
2126 * int *ordclk_in,int *cord_in,int *ncord1,
2127 * int *iord_in,int *niord1,int *oord_in,
2128 * int *noord1,int *zord_in,int *nzord1,
2129 * int *critev_in,int *nblk1,int *ztyp,
2130 * int *zcptr_in,int *subscr,int *nsubs,
2131 * double *simpar,int *flag__,int *ierr_out)
2135 C2F(scicos)(l_state_x, l_sim_xptr, l_state_z,
2136 l_state_iz, l_sim_zptr, l_sim_modptr,
2137 oz, ozsz, oztyp, l_sim_ozptr,
2138 l_sim_lab, il_sim_labptr, l_sim_uid, il_sim_uidptr, l_tcur,
2139 l_tf, l_state_tevts, l_state_evtspt,
2140 &m1e5, l_pointi, outtbptr,
2142 outtb_elem, &nelem, &nlnk,
2143 lfunpt, l_sim_funtyp, l_sim_inpptr,
2144 l_sim_outptr, l_sim_inplnk, l_sim_outlnk,
2145 l_sim_rpar, l_sim_rpptr, l_sim_ipar, l_sim_ipptr,
2146 opar, oparsz, opartyp, l_sim_opptr,
2147 l_sim_clkptr, l_sim_ordptr, &m_ordptr,
2148 l_sim_ordclk, l_sim_cord, &m_cord,
2149 l_sim_iord, &m_iord, l_sim_oord,
2150 &m_oord, l_sim_zord, &m_zord,
2151 l_sim_critev, &nblk, l_sim_ztyp,
2152 l_sim_zcptr, l_sim_subscr, &m_subscr,
2153 simpar, &flag, &ierr);
2155 /**********************
2156 * Free allocated arrays
2157 **********************/
2170 /*************************************
2171 * Switch to appropriate message error
2172 *************************************/
2179 error = _("scheduling problem");
2180 C2F(curblk).kfun = 0;
2184 error = _("input to zero-crossing stuck on zero");
2185 C2F(curblk).kfun = 0;
2189 error = _("event conflict");
2190 C2F(curblk).kfun = 0;
2194 error = _("algebraic loop detected");
2195 C2F(curblk).kfun = 0;
2199 error = _("cannot allocate memory");
2200 C2F(curblk).kfun = 0;
2206 error = new char[MAX_ERROR_LEN];
2207 allocatedError = TRUE;
2208 snprintf(error, MAX_ERROR_LEN, _("the block %d has been called with input out of its domain"), C2F(curblk).kfun);
2210 catch (const std::bad_alloc& /*e*/)
2212 error = _("cannot allocate memory");
2217 error = _("singularity in a block");
2221 error = _("block produces an internal error");
2229 error = _("initial conditions not converging");
2235 error = new char[MAX_ERROR_LEN];
2236 allocatedError = TRUE;
2237 snprintf(error, MAX_ERROR_LEN, _("cannot allocate memory in block=%d"), C2F(curblk).kfun);
2239 catch (const std::bad_alloc& /*e*/)
2241 error = _("cannot allocate memory");
2246 error = _("sliding mode condition, cannot integrate");
2250 error = _("Cannot find the initial mode, maybe there is a sliding mode condition");
2253 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.");
2257 error = _("Undefined data type.");
2260 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");
2262 // 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
2267 error = _("unknown or erroneous block");
2269 else if (ierr >= 201 && ierr <= 416) // Sundials error messages, stored in coserr.buf
2273 else if (ierr >= 100)
2275 int istate = -(ierr - 100);
2278 error = new char[MAX_ERROR_LEN];
2279 allocatedError = TRUE;
2280 snprintf(error, MAX_ERROR_LEN, _("integration problem istate=%d"), istate);
2282 catch (const std::bad_alloc& /*e*/)
2284 error = _("cannot allocate memory");
2286 C2F(curblk).kfun = 0;
2290 error = _("scicos unexpected error, please report...");
2291 C2F(curblk).kfun = 0;
2296 Scierror(888, "%s\n", error);
2297 il_state->DecreaseRef();
2299 il_tcur->DecreaseRef();
2301 il_sim->DecreaseRef();
2303 delete[] il_sim_labptr;
2305 delete[] il_sim_uidptr;
2311 return types::Function::Error;
2314 /*********************
2315 * Return Lhs variables
2316 *********************/
2317 // Convert integer parameters of 'il_state' into double parameters
2318 il_state_evtspt->convertFromInteger();
2319 il_pointi->convertFromInteger();
2321 out.push_back(il_state);
2322 out.push_back(il_tcur);
2325 il_sim->DecreaseRef();
2327 delete[] il_sim_labptr;
2329 delete[] il_sim_uidptr;
2331 return types::Function::OK;