Fortran Coder

查看: 2304|回复: 5

[派生类型] Error: The AUTOMATIC object 'tempparticles' must not have the SAVE attribute...

[复制链接]

16

帖子

7

主题

0

精华

入门

F 币
66 元
贡献
36 点
发表于 2022-5-30 21:15:19 | 显示全部楼层 |阅读模式
哪位大佬能解答一下我的困惑,为啥会在第8行报错?采用gfortran编译报错,intel fortran 不会报错。


函数

函数

主程序

主程序

报错内容

报错内容

235

帖子

0

主题

0

精华

版主

World Analyser

F 币
630 元
贡献
464 点

新人勋章美女勋章元老勋章热心勋章规矩勋章管理勋章

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

16

帖子

7

主题

0

精华

入门

F 币
66 元
贡献
36 点
 楼主| 发表于 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

235

帖子

0

主题

0

精华

版主

World Analyser

F 币
630 元
贡献
464 点

新人勋章美女勋章元老勋章热心勋章规矩勋章管理勋章

QQ
发表于 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


16

帖子

7

主题

0

精华

入门

F 币
66 元
贡献
36 点
 楼主| 发表于 2022-5-31 18:50:11 | 显示全部楼层
感谢这个部分懂了
作为一个编程小白还想问一个问题,如果放弃参数化,怎么实现将结构体备份,如下所示

subroutine Copytype(TempParticles, CopyTempParticles)
implicit none
CopyTempParticles%MassGas = TempParticles%MassGas
end subroutine Copytype

235

帖子

0

主题

0

精华

版主

World Analyser

F 币
630 元
贡献
464 点

新人勋章美女勋章元老勋章热心勋章规矩勋章管理勋章

QQ
发表于 2022-5-31 23:25:43 | 显示全部楼层
CopyTempParticles=TempParticles
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

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

GMT+8, 2024-3-29 15:28

Powered by Tencent X3.4

© 2013-2024 Tencent

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