Skip to content

Commit

Permalink
Merge pull request #22 from leaver2000/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
leaver2000 authored Jun 21, 2024
2 parents 15c1203 + d22d254 commit 8311b9c
Show file tree
Hide file tree
Showing 20 changed files with 770 additions and 247 deletions.
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ AlignConsecutiveMacros:
AlignEscapedNewlines: Left
AlignOperands: DontAlign
AlignTrailingComments:
Kind: Never # MARK: Adjustable
Kind: Always # MARK: Adjustable
OverEmptyLines: 0
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
Expand Down Expand Up @@ -196,7 +196,7 @@ RemoveBracesLLVM: false
RemoveSemicolon: false
RequiresClausePosition: OwnLine
RequiresExpressionIndentation: OuterScope
SeparateDefinitionBlocks: Leave
SeparateDefinitionBlocks: Always
ShortNamespaceLines: 1
SortIncludes: Never # MARK: Adjustable
SortJavaStaticImport: Before
Expand Down
22 changes: 2 additions & 20 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
# syntax=docker/dockerfile:1

# .................................................................................................
# FROM python:3.10.14 AS py310
# USER root
# WORKDIR /

# ENV PATH="/opt/venv/bin:$PATH"
# RUN python3 -m venv /opt/venv
# COPY tests/requirements.txt .
# RUN pip install --no-cache-dir -r requirements.txt

# COPY . .
# ENV NZTHERMO_BUILD_COVERAGE 1
# RUN pip install --no-cache-dir --no-deps --upgrade --target src/ .

# USER 1001

# .................................................................................................
FROM python:3.11.9 AS py311
USER root
Expand All @@ -27,15 +11,14 @@ COPY tests/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .
ENV NZTHERMO_BUILD_COVERAGE 1
ENV NZTHERMO_BUILD_COVERAGE=1
RUN pip install --no-cache-dir --no-deps --upgrade --target src/ . \
&& pytest tests

# numpy 2.0.0 testing
RUN pip install --no-cache-dir Pint==0.24 numpy==2.0.0 --upgrade \
&& pytest tests


USER 1001

# .................................................................................................
Expand All @@ -49,13 +32,12 @@ COPY tests/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .
ENV NZTHERMO_BUILD_COVERAGE 1
ENV NZTHERMO_BUILD_COVERAGE=1
RUN pip install --no-cache-dir --no-deps --upgrade --target src/ . \
&& pytest tests

# numpy 2.0.0 testing
RUN pip install --no-cache-dir Pint==0.24 numpy==2.0.0 --upgrade \
&& pytest tests


USER 1001
11 changes: 8 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ testpaths = ["tests/"]

