Skip to content
Snippets Groups Projects
Commit 56568e44 authored by Mads M. Pedersen's avatar Mads M. Pedersen
Browse files

fix introduced bugs

parent 881ce532
No related branches found
No related tags found
1 merge request!94Handle disabled mpi
......@@ -81,7 +81,7 @@ class TopFarmProblem(Problem):
self.driver.add_recorder(self.recorder)
self.run_driver()
self.cleanup()
if self.driver._rec_mgr._recorders!=[]: # in openmdao<2.4 cleanup does not delete recorders
if self.driver._rec_mgr._recorders != []: # in openmdao<2.4 cleanup does not delete recorders
self.driver._rec_mgr._recorders.remove(self.recorder)
if isinstance(self.driver, DOEDriver):
costs = self.recorder.get('cost')
......@@ -116,9 +116,9 @@ class TopFarmProblem(Problem):
assert isinstance(self.driver, DOEDriver), 'get_DOE_list only applies to DOEDrivers, and the current driver is: %s' % type(self.driver)
case_gen = self.driver.options['generator']
return [c for c in case_gen(self.model.get_design_vars(recurse=True), self.model)]
def get_DOE_array(self):
return np.array([[v for k,v in c] for c in self.get_DOE_list()])
return np.array([[v for k, v in c] for c in self.get_DOE_list()])
class TurbineTypeOptimizationProblem(TopFarmProblem):
......@@ -329,16 +329,16 @@ def try_me():
plot_comp=plot_comp,
driver=EasyScipyOptimizeDriver(disp=False),
record_id='test:latest')
cost, state, recorder = xyz_opt_problem.optimize()
recorder.save()
tf = TurbineTypeOptimizationProblem(
cost_comp=xyz_opt_problem,
turbineTypes=[0, 0], lower=0, upper=1,
driver=DOEDriver(FullFactorialGenerator(2)))
cost, state, recorder = tf.optimize()
print (state)
print(state)
plot_comp.show()
......
......@@ -37,18 +37,18 @@ def test_list_driver(get_tf):
[('turbineX', [4, 3]), ('turbineY', [6, 5]), ('turbineZ', [2, 1])]]
tf = get_tf(driver=lst) # pure list
npt.assert_array_equal(tf.get_DOE_list(), xyz)
npt.assert_array_equal(tf.get_DOE_array(), xyz)
tf = get_tf(driver=ListGenerator(lst)) # list generator
npt.assert_array_equal(tf.get_DOE_list(), xyz)
npt.assert_array_equal(tf.get_DOE_array(), xyz)
tf = get_tf(driver=DOEDriver(ListGenerator(lst))) # DOEDriver
npt.assert_array_equal(tf.get_DOE_list(), xyz)
npt.assert_array_equal(tf.get_DOE_array(), xyz)
def test_with_uniform_generator(get_tf):
tf = get_tf(driver=DOEDriver(UniformGenerator(10)))
arr = tf.get_DOE_list()
arr = tf.get_DOE_array()
uta.assertGreaterEqual(arr[:, 0].min(), 10) # x
uta.assertLessEqual(arr[:, 0].max(), 11) # x
uta.assertGreaterEqual(arr[:, 1].min(), 6) # y
......@@ -63,7 +63,7 @@ def test_with_constrained_generator_convex_boundary(get_tf):
tf = get_tf(xy_boundary=[(0, 0), (10, 0), (10, 10)], xy_boundary_type='convex_hull',
driver=ConstrainedXYZGenerator(UniformGenerator(10, 0)))
print(tf.xy_boundary)
arr = tf.get_DOE_list()
arr = tf.get_DOE_array()
print(arr.shape)
x, y, z = [arr[:, i] for i in range(3)]
uta.assertGreaterEqual(x.min(), 0) # x
......@@ -75,7 +75,7 @@ def test_with_constrained_generator_polygon(get_tf):
tf = get_tf(xy_boundary=[(0, 0), (10, 0), (10, 10)],
xy_boundary_type='polygon',
driver=ConstrainedXYZGenerator(UniformGenerator(10, 0)))
arr = tf.get_DOE_list()
arr = tf.get_DOE_array()
print(arr.shape)
x, y = [arr[:, i] for i in range(2)]
uta.assertGreaterEqual(x.min(), 0) # x
......@@ -91,8 +91,7 @@ def test_with_constrained_generator_spacing(get_tf):
xy_boundary_type='rectangle',
min_spacing=2,
driver=ConstrainedXYZGenerator(lst))
arr = tf.get_DOE_list()
print(arr)
#x, y, z = [arr[:, i] for i in range(3)]
arr = tf.get_DOE_array()
\ No newline at end of file
x, y, z = [arr[:, i] for i in range(3)]
assert all(np.sqrt(np.diff(x)**2 + np.diff(y)**2) >= 2)
......@@ -23,6 +23,7 @@ optimize
check_gradients
as_component
get_DOE_list
get_DOE_array
"""
......@@ -80,7 +81,7 @@ def test_optimize(turbineTypeOptimizationProblem):
cost, state, recorder = tf.optimize()
assert cost == 0
np.testing.assert_array_equal(state['turbineType'], [2, 0, 1])
doe_list = np.squeeze(tf.get_DOE_list())
doe_list = np.squeeze(tf.get_DOE_array())
np.testing.assert_array_almost_equal(recorder.get('cost'), np.sum((doe_list - [2, 0, 1])**2, 1))
......@@ -139,9 +140,21 @@ def testTopFarmProblem_as_component(turbineTypeOptimizationProblem):
def testTopFarmProblem_get_DOE_list(turbineTypeOptimizationProblem):
tf = turbineTypeOptimizationProblem
npt.assert_array_equal(tf.get_DOE_list().shape, (27, 1, 3))
npt.assert_array_equal(tf.get_DOE_list()[:5], [[[0, 0, 0]],
npt.assert_array_equal(len(tf.get_DOE_list()), 27)
(k,v), = tf.get_DOE_list()[1]
assert k=="indeps.turbineType"
npt.assert_array_equal(v, [1,0,0])
#npt.assert_array_equal(tf.get_DOE_list()[1], [[('indeps.turbineType', array([0., 0., 0.]))], [('indeps.turbineType', array([1., 0., 0.]))]])
def testTopFarmProblem_get_DOE_array(turbineTypeOptimizationProblem):
tf = turbineTypeOptimizationProblem
npt.assert_array_equal(tf.get_DOE_array().shape, (27, 1, 3))
npt.assert_array_equal(tf.get_DOE_array()[:5], [[[0, 0, 0]],
[[1, 0, 0]],
[[2, 0, 0]],
[[0, 1, 0]],
[[1, 1, 0]]])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment