test_analytic_template.F90 19.8 KB
Newer Older
Pavel Kus's avatar
Pavel Kus committed
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
! (c) Copyright Pavel Kus, 2017, MPCDF
!
!    This file is part of ELPA.
!
!    The ELPA library was originally created by the ELPA consortium,
!    consisting of the following organizations:
!
!    - Max Planck Computing and Data Facility (MPCDF), formerly known as
!      Rechenzentrum Garching der Max-Planck-Gesellschaft (RZG),
!    - Bergische Universität Wuppertal, Lehrstuhl für angewandte
!      Informatik,
!    - Technische Universität München, Lehrstuhl für Informatik mit
!      Schwerpunkt Wissenschaftliches Rechnen ,
!    - Fritz-Haber-Institut, Berlin, Abt. Theorie,
!    - Max-Plack-Institut für Mathematik in den Naturwissenschaften,
!      Leipzig, Abt. Komplexe Strukutren in Biologie und Kognition,
!      and
!    - IBM Deutschland GmbH
!
!
!    More information can be found here:
!    http://elpa.mpcdf.mpg.de/
!
!    ELPA is free software: you can redistribute it and/or modify
!    it under the terms of the version 3 of the license of the
!    GNU Lesser General Public License as published by the Free
!    Software Foundation.
!
!    ELPA 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 Lesser General Public License for more details.
!
!    You should have received a copy of the GNU Lesser General Public License
!    along with ELPA.  If not, see <http://www.gnu.org/licenses/>
!
!    ELPA reflects a substantial effort on the part of the original
!    ELPA consortium, and we ask you to respect the spirit of the
!    license that we chose: i.e., please contribute any changes you
!    may have back to the original ELPA library distribution, and keep
!    any derivatives of ELPA under the same license that we chose for
!    the original distribution, the GNU Lesser General Public License.

  subroutine prepare_matrix_analytic_&
    &MATH_DATATYPE&
    &_&
    &PRECISION&
    &(na, a, nblk, myid, np_rows, np_cols, my_prow, my_pcol)
    implicit none
    integer(kind=ik), intent(in)    :: na, nblk, myid, np_rows, np_cols, my_prow, my_pcol
    MATH_DATATYPE(kind=REAL_DATATYPE), intent(inout)   :: a(:,:)

    integer(kind=ik) :: globI, globJ, locI, locJ, levels(num_primes)

55
56
57
58
59
    type(timer_t)    :: timer

    call timer%enable()
    call timer%start("prepare_matrix_analytic")

Pavel Kus's avatar
Pavel Kus committed
60
    ! for debug only, do it systematicaly somehow ... unit tests
Pavel Kus's avatar
Pavel Kus committed
61
62
63
64
65
    call check_module_sanity_&
            &MATH_DATATYPE&
            &_&
            &PRECISION&
            &(myid)
Pavel Kus's avatar
Pavel Kus committed
66
67
68
69
70
71
72
73

    if(.not. decompose(na, levels)) then
      if(myid == 0) then
        print *, "Analytic test can be run only with matrix sizes of the form 2^n * 3^m * 5^o"
        stop 1
      end if
    end if

74
    call timer%start("loop")
Pavel Kus's avatar
Pavel Kus committed
75
76
77
78
    do globI = 1, na
      do globJ = 1, na
        if(map_global_array_index_to_local_index(globI, globJ, locI, locJ, &
                 nblk, np_rows, np_cols, my_prow, my_pcol)) then
79
           call timer%start("evaluation")
Pavel Kus's avatar
Pavel Kus committed
80
81
82
83
84
           a(locI, locJ) = analytic_matrix_&
              &MATH_DATATYPE&
              &_&
              &PRECISION&
              &(na, globI, globJ)
85
          call timer%stop("evaluation")
Pavel Kus's avatar
Pavel Kus committed
86
87
88
        end if
      end do
    end do
89
    call timer%stop("loop")
