1 | using System;
|
---|
2 | using HeuristicLab.Problems.DynamicalSystemsModelling;
|
---|
3 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
4 |
|
---|
5 | namespace AutoDiffForDynamicalModelsTest {
|
---|
6 | [TestClass]
|
---|
7 | public class TestCvodes {
|
---|
8 | [TestMethod]
|
---|
9 | public unsafe void TestCvodesMethod() {
|
---|
10 | // test vectors
|
---|
11 | var arr = new double[] { 3.14, 2.71 };
|
---|
12 | var vec = Problem.N_VMake_Serial(2, arr);
|
---|
13 | Problem.N_VDestroy_Serial(vec);
|
---|
14 |
|
---|
15 | vec = Problem.N_VNew_Serial(10);
|
---|
16 |
|
---|
17 | unsafe {
|
---|
18 | int* content = (int*)*(int*)vec.ToPointer();
|
---|
19 | long length = *content;
|
---|
20 | Console.WriteLine(*content);
|
---|
21 | int own_data = *(content + 2);
|
---|
22 | Console.WriteLine(own_data);
|
---|
23 | double* data = (double*) *(content + 3);
|
---|
24 | double data0 = *data;
|
---|
25 | }
|
---|
26 | Problem.N_VConst_Serial(2.0, vec);
|
---|
27 | Problem.N_VPrint_Serial(vec);
|
---|
28 | Assert.AreEqual(20, Problem.N_VL1Norm_Serial(vec));
|
---|
29 | Problem.N_VDestroy_Serial(vec);
|
---|
30 |
|
---|
31 |
|
---|
32 | // linear oscillator
|
---|
33 | int numberOfEquations = 2;
|
---|
34 | var y = Problem.N_VNew_Serial(numberOfEquations);
|
---|
35 | // y must be initialized before calling CVodeInit
|
---|
36 | // Problem.N_VConst_Serial(100.0, y);
|
---|
37 | Problem.NV_Set_Ith_S(y, 0, 0.5); // x
|
---|
38 | Problem.NV_Set_Ith_S(y, 1, 1); // v
|
---|
39 |
|
---|
40 | var cvode_mem = Problem.CVodeCreate(Problem.MultistepMethod.CV_ADAMS, Problem.NonlinearSolverIteration.CV_FUNCTIONAL);
|
---|
41 |
|
---|
42 | var flag = Problem.CVodeInit(cvode_mem, F, 0.0, y);
|
---|
43 | Assert.AreEqual(Problem.CV_SUCCESS, flag);
|
---|
44 |
|
---|
45 | flag = Problem.CVodeSStolerances(cvode_mem, 1E-4, 1.0);
|
---|
46 | Assert.AreEqual(Problem.CV_SUCCESS, flag);
|
---|
47 |
|
---|
48 | var A = Problem.SUNDenseMatrix(numberOfEquations, numberOfEquations);
|
---|
49 | Assert.AreNotSame(A, IntPtr.Zero);
|
---|
50 |
|
---|
51 |
|
---|
52 | var linearSolver = Problem.SUNDenseLinearSolver(y, A);
|
---|
53 | Assert.AreNotSame(linearSolver, IntPtr.Zero);
|
---|
54 |
|
---|
55 | flag = Problem.CVDlsSetLinearSolver(cvode_mem, linearSolver, A);
|
---|
56 | Assert.AreEqual(Problem.CV_SUCCESS, flag);
|
---|
57 |
|
---|
58 | flag = Problem.CVDlsSetJacFn(cvode_mem, JacF);
|
---|
59 | Assert.AreEqual(Problem.CV_SUCCESS, flag);
|
---|
60 |
|
---|
61 |
|
---|
62 | double t = 0.0;
|
---|
63 | double tout = 0.1; // first output time
|
---|
64 | int nout = 100; // number of output times
|
---|
65 | for (int iout = 0; iout < nout; iout++) {
|
---|
66 | flag = Problem.CVode(cvode_mem, tout, y, ref t, Problem.CV_NORMAL);
|
---|
67 | Assert.AreEqual(Problem.CV_SUCCESS, flag);
|
---|
68 | Console.WriteLine("{0} {1} {2}", t, Problem.NV_Get_Ith_S(y, 0), Problem.NV_Get_Ith_S(y, 1));
|
---|
69 | // Problem.N_VPrint_Serial(y);
|
---|
70 | tout += 0.1;
|
---|
71 | }
|
---|
72 |
|
---|
73 | Problem.N_VDestroy_Serial(y);
|
---|
74 | Problem.CVodeFree(cvode_mem);
|
---|
75 | Problem.SUNLinSolFree(linearSolver);
|
---|
76 | Problem.SUNMatDestroy(A);
|
---|
77 | }
|
---|
78 |
|
---|
79 | private int JacF(double t, IntPtr y, IntPtr fy, IntPtr Jac, IntPtr user_data, IntPtr tmp1, IntPtr tmp2, IntPtr tmp3) {
|
---|
80 | // TODO
|
---|
81 | return 0;
|
---|
82 | }
|
---|
83 |
|
---|
84 | public static int F(
|
---|
85 | double t, // realtype
|
---|
86 | IntPtr y, // N_Vector
|
---|
87 | IntPtr ydot, // N_Vector
|
---|
88 | IntPtr user_data) {
|
---|
89 |
|
---|
90 | Problem.NV_Set_Ith_S(ydot, 0, Problem.NV_Get_Ith_S(y, 1));
|
---|
91 | Problem.NV_Set_Ith_S(ydot, 1, -0.3 * Problem.NV_Get_Ith_S(y, 0));
|
---|
92 | return 0;
|
---|
93 | ;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|