3 * Copyright (C) 2015 - Scilab Enterprises - Paul Bignier
4 * Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * See the file ./license.txt
22 /*--------------------------------------------------------------------------*/
27 #include "var2vec.hxx"
28 #include "vec2var.hxx"
30 #include "internal.hxx"
31 #include "callable.hxx"
34 #include "function.hxx"
35 #include "execvisitor.hxx"
40 #include "scicos.h" /* set_block_error() */
43 static double toDouble(const int i)
45 return static_cast<double>(i);
48 static void setErrAndFree(const int flag, types::typed_list out)
50 set_block_error(flag);
51 for (size_t i = 0; i < out.size(); ++i)
57 /*--------------------------------------------------------------------------*/
58 void sciblk2(int* flag, int* nevprt, double* t, double xd[], double x[], int* nx, double z[], int* nz, double tvec[], int* ntvec, double rpar[], int* nrpar,
59 int ipar[], int* nipar, double* inptr[], int insz[], int* nin, double* outptr[], int outsz[], int* nout, void* scsptr)
61 types::typed_list in(8), out;
63 types::Double* Flag = new types::Double(*flag);
66 types::Double* Nevprt = new types::Double(*nevprt);
69 types::Double* T = new types::Double(*t);
72 types::Double* X = new types::Double(*nx, 1);
73 memcpy(X->get(), x, *nx * sizeof(double));
76 types::InternalType* Z;
79 Z = types::Double::Empty();
83 if (!vec2var(std::vector<double>(z, z + *nz), Z))
85 setErrAndFree(-1, out);
90 setErrAndFree(-1, out);
93 //types::Double* Z = new types::Double(*nz, 1);
94 //memcpy(Z->get(), z, *nz * sizeof(double));
96 in[4] = Z->getAs<types::Double>();
98 types::Double* Rpar = new types::Double(*nrpar, 1);
99 memcpy(Rpar->get(), rpar, *nrpar * sizeof(double));
102 // Treating 'ipar' differently because it is an int tab, unlike the other double ones
103 types::Double* Ipar = new types::Double(*nipar, 1);
104 std::transform(ipar, ipar + *nipar, Ipar, toDouble);
107 types::List* Nin = new types::List();
108 for (int i = 0; i < *nin; ++i)
111 int nu2 = insz[*nin + i];
112 types::Double* U = new types::Double(nu, nu2);
113 memcpy(U->get(), inptr[i], nu * nu2 * sizeof(double));
118 /***********************
119 * Call Scilab function *
120 ***********************/
121 ast::ExecVisitor exec;
122 types::Callable* pCall = static_cast<types::Callable*>(scsptr);
126 types::optional_list opt;
127 if (pCall->call(in, opt, 5, out, &exec) != types::Function::OK)
129 setErrAndFree(-1, out);
135 setErrAndFree(-1, out);
139 catch (ast::ScilabMessage& /*sm*/)
141 setErrAndFree(-1, out);
153 if (!out[2]->isDouble())
155 setErrAndFree(-1, out);
158 std::vector<double> Zout;
159 if (!var2vec(out[2], Zout))
161 setErrAndFree(-1, out);
164 memcpy(z, &Zout[0], *nz * sizeof(double));
166 if (!out[3]->isDouble())
168 setErrAndFree(-1, out);
171 types::Double* Xout = out[3]->getAs<types::Double>();
172 memcpy(x, Xout->get(), *nx * sizeof(double));
176 if (!out[4]->isList())
178 setErrAndFree(-1, out);
181 types::List* Yout = out[4]->getAs<types::List>();
182 if (Yout->getSize() < *nout)
184 // Consider that 'outptr' has not been defined in the macro: do not update the current 'outptr'
187 for (int k = *nout - 1; k >= 0; --k)
189 if (!Yout->get(k)->isDouble())
191 setErrAndFree(-1, out);
194 types::Double* KthElement = Yout->get(k)->getAs<types::Double>();
195 double* y = (double*)outptr[k];
200 ny2 = outsz[*nout + k];
202 memcpy(y, KthElement->get(), ny * ny2 * sizeof(double));
211 if (!out[0]->isDouble())
213 setErrAndFree(-1, out);
216 types::Double* XDout = out[0]->getAs<types::Double>();
217 memcpy(xd, XDout->get(), *nx * sizeof(double));
223 if (!out[1]->isDouble())
225 setErrAndFree(-1, out);
228 types::Double* Tout = out[1]->getAs<types::Double>();
229 memcpy(tvec, Tout->get(), *ntvec * sizeof(double));
235 setErrAndFree(-1, out);
240 for (size_t i = 0; i < out.size(); ++i)