Pavel Kus's avatar
Pavel Kus committed
90

91
92
93
94
95
    call timer%stop("prepare_matrix_analytic")
    if(myid == 0) then
      call timer%print("prepare_matrix_analytic")
    end if
    call timer%free()
Pavel Kus's avatar
Pavel Kus committed
96
97
98
99
100
101
  end subroutine

  function check_correctness_analytic_&
    &MATH_DATATYPE&
    &_&
    &PRECISION&
Andreas Marek's avatar
Andreas Marek committed
102
    &(na, nev, ev, z, nblk, myid, np_rows, np_cols, my_prow, my_pcol, check_all_evals, check_eigenvectors) result(status)
Pavel Kus's avatar
Pavel Kus committed
103
104
    implicit none
#include "../../src/general/precision_kinds.F90"
Andreas Marek's avatar
Andreas Marek committed
105
106
107
108
109
110
    integer(kind=ik), intent(in)           :: na, nev, nblk, myid, np_rows, &
                                              np_cols, my_prow, my_pcol
    integer(kind=ik)                       :: status, mpierr
    MATH_DATATYPE(kind=rck), intent(inout) :: z(:,:)
    real(kind=rk), intent(inout)           :: ev(:)
    logical, intent(in)                    :: check_all_evals, check_eigenvectors
Pavel Kus's avatar
Pavel Kus committed
111

Andreas Marek's avatar
Andreas Marek committed
112
113
114
115
    integer(kind=ik)                       :: globI, globJ, locI, locJ, &
                                              levels(num_primes)
    real(kind=rk)                          :: diff, max_z_diff, max_ev_diff, &
                                              glob_max_z_diff, max_curr_z_diff
Pavel Kus's avatar
Pavel Kus committed
116
#ifdef DOUBLE_PRECISION
Andreas Marek's avatar
Andreas Marek committed
117
118
    real(kind=rk), parameter               :: tol_eigenvalues = 5e-14_rk8
    real(kind=rk), parameter               :: tol_eigenvectors = 6e-11_rk8
Pavel Kus's avatar
Pavel Kus committed
119
120
121
122
#endif
#ifdef SINGLE_PRECISION
    ! tolerance needs to be very high due to qr tests
    ! it should be distinguished somehow!
Andreas Marek's avatar
Andreas Marek committed
123
124
    real(kind=rk), parameter               :: tol_eigenvalues = 7e-6_rk4
    real(kind=rk), parameter               :: tol_eigenvectors = 4e-3_rk4
Pavel Kus's avatar
Pavel Kus committed
125
#endif
Andreas Marek's avatar
Andreas Marek committed
126
127
    real(kind=rk)                          :: computed_ev, expected_ev
    MATH_DATATYPE(kind=rck)                :: computed_z,  expected_z
Pavel Kus's avatar
Pavel Kus committed
128

Andreas Marek's avatar
Andreas Marek committed
129
130
131
    MATH_DATATYPE(kind=rck)                :: max_value_for_normalization, &
                                              computed_z_on_max_position,  &
                                              normalization_quotient
132
133
    MATH_DATATYPE(kind=rck)                :: max_values_array(np_rows * np_cols), &
                                              corresponding_exact_value
Andreas Marek's avatar
Andreas Marek committed
134
135
136
    integer(kind=ik)                       :: max_value_idx, rank_with_max, &
                                              rank_with_max_reduced,        &
                                              num_checked_evals
137
138
    integer(kind=ik)                       :: max_idx_array(np_rows * np_cols), &
                                              rank
Pavel Kus's avatar
Pavel Kus committed
139

140
141
142
143
144
    type(timer_t)    :: timer

    call timer%enable()
    call timer%start("check_correctness_analytic")

Pavel Kus's avatar
Pavel Kus committed
145

Pavel Kus's avatar
Pavel Kus committed
146
147
148
149
150
    if(.not. decompose(na, levels)) then
      print *, "can not decomopse matrix size"
      stop 1
    end if

