File: test_math.f90

package info (click to toggle)
mctc-lib 0.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,088 kB
  • sloc: f90: 12,894; python: 23; ansic: 15; makefile: 2
file content (160 lines) | stat: -rw-r--r-- 4,632 bytes parent folder | download | duplicates (2)
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
! This file is part of mctc-lib.
!
! Licensed under the Apache License, Version 2.0 (the "License");
! you may not use this file except in compliance with the License.
! You may obtain a copy of the License at
!
!     http://www.apache.org/licenses/LICENSE-2.0
!
! Unless required by applicable law or agreed to in writing, software
! distributed under the License is distributed on an "AS IS" BASIS,
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
! See the License for the specific language governing permissions and
! limitations under the License.

module test_math
   use mctc_env_accuracy, only : wp
   use mctc_env_testing, only : new_unittest, unittest_type, error_type, check
   use mctc_io_math
   implicit none
   private

   public :: collect_math


   real(wp), parameter :: thr = sqrt(epsilon(1.0_wp))

contains


!> Collect all exported unit tests
subroutine collect_math(testsuite)

   !> Collection of tests
   type(unittest_type), allocatable, intent(out) :: testsuite(:)

   testsuite = [ &
      & new_unittest("valid-eigval", test_eigval), &
      & new_unittest("valid-eigvec", test_eigvec), &
      & new_unittest("valid-matdet", test_matdet), &
      & new_unittest("valid-matinv", test_matinv) &
      & ]

end subroutine collect_math


subroutine test_matdet(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   real(wp) :: mat(3, 3), det

   mat = reshape(&
      & [2.0_wp, 1.0_wp, 3.0_wp, 1.0_wp, 3.0_wp, 0.0_wp, 3.0_wp, 0.0_wp, 4.0_wp], &
      & shape(mat))

   det = matdet_3x3(mat)

   call check(error, det, -7.0_wp, thr=thr)

end subroutine test_matdet


subroutine test_matinv(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   real(wp) :: mat(3, 3), inv(3, 3)

   mat = reshape(&
      & [2.0_wp, 1.0_wp, 3.0_wp, 1.0_wp, 3.0_wp, 0.0_wp, 3.0_wp, 0.0_wp, 4.0_wp], &
      & shape(mat))

   inv = matinv_3x3(mat)

   mat = matmul(mat, inv)

   call check(error, mat(1, 1), 1.0_wp, thr=thr)
   if (allocated(error)) return
   call check(error, mat(2, 2), 1.0_wp, thr=thr)
   if (allocated(error)) return
   call check(error, mat(3, 3), 1.0_wp, thr=thr)
   if (allocated(error)) return
   call check(error, mat(1, 2), 0.0_wp, thr=thr)
   if (allocated(error)) return
   call check(error, mat(1, 3), 0.0_wp, thr=thr)
   if (allocated(error)) return
   call check(error, mat(2, 3), 0.0_wp, thr=thr)
   if (allocated(error)) return

end subroutine test_matinv


subroutine test_eigval(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   real(wp) :: mat(3, 3), eval(3)

   mat = reshape(&
      & [2.0_wp, 1.0_wp, 3.0_wp, 1.0_wp, 3.0_wp, 0.0_wp, 3.0_wp, 0.0_wp, 4.0_wp], &
      & shape(mat))

   call eigval_3x3(mat, eval)

   call check(error, eval(1),-0.3611775878183057_wp, thr=thr)
   if (allocated(error)) return
   call check(error, eval(2), 3.0909775466663136_wp, thr=thr)
   if (allocated(error)) return
   call check(error, eval(3), 6.2702000411519920_wp, thr=thr)
   if (allocated(error)) return

end subroutine test_eigval


subroutine test_eigvec(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   real(wp) :: mat(3, 3), eval(3), evec(3, 3)

   mat = reshape(&
      & [2.0_wp, 1.0_wp, 3.0_wp, 1.0_wp, 3.0_wp, 0.0_wp, 3.0_wp, 0.0_wp, 4.0_wp], &
      & shape(mat))

   call eigvec_3x3(mat, eval, evec)

   call check(error, eval(1),-0.3611775878183057_wp, thr=thr)
   if (allocated(error)) return
   call check(error, eval(2), 3.0909775466663136_wp, thr=thr)
   if (allocated(error)) return
   call check(error, eval(3), 6.2702000411519920_wp, thr=thr)
   if (allocated(error)) return

   call check(error, evec(1, 1), 0.80020375200069072_wp, thr=thr)
   if (allocated(error)) return
   call check(error, evec(2, 1),-0.23807244071268843_wp, thr=thr)
   if (allocated(error)) return
   call check(error, evec(3, 1),-0.55045024139982024_wp, thr=thr)
   if (allocated(error)) return
   call check(error, evec(1, 2), 0.08680581175113650_wp, thr=thr)
   if (allocated(error)) return
   call check(error, evec(2, 2), 0.95414544502416110_wp, thr=thr)
   if (allocated(error)) return
   call check(error, evec(3, 2),-0.28648075116117611_wp, thr=thr)
   if (allocated(error)) return
   call check(error, evec(1, 3), 0.59341276219023387_wp, thr=thr)
   if (allocated(error)) return
   call check(error, evec(2, 3), 0.18146069192182893_wp, thr=thr)
   if (allocated(error)) return
   call check(error, evec(3, 3), 0.78417683653434167_wp, thr=thr)
   if (allocated(error)) return

end subroutine test_eigvec


end module test_math