21 double source_fn(
const std::pair<double,double>& coord ) {
22 double x = coord.first;
double y = coord.second;
27 double x = coord.first;
double y = coord.second;
38int main(
int argc,
char *argv[])
40 PetscSession::getInstance(argc,argv);
49 double dx2 = pow(X[1]-X[0],2);
50 double dy2 = pow(Y[1]-Y[0],2);
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";
63 for (
unsigned j = 0; j < Ny; ++j) {
65 B[j] = Example::boundary_fn(mesh.
coord(i,j));
69 for (
unsigned i = 1; i < Nx-1; ++i) {
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) {
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));
85 A(i*Ny+j,i*Ny+j) = 1.0;
86 B[i*Ny+j] = Example::boundary_fn(mesh.
coord(i,j));
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));
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;
109 mesh(i,j,0)=B[i*Nx+j];
110 mesh(i,j,1)=mesh(i,j,0) - pow(x*y,2.0);
117 const double tol(1.e-14);
118 double abserror = mesh.
max_abs(1);
119 cout <<
"error = " << abserror <<
"\n";
121 if ( abserror < tol ) failed =
false;
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";
132 cout <<
"\033[1;32;48m * PASSED \033[0m\n";
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.
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.
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...
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...