Debian Codegraph @

mex/sources/heterogeneity/utils/gray_code.f08

raw
1 ! Copyright © 2025-2026 Dynare Team
2 !
3 ! This file is part of Dynare.
4 !
5 ! Dynare is free software: you can redistribute it and/or modify it under the terms of the
6 ! GNU General Public License as published by the Free Software Foundation, either version 3 of
7 ! the License, or (at your option) any later version.
8 !
9 ! Dynare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 ! even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ! GNU General Public License for more details.
12 !
13 ! You should have received a copy of the GNU General Public License along with Dynare. If not,
14 ! see <https://www.gnu.org/licenses/>.
15 !
16 ! Original author: Normann Rion <[email protected]>
17 !
18 ! Gray code utilities for efficient hypercube corner traversal
19
20 module gray_code
21 use iso_fortran_env, only: real64, int32
22 implicit none (type, external)
23
24 contains
25
26 ! Generate Gray code sequence and flip indices
27 ! This is used for efficient traversal of hypercube corners in multi-dimensional
28 ! interpolation and expectation computations
29 subroutine generate_flip_indices(n, flip_idx, Kcorn)
30 integer(int32), intent(in) :: n
31 integer(int32), allocatable, intent(out) :: flip_idx(:)
32 integer(int32), intent(out), optional :: Kcorn
33
34 ! Local variables
35 integer(int32) :: Kcorn_local, t
36 integer(int32), allocatable :: g(:)
37
38 ! Number of corners in n-dimensional hypercube
39 Kcorn_local = ishft(1, n)
40
41 ! Generate Gray code sequence
42 allocate(g(0:Kcorn_local-1))
43 allocate(flip_idx(1:Kcorn_local-1))
44
45 do t = 0, Kcorn_local-1
46 g(t) = ieor(t, ishft(t,-1))
47 end do
48
49 ! Find which dimension flips between consecutive Gray codes
50 do t = 2, Kcorn_local
51 flip_idx(t-1) = trailz(ieor(g(t-1), g(t-2)))+1
52 end do
53
54 if (present(Kcorn)) Kcorn = Kcorn_local
55
56 end subroutine generate_flip_indices
57
58 ! Compute strides for state-only linear indices
59 ! Converts multi-dimensional indices to linear indices
60 subroutine compute_strides(dims, stride)
61 integer(int32), intent(in) :: dims(:)
62 integer(int32), allocatable, intent(out) :: stride(:)
63
64 ! Local variables
65 integer(int32) :: n, k
66
67 n = size(dims, 1, int32)
68 allocate(stride(n))
69
70 stride(1) = 1_int32
71 do k = 2, n
72 stride(k) = stride(k-1) * dims(k-1)
73 end do
74
75 end subroutine compute_strides
76
77 ! Compute low-corner linear indices from multi-dimensional indices
78 subroutine compute_linear_indices(ind, stride, a)
79 integer(int32), contiguous, intent(in) :: ind(:,:), stride(:)
80 integer(int32), contiguous, intent(inout) :: a(:)
81
82 ! Local variables
83 integer(int32) :: n, k
84
85 n = size(stride, 1, int32)
86 a = 1_int32
87 do concurrent (k=1:n)
88 a = a + stride(k)*(ind(:, k)-1_int32)
89 end do
90 end subroutine compute_linear_indices
91
92 ! Compute coefficient updates r_up, r_down and identify hard dimensions
93 ! Hard dimensions are those where interpolation weight w = 1.0 (is_hard_one)
94 ! or w = 0.0 (is_hard_zero), requiring special handling to avoid division by zero
95 subroutine compute_coefficient_updates(w, r_up, r_down, is_hard_one, is_hard_zero)
96 real(real64), contiguous, intent(in) :: w(:,:)
97 real(real64), contiguous, intent(inout) :: r_up(:,:), r_down(:,:)
98 logical, contiguous, intent(inout) :: is_hard_one(:,:), is_hard_zero(:,:)
99
100 ! Local variables
101 integer(int32) :: n, N_om, k, j
102
103 N_om = size(w, 1, int32)
104 n = size(w, 2, int32)
105
106 ! Identify hard dimensions
107 is_hard_one = (w == 1.0_real64) ! Lower boundary: use x(ilow) only
108 is_hard_zero = (w == 0.0_real64) ! Upper boundary: use x(ilow+1) only
109
110 ! Coefficient updates for moving between hypercube corners
111 ! Safe computation avoiding division by zero
112 do concurrent(k=1:n, j=1:N_om)
113 if (is_hard_one(j,k)) then
114 ! w=1: only lower corner contributes, never flip to upper
115 r_up(j,k) = 0.0_real64 ! Not used, but safe default
116 r_down(j,k) = 1.0_real64 ! Not used, but safe default
117 else if (is_hard_zero(j,k)) then
118 ! w=0: only upper corner contributes, start flipped
119 r_up(j,k) = 1.0_real64 ! Not used, but safe default
120 r_down(j,k) = 0.0_real64 ! Not used, but safe default
121 else
122 ! Normal interpolation: 0 < w < 1
123 r_up(j,k) = (1.0_real64-w(j,k))/w(j,k)
124 r_down(j,k) = w(j,k)/(1.0_real64-w(j,k))
125 end if
126 end do
127
128 end subroutine compute_coefficient_updates
129
130 end module gray_code