From 62f34d262382411d49a6d546bc6e6a39999ece7a Mon Sep 17 00:00:00 2001 From: dslosky-usgs Date: Mon, 10 Sep 2018 10:56:04 -0600 Subject: [PATCH] Fix intersection and add test for intersection --- setup.py | 2 +- shakecastaebm/performance_point.py | 4 ++-- shakecastaebm/tests/performance_point.py | 29 +++++++++++++++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/setup.py b/setup.py index 23d6b72..c318437 100644 --- a/setup.py +++ b/setup.py @@ -29,7 +29,7 @@ # Versions should comply with PEP440. For a discussion on single-sourcing # the version across setup.py and the project code, see # https://packaging.python.org/en/latest/single_source_version.html - version='0.0a4', + version='0.0a5', description='Potential impact calculation for well defined facilities due to an input earthquake hazard', long_description=long_description, diff --git a/shakecastaebm/performance_point.py b/shakecastaebm/performance_point.py index af23f3c..a7f563c 100644 --- a/shakecastaebm/performance_point.py +++ b/shakecastaebm/performance_point.py @@ -54,8 +54,8 @@ def get_intersection(seg1, seg2, x, y): (dy2 * dx1 - dx2 * dy1)) if (s >= 0 and s <= 1 and t >= 0 and t <= 1): - x_val = abs(round(seg1[0][x] + t * dx1 * 1000) / 1000) - y_val = abs(round(seg1[0][y] + t * dy1 * 1000) / 1000) + x_val = abs(round((seg1[0][x] + t * dx1) * 1000) / 1000) + y_val = abs(round((seg1[0][y] + t * dy1) * 1000) / 1000) return {x: x_val, y: y_val} else: return False diff --git a/shakecastaebm/tests/performance_point.py b/shakecastaebm/tests/performance_point.py index 805b8ec..aa02a43 100644 --- a/shakecastaebm/tests/performance_point.py +++ b/shakecastaebm/tests/performance_point.py @@ -21,6 +21,33 @@ def test_performancePoint(self): self.assertTrue('acc' in pp.keys()) self.assertTrue('period' in pp.keys()) + def test_intersection(self): + capacity = [ + {'disp': 1, 'acc': .1}, + {'disp': 2, 'acc': 1} + ] + demand = [ + {'disp': 1, 'acc': 1}, + {'disp': 2, 'acc': .1} + ] + + pp = get_performance_point(capacity, demand)[0] + + # slopes for the intersection with the demand + dem_slope1 = ((demand[0]['acc'] - pp['acc']) / + (demand[0]['disp'] - pp['disp'])) + dem_slope2 = ((demand[1]['acc'] - pp['acc']) / + (demand[1]['disp'] - pp['disp'])) + + # slopes for intersection with capacity + cap_slope1 = ((capacity[0]['acc'] - pp['acc']) / + (capacity[0]['disp'] - pp['disp'])) + cap_slope2 = ((capacity[1]['acc'] - pp['acc']) / + (capacity[1]['disp'] - pp['disp'])) + + self.assertAlmostEqual(dem_slope1, dem_slope2) + self.assertAlmostEqual(cap_slope1, cap_slope2) + if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()