【问题标题】:updating values of an array in a subroutine in fortran在fortran的子例程中更新数组的值
【发布时间】:2021-02-06 22:48:31
【问题描述】:

我有以下模块:

module prestel
implicit none
contains
  subroutine gradient(rho,N,gradiant_rho)
    implicit none
    real(kind=real), allocatable  :: rho(:) 
    real(kind=real),allocatable   :: gradiant_rho(:)
    integer,intent(in)          :: N
    integer                     :: i
    do i = 1,N
      gradiant_rho(i) = rho(i) - 2
    end do
  end subroutine gradient

  subroutine state_system(density,N,grad_density)

  implicit none
  real(kind=real), allocatable :: density(:)
  real(kind=real), allocatable :: grad_density(:)
  integer :: i,j
  integer, intent(in) :: N
    call gradient(density,velocity,N,grad_density,grad_velocity)
    do i=1,N
      density (i+1) = density(i) - 2*grad_density(i))
    end do
end subroutine state_system
end module prestel

现在子程序“状态系统”使用第一个子程序“梯度”。我们从一个初始数组“密度”开始,“状态系统”子程序对它的每个元素进行操作,直到我们有一个修改后的新数组。
现在我的问题是我需要把它放在一个循环中(比如说做 j=1,10)。因此,在此循环的每次迭代中,子程序(系统状态和梯度)将返回一个修改后的数组,然后我希望我的程序用获得的新数组替换旧数组(即两个子程序的输入)并执行再次执行。
我不知道如何在每次迭代时更新子例程中的这两个数组。我尝试定义一个新数组并将所有新值放入其中,但它并没有导致任何地方。
如果我想这样做,是在主程序调用子程序的时候,还是在子程序里面?

【问题讨论】:

    标签: arrays loops fortran subroutine


    【解决方案1】:

    您应该将循环放在主程序中,每次调用state_system 子例程时,density 数组都会更新并再次馈送到子例程等。

    module prestel_m
      implicit none
      private
      public state_system
    
    contains
    
      subroutine gradient(density, gradiant_density)
        real, intent(in) :: density(:)
        real, intent(out) :: gradiant_density(:)    
        gradiant_density = density - 2  
      end subroutine gradient
    
      subroutine state_system(density)
        real, intent(inout) :: density(:)
        real, allocatable :: grad_density(:)
    
        allocate (grad_density(size(density)))
    
        call gradient(density, grad_density)
        density = density - 2*grad_density
      end subroutine
    
    end module
    
    
    program test_state_system
      use prestel_m          
      integer, parameter :: n = 5 ! just for this example
      real, allocatable :: density(:)
      integer :: j
      
      allocate (density(n))
      call random_number(density) ! just for this example
    
      do j = 1, 10
        ! here, you get updated version of 'density' for each iteration
        call state_system(density)
      end do
    
    end program
    

    【讨论】:

    • 非常感谢您的回答!我做了循环,而是打印了 10 次密度值,您认为这可能与我必须修复的代码中的错误有关,然后循环应该可以正常工作吗?
    • 是的,我认为你应该修复算法,因为目前打印的数组是无限交替的......
    【解决方案2】:

    我不是 100% 肯定能正确理解您的问题,但以下代码应该按照您的意图做一些事情。 如果我误解了您的问题,请随时提供反馈。

    module prestel_m
      implicit none
    
      private
      public state_system
    
    contains
    
      subroutine gradient(density, gradiant_density)
        real, intent(in)  :: density(:)
        real, intent(out) :: gradiant_density(:)
    
        gradiant_density = density - 2
      end subroutine gradient
    
      subroutine state_system(density)
        real, intent(inout) :: density(:)
    
        integer           :: j
        real, allocatable :: grad_density(:)
    
        allocate (grad_density(size(density)))
    
        do j = 1, 10
          call gradient(density, grad_density)
    
          density = density - 2*grad_density
        end do
      end subroutine
    
    end module
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      • 2014-08-24
      • 1970-01-01
      • 2013-10-06
      • 2017-02-11
      • 2015-08-08
      • 1970-01-01
      相关资源
      最近更新 更多