{ "cells": [ { "cell_type": "markdown", "id": "8793adf2-29c8-4434-9b19-88f2c3185d56", "metadata": {}, "source": [ "# Test your Python skills: Darcy's law\n", "\n", "In this exercise, you will write a Python function that uses **Darcy's law** to calculate the flow rate through a porous medium.\n", "\n", "## Problem description\n", "\n", "Darcy's law states that the flow rate ($Q$) through a porous medium is given by:\n", "\n", "$$ \n", "Q = k \\times A \\times \\left(\\frac{\\Delta h}{L}\\right)\n", "$$ \n", "\n", "Where:\n", "- **$k$** is the hydraulic conductivity (in meters per second, $m/s$).\n", "- **$A$** is the cross-sectional area (in square meters, $m^2$).\n", "- **$\\Delta h$** is the hydraulic head difference (in meters, $m$).\n", "- **$L$** is the length over which the head difference occurs (in meters, $m$).\n", "\n", "**Your Task:**\n", "\n", "1. **Write a function** `darcy_flow(k, A, delta_h, L)` that:\n", " - Accepts four parameters: `k`, `A`, `delta_h`, and `L`.\n", " - Calculates the hydraulic gradient ($\\frac{\\Delta h}{L}$).\n", " - Returns the flow rate ($Q$) using Darcy's law.\n", " \n", "2. **Test your function** by computing flow rates for a range of hydraulic head differences (`delta_h`) while keeping `k`, `A`, and `L` constant.\n", "\n", "3. **Plot the results:** Create a plot of flow rate ($Q$) versus hydraulic head difference ($\\Delta h$).\n", "\n", "## Helpful Hints\n", "\n", "- Use the `def` keyword to define your function.\n", "- The hydraulic gradient is calculated as `delta_h / L`.\n", "- You can use the `numpy` library's `linspace` to create an array of `delta_h` values.\n", "\n", "Let's get started!\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9067c4f1-928e-4ae7-aaf2-80edfb91ed55", "metadata": {}, "outputs": [], "source": [ "# Complete the function here\n", "def darcy_flow(k, A, ...): # Finalize introducing ALL PARAMETERS HERE\n", " \"\"\"\n", " Calculate the flow rate Q using Darcy's law.\n", "\n", " Parameters:\n", " k: Hydraulic conductivity (m/s)\n", " A: Cross-sectional area (m^2)\n", " delta_h: Hydraulic head difference (m)\n", " L: Length over which the head difference occurs (m)\n", "\n", " Returns:\n", " Q: Flow rate Q (m^3/s)\n", " \"\"\"\n", " \n", " Q = # THE EXPRESSION HERE\n", " \n", " return Q\n", "\n", "# You can test your function with a simple example:\n", "print(\"Test Function Output:\", darcy_flow(1e-5, 10, 5, 100))\n" ] }, { "cell_type": "markdown", "id": "3e6dfd9d-6aa2-43c1-9fbf-3ea026a11497", "metadata": {}, "source": [ "## Step 2: Compute flow rates for a range of hydraulic head differences\n", "\n", "Now, let’s use your function to calculate the flow rate \\( Q \\) for a range of hydraulic head differences (`delta_h`), while keeping the other parameters constant.\n", "\n", "For example, assume:\n", "- \\( k = 1 \\times 10^{-5} \\) m/s,\n", "- \\( A = 10 \\) m\\(^2\\),\n", "- \\( L = 100 \\) m.\n", "\n", "We'll vary `delta_h` from 0.1 m to 10 m.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "4886334a-fba3-401f-94f5-fafe7e749f14", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "# Given constant parameters\n", "k = 1e-5 # Hydraulic conductivity in m/s\n", "A = 10 # Cross-sectional area in m^2\n", "L = 100 # Length in m\n", "\n", "# Create an array of delta_h values from 0.1 m to 10 m (avoiding 0 to prevent any division issues)\n", "delta_h_values = np.linspace(0.1, 10, 50)\n", "\n", "# Calculate Q for each delta_h value using your darcy_flow function\n", "Q_values = []\n", "for dh in delta_h_values:\n", " Q_values.append(darcy_flow(k, A, dh, L))\n", "\n", "# Print a few computed values to check your results\n", "print(\"Sample computed Q values:\", Q_values[:5])\n" ] }, { "cell_type": "markdown", "id": "64c1ccd5-7cc0-4a1b-b74e-82d2d53fff67", "metadata": {}, "source": [ "## Step 3: Plot the results\n", "\n", "Finally, plot the flow rate \\( Q \\) versus the hydraulic head difference \\( \\Delta h \\) using `matplotlib`.\n", "\n", "This plot will help visualize how the flow rate increases as the hydraulic head difference increases.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "2e4f59df-1caa-4ea3-bcbb-2477c2ba1e9f", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "plt.figure(figsize=(8, 5))\n", "plt.plot(delta_h_values, Q_values, marker='o', linestyle='-', color='steelblue')\n", "plt.xlabel('Hydraulic head difference (Δh) [m]')\n", "plt.ylabel('Flow rate Q [m³/s]')\n", "plt.title(\"Darcy's law: flow rate vs. hydraulic head difference\")\n", "plt.grid(True)\n", "plt.show()\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.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }