Skip to content

Commit c03a979

Browse files
Copilotivan-pi
andauthored
Add required idid status flag to stiff3 APIs
Agent-Logs-Url: https://github.com/ivan-pi/stiff3/sessions/cfc168cc-31b3-4e0c-9237-d65abc89893e Co-authored-by: ivan-pi <21085643+ivan-pi@users.noreply.github.com>
1 parent 78d2989 commit c03a979

24 files changed

Lines changed: 291 additions & 48 deletions

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This repository provides a heavily refactored version of the original implementa
1515

1616
- Third-order Rosenbrock-type (semi-implicit Runge-Kutta) integrator suitable for stiff ODE systems
1717
- Adaptive stepsize control with error tolerance per component
18+
- Required integer exit flag (`idid`) reporting success and early-exit conditions
1819
- Optional maximum absolute half-step size (`hmax`) to cap step growth
1920
- Optional runtime statistics output: successful steps (`nacc`), rejected steps (`nrej`), rhs evaluations (`nfev`), Jacobian evaluations (`njev`), LU decompositions (`nlu`), and linear-system solves (`nsol`)
2021
- Optional explicit workspace interface via `stiff3(..., rwork, iwork, ...)` for caller-managed memory
@@ -103,7 +104,7 @@ program vanpol
103104
integer, parameter :: n = 2
104105
real(wp), parameter :: mu = 10.0_wp
105106
real(wp) :: y(n), w(n), x0, x, x1, h0, eps, hmax
106-
integer :: stats(6)
107+
integer :: idid, stats(6)
107108
108109
! initial value
109110
y = [1.0_wp, 1.0_wp]
@@ -119,8 +120,9 @@ program vanpol
119120
x = x0
120121
x1 = 100.0_wp
121122
! integrate system of ODEs
122-
call stiff3(n,fun,x,y,x1,jac,h0,eps,w,solout=out,stats=stats,hmax=hmax)
123-
! on successful exit, x == x1
123+
call stiff3(n,fun,x,y,x1,jac,h0,eps,w,idid,solout=out,stats=stats,hmax=hmax)
124+
if (idid /= 0) error stop 'stiff3 failed'
125+
! on successful exit, idid == 0 and x == x1
124126
print '(A)', 'Solver statistics'
125127
print '(A,I0)', ' nacc: ', stats(1)
126128
print '(A,I0)', ' nrej: ', stats(2)
@@ -180,7 +182,17 @@ The two tolerance parameters `eps` and `w` together control how accurately the s
180182

181183
A reasonable first choice is `eps = 1.0e-4` with all `w(i) = 1.0`, which requests roughly four significant digits from every component.
182184

183-
For interoperability scenarios where the caller manages memory allocation (e.g. language bindings), `stiff3` also provides an overload with explicit work arrays: `rwork` of size `n*(7 + 2*n)` and `iwork` of size `n`.
185+
For interoperability scenarios where the caller manages memory allocation (e.g. language bindings), `stiff3` also provides an overload with explicit work arrays: `rwork` of size `n*(7 + 2*n)` and `iwork` of size `n`. The required `idid` argument appears before the optional arguments in both overloads:
186+
187+
- `call stiff3(n, fun, x, y, x1, jac, h0, eps, w, idid, [solout=...], [stats=...], [hmax=...])`
188+
- `call stiff3(n, fun, x, y, x1, jac, h0, eps, w, rwork, iwork, idid, [solout=...], [stats=...], [hmax=...])`
189+
190+
The integer exit flag `idid` reports solver status:
191+
192+
- `0` — successful completion at `x1`
193+
- `-1` — LU factorization failed because the Jacobian system matrix was singular
194+
- `-2` — integration interrupted by the user `solout` callback (`irtrn < 0`)
195+
- `-3` — step-size underflow occurred during bisection (`abs(h) <= spacing(x_current)`)
184196

185197
When using this explicit-workspace overload together with `solout`, accepted-step dense output is available through the generic interface `stiff3_interp`:
186198

example/e5_chemical.f90

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ subroutine integrate_case(eps, y, stats, err, elapsed)
6565
real(wp), intent(out), optional :: err, elapsed
6666

6767
real(wp) :: h0, t0, t1, x
68+
integer :: idid
6869

