AryaRaeesi / heat-equation-numeric

0 stars 0 forks source link

Varmelikningen PDE TMA4121 #1

Open AryaRaeesi opened 7 months ago

AryaRaeesi commented 7 months ago

Video og kode

AryaRaeesi commented 7 months ago

https://github.com/AryaRaeesi/heat-equation-numeric/assets/78743888/9dd80c0f-7a95-4445-8671-f410f6cda4c6

AryaRaeesi commented 7 months ago

`#%%

import scipy as sp import time from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt import numpy as np import mpl_toolkits.mplot3d.axes3d as p3 import matplotlib.animation as animation from matplotlib.animation import FFMpegWriter

dx=0.01
dy=0.01
a=0.5
tidssteg=500
t=0.

nX = int(1/dx) nY = int(1/dy)

dx2=dx2 dy2=dy2

dt = dx2dy2/( 2a*(dx2+dy2) )

ui = np.zeros([nX,nY]) u = np.zeros([nX,nY])

for i in range(nX): for j in range(nY): if ( ( (i*dx-0.5)2+(j*dy-0.5)*2 <= 0.1) & ((idx-0.5)2+(j*dy-0.5)*2>=.05) ): ui[i,j] = 1 def evolve_ts(u, ui): u[1:-1, 1:-1] = ui[1:-1, 1:-1] + adt( (ui[2:, 1:-1] - 2ui[1:-1, 1:-1] + ui[:-2, 1:-1])/dx2 + (ui[1:-1, 2:] - 2*ui[1:-1, 1:-1] + ui[1:-1, :-2])/dy2 )

def data_gen(framenumber, Z ,surf): global u global ui evolve_ts(u,ui) ui[:] = u[:] Z = ui

akser.clear()
plotset()
surf = akser.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
                   linewidth=0, antialiased=False, alpha=0.7)
return surf,

figur = plt.figure() akser = figur.add_subplot(111, projection='3d')

X = np.arange(0,1,dx) Y = np.arange(0,1,dy) X,Y= np.meshgrid(X,Y)

Z = ui

def plotset(): akser.set_xlim3d(0., 1.) akser.set_ylim3d(0., 1.) akser.set_zlim3d(-1.,1.) akser.set_autoscalez_on(False) akser.zakseris.set_major_locator(LinearLocator(10)) akser.zakseris.set_major_formatter(FormatStrFormatter('%.02f')) cset = akser.contour(X, Y, Z, zdir='x', offset=0. , cmap=cm.coolwarm) cset = akser.contour(X, Y, Z, zdir='y', offset=1. , cmap=cm.coolwarm) cset = akser.contour(X, Y, Z, zdir='z', offset=-1., cmap=cm.coolwarm)

plotset() surf = akser.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False, alpha=0.7)

figur.colorbar(surf, shrink=0.5, aspect=5)

ani = animation.FuncAnimation(figur, data_gen, fargs=(Z, surf),frames=1000, interval=30, blit=False) ani.save("2dDiffusion.mp4", bitrate=1024)

plt.show()`