<commit_before>"""
This module contains the alpha-s calculation
"""

import warnings

import scipy

from ..classes.adsorbate import Adsorbate
from ..graphing.calcgraph import plot_tp
from ..utilities.exceptions import ParameterError
from ..utilities.math_utilities import find_linear_sections
from .bet import area_BET


def alpha_s(isotherm, reference_isotherm, reference_area=None,
            reducing_pressure=0.4, limits=None, verbose=False):
    """
    Calculates the external surface area and adsorbed volume using the alpha s method


    Pass an isotherm object to the function to have the alpha-s method applied to it.
    The ``reference_isotherm`` parameter is an Isotherm class which will form the
    x-axis of the alpha-s method.
    The ``limits`` parameter takes the form of an array of two numbers, which are the
    upper and lower limits of the section which should be taken for analysis.

    Parameters
    ----------
    isotherm : PointIsotherm
        the isotherm of which to calculate the alpha-s plot parameters
    reference_isotherm : PointIsotherm
        the isotherm to use as reference
    reference_area : str, optional
        area of the reference material
        if not specified, the BET method is used to calculate it
    reducing_pressure : float, optional
        p/p0 value at which the loading is reduced
        default is 0.4 as it is the closing point for the nitrogen
        hysteresis loop
    limits : [:obj:`float`, :obj:`float`], optional
        manual limits for region selection
    verbose : bool, optional
        prints extra information and plots graphs of the calculation

    Returns
    -------
    list
        a list of dictionaries containing the calculated parameters for each
        straight section, with each dictionary of the form. The basis of these
        results will be derived from the basis of the isotherm (per mass or per
        volume of adsorbent):

            - ``section(array)`` : the points of the plot chosen for the line
            - ``area(float)`` : calculated surface area, from the section parameters
            - ``adsorbed_volume(float)`` : the amount adsorbed in the pores as calculated
              per section
            - ``slope(float)`` : slope of the straight trendline fixed through the region
            - ``intercept(float)`` : intercept of the straight trendline through the region
            - ``corr_coef(float)`` : correlation coefficient of the linear region

    Notes
    -----
    *Description*

    In order to extend the t-plot analysis with other adsorbents and non-standard
    thickness curves, the :math:`\\alpha_s` method was devised [#]_. Instead of
    a formula that describes the thickness of the adsorbed layer, a reference
    isotherm is used. This isotherm is measured on a non-porous version of the
    material with the same surface characteristics and with the same adsorbate.
    The :math:`\\alpha_s` values are obtained from this isotherm by regularisation with
    an adsorption amount at a specific relative pressure, usually taken as 0.4 since
    nitrogen hysterisis loops theoretically close at this value

    .. math::

        \\alpha_s = \\frac{n_a}{n_{0.4}}

    The analysis then proceeds as in the t-plot method.

    The slope of the linear section can be used to calculate the area where the adsorption
    is taking place. If it is of a linear region at the start of the curve, it will represent
    the total surface area of the material. If at the end of the curve, it will instead
    represent external surface area of the sample.
    The calculation uses the known area of the reference material. If unknown, the area
    will be calculated here using the BET method.

    .. math::

        A = \\frac{s A_{ref}}{(n_{ref})_{0.4}}

    If the region selected is after a vertical deviation, the intercept of the line
    will no longer pass through the origin. This intercept be used to calculate the
    pore volume through the following equation:

    .. math::

        V_{ads} = \\frac{i M_m}{\\rho_{l}}

    *Limitations*

    The reference isotherm chosen for the :math:`\\alpha_s` mehtod must be a description
    of the adsorption on a completely non-porous sample of the same material. It is
    often impossible to obtain such non-porous versions, therefore care must be taken
    how the reference isotherm is defined.
<commit_msg>fixed typo<commit_after>"""
This module contains the alpha-s calculation
"""

import warnings

import scipy

from ..classes.adsorbate import Adsorbate
from ..graphing.calcgraph import plot_tp
from ..utilities.exceptions import ParameterError
from ..utilities.math_utilities import find_linear_sections
from .bet import area_BET


def alpha_s(isotherm, reference_isotherm, reference_area=None,
            reducing_pressure=0.4, limits=None, verbose=False):
    """
    Calculates the external surface area and adsorbed volume using the alpha s method


    Pass an isotherm object to the function to have the alpha-s method applied to it.
    The ``reference_isotherm`` parameter is an Isotherm class which will form the
    x-axis of the alpha-s method.
    The ``limits`` parameter takes the form of an array of two numbers, which are the
    upper and lower limits of the section which should be taken for analysis.

    Parameters
    ----------
    isotherm : PointIsotherm
        the isotherm of which to calculate the alpha-s plot parameters
    reference_isotherm : PointIsotherm
        the isotherm to use as reference
    reference_area : str, optional
        area of the reference material
        if not specified, the BET method is used to calculate it
    reducing_pressure : float, optional
        p/p0 value at which the loading is reduced
        default is 0.4 as it is the closing point for the nitrogen
        hysteresis loop
    limits : [:obj:`float`, :obj:`float`], optional
        manual limits for region selection
    verbose : bool, optional
        prints extra information and plots graphs of the calculation

    Returns
    -------
    list
        a list of dictionaries containing the calculated parameters for each
        straight section, with each dictionary of the form. The basis of these
        results will be derived from the basis of the isotherm (per mass or per
        volume of adsorbent):

            - ``section(array)`` : the points of the plot chosen for the line
            - ``area(float)`` : calculated surface area, from the section parameters
            - ``adsorbed_volume(float)`` : the amount adsorbed in the pores as calculated
              per section
            - ``slope(float)`` : slope of the straight trendline fixed through the region
            - ``intercept(float)`` : intercept of the straight trendline through the region
            - ``corr_coef(float)`` : correlation coefficient of the linear region

    Notes
    -----
    *Description*

    In order to extend the t-plot analysis with other adsorbents and non-standard
    thickness curves, the :math:`\\alpha_s` method was devised [#]_. Instead of
    a formula that describes the thickness of the adsorbed layer, a reference
    isotherm is used. This isotherm is measured on a non-porous version of the
    material with the same surface characteristics and with the same adsorbate.
    The :math:`\\alpha_s` values are obtained from this isotherm by regularisation with
    an adsorption amount at a specific relative pressure, usually taken as 0.4 since
    nitrogen hysterisis loops theoretically close at this value

    .. math::

        \\alpha_s = \\frac{n_a}{n_{0.4}}

    The analysis then proceeds as in the t-plot method.

    The slope of the linear section can be used to calculate the area where the adsorption
    is taking place. If it is of a linear region at the start of the curve, it will represent
    the total surface area of the material. If at the end of the curve, it will instead
    represent external surface area of the sample.
    The calculation uses the known area of the reference material. If unknown, the area
    will be calculated here using the BET method.

    .. math::

        A = \\frac{s A_{ref}}{(n_{ref})_{0.4}}

    If the region selected is after a vertical deviation, the intercept of the line
    will no longer pass through the origin. This intercept be used to calculate the
    pore volume through the following equation:

    .. math::

        V_{ads} = \\frac{i M_m}{\\rho_{l}}

    *Limitations*

    The reference isotherm chosen for the :math:`\\alpha_s` method must be a description
    of the adsorption on a completely non-porous sample of the same material. It is
    often impossible to obtain such non-porous versions, therefore care must be taken
    how the reference isotherm is defined.

{'fraction_non_alphanumeric': 0.05927103440445101, 'fraction_numerical': 0.002498012944248893, 'mean_word_length': 2.907719609582964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6939691', 'n_tokens_mistral': 2279, 'n_tokens_neox': 2162, 'n_words': 1127}