<commit_before># -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright (C) 2014-2022 GEM Foundation
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>.

"""
Module exports :class:`DowrickRhoades2005Asc`,:class:`DowrickRhoades2005SInter`
:class:`DowrickRhoades2005SSlab`, and :class:`DowrickRhoades2005Volc`.
"""
import numpy as np

from openquake.baselib.general import CallableDict
from openquake.hazardlib.gsim.base import GMPE, CoeffsTable
from openquake.hazardlib import const
from openquake.hazardlib.imt import MMI


def _compute_mean(C, mag, rrup, hypo_depth, delta_R, delta_S,
                  delta_V, delta_I, vs30):

    """
    Compute MMI Intensity Value as per Equation in Table 5 and
    Table 7 pag 198.
    """
    # mean is calculated for all the 4 classes using the same equation.
    # For DowrickRhoades2005SSlab, the coefficients which don't appear in
    # Model 3 equationare assigned to zero

    mean = (C['A1'] + (C['A2'] + C['A2R'] * delta_R + C['A2V'] * delta_V) *
            mag + (C['A3'] + C['A3S'] * delta_S + C['A3V'] * delta_V) *
            np.log10(np.power((rrup**3 + C['d']**3), 1.0 / 3.0)) +
            C['A4'] * hypo_depth + C['A5'] * delta_I)

    # Get S site class term
    S = _get_site_class(vs30, mean)

    # Add S amplification term to mean value
    mean = mean + S

    return mean


_get_deltas = CallableDict()


@_get_deltas.add(const.TRT.ACTIVE_SHALLOW_CRUST)
def _get_deltas_1(trt, rake):
    """
    Return the value of deltas (delta_R, delta_S, delta_V, delta_I),
    as defined in "Table 5: Model 1" pag 198
    """
    # delta_R = 1 for reverse focal mechanism (45<rake<135)
    # and for interface events, 0 for all other events
    # delta_S = 1 for Strike-slip focal mechanisms (0<=rake<=45) or
    # (135<=rake<=180) or (-45<=rake<=0), 0 for all other events
    # delta_V = 1 for TVZ events, 0 for all other events
    # delta_I = 1 for interface events, 0 for all other events

    # All deltas = 0 for Model 3: Deep Region, pag 198

    delta_R, delta_S = 0, 0
    delta_V, delta_I = 0, 0

    if rake > 45.0 and rake < 135.0:
        delta_R = 1

    if (rake >= 0.0 and rake <= 45.0) or \
       (rake >= 135 and rake <= 180.0) or \
       (rake >= -180.0 and rake <= -135.0) or \
       (rake >= -45.0 and rake < 0.0):
        delta_S = 1

    return delta_R, delta_S, delta_V, delta_I


@_get_deltas.add(const.TRT.SUBDUCTION_INTERFACE)
def _get_deltas_2(trt, rake):
    """
    Return the value of deltas (delta_R, delta_S, delta_V, delta_I),
    as defined in "Table 5: Model 1" pag 198
    """
    # delta_I = 1, delta_R = 1 for interface events
    # delta_S = 0, delta_V = 0

    delta_R, delta_S = 1, 0
    delta_V, delta_I = 0, 1

    return delta_R, delta_S, delta_V, delta_I


@_get_deltas.add(const.TRT.SUBDUCTION_INTRASLAB)
def _get_deltas_3(trt, rake):
    """
    Return the value of deltas (delta_R, delta_S, delta_V, delta_I),
    as defined in "Table 5: Model 1" pag 198
    """
    # All deltas = 0 for DowrickRhoades2005SSlab Model 3: Deep Region,
    # pag 198

    delta_R, delta_S = 0, 0
    delta_V, delta_I = 0, 0

    return delta_R, delta_S, delta_V, delta_I


@_get_deltas.add(const.TRT.VOLCANIC)
def _get_deltas_4(trt, rake):
    """
    Return the value of deltas (delta_R, delta_S, delta_V, delta_I),
    as defined in "Table 5: Model 1" pag 198
    """
    # delta_V = 1 for TVZ events, 0 for all other events
    # delta_R =0, delta_S = 0, delta_I = 0

    delta_R, delta_S = 0, 0
    delta_V, delta_I = 1, 0

    return delta_R, delta_S, delta_V, delta_I


