读取输入,然后写入文件

时间:2016-09-26 01:31:23

标签: segmentation-fault fortran fortran90 gfortran

我正在尝试使用4个模块将。矩阵写入.txt文件:

  1. input.f90从.txt文件中读取输入
  2. navierstokes_loser.f90创建矩阵(我会在这里添加一些do循环,但首先我只需要它来打印)
  3. resultatplot.f90从navierstokes_loser中获取矩阵,然后将其打印到.txt文件
  4. main.f90结合了所有模块并运行程序
  5. 当我运行程序时,我收到程序接收信号SIGSEGV:分段错误 - 内存参考无效。

    我做错了什么?

    input.f90

     module input
    
    implicit none
    
    contains
    
    
    subroutine lesinput(n,omega)
    
    !Output
    integer :: n
    real(8) :: omega
    
    
    !Åpner fil og gir variable verdier
    open(1, file='input.txt',status='old',action='read')
    
    read(1,*), n
    read(1,*), omega
    
    
    close(1)
    
    end subroutine lesinput
    
    
    
    end module input
    

    navierstokes_loser.f90

    module navierstokes_loser
    
    
    implicit none
    
    contains
    
    
    subroutine los_navier(n,omega,u)
    
    !input
    integer :: n
    real(8) :: omega
    
    
    !lokal
    real(8) :: u(n+1,n+1)
    integer :: i,j
    
    
    u(n+1,n+1)=0.0d0
    
    
    end subroutine los_navier
    
    end module navierstokes_loser
    

    resultatplot.f90

    module resultatplot
    
    implicit none
    
    contains
    
    
    subroutine vektorplot(n,u)
    
    !input
    integer :: n
    real(8) :: u(n+1,n+1)
    
    !lokale
    integer :: i,j
    real(8) :: vek_x
    
    
    !Skriver vektor verdier til fil som gnuplot skal bruke
    open(2,access='sequential',file='vekdata.txt',status='unknown')
    
    write(2,*)'# x y vx vy'
    
     do i=1,n+1
        do j=1,n+1
    
        vek_x=u(j,i)
    
    
        write(2,*) i, j, vek_x
    
        end do
    write(2,*)''
    end do
    
    close(2,status='keep')
    
    
    end subroutine vektorplot
    
    
    end module resultatplot
    

    main.f90时

    program main
    
    use input
    use navierstokes_loser
    use resultatplot
    
    implicit none
    
    integer :: n
    real(8) :: omega
    real(8), dimension (:,:), allocatable :: u
    
    call lesinput(n,omega)
    
    allocate(u(n+1,n+1))
    
    call los_navier(n,omega,u)
    
    
    call vektorplot(n,u)
    
    
    end program main
    

1 个答案:

答案 0 :(得分:1)

好的,我在这里看到了很多东西:

  1. 小于10的单位 - 这总是危险的。最佳:在open文件时,使用newunit=<somevar>然后使用此<somevar>作为阅读,写入和关闭的单位。但至少要使用大于10的数字,而不是1和2。
  2. 让我们谈谈u
    1. main.f90中,它是一个可分配的一维数组,但在los_navier中它是一个二维数组。
    2. 从未实际分配。
  3. 在子程序中清楚地指出intent有助于编译器及早发现错误。
  4. 所以,不用多说,这是我的建议:

    module input
        implicit none
    contains
        subroutine lesinput(n,omega)
            integer :: n
            real(8) :: omega
            integer :: read_unit
            open(newunit=read_unit, file='input.txt',status='old',action='read')
            read(read_unit,*), n
            read(read_unit,*), omega
            close(read_unit)
        end subroutine lesinput
    end module input
    
    module navierstokes_loser
        implicit none
    contains
        subroutine los_navier(n,omega,u)
            integer, intent(in) :: n
            real(8), intent(in) :: omega
            real(8), allocatable, intent(out) :: u(:,:)
            integer :: i,j
            if (allocated(u)) deallocate(u)
            allocate(u(n+1, n+1))
            u=0.0d0
        end subroutine los_navier
    end module navierstokes_loser
    
    module resultatplot
        implicit none
    contains
        subroutine vektorplot(n,u)
            integer, intent(in) :: n
            real(8), intent(in) :: u(n+1,n+1)
            integer :: i,j
            integer :: write_unit
            open(newunit=write_unit,access='sequential',file='vekdata.txt',status='unknown')
            write(write_unit,*)'# x y vx vy'
            do i=1,n+1
                do j=1,n+1
                    write(write_unit,*) i, j, u(j, i)
                end do
                write(write_unit,*)''
            end do
            close(write_unit,status='keep')
        end subroutine vektorplot
    end module resultatplot
    
    program main
        use input
        use navierstokes_loser
        use resultatplot
        implicit none
        integer :: n
        real(8) :: omega
        real(8), dimension (:, :), allocatable :: u
        call lesinput(n,omega)
        call los_navier(n,omega,u)
        call vektorplot(n,u)
    end program main