-
Notifications
You must be signed in to change notification settings - Fork 68
/
emul_complex.f90
264 lines (214 loc) · 6.75 KB
/
emul_complex.f90
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
!!
!! Copyright (C) 2011-2017 Johns Hopkins University
!!
!! This file is part of lesgo.
!!
!! lesgo is free software: you can redistribute it and/or modify
!! it under the terms of the GNU General Public License as published by
!! the Free Software Foundation, either version 3 of the License, or
!! (at your option) any later version.
!!
!! lesgo is distributed in the hope that it will be useful,
!! but WITHOUT ANY WARRANTY; without even the implied warranty of
!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
!! GNU General Public License for more details.
!!
!! You should have received a copy of the GNU General Public License
!! along with lesgo. If not, see <http://www.gnu.org/licenses/>.
!!
!*******************************************************************************
module emul_complex
!*******************************************************************************
!
! The purpose of this module is to provide methods for performing
! complex operations against real arrays emulating complex arrays. The
! real array is to contain interleaved complex information.
!
use types, only : rprec
use messages, only : error
implicit none
save
private
public :: operator( .MUL. ), operator( .MULI. ), operator( .MULR. )
! REAL X COMPLEX
interface operator (.MUL.)
module procedure mul_real_complex_2D
end interface
! REAL X IMAG(COMPLEX)
interface operator (.MULI.)
module procedure mul_real_complex_imag_scalar, mul_real_complex_imag_2D
end interface
! REAL X REAL(COMPLEX)
interface operator (.MULR.)
module procedure mul_real_complex_real_2D
end interface
contains
!*******************************************************************************
function mul_real_complex_imag_scalar( a, a_c ) result(b)
!*******************************************************************************
!
! This function emulates the multiplication of two complex scalars
! by emulating the input real vector (a) as a complex type. This
! subroutine ignores the real part of a_c (e.g. would use this when
! real(a_c) = 0).
!
! Input:
!
! a (real,size(2,1)) - input real vector
! a_c (real) - input imaginary part of complex scalar
!
! Output:
!
! b (real, size(2,1)) - output real vector
!
implicit none
real(rprec), dimension(2), intent(in) :: a
real(rprec), intent(in) :: a_c
real(rprec), dimension(2) :: b
! Cached variables
real(rprec) :: a_c_i
! Cache multi-usage variables
a_c_i = a_c
! Perform multiplication
b(1) = - a(2) * a_c_i
b(2) = a(1) * a_c_i
end function mul_real_complex_imag_scalar
!*******************************************************************************
function mul_real_complex_2D( a, a_c ) result(b)
!*******************************************************************************
! This function emulates the multiplication of two complex 2D array by
! emulating the input real array (a) as a complex type.
!
! Input:
!
! a (real,size(nx_r,ny)) - input real array
! a_c (complex,size(nx_c,ny))- input complex array
!
! Output:
!
! b (real,size(nx_r,ny)) - output real array
!
! Note: nx_c must be nx_r/2
!
implicit none
real(rprec), dimension( :, :), intent(in) :: a
complex(rprec), dimension( :, : ), intent(in) :: a_c
real(rprec), allocatable, dimension(:, :) :: b
! Cached variables
real(rprec) :: a_r, a_i, a_c_r, a_c_i
integer :: i,j,ir,ii
integer :: nx_r, nx_c, ny
! Get the size of the incoming arrays
nx_r = size(a,1)
ny = size(a,2)
nx_c = size(a_c,1)
! Allocate returned array
allocate( b(nx_r, ny) )
! Emulate complex multiplication
! Using outer loop to get contiguous memory access
do j = 1, ny
do i = 1, nx_c
! Real and imaginary indicies of a
ii = 2*i
ir = ii-1
! Cache multi-usage variables
a_r = a(ir,j)
a_i = a(ii,j)
a_c_r = real(a_c(i,j),kind=rprec)
a_c_i = dimag(a_c(i,j))
! Perform multiplication
b(ir,j) = a_r * a_c_r - a_i * a_c_i
b(ii,j) = a_r * a_c_i + a_i * a_c_r
enddo
enddo
end function mul_real_complex_2D
!*******************************************************************************
function mul_real_complex_imag_2D( a, a_c ) result(b)
!*******************************************************************************
! This function emulates the multiplication of two complex 2D array by
! emulating the input real array (a) as a complex type. This subroutine
! ignores the real part of a_c (e.g. would use this when real(a_c) = 0)
!
! Input:
!
! a (real,size(nx_r,ny)) - input real array
! a_c (real,size(nx_c,ny)) - input imaginary part of complex array
!
! Output:
!
! b (real,size(nx_r,ny)) - output real array
!
! Note: nx_c must be nx_r/2
!
implicit none
real(rprec), dimension( :, : ), intent(in) :: a
real(rprec), dimension( :, : ), intent(in) :: a_c
real(rprec), allocatable, dimension(:, :) :: b
! Cached variables
real(rprec) :: a_c_i, cache
integer :: i,j,ii,ir
integer :: nx_r, nx_c, ny
! Get the size of the incoming arrays
nx_r = size(a,1)
ny = size(a,2)
nx_c = size(a_c,1)
! Allocate the returned array
allocate( b(nx_r, ny ) )
! Emulate complex multiplication
do j = 1, ny ! Using outer loop to get contiguous memory access
do i = 1,nx_c
! Real and imaginary indicies of a
ii = 2*i
ir = ii-1
! Cache multi-usage variables
a_c_i = a_c(i,j)
! Perform multiplication (cache data to ensure sequential access)
cache = a(ir,j) * a_c_i
b(ir,j) = - a(ii,j) * a_c_i
b(ii,j) = cache
enddo
enddo
end function mul_real_complex_imag_2D
!*******************************************************************************
function mul_real_complex_real_2D( a, a_c ) result(b)
!*******************************************************************************
! This function emulates the multiplication of two complex 2D array by
! emulating the input real array (a) as a complex type. This subroutine
! ignores the imaginary part of a_c (e.g. would use this when imag(a_c)
! = 0).
!
! Input:
!
! a (real,size(nx_r,ny)) - input/output real array
! a_c (real,size(nx_c,ny)) - input real part of complex array
!
! Output:
!
! b (real, size(nx_r,ny)) - output real array
!
! Note: nx_c must be nx_r/2
!
use types, only : rprec
implicit none
real(rprec), dimension( :, : ), intent(in) :: a
real(rprec), dimension( :, : ), intent(in) :: a_c
real(rprec), allocatable, dimension(:, :) :: b
integer :: i,j,ii,ir
integer :: nx_r, nx_c, ny
! Get the size of the incoming arrays
nx_r = size(a,1)
ny = size(a,2)
nx_c = size(a_c,1)
allocate(b(nx_r,ny))
! Emulate complex multiplication
do j = 1, ny ! Using outer loop to get contiguous memory access
do i = 1, nx_c
! Real and imaginary indicies of a
ii = 2*i
ir = ii-1
! Perform multiplication
b(ir:ii,j) = a_c(i,j)*a(ir:ii,j)
enddo
enddo
end function mul_real_complex_real_2D
end module emul_complex