示例#1
0
class ScenarioCalculator(base.HazardCalculator):
    """
    Scenario hazard calculator
    """
    is_stochastic = True

    def pre_execute(self):
        """
        Read the site collection and initialize GmfComputer and seeds
        """
        oq = self.oqparam
        cinfo = source.CompositionInfo.fake(readinput.get_gsim_lt(oq))
        self.datastore['csm_info'] = cinfo
        if 'rupture_model' not in oq.inputs:
            logging.warn('There is no rupture_model, the calculator will just '
                         'import data without performing any calculation')
            super().pre_execute()
            return
        self.rup = readinput.get_rupture(oq)
        self.gsims = readinput.get_gsims(oq)
        self.cmaker = ContextMaker(self.gsims, oq.maximum_distance,
                                   {'filter_distance': oq.filter_distance})
        super().pre_execute()
        self.datastore['oqparam'] = oq
        self.rlzs_assoc = cinfo.get_rlzs_assoc()
        E = oq.number_of_ground_motion_fields
        events = numpy.zeros(E, readinput.stored_event_dt)
        events['eid'] = numpy.arange(E)
        ebr = EBRupture(self.rup, 0, self.sitecol.sids, events)
        self.datastore['events'] = ebr.events
        rupser = calc.RuptureSerializer(self.datastore)
        rupser.save([ebr])
        rupser.close()
        self.computer = GmfComputer(ebr, self.sitecol, oq.imtls, self.cmaker,
                                    oq.truncation_level, oq.correl_model)

    def init(self):
        pass

    def execute(self):
        """
        Compute the GMFs and return a dictionary gsim -> array(N, E, I)
        """
        self.gmfa = collections.OrderedDict()
        if 'rupture_model' not in self.oqparam.inputs:
            return self.gmfa
        with self.monitor('computing gmfs'):
            E = self.oqparam.number_of_ground_motion_fields
            for gsim in self.gsims:
                gmfa = self.computer.compute(gsim, E)  # shape (I, N, E)
                self.gmfa[gsim] = gmfa.transpose(1, 2, 0)  # shape (N, E, I)
        return self.gmfa

    def post_execute(self, dummy):
        if self.gmfa:
            with self.monitor('saving gmfs', autoflush=True):
                base.save_gmf_data(self.datastore, self.sitecol,
                                   numpy.array(list(self.gmfa.values())),
                                   self.oqparam.imtls)
示例#2
0
def calculate_gmfs_filter(source_model, gsimlt, filter1, cmake,
                          gsim_list, recMeshExposure, matrixMagsMin,
                          matrixMagsStep, matrixDistsMin, matrixDistsStep,
                          GMPEmatrix, imts, trunc_level):
    # The source filter will return only sites within the integration distance
    gmfs_median = []
    # properties = []
    # Source-Site Filtering
    for source in source_model:
        trt = source.trt
        gmpe_lt_index = gsimlt.all_trts.index(trt)

        for src, s_sites in filter1(source[:]):
            hypo_list = []
            Mag = []
            for rup in src.iter_ruptures():
                Mag.append(round(rup.mag, 1))
                hypo_rup = rup.hypocenter
                hypo_list.append(hypo_rup)

            rupsHypo = Mesh.from_points_list(hypo_list)
            if gsim_list[gmpe_lt_index].REQUIRES_DISTANCES == {'rjb'}:
                distRupExposure = np.around(
                        RectangularMesh.get_joyner_boore_distance(
                                recMeshExposure, rupsHypo))
            elif gsim_list[gmpe_lt_index].REQUIRES_DISTANCES == {'rrup'}:
                distRupExposure = np.around(RectangularMesh.get_min_distance(
                        recMeshExposure, rupsHypo))

            filteringIndex = []
            for i in range(len(Mag)):
                indexMag = int((Mag[i] - matrixMagsMin) / matrixMagsStep)
                indexDist = int((distRupExposure[i] - matrixDistsMin)
                                / matrixDistsStep)
                filteringIndex.append(GMPEmatrix[indexDist, indexMag])

            src_iter = src.iter_ruptures()
            filteredRup = list(compress(src_iter, filteringIndex))

            for rup in filteredRup:
                gmf_computer = GmfComputer(rup, s_sites, imts, cmake,
                                           truncation_level=trunc_level)
                gmf_median = {}
                # if we have more than one gsim per trt, we need to do this
                # for gsim in gsim_list:
                #     gmf_median[gsim] = gmf_computer.compute(gsim,
                # num_events=1)
                gmf_median[gsim_list[gmpe_lt_index]] = gmf_computer.compute(
                                    gsim_list[gmpe_lt_index], num_events=1)
                gmf_median['rate'] = rup.occurrence_rate
                gmf_median['sites'] = s_sites
                gmfs_median.append(gmf_median)
                # FiltMag = str(rup.mag)
                # FiltHypo = str(rup.hypocenter)
                # FiltRate = str(rup.occurrence_rate)
                # properties.append([FiltMag,FiltHypo,FiltRate])
    return gmfs_median