def _get_site_class(vs30, mmi_mean):
    """
    Return site class flag for:
    Class E - Very Soft Soil        vs30 < 180
    Class D - Deep or Soft Soil     vs30 >= 180 and vs30 <= 360
    Class C - Shallow Soil          vs30 > 360 and vs30 <= 760
    Class B - Rock                  vs30 > 760 and vs30 <= 1500
    Class A - Strong Rock           vs30 >= 180 and vs30 <= 360
    The S site class is equal to
        S = c1 if MMI <= 7
        S = c1 - d *(MMI - 7.0) if 7<MMI<9.5
        S = c2 if MMI >= 9.5
    """

    if vs30[0] < 180:
        c1 = 1.0
        c2 = -0.25<commit_msg>Vectorizing dowrickrhoades<commit_after># -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright (C) 2014-2022 GEM Foundation
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>.

"""
Module exports :class:`DowrickRhoades2005Asc`,:class:`DowrickRhoades2005SInter`
:class:`DowrickRhoades2005SSlab`, and :class:`DowrickRhoades2005Volc`.
"""
import numpy as np

from openquake.baselib.general import CallableDict
from openquake.hazardlib.gsim.base import GMPE, CoeffsTable
from openquake.hazardlib import const
from openquake.hazardlib.imt import MMI


def _compute_mean(C, mag, rrup, hypo_depth, delta_R, delta_S,
                  delta_V, delta_I, vs30):

    """
    Compute MMI Intensity Value as per Equation in Table 5 and
    Table 7 pag 198.
    """
    # mean is calculated for all the 4 classes using the same equation.
    # For DowrickRhoades2005SSlab, the coefficients which don't appear in
    # Model 3 equationare assigned to zero

    mean = (C['A1'] + (C['A2'] + C['A2R'] * delta_R + C['A2V'] * delta_V) *
            mag + (C['A3'] + C['A3S'] * delta_S + C['A3V'] * delta_V) *
            np.log10(np.power((rrup**3 + C['d']**3), 1.0 / 3.0)) +
            C['A4'] * hypo_depth + C['A5'] * delta_I)

    # Add site class amplification term to mean value
    return mean + _get_site_class(vs30, mean)


def _get_deltas(trt, rake):
    """
    Return the value of deltas (delta_R, delta_S, delta_V, delta_I),
    as defined in "Table 5: Model 1" pag 198
    """
    # delta_R = 1 for reverse focal mechanism (45<rake<135)
    # and for interface events, 0 for all other events
    # delta_S = 1 for Strike-slip focal mechanisms (0<=rake<=45) or
    # (135<=rake<=180) or (-45<=rake<=0), 0 for all other events
    # delta_V = 1 for TVZ events, 0 for all other events
    # delta_I = 1 for interface events, 0 for all other events

    # All deltas = 0 for Model 3: Deep Region, pag 198

    if trt == const.TRT.ACTIVE_SHALLOW_CRUST:
        delta_R, delta_S = 0, 0
        delta_V, delta_I = 0, 0
        if rake > 45.0 and rake < 135.0:
            delta_R = 1
        if (rake >= 0.0 and rake <= 45.0) or \
           (rake >= 135 and rake <= 180.0) or \
           (rake >= -180.0 and rake <= -135.0) or \
           (rake >= -45.0 and rake < 0.0):
            delta_S = 1
    elif trt == const.TRT.SUBDUCTION_INTERFACE:
        delta_R, delta_S = 1, 0
        delta_V, delta_I = 0, 1
    elif trt == const.TRT.SUBDUCTION_INTRASLAB:
        delta_R, delta_S = 0, 0
        delta_V, delta_I = 0, 0
    elif trt == const.TRT.VOLCANIC:
        delta_R, delta_S = 0, 0
        delta_V, delta_I = 1, 0

    return delta_R, delta_S, delta_V, delta_I


def _get_site_class(vs30, mmi_mean):
    """
    Return site class flag for:
    Class E - Very Soft Soil        vs30 < 180
    Class D - Deep or Soft Soil     vs30 >= 180 and vs30 <= 360
    Class C - Shallow Soil          vs30 > 360 and vs30 <= 760
    Class B - Rock                  vs30 > 760 and vs30 <= 1500
    Class A - Strong Rock           vs30 >= 180 and vs30 <= 360
    The S site class is equal to
        S = c1 if MMI <= 7
        S = c1 - d *(MMI - 7.0) if 7<MMI<9.5
        S = c2 if MMI >= 9.5
    """
    if vs30[0] < 180:
        c1 = 1.0
        c2 = -0.25
{'fraction_non_alphanumeric': 0.1063238359972203, 'fraction_numerical': 0.05478341440815381, 'mean_word_length': 2.648077735530207, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4732150', 'n_tokens_mistral': 3416, 'n_tokens_neox': 3024, 'n_words': 1060}