[tool.pytest.ini_options]
markers = [
"regression",
"broadcasting",
"self: this is a flag to target a specifc test",
"ccl: test functions for convective condensation level",
"moist_lapse",
Expand All @@ -44,14 +46,16 @@ markers = [
"cape_el_top",
"cape_el_bottom",
"cape_lfc",
"most_unstable_cape_cin",
"most_unstable_parcel",
"cape_lfc_top",
"cape_lfc_bottom",
"mu_cape",
"parcel_profile",
"regression",
"broadcasting",
"mixed_layer",
"mixed_parcel",
"mixed_layer_cape_cin",
]

pythonpath = ["src"]

[tool.coverage.run]
Expand Down Expand Up @@ -116,3 +120,4 @@ venvPath = "."
venv = ".venv"
pythonVersion = "3.12"
reportOverlappingOverload = "none"
reportMissingModuleSource = "none"
39 changes: 10 additions & 29 deletions src/include/libthermo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,24 @@

namespace libthermo {
#define DEFAULT_STEP 1000.0 // `(Pa)` - default step for moist_lapse
#define DEFAULT_EPS 0.1 // default epsilon for lcl
#define DEFAULT_ITERS 5 // default number of iterations for lcl
#define DEFAULT_EPS 0.1 // default epsilon for lcl
#define DEFAULT_ITERS 5 // default number of iterations for lcl

/* ........................................{ const }........................................... */

static constexpr double T0 = 273.15; /* `(J/kg*K)` - freezing point in kelvin */
static constexpr double E0 = 611.21; // `(Pa)` - vapor pressure at T0
static constexpr double T0 = 273.15; /* `(J/kg*K)` - freezing point in kelvin */
static constexpr double E0 = 611.21; // `(Pa)` - vapor pressure at T0
static constexpr double Cp = 1004.6662184201462; // `(J/kg*K)` - specific heat of dry air
static constexpr double Rd = 287.04749097718457; // `(J/kg*K)` - gas constant for dry air
static constexpr double Rv = 461.52311572606084; // `(J/kg*K)` - gas constant for water vapor
static constexpr double Lv = 2501000.0; // `(J/kg)` - latent heat of vaporization
static constexpr double P0 = 100000.0; // `(Pa)` - standard pressure at sea level
static constexpr double Mw = 18.01528; // `(g/mol)` - molecular weight of water
static constexpr double Md = 28.96546; // `(g/mol)` - molecular weight of dry air
static constexpr double epsilon = Mw / Md; // `Mw / Md` - molecular weight ratio
static constexpr double kappa = Rd / Cp; // `Rd / Cp` - ratio of gas constants

/* ........................................{ helper }........................................... */
static constexpr double Lv = 2501000.0; // `(J/kg)` - latent heat of vaporization
static constexpr double P0 = 100000.0; // `(Pa)` - standard pressure at sea level
static constexpr double Mw = 18.01528; // `(g/mol)` - molecular weight of water
static constexpr double Md = 28.96546; // `(g/mol)` - molecular weight of dry air
static constexpr double epsilon = Mw / Md; // `Mw / Md` - molecular weight ratio
static constexpr double kappa = Rd / Cp; // `Rd / Cp` - ratio of gas constants

template <floating T>
constexpr size_t index_pressure(const T x[], const T value, const size_t size) noexcept;

/* ........................................{ struct }........................................... */

template <floating T>
constexpr T mixing_ratio(const T partial_press, const T total_press) noexcept;

Expand Down Expand Up @@ -72,15 +66,11 @@ constexpr T wet_bulb_potential_temperature(
const T pressure, const T temperature, const T dewpoint
) noexcept;

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

template <floating T>
constexpr T moist_lapse(
const T pressure, const T next_pressure, const T temperature, const T step = DEFAULT_STEP
) noexcept;

/* ........................................{ lcl }........................................... */

template <floating T>
constexpr T lcl_pressure(
const T pressure,
Expand All @@ -92,23 +82,18 @@ constexpr T lcl_pressure(

template <floating T>
class lcl {
// TODO: a base class can be made to avoid code duplication for any struct that
// may be used to produce pressure and temperature fields. Then just overload the
// constructor with the function arguments.
public:
T pressure, temperature;
constexpr lcl() noexcept = default;
constexpr lcl(const T pressure, const T temperature) noexcept :
pressure(pressure), temperature(temperature){};

constexpr lcl(
const T pressure,
const T temperature,
const T dewpoint,
const T eps = DEFAULT_EPS,
const size_t max_iters = DEFAULT_ITERS
) noexcept;

constexpr T wet_bulb_temperature(const T pressure, const T step = DEFAULT_STEP) noexcept;
constexpr size_t index(const T pressure[], const size_t size) noexcept;
};
Expand All @@ -123,11 +108,7 @@ constexpr T wet_bulb_temperature(
const size_t max_iters = DEFAULT_ITERS
) noexcept;

/* ........................................{ sharp }........................................... */

template <floating T>
constexpr T wobus(T temperature);

/* ........................................{ ecape }........................................... */

} // namespace libthermo
5 changes: 2 additions & 3 deletions src/include/wind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class wind_components;

template <floating T>
constexpr T wind_direction(const T u, const T v) noexcept;

template <floating T>
constexpr T wind_magnitude(const T u, const T v) noexcept;

Expand All @@ -23,7 +22,7 @@ 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 wind_components(const T u, const T v) noexcept : u(u), v(v){};
constexpr explicit wind_components(const wind_vector<T>& uv) noexcept;
};

Expand All @@ -32,7 +31,7 @@ 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 wind_vector(const T d, const T m) noexcept : direction(d), magnitude(m){};
constexpr explicit wind_vector(const wind_components<T>& uv) noexcept;
};

Expand Down
6 changes: 6 additions & 0 deletions src/lib/functional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ constexpr T linear_interpolate(
) noexcept {
return y0 + (x - x0) * (y1 - y0) / LIMIT_ZERO(x1 - x0);
}

/**
* @author Kelton Halbert - NWS Storm Prediction Center/OU-CIWRO
*
Expand Down Expand Up @@ -116,6 +117,7 @@ size_t search_sorted(const T x[], const T value, const size_t size, const bool i

return upper_bound(x, size, value, std::less_equal());
}

/**
* @brief The Heaviside step function, or the unit step function, usually denoted by H or θ, is a
* mathematical function that is zero for negative arguments and one for positive arguments.
Expand All @@ -136,6 +138,7 @@ constexpr T heaviside(const T x, const T h0) noexcept {

return 1.0;
}

/**
* @author Jason Leaver - USAF 557WW/1WXG
*
Expand Down Expand Up @@ -169,6 +172,7 @@ constexpr T rk2(Fn<T, T, T> fn, T x0, T x1, T y, T step /* 1000.0 (Pa) */) noexc

