From 8ca735903dd57a5528f77e6cb0f2165104412996 Mon Sep 17 00:00:00 2001
From: David Robert Verelst <dave@dtu.dk>
Date: Fri, 5 May 2017 13:34:02 +0200
Subject: [PATCH] prepost: add first version of hydro files generation by @shfe

---
 docs/howto-make-dlcs.md       | 28 ++++++++++++
 wetb/prepost/GenerateHydro.py | 84 +++++++++++++++++++++++++++++++++++
 wetb/prepost/Simulations.py   | 20 +++++++++
 3 files changed, 132 insertions(+)
 create mode 100755 wetb/prepost/GenerateHydro.py

diff --git a/docs/howto-make-dlcs.md b/docs/howto-make-dlcs.md
index 9e2db0e..bad92e6 100644
--- a/docs/howto-make-dlcs.md
+++ b/docs/howto-make-dlcs.md
@@ -464,6 +464,34 @@ tags:
 * ```[high_freq_comp]```
 
 
+### Tags required for hydro file generation
+
+* ```[hydro_dir]```
+* ```[hydro input name]```
+* ```[wave_type]``` : see HAWC2 manual for options
+* ```[wave_spectrum]``` : see HAWC2 manual for options
+* ```[hydro_dir]```
+* ```[wdepth]```
+* ```[hs]``` : see HAWC2 manual for options
+* ```[tp]``` : see HAWC2 manual for options
+* ```[wave_seed]``` : see HAWC2 manual for options
+
+And the corresponding section the htc master file:
+
+```
+begin hydro;
+  begin water_properties;
+    rho 1027 ; kg/m^3
+    gravity 9.81 ; m/s^2
+    mwl 0.0;
+    mudlevel [wdepth];
+    wave_direction [wave_dir];
+    water_kinematics_dll ./wkin_dll.dll   ./[hydro_dir][hydro input name].inp;
+  end water_properties;
+end hydro;
+```
+
+
 Launching the jobs on the cluster
 ---------------------------------
 
diff --git a/wetb/prepost/GenerateHydro.py b/wetb/prepost/GenerateHydro.py
new file mode 100755
index 0000000..f2cfadf
--- /dev/null
+++ b/wetb/prepost/GenerateHydro.py
@@ -0,0 +1,84 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Fri Apr 15 18:34:15 2016
+
+@author: shfe
+
+Description: This script is used for writing the hydro input files for HAWC2
+(the wave type det_airy is not included)
+
+"""
+
+import os
+
+
+class hydro_input(object):
+
+    """ Basic class aiming for write the hydrodynamics input file"""
+
+    def __init__(self, wavetype, wdepth, spectrum, Hs, Tp, seed,
+                 gamma = 3.3, stretching = 1, wn = None, coef = 200,
+                 spreading = None):
+
+        self.wdepth = wdepth
+        if self.wdepth < 0:
+            raise ValueError('water depth must be greater than 0')
+
+        # Regular Airy Wave Input
+        if wavetype == 'reg_airy':
+            self.waveno = 0
+            self.argument = 'begin %s ;\n\t\tstretching %d;\n\t\twave %d %d;\n\tend;' \
+                            %(wavetype,stretching,Hs,Tp)
+
+        # Iregular Airy Wave Input
+        if wavetype == 'ireg_airy':
+            self.waveno = 1
+            # jonswap spectrum
+            if spectrum == 'jonswap':
+                spectrumno = 1
+                self.argument = 'begin %s ;\n\t\tstretching %d;\n\t\tspectrum %d;'\
+                                '\n\t\tjonswap %.1f %.1f %.1f;\n\t\tcoef %d %d;' \
+                                %(wavetype,stretching,spectrumno,Hs,Tp,gamma,coef,seed)
+
+            # Pierson Moscowitz spectrum
+            elif spectrum == 'pm':
+                spectrumno = 2
+                self.argument = 'begin %s ;\n\t\tstretching %d;\n\t\tspectrum %d;'\
+                                '\n\t\tpm %.1f %.1f ;\n\t\tcoef %d %d;' \
+                                %(wavetype,stretching,spectrumno,Hs,
+                                  Tp,coef,seed)
+
+            # check the spreading function
+            if spreading is not None:
+                self.argument += '\n\t\tspreading 1 %d;'%(spreading)
+            self.argument += '\n\tend;';
+
+        # Stream Wave Input
+        if wavetype == 'strf':
+            self.waveno = 3
+            self.argument = 'begin %s ;\n\t\twave %d %d;\n\tend;' \
+                            %(wavetype,Hs,Tp)
+
+    def execute(self, filename, folder):
+        cwd = os.getcwd()
+        folder_path = os.path.join(cwd,folder)
+        file_path = os.path.join(folder_path,filename)
+        # check if the hydro input file exists
+        if os.path.exists(file_path):
+            pass
+        else:
+            FILE = open(file_path,'w+')
+            line1 = 'begin wkin_input ;'
+            line2 = 'wavetype %d ;' %self.waveno
+            line3 = 'wdepth %d ;' %self.wdepth
+            line4 = 'end ;'
+            file_contents = '%s\n\t%s\n\t%s\n\t%s\n%s\n;\nexit;' \
+                            %(line1,line2,line3,self.argument,line4)
+            FILE.write(file_contents)
+            FILE.close()
+
+if __name__ == '__main__':
+    hs = 3
+    Tp = 11
+    hydro = hydro_input(Hs = hs, Tp = Tp, wdepth = 33,spectrum='jonswap',spreading = 2)
+    hydro.execute(filename='sss')
diff --git a/wetb/prepost/Simulations.py b/wetb/prepost/Simulations.py
index d07bce1..d26c64e 100755
--- a/wetb/prepost/Simulations.py
+++ b/wetb/prepost/Simulations.py
@@ -54,6 +54,7 @@ from wetb.prepost import misc
 from wetb.prepost import windIO
 from wetb.prepost import prepost
 from wetb.dlc import high_level as dlc
+from wetb.prepost.GenerateHydro import hydro_input
 
 def load_pickled_file(source):
     FILE = open(source, 'rb')
@@ -737,6 +738,25 @@ def prepare_launch(iter_dict, opt_tags, master, variable_tag_func,
             if verbose:
                 print('created cases for: %s.htc\n' % master.tags['[case_id]'])
 
+            # shfe: flag to generate hydro input file
+            if master.tags['[hydro_dir]'] is not False:
+                if '[hydro input name]' not in master.tags:
+                    continue
+                hydro_filename = master.tags['[hydro input name]']
+                print('creating hydro input file for: %s.inp\n' % hydro_filename)
+                wavetype = master.tags['[wave_type]']
+                wavespectrum = master.tags['[wave_spectrum]']
+                hydro_folder = master.tags['[hydro_dir]']
+                wdepth = float(master.tags['[wdepth]'])
+                hs = float(master.tags['[hs]'])
+                tp = float(master.tags['[tp]'])
+                wave_seed = int(float(master.tags['[wave_seed]']))
+                hydro_inputfile = hydro_input(wavetype=wavetype, Hs=hs, Tp=tp,
+                                              wdepth = wdepth, seed=wave_seed,
+                                              spectrum=wavespectrum,
+                                              spreading=None)
+                hydro_inputfile.execute(filename=hydro_filename + '.inp',
+                                        folder=hydro_folder)
 #    print(master.queue.get())
 
     # only copy data and create zip after all htc files have been created.
-- 
GitLab