Пример #1
0
    def test_get_site_model_too_many_site_models(self):
        haz_calc = self._create_haz_calc()

        site_model_inp1 = models.Input(
            owner=haz_calc.owner, digest='fake', path='fake',
            input_type='site_model', size=0
        )
        site_model_inp1.save()
        site_model_inp2 = models.Input(
            owner=haz_calc.owner, digest='fake', path='fake',
            input_type='site_model', size=0
        )
        site_model_inp2.save()

        # link both site models to the calculation:
        models.Input2hcalc(
            input=site_model_inp1, hazard_calculation=haz_calc).save()
        models.Input2hcalc(
            input=site_model_inp2, hazard_calculation=haz_calc).save()

        with self.assertRaises(RuntimeError) as assert_raises:
            general.get_site_model(haz_calc.id)

        self.assertEqual('Only 1 site model per job is allowed, found 2.',
                         assert_raises.exception.message)
Пример #2
0
    def test_get_site_model(self):
        job = engine.prepare_job()
        site_model_inp = models.Input(
            owner=job.owner, digest='fake', path='fake',
            input_type='site_model', size=0
        )
        site_model_inp.save()

        # The link has not yet been made in the input2job table.
        self.assertIsNone(general.get_site_model(job.id))

        # Complete the link:
        models.Input2job(input=site_model_inp, oq_job=job).save()

        actual_site_model = general.get_site_model(job.id)
        self.assertEqual(site_model_inp, actual_site_model)
Пример #3
0
    def test_parameterize_sites_with_site_model(self):
        job_ctxt = helpers.prepare_job_context(
            helpers.demo_file(
                'simple_fault_demo_hazard/config_with_site_model.gem'))

        calc = classical.ClassicalHazardCalculator(job_ctxt)
        calc.initialize()

        # This tests to ensure that the `initialize` implementation for this
        # calculator properly stores the site model in the DB.

        # NOTE: If this test ever breaks, it's probably because the
        # ClassicalHazardCalculator is no longer calling the `initalize` code
        # in its super class (BaseHazardCalculator).
        site_model = hazard_general.get_site_model(job_ctxt.oq_job.id)
        self.assertIsNotNone(site_model)

        set_params_patch = helpers.patch(
            'openquake.calculators.hazard.general.set_java_site_parameters')
        closest_data_patch = helpers.patch(
            'openquake.calculators.hazard.general.get_closest_site_model_data')
        sp_mock = set_params_patch.start()
        cd_mock = closest_data_patch.start()

        try:
            calc.parameterize_sites(job_ctxt.sites_to_compute())

            exp_call_count = len(job_ctxt.sites_to_compute())
            self.assertEqual(exp_call_count, sp_mock.call_count)
            self.assertEqual(exp_call_count, cd_mock.call_count)

        finally:
            # tear down the patches
            set_params_patch.stop()
            closest_data_patch.stop()
Пример #4
0
    def test_get_site_model(self):
        job = engine.prepare_job()
        site_model_inp = models.Input(
            owner=job.owner, digest='fake', path='fake',
            input_type='site_model', size=0
        )
        site_model_inp.save()

        # The link has not yet been made in the input2job table.
        self.assertIsNone(general.get_site_model(job.id))

        # Complete the link:
        models.Input2job(input=site_model_inp, oq_job=job).save()

        actual_site_model = general.get_site_model(job.id)
        self.assertEqual(site_model_inp, actual_site_model)
Пример #5
0
    def test_get_site_model(self):
        haz_calc = self._create_haz_calc()

        site_model_inp = models.Input(
            owner=haz_calc.owner, digest='fake', path='fake',
            input_type='site_model', size=0
        )
        site_model_inp.save()

        # The link has not yet been made in the input2haz_calc table.
        self.assertIsNone(general.get_site_model(haz_calc.id))

        # Complete the link:
        models.Input2hcalc(
            input=site_model_inp, hazard_calculation=haz_calc).save()

        actual_site_model = general.get_site_model(haz_calc.id)
        self.assertEqual(site_model_inp, actual_site_model)
Пример #6
0
    def test_get_site_model_too_many_site_models(self):
        job = engine.prepare_job()
        site_model_inp1 = models.Input(
            owner=job.owner, digest='fake', path='fake',
            input_type='site_model', size=0
        )
        site_model_inp1.save()
        site_model_inp2 = models.Input(
            owner=job.owner, digest='fake', path='fake',
            input_type='site_model', size=0
        )
        site_model_inp2.save()

        # link both site models to the job:
        models.Input2job(input=site_model_inp1, oq_job=job).save()
        models.Input2job(input=site_model_inp2, oq_job=job).save()

        with self.assertRaises(RuntimeError) as assert_raises:
            general.get_site_model(job.id)

        self.assertEqual('Only 1 site model per job is allowed, found 2.',
                         assert_raises.exception.message)
