-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSTAC.BAS
3034 lines (2819 loc) · 90.6 KB
/
BSTAC.BAS
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
' program BSTAC4
' BASIC version 7.3 - 28 March 2001
' compiler QuickBASIC version 4.5
' maxr - no. of parameters to be refined (maxpr-maxco)
' maxp - no. of total points
' maxco - no. of constraints
' maxs - no. of species
' maxc - no. of components
' maxt - no. of titrations
' maxpr - no. of total parameters to be refined
' maxsp - no. of special parameters (titration parameters)
DefDbl A-H, O-Z: DefInt I-N
Option Base 1
DECLARE SUB CCCS ()
DECLARE SUB CCFR ()
DECLARE SUB CHECK ()
DECLARE SUB CONSTR ()
DECLARE SUB DINS ()
DECLARE SUB DINSM ()
DECLARE SUB DOUS ()
DECLARE SUB FACT ()
DECLARE SUB FUNV ()
DECLARE SUB HJA (EO, RTJC, IVAL#, JA#, JB#)
DECLARE SUB INVERT ()
DECLARE SUB SNTPS ()
DECLARE SUB STANS (IPRST)
DECLARE SUB WCURV ()
DECLARE SUB GRAPH ()
DECLARE SUB FILPOUT ()
Common Shared DT()
Common Shared IKA()
Common Shared TOMM(), TT(), XX()
Common Shared ADC(), LOK()
Common Shared JQRM()
Common Shared C(), U(), WA(), A(), X(), DXM()
Common Shared DIV()
Common Shared BIV()
Common Shared PARF(), PARAS()
Common Shared KCONS()
Common Shared AST$()
Common Shared KSP(), RADIB()
Common Shared CV()
Common Shared MAXP, IPP, MAXT, MAXR, MAXC, MAXS
Common Shared MAXPR, KVSP, MAXCO, NCONS, MAXSP, MAXPT
Common Shared MAXRC, MAXCS, IBM#, MAXPG
Common Shared JP, K, SIGV, SIGE, ISP, NKAM
Common Shared INDKW, KW#
Common Shared c0M, c1M, d0M, d1M, e0M, e1M
Common Shared NITER, UF, NSUB
Common Shared NIV, NFILE$, OFILE$, SUM
Common Shared TOL, IFIL
Common Shared TIME1
Common Shared DLNAM, IP, IPI, NGAU, EPSIT, SUMP, JCK, JACK
Common Shared CX(), HX(), ZAST(), DFI(), DI(), DIR()
Common Shared DI2(), BETA(), CI(), TCR(), JCON(), EPSR()
Common Shared DX(), DIAG(), DM(), SF(), DF(), BF()
Common Shared RELAC, NF, IFAIL, JTC, NCICL, JFAIL, ACCM, AL10, MBET, ACM1
Common Shared NMBE, NK, ICD, CG(), DG(), EG(), JQR(), TITRE!(), EMF!()
Common Shared JTP(), NTC
Common Shared TITLE$
Common Shared MAXIT, MODE, WESP, PERC
Common Shared KEMIC$(), TEMP, PHI, PHF, NKM, NCD
Common Shared IREF#, AT, BT, c0, c1, d0, d1, e0, e1, ZZ(), KCD()
Common Shared KLOG#(), IB#(), KIY(), KEYC(), KEYD(), KEYE()
Common Shared NKAP(), IKAP()
Common Shared TITL$(), JPC(), MBE(), TOTMM(), ADDC(), COI(), CTI(), IT#()
Common Shared VINIT(), SIGMV(), EZERO(), SIGME(), JAM#(), JBM#(), RTJ()
Common Shared SIGMAP!()
Common Shared KAP#(), JPOT(), SD(), PARI(), JPOTM(), JCD(), PARAM()
Common Shared CDI$(), JCOL(), JTYP(), JCUR(), JREA(), NAMTYP$()
Common Shared B(), JS(), JSP(), POT(), ISPER(), IVA#()
Common Shared FIB(), RIB(), FIT(), RIT()
Common Shared Z2(), RES(), FREEC!(), PCX(), ZT()
Common Shared SUBR$(), STER$(), WS!(), PCS(), IPERC()
Common Shared SIGM, SRM, NKV, IW, CGE, DGE, EGE, NSP, NPAR, EO, RTJC
Common Shared IVAL#, JA#, JB#
Common Shared XGX!(), YY!(), YGE!(), TC$, TIT$
Common Shared TS, ISC, IGR, NX
Dim Shared SUBR$(15), STER$(15)
Dim Shared AST$(4), NAMTYP$(5), KCD(6), JCD(6), CDI$(6), TABIVA(21)
Dim BM(2), ISCC(4)
SUBR$(1) = "main"
SUBR$(2) = "CCCS"
SUBR$(3) = "CCFR"
SUBR$(4) = "CHECK"
SUBR$(5) = "CONSTR"
SUBR$(6) = "DINS"
SUBR$(7) = "DOUS"
SUBR$(8) = "FACT"
SUBR$(9) = "FUNV"
SUBR$(10) = "INVERT"
SUBR$(11) = "SNTPS"
SUBR$(12) = "STANS"
SUBR$(13) = "WCURV"
SUBR$(14) = "HJA"
SUBR$(15) = "DINSM"
25 Restore
Close
On Error GoTo ERRORPROC
IFIL = 1
Print
NSUB = 1
STER$(1) = SUBR$(1)
Line Input "data INPUT filename ->"; NFILE$
' NFILE = name of INPUT data file
If NFILE$ = "" Then End
Open NFILE$ For Input As #1
IFIL = 2
MAXSP = 2000
ReDim Shared JCOL(MAXSP)
NSUB = NSUB + 1
STER$(NSUB) = SUBR$(15)
Call DINSM 'subroutine
NSUB = NSUB - 1
Erase JCOL
26 Beep
Line Input "OUTPUT device/filename ->"; OFILE$
' OFILE = name of OUTPUT device/filename
If OFILE$ = NFILE$ Then Print "FILE ALREADY OPEN ": GoTo 26
If OFILE$ = "" Then OFILE$ = "SCRN:": GoTo 27
If UCase$(OFILE$) = "PRN" GoTo 27
IFIL = 3
Open OFILE$ For Input As #2
Close #2
Print "FILE EXISTS, OVERWRITE? (Y/N)"
YN$ = Input$(1)
If UCase$(YN$) <> "Y" GoTo 26
27 Open OFILE$ For Output As #2
29 Line Input "OUTPUT filename with refined parameters -> "; OPFILE$
IOPF = 0
If OPFILE$ <> "" Then
If OPFILE$ = NFILE$ Then Print "FILE ALREADY OPEN ": GoTo 29
If OPFILE$ = OFILE$ Then Print "FILE ALREADY OPEN ": GoTo 29
If UCase$(OPFILE$) = "PRN" GoTo 28
IFIL = 4
Open OPFILE$ For Input As #4
Close #4
Print "FILE EXISTS, OVERWRITE? (Y/N)"
YN$ = Input$(1)
If UCase$(YN$) <> "Y" GoTo 29
28 Open OPFILE$ For Output As #4
IOPF = 1
End If
' Procedure to give correct dimension to variables
30 Close #1
MAXP = IPP: MAXT = NTC: MAXR = NPAR: MAXC = NMBE: MAXS = NK
MAXPR = KVSP: MAXCO = NCONS: MAXSP = NSP: MAXPT = MAXP
If MAXR > MAXC Then MAXRC = MAXR Else MAXRC = MAXC
If MAXC < MAXS Then MAXCS = MAXS Else MAXCS = MAXC
If MAXCO = 0 Then MAXCO = 1
If MAXSP = 0 Then MAXSP = 1
If ICD > 0 Then
MAXTI = MAXT
MAXSI = MAXS
Else
MAXTI = 1
MAXSI = 1
End If
9 ReDim Shared JTP(MAXT), JPC(MAXT), VINIT(MAXT), EZERO(MAXT), RTJ(MAXT)
ReDim Shared SIGMV(MAXT), SIGME(MAXT)
ReDim Shared JAM#(MAXT), JBM#(MAXT), MBE(MAXT)
ReDim Shared TITL$(MAXT)
ReDim Shared ISPER(MAXT)
ReDim Shared JPOT(MAXS), KIY(MAXS), BETA(MAXS), POT(MAXS), CI(MAXS), JS(MAXS)
ReDim Shared KEYC(MAXS), KEYD(MAXS), KEYE(MAXS)
ReDim Shared JSP(MAXS), PCS(MAXS), DT(MAXS), JPOTM(MAXS)
ReDim Shared KAP#(MAXS), NKAP(MAXS), IKAP(MAXS, MAXS), IKA(MAXS)
ReDim Shared KLOG#(MAXS)
ReDim Shared KEMIC$(MAXC), HX(MAXC), CX(MAXC), TOMM(MAXC), TT(MAXC)
ReDim Shared XX(MAXC), ADC(MAXC), PCX(MAXC), TCR(MAXC), LOK(MAXC + 4)
ReDim Shared JQRM(MAXC), DM(MAXC, MAXC), DIAG(MAXC)
ReDim Shared JCON(MAXC)
ReDim Shared SD(MAXR), C(MAXR), U(MAXR), WA(MAXR), A(MAXR), X(MAXR)
ReDim Shared B(MAXR, MAXR), DXM(MAXR)
ReDim Shared DX(MAXRC), SF(MAXRC), DF(MAXRC), DIV(MAXRC)
ReDim Shared BIV(MAXRC, MAXRC), BF(MAXRC, MAXRC)
ReDim Shared EPSR(MAXCS)
ReDim Shared JTYP(MAXSP), JCUR(MAXSP), JREA(MAXSP), JCOL(MAXSP)
ReDim Shared PARF(MAXPR), PARI(MAXPR), PARAM(MAXPR), PARAS(MAXPR)
ReDim Shared KCONS(MAXCO, 2)
ReDim Shared JQR(MAXS, MAXC)
ReDim Shared TOTMM(MAXT, MAXC), ADDC(MAXT, MAXC)
ReDim Shared IT#(MAXTI), FIT(MAXTI), RIT(MAXTI)
ReDim Shared COI(MAXT), CTI(MAXT)
ReDim Shared KSP(MAXS), ZAST(MAXS), ZT(MAXS), IB#(MAXSI)
ReDim Shared FIB(MAXSI), RIB(MAXSI), DI(MAXSI), DIR(MAXSI), DFI(MAXSI)
ReDim Shared CG(MAXSI), DG(MAXSI), EG(MAXSI), DI2(MAXSI), RADIB(MAXSI)
ReDim Shared ZZ(MAXC), Z2(MAXC)
ReDim Shared EMF!(MAXP), RES(MAXP), WS!(MAXP), TITRE!(MAXP)
ReDim Shared FREEC!(MAXP, MAXC)
ReDim Shared SIGMAP!(MAXP)
ReDim Shared CV(MAXP), IVA#(MAXP)
ReDim Shared XGX!(MAXPG), YY!(MAXPG), YGE!(MAXPG)
ReDim Shared IPERC(MAXP, MAXS)
Open NFILE$ For Input As #1
If OFILE$ = "SCRN:" Then
ISCR = 0
Else
ISCR = 1: Open "SCRN:" For Output As #3
End If
Dim AVAILABLE As String
Const VGA = 12
Const MCGA = 13
Const EGA256 = 9
Const EGA64 = 8
Const MONO = 10
Const HERC = 3
Const CGA = 2
Data "CO","EO","JA","JB","SL","PA","c0","c1","d0","d1","e0","e1"
Data 0,.01,.05,.1,.15,.25,.5,.75,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7
ACCM = 1D-175
RELAC = .0001
DLNAM = Log(ACCM)
ACM1 = 1D-75
TOL = .0001
EPSIT = 1E-12
AL10 = Log(10#)
BROW$ = " "
For I = 1 To 78
BROW$ = BROW$ + " "
Next I
For I = 1 To 5: Read NAMTYP$(I): Next I
Read PA$
For I = 1 To 6: Read CDI$(I): Next I
For I = 1 To 21: Read TABIVA(I): Next I
NSUB = NSUB + 1
STER$(NSUB) = SUBR$(6)
Call DINS 'subroutine
NSUB = NSUB - 1
Close #1
Locate 25, 1: Print "press ESCAPE to quit after next cycle"
If WESP = 0! Then IW = 0 Else IW = 1
NP = JTP(NTC)
33 SW = 0!
For I = 1 To NP: SW = SW + WS!(I): Next I
FW = NP / SW
For I = 1 To NP: WS!(I) = WS!(I) * FW: Next I
If ICD = 0 Then IVAL# = 1
45 Print #2, ""
Print #2, "Table 2."
Print #2, ""
If IW = 2 Then
Print #2, "weights=1/DE^"; WESP
If ISCR = 1 Then Print #3, "weights=1/DE^"; WESP
End If
JACK = 3
If IW = 2 GoTo 71
c0 = c0M
c1 = c1M
d0 = d0M
d1 = d1M
e0 = e0M
e1 = e1M
71 Print #2, "initial time "; Time$
TIME1 = Timer
If ISCR = 1 Then Print #3, "time "; Time$
ISP = 2
For I = 1 To KVSP: PARAS(I) = PARAM(I): Next I
NSUB = NSUB + 1
STER$(NSUB) = SUBR$(11)
Call SNTPS 'subroutine
NSUB = NSUB - 1
For I = 1 To KVSP
PARAM(I) = PARAS(I)
PARF(I) = PARAM(I)
If IW < 2 Then PARI(I) = PARAM(I)
Next I
Q = 0!
QC = 1!
NCICL = 0
IFAIL = 1
130 For J = 1 To NPAR: C(J) = 0!
For K = J To NPAR: B(J, K) = 0!: Next K
Next J
NSUB = NSUB + 1
STER$(NSUB) = SUBR$(9)
Call FUNV 'subroutine
NSUB = NSUB - 1
SS = UF
If IFAIL <> 1 GoTo 530
For J = 1 To NPAR: U(J) = B(J, J): Next J
' Entry point for subsidiary loop. Q is the MARQUARDT parameter (M.P.)
' and U stores the diagonal elements of the normal equations matrix
160 For J = 1 To NPAR
DX(J) = C(J)
SF(J) = DX(J)
DF(J) = SD(J)
B(J, J) = U(J) * (Q + 1)
For I = 1 To NPAR: BF(J, I) = B(J, I): Next I
Next J
NF = NPAR
JFAIL = 4
NSUB = NSUB + 1
STER$(NSUB) = SUBR$(8)
Call FACT 'subroutine
NSUB = NSUB - 1
For I = 1 To NPAR
DX(I) = SF(I)
SD(I) = DF(I)
For J = 1 To NPAR: B(I, J) = BF(I, J): Next J
Next I
If PERC = 0! GoTo 162
For I = 1 To NPAR
If DX(I) < -PERC Then DX(I) = -PERC
If DX(I) > PERC Then DX(I) = PERC
Next I
162 If IFAIL = 4 GoTo 530
' DX now holds the parameter shifts. VW will be the dot product
' between shift and gradient vectors. It should be positive
VW = 0!
For J = 1 To NPAR: VW = VW + C(J) * DX(J): Next J
If VW <= 0! Then IFAIL = 5: GoTo 530
If Q = 0! GoTo 210
For J = 1 To NPAR
X(J) = C(J)
SF(J) = X(J)
DF(J) = SD(J)
B(J, J) = U(J)
For I = 1 To NPAR: BF(J, I) = B(J, I): Next I
Next J
NF = NPAR
JFAIL = 4
NSUB = NSUB + 1
STER$(NSUB) = SUBR$(8)
Call FACT 'subroutine
NSUB = NSUB - 1
For I = 1 To NPAR
X(I) = SF(I)
SD(I) = DF(I)
For J = 1 To NPAR: B(I, J) = BF(I, J): Next J
Next I
If IFAIL = 4 GoTo 530
210 For I = 1 To NPAR
DIV(I) = SD(I)
For J = 1 To NPAR: BIV(I, J) = B(I, J): Next J
Next I
NIV = NPAR
NSUB = NSUB + 1
STER$(NSUB) = SUBR$(10)
Call INVERT 'subroutine
NSUB = NSUB - 1
For I = 1 To NPAR
SD(I) = DIV(I)
For J = 1 To NPAR: B(I, J) = BIV(I, J): Next J
Next I
SIGMA = Sqr(SS / (NP - NPAR))
SIGM = SIGMA / Sqr(FW)
For I = 1 To KVSP
If I <= NKV Then K = I Else J = I - NKV: K = JCOL(J)
PARAM(I) = PARF(I) * (DX(K) + 1)
SD(K) = Sqr(Abs(B(K, K))) * SIGMA
Next I
KRASH = 1
DQ = 0!
For J = 1 To NPAR: DQ = DQ + DX(J) * DX(J) * U(J): Next J
DQ = VW + Q * DQ
' DQ is a prediction of the reduction in the sum of squares
' based on a linear hypothesis. N.B. when Q=0. DQ=VW
If -DQ > TOL Then IFAIL = 3: GoTo 530
260 ISP = 1
For I = 1 To KVSP: PARAS(I) = PARAM(I): Next I
NSUB = NSUB + 1
STER$(NSUB) = SUBR$(11)
Call SNTPS 'subroutine
NSUB = NSUB - 1
For I = 1 To KVSP: PARAM(I) = PARAS(I): Next I
JACK = 2
For I = 1 To NPAR: DXM(I) = DX(I): Next I
NSUB = NSUB + 1
STER$(NSUB) = SUBR$(9)
Call FUNV 'subroutine
NSUB = NSUB - 1
For I = 1 To NPAR: DX(I) = DXM(I): Next I
SSP = UF
JACK = 1
If IFAIL = 1 GoTo 310
' The calculation of free concentrations has failed, presumably
' because of the presence of negative beta(s). The parameter shifts are
' adjusted so as to reduce the offending parameter by a factor of 10
KRASH = KRASH + 1
IFAIL = 1
FRACT = 1
For K = 1 To NPAR
If FRACT * DX(K) > -1 GoTo 280
FRACT = -.9 / DX(K)
For J = 1 To NPAR: DX(J) = FRACT * DX(J): Next J
280 Next K
For K = 1 To KVSP
If K <= NKV Then J = K Else I = K - NKV: J = JCOL(I)
PARAM(K) = PARF(K) * (DX(J) + 1)
Next K
If KRASH < 10 GoTo 260
IFAIL = 9
GoTo 530
310 DS = SS - SSP
IEXIT = 0
' DS is the difference between the old and new sum of squares
' Now test for convergence with two criteria. (1) shifts should be
' less than 10 percent of the standard deviation on the parameters
For J = 1 To NPAR
If Abs(DX(J) / SD(J)) > .1 Then IEXIT = 1
Next J
' To avoid premature convergence when s.d. S are large 2nd criterion
If Abs(DS / SS) > TOL Then IEXIT = 1
If IEXIT = 0 GoTo 360
If DS >= .25 * DQ GoTo 350
' The reduction in sum of squares is less than 1/4 of the predicted
' value so the M.P. is increased by a factor between 2 and 10
YZ = .5
Z = VW + VW - DS
If Z > 0! Then YZ = VW / Z
If YZ > .5 Then YZ = .5
If YZ < .1 Then YZ = .1
If Q <> 0! GoTo 340
YZ = YZ + YZ
TR = 0!
For J = 1 To NPAR: TR = TR + B(J, J) * U(J): Next J
Q = 1! / TR
QC = Q
' QC is the cut-off value of the M.P. below which it will be set to zero
340 Q = Q / YZ
GoTo 360
350 If DS <= .75 * DQ GoTo 360
' The reduction in the sum of squares is more than 3/4 of the
' predicted value so the M.P. is halved or set to zero
' thus for 1/4 < reduction < 3/4 the M.P. is left unchanged
Q = Q * .5
If Q < QC Then Q = 0!
360 NCICL = NCICL + 1
Print #2, ""
Print #2, "time "; Time$
Print #2, Using " iteration ### sigma= ###.#####"; NCICL; SIGM
If ISCR = 1 Then
SR = 0!
For I = 1 To IPP: SR = SR + Abs(RES(I)): Next I
SRM = SR / IPP
Print #3, ""
Print #3, "time "; Time$
Print #3, Using " iteration ### sigma= ###.#####"; NCICL; SIGM;
Print #3, Using " mean dev.=#####.####"; SRM
End If
If Q = 0! GoTo 370
Print #2, "MARQUARDT parameter for next iteration";
Print #2, Using " #.####^^^^"; Q
If ISCR = 1 Then
Print #3, "MARQUARDT parameter for next iteration";
Print #3, Using " #.####^^^^"; Q
End If
370 Print #2, " parameter old value ";
Print #2, "rel.shift new value rel.error": Print #2, ""
If ISCR = 1 Then
Print #3, " parameter old value ";
Print #3, "rel.shift new value rel.error"
End If
K = 0
KNEG = 0
For J = 1 To NK
JOLD = JPOT(J)
If KIY(J) >= 1 Then
K = K + 1
420 If PARAM(K) <= 0! Then IFAIL = 9: KNEG = J
Z = Log(Abs(PARAM(K)))
If Z >= 0! GoTo 430
PARAM(K) = PARAM(K) * 10!
JPOT(J) = JPOT(J) - 1
GoTo 420
430 If Z < AL10 GoTo 440
PARAM(K) = PARAM(K) / 10!
JPOT(J) = JPOT(J) + 1
GoTo 420
440 PARFL = Log(Abs(PARF(K))) / AL10 + JOLD
PARAML = Log(Abs(PARAM(K))) / AL10 + JPOT(J)
DXK = (PARAML - PARFL) / PARFL
PARFK = Exp(PARFL * AL10)
PARAMK = Exp(PARAML * AL10)
If PARF(K) <= 0! Then PARFK = -PARFK
If PARAM(K) <= 0! Then PARAMK = -PARAMK
Print #2, Using " log K## ####.#####"; J; PARFL;
Print #2, Using " ####.##### ####.#####"; DX(K); PARAML;
If ISCR = 1 Then
Print #3, Using " log K## ####.#####"; J; PARFL;
Print #3, Using " ####.##### ####.#####"; DX(K); PARAML;
End If
If KNEG = J Then
Print #2, " *";
If ISCR = 1 Then Print #3, " *";
Else
Print #2, " ";
If ISCR = 1 Then Print #3, " ";
End If
Print #2, Using " ####.#####"; SD(K)
If ISCR = 1 Then Print #3, Using " ####.#####"; SD(K)
End If
If KEYC(J) = 1 Then
K = K + 1
Print #2, Using " C## ####.#####"; J; PARF(K);
Print #2, Using " ###.##### ####.#####"; DX(K); PARAM(K);
Print #2, Using " ###.#####"; SD(K)
If ISCR = 1 Then
Print #3, Using " C## ####.#####"; J; PARF(K);
Print #3, Using " ###.##### ####.#####"; DX(K); PARAM(K);
Print #3, Using " ###.#####"; SD(K)
End If
End If
If KEYD(J) = 1 Then
K = K + 1
Print #2, Using " D## ####.#####"; J; PARF(K);
Print #2, Using " ###.##### ####.#####"; DX(K); PARAM(K);
Print #2, Using " ###.#####"; SD(K)
If ISCR = 1 Then
Print #3, Using " D## ####.#####"; J; PARF(K);
Print #3, Using " ###.##### ####.#####"; DX(K); PARAM(K);
Print #3, Using " ###.#####"; SD(K)
End If
End If
If KEYE(J) = 1 Then
K = K + 1
Print #2, Using " E## ####.#####"; J; PARF(K);
Print #2, Using " ###.##### ####.#####"; DX(K); PARAM(K);
Print #2, Using " ###.#####"; SD(K)
If ISCR = 1 Then
Print #3, Using " E## ####.#####"; J; PARF(K);
Print #3, Using " ###.##### ####.#####"; DX(K); PARAM(K);
Print #3, Using " ###.#####"; SD(K)
End If
End If
Next J
If NCD = 0 GoTo 480
For I = 1 To NCD
K = K + 1
J = JCD(I)
Print #2, Using " \\ ####.##### "; CDI$(J); PARF(K);
Print #2, Using " ######.##### ####.##### "; DX(K); PARAM(K);
Print #2, Using " #####.#####"; SD(K)
If ISCR = 1 Then
Print #3, Using " \\ ####.##### "; CDI$(J); PARF(K);
Print #3, Using " ######.##### ####.##### "; DX(K); PARAM(K);
Print #3, Using " #####.#####"; SD(K)
End If
Next I
480 If K = NPAR GoTo 510
For J = K + 1 To KVSP
JPAR = J - NKV
I = JCOL(JPAR)
KIND = JTYP(JPAR)
JTC = JCUR(JPAR)
Print #2, Using " curve ## \\"; JTC; NAMTYP$(KIND);
If KIND = 1 Then Print #2, Using "#"; JREA(JPAR); Else Print #2, " ";
Print #2, Using " ####.##### ####.#####"; PARF(J); DX(I);
Print #2, Using " ####.##### ####.#####"; PARAM(J); SD(I)
Next J
510 For J = 1 To KVSP: PARF(J) = PARAM(J): Next J
If IEXIT = 0 GoTo 530
If InKey$ = Chr$(27) Then
Beep
Locate 25, 1
Print " "
Close
End
End If
If NCICL > MAXIT GoTo 525
ISP = 1
For I = 1 To KVSP: PARAS(I) = PARF(I): Next I
NSUB = NSUB + 1
STER$(NSUB) = SUBR$(11)
Call SNTPS 'subroutine
NSUB = NSUB - 1
For I = 1 To KVSP: PARF(I) = PARAS(I): Next I
GoTo 130
525 IFAIL = 2
530 Locate 25, 1
Print #2, BROW$
NSUB = NSUB + 1
STER$(NSUB) = SUBR$(4)
Call CHECK 'subroutine
NSUB = NSUB - 1
If IFAIL = 9 GoTo 570
If IFAIL > 3 GoTo 25
SR = 0!
For I = 1 To NP: SR = SR + Abs(RES(I)): Next I
SRM = SR / NP
NSUB = NSUB + 1
STER$(NSUB) = SUBR$(7)
Call DOUS 'subroutine
NSUB = NSUB - 1
If IND = 2 GoTo 570
If ICD = 0 GoTo 560
Print #2, ""
Print #2, "Table 5."
Print #2, ""
Print #2, "": Print #2, "log(BETA) = f(ionic strength)"
Print #2, ""
IMAX! = 0
For I = 1 To NP
If IVA#(I) > IMAX! Then IMAX! = IVA#(I)
Next I
IMAX! = IMAX! + .2
For J = 1 To 21
If IMAX! < TABIVA(J) Then LASTJ = J: GoTo 535
Next J
535 Print #2, " I ";
For J = 1 To NK
If J > 1 And (J Mod 9) = 1 Then Print #2, "": Print #2, Tab(7);
Print #2, Using "logB## "; J;
Next J
Print #2, ""
For J = 1 To LASTJ
IVAL# = TABIVA(J)
RADI = Sqr(IVAL#)
Print #2, Using "#.##"; IVAL#;
For L = 1 To NK
DI(L) = IVAL# - IB#(L)
DI2(L) = IVAL# * IVAL# - IB#(L) * IB#(L)
DIR(L) = IVAL# * RADI - IB#(L) * RADIB(L)
DFI(L) = AT * RADI / (1 + BT * RADI) - AT * RADIB(L) / (1 + BT * RADIB(L))
W = Log(BETA(L)) / AL10
W = W - (ZAST(L) * DFI(L) - CG(L) * DI(L) - DG(L) * DIR(L) - EG(L) * DI2(L))
If L > 1 And (L Mod 9) = 1 Then Print #2, "": Print #2, " ";
Print #2, Using "####.###"; W;
Next L
Print #2, ""
Next J
560 If IND = 2 GoTo 570
NSUB = NSUB + 1
STER$(NSUB) = SUBR$(12)
IPRST = 1
Call STANS(IPRST) 'subroutine
NSUB = NSUB - 1
If IW = 1 Then IW = 2: GoTo 33
GoTo 36
570 Print #2, Using " beta ## negative"; KNEG;
Print #2, " - this model cannot be refined"
GoTo 25
36 If IOPF = 1 Then
For I = 1 To NK
KLOG#(I) = Log(KAP#(I)) / AL10 + JPOT(I)
Next I
Open NFILE$ For Input As #1
Call FILPOUT
Close #1
End If
Beep
Locate 25, 1
Print #2, BROW$
If ISCR = 1 Then
Locate 25, 1
Print #3, BROW$
End If
Locate 25, 1
35 Line Input "GRAPHICS (Y/N)? ", GR$
If GR$ <> "Y" And GR$ <> "y" Then
Locate 25, 1: Print " "
GoTo 25
End If
IGR = 1
On Error GoTo 0
BM(1) = EGA256
BM(2) = CGA
ISCC(1) = 4
ISCC(2) = 2
HH = 0
BESTMODE = HERC
ISC = 1
On Error GoTo VIDEOERR
Screen BESTMODE
On Error GoTo EGAERR
TS = BESTMODE
Screen 0
On Error GoTo 0
IP = 1
For IGRA = 1 To NTC
TIT$ = TITL$(IGRA)
NCET = MBE(IGRA)
IP1 = JTP(IGRA)
IPER = JPC(IGRA)
TC$ = Left$(KEMIC$(IPER), 4)
NX = IP1 - IP + 1
J = 0
For I = IP To IP1
J = J + 1
XGX!(J) = TITRE!(I)
YGE!(J) = EMF!(I)
YY!(J) = EMF!(I) - RES(I)
Next I
IP = IP1 + 1
Call GRAPH 'module GRAMOD
If IGR = 0 GoTo 35
Next IGRA
GoTo 35
End
VIDEOERR:
HH = HH + 1
BESTMODE = BM(HH)
ISC = ISCC(HH)
If HH > 2 Then
Print "Sorry graphics not available."
Input " Push Enter", PP
End If
Resume
' Trap to detect 64K EGA
EGAERR:
BESTMODE = EGA64
AVAILABLE = "12789"
ISC = 4
Resume Next
ERRORPROC:
Select Case Err
Case 4
Print "Out of data"
Resume 25
Case 5
GoSub patherr
Print "Illegal function call between label "; Erl;
Print " and subsequent label"
Resume 25
Case 6
GoSub patherr
Print "Overflow between label "; Erl; " and subsequent label"
Resume 25
Case 7
GoSub patherr
Print "Out of memory"
Print "Please recover memory in your RAM!"
Print "Remove TSR program from memory or load TSR in high memory."
Resume 25
Case 9
Print "memory overflow"
End
Case 11
GoSub patherr
Print "Division by zero between label "; Erl; " and subsequent label"
Resume 25
Case 25
Print "Device fault - Hit any key (ESC for exit)"
Beep
A$ = Input$(1)
If A$ = Chr$(27) Then End
Resume
Case 27
Print "Out of paper"
Print "Insert the paper, then hit any key to continue"
A$ = Input$(1)
Resume
Case 52
Print "Bad file number"
If IFIL = 1 Then Resume 25 Else Resume 26
Case 53
If IFIL = 3 Then Close #2: Resume 27
If IFIL = 4 Then Close #4: Resume 28
Print "File not found"
If IFIL = 1 Then Resume 25 Else Resume 26
Case 55
Print "File already open"
Resume 26
Case 57
Print "Device I/O error"
If IFIL = 1 Then Resume 25 Else Resume 26
Case 61
Print "Disk full"
End
Case 62
Print "Input past end"
Resume 25
Case 64
Print "Bad file name"
If IFIL = 1 Then Resume 25 Else Resume 26
Case 68
Print "Device unavailable"
Resume 25
Case 70
Print "Disk write protect"
Print "Remove protection, then hit any key to continue"
A$ = Input$(1)
Resume
Case 71
If UCase$(OFILE$) <> "PRN" GoTo 5
Print "Printer not ready - active printer!": GoTo 6
5 Print "Disk not ready - Insert floppy disk!"
6 Print "Then hit any key to continue"
A$ = Input$(1)
Resume
Case 72
Print "Disk media error"
End
Case 75
Print "Path/file access error"
If IFIL = 1 Then Resume 25 Else Resume 26
Case 76
Print "Path not found"
If IFIL = 1 Then Resume 25 Else Resume 26
Case Else
GoSub patherr
Print "Code Error"; Err; " between label "; Erl;
Print #2, " and subsequent label"
End
End Select
patherr:
Print #2, ""
Print #2, "main";
For IER = 2 To NSUB: Print #2, " -> " + STER$(IER);: Next IER
Print #2, ""
Print #2, "error in "; STER$(NSUB)
Return
'
Sub CHECK
' prints the title and checks for failure
TIME2 = Timer
DTIME = TIME2 - TIME1
HOUR = Int(DTIME / 3600)
DT1 = DTIME - HOUR * 3600
MIN = Int(DT1 / 60)
SEC = DT1 - MIN * 60
Print #2, ""
Print #2, "final time "; Time$
Print #2, Using "net time ##:##:##.##"; HOUR; MIN; SEC
Print #2, ""
Print #2, "Table 3."
Print #2, ""
Print #2, Using " #### iterations"; NCICL
On IFAIL GOTO 622, 624, 626, 628, 630, 632, 632, 634, 636
622 Print #2, " refinement terminated successfully": Exit Sub
624 Print #2, Using "#### iterations (maximum) completed"; MAXIT: Exit Sub
626 Print #2, " predicted improvement negative in MAIN program"
Print #2, " convergence probably attained": Exit Sub
628 Print #2, " normal equations singular in MAIN program"
Print #2, " parameters cannot be refined": Exit Sub
630 Print #2, " shift vector points uphill in MAIN program"
Print #2, " parameters cannot be refined": Exit Sub
632 Print #2, " free concentrations cannot be calculated"
Print #2, " there may be a gross inconsistency in the data": Exit Sub
636 Print #2, " free concentrations cannot be calculated"
Print #2, " but refinement may have converged": IFAIL = 2
634 End Sub
'
Sub CONSTR
' sets up the constraints and allocates to each dangerous parameter
' the appropriate column in the design matrix. Constrained parameters
' are associated with the same column there
K = NKV
NCONS = 0
For I = 1 To NSP
JI = JCOL(I)
If JI > 0 GoTo 648
K = K + 1
JCOL(I) = K
If JI = -100 GoTo 648
IP1 = I + 1
For J = IP1 To NSP
If JCOL(J) = JI Then
NCONS = NCONS + 1
If JTYP(I) <> JTYP(J) Then
Print #2, " wrong constraint setting between special parameters";
Print #2, Using "### and ###"; J; I: Stop
End If
KCONS(NCONS, 1) = J
KCONS(NCONS, 2) = I
JCOL(J) = JCOL(I)
End If
Next J
648 Next I
If NCONS = 0 Then Exit Sub
' the equality constraints on the dangerous parameters are written out
Print #2, "": Print #2, Using "### constraints"; NCONS
Print #2, " the relative shifts of the";
Print #2, " following parameters are constrained to be equal"
For K = 1 To NCONS
I = KCONS(K, 1)
J = KCONS(K, 2)
JTI = JTYP(I)
JCI = JCUR(I)
JCJ = JCUR(J)
If JTI = 1 Then JRI = JREA(I): JRJ = JREA(J)
Print #2, Using " \\"; NAMTYP$(JTI);
If JTI = 1 Then Print #2, Using "#"; JRI; Else Print #2, " ";
Print #2, Using " curve### = \\"; JCI; NAMTYP$(JTI);
If JTI = 1 Then Print #2, Using "#"; JRJ; Else Print #2, " ";
Print #2, Using " curve###"; JCJ
Next K
End Sub
'
Sub DINS
' this subroutine collects the titration curve data
ReDim KEYMM(200)
650 Line Input #1, TITLE$
' TITLE$: title of the job
Print #2, Chr$(12)
Print #2, "program BSTAC4"
Print #2, "BASIC version 7.3 - 282001"
Print #2, "INPUT filename: "; NFILE$
Print #2, "OUTPUT device/filename: "; OFILE$
Print #2, ""
Print #2, "day "; Date$
Print #2, ""
Print #2, TITLE$
Print #2, ""
Input #1, MAXIT, NMBE, NKM, MODE, ICD, WESP, PERC
' MAXIT = maximum no. of iterations cycles
' NMBE = no. of components
' NKM = no. of species
' MODE = 1: weights=1 (unit weight for each experimental point)
' = 0: weights=1/SIGMA**2
' = 2: weights=1/SIGP**2 (SIGP given by the user)
' ICD = 0: the constant medium method is used in the job
' = 1: the titrations in the job are at different ionic strenghts
' = 2: as ICD=1 with E0=f(CGE,DGE,EGE) , reads values CGE,DGE,EGE
' WESP = weights=1/DE^WESP
' PERC =limit of the shifts % (PERC=0 no limit for shifts)
For I = 1 To NMBE: Input #1, KEMIC$(I): Next I
' KEMIC$ = name of mass balance equation
Input #1, TEMP, PHI, PHF
' TEMP = temperature in degrees cent.
' PHI,PHF - pH range to be considered in the job
' (if PHI=PHF=0 all the points are considered)
Print #2, Using "Temperature ###.## deg.cent."; TEMP
If MODE = 0 Then
Print #2, "weights 1/sig2"
Else
Print #2, "weights 1"
End If
JPH = 0
If PHI + PHF = 0! GoTo 652