From 95e800f8aec45f4eb871eb036ef29cdc1757e374 Mon Sep 17 00:00:00 2001
From: Cristian C Lalescu <Cristian.Lalescu@ds.mpg.de>
Date: Sun, 24 Jan 2016 14:23:40 +0100
Subject: [PATCH] partial move of parser generation to fluid classes

---
 bfps/FluidConvert.py |  19 +++++
 bfps/FluidResize.py  |  11 +++
 bfps/Launcher.py     | 182 ++++++++++++++-----------------------------
 bfps/NavierStokes.py |  55 +++++++++++++
 4 files changed, 142 insertions(+), 125 deletions(-)

diff --git a/bfps/FluidConvert.py b/bfps/FluidConvert.py
index 8b322c05..72afa2a8 100644
--- a/bfps/FluidConvert.py
+++ b/bfps/FluidConvert.py
@@ -90,4 +90,23 @@ class FluidConvert(_fluid_particle_base):
                 """
         self.fluid_end += 'delete fs;\n'
         return None
+    def add_parser_arguments(
+            self,
+            parser):
+        parser.add_argument(
+                '--src-wd',
+                type = str,
+                dest = 'src_work_dir',
+                default = './')
+        parser.add_argument(
+                '--src-simname',
+                type = str,
+                dest = 'src_simname',
+                default = '')
+        parser.add_argument(
+                '--src-iteration',
+                type = int,
+                dest = 'src_iteration',
+                default = 0)
+        return None
 
diff --git a/bfps/FluidResize.py b/bfps/FluidResize.py
index e9fa8f9f..253c6d79 100644
--- a/bfps/FluidResize.py
+++ b/bfps/FluidResize.py
@@ -104,4 +104,15 @@ class FluidResize(_fluid_particle_base):
                 //endcpp
                 """
         return None
+    def add_parser_arguments(
+            self,
+            parser):
+        parser.add_argument(
+                '-n',
+                type = int,
+                dest = 'n',
+                default = 32,
+                metavar = 'N',
+                help = 'resize to N')
+        return None
 
diff --git a/bfps/Launcher.py b/bfps/Launcher.py
index 736ef7ae..a2dc0803 100644
--- a/bfps/Launcher.py
+++ b/bfps/Launcher.py
@@ -66,10 +66,10 @@ class Launcher:
                 '--wd',
                 type = str, dest = 'work_dir',
                 default = './')
+        c = self.base_class()
         # now add class specific arguments
-        self.add_class_specific_arguments(self.base_class.__name__)
+        c.add_parser_arguments(self.parser)
         # now add code parameters
-        c = self.base_class()
         for k in sorted(c.parameters.keys()):
             self.parser.add_argument(
                     '--{0}'.format(k),
@@ -77,75 +77,6 @@ class Launcher:
                     dest = k,
                     default = None)
         return None
-    def add_class_specific_arguments(
-            self,
-            class_name):
-        """add arguments specific to different classes.
-
-        .. warning:: Reimplement this for custom classes.
-        """
-        if class_name in ['NavierStokes', 'FluidResize']:
-            self.parser.add_argument(
-                    '--src-wd',
-                    type = str,
-                    dest = 'src_work_dir',
-                    default = './')
-            self.parser.add_argument(
-                    '--src-simname',
-                    type = str,
-                    dest = 'src_simname',
-                    default = '')
-            self.parser.add_argument(
-                    '--src-iteration',
-                    type = int,
-                    dest = 'src_iteration',
-                    default = 0)
-        if class_name == 'FluidResize':
-            self.parser.add_argument(
-                    '-n',
-                    type = int,
-                    dest = 'n',
-                    default = 32,
-                    metavar = 'N',
-                    help = 'resize to N')
-        if class_name == 'NavierStokes':
-            self.parser.add_argument(
-                    '-n',
-                    type = int,
-                    dest = 'n',
-                    default = 32,
-                    metavar = 'N',
-                    help = 'code is run by default in a grid of NxNxN')
-            self.parser.add_argument(
-                    '--precision',
-                    type = str, dest = 'precision',
-                    default = 'single')
-            self.parser.add_argument(
-                    '--njobs',
-                    type = int, dest = 'njobs',
-                    default = 1)
-            self.parser.add_argument(
-                    '--QR-stats',
-                    action = 'store_true',
-                    dest = 'QR_stats',
-                    help = 'add this option if you want to compute velocity gradient and QR stats')
-            self.parser.add_argument(
-                    '--kMeta',
-                    type = float,
-                    dest = 'kMeta',
-                    default = 2.0)
-            self.parser.add_argument(
-                    '--dtfactor',
-                    type = float,
-                    dest = 'dtfactor',
-                    default = 0.5,
-                    help = 'dt is computed as DTFACTOR / N')
-            self.parser.add_argument(
-                    '--particle-rand-seed',
-                    type = int,
-                    dest = 'particle_rand_seed',
-                    default = None)
-        return None
     def __call__(
             self,
             args = None):
