From b16457abe711cfe6d271ae94e7e8b55abd79e61e Mon Sep 17 00:00:00 2001
From: "Mads M. Pedersen" <mmpe@dtu.dk>
Date: Tue, 10 Oct 2017 11:09:09 +0200
Subject: [PATCH] closes #35

---
 wetb/hawc2/htc_contents.py        | 10 ++++++
 wetb/hawc2/htc_file.py            | 52 ++++++++++++++++++-------------
 wetb/hawc2/tests/test_htc_file.py | 12 +++++--
 3 files changed, 50 insertions(+), 24 deletions(-)

diff --git a/wetb/hawc2/htc_contents.py b/wetb/hawc2/htc_contents.py
index ddc60008..4e184974 100644
--- a/wetb/hawc2/htc_contents.py
+++ b/wetb/hawc2/htc_contents.py
@@ -175,6 +175,16 @@ class HTCSection(HTCContents):
         s += "".join([c.__str__(level + 1) for c in self])
         s += "%send %s;%s\n" % ("  "*level, self.name_, (("", "\t" + self.end_comments)[self.end_comments.strip() != ""]).replace("\t\n","\n"))
         return s
+    
+    def get_subsection_by_name(self, name):
+        lst = [s for s in self if 'name' in s and s.name[0]==name]
+        if len(lst)==1:
+            return lst[0]
+        else:
+            if len(lst)==0:
+                raise ValueError("subsection '%s' not found"%name)
+            else:
+                raise NotImplementedError()
 
 class HTCLine(HTCContents):
     values = None
diff --git a/wetb/hawc2/htc_file.py b/wetb/hawc2/htc_file.py
index 087fb242..71919752 100644
--- a/wetb/hawc2/htc_file.py
+++ b/wetb/hawc2/htc_file.py
@@ -197,21 +197,21 @@ class HTCFile(HTCContents, HTCDefaults):
         with open(filename, 'w', encoding='cp1252') as fid:
             fid.write(str(self))
 
-    def set_name(self, name, htc_folder="htc", log_folder="log", res_folder="res", animation_folder='animation', visualization_folder="visualization"):
+    def set_name(self, name, subfolder=''):
         #if os.path.isabs(folder) is False and os.path.relpath(folder).startswith("htc" + os.path.sep):
         self.contents #load if not loaded
-        fmt_folder = lambda folder : "./" + os.path.relpath(folder).replace("\\", "/")
-
-        self.filename = os.path.abspath(os.path.join(self.modelpath, fmt_folder(htc_folder), "%s.htc" % name)).replace("\\", "/")
+        fmt_folder = lambda folder, subfolder : "./" + os.path.relpath(os.path.join(folder, subfolder)).replace("\\", "/")
+        
+        self.filename = os.path.abspath(os.path.join(self.modelpath, fmt_folder('htc', subfolder), "%s.htc" % name)).replace("\\", "/")
         if 'simulation' in self and 'logfile' in self.simulation:
-            self.simulation.logfile = os.path.join(fmt_folder(log_folder), "%s.log" % name).replace("\\", "/")
+            self.simulation.logfile = os.path.join(fmt_folder('log', subfolder), "%s.log" % name).replace("\\", "/")
+            if 'animation' in self.simulation:
+                self.simulation.animation = os.path.join(fmt_folder('animation', subfolder), "%s.dat" % name).replace("\\", "/")
+            if 'visualization' in self.simulation:
+                self.simulation.visualization = os.path.join(fmt_folder('visualization', subfolder), "%s.hdf5" % name).replace("\\", "/")
         elif 'test_structure' in self and 'logfile' in self.test_structure:  # hawc2aero
