{ "cells": [ { "cell_type": "markdown", "id": "0cffa192-e4d1-4208-b686-593549d7bce3", "metadata": {}, "source": [ "# Cartesian 1D: Solve a 1D diffusion equation \n", "\n", "with a fixed concentration at the left boundary and a closed boundary on the right side" ] }, { "cell_type": "code", "execution_count": 1, "id": "53a38ae4-912d-434b-becd-f6702bc45c55", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import pyfvtool as pf" ] }, { "cell_type": "code", "execution_count": 2, "id": "1d333940-74bc-4429-8614-cf17483fd5f5", "metadata": {}, "outputs": [], "source": [ "Nx = 20 # number of finite volume cells\n", "Lx = 1.0 # [m] length of the domain \n", "c_left = 1.0 # left boundary concentration\n", "c_init = 0.0 # initial concentration\n", "D_val = 1e-5 # diffusion coefficient (gas phase)\n", "t_simulation = 3600.0 # [s] simulation time\n", "dt = 60.0 # [s] time step" ] }, { "cell_type": "code", "execution_count": 3, "id": "5829a9f1-f86e-4ef7-ac31-1a113d485a7a", "metadata": {}, "outputs": [], "source": [ "m1 = pf.Grid1D(Nx, Lx) # mesh object" ] }, { "cell_type": "code", "execution_count": 4, "id": "3ab70b47-86e9-4df7-8f04-77230c62bcb9", "metadata": {}, "outputs": [], "source": [ "# create a cell variable with initial concentration\n", "c = pf.CellVariable(m1, c_init)" ] }, { "cell_type": "code", "execution_count": 5, "id": "7afe5456-8c75-44eb-9a3c-b48848374fe0", "metadata": {}, "outputs": [], "source": [ "# switch the left boundary to Dirichlet: fixed concentration\n", "c.BCs.left.a = 0.0\n", "c.BCs.left.b = 1.0\n", "c.BCs.left.c = c_left" ] }, { "cell_type": "code", "execution_count": 6, "id": "192dd928-f9ce-4d66-bf85-de947483668e", "metadata": {}, "outputs": [], "source": [ "# assign diffusivity to cell faces\n", "D_face = pf.FaceVariable(m1, D_val) # average value of diffusivity at the interfaces between cells" ] }, { "cell_type": "code", "execution_count": 7, "id": "62130474-0f52-4c8e-b83e-bfa9117bd8a8", "metadata": {}, "outputs": [], "source": [ "# time loop\n", "t = 0.0\n", "while (t" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pf.visualizeCells(c)\n", "plt.xlabel('x position / a.u.')\n", "plt.ylabel('concentration / a.u.');" ] } ], "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.12.9" } }, "nbformat": 4, "nbformat_minor": 5 }