Fredh Rodrigues
Fredh Rodrigues

Reputation: 21

Call a function / subroutine only once every X iterations in Fortran

I have a fortran code where, amongst other things, I put some values like temperature and call a subroutine.

This subroutine gives me back a radiation solution that is then used for new iterations.

The problem is that this subroutine takes a lot of time to process, so I wish to call it only once every, say, twenty iterations and keep the last solution for the rest of the program until then.

Is there a viable way of doing this?

Upvotes: 0

Views: 357

Answers (2)

agentp
agentp

Reputation: 6989

another approach is to use nested loops:

       do i = 1,bignum
         do j = i,20
            code to be repeated many times
         end do
         call subroutine_to_be_called_ocasionally()
       end do

Upvotes: 0

Simply do just

  n_iterations = 100000
  nth = 20

  do i = 1, n_iterations
    if (mod(i, nth)==0) call my_subroutine
  end do

expression mod(i, nth)==0 is true only if i can be divided by nth with zero remainder.

Upvotes: 1

Related Questions