Pip*_*ppi 1 arrays fortran allocation subroutine
更新:我修改过的代码如下所示:
program run_module_test
use module_test
implicit none
TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:)
call update(xyzArray)
write(6,*)'xyzArray',xyzArray
end program run_module_test
module module_test
implicit none
TYPE :: newXYZ
real(4) :: x, u
real(4) :: y, v
real(4) :: z, w
real(4),dimension(3) :: uvw
END TYPE
integer(4) :: shape = 3
contains
subroutine update(xyzArray)
integer(4) :: i
TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:)
allocate( xyzArray(shape) )
do i = 1, shape
xyzArray(i)%x = 0
xyzArray(i)%y = 0
xyzArray(i)%z = 0
xyzArray(i)%u = 0
xyzArray(i)%v = 0
xyzArray(i)%w = 0
xyzArray(i)%uvw = (/0,0,0/)
end do
return
end subroutine update
end module module_test
Run Code Online (Sandbox Code Playgroud)
编译时,它们会产生类似的错误:
TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:)
1
Error: ALLOCATABLE attribute conflicts with DUMMY attribute at (1)
Run Code Online (Sandbox Code Playgroud)
当我消除update()子例程中的参数时,我收到一个矛盾的错误:
TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:)
1
Error: Symbol at (1) is not a DUMMY variable
Run Code Online (Sandbox Code Playgroud)
我是否消除了有用建议中指出的错误来源?这可能是编译器相关的错误(使用mpi90)?
~~~首先编辑~~~我有一个子程序,其输入参数是用户定义类型XYZ的数组.我希望解除分配xyzArray并在子例程的主体中将其分配/修改为不同的大小.我尝试通过更改fortran中的数组维度建议的方法,但是当我执行以下操作时:
subroutine update(xyzArray, ...)
...
TYPE (XYZ), allocatable :: xyzArray(:)
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
Error: ALLOCATABLE attribute conflicts with DUMMY attribute at (1)
Run Code Online (Sandbox Code Playgroud)
当我尝试:
subroutine update(xyzArray, ...)
...
deallocate( xyzArray(myshape) )
allocate( xyzArray(newshape) )
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
Error: Expression in DEALLOCATE statement at (1) must be ALLOCATABLE or a POINTER
Error: Expression in ALLOCATE statement at (1) must be ALLOCATABLE or a POINTER
Run Code Online (Sandbox Code Playgroud)
我需要做些什么才能在子程序中更改数组的大小?
去做这个:
伪参数必须是可分配的.可分配的伪参数需要一个编译器来实现Fortran 2003标准的相关部分(或实现所谓的"可分配"TR的Fortran 95编译器).
需要显示该过程的显式接口(该过程必须是模块过程,内部过程或在调用范围中具有接口块).
伪参数不能是intent(in).如果您在子例程中根本没有使用分配状态或伪参数值的其他方面,则intent(out)可能是合适的(如果事先分配,则在调用过程时将自动释放伪参数),否则意图(inout)或无意图.
(您的第二个示例代码块与deallocate语句有语法错误 - 您应该只指定xyzArray变量,不要使用(myshape)形状规范))
例如,在一个模块中:
subroutine update(xyzArray)
type(xyz), allocatable, intent(inout) :: xyzArray(:)
...
if (allocated(xyzArray)) deallocate(xyzArray)
allocate(xyzArray(newshape))
...
Run Code Online (Sandbox Code Playgroud)