|
162 | 162 | "metadata": {},
|
163 | 163 | "outputs": [],
|
164 | 164 | "source": [
|
| 165 | + "def spectral_representation(x0,uhat):\n", |
| 166 | + " u_fun = lambda y : np.real(np.sum(uhat*np.exp(1j*xi*(y+x0))))/len(uhat)\n", |
| 167 | + " u_fun = np.vectorize(u_fun)\n", |
| 168 | + " return u_fun\n", |
| 169 | + "\n", |
165 | 170 | "# Spatial grid\n",
|
166 | 171 | "m=64 # Number of grid points in space\n",
|
167 | 172 | "L = 2 * np.pi # Width of spatial domain\n",
|
|
185 | 190 | "\n",
|
186 | 191 | "# Store solutions in a list for plotting later\n",
|
187 | 192 | "frames = [u.copy()]\n",
|
| 193 | + "ftframes = [uhat0.copy()]\n", |
188 | 194 | "\n",
|
189 | 195 | "# Now we solve the problem\n",
|
190 | 196 | "for n in range(1,N+1):\n",
|
191 | 197 | " t = n*k\n",
|
192 | 198 | " uhat = np.exp(-(1.j*xi*a + epsilon*xi**2)*t) * uhat0\n",
|
193 | 199 | " u = np.real(np.fft.ifft(uhat))\n",
|
194 | 200 | " frames.append(u.copy())\n",
|
| 201 | + " ftframes.append(uhat.copy())\n", |
195 | 202 | " \n",
|
196 | 203 | "# Set up plotting\n",
|
197 | 204 | "fig = plt.figure(figsize=(9,4)); axes = fig.add_subplot(111)\n",
|
198 | 205 | "line, = axes.plot([],[],lw=3)\n",
|
199 | 206 | "axes.set_xlim((x[0],x[-1])); axes.set_ylim((0.,1.))\n",
|
200 | 207 | "plt.close()\n",
|
201 | 208 | "\n",
|
| 209 | + "x_fine = np.linspace(x[0],x[-1],1000)\n", |
| 210 | + "\n", |
202 | 211 | "def plot_frame(i):\n",
|
203 |
| - " line.set_data(x,frames[i])\n", |
| 212 | + " uhat = ftframes[i]\n", |
| 213 | + " u_spectral = spectral_representation(x[0],uhat)\n", |
| 214 | + " line.set_data(x_fine,u_spectral(x_fine));\n", |
| 215 | + " #line.set_data(x,frames[i])\n", |
204 | 216 | " axes.set_title('t='+str(i*k))\n",
|
205 | 217 | "\n",
|
206 | 218 | "# Animate the solution\n",
|
|
310 | 322 | " A = np.diag(a(x))\n",
|
311 | 323 | " M = -np.dot(A,np.dot(Finv,np.dot(D,F)))\n",
|
312 | 324 | " lamda = np.linalg.eigvals(M)\n",
|
| 325 | + " print(np.max(np.abs(lamda)))\n", |
313 | 326 | " fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8,4),\n",
|
314 | 327 | " gridspec_kw={'width_ratios': [3, 1]})\n",
|
315 | 328 | " ax1.plot(x,a(x)); ax1.set_xlim(x[0],x[-1])\n",
|
|
338 | 351 | "outputs": [],
|
339 | 352 | "source": [
|
340 | 353 | "a = lambda x : 2 + np.sin(x)\n",
|
341 |
| - "plot_spectrum(a)" |
| 354 | + "plot_spectrum(a,m=64)" |
342 | 355 | ]
|
343 | 356 | },
|
344 | 357 | {
|
|
384 | 397 | "D = np.diag(1.j*xi)\n",
|
385 | 398 | "x = np.arange(-m/2,m/2)*(L/m)\n",
|
386 | 399 | "A = np.diag(a(x))\n",
|
387 |
| - "M = -np.dot(A,np.dot(Finv,np.dot(D,F)))\n", |
| 400 | + "#M = -np.dot(A,np.dot(Finv,np.dot(D,F)))\n", |
| 401 | + "M = -A@Finv@D@F\n", |
388 | 402 | "\n",
|
389 | 403 | "# Initial data\n",
|
390 | 404 | "u = np.sin(2*x)**2 * (x<-L/4)\n",
|
391 | 405 | "dx = x[1]-x[0]\n",
|
392 |
| - "dt = 2.0/m/np.max(np.abs(a(x)))/2.\n", |
| 406 | + "dt = 2.0/m/np.max(np.abs(a(x)))\n", |
| 407 | + "#dt = 1./86.73416328005729 + 1e-4\n", |
393 | 408 | "T = 10.\n",
|
394 | 409 | "N = int(np.round(T/dt))\n",
|
395 | 410 | "\n",
|
|
404 | 419 | " t = n*dt\n",
|
405 | 420 | " u_old = u.copy()\n",
|
406 | 421 | " u = u_new.copy()\n",
|
407 |
| - " u_new = u_old + 2*dt*np.dot(M,u)\n", |
| 422 | + " u_new = np.real(u_old + 2*dt*np.dot(M,u))\n", |
408 | 423 | " if ((n % skip) == 0):\n",
|
409 | 424 | " frames.append(u_new.copy())\n",
|
410 | 425 | " \n",
|
|
493 | 508 | "T = 5.\n",
|
494 | 509 | "N = int(np.round(T/dt))\n",
|
495 | 510 | "\n",
|
496 |
| - "frames = [u.copy()]\n", |
| 511 | + "ftframes = [np.fft.fft(u)]\n", |
497 | 512 | "skip = N//100\n",
|
498 | 513 | "\n",
|
499 | 514 | "# Start with an explicit Euler step\n",
|
|
509 | 524 | " \n",
|
510 | 525 | " A = np.diag(u)\n",
|
511 | 526 | " M = -np.dot(A,np.dot(Finv,np.dot(D,F)))\n",
|
512 |
| - " u_new = u_old + 2*dt*np.dot(M,u)\n", |
| 527 | + " u_new = np.real(u_old + 2*dt*np.dot(M,u))\n", |
513 | 528 | " if ((n % skip) == 0):\n",
|
514 |
| - " frames.append(u_new.copy())\n", |
| 529 | + " ftframes.append(np.fft.fft(u_new))\n", |
515 | 530 | " \n",
|
516 | 531 | "# Set up plotting\n",
|
517 | 532 | "fig, ax1 = plt.subplots(1, 1, figsize=(8,4))\n",
|
|
521 | 536 | "plt.close()\n",
|
522 | 537 | "\n",
|
523 | 538 | "def plot_frame(i):\n",
|
524 |
| - " line1.set_data(x,frames[i])\n", |
| 539 | + " uhat = ftframes[i]\n", |
| 540 | + " u_spectral = spectral_representation(x[0],uhat)\n", |
| 541 | + " line1.set_data(x_fine,u_spectral(x_fine));\n", |
525 | 542 | " ax1.set_title('t='+str(i*skip*dt))\n",
|
526 | 543 | "\n",
|
527 | 544 | "# Animate the solution\n",
|
528 | 545 | "anim = matplotlib.animation.FuncAnimation(fig, plot_frame,\n",
|
529 |
| - " frames=len(frames),\n", |
| 546 | + " frames=len(ftframes),\n", |
530 | 547 | " interval=200)\n",
|
531 | 548 | "\n",
|
532 | 549 | "HTML(anim.to_jshtml())"
|
|
0 commit comments