From fc733406b073bbf716e9db957c40251361ed739d Mon Sep 17 00:00:00 2001
From: mikf <mikf@dtu.dk>
Date: Wed, 27 Jun 2018 14:42:55 +0200
Subject: [PATCH] misc. updates. try make utils tests pass

---
 topfarm/_topfarm.py                           |  7 ++--
 .../spacing_component.py                      |  1 +
 topfarm/tests/topfarm/test_utils.py           |  4 +--
 topfarm/utils.py                              | 33 ++++++++++---------
 4 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/topfarm/_topfarm.py b/topfarm/_topfarm.py
index 230d40d9..249e3a24 100644
--- a/topfarm/_topfarm.py
+++ b/topfarm/_topfarm.py
@@ -7,9 +7,11 @@ import os
 import time
 import numpy as np
 import warnings
+from openmdao.api import Problem, ScipyOptimizeDriver, IndepVarComp, \
+    SqliteRecorder
 with warnings.catch_warnings():
     warnings.simplefilter('ignore', FutureWarning)
-    from openmdao.api import Problem, ScipyOptimizeDriver, IndepVarComp, \
+
 class TopFarm(object):
     """Optimize wind farm layout in terms of
     - Position of turbines
@@ -26,12 +28,10 @@ class TopFarm(object):
             self.initial_positions = turbines = np.array(turbines)
         elif rerun_case_id is 'latest':
             rerun_case_id = latest_id(case_recorder_dir)
-
             self.initial_positions = turbines = pos_from_case(rerun_case_id)
             print('*Initial positions loaded from file: {}\n'.format(
                     rerun_case_id))
         else:
-
             self.initial_positions = turbines = pos_from_case(rerun_case_id)
         n_wt = turbines.shape[0]
         if boundary_type == 'polygon':
@@ -39,7 +39,6 @@ class TopFarm(object):
         else:
             self.boundary_comp = BoundaryComp(boundary, n_wt, boundary_type)
         self.problem = prob = Problem()
-
         indeps = prob.model.add_subsystem('indeps', IndepVarComp(),
                                           promotes=['*'])
         min_x, min_y = self.boundary_comp.vertices.min(0)
diff --git a/topfarm/constraint_components/spacing_component.py b/topfarm/constraint_components/spacing_component.py
index 2422089c..96c96452 100644
--- a/topfarm/constraint_components/spacing_component.py
+++ b/topfarm/constraint_components/spacing_component.py
@@ -85,3 +85,4 @@ class SpacingComp(ExplicitComponent):
                 dSdy[k, i] = -2 * (turbineY[j] - turbineY[i])
                 # increment turbine pair counter
                 k += 1
+        return dSdx, dSdy
\ No newline at end of file
diff --git a/topfarm/tests/topfarm/test_utils.py b/topfarm/tests/topfarm/test_utils.py
index 055807cd..90aafeed 100644
--- a/topfarm/tests/topfarm/test_utils.py
+++ b/topfarm/tests/topfarm/test_utils.py
@@ -14,13 +14,13 @@ turbines = np.array([[ 2.4999377 , -2.99987763],
                    [ 3.00004123, -6.9999519 ]])
 
 def test_pos_from_case():
-    crf = "..\\test_files\\recordings\cases_20180621_111710.sql"
+    crf = "../test_files/recordings/cases_20180621_111710.sql"
     path = os.path.join(thisdir, crf)
     np.testing.assert_allclose(turbines, pos_from_case(path))
 
 
 def test_latest_id():
-    crd = "..\\test_files\\recordings"
+    crd = "../test_files/recordings"
     path = os.path.join(thisdir, crd)
     ref_path = os.path.join(path,'cases_20180621_111710.sql')
     assert latest_id(path) == ref_path
diff --git a/topfarm/utils.py b/topfarm/utils.py
index df16d39d..18894baf 100644
--- a/topfarm/utils.py
+++ b/topfarm/utils.py
@@ -46,16 +46,16 @@ def latest_id(case_recorder_dir):
     return latest
 
 
-def random_positions(boundary, n_wt, n_iter, step_size, min_space, 
-                     pad = 1.1, plot=False, verbose=True):
+def random_positions(boundary, n_wt, n_iter, step_size, min_space,
+                     pad=1.1, plot=False, verbose=True):
     '''
     Input:
         boundary:   list of tuples, e.g.: [(0, 0), (6, 1), (7, -11), (-1, -10)]
         n_wt:       number of wind turbines
         n_iter:     number of iterations allowed to try and satisfy the minimum
                     spacing constraint
-        step_size:  the multiplier on the spacing gradient that the turbines 
-                    are moved in each step 
+        step_size:  the multiplier on the spacing gradient that the turbines
+                    are moved in each step
         min_space:  the minimum spacing between turbines
         pad:        the multiplier on the boundary gradient
         plot:       plot the generated random layout
@@ -113,8 +113,7 @@ def _random(b):
 
 def _contain(n_wt, turbineX, turbineY, boundary_comp, pad):
     for i in range(0, n_wt):
-        dng = boundary_comp.calc_distance_and_gradients(turbineX,
-                                                        turbineY)
+        dng = boundary_comp.calc_distance_and_gradients(turbineX, turbineY)
         dist = dng[0][i]
         if dist < 0:
             dx = dng[1][i]
@@ -125,23 +124,25 @@ def _contain(n_wt, turbineX, turbineY, boundary_comp, pad):
 
 
 if __name__ == '__main__':
-#    this_dir = os.path.dirname(os.path.abspath(__file__))
-#    crf = r"C:\Sandbox\Git\TopFarm2\topfarm\cases_20180621_111710.sql"
-#    case_recorder_filename = crf
-#    turbines = pos_from_case(case_recorder_filename)
-#    print(turbines)
-#
-#    case_recorder_dir = r'C:\Sandbox\Git\TopFarm2\topfarm'
-#    latest_id = latest_id(case_recorder_dir)
-#    print(latest_id)
+    this_dir = os.getcwd()
+    crf = r"tests\test_files\recordings\cases_20180621_111710.sql"
+    case_recorder_filename = crf
+    path = os.path.join(this_dir, crf)
+    turbines = pos_from_case(path)
+    print(turbines)
+
+    case_recorder_dir = r'tests\test_files\recordings'
+    latest_id = latest_id(case_recorder_dir)
+    print(latest_id)
 
     boundary = [(0, 0), (6, 1), (7, -11), (-1, -10)]
     n_wt = 20
     n_iter = 1000
     step_size = 0.1
     min_space = 2.1
+    pad = 1.01
     plot = True
     verbose = True
     turbines = random_positions(boundary, n_wt, n_iter, step_size, min_space,
-                                plot, verbose)
+                                pad, plot, verbose)
     print(turbines)
-- 
GitLab