2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2014 - Scilab Enterprises - Paul Bignier
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
18 #include "gw_scicos.hxx"
20 #include "internal.hxx"
22 #include "context.hxx"
24 #include "function.hxx"
31 #include "il_state.hxx"
36 #include "machine.h" /* C2F */
38 #include "sci_malloc.h"
39 #include "scicos.h" /* funnum2() */
40 #include "scicos-def.h"
41 #include "charEncoding.h"
43 #include "localization.h"
46 COSIM_struct C2F(cosim);
47 /*--------------------------------------------------------------------------*/
48 // Variables defined in scicos.c
49 extern COSERR_struct coserr;
50 extern CURBLK_struct C2F(curblk);
51 /*--------------------------------------------------------------------------*/
55 #define snprintf _snprintf
58 /*--------------------------------------------------------------------------*/
59 /* intsicosimc scicosim interface routine.
61 * [state,t] = scicosim(state,tcur,tf,sim,str,tol)
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
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
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]]'
119 * 16/03/06, A.Layec : Rewritten from original fortran
120 * source code intsscicos in intcos.f.
122 * 29/03/06, Alan : Improvement in accordance to c_pass2
123 * (int32 parameters).
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).
130 * 14/06/06, Alan : Save common intersci before calling scicos
131 * (to disable scilab crash with scifunc.f).
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.
137 * 15/12/06, Alan : Warnings compilation removed.
138 * This can crash scilab/scicos.
141 * xx/02/07, Alan : Add opar/odstate : scilab lists of arbitrary object
144 * 08/12/14, Paul : Rewrite to C++.
147 /*--------------------------------------------------------------------------*/
149 static const std::string funname = "scicosim";
151 types::Function::ReturnValue sci_scicosim(types::typed_list &in, int _iRetCount, types::typed_list &out)
153 /************************************
154 * Variables and constants definition
155 ************************************/
158 BOOL allocatedError = FALSE;
159 const int MAX_ERROR_LEN = 512;
163 Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), funname.data(), 6);
164 return types::Function::Error;
169 Scierror(78, _("%s: Wrong number of output argument(s): %d to %d expected.\n"), funname.data(), 1, 2);
170 return types::Function::Error;
176 if (in[0]->isTList() == false)
178 Scierror(999, _("%s: Wrong type for input argument #%d : A tlist expected.\n"), funname.data(), 1);
179 return types::Function::Error;
181 types::TList* il_state_input = in[0]->getAs<types::TList>();
182 types::TList* il_state = new types::TList();
184 if (il_state_input->getSize() < 9)
186 Scierror(999, _("%s: Wrong size for input argument #%d : %d elements expected.\n"), funname.data(), 1, 9);
187 il_state->DecreaseRef();
189 return types::Function::Error;
192 // Make a copy of 'il_state' in a global variabe
193 set_il_state(il_state_input);
195 types::String* header_state = il_state_input->get(0)->getAs<types::String>();
196 il_state->append(header_state->clone());
199 if (il_state_input->get(1)->isDouble() == false)
201 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 2, 1);
202 il_state->DecreaseRef();
204 return types::Function::Error;
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();
212 if (il_state_input->get(2)->isDouble() == false)
214 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 3, 1);
215 il_state->DecreaseRef();
217 return types::Function::Error;
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();
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)
229 types::InternalType* l_state_oz_input = il_state_oz_input->get(i);
230 il_state_oz->append(l_state_oz_input->clone());
232 il_state->append(il_state_oz);
233 int noz = il_state_oz->getSize(); // 'nlnk' is the dimension of the list 'state.oz'
236 if (il_state_input->get(4)->isDouble() == false)
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;
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();
248 if (il_state_input->get(5)->isDouble() == false)
250 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 6, 1);
251 il_state->DecreaseRef();
253 return types::Function::Error;
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();
261 /*7 : state.evtspt */
262 if (il_state_input->get(6)->isDouble() == false)
264 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 7, 1);
265 il_state->DecreaseRef();
267 return types::Function::Error;
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();
276 /*8 : state.pointi */
277 if (il_state_input->get(7)->isDouble() == false)
279 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 8, 1);
280 il_state->DecreaseRef();
282 return types::Function::Error;
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();
291 if (il_state_input->get(8)->isList() == false)
293 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A list expected.\n"), funname.data(), 9, 1);
294 il_state->DecreaseRef();
296 return types::Function::Error;
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)
302 types::InternalType* l_state_outtb_input = il_state_outtb_input->get(i);
303 il_state_outtb->append(l_state_outtb_input->clone());
305 il_state->append(il_state_outtb);
306 int nlnk = il_state_outtb->getSize(); // 'nlnk' is the dimension of the list 'state.outtb'
311 if (in[1]->isDouble() == false)
313 Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 2);
314 il_state->DecreaseRef();
316 return types::Function::Error;
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)
322 Scierror(999, _("%s: Wrong size for input argument #%d : A scalar expected.\n"), funname.data(), 2);
323 il_state->DecreaseRef();
325 il_tcur->DecreaseRef();
327 return types::Function::Error;
329 double* l_tcur = il_tcur->get();
334 if (in[2]->isDouble() == false)
336 Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 3);
337 il_state->DecreaseRef();
339 il_tcur->DecreaseRef();
341 return types::Function::Error;
343 types::Double* il_tf = in[2]->getAs<types::Double>();
344 if (il_tf->isScalar() == false)
346 Scierror(999, _("%s: Wrong size for input argument #%d : A scalar expected.\n"), funname.data(), 3);
347 il_state->DecreaseRef();
349 il_tcur->DecreaseRef();
351 return types::Function::Error;
353 double* l_tf = il_tf->get();
358 if (in[3]->isTList() == false)
360 Scierror(999, _("%s: Wrong type for input argument #%d : A tlist expected.\n"), funname.data(), 4);
361 il_state->DecreaseRef();
363 il_tcur->DecreaseRef();
365 return types::Function::Error;
367 types::TList* il_sim_input = in[3]->getAs<types::TList>();
368 types::TList* il_sim = new types::TList();
370 // Make a copy of 'il_sim' in a global variabe
371 set_il_sim(il_sim_input);
373 if (il_sim_input->getSize() < 34)
375 Scierror(999, _("%s: Wrong size for input argument #%d : %d elements expected.\n"), funname.data(), 4, 34);
376 il_state->DecreaseRef();
378 il_tcur->DecreaseRef();
380 il_sim->DecreaseRef();
382 return types::Function::Error;
385 types::String* header_sim = il_sim_input->get(0)->getAs<types::String>();
386 il_sim->append(header_sim->clone());
389 if (il_sim_input->get(1)->isList() == false)
391 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A list expected.\n"), funname.data(), 2, 4);
392 il_state->DecreaseRef();
394 il_tcur->DecreaseRef();
396 il_sim->DecreaseRef();
398 return types::Function::Error;
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)
404 types::InternalType* l_sim_fun_input = il_sim_fun_input->get(i);
405 il_sim_fun->append(l_sim_fun_input->clone());
407 il_sim->append(il_sim_fun);
408 int nblk = il_sim_fun->getSize(); // 'nblk' is the dimension of the list 'sim.funs'
411 if (il_sim_input->get(2)->isDouble() == false)
413 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 3, 4);
414 il_state->DecreaseRef();
416 il_tcur->DecreaseRef();
418 il_sim->DecreaseRef();
420 return types::Function::Error;
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();
430 if (il_sim_input->get(3)->isDouble() == false)
432 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 4, 4);
433 il_state->DecreaseRef();
435 il_tcur->DecreaseRef();
437 il_sim->DecreaseRef();
439 return types::Function::Error;
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();
449 if (il_sim_input->get(4)->isDouble() == false)
451 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 5, 4);
452 il_state->DecreaseRef();
454 il_tcur->DecreaseRef();
456 il_sim->DecreaseRef();
458 return types::Function::Error;
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();
468 if (il_sim_input->get(5)->isDouble() == false)
470 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 6, 4);
471 il_state->DecreaseRef();
473 il_tcur->DecreaseRef();
475 il_sim->DecreaseRef();
477 return types::Function::Error;
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();
487 if (il_sim_input->get(6)->isDouble() == false)
489 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 7, 4);
490 il_state->DecreaseRef();
492 il_tcur->DecreaseRef();
494 il_sim->DecreaseRef();
496 return types::Function::Error;
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();
506 if (il_sim_input->get(7)->isDouble() == false)
508 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 8, 4);
509 il_state->DecreaseRef();
511 il_tcur->DecreaseRef();
513 il_sim->DecreaseRef();
515 return types::Function::Error;
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();
525 if (il_sim_input->get(8)->isDouble() == false)
527 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 9, 4);
528 il_state->DecreaseRef();
530 il_tcur->DecreaseRef();
532 il_sim->DecreaseRef();
534 return types::Function::Error;
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();
543 if (il_sim_input->get(9)->isDouble() == false)
545 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 10, 4);
546 il_state->DecreaseRef();
548 il_tcur->DecreaseRef();
550 il_sim->DecreaseRef();
552 return types::Function::Error;
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();
561 if (il_sim_input->get(10)->isDouble() == false)
563 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 11, 4);
564 il_state->DecreaseRef();
566 il_tcur->DecreaseRef();
568 il_sim->DecreaseRef();
570 return types::Function::Error;
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();
578 if (il_sim_input->get(11)->isDouble() == false)
580 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 12, 4);
581 il_state->DecreaseRef();
583 il_tcur->DecreaseRef();
585 il_sim->DecreaseRef();
587 return types::Function::Error;
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();
597 if (il_sim_input->get(12)->isDouble() == false)
599 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 13, 4);
600 il_state->DecreaseRef();
602 il_tcur->DecreaseRef();
604 il_sim->DecreaseRef();
606 return types::Function::Error;
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();
615 if (il_sim_input->get(13)->isDouble() == false)
617 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 14, 4);
618 il_state->DecreaseRef();
620 il_tcur->DecreaseRef();
622 il_sim->DecreaseRef();
624 return types::Function::Error;
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();
634 if (il_sim_input->get(14)->isList() == false)
636 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A list expected.\n"), funname.data(), 15, 4);
637 il_state->DecreaseRef();
639 il_tcur->DecreaseRef();
641 il_sim->DecreaseRef();
643 return types::Function::Error;
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)
649 types::InternalType* l_sim_opar_input = il_sim_opar_input->get(i);
650 il_sim_opar->append(l_sim_opar_input->clone());
652 il_sim->append(il_sim_opar);
653 int nopar = il_sim_opar->getSize(); // 'nopar' is the dimension of the list 'sim.opar'
656 if (il_sim_input->get(15)->isDouble() == false)
658 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 16, 4);
659 il_state->DecreaseRef();
661 il_tcur->DecreaseRef();
663 il_sim->DecreaseRef();
665 return types::Function::Error;
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();
675 if (il_sim_input->get(16)->isDouble() == false)
677 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 17, 4);
678 il_state->DecreaseRef();
680 il_tcur->DecreaseRef();
682 il_sim->DecreaseRef();
684 return types::Function::Error;
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();
694 if (il_sim_input->get(17)->isDouble() == false)
696 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 18, 4);
697 il_state->DecreaseRef();
699 il_tcur->DecreaseRef();
701 il_sim->DecreaseRef();
703 return types::Function::Error;
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();
713 if (il_sim_input->get(18)->isDouble() == false)
715 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 19, 4);
716 il_state->DecreaseRef();
718 il_tcur->DecreaseRef();
720 il_sim->DecreaseRef();
722 return types::Function::Error;
724 types::Double* il_sim_execlk_input = il_sim_input->get(18)->getAs<types::Double>();
725 il_sim->append(il_sim_execlk_input->clone());
728 if (il_sim_input->get(19)->isDouble() == false)
730 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 20, 4);
731 il_state->DecreaseRef();
733 il_tcur->DecreaseRef();
735 il_sim->DecreaseRef();
737 return types::Function::Error;
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();
748 if (il_sim_input->get(20)->isDouble() == false)
750 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 21, 4);
751 il_state->DecreaseRef();
753 il_tcur->DecreaseRef();
755 il_sim->DecreaseRef();
757 return types::Function::Error;
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();
768 if (il_sim_input->get(21)->isDouble() == false)
770 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 22, 4);
771 il_state->DecreaseRef();
773 il_tcur->DecreaseRef();
775 il_sim->DecreaseRef();
777 return types::Function::Error;
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();
788 if (il_sim_input->get(22)->isDouble() == false)
790 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 23, 4);
791 il_state->DecreaseRef();
793 il_tcur->DecreaseRef();
795 il_sim->DecreaseRef();
797 return types::Function::Error;
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();
808 if (il_sim_input->get(23)->isDouble() == false)
810 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 24, 4);
811 il_state->DecreaseRef();
813 il_tcur->DecreaseRef();
815 il_sim->DecreaseRef();
817 return types::Function::Error;
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();
826 if (il_sim_input->get(24)->isDouble() == false)
828 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 25, 4);
829 il_state->DecreaseRef();
831 il_tcur->DecreaseRef();
833 il_sim->DecreaseRef();
835 return types::Function::Error;
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();
842 if (static_cast<int>(l_sim_nb[0]) != nblk) // Value of 'nb' must be equal to 'nblk'
844 Scierror(42, _("%s : Incompatible sim.nb RHS parameter.\n"), funname.data());
845 il_state->DecreaseRef();
847 il_tcur->DecreaseRef();
849 il_sim->DecreaseRef();
851 return types::Function::Error;
855 if (il_sim_input->get(25)->isDouble() == false)
857 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 26, 4);
858 il_state->DecreaseRef();
860 il_tcur->DecreaseRef();
862 il_sim->DecreaseRef();
864 return types::Function::Error;
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();
873 if (il_sim_input->get(26)->isDouble() == false)
875 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 27, 4);
876 il_state->DecreaseRef();
878 il_tcur->DecreaseRef();
880 il_sim->DecreaseRef();
882 return types::Function::Error;
884 types::Double* il_sim_nblk_input = il_sim_input->get(26)->getAs<types::Double>();
885 il_sim->append(il_sim_nblk_input->clone());
888 if (il_sim_input->get(27)->isDouble() == false)
890 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 28, 4);
891 il_state->DecreaseRef();
893 il_tcur->DecreaseRef();
895 il_sim->DecreaseRef();
897 return types::Function::Error;
899 types::Double* il_sim_ndcblk_input = il_sim_input->get(27)->getAs<types::Double>();
900 il_sim->append(il_sim_ndcblk_input->clone());
903 if (il_sim_input->get(28)->isDouble() == false)
905 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 29, 4);
906 il_state->DecreaseRef();
908 il_tcur->DecreaseRef();
910 il_sim->DecreaseRef();
912 return types::Function::Error;
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();
922 if (il_sim_input->get(29)->isDouble() == false)
924 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 30, 4);
925 il_state->DecreaseRef();
927 il_tcur->DecreaseRef();
929 il_sim->DecreaseRef();
931 return types::Function::Error;
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();
940 if (il_sim_input->get(30)->isDouble() == false)
942 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 31, 4);
943 il_state->DecreaseRef();
945 il_tcur->DecreaseRef();
947 il_sim->DecreaseRef();
949 return types::Function::Error;
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();
960 if (il_sim_input->get(31)->isString() == false)
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();
965 il_tcur->DecreaseRef();
967 il_sim->DecreaseRef();
969 return types::Function::Error;
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>();
975 if ((il_sim_labptr = new (std::nothrow) int[il_sim_lab->getSize()]) == nullptr)
977 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
978 il_state->DecreaseRef();
980 il_tcur->DecreaseRef();
982 il_sim->DecreaseRef();
984 return types::Function::Error;
987 if ((l_sim_lab = new (std::nothrow) char*[il_sim_lab->getSize()]) == nullptr)
989 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
990 il_state->DecreaseRef();
992 il_tcur->DecreaseRef();
994 il_sim->DecreaseRef();
996 delete[] il_sim_labptr;
997 return types::Function::Error;
999 for (int i = 0; i < il_sim_lab->getSize(); ++i)
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]));
1005 /*33 : sim.modptr */
1006 if (il_sim_input->get(32)->isDouble() == false)
1008 Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 33, 4);
1009 il_state->DecreaseRef();
1011 il_tcur->DecreaseRef();
1013 il_sim->DecreaseRef();
1015 delete[] il_sim_labptr;
1017 return types::Function::Error;
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();
1026 if (il_sim_input->get(33)->isString() == false)
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();
1031 il_tcur->DecreaseRef();
1033 il_sim->DecreaseRef();
1035 delete[] il_sim_labptr;
1037 return types::Function::Error;
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>();
1043 if ((il_sim_uidptr = new (std::nothrow) int[il_sim_uid->getSize()]) == nullptr)
1045 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1046 il_state->DecreaseRef();
1048 il_tcur->DecreaseRef();
1050 il_sim->DecreaseRef();
1052 delete[] il_sim_labptr;
1054 return types::Function::Error;
1057 if ((l_sim_uid = new (std::nothrow) char*[il_sim_uid->getSize()]) == nullptr)
1059 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1060 il_state->DecreaseRef();
1062 il_tcur->DecreaseRef();
1064 il_sim->DecreaseRef();
1066 delete[] il_sim_labptr;
1068 delete[] il_sim_uidptr;
1069 return types::Function::Error;
1071 for (int i = 0; i < il_sim_uid->getSize(); ++i)
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]));
1080 if (in[4]->isString() == false)
1082 Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 5);
1083 il_state->DecreaseRef();
1085 il_tcur->DecreaseRef();
1087 il_sim->DecreaseRef();
1089 delete[] il_sim_labptr;
1091 delete[] il_sim_uidptr;
1093 return types::Function::Error;
1095 types::String* il_str = in[4]->getAs<types::String>();
1096 if (il_str->isScalar() == false)
1098 Scierror(999, _("%s: Wrong size for input argument #%d : A scalar expected.\n"), funname.data(), 5);
1099 il_state->DecreaseRef();
1101 il_tcur->DecreaseRef();
1103 il_sim->DecreaseRef();
1105 delete[] il_sim_labptr;
1107 delete[] il_sim_uidptr;
1109 return types::Function::Error;
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");
1118 if (il_str->get(0) == start)
1122 else if (il_str->get(0) == run)
1126 else if (il_str->get(0) == finish)
1130 else if (il_str->get(0) == linear)
1134 else if (il_str->get(0) == Kinsol)
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();
1144 il_tcur->DecreaseRef();
1146 il_sim->DecreaseRef();
1148 delete[] il_sim_labptr;
1150 delete[] il_sim_uidptr;
1152 return types::Function::Error;
1158 if (in[5]->isDouble() == false)
1160 Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 6);
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;
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
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();
1180 il_tcur->DecreaseRef();
1182 il_sim->DecreaseRef();
1184 delete[] il_sim_labptr;
1186 delete[] il_sim_uidptr;
1188 return types::Function::Error;
1192 * Set 'simpar' array
1194 for (int i = 0; i < m6; ++i)
1196 simpar[i] = il_tol->get(i);
1198 for (int i = m6; i < 7; ++i)
1202 int solver = static_cast<int>(simpar[5]); // Set 'solver' variable
1204 /******************************
1205 * Cross variable size checking
1206 ******************************/
1210 err_check = 1; // tevts vs evtspt
1212 else if (m_xptr != m_zptr)
1214 err_check = 2; // xptr vs zptr
1216 else if (m_xptr != m_ozptr)
1218 err_check = 3; // xptr vs ozptr
1220 else if (m_xptr != m_zcptr)
1222 err_check = 4; // xptr vs zcptr
1224 else if (m_xptr != m_inpptr)
1226 err_check = 5; // xptr vs inpptr
1228 else if (m_xptr != m_outptr)
1230 err_check = 6; // xptr vs outptr
1232 else if (m_xptr != m_rpptr)
1234 err_check = 7; // xptr vs rpptr
1236 else if (m_xptr != m_ipptr)
1238 err_check = 8; // xptr vs ipptr
1240 else if (m_xptr != m_opptr)
1242 err_check = 8; // xptr vs opptr
1244 else if (m_xptr != m_clkptr)
1246 err_check = 10; // xptr vs clkptr
1248 else if ((n_ordclk != 2) & (m_ordclk != 0))
1250 err_check = 11; // sim.ordclk
1252 else if ((n_cord != 2) & (m_cord != 0))
1254 err_check = 12; // sim.cord
1256 else if ((n_oord != 2) & (m_oord != 0))
1258 err_check = 13; // sim.oord
1260 else if ((n_zord != 2) & (m_zord != 0))
1262 err_check = 14; // sim.zord
1264 else if ((n_iord != 2) & (m_iord != 0))
1266 err_check = 15; // sim.iord
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();
1274 il_tcur->DecreaseRef();
1276 il_sim->DecreaseRef();
1278 delete[] il_sim_labptr;
1280 return types::Function::Error;
1283 /*******************************
1284 * Set function table for blocks
1285 *******************************/
1286 // Define new variable 'lfunpt'
1288 if ((lfunpt = new (std::nothrow) void*[nblk]) == nullptr)
1290 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1291 il_state->DecreaseRef();
1293 il_tcur->DecreaseRef();
1295 il_sim->DecreaseRef();
1297 delete[] il_sim_labptr;
1299 delete[] il_sim_uidptr;
1301 return types::Function::Error;
1305 for (int i = 0; i < nblk; ++i) // For each block
1307 types::InternalType* pIT = il_sim_fun->get(i);
1308 // Block is defined by a Scilab function
1309 if (pIT->isCallable())
1311 lfunpt[i] = (void*)pIT;
1312 // Keep 'l_sim_funtyp' negative for Scilab macros
1313 if (l_sim_funtyp[i] > 0)
1315 l_sim_funtyp[i] *= -1;
1318 // Block is defined by a function described by a string
1319 else if (pIT->isString())
1321 types::String* funStr = pIT->getAs<types::String>();
1322 if (funStr->isScalar() == false)
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();
1327 il_tcur->DecreaseRef();
1329 il_sim->DecreaseRef();
1331 delete[] il_sim_labptr;
1333 delete[] il_sim_uidptr;
1336 return types::Function::Error;
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)
1343 l_sim_funtyp[i] = 11; // Magic value for "if-then-else" block
1345 else if (strcmp(c_str, "eselect") == 0)
1347 l_sim_funtyp[i] = 12; // Magic value for "eselect" block
1351 void* f = funnum2(c_str); // Search associated function number of function name
1352 // Block is defined by a C or Fortran function
1355 // C interface from "tabsim" defined in blocks.h
1357 if (l_sim_funtyp[i] < 0)
1359 // Keep 'l_sim_funtyp' positive for Fortran functions
1360 l_sim_funtyp[i] *= -1;
1363 // Block is defined by a predefined scilab function
1366 ConfigVariable::EntryPointStr* pEP = ConfigVariable::getEntryPoint(w_str);
1370 lfunpt[i] = (void*)pEP->functionPtr;
1374 types::InternalType* pMacro = symbol::Context::getInstance()->get(symbol::Symbol(w_str));
1375 if (pMacro && pMacro->isCallable())
1378 lfunpt[i] = (void*)pMacro;
1379 l_sim_funtyp[i] *= -1;
1383 Scierror(888, _("%s : unknown block : %s\n"), funname.data(), c_str);
1384 il_state->DecreaseRef();
1386 il_tcur->DecreaseRef();
1388 il_sim->DecreaseRef();
1390 delete[] il_sim_labptr;
1392 delete[] il_sim_uidptr;
1396 return types::Function::Error;
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();
1408 il_tcur->DecreaseRef();
1410 il_sim->DecreaseRef();
1412 delete[] il_sim_labptr;
1414 delete[] il_sim_uidptr;
1417 return types::Function::Error;
1421 /**********************
1422 * Set oz, ozsz, oztyp
1423 **********************/
1424 void** oz = nullptr;
1425 int* ozsz = nullptr;
1426 int* oztyp = nullptr;
1429 // Allocation of 'oz'
1430 if ((oz = new (std::nothrow) void*[noz]) == nullptr)
1432 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1433 il_state->DecreaseRef();
1435 il_tcur->DecreaseRef();
1437 il_sim->DecreaseRef();
1439 delete[] il_sim_labptr;
1441 delete[] il_sim_uidptr;
1444 return types::Function::Error;
1446 // Allocation of 'ozsz'
1447 if ((ozsz = new (std::nothrow) int[2 * noz]) == nullptr)
1449 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1450 il_state->DecreaseRef();
1452 il_tcur->DecreaseRef();
1454 il_sim->DecreaseRef();
1456 delete[] il_sim_labptr;
1458 delete[] il_sim_uidptr;
1462 return types::Function::Error;
1464 // Allocation of 'oztyp'
1465 if ((oztyp = new (std::nothrow) int[noz]) == nullptr)
1467 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1468 il_state->DecreaseRef();
1470 il_tcur->DecreaseRef();
1472 il_sim->DecreaseRef();
1474 delete[] il_sim_labptr;
1476 delete[] il_sim_uidptr;
1481 return types::Function::Error;
1484 // Set vectors of 'oz'
1485 for (int j = 0; j < noz; ++j)
1487 int subtype = il_state_oz->get(j)->getType();
1489 switch (subtype) // Store type and address
1491 case types::InternalType::ScilabDouble :
1493 types::Double* ozDouble = il_state_oz->get(j)->getAs<types::Double>();
1494 if (ozDouble->isComplex() == false)
1496 oztyp[j] = SCSREAL_N; // Double real matrix
1497 oz[j] = (SCSREAL_COP *) ozDouble->get();
1501 oztyp[j] = SCSCOMPLEX_N; // Double complex matrix
1502 oz[j] = (SCSCOMPLEX_COP *) ozDouble->get();
1504 ozsz[j] = ozDouble->getRows();
1505 ozsz[j + noz] = ozDouble->getCols();
1508 case types::InternalType::ScilabInt8 :
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();
1517 case types::InternalType::ScilabInt16 :
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();
1526 case types::InternalType::ScilabInt32 :
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();
1535 case types::InternalType::ScilabUInt8 :
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();
1544 case types::InternalType::ScilabUInt16 :
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();
1553 case types::InternalType::ScilabUInt32 :
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();
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();
1567 il_tcur->DecreaseRef();
1569 il_sim->DecreaseRef();
1571 delete[] il_sim_labptr;
1573 delete[] il_sim_uidptr;
1579 return types::Function::Error;
1585 /****************************
1586 * Set opar, oparsz, opartyp
1587 ****************************/
1588 void** opar = nullptr;
1589 int* oparsz = nullptr;
1590 int* opartyp = nullptr;
1593 // Allocation of 'opar'
1594 if ((opar = new (std::nothrow) void*[nopar]) == nullptr)
1596 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1597 il_state->DecreaseRef();
1599 il_tcur->DecreaseRef();
1601 il_sim->DecreaseRef();
1603 delete[] il_sim_labptr;
1605 delete[] il_sim_uidptr;
1611 return types::Function::Error;
1613 // Allocation of 'oparsz'
1614 if ((oparsz = new (std::nothrow) int[2 * nopar]) == nullptr)
1616 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1617 il_state->DecreaseRef();
1619 il_tcur->DecreaseRef();
1621 il_sim->DecreaseRef();
1623 delete[] il_sim_labptr;
1625 delete[] il_sim_uidptr;
1632 return types::Function::Error;
1634 // Allocation of 'opartyp'
1635 if ((opartyp = new (std::nothrow) int[nopar]) == nullptr)
1637 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1638 il_state->DecreaseRef();
1640 il_tcur->DecreaseRef();
1642 il_sim->DecreaseRef();
1644 delete[] il_sim_labptr;
1646 delete[] il_sim_uidptr;
1654 return types::Function::Error;
1657 // Set vectors of 'opar'
1658 for (int j = 0; j < nopar; ++j)
1660 int subtype = il_sim_opar->get(j)->getType();
1662 switch (subtype) // Store type and address
1664 case types::InternalType::ScilabDouble :
1666 types::Double* oparDouble = il_sim_opar->get(j)->getAs<types::Double>();
1667 if (oparDouble->isComplex() == false)
1669 opartyp[j] = SCSREAL_N; // Double real matrix
1670 opar[j] = (SCSREAL_COP *) oparDouble->get();
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)
1678 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1679 il_state->DecreaseRef();
1681 il_tcur->DecreaseRef();
1683 il_sim->DecreaseRef();
1685 delete[] il_sim_labptr;
1687 delete[] il_sim_uidptr;
1696 return types::Function::Error;
1698 memcpy(opar[j], oparDouble->get(), oparDouble->getSize() * sizeof(double)); // Real part
1699 memcpy((double*)(opar[j]) + oparDouble->getSize(), oparDouble->getImg(), oparDouble->getSize() * sizeof(double)); // Complex part
1700 // FIXME: delete oparDouble because we copied it instead of using it?
1702 oparsz[j] = oparDouble->getRows();
1703 oparsz[j + nopar] = oparDouble->getCols();
1706 case types::InternalType::ScilabInt8 :
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();
1715 case types::InternalType::ScilabInt16 :
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();
1724 case types::InternalType::ScilabInt32 :
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();
1733 case types::InternalType::ScilabUInt8 :
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();
1742 case types::InternalType::ScilabUInt16 :
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();
1751 case types::InternalType::ScilabUInt32 :
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();
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();
1765 il_tcur->DecreaseRef();
1767 il_sim->DecreaseRef();
1769 delete[] il_sim_labptr;
1771 delete[] il_sim_uidptr;
1780 return types::Function::Error;
1786 // Declaration of 'outtb_elem'
1787 outtb_el* outtb_elem = nullptr;
1790 /*******************************
1791 * Set outtbptr, outtbsz, outtbtyp
1792 *******************************/
1793 void** outtbptr = nullptr;
1794 int* outtbsz = nullptr;
1795 int* outtbtyp = nullptr;
1798 // Allocation of 'outtbptr'
1799 if ((outtbptr = new (std::nothrow) void*[nlnk]) == nullptr)
1801 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1802 il_state->DecreaseRef();
1804 il_tcur->DecreaseRef();
1806 il_sim->DecreaseRef();
1808 delete[] il_sim_labptr;
1810 delete[] il_sim_uidptr;
1819 return types::Function::Error;
1821 // Allocation of 'outtbptrsz'
1822 if ((outtbsz = new (std::nothrow) int[2 * nlnk]) == nullptr)
1824 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1825 il_state->DecreaseRef();
1827 il_tcur->DecreaseRef();
1829 il_sim->DecreaseRef();
1831 delete[] il_sim_labptr;
1833 delete[] il_sim_uidptr;
1843 return types::Function::Error;
1845 // Allocation of 'outtbtyp'
1846 if ((outtbtyp = new (std::nothrow) int[nlnk]) == nullptr)
1848 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1849 il_state->DecreaseRef();
1851 il_tcur->DecreaseRef();
1853 il_sim->DecreaseRef();
1855 delete[] il_sim_labptr;
1857 delete[] il_sim_uidptr;
1868 return types::Function::Error;
1871 // Set vectors of 'outtbptr'
1872 for (int j = 0; j < nlnk; ++j)
1874 int subtype = il_state_outtb->get(j)->getType();
1876 switch (subtype) // Store type and address
1878 case types::InternalType::ScilabDouble :
1880 types::Double* outtbDouble = il_state_outtb->get(j)->getAs<types::Double>();
1881 if (outtbDouble->isComplex() == false)
1883 outtbtyp[j] = SCSREAL_N; // Double real matrix
1884 outtbptr[j] = (SCSREAL_COP *) outtbDouble->get();
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)
1892 Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1893 il_state->DecreaseRef();
1895 il_tcur->DecreaseRef();
1897 il_sim->DecreaseRef();
1899 delete[] il_sim_labptr;
1901 delete[] il_sim_uidptr;
1912 return types::Function::Error;
1914 memcpy(outtbptr[j], outtbDouble->get(), outtbDouble->getSize() * sizeof(double)); // Real part
1915 memcpy((double*)(outtbptr[j]) + outtbDouble->getSize(), outtbDouble->getImg(), outtbDouble->getSize() * sizeof(double)); // Complex part
1916 // FIXME: delete outtbDouble because we copied it instead of using it?
1918 outtbsz[j] = outtbDouble->getRows();
1919 outtbsz[j + nlnk] = outtbDouble->getCols();
1922 case types::InternalType::ScilabInt8 :
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();
1931 case types::InternalType::ScilabInt16 :
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();
1940 case types::InternalType::ScilabInt32 :
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();
1949 case types::InternalType::ScilabUInt8 :
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();
1958 case types::InternalType::ScilabUInt16 :
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();
1967 case types::InternalType::ScilabUInt32 :
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();
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();
1981 il_tcur->DecreaseRef();
1983 il_sim->DecreaseRef();
1985 delete[] il_sim_labptr;
1987 delete[] il_sim_uidptr;
2000 return types::Function::Error;
2004 // Store 'lnk' and 'pos' in 'outtb_elem'
2006 nelem += outtbsz[j] * outtbsz[j + nlnk];
2007 if ((outtb_elem = (outtb_el *) REALLOC(outtb_elem, nelem * sizeof(outtb_el))) == nullptr)
2009 Scierror(999, _("%s : No more free memory.\n"), funname.data());
2010 il_state->DecreaseRef();
2012 il_tcur->DecreaseRef();
2014 il_sim->DecreaseRef();
2016 delete[] il_sim_labptr;
2018 delete[] il_sim_uidptr;
2031 return types::Function::Error;
2033 for (int i = 0; i < outtbsz[j]*outtbsz[j + nlnk]; ++i)
2035 outtb_elem[k + i].lnk = j;
2036 outtb_elem[k + i].pos = i;
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)
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,
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);
2087 /**********************
2088 * Free allocated arrays
2089 **********************/
2102 /*************************************
2103 * Switch to appropriate message error
2104 *************************************/
2111 error = _("scheduling problem");
2112 C2F(curblk).kfun = 0;
2116 error = _("input to zero-crossing stuck on zero");
2117 C2F(curblk).kfun = 0;
2121 error = _("event conflict");
2122 C2F(curblk).kfun = 0;
2126 error = _("algebraic loop detected");
2127 C2F(curblk).kfun = 0;
2131 error = _("cannot allocate memory");
2132 C2F(curblk).kfun = 0;
2136 if ((error = new (std::nothrow) char[MAX_ERROR_LEN]) == nullptr)
2138 allocatedError = TRUE;
2139 snprintf(error, MAX_ERROR_LEN, _("the block %d has been called with input out of its domain"), C2F(curblk).kfun);
2143 error = _("cannot allocate memory");
2148 error = _("singularity in a block");
2152 error = _("block produces an internal error");
2160 error = _("initial conditions not converging");
2164 if ((error = new (std::nothrow) char[MAX_ERROR_LEN]) == nullptr)
2166 allocatedError = TRUE;
2167 snprintf(error, MAX_ERROR_LEN, _("cannot allocate memory in block=%d"), C2F(curblk).kfun);
2171 error = _("cannot allocate memory");
2176 error = _("sliding mode condition, cannot integrate");
2180 error = _("Cannot find the initial mode, maybe there is a sliding mode condition");
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.");
2187 error = _("Undefined data type.");
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");
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
2197 error = _("unknown or erroneous block");
2199 else if (ierr >= 201 && ierr <= 416) // Sundials error messages, stored in coserr.buf
2203 else if (ierr >= 100)
2205 int istate = -(ierr - 100);
2206 if ((error = new (std::nothrow) char[MAX_ERROR_LEN]) == nullptr)
2208 allocatedError = TRUE;
2209 snprintf(error, MAX_ERROR_LEN, _("integration problem istate=%d"), istate);
2213 error = _("cannot allocate memory");
2215 C2F(curblk).kfun = 0;
2219 error = _("scicos unexpected error, please report...");
2220 C2F(curblk).kfun = 0;
2225 Scierror(888, "%s\n", error);
2226 il_state->DecreaseRef();
2228 il_tcur->DecreaseRef();
2230 il_sim->DecreaseRef();
2232 delete[] il_sim_labptr;
2234 delete[] il_sim_uidptr;
2240 return types::Function::Error;
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();
2250 out.push_back(il_state);
2251 out.push_back(il_tcur);
2254 il_sim->DecreaseRef();
2256 delete[] il_sim_labptr;
2258 delete[] il_sim_uidptr;
2260 return types::Function::OK;