return y;
}

/**
* @author Jason Leaver - USAF 557WW/1WXG
*
Expand Down Expand Up @@ -212,6 +216,7 @@ constexpr T fixed_point(

return NAN;
}

/**
* @brief Interpolates a 1D function using a linear interpolation.
*
Expand All @@ -230,6 +235,7 @@ constexpr T interpolate_1d(const T x, const T xp[], const T fp[], const size_t s

return linear_interpolate(x, xp[i - 1], xp[i], fp[i - 1], fp[i]);
}

/**
* @author Jason Leaver - USAF 557WW/1WXG
*
Expand Down
5 changes: 2 additions & 3 deletions src/lib/libthermo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ constexpr size_t index_pressure(const T levels[], const T value, const size_t si
return idx;
}

// thermodynamic functions

template <floating T>
constexpr T mixing_ratio(const T partial_press, const T total_press) noexcept {
return epsilon * partial_press / (total_press - partial_press);
Expand Down Expand Up @@ -75,7 +73,6 @@ constexpr T dry_lapse(const T pressure, const T reference_pressure, const T temp
template <floating T>
constexpr T dewpoint(const T vapor_pressure) noexcept {
const T ln = log(vapor_pressure / E0);

return T0 + 243.5 * ln / (17.67 - ln);
}

Expand All @@ -93,6 +90,7 @@ template <floating T>
constexpr T potential_temperature(const T pressure, const T temperature) noexcept {
return temperature / exner_function(pressure);
}

/**
* @brief theta_e
*
Expand All @@ -114,6 +112,7 @@ constexpr T equivalent_potential_temperature(

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

/**
* @brief theta_w
*
Expand Down
2 changes: 2 additions & 0 deletions src/lib/wind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ template <floating T>
constexpr T wind_direction(const T u, const T v) noexcept {
return fmod(degrees(atan2(u, v)) + 180.0, 360.0);
}

template <floating T>
constexpr T wind_direction(const wind_components<T>& uv) noexcept {
return wind_direction(uv.u, uv.v);
Expand All @@ -25,6 +26,7 @@ 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);
Expand Down
2 changes: 2 additions & 0 deletions src/nzthermo/_C.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ cdef extern from "libthermo.cpp" namespace "libthermo" nogil:

# 1x1
T saturation_vapor_pressure[T](T temperature) noexcept
T exner_function[T](T pressure) noexcept
T exner_function[T](T pressure, T reference_pressure) noexcept # .. overload ..
# 2x1
T mixing_ratio[T](T pressure, T vapor_pressure) noexcept
T mixing_ratio_from_dewpoint[T](T pressure, T dewpoint) noexcept
Expand Down
4 changes: 4 additions & 0 deletions src/nzthermo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
"el",
"lfc",
"mixed_layer",
"mixed_layer_cape_cin",
"mixed_parcel",
"mixing_ratio",
"most_unstable_cape_cin",
"most_unstable_parcel",
Expand Down Expand Up @@ -104,6 +106,8 @@
el,
lfc,
mixed_layer,
mixed_layer_cape_cin,
mixed_parcel,
mixing_ratio,
most_unstable_cape_cin,
most_unstable_parcel,
Expand Down
Loading

0 comments on commit 8311b9c

Please sign in to comment.