source_modelling.moment

Utility functions for working with moment rate and moment.

 1"""Utility functions for working with moment rate and moment."""
 2
 3import numpy as np
 4import numpy.typing as npt
 5import pandas as pd
 6import scipy as sp
 7from scipy.sparse import csr_array
 8
 9# Shear scaling constant
10MU = 3.3e10
11
12
13def moment_rate_over_time_from_slip(
14    area: npt.ArrayLike, slip: csr_array, dt: float, nt: int
15) -> pd.DataFrame:
16    """Compute a moment rate dataframe from subfaults given with area and slip.
17
18    Parameters
19    ----------
20    area : array like
21        The area of each subfault (in cm^2).
22    slip : csr_array
23        A sparse containing the slip for each subfault and each time
24        window. Has shape (number of points, number of time windows).
25    dt : float
26        Length of each time window (s).
27    nt : int
28        The number of time windows.
29
30    Returns
31    -------
32    pd.DataFrame
33        A dataframe with index in time (s) and column 'moment_rate' (Nm/s).
34    """
35    slip_over_time = (
36        np.asarray(sp.sparse.diags(np.asarray(area)).dot(slip).sum(axis=0))[0] / 1e6
37    )
38    moment_rate = MU * slip_over_time
39    time_values = np.arange(nt) * dt
40
41    moment_rate_df = pd.DataFrame(
42        {"t": time_values, "moment_rate": moment_rate}
43    ).set_index("t")
44    return moment_rate_df
45
46
47def moment_to_magnitude(moment: float) -> float:
48    """Convert moment to magnitude.
49
50    NOTE: the qcore mag_scaling module does not have this expression.
51
52    Parameters
53    ----------
54    moment : float
55        The moment of the rupture.
56
57    Returns
58    -------
59    float
60        Rupture magnitude
61    """
62    return 2 / 3 * np.log10(moment) - 6.03333
63
64
65def moment_over_time_from_moment_rate(moment_rate_df: pd.DataFrame) -> pd.DataFrame:
66    """Integrate a moment rate dataframe into a cumulative moment dataframe.
67
68    Parameters
69    ----------
70    moment_rate_df : pd.DataFrame
71        Dataframe containing time as an index, and 'moment_rate' column.
72
73    Returns
74    -------
75    pd.DataFrame
76        A dataframe the same index and a 'moment' column with cumulative rupture moment.
77    """
78    integrate_f = np.vectorize(
79        lambda i: np.trapz(
80            moment_rate_df["moment_rate"].iloc[: i + 1].to_numpy(),
81            moment_rate_df.index.values[: i + 1],
82        )
83    )
84    return pd.DataFrame(
85        {
86            "t": moment_rate_df.index.values,
87            "moment": integrate_f(np.arange(len(moment_rate_df))),
88        }
89    ).set_index("t")
MU = 33000000000.0
def moment_rate_over_time_from_slip( area: Union[Buffer, numpy._typing._array_like._SupportsArray[numpy.dtype[Any]], numpy._typing._nested_sequence._NestedSequence[numpy._typing._array_like._SupportsArray[numpy.dtype[Any]]], bool, int, float, complex, str, bytes, numpy._typing._nested_sequence._NestedSequence[Union[bool, int, float, complex, str, bytes]]], slip: scipy.sparse._csr.csr_array, dt: float, nt: int) -> pandas.core.frame.DataFrame:
14def moment_rate_over_time_from_slip(
15    area: npt.ArrayLike, slip: csr_array, dt: float, nt: int
16) -> pd.DataFrame:
17    """Compute a moment rate dataframe from subfaults given with area and slip.
18
19    Parameters
20    ----------
21    area : array like
22        The area of each subfault (in cm^2).
23    slip : csr_array
24        A sparse containing the slip for each subfault and each time
25        window. Has shape (number of points, number of time windows).
26    dt : float
27        Length of each time window (s).
28    nt : int
29        The number of time windows.
30
31    Returns
32    -------
33    pd.DataFrame
34        A dataframe with index in time (s) and column 'moment_rate' (Nm/s).
35    """
36    slip_over_time = (
37        np.asarray(sp.sparse.diags(np.asarray(area)).dot(slip).sum(axis=0))[0] / 1e6
38    )
39    moment_rate = MU * slip_over_time
40    time_values = np.arange(nt) * dt
41
42    moment_rate_df = pd.DataFrame(
43        {"t": time_values, "moment_rate": moment_rate}
44    ).set_index("t")
45    return moment_rate_df

Compute a moment rate dataframe from subfaults given with area and slip.

Parameters
  • area (array like): The area of each subfault (in cm^2).
  • slip (csr_array): A sparse containing the slip for each subfault and each time window. Has shape (number of points, number of time windows).
  • dt (float): Length of each time window (s).
  • nt (int): The number of time windows.
Returns
  • pd.DataFrame: A dataframe with index in time (s) and column 'moment_rate' (Nm/s).
def moment_to_magnitude(moment: float) -> float:
48def moment_to_magnitude(moment: float) -> float:
49    """Convert moment to magnitude.
50
51    NOTE: the qcore mag_scaling module does not have this expression.
52
53    Parameters
54    ----------
55    moment : float
56        The moment of the rupture.
57
58    Returns
59    -------
60    float
61        Rupture magnitude
62    """
63    return 2 / 3 * np.log10(moment) - 6.03333

Convert moment to magnitude.

NOTE: the qcore mag_scaling module does not have this expression.

Parameters
  • moment (float): The moment of the rupture.
Returns
  • float: Rupture magnitude
def moment_over_time_from_moment_rate( moment_rate_df: pandas.core.frame.DataFrame) -> pandas.core.frame.DataFrame:
66def moment_over_time_from_moment_rate(moment_rate_df: pd.DataFrame) -> pd.DataFrame:
67    """Integrate a moment rate dataframe into a cumulative moment dataframe.
68
69    Parameters
70    ----------
71    moment_rate_df : pd.DataFrame
72        Dataframe containing time as an index, and 'moment_rate' column.
73
74    Returns
75    -------
76    pd.DataFrame
77        A dataframe the same index and a 'moment' column with cumulative rupture moment.
78    """
79    integrate_f = np.vectorize(
80        lambda i: np.trapz(
81            moment_rate_df["moment_rate"].iloc[: i + 1].to_numpy(),
82            moment_rate_df.index.values[: i + 1],
83        )
84    )
85    return pd.DataFrame(
86        {
87            "t": moment_rate_df.index.values,
88            "moment": integrate_f(np.arange(len(moment_rate_df))),
89        }
90    ).set_index("t")

Integrate a moment rate dataframe into a cumulative moment dataframe.

Parameters
  • moment_rate_df (pd.DataFrame): Dataframe containing time as an index, and 'moment_rate' column.
Returns
  • pd.DataFrame: A dataframe the same index and a 'moment' column with cumulative rupture moment.