-- FreeFem++ v4.14 (mer. 06 mars 2024 16:59:04 CET - git v4.14-1-g2b2052ae) file : poisson_2d.edp Load: lg_fem lg_mesh lg_mesh3 eigenvalue 1 : // poisson_2d.edp 2 : // 3 : // Discussion: 4 : // 5 : // Solve the steady Poisson equation in the unit square. 6 : // 7 : // - uxx - uyy = f(x,y) 8 : // 9 : // with boundary condition 10 : // 11 : // u = 0 12 : // 13 : // Licensing: 14 : // 15 : // This code is distributed under the MIT license. 16 : // 17 : // Modified: 18 : // 19 : // 12 February 2021 20 : // 21 : // Reference: 22 : // 23 : // John Chrispell, Jason Howell, 24 : // Finite Element Approximation of Partial Differential Equations 25 : // Using FreeFem++, or, How I Learned to Stop Worrying and Love 26 : // Numerical Analysis. 27 : // 28 : // Frederic Hecht, 29 : // New development in FreeFem++, 30 : // Journal of Numerical Mathematics, 31 : // Volume 20, Number 3-4, 2012, pages 251-265. 32 : // 33 : cout << "\n"; 34 : cout << "poisson_2d():\n"; 35 : cout << " FreeFem++ version.\n"; 36 : cout << " Solve the Poisson equation in a square region.\n"; 37 : // 38 : // n: number of nodes in each spatial direction. 39 : // 40 : int nx = 11; 41 : int ny = 11; 42 : // 43 : // h: spacing between nodes. 44 : // 45 : real hx = 1.0 / nx; 46 : real hy = 1.0 / ny; 47 : // 48 : // Th: the triangulation of the unit square. 49 : // 50 : mesh Th = square ( nx, ny ); 51 : // 52 : // Plot the mesh. 53 : // 54 : plot ( Th, 55 : ps = "poisson_2d_mesh.ps", 56 : cmm = "poisson_2d: the mesh" ); 57 : // 58 : // Define Vh, the finite element space. 59 : // Here, "P1" requests piecewise linear functions. 60 : // 61 : fespace Vh ( Th, P1 ); 62 : // 63 : // uh: the trial function that approximates the solution. 64 : // 65 : Vh uh; 66 : // 67 : // vh: the test functions that multiply the equation. 68 : // 69 : Vh vh; 70 : // 71 : // f: the right hand side function f(x,y). 72 : // 73 : func f = 74 : + sin(5*pi*x*(1-x)) * sin(4*pi*y*(1-y)) * pow(5*pi*(1-x)-5*pi*x,2) 75 : + 10 * cos(5*pi*x*(1-x)) * pi * sin(4*pi*y*(1-y)) 76 : + sin(5*pi*x*(1-x)) * sin(4*pi*y*(1-y)) * pow(4*pi*(1-y)-4*pi*y,2) 77 : + 8 * sin(5*pi*x*(1-x)) * pi * cos(4*pi*y*(1-y)); 78 : // 79 : // g: the boundary condition function g(x,y). 80 : // 81 : func g = 0.0; 82 : // 83 : // define "poisson()" to be the problem to be solved. 84 : // 85 : problem poisson ( uh, vh ) 86 : = int2d ( Th ) 87 : ( 88 : dx(uh) * dx(vh) 89 : + dy(uh) * dy(vh) 90 : ) 91 : - int2d ( Th ) 92 : ( 93 : f * vh 94 : ) 95 : + on ( 1, 2, 3, 4, uh = g ); 96 : // 97 : // Solve the problem for uh. 98 : // 99 : poisson; 100 : // 101 : // Plot the computed solution. 102 : // 103 : plot ( uh, fill = true, value = true, 104 : ps = "poisson_2d_fem.ps", 105 : cmm = "poisson_2d: FEM solution" ); 106 : // 107 : // For this problem, we know the exact solution. 108 : // u: the exact solution, projected into finite element space. 109 : // 110 : Vh u = sin ( 5 * pi * x * ( 1 - x ) ) * sin ( 4 * pi * y * ( 1 - y ) ); 111 : // 112 : // Plot the exact solution. 113 : // 114 : plot ( u, fill = true, value = true, 115 : ps = "poisson_2d_exact.ps", 116 : cmm = "poisson_2d: exact" ); 117 : // 118 : // Compute the L2 error. 119 : // 120 : real l2error = sqrt ( int2d ( Th ) ( ( u - uh )^2 ) ); 121 : cout << endl; 122 : cout << " Hx = " << hx << endl; 123 : cout << " Hy = " << hy << endl; 124 : cout << " L2 error = " << l2error << endl; 125 : // 126 : // Terminate. 127 : // 128 : cout << "\n"; 129 : cout << "poisson_2d():\n"; 130 : cout << " Normal end of execution.\n"; 131 : 132 : exit ( 0 ); 133 : sizestack + 1024 =8360 ( 7336 ) poisson_2d(): FreeFem++ version. Solve the Poisson equation in a square region. -- Square mesh : nb vertices =144 , nb triangles = 242 , nb boundary edges 44 -- Solve : min -0.639059 max 0.87817 Hx = 0.0909091 Hy = 0.0909091 L2 error = 0.0219103 poisson_2d(): Normal end of execution. current line = 132 exit(0) err code 0 , mpirank 0 CodeAlloc : nb ptr 4131, size :528360 mpirank: 0 Ok: Normal End