Source code for buffalo_wings.airfoil.internal.analytic.naca.naca4_modified_airfoil

"""Spec-backed runtime classes for modified NACA 4-digit airfoils."""

from __future__ import annotations

from copy import deepcopy
from typing import Literal, Self, cast

from buffalo_core.numeric import as_float_scalar
from buffalo_core.typing import FloatScalar

from buffalo_wings.airfoil.internal.analytic.orthogonal_base import (
    OrthogonalAirfoil,
)
from buffalo_wings.airfoil.internal.schema.naca import (
    Naca4ModifiedAirfoilParamsSpec,
    Naca4ModifiedAirfoilSpec,
)

from .naca4_airfoil import (
    NACA4_MAX_CAMBER_LIMIT,
    NACA4_MAX_CAMBER_LOCATION_LIMIT,
    NACA4_MAX_THICKNESS_LIMIT,
)
from .naca4_camber import Naca4DigitCamber
from .naca45_modified_thickness import (
    LEADING_EDGE_INDEX_MAX,
    LEADING_EDGE_INDEX_MIN,
    MAX_THICKNESS_LOCATION_MAX,
    MAX_THICKNESS_LOCATION_MIN,
    Naca45DigitModifiedThicknessClassic,
    Naca45DigitModifiedThicknessParams,
)

NACA4_MODIFIED_DESIGNATION_LENGTH = 7
NACA4_MODIFIED_DESIGNATION_DASH_INDEX = 4
NACA4_MODIFIED_MIN_THICKNESS_LOCATION = 0.1


def _validate_naca4_modified_designation(designation: str) -> None:
    """
    Validate a modified NACA 4-digit designation string.

    Parameters
    ----------
    designation : str
        Modified NACA 4-digit designation string in ``####-##`` form.

    Raises
    ------
    ValueError
        If ``designation`` does not match the supported modified-series
        format.
    """
    if len(designation) != NACA4_MODIFIED_DESIGNATION_LENGTH:
        raise ValueError(
            "Modified NACA 4-digit designations must be 7 characters."
        )
    if designation[NACA4_MODIFIED_DESIGNATION_DASH_INDEX] != "-":
        raise ValueError(
            "Modified NACA 4-digit designations must use the form ####-##."
        )
    digits = designation.replace("-", "")
    if not digits.isdigit():
        raise ValueError(
            "Modified NACA 4-digit designations must contain only digits."
        )


def _validate_naca4_modified_params(
    params: Naca4ModifiedAirfoilParamsSpec,
) -> None:
    """
    Validate the explicit modified NACA 4-digit parameters.

    Parameters
    ----------
    params : Naca4ModifiedAirfoilParamsSpec
        Explicit modified NACA 4-digit parameter specification to validate.

    Raises
    ------
    ValueError
        If any parameter violates the supported modified-series bounds or
        consistency rules.
    """
    if not 0.0 <= params.m < NACA4_MAX_CAMBER_LIMIT:
        raise ValueError(
            "Modified NACA 4-digit parameter m must satisfy 0 <= m < 0.1."
        )
    if not 0.0 <= params.p <= NACA4_MAX_CAMBER_LOCATION_LIMIT:
        raise ValueError(
            "Modified NACA 4-digit parameter p must satisfy 0 <= p <= 0.9."
        )
    if not 0.0 <= params.max_thickness <= NACA4_MAX_THICKNESS_LIMIT:
        raise ValueError(
            "Modified NACA 4-digit parameter max_thickness must satisfy "
            "0 <= max_thickness <= 0.4."
        )
    if params.m == 0.0 and params.p != 0.0:  # noqa: RUF069
        raise ValueError(
            "Modified NACA 4-digit parameter p must be 0 when m is 0."
        )
    if params.m > 0.0 and params.p <= 0.0:
        raise ValueError(
            "Modified NACA 4-digit parameter p must be positive when m > 0."
        )
    if (
        not LEADING_EDGE_INDEX_MIN
        <= params.leading_edge_index
        < (LEADING_EDGE_INDEX_MAX)
    ):
        raise ValueError(
            "Modified NACA 4-digit leading_edge_index must satisfy "
            "1 <= leading_edge_index < 10."
        )
    if not (
        NACA4_MODIFIED_MIN_THICKNESS_LOCATION
        <= params.max_thickness_location
        < 1.0
    ):
        raise ValueError(
            "Modified NACA 4-digit max_thickness_location must satisfy "
            "0.1 <= max_thickness_location < 1.0."
        )


