@@ -663,7 +663,7 @@ subroutine evolve_until(id, delta_t, ierr)
663663 real (dp), intent (in ) :: delta_t
664664 integer , intent (out ) :: ierr
665665 type (star_info), pointer :: s
666- integer :: result, count
666+ integer :: result, count_calls_to_evolve_one_step
667667 logical ,parameter :: dbg= .false.
668668 real (dp) :: end_time, dt_save
669669
@@ -681,14 +681,28 @@ subroutine evolve_until(id, delta_t, ierr)
681681 ! End time of evolution
682682 end_time = s% star_age + delta_t
683683
684- ! To check whether the evolve loop was entered
685- count = 0
684+ ! Counts the number of calls to evolve_one_step
685+ count_calls_to_evolve_one_step = 0
686+
687+ ! If MESA delta_t (dt_next) >= AMUSE delta_t (delta_t), only one call to
688+ ! evolve_one_step is needed (setting MESA_delta_t to AMUSE_delta_t). In this case
689+ ! after the loop count_calls_to_evolve_one_step = 1
690+ ! If MESA delta_t < AMUSE delta_t, several calls to evolve_one_step. At last step
691+ ! MESA delta_t is set so that star_age reaches end_time. In this case after the loop
692+ ! count_calls_to_evolve_one_step > 1 and MESA time_step is set to the value it was
693+ ! before being set to reach end_time.
686694
687695 ! Evolve loop while end_time not reached
688696 ! Use of epsilon(0.0d0) to avoid precision issues
689- do while (s% star_age + s% dt_next/ secyer < end_time* (1.0d0 - epsilon (0.0d0 )))
697+ do while (s% star_age < end_time* (1.0d0 - epsilon (0.0d0 )))
698+ if (s% star_age + s% dt_next/ secyer > end_time* (1.0d0 - epsilon (0.0d0 ))) then
699+ ! Last step: save the next timestep MESA would have required if it was not imposed by end_time
700+ dt_save = s% dt_next
701+
702+ ! Set the last timestep so that star_age reaches end_time
703+ s% dt_next = (end_time - s% star_age)* secyer
704+ end if
690705 call do_evolve_one_step(id, result, ierr)
691- count = count + 1
692706 if (result /= keep_going) then
693707 if (s% result_reason == result_reason_normal) then
694708 exit
@@ -698,27 +712,15 @@ subroutine evolve_until(id, delta_t, ierr)
698712 end if
699713 end if
700714 s% doing_first_model_of_run = .false.
715+ count_calls_to_evolve_one_step = count_calls_to_evolve_one_step + 1
701716 end do
702717
703- ! Save the next timestep MESA would have required if it was not imposed by end_time
704- dt_save = s% dt_next
705-
706- ! Set the timestep required to reach end_time
707- s% dt_next = (end_time - s% star_age)* secyer
708-
709- ! Evolve one last step to reach end_time
710- call do_evolve_one_step(id, result, ierr)
711- if (result /= keep_going) then
712- if (s% result_reason /= result_reason_normal) then
713- ierr = - 1
714- end if
715- end if
716-
717- ! If several evolve steps were performed, set the timestep for the future evolution
718- ! to the value it would have reached without the condition to finish at end_time
719- if (count /= 0 ) then
718+ ! In case evolve_until is called several times subsequently.
719+ ! If there was more than one call to evolve_one_step, sets the next timestep
720+ ! to the value it was before it was set for star_age to reach end_time.
721+ if (count_calls_to_evolve_one_step > 1 ) then
720722 s% dt_next = dt_save
721- endif
723+ end if
722724
723725 s% doing_first_model_of_run = .false.
724726
0 commit comments