Accessing variable in main program from external subroutine

痞子三分冷 提交于 2019-12-24 09:17:50

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/43159202/accessing-variable-in-main-program-from-external-subroutine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!