Fortran Coder

查看: 2662|回复: 4
打印 上一主题 下一主题

[子程序] 子程序有问题请大家帮忙看下

[复制链接]

4

帖子

2

主题

0

精华

新人

F 币
22 元
贡献
10 点
跳转到指定楼层
楼主
发表于 2022-11-29 16:02:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在运行时,显示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] 纯文本查看 复制代码
module mod_fdm_tool_C3
    use iso_fortran_env
    implicit none
    real(real64), parameter :: pi = 4.d0*datan(1.d0)
contains

subroutine Grid_2D_Type(nx, nt, xmin, xmax, tmin, tmax, hx, ht, x, t)
    implicit none
    integer(int32), intent(in)  :: nx, nt
    real(real64),   intent(in)  :: xmin, xmax, tmin, tmax
    real(real64),   intent(out) :: hx, ht, x(0:nx), t(0:nt)
    integer(int32) :: i, j

    hx = (xmax - xmin) / dble(nx)
    ht = (tmax - tmin) / dble(nt)

    do concurrent (i = 0 : nx)
        x(i) = xmin + dble(i) * hx
    end do
    x(nx) = xmax

    do concurrent (j = 0 : nt)
        t(j) = tmin + dble(j) * ht
    end do
    t(nt) = tmax

    return
end subroutine Grid_2D_Type



!===============================================================================
pure elemental function f_rhs(x,t) result(res)
    implicit none
    real(real64), intent(in) :: x, t
    real(real64) :: res

    res = 0

    return
end function f_rhs

!===============================================================================
pure elemental function Solu_Exact(x,t) result(res)
    implicit none
    real(real64), intent(in) :: x, t
    real(real64) :: res

    res = dexp(x+t)

    return
end function Solu_Exact


subroutine Apply_DBC(x, t,u)
    implicit none
    real(real64), intent(in)  :: x(:), t(:)
    real(real64), intent(out) :: u(:,:)
    integer(int32) :: nt

    nt = size(t)

    u(:, 1) = dexp(x)
    u(1,:) = dexp(t)
    u(nt,:) = dexp(1.0+t)



    return
end subroutine Apply_DBC

!======================================================
subroutine Gene_Alg_Sys_DBC(ht,hx,nx,r,coef1,coef2)
    ! Algebraic system, i.e., Au = f
    implicit none
    integer(int32) ,intent(in):: nx
    real(real64), intent(in)  :: hx,ht
    real(real64), intent(out) :: coef1(nx-1,3),coef2(nx-1,3),r

    r = ht/hx/hx


    ! all the following variable are arraies, not scalars
    coef1(:,1) = -r/2.d0
    coef1(:,2) = 1+r
    coef1(:,3) = -r/2.d0

    coef2(:,1) = r/2.d0
    coef2(:,2) = 1-r
    coef2(:,3) = r/2.d0



    coef1(1,1) = 0.d0
    coef1(nx-1,3) = 0.d0

    coef2(1,1) = 0.d0
    coef2(nx-1,3) = 0.d0



    return
end subroutine Gene_Alg_Sys_DBC


!这个子程序有问题======================================================
subroutine Solver_Crank-Nicolson (coef1,coef2,f,u)

    implicit none
    integer(int32) :: nx,nt,i,j
    real(real64), intent(in)  :: coef1(:,:),coef2(:,:)
    real(real64) :: f(:)
    real(real64) :: u(:,:)
    real(real64) ::r
    real(real64),allocatable::g(:),w(:)


    do j = 1, nt-1
      f(1)=coef2(1,2)*u(1,j)
      f(nx-1)=coef2(nx-1,1)*u(nx-2,j)+coef2(nx-1,2)*u(nx-1,j)
      f(2:nx-1)=coef2(2:nx-2,1)*u(1:nx-3,j)+coef2(2:nx-2,2)*u(2:nx-2,j)+coef2(2,nx-2,3)*u(3:nx-1,j)

      allocate(g(nx-1),w(nx-1))

      g(1)=f(1)/coef1(1,2)
      w(1)=coef1(1,3)/coef1(1,2)

      do i=2:nx-1
        w(i)=coef1(i,2)-coef1(i,1)*w(i-1)
        g(i)=(f(i)-coef1(i,1)*g(i-1))/w(i)
        w(i)=coef1(i,3)/w(i)
      end do
      u(nx-1,:)=g(nx-1)

      do i=nx-2,1,-1
        u(i,j+1)=g(i)-w(i)*u(i+1,j+1)
      end do
    end do

      deallocate(g,w)

    return
end subroutine Solver_Crank-Nicolson

end module mod_fdm_tool_C3

