def get_rup_array(ebruptures): """ Convert a list of EBRuptures into a numpy composite array """ if not BaseRupture._code: BaseRupture.init() # initialize rupture codes lst = [] geoms = [] nbytes = 0 offset = 0 for ebrupture in ebruptures: rup = ebrupture.rupture mesh = surface_to_array(rup.surface) sy, sz = mesh.shape[1:] # sanity checks assert sy < TWO16, 'Too many multisurfaces: %d' % sy assert sz < TWO16, 'The rupture mesh spacing is too small' hypo = rup.hypocenter.x, rup.hypocenter.y, rup.hypocenter.z rate = getattr(rup, 'occurrence_rate', numpy.nan) points = mesh.reshape(3, -1).T # shape (n, 3) n = len(points) tup = (ebrupture.serial, ebrupture.srcidx, ebrupture.grp_id, rup.code, ebrupture.n_occ, offset, offset + n, -1, rup.mag, rup.rake, rate, hypo, sy, sz) offset += n lst.append(tup) geoms.append(numpy.array([tuple(p) for p in points], point3d)) nbytes += rupture_dt.itemsize + mesh.nbytes dic = dict(geom=numpy.concatenate(geoms), nbytes=nbytes) # TODO: PMFs for nonparametric ruptures are not converted return hdf5.ArrayWrapper(numpy.array(lst, rupture_dt), dic)
def make_non_parametric_source(): surf1 = PlanarSurface(strike=0, dip=90, top_left=Point(0., -1., 0.), top_right=Point(0., 1., 0.), bottom_right=Point(0., 1., 10.), bottom_left=Point(0., -1., 10.)) surf2 = PlanarSurface(strike=90., dip=90., top_left=Point(-1., 0., 0.), top_right=Point(1., 0., 0.), bottom_right=Point(1., 0., 10.), bottom_left=Point(-1., 0., 10.)) rup1 = BaseRupture(mag=5., rake=90., tectonic_region_type='ASC', hypocenter=Point(0., 0., 5.), surface=surf1) rup2 = BaseRupture(mag=6, rake=0, tectonic_region_type='ASC', hypocenter=Point(0., 0., 5.), surface=surf2) pmf1 = PMF([(0.7, 0), (0.3, 1)]) pmf2 = PMF([(0.7, 0), (0.2, 1), (0.1, 2)]) kwargs = { 'source_id': 'source_id', 'name': 'source name', 'tectonic_region_type': 'tectonic region', 'data': [(rup1, pmf1), (rup2, pmf2)] } npss = NonParametricSeismicSource(**kwargs) assert_pickleable(npss) return npss, kwargs
def get_rup_array(ebruptures, srcfilter=nofilter): """ Convert a list of EBRuptures into a numpy composite array, by filtering out the ruptures far away from every site """ if not BaseRupture._code: BaseRupture.init() # initialize rupture codes rups = [] geoms = [] nbytes = 0 offset = 0 for ebrupture in ebruptures: rup = ebrupture.rupture mesh = surface_to_array(rup.surface) sy, sz = mesh.shape[1:] # sanity checks assert sy < TWO16, 'Too many multisurfaces: %d' % sy assert sz < TWO16, 'The rupture mesh spacing is too small' hypo = rup.hypocenter.x, rup.hypocenter.y, rup.hypocenter.z points = mesh.reshape(3, -1).T # shape (n, 3) rec = numpy.zeros(1, rupture_dt)[0] rec['serial'] = rup.rup_id rec['minlon'] = minlon = points[:, 0].min() rec['minlat'] = minlat = points[:, 1].min() rec['maxlon'] = maxlon = points[:, 0].max() rec['maxlat'] = maxlat = points[:, 1].max() rec['mag'] = rup.mag rec['hypo'] = hypo if srcfilter.integration_distance and len( srcfilter.close_sids(rec, rup.tectonic_region_type)) == 0: continue rate = getattr(rup, 'occurrence_rate', numpy.nan) tup = (0, ebrupture.rup_id, ebrupture.srcidx, ebrupture.grp_id, rup.code, ebrupture.n_occ, rup.mag, rup.rake, rate, minlon, minlat, maxlon, maxlat, hypo, offset, offset + len(points), sy, sz, 0, 0) #,ebrupture.source_id) offset += len(points) rups.append(tup) geoms.append(numpy.array([tuple(p) for p in points], point3d)) nbytes += rupture_dt.itemsize + mesh.nbytes if not rups: return () dic = dict(geom=numpy.concatenate(geoms), nbytes=nbytes) # NB: PMFs for nonparametric ruptures are not saved since they # are useless for the GMF computation return hdf5.ArrayWrapper(numpy.array(rups, rupture_dt), dic)
def _create_rupture(distance, magnitude, tectonic_region_type=TRT.ACTIVE_SHALLOW_CRUST): # Return a rupture with a fixed geometry located at a given r_jb distance # from a site located at (0.0, 0.0). # parameter float distance: # Joyner and Boore rupture-site distance # parameter float magnitude: # Rupture magnitude # Find the point at a given distance lonp, latp = point_at(0.0, 0.0, 90., distance) mag = magnitude rake = 0.0 tectonic_region_type = tectonic_region_type hypocenter = Point(lonp, latp, 2.5) surface = PlanarSurface.from_corner_points( Point(lonp, -1, 0.), Point(lonp, +1, 0.), Point(lonp, +1, 5.), Point(lonp, -1, 5.)) surface = SimpleFaultSurface.from_fault_data( fault_trace=Line([Point(lonp, -1), Point(lonp, 1)]), upper_seismogenic_depth=0.0, lower_seismogenic_depth=5.0, dip=90.0, mesh_spacing=1.0) # check effective rupture-site distance from openquake.hazardlib.geo.mesh import Mesh mesh = Mesh(numpy.array([0.0]), numpy.array([0.0])) assert abs(surface.get_joyner_boore_distance(mesh)-distance) < 1e-2 return BaseRupture(mag, rake, tectonic_region_type, hypocenter, surface, NonParametricSeismicSource)
def get_rup_array(ebruptures, srcfilter=nofilter): """ Convert a list of EBRuptures into a numpy composite array, by filtering out the ruptures far away from every site """ if not BaseRupture._code: BaseRupture.init() # initialize rupture codes rups = [] geoms = [] nbytes = 0 offset = 0 for ebrupture in ebruptures: rup = ebrupture.rupture mesh = surface_to_array(rup.surface) sy, sz = mesh.shape[1:] # sanity checks assert sy < TWO16, 'Too many multisurfaces: %d' % sy assert sz < TWO16, 'The rupture mesh spacing is too small' points = mesh.reshape(3, -1).T # shape (n, 3) minlon = points[:, 0].min() minlat = points[:, 1].min() maxlon = points[:, 0].max() maxlat = points[:, 1].max() if srcfilter.integration_distance and len(srcfilter.close_sids( (minlon, minlat, maxlon, maxlat), rup.tectonic_region_type, rup.mag)) == 0: continue hypo = rup.hypocenter.x, rup.hypocenter.y, rup.hypocenter.z rate = getattr(rup, 'occurrence_rate', numpy.nan) tup = (ebrupture.serial, ebrupture.srcidx, ebrupture.grp_id, rup.code, ebrupture.n_occ, rup.mag, rup.rake, rate, minlon, minlat, maxlon, maxlat, hypo, offset, offset + len(points), sy, sz) offset += len(points) rups.append(tup) geoms.append(numpy.array([tuple(p) for p in points], point3d)) nbytes += rupture_dt.itemsize + mesh.nbytes if not rups: return () dic = dict(geom=numpy.concatenate(geoms), nbytes=nbytes) # TODO: PMFs for nonparametric ruptures are not converted return hdf5.ArrayWrapper(numpy.array(rups, rupture_dt), dic)
def get_source_ids(ebruptures, srcfilter): """ Save source_id given by source model and srcidx found in ebruptures :param ebruptures: list of EBRuptures objects """ if not BaseRupture._code: BaseRupture.init() # initialize rupture codes srcs = [] for ebrupture in ebruptures: rup = ebrupture.rupture mesh = surface_to_array(rup.surface) sy, sz = mesh.shape[1:] # sanity checks assert sy < TWO16, 'Too many multisurfaces: %d' % sy assert sz < TWO16, 'The rupture mesh spacing is too small' hypo = rup.hypocenter.x, rup.hypocenter.y, rup.hypocenter.z points = mesh.reshape(3, -1).T # shape (n, 3) rec = numpy.zeros(1, rupture_dt)[0] rec['minlon'] = points[:, 0].min() rec['minlat'] = points[:, 1].min() rec['maxlon'] = points[:, 0].max() rec['maxlat'] = points[:, 1].max() rec['mag'] = rup.mag rec['hypo'] = hypo if srcfilter.integration_distance and len( srcfilter.close_sids(rec, rup.tectonic_region_type)) == 0: continue tup = (ebrupture.source_id, ebrupture.srcidx, ebrupture.trt) #if tup not in srcs: srcs.append(tup) if not srcs: return () dic = {} return hdf5.ArrayWrapper(numpy.array(srcs, source_ids_dt), dic)
def get_rupture(self, **kwargs): # Parameters if 'mag' in kwargs: mag = kwargs['mag'] else: mag = 6.0 if 'rake' in kwargs: rake = kwargs['rake'] else: rake = 0 if 'trt' in kwargs: trt = kwargs['trt'] else: trt = 0 # Get surface sfc, hyp = self.get_surface() # Create rupture rup = BaseRupture(mag, rake, trt, hyp, sfc) vars(rup).update(kwargs) # Set attributes return rup
import logging import unittest.mock as mock import numpy from openquake.baselib import hdf5, datastore, general from openquake.hazardlib.gsim.base import ContextMaker, FarAwayRupture from openquake.hazardlib import calc, probability_map, stats from openquake.hazardlib.source.rupture import ( EBRupture, BaseRupture, events_dt, RuptureProxy) from openquake.risklib.riskinput import rsi2str from openquake.commonlib.calc import _gmvs_to_haz_curve U16 = numpy.uint16 U32 = numpy.uint32 F32 = numpy.float32 by_taxonomy = operator.attrgetter('taxonomy') code2cls = BaseRupture.init() def build_stat_curve(poes, imtls, stat, weights): """ Build statistics by taking into account IMT-dependent weights """ assert len(poes) == len(weights), (len(poes), len(weights)) L = len(imtls.array) array = numpy.zeros((L, 1)) if isinstance(weights, list): # IMT-dependent weights # this is slower since the arrays are shorter for imt in imtls: slc = imtls(imt) ws = [w[imt] for w in weights] if sum(ws) == 0: # expect no data for this IMT
# in this way even on 32 bit machines Python will not have to convert # the generated seed into a long integer U8 = numpy.uint8 U16 = numpy.uint16 I32 = numpy.int32 U32 = numpy.uint32 F32 = numpy.float32 U64 = numpy.uint64 F64 = numpy.float64 event_dt = numpy.dtype([('eid', U64), ('ses', U32), ('sample', U32)]) sids_dt = h5py.special_dtype(vlen=U32) BaseRupture.init() # initialize rupture codes # ############## utilities for the classical calculator ############### # def convert_to_array(pmap, nsites, imtls): """ Convert the probability map into a composite array with header of the form PGA-0.1, PGA-0.2 ... :param pmap: probability map :param nsites: total number of sites :param imtls: a DictArray with IMT and levels :returns: a composite array of lenght nsites """ lst = []
def setUp(self): super(MakeContextsTestCase, self).setUp() self.site1_location = Point(1, 2) self.site2_location = Point(-2, -3) self.site1 = Site(vs30=456, vs30measured=False, z1pt0=12.1, z2pt5=15.1, location=self.site1_location) self.site2 = Site(vs30=1456, vs30measured=True, z1pt0=112.1, z2pt5=115.1, location=self.site2_location) min_distance = numpy.array([10, 11]) rx_distance = numpy.array([4, 5]) jb_distance = numpy.array([6, 7]) ry0_distance = numpy.array([8, 9]) azimuth = numpy.array([12, 34]) top_edge_depth = 30 width = 15 strike = 60.123 class FakeSurface(object): call_counts = collections.Counter() def get_azimuth(self, mesh): self.call_counts['get_azimuth'] += 1 return azimuth def get_strike(self): self.call_counts['get_strike'] += 1 return strike def get_dip(self): self.call_counts['get_dip'] += 1 return 45.4545 def get_min_distance(fake_surface, mesh): self.assertIsInstance(mesh, Mesh) [point1, point2] = mesh self.assertEqual(point1, self.site1_location) self.assertEqual(point2, self.site2_location) fake_surface.call_counts['get_min_distance'] += 1 return min_distance def get_rx_distance(fake_surface, mesh): self.assertIsInstance(mesh, Mesh) [point1, point2] = mesh self.assertEqual(point1, self.site1_location) self.assertEqual(point2, self.site2_location) fake_surface.call_counts['get_rx_distance'] += 1 return rx_distance def get_ry0_distance(fake_surface, mesh): self.assertIsInstance(mesh, Mesh) [point1, point2] = mesh self.assertEqual(point1, self.site1_location) self.assertEqual(point2, self.site2_location) fake_surface.call_counts['get_ry0_distance'] += 1 return ry0_distance def get_joyner_boore_distance(fake_surface, mesh): self.assertIsInstance(mesh, Mesh) [point1, point2] = mesh self.assertEqual(point1, self.site1_location) self.assertEqual(point2, self.site2_location) fake_surface.call_counts['get_joyner_boore_distance'] += 1 return jb_distance def get_top_edge_depth(fake_surface): fake_surface.call_counts['get_top_edge_depth'] += 1 return top_edge_depth def get_width(fake_surface): fake_surface.call_counts['get_width'] += 1 return width self.rupture_hypocenter = Point(2, 3, 40) self.rupture = BaseRupture(mag=123.45, rake=123.56, tectonic_region_type=const.TRT.VOLCANIC, hypocenter=self.rupture_hypocenter, surface=FakeSurface(), source_typology=object()) self.gsim_class.DEFINED_FOR_TECTONIC_REGION_TYPE = const.TRT.VOLCANIC self.fake_surface = FakeSurface
def get_rup_array(ebruptures, srcfilter=nofilter): """ Convert a list of EBRuptures into a numpy composite array, by filtering out the ruptures far away from every site """ if not BaseRupture._code: BaseRupture.init() # initialize rupture codes rups = [] geoms = [] for ebrupture in ebruptures: rup = ebrupture.rupture arrays = surface_to_arrays(rup.surface) # one array per surface points = [] shapes = [] for array in arrays: s0, s1, s2 = array.shape assert s0 == 3, s0 assert s1 < TWO16, 'Too many lines' assert s2 < TWO16, 'The rupture mesh spacing is too small' shapes.append(s1) shapes.append(s2) points.extend(array.flat) # example of points: [25.0, 25.1, 25.1, 25.0, # -24.0, -24.0, -24.1, -24.1, # 5.0, 5.0, 5.0, 5.0] points = F32(points) shapes = U32(shapes) hypo = rup.hypocenter.x, rup.hypocenter.y, rup.hypocenter.z rec = numpy.zeros(1, rupture_dt)[0] rec['seed'] = rup.rup_id n = len(points) // 3 lons = points[0:n] lats = points[n:2 * n] rec['minlon'] = minlon = lons.min() rec['minlat'] = minlat = lats.min() rec['maxlon'] = maxlon = lons.max() rec['maxlat'] = maxlat = lats.max() rec['mag'] = rup.mag rec['hypo'] = hypo if srcfilter.integration_distance and len( srcfilter.close_sids(rec, rup.tectonic_region_type)) == 0: continue rate = getattr(rup, 'occurrence_rate', numpy.nan) tup = (0, ebrupture.rup_id, ebrupture.source_id, ebrupture.trt_smrlz, rup.code, ebrupture.n_occ, rup.mag, rup.rake, rate, minlon, minlat, maxlon, maxlat, hypo, 0, 0) rups.append(tup) # we are storing the geometries as arrays of 32 bit floating points; # the first element is the number of surfaces, then there are # 2 * num_surfaces integers describing the first and second # dimension of each surface, and then the lons, lats and deps of # the underlying meshes of points. geom = numpy.concatenate([[len(shapes) // 2], shapes, points]) geoms.append(geom) if not rups: return () dic = dict(geom=numpy.array(geoms, object)) # NB: PMFs for nonparametric ruptures are not saved since they # are useless for the GMF computation return hdf5.ArrayWrapper(numpy.array(rups, rupture_dt), dic)
from openquake.hazardlib import calc, probability_map TWO16 = 2**16 MAX_INT = 2**31 - 1 # this is used in the random number generator # in this way even on 32 bit machines Python will not have to convert # the generated seed into a long integer U8 = numpy.uint8 U16 = numpy.uint16 I32 = numpy.int32 U32 = numpy.uint32 F32 = numpy.float32 U64 = numpy.uint64 F64 = numpy.float64 BaseRupture.init() # ############## utilities for the classical calculator ############### # def convert_to_array(pmap, nsites, imtls, inner_idx=0): """ Convert the probability map into a composite array with header of the form PGA-0.1, PGA-0.2 ... :param pmap: probability map :param nsites: total number of sites :param imtls: a DictArray with IMT and levels :returns: a composite array of lenght nsites """ lst = []
from openquake.hazardlib import calc, probability_map TWO16 = 2 ** 16 MAX_INT = 2 ** 31 - 1 # this is used in the random number generator # in this way even on 32 bit machines Python will not have to convert # the generated seed into a long integer U8 = numpy.uint8 U16 = numpy.uint16 I32 = numpy.int32 U32 = numpy.uint32 F32 = numpy.float32 U64 = numpy.uint64 F64 = numpy.float64 BaseRupture.init() # ############## utilities for the classical calculator ############### # def convert_to_array(pmap, nsites, imtls, inner_idx=0): """ Convert the probability map into a composite array with header of the form PGA-0.1, PGA-0.2 ... :param pmap: probability map :param nsites: total number of sites :param imtls: a DictArray with IMT and levels :returns: a composite array of lenght nsites """ lst = []