Vlasov-Poisson-1D1V.ipynb 44.8 KB
 Pierre Navaro committed Nov 02, 2018 1 2 3 4 { "cells": [ { "cell_type": "code",  Pierre Navaro committed Nov 04, 2018 5  "execution_count": 1,  Pierre Navaro committed Nov 02, 2018 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36  "metadata": {}, "outputs": [], "source": [ "using Plots, LinearAlgebra, Statistics, BenchmarkTools\n", "using HermiteGF\n", "using SparseArrays" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Vlasov-Poisson equation\n", "We consider the dimensionless Vlasov-Poisson equation for one species\n", "with a neutralizing background.\n", "\n", "$$\n", "\\frac{\\partial f}{\\partial t}+ v\\cdot \\nabla_x f + E(t,x) \\cdot \\nabla_v f = 0, \\\\\n", "- \\Delta \\phi = 1 - \\rho, E = - \\nabla \\phi \\\\\n", "\\rho(t,x) = \\int f(t,x,v)dv.\n", "$$\n", "\n", "- [Vlasov Equation - Wikipedia](https://en.wikipedia.org/wiki/Vlasov_equation)" ] }, { "cell_type": "code",  Pierre Navaro committed Nov 04, 2018 37  "execution_count": 2,  Pierre Navaro committed Nov 02, 2018 38 39 40 41 42 43 44 45  "metadata": {}, "outputs": [ { "data": { "text/plain": [ "SparseMatrixCSC{Float64,Int64}" ] },  Pierre Navaro committed Nov 04, 2018 46  "execution_count": 2,  Pierre Navaro committed Nov 02, 2018 47 48 49 50 51 52 53  "metadata": {}, "output_type": "execute_result" } ], "source": [ "nx = 64\n", "matrix = spdiagm(-1 => ones(Float64,nx-2),\n",  Pierre Navaro committed Nov 03, 2018 54 55  " 0 => -2*ones(Float64,nx),\n", " 1 => ones(Float64,nx-2))\n",  Pierre Navaro committed Nov 02, 2018 56 57 58 59 60 61  "\n", "typeof(matrix)" ] }, { "cell_type": "code",  Pierre Navaro committed Nov 04, 2018 62  "execution_count": 3,  Pierre Navaro committed Nov 02, 2018 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79  "metadata": {}, "outputs": [], "source": [ "struct UniformMesh\n", " nx :: Int64\n", " dx :: Float64\n", " x :: Vector{Float64}\n", " function UniformMesh( xmin, xmax, nx)\n", " dx = (xmax - xmin)/ nx\n", " x = range(xmin, stop=xmax, length=nx+1)[1:end-1]\n", " new( nx, dx, x)\n", " end\n", "end" ] }, { "cell_type": "code",  Pierre Navaro committed Nov 04, 2018 80  "execution_count": 4,  Pierre Navaro committed Nov 02, 2018 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110  "metadata": {}, "outputs": [], "source": [ "struct ChebyshevMesh\n", " nx :: Int64\n", " x :: Vector{Float64}\n", " dx :: Vector{Float64}\n", " function ChebyshevMesh( nodes::HermiteGF.NodesType)\n", " dx =[ x1 - x0 for (x1,x0) in zip(nodes.xk[2:end],nodes[1:end-1]) ]\n", " x = nodes.xk\n", " new( nx, dx, x)\n", " end\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "f_i'' \\approx \\frac{2 \\Big[ f_{i+1}\n", "- \\big(1+\\frac{h_i}{h_{i-1}}\\big) f_i\n", "+\\frac{h_i}{h_{i-1}}f_{i-1} \n", " \\Big]}\n", "{ h_i h_{i-1} (1+\\frac{h_i}{h_{i-1}})}\n", "$$" ] }, { "cell_type": "code",  Pierre Navaro committed Nov 04, 2018 111  "execution_count": 6,  Pierre Navaro committed Nov 02, 2018 112 113 114  "metadata": {}, "outputs": [ {  Pierre Navaro committed Nov 03, 2018 115 116 117 118 119  "data": { "image/svg+xml": [ "\n", "\n" ] },  Pierre Navaro committed Nov 04, 2018 350  "execution_count": 6,  Pierre Navaro committed Nov 03, 2018 351 352  "metadata": {}, "output_type": "execute_result"  Pierre Navaro committed Nov 02, 2018 353 354 355  } ], "source": [  Pierre Navaro committed Nov 04, 2018 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377  "struct ChebyshevPoisson\n", " x :: Vector{Float64}\n", " D2 :: Array{Float64, 2}\n", " function ChebyshevPoisson( xmin, xmax, nx )\n", " @assert( isodd(nx) )\n", " N = nx-1\n", " x = zeros(Float64, N+1)\n", " x .= cos.(pi*(0:N)/N)\n", " x .= xmin .+ (0.5 .+ 0.5 * x) * (xmax - xmin)\n", " c = ones(N+1) \n", " c[1] = 2\n", " c[end] = 2\n", " c[2:2:end-1] = -c[2:2:end-1]\n", " dx = x .- transpose(x)\n", " dx .+= Matrix{Float64}(I, N+1,N+1)\n", " D = c ./ transpose(c) \n", " D = D ./ dx # off-diagonal entries\n", " D = D - Diagonal(vec(sum(D,dims=2)))\n", " D2 = zeros(Float64,(N-1,N-1))\n", " D2 .= -(D*D)[2:end-1,2:end-1]\n", " new( x, D2)\n", " end \n",  Pierre Navaro committed Nov 03, 2018 378  "end\n",  Pierre Navaro committed Nov 04, 2018 379 380 381 382 383 384 385 386 387 388 389 390 391  "\n", "function(p::ChebyshevPoisson)(f::Vector{Float64})\n", " @assert (length(f) == length(p.x))\n", " [f[1]; p.D2 \\ f[2:end-1]; f[end]]\n", "end\n", "\n", "xmin, xmax, nx = 0., 1., 41\n", "poisson = ChebyshevPoisson(xmin, xmax, nx)\n", "x = poisson.x\n", "f = sin.(2π * x) * 4π^2\n", "u = poisson(f)\n", "plot( x, sin.(2π * x) )\n", "scatter!( x, u)"  Pierre Navaro committed Nov 02, 2018 392 393 394 395  ] }, { "cell_type": "code",  Pierre Navaro committed Nov 04, 2018 396  "execution_count": 10,  Pierre Navaro committed Nov 02, 2018 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785  "metadata": {}, "outputs": [], "source": [ "struct Poisson\n", " \n", " nx :: Int64\n", " dx :: Float64\n", " Φ :: Array{Float64,1}\n", " matrix :: SparseMatrixCSC{Float64,Int64}\n", " \n", " function Poisson( meshx )\n", " nx = meshx.nx\n", " dx = meshx.dx\n", " Φ = zeros(Float64,nx)\n", " matrix = spdiagm(-1 => -ones(Float64,nx-2),\n", " 0 => +2*ones(Float64,nx),\n", " 1 => -ones(Float64,nx-2))\n", " new( nx, dx, Φ, matrix)\n", "\n", " end\n", " \n", "end" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "function (p::Poisson)( rho::Array{Float64,1} )\n", " p.Φ .= p.matrix \\ rho\n", " (circshift(p.Φ, 1) - circshift(p.Φ, -1)) ./ (2*p.dx)\n", "end " ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "compute_rho" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", "Compute charge density\n", "ρ(x,t) = ∫ f(x,v,t) dv\n", "\"\"\"\n", "function compute_rho(meshv::UniformMesh, \n", " f::Array{Complex{Float64},2})\n", " \n", " dv = meshv.dx\n", " rho = dv * vec(sum(real(f), dims=2))\n", " rho .- mean(rho)\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Landau Damping\n", "\n", "[Landau damping - Wikipedia](https://en.wikipedia.org/wiki/Landau_damping)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "landau (generic function with 1 method)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function landau(tf::Float64, nt::Int64)\n", " \n", " nx, nv = 128, 256\n", " xmin, xmax = 0.0, 4*pi\n", " vmin, vmax = -6., 6.\n", " meshx = UniformMesh(xmin, xmax, nx)\n", " meshv = UniformMesh(vmin, vmax, nv)\n", " x = meshx.x\n", " v = meshv.x\n", " dx = meshx.dx\n", " \n", " # Create Vlasov-Poisson simulation\n", " poisson = Poisson(meshx)\n", " \n", " eps, kx = 0.001, 0.5\n", " f = zeros(Complex{Float64},(nx,nv))\n", " f .= (1.0.+eps*cos.(kx*x))/sqrt(2π) * transpose(exp.(-0.5*v.^2))\n", "\n", " ρ = compute_rho( meshv, f)\n", " \n", " e = poisson( ρ )\n", " \n", " ## Set time domain\n", " #dt = tf / nt\n", " #\n", " ## Run simulation\n", " #ℰ = Float64[]\n", " #\n", " #for it in 1:nt\n", " # advection!(f, p, meshx, v, nv, 0.5*dt)\n", " # rho = compute_rho(meshv, f)\n", " # e = compute_e(meshx, rho)\n", " # push!(ℰ, 0.5*log(sum(e.*e)*dx))\n", " # transpose!(fᵗ, f)\n", " # advection!(fᵗ, p, meshv, e, nx, dt)\n", " # transpose!(f, fᵗ)\n", " # advection!(f, p, meshx, v, nv, 0.5*dt)\n", " #end\n", " # \n", " #ℰ\n", " meshx.x, e\n", "\n", "end" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nt = 1000\n", "tf = 100.0\n", "t = range(0.0, stop=tf, length=nt)\n", "x, e = landau(tf, nt)\n", "plot( x, e)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "ename": "MethodError", "evalue": "MethodError: no method matching adjoint(::Type{Poisson})\nClosest candidates are:\n adjoint(!Matched::Missing) at missing.jl:79\n adjoint(!Matched::Number) at number.jl:193\n adjoint(!Matched::Adjoint{#s177,#s176} where #s176<:Union{StaticArray{Tuple{N},T,1} where T where N, StaticArray{Tuple{N,M},T,2} where T where M where N} where #s177) at /Users/navaro/.julia/packages/StaticArrays/WmJnA/src/linalg.jl:78\n ...", "output_type": "error", "traceback": [ "MethodError: no method matching adjoint(::Type{Poisson})\nClosest candidates are:\n adjoint(!Matched::Missing) at missing.jl:79\n adjoint(!Matched::Number) at number.jl:193\n adjoint(!Matched::Adjoint{#s177,#s176} where #s176<:Union{StaticArray{Tuple{N},T,1} where T where N, StaticArray{Tuple{N,M},T,2} where T where M where N} where #s177) at /Users/navaro/.julia/packages/StaticArrays/WmJnA/src/linalg.jl:78\n ...", "", "Stacktrace:", " [1] \\(::Type, ::Array{Float64,1}) at ./operators.jl:536", " [2] (::Poisson)(::Array{Float64,1}) at ./In[5]:2", " [3] landau(::Float64, ::Int64) at ./In[7]:21", " [4] top-level scope at util.jl:156", " [5] top-level scope at In[9]:6" ] } ], "source": [ "using Profile\n", "\n", "nt = 1000\n", "tf = 100.0\n", "t = range(0.0, stop=tf, length=nt)\n", "@time nrj = landau(tf, nt)\n", "plot( t, nrj; label = \"E\")\n", "plot!(t, -0.1533*t.-5.50; label=\"-0.1533t.-5.5\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using ProfileView\n", "ProfileView.v" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.0.1", "language": "julia", "name": "julia-1.0" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.0.1" } }, "nbformat": 4, "nbformat_minor": 2 }