| CopyTempParticles=TempParticles |
感谢 这个部分懂了作为一个编程小白还想问一个问题,如果放弃参数化,怎么实现将结构体备份,如下所示 subroutine Copytype(TempParticles, CopyTempParticles) implicit none CopyTempParticles%MassGas = TempParticles%MassGas end subroutine Copytype |
|
本帖最后由 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 |
|
[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 |
| 代码用复制粘贴的多好,截图不是个好方法。比如我想帮你测,但懒得把代码敲一遍。 |
捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )
GMT+8, 2025-12-21 18:15