def setUpClass(cls): cls.parser = nrml.SourceModelParser(s.SourceConverter( investigation_time=50., rupture_mesh_spacing=1, # km complex_fault_mesh_spacing=1, # km width_of_mfd_bin=1., # for Truncated GR MFDs area_source_discretization=1.)) cls.source_collector = { sc.trt: sc for sc in cls.parser.parse_src_groups(MIXED_SRC_MODEL)} cls.sitecol = site.SiteCollection(cls.SITES)
def test_duplicate_id(self): parser = nrml.SourceModelParser(s.SourceConverter( investigation_time=50., rupture_mesh_spacing=1, complex_fault_mesh_spacing=1, width_of_mfd_bin=0.1, area_source_discretization=10, )) with self.assertRaises(nrml.DuplicatedID): parser.parse_groups(DUPLICATE_ID_SRC_MODEL)
def test(self): fname = os.path.join(os.path.dirname(__file__), 'faults_backg_source_model.xml') converter = sourceconverter.SourceConverter() # check that the input file requires the fix indeed with self.assertRaises(InvalidFile): nrml.SourceModelParser(converter).parse_groups(fname) fd, tmpname = tempfile.mkstemp(suffix='.xml') os.close(fd) fix(fname, tmpname) # invoke the fix print('meld %s %s' % (fname, tmpname))
def setUpClass(cls): cls.parser = nrml.SourceModelParser(s.SourceConverter( investigation_time=50., rupture_mesh_spacing=1, # km complex_fault_mesh_spacing=1, # km width_of_mfd_bin=1., # for Truncated GR MFDs area_source_discretization=1., # km )) groups = cls.parser.parse_groups(MIXED_SRC_MODEL) ([cls.point], [cls.cmplx], [cls.area, cls.simple], [cls.char_simple, cls.char_complex, cls.char_multi]) = groups # the parameters here would typically be specified in the job .ini cls.investigation_time = 50. cls.rupture_mesh_spacing = 1 # km cls.complex_fault_mesh_spacing = 1 # km cls.width_of_mfd_bin = 1. # for Truncated GR MFDs cls.area_source_discretization = 1. # km
def test(self): mod = mock.Mock(reference_vs30_value=760, reference_vs30_type='measured', reference_depth_to_1pt0km_per_sec=100., reference_depth_to_2pt5km_per_sec=5.0, reference_backarc=False) sitecol = site.SiteCollection.from_points([102.32], [-2.9107], [0], mod) parser = nrml.SourceModelParser( s.SourceConverter( investigation_time=50., rupture_mesh_spacing=1, # km complex_fault_mesh_spacing=1, # km width_of_mfd_bin=1., # for Truncated GR MFDs area_source_discretization=1., # km )) [[src]] = parser.parse_groups(self.bad_source) with self.assertRaises(AttributeError) as ctx, context(src): max_dist = 250 # NB: with a distance of 200 km the error does not happen src.filter_sites_by_distance_to_source(max_dist, sitecol) self.assertIn('An error occurred with source id=61', str(ctx.exception))
def get_source_models(oqparam, gsim_lt, source_model_lt, in_memory=True): """ Build all the source models generated by the logic tree. :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :param gsim_lt: a :class:`openquake.commonlib.logictree.GsimLogicTree` instance :param source_model_lt: a :class:`openquake.commonlib.logictree.SourceModelLogicTree` instance :param in_memory: if True, keep in memory the sources, else just collect the TRTs :returns: an iterator over :class:`openquake.commonlib.logictree.SourceModel` tuples """ converter = sourceconverter.SourceConverter( oqparam.investigation_time, oqparam.rupture_mesh_spacing, oqparam.complex_fault_mesh_spacing, oqparam.width_of_mfd_bin, oqparam.area_source_discretization) psr = nrml.SourceModelParser(converter) # consider only the effective realizations for sm in source_model_lt.gen_source_models(gsim_lt): src_groups = [] for name in sm.name.split(): fname = possibly_gunzip( os.path.abspath(os.path.join(oqparam.base_path, name))) if in_memory: apply_unc = source_model_lt.make_apply_uncertainties(sm.path) try: logging.info('Parsing %s', fname) src_groups.extend(psr.parse_src_groups(fname, apply_unc)) except ValueError as e: if str(e) in ('Surface does not conform with Aki & ' 'Richards convention', 'Edges points are not in the right order'): raise InvalidFile('''\ %s: %s. Probably you are using an obsolete model. In that case you can fix the file with the command python -m openquake.engine.tools.correct_complex_sources %s ''' % (fname, e, fname)) else: raise else: # just collect the TRT models smodel = nrml.read(fname).sourceModel if smodel[0].tag.endswith('sourceGroup'): # NRML 0.5 format for sg_node in smodel: sg = sourceconverter.SourceGroup( sg_node['tectonicRegion']) sg.sources = sg_node.nodes src_groups.append(sg) else: # NRML 0.4 format: smodel is a list of source nodes src_groups.extend( sourceconverter.SourceGroup.collect(smodel)) num_sources = sum(len(sg.sources) for sg in src_groups) sm.src_groups = src_groups trts = [mod.trt for mod in src_groups] source_model_lt.tectonic_region_types.update(trts) logging.info( 'Processed source model %d with %d potential gsim path(s) and %d ' 'sources', sm.ordinal + 1, sm.num_gsim_paths, num_sources) gsim_file = oqparam.inputs.get('gsim_logic_tree') if gsim_file: # check TRTs for src_group in src_groups: if src_group.trt not in gsim_lt.values: raise ValueError( "Found in %r a tectonic region type %r inconsistent " "with the ones in %r" % (sm, src_group.trt, gsim_file)) yield sm # log if some source file is being used more than once for fname, hits in psr.fname_hits.items(): if hits > 1: logging.info('%s has been considered %d times', fname, hits)
def get_source_models(oqparam, gsim_lt, source_model_lt, in_memory=True): """ Build all the source models generated by the logic tree. :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :param gsim_lt: a :class:`openquake.commonlib.logictree.GsimLogicTree` instance :param source_model_lt: a :class:`openquake.commonlib.logictree.SourceModelLogicTree` instance :param in_memory: if True, keep in memory the sources, else just collect the TRTs :returns: an iterator over :class:`openquake.commonlib.logictree.LtSourceModel` tuples """ converter = sourceconverter.SourceConverter( oqparam.investigation_time, oqparam.rupture_mesh_spacing, oqparam.complex_fault_mesh_spacing, oqparam.width_of_mfd_bin, oqparam.area_source_discretization) psr = nrml.SourceModelParser(converter) if oqparam.calculation_mode.startswith('ucerf'): [grp] = nrml.to_python(oqparam.inputs["source_model"], converter) # consider only the effective realizations smlt_dir = os.path.dirname(source_model_lt.filename) for sm in source_model_lt.gen_source_models(gsim_lt): src_groups = [] for name in sm.names.split(): fname = os.path.abspath(os.path.join(smlt_dir, name)) if oqparam.calculation_mode.startswith('ucerf'): sg = copy.copy(grp) sg.id = sm.ordinal sg.sources = [sg[0].new(sm.ordinal, sm.names)] # one source src_groups.append(sg) elif in_memory: apply_unc = source_model_lt.make_apply_uncertainties(sm.path) logging.info('Reading %s', fname) src_groups.extend(psr.parse_src_groups(fname, apply_unc)) else: # just collect the TRT models src_groups.extend(read_source_groups(fname)) num_sources = sum(len(sg.sources) for sg in src_groups) sm.src_groups = src_groups trts = [mod.trt for mod in src_groups] source_model_lt.tectonic_region_types.update(trts) logging.info( 'Processed source model %d with %d potential gsim path(s) and %d ' 'sources', sm.ordinal + 1, sm.num_gsim_paths, num_sources) gsim_file = oqparam.inputs.get('gsim_logic_tree') if gsim_file: # check TRTs for src_group in src_groups: if src_group.trt not in gsim_lt.values: raise ValueError( "Found in %r a tectonic region type %r inconsistent " "with the ones in %r" % (sm, src_group.trt, gsim_file)) yield sm # check investigation_time psr.check_nonparametric_sources(oqparam.investigation_time) # log if some source file is being used more than once dupl = 0 for fname, hits in psr.fname_hits.items(): if hits > 1: logging.info('%s has been considered %d times', fname, hits) if not psr.changed_sources: dupl += hits if dupl and not oqparam.optimize_same_id_sources: logging.warn('You are doing redundant calculations: please make sure ' 'that different sources have different IDs and set ' 'optimize_same_id_sources=true in your .ini file')