@@ -155,61 +86,62 @@ class Launcher:
         opt.work_dir = os.path.join(
                 os.path.realpath(opt.work_dir),
                 'N{0:0>4}'.format(opt.n))
-        c = self.base_class(
-                work_dir = opt.work_dir,
-                fluid_precision = opt.precision,
-                simname = opt.simname,
-                QR_stats_on = opt.QR_stats)
-        # with the default Lundgren forcing, I can estimate the dissipation
-        # with nondefault forcing, figure out the amplitude for this viscosity
-        # yourself
-        c.parameters['nu'] = (opt.kMeta * 2 / opt.n)**(4./3)
-        c.parameters['dt'] = (opt.dtfactor / opt.n)
-        if ((c.parameters['niter_todo'] % c.parameters['niter_out']) != 0):
-            c.parameters['niter_out'] = c.parameters['niter_todo']
-        if c.QR_stats_on:
-            # max_Q_estimate and max_R_estimate are just used for the 2D pdf
-            # therefore I just want them to be small multiples of mean trS2
-            # I'm already estimating the dissipation with kMeta...
-            meantrS2 = (opt.n//2 / opt.kMeta)**4 * c.parameters['nu']**2
-            c.parameters['max_Q_estimate'] = meantrS2
-            c.parameters['max_R_estimate'] = .4*meantrS2**1.5
+        if self.base_class.__name__ == 'NavierStokes':
+            c = self.base_class(
+                    work_dir = opt.work_dir,
+                    fluid_precision = opt.precision,
+                    simname = opt.simname,
+                    QR_stats_on = opt.QR_stats)
+            # with the default Lundgren forcing, I can estimate the dissipation
+            # with nondefault forcing, figure out the amplitude for this viscosity
+            # yourself
+            c.parameters['nu'] = (opt.kMeta * 2 / opt.n)**(4./3)
+            c.parameters['dt'] = (opt.dtfactor / opt.n)
+            if ((c.parameters['niter_todo'] % c.parameters['niter_out']) != 0):
+                c.parameters['niter_out'] = c.parameters['niter_todo']
+            if c.QR_stats_on:
+                # max_Q_estimate and max_R_estimate are just used for the 2D pdf
+                # therefore I just want them to be small multiples of mean trS2
+                # I'm already estimating the dissipation with kMeta...
+                meantrS2 = (opt.n//2 / opt.kMeta)**4 * c.parameters['nu']**2
+                c.parameters['max_Q_estimate'] = meantrS2
+                c.parameters['max_R_estimate'] = .4*meantrS2**1.5
 
-        # command line parameters will overwrite any defaults
-        cmd_line_pars = vars(opt)
-        for k in ['nx', 'ny', 'nz']:
-            if type(cmd_line_pars[k]) == type(None):
-                cmd_line_pars[k] = opt.n
-        for k in c.parameters.keys():
-            if k in cmd_line_pars.keys():
-                if not type(cmd_line_pars[k]) == type(None):
-                    c.parameters[k] = cmd_line_pars[k]
-        c.fill_up_fluid_code()
-        c.finalize_code()
-        c.write_src()
-        c.set_host_info(bfps.host_info)
-        if not os.path.exists(os.path.join(c.work_dir, c.simname + '.h5')):
-            c.write_par()
-            if c.parameters['nparticles'] > 0:
-                data = c.generate_tracer_state(species = 0, rseed = opt.particle_rand_seed)
-                for s in range(1, c.particle_species):
-                    c.generate_tracer_state(species = s, data = data)
-            init_condition_file = os.path.join(
-                    c.work_dir,
-                    c.simname + '_cvorticity_i{0:0>5x}'.format(0))
-            if not os.path.exists(init_condition_file):
-                if len(opt.src_simname) > 0:
-                    src_file = os.path.join(
-                            c.work_dir,
-                            opt.src_simname + '_cvorticity_i{0:0>5x}'.format(opt.src_iteration))
-                    os.symlink(src_file, init_condition_file)
-                else:
-                   c.generate_vector_field(
-                           write_to_file = True,
-                           spectra_slope = 2.0,
-                           amplitude = 0.25)
-        c.run(ncpu = opt.ncpu,
-              njobs = opt.njobs)
+            # command line parameters will overwrite any defaults
+            cmd_line_pars = vars(opt)
+            for k in ['nx', 'ny', 'nz']:
+                if type(cmd_line_pars[k]) == type(None):
+                    cmd_line_pars[k] = opt.n
+            for k in c.parameters.keys():
+                if k in cmd_line_pars.keys():
+                    if not type(cmd_line_pars[k]) == type(None):
+                        c.parameters[k] = cmd_line_pars[k]
+            c.fill_up_fluid_code()
+            c.finalize_code()
+            c.write_src()
+            c.set_host_info(bfps.host_info)
+            if not os.path.exists(os.path.join(c.work_dir, c.simname + '.h5')):
+                c.write_par()
+                if c.parameters['nparticles'] > 0:
+                    data = c.generate_tracer_state(species = 0, rseed = opt.particle_rand_seed)
+                    for s in range(1, c.particle_species):
+                        c.generate_tracer_state(species = s, data = data)
+                init_condition_file = os.path.join(
+                        c.work_dir,
+                        c.simname + '_cvorticity_i{0:0>5x}'.format(0))
+                if not os.path.exists(init_condition_file):
+                    if len(opt.src_simname) > 0:
+                        src_file = os.path.join(
+                                c.work_dir,
+                                opt.src_simname + '_cvorticity_i{0:0>5x}'.format(opt.src_iteration))
+                        os.symlink(src_file, init_condition_file)
+                    else:
+                       c.generate_vector_field(
+                               write_to_file = True,
+                               spectra_slope = 2.0,
+                               amplitude = 0.25)
+            c.run(ncpu = opt.ncpu,
+                  njobs = opt.njobs)
         return c
 
 
diff --git a/bfps/NavierStokes.py b/bfps/NavierStokes.py
index 98074c3a..34433ace 100644
--- a/bfps/NavierStokes.py
+++ b/bfps/NavierStokes.py
@@ -921,4 +921,59 @@ class NavierStokes(_fluid_particle_base):
         self.fluid_start += update_fields
         self.fluid_loop += update_fields
         return None
+    def add_parser_arguments(
+            self,
+            parser):
+        parser.add_argument(
+                '--src-wd',
+                type = str,
+                dest = 'src_work_dir',
+                default = './')
+        parser.add_argument(
+                '--src-simname',
+                type = str,
+                dest = 'src_simname',
+                default = '')
+        parser.add_argument(
+                '--src-iteration',
+                type = int,
+                dest = 'src_iteration',
+                default = 0)
+        parser.add_argument(
+               '-n', '--cube-size',
+               type = int,
+               dest = 'n',
+               default = 32,
+               metavar = 'N',
+               help = 'code is run by default in a grid of NxNxN')
+        parser.add_argument(
+               '--precision',
+               type = str, dest = 'precision',
+               default = 'single')
+        parser.add_argument(
+               '--njobs',
+               type = int, dest = 'njobs',
+               default = 1)
+        parser.add_argument(
+               '--QR-stats',
+               action = 'store_true',
+               dest = 'QR_stats',
+               help = 'add this option if you want to compute velocity gradient and QR stats')
+        parser.add_argument(
+               '--kMeta',
+               type = float,
+               dest = 'kMeta',
+               default = 2.0)
+        parser.add_argument(
+               '--dtfactor',
+               type = float,
+               dest = 'dtfactor',
+               default = 0.5,
+               help = 'dt is computed as DTFACTOR / N')
+        parser.add_argument(
+               '--particle-rand-seed',
+               type = int,
+               dest = 'particle_rand_seed',
+               default = None)
+        return None
 
-- 
GitLab