def test(): print('Testing Vector class...') a = Vector(1, 1, 1) b = Vector(2, 2, 2) c = Vector(1, 1, 1) np.testing.assert_almost_equal(a.getArray(), np.array([1, 1, 1])) assert a == c alen = a.mag() np.testing.assert_almost_equal(alen, 1.73205, decimal=5) anorm = a.norm() bnorm = b.norm() assert anorm == bnorm acrossb = a.cross(b) assert acrossb == Vector(0, 0, 0) adotb = a.dot(b) assert adotb == 6 aplusb = a + b print('Passed Vector class tests.')
def __setPseudoHypocenters(self): """ Set a pseudo-hypocenter. Adapted from ShakeMap 3.5 src/contour/directivity.c From Bayless and Somerville: "Define the pseudo-hypocenter for rupture of successive segments as the point on the side edge of the rupture segment that is closest to the side edge of the previous segment, and that lies half way between the top and bottom of the rupture. We assume that the rupture is segmented along strike, not updip. All geometric parameters are computed relative to the pseudo-hypocenter." """ hyp_ecef = Vector.fromPoint( geo.point.Point(self._hyp.longitude, self._hyp.latitude, self._hyp.depth)) # Loop over each quad self.phyp = [None] * self._nq for i in range(self._nq): P0, P1, P2, P3 = self._rup.getQuadrilaterals()[i] p0 = Vector.fromPoint(P0) # convert to ECEF p1 = Vector.fromPoint(P1) p2 = Vector.fromPoint(P2) p3 = Vector.fromPoint(P3) # Create 4 planes with normals pointing outside rectangle hpnp = Vector.cross(p1 - p0, p2 - p0).norm() hpp = -hpnp.x * p0.x - hpnp.y * p0.y - hpnp.z * p0.z n0 = Vector.cross(p1 - p0, hpnp) n1 = Vector.cross(p2 - p1, hpnp) n2 = Vector.cross(p3 - p2, hpnp) n3 = Vector.cross(p0 - p3, hpnp) #------------------------------------------------------------------- # Is the hypocenter inside the projected rectangle? # Dot products show which side the origin is on. # If origin is on same side of all the planes, then it is 'inside' #------------------------------------------------------------------- sgn0 = np.signbit(Vector.dot(n0, p0 - hyp_ecef)) sgn1 = np.signbit(Vector.dot(n1, p1 - hyp_ecef)) sgn2 = np.signbit(Vector.dot(n2, p2 - hyp_ecef)) sgn3 = np.signbit(Vector.dot(n3, p3 - hyp_ecef)) if (sgn0 == sgn1) and (sgn1 == sgn2) and (sgn2 == sgn3): # Origin is inside. Use distance-to-plane formula. d = Vector.dot(hpnp, hyp_ecef) + hpp d = d * d # Put the pseudo hypocenter on the plane D = Vector.dot(hpnp, hyp_ecef) + hpp self.phyp[i] = hyp_ecef - hpnp * D else: # Origin is outside. Find distance to edges p0p = np.reshape(p0.getArray() - hyp_ecef.getArray(), [1, 3]) p1p = np.reshape(p1.getArray() - hyp_ecef.getArray(), [1, 3]) p2p = np.reshape(p2.getArray() - hyp_ecef.getArray(), [1, 3]) p3p = np.reshape(p3.getArray() - hyp_ecef.getArray(), [1, 3]) s1 = _distance_sq_to_segment(p1p, p2p) s3 = _distance_sq_to_segment(p3p, p0p) # Assuming that the rupture is segmented along strike and not # updip (as described by Bayless and somerville), we only # need to consider s1 and s3: if s1 > s3: e30 = p0 - p3 e30norm = e30.norm() mag = e30.mag() self.phyp[i] = p3 + e30norm * (0.5 * mag) else: e21 = p1 - p2 e21norm = e21.norm() mag = e21.mag() self.phyp[i] = p2 + e21norm * (0.5 * mag)
def __setPseudoHypocenters(self): """ Set a pseudo-hypocenter. Adapted from ShakeMap 3.5 src/contour/directivity.c From Bayless and Somerville: "Define the pseudo-hypocenter for rupture of successive segments as the point on the side edge of the fault segment that is closest to the side edge of the previous segment, and that lies half way between the top and bottom of the fault. We assume that the fault is segmented along strike, not updip. All geometric parameters are computed relative to the pseudo-hypocenter." """ hyp_ecef = Vector.fromPoint( geo.point.Point(self._hyp.longitude, self._hyp.latitude, self._hyp.depth)) # Loop over each quad self.phyp = [None] * self._nq for i in range(self._nq): P0, P1, P2, P3 = self._flt.getQuadrilaterals()[i] p0 = Vector.fromPoint(P0) # convert to ECEF p1 = Vector.fromPoint(P1) p2 = Vector.fromPoint(P2) p3 = Vector.fromPoint(P3) # Create 4 planes with normals pointing outside rectangle hpnp = Vector.cross(p1 - p0, p2 - p0).norm() hpp = -hpnp.x * p0.x - hpnp.y * p0.y - hpnp.z * p0.z n0 = Vector.cross(p1 - p0, hpnp) n1 = Vector.cross(p2 - p1, hpnp) n2 = Vector.cross(p3 - p2, hpnp) n3 = Vector.cross(p0 - p3, hpnp) # Is the hypocenter inside the projected rectangle? # Dot products show which side the origin is on. # If origin is on same side of all the planes, then it is 'inside' sgn0 = np.signbit(Vector.dot(n0, p0 - hyp_ecef)) sgn1 = np.signbit(Vector.dot(n1, p1 - hyp_ecef)) sgn2 = np.signbit(Vector.dot(n2, p2 - hyp_ecef)) sgn3 = np.signbit(Vector.dot(n3, p3 - hyp_ecef)) if (sgn0 == sgn1) and (sgn1 == sgn2) and (sgn2 == sgn3): # Origin is inside. Use distance-to-plane formula. d = Vector.dot(hpnp, hyp_ecef) + hpp d = d * d # Put the pseudo hypocenter on the plane D = Vector.dot(hpnp, hyp_ecef) + hpp self.phyp[i] = hyp_ecef - hpnp * D else: # Origin is outside. Find distance to edges p0p = np.reshape(p0.getArray() - hyp_ecef.getArray(), [1, 3]) p1p = np.reshape(p1.getArray() - hyp_ecef.getArray(), [1, 3]) p2p = np.reshape(p2.getArray() - hyp_ecef.getArray(), [1, 3]) p3p = np.reshape(p3.getArray() - hyp_ecef.getArray(), [1, 3]) s1 = dist2_to_segment(p1p, p2p) s2 = dist2_to_segment(p2p, p3p) s3 = dist2_to_segment(p3p, p0p) # Assuming that the fault is segmented along strike and not # updip (as described by Bayless and somerville), we only # need to consider s1 and s3: if s1 > s3: e30 = p0 - p3 e30norm = e30.norm() mag = e30.mag() self.phyp[i] = p3 + e30norm * (0.5 * mag) else: e21 = p1 - p2 e21norm = e21.norm() mag = e21.mag() self.phyp[i] = p2 + e21norm * (0.5 * mag)