| 1 |
// Copyright (C) 2006-2008 Anders Logg |
| 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 |
// First added: 2006-02-09 |
| 19 |
// Last changed: 2012-07-05 |
| 20 |
// |
| 21 |
// This demo solves the Stokes equations, using quadratic elements for |
| 22 |
// the velocity and first degree elements for the pressure |
| 23 |
// (Taylor-Hood elements). The sub domains for the different boundary |
| 24 |
// conditions used in this simulation are computed by the demo program |
| 25 |
// in src/demo/mesh/subdomains. |
| 26 |
|
| 27 |
#include <dolfin.h> |
| 28 |
#include "Stokes.h" |
| 29 |
|
| 30 |
using namespace dolfin; |
| 31 |
|
| 32 |
int main() |
| 33 |
{ |
| 34 |
// Function for no-slip boundary condition for velocity |
| 35 |
class Noslip : public Expression |
| 36 |
{ |
| 37 |
public: |
| 38 |
|
| 39 |
Noslip() : Expression(2) {} |
| 40 |
|
| 41 |
void eval(Array<double>& values, const Array<double>& x) const |
| 42 |
{ |
| 43 |
values[0] = 0.0; |
| 44 |
values[1] = 0.0; |
| 45 |
} |
| 46 |
|
| 47 |
}; |
| 48 |
|
| 49 |
// Function for inflow boundary condition for velocity |
| 50 |
class Inflow : public Expression |
| 51 |
{ |
| 52 |
public: |
| 53 |
|
| 54 |
Inflow() : Expression(2) {} |
| 55 |
|
| 56 |
void eval(Array<double>& values, const Array<double>& x) const |
| 57 |
{ |
| 58 |
values[0] = -sin(x[1]*DOLFIN_PI); |
| 59 |
values[1] = 0.0; |
| 60 |
} |
| 61 |
|
| 62 |
}; |
| 63 |
|
| 64 |
// Read mesh and sub domain markers |
| 65 |
auto mesh = std::make_shared<Mesh>("../dolfin_fine.xml.gz"); |
| 66 |
auto sub_domains = std::make_shared<MeshFunction<std::size_t>>(mesh, "../dolfin_fine_subdomains.xml.gz"); |
| 67 |
|
| 68 |
// Create function space |
| 69 |
auto W = std::make_shared<Stokes::FunctionSpace>(mesh); |
| 70 |
|
| 71 |
// Create functions for boundary conditions |
| 72 |
auto noslip = std::make_shared<Noslip>(); |
| 73 |
auto inflow = std::make_shared<Inflow>(); |
| 74 |
auto zero = std::make_shared<Constant>(0.0); |
| 75 |
|
| 76 |
// No-slip boundary condition for velocity |
| 77 |
DirichletBC bc0(W->sub(0), noslip, sub_domains, 0); |
| 78 |
|
| 79 |
// Inflow boundary condition for velocity |
| 80 |
DirichletBC bc1(W->sub(0), inflow, sub_domains, 1); |
| 81 |
|
| 82 |
// Collect boundary conditions |
| 83 |
std::vector<const DirichletBC*> bcs = {{&bc0, &bc1}}; |
| 84 |
|
| 85 |
// Define variational problem |
| 86 |
auto f = std::make_shared<Constant>(0.0, 0.0); |
| 87 |
Stokes::BilinearForm a(W, W); |
| 88 |
Stokes::LinearForm L(W); |
| 89 |
L.f = f; |
| 90 |
|
| 91 |
// Compute solution |
| 92 |
Function w(W); |
| 93 |
solve(a == L, w, bcs); |
| 94 |
Function u = w[0]; |
| 95 |
Function p = w[1]; |
| 96 |
|
| 97 |
// Save solution in VTK format |
| 98 |
File ufile_pvd("velocity.pvd"); |
| 99 |
ufile_pvd << u; |
| 100 |
File pfile_pvd("pressure.pvd"); |
| 101 |
pfile_pvd << p; |
| 102 |
|
| 103 |
File pfile_mf("mf.pvd"); |
| 104 |
pfile_mf << *sub_domains; |
| 105 |
|
| 106 |
return 0; |
| 107 |
} |