From 4f0c310e75818eac419f14e8d9e9ee5d27f12492 Mon Sep 17 00:00:00 2001
From: Jean-Matthieu Etancelin <jean-matthieu.etancelin@univ-reims.fr>
Date: Thu, 12 Mar 2015 21:44:02 +0100
Subject: [PATCH] Fix RMI example

---
 Examples/RMI/RMI_hybrid.py | 88 ++++++++++++++++++++++----------------
 1 file changed, 51 insertions(+), 37 deletions(-)

diff --git a/Examples/RMI/RMI_hybrid.py b/Examples/RMI/RMI_hybrid.py
index c64c5ddb2..223e7d874 100644
--- a/Examples/RMI/RMI_hybrid.py
+++ b/Examples/RMI/RMI_hybrid.py
@@ -6,6 +6,12 @@
 # 4. Is the initial condition is perturbed
 # 5. Is data output
 import sys
+msg = """
+This example computes a two-phase flow.
+Argments must be given in command line. For example:
+mpirun -np 9 python RMI_hybrid.py "[129,129,257]" "[129,129,257]" "{0:0}" "True" "True"
+"""
+assert len(sys.argv) == 6, msg
 USER_NB_ELEM_UW = eval(sys.argv[1])
 USER_NB_ELEM_S = eval(sys.argv[2])
 USER_RANK_DEVICE_ID = eval(sys.argv[3])
@@ -21,7 +27,7 @@ from hysop.problem.simulation import Simulation
 from hysop.fields.variable_parameter import VariableParameter
 from hysop.methods_keys import TimeIntegrator, Interpolation, Remesh,\
     Support, Splitting, MultiScale, MultiScale, SpaceDiscretisation, \
-    GhostUpdate, Scales, dtCrit, ExtraArgs
+    Scales, dtCrit, ExtraArgs
 from hysop.numerics.integrators.runge_kutta2 import RK2 as RK
 from hysop.numerics.integrators.runge_kutta3 import RK3 as RK3
 from hysop.numerics.finite_differences import FD_C_4
@@ -33,7 +39,6 @@ from hysop.operator.stretching import Stretching
 from hysop.operator.baroclinic import Baroclinic
 from hysop.operator.penalization import PenalizeVorticity
 from hysop.operator.poisson import Poisson
-from hysop.operator.differential import Curl
 from hysop.operator.adapt_timestep import AdaptTimeStep
 from hysop.operator.custom import CustomMonitor, CustomOp
 from hysop.operator.redistribute_inter import RedistributeInter
@@ -43,6 +48,9 @@ from hysop.domain.subsets import SubBox
 from hysop.operator.hdf_io import HDF_Writer
 from hysop.operator.energy_enstrophy import EnergyEnstrophy
 import hysop.tools.numpywrappers as npw
+from hysop.tools.profiler import Profiler
+#from hysop.tools.io_utils import IO
+#IO.set_default_path("/scratch_p/jmetancelin")
 pi = np.pi
 cos = np.cos
 sin = np.sin
@@ -50,7 +58,7 @@ exp = np.exp
 abs = np.abs
 tanh = np.tanh
 
-
+assert main_size > 1, "This example must run with at least 2 process"
 TASK_UW = 1
 TASK_SCALAR = 2
 PROC_TASKS = [TASK_UW, ] * main_size
@@ -67,12 +75,7 @@ VISCOSITY = 1e-4
 AMPLITUDE = 0.02
 PERIOD = pi
 SIGMA = 0.1
-NOISE = 0.1
-
-width = 0.01
-ampl3 = 0.3
-ampl = 0.05
-ampl2 = 0.05
+NOISE = 1.
 
 
 def computeVel(res, x, y, z, t):
@@ -112,16 +115,17 @@ def initScal(res, x, y, z, t):
     res[0][...] = z
     return res
 
-
-def initRho(res, x, y, z, t):
-    res[0][...] = np.tanh(z * 10.)
+def phitorho(res, a):
+    res[0][...] = np.tanh(a * 10.)
     res[0][...] += 2.
     return res
 
+def initRho(res, x, y, z, t):
+    return phitorho(res, z)
 
-def func_scal_to_rho(simu, f_in, f_out):
-    f_out[0].data[0][...] = np.tanh(f_in[0].data[0] * 10.) + 2.
 
+def func_scal_to_rho(simu, f_in, f_out):
+    phitorho(f_out[0].data, f_in[0].data[0])
 
 
 temp_maxvelo = npw.zeros((3, ))
@@ -139,10 +143,10 @@ def calc_maxvelo(simu, v):
 
 ctime = MPI.Wtime()
 # Domain
-box = hysop.Box(length=[2., 2., 2.], origin=[-1., -1., -1.],
+box = hysop.Box(length=[2., 2., 4.], origin=[-1., -1., -2.],
                 proc_tasks=PROC_TASKS)
-bc_b = SubBox(length=[2., 2., 0.1], origin=[-1., -1., -1.], parent=box)
-bc_t = SubBox(length=[2., 2., 0.1], origin=[-1., -1., 0.9], parent=box)
+bc_b = SubBox(length=[2., 2., 0.1], origin=[-1., -1., -2.], parent=box)
+bc_t = SubBox(length=[2., 2., 0.1], origin=[-1., -1., 1.9], parent=box)
 mpi_params = MPIParams(comm=box.comm_task, task_id=PROC_TASKS[main_rank])
 mpi_params_S = MPIParams(comm=box.comm_task, task_id=TASK_SCALAR)
 mpi_params_UW = MPIParams(comm=box.comm_task, task_id=TASK_UW)
@@ -160,7 +164,7 @@ rho = hysop.Field(domain=box, formula=initRho,
 
 data = {'dt': 0.001}
 dt = VariableParameter(data)
-simu = Simulation(tinit=0.0, tend=50., timeStep=0.001, iterMax=200)
+simu = Simulation(tinit=0.0, tend=100., timeStep=0.001, iterMax=1)
 
 # Flow discretizations:
 d_uw = Discretization(USER_NB_ELEM_UW)
@@ -241,9 +245,8 @@ dt_adapt = AdaptTimeStep(velo, vorti,
                          discretization=d_uw_ghosts,
                          method={TimeIntegrator: RK3,
                                  SpaceDiscretisation: FD_C_4,
-                                 dtCrit: ['gradU', 'cfl']},
-                         lcfl=0.15,
-                         cfl=0.5,
+                                 dtCrit: ['gradU']},
+                         lcfl=0.1,
                          io_params=dt_output,
                          mpi_params=mpi_params_UW)
 
@@ -307,48 +310,51 @@ if IS_OUTPUT:
 advecToGhost_vorti = RedistributeIntra(variables=[vorti],
                                        source=advec, target=stretch,
                                        mpi_params=mpi_params_UW)
-advecToGhost_vorti.name += '_toG_W'
+advecToGhost_vorti.name += '_advec2Ghost_W'
 fftToGhost_velo = RedistributeIntra(variables=[velo],
                                     source=poisson, target=stretch,
                                     run_till=[stretch, dt_adapt],
                                     mpi_params=mpi_params_UW)
-fftToGhost_velo.name += '_toG_V'
+fftToGhost_velo.name += '_advec2Ghost_U'
 fftToGhost_vorti = RedistributeIntra(variables=[vorti],
                                      source=poisson, target=stretch,
                                      run_till=[stretch, dt_adapt],
                                      mpi_params=mpi_params_UW)
-fftToGhost_vorti.name += '_toG_V'
+fftToGhost_vorti.name += '_fft2Ghost_W'
 fftToScales_velo = RedistributeIntra(variables=[velo],
                                      source=poisson, target=advec,
                                      mpi_params=mpi_params_UW)
-fftToScales_velo.name += '_toScales_V'
+fftToScales_velo.name += '_fft2Scales_U'
 fftToScales_vorti = RedistributeIntra(variables=[vorti],
                                       source=poisson, target=advec,
                                       mpi_params=mpi_params_UW)
-fftToScales_vorti.name += '_toScales_W'
+fftToScales_vorti.name += '_fft2Scales_W'
 ghostToFFT_vorti = RedistributeIntra(variables=[vorti],
                                      source=baroclinic, target=diffusion,
                                      mpi_params=mpi_params_UW)
+ghostToFFT_vorti.name += '_ghost2FFT_W'
 ghostToFFT_velo = RedistributeIntra(variables=[velo],
                                     source=penal, target=poisson,
                                     mpi_params=mpi_params_UW)
-ghostToFFT_vorti.name += '_FromG_W'
+ghostToFFT_velo.name += '_ghost2FFT_U'
 toDev = DataTransfer(source=topo_GPU_velo, target=advec_scal,
                      variables={velo: topo_GPU_velo},
                      mpi_params=mpi_params_S)
-toDev.name += '_ToDev_V'
+toDev.name += '_ToDev_U'
 
 redistrib = RedistributeInter(variables=[velo],
                               parent=main_comm,
                               source=topo_CPU_velo_fft, target=topo_GPU_velo,
                               source_id=TASK_UW, target_id=TASK_SCALAR,
                               run_till=[toDev])
+redistrib.name += '_CPU2GPU_U'
 redistrib_scal = RedistributeInter(variables=[scal],
                                    parent=main_comm,
                                    source=topo_GPU_velo,
                                    target=topo_CPU_velo_ghosts,
                                    source_id=TASK_SCALAR, target_id=TASK_UW,
                                    run_till=[scal_to_rho])
+redistrib_scal.name += '_GPU2CPU_Scal'
 toHost = DataTransfer(source=topo_GPU_scal, target=p_scal,
                       variables={scal: topo_GPU_scal},
                       mpi_params=mpi_params_S,
@@ -395,15 +401,17 @@ if IS_OUTPUT:
                       fftToGhost_velo, fftToScales_velo,
                       fftToScales_vorti, dt_adapt]
 else:
-    assert False
-    # operators_list = [redistrib,
-    #                   toDev, advec_scal,
-    #                   advec, toGhost_vorti, stretch,
-    #                   toHost, redistrib_scal,
-    #                   scal_to_rho, baroclinic,
-    #                   fromGhost_vorti, diffusion, poisson,
-    #                   toGhost_velo, toScales_velo, toScales_vorti]
-    #, dt_adapt]
+    operators_list = [redistrib,
+                      toDev, advec_scal,
+                      toHost,
+                      advec, advecToGhost_vorti, stretch, baroclinic,
+                      redistrib_scal,
+                      scal_to_rho, penal,
+                      ghostToFFT_velo, ghostToFFT_vorti,
+                      diffusion, poisson,
+                      fftToGhost_velo, fftToScales_velo,
+                      fftToScales_vorti, dt_adapt]
+
 
 # Fields initializations
 if box.is_on_task(TASK_SCALAR):
@@ -467,9 +475,15 @@ if IS_OUTPUT:
        simu.time != p_velo._last_written_time:
         p_velo.apply(simu)
 
+prof = Profiler(None, box.comm_task)
 for op in operators_list:
     if box.is_on_task(op.task_id()):
         op.finalize()
+        prof += op.profiler
+for v in (velo, vorti, rho, scal):
+    prof += v.profiler
+prof.summarize()
+print prof
 
 velo.finalize()
 if box.is_on_task(TASK_SCALAR):
-- 
GitLab