Пример #7
0
    def test_get_site_model_too_many_site_models(self):
        job = engine.prepare_job()
        site_model_inp1 = models.Input(
            owner=job.owner, digest='fake', path='fake',
            input_type='site_model', size=0
        )
        site_model_inp1.save()
        site_model_inp2 = models.Input(
            owner=job.owner, digest='fake', path='fake',
            input_type='site_model', size=0
        )
        site_model_inp2.save()

        # link both site models to the job:
        models.Input2job(input=site_model_inp1, oq_job=job).save()
        models.Input2job(input=site_model_inp2, oq_job=job).save()

        with self.assertRaises(RuntimeError) as assert_raises:
            general.get_site_model(job.id)

        self.assertEqual('Only 1 site model per job is allowed, found 2.',
                         assert_raises.exception.message)
Пример #8
0
def compute_uhs(the_job, site):
    """Given a `JobContext` and a site of interest, compute UHS. The Java
    `UHSCalculator` is called to do perform the core computation.

    :param the_job:
        :class:`openquake.engine.JobContext` instance.
    :param site:
        :class:`openquake.shapes.Site` instance.
    :returns:
        An `ArrayList` (Java object) of `UHSResult` objects, one per PoE.
    """

    periods = list_to_jdouble_array(the_job['UHS_PERIODS'])
    poes = list_to_jdouble_array(the_job['POES'])
    imls = general.get_iml_list(the_job['INTENSITY_MEASURE_LEVELS'],
                                the_job['INTENSITY_MEASURE_TYPE'])
    max_distance = the_job['MAXIMUM_DISTANCE']

    cache = java.jclass('KVS')(
        config.get('kvs', 'host'),
        int(config.get('kvs', 'port')))

    erf = general.generate_erf(the_job.job_id, cache)
    gmpe_map = general.generate_gmpe_map(the_job.job_id, cache)
    general.set_gmpe_params(gmpe_map, the_job.params)

    uhs_calc = java.jclass('UHSCalculator')(periods, poes, imls, erf, gmpe_map,
                                            max_distance)

    site_model = general.get_site_model(the_job.oq_job.id)

    if site_model is not None:
        sm_data = general.get_closest_site_model_data(site_model, site)
        vs30_type = sm_data.vs30_type.capitalize()
        vs30 = sm_data.vs30
        z1pt0 = sm_data.z1pt0
        z2pt5 = sm_data.z2pt5
    else:
        jp = the_job.oq_job_profile

        vs30_type = jp.vs30_type.capitalize()
        vs30 = jp.reference_vs30_value
        z1pt0 = jp.depth_to_1pt_0km_per_sec
        z2pt5 = jp.reference_depth_to_2pt5km_per_sec_param

    uhs_results = _compute_uhs(
        uhs_calc, site.latitude, site.longitude, vs30_type, vs30, z1pt0, z2pt5
    )

    return uhs_results
Пример #9
0
    def test_parameterize_sites_with_site_model(self):
        job_ctxt = helpers.prepare_job_context(
            helpers.demo_file(
                'simple_fault_demo_hazard/config_with_site_model.gem'
            )
        )

        calc = classical.ClassicalHazardCalculator(job_ctxt)
        calc.initialize()

        # This tests to ensure that the `initialize` implementation for this
        # calculator properly stores the site model in the DB.

        # NOTE: If this test ever breaks, it's probably because the
        # ClassicalHazardCalculator is no longer calling the `initalize` code
        # in its super class (BaseHazardCalculator).
        site_model = hazard_general.get_site_model(job_ctxt.oq_job.id)
        self.assertIsNotNone(site_model)

        set_params_patch = helpers.patch(
            'openquake.calculators.hazard.general.set_java_site_parameters'
        )
        closest_data_patch = helpers.patch(
            'openquake.calculators.hazard.general.get_closest_site_model_data'
        )
        sp_mock = set_params_patch.start()
        cd_mock = closest_data_patch.start()

        try:
            calc.parameterize_sites(job_ctxt.sites_to_compute())

            exp_call_count = len(job_ctxt.sites_to_compute())
            self.assertEqual(exp_call_count, sp_mock.call_count)
            self.assertEqual(exp_call_count, cd_mock.call_count)

        finally:
            # tear down the patches
            set_params_patch.stop()
            closest_data_patch.stop()
