<commit_before># -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright (C) 2012-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:`BooreAtkinson2008`,
:class:`AtkinsonBoore2006`,
:class:`AtkinsonBoore2006Modified2011`.
:class:`AtkinsonBoore2006SGS`.
"""
import numpy as np
from scipy.constants import g
from math import log10

from openquake.hazardlib.gsim.base import GMPE, CoeffsTable, add_alias
from openquake.hazardlib import const
from openquake.hazardlib.imt import PGA, PGV, SA
from openquake.hazardlib.gsim.utils import (
    mblg_to_mw_atkinson_boore_87, mblg_to_mw_johnston_96, clip_mean,
    get_fault_type_dummy_variables)

#: IMT-independent coefficients. std_total is the total standard deviation,
#: see Table 6, pag 2192 and Table 9, pag 2202. R0, R1, R2 are coefficients
#: required for mean calculation - see equation (5) pag 2191. v1, v2, Vref
#: are coefficients required for soil response calculation, see table 8,
#: p. 2201
# the std is converted from base 10 to base e
std_total = np.log(10 ** 0.30),
R0 = 10.0
R1 = 70.0
R2 = 140.0
# v1 = 180.0
# v2 = 300.0
# Vref = 760.0


def _clip_distances(rrup):
    """
    Return array of distances with values clipped to 1. See end of
    paragraph 'Methodology and Model Parameters', p. 2182. The equations
    have a singularity for distance = 0, so that's why distances are
    clipped to 1.
    """
    rrup = rrup.copy()
    rrup[rrup < 1] = 1
    return rrup


def _compute_distance_scaling(ctx, C):
    """
    Compute distance-scaling term, equations (3) and (4), pag 107.
    """
    Mref = 4.5
    Rref = 1.0
    R = np.sqrt(ctx.rjb ** 2 + C['h'] ** 2)
    return (C['c1'] + C['c2'] * (ctx.mag - Mref)) * np.log(R / Rref) + \
        C['c3'] * (R - Rref)


def _compute_f0_factor(rrup):
    """
    Compute and return factor f0 - see equation (5), 6th term, p. 2191.
    """
    f0 = np.log10(R0 / rrup)
    f0[f0 < 0] = 0.0
    return f0


def _compute_f1_factor(rrup):
    """
    Compute and return factor f1 - see equation (5), 4th term, p. 2191
    """
    f1 = np.log10(rrup)
    logR1 = np.log10(R1)
    f1[f1 > logR1] = logR1
    return f1


def _compute_f2_factor(rrup):
    """
    Compute and return factor f2, see equation (5), 5th term, pag 2191
    """
    f2 = np.log10(rrup / R2)
    f2[f2 < 0] = 0.0
    return f2


def _compute_magnitude_scaling(ctx, C):
    """
    Compute magnitude-scaling term, equations (5a) and (5b), pag 107.
    """
    return _compute_ms(ctx, C)


def _compute_mean(C, f0, f1, f2, SC, mag, rrup, idxs, mean, scale_fac):
    """
    Compute mean value (for a set of indexes) without site amplification
    terms. This is equation (5), p. 2191, without S term.
    """
    if idxs.sum() == 0:  # no effect
        return
    mag = mag[idxs]
    sda = _compute_stress_drop_adjustment(SC, mag, scale_fac[idxs])
    mean[idxs] = (C['c1'] +
                  C['c2'] * mag +
                  C['c3'] * (mag ** 2) +
                  (C['c4'] + C['c5'] * mag) * f1[idxs] +
                  (C['c6'] + C['c7'] * mag) * f2[idxs] +
                  (C['c8'] + C['c9'] * mag) * f0[idxs] +
                  C['c10'] * rrup[idxs] + sda)


def _compute_ms(ctx, C):
    SS, NS, RS = get_fault_type_dummy_variables(ctx)
    res = C['e2'] * SS + C['e3'] * NS + C['e4'] * RS + C['e7'] * (
        ctx.mag - C['Mh'])
    less = ctx.mag <= C['Mh']
    res[less] = (C['e2'] * SS[less] + C['e3'] * NS[less] + C['e4'] * RS[less] +
                 C['e5'] * (ctx.mag[less] - C['Mh']) + C['e6'] *
                 (ctx.mag[less] - C['Mh']) ** 2)
    return res


def _compute_non_linear_slope(vs30, C):
    """
    Compute non-linear slope factor,
    equations (13a) to (13d), pag 108-109.<commit_msg>Fixed test<commit_after># -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright (C) 2012-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:`BooreAtkinson2008`,
