-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBMmod.f90
More file actions
1525 lines (1312 loc) · 43.8 KB
/
Copy pathBMmod.f90
File metadata and controls
1525 lines (1312 loc) · 43.8 KB
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
! =====================================================================
! Open-Source Summary
! Project: BMmod — Markov Chain Spatial Modeling
! Overview: Modern Fortran 2008/2018 codebase for spatial Markov chain
! modeling and spectral methods. Provides numerical routines
! for balancing, Hessenberg reduction, QR eigenvalue analysis,
! and spectral decomposition, plus domain functions to derive
! transition rates, probabilities, and 3-D models.
! Author: Bosszz
! Date: 2025-12-02
! =====================================================================
! Module: bmmod_types — precision kinds and size constants
module bmmod_types
use iso_fortran_env, only: real64, int32
implicit none
! Define precision constants
integer, parameter :: wp = real64
integer, parameter :: ip = int32
real(wp), parameter :: tol = 1.0e-12_wp
real(wp), parameter :: tol_match = 1.0e-9_wp
! Define array size constants
integer, parameter :: mcat_max = 10
integer, parameter :: mlag_max = 2700000
integer, parameter :: mdat_max = mcat_max * mcat_max * mlag_max
end module bmmod_types
! Module: bmmod_data — global model parameters and shared state
! Purpose: Holds proportions (`p`), directional rates (`rd`), grid spacing
! (`dhx`,`dhy`,`dhz`), grid extents (`nhx`,`nhy`,`nhz`), category
! count (`ncat`), and background category index (`ibkgr`).
module bmmod_data
use bmmod_types
implicit none
! Global variables (formerly common blocks)
real(wp) :: p(mcat_max)
real(wp) :: rd(3, mcat_max, mcat_max)
real(wp) :: dhx, dhy, dhz
integer(ip) :: nhx, nhy, nhz
integer(ip) :: ncat
integer(ip) :: ibkgr
! Constants
integer(ip), parameter :: ldbg = 9
end module bmmod_data
! Module: bmmod_linalg — linear algebra utilities
! Purpose: Implements `balanc`, `elmhes`, `hqr`, and `spectral` based on
! robust EISPACK-style algorithms for stable eigen computations.
! `spectral` constructs spectral components used by core routines.
module bmmod_linalg
use bmmod_types
implicit none
contains
!> Performs spectral decomposition of matrix A
subroutine spectral(n, A, wr, wi, spec)
integer(ip), intent(in) :: n
real(wp), intent(in) :: A(mcat_max, mcat_max)
real(wp), intent(out) :: wr(mcat_max), wi(mcat_max)
complex(wp), intent(out) :: spec(mcat_max, mcat_max, mcat_max)
real(wp) :: mat_a(mcat_max, mcat_max) ! Local copy
real(wp) :: r_mat(mcat_max, mcat_max) ! Original matrix copy
complex(wp) :: w(mcat_max), denom(mcat_max), s(mcat_max, mcat_max)
complex(wp) :: cc
integer(ip) :: i, j, k, l, m, ierr
integer(ip) :: low, igh
! Copy A to local working arrays
r_mat = A
mat_a = A
! Find eigenvalues
call balanc(mcat_max, n, mat_a, low, igh)
call elmhes(mcat_max, n, low, igh, mat_a)
call hqr(mcat_max, n, low, igh, mat_a, wr, wi, ierr)
if (ierr /= 0) then
print *, "Error in HQR: ierr =", ierr
end if
! Find the spectrum
! Convert wr and wi to complex
do i = 1, n
w(i) = cmplx(wr(i), wi(i), kind=wp)
end do
! Calculate spectrum
do i = 1, n
denom(i) = cmplx(1.0_wp, 0.0_wp, kind=wp)
! Initialize spec
spec(i, :, :) = cmplx(0.0_wp, 0.0_wp, kind=wp)
do k = 1, n
spec(i, k, k) = cmplx(1.0_wp, 0.0_wp, kind=wp)
end do
! Calculate denominator and numerator
do j = 1, n
if (j /= i) then
denom(i) = denom(i) * (w(j) - w(i))
! Assign spec to s
s = spec(i, :, :)
! Update spec: spec(i) = s * (w(j)*I - R)
do l = 1, n
do k = 1, n
cc = cmplx(0.0_wp, 0.0_wp, kind=wp)
do m = 1, n
if (m == l) then
cc = cc + s(k, m) * (w(j) - r_mat(m, l))
else
cc = cc + s(k, m) * (-r_mat(m, l))
end if
end do
spec(i, k, l) = cc
end do
end do
end if
end do
! Divide spec by denom
spec(i, :, :) = spec(i, :, :) / denom(i)
end do
end subroutine spectral
!> Balances a real matrix and isolates eigenvalues whenever possible
subroutine balanc(nm, n, a, low, igh)
integer(ip), intent(in) :: nm, n
real(wp), intent(inout) :: a(nm, n)
integer(ip), intent(out) :: low, igh
real(wp) :: c, f, g, r, s, b2, radix
integer(ip) :: i, j, k, l, m, iexc
logical :: noconv
radix = 16.0_wp
b2 = radix * radix
k = 1
l = n
! Search for rows isolating an eigenvalue and push them down
do
iexc = 0
do j = l, 1, -1
! Check if row j has only zero off-diagonal elements
do i = 1, l
if (i /= j .and. abs(a(j, i)) > tol) goto 10
end do
! Row j is isolated
m = l
iexc = 1
goto 20
10 continue
end do
goto 30
20 continue
! Exchange row/col j and m
if (j /= m) then
do i = 1, l
f = a(i, j); a(i, j) = a(i, m); a(i, m) = f
end do
do i = k, n
f = a(j, i); a(j, i) = a(m, i); a(m, i) = f
end do
end if
if (iexc == 1) then
l = l - 1
if (l == 1) goto 30
end if
end do
30 continue
! Search for columns isolating an eigenvalue and push them left
do
iexc = 0
do j = k, l
! Check if col j has only zero off-diagonal elements
do i = k, l
if (i /= j .and. abs(a(i, j)) > tol) goto 40
end do
! Col j is isolated
m = k
iexc = 2
goto 50
40 continue
end do
goto 60
50 continue
! Exchange row/col j and m
if (j /= m) then
do i = 1, l
f = a(i, j); a(i, j) = a(i, m); a(i, m) = f
end do
do i = k, n
f = a(j, i); a(j, i) = a(m, i); a(m, i) = f
end do
end if
if (iexc == 2) then
k = k + 1
end if
end do
60 continue
! Iterative loop for norm reduction
do
noconv = .false.
do i = k, l
c = 0.0_wp
r = 0.0_wp
do j = k, l
if (j /= i) then
c = c + abs(a(j, i))
r = r + abs(a(i, j))
end if
end do
if (abs(c) <= tol .or. abs(r) <= tol) cycle
g = r / radix
f = 1.0_wp
s = c + r
do while (c < g)
f = f * radix
c = c * b2
end do
g = r * radix
do while (c >= g)
f = f / radix
c = c / b2
end do
if ((c + r) / f < 0.95_wp * s) then
g = 1.0_wp / f
noconv = .true.
do j = k, n
a(i, j) = a(i, j) * g
end do
do j = 1, l
a(j, i) = a(j, i) * f
end do
end if
end do
if (.not. noconv) exit
end do
low = k
igh = l
end subroutine balanc
!> Reduces a submatrix to upper Hessenberg form
subroutine elmhes(nm, n, low, igh, a)
integer(ip), intent(in) :: nm, n, low, igh
real(wp), intent(inout) :: a(nm, n)
integer(ip) :: i, j, m, ip
real(wp) :: x, y
do m = low + 1, igh - 1
x = 0.0_wp
ip = m
do j = m, igh
if (abs(a(j, m - 1)) > abs(x)) then
x = a(j, m - 1)
ip = j
end if
end do
if (abs(x) > tol) then
! Interchange columns
do j = 1, igh
y = a(j, ip)
a(j, ip) = a(j, m)
a(j, m) = y
end do
! Interchange rows
y = a(ip, m - 1)
a(ip, m - 1) = a(m, m - 1)
a(m, m - 1) = y
do i = m + 1, igh
a(i, m - 1) = a(i, m - 1) / x
y = a(ip, m)
a(ip, m) = a(m, m)
a(m, m) = y
y = -y
do j = m + 1, igh
a(i, j) = a(i, j) + y * a(i, m - 1)
end do
do j = 1, igh
a(j, m) = a(j, m) + a(j, m - 1) * a(i, j)
end do
end do
do j = igh + 1, n
y = a(ip, j)
a(ip, j) = a(m, j)
a(m, j) = y
y = -y
do i = m + 1, igh
a(i, j) = a(i, j) + y * a(i, m - 1)
end do
end do
end if
end do
end subroutine elmhes
!> Finds eigenvalues of a real upper Hessenberg matrix
subroutine hqr(nm, n, low, igh, h, wr, wi, ierr)
integer(ip), intent(in) :: nm, n, low, igh
real(wp), intent(inout) :: h(nm, n)
real(wp), intent(out) :: wr(n), wi(n)
integer(ip), intent(out) :: ierr
integer(ip) :: i, j, k, l, m, en, itn, its
real(wp) :: p, q, r, s, t, w, x, y, zz, norm, tst1, tst2
logical :: notlas
ierr = 0
norm = 0.0_wp
do j = 1, n
do i = 1, min(n, j + 1)
norm = norm + abs(h(i, j))
end do
end do
wr = 0.0_wp
wi = 0.0_wp
do i = 1, low - 1
wr(i) = h(i, i)
end do
do i = igh + 1, n
wr(i) = h(i, i)
end do
en = igh
t = 0.0_wp
itn = 30 * n
! Main loop
do while (en >= low)
! Loop for finding next eigenvalue
do
if (itn == 0) then
ierr = en
return
end if
its = 0
! Inner loop
do
! Look for single small sub-diagonal element
do l = en, low + 1, -1
s = abs(h(l - 1, l - 1)) + abs(h(l, l))
if (abs(s) <= tol) s = norm
tst1 = s
tst2 = tst1 + abs(h(l, l - 1))
if (abs(tst2 - tst1) <= tol * max(1.0_wp, abs(tst1))) exit
end do
! if l loop finishes naturally, l = low. But we use exit.
x = h(en, en)
if (l == en) goto 100 ! One root found
y = h(en - 1, en - 1)
w = h(en, en - 1) * h(en - 1, en)
if (l == en - 1) goto 200 ! Two roots found
if (its == 10 .or. its == 20) then
! Exceptional shift
t = t + x
do i = low, en
h(i, i) = h(i, i) - x
end do
s = abs(h(en, en - 1)) + abs(h(en - 1, en - 2))
x = 0.75_wp * s
y = x
w = -0.4375_wp * s * s
end if
its = its + 1
itn = itn - 1
! Look for two consecutive small sub-diagonal elements
do m = en - 2, l, -1
zz = h(m, m)
r = x - zz
s = y - zz
p = (r * s - w) / h(m + 1, m) + h(m, m + 1)
q = h(m + 1, m + 1) - zz - r - s
r = h(m + 2, m + 1)
s = abs(p) + abs(q) + abs(r)
p = p / s
q = q / s
r = r / s
if (m == l) exit
tst1 = abs(p) * (abs(h(m - 1, m - 1)) + abs(zz) + abs(h(m + 1, m + 1)))
tst2 = tst1 + abs(h(m, m - 1)) * (abs(q) + abs(r))
if (abs(tst2 - tst1) <= tol * max(1.0_wp, abs(tst1))) exit
end do
h(m + 2, m) = 0.0_wp
do i = m + 3, en
h(i, i - 2) = 0.0_wp
h(i, i - 3) = 0.0_wp
end do
! Double QR step
do k = m, en - 1
notlas = (k /= en - 1)
if (k /= m) then
p = h(k, k - 1)
q = h(k + 1, k - 1)
r = 0.0_wp
if (notlas) r = h(k + 2, k - 1)
x = abs(p) + abs(q) + abs(r)
if (abs(x) <= tol) cycle
p = p / x
q = q / x
r = r / x
end if
s = sign(sqrt(p * p + q * q + r * r), p)
if (k /= m) then
h(k, k - 1) = -s * x
else if (l /= m) then
h(k, k - 1) = -h(k, k - 1)
end if
p = p + s
x = p / s
y = q / s
zz = r / s
q = q / p
r = r / p
! Row modification
do j = k, en
p = h(k, j) + q * h(k + 1, j)
if (notlas) then
p = p + r * h(k + 2, j)
h(k + 2, j) = h(k + 2, j) - p * zz
end if
h(k + 1, j) = h(k + 1, j) - p * y
h(k, j) = h(k, j) - p * x
end do
! Column modification
do i = l, min(en, k + 3)
p = x * h(i, k) + y * h(i, k + 1)
if (notlas) then
p = p + zz * h(i, k + 2)
h(i, k + 2) = h(i, k + 2) - p * r
end if
h(i, k + 1) = h(i, k + 1) - p * q
h(i, k) = h(i, k) - p
end do
end do
end do ! End inner loop (its)
100 continue
! One root found
wr(en) = x + t
wi(en) = 0.0_wp
en = en - 1
exit ! Back to while(en >= low)
200 continue
! Two roots found
p = (y - x) / 2.0_wp
q = p * p + w
zz = sqrt(abs(q))
x = x + t
if (q >= 0.0_wp) then
! Real pair
zz = p + sign(zz, p)
wr(en - 1) = x + zz
wr(en) = wr(en - 1)
if (abs(zz) > tol) wr(en) = x - w / zz
wi(en - 1) = 0.0_wp
wi(en) = 0.0_wp
else
! Complex pair
wr(en - 1) = x + p
wr(en) = x + p
wi(en - 1) = zz
wi(en) = -zz
end if
en = en - 2
exit ! Back to while(en >= low)
end do
end do
end subroutine hqr
end module bmmod_linalg
! Module: bmmod_core — domain logic
! Purpose: Converts between conceptual parameters and transition rates,
! checks consistency, computes embedded probabilities/frequencies,
! builds 3-D models (`tp3d`), and interpolates (`r2txyz`).
module bmmod_core
use bmmod_types
use bmmod_data
use bmmod_linalg
implicit none
contains
!> Calculates Embedded Transition Frequency matrix and entropy
subroutine r2etf(r, f, entropy)
real(wp), intent(in) :: r(mcat_max, mcat_max)
real(wp), intent(out) :: f(mcat_max, mcat_max)
real(wp), intent(out) :: entropy
real(wp) :: s(mcat_max), tot
integer(ip) :: j, k
! Calculate 'tot' and row totals
tot = 0.0_wp
do j = 1, ncat
s(j) = -p(j) * r(j, j)
tot = tot + s(j)
end do
! Calculate 'frequencies'
entropy = 0.0_wp
f = 0.0_wp
do j = 1, ncat
do k = 1, ncat
if (k /= j) then
f(j, k) = p(j) * r(j, k) / tot
f(j, j) = f(j, j) + f(j, k)
if (f(j, k) > 0.0_wp) then
entropy = entropy - f(j, k) * log(f(j, k))
else if (f(j, k) < 0.0_wp) then
entropy = entropy + 10000.0_wp * f(j, k)
end if
end if
end do
end do
end subroutine r2etf
!> Calculate embedded transition probabilities
subroutine r2etp(r, etp)
real(wp), intent(in) :: r(mcat_max, mcat_max)
real(wp), intent(out) :: etp(mcat_max, mcat_max)
integer(ip) :: j, k
do j = 1, ncat
etp(j, j) = 1.0_wp
do k = 1, ncat
if (j /= k) etp(j, k) = -r(j, k) / r(j, j)
end do
end do
end subroutine r2etp
!> Calculate rates with respect to volumetric proportions
subroutine r2p(r, rp)
real(wp), intent(in) :: r(mcat_max, mcat_max)
real(wp), intent(out) :: rp(mcat_max, mcat_max)
integer(ip) :: j, k
do j = 1, ncat
do k = 1, ncat
if (k /= j) rp(j, k) = -r(j, j) * p(k) / (1.0_wp - p(j))
end do
end do
do j = 1, ncat
rp(j, j) = -1.0_wp / r(j, j)
do k = 1, ncat
if (k /= j) rp(j, k) = r(j, k) / rp(j, k)
end do
end do
end subroutine r2p
!> Calculate rates with respect to number of embedded occurrences
subroutine r2n(r, rn)
real(wp), intent(in) :: r(mcat_max, mcat_max)
real(wp), intent(out) :: rn(mcat_max, mcat_max)
real(wp) :: tot
integer(ip) :: j, k
tot = 0.0_wp
do j = 1, ncat
tot = tot - p(j) * r(j, j)
end do
do j = 1, ncat
do k = 1, ncat
if (k /= j) rn(j, k) = r(j, j) * p(k) * r(k, k) / (tot + p(j) * r(j, j))
end do
end do
do j = 1, ncat
rn(j, j) = -1.0_wp / r(j, j)
do k = 1, ncat
if (k /= j) rn(j, k) = r(j, k) / rn(j, k)
end do
end do
end subroutine r2n
!> Calculate rates w.r.t. independent transition frequencies
subroutine r2i(r, ri)
real(wp), intent(in) :: r(mcat_max, mcat_max)
real(wp), intent(out) :: ri(mcat_max, mcat_max)
real(wp) :: r0(mcat_max, mcat_max), entmax
integer(ip) :: j, k
do j = 1, ncat
r0(j, j) = -1.0_wp / r(j, j)
do k = 1, ncat
if (k /= j) r0(j, k) = 1.0_wp
end do
end do
call indep(r0, ri, entmax)
do j = 1, ncat
ri(j, j) = -1.0_wp / r(j, j)
do k = 1, ncat
if (k /= j) ri(j, k) = r(j, k) / ri(j, k)
end do
end do
end subroutine r2i
!> Independent juxtapositional tendencies (Max Entropy)
subroutine indep(r0, rconc, entmax)
real(wp), intent(in) :: r0(mcat_max, mcat_max)
real(wp), intent(out) :: rconc(mcat_max, mcat_max)
real(wp), intent(out) :: entmax
real(wp) :: s(mcat_max), f(mcat_max), rtot(mcat_max)
real(wp) :: tot, freq
integer(ip) :: i, j, k
tot = 0.0_wp
do i = 1, ncat
s(i) = p(i) / r0(i, i)
tot = tot + s(i)
end do
s = s / tot
f = s ! Initialize marginal frequencies
! IPF Loop
do i = 1, 30
do j = 1, ncat
rtot(j) = 0.0_wp
do k = 1, ncat
if (k /= j) rtot(j) = rtot(j) + f(j) * f(k)
end do
f(j) = f(j) * s(j) / rtot(j)
end do
end do
! Convert to rate
do j = 1, ncat
rconc(j, j) = -1.0_wp / r0(j, j)
rtot(j) = 0.0_wp
do k = 1, ncat
if (k /= j) rtot(j) = rtot(j) + f(j) * f(k)
end do
do k = 1, ncat
if (k /= j) rconc(j, k) = -rconc(j, j) * f(j) * f(k) / rtot(j)
end do
end do
! Calculate max entropy
entmax = 0.0_wp
do j = 1, ncat - 1
do k = j + 1, ncat
freq = f(j) * f(k)
if (freq > 0.0_wp) then
entmax = entmax - 2.0_wp * freq * log(freq)
else if (freq < 0.0_wp) then
entmax = entmax - 20000.0_wp * freq
end if
end do
end do
end subroutine indep
!> Embedded Markov chain transition probabilities
subroutine emctp(r0, r)
real(wp), intent(inout) :: r0(mcat_max, mcat_max)
real(wp), intent(out) :: r(mcat_max, mcat_max)
integer(ip) :: j, k
! Check for symmetry assumptions
do j = 1, ncat
if (j /= ibkgr) then
do k = 1, ncat
if (k /= j .and. k /= ibkgr .and. abs(r0(j, k) + 1.0_wp) <= tol_match) then
r0(j, k) = r0(k, j) * p(k) * r0(j, j) / (p(j) * r0(k, k))
end if
end do
end if
end do
! Convert to transition rates
do j = 1, ncat
if (j /= ibkgr) then
do k = 1, ncat
if (k /= ibkgr) then
if (k == j) then
r(j, j) = -1.0_wp / r0(j, j)
else
r(j, k) = r0(j, k) / r0(j, j)
end if
end if
end do
end if
end do
end subroutine emctp
!> Embedded Markov chain transition frequencies
subroutine emctf(r0, r)
real(wp), intent(inout) :: r0(mcat_max, mcat_max)
real(wp), intent(out) :: r(mcat_max, mcat_max)
real(wp) :: fmarg(mcat_max), x(mcat_max)
real(wp) :: tot, totx
integer(ip) :: j, k
logical :: diff
tot = 0.0_wp
do j = 1, ncat
if (r0(j, j) > 0.0_wp) then
tot = tot + p(j) / r0(j, j)
else
write(ldbg, "('Mean length for category',i3,' must be greater than zero')") j
end if
end do
do j = 1, ncat
if (j /= ibkgr) then
fmarg(j) = p(j) / (r0(j, j) * tot)
do k = 1, ncat
if (k /= ibkgr) then
if (j == k) then
r(j, j) = -1.0_wp / r0(j, j)
else
if (abs(r0(j, k) + 1.0_wp) <= tol_match) r0(j, k) = r0(k, j)
r(j, k) = (1.0_wp / r0(j, j)) * (r0(j, k) / fmarg(j))
end if
end if
end do
end if
end do
! Check consistency
if (ibkgr <= 0 .or. ibkgr > ncat) then
totx = 0.0_wp
do j = 1, ncat
x(j) = 0.0_wp
do k = 1, ncat
if (k /= j) x(j) = x(j) + r0(j, k)
end do
totx = totx + x(j)
end do
do j = 1, ncat
x(j) = x(j) * totx * r0(j, j)
end do
diff = .false.
do j = 1, ncat
if (abs(fmarg(j) - x(j)) > 1e-5) diff = .true. ! Added epsilon
end do
if (diff) then
print *, "WARNING: Proportions intrinsic to rate matrix differ from input."
write(ldbg, *) "WARNING: Proportions intrinsic to rate matrix differ from input."
write(ldbg, "(10f7.4)") (x(j), j=1, ncat)
end if
end if
end subroutine emctf
!> Volumetric proportions
subroutine vprop(r0, rconc)
real(wp), intent(in) :: r0(mcat_max, mcat_max)
real(wp), intent(out) :: rconc(mcat_max, mcat_max)
integer(ip) :: j, k
do j = 1, ncat
if (j /= ibkgr) then
rconc(j, j) = -1.0_wp / r0(j, j)
do k = 1, ncat
if (k /= ibkgr .and. k /= j) then
rconc(j, k) = p(k) / (r0(j, j) * (1.0_wp - p(j)))
end if
end do
end if
end do
end subroutine vprop
!> Number of embedded occurrences
subroutine nprop(r0, rconc)
real(wp), intent(inout) :: r0(mcat_max, mcat_max)
real(wp), intent(out) :: rconc(mcat_max, mcat_max)
real(wp) :: denom
integer(ip) :: j, k, iloop
if (ibkgr == 0) then
do j = 1, ncat
rconc(j, j) = -1.0_wp / r0(j, j)
denom = 0.0_wp
do k = 1, ncat
if (k /= j) denom = denom + p(k) / r0(k, k)
end do
do k = 1, ncat
if (k /= j) rconc(j, k) = -rconc(j, j) * p(k) / (r0(k, k) * denom)
end do
end do
else if (ibkgr >= 1) then
do k = 1, ncat
if (r0(k, k) <= 0.0_wp) then
print *, 'Need a mean length for category', k, '!'
r0(k, k) = 1.0_wp
end if
rconc(k, k) = -1.0_wp / r0(k, k)
end do
do iloop = 1, 20
do j = 1, ncat
denom = 0.0_wp
do k = 1, ncat
if (k /= j) denom = denom - p(k) * rconc(k, k)
end do
if (j /= ibkgr) then
rconc(j, ibkgr) = -rconc(j, j)
do k = 1, ncat
if (k /= j .and. k /= ibkgr) then
rconc(j, k) = rconc(j, j) * p(k) * rconc(k, k) / denom
rconc(j, ibkgr) = rconc(j, ibkgr) - rconc(j, k)
end if
end do
end if
end do
! Calculate new background category mean length
rconc(ibkgr, ibkgr) = 0.0_wp
do j = 1, ncat
if (j /= ibkgr) rconc(ibkgr, ibkgr) = rconc(ibkgr, ibkgr) - p(j) * rconc(j, ibkgr)
end do
rconc(ibkgr, ibkgr) = rconc(ibkgr, ibkgr) / p(ibkgr)
end do
do k = 1, ncat
if (k /= ibkgr) then
rconc(ibkgr, k) = 0.0_wp
do j = 1, ncat
if (j /= ibkgr) rconc(ibkgr, k) = rconc(ibkgr, k) - p(j) * rconc(j, k)
end do
end if
end do
rconc(ibkgr, :) = rconc(ibkgr, :) / p(ibkgr)
end if
end subroutine nprop
!> Check rate matrix consistency
subroutine checkr(r, diff)
real(wp), intent(in) :: r(mcat_max, mcat_max)
logical, intent(out) :: diff
real(wp) :: wr(mcat_max), wi(mcat_max)
complex(wp) :: spec(mcat_max, mcat_max, mcat_max)
real(wp) :: dif
integer(ip) :: i, j, k, iprop
diff = .false.
call spectral(ncat, r, wr, wi, spec)
do i = 1, ncat
if (abs(wr(i)) < 1e-5 .and. abs(wi(i)) < 1e-5) then
iprop = i
do k = 1, ncat
do j = 1, ncat
dif = abs(real(spec(i, j, k)) - p(k))
if (dif > 0.005_wp) diff = .true.
end do
end do
end if
end do
if (diff) then
print *, "WARNING: Proportions intrinsic to rate matrix differ from input."
write(ldbg, *) "WARNING: Proportions intrinsic to rate matrix differ from input."
write(ldbg, "(10f7.4)") (real(spec(iprop, 1, k)), k=1, ncat)
print "(10f7.4)", (real(spec(iprop, 1, k)), k=1, ncat)
end if
end subroutine checkr
!> Matrix print
subroutine mprint(txt, n, a, unit, fmt, iparen)
character(len=*), intent(in) :: txt
integer(ip), intent(in) :: n, unit, iparen
real(wp), intent(in) :: a(mcat_max, mcat_max)
character(len=*), intent(in) :: fmt
integer(ip) :: i, j
write(unit, *) ' '
write(unit, "(a)") trim(txt)
do i = 1, n
if (iparen == 1) then
write(unit, "(*(a, " // trim(fmt) // ", a))") ("(", a(i, j), ")", j=1, n)
else
write(unit, "(*(" // trim(fmt) // "))") (a(i, j), j=1, n)
end if
end do
end subroutine mprint
!> Build 3-D transition probability model
subroutine tp3d(filtxyz, fildet)
character(len=*), intent(in) :: filtxyz, fildet
real(wp), allocatable :: txyz(:), det(:)
real(wp) :: t(mcat_max, mcat_max)
complex(wp) :: s
integer(ip) :: idim(5), idimdet(3)
integer(ip) :: ihx, ihy, ihz, ia, ikl, idat
integer(ip) :: nxyz, ntxyz, ndetxyz
integer(ip) :: k, l
real(wp) :: pwr
idim(1) = 2 * nhx + 1
idim(2) = 2 * nhy + 1
idim(3) = 2 * nhz + 1
idim(4) = ncat
idim(5) = ncat
idimdet = idim(1:3)
ia = 0
pwr = 1.0_wp / real(ncat - 1, wp)
nxyz = idim(1) * idim(2) * idim(3)
ntxyz = nxyz * ncat * ncat
ndetxyz = nxyz
allocate(txyz(ntxyz))
allocate(det(ndetxyz))
do ihz = -nhz, nhz
do ihy = -nhy, nhy
do ihx = -nhx, nhx
ia = ia + 1
call r2txyz(ihx, ihy, ihz, t, s)
det(ia) = real(s, wp)**pwr
! Flatten t into txyz
ikl = 0
do k = 1, ncat
do l = 1, ncat
ikl = ikl + 1
idat = ia + (ikl - 1) * nxyz
txyz(idat) = t(k, l)
end do
end do
end do
end do
end do
open(8, file=filtxyz, status='unknown')
write(8, *) 5
write(8, *) idim
write(8, *) txyz(1:ntxyz)
close(8)