From 6d44116532479c37d55726d1827974b6128e90c7 Mon Sep 17 00:00:00 2001
From: "Mads M. Pedersen" <mmpe@dtu.dk>
Date: Wed, 16 May 2018 09:16:27 +0200
Subject: [PATCH] pyFuga interface v3

removed tests/__init__.py

adapt test to new structure

set to correct colonel commit

set colonel to master

switched back to cmdinterpreter
---
 tests/test_fuga/test_pyfuga.py             | 37 +++++++++++---------
 topfarm/cost_models/fuga/Colonel           |  2 +-
 topfarm/cost_models/fuga/py_fuga.py        | 39 ++++++++--------------
 topfarm/cost_models/fuga/tests/__init__.py |  0
 4 files changed, 37 insertions(+), 41 deletions(-)
 delete mode 100644 topfarm/cost_models/fuga/tests/__init__.py

diff --git a/tests/test_fuga/test_pyfuga.py b/tests/test_fuga/test_pyfuga.py
index 033893c4..d1e6d571 100644
--- a/tests/test_fuga/test_pyfuga.py
+++ b/tests/test_fuga/test_pyfuga.py
@@ -19,11 +19,12 @@ fuga_path = os.path.abspath(os.path.dirname(py_fuga.__file__)) + '/Colonel/'
 
 
 def _test_parallel(id):
-    pyFuga = PyFuga(farm_name='Horns Rev 1',
-                    turbine_model_path=fuga_path + 'LUT/', turbine_model_name='Vestas_V80_(2_MW_offshore)[h=67.00]',
-                    tb_x=[423974, 424033], tb_y=[6151447, 6150889],
-                    mast_position=(0, 0, 70), z0=0.0001, zi=400, zeta0=0,
-                    farms_dir=fuga_path + 'LUT/Farms/', wind_atlas_path='Horns Rev 1\hornsrev0.lib')
+    pyFuga = PyFuga()
+    pyFuga.setup(farm_name='Horns Rev 1',
+                 turbine_model_path=fuga_path + 'LUT/', turbine_model_name='Vestas_V80_(2_MW_offshore)[h=67.00]',
+                 tb_x=[423974, 424033], tb_y=[6151447, 6150889],
+                 mast_position=(0, 0, 70), z0=0.0001, zi=400, zeta0=0,
+                 farms_dir=fuga_path + 'LUT/Farms/', wind_atlas_path='Horns Rev 1\hornsrev0.lib')
     print(pyFuga.stdout_filename, id)
     for i in range(1):
         print(threading.current_thread(), id)
@@ -38,21 +39,25 @@ class Test(unittest.TestCase):
         return os.path.isfile(lib_path) is False
 
     def get_fuga(self, tb_x=[423974, 424033], tb_y=[6151447, 6150889]):
-        return PyFuga(farm_name='Horns Rev 1',
-                      turbine_model_path=fuga_path + 'LUT/', turbine_model_name='Vestas_V80_(2_MW_offshore)[h=67.00]',
-                      tb_x=tb_x, tb_y=tb_y,
-                      mast_position=(0, 0, 70), z0=0.0001, zi=400, zeta0=0,
-                      farms_dir=fuga_path + 'LUT/Farms/', wind_atlas_path='Horns Rev 1/hornsrev_north_only.lib', climate_interpolation=False)
+        pyFuga = PyFuga()
+        pyFuga.setup(farm_name='Horns Rev 1',
+                     turbine_model_path=fuga_path + 'LUT/', turbine_model_name='Vestas_V80_(2_MW_offshore)[h=67.00]',
+                     tb_x=tb_x, tb_y=tb_y,
+                     mast_position=(0, 0, 70), z0=0.0001, zi=400, zeta0=0,
+                     farms_dir=fuga_path + 'LUT/Farms/', wind_atlas_path='Horns Rev 1/hornsrev_north_only.lib', climate_interpolation=False)
+        return pyFuga
 
     def testCheckVersion(self):
-        if self.lib_missing(): return
+        if self.lib_missing():
+            return
         lib = PascalDLL(fuga_path + "FugaLib/FugaLib.%s" % ('so', 'dll')[os.name == 'nt'])
         self.assertRaisesRegex(Exception, "This version of FugaLib supports interface version ", lib.CheckInterfaceVersion, 1)
         pyFuga = self.get_fuga()  # check that current interface version match
         pyFuga.cleanup()
 
     def testSetup(self):
-        if self.lib_missing(): return
+        if self.lib_missing():
+            return
         pyFuga = self.get_fuga()
         self.assertEqual(pyFuga.get_no_tubines(), 2)
         self.assertIn("Loading", pyFuga.log)
@@ -63,13 +68,15 @@ class Test(unittest.TestCase):
         pyFuga.cleanup()
 
     def testAEP_one_tb(self):
-        if self.lib_missing(): return
+        if self.lib_missing():
+            return
         pyFuga = self.get_fuga([0], [0])
         np.testing.assert_array_almost_equal(pyFuga.get_aep(np.array([[0], [0]]).T), [7.450272, 7.450272, 0.424962, 1.])
         pyFuga.cleanup()
 
     def testAEP(self):