Пример #10
0
def compute_disagg_matrix(job_ctxt, site, poe, result_dir):
    """ Compute a complete 5D Disaggregation matrix. This task leans heavily
    on the DisaggregationCalculator (in the OpenQuake Java lib) to handle this
    computation.

    The 5D matrix returned from the java calculator will be saved to a file in
    HDF5 format.

    :param job_ctxt:
        A :class:`openquake.engine.JobContext` which holds all of the
        data we need to run this computation.
    :param site: a single site of interest
    :type site: :class:`openquake.shapes.Site` instance`
    :param poe: Probability of Exceedence
    :type poe: `float`
    :param result_dir: location for the Java code to write the matrix in an
        HDF5 file (in a distributed environment, this should be the path of a
        mounted NFS)

    :returns: 2-tuple of (ground_motion_value, path_to_h5_matrix_file)
    """
    lat_bin_lims = job_ctxt[job_cfg.LAT_BIN_LIMITS]
    lon_bin_lims = job_ctxt[job_cfg.LON_BIN_LIMITS]
    mag_bin_lims = job_ctxt[job_cfg.MAG_BIN_LIMITS]
    eps_bin_lims = job_ctxt[job_cfg.EPS_BIN_LIMITS]

    jd = list_to_jdouble_array

    disagg_calc = java.jclass('DisaggregationCalculator')(jd(lat_bin_lims),
                                                          jd(lon_bin_lims),
                                                          jd(mag_bin_lims),
                                                          jd(eps_bin_lims))

    cache = java.jclass('KVS')(config.get('kvs', 'host'),
                               int(config.get('kvs', 'port')))

    erf = general.generate_erf(job_ctxt.job_id, cache)
    gmpe_map = general.generate_gmpe_map(job_ctxt.job_id, cache)
    general.set_gmpe_params(gmpe_map, job_ctxt.params)

    imls = general.get_iml_list(job_ctxt['INTENSITY_MEASURE_LEVELS'],
                                job_ctxt['INTENSITY_MEASURE_TYPE'])

    site_model = general.get_site_model(job_ctxt.oq_job.id)

    if site_model is not None:
        sm_data = general.get_closest_site_model_data(site_model, site)
        vs30_type = sm_data.vs30_type.capitalize()
        vs30 = sm_data.vs30
        z1pt0 = sm_data.z1pt0
        z2pt5 = sm_data.z2pt5
    else:
        jp = job_ctxt.oq_job_profile

        vs30_type = jp.vs30_type.capitalize()
        vs30 = jp.reference_vs30_value
        z1pt0 = jp.depth_to_1pt_0km_per_sec
        z2pt5 = jp.reference_depth_to_2pt5km_per_sec_param

    matrix_result = _compute_matrix(disagg_calc, site.latitude, site.longitude,
                                    erf, gmpe_map, poe, imls, vs30_type, vs30,
                                    z1pt0, z2pt5)

    matrix_path = save_5d_matrix_to_h5(result_dir,
                                       numpy.array(matrix_result.getMatrix()))

    return (matrix_result.getGMV(), matrix_path)
Пример #11
0
def compute_disagg_matrix(job_ctxt, site, poe, result_dir):
    """ Compute a complete 5D Disaggregation matrix. This task leans heavily
    on the DisaggregationCalculator (in the OpenQuake Java lib) to handle this
    computation.

    The 5D matrix returned from the java calculator will be saved to a file in
    HDF5 format.

    :param job_ctxt:
        A :class:`openquake.engine.JobContext` which holds all of the
        data we need to run this computation.
    :param site: a single site of interest
    :type site: :class:`openquake.shapes.Site` instance`
    :param poe: Probability of Exceedence
    :type poe: `float`
    :param result_dir: location for the Java code to write the matrix in an
        HDF5 file (in a distributed environment, this should be the path of a
        mounted NFS)

    :returns: 2-tuple of (ground_motion_value, path_to_h5_matrix_file)
    """
    lat_bin_lims = job_ctxt[job_cfg.LAT_BIN_LIMITS]
    lon_bin_lims = job_ctxt[job_cfg.LON_BIN_LIMITS]
    mag_bin_lims = job_ctxt[job_cfg.MAG_BIN_LIMITS]
    eps_bin_lims = job_ctxt[job_cfg.EPS_BIN_LIMITS]

    jd = list_to_jdouble_array

    disagg_calc = java.jclass('DisaggregationCalculator')(
        jd(lat_bin_lims), jd(lon_bin_lims),
        jd(mag_bin_lims), jd(eps_bin_lims))

    cache = java.jclass('KVS')(
        config.get('kvs', 'host'),
        int(config.get('kvs', 'port')))

    erf = general.generate_erf(job_ctxt.job_id, cache)
    gmpe_map = general.generate_gmpe_map(job_ctxt.job_id, cache)
    general.set_gmpe_params(gmpe_map, job_ctxt.params)

    imls = general.get_iml_list(job_ctxt['INTENSITY_MEASURE_LEVELS'],
                                job_ctxt['INTENSITY_MEASURE_TYPE'])

    site_model = general.get_site_model(job_ctxt.oq_job.id)

    if site_model is not None:
        sm_data = general.get_closest_site_model_data(site_model, site)
        vs30_type = sm_data.vs30_type.capitalize()
        vs30 = sm_data.vs30
        z1pt0 = sm_data.z1pt0
        z2pt5 = sm_data.z2pt5
    else:
        jp = job_ctxt.oq_job_profile

        vs30_type = jp.vs30_type.capitalize()
        vs30 = jp.reference_vs30_value
        z1pt0 = jp.depth_to_1pt_0km_per_sec
        z2pt5 = jp.reference_depth_to_2pt5km_per_sec_param

    matrix_result = _compute_matrix(
        disagg_calc, site.latitude, site.longitude, erf, gmpe_map, poe, imls,
        vs30_type, vs30, z1pt0, z2pt5)

    matrix_path = save_5d_matrix_to_h5(result_dir,
                                       numpy.array(matrix_result.getMatrix()))

    return (matrix_result.getGMV(), matrix_path)