From 810fad5fd10c0836752041d56061b39c1781083f Mon Sep 17 00:00:00 2001
From: lucieb92 <lucie.bakels@univie.ac.at>
Date: Fri, 12 Jul 2024 15:47:02 +0200
Subject: [PATCH] temporary fix for slow performence due to keeping memory
 footprint small. A flag in the par_mod can switch off the overwriting of
 terminated particles

---
 src/initialise_mod.f90 | 12 ++++++------
 src/par_mod.f90        |  3 +++
 src/particle_mod.f90   |  4 ++--
 3 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/initialise_mod.f90 b/src/initialise_mod.f90
index 9a1cd6d4..052f070f 100644
--- a/src/initialise_mod.f90
+++ b/src/initialise_mod.f90
@@ -128,7 +128,7 @@ subroutine releaseparticles(itime)
   !***************************************************************************
   ! First allocate all particles that are going to be in the simulation
   ! If ipin==0,1, and ipout==0, then dead particles can be overwritten to save memory
-  if (ipin.gt.1 .or. ipout.ne.0) then
+  if (ipin.gt.1 .or. ipout.ne.0 .or. ispeed.eq.1) then
     if (itime.eq.0) then
       totpart=0
       do i=1,numpoint
@@ -145,7 +145,7 @@ subroutine releaseparticles(itime)
   endif
 
   call get_totalpart_num(istart)
-  if (ipin.le.1 .and. ipout.eq.0) call rewrite_iterm()
+  if (ipin.le.1 .and. ipout.eq.0 .and. ispeed.eq.0) call rewrite_iterm()
   minpart=1
   do i=1,numpoint
     if ((itime.ge.ireleasestart(i)).and. &! are we within release interval?
@@ -212,7 +212,7 @@ subroutine releaseparticles(itime)
       yaux=ypoint2(i)-ypoint1(i)
       zaux=zpoint2(i)-zpoint1(i)
 
-      if (ipin.le.1 .and. ipout.eq.0) then
+      if (ipin.le.1 .and. ipout.eq.0 .and. ispeed.eq.0 ) then
         call rewrite_iterm()
         totpart = numrel-count%iterm_max
         if (totpart.gt.0) call alloc_particles(totpart)
@@ -286,7 +286,7 @@ subroutine releaseparticles(itime)
     endif ! releasepoint
   end do ! numpoint
 
-  if (ipin.le.1 .and. ipout.eq.0) call rewrite_iterm()
+  if (ipin.le.1 .and. ipout.eq.0 .and. ispeed.eq.0 ) call rewrite_iterm()
 
   call get_totalpart_num(iend)
 
@@ -1418,7 +1418,7 @@ subroutine boundcond_domainfill(itime,loutend)
   ! Terminate trajectories that have left the domain, if domain-filling
   ! trajectory calculation domain is not global
   !********************************************************************
-  if (ipin.le.1 .and. ipout.eq.0) call rewrite_iterm()
+  if (ipin.le.1 .and. ipout.eq.0 .and. ispeed.eq.0) call rewrite_iterm()
 
   iterminate=0
   do i=1,count%allocated
@@ -1875,7 +1875,7 @@ subroutine boundcond_domainfill(itime,loutend)
   end do ! north south
 ! !$OMP END DO
 ! !$OMP END PARALLEL
-  if (ipin.le.1 .and. ipout.eq.0) call rewrite_iterm()
+  if (ipin.le.1 .and. ipout.eq.0 .and. ispeed.eq.0) call rewrite_iterm()
   numparticlecount = numparticlecount_tmp
   ! If particles shall be dumped, then accumulated masses at the domain boundaries
   ! must be dumped, too, to be used for later runs
diff --git a/src/par_mod.f90 b/src/par_mod.f90
index 53b482f8..aad5b842 100644
--- a/src/par_mod.f90
+++ b/src/par_mod.f90
@@ -311,4 +311,7 @@ module par_mod
   ! Particle dump: one file per field or all in one netcdf file
   logical,parameter :: lpartoutputperfield=.false.
 
+  ! To save memory, terminated particles can be overwritted, but this can cause overhead.
+  ! set ispeed=1 to decrease this overhead, but increasing the memory footprint
+  integer,parameter :: ispeed=0
 end module par_mod
diff --git a/src/particle_mod.f90 b/src/particle_mod.f90
index 35363566..c03f6d82 100644
--- a/src/particle_mod.f90
+++ b/src/particle_mod.f90
@@ -13,7 +13,7 @@
 module particle_mod
   use com_mod, only: maxspec,DRYDEP,WETDEP,DRYBKDEP,WETBKDEP,iout, &
     n_average,nspec,ipout,ipin
-  use par_mod, only: dp
+  use par_mod, only: dp,ispeed
 
   implicit none
   
@@ -395,7 +395,7 @@ contains
 
     count%alive=j-1
 
-    if (ipin.le.1 .and. ipout.eq.0) call rewrite_iterm
+    if (ipin.le.1 .and. ipout.eq.0 .and. ispeed.eq.0) call rewrite_iterm
   end subroutine rewrite_ialive
 
   subroutine rewrite_iterm()
-- 
GitLab