| 1 |
// Copyright (C) 2007 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 |
// Unit tests for the function library |
| 19 |
|
| 20 |
#include <dolfin.h> |
| 21 |
#include "Projection.h" |
| 22 |
#include <catch.hpp> |
| 23 |
|
| 24 |
using namespace dolfin; |
| 25 |
|
| 26 |
namespace |
| 27 |
{ |
| 28 |
void arbitrary_eval() |
| 29 |
{ |
| 30 |
class F0 : public Expression |
| 31 |
{ |
| 32 |
public: |
| 33 |
F0() {} |
| 34 |
void eval(Array<double>& values, const Array<double>& x) const |
| 35 |
{ values[0] = sin(3.0*x[0])*sin(3.0*x[1])*sin(3.0*x[2]); } |
| 36 |
}; |
| 37 |
|
| 38 |
class F1 : public Expression |
| 39 |
{ |
| 40 |
public: |
| 41 |
F1() {} |
| 42 |
void eval(Array<double>& values, const Array<double>& x) const |
| 43 |
{ values[0] = 1.0 + 3.0*x[0] + 4.0*x[1] + 0.5*x[2]; } |
| 44 |
}; |
| 45 |
|
| 46 |
auto mesh = std::make_shared<UnitCubeMesh>(8, 8, 8); |
| 47 |
|
| 48 |
Array<double> x(3); |
| 49 |
x[0] = 0.31; x[1] = 0.32; x[2] = 0.33; |
| 50 |
|
| 51 |
Array<double> u0(1); |
| 52 |
Array<double> u1(1); |
| 53 |
|
| 54 |
// User-defined functions (one from finite element space, one not) |
| 55 |
F0 f0; |
| 56 |
auto f1 = std::make_shared<F1>(); |
| 57 |
|
| 58 |
// Test evaluation of a user-defined function |
| 59 |
f0.eval(u0, x); |
| 60 |
CHECK(u0[0] == Approx(sin(3.0*x[0])*sin(3.0*x[1])*sin(3.0*x[2]))); |
| 61 |
|
| 62 |
// Test for single core only |
| 63 |
if (dolfin::MPI::size(mesh->mpi_comm()) == 1) |
| 64 |
{ |
| 65 |
// Test evaluation of a discrete function |
| 66 |
auto V = std::make_shared<Projection::FunctionSpace>(mesh); |
| 67 |
Projection::BilinearForm a(V, V); |
| 68 |
Projection::LinearForm L(V); |
| 69 |
L.f = f1; |
| 70 |
Function g(V); |
| 71 |
solve(a == L, g); |
| 72 |
|
| 73 |
const double tol = 1.0e-6; |
| 74 |
f1->eval(u0, x); |
| 75 |
g.eval(u1, x); |
| 76 |
CHECK(std::abs(u0[0] - u1[0]) < tol); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
//----------------------------------------------------------------------------- |
| 82 |
TEST_CASE( "Test arbitray eval", "[eval]" ) |
| 83 |
{ |
| 84 |
CHECK_NOTHROW(arbitrary_eval()); |
| 85 |
} |