{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Wall heat conduction example\nThis example creates and simulates stationary heat conduction through a wall with two materials.\nIt utilizes the geometric Nodes, which enables visualizing the network using VPython or ParaView.\nThe case uses only one dimension (x-axis), so each y and z delta value is set to 1. Currently, sphinx-gallery (which\nis used to create the examples html pages) don't let you manually add figures to the example, so you need to run the\ncode yourself to see the result.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# sphinx_gallery_tags = [\"network\",\"solving\",\"build\", \"stationary\", \"geometric\"]\n# sphinx_gallery_thumbnail_path = \"_static/examples/wall_heat_conduction.svg\"\n\nimport os.path\n\nfrom pyrc import FluidBoundaryConditionCell, Node, connect_cells_with_resistors, RCNetwork, Cell\nfrom pyrc.core.materials import SandLimeBrick, EPS032\nfrom pyrc.visualization.plot import LinePlot"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create the boundary conditions\nOne warm, one cold.\nThe position of the cold boundary condition is adjusted later on.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "bc_warm = FluidBoundaryConditionCell(20, position=(-0.5, 0, 0), delta=(1, 1, 1), heat_transfer_coefficient=5)\nbc_cold = FluidBoundaryConditionCell(-4, position=(1, 0, 0), delta=(1, 1, 1), heat_transfer_coefficient=8)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create the wall out of two materials\nThe Wall is made of sand lime brick with 240 mm width and some heat insulation (ESP032) with 150 mm width. Each\nmaterial gets two nodes per 10 mm. Because the grid is returned in a three dimensional array, we flatten it before\nfilling it in the capacitors list.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "capacitors: list[Node] = [\n    *Node.create_grid(grid_size=(48, 1, 1), delta=(0.24, 1, 1), material=SandLimeBrick(), temperature=18.325).flat,\n    *Node.create_grid(grid_size=(30, 1, 1), delta=(0.15, 1, 1), material=EPS032(), temperature=5).flat,\n]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Place the nodes adjacent to each other\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "bc_warm.place_adjacent(capacitors[0], \"x\")\nfor i, capacitor in enumerate(capacitors[:-1]):\n    capacitor.place_adjacent(capacitors[i + 1], \"x\")\ncapacitors[-1].place_adjacent(bc_cold, \"x\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Add resistors between the nodes\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "resistors = connect_cells_with_resistors([*capacitors, bc_warm, bc_cold])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Add the objects (Capacitors, Resistors, BCs) to an RCNetwork object\nThe RCNetwork class is used to solve the RC network. It just needs all the building parts in its RCObject.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "network = RCNetwork(load_from_pickle=False, load_solution=False)\nnetwork.rc_objects.set_lists(capacitors=capacitors, resistors=resistors, boundaries=[bc_warm, bc_cold])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Solve network.\nWe solve the network stationary, which is done analytically. This is fast and mathematically \"perfect\".\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "network.solve_stationary()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Plot heat conduction over wall width\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "positions_in_cm = [c.position[0] * 100 for c in capacitors]\n\nplot = LinePlot(x=positions_in_cm, ys=network.rc_solution.y[-1], y_title=\"Temperature / \u00b0C\", x_title=\"Wall Width / cm\")\nplot.plot()\nplot.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Parse the result to show it in ParaView\nOpen the case in ParaView using the 00_simulation.pvd file\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "network.rc_solution.write_paraview_data(\n    folder=os.path.normpath(r\"/path/to/result/folder\"),\n    use_degrees_celsius=False,  # temperature is already in \u00b0C\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The resulting diagram looks like this:\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<img src=\"file://_static/examples/wall_heat_conduction_result.svg\" width=\"100%\">\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "It doesn't fit the boundary temperatures because of the assumed heat transfer coefficient between the ambient fluid\nand the wall. So this is actually correct.\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "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.13.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}