示例#3
0
class ScenarioCalculator(base.HazardCalculator):
    """
    Scenario hazard calculator
    """

    is_stochastic = True

    def pre_execute(self):
        """
        Read the site collection and initialize GmfComputer and seeds
        """
        super(ScenarioCalculator, self).pre_execute()
        oq = self.oqparam
        trunc_level = oq.truncation_level
        correl_model = oq.get_correl_model()
        self.datastore["rupture"] = rupture = readinput.get_rupture(oq)
        self.gsims = readinput.get_gsims(oq)
        maxdist = oq.maximum_distance["default"]
        with self.monitor("filtering sites", autoflush=True):
            self.sitecol = filters.filter_sites_by_distance_to_rupture(rupture, maxdist, self.sitecol)
        if self.sitecol is None:
            raise RuntimeError("All sites were filtered out! maximum_distance=%s km" % maxdist)
        self.computer = GmfComputer(rupture, self.sitecol, oq.imtls, self.gsims, trunc_level, correl_model)
        gsim_lt = readinput.get_gsim_lt(oq)
        cinfo = source.CompositionInfo.fake(gsim_lt)
        self.datastore["csm_info"] = cinfo
        self.rlzs_assoc = cinfo.get_rlzs_assoc()

    def init(self):
        pass

    def execute(self):
        """
        Compute the GMFs and return a dictionary rlzi -> array gmv_dt
        """
        res = collections.defaultdict(list)
        sids = self.sitecol.sids
        with self.monitor("computing gmfs", autoflush=True):
            n = self.oqparam.number_of_ground_motion_fields
            for i, gsim in enumerate(self.gsims):
                gmfa = self.computer.compute(gsim, n, self.oqparam.random_seed)
                for (imti, sid, eid), gmv in numpy.ndenumerate(gmfa):
                    res[i].append((sids[sid], eid, imti, gmv))
            return {rlzi: numpy.array(res[rlzi], gmv_dt) for rlzi in res}

    def post_execute(self, gmfa_by_rlzi):
        """
        :param gmfa: a dictionary rlzi -> gmfa
        """
        with self.monitor("saving gmfs", autoflush=True):
            for rlzi, gsim in enumerate(self.gsims):
                rlzstr = "gmf_data/%04d" % rlzi
                self.datastore[rlzstr] = gmfa_by_rlzi[rlzi]
                self.datastore.set_attrs(rlzstr, gsim=str(gsim))
            self.datastore.set_nbytes("gmf_data")
示例#4
0
class ScenarioCalculator(base.HazardCalculator):
    """
    Scenario hazard calculator
    """
    is_stochastic = True

    def pre_execute(self):
        """
        Read the site collection and initialize GmfComputer and seeds
        """
        super(ScenarioCalculator, self).pre_execute()
        oq = self.oqparam
        gsim_lt = readinput.get_gsim_lt(oq)
        cinfo = source.CompositionInfo.fake(gsim_lt)
        self.datastore['csm_info'] = cinfo
        self.datastore['oqparam'] = oq
        self.rlzs_assoc = cinfo.get_rlzs_assoc()
        if 'rupture_model' not in oq.inputs:
            logging.warn('There is no rupture_model, the calculator will just '
                         'import data without performing any calculation')
            return
        ebr, self.sitecol = readinput.get_rupture_sitecol(oq, self.sitecol)
        self.gsims = readinput.get_gsims(oq)
        self.datastore['events'] = ebr.events
        rupser = calc.RuptureSerializer(self.datastore)
        rupser.save([ebr])
        rupser.close()
        trunc_level = oq.truncation_level
        correl_model = oq.get_correl_model()
        self.computer = GmfComputer(ebr, self.sitecol, oq.imtls,
                                    ContextMaker(self.gsims), trunc_level,
                                    correl_model)

    def init(self):
        pass

    def execute(self):
        """
        Compute the GMFs and return a dictionary gsim -> array(N, E, I)
        """
        self.gmfa = collections.OrderedDict()
        if 'rupture_model' not in self.oqparam.inputs:
            return self.gmfa
        with self.monitor('computing gmfs', autoflush=True):
            n = self.oqparam.number_of_ground_motion_fields
            for gsim in self.gsims:
                gmfa = self.computer.compute(gsim, n)  # shape (I, N, E)
                self.gmfa[gsim] = gmfa.transpose(1, 2, 0)  # shape (N, E, I)
        return self.gmfa

    def post_execute(self, dummy):
        if self.gmfa:
            with self.monitor('saving gmfs', autoflush=True):
                base.save_gmf_data(self.datastore, self.sitecol,
                                   numpy.array(list(self.gmfa.values())))
示例#5
0
 def get_displacement_field(self,
                            rupture,
                            sitecol,
                            cmaker,
                            num_events=1,
                            truncation_level=None,
                            correlation_model=None):
     """
     Returns the field of displacements (m) and ground motion values
     """
     gmf_loc = self._get_gmv_field_location()
     # Gets the ground motion fields
     gmf_computer = GmfComputer(rupture, sitecol,
                                [str(imt) for imt in self.imts], cmaker,
                                truncation_level, correlation_model)
     gmfs = gmf_computer.compute(self, num_events, seed=None)
     # Get the PGA field
     gmv = gmfs[gmf_loc[0]]
     # Return the critical acceleration and proportion of mapped area
     properties = self._setup_properties(gmf_computer.sctx,
                                         gmf_computer.rupture)
     # Get probability of failure. If PGA < a_c, no failure is possible,
     # whilst for PGA > a_c the probability of any individual location
     # observing displacement is equal to the proportion of mapped area
     p_failure = np.zeros_like(gmv)
     for i in range(num_events):
         p_failure[:,
                   i] = properties["pma"] * (gmv[:, i] >= properties["a_c"])
     # Sample the occurence of landsliding.
     mask = np.random.uniform(0., 1., p_failure.shape) <= p_failure
     displacement = np.zeros([1, len(gmf_computer.sctx.sids), num_events],
                             dtype=np.float32)
     if not np.any(mask):
         # No displacement at any site - return zeros and the ground motion
         # fields
         return displacement, gmfs, p_failure
     # If any displacement is registered then need to turn a_c into
     # array of shape [nsites, num_events]
     properties["a_c"] = np.tile(
         # Re-shape to 1-D vector then repeat for num. events
         np.reshape(properties["a_c"], [properties["n"], 1]),
         num_events)
     # Calculate number of cycles factor
     n_cycles = self._get_number_cycles(gmf_computer.rupture.mag)
     # Calculate ratio of acceleration to critical acceleration
     a_c_ais = properties["a_c"][mask] / gmv[mask]
     # Expected displacement factor returns lower and upper bounds, assumed
     # to be uniformly distributed
     e_d_ub, e_d_lb = self._get_expected_displacement_factor(a_c_ais)
     # Final displacement sampling uniformly between the upper and
     # lower bound displacement factors - as described by Equation 4-25
     displacement[0][mask] = M_PER_INCH * n_cycles * gmv[mask] *\
         np.random.uniform(e_d_lb, e_d_ub, a_c_ais.shape)
     return displacement, gmfs, p_failure
示例#6
0
 def calculate_from_rupture(self, rupture, rup_sitecol=None):
     """Method to generate scenario ground motion
     for a specific rupture
     """
     if rup_sitecol is None:
         rup_sitecol = self.sitecol
     computer = GmfComputer(rupture,
                            rup_sitecol,
                            self.imts, [self.gsim],
                            truncation_level=0)
     gmf = computer.compute(self.gsim, 1)
     gmf = gmf.flatten()
     print 'gmf', gmf
     self.rupture_scenario = rupture
     self.rupture_gmf = gmf
     self.rupture_gmf_mmi = rsa2mmi8(gmf, period=1.0)
示例#7
0
def gmfs(job_id, ses_ruptures, sitecol, gmf_id):
    """
    :param int job_id: the current job ID
    :param ses_ruptures: a set of `SESRupture` instances
    :param sitecol: a `SiteCollection` instance
    :param int gmf_id: the ID of a `Gmf` instance
    """
    job = models.OqJob.objects.get(pk=job_id)
    hc = job.hazard_calculation
    # distinct is here to make sure that IMTs such as
    # SA(0.8) and SA(0.80) are considered the same
    imts = distinct(from_string(x) for x in sorted(hc.intensity_measure_types))
    gsim = AVAILABLE_GSIMS[hc.gsim]()  # instantiate the GSIM class
    correlation_model = models.get_correl_model(job)

    cache = collections.defaultdict(list)  # {site_id, imt -> gmvs}
    inserter = writer.CacheInserter(models.GmfData, 1000)
    # insert GmfData in blocks of 1000 sites

    # NB: ses_ruptures a non-empty list produced by the block_splitter
    rupture = ses_ruptures[0].rupture  # ProbabilisticRupture instance
    with EnginePerformanceMonitor('computing gmfs', job_id, gmfs):
        gmf = GmfComputer(rupture, sitecol, imts, [gsim], hc.truncation_level,
                          correlation_model)
        gname = gsim.__class__.__name__
        for ses_rup in ses_ruptures:
            for (gname, imt), gmvs in gmf.compute(ses_rup.seed):
                for site_id, gmv in zip(sitecol.sids, gmvs):
                    # float may be needed below to convert 1x1 matrices
                    cache[site_id, imt].append((gmv, ses_rup.id))

    with EnginePerformanceMonitor('saving gmfs', job_id, gmfs):
        for (site_id, imt_str), data in cache.iteritems():
            imt = from_string(imt_str)
            gmvs, rup_ids = zip(*data)
            inserter.add(
                models.GmfData(
                    gmf_id=gmf_id,
                    task_no=0,
                    imt=imt[0],
                    sa_period=imt[1],
                    sa_damping=imt[2],
                    site_id=site_id,
                    rupture_ids=rup_ids,
                    gmvs=gmvs))
        inserter.flush()
