| 1 |
Auto adaptive Poisson equation (C++) |
| 2 |
==================================== |
| 3 |
|
| 4 |
Implementation |
| 5 |
-------------- |
| 6 |
|
| 7 |
Running this demo requires the files: :download:`main.cpp`, |
| 8 |
:download:`AdaptivePoisson.ufl` and :download:`CMakeLists.txt`. |
| 9 |
|
| 10 |
Under construction. |
| 11 |
|
| 12 |
.. code-block:: cpp |
| 13 |
|
| 14 |
#include <dolfin.h> |
| 15 |
#include "AdaptivePoisson.h" |
| 16 |
|
| 17 |
using namespace dolfin; |
| 18 |
|
| 19 |
// Source term (right-hand side) |
| 20 |
class Source : public Expression |
| 21 |
{ |
| 22 |
void eval(Array<double>& values, const Array<double>& x) const |
| 23 |
{ |
| 24 |
double dx = x[0] - 0.5; |
| 25 |
double dy = x[1] - 0.5; |
| 26 |
values[0] = 10*exp(-(dx*dx + dy*dy) / 0.02); |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
// Normal derivative (Neumann boundary condition) |
| 31 |
class dUdN : public Expression |
| 32 |
{ |
| 33 |
void eval(Array<double>& values, const Array<double>& x) const |
| 34 |
{ values[0] = sin(5*x[0]); } |
| 35 |
}; |
| 36 |
|
| 37 |
// Sub domain for Dirichlet boundary condition |
| 38 |
class DirichletBoundary : public SubDomain |
| 39 |
{ |
| 40 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 41 |
{ return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS; } |
| 42 |
}; |
| 43 |
|
| 44 |
int main() |
| 45 |
{ |
| 46 |
if (dolfin::MPI::size(MPI_COMM_WORLD) > 1) |
| 47 |
{ |
| 48 |
std::cout << "This demo does not work in parallel" << std::endl; |
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
// Create mesh and define function space |
| 53 |
auto mesh = std::make_shared<UnitSquareMesh>(8, 8); |
| 54 |
auto V = std::make_shared<AdaptivePoisson::BilinearForm::TrialSpace>(mesh); |
| 55 |
|
| 56 |
// Define boundary condition |
| 57 |
auto u0 = std::make_shared<Constant>(0.0); |
| 58 |
auto boundary = std::make_shared<DirichletBoundary>(); |
| 59 |
auto bc = std::make_shared<DirichletBC>(V, u0, boundary); |
| 60 |
|
| 61 |
// Define variational forms |
| 62 |
auto a = std::make_shared<AdaptivePoisson::BilinearForm>(V, V); |
| 63 |
auto L = std::make_shared<AdaptivePoisson::LinearForm>(V); |
| 64 |
auto f = std::make_shared<Source>(); |
| 65 |
auto g = std::make_shared<dUdN>(); |
| 66 |
L->f = f; |
| 67 |
L->g = g; |
| 68 |
|
| 69 |
// Define Function for solution |
| 70 |
auto u = std::make_shared<Function>(V); |
| 71 |
|
| 72 |
// Define goal functional (quantity of interest) |
| 73 |
auto M = std::make_shared<AdaptivePoisson::GoalFunctional>(mesh); |
| 74 |
|
| 75 |
// Define error tolerance |
| 76 |
double tol = 1.e-5; |
| 77 |
|
| 78 |
// Solve equation a = L with respect to u and the given boundary |
| 79 |
// conditions, such that the estimated error (measured in M) is less |
| 80 |
// than tol |
| 81 |
std::vector<std::shared_ptr<const DirichletBC>> bcs({bc}); |
| 82 |
auto problem = std::make_shared<LinearVariationalProblem>(a, L, u, bcs); |
| 83 |
AdaptiveLinearVariationalSolver solver(problem, M); |
| 84 |
solver.parameters("error_control")("dual_variational_solver")["linear_solver"] |
| 85 |
= "cg"; |
| 86 |
solver.parameters("error_control")("dual_variational_solver")["symmetric"] |
| 87 |
= true; |
| 88 |
solver.solve(tol); |
| 89 |
|
| 90 |
solver.summary(); |
| 91 |
|
| 92 |
// Output solution to XDMF for Paraview |
| 93 |
XDMFFile("initial_solution.xdmf").write(u->root_node()); |
| 94 |
XDMFFile("final_solution.xdmf").write(u->leaf_node()); |
| 95 |
|
| 96 |
return 0; |
| 97 |
} |