复制到另一个可分配的数组后,Fortran 90不会保持数组上下边界 [英] Fortran 90 doesn't keep lower/upper array bounds after copy to another allocatable array

查看:65
本文介绍了复制到另一个可分配的数组后,Fortran 90不会保持数组上下边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不起作用

program main
   implicit none
   integer :: nx = 3
   integer :: ny = 5
   integer :: nz = 8

   real, allocatable, dimension(:,:,:) :: A
   real, allocatable, dimension(:,:) :: B

   allocate(A(nx,0:ny,nz) )
   ! ...do something with array A and at some point cope a slice of A to B:
   B = A(:,:,1)
   ! in this case B is (1:nx, 1: ny+1) 
end program main

上面的代码自动分配B并将A(:,:,1)复制到B.但是,它没有保持0/ny的下限/上限,而是B的下限为1,上限为ny + 1.

The code above automatically allocates B and copies A(:,:,1) to B. However it doesn't keep the lower/upper bound of 0/ny, instead B has its lower bound to 1 and upper bound to ny+1.

我发现保持A 2dn-dim上下限的唯一方法是将B显式分配为:

The only way I found to keep the lower/upper bound of A 2dn-dim is by explicitly allocate B as:

   allocate(B(nx, 0:ny))
   B = A(:,:,1)
   ! in this case B is (1:nx, 0:ny)

鉴于我有比此简单示例更多的变量,是否有一种方法可以像B = A(:,:,1)这样进行赋值,并且在不显式分配B的情况下保持A的边界?

Given that I have many more variables than in this simple example, Is there a way to assign like B=A(:,:,1) and also keep the bounds of A without explicitly allocate B?

推荐答案

A(:,:,1)是一个表达式.它的边界为(1:nx,1:ny),两个行都从1开始.它不是原始数组,而是一个新表达式.

A(:,:,1) is an expression. It has bounds (1:nx, 1:ny), BOTH ranks start at 1. It is not the original array, it is a new expression.

但是,即使它是一个具有其他下限的数组,自动分配也总是从1开始分配索引.基本上,右侧又是一个表达式.

However, even if it was an array that had some other lower bounds, automatic allocation always allocates indexes starting from 1. Basically, the right hand side is an expression again.

对于您的情况,您必须明确分配.

For your case you do have to allocate explicitly.

这篇关于复制到另一个可分配的数组后,Fortran 90不会保持数组上下边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