示例#8
0
def calc_gmfs_no_IM_filter(source_model, imts, gsim_list, trunc_level,
                           gsimlt, filter1, cmake):
    gmfs_median = []
    for source in source_model:
        trt = source.trt
        gmpe_lt_index = gsimlt.all_trts.index(trt)
        for src, s_sites in filter1(source[:]):
            src_iter = src.iter_ruptures()
            for rup in src_iter:
                gmf_computer = GmfComputer(rup, s_sites, imts, cmake,
                                           truncation_level=trunc_level)
                gmf_median = {}
                gmf_median[gsim_list[gmpe_lt_index]] = gmf_computer.compute(
                                    gsim_list[gmpe_lt_index], num_events=1)
                gmf_median['rate'] = rup.occurrence_rate
                gmf_median['sites'] = s_sites

                gmfs_median.append(gmf_median)
    return gmfs_median
示例#9
0
 def calculate_from_pts(self):
     """Generates ruptures for each pt source and calculates ground motion
     field.
     :returns gmfs:
         Set of ruptures and associated parameters for ground motion
         calculations  
     """
     for pt in self.sources:
         #        rupture_mags = []
         #        rupture_hypocenter = []
         ruptures = pt.iter_ruptures()
         for rupture in ruptures:
             computer = GmfComputer(rupture,
                                    self.sitecol,
                                    self.imts, [self.gsim],
                                    truncation_level=0)
             gmf = computer.compute(self.gsim, 1)
             gmf = gmf.flatten()
             self.rupture_list.append(rupture)
             self.gmf_list.append(gmf)
示例#10
0
 def build_data(self, rupture, sitecol, rupid_seed_pairs,
                truncation_level=None, correl_model=None):
     """
     :param rupture:
         a ProbabilisticRupture instance
     :param sitecol:
         the collections of sites where to compute the GMFs
     :param rupid_seed_pairs:
         [(r.id, r.seed), ...] for each SESRupture associated the rupture
     :param truncation_level:
         the truncation level (or None)
     :param correl_model:
         the correlation model (or None)
     """
     c = GmfComputer(rupture, sitecol, self.sorted_imts, self.gsims,
                     truncation_level, correl_model)
     for rupid, seed in rupid_seed_pairs:
         self.rupture_ids.append(rupid)
         self.seeds.append(seed)
         for (gsim_name, imt), gmvs in c.compute(seed):
             for site_id, gmv in zip(sitecol.sids, gmvs):
                 self.gmv_dict[imt][site_id][rupid] = gmv
示例#11
0
class ScenarioCalculator(base.HazardCalculator):
    """
    Scenario hazard calculator
    """
    is_stochastic = True

    def pre_execute(self):
        """
        Read the site collection and initialize GmfComputer and seeds
        """
        oq = self.oqparam
        cinfo = source.CompositionInfo.fake(readinput.get_gsim_lt(oq))
        self.datastore['csm_info'] = cinfo
        if 'rupture_model' not in oq.inputs:
            logging.warning(
                'There is no rupture_model, the calculator will just '
                'import data without performing any calculation')
            super().pre_execute()
            return
        self.rup = readinput.get_rupture(oq)
        self.gsims = readinput.get_gsims(oq)
        R = len(self.gsims)
        self.cmaker = ContextMaker('*', self.gsims, oq.maximum_distance,
                                   {'filter_distance': oq.filter_distance})
        super().pre_execute()
        self.datastore['oqparam'] = oq
        self.rlzs_assoc = cinfo.get_rlzs_assoc()
        self.store_rlz_info()
        rlzs_by_gsim = self.rlzs_assoc.get_rlzs_by_gsim(0)
        E = oq.number_of_ground_motion_fields
        n_occ = numpy.array([E])
        ebr = EBRupture(self.rup, 0, 0, n_occ)
        events = numpy.zeros(E * R, events_dt)
        for rlz, eids in ebr.get_eids_by_rlz(rlzs_by_gsim).items():
            events[rlz * E: rlz * E + E]['eid'] = eids
            events[rlz * E: rlz * E + E]['rlz'] = rlz
        self.datastore['events'] = self.events = events
        rupser = calc.RuptureSerializer(self.datastore)
        rup_array = get_rup_array([ebr], self.src_filter)
        if len(rup_array) == 0:
            maxdist = oq.maximum_distance(
                self.rup.tectonic_region_type, self.rup.mag)
            raise RuntimeError('There are no sites within the maximum_distance'
                               ' of %s km from the rupture' % maxdist)
        rupser.save(rup_array)
        rupser.close()
        self.computer = GmfComputer(
            ebr, self.sitecol, oq.imtls, self.cmaker, oq.truncation_level,
            oq.correl_model)

    def init(self):
        pass

    def execute(self):
        """
        Compute the GMFs and return a dictionary gsim -> array(N, E, I)
        """
        arrays = []
        if 'rupture_model' not in self.oqparam.inputs:
            return ()
        n = self.oqparam.number_of_ground_motion_fields
        with self.monitor('computing gmfs'):
            for gsim in self.gsims:
                gmfa = self.computer.compute(gsim, n)  # shape (I, N, n)
                arrays.append(gmfa.transpose(1, 2, 0))  # shape (N, n, I)
        return numpy.concatenate(arrays, axis=1)  # shape (N, E, I)

    def post_execute(self, gmfa):
        if len(gmfa) == 0:  # no rupture_model
            return
        with self.monitor('saving gmfs', autoflush=True):
            base.save_gmf_data(
                self.datastore, self.sitecol, gmfa,
                self.oqparam.imtls, self.events)