6970
y = y0
7071
x = x0
7172
h0 = h0_initial
7273
call cpu_time(t0)
73-
call stiff3(n, fun, x, y, x1, jac, h0, eps, w, stats=stats)
74+
call stiff3(n, fun, x, y, x1, jac, h0, eps, w, idid, stats=stats)
7475
call cpu_time(t1)
76+
if (idid /= 0) error stop 'stiff3 failed in e5_chemical example'
7577
if (present(err)) err = max_relative_error(y, yref)
7678
if (present(elapsed)) elapsed = max(t1 - t0, 0.0_wp)
7779
end subroutine

example/hires.f90

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ subroutine integrate_case(eps, y, stats, err, cpu_time_seconds)
6868

6969
real(wp), parameter :: min_cpu_time = 1.0e-12_wp
7070
real(wp) :: h0, cpu_start, cpu_end, x
71+
integer :: idid
7172

7273
y = y0
7374
x = x0
7475
h0 = h0_initial
7576
call cpu_time(cpu_start)
76-
call stiff3(n, fun, x, y, x1, jac, h0, eps, w, stats=stats)
77+
call stiff3(n, fun, x, y, x1, jac, h0, eps, w, idid, stats=stats)
7778
call cpu_time(cpu_end)
79+
if (idid /= 0) error stop 'stiff3 failed in hires example'
7880
if (present(err)) err = max_relative_error(y, yref)
7981
if (present(cpu_time_seconds)) cpu_time_seconds = max(min_cpu_time, cpu_end - cpu_start)
8082
end subroutine

example/lorenz.f90

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ program lorenz
77
integer, parameter :: n = 3
88
real(wp), parameter :: sigma = 10.0_wp, rho = 28.0_wp, beta = 8.0_wp/3.0_wp
99
real(wp) :: y(n), w(n), x0, x1, h0, eps
10+
integer :: idid
1011

1112
! initial value
1213
y = [1.0_wp, 1.0_wp, 1.0_wp]
@@ -19,7 +20,8 @@ program lorenz
1920
x0 = 0.0_wp
2021
x1 = 40.0_wp
2122
! integrate system of ODEs
22-
call stiff3(n,fun,x0,y,x1,jac,h0,eps,w,solout=out)
23+
call stiff3(n,fun,x0,y,x1,jac,h0,eps,w,idid,solout=out)
24+
if (idid /= 0) error stop 'stiff3 failed in lorenz example'
2325

2426
contains
2527

example/oregonator.f90

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ subroutine integrate_case(eps, y, stats, cpu_time_seconds, solution_filename)
7070
real(wp), intent(out) :: cpu_time_seconds
7171
character(len=*), intent(in), optional :: solution_filename
7272

73-
integer :: ios
73+
integer :: ios, idid
7474
real(wp), parameter :: min_cpu_time = 1.0e-12_wp
7575
real(wp) :: cpu_start, cpu_end, h0, x
7676
logical :: write_solution
@@ -87,8 +87,9 @@ subroutine integrate_case(eps, y, stats, cpu_time_seconds, solution_filename)
8787
end if
8888

8989
call cpu_time(cpu_start)
90-
call stiff3(n, fun, x, y, x1, jac, h0, eps, w, solout=output_solution, stats=stats)
90+
call stiff3(n, fun, x, y, x1, jac, h0, eps, w, idid, solout=output_solution, stats=stats)
9191
call cpu_time(cpu_end)
92+
if (idid /= 0) error stop 'stiff3 failed in oregonator example'
9293
cpu_time_seconds = max(min_cpu_time, cpu_end - cpu_start)
9394

9495
if (write_solution) then

example/pendant_drop.f90

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ program pendant_drop
77
integer, parameter :: n = 3
88
real(wp) :: y(n), w(n), x0, x1, h0, eps
99
real(wp) :: pL, drho, ds
10-
integer :: stats(6)
10+
integer :: stats(6), idid
1111

1212
! dimensionless pressure offset and density difference
1313
pL = 3.3888_wp
@@ -29,7 +29,8 @@ program pendant_drop
2929
w = 1.0_wp
3030

