Raj Kumar Paudel
Raj Kumar Paudel

Reputation: 121

I have problem translating C code in fortran

for(i=1;i<=ntype;i++)
        for(cnt=0,j=1;j<=nbeta[i];j++) {
        cnt++;
        pos[i][cnt]=j;
        if (lll[i][j]==0)  {
         for(kk=1;kk<=kkbeta[i];kk++)
                bfunc[i][cnt][kk]=bfunctmp[i][j][kk];
                llltmp[i][cnt]=lll[i][j];
        } // if
        if (lll[i][j]>0)  {
         for(kk=1;kk<=kkbeta[i];kk++)
                bfunc[i][cnt][kk]=0.5*(bfunctmp[i][j  ][kk]+
                                       bfunctmp[i][j+1][kk]);
                llltmp[i][cnt]=lll[i][j];
         j++;
        } // if
        } // j,i

!my translated fortran code is

     do i =1,ntype
         cnt =0
         do j =1,nbeta(i)
             cnt = cnt+1
             pos(i,cnt) =j
             
             if(lll(i,j) ==0) then
                 do kk =1, kkbeta(i)
                     bfunc(i,cnt,kk)= bfunctmp(i,j,kk)
                 end do !end kk
                 llltmp(i,cnt) =lll(i,j)
!                 write(*,*)i,j,lll(i,j)
!                 write(*,*)i,cnt,llltmp(i,cnt)  ! '(5x,3I5)'
             end if

    !         cnt1 =j 
             if(lll(i,j) > 0) then 
                 do kk =1, kkbeta(i)
                     bfunc(i,cnt,kk)= 0.5 *(bfunctmp(i,j,kk) + bfunctmp(i,j+1,kk))
                 end do !end kk
                 llltmp(i,cnt) =lll(i,j)
               j =j+1
             end if
             
         end do !end j
     end do !end i

! A do-variable within a DO body shall not appear in a variable definition context. [j]
I am looking forward to solve this issue soon. Thank you!

Upvotes: 1

Views: 95

Answers (1)

John Bollinger
John Bollinger

Reputation: 180181

As @TomKarzes remarked in comments, you may not assign to the control variable of a DO loop within the body of that loop. Sometimes you can resolve such issues by using a different variable, instead. In your code, however, the objective of the j=j+1 appears to be to skip a loop iteration, and you simply cannot do that in a DO loop.

You can, however, replace your DO loop with a DO WHILE loop:

      j = 1
      do while (j <= nbeta(i))
!       ...

        if (lll(i,j) > 0) then
!           ...
            j = j + 1
        end if

        j = j + 1
      end do

That should work for your particular code, but you must take considerable care when making such a transformation. There are at least these considerations that must be accounted for:

  • In an iterated DO loop, the loop variable is automatically incremented after each execution of the loop body, even if execution of the body terminates early by execution of a CYCLE statement. In a DO WHILE loop, however, variables are modified only by statements that are executed, so a CYCLE will not, itself, cause any variable to be updated.

  • The number of iterations for an iterated DO loop to perform is computed before the first iteration is performed, based on data (such as the value of nbeta(i)) gathered when execution reaches the DO statement. That number of iterations can be changed only by prematurely discontinuing the loop via an EXIT, RETURN, GOTO, or STOP statement, or by executing a procedure that causes the program to terminate. On the other hand, the condition of a DO WHILE is evaluated before every iteration.

Upvotes: 4

Related Questions