-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsoda.c
3003 lines (2687 loc) · 76.4 KB
/
lsoda.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Downloaded from: http://lh3lh3.users.sourceforge.net/download/lsoda.c
*/
/*
This is a C version of the LSODA library. I acquired the original
source code from this web page:
http://www.ccl.net/cca/software/SOURCES/C/kinetics2/index.shtml
I merged several C files into one and added a simpler interface. I
also made the array start from zero in functions called by lsoda(),
and fixed two minor bugs: a) small memory leak in freevectors(); and
b) misuse of lsoda() in the example.
The original source code came with no license or copyright
information. I now release this file under the MIT/X11 license. All
authors' notes are kept in this file.
- Heng Li <[email protected]>
*/
/* The MIT License
Copyright (c) 2009 Genome Research Ltd (GRL).
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/* Contact: Heng Li <[email protected]> */
typedef void (*_lsoda_f) (double, double *, double *, void *);
/************
* idamax.c *
************/
#include <math.h>
static int
idamax(n, dx, incx)
double *dx;
int n, incx;
/* Purpose : Find largest component of double vector dx
--- Input ---
n : number of elements in input vector
dx : double vector with n+1 elements, dx[0] is not used
incx : storage spacing between elements of dx
--- Output ---
idamax : smallest index, 0 if n <= 0
Find smallest index of maximum magnitude of dx.
idamax = first i, i=1 to n, to minimize fabs( dx[1-incx+i*incx] ).
*/
{
double dmax, xmag;
int i, ii, xindex;
xindex = 0;
if (n <= 0)
return xindex;
xindex = 1;
if (n <= 1 || incx <= 0)
return xindex;
/* Code for increments not equal to 1. */
if (incx != 1) {
dmax = fabs(dx[1]);
ii = 2;
for (i = 1 + incx; i <= n * incx; i = i + incx) {
xmag = fabs(dx[i]);
if (xmag > dmax) {
xindex = ii;
dmax = xmag;
}
ii++;
}
return xindex;
}
/* Code for increments equal to 1. */
dmax = fabs(dx[1]);
for (i = 2; i <= n; i++) {
xmag = fabs(dx[i]);
if (xmag > dmax) {
xindex = i;
dmax = xmag;
}
}
return xindex;
}
/***********
* dscal.c *
***********/
void
dscal(n, da, dx, incx)
double da, *dx;
int n, incx;
/* Purpose : scalar vector multiplication
dx = da * dx
--- Input ---
n : number of elements in input vector
da : double scale factor
dx : double vector with n+1 elements, dx[0] is not used
incx : storage spacing between elements of dx
--- Output ---
dx = da * dx, unchanged if n <= 0
For i = 0 to n-1, replace dx[1+i*incx] with
da * dx[1+i*incx].
*/
{
int m, i;
if (n <= 0)
return;
/* Code for increments not equal to 1. */
if (incx != 1) {
for (i = 1; i <= n * incx; i = i + incx)
dx[i] = da * dx[i];
return;
}
/* Code for increments equal to 1. */
/* Clean-up loop so remaining vector length is a multiple of 5. */
m = n % 5;
if (m != 0) {
for (i = 1; i <= m; i++)
dx[i] = da * dx[i];
if (n < 5)
return;
}
for (i = m + 1; i <= n; i = i + 5) {
dx[i] = da * dx[i];
dx[i + 1] = da * dx[i + 1];
dx[i + 2] = da * dx[i + 2];
dx[i + 3] = da * dx[i + 3];
dx[i + 4] = da * dx[i + 4];
}
return;
}
/**********
* ddot.c *
**********/
static double
ddot(n, dx, incx, dy, incy)
double *dx, *dy;
int n, incx, incy;
/*
Purpose : Inner product dx . dy
--- Input ---
n : number of elements in input vector(s)
dx : double vector with n+1 elements, dx[0] is not used
incx : storage spacing between elements of dx
dy : double vector with n+1 elements, dy[0] is not used
incy : storage spacing between elements of dy
--- Output ---
ddot : dot product dx . dy, 0 if n <= 0
ddot = sum for i = 0 to n-1 of
dx[lx+i*incx] * dy[ly+i*incy] where lx = 1 if
incx >= 0, else lx = (-incx)*(n-1)+1, and ly
is defined in a similar way using incy.
*/
{
double dotprod;
int ix, iy, i, m;
dotprod = 0.;
if (n <= 0)
return dotprod;
/* Code for unequal or nonpositive increments. */
if (incx != incy || incx < 1) {
ix = 1;
iy = 1;
if (incx < 0)
ix = (-n + 1) * incx + 1;
if (incy < 0)
iy = (-n + 1) * incy + 1;
for (i = 1; i <= n; i++) {
dotprod = dotprod + dx[ix] * dy[iy];
ix = ix + incx;
iy = iy + incy;
}
return dotprod;
}
/* Code for both increments equal to 1. */
/* Clean-up loop so remaining vector length is a multiple of 5. */
if (incx == 1) {
m = n % 5;
if (m != 0) {
for (i = 1; i <= m; i++)
dotprod = dotprod + dx[i] * dy[i];
if (n < 5)
return dotprod;
}
for (i = m + 1; i <= n; i = i + 5)
dotprod = dotprod + dx[i] * dy[i] + dx[i + 1] * dy[i + 1] +
dx[i + 2] * dy[i + 2] + dx[i + 3] * dy[i + 3] +
dx[i + 4] * dy[i + 4];
return dotprod;
}
/* Code for positive equal nonunit increments. */
for (i = 1; i <= n * incx; i = i + incx)
dotprod = dotprod + dx[i] * dy[i];
return dotprod;
}
/***********
* daxpy.c *
***********/
/*
From [email protected] Wed Apr 24 15:48:31 1991
Return-Path: <tam>
Date: Wed, 24 Apr 91 17:48:43 CDT
From: [email protected]
*/
static void
daxpy(n, da, dx, incx, dy, incy)
double da, *dx, *dy;
int n, incx, incy;
/*
Purpose : To compute
dy = da * dx + dy
--- Input ---
n : number of elements in input vector(s)
da : double scalar multiplier
dx : double vector with n+1 elements, dx[0] is not used
incx : storage spacing between elements of dx
dy : double vector with n+1 elements, dy[0] is not used
incy : storage spacing between elements of dy
--- Output ---
dy = da * dx + dy, unchanged if n <= 0
For i = 0 to n-1, replace dy[ly+i*incy] with
da*dx[lx+i*incx] + dy[ly+i*incy], where lx = 1
if incx >= 0, else lx = (-incx)*(n-1)+1 and ly is
defined in a similar way using incy.
*/
{
int ix, iy, i, m;
if (n < 0 || da == 0.)
return;
/* Code for nonequal or nonpositive increments. */
if (incx != incy || incx < 1) {
ix = 1;
iy = 1;
if (incx < 0)
ix = (-n + 1) * incx + 1;
if (incy < 0)
iy = (-n + 1) * incy + 1;
for (i = 1; i <= n; i++) {
dy[iy] = dy[iy] + da * dx[ix];
ix = ix + incx;
iy = iy + incy;
}
return;
}
/* Code for both increments equal to 1. */
/* Clean-up loop so remaining vector length is a multiple of 4. */
if (incx == 1) {
m = n % 4;
if (m != 0) {
for (i = 1; i <= m; i++)
dy[i] = dy[i] + da * dx[i];
if (n < 4)
return;
}
for (i = m + 1; i <= n; i = i + 4) {
dy[i] = dy[i] + da * dx[i];
dy[i + 1] = dy[i + 1] + da * dx[i + 1];
dy[i + 2] = dy[i + 2] + da * dx[i + 2];
dy[i + 3] = dy[i + 3] + da * dx[i + 3];
}
return;
}
/* Code for equal, positive, nonunit increments. */
for (i = 1; i <= n * incx; i = i + incx)
dy[i] = da * dx[i] + dy[i];
return;
}
/***********
* dgesl.c *
***********/
static void
dgesl(a, n, ipvt, b, job)
double **a, *b;
int n, *ipvt, job;
/*
Purpose : dgesl solves the linear system
a * x = b or Transpose(a) * x = b
using the factors computed by dgeco or degfa.
On Entry :
a : double matrix of dimension ( n+1, n+1 ),
the output from dgeco or dgefa.
The 0-th row and column are not used.
n : the row dimension of a.
ipvt : the pivot vector from degco or dgefa.
b : the right hand side vector.
job : = 0 to solve a * x = b,
= nonzero to solve Transpose(a) * x = b.
On Return :
b : the solution vector x.
Error Condition :
A division by zero will occur if the input factor contains
a zero on the diagonal. Technically this indicates
singularity but it is often caused by improper argments or
improper setting of the pointers of a. It will not occur
if the subroutines are called correctly and if dgeco has
set rcond > 0 or dgefa has set info = 0.
BLAS : daxpy, ddot
*/
{
int nm1, k, j;
double t;
nm1 = n - 1;
/*
Job = 0, solve a * x = b.
*/
if (job == 0) {
/*
First solve L * y = b.
*/
for (k = 1; k <= n; k++) {
t = ddot(k - 1, a[k], 1, b, 1);
b[k] = (b[k] - t) / a[k][k];
}
/*
Now solve U * x = y.
*/
for (k = n - 1; k >= 1; k--) {
b[k] = b[k] + ddot(n - k, a[k] + k, 1, b + k, 1);
j = ipvt[k];
if (j != k) {
t = b[j];
b[j] = b[k];
b[k] = t;
}
}
return;
}
/*
Job = nonzero, solve Transpose(a) * x = b.
First solve Transpose(U) * y = b.
*/
for (k = 1; k <= n - 1; k++) {
j = ipvt[k];
t = b[j];
if (j != k) {
b[j] = b[k];
b[k] = t;
}
daxpy(n - k, t, a[k] + k, 1, b + k, 1);
}
/*
Now solve Transpose(L) * x = y.
*/
for (k = n; k >= 1; k--) {
b[k] = b[k] / a[k][k];
t = -b[k];
daxpy(k - 1, t, a[k], 1, b, 1);
}
}
/***********
* dgefa.c *
***********/
void
dgefa(a, n, ipvt, info)
double **a;
int n, *ipvt, *info;
/*
Purpose : dgefa factors a double matrix by Gaussian elimination.
dgefa is usually called by dgeco, but it can be called directly
with a saving in time if rcond is not needed.
(Time for dgeco) = (1+9/n)*(time for dgefa).
This c version uses algorithm kji rather than the kij in dgefa.f.
Note that the fortran version input variable lda is not needed.
On Entry :
a : double matrix of dimension ( n+1, n+1 ),
the 0-th row and column are not used.
a is created using NewDoubleMatrix, hence
lda is unnecessary.
n : the row dimension of a.
On Return :
a : a lower triangular matrix and the multipliers
which were used to obtain it. The factorization
can be written a = L * U where U is a product of
permutation and unit upper triangular matrices
and L is lower triangular.
ipvt : an n+1 integer vector of pivot indices.
*info : = 0 normal value,
= k if U[k][k] == 0. This is not an error
condition for this subroutine, but it does
indicate that dgesl or dgedi will divide by
zero if called. Use rcond in dgeco for
a reliable indication of singularity.
Notice that the calling program must use &info.
BLAS : daxpy, dscal, idamax
*/
{
int j, k, i;
double t;
/* Gaussian elimination with partial pivoting. */
*info = 0;
for (k = 1; k <= n - 1; k++) {
/*
Find j = pivot index. Note that a[k]+k-1 is the address of
the 0-th element of the row vector whose 1st element is a[k][k].
*/
j = idamax(n - k + 1, a[k] + k - 1, 1) + k - 1;
ipvt[k] = j;
/*
Zero pivot implies this row already triangularized.
*/
if (a[k][j] == 0.) {
*info = k;
continue;
}
/*
Interchange if necessary.
*/
if (j != k) {
t = a[k][j];
a[k][j] = a[k][k];
a[k][k] = t;
}
/*
Compute multipliers.
*/
t = -1. / a[k][k];
dscal(n - k, t, a[k] + k, 1);
/*
Column elimination with row indexing.
*/
for (i = k + 1; i <= n; i++) {
t = a[i][j];
if (j != k) {
a[i][j] = a[i][k];
a[i][k] = t;
}
daxpy(n - k, t, a[k] + k, 1, a[i] + k, 1);
}
} /* end k-loop */
ipvt[n] = n;
if (a[n][n] == 0.)
*info = n;
}
/***********
* lsoda.c *
***********/
/*
From [email protected] Wed Apr 24 01:35:52 1991
Return-Path: <tam>
Date: Wed, 24 Apr 91 03:35:24 CDT
From: [email protected]
Subject: lsoda.c
I'm told by Steve Nichols at Georgia Tech that you are interested in
a stiff integrator. Here's a translation of the fortran code LSODA.
Please note
that there is no comment. The interface is the same as the FORTRAN
code and I believe the documentation in LSODA will suffice.
As usual, a free software comes with no guarantee.
Hon Wah Tam
Wolfram Research, Inc.
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define max( a , b ) ( (a) > (b) ? (a) : (b) )
#define min( a , b ) ( (a) < (b) ? (a) : (b) )
#define ETA 2.2204460492503131e-16
static void stoda(int neq, double *y, _lsoda_f f, void *_data);
static void correction(int neq, double *y, _lsoda_f f, int *corflag, double pnorm, double *del, double *delp, double *told,
int *ncf, double *rh, int *m, void *_data);
static void prja(int neq, double *y, _lsoda_f f, void *_data);
static void terminate(int *istate);
static void terminate2(double *y, double *t);
static void successreturn(double *y, double *t, int itask, int ihit, double tcrit, int *istate);
static void freevectors(void); /* this function does nothing */
static void _freevectors(void);
static void ewset(int itol, double *rtol, double *atol, double *ycur);
static void resetcoeff(void);
static void solsy(double *y);
static void endstoda(void);
static void orderswitch(double *rhup, double dsm, double *pdh, double *rh, int *orderflag);
static void intdy(double t, int k, double *dky, int *iflag);
static void corfailure(double *told, double *rh, int *ncf, int *corflag);
static void methodswitch(double dsm, double pnorm, double *pdh, double *rh);
static void cfode(int meth);
static void scaleh(double *rh, double *pdh);
static double fnorm(int n, double **a, double *w);
static double vmnorm(int n, double *v, double *w);
static int g_nyh = 0, g_lenyh = 0;
/* newly added static variables */
static int ml, mu, imxer;
static int mord[3] = {0, 12, 5};
static double sqrteta, *yp1, *yp2;
static double sm1[13] = {0., 0.5, 0.575, 0.55, 0.45, 0.35, 0.25, 0.2, 0.15, 0.1, 0.075, 0.05, 0.025};
/* static variables for lsoda() */
static double ccmax, el0, h, hmin, hmxi, hu, rc, tn;
static int illin = 0, init = 0, mxstep, mxhnil, nhnil, ntrep = 0, nslast, nyh, ierpj, iersl,
jcur, jstart, kflag, l, meth, miter, maxord, maxcor, msbp, mxncf, n, nq, nst,
nfe, nje, nqu;
static double tsw, pdnorm;
static int ixpr = 0, jtyp, mused, mxordn, mxords;
/* no static variable for prja(), solsy() */
/* static variables for stoda() */
static double conit, crate, el[14], elco[13][14], hold, rmax, tesco[13][4];
static int ialth, ipup, lmax, nslp;
static double pdest, pdlast, ratio, cm1[13], cm2[6];
static int icount, irflag;
/* static variables for various vectors and the Jacobian. */
static double **yh, **wm, *ewt, *savf, *acor;
static int *ipvt;
/*
The following are useful statistics.
hu,
h,
tn,
tolsf,
tsw,
nst,
nfe,
nje,
nqu,
nq,
imxer,
mused,
meth
*/
/* Terminate lsoda due to illegal input. */
static void terminate(int *istate)
{
if (illin == 5) {
fprintf(stderr, "[lsoda] repeated occurrence of illegal input. run aborted.. apparent infinite loop\n");
} else {
illin++;
*istate = -3;
}
}
/* Terminate lsoda due to various error conditions. */
static void terminate2(double *y, double *t)
{
int i;
yp1 = yh[1];
for (i = 1; i <= n; i++)
y[i] = yp1[i];
*t = tn;
illin = 0;
freevectors();
return;
}
/*
The following block handles all successful returns from lsoda.
If itask != 1, y is loaded from yh and t is set accordingly.
*Istate is set to 2, the illegal input counter is zeroed, and the
optional outputs are loaded into the work arrays before returning.
*/
static void successreturn(double *y, double *t, int itask, int ihit, double tcrit, int *istate)
{
int i;
yp1 = yh[1];
for (i = 1; i <= n; i++)
y[i] = yp1[i];
*t = tn;
if (itask == 4 || itask == 5)
if (ihit)
*t = tcrit;
*istate = 2;
illin = 0;
freevectors();
}
/*
c-----------------------------------------------------------------------
c this is the march 30, 1987 version of
c lsoda.. livermore solver for ordinary differential equations, with
c automatic method switching for stiff and nonstiff problems.
c
c this version is in double precision.
c
c lsoda solves the initial value problem for stiff or nonstiff
c systems of first order ode-s,
c dy/dt = f(t,y) , or, in component form,
c dy(i)/dt = f(i) = f(i,t,y(1),y(2),...,y(neq)) (i = 1,...,neq).
c
c this a variant version of the lsode package.
c it switches automatically between stiff and nonstiff methods.
c this means that the user does not have to determine whether the
c problem is stiff or not, and the solver will automatically choose the
c appropriate method. it always starts with the nonstiff method.
c
c authors..
c linda r. petzold and alan c. hindmarsh,
c computing and mathematics research division, l-316
c lawrence livermore national laboratory
c livermore, ca 94550.
c
c references..
c 1. alan c. hindmarsh, odepack, a systematized collection of ode
c solvers, in scientific computing, r. s. stepleman et al. (eds.),
c north-holland, amsterdam, 1983, pp. 55-64.
c 2. linda r. petzold, automatic selection of methods for solving
c stiff and nonstiff systems of ordinary differential equations,
c siam j. sci. stat. comput. 4 (1983), pp. 136-148.
c-----------------------------------------------------------------------
c summary of usage.
c
c communication between the user and the lsoda package, for normal
c situations, is summarized here. this summary describes only a subset
c of the full set of options available. see the full description for
c details, including alternative treatment of the jacobian matrix,
c optional inputs and outputs, nonstandard options, and
c instructions for special situations. see also the example
c problem (with program and output) following this summary.
c
c a. first provide a subroutine of the form..
c subroutine f (neq, t, y, ydot)
c dimension y(neq), ydot(neq)
c which supplies the vector function f by loading ydot(i) with f(i).
c
c b. write a main program which calls subroutine lsoda once for
c each point at which answers are desired. this should also provide
c for possible use of logical unit 6 for output of error messages
c by lsoda. on the first call to lsoda, supply arguments as follows..
c f = name of subroutine for right-hand side vector f.
c this name must be declared external in calling program.
c neq = number of first order ode-s.
c y = array of initial values, of length neq.
c t = the initial value of the independent variable.
c tout = first point where output is desired (.ne. t).
c itol = 1 or 2 according as atol (below) is a scalar or array.
c rtol = relative tolerance parameter (scalar).
c atol = absolute tolerance parameter (scalar or array).
c the estimated local error in y(i) will be controlled so as
c to be less than
c ewt(i) = rtol*abs(y(i)) + atol if itol = 1, or
c ewt(i) = rtol*abs(y(i)) + atol(i) if itol = 2.
c thus the local error test passes if, in each component,
c either the absolute error is less than atol (or atol(i)),
c or the relative error is less than rtol.
c use rtol = 0.0 for pure absolute error control, and
c use atol = 0.0 (or atol(i) = 0.0) for pure relative error
c control. caution.. actual (global) errors may exceed these
c local tolerances, so choose them conservatively.
c itask = 1 for normal computation of output values of y at t = tout.
c istate = integer flag (input and output). set istate = 1.
c iopt = 0 to indicate no optional inputs used.
c rwork = real work array of length at least..
c 22 + neq * max(16, neq + 9).
c see also paragraph e below.
c lrw = declared length of rwork (in user-s dimension).
c iwork = integer work array of length at least 20 + neq.
c liw = declared length of iwork (in user-s dimension).
c jac = name of subroutine for jacobian matrix.
c use a dummy name. see also paragraph e below.
c jt = jacobian type indicator. set jt = 2.
c see also paragraph e below.
c note that the main program must declare arrays y, rwork, iwork,
c and possibly atol.
c
c c. the output from the first call (or any call) is..
c y = array of computed values of y(t) vector.
c t = corresponding value of independent variable (normally tout).
c istate = 2 if lsoda was successful, negative otherwise.
c -1 means excess work done on this call (perhaps wrong jt).
c -2 means excess accuracy requested (tolerances too small).
c -3 means illegal input detected (see printed message).
c -4 means repeated error test failures (check all inputs).
c -5 means repeated convergence failures (perhaps bad jacobian
c supplied or wrong choice of jt or tolerances).
c -6 means error weight became zero during problem. (solution
c component i vanished, and atol or atol(i) = 0.)
c -7 means work space insufficient to finish (see messages).
c
c d. to continue the integration after a successful return, simply
c reset tout and call lsoda again. no other parameters need be reset.
c
c e. note.. if and when lsoda regards the problem as stiff, and
c switches methods accordingly, it must make use of the neq by neq
c jacobian matrix, j = df/dy. for the sake of simplicity, the
c inputs to lsoda recommended in paragraph b above cause lsoda to
c treat j as a full matrix, and to approximate it internally by
c difference quotients. alternatively, j can be treated as a band
c matrix (with great potential reduction in the size of the rwork
c array). also, in either the full or banded case, the user can supply
c j in closed form, with a routine whose name is passed as the jac
c argument. these alternatives are described in the paragraphs on
c rwork, jac, and jt in the full description of the call sequence below.
c
c-----------------------------------------------------------------------
*/
void lsoda(_lsoda_f f, int neq, double *y, double *t, double tout, int itol, double *rtol, double *atol,
int itask, int *istate, int iopt, int jt,
int iwork1, int iwork2, int iwork5, int iwork6, int iwork7, int iwork8, int iwork9,
double rwork1, double rwork5, double rwork6, double rwork7, void *_data)
/*
void
lsoda(f, neq, y, t, tout, itol, rtol, atol, itask, istate,
iopt, jt, iwork1, iwork2, iwork5, iwork6, iwork7, iwork8,
iwork9, rwork1, rwork5, rwork6, rwork7, _data)
_lsoda_f f;
void *_data;
int neq, itol, itask, *istate, iopt, jt;
int iwork1, iwork2, iwork5, iwork6, iwork7, iwork8, iwork9;
double *y, *t, tout, *rtol, *atol;
double rwork1, rwork5, rwork6, rwork7;
*/
/*
If the user does not supply any of these values, the calling program
should initialize those untouched working variables to zero.
ml = iwork1
mu = iwork2
ixpr = iwork5
mxstep = iwork6
mxhnil = iwork7
mxordn = iwork8
mxords = iwork9
tcrit = rwork1
h0 = rwork5
hmax = rwork6
hmin = rwork7
*/
{
int mxstp0 = 500, mxhnl0 = 10;
int i, iflag, lenyh, ihit;
double atoli, ayi, big, h0, hmax, hmx, rh, rtoli, tcrit, tdist, tnext, tol,
tolsf, tp, size, sum, w0;
if (*istate == 1) _freevectors();
/*
Block a.
This code block is executed on every call.
It tests *istate and itask for legality and branches appropriately.
If *istate > 1 but the flag init shows that initialization has not
yet been done, an error return occurs.
If *istate = 1 and tout = t, return immediately.
*/
if (*istate < 1 || *istate > 3) {
fprintf(stderr, "[lsoda] illegal istate = %d\n", *istate);
terminate(istate);
return;
}
if (itask < 1 || itask > 5) {
fprintf(stderr, "[lsoda] illegal itask = %d\n", itask);
terminate(istate);
return;
}
if (init == 0 && (*istate == 2 || *istate == 3)) {
fprintf(stderr, "[lsoda] istate > 1 but lsoda not initialized\n");
terminate(istate);
return;
}
if (*istate == 1) {
init = 0;
if (tout == *t) {
ntrep++;
if (ntrep < 5) return;
fprintf(stderr, "[lsoda] repeated calls with istate = 1 and tout = t. run aborted.. apparent infinite loop\n");
return;
}
}
/*
Block b.
The next code block is executed for the initial call ( *istate = 1 ),
or for a continuation call with parameter changes ( *istate = 3 ).
It contains checking of all inputs and various initializations.
First check legality of the non-optional inputs neq, itol, iopt,
jt, ml, and mu.
*/
if (*istate == 1 || *istate == 3) {
ntrep = 0;
if (neq <= 0) {
fprintf(stderr, "[lsoda] neq = %d is less than 1\n", neq);
terminate(istate);
return;
}
if (*istate == 3 && neq > n) {
fprintf(stderr, "[lsoda] istate = 3 and neq increased\n");
terminate(istate);
return;
}
n = neq;
if (itol < 1 || itol > 4) {
fprintf(stderr, "[lsoda] itol = %d illegal\n", itol);
terminate(istate);
return;
}
if (iopt < 0 || iopt > 1) {
fprintf(stderr, "[lsoda] iopt = %d illegal\n", iopt);
terminate(istate);
return;
}
if (jt == 3 || jt < 1 || jt > 5) {
fprintf(stderr, "[lsoda] jt = %d illegal\n", jt);
terminate(istate);
return;
}
jtyp = jt;
if (jt > 2) {
ml = iwork1;
mu = iwork2;
if (ml < 0 || ml >= n) {
fprintf(stderr, "[lsoda] ml = %d not between 1 and neq\n", ml);
terminate(istate);
return;
}
if (mu < 0 || mu >= n) {
fprintf(stderr, "[lsoda] mu = %d not between 1 and neq\n", mu);
terminate(istate);
return;
}
}
/* Next process and check the optional inpus. */
/* Default options. */
if (iopt == 0) {
ixpr = 0;
mxstep = mxstp0;
mxhnil = mxhnl0;
hmxi = 0.;
hmin = 0.;
if (*istate == 1) {
h0 = 0.;
mxordn = mord[1];
mxords = mord[2];
}
}
/* end if ( iopt == 0 ) */
/* Optional inputs. */
else { /* if ( iopt = 1 ) */
ixpr = iwork5;
if (ixpr < 0 || ixpr > 1) {
fprintf(stderr, "[lsoda] ixpr = %d is illegal\n", ixpr);
terminate(istate);