Eötvös Quantum Utilities  v4.9.146
Providing the Horsepowers in the Quantum Realm
export_complex_sparse.F
Go to the documentation of this file.
1 #include "fintrf.h"
2 C======================================================================
3 C Gateway routine to call the zggev.f function from LAPACK package.
4 C (See: http://www.netlib.org/lapack/explore-html/d9/d52/dggev_8f.html for details)
5 C Copyright (C) 2009-2015 Peter Rakyta, Ph.D.
6 C
7 C This program is free software: you can redistribute it and/or modify
8 C it under the terms of the GNU General Public License as published by
9 C the Free Software Foundation, either version 3 of the License, or
10 C (at your option) any later version.
11 C
12 C This program is distributed in the hope that it will be useful,
13 C but WITHOUT ANY WARRANTY; without even the implied warranty of
14 C MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 C GNU General Public License for more details.
16 C
17 C You should have received a copy of the GNU General Public License
18 C along with this program. If not, see http://www.gnu.org/licenses/.
19 C
20 C======================================================================
21 C Gateway routine
22  subroutine mexfunction(nlhs, plhs, nrhs, prhs)
23 
24 C Declarations
25  implicit none
26 
27 C mexFunction arguments:
28  mwpointer plhs(*), prhs(*)
29  integer nlhs, nrhs
30 
31 
32 C Function declarations:
33  mwsize mxgetnzmax
34  mwpointer mxgetjc
35  mwpointer mxgetir
36  mwpointer mxcreatestring
37  mwpointer mxgetstring, getstring_stat
38  integer mxIsChar
39 
40 C Arguments for copying data between fortran and MATLAB
41  integer*4 complexflag
42 
43  mwpointer mxgetpr, mxgetpi
44  mwpointer mxcreatedoublematrix
45  mwpointer mxgetm, mxgetn
46  integer*4 mxIsDouble
47  integer*4 mxIsComplex
48  integer*4 mexPrintf
49 
50  logical fileExist
51  character(len=100) :: filename
52  character(len=7) :: file_status
53 
54 C Array information:
55  mwsize nonzerosa !number of nonzeros elements in A
56  mwsize sizeam, sizean !size of the matrix A (rows, and cols)
57  mwpointer mrows, ncols !size of the input string containing the filename
58  integer*4 strlen ! the length of the input string
59  integer*4 sizeS !Size of the Schur complement to be calculated
60 
61 C Arguments for computational routine:
62 C fortran representation of the input matrix (CSR format)
63  integer*8, allocatable :: ia_int8(:), ja_int8(:)
64  integer*4, allocatable :: ia(:), ja(:)
65  complex*16, allocatable :: a(:)
66 
67 C Parameters for pardiso
68  integer i,j
69 
70 #ifdef DEBUG
71 C Debug parameters
72  integer*4 tmp
73  character*800 line
74 #endif
75 
76 
77 C-----------------------------------------------------------------------
78 C Check for proper number of arguments.
79  if(nrhs .ne. 2) then
80  call mexerrmsgidandtxt ('MATLAB:export_real_sparse:nInput',
81  + 'Two inputs required.')
82  elseif(nlhs .gt. 0) then
83  call mexerrmsgidandtxt ('MATLAB:export_real_sparse:nOutput',
84  + 'Too many output arguments.')
85  endif
86 
87 C Validate inputs
88 
89 C Check that the input is a double.
90  if ((mxisdouble(prhs(1)) .eq. 0)) then
91  call mexerrmsgidandtxt ('MATLAB:export_real_sparse:NonDouble',
92  + 'Input must be a double.')
93  endif
94 
95 C Check if input matrix must be complex
96  if ((mxiscomplex(prhs(1)) .ne. 1) ) then
97  call mexerrmsgidandtxt ('MATLAB:convec:NonComplex',
98  + 'Inputs must be complex.')
99  endif
100 
101 C The input filename must be a string.
102  if(mxischar(prhs(2)) .ne. 1) then
103  call mexerrmsgidandtxt ('MATLAB:export_real_sparse:NonString',
104  + 'Input filename must be a string.')
105  endif
106 
107 C Get the size of the input string.
108  mrows = mxgetm(prhs(2))
109  ncols = mxgetn(prhs(2))
110 
111 C Get the length of the input string and validate.
112  strlen = int(mrows*ncols, 4)
113  if (strlen .gt. len(filename)) then
114  call mexerrmsgidandtxt ('MATLAB:export_real_sparse:NonDouble',
115  + 'input filename is greater than max str size.')
116  endif
117 
118 C Get the input filename.
119  getstring_stat =
120  + mxgetstring(prhs(2), filename, int(len(filename), 8))
121 
122 #ifdef DEBUG
123  write(line,*) 'Input file name: ',filename, new_line('A')
124  tmp = mexprintf(line)
125 #endif
126 
127 
128 C Get the size of the input array. (MATLAB)
129  nonzerosa = mxgetnzmax(prhs(1))
130  sizeam = mxgetm(prhs(1))
131  sizean = mxgetn(prhs(1))
132 
133 
134 C Allocating fortran arrays
135  allocate(ia_int8(sizean+1))
136  allocate(ja_int8(nonzerosa))
137  allocate(ia(sizean+1))
138  allocate(ja(nonzerosa))
139  allocate(a(nonzerosa))
140 C
141 C Check the allocated arrays
142  if (.not. ( allocated(ia_int8) .and.
143  + allocated(ja_int8) .and.
144  + allocated(ia) .and.
145  + allocated(ja) .and.
146  + allocated(a) )) then
147  call mexerrmsgidandtxt ('MATLAB:dgetSchur:BadAllocation',
148  + 'The allocation of variables was unsuccesfull.')
149  endif
150 C Copy data into the fortran arrays
151 C MATLAB does CSC format and PARDISO CSR format for the sparse matrices! Need to transpose.
152  call mxcopyptrtointeger8(
153  + mxgetjc(prhs(1)), ia_int8, sizean+1 )
154  call mxcopyptrtointeger8(
155  + mxgetir(prhs(1)), ja_int8, nonzerosa )
156  call mxcopyptrtocomplex16( mxgetpr(prhs(1)), mxgetpi(prhs(1)),
157  + a, nonzerosa )
158 C Fortran starts the indexes from 1
159  ia = int(ia_int8, 4) + 1
160  ja = int(ja_int8, 4) + 1
161  deallocate(ia_int8)
162  deallocate(ja_int8)
163 
164 
165 
166 C Opening the file to export data
167  inquire(file=trim(filename),exist=fileexist)!check if it exist
168  if(.not.fileexist)then
169  file_status = 'NEW'
170  else
171  file_status = 'REPLACE'
172  endif
173  OPEN(unit=6, file=trim(filename),status=file_status
174  + ,form='UNFORMATTED')
175 
176  WRITE(6) int(sizean, 4)
177  WRITE(6) int(nonzerosa, 4)
178  WRITE(6) ia(:)
179  WRITE(6) ja(:)
180  WRITE(6) a(:)
181 
182 
183 C Closing the file
184  CLOSE(unit=6)
185 
186 C deallocate the fortran matrix A
187  deallocate(ia)
188  deallocate(ja)
189  deallocate(a)
190 
191  return
192 
193 
194  end
195 
196 
subroutine mexfunction(nlhs, plhs, nrhs, prhs)