From 83f045cc4bdac25ee78281dcf338fa0564d22ab5 Mon Sep 17 00:00:00 2001
From: Jean-Baptiste Keck <Jean-Baptiste.Keck@imag.fr>
Date: Thu, 15 Mar 2018 10:20:15 +0100
Subject: [PATCH] cleaned shear layer example

---
 examples/example_utils.py             |  7 +--
 examples/shear_layer/shear_layer.py   | 69 ++++++++++++++-------------
 examples/taylor_green/taylor_green.py |  6 ++-
 3 files changed, 43 insertions(+), 39 deletions(-)

diff --git a/examples/example_utils.py b/examples/example_utils.py
index c418651ae..405185213 100644
--- a/examples/example_utils.py
+++ b/examples/example_utils.py
@@ -39,7 +39,7 @@ class SplitAppendAction(argparse._AppendAction):
         elif (self._container is set):
             items.update(values)
         else:
-            msg='Unknown container type {}.'.format(_container)
+            msg='Unknown container type {}.'.format(self._container)
             raise TypeError(msg)
         setattr(namespace, self.dest, items)
 
@@ -58,6 +58,7 @@ class HysopArgParser(argparse.ArgumentParser):
     
     @classmethod
     def get_fs_type(cls, path):
+        import subprocess
         cmd = ['stat', '-f', '-c', '%T', path]
         fs_type = subprocess.check_output(cmd)
         return fs_type.replace('\n','')
@@ -292,7 +293,7 @@ class HysopArgParser(argparse.ArgumentParser):
                     help='Physical length of the box.')
             return discretization
         else:
-            msg='Unknown domain value {}.'.format(domain)
+            msg='Unknown domain value {}.'.format(self._domain)
             raise ValueError(msg)
     
     def _check_domain_args(self, args):
@@ -335,7 +336,7 @@ class HysopArgParser(argparse.ArgumentParser):
             args.box_origin = box_origin
             args.box_length = box_length
         else:
-            msg='Unknown domain value {}.'.format(domain)
+            msg='Unknown domain value {}.'.format(self._domain)
             raise ValueError(msg)
 
 
diff --git a/examples/shear_layer/shear_layer.py b/examples/shear_layer/shear_layer.py
index 88d3d3a6e..ba0cb4e00 100644
--- a/examples/shear_layer/shear_layer.py
+++ b/examples/shear_layer/shear_layer.py
@@ -3,26 +3,25 @@
 ## See Brown 1995: 
 ## Performance of under-resolved two dimensional incompressible flow simulations
 
+import sympy as sm
+import numpy as np
+
 def compute(args):
-    from hysop import IO, Domain, Field, Box, Discretization, \
-                      Simulation, Problem, IOParams, MPIParams
-    from hysop.deps import np, sm, sys
-    from hysop.symbolic import space_symbols
-    from hysop.symbolic.field import curl
-    from hysop.tools.sympy_utils import greak
-    from hysop.parameters.scalar_parameter import ScalarParameter
-    from hysop.constants import Backend, AdvectionCriteria, Implementation
-    from hysop.tools.contexts import printoptions
+    from hysop import Box, Simulation, Problem, MPIParams,\
+                      ScalarParameter
+    from hysop.tools.string_utils import vprint_banner, vprint
+    from hysop.defaults import VelocityField, VorticityField, \
+                               TimeParameters
+    from hysop.constants import Implementation, AdvectionCriteria
 
     from hysop.operators import DirectionalAdvection, DirectionalDiffusion, \
                                 PoissonRotational, AdaptiveTimeStep,        \
-                                MinMaxFieldStatistics
+                                MinMaxFieldStatistics, StrangSplitting
 
-    from hysop.methods import StrangOrder, SpaceDiscretization, Remesh, \
-                              TimeIntegrator, ComputeGranularity, Interpolation
+    from hysop.methods import SpaceDiscretization, Remesh, TimeIntegrator, \
+                              ComputeGranularity, Interpolation
 
-    from hysop.numerics.splitting.strang import StrangSplitting
-    from hysop.operators import SpaceDerivative
+    from hysop.symbolic import space_symbols
     
     # Define the domain
     dim  = args.ndim
@@ -44,8 +43,9 @@ def compute(args):
                 
         # Create an explicit OpenCL context from user parameters
         from hysop.backend.device.opencl.opencl_tools import get_or_create_opencl_env
-        cl_env = get_or_create_opencl_env(mpi_params=mpi_params, platform_id=args.cl_platform_id, 
-                                                                 device_id=args.cl_device_id)
+        cl_env = get_or_create_opencl_env(mpi_params=mpi_params, 
+                                          platform_id=args.cl_platform_id, 
+                                          device_id=args.cl_device_id)
         
         # Configure OpenCL kernel generation and tuning (already done by HysopArgParser)
         from hysop.methods import OpenClKernelConfig