3131
! integrate Young-Laplace system
32-
call stiff3(n,fun,x0,y,x1,jac,h0,eps,w,solout=out,stats=stats)
32+
call stiff3(n,fun,x0,y,x1,jac,h0,eps,w,idid,solout=out,stats=stats)
33+
if (idid /= 0) error stop 'stiff3 failed in pendant_drop example'
3334
print '(A,3(I0,1X))', 'accepted rejected nfev: ', stats(1), stats(2), stats(3)
3435
print '(A,3(I0,1X))', 'njev nlu nsol: ', stats(4), stats(5), stats(6)
3536

example/predator_prey.f90

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ program predator_prey
88
real(wp), parameter :: alpha = 1.1_wp, beta = 0.4_wp
99
real(wp), parameter :: delta = 0.1_wp, gamma = 0.4_wp
1010
real(wp) :: y(n), w(n), x0, x1, h0, eps
11+
integer :: idid
1112

1213
! initial value
1314
y = [10.0_wp, 5.0_wp]
@@ -20,7 +21,8 @@ program predator_prey
2021
x0 = 0.0_wp
2122
x1 = 40.0_wp
2223
! integrate system of ODEs
23-
call stiff3(n,fun,x0,y,x1,jac,h0,eps,w,solout=out)
24+
call stiff3(n,fun,x0,y,x1,jac,h0,eps,w,idid,solout=out)
25+
if (idid /= 0) error stop 'stiff3 failed in predator_prey example'
2426

2527
contains
2628

example/ring_modulator.f90

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,15 @@ subroutine integrate_case(eps, y, stats, err, cpu_time_seconds)
7676

7777
real(wp), parameter :: min_cpu_time = 1.0e-12_wp
7878
real(wp) :: h0, cpu_start, cpu_end, x
79+
integer :: idid
7980

8081
y = y0
8182
x = x0
8283
h0 = initial_step_scale*eps
8384
call cpu_time(cpu_start)
84-
call stiff3(n, fun, x, y, x1, jac, h0, eps, w, stats=stats)
85+
call stiff3(n, fun, x, y, x1, jac, h0, eps, w, idid, stats=stats)
8586
call cpu_time(cpu_end)
87+
if (idid /= 0) error stop 'stiff3 failed in ring_modulator example'
8688
if (present(err)) err = max_relative_error(y(1:nphys), yref)
8789
if (present(cpu_time_seconds)) cpu_time_seconds = max(min_cpu_time, cpu_end - cpu_start)
8890
end subroutine

example/robertson.f90

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ program main
4848
integer, parameter :: n = 3
4949
real(wp) :: y(n), w(n)
5050
real(wp) :: h0, eps, x0, x1
51+
integer :: idid
5152

5253

5354
! initial value
@@ -65,7 +66,8 @@ program main
6566
x0 = 0.0_wp
6667
x1 = 10.0_wp
6768

68-
call stiff3(n,fun,x0,y,x1,dfun,h0,eps,w,solout=output)
69+
call stiff3(n,fun,x0,y,x1,dfun,h0,eps,w,idid,solout=output)
70+
if (idid /= 0) error stop 'stiff3 failed in robertson example'
6971

7072
contains
7173

example/three_equation_system.f90

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ program three_equation_system
99
real(wp), parameter :: k2 = 1000.0_wp
1010
real(wp), parameter :: k3 = 2500.0_wp
1111
real(wp) :: y(n), w(n), x0, x1, h0, eps
12-
integer :: stats(6)
12+
integer :: stats(6), idid
1313

1414
! initial value
1515
y = [1.0_wp, 1.0_wp, 0.0_wp]
@@ -22,7 +22,8 @@ program three_equation_system
2222
x0 = 0.0_wp
2323
x1 = 50.0_wp
2424

25-
call stiff3(n,fun,x0,y,x1,jac,h0,eps,w,stats=stats)
25+
call stiff3(n,fun,x0,y,x1,jac,h0,eps,w,idid,stats=stats)
26+
if (idid /= 0) error stop 'stiff3 failed in three_equation_system example'
2627
print '(A,3(I0,1X))', 'accepted rejected nfev: ', stats(1), stats(2), stats(3)
2728
print '(A,3(I0,1X))', 'njev nlu nsol: ', stats(4), stats(5), stats(6)
2829

0 commit comments

Comments
 (0)