-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathafactor.bas
executable file
·2294 lines (1836 loc) · 76.5 KB
/
afactor.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
Attribute VB_Name = "CodeAFACTOR"
' (c) Copyright 1995-2025 by John J. Donovan
Option Explicit
' 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.
' Alpha factors have "lastelm" rows, corresponding to the emitting elements
' and "lastchan" columns, corresponding to the absorbing or fluorescing
' elements that are present in the sample.
'
' In the alpha arrays, the row or record is the radiation, while the
' column is the absorbing or fluorescing matrix element. The
' record and column a certain element occupies is determined by its
' atomic number.
' Sample for alpha-factor binary k-factor calculations
Dim AFactorTmpSample(1 To 1) As TypeSample
Dim FormulaTmpSample(1 To 1) As TypeSample
' Dimension here to return values for plotting
Dim AlphaNpts1 As Integer
Dim AlphaXdata1() As Single
Dim AlphaYdata1() As Single
Dim AlphaAcoeff1() As Single
Dim AlphaAstddev1 As Single
Dim AlphaNpts2 As Integer
Dim AlphaXdata2() As Single
Dim AlphaYdata2() As Single
Dim AlphaAcoeff2() As Single
Dim AlphaAstddev2 As Single
' Empirical alpha factors (elemental only)
Dim empfackev(1 To MAXEMPFAC%) As Single
Dim empfactof(1 To MAXEMPFAC%) As Single
Dim empfaco(1 To MAXEMPFAC%) As Integer ' alpha regression mode (1, 2, 3 or 4) (constant, linear, polynomial or non-linear)
Dim empface(1 To MAXEMPFAC%) As String ' emitter
Dim empfacx(1 To MAXEMPFAC%) As String ' xray
Dim empfaca(1 To MAXEMPFAC%) As String ' absorber
Dim empfac1(1 To MAXEMPFAC%) As Single
Dim empfac2(1 To MAXEMPFAC%) As Single
Dim empfac3(1 To MAXEMPFAC%) As Single
Dim empfac4(1 To MAXEMPFAC%) As Single
Dim empfacstr(1 To MAXEMPFAC%) As String ' string
' Operating voltage and takeoff for alpha-factors look-up tables (combined condition samples not supported yet!)
Dim alphakev As Single, alphatof As Single
' X-rays for alpha-factor look up tables (blank = not calculated)
Dim AlphaXray(1 To MAXELM%, 1 To MAXELM%) As String ' emitter x-ray
' Alpha-factor look-up tables
Dim alphal1(1 To MAXELM%, 1 To MAXELM%) As Single
Dim alphal2(1 To MAXELM%, 1 To MAXELM%) As Single
Dim alphal3(1 To MAXELM%, 1 To MAXELM%) As Single
Dim alphal4(1 To MAXELM%, 1 To MAXELM%) As Single
' Alpha-factor sample arrays used for quantitative calculations
Dim alpha1(1 To MAXCHAN%, 1 To MAXCHAN%) As Single
Dim alpha2(1 To MAXCHAN%, 1 To MAXCHAN%) As Single
Dim alpha3(1 To MAXCHAN%, 1 To MAXCHAN%) As Single
Dim alpha4(1 To MAXCHAN%, 1 To MAXCHAN%) As Single
Dim PenepmaKratiosFlag(1 To MAXCHAN%, 1 To MAXCHAN%) As Boolean
Sub AFactorBeta(analysis As TypeAnalysis, sample() As TypeSample)
' This routine accepts an array of weight percents and returns an array of beta factors
ierror = False
On Error GoTo AFactorBetaError
Dim i As Integer, ip As Integer
Dim emitter As Integer, absorber As Integer
Dim betafraction As Single
Dim astring As String
ReDim wtfractions(1 To MAXCHAN%) As Single
' Convert to weight fractions
If analysis.TotalPercent! = 0# Then GoTo AFactorBetaBadTotal
For i% = 1 To sample(1).LastChan%
wtfractions!(i%) = analysis.WtPercents!(i%) / analysis.TotalPercent!
Next i%
If VerboseMode Then
Call IOWriteLog(vbNullString)
End If
' Factors for "LastElm" emitters and "LastChan" absorbers
For emitter% = 1 To sample(1).LastElm%
analysis.UnkBetas!(emitter%) = 0#
' Skip if quant is disabled
If sample(1).DisableQuantFlag(emitter%) = 0 Then
' Determine if element is duplicated
ip% = IPOS8(emitter%, sample(1).Elsyms$(emitter%), sample(1).Xrsyms$(emitter%), sample())
If Not UseAggregateIntensitiesFlag Or (UseAggregateIntensitiesFlag And ip% = 0) Then
' Calculate beta factors (0 = phi/rho/z, 1,2,3,4 = alpha fits, 5 = calilbration curve, 6 = fundamental parameters)
For absorber% = 1 To sample(1).LastChan%
' Calculate constant, linear and polynomial beta factors
If CorrectionFlag% < 4 Then
betafraction! = (alpha1!(emitter%, absorber%) + wtfractions!(absorber%) * alpha2!(emitter%, absorber%) + wtfractions!(absorber%) ^ 2 * alpha3!(emitter%, absorber%)) * wtfractions!(absorber%)
' Calculate non-linear beta factors
Else
betafraction! = (alpha1!(emitter%, absorber%) + wtfractions!(absorber%) * alpha2!(emitter%, absorber%) + wtfractions!(absorber%) ^ 2 * alpha3!(emitter%, absorber%) + Exp(wtfractions!(absorber%)) * alpha4!(emitter%, absorber%)) * wtfractions!(absorber%)
End If
' Debug mode
If VerboseMode Then
astring$ = "AFactorBeta: " & sample(1).Elsyup$(emitter%) & " " & sample(1).Xrsyms$(emitter%) & " in " & sample(1).Elsyup$(absorber%) & ", betafraction=" & Format$(betafraction!)
Call IOWriteLog(astring$)
End If
' Sum beta factors fractions
analysis.UnkBetas!(emitter%) = analysis.UnkBetas!(emitter%) + betafraction!
Next absorber%
End If
End If
' Debug mode
If VerboseMode Then
astring$ = "AFactorBeta: " & sample(1).Elsyup$(emitter%) & " " & sample(1).Xrsyms$(emitter%) & ", Total Beta=" & Format$(analysis.UnkBetas!(emitter%))
Call IOWriteLog(astring$)
End If
Next emitter%
Exit Sub
' Errors
AFactorBetaError:
MsgBox Error$, vbOKOnly + vbCritical, "AFactorBeta"
ierror = True
Exit Sub
AFactorBetaBadTotal:
msg$ = "Total percent equals zero, cannot calculate beta factors"
MsgBox msg$, vbOKOnly + vbExclamation, "AFactorBeta"
ierror = True
Exit Sub
End Sub
Sub AFactorCalculateFactor(i As Integer, j As Integer, analysis As TypeAnalysis, sample() As TypeSample)
' Calculate the specified or all alpha-factors for the sample
' i = emitter, j = absorber position of binary in sample()
ierror = False
On Error GoTo AFactorCalculateFactorError
Dim emitter As Integer, absorber As Integer
Dim numberofbinaries As Integer, inum As Integer
' If emitter and absorber are zero, calculate all binaries in sample
icancel = False
If i% = 0 And j% = 0 Then
' Calculate total number of binaries
numberofbinaries% = 0
For emitter% = 1 To sample(1).LastElm%
For absorber% = emitter% + 1 To sample(1).LastChan%
numberofbinaries% = numberofbinaries% + 1
Next absorber%
Next emitter%
Call IOWriteLog("Number of alpha-factor binaries to be calculated = " & Str$(numberofbinaries%))
Call AnalyzeStatusAnal(vbNullString)
' Calculate each binary in sample (all elements)
inum% = 0
For emitter% = 1 To sample(1).LastElm%
If sample(1).DisableQuantFlag(emitter%) = 0 Then
For absorber% = emitter% + 1 To sample(1).LastChan%
inum% = inum% + 1
msg$ = "Calculating alpha-factor binary " & Format$(inum%, a50$) & " of " & Format$(numberofbinaries%, a50$) & "..."
Call AnalyzeStatusAnal(msg$)
If icancel Then
Call AnalyzeStatusAnal(vbNullString)
ierror = True
Exit Sub
End If
' K-factor calculation
Screen.MousePointer = vbHourglass
Call AFactorCalculateKFactors(emitter%, absorber%, analysis, sample())
Screen.MousePointer = vbDefault
If ierror Then Exit Sub
Next absorber%
End If
Next emitter%
Call AnalyzeStatusAnal(vbNullString)
' Save alpha conditions for next time
alphakev! = sample(1).kilovolts!
alphatof! = sample(1).takeoff!
' Calculate a single binary in sample
Else
emitter% = i%
absorber% = j%
msg$ = "Calculating alpha-factor binary " & sample(1).Elsyms$(emitter%) & " " & sample(1).Xrsyms$(emitter%) & " in " & sample(1).Elsyms$(absorber%) & "..."
Call AnalyzeStatusAnal(msg$)
If icancel Then
Call AnalyzeStatusAnal(vbNullString)
ierror = True
Exit Sub
End If
' K-factor calculation
Screen.MousePointer = vbHourglass
Call AFactorCalculateKFactors(emitter%, absorber%, analysis, sample())
Screen.MousePointer = vbDefault
If ierror Then Exit Sub
End If
Call AnalyzeStatusAnal(vbNullString)
Call IOStatusAuto(vbNullString)
Exit Sub
' Errors
AFactorCalculateFactorError:
MsgBox Error$, vbOKOnly + vbCritical, "AFactorCalculateFactor"
Call AnalyzeStatusAnal(vbNullString)
Call IOStatusAuto(vbNullString)
ierror = True
Exit Sub
End Sub
Sub AFactorCalculateFitFactors(k As Integer, l As Integer, wout() As Single, kout() As Single, eout() As String, xout() As String, zout() As Integer)
' Calculate the alpha-factor from the passed concentration and intensity arrays
ierror = False
On Error GoTo AFactorCalculateFitFactorsError
Dim m As Integer, ibin As Integer, p As Integer
Dim kmax As Integer, nmax As Integer, npts As Integer
Dim temp As Single
Dim astring As String
Dim syme As String, symx As String, symm As String
Dim rec1 As Integer, rec2 As Integer
Dim stddev As Single
Dim alph1 As Single, alph2 As Single, alph3 As Single, alph4 As Single
ReDim weightfractions(1 To MAXBINARY% * 2) As Single
ReDim xdata(1 To MAXBINARY%) As Single
ReDim ydata(1 To MAXBINARY%) As Single
ReDim acoeff(1 To MAXCOEFF4%) As Single
ReDim AlphaXdata1(1 To MAXBINARY%) As Single
ReDim AlphaYdata1(1 To MAXBINARY%) As Single
ReDim AlphaAcoeff1(1 To MAXCOEFF4%) As Single
ReDim AlphaXdata2(1 To MAXBINARY%) As Single
ReDim AlphaYdata2(1 To MAXBINARY%) As Single
ReDim AlphaAcoeff2(1 To MAXCOEFF4%) As Single
' 0 = phi/rho/z, 1,2,3,4 = alpha fits, 5 = calilbration curve, 6 = fundamental parameters
If DebugMode Then
Call IOWriteLog(vbNullString)
If CorrectionFlag% = 1 Then astring$ = " CONSTANT Alpha Factors"
If CorrectionFlag% = 2 Then astring$ = " LINEAR Alpha Factors"
If CorrectionFlag% = 3 Then astring$ = " POLYNOMIAL Alpha Factors"
If CorrectionFlag% = 4 Then astring$ = " NON-LINEAR Alpha Factors"
astring$ = astring$ & ", Takeoff=" & Str$(DefaultTakeOff!) & ", KeV=" & Str$(DefaultKiloVolts!)
Call IOWriteLog(astring$)
End If
' Convert to weight fraction
For m% = 1 To MAXBINARY% * 2
weightfractions!(m%) = wout!(m%) / 100#
Next m%
' Calculate alpha-factors for both binary components. "ibin" is the emitting element index in the binary calculation.
For ibin% = 1 To 2
' Init labels for each binary
syme$ = vbNullString ' emitting element
symx$ = vbNullString ' emitting x-ray
symm$ = vbNullString ' matrix element
rec1% = 0
rec2% = 0
' Skip hydrogen emitter and helium emitter components (check first binary only)
If LCase$(eout$(ibin%)) = Symlo$(ATOMIC_NUM_HYDROGEN%) Or LCase$(eout$(ibin%)) = Symlo$(ATOMIC_NUM_HELIUM%) Then GoTo 600
' Calculate the nominal alpha-factor at the given concentrations and perform a least squares fit based on "CorrectionFlag%".
'
' The alpha-factors use the following alpha expression. Constant alpha factors are based on a linear fit of alpha vs. concentration, and
' back calculated for a 50/50 concentration. Polynomial alpha-factors are fit to a 2nd order polynomial. Non-linear alpha-factors are fit to
' a non-linear equation.
'
' (C/K - C)/(1 - C) = alpha or alpha vs. C
'
' Load weight fractions and k-ratios into arrays to fit
npts% = 0
If DebugMode Then Call IOWriteLog(vbNullString)
For p% = 1 To MAXBINARY%
xdata!(p%) = 0#
ydata!(p%) = 0#
m% = (p% - 1) * 2 + ibin%
' Check for absorber only
If Trim$(xout$(m%)) <> vbNullString Then
temp! = ((weightfractions!(m%) / kout!(m%)) - weightfractions!(m%)) / (1# - weightfractions!(m%))
' Only fit points with positive k-ratios and alpha factors
If kout!(m%) > 0# And temp! > 0# Then
npts% = npts% + 1
xdata!(npts%) = weightfractions!(m%)
ydata!(npts%) = temp!
If DebugMode% Then
msg$ = "P=" & Format$(p%) & ", C=" & Format$(weightfractions!(m%), f84$) & ", K=" & Format$(kout!(m%), f84$) & ", Alpha=" & Format$(ydata!(npts%), f84$)
Call IOWriteLog(msg$)
End If
' Load labels and alpha array index numbers if not yet loaded (this is in case using PenepmaKratioLimits)
If syme$ = vbNullString Then
syme$ = eout$(m%)
symx$ = xout$(m%)
symm$ = eout$(m% + 1)
rec1% = zout%(m%)
rec2% = zout%(m% + 1)
End If
End If
End If
Next p%
' Check for at least two data points
If npts% < 2 Then GoTo 600
nmax% = npts%
' Constant and linear alpha factors (linear fit)
If CorrectionFlag% = 1 Or CorrectionFlag% = 2 Then
kmax% = 1 ' first order
Call LeastSquares(kmax%, nmax%, xdata!(), ydata!(), acoeff!()) ' constant or linear regression
If ierror Then Exit Sub
' Calculate a constant alpha-factor at 50/50 composition
If CorrectionFlag% = 1 Then
alph1! = acoeff!(1) + acoeff!(2) * 0.5
alph2! = 0#
alph3! = 0#
' Calculate average % deviation for constant alpha-factor
acoeff(2) = 0# ' to force constant
Call LeastDeviation(Int(1), stddev!, nmax%, xdata!(), ydata!(), acoeff!()) ' average deviation
If ierror Then Exit Sub
End If
' Linear alpha-factor fit
If CorrectionFlag% = 2 Then
alph1! = acoeff!(1)
alph2! = acoeff!(2)
alph3! = 0#
' Calculate average % deviation of linear fit from mean for linear alpha-factor
Call LeastDeviation(Int(1), stddev!, nmax%, xdata!(), ydata!(), acoeff!()) ' average deviation
If ierror Then Exit Sub
End If
End If
' Do polynomial alpha-factor fit next
If CorrectionFlag% = 3 Then
kmax% = 2 ' second order
Call LeastSquares(kmax%, nmax%, xdata!(), ydata!(), acoeff!()) ' polynomial regression
If ierror Then Exit Sub
alph1! = acoeff!(1)
alph2! = acoeff!(2)
alph3! = acoeff!(3)
' Calculate average % deviation of 2nd order fit
Call LeastDeviation(Int(1), stddev!, nmax%, xdata!(), ydata!(), acoeff!()) ' quadratic deviation
If ierror Then Exit Sub
End If
' Do non-linear alpha factor fit next
If CorrectionFlag% = 4 Then
' Try non-linear fit
Call LeastMathNonLinear(nmax%, xdata!(), ydata!(), acoeff!())
If ierror Then Exit Sub
alph1! = acoeff!(1)
alph2! = acoeff!(2)
alph3! = acoeff!(3)
alph4! = acoeff!(4)
Call LeastMathNonLinearDeviation(stddev!, nmax%, xdata!(), ydata!(), acoeff!())
If ierror Then Exit Sub
End If
' Display results
If DebugMode Then
If ibin% = 1 And PenepmaKratiosFlag(k%, l%) Then
Call IOWriteLog("Xray Matrix Alpha1 Alpha2 Alpha3 Alpha4 %AvgDev *from Penepma 2012 Calculations")
ElseIf ibin% = 2 And PenepmaKratiosFlag(l%, k%) Then
Call IOWriteLog("Xray Matrix Alpha1 Alpha2 Alpha3 Alpha4 %AvgDev *from Penepma 2012 Calculations")
Else
Call IOWriteLog("Xray Matrix Alpha1 Alpha2 Alpha3 Alpha4 %AvgDev")
End If
msg$ = syme$ & " " & symx$ & " in " & symm$ & " "
msg$ = msg$ & Format$(Format$(alph1!, f84$), a80$) & Format$(Format$(alph2!, f84$), a80$) & Format$(Format$(alph3!, f84$), a80$) & Format$(Format$(alph4!, f84$), a80$) & MiscAutoFormat$(stddev!)
Call IOWriteLog(msg$)
End If
' Save calculated alpha-factors to look-up table
If rec1% = 0 Or rec2% = 0 Then GoTo AFactorCalculateFitFactorsNoRecord
alphal1!(rec1%, rec2%) = alph1!
alphal2!(rec1%, rec2%) = alph2!
alphal3!(rec1%, rec2%) = alph3!
alphal4!(rec1%, rec2%) = alph4!
' Save x-ray symbol for alpha-factor look-up table
AlphaXray$(rec1%, rec2%) = symx$
' Save to module level for plotting
If ibin% = 1 Then
AlphaNpts1% = npts%
For p% = 1 To npts%
AlphaXdata1!(p%) = xdata!(p%)
AlphaYdata1!(p%) = ydata!(p%)
Next p%
AlphaAcoeff1!(1) = acoeff!(1)
AlphaAcoeff1!(2) = acoeff!(2)
AlphaAcoeff1!(3) = acoeff!(3)
AlphaAcoeff1!(4) = acoeff!(4)
AlphaAstddev1! = stddev!
Else
AlphaNpts2% = npts%
For p% = 1 To npts%
AlphaXdata2!(p%) = xdata!(p%)
AlphaYdata2!(p%) = ydata!(p%)
Next p%
AlphaAcoeff2!(1) = acoeff!(1)
AlphaAcoeff2!(2) = acoeff!(2)
AlphaAcoeff2!(3) = acoeff!(3)
AlphaAcoeff2!(4) = acoeff!(4)
AlphaAstddev2! = stddev!
End If
600: Next ibin%
Exit Sub
' Errors
AFactorCalculateFitFactorsError:
MsgBox Error$, vbOKOnly + vbCritical, "AFactorCalculateFitFactors"
ierror = True
Exit Sub
AFactorCalculateFitFactorsNoRecord:
msg$ = "The alpha array index numbers for the calculated binary are zero. This error should not occur. Please contact Probe Software with details."
MsgBox msg$, vbOKOnly + vbExclamation, "AFactorCalculateFitactors"
ierror = True
Exit Sub
End Sub
Sub AFactorCalculateKFactors(k As Integer, l As Integer, analysis As TypeAnalysis, sample() As TypeSample)
' Calculate k-factors for alpha-factor calculations of the passed binary.
'
' This routine will calculate K-factors for a binary elemental system
' at MAXBIN compositions calculated using the currently selected ZAF
' selections. If both elements are loaded as absorbers only, then
' an error is returned.
'
' k = array position of 1st binary component in sample array
' l = array position of 2nd binary component in sample array
ierror = False
On Error GoTo AFactorCalculateKFactorsError
Dim notfoundA As Boolean, notfoundB As Boolean
Dim ibin As Integer, m As Integer, n As Integer
Dim astring As String
ReDim wout(1 To MAXBINARY% * 2) As Single, rout(1 To MAXBINARY% * 2) As Single
ReDim eout(1 To MAXBINARY% * 2) As String, xout(1 To MAXBINARY% * 2) As String
ReDim zout(1 To MAXBINARY% * 2) As Integer
' Check for emitter and absorber equal (already initialized to 1.000)
If sample(1).Elsyms$(k%) = sample(1).Elsyms(l%) Then Exit Sub
' Check for both elements as absorbers
If sample(1).Xrsyms$(k%) = vbNullString And sample(1).Xrsyms$(l%) = vbNullString Then Exit Sub
msg$ = vbCrLf & "Calculating alpha-factor binary " & Format$(sample(1).Elsyms$(k%), a20$) & " " & sample(1).Xrsyms$(k%) & " in " & Format$(sample(1).Elsyms$(l%), a20$)
If DebugMode Then Call IOWriteLog(msg$)
Call AnalyzeStatusAnal(msg$)
' Create a dummy sample for this binary k-factor calculation
AFactorTmpSample(1) = sample(1)
AFactorTmpSample(1).LastChan% = 2 ' binary only
AFactorTmpSample(1).OxideOrElemental% = 2 ' always elemental alpha-factors
' Load analyzed, then specified
If sample(1).Xrsyms$(k%) = vbNullString Then
AFactorTmpSample(1).LastElm% = 1 ' one absorber
AFactorTmpSample(1).Elsyms$(1) = sample(1).Elsyms$(l%)
AFactorTmpSample(1).Xrsyms$(1) = sample(1).Xrsyms$(l%)
AFactorTmpSample(1).Elsyms$(2) = sample(1).Elsyms$(k%)
AFactorTmpSample(1).Xrsyms$(2) = sample(1).Xrsyms$(k%)
ElseIf sample(1).Xrsyms$(l%) = vbNullString Then
AFactorTmpSample(1).LastElm% = 1 ' one absorber
AFactorTmpSample(1).Elsyms$(1) = sample(1).Elsyms$(k%)
AFactorTmpSample(1).Xrsyms$(1) = sample(1).Xrsyms$(k%)
AFactorTmpSample(1).Elsyms$(2) = sample(1).Elsyms$(l%)
AFactorTmpSample(1).Xrsyms$(2) = sample(1).Xrsyms$(l%)
Else
AFactorTmpSample(1).LastElm% = 2 ' no absorbers
AFactorTmpSample(1).Elsyms$(1) = sample(1).Elsyms$(k%)
AFactorTmpSample(1).Xrsyms$(1) = sample(1).Xrsyms$(k%)
AFactorTmpSample(1).Elsyms$(2) = sample(1).Elsyms$(l%)
AFactorTmpSample(1).Xrsyms$(2) = sample(1).Xrsyms$(l%)
End If
' Load element arrays
Call ElementGetData(AFactorTmpSample())
If ierror Then Exit Sub
' Initialize
For ibin% = 1 To 2
For n% = 1 To MAXBINARY%
m% = (n% - 1) * 2 + ibin%
wout!(m%) = 0#
rout!(m%) = 1#
eout$(m%) = AFactorTmpSample(1).Elsyms$(ibin%)
xout$(m%) = AFactorTmpSample(1).Xrsyms$(ibin%)
zout%(m%) = AFactorTmpSample(1).AtomicNums%(ibin%)
Next n%
Next ibin%
' If use Penepma k-ratios flag then load values for this binary (if found) (1 = do not use, 2 = use)
If UsePenepmaKratiosFlag% = 2 Then
notfoundA = True
notfoundB = True
Call AFactorPenepmaReadMatrix(wout!(), rout!(), eout$(), xout$(), zout%(), notfoundA, notfoundB, AFactorTmpSample())
If ierror Then Exit Sub
If Not notfoundA And sample(1).Xrsyms$(k%) <> vbNullString Then PenepmaKratiosFlag(k%, l%) = True
If Not notfoundB And sample(1).Xrsyms$(l%) <> vbNullString Then PenepmaKratiosFlag(l%, k%) = True
End If
' If Penepma k-ratios were not found, initialize calculations for ZAF or phi-rho-z calculations (0 = phi/rho/z, 1,2,3,4 = alpha fits, 5 = calilbration curve, 6 = fundamental parameters)
If UsePenepmaKratiosFlag% = 1 Or (UsePenepmaKratiosFlag% = 2 And (notfoundA And sample(1).Xrsyms$(k%) <> vbNullString) Or (notfoundB And sample(1).Xrsyms$(l%) <> vbNullString)) Then
If CorrectionFlag% <> MAXCORRECTION% Then
Call ZAFSetZAF(AFactorTmpSample())
If ierror Then Exit Sub
Else
'Call ZAFSetZAF3(AFactorTmpSample())
'If ierror Then Exit Sub
End If
' Calculate array of intensities using ZAF or Phi-Rho-Z (zout() is the atomic number for alpha record)
Call ZAFAFactor(wout!(), rout!(), eout$(), xout$(), zout%(), analysis, AFactorTmpSample())
If ierror Then Exit Sub
End If
' Calculate alpha factors, fit and load into alpha-factor look up tables
Call AFactorCalculateFitFactors(k%, l%, wout!(), rout!(), eout$(), xout$(), zout%())
If ierror Then Exit Sub
' Output to log
If VerboseMode Then
For ibin% = 1 To 2
If AFactorTmpSample(1).Xrsyms$(ibin%) <> vbNullString Then
If ibin% = 1 Then
astring$ = "AFactorCalculateKFactors: " & AFactorTmpSample(1).Elsyms$(1) & " " & AFactorTmpSample(1).Xrsyms$(1) & " in " & AFactorTmpSample(1).Elsyms$(2)
Call IOWriteLog$(vbCrLf & astring$ & " at " & Format$(AFactorTmpSample(1).TakeoffArray!(1)) & " degrees and " & Format$(AFactorTmpSample(1).KilovoltsArray!(1)) & " keV")
Else
astring$ = "AFactorCalculateKFactors: " & AFactorTmpSample(1).Elsyms$(2) & " " & AFactorTmpSample(1).Xrsyms$(2) & " in " & AFactorTmpSample(1).Elsyms$(1)
Call IOWriteLog$(vbCrLf & astring$ & " at " & Format$(AFactorTmpSample(1).TakeoffArray!(2)) & " degrees and " & Format$(AFactorTmpSample(1).KilovoltsArray!(2)) & " keV")
End If
astring$ = Format$(vbTab & "Conc%", a08$) & vbTab & Format$("Kratios", a08$) & vbTab & Format$("Alpha", a08$)
Call IOWriteLog$(astring$)
For n% = 1 To MAXBINARY%
m% = (n% - 1) * 2 + ibin%
If ibin% = 1 Then
astring$ = vbTab & MiscAutoFormat$(wout!(m%)) & vbTab & MiscAutoFormat$(rout!(m%)) & MiscAutoFormat$(AlphaYdata1!(n%))
Else
astring$ = vbTab & MiscAutoFormat$(wout!(m%)) & vbTab & MiscAutoFormat$(rout!(m%)) & MiscAutoFormat$(AlphaYdata2!(n%))
End If
Call IOWriteLog$(astring$)
Next n%
End If
Next ibin%
End If
Exit Sub
' Errors
AFactorCalculateKFactorsError:
MsgBox Error$, vbOKOnly + vbCritical, "AFactorCalculateKFactors"
ierror = True
Exit Sub
End Sub
Sub AFactorInitFactors()
' Initialize the alpha arrays (to force a recalculation)
ierror = False
On Error GoTo AFactorInitFactorsError
Dim i As Integer, j As Integer
Call IOWriteLog(vbCrLf & "Initializing alpha-factors...")
alphakev! = 0#
alphatof! = 0#
For i% = 1 To MAXELM%
For j% = 1 To MAXELM%
AlphaXray$(i%, j%) = vbNullString
alphal1!(i%, j%) = 1#
alphal2!(i%, j%) = 0#
alphal3!(i%, j%) = 0#
alphal4!(i%, j%) = 0#
Next j%
Next i%
For i% = 1 To MAXCHAN%
For j% = 1 To MAXCHAN%
alpha1!(i%, j%) = 0#
alpha2!(i%, j%) = 0#
alpha3!(i%, j%) = 0#
alpha4!(i%, j%) = 0#
PenepmaKratiosFlag(i%, j%) = False
Next j%
Next i%
' Indicate that alpha-factors are initialized
AllAFactorUpdateNeeded = False
Exit Sub
' Errors
AFactorInitFactorsError:
MsgBox Error$, vbOKOnly + vbCritical, "AFactorInitFactors"
ierror = True
Exit Sub
End Sub
Sub AFactorLoadEmpirical(sample() As TypeSample)
' Load empirical alpha-factors from the empirical alpha factor arrays
' Note that only "elemental" alpha-factors are supported in Probe, "oxide"
' alpha factors will have to be re-calculated based on the original measured
' k-factors by first normalizing them to oxide end-member k-factors.
ierror = False
On Error GoTo AFactorLoadEmpiricalError
Dim ip As Integer, ipp As Integer
Dim n As Integer
Call IOWriteLog("Loading Empirical Alpha-Factors...")
' Loop on each empirical alpha and check if it is needed
For n% = 1 To MAXEMPFAC%
' Check conditions
If empfackev!(n%) = sample(1).kilovolts! Then
If empfactof!(n%) = sample(1).takeoff! Then
' Find each emitter/absorber position in the sample
ip% = IPOS1(sample(1).LastElm%, empface$(n%), sample(1).Elsyms$())
ipp% = IPOS1(sample(1).LastChan%, empfaca$(n%), sample(1).Elsyms$())
' Check that emitter and absorber match
If ip% <> 0 And ipp% <> 0 Then
' Check that emitter x-ray match
If MiscStringsAreSame(empfacx$(n%), sample(1).Xrsyms$(ip%)) Then
' Load pre-calculated alpha-factor in alpha arrays (0 = phi/rho/z, 1,2,3,4 = alpha fits, 5 = calilbration curve, 6 = fundamental parameters)
If CorrectionFlag% = 1 And empfaco%(n%) = 1 Then
alpha1!(ip%, ipp%) = empfac1!(n%)
alpha2!(ip%, ipp%) = 0#
alpha3!(ip%, ipp%) = 0#
alpha4!(ip%, ipp%) = 0#
If DebugMode Then
Call IOWriteLog("Loading constant empirical alpha for " & sample(1).Elsyms$(ip%) & " " & sample(1).Xrsyms$(ip%) & " in " & sample(1).Elsyms$(ipp%) & Str$(empfac1!(n%)) & Str$(empfac2!(n%)) & Str$(empfac3!(n%)))
End If
End If
' Linear
If CorrectionFlag% = 2 And empfaco%(n%) = 2 Then
alpha1!(ip%, ipp%) = empfac1!(n%)
alpha2!(ip%, ipp%) = empfac2!(n%)
alpha3!(ip%, ipp%) = 0#
alpha4!(ip%, ipp%) = 0#
If DebugMode Then
Call IOWriteLog("Loading linear empirical alpha for " & sample(1).Elsyms$(ip%) & " " & sample(1).Xrsyms$(ip%) & " in " & sample(1).Elsyms$(ipp%) & Str$(empfac1!(n%)) & Str$(empfac2!(n%)) & Str$(empfac3!(n%)))
End If
End If
' Polynomial
If CorrectionFlag% = 3 And empfaco%(n%) = 3 Then
alpha1!(ip%, ipp%) = empfac1!(n%)
alpha2!(ip%, ipp%) = empfac2!(n%)
alpha3!(ip%, ipp%) = empfac3!(n%)
alpha4!(ip%, ipp%) = 0#
If DebugMode Then
Call IOWriteLog("Loading polynomial empirical alpha for " & sample(1).Elsyms$(ip%) & " " & sample(1).Xrsyms$(ip%) & " in " & sample(1).Elsyms$(ipp%) & Str$(empfac1!(n%)) & Str$(empfac2!(n%)) & Str$(empfac3!(n%)))
End If
End If
' Non-linear
If CorrectionFlag% = 4 And empfaco%(n%) = 4 Then
alpha1!(ip%, ipp%) = empfac1!(n%)
alpha2!(ip%, ipp%) = empfac2!(n%)
alpha3!(ip%, ipp%) = empfac3!(n%)
alpha4!(ip%, ipp%) = empfac4!(n%)
If DebugMode Then
Call IOWriteLog("Loading non-linear empirical alpha for " & sample(1).Elsyms$(ip%) & " " & sample(1).Xrsyms$(ip%) & " in " & sample(1).Elsyms$(ipp%) & Str$(empfac1!(n%)) & Str$(empfac2!(n%)) & Str$(empfac3!(n%)) & Str$(empfac4!(n%)))
End If
End If
End If
End If
End If
End If
Next n%
Exit Sub
' Errors
AFactorLoadEmpiricalError:
MsgBox Error$, vbOKOnly + vbCritical, "AFactorLoadEmpirical"
ierror = True
Exit Sub
End Sub
Sub AFactorLoadFactors(analysis As TypeAnalysis, sample() As TypeSample)
' Load the specific alpha-factors based on sample setup
ierror = False
On Error GoTo AFactorLoadFactorsError
Dim emitter As Integer, absorber As Integer
Dim ip As Integer, ipp As Integer, i As Integer
' Check that sample is not a combined conditions sample (only check takeoff and kilovolts)
If sample(1).LastElm% > 1 Then
If MiscIsDifferent3(sample(1).LastElm%, sample(1).TakeoffArray!()) Then GoTo AFactorLoadFactorsCombinedConditions
If MiscIsDifferent3(sample(1).LastElm%, sample(1).KilovoltsArray!()) Then GoTo AFactorLoadFactorsCombinedConditions
End If
' Check for update
If AllAFactorUpdateNeeded Then
Call AFactorInitFactors
If ierror Then Exit Sub
' Re-load the empirical alpha-factors
If EmpiricalAlphaFlag = 2 Then
Call AFactorReadEmpirical
If ierror Then Exit Sub
End If
End If
' Check for a change in operating conditions and re-calculate all if necessary
If alphakev! <> sample(1).kilovolts! Or alphatof! <> sample(1).takeoff! Then
Call AFactorCalculateFactor(Int(0), Int(0), analysis, sample())
If ierror Then Exit Sub
' Loop on each emitting element in sample
Else
For emitter% = 1 To sample(1).LastElm%
ip% = IPOS1(MAXELM%, sample(1).Elsyms$(emitter%), Symlo$())
' Loop on each absorbing element in sample
For absorber% = 1 To sample(1).LastChan%
ipp% = IPOS1(MAXELM%, sample(1).Elsyms$(absorber%), Symlo$())
' Check for missing emitter, xray or absorber combination
If ip% > 0 And ipp% > 0 Then
If AlphaXray$(ip%, ipp%) <> sample(1).Xrsyms$(emitter%) Then
Call AFactorCalculateFactor(emitter%, absorber%, analysis, sample())
If ierror Then Exit Sub
End If
End If
Next absorber%
Next emitter%
End If
' Loop on each emitting element in sample
For emitter% = 1 To sample(1).LastElm%
ip% = IPOS1(MAXELM%, sample(1).Elsyms$(emitter%), Symlo$())
' Loop on each absorbing element in sample
For absorber% = 1 To sample(1).LastChan%
ipp% = IPOS1(MAXELM%, sample(1).Elsyms$(absorber%), Symlo$())
' Load pre-calculated alpha-factors into sample alpha arrays
If ip% > 0 And ipp% > 0 Then
alpha1!(emitter%, absorber%) = alphal1!(ip%, ipp%)
alpha2!(emitter%, absorber%) = alphal2!(ip%, ipp%)
alpha3!(emitter%, absorber%) = alphal3!(ip%, ipp%)
alpha4!(emitter%, absorber%) = alphal4!(ip%, ipp%)
End If
Next absorber%
Next emitter%
' Next check for empirical alpha factors
If EmpiricalAlphaFlag = 2 Then
Call AFactorLoadEmpirical(sample())
If ierror Then Exit Sub
End If
Exit Sub
' Errors
AFactorLoadFactorsError:
MsgBox Error$, vbOKOnly + vbCritical, "AFactorLoadFactors"
ierror = True
Exit Sub
AFactorLoadFactorsCombinedConditions:
msg$ = "Alpha factor calculations are not supported for combined condition samples at this time"
msg$ = msg$ & vbCrLf & "Lastelm: " & Format$(sample(1).LastElm%)
For i% = 1 To sample(1).LastElm%
msg$ = msg$ & vbCrLf & "Element: " & sample(1).Elsyms$(i%) & " " & sample(1).Xrsyms$(i%) & ", TO: " & Format$(sample(1).TakeoffArray!(i%)) & ", KeV: " & Format$(sample(1).KilovoltsArray!(i%))
Next i%
MsgBox msg$, vbOKOnly + vbExclamation, "AFactorLoadFactors"
ierror = True
Exit Sub
End Sub
Sub AFactorLoadFactorsReturn(sample() As TypeSample, talpha1() As Single, talpha2() As Single, talpha3() As Single, talpha4() As Single)
' Return the module level sample specific alpha factors
ierror = False
On Error GoTo AFactorLoadFactorsReturnError
Dim emitter As Integer, absorber As Integer
Dim ip As Integer, ipp As Integer
' Loop on each emitting element in sample
For emitter% = 1 To sample(1).LastElm%
ip% = IPOS1(MAXELM%, sample(1).Elsyms$(emitter%), Symlo$())
' Loop on each absorbing element in sample
For absorber% = 1 To sample(1).LastChan%
ipp% = IPOS1(MAXELM%, sample(1).Elsyms$(absorber%), Symlo$())
' Load pre-calculated alpha-factors into sample alpha arrays
If ip% > 0 And ipp% > 0 Then
talpha1!(emitter%, absorber%) = alphal1!(ip%, ipp%)
talpha2!(emitter%, absorber%) = alphal2!(ip%, ipp%)
talpha3!(emitter%, absorber%) = alphal3!(ip%, ipp%)
talpha4!(emitter%, absorber%) = alphal4!(ip%, ipp%)
End If
Next absorber%
Next emitter%
Exit Sub
' Errors
AFactorLoadFactorsReturnError:
MsgBox Error$, vbOKOnly + vbCritical, "AFactorLoadFactorsReturn"
ierror = True
Exit Sub
End Sub
Sub AFactorReadEmpirical()
' Read empirical alpha-factors from ASCII file
ierror = False
On Error GoTo AFactorReadEmpiricalError
Dim n As Integer
' Load filename
If Dir$(EmpFACFile$) = vbNullString Then GoTo AFactorReadEmpiricalNotFound
Call IOWriteLog("Reading file " & EmpFACFile$ & "...")
' Open file
Open EmpFACFile$ For Input As #EMPFacFileNumber%
n% = 1
Do While Not EOF(EMPFacFileNumber%)
Input #EMPFacFileNumber%, empfactof!(n%), empfackev!(n%), empfaco%(n%), empface$(n%), empfacx$(n%), empfaca$(n%), empfac1!(n%), empfac2!(n%), empfac3!(n%), empfacstr$(n%)
n% = n% + 1
If n% > MAXEMPFAC% Then Exit Do
Loop
Close #EMPFacFileNumber%
Exit Sub
' Errors
AFactorReadEmpiricalError:
MsgBox Error$, vbOKOnly + vbCritical, "AFactorReadEmpirical"
Close #EMPFacFileNumber%
ierror = True
Exit Sub
AFactorReadEmpiricalNotFound:
msg$ = "Empirical alpha-factor file " & EmpFACFile$ & " was not found"
MsgBox msg$, vbOKOnly + vbExclamation, "AFactorReadEmpirical"
ierror = True
Exit Sub
End Sub
Sub AFactorAFASaveFactors(analysis As TypeAnalysis, sample() As TypeSample)
' Save the caluclated conditions and alpha-factors to an ASCII file.
' Note that in addition to the alpha-factors (constant, linear, polynomial and
' non-linear) themselves, this routine stores other information required
' for quantitative calculations (e.g., standard assignments and intensities)
' for quantitative image calculations (CalcImage).
ierror = False
On Error GoTo AFactorAFASaveFactorsError
Dim i As Integer, j As Integer
' Open the alpha-factor ASCII file
Open AFactorDataFile$ For Output As #AFactorDataFileNumber%
' Write run info
Print #AFactorDataFileNumber%, VbDquote$ & ProbeDataFile$ & VbDquote$
Print #AFactorDataFileNumber%, VbDquote$ & MDBUserName$ & VbDquote$
Print #AFactorDataFileNumber%, VbDquote$ & MDBFileTitle$ & VbDquote$
Print #AFactorDataFileNumber%, VbDquote$ & Now & VbDquote$
Print #AFactorDataFileNumber%, sample(1).kilovolts!, sample(1).takeoff!
Print #AFactorDataFileNumber%, sample(1).LastElm%, sample(1).LastChan%, sample(1).OxideOrElemental%, NumberofStandards%
' Write column labels
msg$ = Space$(8) & vbTab
msg$ = msg$ & Format$(VbDquote$ & "Spec" & VbDquote$, a80$) & vbTab
msg$ = msg$ & Format$(VbDquote$ & "Xtal" & VbDquote$, a80$) & vbTab
msg$ = msg$ & Format$(VbDquote$ & "Std#" & VbDquote$, a80$) & vbTab
msg$ = msg$ & Format$(VbDquote$ & "Std%" & VbDquote$, a80$) & vbTab
msg$ = msg$ & Format$(VbDquote$ & "Beta" & VbDquote$, a80$) & vbTab
msg$ = msg$ & Format$(VbDquote$ & "Count" & VbDquote$, a80$) & vbTab
Print #AFactorDataFileNumber%, msg$
' Write standard counts, weight percents and beta factors
For j% = 1 To sample(1).LastElm%
msg$ = Format$(VbDquote$ & sample(1).Elsyms$(j%) & VbDquote$ & " " & VbDquote$ & sample(1).Xrsyms$(j%) & VbDquote$, a80$) & vbTab
msg$ = msg$ & Format$(sample(1).MotorNumbers%(j%), a80$) & vbTab
msg$ = msg$ & Format$(VbDquote$ & sample(1).CrystalNames$(j%), a80$) & VbDquote$ & vbTab
msg$ = msg$ & Format$(sample(1).StdAssigns%(j%), a80$) & vbTab
msg$ = msg$ & Format$(Format$(analysis.StdAssignsPercents!(j%), f83$), a80$) & vbTab
msg$ = msg$ & Format$(Format$(analysis.StdAssignsBetas!(j%), f84$), a80$) & vbTab
msg$ = msg$ & Format$(Format$(analysis.StdAssignsCounts!(j%), f81$), a80$) & vbTab
Print #AFactorDataFileNumber%, msg$
Next j%
' Write column labels
msg$ = Space$(8) & vbTab
msg$ = msg$ & Format$(VbDquote$ & "Oxide" & VbDquote$, a80$) & vbTab