FORTRAN变量输出不等于输入 [英] FORTRAN variable output not equal to input

查看:181
本文介绍了FORTRAN变量输出不等于输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是FORTRAN的完整新手,并且是大多数一般形式的编程的新手.有了这样的说法,我试图建立一个盒子,然后随机生成100个原子的x,y,z坐标.从那里开始,目标是计算每个原子之间的距离,并对距离结果进行一些数学运算.下面是我的代码.即使n被定义为100,并且将打印'100',但是当我打印cx时,我只会得到20个结果.

First of all I am a complete novice to FORTRAN, and most forms of programming in general. With that said I am attempting to build a box, then randomly generate x, y, z coordinates for 100 atoms. From there, the goal is to calculate the distance between each atom and perform some math on the distance result. Below is my code. Even though n is defined as 100, and will print '100', when I print cx I only get 20 results.

    program energytot
    implicit none

    integer :: i, n, j, seed(12), k, m
    double precision:: sigma, r, epsilon, lx, ly, lz
    double precision, dimension(:), allocatable :: cx, cy, cz, dx, dy, dz, x, y, z, LJx, LJy, LJz
    allocate(x(n), y(n), z(n), LJx(n), LJy(n), LJz(n), dx(n), dy(n), dz(n))
    n = 100 !Number of molecules inside the box
    sigma = 4.1
    epsilon = 1.7
    !Box length with respect to the axis
    lx = 15
    ly = 15
    lz = 15

    do i=1,12
        seed(i)=1+3
    end do
    !generate n random numbers for x, y, z
    call RANDOM_SEED(PUT = seed)
    call random_number(x)
    call random_number(y)
    call random_number(z)
    !convert random numbers into x, y, z coordinates with (0,0,0) as the central point
    cx = ((2*x)-1)*(lx*0.5)
    cy = ((2*y)-1)*(lx*0.5)
    cz = ((2*z)-1)*(lz*0.5)

    do j=1,n-1
        do k=j+1,n
            dx = ABS((cx(j) - cx(j+1)))
            LJx = 4 * epsilon * ((sigma/dx(j))**12 - (sigma/dx(j))**6)
            dy = ABS((cy(j) - cy(j+1)))
            LJy = 4 * epsilon * ((sigma/dy(j))**12 - (sigma/dy(j))**6)
            dz = ABS((cz(j) - cz(j+1)))
            LJz = 4 * epsilon * ((sigma/dz(j))**12 - (sigma/dz(j))**6)
        end do
    end do
    print*,cx
    print*,x
    end program energytot

推荐答案

您声明了cx(以及cycz)allocatable,但是没有为它们分配空间.此外,在为变量n分配值之前,可以将其用作要为其他allocatable分配的元素数.为什么其中的任何一个甚至都需要首先动态分配?

You declare cx (and cy and cz) allocatable, but you do not allocate space for them. Moreover, before you assign a value to variable n, you use it as the number of elements to allocate for your other allocatables. Why do any of those even need to be dynamically allocated in the first place?

我将替换此代码...

I would replace this code ...

    integer :: i, n, j, seed(12), k, m
    double precision:: sigma, r, epsilon, lx, ly, lz
    double precision, dimension(:), allocatable :: cx, cy, cz, dx, dy, dz, x, y, z, LJx, LJy, LJz
    allocate(x(n), y(n), z(n), LJx(n), LJy(n), LJz(n), dx(n), dy(n), dz(n))
    n = 100 !Number of molecules inside the box

...与此:

    integer, parameter :: n = 100
    integer :: i, j, seed(12), k, m
    double precision :: sigma, r, epsilon, lx, ly, lz
    double precision, dimension(n) :: cx, cy, cz, dx, dy, dz, x, y, z, LJx, LJy, LJz

我还观察到在计算距离的循环中,您遍历了变量k,但是没有使用其值.结果,看起来您多次计算相同的距离.

I also observe that in the loop where you compute distances, you loop over variable k, but you do not use its value. As a result, it looks like you compute the same distances many times over.

这篇关于FORTRAN变量输出不等于输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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