Changeset 16248 for branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Problem.cs
- Timestamp:
- 10/22/18 10:02:42 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Problem.cs
r16246 r16248 24 24 using System.Diagnostics; 25 25 using System.Linq; 26 using System.Runtime.InteropServices;27 26 using HeuristicLab.Analysis; 28 27 using HeuristicLab.Collections; … … 132 131 [StorableClass] 133 132 public sealed class Problem : SingleObjectiveBasicProblem<MultiEncoding>, IRegressionProblem, IProblemInstanceConsumer<IRegressionProblemData>, IProblemInstanceExporter<IRegressionProblemData> { 134 135 // CVODES types136 public enum MultistepMethod : int { CV_ADAMS = 1, CV_BDF = 2 };137 public enum NonlinearSolverIteration : int { CV_NEWTON = 1, CV_FUNCTIONAL = 2 };138 139 140 /* itask */141 public const int CV_NORMAL = 1;142 public const int CV_ONE_STEP = 2;143 144 /* ism */145 public const int CV_SIMULTANEOUS = 1;146 public const int CV_STAGGERED = 2;147 public const int CV_STAGGERED1 = 3;148 149 /* DQtype */150 public const int CV_CENTERED = 1;151 public const int CV_FORWARD = 2;152 153 /* interp */154 public const int CV_HERMITE = 1;155 public const int CV_POLYNOMIAL = 2;156 157 /*158 * ----------------------------------------159 * CVODES return flags160 * ----------------------------------------161 */162 163 public const int CV_SUCCESS = 0;164 public const int CV_TSTOP_RETURN = 1;165 public const int CV_ROOT_RETURN = 2;166 167 public const int CV_WARNING = 99;168 169 public const int CV_TOO_MUCH_WORK = -1;170 public const int CV_TOO_MUCH_ACC = -2;171 public const int CV_ERR_FAILURE = -3;172 public const int CV_CONV_FAILURE = -4;173 174 public const int CV_LINIT_FAIL = -5;175 public const int CV_LSETUP_FAIL = -6;176 public const int CV_LSOLVE_FAIL = -7;177 public const int CV_RHSFUNC_FAIL = -8;178 public const int CV_FIRST_RHSFUNC_ERR = -9;179 public const int CV_REPTD_RHSFUNC_ERR = -10;180 public const int CV_UNREC_RHSFUNC_ERR = -11;181 public const int CV_RTFUNC_FAIL = -12;182 public const int CV_CONSTR_FAIL = -13;183 184 public const int CV_MEM_FAIL = -20;185 public const int CV_MEM_NULL = -21;186 public const int CV_ILL_INPUT = -22;187 public const int CV_NO_MALLOC = -23;188 public const int CV_BAD_K = -24;189 public const int CV_BAD_T = -25;190 public const int CV_BAD_DKY = -26;191 public const int CV_TOO_CLOSE = -27;192 193 public const int CV_NO_QUAD = -30;194 public const int CV_QRHSFUNC_FAIL = -31;195 public const int CV_FIRST_QRHSFUNC_ERR = -32;196 public const int CV_REPTD_QRHSFUNC_ERR = -33;197 public const int CV_UNREC_QRHSFUNC_ERR = -34;198 199 public const int CV_NO_SENS = -40;200 public const int CV_SRHSFUNC_FAIL = -41;201 public const int CV_FIRST_SRHSFUNC_ERR = -42;202 public const int CV_REPTD_SRHSFUNC_ERR = -43;203 public const int CV_UNREC_SRHSFUNC_ERR = -44;204 205 public const int CV_BAD_IS = -45;206 207 public const int CV_NO_QUADSENS = -50;208 public const int CV_QSRHSFUNC_FAIL = -51;209 public const int CV_FIRST_QSRHSFUNC_ERR = -52;210 public const int CV_REPTD_QSRHSFUNC_ERR = -53;211 public const int CV_UNREC_QSRHSFUNC_ERR = -54;212 213 /*214 * ----------------------------------------215 * CVODEA return flags216 * ----------------------------------------217 */218 219 public const int CV_NO_ADJ = -101;220 public const int CV_NO_FWD = -102;221 public const int CV_NO_BCK = -103;222 public const int CV_BAD_TB0 = -104;223 public const int CV_REIFWD_FAIL = -105;224 public const int CV_FWD_FAIL = -106;225 226 227 public const int CV_GETY_BADT = -107;228 229 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]230 public delegate int CVRhsFunc(231 double t, // realtype232 IntPtr y, // N_Vector233 IntPtr ydot, // N_Vector234 IntPtr user_data235 );236 237 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]238 public delegate int CVRhsFuncB(239 double t, // realtype240 IntPtr y, // N_Vector241 IntPtr yB, // N_Vector242 IntPtr yBdot, // N_Vector243 IntPtr user_data244 );245 246 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]247 public delegate int CVDlsJacFunc(248 double t,249 IntPtr y, // N_Vector250 IntPtr fy, // N_Vector251 IntPtr Jac, // SUNMatrix252 IntPtr user_data,253 IntPtr tmp1, // N_Vector254 IntPtr tmp2, // N_Vector255 IntPtr tmp3 // N_Vector256 );257 258 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]259 public delegate int CVDlsJacFuncB(260 double t,261 IntPtr y, // N_Vector262 IntPtr yB, // N_Vector263 IntPtr fyB, // N_Vector264 IntPtr Jac, // SUNMatrix265 IntPtr user_data,266 IntPtr tmp1, // N_Vector267 IntPtr tmp2, // N_Vector268 IntPtr tmp3 // N_Vector269 );270 271 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]272 public delegate int CVQuadRhsFn(273 double t,274 IntPtr y, // N_Vector275 IntPtr yQdot, // N_Vector276 IntPtr user_data277 );278 279 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]280 public delegate int CVQuadRhsFnB(281 double t,282 IntPtr y, // N_Vector283 IntPtr yB, // N_Vector284 IntPtr qBdot, // N_Vector285 IntPtr user_data286 );287 288 289 // to calculate sensitivities RHS for all equations at once290 // must compute (∂f/∂y)s_i(t) + ∂f/∂p_i291 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]292 public delegate int CVSensRhsFn(293 int Ns,294 double t,295 IntPtr y, // N_Vector296 IntPtr ydot, // N_Vector297 IntPtr yS, // N_Vector* one vector for each parameter298 IntPtr ySdot, // N_Vector* one vector for each parameter299 IntPtr user_data,300 IntPtr tmp1, // N_Vector301 IntPtr tmp2 // N_Vector302 );303 304 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeCreate", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]305 // returns a void* to the cvodes memory block if successful otherwise NULL306 public static extern IntPtr CVodeCreate(MultistepMethod lmm, NonlinearSolverIteration iter);307 308 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]309 public static extern int CVodeInit(310 IntPtr cvode_mem, // pointer returned by CVodeCreate311 CVRhsFunc f,312 double t0, // realtype, the inital value of t313 IntPtr y0 // N_Vector the initial value of y314 );315 316 317 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeSStolerances", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]318 public static extern int CVodeSStolerances(319 IntPtr cvode_mem,320 double reltol,321 double abstol322 );323 324 [DllImport("sundials_cvodes.dll", EntryPoint = "CVDlsSetLinearSolver", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]325 public static extern int CVDlsSetLinearSolver(326 IntPtr cvode_mem,327 IntPtr linearSolver, // SUNLinearSolver328 IntPtr j // SUNMatrix329 );330 331 [DllImport("sundials_cvodes.dll", EntryPoint = "CVDlsSetJacFn", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]332 public static extern int CVDlsSetJacFn(333 IntPtr cvode_mem,334 CVDlsJacFunc jacFunc335 );336 337 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeSensInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]338 public static extern int CVodeSensInit(339 IntPtr cvode_mem,340 int Ns, // number of parameters341 int ism, // sensitivity solution method, CV_SIMULTANEOUS or CV_STAGGERED342 CVSensRhsFn fS, // right hand side function which computes all sensitivity RHS at the same time343 IntPtr yS0 // N_Vector344 );345 346 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeSensSStolerances", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]347 public static extern int CVodeSensSStolerances(348 IntPtr cvode_mem,349 double reltol,350 double[] abstol351 );352 353 /* Call CVodeSensEEtolerances to estimate tolerances for sensitivity354 variables based on the rolerances supplied for states variables and355 the scaling factor pbar */356 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeSensEEtolerances", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]357 public static extern int CVodeSensEEtolerances(358 IntPtr cvode_mem359 );360 361 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeGetSens", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]362 public static extern int CVodeGetSens(363 IntPtr cvode_mem,364 ref double tret,365 IntPtr yS //N_Vector*, one vector for each parameter366 );367 368 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeQuadInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]369 public static extern int CVodeQuadInit(370 IntPtr cvode_mem,371 CVQuadRhsFn qF,372 IntPtr yQ0 // N_Vector, initial value of yQ373 );374 375 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeQuadInitB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]376 public static extern int CVodeQuadInitB(377 IntPtr cvode_mem,378 int indexB,379 CVQuadRhsFnB rhsQB,380 IntPtr yQB0 // N_Vector, initial value of yQB381 );382 383 384 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeAdjInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]385 public static extern int CVodeAdjInit(386 IntPtr cvode_mem,387 int nd, // integration steps between checkpoints388 int interpType // either CV_POLYNOMIAL or CV_HERMITE389 );390 391 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeF", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]392 public static extern int CVodeF(393 IntPtr cvode_mem,394 double t_out, // the next time at which a computed solution is desired395 IntPtr y, // N_Vector, the solution vector y396 ref double t_ret, // the time reached by the solver (output)397 int itask, // CV_NORMAL or CV_ONE_STEP398 ref int ncheck // the number of internal checkpoints stored so far.399 );400 401 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeGetNumSteps", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]402 public static extern int CVodeGetNumSteps(403 IntPtr cvode_mem,404 ref long ncheck // the number of steps taken405 );406 407 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeCreateB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]408 public static extern int CVodeCreateB(409 IntPtr cvode_mem,410 MultistepMethod lmmB,411 NonlinearSolverIteration iterB,412 ref int which413 );414 415 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeInitB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]416 public static extern int CVodeInitB(417 IntPtr cvode_mem,418 int which,419 CVRhsFuncB rightHandSide,420 double tB0, // endpoint T where final conditions are provided (equal to endpoint of forward integration)421 IntPtr yB0 // N_Vector inital value at t=tb0 of backward solution422 );423 424 [DllImport("sundials_cvodes.dll", EntryPoint = "CVode", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]425 public static extern int CVode(426 IntPtr cvode_mem,427 double tout, // next time at which a solution is desired428 IntPtr yout, // N_Vector429 ref double tret, // the time reached by the solver (output)430 int itask // flag indicating the job of the solver for the next step.431 );432 433 434 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeSStolerancesB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]435 public static extern int CVodeSStolerancesB(436 IntPtr cvode_mem,437 int which,438 double reltol,439 double abstol440 );441 442 [DllImport("sundials_cvodes.dll", EntryPoint = "CVDlsSetLinearSolverB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]443 public static extern int CVDlsSetLinearSolverB(444 IntPtr cvode_mem,445 int which,446 IntPtr linearSolver, // SUNLinearSolver447 IntPtr j // SUNMatrix448 );449 450 [DllImport("sundials_cvodes.dll", EntryPoint = "CVDlsSetJacFnB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]451 public static extern int CVDlsSetJacFnB(452 IntPtr cvode_mem,453 int which,454 CVDlsJacFuncB jacFunc455 );456 457 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]458 public static extern int CVodeB(459 IntPtr cvode_mem,460 double tout, // next time at which a solution is desired461 int itask // flag indicating the job of the solver for the next step.462 );463 464 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeGetB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]465 public static extern int CVodeGetB(466 IntPtr cvode_mem,467 int which,468 ref double tret,469 IntPtr yB // the backward solution at time tret470 );471 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeGetAdjY", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]472 public static extern int CVodeGetAdjY(473 IntPtr cvode_mem,474 double t,475 IntPtr y // the forward solution y(t)476 );477 478 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeGetAdjCVodeBmem", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]479 public static extern IntPtr CVodeGetAdjCVodeBmem(480 IntPtr cvode_mem,481 int which482 );483 484 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeGetQuadB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]485 public static extern int CVodeGetQuadB(486 IntPtr cvode_mem,487 int which,488 ref double tret,489 IntPtr yQB // N_Vector490 );491 492 [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeFree", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]493 494 public static extern int CVodeFree(IntPtr cvode_mem);495 496 #region matrix497 [DllImport("sundials_cvodes.dll", EntryPoint = "SUNDenseMatrix", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]498 public static extern IntPtr SUNDenseMatrix(long m, long n);499 500 [DllImport("sundials_cvodes.dll", EntryPoint = "SUNMatDestroy", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]501 public static extern void SUNMatDestroy(IntPtr A);502 503 [DllImport("sundials_cvodes.dll", EntryPoint = "SUNDenseMatrix_Data", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]504 public unsafe static extern double* SUNDenseMatrix_Data(IntPtr matrix);505 506 [DllImport("sundials_cvodes.dll", EntryPoint = "SUNDenseMatrix_Cols", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]507 public static extern long SUNDenseMatrix_Cols(IntPtr matrix);508 509 [DllImport("sundials_cvodes.dll", EntryPoint = "SUNDenseMatrix_Rows", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]510 public static extern long SUNDenseMatrix_Rows(IntPtr matrix);511 512 513 public unsafe static double SUNDenseMatrix_Get(IntPtr mat, long i, long j) {514 long M = SUNDenseMatrix_Rows(mat);515 // the(i, j)th element of A(with 0 <= i<M and 0 <= j<N) is given by(A->data)[j*M+i]516 return SUNDenseMatrix_Data(mat)[j*M+i];517 }518 519 public unsafe static void SUNDenseMatrix_Set(IntPtr mat, long i, long j, double val) {520 long M = SUNDenseMatrix_Rows(mat);521 // the(i, j)th element of A(with 0 <= i<M and 0 <= j<N) is given by(A->data)[j*M+i]522 SUNDenseMatrix_Data(mat)[j * M + i] = val;523 }524 525 #endregion526 527 #region linear solver528 [DllImport("sundials_cvodes.dll", EntryPoint = "SUNDenseLinearSolver", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]529 public static extern IntPtr SUNDenseLinearSolver(530 IntPtr y, // N_Vector531 IntPtr A // SUNMatrix532 );533 534 [DllImport("sundials_cvodes.dll", EntryPoint = "SUNLinSolInitialize", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]535 public static extern int SUNLinSolInitialize(IntPtr linearSolver);536 537 [DllImport("sundials_cvodes.dll", EntryPoint = "SUNLinSolSetup", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]538 public static extern int SUNLinSolSetup(539 IntPtr linearSolver,540 IntPtr A // SUNMatrix541 );542 543 [DllImport("sundials_cvodes.dll", EntryPoint = "SUNLinSolSolve", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]544 public static extern int SUNLinSolSolve(545 IntPtr linearSolver,546 IntPtr A, // SUNMatrix547 IntPtr x, // N_Vector548 IntPtr b, // N_Vector549 double tol550 );551 552 [DllImport("sundials_cvodes.dll", EntryPoint = "SUNLinSolFree", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]553 public static extern int SUNLinSolFree(IntPtr linearSolver);554 555 #endregion556 557 #region N_Vector558 [DllImport("sundials_cvodes.dll", EntryPoint = "N_VNew_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]559 public static extern IntPtr N_VNew_Serial(long vec_length);560 561 [DllImport("sundials_cvodes.dll", EntryPoint = "N_VDestroy_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]562 public static extern void N_VDestroy_Serial(IntPtr vec);563 564 [DllImport("sundials_cvodes.dll", EntryPoint = "N_VPrint_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]565 public static extern void N_VPrint_Serial(IntPtr vec);566 567 [DllImport("sundials_cvodes.dll", EntryPoint = "N_VConst_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]568 public static extern void N_VConst_Serial(double c, IntPtr vec);569 570 571 [DllImport("sundials_cvodes.dll", EntryPoint = "N_VL1Norm_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]572 public static extern double N_VL1Norm_Serial(IntPtr vec);573 574 [DllImport("sundials_cvodes.dll", EntryPoint = "N_VMake_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]575 public static extern IntPtr N_VMake_Serial(long vec_length, double[] v_data);576 577 [DllImport("sundials_cvodes.dll", EntryPoint = "N_VScale", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]578 /// Performs the operation z = c*x579 public static extern void N_VScale(double s,580 IntPtr x, // N_Vector581 IntPtr z // N_Vector582 );583 584 [DllImport("sundials_cvodes.dll", EntryPoint = "N_VMake_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]585 public unsafe static extern double* N_VGetArrayPointer_Serial(IntPtr vec);586 587 [DllImport("sundials_cvodes.dll", EntryPoint = "N_VCloneVectorArray_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]588 public static extern IntPtr N_VCloneVectorArray_Serial(int count, IntPtr vec); // returns N_Vector* !589 590 591 /*592 #define NV_CONTENT_S(v) ( (N_VectorContent_Serial)(v->content) )593 594 #define NV_LENGTH_S(v) ( NV_CONTENT_S(v)->length )595 596 #define NV_OWN_DATA_S(v) ( NV_CONTENT_S(v)->own_data )597 598 #define NV_DATA_S(v) ( NV_CONTENT_S(v)->data )599 600 #define NV_Ith_S(v,i) ( NV_DATA_S(v)[i] )601 */602 // methods for macros603 public unsafe static int* NV_CONTENT_S(IntPtr v) {604 int* content = (int*)*(int*)v.ToPointer();605 return content;606 }607 608 public unsafe static long NV_LENGTH_S(IntPtr v) {609 long length = *NV_CONTENT_S(v);610 return length;611 }612 public unsafe static bool NV_OWN_DATA_S(IntPtr v) {613 var content = NV_CONTENT_S(v);614 int own_data = *(content + 2);615 return own_data > 0;616 }617 public unsafe static double* NV_DATA_S(IntPtr v) {618 var content = NV_CONTENT_S(v);619 double* data = (double*)*(content + 3);620 return data;621 }622 public unsafe static double NV_Get_Ith_S(IntPtr v, long i) {623 return NV_DATA_S(v)[i];624 }625 public unsafe static void NV_Set_Ith_S(IntPtr v, long i, double val) {626 NV_DATA_S(v)[i] = val;627 }628 #endregion629 630 133 631 134
Note: See TracChangeset
for help on using the changeset viewer.