{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {
    "vscode": {
     "languageId": "raw"
    }
   },
   "source": [
    "---\n",
    "title: \"Mass-volume relationships\"\n",
    "author: \"Andres Patrignani\", revised by Guo-Shiuan Lin \n",
    "source: Pynotes Agriscience\n",
    "date: \"1/5/2024\"\n",
    "keywords: [\"arithmetic operations\", \"simple examples\", \"calculator\"]\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Mass-volume relationships are fundamental variables for understanding and describing the soil composition in terms of the mass and volume of the main soil constituents (solids, water, air). Key variables include bulk density, which measures the mass of soil per unit volume, and porosity, indicating the volume fraction of soil that can be occupied by air and water. These relationships are crucial for assessing soil health, estimating soil fertility, and water storage. Understanding mass-volume relationships is vital for effective soil management, influencing practices like irrigation, tillage, and crop selection, ultimately impacting agricultural productivity.\n",
    "\n",
    "\n",
    "## Problem 1\n",
    "\n",
    "A cylindrical soil sample with a diameter of 5.00 cm and a height of 5.00 cm has a wet mass of 180.0 g. After oven-drying the soil sample at 105 degrees Celsius for 48 hours, the sample has a dry mass of 147.0 g. Based on the provided information, calculate:\n",
    "\n",
    "- volume of the soil sample:  $V_t = \\pi \\ r^2 \\ h$ <br>\n",
    "- mass of water in the sample:  $M_{water} = M_{wet \\ soil} - M_{dry \\ soil}$ <br>\n",
    "- bulk density:  $\\rho_b = M_{dry \\ soil}/V_t$ <br>\n",
    "- porosity:  $f = 1 - \\rho_b/\\rho_p$ <br>\n",
    "- gravimetric water content:  $\\theta_g = M_{water} / M_{dry \\ soil}$ <br>\n",
    "- volumetric water content:  $\\theta_v = V_{water} / V_{t}$ <br>\n",
    "- relative saturation:  $\\theta_rel = \\theta_v / f$ <br>\n",
    "- soil water storage expressed in mm and inches of water:  $S = \\theta_v * z$ <br>\n",
    "\n",
    "Throughout the exercise, assume a particle density of 2.65 g cm$^{-3}$ and that water has a density of 0.998 g cm$^{-3}$\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import modules\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Problem information\n",
    "sample_diameter = 5.00 # cm\n",
    "sample_height = 5.00 # cm\n",
    "wet_mass = 180.0 # grams\n",
    "dry_mass = 147.0 # grams\n",
    "density_water = 0.998 # g/cm^3 at 20 Celsius\n",
    "particle_density = 2.65 # g/cm^3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sample volume\n",
    "sample_volume = np.pi * (sample_diameter/2)**2 * __ # Question 1: type in one variable from above\n",
    "print(f'Volume of sample is: {sample_volume:.0f} cm^3')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Mass of water in the sample\n",
    "mass_water = __ # Question 2: calculate by using 2 variable from above\n",
    "print(f'Mass of water is: {mass_water:.0f} g')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Bulk density\n",
    "bulk_density = dry_mass/ __ # Question 3: day_mass divided by one variable we calculated before\n",
    "print('Bulk density of the sample is:', round(bulk_density,2), 'g/cm^3')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Porosity\n",
    "f = (1 - bulk_density/particle_density)\n",
    "print(f'Porosity of the sample is: {f:.2f}') # Second `f` is for floating-point # The correct answer should be 0.43\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Gravimetric soil mositure \n",
    "# Mass of water per unit mass of dry soil. Typically in g/g or kg/kg\n",
    "theta_g = mass_water / __ # Question 4: divided by what variable?\n",
    "print(f'Gravimetric water content is: {theta_g:.3f} g/g')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Volumetric soil mositure\n",
    "# Volume of water per unit volume of dry soil. Typically in cm^3/cm^3 or m^3/m^3\n",
    "volume_water = mass_water / density_water\n",
    "theta_v = volume_water / __ # Question 5: divided by what variable?\n",
    "print(f'Volumetric water content is: {theta_v:.3f} cm^3/cm^3')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Relative saturation\n",
    "rel_sat = theta_v/f\n",
    "print(f'Relative saturation is: {rel_sat:.2f}') # The answer should be 0.77\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Soil water storage\n",
    "storage_mm = sample_height*10* __ # Question 6: it should time what variable? (10 is to convert from cm to mm)\n",
    "storage_in = storage_mm/25.4 # 1 inch = 25.4 mm\n",
    "print(f'The soil water storage in mm is: {storage_mm:.1f} mm')\n",
    "print(f'The soil water storage in inches is: {storage_in:.3f} inches')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem 2\n",
    "\n",
    "How many liters of water are stored in the top 1 meter of the soil profile of a field that has an area of 64 hectares (about 160 acres)? Assume that average soil moisture of the field is the volumetric water content computed in the previous problem.\n"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "::: {.callout-note}\n",
    "Note\n",
    "Recall that the volume ratio is the same as the length ratio. This means that we can use the volumetric water content to represent the 'height of water' in the field. If you are having trouble visualizing this fact, grab a cylindrical container and fill it with 25% of its volume in water. Then measure the height of the water relative to the height of the container, it should also be 25%. Note that this will not work for conical shapes, so make sure to grab a uniform shape like a cylinder or a cube.\n",
    ":::\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Liters of water in a field\n",
    "field_area = 64*10_000 # 1 hectare = 10,000 m^2\n",
    "profile_length = 1 # meter\n",
    "equivalent_height_of_water = profile_length * __ # Question 7: it should time what variable? # unit: meter of water\n",
    "volume_of_water = field_area * equivalent_height_of_water # m^3 of water\n",
    "\n",
    "# Use the fact that 1 m^3 = 1,000 liters\n",
    "liters_of_water = volume_of_water * 1_000\n",
    "print(f'There are {round(liters_of_water)} liters of water')\n",
    "\n",
    "# Compare volume of water to an Olympic-size swimming pool (50 m x 25 m x 2 m)\n",
    "pool_volume = 50 * 25 * 2 # m^3\n",
    "print(f'This is equivalent to {round(liters_of_water/pool_volume)} olympic swimming pools!')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem 3\n",
    "\n",
    "Imagine that we want to change the soil texture of this field in the rootzone (top 1 m). How much sand, silt, or clay do we need to haul in to change the textural composition by say 1%? While different soil fractions usually have slightly different bulk densities, let's use the value from the previous problem. We are not looking for the exact number, we just want a ballpark idea. So, what is 1% of the total mass of the field considering the top 1 m of the soil profile?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Field area was already in converted from hectares to m^2\n",
    "field_volume = field_area * 1 # m^3\n",
    "\n",
    "# Since 1 Mg/m^3 = g/cm^3, we use the same value (1 Megagram = 1 metric ton)\n",
    "field_mass = field_volume * __ # Question 8: it should time what variable to converet volume to mass? \n",
    "\n",
    "one_percent_mass = field_mass/100\n",
    "print(f'1% of the entire field mass is {one_percent_mass:.1f} Mg')\n",
    "\n",
    "ultra_truck_maxload = 450 # Mg or metric tons\n",
    "number_of_truck_loads_required = one_percent_mass / ultra_truck_maxload\n",
    "print(f'It would require the load of {number_of_truck_loads_required:.0f} trucks')\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