!=============================================================
! cLi, 2022-11-24
!
! Solving example 3.1.1 in p.67:
!
!   -u'(t) - u''(x) = 0,  x \in (0, 1),t \in(0,1]
!   u(x,0) =  exp(x),   x \in (0, 1)
!   u(0,t) = exp(t) ,u(1,t) = exp(1+t) ,t \in(0,1]
!
!
!   
!   
!   u(x,t) = exp(x+t)
!=============================================================
program Crank-Nicolson_2D_DBC

    use iso_fortran_env
    use mod_fdm_tool_C3

implicit none

integer(int32) :: nx, nt, m, k, k_max, fileunit,  i,j
real(real64)   :: hx, xmin, xmax
real(real64)   :: ht, tmin, tmax

real(real64)   :: iter_tol, iter_fac, error = 0.d0
real(real64), allocatable :: x(:), t(:), f(:,:), u(:,:), v(:,:)
character(len=:), allocatable :: dirname, filename, output_fmt
logical :: istat, debug = .false.

namelist /nml_para/ nx, nt, xmin, xmax, tmin, tmax, k_max

print '(1X,A)', 'Example_311'

filename = 'nml_para_DBC.txt'
open(newunit=fileunit, file=filename)
read(unit=fileunit, nml=nml_para)
close(fileunit)

dirname = 'Out'
inquire(file=dirname, exist=istat)
if (.not. istat) then
    call Make_Directory(dirname)
end if


filename = dirname//'/results.txt'
open(newunit=fileunit, file=trim(filename))
write(fileunit, '(a)') 'Inf error at x = 0.5, 0.5,  0.5, 0.5,  0.5,'
write(fileunit, '(a)') '         and t = 0.1, 0.2,  0.3, 0.4,  0.5 are'

do k=1,k_max
   m  = 2**(k-1)
    nx = 10*m
    nt = 10*m
    print '(1X, A, I3, A)', 'The spatial step size of 1/', nx, ',the time step size of 1/',nt,' is running...'

    if (allocated(u)) deallocate(x, t, f, u, v)
    allocate(x(0:nx), t(0:nt), f(0:nx,0:nt), u(0:nx,0:nt), v(0:nx,0:nt))

     ! start the FDM solving procedure
    call Grid_2D_Type (nx, nt, xmin, xmax, tmin, tmax, hx, ht, x, t)

    call Apply_DBC(x, t, u)

    call Gene_Alg_Sys_DBC(ht,hx,nx,coef1,coef2)

    do concurrent (i = 0 : nx)

        v(i,:) = Solu_Exact(x(i),t)
    end do


    call Solver_Crank-Nicolson (coef1,coef2,f,u)

     output_fmt = '(A, I3, 2X, 6(ES9.3, 2X), F5.2)'
    write(fileunit, output_fmt) '1/', nx,'1/', nt,       &

            abs(u(nx/2*1,nt/10*1)-v(nx/2*1,nt/10*1), &

            abs(u(nx/2*1,nt/10*2)-v(u(nx/2*1,nt/10*2), &
            abs(u(nx/2*1,nt/10*3)-v(u(nx/2*1,nt/10*3), &
            abs(u(nx/2*1,nt/10*4)-v(nx/2*1,nt/10*4), &
            abs(u(nx/2*1,nt/10*5)-v(nx/2*1,nt/10*5), &
            maxval(abs(u-v)), error/maxval(abs(u-v))

    error = maxval(abs(u-v))
end do

close(fileunit)
print *, ''
print *, 'The program is done.'

return
end program Crank-Nicolson_2D_DBC

分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

954

帖子

0

主题

0

精华

大师

F 币
184 元
贡献
75 点

规矩勋章元老勋章新人勋章水王勋章热心勋章

QQ
沙发
发表于 2022-11-29 16:16:48 | 只看该作者
Solver_Crank-Nicolson
改为
Solver_Crank_Nicolson

Crank-Nicolson_2D_DBC
改为
Crank_Nicolson_2D_DBC


Fortran 中 - 不能做为变量名的组成部分。

54

帖子

0

主题

0

精华

实习版主

F 币
653 元
贡献
214 点

元老勋章新人勋章

QQ
板凳
发表于 2022-11-30 12:34:21 | 只看该作者
这代码里面有很多笔误。让人感觉是从书上扫描的,或者照着打但是打错了。

4

帖子

2

主题

0

精华

新人

F 币
22 元
贡献
10 点
地板
 楼主| 发表于 2022-11-30 20:33:17 | 只看该作者
vvt 发表于 2022-11-29 16:16
Solver_Crank-Nicolson
改为
Solver_Crank_Nicolson

非常感谢 问题解决了

4

帖子

2

主题

0

精华

新人

F 币
22 元
贡献
10 点
5#
 楼主| 发表于 2022-11-30 20:34:01 | 只看该作者
布衣龙共 发表于 2022-11-30 12:34
这代码里面有很多笔误。让人感觉是从书上扫描的,或者照着打但是打错了。 ...

刚开始学习  检查了下确实打错了很多
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-4-19 17:26

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表