Fortran Coder

查看: 3128|回复: 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 不会报错。


下载.png (7.27 KB, 下载次数: 237)

函数

函数

下载 (1).png (5.83 KB, 下载次数: 220)

主程序

主程序

下载 (2).png (8.47 KB, 下载次数: 219)

报错内容

报错内容
分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

268

帖子

0

主题

0

精华

版主

World Analyser

F 币
749 元
贡献
526 点

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

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

16

帖子

7

主题

0

精华

入门

F 币
66 元
贡献
36 点
板凳
 楼主| 发表于 2022-5-30 23:19:52 | 只看该作者
[Fortran] 纯文本查看 复制代码
01PROGRAM Main
02    use ModParticle
03 
04    implicit none
05    integer::i
06 
07    ! Local variables
08    type(ParticleType(x))::TempParticles
09 
10    do i = 1, 1
11        TempParticles = CopyParticle(i)
12    end do
13 
14    write(*,*) TempParticles
15END PROGRAM Main


[Fortran] 纯文本查看 复制代码
01module ModParticle
02    implicit none
03    integer::x=1
04        type ParticleType(n)
05        integer(4), len :: n
06          !> Particle mass [kg] (corresponds to the gas density)(fixed during simulation - ensures conservation of mass)
07        real(4),allocatable:: MassGas(:)
08    end type
09    contains ! Routines of this module
10 
11type(ParticleType(x)) function CopyParticle( ID)
12    implicit none
13    integer(4), intent(in) ::  ID
14    CopyParticle%MassGas = 0.1
15write(*,*) CopyParticle%MassGas
16end function
17 
18 
19end module

268

帖子

0

主题

0

精华

版主

World Analyser

F 币
749 元
贡献
526 点

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

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] 纯文本查看 复制代码
01module ModParticle
02  implicit none
03  integer,parameter::x=4
04  type ParticleType(n)
05    integer(4), len :: n
06    real(4):: MassGas(n)
07  end type
08contains ! Routines of this module
09 
10  Subroutine CopyParticle(this, ID)
11    type(ParticleType(*)) :: this
12    integer(4), intent(in) ::  ID
13    this%MassGas = 0.1
14  End Subroutine CopyParticle
15   
16end module
17 
18PROGRAM Main
19  use ModParticle
20  implicit none
21  integer::i
22  type(ParticleType(x))::TempParticles
23  do i = 1, 1
24    call CopyParticle(TempParticles,i)
25  end do
26  write(*,*) TempParticles%MassGas
27END PROGRAM Main


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

[Fortran] 纯文本查看 复制代码
01module ModParticle
02  implicit none
03  integer::x=4
04  type ParticleType(n)
05    integer(4), len :: n
06    real(4) :: MassGas(n)
07  contains
08    Procedure :: CopyParticle
09  end type
10contains ! Routines of this module
11 
12  Subroutine CopyParticle( this , ID )
13    integer(4), intent(in) ::  ID
14    class(ParticleType(*)) :: this   
15    this%MassGas = 0.1
16  End Subroutine CopyParticle
17 
18end module
19 
20PROGRAM Main
21  use ModParticle
22  implicit none
23  integer::i
24  type(ParticleType(:)),allocatable::TempParticles
25  allocate(ParticleType(x)::TempParticles)
26  do i = 1, TempParticles%n
27    call TempParticles%CopyParticle(i)
28  end do
29  write(*,*) TempParticles%MassGas
30  deallocate(TempParticles)
31END PROGRAM Main


第三,放弃参数化
[Fortran] 纯文本查看 复制代码
01module ModParticle
02  implicit none
03  integer::x=4
04  type ParticleType
05    real(4) , allocatable :: MassGas(:)
06  contains
07    Procedure :: CopyParticle
08  end type
09contains ! Routines of this module
10 
11  Subroutine CopyParticle( this , ID )
12    integer(4), intent(in) ::  ID
13    class(ParticleType) :: this   
14    this%MassGas = 0.1
15  End Subroutine CopyParticle
16 
17end module
18 
19PROGRAM Main
20  use ModParticle
21  implicit none
22  integer::i
23  type(ParticleType)::TempParticles
24  allocate(TempParticles%MassGas(x))
25  do i = 1, size(TempParticles%MassGas)
26    call TempParticles%CopyParticle(i)
27  end do
28  write(*,*) TempParticles%MassGas
29  deallocate(TempParticles%MassGas)
30END PROGRAM Main


16

帖子

7

主题

0

精华

入门

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

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

268

帖子

0

主题

0

精华

版主

World Analyser

F 币
749 元
贡献
526 点

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

QQ
6#
发表于 2022-5-31 23:25:43 | 只看该作者
CopyTempParticles=TempParticles
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

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

GMT+8, 2025-3-15 07:05

Powered by Discuz! X3.4

© 2013-2025 Comsenz Inc.

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