-            self.test_structure.logfile = os.path.join(fmt_folder(log_folder), "%s.log" % name).replace("\\", "/")
-        if 'simulation' in self and 'animation' in self.simulation:
-            self.simulation.animation = os.path.join(fmt_folder(animation_folder), "%s.dat" % name).replace("\\", "/")
-        if 'simulation' in self and 'visualization' in self.simulation:
-            self.simulation.visualization = os.path.join(fmt_folder(visualization_folder), "%s.hdf5" % name).replace("\\", "/")
-        self.output.filename = os.path.join(fmt_folder(res_folder), "%s" % name).replace("\\", "/")
+            self.test_structure.logfile = os.path.join(fmt_folder('log', subfolder), "%s.log" % name).replace("\\", "/")
+        self.output.filename = os.path.join(fmt_folder('res', subfolder), "%s" % name).replace("\\", "/")
 
     def set_time(self, start=None, stop=None, step=None):
         self.contents # load if not loaded
@@ -262,6 +262,14 @@ class HTCFile(HTCContents, HTCDefaults):
         if 'soil' in self:
             if 'soil_element' in self.soil:
                 files.append(self.soil.soil_element.get('datafile', [None])[0])
+        try:
+            dtu_we_controller = self.dll.get_subsection_by_name('dtu_we_controller')
+            theta_min = dtu_we_controller.init.constant__5[1]
+            files.append(os.path.join(os.path.dirname(dtu_we_controller.filename[0]), "wpdata.%d"%theta_min).replace("\\","/"))
+        except:
+            pass
+            
+    
         try:
             files.append(self.force.dll.dll[0])
         except:
@@ -362,16 +370,18 @@ class HTCFile(HTCContents, HTCDefaults):
     def deltat(self):
         return self.simulation.newmark.deltat[0]
     
-    def get_body(self, name):
-        lst = [b for b in self.new_htc_structure if b.name_=="main_body" and b.name[0]==name]
-        if len(lst)==1:
-            return lst[0]
-        else:
-            if len(lst)==0:
-                raise ValueError("Body '%s' not found"%name)
-            else:
-                raise NotImplementedError()
-        
+
+#     
+#     def get_body(self, name):
+#         lst = [b for b in self.new_htc_structure if b.name_=="main_body" and b.name[0]==name]
+#         if len(lst)==1:
+#             return lst[0]
+#         else:
+#             if len(lst)==0:
+#                 raise ValueError("Body '%s' not found"%name)
+#             else:
+#                 raise NotImplementedError()
+#         
 
 class H2aeroHTCFile(HTCFile):
     def __init__(self, filename=None, modelpath=None):
diff --git a/wetb/hawc2/tests/test_htc_file.py b/wetb/hawc2/tests/test_htc_file.py
index 0326807e..5ea412ee 100644
--- a/wetb/hawc2/tests/test_htc_file.py
+++ b/wetb/hawc2/tests/test_htc_file.py
@@ -107,12 +107,15 @@ class TestHtcFile(unittest.TestCase):
   
     def test_htcfile_setname(self):
         htcfile = HTCFile(self.testfilepath + "test.htc")
-        htcfile.set_name("mytest", htc_folder="htcfiles")
-        self.assertEqual(os.path.relpath(htcfile.filename, self.testfilepath), r'mytest.htc')
+        htcfile.set_name("mytest")
+        self.assertEqual(os.path.relpath(htcfile.filename, self.testfilepath), r'..\htc\mytest.htc')
         self.assertEqual(htcfile.simulation.logfile[0], './log/mytest.log')
         self.assertEqual(htcfile.output.filename[0], './res/mytest')
   
-  
+        htcfile.set_name("mytest", 'subfolder')
+        self.assertEqual(os.path.relpath(htcfile.filename, self.testfilepath), r'..\htc\subfolder\mytest.htc')
+        self.assertEqual(htcfile.simulation.logfile[0], './log/subfolder/mytest.log')
+        self.assertEqual(htcfile.output.filename[0], './res/subfolder/mytest')
   
     def test_set_time(self):
         htcfile = HTCFile(self.testfilepath + "test.htc")
@@ -248,6 +251,9 @@ end turb_export;"""
             except ValueError:
                 raise ValueError(f + " is not in list")
         self.assertFalse(input_files)
+        
+        htcfile = HTCFile(self.testfilepath + "DTU_10MW_RWT.htc")
+        self.assertTrue('./control/wpdata.100' in htcfile.input_files())
   
     def test_input_files2(self):
         htcfile = HTCFile(self.testfilepath + "ansi.htc",'../')
-- 
GitLab