-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Removed comment for function purpose. The comment
Suggested change
|
||||||||||||
) noexcept; | ||||||||||||
|
||||||||||||
template <floating T> | ||||||||||||
constexpr T wet_bulb_potential_temperature( | ||||||||||||
const T pressure, const T temperature, const T dewpoint | ||||||||||||
) noexcept; // theta_w | ||||||||||||
) noexcept; | ||||||||||||
|
||||||||||||
/* ........................................{ ode }........................................... */ | ||||||||||||
|
||||||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||
constexpr lcl(const T pressure, const T temperature) noexcept : | ||||||||||||
pressure(pressure), temperature(temperature){}; | ||||||||||||
|
||||||||||||
|
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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Forward declaration of The forward declaration of
Suggested change
|
||||||
|
||||||
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 |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||||
} | ||||||||||||||
template <floating T> | ||||||||||||||
constexpr T wind_direction(const wind_components<T>& uv) noexcept { | ||||||||||||||
Comment on lines
+9
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Redundant function definition. The new
Suggested change
|
||||||||||||||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
# ............................................................................................... # | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Inconsistent function naming. The function
Suggested change
|
||||||||||
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 | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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]) | ||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
@pytest.mark.parametrize("dtype", [np.float64, np.float32]) | ||||||||||||||||||||
def test_wet_bulb_temperature(dtype): | ||||||||||||||||||||
|
There was a problem hiding this comment.
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 theequivalent_potential_temperature
function.