A. Fortran命令行启动怎么往主程序输入参数
主要使用下面的三个函数实现:
第一个:
COMMAND_ARGUMENT_COUNT — Get number of command line arguments
Description:
COMMAND_ARGUMENT_COUNT returns the number of arguments passed on the command line when the containing program was invoked.
Standard:
Fortran 2003 and later
Class:
Inquiry function
Syntax:
RESULT = COMMAND_ARGUMENT_COUNT()
Arguments:
None
Return value:
The return value is an INTEGER of default kind.
Example:
program test_command_argument_count
integer :: count
count = command_argument_count()
print *, count
end program test_command_argument_count
COMMAND_ARGUMENT_COUNT — Get number of command line arguments
Description:
COMMAND_ARGUMENT_COUNT returns the number of arguments passed on the command line when the containing program was invoked.
Standard:
Fortran 2003 and later
Class:
Inquiry function
Syntax:
RESULT = COMMAND_ARGUMENT_COUNT()
Arguments:
None
Return value:
The return value is an INTEGER of default kind.
Example:
program test_command_argument_count
integer :: count
count = command_argument_count()
print *, count
end program test_command_argument_count
第二个:
GET_COMMAND_ARGUMENT — Get command line arguments
Description:
Retrieve the NUMBER-th argument that was passed on the command line when the containing program was invoked.
Standard:
Fortran 2003 and later
Class:
Subroutine
Syntax:
CALL GET_COMMAND_ARGUMENT(NUMBER [, VALUE, LENGTH, STATUS])
Arguments:
NUMBER Shall be a scalar of type INTEGER and of default kind, NUMBER \geq 0
VALUE (Optional) Shall be a scalar of type CHARACTER and of default kind.
LENGTH (Optional) Shall be a scalar of type INTEGER and of default kind.
STATUS (Optional) Shall be a scalar of type INTEGER and of default kind.
Return value:
After GET_COMMAND_ARGUMENT returns, the VALUE argument holds the
NUMBER-th command line argument. If VALUE can not hold the argument, it
is truncated to fit the length of VALUE. If there are less than NUMBER
arguments specified at the command line, VALUE will be filled with
blanks. If NUMBER = 0, VALUE is set to the name of the program (on
systems that support this feature). The LENGTH argument contains the
length of the NUMBER-th command line argument. If the argument retrieval
fails, STATUS is a positive number; if VALUE contains a truncated
command line argument, STATUS is -1; and otherwise the STATUS is zero.
Example:
PROGRAM test_get_command_argument
INTEGER :: i
CHARACTER(len=32) :: arg
i = 0
DO
CALL get_command_argument(i, arg)
IF (LEN_TRIM(arg) == 0) EXIT
WRITE (*,*) TRIM(arg)
i = i+1
END DO
END PROGRAM
GET_COMMAND_ARGUMENT — Get command line arguments
Description:
Retrieve the NUMBER-th argument that was passed on the command line when the containing program was invoked.
Standard:
Fortran 2003 and later
Class:
Subroutine
Syntax:
CALL GET_COMMAND_ARGUMENT(NUMBER [, VALUE, LENGTH, STATUS])
Arguments:
NUMBER Shall be a scalar of type INTEGER and of default kind, NUMBER \geq 0
VALUE (Optional) Shall be a scalar of type CHARACTER and of default kind.
LENGTH (Optional) Shall be a scalar of type INTEGER and of default kind.
STATUS (Optional) Shall be a scalar of type INTEGER and of default kind.
Return value:
After GET_COMMAND_ARGUMENT returns, the VALUE argument holds the
NUMBER-th command line argument. If VALUE can not hold the argument, it
is truncated to fit the length of VALUE. If there are less than NUMBER
arguments specified at the command line, VALUE will be filled with
blanks. If NUMBER = 0, VALUE is set to the name of the program (on
systems that support this feature). The LENGTH argument contains the
length of the NUMBER-th command line argument. If the argument retrieval
fails, STATUS is a positive number; if VALUE contains a truncated
command line argument, STATUS is -1; and otherwise the STATUS is zero.
Example:
PROGRAM test_get_command_argument
INTEGER :: i
CHARACTER(len=32) :: arg
i = 0
DO
CALL get_command_argument(i, arg)
IF (LEN_TRIM(arg) == 0) EXIT
WRITE (*,*) TRIM(arg)
i = i+1
END DO
END PROGRAM
第三个:
GET_COMMAND — Get the entire command line
Description:
Retrieve the entire command line that was used to invoke the program.
Standard:
Fortran 2003 and later
Class:
Subroutine
Syntax:
CALL GET_COMMAND([COMMAND, LENGTH, STATUS])
Arguments:
COMMAND (Optional) shall be of type CHARACTER and of default kind.
LENGTH (Optional) Shall be of type INTEGER and of default kind.
STATUS (Optional) Shall be of type INTEGER and of default kind.
Return value:
If COMMAND is present, stores the entire command line that was used to
invoke the program in COMMAND. If LENGTH is present, it is assigned the
length of the command line. If STATUS is present, it is assigned 0 upon
success of the command, -1 if COMMAND is too short to store the command
line, or a positive value in case of an error.
Example:
PROGRAM test_get_command
CHARACTER(len=255) :: cmd
CALL get_command(cmd)
WRITE (*,*) TRIM(cmd)
END PROGRAM
GET_COMMAND — Get the entire command line
Description:
Retrieve the entire command line that was used to invoke the program.
Standard:
Fortran 2003 and later
Class:
Subroutine
Syntax:
CALL GET_COMMAND([COMMAND, LENGTH, STATUS])
Arguments:
COMMAND (Optional) shall be of type CHARACTER and of default kind.
LENGTH (Optional) Shall be of type INTEGER and of default kind.
STATUS (Optional) Shall be of type INTEGER and of default kind.
Return value:
If COMMAND is present, stores the entire command line that was used to
invoke the program in COMMAND. If LENGTH is present, it is assigned the
length of the command line. If STATUS is present, it is assigned 0 upon
success of the command, -1 if COMMAND is too short to store the command
line, or a positive value in case of an error.
Example:
PROGRAM test_get_command
CHARACTER(len=255) :: cmd
CALL get_command(cmd)
WRITE (*,*) TRIM(cmd)
END PROGRAM
最近在Fortran的数据传递设计上碰到了问题。现在略归纳下子程序间参数传递的实现办法。
首先,Fortran subroutine
独立地拥有属于自己的变量申明。有说:Fortran的变量作用域只有两类:一是全局作用域,二是子程序作用域,并且子程序内部不存在嵌套作用域。最后一
点很重要哟。举例:在一个子程序里申明的变量,不能在与之并列的另外一个子程序里面使用。即两个子程序不能有变量重叠。即使在这个两个子程序里都使用了同
名的某个变量,本质上讲,在这两个子程序里的同名变量也是没任何联系的。
那么,如果需要在子程序之间传递变量(参数),怎么办呢?还要细说一点:传参的结构有两类:第一类,诸如主程序-子程序、外层子程序-内层子程序(子程序嵌套)之间的参数传递,另外一类,就是并列的两个子程序(即它们之间不需要有关系,仅仅由于需求相同的变量)之间的参数传递。
第一类传参结构的实现常见传址调用。
第二类传参结构的实现常见申明公共内存块,将需求的相同变量申明成全局变量,那么各个子程序自然都可以用了。注意,仍然是地址,所以子程序可以修改这些全局变量的。
其中,FORTRAN77的公共块(全局变量申明)使用COMMON;FORTRAN90以后使用MODULE来包装公共块。
来看几个问题:
一、以数组A中满足某一条件的元素的编号构成新的数组B
do bi=1,bn
do ai=1,an
if(A(ai).eq.a0) then
B(bi)=ai
endif
enddo
enddo
二、以数组A的元素为编号,取数组B的元素进行其他运算
do bi=1,bn
do ai=1,an
if (bi.eq.A(ai)) then
fun(i)=fun(i)+fun2(bi)
endif
enddo
enddo