From 8db37ac0231a581d712fdfc7e1808643a84b28ba Mon Sep 17 00:00:00 2001
From: JM Etancelin <jean-matthieu.etancelin@univ-pau.fr>
Date: Mon, 23 Mar 2020 09:38:37 +0100
Subject: [PATCH] move Examples interface (argument parser) to hysop internal
 module. Should solve #40.

---
 examples/analytic/analytic.py                 | 91 ++++++++++---------
 examples/bubble/periodic_bubble.py            |  2 +-
 examples/bubble/periodic_bubble_levelset.py   |  2 +-
 .../periodic_bubble_levelset_penalization.py  |  2 +-
 examples/bubble/periodic_jet_levelset.py      |  2 +-
 examples/cylinder/oscillating_cylinder.py     |  2 +-
 examples/fixed_point/heat_equation.py         |  2 +-
 .../flow_around_sphere/flow_around_sphere.py  |  2 +-
 examples/multiresolution/scalar_advection.py  |  2 +-
 .../particles_above_salt_bc.py                |  2 +-
 .../particles_above_salt_bc_3d.py             |  2 +-
 .../particles_above_salt_periodic.py          |  2 +-
 .../particles_above_salt_symmetrized.py       |  2 +-
 examples/scalar_advection/levelset.py         |  2 +-
 examples/scalar_advection/scalar_advection.py |  2 +-
 examples/scalar_diffusion/scalar_diffusion.py |  2 +-
 examples/sediment_deposit/sediment_deposit.py |  2 +-
 .../sediment_deposit_levelset.py              |  2 +-
 examples/shear_layer/shear_layer.py           |  2 +-
 examples/taylor_green/taylor_green.py         |  2 +-
 .../taylor_green/taylor_green_cpuFortran.py   |  2 +-
 {examples => hysop/examples}/__init__.py      |  0
 {examples => hysop/examples}/example_utils.py |  0
 23 files changed, 66 insertions(+), 65 deletions(-)
 rename {examples => hysop/examples}/__init__.py (100%)
 rename {examples => hysop/examples}/example_utils.py (100%)

diff --git a/examples/analytic/analytic.py b/examples/analytic/analytic.py
index 631e14b44..39f15a551 100755
--- a/examples/analytic/analytic.py
+++ b/examples/analytic/analytic.py
@@ -1,63 +1,64 @@
 #!/usr/bin/env python2
 import numpy as np
 import sympy as sm
-             
+
+
 def compute(args):
     '''
     HySoP Analytic Example: Initialize a field with a space and time dependent analytic formula.
     '''
     from hysop import Field, Box, IOParams, MPIParams, \
-                      Simulation, Problem, ScalarParameter
+        Simulation, Problem, ScalarParameter
     from hysop.constants import Implementation
     from hysop.operators import AnalyticField, HDF_Writer
-    
+
     # Define domain
     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=args.ndim)
+
     # Define parameters and field (time and analytic field)
-    t      = ScalarParameter('t', dtype=args.dtype)
+    t = ScalarParameter('t', dtype=args.dtype)
     scalar = Field(domain=box, name='S0', 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 = {'mpi_params': mpi_params}
     if (impl is Implementation.PYTHON):
         # Setup python specific extra operator keyword arguments
         # (mapping: variable name => variable value)
-        op_kwds['extra_input_kwds'] = {'t': t} 
+        op_kwds['extra_input_kwds'] = {'t': t}
     elif (impl is Implementation.OPENCL):
         # For the OpenCL implementation we need to setup the compute device
         # and configure how the code is generated and compiled at runtime.
-        
+
         # Create an explicit OpenCL context from user parameters
         from hysop.backend.device.opencl.opencl_tools import get_or_create_opencl_env, get_device_number
         cl_env = get_or_create_opencl_env(
             mpi_params=mpi_params,
-            platform_id=args.cl_platform_id, 
-            device_id=box.machine_rank%get_device_number() if args.cl_device_id is None else None)
+            platform_id=args.cl_platform_id,
+            device_id=box.machine_rank % get_device_number() if args.cl_device_id is None else None)
 
         # Configure OpenCL kernel generation and tuning (already done by HysopArgParser)
         from hysop.methods import OpenClKernelConfig
         method = {OpenClKernelConfig: args.opencl_kernel_config}