示例#12
0
class ScenarioCalculator(base.HazardCalculator):
    """
    Scenario hazard calculator
    """
    is_stochastic = True

    def pre_execute(self):
        """
        Read the site collection and initialize GmfComputer and seeds
        """
        super(ScenarioCalculator, self).pre_execute()
        oq = self.oqparam
        trunc_level = oq.truncation_level
        correl_model = oq.get_correl_model()
        rup = readinput.get_rupture(oq)
        rup.seed = self.oqparam.random_seed
        self.gsims = readinput.get_gsims(oq)
        maxdist = oq.maximum_distance['default']
        with self.monitor('filtering sites', autoflush=True):
            self.sitecol = filters.filter_sites_by_distance_to_rupture(
                rup, maxdist, self.sitecol)
        if self.sitecol is None:
            raise RuntimeError(
                'All sites were filtered out! maximum_distance=%s km' %
                maxdist)
        # eid, ses, occ, sample
        events = numpy.zeros(oq.number_of_ground_motion_fields,
                             calc.stored_event_dt)
        events['eid'] = numpy.arange(oq.number_of_ground_motion_fields)
        rupture = calc.EBRupture(rup, self.sitecol.sids, events, 0, 0)
        rupture.sidx = 0
        rupture.eidx1 = 0
        rupture.eidx2 = len(events)
        self.datastore['sids'] = self.sitecol.sids
        self.datastore['events/grp-00'] = events
        array, nbytes = calc.RuptureSerializer.get_array_nbytes([rupture])
        self.datastore.extend('ruptures/grp-00', array, nbytes=nbytes)
        self.computer = GmfComputer(rupture, self.sitecol, oq.imtls,
                                    self.gsims, trunc_level, correl_model)
        gsim_lt = readinput.get_gsim_lt(oq)
        cinfo = source.CompositionInfo.fake(gsim_lt)
        self.datastore['csm_info'] = cinfo
        self.rlzs_assoc = cinfo.get_rlzs_assoc()

    def init(self):
        pass

    def execute(self):
        """
        Compute the GMFs and return a dictionary gsim -> array(N, E, I)
        """
        self.gmfa = collections.OrderedDict()
        with self.monitor('computing gmfs', autoflush=True):
            n = self.oqparam.number_of_ground_motion_fields
            for gsim in self.gsims:
                gmfa = self.computer.compute(gsim, n)  # shape (I, N, E)
                self.gmfa[gsim] = gmfa.transpose(1, 2, 0)  # shape (N, E, I)
        return self.gmfa

    def post_execute(self, dummy):
        with self.monitor('saving gmfs', autoflush=True):
            self.datastore['gmf_data/grp-00'] = calc.get_gmv_data(
                self.sitecol.sids, list(self.gmfa.values()))
            self.datastore.set_nbytes('gmf_data')
