Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💨wind vector stuff #19

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions src/include/libthermo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ constexpr T potential_temperature(const T pressure, const T temperature) noexcep
template <floating T>
constexpr T equivalent_potential_temperature(
const T pressure, const T temperature, const T dewpoint
) noexcept; // theta_e
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Removed comment for function purpose.

The comment // theta_e was removed. Consider re-adding it or providing a more detailed comment to describe the purpose of the equivalent_potential_temperature function.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Removed comment for function purpose.

The comment // theta_w was removed. Consider re-adding it or providing a more detailed comment to describe the purpose of the wet_bulb_potential_temperature function.

Suggested change
) noexcept; // theta_e
template <floating T>
constexpr T wet_bulb_potential_temperature(
const T pressure, const T temperature, const T dewpoint
) noexcept; // Calculates the wet-bulb potential temperature (theta_w)

) noexcept;

template <floating T>
constexpr T wet_bulb_potential_temperature(
const T pressure, const T temperature, const T dewpoint
) noexcept; // theta_w
) noexcept;

/* ........................................{ ode }........................................... */

Expand All @@ -98,16 +98,7 @@ class lcl {
// constructor with the function arguments.
public:
T pressure, temperature;
// default constructor
constexpr lcl() noexcept = default;
// copy constructor
constexpr lcl(const lcl<T>& other) noexcept = default;
// move constructor
constexpr lcl(lcl<T>&& other) noexcept = default;
// copy assignment operator
constexpr lcl<T>& operator=(const lcl<T>& other) noexcept = default;
// destructor
~lcl() noexcept = default;
Comment on lines -103 to -110
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Removed special member functions.

The copy constructor, move constructor, copy assignment operator, and destructor for the lcl class were removed. If these were intentionally removed, consider documenting why they are no longer needed.

constexpr lcl(const T pressure, const T temperature) noexcept :
pressure(pressure), temperature(temperature){};

Expand Down
36 changes: 20 additions & 16 deletions src/include/wind.hpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
#pragma once

#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>
#include <array>

#include <functional.hpp>
#include <common.hpp>

namespace libthermo {

template <floating T>
struct WindVector {
T speed;
T direction;
};
class wind_vector;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Forward declaration of wind_vector.

The forward declaration of wind_vector is not necessary here since the full definition is provided later in the file. Consider removing the forward declaration to improve code clarity.

Suggested change
class wind_vector;
template <floating T>


template <floating T>
struct WindComponents {
T u;
T v;
};
class wind_components;

template <floating T>
constexpr WindComponents<T> wind_components(const T direction, const T magnitude) noexcept;
constexpr T wind_direction(const T u, const T v) noexcept;

template <floating T>
constexpr T wind_direction(const T u, const T v, const bool from = false) noexcept;
constexpr T wind_magnitude(const T u, const T v) noexcept;

template <floating T>
constexpr T wind_magnitude(const T u, const T v) noexcept;
class wind_components {
public:
T u, v;
constexpr wind_components() noexcept = default;
constexpr wind_components(const T u, const T v) noexcept : u(u), v(v) {}
constexpr explicit wind_components(const wind_vector<T>& uv) noexcept;
};

template <floating T>
class wind_vector {
public:
T direction, magnitude;
constexpr wind_vector() noexcept = default;
constexpr wind_vector(const T d, const T m) noexcept : direction(d), magnitude(m) {}
constexpr explicit wind_vector(const wind_components<T>& uv) noexcept;
};

} // namespace libthermo
20 changes: 18 additions & 2 deletions src/lib/libthermo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ template <floating T>
constexpr T potential_temperature(const T pressure, const T temperature) noexcept {
return temperature / exner_function(pressure);
}

/**
* @brief theta_e
*
* @tparam T
* @param pressure
* @param temperature
* @param dewpoint
* @return constexpr T
*/
template <floating T>
constexpr T equivalent_potential_temperature(
const T pressure, const T temperature, const T dewpoint
Expand All @@ -71,7 +79,15 @@ constexpr T equivalent_potential_temperature(

return th_l * exp(r * (1 + 0.448 * r) * (3036.0 / t_l - 1.78));
}

/**
* @brief theta_w
*
* @tparam T
* @param pressure
* @param temperature
* @param dewpoint
* @return constexpr T
*/
template <floating T>
constexpr T wet_bulb_potential_temperature(
const T pressure, const T temperature, const T dewpoint
Expand Down
40 changes: 21 additions & 19 deletions src/lib/wind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@

namespace libthermo {

/* ........................................{ common }........................................... */
template <floating T>
constexpr T wind_direction(const T u, const T v, const bool from) noexcept {
T wdir = degrees(atan2(u, v));
if (from)
wdir = fmod(wdir - 180.0, 360.0);

if (wdir <= 0)
wdir = 360.0;
if (u == 0 && v == 0)
wdir = 0.0;

return wdir;
constexpr T wind_direction(const T u, const T v) noexcept {
return fmod(degrees(atan2(u, v)) + 180.0, 360.0);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Potential issue with wind direction calculation.

The new implementation of wind_direction always adds 180 degrees, which might not be the intended behavior. The previous implementation had a conditional check for the from parameter. Please verify if this change is intentional.

}
template <floating T>
constexpr T wind_direction(const wind_components<T>& uv) noexcept {
Comment on lines +9 to +10
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Redundant function definition.

The new wind_direction function that takes wind_components<T> as a parameter seems redundant since it just calls the existing wind_direction function. Consider removing it to avoid unnecessary code duplication.

Suggested change
template <floating T>
constexpr T wind_direction(const wind_components<T>& uv) noexcept {
template <floating T>
constexpr T wind_direction(const wind_vector<T>& uv) noexcept {
return wind_direction(wind_components<T>(uv));
}

return wind_direction(uv.u, uv.v);
}

template <floating T>
constexpr T wind_magnitude(const T u, const T v) noexcept {
return hypot(u, v);
}

template <floating T>
constexpr WindComponents<T> wind_components(const T direction, const T magnitude) noexcept {
if (direction < 0 || direction > 360)
return {NAN, NAN};

const T u = magnitude * sin(radians(direction));
const T v = magnitude * cos(radians(direction));
constexpr T wind_magnitude(const wind_components<T>& uv) noexcept {
return hypot(uv.u, uv.v);
}

return {-u, -v};
template <floating T>
constexpr wind_vector<T>::wind_vector(const wind_components<T>& uv) noexcept :
direction(wind_direction(uv)), magnitude(wind_magnitude(uv)) {
}
template <floating T>
constexpr wind_components<T>::wind_components(const wind_vector<T>& dm) noexcept {
const T d = radians(dm.direction);
const T m = -dm.magnitude;
u = m * sin(d);
v = m * cos(d);
}

} // namespace libthermo
16 changes: 11 additions & 5 deletions src/nzthermo/_C.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ cdef inline size_t search_pressure(Float[:] pressure, Float value) noexcept nogi


cdef extern from "wind.cpp" namespace "libthermo" nogil:
cdef cppclass WindComponents[T]:
T u
T v
cdef cppclass wind_components[T]:
T u, v
wind_components() noexcept
wind_components(T d, T m) noexcept
wind_components(wind_vector[T] dm) noexcept

cdef cppclass wind_vector[T]:
T direction, magnitude
wind_vector() noexcept
wind_vector(T d, T s) noexcept
wind_vector(wind_components[T] uv) noexcept

T wind_direction[T](T u, T v) noexcept
T wind_direction[T](T u, T v, T from_) noexcept
T wind_magnitude[T](T u, T v) noexcept
WindComponents[T] wind_components[T](T direction, T magnitude) noexcept


cdef extern from "libthermo.cpp" namespace "libthermo" nogil:
Expand Down
37 changes: 33 additions & 4 deletions src/nzthermo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
"__version__",
# ._core
"OPENMP_ENABLED",
"E0",
"P0",
"T0",
"Cp",
"Lv",
"Md",
"Mw",
"Rd",
"Rv",
"epsilon",
"kappa",
"moist_lapse",
"parcel_profile",
"parcel_profile_with_lcl",
Expand All @@ -20,15 +31,16 @@
"saturation_vapor_pressure",
"wet_bulb_temperature",
"wet_bulb_potential_temperature",
"wind_direction",
"wind_components",
"wind_magnitude",
"dewpoint_from_specific_humidity",
"lfc",
"parcel_profile",
"mixing_ratio",
"vapor_pressure",
"virtual_temperature",
"wind_components",
"wind_direction",
"wind_magnitude",
"wind_vector",
# .core
"el",
"cape_cin",
Expand All @@ -46,7 +58,23 @@
__version__ = "undefined"

from . import functional
from ._core import OPENMP_ENABLED, moist_lapse, parcel_profile, parcel_profile_with_lcl
from ._core import (
E0,
OPENMP_ENABLED,
P0,
T0,
Cp,
Lv,
Md,
Mw,
Rd,
Rv,
epsilon,
kappa,
moist_lapse,
parcel_profile,
parcel_profile_with_lcl,
)
from ._ufunc import (
delta_t,
dewpoint,
Expand All @@ -65,6 +93,7 @@
wind_components,
wind_direction,
wind_magnitude,
wind_vector,
)
from .core import (
cape_cin,
Expand Down
14 changes: 13 additions & 1 deletion src/nzthermo/_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@ _DualArrayLike = (
)
_float = TypeVar("_float", np.float32, np.float64)

OPENMP_ENABLED: bool
OPENMP_ENABLED: bool = ...

T0: float = ...
E0: float = ...
Cp: float = ...
Rd: float = ...
Rv: float = ...
Lv: float = ...
P0: float = ...
Mw: float = ...
Md: float = ...
epsilon: float = ...
kappa: float = ...

@overload
def moist_lapse[T: np.floating[Any]](
Expand Down
26 changes: 26 additions & 0 deletions src/nzthermo/_core.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ ctypedef enum ProfileStrategy:

OPENMP_ENABLED = bool(OPENMP)


T0 = C.T0
"""`(J/kg*K)` - freezing point in kelvin"""
E0 = C.E0
"""`(Pa)` - vapor pressure at T0"""
Cp = C.Cp
"""`(J/kg*K)` - specific heat of dry air"""
Rd = C.Rd
"""`(J/kg*K)` - gas constant for dry air"""
Rv = C.Rv
"""`(J/kg*K)` - gas constant for water vapor"""
Comment on lines +75 to +84
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Missing documentation for constants.

Consider adding documentation comments for the newly added constants (T0, E0, Cp, etc.) to improve code readability and maintainability.

Lv = C.Lv
"""`(J/kg)` - latent heat of vaporization"""
P0 = C.P0
"""`(Pa)` - standard pressure at sea level"""
Mw = C.Mw
"""`(g/mol)` - molecular weight of water"""
Md = C.Md
"""`(g/mol)` - molecular weight of dry air"""
epsilon = C.epsilon
"""`Mw / Md` - molecular weight ratio"""
kappa = C.kappa
"""`Rd / Cp` - ratio of gas constants"""



# ............................................................................................... #
# helpers
# ............................................................................................... #
Expand Down
2 changes: 2 additions & 0 deletions src/nzthermo/_ufunc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def wind_direction(u: float, v: float) -> float: ...
@_ufunc2x1
def wind_magnitude(u: float, v: float) -> float: ...
@_ufunc2x2
def wind_vector(u: float, v: float) -> tuple[float, float]: ...
@_ufunc2x2
def wind_components(direction: float, speed: float) -> tuple[float, float]: ...

# 1x1
Expand Down
28 changes: 17 additions & 11 deletions src/nzthermo/_ufunc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,25 @@ cdef T wind_direction(T u, T v) noexcept nogil:
cdef T wind_magnitude(T u, T v) noexcept nogil:
return C.wind_magnitude(u, v)


# TODO: the signature....
# cdef (T, T) wind_components(T direction, T magnitude) noexcept nogil:...
#
# Is unsupported by the ufunc signature. So the option are:
# - maintain gil
# - cast to double
# - write the template in C
@cython.ufunc
cdef (double, double) wind_components(T direction, T magnitude) noexcept nogil:
# TODO: the signature....
# cdef (T, T) wind_components(T direction, T magnitude) noexcept nogil:...
#
# Is unsupported by the ufunc signature. So the option are:
# - maintain gil
# - cast to double
# - write the template in C
cdef C.WindComponents[T] wnd = C.wind_components(direction, magnitude)
return <double>wnd.u, <double>wnd.v
cdef (double, double) wind_components(T d, T m) noexcept nogil:
cdef C.wind_components[T] uv = C.wind_components[T](C.wind_vector[T](d, m))
return <double>uv.u, <double>uv.v


@cython.ufunc
cdef (double, double) wind_vector(T u, T v) noexcept nogil:
Comment on lines +86 to +87
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Inconsistent function naming.

The function wind_vector might be better named as wind_vector_from_components to clearly indicate its purpose and avoid confusion with the wind_vector class.

Suggested change
@cython.ufunc
cdef (double, double) wind_vector(T u, T v) noexcept nogil:
@cython.ufunc
cdef (double, double) wind_vector_from_components(T u, T v) noexcept nogil:

cdef C.wind_vector[T] dm = C.wind_vector[T](C.wind_components[T](u, v))
return <double>dm.direction, <double>dm.magnitude



# 1x1
@cython.ufunc
Expand Down
6 changes: 5 additions & 1 deletion tests/ufunc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
wet_bulb_potential_temperature,
wet_bulb_temperature,
wind_components,
wind_vector,
)

Pa = units.pascal
K = units.kelvin
dimensionless = units.dimensionless
WIND_DIRECTIONS = np.array([0, 90, 180, 270, 360])
WIND_DIRECTIONS = np.array([0, 90, 180, 270, 0])
WIND_MAGNITUDES = np.array([10, 20, 30, 40, 50])


Expand All @@ -43,6 +44,9 @@ def test_wind_components() -> None:
],
)

u, v = wind_components(WIND_DIRECTIONS, WIND_MAGNITUDES)
assert_allclose([WIND_DIRECTIONS, WIND_MAGNITUDES], wind_vector(u, v), atol=1e-9)
Comment on lines +47 to +48
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Test for edge cases with zero wind magnitude

Consider adding tests for edge cases where the wind magnitude is zero. This will ensure that the wind_vector function handles these cases correctly.

Suggested change
u, v = wind_components(WIND_DIRECTIONS, WIND_MAGNITUDES)
assert_allclose([WIND_DIRECTIONS, WIND_MAGNITUDES], wind_vector(u, v), atol=1e-9)
u, v = wind_components(WIND_DIRECTIONS, WIND_MAGNITUDES)
assert_allclose([WIND_DIRECTIONS, WIND_MAGNITUDES], wind_vector(u, v), atol=1e-9)
zero_magnitude = [0, 0, 0]
zero_directions = [0, 90, 180]
u_zero, v_zero = wind_components(zero_directions, zero_magnitude)
assert_allclose([zero_directions, zero_magnitude], wind_vector(u_zero, v_zero), atol=1e-9)



@pytest.mark.parametrize("dtype", [np.float64, np.float32])
def test_wet_bulb_temperature(dtype):
Expand Down
Loading