151
152
153
154
155
    if(check_all_evals) then
        num_checked_evals = na
    else
        num_checked_evals = nev
    endif
Pavel Kus's avatar
Pavel Kus committed
156
157
158
    !call print_matrix(myid, na, z, "z")
    max_z_diff = 0.0_rk
    max_ev_diff = 0.0_rk
159
    call timer%start("loop_eigenvalues")
160
    do globJ = 1, num_checked_evals
Pavel Kus's avatar
Pavel Kus committed
161
      computed_ev = ev(globJ)
162
      call timer%start("evaluation")
Pavel Kus's avatar
Pavel Kus committed
163
164
165
      expected_ev = analytic_eigenvalues_real_&
              &PRECISION&
              &(na, globJ)
166
      call timer%stop("evaluation")
Pavel Kus's avatar
Pavel Kus committed
167
168
169
      diff = abs(computed_ev - expected_ev)
      max_ev_diff = max(diff, max_ev_diff)
    end do
170
    call timer%stop("loop_eigenvalues")
Pavel Kus's avatar
Pavel Kus committed
171

172
    call timer%start("loop_eigenvectors")
Pavel Kus's avatar
Pavel Kus committed
173
    do globJ = 1, nev
Pavel Kus's avatar
Pavel Kus committed
174
175
176
177
      max_curr_z_diff = 0.0_rk

      ! eigenvectors are unique up to multiplication by scalar (complex in complex case)
      ! to be able to compare them with analytic, we have to normalize them somehow
178
      ! we will find a value in computed eigenvector with highest absolut value and enforce
Pavel Kus's avatar
Pavel Kus committed
179
      ! such multiple of computed eigenvector, that the value on corresponding position is the same
180
181
182
      ! as an corresponding value in the analytical eigenvector

      ! find the maximal value in the local part of given eigenvector (with index globJ)
Pavel Kus's avatar
Pavel Kus committed
183
184
185
      max_value_for_normalization = 0.0_rk
      max_value_idx = -1
      do globI = 1, na
186
187
188
189
190
191
192
        if(map_global_array_index_to_local_index(globI, globJ, locI, locJ, &
                 nblk, np_rows, np_cols, my_prow, my_pcol)) then
          computed_z = z(locI, locJ)
          if(abs(computed_z) > abs(max_value_for_normalization)) then
            max_value_for_normalization = computed_z
            max_value_idx = globI
          end if
Pavel Kus's avatar
Pavel Kus committed
193
194
195
        end if
      end do

196
197
198
199
200
      ! find the global maximum and its position. From technical reasons (looking for a 
      ! maximum of complex number), it is not so easy to do it nicely. Therefore we 
      ! communicate local maxima to mpi rank 0 and resolve there. If we wanted to do
      ! it without this, it would be tricky.. question of uniquness - two complex numbers
      ! with the same absolut values, but completely different... 
Pavel Kus's avatar
Pavel Kus committed
201
#ifdef WITH_MPI
202
203
204
205
206
207
208
209
210
211
212
213
214
      call MPI_Gather(max_value_for_normalization, 1, MPI_MATH_DATATYPE_PRECISION, &
                     max_values_array, 1, MPI_MATH_DATATYPE_PRECISION, 0, MPI_COMM_WORLD, mpierr)
      call MPI_Gather(max_value_idx, 1, MPI_INT, max_idx_array, 1, MPI_INT, 0, MPI_COMM_WORLD, mpierr)
      max_value_for_normalization = 0.0_rk
      max_value_idx = -1
      do rank = 1, np_cols * np_rows 
        if(abs(max_values_array(rank)) > abs(max_value_for_normalization)) then
          max_value_for_normalization = max_values_array(rank)
          max_value_idx = max_idx_array(rank)
        end if
      end do
      call MPI_Bcast(max_value_for_normalization, 1, MPI_MATH_DATATYPE_PRECISION, 0, MPI_COMM_WORLD, mpierr)
      call MPI_Bcast(max_value_idx, 1, MPI_INT, 0, MPI_COMM_WORLD, mpierr)
