{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Internal heat source example\nThis example creates and simulates a plate with a heat source in the middle and different boundary conditions on each\nside.\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/internal_heat_source.svg\"\n\nimport os\nimport numpy as np\n\nfrom pyrc import (\n    Node,\n    InternalHeatSource,\n    SolidBoundaryConditionCell,\n    Viewer,\n    connect_cells_with_resistors,\n    RCNetwork,\n    LinePlot,\n)\nfrom pyrc.core.materials import Copper"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create the plate as grid of cells\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "grid_resolution = 15\nplate_thickness = 0.003\nmaterial = Copper()\ngrid = Node.create_grid(\n    grid_size=(grid_resolution, grid_resolution, 1),\n    delta=(1, 1, plate_thickness),\n    material=material,\n    temperature=20,\n)\ncapacitors: list[Node] = grid.flatten().tolist()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Add heat source in a circle at the center\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "radius = 2.5\ncenter_x, center_y = (grid.shape[0] - 1) / 2, (grid.shape[1] - 1) / 2\nx, y = np.ogrid[: grid.shape[0], : grid.shape[1]]\nmask = (x - center_x) ** 2 + (y - center_y) ** 2 <= radius**2\ncells_in_circle = grid[mask, 0]\n\nfor cell in cells_in_circle:\n    cell.add_internal_heat_source(\n        InternalHeatSource(\n            node=cell,\n            specific_power_in_w_per_meter_squared=1000,\n            area_direction=np.array((0, 0, 1)),\n        )\n    )"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create boundary conditions and place them adjacent to cell grid\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "bc_x_negative = SolidBoundaryConditionCell(temperature=-10, position=(0, 0, 0), delta=(0.1, 1, plate_thickness))\ncapacitors[0].place_adjacent(bc_x_negative, \"-x\")\nbc_x_positive = SolidBoundaryConditionCell(temperature=-5, position=(0, 0, 0), delta=(0.1, 1, plate_thickness))\ncapacitors[-1].place_adjacent(bc_x_positive, \"x\")\nbc_y_negative = SolidBoundaryConditionCell(temperature=-2, position=(0, 0, 0), delta=(1, 0.1, plate_thickness))\ncapacitors[0].place_adjacent(bc_y_negative, \"-y\")\nbc_y_positive = SolidBoundaryConditionCell(temperature=3, position=(0, 0, 0), delta=(1, 0.1, plate_thickness))\ncapacitors[-1].place_adjacent(bc_y_positive, \"y\")\n\nboundaries = [bc_x_negative, bc_x_positive, bc_y_negative, bc_y_positive]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## View the network with VPython (add debugger breakpoint to view)\nThe coordinate system arrows are disabled because they are very big for this network.\nComment out these lines if you just want to run the code. But during network build up it is nice to see if you placed\neverything correct.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "viewer = Viewer(draw_coordinate_system=False)\nviewer.add_new_from_list(capacitors)\nviewer.add_new_from_list(boundaries)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Connect everything with resistors between all Cells\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "resistors = connect_cells_with_resistors([*capacitors, *boundaries])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create RCNetwork\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=boundaries)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Solve network.\nWe solve the network stationary (analytical).\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "network.solve_stationary()"
      ]
    },
    {
      "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 result looks like this (or at least quite similar; the picture might be from an earlier version):\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<img src=\"file://_static/examples/internal_heat_source.svg\" width=\"100%\">\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
}