示例#13
0
class ScenarioCalculator(base.HazardCalculator):
    """
    Scenario hazard calculator
    """
    is_stochastic = True

    def pre_execute(self):
        """
        Read the site collection and initialize GmfComputer and seeds
        """
        super(ScenarioCalculator, self).pre_execute()
        oq = self.oqparam
        trunc_level = oq.truncation_level
        correl_model = oq.get_correl_model()
        rup = readinput.get_rupture(oq)
        rup.seed = self.oqparam.random_seed
        self.gsims = readinput.get_gsims(oq)
        maxdist = oq.maximum_distance['default']
        with self.monitor('filtering sites', autoflush=True):
            self.sitecol = filters.filter_sites_by_distance_to_rupture(
                rup, maxdist, self.sitecol)
        if self.sitecol is None:
            raise RuntimeError(
                'All sites were filtered out! maximum_distance=%s km' %
                maxdist)
        # eid, ses, occ, sample
        events = numpy.array(
            [(eid, 1, 1, 0)
             for eid in range(oq.number_of_ground_motion_fields)],
            calc.event_dt)
        rupture = calc.EBRupture(
            rup, self.sitecol.sids, events, 'single_rupture', 0, 0)
        self.datastore['ruptures/grp-00/0'] = rupture
        self.computer = GmfComputer(
            rupture, self.sitecol, oq.imtls, self.gsims,
            trunc_level, correl_model)
        gsim_lt = readinput.get_gsim_lt(oq)
        cinfo = source.CompositionInfo.fake(gsim_lt)
        self.datastore['csm_info'] = cinfo
        self.rlzs_assoc = cinfo.get_rlzs_assoc()

    def init(self):
        pass

    def execute(self):
        """
        Compute the GMFs and return a dictionary rlzi -> array gmv_dt
        """
        res = collections.defaultdict(list)
        sids = self.sitecol.sids
        self.gmfa = {}
        with self.monitor('computing gmfs', autoflush=True):
            n = self.oqparam.number_of_ground_motion_fields
            for i, gsim in enumerate(self.gsims):
                gmfa = self.computer.compute(gsim, n)
                self.gmfa[gsim] = gmfa
                for (imti, sid, eid), gmv in numpy.ndenumerate(gmfa):
                    res[i].append((sids[sid], eid, imti, gmv))
            return {rlzi: numpy.array(res[rlzi], gmv_dt) for rlzi in res}

    def post_execute(self, gmfa_by_rlzi):
        """
        :param gmfa: a dictionary rlzi -> gmfa
        """
        with self.monitor('saving gmfs', autoflush=True):
            for rlzi, gsim in enumerate(self.gsims):
                rlzstr = 'gmf_data/sm-0000/%04d' % rlzi
                self.datastore[rlzstr] = gmfa_by_rlzi[rlzi]
                self.datastore.set_attrs(rlzstr, gsim=str(gsim))
            self.datastore.set_nbytes('gmf_data')
示例#14
0
    def get_displacement_field(self,
                               rupture,
                               sitecol,
                               cmaker,
                               num_events=1,
                               truncation_level=None,
                               correlation_model=None):
        """
        Returns the field of displacements
        """
        # Calculate the ground motion fields
        gmf_loc = self._get_gmv_field_location()
        gmf_computer = GmfComputer(rupture, sitecol,
                                   [str(imt) for imt in self.imts], cmaker,
                                   truncation_level, correlation_model)
        gmfs = gmf_computer.compute(self, num_events, seed=None)
        # Get the PGA field - should have the dimension [nsites, num_events]
        gmvs = gmfs[gmf_loc[0]]
        # Get site and rupture related properties
        properties = self._setup_properties(gmf_computer.sctx,
                                            gmf_computer.rupture)

        # Determine the probability of failure
        # Both the pga threshold and the settlement need to take the same shape
        # as the ground motion values - in this case a 2-D form is needed
        for key in ["pga_threshold", "settlement"]:
            # Tile
            properties[key] = np.tile(
                # Re-shape to 1-D vector then repeat for num. events
                np.reshape(properties[key], [properties["n"], 1]),
                num_events)

        p_failure = np.zeros_like(gmvs)
        for j in range(p_failure.shape[1]):
            p_failure[:, j] = self.get_failure_model(gmf_computer.sctx,
                                                     gmvs[:, j], properties)
        # Setup displacement field
        displacement = np.zeros([2, len(gmf_computer.sctx.sids), num_events],
                                dtype=np.float32)
        # Sample the field
        mask = np.random.uniform(0., 1., p_failure.shape) <= p_failure
        if not np.any(mask):
            # No liquefaction is observed - return a field of zeros!
            # Note that there are two intensity measures, so to speak, which
            # represent lateral spread and settlement respectively
            # Return the zero displacement fields and the GMFs
            return displacement, gmfs, p_failure

        # Some sites observe liquefaction - now to calculate lateral
        # spread and settlement
        # Calculate PGA to threshold PGA ratio
        pga_pgat = gmvs[mask] / properties["pga_threshold"][mask]

        # Calculate lateral spread
        lateral_spread = np.zeros_like(pga_pgat)
        # Get the displacement correction factor - which is a function of mag
        for (low, high), (m, c) in D_LATERAL_SPREAD:
            dls_idx = np.logical_and(pga_pgat >= low, pga_pgat < high)
            if np.any(dls_idx):
                lateral_spread[dls_idx] = M_PER_INCH * (
                    properties["kdelta"] * (m * pga_pgat[dls_idx] + c))
        displacement[0][mask] = np.copy(lateral_spread)
        # Settlement is a simple scalar function
        displacement[1][mask] = properties["settlement"][mask]
        return displacement, gmfs, p_failure
