|
在运行时,显示Solver_Crank-Nicolson (coef1,coef2,f,u)这个子程序每一行都显示有问题,我把代码都附在下面了,麻烦大家帮忙运行看下,非常感谢!
用到的文本信息nml-pata-DBC.txt
&nml_para
nx = 100,
nt = 100,
xmin = 0.d0,
xmax = 1.d0,
tmin = 0.d0,
tmax = 1.d0,
k_max =5 ,
/
[Fortran] 纯文本查看 复制代码 001 | module mod_fdm_tool_C 3 |
004 | real ( real 64 ) , parameter :: pi = 4 .d 0 * datan ( 1 .d 0 ) |
007 | subroutine Grid_ 2 D_Type ( nx , nt , xmin , xmax , tmin , tmax , hx , ht , x , t ) |
009 | integer ( int 32 ) , intent ( in ) :: nx , nt |
010 | real ( real 64 ) , intent ( in ) :: xmin , xmax , tmin , tmax |
011 | real ( real 64 ) , intent ( out ) :: hx , ht , x ( 0 : nx ) , t ( 0 : nt ) |
012 | integer ( int 32 ) :: i , j |
014 | hx = ( xmax - xmin ) / dble ( nx ) |
015 | ht = ( tmax - tmin ) / dble ( nt ) |
017 | do concurrent ( i = 0 : nx ) |
018 | x ( i ) = xmin + dble ( i ) * hx |
022 | do concurrent ( j = 0 : nt ) |
023 | t ( j ) = tmin + dble ( j ) * ht |
028 | end subroutine Grid_ 2 D_Type |
033 | pure elemental function f_rhs ( x , t ) result ( res ) |
035 | real ( real 64 ) , intent ( in ) :: x , t |
044 | pure elemental function Solu_Exact ( x , t ) result ( res ) |
046 | real ( real 64 ) , intent ( in ) :: x , t |
052 | end function Solu_Exact |
055 | subroutine Apply_DBC ( x , t , u ) |
057 | real ( real 64 ) , intent ( in ) :: x ( : ) , t ( : ) |
058 | real ( real 64 ) , intent ( out ) :: u ( : , : ) |
065 | u ( nt , : ) = dexp ( 1.0 + t ) |
070 | end subroutine Apply_DBC |
073 | subroutine Gene_Alg_Sys_DBC ( ht , hx , nx , r , coef 1 , coef 2 ) |
076 | integer ( int 32 ) , intent ( in ) :: nx |
077 | real ( real 64 ) , intent ( in ) :: hx , ht |
078 | real ( real 64 ) , intent ( out ) :: coef 1 ( nx -1 , 3 ) , coef 2 ( nx -1 , 3 ) , r |
103 | end subroutine Gene_Alg_Sys_DBC |
107 | subroutine Solver_Crank - Nicolson ( coef 1 , coef 2 , f , u ) |
110 | integer ( int 32 ) :: nx , nt , i , j |
111 | real ( real 64 ) , intent ( in ) :: coef 1 ( : , : ) , coef 2 ( : , : ) |
113 | real ( real 64 ) :: u ( : , : ) |
115 | real ( real 64 ) , allocatable :: g ( : ) , w ( : ) |
119 | f ( 1 ) = coef 2 ( 1 , 2 ) * u ( 1 , j ) |
120 | f ( nx -1 ) = coef 2 ( nx -1 , 1 ) * u ( nx -2 , j ) + coef 2 ( nx -1 , 2 ) * u ( nx -1 , j ) |
121 | f ( 2 : nx -1 ) = coef 2 ( 2 : nx -2 , 1 ) * u ( 1 : nx -3 , j ) + coef 2 ( 2 : nx -2 , 2 ) * u( 2 : nx -2 , j ) + coef 2 ( 2 , nx -2 , 3 ) * u ( 3 : nx -1 , j ) |
123 | allocate ( g ( nx -1 ) , w ( nx -1 ) ) |
126 | w ( 1 ) = coef 1 ( 1 , 3 ) / coef 1 ( 1 , 2 ) |
129 | w ( i ) = coef 1 ( i , 2 ) - coef 1 ( i , 1 ) * w ( i -1 ) |
130 | g ( i ) = ( f ( i ) - coef 1 ( i , 1 ) * g ( i -1 ) ) / w ( i ) |
136 | u ( i , j +1 ) = g ( i ) - w ( i ) * u ( i +1 , j +1 ) |
143 | end subroutine Solver_Crank - Nicolson |
145 | end module mod_fdm_tool_C 3 |
161 | program Crank - Nicolson_ 2 D_DBC |
168 | integer ( int 32 ) :: nx , nt , m , k , k_max , fileunit , i , j |
169 | real ( real 64 ) :: hx , xmin , xmax |
170 | real ( real 64 ) :: ht , tmin , tmax |
172 | real ( real 64 ) :: iter_tol , iter_fac , error = 0 .d 0 |
173 | real ( real 64 ) , allocatable :: x ( : ) , t ( : ) , f ( : , : ) , u ( : , : ) , v ( : , : ) |
174 | character ( len = : ) , allocatable :: dirname , filename , output_fmt |
175 | logical :: istat , debug = .false. |
177 | namelist / nml_para / nx , nt , xmin , xmax , tmin , tmax , k_max |
179 | print '(1X,A)' , 'Example_311' |
181 | filename = 'nml_para_DBC.txt' |
182 | open ( newunit = fileunit , file = filename ) |
183 | read ( unit = fileunit , nml = nml_para ) |
187 | inquire ( file = dirname , exist = istat ) |
189 | call Make_Directory ( dirname ) |
193 | filename = dirname / / '/results.txt' |
194 | open ( newunit = fileunit , file = trim ( filename ) ) |
195 | write ( fileunit , '(a)' ) 'Inf error at x = 0.5, 0.5, 0.5, 0.5, 0.5,' |
196 | write ( fileunit , '(a)' ) ' and t = 0.1, 0.2, 0.3, 0.4, 0.5 are' |
202 | print '(1X, A, I3, A)' , 'The spatial step size of 1/' , nx , ',the time step size of 1/' , nt , ' is running...' |
204 | if ( allocated ( u ) ) deallocate ( x , t , f , u , v ) |
205 | allocate ( x ( 0 : nx ) , t ( 0 : nt ) , f ( 0 : nx , 0 : nt ) , u ( 0 : nx , 0 : nt ) , v ( 0 : nx , 0 : nt ) ) |
208 | call Grid_ 2 D_Type ( nx , nt , xmin , xmax , tmin , tmax , hx , ht , x , t ) |
210 | call Apply_DBC ( x , t , u ) |
212 | call Gene_Alg_Sys_DBC ( ht , hx , nx , coef 1 , coef 2 ) |
214 | do concurrent ( i = 0 : nx ) |
216 | v ( i , : ) = Solu_Exact ( x ( i ) , t ) |
220 | call Solver_Crank - Nicolson ( coef 1 , coef 2 , f , u ) |
222 | output_fmt = '(A, I3, 2X, 6(ES9.3, 2X), F5.2)' |
223 | write ( fileunit , output_fmt ) '1/' , nx , '1/' , nt , & |
225 | abs ( u ( nx / 2 * 1 , nt / 10 * 1 ) - v ( nx / 2 * 1 , nt / 10 * 1 ) , & |
227 | abs ( u ( nx / 2 * 1 , nt / 10 * 2 ) - v ( u ( nx / 2 * 1 , nt / 10 * 2 ) , & |
228 | abs ( u ( nx / 2 * 1 , nt / 10 * 3 ) - v ( u ( nx / 2 * 1 , nt / 10 * 3 ) , & |
229 | abs ( u ( nx / 2 * 1 , nt / 10 * 4 ) - v ( nx / 2 * 1 , nt / 10 * 4 ) , & |
230 | abs ( u ( nx / 2 * 1 , nt / 10 * 5 ) - v ( nx / 2 * 1 , nt / 10 * 5 ) , & |
231 | maxval ( abs ( u - v ) ) , error / maxval ( abs ( u - v ) ) |
233 | error = maxval ( abs ( u - v ) ) |
238 | print * , 'The program is done.' |
241 | end program Crank - Nicolson_ 2 D_DBC |
|
|