| 1 |
// Copyright (C) 2007-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 |
// Modified by Garth N. Wells, 2009 |
| 19 |
// Modified by Martin S. Alnæs, 2012 |
| 20 |
// |
| 21 |
// First added: 2007-05-14 |
| 22 |
// Last changed: 2012-08-31 |
| 23 |
// |
| 24 |
// This demo demonstrates how to compute functionals (or forms in |
| 25 |
// general) over subsets of the mesh. The two functionals lift and |
| 26 |
// drag are computed for the pressure field around a dolphin. Here, we |
| 27 |
// use the pressure field obtained from solving the Stokes equations |
| 28 |
// (see demo program in the sub directory |
| 29 |
// src/demo/pde/stokes/taylor-hood). |
| 30 |
// |
| 31 |
// The calculation only includes the pressure contribution (not shear |
| 32 |
// forces). |
| 33 |
|
| 34 |
#include <dolfin.h> |
| 35 |
#include "Functionals.h" |
| 36 |
|
| 37 |
using namespace dolfin; |
| 38 |
|
| 39 |
// Define sub domain for the dolphin |
| 40 |
class Fish : public SubDomain |
| 41 |
{ |
| 42 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 43 |
{ |
| 44 |
return (x[0] > DOLFIN_EPS && x[0] < (1.0 - DOLFIN_EPS) && |
| 45 |
x[1] > DOLFIN_EPS && x[1] < (1.0 - DOLFIN_EPS) && |
| 46 |
on_boundary); |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
int main() |
| 51 |
{ |
| 52 |
// Read mesh from file |
| 53 |
auto mesh = std::make_shared<Mesh>("../dolfin_fine.xml.gz"); |
| 54 |
|
| 55 |
// Read velocity field from file |
| 56 |
auto Vp = std::make_shared<Functionals::CoefficientSpace_p>(mesh); |
| 57 |
auto p = std::make_shared<Function>(Vp, "../dolfin_fine_pressure.xml.gz"); |
| 58 |
|
| 59 |
// Mark 'fish' |
| 60 |
auto markers = std::make_shared<MeshFunction<std::size_t>>(mesh, mesh->topology().dim()-1, 1); |
| 61 |
Fish fish; |
| 62 |
fish.mark(*markers, 1); |
| 63 |
|
| 64 |
// Functionals for lift and drag |
| 65 |
Functionals::Form_lift L(mesh, p); |
| 66 |
Functionals::Form_drag D(mesh, p); |
| 67 |
|
| 68 |
// Attach markers to functionals |
| 69 |
L.ds = markers; |
| 70 |
D.ds = markers; |
| 71 |
|
| 72 |
// Assemble functionals over sub domain |
| 73 |
const double lift = assemble(L); |
| 74 |
const double drag = assemble(D); |
| 75 |
|
| 76 |
info("Lift: %f", lift); |
| 77 |
info("Drag: %f", drag); |
| 78 |
} |