示例#15
0
class ScenarioCalculator(base.HazardCalculator):
    """
    Scenario hazard calculator
    """
    is_stochastic = True

    def pre_execute(self):
        """
        Read the site collection and initialize GmfComputer and seeds
        """
        super(ScenarioCalculator, self).pre_execute()
        oq = self.oqparam
        trunc_level = oq.truncation_level
        correl_model = oq.get_correl_model()
        rup = readinput.get_rupture(oq)
        rup.seed = self.oqparam.random_seed
        self.gsims = readinput.get_gsims(oq)
        maxdist = oq.maximum_distance['default']
        with self.monitor('filtering sites', autoflush=True):
            self.sitecol = filters.filter_sites_by_distance_to_rupture(
                rup, maxdist, self.sitecol)
        if self.sitecol is None:
            raise RuntimeError(
                'All sites were filtered out! maximum_distance=%s km' %
                maxdist)
        # eid, ses, occ, sample
        events = numpy.zeros(oq.number_of_ground_motion_fields,
                             calc.stored_event_dt)
        events['eid'] = numpy.arange(oq.number_of_ground_motion_fields)
        rupture = calc.EBRupture(rup, self.sitecol.sids, events, 0, 0)
        rupture.sidx = 0
        rupture.eidx1 = 0
        rupture.eidx2 = len(events)
        self.datastore['sids'] = self.sitecol.sids
        self.datastore['events/grp-00'] = events
        array, nbytes = calc.RuptureSerializer.get_array_nbytes([rupture])
        self.datastore.extend('ruptures/grp-00', array, nbytes=nbytes)
        self.computer = GmfComputer(rupture, self.sitecol, oq.imtls,
                                    self.gsims, trunc_level, correl_model)
        gsim_lt = readinput.get_gsim_lt(oq)
        cinfo = source.CompositionInfo.fake(gsim_lt)
        self.datastore['csm_info'] = cinfo
        self.rlzs_assoc = cinfo.get_rlzs_assoc()

    def init(self):
        pass

    def execute(self):
        """
        Compute the GMFs and return a dictionary gsim -> array gmf_data_dt
        """
        res = collections.defaultdict(list)
        sids = self.sitecol.sids
        self.gmfa = {}
        with self.monitor('computing gmfs', autoflush=True):
            n = self.oqparam.number_of_ground_motion_fields
            for gsim in self.gsims:
                gmfa = self.computer.compute(gsim, n)  # shape (I, N, E)
                self.gmfa[gsim] = gmfa.transpose(1, 0, 2)  # shape (N, I, E)
                for (imti, sid, eid), gmv in numpy.ndenumerate(gmfa):
                    res[gsim].append((0, sids[sid], eid, imti, gmv))
            return {gsim: numpy.array(res[gsim], gmf_data_dt) for gsim in res}

    def post_execute(self, gmfa_by_gsim):
        """
        :param gmfa: a dictionary gsim -> gmfa
        """
        with self.monitor('saving gmfs', autoflush=True):
            for gsim in self.gsims:
                rlzstr = 'gmf_data/grp-00/%s' % gsim
                self.datastore[rlzstr] = gmfa_by_gsim[gsim]
                self.datastore.set_attrs(rlzstr, gsim=str(gsim))
            self.datastore.set_nbytes('gmf_data')
