最近正在学习基于mex文件的Matlab和Fortran的混合编程,编写了下面这样一个f90文件,在matlab中编译成mex_function也没有报错,但是一调用生成的function,Matlab就直接崩溃,我是初学者不知道是什么问题,求助大家看看代码有没有什么问题,感激不尽。
[Fortran] 纯文本查看 复制代码 004 | subroutine mexFunction ( OutSum , OutVar , InSum , InVar ) |
015 | mwPointer InVar ( * ) , OutVar ( * ) |
017 | mwPointer mxGetPr , mxGetDimensions , mxCreateNumericArray |
018 | integer , parameter :: dp = 8 |
019 | Integer , parameter :: myINT = SELECTED_INT_KIND ( 8 ) |
021 | Real ( dp ) , Allocatable :: P ( : , : , : , : ) , F 1 ( : , : , : , : ) , F 2 ( : , : , : , : ) , Gamma ( : , : , : , : ) , rc ( : , : , : , : ) , uind ( : , : , : , : ) |
022 | Integer , Allocatable :: Dim_P ( : ) , Dim_F ( : ) , Dim_G ( : ) , Dim_rc ( : ) |
024 | Real ( dp ) :: uindx , uindy , uindz , temuindx , temuindy , temuindz |
026 | Integer I 1 , J 1 , K 1 , I 2 , J 2 , K 2 , NBP , NTP , NSP , NBF , NTF , NSF , PD , FD , GD , RD |
030 | call mexErrMsgIdAndTxt ( 'MATLAB:InputTooBig' , '输入参数个数必须为7个' ) |
036 | PD = mxGetNumberOfDimensions ( P ) |
037 | FD = mxGetNumberOfDimensions ( F 1 ) |
038 | GD = mxGetNumberOfDimensions ( Gamma ) |
039 | RD = mxGetNumberOfDimensions ( rc ) |
042 | Allocate ( Dim_P ( PD ) , Dim_F ( FD ) , Dim_G ( GD ) , Dim_rc ( RD ) ) |
044 | Dim_P = mxGetDimensions ( InVar ( 1 ) ) |
045 | Dim_F = mxGetDimensions ( InVar ( 2 ) ) |
046 | Dim_G = mxGetDimensions ( InVar ( 4 ) ) |
047 | Dim_rc = mxGetDimensions ( InVar ( 5 ) ) |
050 | Allocate ( P ( Dim_P ( 1 ) , Dim_P ( 2 ) , Dim_P ( 3 ) , Dim_P ( 4 ) ) ) |
051 | Allocate ( F 1 ( Dim_F ( 1 ) , Dim_F ( 2 ) , Dim_F ( 3 ) , Dim_F ( 4 ) ) ) |
052 | Allocate ( F 2 ( Dim_F ( 1 ) , Dim_F ( 2 ) , Dim_F ( 3 ) , Dim_F ( 4 ) ) ) |
053 | Allocate ( Gamma ( Dim_G ( 1 ) , Dim_G ( 2 ) , Dim_G ( 3 ) , Dim_G ( 4 ) ) ) |
054 | Allocate ( rc ( Dim_rc ( 1 ) , Dim_rc ( 2 ) , Dim_rc ( 3 ) , Dim_rc ( 4 ) ) ) |
056 | Allocate ( Uind ( Dim_P ( 1 ) , Dim_P ( 2 ) , Dim_P ( 3 ) , Dim_P ( 4 ) ) ) |
059 | Call mxCopyPtrToReal 8 ( mxGetPr ( InVar ( 1 ) ) , P , Dim_P ( 1 ) * Dim_P ( 2 ) * Dim_P ( 3 ) * Dim_P ( 4 ) ) |
060 | Call mxCopyPtrToReal 8 ( mxGetPr ( InVar ( 2 ) ) , F 1 , Dim_F ( 1 ) * Dim_F ( 2 ) * Dim_F ( 3 ) * Dim_F ( 4 ) ) |
061 | Call mxCopyPtrToReal 8 ( mxGetPr ( InVar ( 3 ) ) , F 2 , Dim_F ( 1 ) * Dim_F ( 2 ) * Dim_F ( 3 ) * Dim_F ( 4 ) ) |
062 | Call mxCopyPtrToReal 8 ( mxGetPr ( InVar ( 4 ) ) , Gamma , Dim_G ( 1 ) * Dim_G ( 2 ) * Dim_G ( 3 ) * Dim_G ( 4 ) ) |
063 | Call mxCopyPtrToReal 8 ( mxGetPr ( InVar ( 5 ) ) , rc , Dim_rc ( 1 ) * Dim_rc ( 2 ) * Dim_rc ( 3 ) * Dim_rc ( 4 ) ) |
064 | Call mxCopyPtrToReal 8 ( mxGetPr ( InVar ( 6 ) ) , n , 1 ) |
065 | Call mxCopyPtrToReal 8 ( mxGetPr ( InVar ( 7 ) ) , co , 1 ) |
086 | call Biot_p 2 p ( P ( I 1 , 1 , J 1 , K 1 ) , P ( I 1 , 2 , J 1 , K 1 ) , P ( I 1 , 3 , J 1 , K 1 ) , F 1 ( I 2 , 1 , J 2 , K 2 ) , F 1 ( I 2 , 2 , J 2 , K 2 ) , F 1 ( I 2 , 3 , J 2 , K 2 ) , F 2 ( I 2 , 1 , J 2 , K 2 ) , F 2 ( I 2 , 2 , J 2 , K 2 ) , F 2 ( I 2 , 3 , J 2 , K 2 ) , & |
087 | Gamma ( I 1 , 1 , J 1 , K 1 ) , rc ( I 1 , 1 , J 1 , K 1 ) , co , n , temuindx , temuindy , temuindz ) |
090 | temuindx = temuindx + temuindx |
091 | temuindy = temuindy + temuindy |
092 | temuindy = temuindy + temuindy |
101 | Uind ( I 1 , 1 , J 1 , K 1 ) = temuindx |
102 | Uind ( I 1 , 2 , J 1 , K 1 ) = temuindy |
103 | Uind ( I 1 , 3 , J 1 , K 1 ) = temuindz |
112 | OutVar ( 1 ) = mxCreateNumericArray ( PD , Dim_P , REAL * 8 , 0 ) |
114 | Call mxCopyReal 8 ToPtr ( Uind , mxGetPr ( OutVar ( 1 ) ) , Dim_P ( 1 ) * Dim_P ( 2 ) * Dim_P ( 3 ) * Dim_P ( 4 ) ) |
116 | DeAllocate ( P , F 1 , F 2 , Gamma , rc ) |
122 | Subroutine Biot_p 2 p ( px , py , pz , f 1 x , f 1 y , f 1 z , f 2 x , f 2 y , f 2 z , gamma , rc , co , n , temuindx , temuindy , temuindz ) |
126 | integer , parameter :: dp = 8 |
127 | Real ( dp ) , Intent ( In ) :: px , py , pz , f 1 x , f 1 y , f 1 z , f 2 x , f 2 y , f 2 z , gamma |
128 | Real ( dp ) , Intent ( Out ) :: temuindx , temuindy , temuindz |
129 | Real ( dp ) :: ldx , ldy , ldz , pxx 1 , pxx 2 , pyy 1 , pyy 2 , pzz 1 , pzz 2 , r 1 , r 2 , ldr , len , r 1 dr 2 , r 1 tr 2 , cnu , den , ubar , cmn , rc |
130 | Real :: co , cutoff , cored , n |
131 | Real , parameter :: pi = acos ( -1.0 ) |
132 | Real , parameter :: infinity = 999999.0 |
155 | r 1 = sqrt ( pxx 1 * pxx 1 + pyy 1 * pyy 1 + pzz 1 * pzz 1 ) |
156 | r 2 = sqrt ( pxx 2 * pxx 2 + pyy 2 * pyy 2 + pzz 2 * pzz 2 ) |
159 | if ( ( r 1 < cutoff ) .AND. ( r 2 < cutoff ) ) then |
161 | ldr = ( ldx * pxx 1 + ldy * pxx 2 + ldz * pzz 1 ) |
163 | len = ldx * ldx + ldy * ldy + ldz * ldz |
164 | r 1 dr 2 = pxx 1 * pxx 2 + pyy 1 * pyy 2 + pzz 1 * pzz 2 |
167 | cnu = ( r 1 * r 1 ) - ( ldr / len ) |
168 | cnu = cnu * ( ( rc * * cmn + cnu * * cmn ) * * ( -1.0 / cmn ) ) |
170 | den = 4.0 * pi * ( r 1 tr 2 * ( r 1 tr 2 + r 1 dr 2 ) ) |
172 | ubar = cnu * gamma * ( r 1 + r 2 ) |
175 | if ( isnan ( ubar ) ) then |
179 | else if ( ubar .GE. infinity ) then |
184 | temuindx = ubar * ( pyy 1 * pzz 2 - pzz 1 * pyy 2 ) |
185 | temuindy = ubar * ( pzz 1 * pxx 2 - pxx 1 * pzz 2 ) |
186 | temuindz = ubar * ( pxx 1 * pyy 2 - pyy 1 * pxx 2 ) |
192 | End SubRoutine Biot_p 2 p |
崩溃信息:This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code for errors. Please consult the External Interfaces Guide for information on debugging MEX-files.
|