-        if self.lib_missing(): return
+        if self.lib_missing():
+            return
         pyFuga = self.get_fuga()
 
         np.testing.assert_array_almost_equal(pyFuga.get_aep(np.array([[0, 200], [0, 0]]).T), [14.866138, 14.900544, 0.423981, 0.997691])
@@ -86,7 +93,7 @@ class Test(unittest.TestCase):
 
 #     def test_parallel(self):
 #         from multiprocessing import Pool
-# 
+#
 #         with Pool(5) as p:
 #             print(p.map(_test_parallel, [1, 2]))
 
diff --git a/topfarm/cost_models/fuga/Colonel b/topfarm/cost_models/fuga/Colonel
index 41cdbf94..100b7842 160000
--- a/topfarm/cost_models/fuga/Colonel
+++ b/topfarm/cost_models/fuga/Colonel
@@ -1 +1 @@
-Subproject commit 41cdbf9403170ce0f132f226fa7dab0d4d12b894
+Subproject commit 100b7842bbeb29ba6f51556fbc24d01cf153efe1
diff --git a/topfarm/cost_models/fuga/py_fuga.py b/topfarm/cost_models/fuga/py_fuga.py
index 54c7423d..a7e8f8be 100644
--- a/topfarm/cost_models/fuga/py_fuga.py
+++ b/topfarm/cost_models/fuga/py_fuga.py
@@ -18,16 +18,13 @@ from topfarm.cost_models.fuga.pascal_dll import PascalDLL
 c_double_p = POINTER(c_double)
 c_int_p = POINTER(ctypes.c_int32)
 
+fuga_path = os.path.abspath(os.path.dirname(__file__)) + '/Colonel/'
+
 
 class PyFuga(object):
-    interface_version = 2
-
-    def __init__(self,
-                 farm_name='Horns Rev 1',
-                 turbine_model_path='./LUT/', turbine_model_name='Vestas_V80_(2_MW_offshore)[h=67.00]',
-                 tb_x=[423974, 424033], tb_y=[6151447, 6150889],
-                 mast_position=(0, 0, 70), z0=0.0001, zi=400, zeta0=0,
-                 farms_dir='./LUT/', wind_atlas_path='Horns Rev 1\hornsrev.lib', climate_interpolation=True):
+    interface_version = 3
+
+    def __init__(self):
         atexit.register(self.cleanup)
         with NamedTemporaryFile() as f:
             self.stdout_filename = f.name + "pyfuga.txt"
@@ -38,7 +35,14 @@ class PyFuga(object):
 
         self.lib = PascalDLL(lib_path)
         self.lib.CheckInterfaceVersion(self.interface_version)
-        self.lib.Setup(self.stdout_filename, float(mast_position[0]), float(mast_position[1]), float(mast_position[2]),
+        self.lib.Initialize(self.stdout_filename)
+
+    def setup(self, farm_name='Horns Rev 1',
+              turbine_model_path='./LUT/', turbine_model_name='Vestas_V80_(2_MW_offshore)[h=67.00]',
+              tb_x=[423974, 424033], tb_y=[6151447, 6150889],
+              mast_position=(0, 0, 70), z0=0.0001, zi=400, zeta0=0,
+              farms_dir='./LUT/', wind_atlas_path='Horns Rev 1\hornsrev.lib', climate_interpolation=True):
+        self.lib.Setup(float(mast_position[0]), float(mast_position[1]), float(mast_position[2]),
                        float(z0), float(zi), float(zeta0))
 
         tb_x_ctype = np.array(tb_x, dtype=np.float).ctypes
@@ -51,13 +55,7 @@ class PyFuga(object):
         assert os.path.isfile(farms_dir + wind_atlas_path), farms_dir + wind_atlas_path
         self.lib.SetupWindClimate(farms_dir, wind_atlas_path, climate_interpolation)
 
-        assert len(tb_x) == self.get_no_tubines(), self.log + "\n%d" % self.get_no_tubines()
-
-
-#         self.lib.setup_old(farms_dir, farm_name, turbine_model_path,
-#                        float(mast_position[0]), float(mast_position[1]), float(mast_position[2]),
-#                        float(z0),float(zi),float(zeta0),
-#                        wind_atlas_path)
+        #assert len(tb_x) == self.get_no_tubines(), self.log + "\n%d!=%d" % (self.get_no_tubines(),len(tb_x))
 
     def cleanup(self):
         if hasattr(self, 'lib'):
@@ -74,15 +72,6 @@ class PyFuga(object):
                 except Exception:
                     pass
 
-    def __init__old(self):
-        path = r'C:\mmpe\programming\pascal\Fuga\Colonel\FugaLib/'
-        self.lib = PascalDLL(path + 'FugaLib.dll')
-
-        self.lib.setup(path + '../LUT/Farms/', 'Horns Rev 1', path + '../LUT/',
-                       0., 0., 70.,
-                       0.0001, 400., 0.,
-                       'Horns Rev 1\hornsrev0.lib')
-
     def get_no_tubines(self):
         no_turbines_p = c_int_p(c_int(0))
         self.lib.GetNoTurbines(no_turbines_p)
diff --git a/topfarm/cost_models/fuga/tests/__init__.py b/topfarm/cost_models/fuga/tests/__init__.py
deleted file mode 100644
index e69de29b..00000000
-- 
GitLab