示例#16
0
class ScenarioCalculator(base.HazardCalculator):
    """
    Scenario hazard calculator
    """
    is_stochastic = True

    def pre_execute(self):
        """
        Read the site collection and initialize GmfComputer and seeds
        """
        oq = self.oqparam
        cinfo = logictree.FullLogicTree.fake(readinput.get_gsim_lt(oq))
        self.realizations = cinfo.get_realizations()
        self.datastore['full_lt'] = cinfo
        if 'rupture_model' not in oq.inputs:
            logging.warning(
                'There is no rupture_model, the calculator will just '
                'import data without performing any calculation')
            super().pre_execute()
            return
        self.rup = readinput.get_rupture(oq)
        self.gsims = readinput.get_gsims(oq)
        R = len(self.gsims)
        self.cmaker = ContextMaker(
            '*', self.gsims, {
                'maximum_distance': oq.maximum_distance,
                'filter_distance': oq.filter_distance
            })
        super().pre_execute()
        self.datastore['oqparam'] = oq
        self.store_rlz_info({})
        rlzs_by_gsim = cinfo.get_rlzs_by_gsim(0)
        E = oq.number_of_ground_motion_fields
        n_occ = numpy.array([E])
        ebr = EBRupture(self.rup, 0, 0, n_occ)
        ebr.e0 = 0
        events = numpy.zeros(E * R, events_dt)
        for rlz, eids in ebr.get_eids_by_rlz(rlzs_by_gsim).items():
            events[rlz * E:rlz * E + E]['id'] = eids
            events[rlz * E:rlz * E + E]['rlz_id'] = rlz
        self.datastore['events'] = self.events = events
        rupser = calc.RuptureSerializer(self.datastore)
        rup_array = get_rup_array([ebr], self.src_filter())
        if len(rup_array) == 0:
            maxdist = oq.maximum_distance(self.rup.tectonic_region_type,
                                          self.rup.mag)
            raise RuntimeError('There are no sites within the maximum_distance'
                               ' of %s km from the rupture' % maxdist)
        rupser.save(rup_array)
        rupser.close()
        self.computer = GmfComputer(ebr, self.sitecol, oq.imtls, self.cmaker,
                                    oq.truncation_level, oq.correl_model,
                                    self.amplifier)
        M32 = (numpy.float32, len(self.oqparam.imtls))
        self.sig_eps_dt = [('eid', numpy.uint64), ('sig', M32), ('eps', M32)]

    def init(self):
        pass

    def execute(self):
        """
        Compute the GMFs and return a dictionary gsim -> array(N, E, I)
        """
        arrays = []
        if 'rupture_model' not in self.oqparam.inputs:
            return ()
        n = self.oqparam.number_of_ground_motion_fields
        with self.monitor('computing gmfs'):
            ei = 0
            for gsim in self.gsims:
                gmfa, sig, eps = self.computer.compute(gsim, n)
                lst = []
                for s, e in zip(sig.T, eps.T):  # shape (M, E) -> (E, M)
                    lst.append((ei, s, e))
                    ei += 1
                arrays.append(gmfa.transpose(1, 2, 0))  # shape (N, n, I)
        self.datastore['gmf_data/sigma_epsilon'] = numpy.array(
            lst, self.sig_eps_dt)
        return numpy.concatenate(arrays, axis=1)  # shape (N, E, I)

    def post_execute(self, gmfa):
        if len(gmfa) == 0:  # no rupture_model
            return
        with self.monitor('saving gmfs'):
            base.save_gmf_data(self.datastore, self.sitecol, gmfa,
                               self.oqparam.imtls, self.events)
示例#17
0
class ScenarioCalculator(base.HazardCalculator):
    """
    Scenario hazard calculator
    """
    is_stochastic = True

    def pre_execute(self):
        """
        Read the site collection and initialize GmfComputer, etags and seeds
        """
        super(ScenarioCalculator, self).pre_execute()
        oq = self.oqparam
        trunc_level = oq.truncation_level
        correl_model = readinput.get_correl_model(oq)
        n_gmfs = oq.number_of_ground_motion_fields
        rupture = readinput.get_rupture(oq)
        self.gsims = readinput.get_gsims(oq)
        maxdist = oq.maximum_distance['default']
        with self.monitor('filtering sites', autoflush=True):
            self.sitecol = filters.filter_sites_by_distance_to_rupture(
                rupture, maxdist, self.sitecol)
        if self.sitecol is None:
            raise RuntimeError(
                'All sites were filtered out! maximum_distance=%s km' %
                maxdist)
        self.etags = numpy.array(
            sorted(['scenario-%010d~ses=1' % i for i in range(n_gmfs)]),
            (bytes, 100))
        self.computer = GmfComputer(rupture, self.sitecol, oq.imtls,
                                    self.gsims, trunc_level, correl_model)
        gsim_lt = readinput.get_gsim_lt(oq)
        cinfo = source.CompositionInfo.fake(gsim_lt)
        self.datastore['csm_info'] = cinfo
        self.rlzs_assoc = cinfo.get_rlzs_assoc()

    def init(self):
        pass

    def execute(self):
        """
        Compute the GMFs and return a dictionary rlzi -> array gmv_dt
        """
        res = collections.defaultdict(list)
        sids = self.sitecol.sids
        with self.monitor('computing gmfs', autoflush=True):
            n = self.oqparam.number_of_ground_motion_fields
            for i, gsim in enumerate(self.gsims):
                gmfa = self.computer.compute(self.oqparam.random_seed, gsim, n)
                for (imti, sid, eid), gmv in numpy.ndenumerate(gmfa):
                    res[i].append((sids[sid], eid, imti, gmv))
            return {rlzi: numpy.array(res[rlzi], gmv_dt) for rlzi in res}

    def post_execute(self, gmfa_by_rlzi):
        """
        :param gmfa: a dictionary rlzi -> gmfa
        """
        with self.monitor('saving gmfs', autoflush=True):
            for rlzi, gsim in enumerate(self.gsims):
                rlzstr = 'gmf_data/%04d' % rlzi
                self.datastore[rlzstr] = gmfa_by_rlzi[rlzi]
                self.datastore.set_attrs(rlzstr, gsim=str(gsim))
            self.datastore.set_nbytes('gmf_data')