{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Physical constants" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A couple of short examples of using `aeolus` physical contants submodule, which can serve as a convenient way to store a number of constants used in an atmospheric model." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from aeolus.const import init_const" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## General constants" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, generic (planet-independent constants) are available." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "gen = init_const()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, `gen` is a dataclass-like object containing a few constants. Each of the constants is stored with appropriate units as an `iris.cube.Cube`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example, in this example the container has the heat of vaporization of water (at 0 deg C)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", "\n", "\n", "\n", " \n", " \n", "
Water Heat Vaporization (m2 s-2)(scalar cube)
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = gen.water_heat_vaporization\n", "L" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "iris.cube.Cube" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(L)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array(2501000.), Unit('m2 s-2'))" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L.data, L.units" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Having constants stored as iris cubes is convenient for unit-aware operations." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Earth constants" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Along with `aeolus`, some basic constants of the Earth atmosphere are also provided. Note that all the \"generic\" constants are also added to this container." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "EarthConstants(earth_day [s], stefan_boltzmann [W m-2 K-4], water_heat_vaporization [m2 s-2], water_molecular_weight [kg mol-1], molar_gas_constant [J K-1 mol-1], boltzmann [m^2 kg s^-2 K^-1], avogadro [mol-1], gravity [m s-2], radius [m], day [s], solar_constant [W m-2], semi_major_axis [au], eccentricity [1], obliquity [degree], dry_air_spec_heat_press [m2 s-2 K-1], dry_air_molecular_weight [kg mol-1], condensible_density [kg m-3], condensible_heat_vaporization [m2 s-2], condensible_molecular_weight [kg mol-1], reference_surface_pressure [Pa])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "earth_const = init_const(\"earth\")\n", "earth_const" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "c_p = earth_const.dry_air_spec_heat_press" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is possible to derive the gas constant for dry air using the universal gas constant." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "r_d = gen.molar_gas_constant / earth_const.dry_air_molecular_weight\n", "r_d.rename(\"dry_air_gas_constant\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", "\n", "\n", "\n", " \n", " \n", "
Dry Air Gas Constant (m2.s-2.K-1)(scalar cube)
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r_d" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(287.05797807)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r_d.data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Extending constants container" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All constants shown above are stored in JSON files, so it is possible to create a custom JSON file and read constants from it." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "from pathlib import Path" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example, we have an `dummy.json` file in the `data/test_data/json` sub-directory (downloaded from the `exoclim/aeolus_data` repository). The file looks like this:\n", "\n", "```json\n", "[ \n", " { \n", " \"name\": \"my_constant\", \n", " \"units\": \"m s-1\", \n", " \"value\": 123 \n", " } \n", "] \n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then it can be loaded in the following way." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "ex_const = init_const(name=\"dummy\", directory=(Path.cwd().parent / \"tests\" / \"data\" / \"test_data\" / \"json\"))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DummyConstants(earth_day [s], stefan_boltzmann [W m-2 K-4], water_heat_vaporization [m2 s-2], water_molecular_weight [kg mol-1], molar_gas_constant [J K-1 mol-1], boltzmann [m^2 kg s^-2 K^-1], avogadro [mol-1], my_constant [m s-1])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ex_const" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case, the JSON file contains only one constant, `my_constant`, the rest are \"general\" constants." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", "\n", "\n", "\n", " \n", " \n", "
My Constant (m s-1)(scalar cube)
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ex_const.my_constant" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(123)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ex_const.my_constant.data" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:aeolus_test]", "language": "python", "name": "conda-env-aeolus_test-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.4" } }, "nbformat": 4, "nbformat_minor": 4 }