Commit 12b269e1 authored by Mikkel Friis-Møller's avatar Mikkel Friis-Møller
Browse files

fixing bug in interaction between MultiPolygonBoundaryComp and design vars

parent 44f49c3a
Pipeline #30634 passed with stages
in 18 minutes and 45 seconds
......@@ -41,5 +41,6 @@ setup(name='topfarm',
'mock', # replace variables during tests
'tensorflow', # loads examples with surrogates
'shapely', # for multiple polygon boundaries and exclusion zones
],
'memory_profiler', # memory profiling
],
zip_safe=True)
......@@ -33,7 +33,7 @@ class XYBoundaryConstraint(Constraint):
if np.ndim(boundary[0][0]) < 2:
self.multi_boundary = [(np.asarray(boundary), 1)]
else:
self.multi_boundary = boundary
self.multi_boundary = [(np.asarray(bound), boolean) for bound, boolean in boundary]
boundary = boundary[0][0]
self.boundary = np.asarray(boundary)
self.boundary_type = boundary_type
......@@ -56,9 +56,15 @@ class XYBoundaryConstraint(Constraint):
return self.boundary_comp
def set_design_var_limits(self, design_vars):
if hasattr(self, 'multi_boundary'):
bound_min = np.vstack([(bound[0]).min(0) for bound in self.multi_boundary]).min(0)
bound_max = np.vstack([(bound[0]).max(0) for bound in self.multi_boundary]).max(0)
else:
bound_min = self.boundary_comp.xy_boundary.min(0)
bound_max = self.boundary_comp.xy_boundary.max(0)
for k, l, u in zip([topfarm.x_key, topfarm.y_key],
self.boundary_comp.xy_boundary.min(0),
self.boundary_comp.xy_boundary.max(0)):
bound_min,
bound_max):
if k in design_vars:
if len(design_vars[k]) == 4:
design_vars[k] = (design_vars[k][0], np.maximum(design_vars[k][1], l),
......@@ -175,7 +181,7 @@ class BoundaryBaseComp(ConstraintComponent):
"""Plot boundary"""
if isinstance(self, MultiPolygonBoundaryComp):
colors = ['--k', 'k']
for bound, io in self.xy_multi_boundary:
for bound, io in self.boundaries:
ax.plot(np.asarray(bound)[:, 0].tolist() + [np.asarray(bound)[0, 0]],
np.asarray(bound)[:, 1].tolist() + [np.asarray(bound)[0, 1]], colors[io])
else:
......
......@@ -20,24 +20,6 @@ def testPolygonConcave():
plot_comp.show()
def testMultiPolygon():
optimal = [(1.75, 1.3), (4, 1)]
boundary = [([(0, 0), (5, 0), (5, 2), (3, 2), (3, 1), (2, 1), (2, 2), (0, 2), (0, 0)], 1),
([(3.5, 0.5), (4.5, 0.5), (4.5, 1.5), (3.5, 1.5)], 1),
([(0.5, 0.5), (1.75, 0.5), (1.75, 1.5), (0.5, 1.5)], 0),
([(0.75, 0.75), (1.25, 0.75), (1.25, 1.25), (0.75, 1.25)], 0),
]
plot_comp = NoPlot() # DummyCostPlotComp(optimal)
initial = [(-0, .1), (4, 1.5)][::-1]
tf = TopFarm(initial, DummyCost(optimal, inputs=['x', 'y']), 0,
boundary=boundary, boundary_type='multi_polygon', plot_comp=plot_comp,
driver=EasyScipyOptimizeDriver(tol=1e-8, disp=False))
tf.evaluate()
tf.optimize()
np.testing.assert_array_almost_equal(tf.turbine_positions[:, :2], optimal, 4)
plot_comp.show()
def testPolygonTwoRegionsStartInWrong():
optimal = [(1, 1), (4, 1)]
boundary = [(0, 0), (5, 0), (5, 2), (3, 2), (3, 0), (2, 0), (2, 2), (0, 2), (0, 0)]
......
......@@ -52,3 +52,21 @@ def testPolygonTwoRegionsStartInWrong():
tf.optimize()
plot_comp.show()
np.testing.assert_array_almost_equal(tf.turbine_positions[:, :2], optimal, 4)
def testMultiPolygon():
optimal = [(1.75, 1.3), (4, 1)]
boundary = [([(0, 0), (5, 0), (5, 2), (3, 2), (3, 1), (2, 1), (2, 2), (0, 2), (0, 0)], 1),
([(3.5, 0.5), (4.5, 0.5), (4.5, 1.5), (3.5, 1.5)], 1),
([(0.5, 0.5), (1.75, 0.5), (1.75, 1.5), (0.5, 1.5)], 0),
([(0.75, 0.75), (1.25, 0.75), (1.25, 1.25), (0.75, 1.25)], 0),
]
plot_comp = NoPlot() # DummyCostPlotComp(optimal)
initial = [(-0, .1), (4, 1.5)][::-1]
tf = TopFarm(initial, DummyCost(optimal, inputs=['x', 'y']), 0,
boundary=boundary, boundary_type='multi_polygon', plot_comp=plot_comp,
driver=EasyScipyOptimizeDriver(tol=1e-8, disp=False))
tf.evaluate()
tf.optimize()
np.testing.assert_array_almost_equal(tf.turbine_positions[:, :2], optimal, 4)
plot_comp.show()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment