39{
40 PetscSession::getInstance(argc,argv);
41
42
43 unsigned Nx = 5;
44 unsigned Ny = 5;
45
49 double dx2 = pow(X[1]-X[0],2);
50 double dy2 = pow(Y[1]-Y[0],2);
51
52 cout << "\n";
53 cout << "=== Poisson: Cartesian geometry =====================\n";
54 cout << " Solving in [-2 , 2] x [-1 , 1] \n";
55 cout << " Using a " << Nx << "x" << Ny << " mesh\n";
56 cout << "\n";
57
60 {
61
62 unsigned i(0);
63 for (unsigned j = 0; j < Ny; ++j) {
65 B[j] = Example::boundary_fn(mesh.coord(i,j));
66 }
67 }
68
69 for (unsigned i = 1; i < Nx-1; ++i) {
70
71 unsigned j = 0;
72 A(i*Ny+j,i*Ny+j) = 1.0;
73 B[i*Ny+j] = Example::boundary_fn(mesh.coord(i,j));
74 for ( j = 1; j < Ny-1; ++j) {
75
76 A(i*Ny+j,i*Ny+j-1) = 1./dy2;
77 A(i*Ny+j,i*Ny+j) = -2./dx2-2./dy2;
78 A(i*Ny+j,i*Ny+j+1) = 1./dy2;
79 A(i*Ny+j,i*Ny+j+Ny) = 1./dx2;
80 A(i*Ny+j,i*Ny+j-Ny) = 1./dx2;
81 B[i*Ny+j] = Example::source_fn(mesh.coord(i,j));
82 }
83
84 j = Ny-1;
85 A(i*Ny+j,i*Ny+j) = 1.0;
86 B[i*Ny+j] = Example::boundary_fn(mesh.coord(i,j));
87 }
88 {
89
90 unsigned i(Nx-1);
91 for (unsigned j = 0; j < Ny; ++j) {
92 A(i*Ny+j,i*Ny+j) = 1.0;
93 B[i*Ny+j] = Example::boundary_fn(mesh.coord(i,j));
94 }
95 }
96
97
98
99
101 system.solve();
102
103 for (unsigned i = 0; i < Nx; ++i) {
104 for (unsigned j = 0; j < Ny; ++j) {
105 const double x = mesh.coord(i,j).first;
106 const double y = mesh.coord(i,j).second;
107
108
109 mesh(i,j,0)=B[i*Nx+j];
110 mesh(i,j,1)=mesh(i,j,0) - pow(x*y,2.0);
111 }
112 }
113
114 mesh.dump_gnu("./DATA/mesh.dat");
115
117 const double tol(1.e-14);
118 double abserror = mesh.max_abs(1);
119 cout << "error = " << abserror << "\n";
120
121 if ( abserror < tol )
failed =
false;
122
123 if ( failed )
124 {
125 cout << "Poisson solver failed to give exact solution.\n";
126 cout << "error = " << abserror << "\n";
127 cout << "\033[1;31;48m * FAILED \033[0m\n";
128 return 1;
129 }
130 else
131 {
132 cout << "\033[1;32;48m * PASSED \033[0m\n";
133 return 0;
134 }
135
136}
An DenseVector class – a dense vector object.
A linear system class for vector right-hand sides.
A matrix class that constructs a SPARSE matrix as a row major std::vector of SparseVectors.
A two dimensional mesh utility object.
double A(1.0)
initial hump amplitude
DenseVector< double > uniform_node_vector(const double &lower, const double &upper, const std::size_t &N)
Return a DENSE vector with the nodal points of a uniform mesh distributed between the upper/lower bou...