-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstiff.c
887 lines (827 loc) · 23 KB
/
stiff.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
#ifndef NOCOOLING
/*
* Integrate "stiff" equations similar to chemical equilibrium
* equations.
*
* The source code below is taken from Mott, D.R. & Oran, E.S., 2001,
* "CHEMEQ2: A Solver for the Stiff Ordinary Differential Equations of
* Chemical Kinetics", Naval Research Laboratory,
* NRL/MR/6400-01-8553. The report documentation page states under
* distribution/availability statement states:
* "Approved for public release; distribution is unlimited."
*/
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include "stiff.h"
inline double max(double x, double y)
{
if(x > y) return x;
else return y;
}
inline double min(double a, double b)
{
if(a < b) return a;
else return b;
}
/* implement fortran sign function: return a with the sign of b */
inline double sign(double a, double b)
{
double aabs = fabs(a);
if(b >= 0.0) return aabs;
else return -aabs;
}
/*
* Set integration parameters and allocate scratch arrays.
*/
STIFF *StiffInit( double eps, int nv, void *Data,
void (*derivs)(double, const double *, double *, double *,
void *Data)
)
{
STIFF *s;
int i;
s = (STIFF *) malloc(sizeof(STIFF));
assert(s!=NULL);
s->nv = nv;
s->epsmin = eps;
s->sqreps = 5.0*sqrt(eps);
s->epscl = 1.0/eps;
s->epsmax = 10.0;
s->dtmin = 1e-15;
s->itermax = 3; /*Increased from 1 to 3 to speed integration by
calculating more correctors. Adjustable parameter*/
s->ymin = malloc(nv*sizeof(*(s->ymin)));
for(i = 0; i < nv; i++)
s->ymin[i] = 1e-300;
s->y0 = malloc(nv*sizeof(*(s->y0)));
s->y1 = malloc(nv*sizeof(*(s->y1)));
s->q = malloc(nv*sizeof(*(s->q)));
s->d = malloc(nv*sizeof(*(s->d)));
s->rtau = malloc(nv*sizeof(*(s->rtau)));
s->ys = malloc(nv*sizeof(*(s->ys)));
s->qs = malloc(nv*sizeof(*(s->qs)));
s->rtaus = malloc(nv*sizeof(*(s->rtaus)));
s->scrarray = malloc(nv*sizeof(*(s->scrarray)));
s->Data = Data;
s->derivs = derivs;
s->epsmax = 10.0;
return s;
}
/**
* Specify minimum values of quantities.
*/
void StiffSetYMin(STIFF *s, const double *ymin)
{
int i;
for(i = 0; i < s->nv; i++)
s->ymin[i] = ymin[i];
}
void StiffFinalize( STIFF *s )
{
free(s->ymin);
free(s->y0);
free(s->y1);
free(s->q);
free(s->d);
free(s->rtau);
free(s->ys);
free(s->qs);
free(s->rtaus);
free(s->scrarray);
free(s);
}
void StiffStep(STIFF *s,
double y[], /* dependent variables */
double tstart, /* start time */
double dtg) /* time step */
{
/*
C
cd* * *** ** ********** * * * *** **** * ** ** * *
cd
cd chemeq2(dtg, gsub, n, y)
cd
cd original chemeq development:
cd originators: t.r. young nrl 1982
cd vax version: t.r. young nrl code 4040 may 1983
cd workstation: g. patnaik berkeley research jun 1995
cd
cd chemeq2 development: d.r. mott nrl code 6404 may 1999
Conversion to C: T. Quinn, UW, Dec, 2011
cd
cd Description: Subroutine chemeq2 solves a class of "stiff" ODEs
cd associated with reactive flow problems that cannot be readily
cd solved by the standard classical methods. In contrast to the
cd original chemeq subroutine, this version uses the same
cd quasi-steady-state update for every species regardless of the
cd timescale for that species. An adaptive stepsize is chosen to
cd give accurate results for the fastest changing quantity, and a
cd stability check on the timestep is also available when the
cd corrector is iterated.
cd
cd NOTE: The accuracy-based timestep calculation can be augmented
cd with a stability-based check when at least three corrector
cd iterations are performed. To include this check, "uncomment"
cd the lines that start with "D", or use the compiler flag -d-lines"
cd if available to compile the code including these lines. If the
cd lines are manually uncommented, the continuation characters
cd must be placed in the correct column. For most problems, the
cd stability check is not needed, and eliminating the calculations
cd and logic associated with the check enhances performance.
cd
cd The routine assumes that all of the integrated quantites and the
cd time step are positive.
cd
cd argument list definition (name, type, description, input vs. output):
cd dtg real the interval of integration or the i
cd range of the independent variable.
cd 0.0 <= t <= dtg. (global timestep)
cd gsub real the name of the derivitive function i
cd evaluator subroutine.
cd n integer the number of equations to be i
cd integrated. an error exisis if n is
cd greater than nd set by the parameter
cd statement.
cd y(n) real the initial values at call time i/o
cd and the final values at return time.
cd
cd Language and limitations: This subroutine is written in standard
cd FORTRAN 77. For high accuracy, this routine should be compiled
cd using whatever "double precision" flag is appropriate for the
cd platform being used (such as "f77 -r8 . . . .)
cd
cd subroutines referenced:
cd
cd gsub; whose actual name and definition are supplied by the user
cd is called to obtain the derivitive functions.
cd
cd call gsub(y, q, d, t)
cd argument list to gsub;
cd y(n) real current values of the dependent i
cd variable.
cd q(n) real calculated formation rates. o
cd d(n) real calculated loss rates. o
cd t real current value of the independent i
cd variable.
cd
*/
double tn; /* time within step */
int i;
/*
* Local copies of Stiff context
*/
int n = s->nv;
double *y0 = s->y0;
double *ymin = s->ymin;
double *q = s->q;
double *d = s->d;
double *rtau = s->rtau;
double *ys = s->ys;
double *qs = s->qs;
double *rtaus = s->rtaus;
double *scrarray = s->scrarray;
double *y1 = s->y1;
double epsmin = s->epsmin;
double sqreps = s->sqreps;
double epscl = s->epscl;
double epsmax = s->epsmax;
double dtmin = s->dtmin;
int itermax = s->itermax;
int gcount = 0; /* count calls to derivs */
int rcount = 0; /* count restart steps */
double scrtch;
double ascr;
double scr1;
double scr2;
double dt; /* timestep used by the integrator */
double ts; /* t at start of the chemical timestep */
double alpha; /* solution parameter used in update */
int iter; /* counter for corrector iterations */
double eps; /* maximum correction term */
double rtaub;
double qt; /* alpha weighted average of q */
double pb;
const double tfd = 1.000008; /* fudge for completion of timestep */
double rteps; /* estimate of sqrt(eps) */
double dto; /* old timestep; to rescale rtaus */
double temp;
tn = 0.0;
for(i = 0; i < n; i++) {
q[i] = 0.0;
d[i] = 0.0;
y0[i] = y[i];
y[i] = max(y[i], ymin[i]);
}
s->derivs(tn + tstart, y, q, d, s->Data);
gcount++;
/*
C
c estimate the initial stepsize.
C
c strongly increasing functions(q >>> d assumed here) use a step-
c size estimate proportional to the step needed for the function to
c reach equilibrium where as functions decreasing or in equilibrium
c use a stepsize estimate directly proportional to the character-
c istic stepsize of the function. convergence of the integration
c scheme is likely since the smallest estimate is chosen for the
c initial stepsize.
*/
scrtch = 1.0e-25;
for(i = 0; i < n; i++) {
ascr = fabs(q[i]);
scr2 = sign(1./y[i],.1*epsmin*ascr - d[i]);
scr1 = scr2 * d[i];
temp = -fabs(ascr-d[i])*scr2;
/* If the species is already at the minimum, disregard
destruction when calculating step size */
if (y[i] == ymin[i]) temp = 0.0;
scrtch = max(scr1,max(temp,scrtch));
}
dt = min(sqreps/scrtch,dtg);
while(1) {
/*
c the starting values are stored.
*/
ts = tn;
for(i = 0; i < n; i++) {
rtau[i] = dt*d[i]/y[i];
ys[i] = y[i];
qs[i] = q[i];
rtaus[i] = rtau[i];
}
/*
* find the predictor terms.
*/
restart:
for(i = 0; i < n; i++) {
/*
* prediction
*/
double rtaui = rtau[i];
/*
c note that one of two approximations for alpha is chosen:
c 1) Pade b for all rtaui (see supporting memo report)
c or
c 2) Pade a for rtaui<=rswitch,
c linear approximation for rtaui > rswitch
c (again, see supporting NRL memo report (Mott et al., 2000))
c
c Option 1): Pade b
*/
alpha = (180.+rtaui*(60.+rtaui*(11.+rtaui)))
/(360.+ rtaui*(60. + rtaui*(12. + rtaui)));
/*
c Option 2): Pade a or linear
c
c if(rtaui.le.rswitch) then
c alpha = (840.+rtaui*(140.+rtaui*(20.+rtaui)))
c & / (1680. + 40. * rtaui*rtaui)
c else
c alpha = 1.-1./rtaui
c end if
*/
scrarray[i] = (q[i]-d[i])/(1.0 + alpha*rtaui);
}
iter = 1;
while(iter <= itermax) {
for(i = 0; i < n; i++) {
/*
C ym2(i) = ym1(i)
C ym1(i) = y(i)
*/
y[i] = max(ys[i] + dt*scrarray[i], ymin[i]);
}
/* if(iter == 1) { Removed from original algorithm
so that previous, rather than first, corrector is
compared to. Results in faster integration. */
/*
c the first corrector step advances the time (tentatively) and
c saves the initial predictor value as y1 for the timestep
check later.
*/
tn = ts + dt;
for(i = 0; i < n; i++)
y1[i] = y[i];
/* } Close for "if(iter == 1)" above */
/*
evaluate the derivitives for the corrector.
*/
s->derivs(tn + tstart, y, q, d, s->Data);
gcount++;
eps = 1.0e-10;
for(i = 0; i < n; i++) {
rtaub = .5*(rtaus[i]+dt*d[i]/y[i]);
/*
c Same options for calculating alpha as in predictor:
c
c Option 1): Pade b
*/
alpha = (180.+rtaub*(60.+rtaub*(11.+rtaub)))
/ (360. + rtaub*(60. + rtaub*(12. + rtaub)));
/*
c Option 2): Pade a or linear
c
c if(rtaub.le.rswitch)
c then
c alpha = (840.+rtaub*(140.+rtaub*(20.+rtaub)))
c & / (1680. + 40.*rtaub*rtaub)
c else
c alpha = 1.- 1./rtaub
c end if
*/
qt = qs[i]*(1. - alpha) + q[i]*alpha;
pb = rtaub/dt;
scrarray[i] = (qt - ys[i]*pb) / (1.0 + alpha*rtaub);
}
iter++;
}
/*
c calculate new f, check for convergence, and limit decreasing
c functions. the order of the operations in this loop is important.
*/
for(i = 0; i < n; i++) {
scr2 = max(ys[i] + dt*scrarray[i], 0.0);
scr1 = fabs(scr2 - y1[i]);
y[i] = max(scr2, ymin[i]);
/*
C ym2(i) = ymi(i)
C yml(i) = y(i)
*/
if(.25*(ys[i] + y[i]) > ymin[i]) {
scr1 = scr1/y[i];
eps = max(.5*(scr1+
min(fabs(q[i]-d[i])/(q[i]+d[i]+1.0e-30),scr1)),eps);
}
}
eps = eps*epscl;
/*
print out dianostics if stepsize becomes too small.
*/
if(dt <= dtmin + 1.0e-16*tn) {
fprintf(stderr, "stiffchem: step size too small\n");
assert(0);
}
/*
c check for convergence.
c
c The following section is used for the stability check
C stab = 0.01
C if(itermax.ge.3) then
C do i=1,n
C stab = max(stab, abs(y(i)-yml(i))/
C & (abs(ymi(i)-ym2(i))+1.e-20*y(i)))
C end do
C endif
*/
if(eps <= epsmax) {
/*
& .and.stab.le.1.
c
c Valid step. Return if dtg has been reached.
*/
if(dtg <= tn*tfd) return;
}
else {
/*
Invalid step; reset tn to ts
*/
tn = ts;
}
/*
perform stepsize modifications.
estimate sqrt(eps) by newton iteration.
*/
rteps = 0.5*(eps + 1.0);
rteps = 0.5*(rteps + eps/rteps);
rteps = 0.5*(rteps + eps/rteps);
dto = dt;
dt = min(dt*(1.0/rteps+.005), tfd*(dtg - tn));
/* & ,dto/(stab+.001) */
/*
begin new step if previous step converged.
*/
if(eps > epsmax) {
/* & .or. stab. gt. 1 */
rcount++;
/*
c After an unsuccessful step the initial timescales don't
c change, but dt does, requiring rtaus to be scaled by the
c ratio of the new and old timesteps.
*/
dto = dt/dto;
for(i = 0; i < n; i++) {
rtaus[i] = rtaus[i]*dto;
}
/*
* Unsuccessful steps return to line 101 so that the initial
* source terms do not get recalculated.
*/
goto restart;
}
/*
Successful step; get the source terms for the next step
and continue back at line 100
*/
s->derivs(tn + tstart, y, q, d, s->Data);
gcount++;
}
}
#ifdef TESTCHEMEQ
/*
* Test case for the CHEMEQ2 solver.
*/
void csdfe(double t, double *y, double *q, double *d, void *data);
#include <time.h>
int
main(int argc, char** argv)
{
/*
C
C This is the driver program for the seven-species cesium
C mechanism test problem. The code integrates the system
C MXCASE times using differnt values of the chemeq2 variable
C epsmin (set by passing an entry from array EPS through
C CHEMSP before each integration).
C
C For this example,the external subroutine that calculates the
C source terms is called CSDFE.
C
*/
char *spsym[7] = {"02-", "CS+", "CS", "CS02", "02", "N2", "NE"} ;
double eps[15] = {.1, .05, .01, .005, .001, .0005, .0001, .00005,
.00001, .000005, .000001, 5.e-7, 1.e-7, 5.e-8, 1.e-8};
/* ,5.e-9, 1.e-9,5.e-10,1.e-10 */
double tscale;
int mxcase = 9;
int inlp, ns, na;
double ti, tf, deltat;
double yi[7], yf[7];
double epsil[15];
int icase;
/*
Note that the timing routines included maynot work on
all systems. Extra timing options are included as comments.
*/
double dtime, delta, tarray[2];
double tnorm;
delta = 1.;
/*
C
C INITIALIZE CONTROL PARAMETERS.
C
C TSCALE is simply a normalization factor for the timing
C results. It can be used to compare results from differnt
C machines (by setting it to the time required for that
C machine to solve a standard problem of some sort) or to
C simply make the timing results more "friendly."
*/
tscale = 1.0/1024.;
/*
C INLP allows the user to subdivide the interval over which
C each test is run. For INLP=1,CHEMEQ2 is sent the full
C interval TF-TI (specified below) as the global timestep.
*/
inlp = 1;
/*
C For this particular test, the electron number density is not
C integrated. The other five reacting species are integrated,
C and the electron density is found through charge conservation.
C This calculation is done within CSDFE. Therefore, NA=5 is
C the number of equations that are integrated, but NS=7 is
C number of species. Species to be integrated must be placed in
C first NA positions within the Y array. CHEMEQ2 only works with
C these first NA entries since NA is passed in the argument list
C below, but all NS values are available to and used by CSDFE.
*/
ns = 7;
na = 5;
/* "TI" - INITIAL TIME, "TF" - FINAL TIME. */
ti = 0.0;
tf = 1000.0;
deltat = (tf - ti)/inlp;
/*
C STORE INITIAL(TI = 0.0) AND FINALT(F = 1000.0)
C 02-
*/
yi[0] = 5.200e+02;
yf[0] = 2.59139492061e+04;
/* CS+ */
yi[1] = 6.200E+02;
yf[1] = 7.55718460300e+04;
/* Cs */
yi[2] = 1.000E+12;
yf[2] = 1.53194051722e+03;
/* CS02 */
yi[3] = 0.;
yf[3] = 9.99999923516e+11;
/* 02 */
yi[4] = 3.600E+14;
yf[4] = 3.59000000051e+14;
/* N2 */
yi[5] = 1.400E+15;
yf[5] = 1.40000000000e+15;
/* NE */
yi[6] = 1.000E+02;
yf[6] = 4.96578968239e+04;
/*
C LOOP OVER THE TEST CASES.
*/
for(icase = 0; icase < mxcase; icase++) {
STIFF *s;
double cput;
double t = 0.0;
double y[7];
double sum;
int istep;
int i;
printf("case %d: eps: %g inlp: %d\n", icase, eps[icase], inlp);
s = StiffInit(eps[icase], 5, NULL, csdfe);
cput = clock()/CLOCKS_PER_SEC;
/* RESET "Y" TO INITIAL VALUES "YI"'. */
for(i = 0; i < ns; i++)
y[i] = yi[i];
/*
C SET TIMER.
T = SECNDS(O.0)
delta = dtime(tarray);
*/
/*
INNER LOOP TO DETERMINE OVERHEAD OR RELATIVE STARTING EFFECIENCY
OF ITEGRATION SCHEME BEING TESTED.
*/
for(istep = 0; istep < inlp; istep++) {
/* CALL INTEGRATOR. */
/* chemeq2(deltat, csdfe, na, y); */
StiffStep(s, y, t, deltat);
t += deltat;
}
/* CALCULATE CPU TIME USED IN THE INTEGRATION PROCESS.
delta = dtime(tarray);
dsec = tarray[0];
*/
/* DSEC = delta */
cput = clock()/CLOCKS_PER_SEC;
tnorm = (int)(cput/tscale + .5);
/*
Calculate final electron density from densities of other
charges species
*/
y[6] = y[1] - y[0];
/* CALCULATE RELATIVE ERROR. */
for(i = 0; i < ns; i++)
epsil[i] = fabs(y[i] - yf[i])/min(y[i] , yf[i]);
sum = 0.0;
for(i = 0; i < ns; i++)
sum = sum + pow(epsil[i],2);
/*
Root-mean-square error is calculated using ns-1 (rather than ns)
since N2 is inert.
*/
sum = sqrt(sum/(ns-1));
/*
PRINT RESULTS.
*/
printf("ti: %g, tf: %g\n", ti, tf);
for(i = 0; i < ns; i++)
printf("%s %g %g %g %g\n", spsym[i], yi[i], yf[i], y[i],
epsil[i]);
printf("sum: %g\n", sum);
printf("cpu %g tnorm %g\n", cput, tnorm);
printf("eps %g, cput %g, tnorm %g, sum %g\n", eps[icase], cput,
tnorm, sum);
/* CALL CHEMCT(TF) */
StiffFinalize(s);
}
return 0;
}
void csdfe(double t, double *y, double *q, double *d, void *data)
{
/*
cd * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
cd
cd csdfe(y, q, d, t)
cd
cd description:
cd derivative function evaluator(gsub) for an atmospheric chemical
cd relaxation test problem involving cesium and cesium ions. format-
cd ion and loss rates are calculated for this set of "stiff ordinary
cd differential equations" that was suggested by by d. edelson of
cd bell laboratories.
cd
cd argument list definitions:
cd y(i) r current values of the functions plus the i/o
cd extra data at the end of the array that may be
cd passed back and forth between "csdfe" and the
cd main program. locations in y(i) which represent
cd the functions being advanced should not be
cd tampered with here.
cd q(i) r total formation rates.
cd d(i) r total loss rates.
cd t r the value of the independent variable.
cd
cd * * * * * * * * * * *
c
c local specifications.
c _ _____ ____ ___
*/
double ne, n2;
double o2m, csp, cs, cso2, o2;
double cr1, cr2, cr3, cr4, cr5, cr6, cr7;
/*
* utilize local storage for varibles.
*/
o2m = y[0];
csp = y[1];
cs =y[2];
cso2 = y[3];
o2 = y[4];
n2 = y[5];
/*
c write(63,*) t
c
c calculate electron density for local use and transmission back to
c the main program via y(7). however in this case this value should
c not be trusted since "chemeq"will not call the gsub" with the
c latest function values after the final step has converged. y(7 )
c will be one iteration behind in this case. y(7) and y(6) are
c examples tho, of how data may be transfered between the gsub" and
c the main program.
*/
ne = max(csp - o2m, 0.0);
y[6] = ne;
/*
c calculate reaction rates.
*/
cr1 = 5.00e-08*o2m*csp;
cr2 = 1.00e-12*csp*ne;
cr3 = 3.24e-03*cs;
cr4 = 4.00e-01*o2m;
cr5 = 1.00e-31*o2*cs*(cs + cso2 + n2 + o2);
cr6 = 1.24e-30*o2*o2*ne;
cr7 = 1.00e-31*o2*n2*ne;
/*
if(t.ge.700.) then
c cr4= 0.
c cr6 = 0.
c cr7 = 0.
c end if
c calculate total formation rates (c(i)) and total loss rates (d(i))
c for each species.
c
c o2m
*/
q[0] = cr6 + cr7;
d[0] = cr1 + cr4;
/*
Cs+
*/
q[1] = cr3;
d[1] = cr1 + cr2;
/*
Cs
*/
q[2] = cr1 + cr2;
d[2] = cr3 + cr5;
/*
cso2
*/
q[3] = cr5;
d[3] = 0.0;
/*
q(4) = q(4) -
d(4) = - l.OOe-31*o2*cs*cso2
*/
/*
o2
*/
q[4] = cr1 + cr4;
d[4]= cr5 + cr6 + cr7;
return;
}
#else
/* TESTCHEMEQ excludes root finder */
/*
* The following is an implementation of the Brent root finding
* algorithm. This is based on code from the GSL which has the
* following copyright. The code was modified to avoid the overhead
* that is somewhat unnecessary for such a simple routine.
*/
/* roots/brent.c
*
* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Reid Priedhorsky, Brian Gou\
gh
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US\
A.
*/
const int itmaxRoot = 100;
const double epsRoot = 3.0e-8;
double RootFind(double (*func)(void *Data, double), void *Data, double x1,
double x2, double tol)
{
int i;
double a = x1;
double fa = (*func)(Data, a);
double b = x2;
double fb = (*func)(Data, b);
double c = x2;
double fc = fb;
double d = x2 - x1;
double e = x2 - x1;
if ((fa < 0.0 && fb < 0.0) || (fa > 0.0 && fb > 0.0))
{
fprintf(stderr, "RootFind: endpoints do not straddle y=0");
assert(0);
}
for(i = 0; i < itmaxRoot; i++) {
double m;
double dTol;
int ac_equal = 0;
if ((fb < 0 && fc < 0) || (fb > 0 && fc > 0)) {
ac_equal = 1;
c = a;
fc = fa;
d = b - a;
e = b - a;
}
if (fabs (fc) < fabs (fb)) {
ac_equal = 1;
a = b;
b = c;
c = a;
fa = fb;
fb = fc;
fc = fa;
}
dTol = 0.5 * epsRoot * fabs (b) + 0.5*tol;
m = 0.5 * (c - b);
if (fb == 0) {
return b; /* SUCCESS */
}
if (fabs (m) <= dTol) {
return b; /* SUCCESS */
}
if (fabs (e) < dTol || fabs (fa) <= fabs (fb)) {
d = m; /* use bisection */
e = m;
}
else {
double p, q, r; /* use inverse cubic interpolation */
double s = fb / fa;
if (ac_equal) {
p = 2 * m * s;
q = 1 - s;
}
else {
q = fa / fc;
r = fb / fc;
p = s * (2 * m * q * (q - r) - (b - a) * (r - 1));
q = (q - 1) * (r - 1) * (s - 1);
}
if (p > 0) {
q = -q;
}
else {
p = -p;
}
if (2 * p < min(3 * m * q - fabs (dTol * q), fabs (e * q))) {
e = d;
d = p / q;
}
else {
/* interpolation failed, fall back to bisection */
d = m;
e = m;
}
}
a = b;
fa = fb;
if (fabs (d) > dTol) {
b += d;
}
else {
b += (m > 0 ? +dTol : -dTol);
}
fb = (*func)(Data, b);
}
fprintf(stderr, "brent: number of interations exceeded");
assert(0);
return 0.0;
}
#endif /* STIFFTEST */
#endif