Fortran Coder

Error: The AUTOMATIC object 'tempparticles' must not have the SAVE attribute...

查看数: 2357 | 评论数: 5 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2022-5-30 21:15

正文摘要:

哪位大佬能解答一下我的困惑,为啥会在第8行报错?采用gfortran编译报错,intel fortran 不会报错。

回复

kyra 发表于 2022-5-31 23:25:43
CopyTempParticles=TempParticles
Neo123 发表于 2022-5-31 18:50:11
感谢这个部分懂了
作为一个编程小白还想问一个问题,如果放弃参数化,怎么实现将结构体备份,如下所示

subroutine Copytype(TempParticles, CopyTempParticles)
implicit none
CopyTempParticles%MassGas = TempParticles%MassGas
end subroutine Copytype
kyra 发表于 2022-5-31 09:08:57
本帖最后由 kyra 于 2022-5-31 09:20 编辑

你这代码我在 intel fortran 上也编译不过呀。

派生类型参数化,本来大家支持得就不好。尽量避免使用。
我想到的修改方案:
第一个:
  integer::x=1 改为: integer , parameter :: x = 1  取消派生类型参数化后的 allocatable ,如果你要 allocatable,就没有参数化的意义了。

[Fortran] 纯文本查看 复制代码
module ModParticle
  implicit none
  integer,parameter::x=4
  type ParticleType(n)
    integer(4), len :: n
    real(4):: MassGas(n)
  end type
contains ! Routines of this module

  Subroutine CopyParticle(this, ID)
    type(ParticleType(*)) :: this
    integer(4), intent(in) ::  ID
    this%MassGas = 0.1
  End Subroutine CopyParticle
  
end module

PROGRAM Main
  use ModParticle
  implicit none
  integer::i
  type(ParticleType(x))::TempParticles
  do i = 1, 1
    call CopyParticle(TempParticles,i)
  end do
  write(*,*) TempParticles%MassGas
END PROGRAM Main


如果不取消 parameter,在 gfortran 9.x 上无法实现。intel fortran 可以这样

[Fortran] 纯文本查看 复制代码
module ModParticle
  implicit none
  integer::x=4
  type ParticleType(n)
    integer(4), len :: n
    real(4) :: MassGas(n)
  contains
    Procedure :: CopyParticle
  end type
contains ! Routines of this module

  Subroutine CopyParticle( this , ID )
    integer(4), intent(in) ::  ID
    class(ParticleType(*)) :: this    
    this%MassGas = 0.1
  End Subroutine CopyParticle

end module

PROGRAM Main
  use ModParticle
  implicit none
  integer::i
  type(ParticleType(:)),allocatable::TempParticles
  allocate(ParticleType(x)::TempParticles)
  do i = 1, TempParticles%n
    call TempParticles%CopyParticle(i)
  end do
  write(*,*) TempParticles%MassGas
  deallocate(TempParticles)
END PROGRAM Main


第三,放弃参数化
[Fortran] 纯文本查看 复制代码
module ModParticle
  implicit none
  integer::x=4
  type ParticleType
    real(4) , allocatable :: MassGas(:)
  contains
    Procedure :: CopyParticle
  end type
contains ! Routines of this module

  Subroutine CopyParticle( this , ID )
    integer(4), intent(in) ::  ID
    class(ParticleType) :: this    
    this%MassGas = 0.1
  End Subroutine CopyParticle

end module

PROGRAM Main
  use ModParticle
  implicit none
  integer::i
  type(ParticleType)::TempParticles
  allocate(TempParticles%MassGas(x))
  do i = 1, size(TempParticles%MassGas)
    call TempParticles%CopyParticle(i)
  end do
  write(*,*) TempParticles%MassGas
  deallocate(TempParticles%MassGas)
END PROGRAM Main


Neo123 发表于 2022-5-30 23:19:52
[Fortran] 纯文本查看 复制代码
PROGRAM Main
    use ModParticle

    implicit none
    integer::i

    ! Local variables
    type(ParticleType(x))::TempParticles

    do i = 1, 1
        TempParticles = CopyParticle(i)
    end do

    write(*,*) TempParticles
END PROGRAM Main


[Fortran] 纯文本查看 复制代码
module ModParticle
    implicit none
    integer::x=1
        type ParticleType(n)
        integer(4), len :: n
          !> Particle mass [kg] (corresponds to the gas density)(fixed during simulation - ensures conservation of mass)
        real(4),allocatable:: MassGas(:)
    end type
    contains ! Routines of this module

type(ParticleType(x)) function CopyParticle( ID)
    implicit none
    integer(4), intent(in) ::  ID
    CopyParticle%MassGas = 0.1
write(*,*) CopyParticle%MassGas
end function


end module
kyra 发表于 2022-5-30 22:46:09
代码用复制粘贴的多好,截图不是个好方法。比如我想帮你测,但懒得把代码敲一遍。

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

GMT+8, 2024-5-15 02:10

Powered by Tencent X3.4

© 2013-2024 Tencent

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