在程序开始时进行系统计算/初始化的正确方法 [英] Proper way of doing the systematic computations / initializations at the beginning of a program

查看:204
本文介绍了在程序开始时进行系统计算/初始化的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,我将在两个主要情况下使用:1D和2D。通过外部文件读取来读取 ndim read 我想在模块 param 中做。



我有各种矢量的声明,其大小取决于维度。所以显然我必须在我的主程序开始时计算这些大小。这样做的正确方法是什么?

 模块尺寸
使用prec
使用参数$ b我想使用一个模块但是失败。 $ b隐式无
整型(int32),参数:: nip = 49!内点数
包含
子程序calc_neqn()
使用prec
隐式无
整型(int32):: nup!未知点数
整数(int32):: neqn!等式数

!计算未知点的数量
nup =(nip + 2)** ndim
!计算方程数
neqn = 2 * nup

结束子程序calc_neqn
!系统计算
调用calc_neqn()
结束模块维度

- 非常明确 - 错误消息:

pre $ 错误:CONTAINS部分的意外CALL语句(1)

换句话说,一个模块可以调用子程序吗?

解决方案

模块无法自行执行代码



你可能想要做的是这样的:

  module my_module 
implicit none
integer :: ndims
real,dimension(:),allocatable :: my_data
包含
子程序initialize()
ndims =。 ..
allocate(my_data(ndims))
结束子程序初始化
结束模块my_module

程序my_program
使用my_module
隐式无
call initialize()
....
结束程序my_program

这会给模块和主模块都带来 ndim 变量以及 my_data 数组程序。只要确保不要在子例程中再次定义,因为它只会在 initialize 范围内创建新变量。


I am writing a program that I will use in two main cases: 1D and 2D. The dimension ndim is loaded through a read of an external file, this read I want to do in module param.

I have various declarations of vectors whose sizes depend on the dimension. So obviously I have to compute these sizes at the beginning of my main program. What is the proper way of doing so? I want to use a module but I fail.

module dimensions
  use prec
  use param
  implicit none
  integer ( int32 ), parameter :: nip = 49   ! Number of interior points
contains
subroutine calc_neqn ( )
  use prec
  implicit none
  integer ( int32 ) :: nup                   ! Number of unknown points
  integer ( int32 ) :: neqn                  ! Number of equations

! compute number of unknown points
  nup = (nip+2)**ndim
! compute number of equations
  neqn = 2*nup

end subroutine calc_neqn
! systematic computations
  call calc_neqn ( )
end module dimensions

I get the following - very clear - error message:

Error: Unexpected CALL statement in CONTAINS section at (1)

In other words, can a module call a subroutine? And what would be the proper syntax?

解决方案

Modules can't execute code by themselves.

What you might want to do is something like this:

module my_module
    implicit none
    integer :: ndims
    real, dimension(:), allocatable :: my_data
contains
    subroutine initialize()
        ndims = ...
        allocate(my_data(ndims))
    end subroutine initialize
end module my_module

program my_program
    use my_module
    implicit none
    call initialize()
    ....
end program my_program

This would give both the ndim variable, as well as the my_data array to both the module as well as the main program. Just make sure that you don't define either again inside the subroutine, as that would create new variables only inside the scope of initialize.

这篇关于在程序开始时进行系统计算/初始化的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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