CppNoddy  0.92
Loading...
Searching...
No Matches
PoissonCartesian_petscd_mumps.cpp
Go to the documentation of this file.
1/// \file PoissonCartesian_petscd.cpp
2/// \ingroup Tests
3/// \ingroup Poisson
4/// Solving a Cartesian Poisson problem:
5/// \f[ \nabla^2 \psi(x,y) = 2( x^2 + y^2 ) \f]
6/// with \f[ \psi(x,\pm 1) = x^2\,, \quad\mbox{and}\quad \psi(\pm 1, y) = y^2 \f]
7/// where \f$ (x,y) \in [-1,1]\times[-1,1] \f$.
8/// The global problem is solved (one step) and result is compared to the
9/// exact solution \f[ \psi(x,y) = x^2y^2 \f].
10
11#include <TwoD_Node_Mesh.h>
12#include <SparseLinearSystem.h>
13#include <DenseLinearSystem.h>
14#include <Utility.h>
15#include <PetscSession.h>
16
17namespace CppNoddy
18{
19 namespace Example
20 {
21 double source_fn( const std::pair<double,double>& coord ) {
22 double x = coord.first; double y = coord.second;
23 return 2*(x*x + y*y);
24 }
25
26 double boundary_fn( const std::pair<double,double>& coord ) {
27 double x = coord.first; double y = coord.second;
28 return x*x*y*y;
29 }
30
31 } // end Example namespace
32} // end CppNoddy namespace
33
34
35using namespace CppNoddy;
36using namespace std;
37
38int main(int argc, char *argv[])
39{
40 PetscSession::getInstance(argc,argv);
41
42 // Number of points in a square mesh.
43 unsigned Nx = 5;
44 unsigned Ny = 5;
45
48 TwoD_Node_Mesh<double> mesh(X,Y,2);
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
58 SparseMatrix<double> A(Nx*Ny,Nx*Ny);
59 DenseVector<double> B(Nx*Ny,0.0);
60 {
61 // LHS
62 unsigned i(0);
63 for (unsigned j = 0; j < Ny; ++j) {
64 A(j,j) = 1.0;
65 B[j] = Example::boundary_fn(mesh.coord(i,j));
66 }
67 }
68 // step through x nodes
69 for (unsigned i = 1; i < Nx-1; ++i) {
70 // bottom
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 // interior nodes
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 // top
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 // RHS
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 //A.dump();
98 //B.dump();
99
100 SparseLinearSystem<double> system(&A,&B,"petsc");
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 // solution is quadratic in x,y so should be accurate on
108 // any mesh
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
116 bool failed(true);
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}
int main()
Definition: ArcCircle.cpp:39
Specification of the linear system class.
Specification of a sparse-storage linear system class.
A specification for a two dimensional mesh object.
A spec for a collection of utility functions.
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
A linear system class for vector right-hand sides.
void solve()
Solve the sparse system.
A matrix class that constructs a SPARSE matrix as a row major std::vector of SparseVectors.
Definition: SparseMatrix.h:31
A two dimensional mesh utility object.
void dump_gnu(std::string filename) const
A simple method for dumping data to a file for gnuplot surface plotting.
double max_abs(unsigned var)
Find the maximum stored absolute value in the mesh for a given variable – no interpolation is used.
std::pair< double, double > coord(const std::size_t nodex, const std::size_t nodey) const
Access the nodal position - as a pair.
double boundary_fn(const std::pair< double, double > &coord)
double source_fn(const std::pair< double, double > &coord)
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...
Definition: Utility.cpp:113
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt