Blame view

SRC/MATHS_MODULES/dmatrix.F90 6.54 KB
886c558b   Steve Greedy   SACAMOS Public Re...
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
!
! This file is part of SACAMOS, State of the Art CAble MOdels in Spice. 
! It was developed by the University of Nottingham and the Netherlands Aerospace 
! Centre (NLR) for ESA under contract number 4000112765/14/NL/HK.
! 
! Copyright (C) 2016-2017 University of Nottingham
! 
! SACAMOS 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.
! 
! SACAMOS 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.
! 
! A copy of the GNU General Public License version 3 can be found in the 
! file GNU_GPL_v3 in the root or at <http://www.gnu.org/licenses/>.
! 
! SACAMOS uses the EISPACK library (in /SRC/EISPACK). EISPACK is subject to 
! the GNU Lesser General Public License. A copy of the GNU Lesser General Public 
! License version can be found in the file GNU_LGPL in the root of EISPACK 
! (/SRC/EISPACK ) or at <http://www.gnu.org/licenses/>.
! 
! The University of Nottingham can be contacted at: ggiemr@nottingham.ac.uk
!
! SUBROUTINE dwrite_matrix(a,ar,ac,dim,unit)
! SUBROUTINE dread_matrix(a,ar,ac,dim,unit)
! SUBROUTINE dinvert_Gauss_Jordan(A,ar,AI,dim) 
44c11f06   Chris Smartt   Include software ...
31
!
886c558b   Steve Greedy   SACAMOS Public Re...
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
!_____________________________________________________________________
!
! NAME
!    dwrite_matrix
!
! DESCRIPTION
!    write real(dp) matrix to file or screen
!
!
! SEE ALSO
!   
!
! HISTORY
!
!     started 26/04/15 CJS
!
! COMMENTS
!   
!
  SUBROUTINE dwrite_matrix(a,ar,ac,dim,unit)

! Modules used

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  integer,intent(IN)  :: dim             ! matrix dimension
  integer,intent(IN)  :: unit               ! output unit. Set to zero to output to screen
  real(dp),intent(IN) :: a(dim,dim)   ! matrix to write
  integer,intent(IN)  :: ar,ac              ! number of rows and columns to write

! Local variables

  integer row,i
 
! START

  do row=1,ar
    if (unit.NE.0) then
      write(unit,8000)(a(row,i),i=1,ac)
    else
      write(*,8000)(a(row,i),i=1,ac) 
    end if
  end do
  
8000 format (1000ES16.8)

! END
 
  RETURN
  
  END SUBROUTINE dwrite_matrix
!
!_____________________________________________________________________
!
! NAME
!    dread_matrix
!
! DESCRIPTION
!    read real(dp) matrix from file
!
! SEE ALSO
!   
!
! HISTORY
!
!     started 2/12/15 CJS
!
! COMMENTS
!   
!
  SUBROUTINE dread_matrix(a,ar,ac,dim,unit)

! Modules used

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  integer,intent(IN)  :: dim             ! matrix dimension
  integer,intent(IN)  :: unit               ! input unit
  real(dp),intent(OUT) :: a(dim,dim)   ! matrix to write
  integer,intent(IN)  :: ar,ac              ! number of rows and columns to read

! Local variables

  integer row,i
 
! START

  do row=1,ar
    if (unit.NE.0) then
      read(unit,*)(a(row,i),i=1,ac)
    else
      read(*,*)(a(row,i),i=1,ac) 
    end if
  end do

! END
 
  RETURN
  
  END SUBROUTINE dread_matrix
!
! NAME
!    dread_matrix
!
! DESCRIPTION
!
! Invert the real matrix A using Gauss Jordan method with pivoting and return the result in AI
! ierr=0 on the successful calculation of the inverse
! If a singular matrix is found then:
! if ierr.EQ.0 on input then the program stops
! if ierr.NE.0 on input then the program returns with ierr=1
!
! HISTORY
!
!     started 2/12/15 CJS
!
! COMMENTS
! 
  SUBROUTINE dinvert_Gauss_Jordan(A,n,AI,dim,ierr) 

USE type_specifications
USE general_module

IMPLICIT NONE

! variables passed to subroutine
       
  integer,intent(IN)    :: dim            ! matrix dimension
  integer,intent(IN)    :: n                 ! size iof matrix to invert
  real(dp),intent(IN)   :: A(dim,dim) ! matrix to invert
  real(dp),intent(OUT)  :: AI(dim,dim) ! matrix inverse
  integer,intent(INOUT) :: ierr

! local variables
                 
  integer    :: row,col,reduce_col,i
  
  real(dp)    :: max_element
  real(dp)    :: pivot_element
  integer     :: max_row
  
  integer    :: pivot_row
  
  integer    :: pivot_row_save(dim)
  
  real(dp)    :: row_multiplier
  real(dp)    :: swap

! START
  
! copy A to AI 
  AI(1:n,1:n)= A(1:n,1:n)

  pivot_row_save(1:dim)=0
  
! loop over columns of the matrix and reduce each column in turn to identity matrix column     
  do reduce_col=1,n
  
! find the largest element in this column and use as the pivot element
    max_element=0d0
    max_row=0
    do row=reduce_col,n
      if (abs(AI(row,reduce_col)).GT.max_element) then
        max_element=abs(AI(row,reduce_col))
    max_row=row
      end if
    end do  
    
    if (max_row.eq.0) then
! all elements are zero so singular matrix
      if (verbose) write(*,*)'Singular matrix found in dinvert_Gauss_Jordan'
      if (ierr.NE.0) then
        run_status='ERROR: Singular matrix in dinvert_Gauss_Jordan'
        CALL write_program_status()
        STOP 1
      else
        ierr=1
        RETURN
      end if
    end if
    
    pivot_row=max_row
    pivot_row_save(reduce_col)=pivot_row
    
! swap pivot row with the row reduce_col

    if (pivot_row.ne.reduce_col) then
      do col=1,n
        swap=AI(reduce_col,col)
    AI(reduce_col,col)=AI(pivot_row,col)
    AI(pivot_row,col)=swap
      end do 
    end if
    
    pivot_row=reduce_col   
    pivot_element=AI(reduce_col,reduce_col)
    
! operate on pivot row    
    do col=1,n
      if (col.ne.reduce_col) then
        AI(pivot_row,col) = AI(pivot_row,col)/pivot_element
      else    
        AI(pivot_row,col) = 1d0/pivot_element
      end if
    end do

! operate on rows other than the pivot row   
    do row=1,n
    
      if (row.ne.pivot_row) then
      
        row_multiplier=AI(row,reduce_col)
    
        do col=1,n
          if (col.ne.reduce_col) then
            AI(row,col) = AI(row,col)- AI(pivot_row,col)*row_multiplier
          else    
            AI(row,reduce_col) =-AI(pivot_row,reduce_col)*row_multiplier
          end if
        end do
    
      end if ! not pivot row
    
    end do ! next row
    
  end do ! next column of the matrix to reduce
  
  do reduce_col=n,1,-1
  
    if (reduce_col.ne.pivot_row_save(reduce_col)) then
! rows were swapped so must swap the corresponding columns

      do row=1,n
        swap=AI(row,pivot_row_save(reduce_col))
        AI(row,pivot_row_save(reduce_col))=AI(row,reduce_col)
    AI(row,reduce_col)=swap
      end do
      
    end if
    
  end do
  
  ierr=0

  RETURN
  
  END SUBROUTINE dinvert_Gauss_Jordan
44c11f06   Chris Smartt   Include software ...