From 555c3a2f8de6b24334bb812c677ac213c7d66eb6 Mon Sep 17 00:00:00 2001 From: Thomas Gastine Date: Tue, 30 Jul 2024 11:18:27 +0200 Subject: [PATCH] cleanups in get_hit_times/l_correct_step Make use of vectorized functions any/where instead of explicit loops --- src/horizontal.f90 | 8 ++++---- src/preCalculations.f90 | 18 ++++-------------- src/useful.f90 | 10 +--------- 3 files changed, 9 insertions(+), 27 deletions(-) diff --git a/src/horizontal.f90 b/src/horizontal.f90 index 642bdc3b..b1633ec9 100644 --- a/src/horizontal.f90 +++ b/src/horizontal.f90 @@ -133,15 +133,15 @@ subroutine horizontal() ! Gauss-Legendre integration of the plms: call gauleg(-one,one,theta_ord,tmp_gauss,n_theta_max) + ! Get dP for all degrees and order m=0 at the equator only + ! Usefull to estimate the flow velocity at the equator + call plm_theta(half*pi,l_max,0,0,minc,Pl0Eq,dPl0Eq,l_max+1,norm) + !-- Legendre polynomials and cos(theta) derivative: do n_theta=1,n_theta_max/2 ! Loop over colat in NHS colat=theta_ord(n_theta) - ! Get dP for all degrees and order m=0 at the equator only - ! Usefull to estimate the flow velocity at the equator - call plm_theta(half*pi,l_max,0,0,minc,Pl0Eq,dPl0Eq,l_max+1,norm) - if ( l_scramble_theta ) then O_sin_theta(2*n_theta-1) =one/sin(colat) O_sin_theta(2*n_theta ) =one/sin(colat) diff --git a/src/preCalculations.f90 b/src/preCalculations.f90 index 74391df0..c51b710a 100644 --- a/src/preCalculations.f90 +++ b/src/preCalculations.f90 @@ -886,9 +886,8 @@ subroutine get_hit_times(t,t_start,t_stop,dt,n_tot,n_step,string,time,& !-- Local variables: logical :: l_t real(cp) :: tmp(size(t)) - integer :: n, n_t_max, n_t + integer :: n, n_t - n_t_max = size(t) t_start=t_start/tScale t_stop =t_stop/tScale dt =dt/tScale @@ -905,13 +904,8 @@ subroutine get_hit_times(t,t_start,t_stop,dt,n_tot,n_step,string,time,& !-- Check whether any time is given explicitly: l_t=.false. n_t=0 - do n=1,n_t_max - if ( t(n) >= 0.0_cp ) then - t(n)=t(n)/tScale - l_t=.true. - n_t=n_t+1 - end if - end do + where ( t >= 0.0_cp ) t=t/tScale + if ( maxval(t(:)) >= 0.0_cp ) l_t=.true.; n_t=count(t(:)>=0.0) !-- Properly reallocate at the right size if ( l_t ) then @@ -938,11 +932,7 @@ subroutine get_hit_times(t,t_start,t_stop,dt,n_tot,n_step,string,time,& deallocate(t) ! Deallocate to get the proper size allocate(t(n_t)) - t(1)=t_start - do n=2,n_t - t(n)=t(n-1)+dt - end do - + t(:) = [(t_start+(n-1)*dt, n=1,n_t)] end if !-- In case only n_step or n_tot is specified such that l_t=.false. diff --git a/src/useful.f90 b/src/useful.f90 index f7114390..65a0e8de 100644 --- a/src/useful.f90 +++ b/src/useful.f90 @@ -46,7 +46,6 @@ logical function l_correct_step(n,t,t_last,n_max,n_step,n_intervals, & !-- Local variables: integer :: n_offset ! offset with no action - integer :: n_t ! counter for times integer :: n_steps ! local step width if ( n_step /= 0 .and. n_intervals /= 0 ) then @@ -56,11 +55,9 @@ logical function l_correct_step(n,t,t_last,n_max,n_step,n_intervals, & end if l_correct_step=.false. - if ( n_intervals /= 0 ) then n_steps=n_max/n_intervals n_offset=n_max-n_steps*n_intervals - if ( n > n_offset .and. mod(n-n_offset,n_steps) == 0 ) l_correct_step=.true. else if ( n_step /= 0 ) then if ( n == n_max .or. mod(n,n_step) == 0 ) l_correct_step=.true. @@ -74,12 +71,7 @@ logical function l_correct_step(n,t,t_last,n_max,n_step,n_intervals, & end if else if ( size(times) > 1 ) then !-- Time array contains multiple entries - do n_t=1,size(times) - if ( times(n_t) < t .and. times(n_t) >= t_last ) then - l_correct_step=.true. - exit - end if - end do + l_correct_step=any((times(:) >= t_last) .and. (times(:) < t), dim=1) end if end function l_correct_step