Pavel Kus's avatar
Pavel Kus committed
215
#endif
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
      ! we decided what the maximum computed value is. Calculate expected value on the same 
      if(abs(max_value_for_normalization) < 0.0001_rk) then 
        if(myid == 0) print *, 'Maximal value in eigenvector too small     :', max_value_for_normalization
        status =1
        return
      end if
      call timer%start("evaluation_helper")
      corresponding_exact_value  = analytic_eigenvectors_&
                                       &MATH_DATATYPE&
                                       &_&
                                       &PRECISION&
                                       &(na, max_value_idx, globJ)
      call timer%stop("evaluation_helper")
      normalization_quotient = corresponding_exact_value / max_value_for_normalization
      ! write(*,*) "normalization q", normalization_quotient

      ! compare computed and expected eigenvector values, but take into account normalization quotient
Pavel Kus's avatar
Pavel Kus committed
233
234
235
236
      do globI = 1, na
        if(map_global_array_index_to_local_index(globI, globJ, locI, locJ, &
                 nblk, np_rows, np_cols, my_prow, my_pcol)) then
           computed_z = z(locI, locJ)
237
           call timer%start("evaluation")
Pavel Kus's avatar
Pavel Kus committed
238
239
240
241
242
           expected_z = analytic_eigenvectors_&
              &MATH_DATATYPE&
              &_&
              &PRECISION&
              &(na, globI, globJ)
243
           call timer%stop("evaluation")
Pavel Kus's avatar
Pavel Kus committed
244
           max_curr_z_diff = max(abs(normalization_quotient * computed_z - expected_z), max_curr_z_diff)
Pavel Kus's avatar
Pavel Kus committed
245
246
247
        end if
      end do
      ! we have max difference of one of the eigenvectors, update global
Pavel Kus's avatar
Pavel Kus committed
248
      max_z_diff = max(max_z_diff, max_curr_z_diff)
249
    end do !globJ
250
    call timer%stop("loop_eigenvectors")
Pavel Kus's avatar
Pavel Kus committed
251
252
253
254
255
256
257

#ifdef WITH_MPI
    call mpi_allreduce(max_z_diff, glob_max_z_diff, 1, MPI_REAL_PRECISION, MPI_MAX, MPI_COMM_WORLD, mpierr)
#else
    glob_max_z_diff = max_z_diff
#endif
    if(myid == 0) print *, 'Maximum error in eigenvalues      :', max_ev_diff
Andreas Marek's avatar
Andreas Marek committed
258
259
260
261
    if (check_eigenvectors) then
      if(myid == 0) print *, 'Maximum error in eigenvectors     :', glob_max_z_diff
    endif

Pavel Kus's avatar
Pavel Kus committed
262
263
264
    status = 0
    if (nev .gt. 2) then
      if (max_ev_diff .gt. tol_eigenvalues .or. max_ev_diff .eq. 0.0_rk) status = 1
Andreas Marek's avatar
Andreas Marek committed
265
266
267
      if (check_eigenvectors) then
        if (glob_max_z_diff .gt. tol_eigenvectors .or. glob_max_z_diff .eq. 0.0_rk) status = 1
      endif
Pavel Kus's avatar
Pavel Kus committed
268
269
    else
      if (max_ev_diff .gt. tol_eigenvalues) status = 1
Andreas Marek's avatar
Andreas Marek committed
270
271
272
      if (check_eigenvectors) then
        if (glob_max_z_diff .gt. tol_eigenvectors) status = 1
      endif
Pavel Kus's avatar
Pavel Kus committed
273
    endif
274
275
276
277
278
279

    call timer%stop("check_correctness_analytic")
    if(myid == 0) then
      call timer%print("check_correctness_analytic")
    end if
    call timer%free()
