EIRAM
atomic and molecular data in form of polynomial fits
eiram_matlab.F
Go to the documentation of this file.
1 
6 
7 ! Copyright (c) 2016 Forschungszentrum Juelich GmbH
8 ! Markus Brenneis, Vladislav Kotov
9 !
10 ! This file is part of EIRAM.
11 !
12 ! EIRAM is free software: you can redistribute it and/or modify
13 ! it under the terms of the GNU General Public License as published by
14 ! the Free Software Foundation, either version 3 of the License, or
15 ! (at your option) any later version.
16 !
17 ! EIRAM is distributed in the hope that it will be useful,
18 ! but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ! GNU General Public License for more details.
21 !
22 ! You should have received a copy of the GNU General Public License
23 ! along with EIRAM. If not, see <http://www.gnu.org/licenses/>.
24 !
25 
26 #include "fintrf.h"
27 
35 
36  subroutine mexfunction(nlhs, plhs, nrhs, prhs)
37  use eiram
38  implicit none
40  integer :: nlhs
42  integer :: nrhs
44  mwpointer, dimension(*) :: plhs
53  mwpointer, dimension(*) :: prhs
54 
55  mwpointer :: mxgetpr, mxcreatedoublematrix, mxgetstring
56  integer :: mxIsNumeric, mxIsChar
57  mwsize :: mxgetm, mxgetn
58 
59  mwsize :: narg1s, narg2s
60  integer :: err,err0
61  mwpointer :: arg1s_pr, arg2s_pr, ys_pr
62  real(kind=8),allocatable :: Arg1s(:), Arg2s(:)
63  real(kind=8),allocatable :: Ys(:,:,:)
64  integer, parameter :: MAX_STR_LEN = 1024
65  character(MAX_STR_LEN) :: filePath,reactionIndexStr
66  character(EIRAM_FILE_NAME_LENGTH) :: fileName
67  character(EIRAM_TYPE_LENGTH) :: reactionType
68  character(EIRAM_INDEX_LENGTH),allocatable :: reactionIndex(:)
69 
70  integer :: numberOfArgs,n_reactions,reactions_length,i,j,ind
71  logical :: lPath,fileExists
72  intrinsic trim,min
73 
74  if (nrhs < 4)
75  f call mexerrmsgtxt('More than three inputs required.')
76 
77 
78  if( mxisnumeric(prhs(1)) /= 1 )
79  f call mexerrmsgtxt('Input # 1 must be a numeric array.')
80  if( mxisnumeric(prhs(2)) == 1 ) then
81  numberofargs = 2
82  else
83  numberofargs = 1
84  end if
85 
86  if( numberofargs == 1 ) then
87  if (nrhs > 5)
88  f call mexerrmsgtxt('Four or five inputs required.')
89  if(mxischar(prhs(2)) /= 1)
90  f call mexerrmsgtxt('Input # 2 must be a string.')
91  if(mxischar(prhs(3)) /= 1)
92  f call mexerrmsgtxt('Input # 3 must be a string.')
93  if(mxischar(prhs(4)) /= 1)
94  f call mexerrmsgtxt('Input # 4 must be a string.')
95  if (nrhs > 4) then
96  if(mxischar(prhs(5)) /= 1)
97  f call mexerrmsgtxt('Input # 5 must be a string.')
98  lpath=.true.
99  else
100  lpath=.false.
101  end if
102  end if
103 
104  if( numberofargs == 2 ) then
105  if (nrhs > 6 .or. nrhs < 5)
106  f call mexerrmsgtxt('Five or six inputs required.')
107  if(mxischar(prhs(3)) /= 1)
108  f call mexerrmsgtxt('Input # 3 must be a string.')
109  if(mxischar(prhs(4)) /= 1)
110  f call mexerrmsgtxt('Input # 4 must be a string.')
111  if(mxischar(prhs(5)) /= 1)
112  f call mexerrmsgtxt('Input # 5 must be a string.')
113  if (nrhs > 5) then
114  if(mxischar(prhs(6)) /= 1)
115  f call mexerrmsgtxt('Input # 6 must be a string.')
116  lpath=.true.
117  else
118  lpath=.false.
119  end if
120  end if
121 
122  narg1s = mxgetn(prhs(1))
123  if(mxgetm(prhs(1)) /= 1) then
124  call mexerrmsgtxt('Input #1 must be a row vector.')
125  endif
126 
127  arg1s_pr = mxgetpr(prhs(1))
128  if(numberofargs == 2) then
129  arg2s_pr = mxgetpr(prhs(2))
130  narg2s = mxgetn(prhs(2))
131  if(mxgetm(prhs(2)) /= 1) then
132  call mexerrmsgtxt('Input #2 must be a row vector.')
133  endif
134  else
135  narg2s = 0
136  endif
137 
138  n_reactions = mxgetm(prhs(numberofargs+3))
139  reactions_length = mxgetn(prhs(numberofargs+3))
140 
141  allocate(reactionindex(n_reactions))
142 
143  err=mxgetstring(prhs(numberofargs+1),filename,
145  err=mxgetstring(prhs(numberofargs+2),
146  , reactiontype,eiram_type_length)
147  err=mxgetstring(prhs(numberofargs+3),
148  , reactionindexstr,max_str_len)
149  if(lpath) then
150  err=mxgetstring(prhs(numberofargs+4),filepath, max_str_len)
151  else
152  filepath=""
153  end if
154  !for reaction index divide long string into substrings
155  reactionindex = " "
156  do i = 1, n_reactions
157  do j = 1, min(reactions_length,eiram_index_length)
158  ind = (j-1)*n_reactions+i
159  reactionindex(i)(j:j) = reactionindexstr(ind:ind)
160  end do
161  end do
162 
163  !convert 1st (and 2nd - if required) argument into array of reals
164  allocate(arg1s(narg1s))
165  call mxcopyptrtoreal8(arg1s_pr, arg1s, narg1s)
166  if(narg2s >0) then
167  allocate(arg2s(narg2s))
168  call mxcopyptrtoreal8(arg2s_pr, arg2s, narg2s)
169  end if
170 
171  eiram_unit = 0
172 
173  call eiram_load(filepath, filename, err)
174  if(err /= 0) then
175  call eiram_deallocate(err0)
176  select case(err)
177  case(310)
178  call mexerrmsgtxt('Input file '//trim(filepath)
179  / //trim(filename)//' is not found.')
180  case(300)
181  call mexerrmsgtxt('Cannot read input file '
182  / //trim(filepath)//trim(filename))
183  case default
184  call mexerrmsgtxt("error during initialization of EIRAM")
185  end select
186  end if
187 
188  if (narg2s>0) then
189  allocate(ys(narg2s,narg1s,n_reactions))
190  else
191  allocate(ys(narg1s,1,n_reactions))
192  end if
193 
194  do i=1,n_reactions
195  if(narg2s > 0) then
196  call eiram_matlab_calc2(ys(:,:,i),arg1s,arg2s,narg1s,narg2s,
197  & filename,reactiontype,reactionindex(i),err)
198  else
199  call eiram_matlab_calc1(ys(:,1,i),arg1s,narg1s,
200  & filename,reactiontype,reactionindex(i),err)
201  end if ! if(nArg2s > 0) then
202  if(err /= 0) call eiram_deallocate(err0)
203  select case(err)
204  case(11)
205  call mexerrmsgtxt("error : negative argument")
206  case(12)
207  call mexerrmsgtxt("error : negative 1st argument")
208  case(13)
209  call mexerrmsgtxt("error : negative 2nd argument")
210  case(110)
211  call mexerrmsgtxt("error: requested reaction "
212  / //trim(filename)//" "//trim(reactiontype)//" "
213  / //trim(reactionindex(i))//" is not found")
214  case(120)
215  call mexerrmsgtxt("error: double fit is requested for reaction"
216  / //" described by a single fit")
217  case(130)
218  call mexerrmsgtxt("error: single fit is requested for reaction"
219  / //" described by a double fit")
220  case default
221  if(err/=0) call mexerrmsgtxt("error in EIRAM")
222  end select
223  end do !do i=1,n_combinations
224 
225  do i=1,n_reactions
226  if(narg2s > 0) then
227  plhs(i) = mxcreatedoublematrix(narg2s,narg1s,0)
228  else
229  plhs(i) = mxcreatedoublematrix(narg1s,1,0)
230  end if
231  ys_pr = mxgetpr(plhs(i))
232  call mxcopyreal8toptr(ys(:,:,i), ys_pr, max(1,narg2s)*narg1s)
233  end do
234 
235  call eiram_deallocate(err)
236 
237  deallocate(reactionindex)
238  deallocate(arg1s)
239  if(allocated(arg2s)) deallocate(arg2s)
240  deallocate(ys)
241 
242  end subroutine mexfunction
integer, public eiram_unit
Unit to which messages are written.
Definition: eiram.f90:112
integer, parameter, public eiram_index_length
Length of the string with reaction index.
Definition: eiram.f90:127
subroutine, public eiram_load(filePath, fileName, err)
Initialization of the module from input files (data sets)
Definition: eiram.f90:206
subroutine, public eiram_deallocate(err)
Deallocate dynamic arrays used by this module.
Definition: eiram.f90:585
integer, parameter, public eiram_file_name_length
Length of the string with a name of an input file.
Definition: eiram.f90:123
integer, parameter, public eiram_type_length
Length of the string with reactiontype.
Definition: eiram.f90:125
subroutine mexfunction(nlhs, plhs, nrhs, prhs)
Matlab interface for EIRAM.
Definition: eiram_matlab.F:37
subroutine eiram_matlab_calc2(Y, X1, X2, M, N, fileName, reactionType, reactionIndex, err)
Binding for eiram_calc2 which has to be called from Matlab/Octave interface.
Definition: eiram.f90:96
subroutine eiram_matlab_calc1(Y, X, N, fileName, reactionType, reactionIndex, err)
Binding for eiram_calc1 which has to be called from Matlab/Octave interface.