Completed runcvsRoberts_ASAi_dns().
[mipav.git] / mipav / src / gov / nih / mipav / model / algorithms / CVODES.java
index 1b809b2..86e7506 100644 (file)
@@ -173,6 +173,8 @@ public abstract class CVODES {
        final int CVApolynomialStorePnt_select = 2;\r
        final int CVAhermiteGetY_select = 1;\r
        final int CVApolynomialGetY_select = 2;\r
+       final int CVAhermiteFree_select = 1;\r
+       final int CVApolynomialFree_select = 2;\r
 \r
        /* \r
         * ----------------------------------------\r
@@ -627,6 +629,9 @@ public abstract class CVODES {
        final double P1_DTOUT = 2.214773875;\r
        final double P1_TOL_FACTOR = 1.0E4;\r
        \r
+       // Used in CVAfindIndex\r
+       private long ilast = 0;\r
+       \r
        // Use the following code to call CVODES or CVODES_ASA from another module:\r
        /*boolean testme = true;\r
        Use only one of the following 2 lines\r
@@ -10722,7 +10727,7 @@ else                return(snrm);
          * sensitivity computations by CVodeSensInit.\r
          */\r
 \r
-        private void CVodeFree(CVodeMemRec cv_mem)\r
+        protected void CVodeFree(CVodeMemRec cv_mem)\r
         {\r
 \r
           if (cv_mem == null) return;\r
@@ -10735,7 +10740,7 @@ else                return(snrm);
 \r
           CVodeQuadSensFree(cv_mem);\r
 \r
-          //CVodeAdjFree(cv_mem);\r
+          CVodeAdjFree(cv_mem);\r
 \r
           cvDlsFree(cv_mem);\r
 \r
@@ -10750,6 +10755,242 @@ else                return(snrm);
           \r
           cv_mem = null;\r
         }\r
+        \r
+        /*\r
+         * CVodeAdjFree\r
+         *\r
+         * This routine frees the memory allocated by CVodeAdjInit.\r
+         */\r
+\r
+        private void CVodeAdjFree(CVodeMemRec cv_mem)\r
+        {\r
+          CVadjMemRec ca_mem;\r
+          int i;\r
+          \r
+          if (cv_mem == null) return;\r
+\r
+          if (cv_mem.cv_adjMallocDone) {\r
+\r
+            ca_mem = cv_mem.cv_adj_mem;\r
+\r
+            /* Delete check points one by one */\r
+            while (ca_mem.ck_mem != null) CVAckpntDelete(ca_mem.ck_mem);\r
+\r
+            /* Free vectors at all data points */\r
+            if (ca_mem.ca_IMmallocDone) {\r
+              if (ca_mem.ca_IMfree == CVAhermiteFree_select) {\r
+                  CVAhermiteFree(cv_mem);\r
+              }\r
+              else {\r
+                  CVApolynomialFree(cv_mem);\r
+              }\r
+            }\r
+            for(i=0; i<=ca_mem.ca_nsteps; i++) {\r
+              ca_mem.dt_mem[i] = null;\r
+            }\r
+            ca_mem.dt_mem = null;\r
+\r
+            /* Delete backward problems one by one */\r
+            while (ca_mem.cvB_mem != null) CVAbckpbDelete(ca_mem.cvB_mem);\r
+\r
+            /* Free CVODEA memory */\r
+            ca_mem = null;\r
+            cv_mem.cv_adj_mem = null;\r
+\r
+          }\r
+\r
+        }\r
+        \r
+        private void CVAbckpbDelete(CVodeBMemRec cvB_memPtr)\r
+        {\r
+          CVodeBMemRec tmp;\r
+          CVodeMemRec cvode_mem;\r
+\r
+          if (cvB_memPtr != null) {\r
+\r
+            /* Save head of the list */\r
+            tmp = cvB_memPtr;\r
+\r
+            /* Move head of the list */\r
+            cvB_memPtr = cvB_memPtr.cv_next;\r
+\r
+            /* Free CVODES memory in tmp */\r
+            cvode_mem = tmp.cv_mem;\r
+            CVodeFree(cvode_mem);\r
+\r
+            /* Free linear solver memory */\r
+            if (tmp.cv_lfree == cvDlsFreeB_select) cvDlsFreeB(tmp);\r
+\r
+            /* Free preconditioner memory */\r
+            //if (tmp.cv_pfree != NULL) tmp.cv_pfree(tmp);\r
+\r
+            /* Free workspace Nvector */\r
+            N_VDestroy(tmp.cv_y);\r
+\r
+            tmp = null;\r
+\r
+          }\r
+\r
+        }\r
+        \r
+        private int cvDlsFreeB(CVodeBMemRec cvB_mem)\r
+        {\r
+          CVDlsMemRecB cvdlsB_mem;\r
+\r
+          /* Return immediately if IDAB_mem or IDAB_mem->ida_lmem are NULL */\r
+          if (cvB_mem == null)  return (CVDLS_SUCCESS);\r
+          if (cvB_mem.cv_lmem == null)  return(CVDLS_SUCCESS);\r
+          cvdlsB_mem = cvB_mem.cv_lmem;\r
+\r
+          /* free CVDlsMemB interface structure */\r
+          cvdlsB_mem = null;\r
+          \r
+          return(CVDLS_SUCCESS);\r
+        }\r
+\r
+\r
+        \r
+        /*\r
+         * CVAhermiteFree\r
+         *\r
+         * This routine frees the memory allocated for data storage.\r
+         */\r
+\r
+        private void CVAhermiteFree(CVodeMemRec cv_mem)\r
+        {  \r
+          CVadjMemRec ca_mem;\r
+          DtpntMemRec dt_mem[];\r
+          HermiteDataMemRec content;\r
+          int i;\r
+\r
+          ca_mem = cv_mem.cv_adj_mem;\r
+\r
+          N_VDestroy(ca_mem.ca_ytmp);\r
+\r
+          if (ca_mem.ca_IMstoreSensi) {\r
+            N_VDestroyVectorArray(ca_mem.ca_yStmp, cv_mem.cv_Ns);\r
+          }\r
+\r
+          dt_mem = ca_mem.dt_mem;\r
+\r
+          for (i=0; i<=ca_mem.ca_nsteps; i++) {\r
+            content = dt_mem[i].hermiteContent;\r
+            N_VDestroy(content.y);\r
+            N_VDestroy(content.yd);\r
+            if (ca_mem.ca_IMstoreSensi) {\r
+              N_VDestroyVectorArray(content.yS, cv_mem.cv_Ns);\r
+              N_VDestroyVectorArray(content.ySd, cv_mem.cv_Ns);\r
+            }\r
+            dt_mem[i].hermiteContent = null;\r
+          }\r
+        }\r
+        \r
+        /*\r
+         * CVApolynomialFree\r
+         *\r
+         * This routine frees the memeory allocated for data storage.\r
+         */\r
+\r
+        private void CVApolynomialFree(CVodeMemRec cv_mem)\r
+        {\r
+          CVadjMemRec ca_mem;\r
+          DtpntMemRec dt_mem[];\r
+          PolynomialDataMemRec content;\r
+          int i;\r
+\r
+          ca_mem = cv_mem.cv_adj_mem;\r
+\r
+          N_VDestroy(ca_mem.ca_ytmp);\r
+\r
+          if (ca_mem.ca_IMstoreSensi) {\r
+            N_VDestroyVectorArray(ca_mem.ca_yStmp, cv_mem.cv_Ns);\r
+          }\r
+\r
+          dt_mem = ca_mem.dt_mem;\r
+\r
+          for (i=0; i<=ca_mem.ca_nsteps; i++) {\r
+            content = dt_mem[i].polynomialContent;\r
+            N_VDestroy(content.y);\r
+            if (ca_mem.ca_IMstoreSensi) {\r
+              N_VDestroyVectorArray(content.yS, cv_mem.cv_Ns);\r
+            }\r
+            dt_mem[i].polynomialContent = null;\r
+          }\r
+        }\r
+\r
+\r
+        \r
+        /*\r
+         * CVAckpntDelete\r
+         *\r
+         * This routine deletes the first check point in list and returns\r
+         * the new list head\r
+         */\r
+\r
+        private void CVAckpntDelete(CkpntMemRec ck_memPtr)\r
+        {\r
+          CkpntMemRec tmp;\r
+          int j;\r
+\r
+          if (ck_memPtr == null) return;\r
+\r
+          /* store head of list */\r
+          tmp = ck_memPtr;\r
+\r
+          /* move head of list */\r
+          ck_memPtr = ck_memPtr.ck_next;\r
+\r
+          /* free N_Vectors in tmp */\r
+          for (j=0;j<=tmp.ck_q;j++) N_VDestroy(tmp.ck_zn[j]);\r
+          if (tmp.ck_zqm != 0) N_VDestroy(tmp.ck_zn[tmp.ck_zqm]);\r
+\r
+          /* free N_Vectors for quadratures in tmp \r
+           * Note that at the check point at t_initial, only znQ_[0] \r
+           * was allocated */\r
+          if (tmp.ck_quadr) {\r
+\r
+            if (tmp.ck_next != null) {\r
+              for (j=0;j<=tmp.ck_q;j++) N_VDestroy(tmp.ck_znQ[j]);\r
+              if (tmp.ck_zqm != 0) N_VDestroy(tmp.ck_znQ[tmp.ck_zqm]);\r
+            } else {\r
+              N_VDestroy(tmp.ck_znQ[0]);\r
+            }\r
+            \r
+          }\r
+\r
+          /* free N_Vectors for sensitivities in tmp\r
+           * Note that at the check point at t_initial, only znS_[0] \r
+           * was allocated */\r
+          if (tmp.ck_sensi) {\r
+            \r
+            if (tmp.ck_next != null) {\r
+              for (j=0;j<=tmp.ck_q;j++) N_VDestroyVectorArray(tmp.ck_znS[j], tmp.ck_Ns);\r
+              if (tmp.ck_zqm != 0) N_VDestroyVectorArray(tmp.ck_znS[tmp.ck_zqm], tmp.ck_Ns);\r
+            } else {\r
+              N_VDestroyVectorArray(tmp.ck_znS[0], tmp.ck_Ns);\r
+            }\r
+            \r
+          }\r
+\r
+          /* free N_Vectors for quadrature sensitivities in tmp\r
+           * Note that at the check point at t_initial, only znQS_[0] \r
+           * was allocated */\r
+          if (tmp.ck_quadr_sensi) {\r
+            \r
+            if (tmp.ck_next != null) {\r
+              for (j=0;j<=tmp.ck_q;j++) N_VDestroyVectorArray(tmp.ck_znQS[j], tmp.ck_Ns);\r
+              if (tmp.ck_zqm != 0) N_VDestroyVectorArray(tmp.ck_znQS[tmp.ck_zqm], tmp.ck_Ns);\r
+            } else {\r
+              N_VDestroyVectorArray(tmp.ck_znQS[0], tmp.ck_Ns);\r
+            }\r
+            \r
+          }\r
+\r
+          tmp = null;\r
+\r
+        }\r
+\r
+\r
 \r
         \r
         /*  \r
@@ -10985,7 +11226,7 @@ else                return(snrm);
 \r
           }\r
           \r
-          private int SUNLinSolFree_Dense(SUNLinearSolver S)\r
+          protected int SUNLinSolFree_Dense(SUNLinearSolver S)\r
           {\r
             /* return if S is already free */\r
             if (S == null)\r
@@ -11962,8 +12203,8 @@ else                return(snrm);
                  int ca_IMtype;\r
 \r
                  /* Functions set by the interpolation module */\r
-                 int   ca_IMmalloc; \r
-                 //cvaIMFreeFn     ca_IMfree;\r
+                 int ca_IMmalloc; \r
+                 int ca_IMfree;\r
                  int ca_IMstore; /* store a new interpolation point */\r
                  int ca_IMget;   /* interpolate forward solution    */\r
 \r
@@ -12032,7 +12273,7 @@ else                return(snrm);
 \r
                  /* Function to free any memory allocated by the linear solver */\r
                  //int (*cv_lfree)(CVodeBMem cvB_mem);\r
-                 int cv_lfree_select;\r
+                 int cv_lfree;\r
 \r
                  /* Memory block for a preconditioner's module interface to CVODEA */ \r
                  //void *cv_pmem;\r
@@ -12354,7 +12595,6 @@ else                return(snrm);
                                        long indx[], boolean newpoint[])\r
                {\r
                  CVadjMemRec ca_mem;\r
-                 long ilast = 0;\r
                  DtpntMemRec dt_mem[];\r
                  int sign;\r
                  boolean to_left, to_right;\r