Pavel Kus's avatar
Pavel Kus committed
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
  end function


  function analytic_matrix_&
    &MATH_DATATYPE&
    &_&
    &PRECISION&
    &(na, i, j) result(element)
    implicit none
    integer(kind=ik), intent(in) :: na, i, j
    MATH_DATATYPE(kind=REAL_DATATYPE)     :: element

    element = analytic_&
    &MATH_DATATYPE&
    &_&
    &PRECISION&
    &(na, i, j, ANALYTIC_MATRIX)

  end function

  function analytic_eigenvectors_&
    &MATH_DATATYPE&
    &_&
    &PRECISION&
    &(na, i, j) result(element)
    implicit none
    integer(kind=ik), intent(in) :: na, i, j
    MATH_DATATYPE(kind=REAL_DATATYPE)               :: element

    element = analytic_&
    &MATH_DATATYPE&
    &_&
    &PRECISION&
    &(na, i, j, ANALYTIC_EIGENVECTORS)

  end function

  function analytic_eigenvalues_&
    &MATH_DATATYPE&
    &_&
    &PRECISION&
    &(na, i) result(element)
    implicit none
    integer(kind=ik), intent(in) :: na, i
    real(kind=REAL_DATATYPE)              :: element

    element = analytic_real_&
    &PRECISION&
    &(na, i, i, ANALYTIC_EIGENVALUES)

  end function

  function analytic_&
    &MATH_DATATYPE&
    &_&
    &PRECISION&
    &(na, i, j, what) result(element)
    implicit none
#include "../../src/general/precision_kinds.F90"
    integer(kind=ik), intent(in)   :: na, i, j, what
    MATH_DATATYPE(kind=rck)        :: element, mat2x2(2,2), mat(5,5)
    real(kind=rk)                  :: a, am, amp
    integer(kind=ik)               :: levels(num_primes)
    integer(kind=ik)               :: ii, jj, m, prime_id, prime, total_level, level

    real(kind=rk), parameter      :: s = 0.5_rk
    real(kind=rk), parameter      :: c = 0.86602540378443864679_rk
    real(kind=rk), parameter      :: sq2 = 1.4142135623730950488_rk

    real(kind=rk), parameter      :: largest_ev = 2.0_rk

    assert(i <= na)
    assert(j <= na)
    assert(i >= 0)
    assert(j >= 0)
    assert(decompose(na, levels))
    ! go to zero-based indexing
    ii = i - 1
    jj = j - 1
    if (na .gt. 2) then
      a = exp(log(largest_ev)/(na-1))
    else
      a = exp(log(largest_ev)/(1))
    endif

    element = 1.0_rck
#ifdef COMPLEXCASE
    element = (1.0_rk, 0.0_rk)
#endif
    total_level = 0
    am = a
    do prime_id = 1,num_primes
      prime = primes(prime_id)
      do  level = 1, levels(prime_id)
        amp = am**(prime-1)
        total_level = total_level + 1
        if(what == ANALYTIC_MATRIX) then
#ifdef REALCASE
Pavel Kus's avatar
Pavel Kus committed
378
379
380
          mat2x2 = reshape((/ c*c + amp * s*s, (amp - 1.0_rk) * s*c,  &
                           (amp - 1.0_rk) * s*c, s*s + amp * c*c  /), &
                                      (/2, 2/), order=(/2,1/))
Pavel Kus's avatar
Pavel Kus committed
381
382
383
384
#endif
#ifdef COMPLEXCASE
          mat2x2 = reshape((/ 0.5_rck * (amp + 1.0_rck) * (1.0_rk, 0.0_rk),   sq2/4.0_rk * (amp - 1.0_rk) * (1.0_rk, 1.0_rk),   &
                              sq2/4.0_rk * (amp - 1.0_rk) * (1.0_rk, -1.0_rk),  0.5_rck * (amp + 1.0_rck) * (1.0_rk, 0.0_rk) /), &
Pavel Kus's avatar
Pavel Kus committed
385
                                      (/2, 2/), order=(/2,1/))
