Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| lehrkraefte:snr:informatik:glf4-23:waermeleitung [2024/05/01 09:07] – Olaf Schnürer | lehrkraefte:snr:informatik:glf4-23:waermeleitung [2024/05/15 14:52] (current) – [Wärmeleitungsgleichung simulieren] Olaf Schnürer | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ~~NOTOC~~ | ||
| + | |||
| + | Falls numpy etc. auf Schulrechnern nicht verfügbar: | ||
| + | |||
| + | https:// | ||
| + | |||
| + | ====== Wärmeleitungsgleichung simulieren ====== | ||
| + | |||
| + | Die Mathematik und Formeln für die Iteration sind hier noch nicht dokumentiert (bisher an Tafel erklärt). | ||
| + | Wärmeleitungsgleichung $u_{t}=\Delta u$. | ||
| + | |||
| + | ===== Numpy und Matplotlib: einige Befehle kennenlernen (und eventuell Bibliotheken installieren) ===== | ||
| + | |||
| + | <hidden Bitte Programm ausklappen> | ||
| + | <code python beispiel-fuer-die-verwendung-von-numpy-und-matplotlib.py> | ||
| + | import numpy as np | ||
| + | from matplotlib import pyplot as plt | ||
| + | |||
| + | # Zweidimensionale Tabelle (= zweidimensionales Array = zweidimensionale Matrix) mit Nullen als Einträgen erstellen. Beachte: | ||
| + | # - erster Index Zeilennummer (also y-Koordinate), | ||
| + | # - zweiter Index Spaltennummer (also x-Koordinate), | ||
| + | u = np.zeros((5, | ||
| + | |||
| + | # Setze die Einträge an den Positionen (0, 0) und ((0, 19) auf 80. | ||
| + | u[0, 0] = 80 | ||
| + | u[0, 9] = 80 | ||
| + | |||
| + | # Setze alle Einträge in der mittleren Zeile (Zeilennummer 2) auf 40. | ||
| + | for j in range(10): | ||
| + | u[2, j] = 40 | ||
| + | |||
| + | # Ausgabe der Matrix | ||
| + | print(u) | ||
| + | |||
| + | plt.title(f" | ||
| + | plt.xlabel(" | ||
| + | plt.ylabel(f" | ||
| + | plt.pcolormesh(u, | ||
| + | plt.colorbar() | ||
| + | plt.savefig(" | ||
| + | plt.show() | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Wärmeausbreitung (1-dimensional) in einem Stab simulieren ===== | ||
| + | |||
| + | {{: | ||
| + | <hidden Bitte Programm ausklappen> | ||
| + | |||
| + | <code python waermeleitung-1d-ort-zeit-diagramm-vorlage.py> | ||
| + | import numpy as np | ||
| + | from matplotlib import pyplot as plt | ||
| + | |||
| + | # Länge des Stabes | ||
| + | n = 100 | ||
| + | iterationen = 400 | ||
| + | |||
| + | # alpha = Temperaturleitfähigkeit, | ||
| + | alpha = 1 | ||
| + | delta_x = 1 | ||
| + | # Die folgende Wahl des Zeitschritts | ||
| + | delta_t = (delta_x ** 2)/(4 * alpha) | ||
| + | # Die folgende Konstante gamma hängt von delta_t ab und taucht dann in der Simulation auf. | ||
| + | gamma = (alpha * delta_t) / (delta_x ** 2) | ||
| + | |||
| + | # Zweidimensionale Tabelle (Array), erste Koordinate Iterationsnummer, | ||
| + | u = np.zeros((iterationen + 1, n)) | ||
| + | |||
| + | # Temperaturverteilung am Anfang | ||
| + | u[0, 1] = 80 | ||
| + | # Aufgabe: Eigene Temperaturverteilung am Anfang definieren! | ||
| + | |||
| + | |||
| + | # Randwerte: Temperaturen an den Enden des Stabes (vorgegeben für alle Zeiten) | ||
| + | for i in range(iterationen): | ||
| + | u[i, 0] = 0 | ||
| + | u[i, n - 1] = 0 | ||
| + | |||
| + | # Start der Simulation | ||
| + | for i in range(1, iterationen + 1): | ||
| + | for x in range(1, n - 1): | ||
| + | # Aufgabe: Die folgende Zeile so anpassen, dass die Temperaturentwicklung wie in der Aufgabenstellung erläutert verläuft. | ||
| + | u[i, x] = u[i - 1, x - 1] | ||
| + | |||
| + | # print(u) | ||
| + | # Energie-/ | ||
| + | # print([sum(z) for z in u]) | ||
| + | |||
| + | plt.title(f" | ||
| + | plt.xlabel(" | ||
| + | plt.ylabel(f" | ||
| + | plt.pcolormesh(u, | ||
| + | plt.colorbar() | ||
| + | plt.savefig(" | ||
| + | plt.show() | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ===== Beispiel zum Erstellen einer animierten gif-Datei (= kleiner Film) ===== | ||
| + | |||
| + | <hidden Bitte Programm ausklappen> | ||
| + | <code python beispiel-fuer-2d-gif-animation.py> | ||
| + | import numpy as np | ||
| + | from matplotlib import pyplot as plt | ||
| + | from matplotlib import colormaps | ||
| + | import matplotlib.animation as animation | ||
| + | from matplotlib.animation import FuncAnimation | ||
| + | |||
| + | # Grösse des quadratischen Feldes | ||
| + | n = 30 | ||
| + | # Anzahl der Iterationen | ||
| + | iterationen = 40 | ||
| + | |||
| + | # Dreidimensionale Tabelle (Array), erste Koordinate Iterationsnummer (oft = Zeit), zweite Koordinate Zeile, dritte Koordinate Spalte. | ||
| + | u = np.zeros((iterationen + 1, n, n)) | ||
| + | |||
| + | # Temperaturverteilung am Anfang | ||
| + | u[0, 5, 9] = 100 | ||
| + | u[0, 5, 10] = 100 | ||
| + | u[0, 5, n - 10] = 100 | ||
| + | u[0, 5, n - 9] = 100 | ||
| + | |||
| + | # Start der Simulation, i für Iterationsnummer, | ||
| + | for i in range(1, iterationen + 1): | ||
| + | for z in range(1, n - 1): | ||
| + | for s in range(1, n - 1): | ||
| + | u[i, z, s] = (u[i - 1, z - 1, s - 1] + u[i - 1, z - 1, s + 1]) / 2 | ||
| + | |||
| + | def animiere(i): | ||
| + | print(i) | ||
| + | plt.clf() | ||
| + | plt.title(f" | ||
| + | plt.xlabel(" | ||
| + | plt.ylabel(" | ||
| + | plt.pcolormesh(u[i, | ||
| + | plt.colorbar() | ||
| + | # plt.show() | ||
| + | |||
| + | anim = animation.FuncAnimation(plt.figure(), | ||
| + | anim.save(" | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ===== Wärmeausbreitung 2-dimensional, | ||
| + | |||
| + | Zum Spass ist am Anfang noch eine Wärmeblase mitten im Raum... | ||
| + | |||
| + | {{: | ||
| + | |||
| + | <hidden Bitte Programm ausklappen> | ||
| + | |||
| + | <code python waermeleitung-2d-gif-animation-vorlage.py> | ||
| + | import numpy as np | ||
| + | from matplotlib import pyplot as plt | ||
| + | from matplotlib import colormaps | ||
| + | import matplotlib.animation as animation | ||
| + | from matplotlib.animation import FuncAnimation | ||
| + | |||
| + | # Grösse des quadratischen Feldes | ||
| + | n = 30 | ||
| + | iterationen = 60 | ||
| + | |||
| + | # alpha = Temperaturleitfähigkeit, | ||
| + | alpha = 2 | ||
| + | delta_x = 1 | ||
| + | delta_t = (delta_x ** 2)/(4 * alpha) | ||
| + | gamma = (alpha * delta_t) / (delta_x ** 2) | ||
| + | |||
| + | # Dreidimensionale Tabelle (Array), erste Koordinate Iterationsnummer, | ||
| + | |||
| + | u = np.zeros((iterationen + 1, n, n)) | ||
| + | |||
| + | # Randwerte: Temperaturen am Rand des Quadrates (vorgegeben für alle Zeiten) | ||
| + | for i in range(iterationen): | ||
| + | for s in range(n): | ||
| + | u[i, 0, s] = 100 | ||
| + | # Aufgabe: interessantere Randwerte vereinbaren! Etwa Raum mit offenem Fenster und Heizungen an den Wänden. | ||
| + | |||
| + | # Anfangswerte | ||
| + | # Aufgabe: Wer mag, definiert interessante Anfangswerte. | ||
| + | u[0, n // 2, n // 2] = 100 | ||
| + | |||
| + | # Start der Simulation | ||
| + | for i in range(1, iterationen + 1): | ||
| + | for z in range(1, n - 1): | ||
| + | for s in range(1, n - 1): | ||
| + | # Aufgabe: Wärmeausbreitungsformel anpassen! | ||
| + | u[i, z, s] = u[i - 1, z - 1, s] | ||
| + | |||
| + | def animiere(i): | ||
| + | print(i) | ||
| + | plt.clf() | ||
| + | plt.title(f" | ||
| + | plt.xlabel(" | ||
| + | plt.ylabel(" | ||
| + | plt.pcolormesh(u[i, | ||
| + | plt.colorbar() | ||
| + | # plt.show() | ||
| + | |||
| + | anim = animation.FuncAnimation(plt.figure(), | ||
| + | anim.save(" | ||
| + | </ | ||
| + | </ | ||
| + | |||