From dc0a4b4bcad481de8cc815a08efc66612a7ab43f Mon Sep 17 00:00:00 2001
From: Jean-Baptiste Keck <Jean-Baptiste.Keck@imag.fr>
Date: Thu, 15 Mar 2018 14:03:31 +0100
Subject: [PATCH] cleaned all examples

---
 examples/analytic/analytic.py                 | 35 +++++++++-------
 examples/scalar_advection/scalar_advection.py |  6 ++-
 examples/scalar_diffusion/scalar_diffusion.py | 41 +++++++++++--------
 examples/taylor_green/taylor_green.py         |  2 +-
 hysop/simulation.py                           |  2 +-
 5 files changed, 53 insertions(+), 33 deletions(-)

diff --git a/examples/analytic/analytic.py b/examples/analytic/analytic.py
index 920ae3d12..f5fc31428 100755
--- a/examples/analytic/analytic.py
+++ b/examples/analytic/analytic.py
@@ -6,9 +6,9 @@ def compute(args):
     '''
     HySoP Analytic Example: Initialize a field with a space and time dependent analytic formula.
     '''
-    from hysop import IO, Field, Box, MPIParams, \
+    from hysop import Field, Box, MPIParams, \
                       Simulation, Problem, ScalarParameter
-    from hysop.constants import Implementation, AutotunerFlags
+    from hysop.constants import Implementation
     from hysop.operators import AnalyticField
     
     # Define domain
@@ -18,10 +18,15 @@ def compute(args):
     # Define parameters and field (time and analytic field)
     t      = ScalarParameter('t', dtype=args.dtype)
     scalar = Field(domain=box, name='S0', nb_components=1, dtype=args.dtype)
+                
+    # We need to first get default MPI parameters (even for non MPI jobs)
+    # so we use default domain communicator and task.
+    mpi_params = MPIParams(comm=box.task_comm,
+                           task_id=box.current_task())
     
     # Setup implementation specific variables
     impl = args.impl
-    op_kwds = {}
+    op_kwds = {'mpi_params': mpi_params}
     if (impl is Implementation.PYTHON):
         # Setup python specific extra operator keyword arguments
         # (mapping: variable name => variable value)
@@ -29,11 +34,6 @@ def compute(args):
     elif (impl is Implementation.OPENCL_CODEGEN):
         # For the OpenCL implementation we need to setup the compute device
         # and configure how the code is generated and compiled at runtime.
-                
-        # We need to first create MPI parameters to initialize an OpenCL environment
-        # (even for non MPI jobs) so we use default domain communicator and task.
-        mpi_params = MPIParams(comm=box.task_comm,
-                               task_id=box.current_task())
         
         # Create an explicit OpenCL context from user parameters
         from hysop.backend.device.opencl.opencl_tools import get_or_create_opencl_env
@@ -44,7 +44,6 @@ def compute(args):
         method = {OpenClKernelConfig: args.opencl_kernel_config}
         
         # Setup opencl specific extra operator keyword arguments
-        op_kwds['mpi_params'] = mpi_params
         op_kwds['cl_env']     = cl_env
         op_kwds['method']     = method
     else:
@@ -64,8 +63,10 @@ def compute(args):
                 data[0][...] *= np.cos(x-t())
     elif (impl is Implementation.OPENCL_CODEGEN):
         # With the opencl codegen implementation we use a symbolic expression
-        # generated using sympy. OpenCL code will be automatically generated.
-        # For a vector field, multiple expressions should be given as a tuple.
+        # generated using sympy. OpenCL code will be automatically generated,
+        # compiled and passed trough a runtime parameter autotuner to achieve
+        # maximal performances on a variety of different devices.
+        # For a vector field, multiple symbolic expressions should be given as a tuple.
         # Here xs and ts are the symbolic variables representing coordinates and time.
         xs = box.frame.coords
         ts = t.s
@@ -89,15 +90,21 @@ def compute(args):
     problem.insert(analytic)
     problem.build()
 
-    # If a visu_rank was provided, display the graph on the given process rank.
-    problem.display(args.visu_rank)
+    # If a visu_rank was provided, and show_graph was set,
+    # display the graph on the given process rank.
+    if args.display_graph:
+        problem.display(args.visu_rank)
 
     # Create a simulation and solve the problem 
     # (do not forget to specify the time parameter here)
     simu = Simulation(start=args.tstart, end=args.tend, 
                       nb_iter=args.nb_iter, dt0=args.dt, 
                       t=t)
-    problem.solve(simu)
+    
+    # Finally solve the problem 
+    problem.solve(simu, dry_run=args.dry_run)
+    
+    # Finalize
     problem.finalize()
 
 
diff --git a/examples/scalar_advection/scalar_advection.py b/examples/scalar_advection/scalar_advection.py
index ac15b61a4..333d3e1ff 100644
--- a/examples/scalar_advection/scalar_advection.py
+++ b/examples/scalar_advection/scalar_advection.py
@@ -111,11 +111,15 @@ def compute(args):
     dt0  = (args.cfl*dx)/Vinf
     if (args.dt is not None):
         dt0 = min(args.dt, dt0)
+    dt0  = 0.99*dt0
     
     # Create a simulation and solve the problem 
     # (do not forget to specify the dt parameter here)
     simu = Simulation(start=args.tstart, end=args.tend, 
-                        dt0=dt0, dt=dt)
+                      nb_iter=args.nb_iter,
+                      max_iter=args.max_iter,
+                      times_of_interest=args.dump_times,
+                      dt=dt, dt0=dt0)
     
     # Finally solve the problem 
     problem.solve(simu, dry_run=args.dry_run)
diff --git a/examples/scalar_diffusion/scalar_diffusion.py b/examples/scalar_diffusion/scalar_diffusion.py
index 1ed136bda..497224c44 100644
--- a/examples/scalar_diffusion/scalar_diffusion.py
+++ b/examples/scalar_diffusion/scalar_diffusion.py
@@ -12,16 +12,16 @@ def compute(args):
                       Simulation, Problem, ScalarParameter, \
                       MPIParams
 
-    from hysop.constants import Implementation
+    from hysop.constants import Implementation, ComputeGranularity
     from hysop.operator.directional.diffusion_dir import DirectionalDiffusion
 
     from hysop.methods import StrangOrder, TimeIntegrator, SpaceDiscretization
     from hysop.numerics.splitting.strang import StrangSplitting
-    from hysop.numerics.odesolvers.runge_kutta import Euler, RK2, RK3, RK4
     
     # Define domain
+    dim  = args.ndim
     npts = args.npts
-    box  = Box(origin=args.box_origin, length=args.box_length, dim=args.ndim)
+    box  = Box(origin=args.box_origin, length=args.box_length, dim=dim)
     
     # Get default MPI Parameters from domain (even for serial jobs)
     mpi_params = MPIParams(comm=box.task_comm,
@@ -33,11 +33,15 @@ def compute(args):
     scalar = Field(domain=box, name='S0', nb_components=1, dtype=args.dtype)
     
     # Diffusion operator discretization parameters
-    method = {TimeIntegrator: Euler, SpaceDiscretization: 2}
+    method = { 
+               ComputeGranularity:    args.compute_granularity,
+               SpaceDiscretization:   args.fd_order,
+               TimeIntegrator:        args.time_integrator,
+    }
     
     # Setup implementation specific variables
     impl = args.impl
-    extra_op_kwds = {}
+    extra_op_kwds = { 'mpi_params': mpi_params }
     if (impl is Implementation.PYTHON):
         pass
     elif (impl is Implementation.OPENCL_CODEGEN):
@@ -51,10 +55,10 @@ def compute(args):
         
         # Configure OpenCL kernel generation and tuning (already done by HysopArgParser)
         from hysop.methods import OpenClKernelConfig
-        method = {OpenClKernelConfig: args.opencl_kernel_config}
+        method[OpenClKernelConfig] = args.opencl_kernel_config
         
         # Setup opencl specific extra operator keyword arguments
-        extra_op_kwds['cl_env']     = cl_env
+        extra_op_kwds['cl_env'] = cl_env
     else:
         msg='Unknown implementation \'{}\'.'.format(impl)
         raise ValueError(msg)
@@ -64,12 +68,10 @@ def compute(args):
                 name='diffusion', dt=dt,
                 fields=scalar, coeffs=nu,
                 variables = {scalar: npts},
-                method=method, mpi_params=mpi_params,
                 **extra_op_kwds)
     
     # Build the directional splitting operator graph
