summaryrefslogtreecommitdiff
path: root/packages/sundials/src/Numeric/Sundials/ARKode
diff options
context:
space:
mode:
authorDominic Steinitz <dominic@steinitz.org>2018-05-17 10:08:57 +0100
committerDominic Steinitz <dominic@steinitz.org>2018-05-17 10:08:57 +0100
commitb2c2bb17b6fa652ea09b2da4ca0375258e2efa3a (patch)
tree92551342bc7da2426951583249a2e906ab27fb9f /packages/sundials/src/Numeric/Sundials/ARKode
parent1675813d8f540af9832a78c7a7a40bbdf1cec42c (diff)
Return with error code if solver fails
Diffstat (limited to 'packages/sundials/src/Numeric/Sundials/ARKode')
-rw-r--r--packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs47
1 files changed, 20 insertions, 27 deletions
diff --git a/packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs b/packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs
index fafc237..48ac887 100644
--- a/packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs
+++ b/packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs
@@ -428,7 +428,7 @@ odeSolveVWith ::
428 -> Matrix Double -- ^ Error code or solution 428 -> Matrix Double -- ^ Error code or solution
429odeSolveVWith method control initStepSize f y0 tt = 429odeSolveVWith method control initStepSize f y0 tt =
430 case odeSolveVWith' opts method control initStepSize f y0 tt of 430 case odeSolveVWith' opts method control initStepSize f y0 tt of
431 Left c -> error $ show c -- FIXME 431 Left (c, _v) -> error $ show c -- FIXME
432 Right (v, _d) -> v 432 Right (v, _d) -> v
433 where 433 where
434 opts = ODEOpts { maxNumSteps = 10000 434 opts = ODEOpts { maxNumSteps = 10000
@@ -452,14 +452,14 @@ odeSolveVWith' ::
452 -> (Double -> V.Vector Double -> V.Vector Double) -- ^ The RHS of the system \(\dot{y} = f(t,y)\) 452 -> (Double -> V.Vector Double -> V.Vector Double) -- ^ The RHS of the system \(\dot{y} = f(t,y)\)
453 -> V.Vector Double -- ^ Initial conditions 453 -> V.Vector Double -- ^ Initial conditions
454 -> V.Vector Double -- ^ Desired solution times 454 -> V.Vector Double -- ^ Desired solution times
455 -> Either Int (Matrix Double, SundialsDiagnostics) -- ^ Error code or solution 455 -> Either (Matrix Double, Int) (Matrix Double, SundialsDiagnostics) -- ^ Error code or solution
456odeSolveVWith' opts method control initStepSize f y0 tt = 456odeSolveVWith' opts method control initStepSize f y0 tt =
457 case solveOdeC (fromIntegral $ maxFail opts) 457 case solveOdeC (fromIntegral $ maxFail opts)
458 (fromIntegral $ maxNumSteps opts) (coerce $ minStep opts) 458 (fromIntegral $ maxNumSteps opts) (coerce $ minStep opts)
459 (fromIntegral $ getMethod method) (coerce initStepSize) jacH (scise control) 459 (fromIntegral $ getMethod method) (coerce initStepSize) jacH (scise control)
460 (coerce f) (coerce y0) (coerce tt) of 460 (coerce f) (coerce y0) (coerce tt) of
461 Left c -> Left $ fromIntegral c 461 Left (v, c) -> Left (reshape l (coerce v), fromIntegral c)
462 Right (v, d) -> Right (reshape l (coerce v), d) 462 Right (v, d) -> Right (reshape l (coerce v), d)
463 where 463 where
464 l = size y0 464 l = size y0
465 scise (X aTol rTol) = coerce (V.replicate l aTol, rTol) 465 scise (X aTol rTol) = coerce (V.replicate l aTol, rTol)
@@ -487,7 +487,8 @@ solveOdeC ::
487 (CDouble -> V.Vector CDouble -> V.Vector CDouble) -- ^ The RHS of the system \(\dot{y} = f(t,y)\) 487 (CDouble -> V.Vector CDouble -> V.Vector CDouble) -- ^ The RHS of the system \(\dot{y} = f(t,y)\)
488 -> V.Vector CDouble -- ^ Initial conditions 488 -> V.Vector CDouble -- ^ Initial conditions
489 -> V.Vector CDouble -- ^ Desired solution times 489 -> V.Vector CDouble -- ^ Desired solution times
490 -> Either CInt ((V.Vector CDouble), SundialsDiagnostics) -- ^ Error code or solution 490 -> Either (V.Vector CDouble, CInt) (V.Vector CDouble, SundialsDiagnostics) -- ^ Partial solution and error code or
491 -- solution and diagnostics
491solveOdeC maxErrTestFails maxNumSteps_ minStep_ method initStepSize 492solveOdeC maxErrTestFails maxNumSteps_ minStep_ method initStepSize
492 jacH (aTols, rTol) fun f0 ts = unsafePerformIO $ do 493 jacH (aTols, rTol) fun f0 ts = unsafePerformIO $ do
493 494
@@ -506,8 +507,6 @@ solveOdeC maxErrTestFails maxNumSteps_ minStep_ method initStepSize
506 nEq = fromIntegral dim 507 nEq = fromIntegral dim
507 nTs :: CInt 508 nTs :: CInt
508 nTs = fromIntegral $ V.length ts 509 nTs = fromIntegral $ V.length ts
509 -- FIXME: I believe this gets taken from the ghc heap and so should
510 -- be subject to garbage collection.
511 quasiMatrixRes <- createVector ((fromIntegral dim) * (fromIntegral nTs)) 510 quasiMatrixRes <- createVector ((fromIntegral dim) * (fromIntegral nTs))
512 qMatMut <- V.thaw quasiMatrixRes 511 qMatMut <- V.thaw quasiMatrixRes
513 diagnostics :: V.Vector CLong <- createVector 10 -- FIXME 512 diagnostics :: V.Vector CLong <- createVector 10 -- FIXME
@@ -644,18 +643,12 @@ solveOdeC maxErrTestFails maxNumSteps_ minStep_ method initStepSize
644 for (i = 1; i < $(int nTs); i++) { 643 for (i = 1; i < $(int nTs); i++) {
645 644
646 flag = ARKode(arkode_mem, ($vec-ptr:(double *ts))[i], y, &t, ARK_NORMAL); /* call integrator */ 645 flag = ARKode(arkode_mem, ($vec-ptr:(double *ts))[i], y, &t, ARK_NORMAL); /* call integrator */
647 if (check_flag(&flag, "ARKode", 1)) break; 646 if (check_flag(&flag, "ARKode solver failure, stopping integration", 1)) return 1;
648 647
649 /* Store the results for Haskell */ 648 /* Store the results for Haskell */
650 for (j = 0; j < NEQ; j++) { 649 for (j = 0; j < NEQ; j++) {
651 ($vec-ptr:(double *qMatMut))[i * NEQ + j] = NV_Ith_S(y,j); 650 ($vec-ptr:(double *qMatMut))[i * NEQ + j] = NV_Ith_S(y,j);
652 } 651 }
653
654 /* unsuccessful solve: break */
655 if (flag < 0) {
656 fprintf(stderr,"Solver failure, stopping integration\n");
657 break;
658 }
659 } 652 }
660 653
661 /* Get some final statistics on how the solve progressed */ 654 /* Get some final statistics on how the solve progressed */
@@ -706,23 +699,23 @@ solveOdeC maxErrTestFails maxNumSteps_ minStep_ method initStepSize
706 699
707 return flag; 700 return flag;
708 } |] 701 } |]
702 preD <- V.freeze diagMut
703 let d = SundialsDiagnostics (fromIntegral $ preD V.!0)
704 (fromIntegral $ preD V.!1)
705 (fromIntegral $ preD V.!2)
706 (fromIntegral $ preD V.!3)
707 (fromIntegral $ preD V.!4)
708 (fromIntegral $ preD V.!5)
709 (fromIntegral $ preD V.!6)
710 (fromIntegral $ preD V.!7)
711 (fromIntegral $ preD V.!8)
712 (fromIntegral $ preD V.!9)
713 m <- V.freeze qMatMut
709 if res == 0 714 if res == 0
710 then do 715 then do
711 preD <- V.freeze diagMut
712 let d = SundialsDiagnostics (fromIntegral $ preD V.!0)
713 (fromIntegral $ preD V.!1)
714 (fromIntegral $ preD V.!2)
715 (fromIntegral $ preD V.!3)
716 (fromIntegral $ preD V.!4)
717 (fromIntegral $ preD V.!5)
718 (fromIntegral $ preD V.!6)
719 (fromIntegral $ preD V.!7)
720 (fromIntegral $ preD V.!8)
721 (fromIntegral $ preD V.!9)
722 m <- V.freeze qMatMut
723 return $ Right (m, d) 716 return $ Right (m, d)
724 else do 717 else do
725 return $ Left res 718 return $ Left (m, res)
726 719
727data ButcherTable = ButcherTable { am :: Matrix Double 720data ButcherTable = ButcherTable { am :: Matrix Double
728 , cv :: Vector Double 721 , cv :: Vector Double