| 1 |
// Copyright (C) 2010 Marie E. Rognes |
| 2 |
// |
| 3 |
// This file is part of DOLFIN. |
| 4 |
// |
| 5 |
// DOLFIN is free software: you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU Lesser General Public License as published by |
| 7 |
// the Free Software Foundation, either version 3 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// DOLFIN is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU Lesser General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
// |
| 18 |
// Modified by Anders Logg 2011 |
| 19 |
// |
| 20 |
// First added: 2010-08-19 |
| 21 |
// Last changed: 2012-07-05 |
| 22 |
|
| 23 |
#include <dolfin.h> |
| 24 |
#include "AdaptiveNavierStokes.h" |
| 25 |
|
| 26 |
using namespace dolfin; |
| 27 |
|
| 28 |
// No-slip boundary |
| 29 |
class Noslip : public SubDomain |
| 30 |
{ |
| 31 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 32 |
{ |
| 33 |
return ((x[1] < DOLFIN_EPS || x[1] > 1.0 - DOLFIN_EPS) || |
| 34 |
(on_boundary && std::abs(x[0] - 1.5) < 0.1 + DOLFIN_EPS)); |
| 35 |
} |
| 36 |
}; |
| 37 |
|
| 38 |
// Subdomain on which goal function should be defined |
| 39 |
class Outflow : public SubDomain |
| 40 |
{ |
| 41 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 42 |
{ |
| 43 |
return x[0] > 4.0 - DOLFIN_EPS; |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
// Applied pressure (Neumann boundary condition) |
| 48 |
class Pressure : public Expression |
| 49 |
{ |
| 50 |
void eval(Array<double>& values, const Array<double>& x) const |
| 51 |
{ |
| 52 |
values[0] = (4.0 - x[0])/4.0; |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
int main() |
| 57 |
{ |
| 58 |
if (dolfin::MPI::size(MPI_COMM_WORLD) > 1) |
| 59 |
{ |
| 60 |
std::cout << "This demo does not work in parallel" << std::endl; |
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
parameters["allow_extrapolation"] = true; |
| 65 |
parameters["refinement_algorithm"] = "plaza_with_parent_facets"; |
| 66 |
|
| 67 |
// Create mesh and function space |
| 68 |
auto mesh = std::make_shared<Mesh>("../channel_with_flap.xml.gz"); |
| 69 |
auto W = std::make_shared<AdaptiveNavierStokes::BilinearForm::TrialSpace>(mesh); |
| 70 |
|
| 71 |
// Unknown field |
| 72 |
auto w = std::make_shared<Function>(W); |
| 73 |
|
| 74 |
// Define boundary condition |
| 75 |
auto u0 = std::make_shared<Constant>(0.0, 0.0); |
| 76 |
Noslip noslip; |
| 77 |
auto noslip_markers = std::make_shared<MeshFunction<std::size_t>>(mesh, mesh->topology().dim()-1, 1); |
| 78 |
noslip.mark(*noslip_markers, 0); |
| 79 |
auto W0 = W->sub(0); |
| 80 |
auto bc = std::make_shared<DirichletBC>(W0, u0, noslip_markers, 0); |
| 81 |
|
| 82 |
// Create variational formulation and assign coefficients |
| 83 |
auto nu = std::make_shared<Constant>(0.02); |
| 84 |
auto F = std::make_shared<AdaptiveNavierStokes::LinearForm>(W); |
| 85 |
auto p0 = std::make_shared<Pressure>(); |
| 86 |
F->p0 = p0; |
| 87 |
F->nu = nu; |
| 88 |
F->w = w; |
| 89 |
|
| 90 |
// Initialize Jacobian dF |
| 91 |
auto dF = std::make_shared<AdaptiveNavierStokes::BilinearForm>(W, W); |
| 92 |
dF->nu = nu; |
| 93 |
dF->w = w; |
| 94 |
|
| 95 |
// Define goal functional |
| 96 |
auto M = std::make_shared<AdaptiveNavierStokes::GoalFunctional>(mesh); |
| 97 |
M->w = w; |
| 98 |
Outflow outflow; |
| 99 |
auto outflow_markers = std::make_shared<MeshFunction<std::size_t>>(mesh, mesh->topology().dim()-1, 1); |
| 100 |
outflow.mark(*outflow_markers, 0); |
| 101 |
M->ds = outflow_markers; |
| 102 |
|
| 103 |
// Define error tolerance |
| 104 |
double tol = 1.e-5; |
| 105 |
|
| 106 |
// If no more control is wanted, do: |
| 107 |
// solve(F == 0, w, bc, dF, tol, M); |
| 108 |
// return 0; |
| 109 |
|
| 110 |
// Define variational problem from the variational form F, specify |
| 111 |
// the unknown Function w and the boundary condition bc |
| 112 |
std::vector<std::shared_ptr<const DirichletBC>> bcs {bc}; |
| 113 |
auto pde = std::make_shared<NonlinearVariationalProblem>(F, w, bcs, dF); |
| 114 |
|
| 115 |
// Define solver |
| 116 |
AdaptiveNonlinearVariationalSolver solver(pde, M); |
| 117 |
|
| 118 |
// Set (precomputed) reference in adaptive solver to evaluate |
| 119 |
// quality of error estimates and adaptive refinement |
| 120 |
solver.parameters["reference"] = 0.40863917; |
| 121 |
|
| 122 |
// Solve problem with goal-oriented error control to given tolerance |
| 123 |
solver.solve(tol); |
| 124 |
|
| 125 |
// Show solver summary |
| 126 |
solver.summary(); |
| 127 |
|
| 128 |
// Show all timings |
| 129 |
list_timings(TimingClear::clear, { TimingType::wall }); |
| 130 |
|
| 131 |
// Output solutions |
| 132 |
Function solution = w->leaf_node(); |
| 133 |
XDMFFile("velocity.xdmf").write(solution[0]); |
| 134 |
XDMFFile("pressure.xdmf").write(solution[1]); |
| 135 |
|
| 136 |
return 0; |
| 137 |
} |