Fortran Coder

查看: 1850|回复: 3
打印 上一主题 下一主题

[数值问题] 数组越界

[复制链接]

12

帖子

4

主题

0

精华

入门

F 币
52 元
贡献
26 点
跳转到指定楼层
楼主
发表于 2023-9-8 15:58:04 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
自己编了一个求激波管问题精确解的代码 但是运行不出来 求各位大佬帮我看看哪里有问题
给下修改意见

D(ZQZ{DW1`7B]Z9)2E%PFOV.png (14.8 KB, 下载次数: 197)

D(ZQZ{DW1`7B]Z9)2E%PFOV.png

main.f90

2.44 KB, 下载次数: 2

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

12

帖子

4

主题

0

精华

入门

F 币
52 元
贡献
26 点
沙发
 楼主| 发表于 2023-9-8 15:58:42 | 只看该作者
[Fortran] 纯文本查看 复制代码
001program shock_tube
002  implicit none
003   
004  ! 常数定义
005  real, parameter :: gamma = 1.4       ! 气体比热比
006  real, parameter :: L = 10.0           ! 激波管长度
007  real, parameter :: nx = 20         ! 空间网格数
008  real, parameter :: dx = L / nx       ! 空间步长
009  real, parameter :: t_final = 1.6     ! 求解的时间
010  real, parameter :: dt = 0.08       ! 时间步长 
011   
012  ! 变量定义
013  integer :: i, n, tsteps
014  real :: x, t
015  real, dimension(0:nx) :: rho, u, p
016  open(100,file='resulr.dat')
017  ! 初始条件
018  do i = 0, nx
019    x = i * dx
020    
021    if (x <= 0.5 * L) then
022      rho(i) =0.445d0
023          u(i) = 0.698d0
024      p(i) = 3.528d0
025    else
026      rho(i) = 0.5d0
027      u(i) = 0.0d0
028      p(i) = 0.571d0
029    end if
030  end do
031   
032  ! 求解时间步进
033  t = 0.0
034  tsteps = t_final / dt
035   
036  do n = 1, tsteps
037    ! Riemann问题求解
038    do i = 0, nx-1
039      x = (i + 0.5) * dx
040       
041      if (x <= 0.5 * L) then
042       rho(i) =0.445d0
043          u(i) = 0.698d0
044      p(i) = 3.528d0
045    else
046      rho(i) = 0.5d0
047      u(i) = 0.0d0
048      p(i) = 0.571d0
049      end if
050    end do
051    
052    ! 更新各个网格的值
053    do i = 1, nx-2
054      rho(i) = rho(i) - dt / dx * (flux(rho, u, p, i) - flux(rho, u, p, i-1))
055      u(i) = u(i) - dt / dx * (flux(rho*u, u*u + p, u*(u(i)+p(i)/(rho(i)*(gamma-1))), i) - &
056                               flux(rho*u, u*u + p, u*(u(i-1)+p(i-1)/(rho(i-1)*(gamma-1))), i-1))
057      p(i) = p(i) - dt / dx * (flux((p/(gamma-1)+0.5*rho*u*u), (u*(u(i)+p(i)/(rho(i)*(gamma-1)))), &
058                               ((rho*u*u+p)*(u(i)+p(i)/(rho(i)*(gamma-1))))/(gamma-1), i) - &
059                               flux((p/(gamma-1)+0.5*rho*u*u), (u*(u(i-1)+p(i-1)/(rho(i-1)*(gamma-1)))), &
060                               ((rho*u*u+p)*(u(i-1)+p(i-1)/(rho(i-1)*(gamma-1))))/(gamma-1), i-1))
061    end do
062    
063    ! 更新左边界值
064    i = 0
065    rho(i) = rho(i+1)
066    u(i) = -u(i+1)
067    p(i) = p(i+1)
068    
069    ! 更新右边界值
070    i = nx-1
071    rho(i) = rho(i-1)
072    u(i) = -u(i-1)
073    p(i) = p(i-1)
074    
075    ! 更新时间
076    t = t + dt
077  end do
078   
079write(100,*) 'variables=', '"x","rho","u","p"'
080 
081! 输出结果
082  do i = 0, nx
083    x = i * dx
084        write(100,"(4(e15.7))") x, rho(i), u(i), p(i)
085  end do
086close(100)
087 
088contains
089   
090  ! 定义通量函数
091  function flux(rho, u, p, i) result(f)
092    real, intent(in) :: rho(:), u(:), p(:)
093    integer, intent(in) :: i
094    real :: f
095    
096    f = rho(i) * u(i)
097    
098  end function flux
099 
100end program shock_tube

174

帖子

2

主题

1

精华

大师

Vim

F 币
1061 元
贡献
497 点

规矩勋章

板凳
发表于 2023-9-8 18:23:32 | 只看该作者
[Fortran] 纯文本查看 复制代码
1function flux(rho, u, p, i) result(f)
2  real, intent(in) :: rho(0:), u(0:), p(0:)
3  integer, intent(in) :: i
4  real :: f
5  
6  f = rho(i) * u(i)
7  
8end function flux


这种传递数组的方式如果没有写下界,默认是1,你这里需要改为默认下界是0

这个最好写成integer
[Fortran] 纯文本查看 复制代码
1integer, parameter :: nx = 20         ! 空间网格数

12

帖子

4

主题

0

精华

入门

F 币
52 元
贡献
26 点
地板
 楼主| 发表于 2023-9-9 15:01:40 | 只看该作者
Transpose 发表于 2023-9-8 18:23
[mw_shl_code=fortran,true]  function flux(rho, u, p, i) result(f)
    real, intent(in) :: rho(0:), u ...

十分感谢大佬
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

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

GMT+8, 2025-5-2 08:29

Powered by Discuz! X3.4

© 2013-2025 Comsenz Inc.

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