-    splitting = StrangSplitting(splitting_dim=box.dim, 
-                    order=StrangOrder.STRANG_SECOND_ORDER)
+    splitting = StrangSplitting(splitting_dim=dim, order=args.strang_order)
     splitting.push_operators(diffusion)
     
     # Create the problem we want to solve and insert our 
@@ -80,8 +82,10 @@ def compute(args):
     problem.dump_inputs(fields=scalar, filename='S0', frequency=args.dump_freq)
     problem.build()
     
-    # If a visu_rank was provided, display the graph on the given process rank.
-    problem.display(args.visu_rank)
+    # If a visu_rank was provided, and show_graph was set,
+    # display the graph on the given process rank.
+    if args.display_graph:
+        problem.display(args.visu_rank)
     
     # Initialize discrete scalar field
     dfields = problem.input_discrete_fields
@@ -90,9 +94,14 @@ def compute(args):
     # Create a simulation and solve the problem 
     # (do not forget to specify the dt parameter here)
     simu = Simulation(start=args.tstart, end=args.tend, 
-                        dt0=args.dt, nb_iter=args.nb_iter,
-                        dt=dt)
-    problem.solve(simu)
+                      nb_iter=args.nb_iter, max_iter=args.max_iter,
+                      times_of_interest=args.dump_times,
+                      dt=dt, dt0=args.dt)
+    
+    # Finally solve the problem 
+    problem.solve(simu, dry_run=args.dry_run)
+    
+    # Finalize
     problem.finalize()
 
 
@@ -128,7 +137,7 @@ if __name__=='__main__':
     parser = ScalarDiffusionArgParser()
 
     parser.set_defaults(box_origin=(0.0,), box_length=(1.0,), 
-                       tstart=0.0, tend=1.0, nb_iter=500,
+                       tstart=0.0, tend=1.0, nb_iter=500, 
                        dump_freq=5, nu=0.01, impl='cl')
 
     parser.run(compute)
diff --git a/examples/taylor_green/taylor_green.py b/examples/taylor_green/taylor_green.py
index 2e273335d..84c4b7fd4 100644
--- a/examples/taylor_green/taylor_green.py
+++ b/examples/taylor_green/taylor_green.py
@@ -158,7 +158,7 @@ def compute(args):
                       max_iter=args.max_iter,
                       dt0=args.dt, times_of_interest=args.dump_times,
                       t=t, dt=dt)
-    simu.write_parameters(t, dt_cfl, dt_advec, dt, enstrophy,  #TODO dt_stretch
+    simu.write_parameters(t, dt_cfl, dt_advec, dt, enstrophy,
             filename='parameters.txt', precision=6)
     
     # Initialize only the vorticity
diff --git a/hysop/simulation.py b/hysop/simulation.py
index 28ce87f9f..74fc8b7da 100644
--- a/hysop/simulation.py
+++ b/hysop/simulation.py
@@ -146,7 +146,7 @@ class Simulation(object):
         
         # backup initial time step, required to reset simulation.
         self.max_iter = first_not_None(max_iter, 1e9)
-        assert max_iter >= nb_iter
+        assert self.max_iter >= nb_iter
         # Starting time for the current iteration
         if (t is None):
             self.t = ScalarParameter(name='t', dtype=HYSOP_REAL, 
-- 
GitLab