def test_Demands(): raise SkipTest wn = wntr.network.WaterNetworkModel() pattern_points1 = [0.5, 1.0, 0.4, 0.2] pattern1 = elements.Pattern('1', multipliers=pattern_points1, time_options=(0, 10)) pattern_points2 = [1.0, 1.2, 1.0] pattern2 = elements.Pattern('2', multipliers=pattern_points2, time_options=(0, 10)) demand1 = elements.TimeSeries(wn.patterns, 2.5, pattern1, '_base_demand') demand2 = elements.TimeSeries(wn.patterns, 1.0, pattern2, 'residential') demand3 = elements.TimeSeries(wn.patterns, 0.8, pattern2, 'residential') expected1 = 2.5 * np.array(pattern_points1 * 3) expected2 = 1.0 * np.array(pattern_points2 * 4) expected3 = 0.8 * np.array(pattern_points2 * 4) expectedtotal = expected1 + expected2 + expected3 expectedresidential = expected2 + expected3 demandlist1 = elements.Demands(wn.patterns, demand1, demand2, demand3) demandlist2 = elements.Demands(wn.patterns) demandlist2.append(demand1) demandlist2.append(demand1) demandlist2[1] = demand2 demandlist2.append((0.8, pattern2, 'residential')) nose.tools.assert_list_equal(list(demandlist1), list(demandlist2)) demandlist2.extend(demandlist1) nose.tools.assert_equal(len(demandlist1), 3) nose.tools.assert_equal(len(demandlist2), 6) del demandlist2[3] del demandlist2[3] del demandlist2[3] del demandlist2[0] demandlist2.insert(0, demand1) nose.tools.assert_list_equal(list(demandlist1), list(demandlist2)) demandlist2.clear() nose.tools.assert_equal(len(demandlist2), 0) nose.tools.assert_false(demandlist2) nose.tools.assert_equal(demandlist1.at(5), expectedtotal[0]) nose.tools.assert_equal(demandlist1.at(13), expectedtotal[1]) nose.tools.assert_equal(demandlist1.at(13, 'residential'), expectedresidential[1]) nose.tools.assert_true( np.all( np.abs(demandlist1.get_values(0, 110, 10) - expectedtotal) < 1.0e-10)) nose.tools.assert_list_equal(demandlist1.base_demand_list(), [2.5, 1.0, 0.8]) nose.tools.assert_list_equal(demandlist1.base_demand_list('_base_demand'), [2.5]) nose.tools.assert_list_equal(demandlist1.pattern_list(), [pattern1, pattern2, pattern2]) nose.tools.assert_list_equal( demandlist1.pattern_list(category='residential'), [pattern2, pattern2]) nose.tools.assert_list_equal( demandlist1.category_list(), ['_base_demand', 'residential', 'residential'])
def test_pattern_interpolation(self): timing = TimeOptions() timing.pattern_interpolation = True p = elements.Pattern('p1', multipliers=[1, 1.2, 1.6], time_options=timing) self.assertAlmostEqual(p.at(0), 1) self.assertAlmostEqual(p.at(3600), 1.2) self.assertAlmostEqual(p.at(7200), 1.6) self.assertAlmostEqual(p.at(1800), 1.1) self.assertAlmostEqual(p.at(5400), 1.4) self.assertAlmostEqual(p.at(2250), 0.2 / 3600 * 2250 + 1) self.assertAlmostEqual(p.at(9000), 1.3) self.assertAlmostEqual(p.at(12600), 1.1)
def test_Pattern(): pattern_points1 = [1, 2, 3, 4, 3, 2, 1] pattern_points2 = [1.0, 1.2, 1.0 ] pattern_points3 = 3.2 # test constant pattern creation timing1 = TimeOptions() timing1.pattern_start = 0 timing1.pattern_timestep = 1 timing2 = TimeOptions() timing2.pattern_start = 0 timing2.pattern_timestep = 5 pattern1a = elements.Pattern('constant', multipliers=pattern_points3, time_options=timing1) pattern1b = elements.Pattern('constant', multipliers=[pattern_points3], time_options=(0,1)) nose.tools.assert_list_equal(pattern1a.multipliers.tolist(), [pattern_points3]) nose.tools.assert_true(np.all(pattern1a.multipliers == pattern1b.multipliers)) nose.tools.assert_false(id(pattern1a.multipliers) == id(pattern1b.multipliers)) nose.tools.assert_equal(pattern1a.time_options, pattern1b.time_options) # def multipliers setter pattern2a = elements.Pattern('oops', multipliers=pattern_points3, time_options=(0,5)) pattern2b = elements.Pattern('oops', multipliers=pattern_points1, time_options=timing2) pattern2a.multipliers = pattern_points1 nose.tools.assert_equal(pattern2a.time_options, pattern2b.time_options) # test pattern evaluations expected_value = pattern_points1[2] nose.tools.assert_equal(pattern2a[2], expected_value) nose.tools.assert_equal(pattern2b.at(10), expected_value) nose.tools.assert_equal(pattern2b.at(12.5), expected_value) nose.tools.assert_equal(pattern2b.at(14), expected_value) nose.tools.assert_equal(pattern2b.at(9*5), expected_value) nose.tools.assert_not_equal(pattern2b.at(15), expected_value) pattern3 = elements.Pattern('nowrap', multipliers=pattern_points2, time_options=(0, 100), wrap=False) nose.tools.assert_equal(pattern3[5], 0.0) nose.tools.assert_equal(pattern3[-39], 0.0) nose.tools.assert_equal(pattern3.at(-39), 0.0) nose.tools.assert_equal(pattern3.at(50), 1.0) pattern4 = elements.Pattern('constant') nose.tools.assert_equal(len(pattern4), 0) nose.tools.assert_equal(pattern4.at(492), 1.0) pattern5a = elements.Pattern('binary', [0.,0.,1.,1.,1.,1.,0.,0.,0.], time_options=(0, 1), wrap=False) pattern5b = elements.Pattern.binary_pattern('binary', step_size=1, start_time=2, end_time=6, duration=9) nose.tools.assert_false(pattern5a.__eq__(pattern5b)) nose.tools.assert_true(np.all(np.abs(pattern5a.multipliers - pattern5b.multipliers)<1.0e-10))
def test_Pattern(self): pattern_points1 = [1, 2, 3, 4, 3, 2, 1] pattern_points2 = [1.0, 1.2, 1.0] pattern_points3 = 3.2 # test constant pattern creation timing1 = TimeOptions() timing1.pattern_start = 0 timing1.pattern_timestep = 1 timing2 = TimeOptions() timing2.pattern_start = 0 timing2.pattern_timestep = 5 pattern1a = elements.Pattern("constant", multipliers=pattern_points3, time_options=timing1) pattern1b = elements.Pattern("constant", multipliers=[pattern_points3], time_options=(0, 1)) self.assertListEqual(pattern1a.multipliers.tolist(), [pattern_points3]) self.assertTrue(np.all(pattern1a.multipliers == pattern1b.multipliers)) self.assertFalse( id(pattern1a.multipliers) == id(pattern1b.multipliers)) self.assertEqual(pattern1a.time_options, pattern1b.time_options) # def multipliers setter pattern2a = elements.Pattern("oops", multipliers=pattern_points3, time_options=(0, 5)) pattern2b = elements.Pattern("oops", multipliers=pattern_points1, time_options=timing2) pattern2a.multipliers = pattern_points1 self.assertEqual(pattern2a.time_options, pattern2b.time_options) # test pattern evaluations expected_value = pattern_points1[2] self.assertEqual(pattern2a[2], expected_value) self.assertEqual(pattern2b.at(10), expected_value) self.assertEqual(pattern2b.at(12.5), expected_value) self.assertEqual(pattern2b.at(14), expected_value) self.assertEqual(pattern2b.at(9 * 5), expected_value) self.assertNotEqual(pattern2b.at(15), expected_value) pattern3 = elements.Pattern("nowrap", multipliers=pattern_points2, time_options=(0, 100), wrap=False) self.assertEqual(pattern3[5], 0.0) self.assertEqual(pattern3[-39], 0.0) self.assertEqual(pattern3.at(-39), 0.0) self.assertEqual(pattern3.at(50), 1.0) pattern4 = elements.Pattern("constant") self.assertEqual(len(pattern4), 0) self.assertEqual(pattern4.at(492), 1.0) pattern5a = elements.Pattern( "binary", [0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0], time_options=(0, 1), wrap=False, ) pattern5b = elements.Pattern.binary_pattern("binary", step_size=1, start_time=2, end_time=6, duration=9) self.assertFalse(pattern5a.__eq__(pattern5b)) self.assertTrue( np.all( np.abs(pattern5a.multipliers - pattern5b.multipliers) < 1.0e-10))
def test_Demands(self): wn = wntr.network.WaterNetworkModel() pattern_points1 = [0.5, 1.0, 0.4, 0.2] pattern1 = elements.Pattern("1", multipliers=pattern_points1, time_options=(0, 10)) pattern_points2 = [1.0, 1.2, 1.0] pattern2 = elements.Pattern("2", multipliers=pattern_points2, time_options=(0, 10)) demand1 = elements.TimeSeries(wn.patterns, 2.5, pattern1, "_base_demand") demand2 = elements.TimeSeries(wn.patterns, 1.0, pattern2, "residential") demand3 = elements.TimeSeries(wn.patterns, 0.8, pattern2, "residential") expected1 = 2.5 * np.array(pattern_points1 * 3) expected2 = 1.0 * np.array(pattern_points2 * 4) expected3 = 0.8 * np.array(pattern_points2 * 4) expectedtotal = expected1 + expected2 + expected3 expectedresidential = expected2 + expected3 demandlist1 = elements.Demands(wn.patterns, demand1, demand2, demand3) demandlist2 = elements.Demands(wn.patterns) demandlist2.append(demand1) demandlist2.append(demand1) demandlist2[1] = demand2 demandlist2.append((0.8, pattern2, "residential")) self.assertListEqual(list(demandlist1), list(demandlist2)) demandlist2.extend(demandlist1) self.assertEqual(len(demandlist1), 3) self.assertEqual(len(demandlist2), 6) del demandlist2[3] del demandlist2[3] del demandlist2[3] del demandlist2[0] demandlist2.insert(0, demand1) self.assertListEqual(list(demandlist1), list(demandlist2)) demandlist2.clear() self.assertEqual(len(demandlist2), 0) self.assertFalse(demandlist2) raise SkipTest self.assertEqual(demandlist1.at(5), expectedtotal[0]) self.assertEqual(demandlist1.at(13), expectedtotal[1]) self.assertEqual(demandlist1.at(13, "residential"), expectedresidential[1]) self.assertTrue( np.all( np.abs(demandlist1.get_values(0, 110, 10) - expectedtotal) < 1.0e-10)) self.assertListEqual(demandlist1.base_demand_list(), [2.5, 1.0, 0.8]) self.assertListEqual(demandlist1.base_demand_list("_base_demand"), [2.5]) self.assertListEqual(demandlist1.pattern_list(), [pattern1, pattern2, pattern2]) self.assertListEqual(demandlist1.pattern_list(category="residential"), [pattern2, pattern2]) self.assertListEqual(demandlist1.category_list(), ["_base_demand", "residential", "residential"])