-        
+
         # Setup opencl specific extra operator keyword arguments
-        op_kwds['cl_env']     = cl_env
-        op_kwds['method']     = method
+        op_kwds['cl_env'] = cl_env
+        op_kwds['method'] = method
     else:
-        msg='Unknown implementation \'{}\'.'.format(impl)
+        msg = 'Unknown implementation \'{}\'.'.format(impl)
         raise ValueError(msg)
-    
+
     # Analytic initialization method depends on chosen implementation
     if (impl is Implementation.PYTHON):
         # With the python implementation we can directly use a python method
         # (using numpy arrays). Here each field component is stored in the
-        # tuple 'data'. Coordinates will be passed as a tuple as a second 
+        # tuple 'data'. Coordinates will be passed as a tuple as a second
         # argument. Finally extra arguments (here t) are passed last.
         # Note that t is a ScalarParameter, so we evaluate it to get its value.
         def compute_scalar(data, coords, component, t):
@@ -77,21 +78,21 @@ def compute(args):
         for xi in xs:
             compute_scalar *= sm.cos(xi-ts)
     else:
-        msg='Unknown implementation {}.'.format(impl)
-    
+        msg = 'Unknown implementation {}.'.format(impl)
+
     # Finally build the operator
-    analytic = AnalyticField(name='analytic', 
-                field=scalar, formula=compute_scalar,
-                variables = {scalar: npts}, implementation=impl,
-                **op_kwds)
+    analytic = AnalyticField(name='analytic',
+                             field=scalar, formula=compute_scalar,
+                             variables={scalar: npts}, implementation=impl,
+                             **op_kwds)
 
     # Write output field at given frequency
     io_params = IOParams(filename='analytic', frequency=args.dump_freq)
     df = HDF_Writer(name='S',
-                     io_params=io_params,
-                     variables={scalar: npts},
-                     **op_kwds)
-   
+                    io_params=io_params,
+                    variables={scalar: npts},
+                    **op_kwds)
+
     # Create the problem we want to solve and insert our operator
     problem = Problem()
     problem.insert(analytic, df)
@@ -102,36 +103,36 @@ def compute(args):
     if args.display_graph:
         problem.display(args.visu_rank)
 
-    # Create a simulation and solve the problem 
+    # 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, 
-                      max_iter=args.max_iter, 
+    simu = Simulation(start=args.tstart, end=args.tend,
+                      nb_iter=args.nb_iter, dt0=args.dt,
+                      max_iter=args.max_iter,
                       times_of_interest=args.dump_times,
                       t=t)
-    
-    # Finally solve the problem 
+
+    # Finally solve the problem
     problem.solve(simu, dry_run=args.dry_run)
-    
+
     # Finalize
     problem.finalize()
 
 
-if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
-    
+if __name__ == '__main__':
+    from hysop.examples.example_utils import HysopArgParser, colors
+
     prog_name = 'analytic'
     default_dump_dir = '{}/hysop_examples/{}'.format(HysopArgParser.tmp_dir(), prog_name)
 
-    description=colors.color('HySoP Analytic Example: ', fg='blue', style='bold')
-    description+='Initialize a field with a space and time dependent analytic formula.'
+    description = colors.color('HySoP Analytic Example: ', fg='blue', style='bold')
+    description += 'Initialize a field with a space and time dependent analytic formula.'
 
     parser = HysopArgParser(prog_name=prog_name,
-             description=description,
-             default_dump_dir=default_dump_dir)
+                            description=description,
+                            default_dump_dir=default_dump_dir)
 
-    parser.set_defaults(box_start=(0.0,), box_length=(2*np.pi,), 
-                       tstart=0.0, tend=10.0, nb_iter=100,
-                       dump_freq=5)
+    parser.set_defaults(box_start=(0.0,), box_length=(2*np.pi,),
+                        tstart=0.0, tend=10.0, nb_iter=100,
+                        dump_freq=5)
 
     parser.run(compute)
diff --git a/examples/bubble/periodic_bubble.py b/examples/bubble/periodic_bubble.py
index af738c800..5de7e1e0e 100644
--- a/examples/bubble/periodic_bubble.py
+++ b/examples/bubble/periodic_bubble.py
@@ -302,7 +302,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class PeriodicBubbleArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/bubble/periodic_bubble_levelset.py b/examples/bubble/periodic_bubble_levelset.py
index 810a926e9..237bf8f67 100644
--- a/examples/bubble/periodic_bubble_levelset.py
+++ b/examples/bubble/periodic_bubble_levelset.py
@@ -300,7 +300,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class PeriodicBubbleArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/bubble/periodic_bubble_levelset_penalization.py b/examples/bubble/periodic_bubble_levelset_penalization.py
index 05a3ef5d5..7cf865cca 100644
--- a/examples/bubble/periodic_bubble_levelset_penalization.py
+++ b/examples/bubble/periodic_bubble_levelset_penalization.py
@@ -342,7 +342,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class PeriodicBubbleArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/bubble/periodic_jet_levelset.py b/examples/bubble/periodic_jet_levelset.py
index ac99a865c..9607f8fde 100644
--- a/examples/bubble/periodic_jet_levelset.py
+++ b/examples/bubble/periodic_jet_levelset.py
@@ -287,7 +287,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class PeriodicJetArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/cylinder/oscillating_cylinder.py b/examples/cylinder/oscillating_cylinder.py
index 73f3e41df..b51b85881 100644
--- a/examples/cylinder/oscillating_cylinder.py
+++ b/examples/cylinder/oscillating_cylinder.py
@@ -260,7 +260,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class OscillatingCylinderArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/fixed_point/heat_equation.py b/examples/fixed_point/heat_equation.py
index 518a77381..d54e802ac 100644
--- a/examples/fixed_point/heat_equation.py
+++ b/examples/fixed_point/heat_equation.py
@@ -197,7 +197,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class IMArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/flow_around_sphere/flow_around_sphere.py b/examples/flow_around_sphere/flow_around_sphere.py
index 807d0d26c..f01e5f9ef 100644
--- a/examples/flow_around_sphere/flow_around_sphere.py
+++ b/examples/flow_around_sphere/flow_around_sphere.py
@@ -327,7 +327,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
     parser = HysopArgParser(prog_name="FlowAroundSphere",
                             description="""HySoP flow around a sphere.\n""",
                             default_dump_dir='{}/hysop_examples/FlowAroundSphere'.format(
diff --git a/examples/multiresolution/scalar_advection.py b/examples/multiresolution/scalar_advection.py
index 82e805e2b..98b763292 100644
--- a/examples/multiresolution/scalar_advection.py
+++ b/examples/multiresolution/scalar_advection.py
@@ -189,7 +189,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class MultiResolutionScalarAdvectionArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/particles_above_salt/particles_above_salt_bc.py b/examples/particles_above_salt/particles_above_salt_bc.py
index ce823f8c3..807120e6b 100644
--- a/examples/particles_above_salt/particles_above_salt_bc.py
+++ b/examples/particles_above_salt/particles_above_salt_bc.py
@@ -323,7 +323,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class ParticleAboveSaltArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/particles_above_salt/particles_above_salt_bc_3d.py b/examples/particles_above_salt/particles_above_salt_bc_3d.py
index afcdbf826..3bd83aeb8 100644
--- a/examples/particles_above_salt/particles_above_salt_bc_3d.py
+++ b/examples/particles_above_salt/particles_above_salt_bc_3d.py
@@ -338,7 +338,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class ParticleAboveSaltArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/particles_above_salt/particles_above_salt_periodic.py b/examples/particles_above_salt/particles_above_salt_periodic.py
index 8a6fcb237..8eaac49a2 100644
--- a/examples/particles_above_salt/particles_above_salt_periodic.py
+++ b/examples/particles_above_salt/particles_above_salt_periodic.py
@@ -333,7 +333,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class ParticleAboveSaltArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/particles_above_salt/particles_above_salt_symmetrized.py b/examples/particles_above_salt/particles_above_salt_symmetrized.py
index 904de36a6..f7a393327 100644
--- a/examples/particles_above_salt/particles_above_salt_symmetrized.py
+++ b/examples/particles_above_salt/particles_above_salt_symmetrized.py
@@ -320,7 +320,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class ParticleAboveSaltArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/scalar_advection/levelset.py b/examples/scalar_advection/levelset.py
index 045bb3330..8e8dbe82c 100644
--- a/examples/scalar_advection/levelset.py
+++ b/examples/scalar_advection/levelset.py
@@ -215,7 +215,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class LevelsetArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/scalar_advection/scalar_advection.py b/examples/scalar_advection/scalar_advection.py
index 7b133555e..0e061844a 100644
--- a/examples/scalar_advection/scalar_advection.py
+++ b/examples/scalar_advection/scalar_advection.py
@@ -142,7 +142,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class ScalarAdvectionArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/scalar_diffusion/scalar_diffusion.py b/examples/scalar_diffusion/scalar_diffusion.py
index 170359e41..085664f09 100755
--- a/examples/scalar_diffusion/scalar_diffusion.py
+++ b/examples/scalar_diffusion/scalar_diffusion.py
@@ -118,7 +118,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class ScalarDiffusionArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/sediment_deposit/sediment_deposit.py b/examples/sediment_deposit/sediment_deposit.py
index 3041c495b..ea0b53c94 100644
--- a/examples/sediment_deposit/sediment_deposit.py
+++ b/examples/sediment_deposit/sediment_deposit.py
@@ -334,7 +334,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class ParticleAboveSaltArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/sediment_deposit/sediment_deposit_levelset.py b/examples/sediment_deposit/sediment_deposit_levelset.py
index 5f5beda14..bb566be49 100644
--- a/examples/sediment_deposit/sediment_deposit_levelset.py
+++ b/examples/sediment_deposit/sediment_deposit_levelset.py
@@ -395,7 +395,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class ParticleAboveSaltArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/shear_layer/shear_layer.py b/examples/shear_layer/shear_layer.py
index 520d42899..29a14fad5 100644
--- a/examples/shear_layer/shear_layer.py
+++ b/examples/shear_layer/shear_layer.py
@@ -205,7 +205,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class ShearLayerArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/taylor_green/taylor_green.py b/examples/taylor_green/taylor_green.py
index a8d07d76f..1d544f4d7 100644
--- a/examples/taylor_green/taylor_green.py
+++ b/examples/taylor_green/taylor_green.py
@@ -315,7 +315,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class TaylorGreenArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/taylor_green/taylor_green_cpuFortran.py b/examples/taylor_green/taylor_green_cpuFortran.py
index cc575e98a..d89e280f7 100644
--- a/examples/taylor_green/taylor_green_cpuFortran.py
+++ b/examples/taylor_green/taylor_green_cpuFortran.py
@@ -232,7 +232,7 @@ def compute(args):
 
 
 if __name__=='__main__':
-    from examples.example_utils import HysopArgParser, colors
+    from hysop.examples.example_utils import HysopArgParser, colors
 
     class TaylorGreenArgParser(HysopArgParser):
         def __init__(self):
diff --git a/examples/__init__.py b/hysop/examples/__init__.py
similarity index 100%
rename from examples/__init__.py
rename to hysop/examples/__init__.py
diff --git a/examples/example_utils.py b/hysop/examples/example_utils.py
similarity index 100%
rename from examples/example_utils.py
rename to hysop/examples/example_utils.py
-- 
GitLab