386
387
388
389
! intel 2018 does not reshape correctly (one would have to specify order=(/1,2/)
! until this is resolved, I resorted to the following
          mat2x2(1,2) = sq2/4.0_rk * (amp - 1.0_rk) * (1.0_rk, 1.0_rk)
          mat2x2(2,1) = sq2/4.0_rk * (amp - 1.0_rk) * (1.0_rk, -1.0_rk)
Pavel Kus's avatar
Pavel Kus committed
390
391
392
393
394
#endif
        else if(what == ANALYTIC_EIGENVECTORS) then
#ifdef REALCASE
          mat2x2 = reshape((/ c, s,  &
                           -s,  c  /), &
Pavel Kus's avatar
Pavel Kus committed
395
                                (/2, 2/), order=(/2,1/))
396
397
398
399
! intel 2018 does not reshape correctly (one would have to specify order=(/1,2/)
! until this is resolved, I resorted to the following
          mat2x2(1,2) = s
          mat2x2(2,1) = -s
Pavel Kus's avatar
Pavel Kus committed
400
401
402
403
#endif
#ifdef COMPLEXCASE
          mat2x2 = reshape((/ -sq2/2.0_rck * (1.0_rk, 0.0_rk),       -sq2/2.0_rck * (1.0_rk, 0.0_rk),  &
                              0.5_rk * (1.0_rk, -1.0_rk),  0.5_rk * (-1.0_rk, 1.0_rk)  /), &
Pavel Kus's avatar
Pavel Kus committed
404
                                (/2, 2/), order=(/2,1/))
405
406
407
408
! intel 2018 does not reshape correctly (one would have to specify order=(/1,2/)
! until this is resolved, I resorted to the following
          mat2x2(1,2) = -sq2/2.0_rck * (1.0_rk, 0.0_rk)
          mat2x2(2,1) = 0.5_rk * (1.0_rk, -1.0_rk)
Pavel Kus's avatar
Pavel Kus committed
409
410
411
412
#endif
        else if(what == ANALYTIC_EIGENVALUES) then
          mat2x2 = reshape((/ 1.0_rck, 0.0_rck,  &
                           0.0_rck, amp  /), &
Pavel Kus's avatar
Pavel Kus committed
413
                                 (/2, 2/), order=(/2,1/))
Pavel Kus's avatar
Pavel Kus committed
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
        else
          assert(.false.)
        end if

        mat = 0.0_rck
        if(prime == 2) then
          mat(1:2, 1:2) = mat2x2
        else if(prime == 3) then
          mat((/1,3/),(/1,3/)) = mat2x2
          if(what == ANALYTIC_EIGENVECTORS) then
            mat(2,2) = 1.0_rck
          else
            mat(2,2) = am
          end if
        else if(prime == 5) then
          mat((/1,5/),(/1,5/)) = mat2x2
          if(what == ANALYTIC_EIGENVECTORS) then
            mat(2,2) = 1.0_rck
            mat(3,3) = 1.0_rck
            mat(4,4) = 1.0_rck
          else
            mat(2,2) = am
            mat(3,3) = am**2
            mat(4,4) = am**3
          end if
        else
          assert(.false.)
        end if

  !      write(*,*) "calc value, elem: ", element, ", mat: ", mod(ii,2), mod(jj,2),  mat(mod(ii,2), mod(jj,2)), "am ", am
  !      write(*,*) " matrix mat", mat
        element = element * mat(mod(ii,prime) + 1, mod(jj,prime) + 1)
        ii = ii / prime
        jj = jj / prime

        am = am**prime
      end do
    end do
    !write(*,*) "returning value ", element
  end function
Pavel Kus's avatar
Pavel Kus committed
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


  subroutine print_matrix_&
    &MATH_DATATYPE&
    &_&
    &PRECISION&
    &(myid, na, mat, mat_name)
    implicit none
#include "../../src/general/precision_kinds.F90"
    integer(kind=ik), intent(in)    :: myid, na
    character(len=*), intent(in)    :: mat_name
    MATH_DATATYPE(kind=rck)         :: mat(na, na)
    integer(kind=ik)                :: i,j
    character(len=20)               :: na_str

    if(myid .ne. 0) &
      return
    write(*,*) "Matrix: "//trim(mat_name)
    write(na_str, *) na
    do i = 1, na
#ifdef REALCASE
      write(*, '('//trim(na_str)//'f8.3)') mat(i, :)
#endif
#ifdef COMPLEXCASE
      write(*,'('//trim(na_str)//'(A,f8.3,A,f8.3,A))') ('(', real(mat(i,j)), ',', aimag(mat(i,j)), ')', j=1,na)
#endif
    end do
    write(*,*)
  end subroutine


  subroutine check_matrices_&
    &MATH_DATATYPE&
    &_&
    &PRECISION&
    &(myid, na)
    implicit none
#include "../../src/general/precision_kinds.F90"
    integer(kind=ik), intent(in)    :: myid, na
    MATH_DATATYPE(kind=rck)                  :: A(na, na), S(na, na), L(na, na), res(na, na)
    integer(kind=ik)                :: i, j, decomposition(num_primes)

496
497
498
499
500
501
502
503
    real(kind=rk)             :: err
#ifdef DOUBLE_PRECISION
    real(kind=rk), parameter  :: TOL =  1e-8
#endif
#ifdef SINGLE_PRECISION
    real(kind=rk), parameter  :: TOL =  1e-4
#endif

Pavel Kus's avatar
Pavel Kus committed
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
    assert(decompose(na, decomposition))

    do i = 1, na
      do j = 1, na
        A(i,j) = analytic_matrix_&
              &MATH_DATATYPE&
              &_&
              &PRECISION&
              &(na, i, j)
        S(i,j) = analytic_eigenvectors_&
              &MATH_DATATYPE&
              &_&
              &PRECISION&
              &(na, i, j)
        L(i,j) = analytic_&
              &MATH_DATATYPE&
              &_&
              &PRECISION&
              &(na, i, j, ANALYTIC_EIGENVALUES)
      end do
    end do

    res = matmul(A,S) - matmul(S,L)
527
528
529
530
531
532
    err = maxval(abs(res))
    
    if(err > TOL) then
      print *, "WARNING: sanity test in module analytic failed, error is ", err
    end if

Pavel Kus's avatar
Pavel Kus committed
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
    if(.false.) then
    !if(na == 2 .or. na == 5) then
      call print_matrix(myid, na, A, "A")
      call print_matrix(myid, na, S, "S")
      call print_matrix(myid, na, L, "L")

      call print_matrix(myid, na, matmul(A,S), "AS")
      call print_matrix(myid, na, matmul(S,L), "SL")

      call print_matrix(myid, na, res , "res")
    end if

  end subroutine

  subroutine check_module_sanity_&
    &MATH_DATATYPE&
    &_&
    &PRECISION&
    &(myid)
    implicit none
    integer(kind=ik), intent(in)   :: myid
    integer(kind=ik)               :: decomposition(num_primes), i
    integer(kind=ik), parameter    :: check_sizes(7) = (/2, 3, 5, 6, 10, 25, 150/)
    if(myid == 0) print *, "Checking test_analytic module sanity.... "
    assert(decompose(1500, decomposition))
    assert(all(decomposition == (/2,1,3/)))
    assert(decompose(6,decomposition))
    assert(all(decomposition == (/1,1,0/)))

    do i =1, size(check_sizes)
      call check_matrices_&
          &MATH_DATATYPE&
          &_&
          &PRECISION&
          &(myid, check_sizes(i))
    end do

    if(myid == 0) print *, "Checking test_analytic module sanity.... DONE"

  end subroutine