Skip to content

Instantly share code, notes, and snippets.

@tonyfast
Last active January 10, 2024 21:04
Show Gist options
  • Save tonyfast/90c41d4994f75c594db804aeba56fc26 to your computer and use it in GitHub Desktop.
Save tonyfast/90c41d4994f75c594db804aeba56fc26 to your computer and use it in GitHub Desktop.
a notebook with revised literate pidgy cells for the first part of thermodyamics course
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "06b0aa9f-83e1-4ac0-a602-e917e406431d",
"metadata": {},
"source": [
"## First and second laws of thermodynamics\n",
"\n",
"A system typically consists of many chemical components. The first law\n",
"of thermodynamics states that the exchanges of heat, work, and\n",
"individual components with its surroundings must obey the law of\n",
"conversation of energy. In the domain of materials science and\n",
"engineering, the energy of interest is at the atomic and molecular\n",
"levels. The energy at the higher and lower levels such as nuclear energy\n",
"and kinetic and potential energies of a rigid body are usually excluded\n",
"from the discussion of thermodynamics of materials."
]
},
{
"cell_type": "markdown",
"id": "de031a0f-511a-437f-a3b7-c8fe7f71f699",
"metadata": {},
"source": [
"marquee of open system images\n",
"\n",
"marquee of vclosed system images"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "2059bc6a-4b8f-4819-beb2-b6b8310c9473",
"metadata": {
"execution": {
"iopub.execute_input": "2024-01-10T21:02:57.659175Z",
"iopub.status.busy": "2024-01-10T21:02:57.659026Z",
"iopub.status.idle": "2024-01-10T21:02:57.663695Z",
"shell.execute_reply": "2024-01-10T21:02:57.663396Z",
"shell.execute_reply.started": "2024-01-10T21:02:57.659163Z"
},
"tags": []
},
"outputs": [
{
"data": {
"text/markdown": [
"<div hidden>\n",
"\n",
" \n",
" from typing import Annotated\n",
" from sympy import Symbol, Sum, Indexed\n",
" def Eq(*x):\n",
" # limit nesting. flat is better than nested.\n",
" from sympy import Eq\n",
" a, *rest = x\n",
" for b in rest:\n",
" a = Eq(a, b)\n",
" return a\n",
"\n",
" class Variable(Symbol):\n",
" def __class_getitem__(cls, object):\n",
" if not isinstance(object, tuple):\n",
" object = object, \n",
" return Annotated[Symbol, *object]\n",
"\n",
"</div>"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
" \n",
" from typing import Annotated\n",
" from sympy import Symbol, Sum, Indexed\n",
" def Eq(*x):\n",
" # limit nesting. flat is better than nested.\n",
" from sympy import Eq\n",
" a, *rest = x\n",
" for b in rest:\n",
" a = Eq(a, b)\n",
" return a\n",
"\n",
" class Variable(Symbol):\n",
" def __class_getitem__(cls, object):\n",
" if not isinstance(object, tuple):\n",
" object = object, \n",
" return Annotated[Symbol, *object]"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "ede303c9-2e57-47e7-ab63-f4fd2dc7008e",
"metadata": {
"execution": {
"iopub.execute_input": "2024-01-10T21:03:02.461459Z",
"iopub.status.busy": "2024-01-10T21:03:02.461208Z",
"iopub.status.idle": "2024-01-10T21:03:02.467590Z",
"shell.execute_reply": "2024-01-10T21:03:02.467251Z",
"shell.execute_reply.started": "2024-01-10T21:03:02.461440Z"
},
"tags": []
},
"outputs": [
{
"data": {
"text/markdown": [
" dQ: Variable[\"Amount of heat\"] = Variable(\"dQ\")\n",
" dW: Variable[\"Amount of work\"] = Variable(\"dW\")\n",
" i: Variable[\"Component index\"] = Variable(\"i\")\n",
" dNi: Variable[\"Independent components\"] = Indexed(Variable(\"dN\"), i)"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
" dQ: Variable[\"Amount of heat\"] = Variable(\"dQ\")\n",
" dW: Variable[\"Amount of work\"] = Variable(\"dW\")\n",
" i: Variable[\"Component index\"] = Variable(\"i\")\n",
" dNi: Variable[\"Independent components\"] = Indexed(Variable(\"dN\"), i)"
]
},
{
"cell_type": "markdown",
"id": "96a6add5-7e96-41c6-86f4-a107a778a5bd",
"metadata": {
"execution": {
"iopub.execute_input": "2024-01-10T21:03:02.880412Z",
"iopub.status.busy": "2024-01-10T21:03:02.880229Z",
"iopub.status.idle": "2024-01-10T21:03:02.885327Z",
"shell.execute_reply": "2024-01-10T21:03:02.885034Z",
"shell.execute_reply.started": "2024-01-10T21:03:02.880399Z"
},
"tags": []
},
"source": [
"## original\n",
"\n",
"Let us consider a system receiving an amount of heat, *dQ*, and an\n",
"amount of work, *dW*, and an amount of each independent component *i*,\n",
"*dN<sub>i</sub>*, from the surroundings. Such a system is called an open\n",
"system in contrast to a closed system when *dN<sub>i</sub>*=0 for all\n",
"components, i.e. no exchange of mass between the system and the\n",
"surrounding. Other types of systems commonly defined in thermodynamics\n",
"include adiabatic systems without exchange of heat, i.e. *dQ*=0, and\n",
"isolated systems without exchange of any, i.e. *dQ*= *dW*=\n",
"*dN<sub>i</sub>*=0."
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "f3feb8d8-2663-4d85-b45f-3e10c9064722",
"metadata": {
"execution": {
"iopub.execute_input": "2024-01-10T21:03:19.147312Z",
"iopub.status.busy": "2024-01-10T21:03:19.147141Z",
"iopub.status.idle": "2024-01-10T21:03:19.154198Z",
"shell.execute_reply": "2024-01-10T21:03:19.153873Z",
"shell.execute_reply.started": "2024-01-10T21:03:19.147299Z"
},
"tags": []
},
"outputs": [
{
"data": {
"text/markdown": [
"Let us consider a system receiving an amount of heat, $\\displaystyle dQ$, and an\n",
"amount of work, $\\displaystyle dQ$, and an amount of each independent component *i*,\n",
"$\\displaystyle {dN}_{i}$, from the surroundings. Such a system is called an open\n",
"system in contrast to a closed system when $\\displaystyle {dN}_{i} = 0$ for all\n",
"components, i.e. no exchange of mass between the system and the\n",
"surrounding. Other types of systems commonly defined in thermodynamics\n",
"include adiabatic systems without exchange of heat, i.e. *dQ*=0, and\n",
"isolated systems without exchange of any, i.e. $\\displaystyle \\text{False}$.\n",
"\n",
"<!--\n",
"\n",
" isolated_system = Eq(dQ, Symbol(\"0\"), dW, dNᵢ)\n",
" \n",
"-->"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"Let us consider a system receiving an amount of heat, {{dQ}}, and an\n",
"amount of work, {{dQ}}, and an amount of each independent component *i*,\n",
"{{dNi}}, from the surroundings. Such a system is called an open\n",
"system in contrast to a closed system when {{Eq(dNi, Symbol(\"0\"))}} for all\n",
"components, i.e. no exchange of mass between the system and the\n",
"surrounding. Other types of systems commonly defined in thermodynamics\n",
"include adiabatic systems without exchange of heat, i.e. *dQ*=0, and\n",
"isolated systems without exchange of any, i.e. {{isolated_system}}.\n",
"\n",
"<!--\n",
"\n",
" isolated_system = Eq(dQ, Symbol(\"0\"), dW, dNᵢ)\n",
" \n",
"-->"
]
},
{
"cell_type": "markdown",
"id": "5990dfdc-c463-4144-9dda-bea2508d6d86",
"metadata": {
"execution": {
"iopub.execute_input": "2024-01-10T20:46:33.834999Z",
"iopub.status.busy": "2024-01-10T20:46:33.834777Z",
"iopub.status.idle": "2024-01-10T20:46:33.854618Z",
"shell.execute_reply": "2024-01-10T20:46:33.854205Z",
"shell.execute_reply.started": "2024-01-10T20:46:33.834979Z"
},
"tags": []
},
"source": [
"## original\n",
"\n",
"The corresponding change of energy in the system, i.e. the internal\n",
"energy change, *dU*, is formulated in terms of the first law of\n",
"thermodynamics as follows,\n",
"\n",
"Eq. ‑ $dU = dQ + dW + \\sum_{}^{}{H_{i}dN_{i}}$\n",
"\n",
"where $H_{i}$ is the unit energy of component *i* in the surroundings,\n",
"and the summation is for all components in the system, which can be\n",
"controlled independently from the surroundings, i.e. the independent\n",
"components of the system."
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "a1b78b48-18da-46b4-938a-74e67a74d9c8",
"metadata": {
"execution": {
"iopub.execute_input": "2024-01-10T21:03:19.929867Z",
"iopub.status.busy": "2024-01-10T21:03:19.928903Z",
"iopub.status.idle": "2024-01-10T21:03:19.937947Z",
"shell.execute_reply": "2024-01-10T21:03:19.937494Z",
"shell.execute_reply.started": "2024-01-10T21:03:19.929805Z"
},
"tags": []
},
"outputs": [
{
"data": {
"text/markdown": [
" dU: Variable[\"Change in Energy\"] = Variable(\"dU\")\n",
" Hᵢ: Variable[\"Unit of Energy\"] = Indexed(Variable(\"H\"), i)\n",
" N: Variable[\"Components\"] = Variable(\"N\")"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
" dU: Variable[\"Change in Energy\"] = Variable(\"dU\")\n",
" Hᵢ: Variable[\"Unit of Energy\"] = Indexed(Variable(\"H\"), i)\n",
" N: Variable[\"Components\"] = Variable(\"N\")"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "68d811f9-1c57-496a-9561-4d6fd31cc2dc",
"metadata": {
"execution": {
"iopub.execute_input": "2024-01-10T21:03:20.111813Z",
"iopub.status.busy": "2024-01-10T21:03:20.111503Z",
"iopub.status.idle": "2024-01-10T21:03:20.118400Z",
"shell.execute_reply": "2024-01-10T21:03:20.118024Z",
"shell.execute_reply.started": "2024-01-10T21:03:20.111791Z"
},
"tags": []
},
"outputs": [
{
"data": {
"text/markdown": [
"The corresponding change of energy in the system, i.e. the internal\n",
"energy change, $\\displaystyle dU$, is formulated in terms of the first law of\n",
"thermodynamics as follows,\n",
"\n",
"Eq. ‑ $\\displaystyle dU = dQ + dW + \\sum_{i=0}^{N} {H}_{i} {dN}_{i}$\n",
"\n",
"where $\\displaystyle {H}_{i}$ is the unit energy of component $\\displaystyle i$ in the surroundings,\n",
"and the summation is for all components in the system, which can be\n",
"controlled independently from the surroundings, i.e. the independent\n",
"components of the system.\n",
"\n",
"<!-- \n",
"\n",
" first_law = Eq(dU, dQ + dW + Sum(Hi * dNi, (i, 0, N)))\n",
" \n",
"-->"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"The corresponding change of energy in the system, i.e. the internal\n",
"energy change, {{dU}}, is formulated in terms of the first law of\n",
"thermodynamics as follows,\n",
"\n",
"Eq. ‑ {{first_law}}\n",
"\n",
"where {{Hi}} is the unit energy of component {{i}} in the surroundings,\n",
"and the summation is for all components in the system, which can be\n",
"controlled independently from the surroundings, i.e. the independent\n",
"components of the system.\n",
"\n",
"<!-- \n",
"\n",
" first_law = Eq(dU, dQ + dW + Sum(Hi * dNi, (i, 0, N)))\n",
" \n",
"-->"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bf9b3815-e926-4800-a69e-7996bfc83472",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "4d0edf13-f375-41ea-84ae-52d68d4622e3",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "cb3b5b38-f77b-4788-ba4a-960b8087edca",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "804bd6e5-1eb4-4f60-a2e3-a99cee3da715",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "82da65a1-9a4c-4b0d-908a-ea19137ff4d9",
"metadata": {
"execution": {
"iopub.execute_input": "2024-01-10T20:46:33.834999Z",
"iopub.status.busy": "2024-01-10T20:46:33.834777Z",
"iopub.status.idle": "2024-01-10T20:46:33.854618Z",
"shell.execute_reply": "2024-01-10T20:46:33.854205Z",
"shell.execute_reply.started": "2024-01-10T20:46:33.834979Z"
},
"tags": []
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "(unicode error) 'unicodeescape' codec can't decode bytes in position 4987-4988: truncated \\xXX escape (1465435561.py, line 103)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[12], line 103\u001b[0;36m\u001b[0m\n\u001b[0;31m for it to take place spontaneously.\"\"\";\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m (unicode error) 'unicodeescape' codec can't decode bytes in position 4987-4988: truncated \\xXX escape\n"
]
}
],
"source": [
"It is self-evident that the left-hand side of refers to the change\n",
"inside the system, while its right-hand side is for the contributions\n",
"from the surroundings to the system. In principle, no matter how the\n",
"heat and mass are added, or how the work is done to the system, as far\n",
"as their summation is the same, the change of the internal energy will\n",
"be the same based on the first law of thermodynamics, indicating that\n",
"the system reaches the same state for a closed system. The internal\n",
"energy is thus a state function in a close system as it does not depend\n",
"on how the state is reached.\n",
"\n",
"On the other hand, for the purpose of easy mathematical treatment, a\n",
"reversible process can be considered for a closed system, in which the\n",
"initial state of the system can be restored reversibly without any\n",
"changes left to the surroundings. Therefore, the heat transferred and\n",
"the work done to the system are identical to the heat and work lost by\n",
"the surroundings and vice versa. The classic example of reversible\n",
"processes is the Carnot’s cycle, which is shown in . It consists of four\n",
"reversible processes for a closed system. The four reversible processes\n",
"are compression at constant temperature *T<sub>1</sub>* (isothermal,),\n",
"compression without heat exchange (adiabatic) ending at *T<sub>2</sub>*,\n",
"isothermal expansion at *T<sub>2</sub>*, and adiabatic expansion ending\n",
"at *T<sub>1</sub>*.\n",
"\n",
"Figure ‑: Schematics of the Carnot’s cycle\n",
"\n",
"The Carnot’s cycle involves a simple type of mechanical work, either\n",
"hydrostatic expansion or compression, with the work that the surrounding\n",
"does to the system represented by\n",
"\n",
"Eq. ‑ $dW = - PdV$\n",
"\n",
"with *P* being the external pressure that the surrounding exerts on the\n",
"system and *V* the volume of the system. It is now necessary to\n",
"differentiate the external and internal variables for further discussion\n",
"with the former representing variables in the surroundings and the\n",
"latter representing variables in the system. For the isothermal\n",
"processes in the Carnot’s cycle, the entropy change of the system, *dS*,\n",
"can be defined as the heat exchange divided by temperature\n",
"\n",
"Eq. ‑ $dS = \\frac{dQ}{T}$\n",
"\n",
"In addition to processes involving heat, work, and mass exchanges\n",
"between the system and the surroundings, there could be internal\n",
"processes taking place inside the system. As the system cannot do work\n",
"to itself, the criterion for whether an internal process can occur\n",
"spontaneously must be related to the heat exchange, which is related to\n",
"the entropy change as shown by .\n",
"\n",
"It is a known fact that heat can spontaneously transfer from a higher\n",
"temperature (*T<sub>2</sub>*) region to a lower temperature\n",
"(*T<sub>1</sub>*) region inside a system if the heat conduction is\n",
"allowed, and this process is irreversible because heat cannot be\n",
"conducted from a low temperature region to a high temperature region by\n",
"itself. indicates that for the same amount of heat change, the entropy\n",
"change at *T<sub>1</sub>* is higher than that at *T<sub>2</sub>*, and\n",
"the heat conduction thus results in a positive entropy change in the\n",
"system, i.e.\n",
"\n",
"Eq. ‑\n",
"$\\mathrm{\\Delta}S = - \\frac{dQ}{T_{2}} + \\frac{dQ}{T_{1}} = \\frac{dQ}{{T_{2}T}_{1}}\\left( {T_{2} - T}_{1} \\right) > 0$\n",
"\n",
"Consequently, the second law of thermodynamics is obtained, which states\n",
"that for an internal process to take place spontaneously or\n",
"irreversibly, this internal process (*ip*) must have a positive entropy\n",
"production, which can be written in a differential form as follows\n",
"\n",
"Eq. ‑ $d_{ip}S > 0$\n",
"\n",
"From the definition of entropy change shown by , the amount of heat\n",
"produced by this irreversible internal process can be calculated as\n",
"follows\n",
"\n",
"Eq. ‑ $d_{ip}Q = Td_{ip}S$\n",
"\n",
"Let us represent this internal process by *dξ* and define the driving\n",
"force for this internal process by *D*. The work done by this internal\n",
"process is thus *Ddξ*, which is released as heat, i.e.\n",
"\n",
"Eq. ‑ $Dd\\xi = d_{ip}Q = Td_{ip}S$\n",
"\n",
"An irreversible process thus must have a positive driving force in order\n",
"for it to take place spontaneously."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1af0ecd0-db49-4c30-9a4f-7edc55821563",
"metadata": {
"execution": {
"iopub.execute_input": "2024-01-10T20:44:34.080064Z",
"iopub.status.busy": "2024-01-10T20:44:34.079876Z",
"iopub.status.idle": "2024-01-10T20:44:34.102917Z",
"shell.execute_reply": "2024-01-10T20:44:34.102618Z",
"shell.execute_reply.started": "2024-01-10T20:44:34.080050Z"
},
"tags": []
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle dQ = 0 = dW = dN_{i}$"
],
"text/plain": [
"Eq(Eq(Eq(dQ, 0), dW), dN_i)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/markdown": [
" (isolated_system := Eq(dQ, Symbol(\"0\"), dW, dNᵢ))"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6cc692e-5d96-49a1-bf66-e419db7be9f9",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "pidgy [conda env:p311] *",
"language": "markdown",
"name": "conda-env-p311-pidgy"
},
"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.3"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment