2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011 - DIGITEO - Cedric DELAMARRE
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-en.txt
12 /*--------------------------------------------------------------------------*/
14 #include "differential_equations_gw.hxx"
15 #include "function.hxx"
19 #include "callable.hxx"
20 #include "differentialequationfunctions.hxx"
21 #include "runvisitor.hxx"
25 #include "sci_malloc.h"
26 #include "localization.h"
29 #include "scifunctions.h"
30 #include "elem_common.h"
31 #include "checkodeerror.h"
36 /*--------------------------------------------------------------------------*/
37 types::Function::ReturnValue sci_dassl(types::typed_list &in, int _iRetCount, types::typed_list &out)
40 types::Double* pDblX0 = NULL;
41 types::Double* pDblT0 = NULL;
42 types::Double* pDblT = NULL;
43 types::Double* pDblRtol = NULL;
44 types::Double* pDblAtol = NULL;
45 types::Double* pDblHd = NULL;
48 double* pdYData = NULL; // contain y0 following by all args data in list case.
49 double* pdYdotData = NULL;
50 int sizeOfpdYData = 0;
55 int info[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
64 int* YSize = NULL; // YSize(1) = size of y0,
65 // YSize(n) = size of Args(n) in list case.
67 // Indicate if the function is given.
69 bool bFuncJac = false;
71 // Indicate if info list is given.
72 bool bListInfo = false;
74 // *** check the minimal number of input args. ***
75 if (in.size() < 4 || in.size() > 9)
77 Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "dassl", 4, 9);
78 return types::Function::Error;
81 // *** check number of output args ***
84 Scierror(78, _("%s: Wrong number of output argument(s): %d to %d expected.\n"), "dassl", 1, 2);
85 return types::Function::Error;
88 // *** check type of input args and get it. ***
90 if (in[iPos]->isDouble() == false)
92 Scierror(999, _("%s: Wrong type for input argument #%d: A matrix expected.\n"), "dassl", iPos + 1);
93 return types::Function::Error;
96 pDblX0 = in[iPos]->getAs<types::Double>();
98 if (pDblX0->isComplex())
100 Scierror(999, _("%s: Wrong type for input argument #%d: A real matrix expected.\n"), "dassl", iPos + 1);
101 return types::Function::Error;
104 if (pDblX0->getCols() > 2)
106 Scierror(999, _("%s: Wrong size for input argument #%d: A real matrix with %d to %d colomn(s) expected.\n"), "dassl", iPos + 1, 1, 2);
107 return types::Function::Error;
110 if (pDblX0->getCols() == 1)
117 if (in[iPos]->isDouble() == false)
119 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "dassl", iPos + 1);
120 return types::Function::Error;
123 pDblT0 = in[iPos]->getAs<types::Double>();
125 if (pDblT0->isScalar() == false)
127 Scierror(999, _("%s: Wrong size for input argument #%d: A scalar expected.\n"), "dassl", iPos + 1);
128 return types::Function::Error;
133 if (in[iPos]->isDouble() == false)
135 Scierror(999, _("%s: Wrong type for input argument #%d: A matrix expected.\n"), "dassl", iPos + 1);
136 return types::Function::Error;
139 pDblT = in[iPos]->getAs<types::Double>();
141 if (pDblT->isComplex())
143 Scierror(999, _("%s: Wrong type for input argument #%d: A real matrix expected.\n"), "dassl", iPos + 1);
144 return types::Function::Error;
148 DifferentialEquationFunctions* deFunctionsManager = new DifferentialEquationFunctions(L"dassl");
149 DifferentialEquation::addDifferentialEquationFunctions(deFunctionsManager);
151 YSize = (int*)MALLOC(sizeOfYSize * sizeof(int));
152 *YSize = pDblX0->getRows();
154 pdYData = (double*)MALLOC(*YSize * sizeof(double));
155 pdYdotData = (double*)MALLOC(*YSize * sizeof(double));
157 C2F(dcopy)(YSize, pDblX0->get(), &one, pdYData, &one);
158 if (pDblX0->getCols() == 2)
160 C2F(dcopy)(YSize, pDblX0->get() + *YSize, &one, pdYdotData, &one);
164 memset(pdYdotData, 0x00, *YSize);
167 deFunctionsManager->setOdeYRows(pDblX0->getRows());
169 for (iPos++; iPos < in.size(); iPos++)
171 if (in[iPos]->isDouble())
173 if (pDblAtol == NULL && bFuncF == false)
175 pDblAtol = in[iPos]->getAs<types::Double>();
176 if (pDblAtol->getSize() != pDblX0->getRows() && pDblAtol->isScalar() == false)
178 Scierror(267, _("%s: Wrong size for input argument #%d: A scalar or a matrix of size %d expected.\n"), "dassl", iPos + 1, pDblX0->getRows());
179 DifferentialEquation::removeDifferentialEquationFunctions();
183 return types::Function::Error;
186 else if (pDblRtol == NULL && bFuncF == false)
188 pDblRtol = in[iPos]->getAs<types::Double>();
189 if (pDblAtol->getSize() != pDblRtol->getSize())
191 Scierror(267, _("%s: Wrong size for input argument #%d: Atol and Rtol must have the same size.\n"), "dassl", iPos + 1, pDblX0->getRows());
192 DifferentialEquation::removeDifferentialEquationFunctions();
196 return types::Function::Error;
199 else if (pDblHd == NULL && bFuncF == true)
201 pDblHd = in[iPos]->getAs<types::Double>();
202 if (in.size() != iPos + 1)
204 Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "dassl", iPos + 1);
205 DifferentialEquation::removeDifferentialEquationFunctions();
209 return types::Function::Error;
214 Scierror(999, _("%s: Wrong type for input argument #%d: A function expected.\n"), "dassl", iPos + 1);
215 DifferentialEquation::removeDifferentialEquationFunctions();
219 return types::Function::Error;
222 else if (in[iPos]->isCallable())
224 types::Callable* pCall = in[iPos]->getAs<types::Callable>();
227 deFunctionsManager->setFFunction(pCall);
230 else if (bFuncJac == false)
232 deFunctionsManager->setJacFunction(pCall);
237 Scierror(999, _("%s: Wrong type for input argument #%d: A matrix or a list expected.\n"), "dassl", iPos + 1);
238 DifferentialEquation::removeDifferentialEquationFunctions();
242 return types::Function::Error;
245 else if (in[iPos]->isString())
247 types::String* pStr = in[iPos]->getAs<types::String>();
252 bOK = deFunctionsManager->setFFunction(pStr);
255 else if (bFuncJac == false)
257 bOK = deFunctionsManager->setJacFunction(pStr);
262 Scierror(999, _("%s: Wrong type for input argument #%d: A matrix or a list expected.\n"), "dassl", iPos + 1);
263 DifferentialEquation::removeDifferentialEquationFunctions();
267 return types::Function::Error;
272 char* pst = wide_string_to_UTF8(pStr->get(0));
273 Scierror(50, _("%s: Subroutine not found: %s\n"), "dassl", pst);
275 DifferentialEquation::removeDifferentialEquationFunctions();
279 return types::Function::Error;
282 else if (in[iPos]->isList())
284 types::List* pList = in[iPos]->getAs<types::List>();
286 if (pList->getSize() == 0)
288 Scierror(50, _("%s: Argument #%d: Subroutine not found in list: %s\n"), "dassl", iPos + 1, "(string empty)");
289 DifferentialEquation::removeDifferentialEquationFunctions();
293 return types::Function::Error;
296 if (bFuncF && bListInfo)
298 Scierror(999, _("%s: Wrong type for input argument #%d: A matrix expected.\n"), "dassl", iPos + 1);
299 DifferentialEquation::removeDifferentialEquationFunctions();
303 return types::Function::Error;
306 if (pList->get(0)->isString())
308 types::String* pStr = pList->get(0)->getAs<types::String>();
314 bOK = deFunctionsManager->setFFunction(pStr);
315 sizeOfpdYData = *YSize;
317 else if (bFuncJac == false)
320 bOK = deFunctionsManager->setJacFunction(pStr);
321 if (sizeOfpdYData == 0)
323 sizeOfpdYData = *YSize;
329 char* pst = wide_string_to_UTF8(pStr->get(0));
330 Scierror(50, _("%s: Argument #%d: Subroutine not found in list: %s\n"), "dassl", iPos + 1, pst);
332 DifferentialEquation::removeDifferentialEquationFunctions();
336 return types::Function::Error;
339 int* sizeTemp = YSize;
340 int totalSize = sizeOfpdYData;
342 YSize = (int*)MALLOC((sizeOfYSize + pList->getSize() - 1) * sizeof(int));
343 memcpy(YSize, sizeTemp, sizeOfYSize * sizeof(int));
345 std::vector<types::Double*> vpDbl;
346 for (int iter = 0; iter < pList->getSize() - 1; iter++)
348 if (pList->get(iter + 1)->isDouble() == false)
350 Scierror(999, _("%s: Wrong type for input argument #%d: Argument %d in the list must be a matrix.\n"), "dassl", iPos + 1, iter + 1);
351 DifferentialEquation::removeDifferentialEquationFunctions();
355 return types::Function::Error;
358 vpDbl.push_back(pList->get(iter + 1)->getAs<types::Double>());
359 YSize[sizeOfYSize + iter] = vpDbl[iter]->getSize();
360 totalSize += YSize[sizeOfYSize + iter];
363 double* pdYDataTemp = pdYData;
364 pdYData = (double*)MALLOC(totalSize * sizeof(double));
365 C2F(dcopy)(&sizeOfpdYData, pdYDataTemp, &one, pdYData, &one);
367 int position = sizeOfpdYData;
368 for (int iter = 0; iter < pList->getSize() - 1; iter++)
370 C2F(dcopy)(&YSize[sizeOfYSize + iter], vpDbl[iter]->get(), &one, &pdYData[position], &one);
371 position += vpDbl[iter]->getSize();
374 sizeOfpdYData = totalSize;
375 sizeOfYSize += pList->getSize() - 1;
379 else if (pList->get(0)->isCallable())
384 deFunctionsManager->setFFunction(pList->get(0)->getAs<types::Callable>());
385 for (int iter = 1; iter < pList->getSize(); iter++)
387 deFunctionsManager->setFArgs(pList->get(iter)->getAs<types::InternalType>());
390 else if (bFuncJac == false)
393 deFunctionsManager->setJacFunction(pList->get(0)->getAs<types::Callable>());
394 for (int iter = 1; iter < pList->getSize(); iter++)
396 deFunctionsManager->setJacArgs(pList->get(iter)->getAs<types::InternalType>());
400 else if (pList->get(0)->isDouble() && bFuncF == true)
402 if (pList->getSize() != 7)
404 Scierror(267, _("%s: Wrong size for input argument #%d: A list of size %d expected.\n"), "dassl", iPos + 1, 7);
405 DifferentialEquation::removeDifferentialEquationFunctions();
409 return types::Function::Error;
412 for (int i = 0; i < 7; i++) // info = list([],0,[],[],[],0,0)
414 if (pList->get(i)->isDouble() == false || (pList->get(i)->getAs<types::Double>()->isScalar() == false && (i == 1 || i == 5 || i == 6)))
416 if (i == 1 || i == 5 || i == 6)
418 Scierror(999, _("%s: Wrong type for input argument #%d: Element %d in the info list must be a scalar.\n"), "dassl", iPos + 1, i);
422 Scierror(999, _("%s: Wrong type for input argument #%d: Element %d in the info list must be a matrix.\n"), "dassl", iPos + 1, i);
424 DifferentialEquation::removeDifferentialEquationFunctions();
428 return types::Function::Error;
432 types::Double* pDblTemp = pList->get(0)->getAs<types::Double>();
433 if (pDblTemp->getSize() != 0)
436 tstop = pDblTemp->get(0);
439 info[2] = (int)pList->get(1)->getAs<types::Double>()->get(0);
441 pDblTemp = pList->get(2)->getAs<types::Double>();
442 if (pDblTemp->getSize() == 2)
445 ml = (int)pDblTemp->get(0);
446 mu = (int)pDblTemp->get(1);
447 deFunctionsManager->setMl(ml);
448 deFunctionsManager->setMu(mu);
450 else if (pDblTemp->getSize() != 0)
452 Scierror(267, _("%s: Wrong size for input argument #%d: Argument %d in te list must be of size %d.\n"), "dassl", iPos + 1, 3, 2);
453 DifferentialEquation::removeDifferentialEquationFunctions();
457 return types::Function::Error;
460 pDblTemp = pList->get(3)->getAs<types::Double>();
461 if (pDblTemp->getSize() != 0)
464 maxstep = pDblTemp->get(0);
467 pDblTemp = pList->get(4)->getAs<types::Double>();
468 if (pDblTemp->getSize() != 0)
471 stepin = pDblTemp->get(0);
474 info[9] = (int)pList->get(5)->getAs<types::Double>()->get(0);
475 if (pList->get(6)->getAs<types::Double>()->get(0) == 1)
484 Scierror(999, _("%s: Wrong type for input argument #%d: The first argument in the list must be a string, a function or a matrix in case of argument info.\n"), "dassl", iPos + 1);
485 DifferentialEquation::removeDifferentialEquationFunctions();
489 return types::Function::Error;
494 Scierror(999, _("%s: Wrong type for input argument #%d: A matrix or a function expected.\n"), "dassl", iPos + 1);
495 DifferentialEquation::removeDifferentialEquationFunctions();
499 return types::Function::Error;
505 Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "dassl", in.size() + 1);
506 DifferentialEquation::removeDifferentialEquationFunctions();
510 return types::Function::Error;
513 if (bFuncJac == true)
518 // *** Initialization. ***
519 double t0 = pDblT0->get(0);
525 //compute itol and set the tolerances rtol and atol.
531 if (pDblAtol->isScalar())
533 atol = (double*)MALLOC(sizeof(double));
534 *atol = pDblAtol->get(0);
538 atol = pDblAtol->get();
544 atol = (double*)MALLOC(sizeof(double));
550 if (pDblRtol->isScalar())
552 rtol = (double*)MALLOC(sizeof(double));
553 *rtol = pDblRtol->get(0);
557 rtol = pDblRtol->get();
560 else // if rtol is not given atol will be used as a scalar.
562 if (pDblAtol && pDblAtol->isScalar() == false) // info[1] == 1
564 double dblSrc = 1.e-9;
565 int iSize = pDblAtol->getSize();
569 rtol = (double*)MALLOC(iSize * sizeof(double));
570 C2F(dcopy)(&iSize, &dblSrc, &iZero, rtol, &iOne);
574 rtol = (double*)MALLOC(sizeof(double));
579 // Compute rwork, iwork size.
581 int iworksize = 20 + pDblX0->getRows();
584 double* rwork = NULL;
588 rworksize = 40 + (maxord + 4) * pDblX0->getRows() + pDblX0->getRows() * pDblX0->getRows();
590 else if (info[4] == 1)
592 rworksize = 40 + (maxord + 4) * pDblX0->getRows() + (2 * ml + mu + 1) * pDblX0->getRows();
594 else if (info[4] == 0)
596 rworksize = 40 + (maxord + 4) * pDblX0->getRows() + (2 * ml + mu + 1) * pDblX0->getRows() + 2 * (pDblX0->getRows() / (ml + mu + 1) + 1);
599 iwork = (int*)MALLOC(iworksize * sizeof(int));
600 rwork = (double*)MALLOC(rworksize * sizeof(double));
604 if (iworksize + rworksize != pDblHd->getSize())
606 Scierror(77, _("%s: Wrong size for input argument(s) %d: %d expected.\n"), "dassl", in.size(), iworksize + rworksize);
607 DifferentialEquation::removeDifferentialEquationFunctions();
613 if (pDblAtol == NULL || pDblAtol->isScalar())
617 if (pDblRtol == NULL || pDblRtol->isScalar())
621 return types::Function::Error;
624 C2F(dcopy)(&rworksize, pDblHd->get(), &one, rwork, &one);
626 for (int i = 0; i < iworksize; i++)
628 iwork[i] = (int)pDblHd->get(rworksize + i);
655 // *** Perform operation. ***
656 std::list<types::Double*> lpDblOut;
657 int size = pDblX0->getRows();
658 int rowsOut = 1 + pDblX0->getRows() * 2;
661 for (int i = 0; i < pDblT->getSize(); i++)
663 types::Double* pDblOut = new types::Double(rowsOut, 1);
664 lpDblOut.push_back(pDblOut);
666 double t = pDblT->get(i);
668 pDblOut->set(pos, t);
673 C2F(dcopy)(&size, pdYData, &one, pDblOut->get() + pos, &one);
674 pos += pDblX0->getRows();
675 C2F(dcopy)(&size, pdYdotData, &one, pDblOut->get() + pos, &one);
682 C2F(dassl)(dassl_f, YSize, &t0, pdYData, pdYdotData, &t, info, rtol, atol, &idid, rwork, &rworksize, iwork, &iworksize, &rpar, &ipar, dassl_jac);
684 iret = checkDasslError(idid);
685 if (iret == 1) // error
687 Scierror(999, _("%s: dassl return with state %d.\n"), "dassl", idid);
690 catch (ast::ScilabError &e)
692 char* pstrMsg = wide_string_to_UTF8(e.GetErrorMessage().c_str());
693 sciprint(_("%s: exception caught in '%s' subroutine.\n"), "dassl", "dassl");
694 Scierror(999, pstrMsg);
695 // set iret to 1 for FREE allocated memory
699 if (iret == 1) // error
702 DifferentialEquation::removeDifferentialEquationFunctions();
708 if (pDblAtol == NULL || pDblAtol->isScalar())
712 if (pDblRtol == NULL || pDblRtol->isScalar())
716 return types::Function::Error;
720 C2F(dcopy)(&size, pdYData, &one, pDblOut->get() + pos, &one);
722 C2F(dcopy)(&size, pdYdotData, &one, pDblOut->get() + pos, &one);
724 if (iret == 2) // warning
748 // *** Return result in Scilab. ***
749 types::Double* pDblOut = new types::Double(rowsOut, (int)lpDblOut.size());
751 int sizeOfList = (int)lpDblOut.size();
752 for (int i = 0; i < sizeOfList; i++)
754 int pos = i * rowsOut;
755 C2F(dcopy)(&rowsOut, lpDblOut.front()->get(), &one, pDblOut->get() + pos, &one);
756 lpDblOut.pop_front();
758 out.push_back(pDblOut);
762 types::Double* pDblHdOut = new types::Double(rworksize + iworksize, 1);
763 C2F(dcopy)(&rworksize, rwork, &one, pDblHdOut->get(), &one);
765 for (int i = 0; i < iworksize; i++)
767 pDblHdOut->set(rworksize + i, (double)iwork[i]);
770 out.push_back(pDblHdOut);
774 if (pDblAtol == NULL || pDblAtol->isScalar())
779 if (pDblRtol == NULL || pDblRtol->isScalar())
790 DifferentialEquation::removeDifferentialEquationFunctions();
792 return types::Function::OK;