:class:`AtkinsonBoore2006`,
:class:`AtkinsonBoore2006Modified2011`.
:class:`AtkinsonBoore2006SGS`.
"""
import numpy as np
from scipy.constants import g
from math import log10

from openquake.hazardlib.gsim.base import GMPE, CoeffsTable, add_alias
from openquake.hazardlib import const
from openquake.hazardlib.imt import PGA, PGV, SA
from openquake.hazardlib.gsim.utils import (
    mblg_to_mw_atkinson_boore_87, mblg_to_mw_johnston_96, clip_mean,
    get_fault_type_dummy_variables)

#: IMT-independent coefficients. std_total is the total standard deviation,
#: see Table 6, pag 2192 and Table 9, pag 2202. R0, R1, R2 are coefficients
#: required for mean calculation - see equation (5) pag 2191. v1, v2, Vref
#: are coefficients required for soil response calculation, see table 8,
#: p. 2201
# the std is converted from base 10 to base e
std_total = np.log(10 ** 0.30),
R0 = 10.0
R1 = 70.0
R2 = 140.0
# v1 = 180.0
# v2 = 300.0
# Vref = 760.0


def _clip_distances(rrup):
    """
    Return array of distances with values clipped to 1. See end of
    paragraph 'Methodology and Model Parameters', p. 2182. The equations
    have a singularity for distance = 0, so that's why distances are
    clipped to 1.
    """
    rrup = rrup.copy()
    rrup[rrup < 1] = 1
    return rrup


def _compute_distance_scaling(ctx, C):
    """
    Compute distance-scaling term, equations (3) and (4), pag 107.
    """
    Mref = 4.5
    Rref = 1.0
    R = np.sqrt(ctx.rjb ** 2 + C['h'] ** 2)
    return (C['c1'] + C['c2'] * (ctx.mag - Mref)) * np.log(R / Rref) + \
        C['c3'] * (R - Rref)


def _compute_f0_factor(rrup):
    """
    Compute and return factor f0 - see equation (5), 6th term, p. 2191.
    """
    f0 = np.log10(R0 / rrup)
    f0[f0 < 0] = 0.0
    return f0


def _compute_f1_factor(rrup):
    """
    Compute and return factor f1 - see equation (5), 4th term, p. 2191
    """
    f1 = np.log10(rrup)
    logR1 = np.log10(R1)
    f1[f1 > logR1] = logR1
    return f1


def _compute_f2_factor(rrup):
    """
    Compute and return factor f2, see equation (5), 5th term, pag 2191
    """
    f2 = np.log10(rrup / R2)
    f2[f2 < 0] = 0.0
    return f2


def _compute_magnitude_scaling(ctx, C):
    """
    Compute magnitude-scaling term, equations (5a) and (5b), pag 107.
    """
    return _compute_ms(ctx, C)


def _compute_mean(C, f0, f1, f2, SC, mag, rrup, idxs, mean, scale_fac):
    """
    Compute mean value (for a set of indexes) without site amplification
    terms. This is equation (5), p. 2191, without S term.
    """
    if idxs.sum() == 0:  # no effect
        return
    if np.isinstance(scale_fac, np.ndarray):
        scale_fac = scale_fac[idxs]
    mag = mag[idxs]
    sda = _compute_stress_drop_adjustment(SC, mag, scale_fac)
    mean[idxs] = (C['c1'] +
                  C['c2'] * mag +
                  C['c3'] * (mag ** 2) +
                  (C['c4'] + C['c5'] * mag) * f1[idxs] +
                  (C['c6'] + C['c7'] * mag) * f2[idxs] +
                  (C['c8'] + C['c9'] * mag) * f0[idxs] +
                  C['c10'] * rrup[idxs] + sda)


def _compute_ms(ctx, C):
    SS, NS, RS = get_fault_type_dummy_variables(ctx)
    res = C['e2'] * SS + C['e3'] * NS + C['e4'] * RS + C['e7'] * (
        ctx.mag - C['Mh'])
    less = ctx.mag <= C['Mh']
    res[less] = (C['e2'] * SS[less] + C['e3'] * NS[less] + C['e4'] * RS[less] +
                 C['e5'] * (ctx.mag[less] - C['Mh']) + C['e6'] *
                 (ctx.mag[less] - C['Mh']) ** 2)
    return res


def _compute_non_linear_slope(vs30, C):
    """
    Compute non-linear slope factor,
    equations (13a) to (13d), pag 108-109.
{'fraction_non_alphanumeric': 0.13668330308529947, 'fraction_numerical': 0.05172413793103448, 'mean_word_length': 2.8301476976542137, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7479449', 'n_tokens_mistral': 3524, 'n_tokens_neox': 3199, 'n_words': 997}