@@ -76,11 +76,10 @@ def compute(args):
         data[0][np.isnan(data[0])] = 0.0
 
     # Define parameters and field (time, timestep, viscosity, velocity, vorticity)
-    t      = ScalarParameter('t',  dtype=args.dtype)
-    dt     = ScalarParameter('dt', dtype=args.dtype)
-    nu     = ScalarParameter('nu', initial_value=args.nu, const=True, dtype=args.dtype)
-    velo   = Field(domain=box, name='V', is_vector=True, dtype=args.dtype)
-    vorti  = Field(domain=box, name='W', nb_components=(1 if dim==2 else dim), dtype=args.dtype)
+    t, dt = TimeParameters(dtype=args.dtype)
+    velo  = VelocityField(domain=box, dtype=args.dtype)
+    vorti = VorticityField(domain=box, dtype=args.dtype)
+    nu    = ScalarParameter('nu', initial_value=args.nu, const=True, dtype=args.dtype)
     
     ### Build the directional operators
     #> Directional advection 
@@ -90,7 +89,6 @@ def compute(args):
             advected_fields = (vorti,),
             velocity_cfl = args.cfl,
             variables = {velo: npts, vorti: npts},
-            method = {Remesh: Remesh.L4_2},
             dt=dt, **extra_op_kwds)
     #> Directional diffusion
     diffusion = DirectionalDiffusion(implementation=impl,
@@ -106,7 +104,8 @@ def compute(args):
     ### Build standard operators
     #> Poisson operator to recover the velocity from the vorticity
     poisson = PoissonRotational(name='poisson', velocity=velo, vorticity=vorti, 
-                            variables={velo:npts, vorti: npts}, projection=None,
+                            variables={velo:npts, vorti: npts}, 
+                            projection=args.reprojection_frequency,
                             implementation=impl, **extra_op_kwds)
     #> We ask to dump the inputs and the outputs of this operator
     poisson.dump_outputs(fields=(vorti,), frequency=args.dump_freq)
@@ -143,8 +142,10 @@ def compute(args):
     problem.insert(poisson, splitting, min_max_U, min_max_W, adapt_dt)
     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
     # (do not forget to specify the t and dt parameters here)
@@ -160,11 +161,14 @@ def compute(args):
     dfields[vorti].initialize(formula=init_vorticity)
 
     # Finally solve the problem 
-    with printoptions(threshold=10000, linewidth=240, 
-                      nanstr='nan', infstr='inf', 
-                      formatter={'float': lambda x: '{:>6.2f}'.format(x)}):
+    if args.dry_run:
+        vprint()
+        vprint_banner('** Dry-run requested, skipping simulation. **')
+    else:
         problem.solve(simu)
-        problem.finalize()
+    
+    # Finalize
+    problem.finalize()
 
 
 if __name__=='__main__':
@@ -255,10 +259,7 @@ if __name__=='__main__':
     parser.set_defaults(impl='cl', ndim=2, npts=(257,),
                         box_origin=(0.0,), box_length=(1.0,), 
                         tstart=0.0, tend=1.25, 
-                        nb_iter=None, dt=1e-4,
-                        cfl=0.5, lcfl=0.125,
-                        case=0, 
-                        dump_freq=0,
-                        dump_times=(0.8, 1.20))
+                        dt=1e-4, cfl=0.5, lcfl=0.125,
+                        case=0, dump_freq=0, dump_times=(0.8, 1.20))
 
     parser.run(compute)
diff --git a/examples/taylor_green/taylor_green.py b/examples/taylor_green/taylor_green.py
index b7b15eb25..bdfb965f2 100644
--- a/examples/taylor_green/taylor_green.py
+++ b/examples/taylor_green/taylor_green.py
@@ -57,8 +57,9 @@ def compute(args):
                 
         # Create an explicit OpenCL context from user parameters
         from hysop.backend.device.opencl.opencl_tools import get_or_create_opencl_env
-        cl_env = get_or_create_opencl_env(mpi_params=mpi_params, platform_id=args.cl_platform_id, 
-                                                                 device_id=args.cl_device_id)
+        cl_env = get_or_create_opencl_env(mpi_params=mpi_params, 
+                                          platform_id=args.cl_platform_id, 
+                                          device_id=args.cl_device_id)
         
         # Configure OpenCL kernel generation and tuning (already done by HysopArgParser)
         from hysop.methods import OpenClKernelConfig
@@ -171,6 +172,7 @@ def compute(args):
     else:
         problem.solve(simu)
 
+    # Finalize
     problem.finalize()
 
 
-- 
GitLab