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