从外部子程序访问主程序中的变量 [英] Accessing variable in main program from external subroutine

查看:208
本文介绍了从外部子程序访问主程序中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Fortran中使用NAG优化库解决最大似然问题,但是我遇到了从外部子程序funct和hess访问主程序中变量的问题(请参见下面的伪代码)。考虑到我无法直接将它们作为参数传递(对NAG库的限制),将主程序中的变量(例如 b )传递给此类例程的最佳方式是什么? 。我试图实现COMMON块,但没有太多成功。

I'm trying to solve a maximum likelihood problem in Fortran using the NAG optimization library, but I'm running into problems accessing the variables in the main program from the external subroutines funct and hess (see pseudo-code below). What is the best way to pass variables (e.g. b) from the main program to such routines, given that I cannot pass them directly as arguments (a restriction of the NAG library). I have tried to implement COMMON blocks, but without much succes.

MODULE user_parameters
    ! Define the user parameters
    INTEGER, PARAMETER :: a = 100
    ... other parameters
END MODULE user_parameters


MODULE process_data
    USE user_parameters

    ! Define some other variables
    INTEGER :: b
    ... other variables

CONTAINS

    SUBROUTINE read_data
        ... read the data (e.g. alter value of b)
    END SUBROUTINE read_data

    SUBROUTINE clean_data
        ... clean the data (e.g. alter value of b)
    END SUBROUTINE clean_data
END MODULE process_data


MODULE maximum_likelihood
    USE user_parameters
    USE process_data

CONTAINS

    SUBROUTINE funct
        ... returns the LL's function value and gradient
    END SUBROUTINE funct

    SUBROUTINE hess
        ... returns the LL's hessian
    END SUBROUTINE hess
END MODULE maximum_likelihood


PROGRAM estimation
    USE user_parameters
    USE process_data
    USE maximum_likelihood
    EXTERNAL funct, hess

    CALL read_data
    CALL clean_data

    ! Call minimization routine
    CALL E04LBF(funct, hess)

END PROGRAM estimation


推荐答案

如何编写模块 maximum_likelihood 如下所示

How about writing your module maximum_likelihood something as following?

MODULE maximum_likelihood
    IMPLICIT NONE
    PRIVATE
    PUBLIC init, fin, funct, hess

    INTEGER, SAVE                         :: aa, bb
    REAL, DIMENSION(:), ALLOCATABLE, SAVE :: vv

CONTAINS

    SUBROUTINE init(aa_arg, bb_arg, vv_arg)
        ! set values module variables
    END SUBROUTINE init

    SUBROUTINE fin()
        deallocate(vv) 
    END SUBROUTINE fin

    SUBROUTINE funct
        ... returns the LL's function value and gradient
        ! using the values of aa, bb, and/or vv
    END SUBROUTINE funct

    SUBROUTINE hess
        ... returns the LL's hessian
        ! using the values of aa, bb, and/or vv
    END SUBROUTINE hess

END MODULE maximum_likelihood

然后,主程序看起来就像

Then, the main program will look like

PROGRAM estimation
    USE maximum_likelihood, ONLY: init, fin, funct, hess

    ! read values for the parameters aaa, bbb, vvv

    ! set these values in the module variables in maximum_likelihood
    CALL init(aaa, bbb, vvv)

    ! Call minimization routine
    CALL E04LBF(funct, hess)

    ! clean up the module variables in maximum_likelihood
    CALL fin()

END PROGRAM estimation

这篇关于从外部子程序访问主程序中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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