def set_psd(self): """ Set the pointsource_distance """ oq = self.oqparam mags = self.datastore['source_mags'] # by TRT if len(mags) == 0: # everything was discarded raise RuntimeError('All sources were discarded!?') gsims_by_trt = self.full_lt.get_gsims_by_trt() mags_by_trt = {} for trt in mags: mags_by_trt[trt] = mags[trt][()] psd = oq.pointsource_distance if psd is not None: psd.interp(mags_by_trt) for trt, dic in psd.ddic.items(): # the sum is zero for {'default': [(1, 0), (10, 0)]} if sum(dic.values()): it = list(dic.items()) md = '%s->%d ... %s->%d' % (it[0] + it[-1]) logging.info('ps_dist %s: %s', trt, md) imts_with_period = [ imt for imt in oq.imtls if imt == 'PGA' or imt.startswith('SA') ] imts_ok = len(imts_with_period) == len(oq.imtls) if (imts_ok and psd and psd.suggested()) or (imts_ok and oq.minimum_intensity): aw = get_effect(mags_by_trt, self.sitecol.one(), gsims_by_trt, oq) if psd: dic = { trt: [(float(mag), int(dst)) for mag, dst in psd.ddic[trt].items()] for trt in psd.ddic if trt != 'default' } logging.info('pointsource_distance=\n%s', pprint.pformat(dic)) if len(vars(aw)) > 1: # more than _extra self.datastore['effect_by_mag_dst'] = aw hint = 1 if self.N <= oq.max_sites_disagg else numpy.ceil( self.N / oq.max_sites_per_tile) self.params = dict(truncation_level=oq.truncation_level, investigation_time=oq.investigation_time, imtls=oq.imtls, reqv=oq.get_reqv(), pointsource_distance=oq.pointsource_distance, shift_hypo=oq.shift_hypo, min_weight=oq.min_weight, collapse_level=int(oq.collapse_level), hint=hint, max_sites_disagg=oq.max_sites_disagg, split_sources=oq.split_sources, af=self.af) return psd
def execute(self): """ Run in parallel `core_task(sources, sitecol, monitor)`, by parallelizing on the sources according to their weight and tectonic region type. """ oq = self.oqparam if oq.hazard_calculation_id and not oq.compare_with_classical: with util.read(self.oqparam.hazard_calculation_id) as parent: self.full_lt = parent['full_lt'] self.calc_stats() # post-processing return {} mags = self.datastore['source_mags'] # by TRT if len(mags) == 0: # everything was discarded raise RuntimeError('All sources were discarded!?') gsims_by_trt = self.full_lt.get_gsims_by_trt() if oq.pointsource_distance is not None: for trt in gsims_by_trt: oq.pointsource_distance[trt] = getdefault( oq.pointsource_distance, trt) mags_by_trt = {} for trt in mags: mags_by_trt[trt] = mags[trt][()] imts_with_period = [imt for imt in oq.imtls if imt == 'PGA' or imt.startswith('SA')] imts_ok = len(imts_with_period) == len(oq.imtls) if (imts_ok and oq.pointsource_distance and oq.pointsource_distance.suggested()) or ( imts_ok and oq.minimum_intensity): aw, self.psd = get_effect( mags_by_trt, self.sitecol.one(), gsims_by_trt, oq) if len(vars(aw)) > 1: # more than _extra self.datastore['effect_by_mag_dst'] = aw elif oq.pointsource_distance: self.psd = oq.pointsource_distance.interp(mags_by_trt) else: self.psd = {} smap = parallel.Starmap(classical, h5=self.datastore.hdf5, num_cores=oq.num_cores) self.submit_tasks(smap) acc0 = self.acc0() # create the rup/ datasets BEFORE swmr_on() self.datastore.swmr_on() smap.h5 = self.datastore.hdf5 self.calc_times = AccumDict(accum=numpy.zeros(3, F32)) try: acc = smap.reduce(self.agg_dicts, acc0) self.store_rlz_info(acc.eff_ruptures) finally: with self.monitor('store source_info'): self.store_source_info(self.calc_times) if self.by_task: logging.info('Storing by_task information') num_tasks = max(self.by_task) + 1, er = self.datastore.create_dset('by_task/eff_ruptures', U32, num_tasks) es = self.datastore.create_dset('by_task/eff_sites', U32, num_tasks) si = self.datastore.create_dset('by_task/srcids', hdf5.vstr, num_tasks, fillvalue=None) for task_no, rec in self.by_task.items(): effrups, effsites, srcids = rec er[task_no] = effrups es[task_no] = effsites si[task_no] = ' '.join(srcids) self.by_task.clear() self.numrups = sum(arr[0] for arr in self.calc_times.values()) numsites = sum(arr[1] for arr in self.calc_times.values()) logging.info('Effective number of ruptures: {:_d}/{:_d}'.format( int(self.numrups), self.totrups)) logging.info('Effective number of sites per rupture: %d', numsites / self.numrups) if self.psd: psdist = max(max(self.psd[trt].values()) for trt in self.psd) if psdist != -1 and self.maxradius >= psdist / 2: logging.warning('The pointsource_distance of %d km is too ' 'small compared to a maxradius of %d km', psdist, self.maxradius) self.calc_times.clear() # save a bit of memory return acc
def execute(self): """ Run in parallel `core_task(sources, sitecol, monitor)`, by parallelizing on the sources according to their weight and tectonic region type. """ oq = self.oqparam if oq.hazard_calculation_id and not oq.compare_with_classical: with util.read(self.oqparam.hazard_calculation_id) as parent: self.full_lt = parent['full_lt'] self.calc_stats() # post-processing return {} srcfilter = self.src_filter() srcs = self.csm.get_sources() calc_times = parallel.Starmap.apply( preclassical, (srcs, srcfilter), concurrent_tasks=oq.concurrent_tasks or 1, num_cores=oq.num_cores, h5=self.datastore.hdf5).reduce() if oq.calculation_mode == 'preclassical': self.store_source_info(calc_times, nsites=True) self.datastore['full_lt'] = self.csm.full_lt self.datastore.swmr_on() # fixes HDF5 error in build_hazard return self.update_source_info(calc_times, nsites=True) # if OQ_SAMPLE_SOURCES is set extract one source for group ss = os.environ.get('OQ_SAMPLE_SOURCES') if ss: for sg in self.csm.src_groups: if not sg.atomic: srcs = [src for src in sg if src.nsites] sg.sources = [srcs[0]] mags = self.datastore['source_mags'] # by TRT if len(mags) == 0: # everything was discarded raise RuntimeError('All sources were discarded!?') gsims_by_trt = self.full_lt.get_gsims_by_trt() mags_by_trt = {} for trt in mags: mags_by_trt[trt] = mags[trt][()] psd = oq.pointsource_distance if psd is not None: psd.interp(mags_by_trt) for trt, dic in psd.ddic.items(): # the sum is zero for {'default': [(1, 0), (10, 0)]} if sum(dic.values()): it = list(dic.items()) md = '%s->%d ... %s->%d' % (it[0] + it[-1]) logging.info('ps_dist %s: %s', trt, md) imts_with_period = [imt for imt in oq.imtls if imt == 'PGA' or imt.startswith('SA')] imts_ok = len(imts_with_period) == len(oq.imtls) if (imts_ok and psd and psd.suggested()) or ( imts_ok and oq.minimum_intensity): aw = get_effect(mags_by_trt, self.sitecol.one(), gsims_by_trt, oq) if psd: dic = {trt: [(float(mag), int(dst)) for mag, dst in psd.ddic[trt].items()] for trt in psd.ddic if trt != 'default'} logging.info('pointsource_distance=\n%s', pprint.pformat(dic)) if len(vars(aw)) > 1: # more than _extra self.datastore['effect_by_mag_dst'] = aw smap = parallel.Starmap(classical, h5=self.datastore.hdf5, num_cores=oq.num_cores) smap.monitor.save('srcfilter', self.src_filter()) rlzs_by_gsim_list = self.submit_tasks(smap) rlzs_by_g = [] for rlzs_by_gsim in rlzs_by_gsim_list: for rlzs in rlzs_by_gsim.values(): rlzs_by_g.append(rlzs) self.datastore['rlzs_by_g'] = [U32(rlzs) for rlzs in rlzs_by_g] acc0 = self.acc0() # create the rup/ datasets BEFORE swmr_on() poes_shape = (self.N, len(oq.imtls.array), len(rlzs_by_g)) # NLG size = numpy.prod(poes_shape) * 8 logging.info('Requiring %s for ProbabilityMap of shape %s', humansize(size), poes_shape) self.datastore.create_dset('_poes', F64, poes_shape) self.datastore.swmr_on() smap.h5 = self.datastore.hdf5 self.calc_times = AccumDict(accum=numpy.zeros(3, F32)) try: acc = smap.reduce(self.agg_dicts, acc0) self.store_rlz_info(acc.eff_ruptures) finally: with self.monitor('store source_info'): self.store_source_info(self.calc_times) if self.by_task: logging.info('Storing by_task information') num_tasks = max(self.by_task) + 1, er = self.datastore.create_dset('by_task/eff_ruptures', U32, num_tasks) es = self.datastore.create_dset('by_task/eff_sites', U32, num_tasks) si = self.datastore.create_dset('by_task/srcids', hdf5.vstr, num_tasks, fillvalue=None) for task_no, rec in self.by_task.items(): effrups, effsites, srcids = rec er[task_no] = effrups es[task_no] = effsites si[task_no] = ' '.join(srcids) self.by_task.clear() self.numrups = sum(arr[0] for arr in self.calc_times.values()) numsites = sum(arr[1] for arr in self.calc_times.values()) logging.info('Effective number of ruptures: {:_d}/{:_d}'.format( int(self.numrups), self.totrups)) logging.info('Effective number of sites per rupture: %d', numsites / self.numrups) if psd: psdist = max(max(psd.ddic[trt].values()) for trt in psd.ddic) if psdist and self.maxradius >= psdist / 2: logging.warning('The pointsource_distance of %d km is too ' 'small compared to a maxradius of %d km', psdist, self.maxradius) self.calc_times.clear() # save a bit of memory return acc