forked from openmc-dev/openmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_header.F90
390 lines (280 loc) · 12.1 KB
/
matrix_header.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
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
module matrix_header
implicit none
private
type, public :: Matrix
integer :: n ! number of rows/cols in matrix
integer :: nnz ! number of nonzeros in matrix
integer :: n_count ! counter for length of matrix
integer :: nz_count ! counter for number of non zeros
integer, allocatable :: row(:) ! csr row vector
integer, allocatable :: col(:) ! column vector
real(8), allocatable :: val(:) ! matrix value vector
contains
procedure :: create => matrix_create
procedure :: destroy => matrix_destroy
procedure :: add_value => matrix_add_value
procedure :: new_row => matrix_new_row
procedure :: assemble => matrix_assemble
procedure :: get_row => matrix_get_row
procedure :: get_col => matrix_get_col
procedure :: vector_multiply => matrix_vector_multiply
procedure :: search_indices => matrix_search_indices
procedure :: write => matrix_write
procedure :: copy => matrix_copy
procedure :: transpose => matrix_transpose
end type matrix
contains
!===============================================================================
! MATRIX_CREATE allocates CSR vectors
!===============================================================================
subroutine matrix_create(self, n, nnz)
integer, intent(in) :: n ! dimension of matrix
integer, intent(in) :: nnz ! number of nonzeros
class(Matrix), intent(inout) :: self ! matrix instance
! Preallocate vectors
if (.not.allocated(self % row)) allocate(self % row(n+1))
if (.not.allocated(self % col)) allocate(self % col(nnz))
if (.not.allocated(self % val)) allocate(self % val(nnz))
! Set counters to 1
self % n_count = 1
self % nz_count = 1
! Set n and nnz
self % n = n
self % nnz = nnz
end subroutine matrix_create
!===============================================================================
! MATRIX_DESTROY deallocates all space associated with a matrix
!===============================================================================
subroutine matrix_destroy(self)
class(Matrix), intent(inout) :: self ! matrix instance
if (allocated(self % row)) deallocate(self % row)
if (allocated(self % col)) deallocate(self % col)
if (allocated(self % val)) deallocate(self % val)
end subroutine matrix_destroy
!===============================================================================
! MATRIX_ADD_VALUE adds a value to the matrix
!===============================================================================
subroutine matrix_add_value(self, col, val)
integer, intent(in) :: col ! col location in matrix
real(8), intent(in) :: val ! value to store in matrix
class(Matrix), intent(inout) :: self ! matrix instance
! Record the data
self % col(self % nz_count) = col
self % val(self % nz_count) = val
! Increment the number of nonzeros currently stored
self % nz_count = self % nz_count + 1
end subroutine matrix_add_value
!===============================================================================
! MATRIX_NEW_ROW adds a new row by saving the column position of the new row
!===============================================================================
subroutine matrix_new_row(self)
class(Matrix), intent(inout) :: self ! matrix instance
! Record the current number of nonzeros
self % row(self % n_count) = self % nz_count
! Increment the current row that we are on
self % n_count = self % n_count + 1
end subroutine matrix_new_row
!===============================================================================
! MATRIX_ASSEMBLE main rountine to sort all the columns in CSR matrix
!===============================================================================
subroutine matrix_assemble(self)
class(Matrix), intent(inout) :: self ! matrix instance
integer :: i
integer :: first
integer :: last
! Loop around row vector
do i = 1, self % n
! Get bounds
first = self % row(i)
last = self % row(i+1) - 1
! Sort a row
call sort_csr(self % col, self % val, first, last)
end do
end subroutine matrix_assemble
!===============================================================================
! SORT_CSR main routine that performs a sort on a CSR col/val subvector
!===============================================================================
recursive subroutine sort_csr(col, val, first, last)
integer :: col(:) ! column vector to sort
integer :: first ! first value in sort
integer :: last ! last value in sort
real(8) :: val(:) ! value vector to be sorted like columns
integer :: mid ! midpoint value
if (first < last) then
call split(col, val, first, last, mid)
call sort_csr(col, val, first, mid-1)
call sort_csr(col, val, mid+1, last)
end if
end subroutine sort_csr
!===============================================================================
! SPLIT bisects the search space for the sorting routine
!===============================================================================
subroutine split(col, val, low, high, mid)
integer :: col(:) ! column vector to sort
integer :: low ! low index to sort
integer :: high ! high index to sort
integer :: mid ! middle of sort
real(8) :: val(:) ! value vector to be sorted like columns
integer :: left ! contains left value in sort
integer :: right ! contains right value in sort
integer :: iswap ! temporary interger swap
integer :: pivot ! pivotting variable for columns
real(8) :: rswap ! temporary real swap
real(8) :: val0 ! pivot for value vector
left = low
right = high
pivot = col(low)
val0 = val(low)
! Repeat the following while left and right havent met
do while (left < right)
! Scan right to left to find element < pivot
do while (left < right .and. col(right) >= pivot)
right = right - 1
end do
! Scan left to right to find element > pivot
do while (left < right .and. col(left) <= pivot)
left = left + 1
end do
! If left and right havent met, exchange the items
if (left < right) then
iswap = col(left)
col(left) = col(right)
col(right) = iswap
rswap = val(left)
val(left) = val(right)
val(right) = rswap
end if
end do
! Switch the element in split position with pivot
col(low) = col(right)
col(right) = pivot
mid = right
val(low) = val(right)
val(right) = val0
end subroutine split
!===============================================================================
! MATRIX_GET_ROW is a method to get row
!===============================================================================
function matrix_get_row(self, i) result(row)
class(Matrix), intent(in) :: self ! matrix instance
integer, intent(in) :: i ! row to get
integer :: row ! index in col where row begins
row = self % row(i)
end function matrix_get_row
!===============================================================================
! MATRIX_GET_COL is a method to get column
!===============================================================================
function matrix_get_col(self, i) result(col)
class(Matrix), intent(in) :: self ! matrix instance
integer, intent(in) :: i ! index from row vector
integer :: col ! the actual column
col = self % col(i)
end function matrix_get_col
!===============================================================================
! MATRIX_TRANSPOSE transposes a sparse matrix
!===============================================================================
function matrix_transpose(self) result(mat)
class(Matrix), intent(in) :: self ! matrix instance
type(Matrix) :: mat ! transposed matrix
integer :: i, j ! loop indices for row/column
integer :: i_transpose ! loop index for row in transposed matrix
integer :: first, last ! indices in col() array
call mat % create(self % n, self % nnz)
do i_transpose = 1, mat % n
! Set up row in transposed matrix
call mat % new_row()
ROW: do i = 1, self % n
! Get range of columns for row i
first = self % row(i)
last = self % row(i+1) - 1
COL: do j = first, last
if (self % col(j) == i_transpose) then
! If column in original matrix matches row in transposed, add value
call mat % add_value(i, self % val(j))
elseif (self % col(j) > i_transpose) then
exit COL
end if
end do COL
end do ROW
end do
call mat % new_row()
end function matrix_transpose
!===============================================================================
! MATRIX_VECTOR_MULTIPLY allow a vector to multiply the matrix
!===============================================================================
subroutine matrix_vector_multiply(self, vec_in, vec_out)
use constants, only: ZERO
use vector_header, only: Vector
class(Matrix), intent(in) :: self ! matrix instance
type(Vector), intent(in) :: vec_in ! vector to multiply matrix against
type(Vector), intent(inout) :: vec_out ! resulting vector
integer :: i ! row iteration counter
integer :: j ! column iteration counter
! Begin loop around rows
ROWS: do i = 1, self % n
! Initialize target location in vector
vec_out % val(i) = ZERO
! Begin loop around columns
COLS: do j = self % get_row(i), self % get_row(i + 1) - 1
vec_out % val(i) = vec_out % val(i) + self % val(j) * &
vec_in % val(self % get_col(j))
end do COLS
end do ROWS
end subroutine matrix_vector_multiply
!===============================================================================
! MATRIX_SEARCH_INDICES searches for an index in column corresponding to a row
!===============================================================================
subroutine matrix_search_indices(self, row, col, idx, found)
class(Matrix), intent(inout) :: self
integer, intent(in) :: row
integer, intent(in) :: col
integer, intent(out) :: idx
logical, intent(out) :: found
integer :: j
found = .false.
COLS: do j = self % get_row(row), self % get_row(row + 1) - 1
if (self % get_col(j) == col) then
idx = j
found = .true.
exit
end if
end do COLS
end subroutine matrix_search_indices
!===============================================================================
! MATRIX_WRITE writes a matrix to file
!===============================================================================
subroutine matrix_write(self, filename)
character(*), intent(in) :: filename
class(Matrix), intent(inout) :: self
integer :: unit_
integer :: i
integer :: j
open(newunit=unit_, file=filename)
do i = 1, self % n
do j = self % get_row(i), self % get_row(i + 1) - 1
write(unit_,*) i, self % get_col(j), self % val(j)
end do
end do
close(unit_)
end subroutine matrix_write
!===============================================================================
! MATRIX_COPY copies a matrix
!===============================================================================
subroutine matrix_copy(self, mattocopy)
class(Matrix), intent(inout) :: self
type(Matrix), intent(in) :: mattocopy
! Set n and nnz
self % n_count = mattocopy % n_count
self % nz_count = mattocopy % nz_count
self % n = mattocopy % n
self % nnz = mattocopy % nnz
! Allocate vectors
if (.not.allocated(self % row)) allocate(self % row(self % n + 1))
if (.not.allocated(self % col)) allocate(self % col(self % nnz))
if (.not.allocated(self % val)) allocate(self % val(self % nnz))
! Copy over data
self % row = mattocopy % row
self % col = mattocopy % col
self % val = mattocopy % val
end subroutine matrix_copy
end module matrix_header