【问题标题】:Fortran Compiling Error (Preconditioned Conjugate gradient method) : I tried to compile fortran code and got errorsFortran 编译错误(预条件共轭梯度法):我尝试编译 fortran 代码并出现错误
【发布时间】:2021-12-09 09:30:10
【问题描述】:

我尝试编译以下代码并得到以下错误,伙计们可以 你帮我解决这个错误

代码:

    DIMENSION x(*),b(*),g(*),h(*),cg(*)
    DIMENSION aa(*),ja(*),ia(*),al(*)
C    
    DO 1 K=1, kMax
C    
        IF (k.EQ.1) THEN
            CALL IAmul(n,aa,ia,ja,x,h)
            DO 2 i=1,n 
                cg(i)=b(i)-h(i)
                g(i)=cg(i)
2           CONTINUE
            CAll ISolvSys(n,g,al,ia,ja)
            gcg=0.
            DO 3 i=1,n 
                gcg=gcg+g(i)*cg(i)
                h(i)=g(i)
3           CONTINUE
        ENDIF
C    
        CALL IAmul (n,aa,ia,ja,h,b)
        hAh=0.
        DO 10 i=1,n 
            hAh=hAh+h(i)*b(i)
10      CONTINUE
        ro=gcg/hAh
        DO 11 i=1,n 
            cg(i)=cg(i)-r0*h(i)
11      CONTINUE
        CALL ISolvSys(n,al,iaja)
        DO 12 i=1,n 
            x(i)=x(i)+ro*h(i)
            g(i)=g(i)-ro*b(i)
12      CONTINUE
        gamma=gcg
        gcg=0 
        DO 13 i=1,n 
            gcg=gcg+g(i)*cg(i)
13      CONTINUE
C 
C 
        write(*,*)'iteration:',k, 'gcg=',gcg
        IF(k.EQ.1) eps=eps*gcg
        IF (gcg.LT.eps) RETURN
        gamma=gcg/gamma 
        DO 14 i=1,n 
            h(i)=g(i)+gamma*h(i)
14      CONTINUE
C 
C
1   CONTINUE
    END

错误:

      6 |         DO i=1,n
          |                 2
    ......
       12 |         DO i=1,n
          |                 1
    Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2)
    <span class="error_line" onclick="ide.gotoLine('main.f95',16)">main.f95:16:7</span>:
       16 |     ENDIF
          |       1
    Error: Expecting END DO statement at (1)
    <span class="error_line" onclick="ide.gotoLine('main.f95',20)">main.f95:20:13</span>:
       12 |         DO i=1,n
          |                 2
    ......
       20 |     DO i=1,n
          |             1
    Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2)
    <span class="error_line" onclick="ide.gotoLine('main.f95',24)">main.f95:24:13</span>:
       20 |     DO i=1,n
          |             2
    ......
       24 |     DO i=1,n
          |             1
    Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2)
    <span class="error_line" onclick="ide.gotoLine('main.f95',28)">main.f95:28:13</span>:
       24 |     DO i=1,n
          |             2
    ......
       28 |     DO i=1,n
          |             1
    Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2)
    <span class="error_line" onclick="ide.gotoLine('main.f95',34)">main.f95:34:13</span>:
       28 |     DO i=1,n
          |             2
    ......
       34 |     DO i=1,n
          |             1
    Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2)
    <span class="error_line" onclick="ide.gotoLine('main.f95',41)">main.f95:41:13</span>:
       34 |     DO i=1,n
          |             2
    ......
       41 |     DO i=1,n
          |             1
    Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2)
    <span class="error_line" onclick="ide.gotoLine('main.f95',45)">main.f95:45:3</span>:
       45 | END
          |   1
    Error: END DO statement expected at (1)
    f951: Error: Unexpected end of file in ‘main.f95’





   

 6 |         DO i=1,n
      |                 2
......

   12 |         DO i=1,n
      |                 1
Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2)
<span class="error_line" onclick="ide.gotoLine('main.f95',16)">main.f95:16:7</span>:
   16 |     ENDIF
      |       1
Error: Expecting END DO statement at (1)
<span class="error_line" onclick="ide.gotoLine('main.f95',20)">main.f95:20:13</span>:
   12 |         DO i=1,n
      |                 2
......
   20 |     DO i=1,n
      |             1
Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2)
<span class="error_line" onclick="ide.gotoLine('main.f95',24)">main.f95:24:13</span>:
   20 |     DO i=1,n
      |             2
......
   24 |     DO i=1,n
      |             1
Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2)
<span class="error_line" onclick="ide.gotoLine('main.f95',28)">main.f95:28:13</span>:
   24 |     DO i=1,n
      |             2
......
   28 |     DO i=1,n
      |             1
Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2)
<span class="error_line" onclick="ide.gotoLine('main.f95',34)">main.f95:34:13</span>:
   28 |     DO i=1,n
      |             2
......
   34 |     DO i=1,n
      |             1
Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2)
<span class="error_line" onclick="ide.gotoLine('main.f95',41)">main.f95:41:13</span>:
   34 |     DO i=1,n
      |             2
......
   41 |     DO i=1,n
      |             1
Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2)
<span class="error_line" onclick="ide.gotoLine('main.f95',45)">main.f95:45:3</span>:
   45 | END
      |   1
Error: END DO statement expected at (1)
f951: Error: Unexpected end of file in ‘main.f95’

如果 Fortran 专家能澄清这个问题,我将不胜感激。

【问题讨论】:

  • 很难看出您给出的错误消息与代码有何关联。无论哪种方式,我们都需要查看更多细节,而不仅仅是程序的一部分。见minimal reproducible example
  • 我必须同意@francescalus - 在您的代码示例中给出的我看不到一行代码DO i=1,n 我看不出您的错误消息与您所呈现的内容有何关系。

标签: loops compiler-errors fortran


【解决方案1】:

问题是DO 循环没有被正确终止。您的每个 DO 循环都继续前一个循环,因此编译器会说:

Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2)

https://www.tutorialspoint.com/fortran/fortran_do_loop.htm

更大的问题是您使用的是 Fortran 95 编译器,但代码不是 Fortran 95。我猜代码是 Fortran 77。请查看您的编译器手册并尝试选择 Fortran 77。

https://web.stanford.edu/class/me200c/tutorial_77/09_loops.html

【讨论】:

  • 代码片段显示的哪些功能不是 Fortran 95?显示的哪个 DO 结构没有终止? (我没有问过编译器,但我看不到一个没有正确终止的编译器,因此显示错误和更正会很有帮助。)
  • Fortran95 接近于 Fortran77 的严格超集。 Fortran 惊人地向后兼容。在显示的内容上使用现代编译器应该不是问题。
  • 固定格式坏了。当我复制代码并编译它时,代码前有四列——应该有六列。我已经更正了我的副本。现在它抱怨 DIMENSION,假设 (1) 处的大小数组必须是一个虚拟参数 (GFortran)。
  • @FrancisKing 我不认为你的回答是正确的。 OP 代码片段中的所有do 循环似乎都使用旧的do label; label continue 样式正确终止。参见例如here 这个。
  • 另外,您的声明“代码不是 Fortran 95。我猜代码是 Fortran 77”。正如 Ian Bush 上面所说,如果代码是有效的 Fortran 77(它不是),那么它也是有效的 Fortran 95(可能除了一些我不知道的非常小众的边缘情况)。
猜你喜欢
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
相关资源
最近更新 更多