def _copy_naca4_modified_spec(
    spec: Naca4ModifiedAirfoilSpec,
) -> Naca4ModifiedAirfoilSpec:
    """
    Return a defensive copy of a modified NACA 4-digit spec.

    Parameters
    ----------
    spec : Naca4ModifiedAirfoilSpec
        Source specification to copy.

    Returns
    -------
    Naca4ModifiedAirfoilSpec
        Deep copy of ``spec``.
    """
    return deepcopy(spec)


def _designation_to_lmti(designation: str) -> FloatScalar:
    """
    Return the max-thickness-location index from a designation.

    Parameters
    ----------
    designation : str
        Modified NACA 4-digit designation string.

    Returns
    -------
    FloatScalar
        Max-thickness-location index extracted from ``designation``.

    Raises
    ------
    ValueError
        If the designation encodes an unsupported thickness-location index.
    """
    lmti = as_float_scalar(float(designation[6]))
    if not MAX_THICKNESS_LOCATION_MIN <= lmti < MAX_THICKNESS_LOCATION_MAX:
        raise ValueError(
            "Modified NACA 4-digit max thickness location must be 1-9."
        )
    return lmti


[docs] class Naca4ModifiedAirfoilClassic(OrthogonalAirfoil): """Classic modified NACA 4-digit airfoil built from a designation.""" def __init__(self, spec: Naca4ModifiedAirfoilSpec) -> None: """ Build a classic modified NACA 4-digit airfoil from its spec. Parameters ---------- spec : Naca4ModifiedAirfoilSpec Serialized specification containing a modified-series designation. Raises ------ ValueError If ``spec`` does not contain a valid designation-only definition. """ if spec.designation is None or spec.params is not None: raise ValueError( "Classic modified NACA 4-digit airfoils require " "designation-only specs." ) _validate_naca4_modified_designation(spec.designation) lmti = _designation_to_lmti(spec.designation) camber = Naca4DigitCamber( mci=as_float_scalar(float(spec.designation[0])), lci=as_float_scalar(float(spec.designation[1])), ) thickness = Naca45DigitModifiedThicknessClassic( mti=as_float_scalar(float(spec.designation[2:4])), lei=as_float_scalar(float(spec.designation[5])), lmti=lmti, ) super().__init__(camber=camber, thickness=thickness) self._spec = _copy_naca4_modified_spec(spec)
[docs] @classmethod def from_designation(cls, designation: str) -> Self: """ Build a classic modified NACA 4-digit airfoil from a designation. Parameters ---------- designation : str Modified NACA 4-digit designation such as ``"0003-64"``. Returns ------- Self Runtime airfoil built from ``designation``. Raises ------ ValueError If ``designation`` is not a valid modified NACA 4-digit code. """ return cls(Naca4ModifiedAirfoilSpec(designation=designation))
@property def spec(self) -> Naca4ModifiedAirfoilSpec: """ Return the source spec for this airfoil. Returns ------- Naca4ModifiedAirfoilSpec Defensive copy of the serialized source spec. """ return _copy_naca4_modified_spec(self._spec) @property def camber(self) -> Naca4DigitCamber: """ Return the concrete modified-series camber model. This property exposes the 4-digit camber-line object constructed from the stored modified-series definition. """ return cast(Naca4DigitCamber, self._camber) @property def thickness(self) -> Naca45DigitModifiedThicknessClassic: """ Return the concrete classic modified-series thickness model. This property exposes the modified 4/5-digit thickness relation constructed from the stored classic modified-series definition. """ return cast(Naca45DigitModifiedThicknessClassic, self._thickness)
[docs] def to_spec(self) -> Naca4ModifiedAirfoilSpec: """ Return the schema definition needed to recreate this airfoil. Returns ------- Naca4ModifiedAirfoilSpec Serialized source spec for this airfoil. """ return self.spec
[docs] class Naca4ModifiedAirfoilParams(OrthogonalAirfoil): """Parametric modified NACA 4-digit airfoil built from explicit params.""" def __init__(self, spec: Naca4ModifiedAirfoilSpec) -> None: """ Build a parametric modified NACA 4-digit airfoil from its spec. Parameters ---------- spec : Naca4ModifiedAirfoilSpec Serialized specification containing explicit modified-series parameters. Raises ------ ValueError If ``spec`` does not contain a valid params-only definition. """ if spec.designation is not None or spec.params is None: raise ValueError( "Parametric modified NACA 4-digit airfoils require " "params-only specs." ) _validate_naca4_modified_params(spec.params) camber = Naca4DigitCamber( mci=100.0 * spec.params.m, lci=10.0 * spec.params.p, ) thickness = Naca45DigitModifiedThicknessParams( mti=100.0 * spec.params.max_thickness, lei=spec.params.leading_edge_index, lmti=10.0 * spec.params.max_thickness_location, closed_te=spec.params.trailing_edge == "sharp", ) super().__init__(camber=camber, thickness=thickness) self._spec = _copy_naca4_modified_spec(spec) @property def camber(self) -> Naca4DigitCamber: """ Return the concrete modified-series camber model. This property exposes the 4-digit camber-line object constructed from the stored modified-series parameter definition. """ return cast(Naca4DigitCamber, self._camber) @property def thickness(self) -> Naca45DigitModifiedThicknessParams: """ Return the concrete parametric modified-series thickness model. This property exposes the modified 4/5-digit thickness relation constructed from the stored parametric modified-series definition. """ return cast(Naca45DigitModifiedThicknessParams, self._thickness)
[docs] @classmethod def from_params( cls, *, m: FloatScalar, p: FloatScalar, t: FloatScalar, leading_edge_index: FloatScalar, max_thickness_location: FloatScalar, trailing_edge: Literal["standard", "sharp"] = "standard", ) -> Self: """ Build a parametric modified NACA 4-digit airfoil from params. Parameters ---------- m : FloatScalar Maximum camber as a fraction of chord. p : FloatScalar Chordwise location of maximum camber as a fraction of chord. t : FloatScalar Maximum thickness as a fraction of chord. leading_edge_index : FloatScalar Modified-series leading-edge shape index. max_thickness_location : FloatScalar Chordwise location of maximum thickness as a fraction of chord. trailing_edge : {"standard", "sharp"}, default="standard" Trailing-edge closure model for the thickness distribution. Returns ------- Self Runtime airfoil built from the explicit parameters. Raises ------ ValueError If the parameter set falls outside the supported modified-series range. """ params = Naca4ModifiedAirfoilParamsSpec( m=m, p=p, max_thickness=t, leading_edge_index=leading_edge_index, max_thickness_location=max_thickness_location, trailing_edge=trailing_edge, ) return cls(Naca4ModifiedAirfoilSpec(params=params))
@property def spec(self) -> Naca4ModifiedAirfoilSpec: """ Return the source spec for this airfoil. Returns ------- Naca4ModifiedAirfoilSpec Defensive copy of the serialized source spec. """ return _copy_naca4_modified_spec(self._spec)
[docs] def to_spec(self) -> Naca4ModifiedAirfoilSpec: """ Return the schema definition needed to recreate this airfoil. Returns ------- Naca4ModifiedAirfoilSpec Serialized source spec for this airfoil. """ return self.spec