| 1 |
// Copyright (C) 2010 Andre Massing |
| 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: 2010-06-10 |
| 19 |
// Last changed: 2012-12-12 |
| 20 |
// |
| 21 |
// Description: Benchmark for the evaluations of functions at arbitrary points. |
| 22 |
|
| 23 |
#include <dolfin.h> |
| 24 |
#include "P1.h" |
| 25 |
|
| 26 |
using namespace dolfin; |
| 27 |
|
| 28 |
class F : public Expression |
| 29 |
{ |
| 30 |
public: |
| 31 |
|
| 32 |
void eval(Array<double>& values, const Array<double>& x) const |
| 33 |
{ |
| 34 |
values[0] = sin(3.0*x[0])*sin(3.0*x[1])*sin(3.0*x[2]); |
| 35 |
} |
| 36 |
|
| 37 |
}; |
| 38 |
|
| 39 |
int main(int argc, char* argv[]) |
| 40 |
{ |
| 41 |
not_working_in_parallel("Function evalutation benchmark"); |
| 42 |
|
| 43 |
info("Evaluations of functions at arbitrary points."); |
| 44 |
|
| 45 |
const std::size_t mesh_max_size = 32; |
| 46 |
const std::size_t num_points = 10000000; |
| 47 |
|
| 48 |
// Start timing |
| 49 |
tic(); |
| 50 |
for (std::size_t N = 10; N < mesh_max_size; N += 2) |
| 51 |
{ |
| 52 |
const auto mesh = std::make_shared<const UnitCubeMesh>(N, N, N); |
| 53 |
|
| 54 |
const auto V0 = std::make_shared<const P1::FunctionSpace>(mesh); |
| 55 |
Function f0(V0); |
| 56 |
F f; |
| 57 |
f0.interpolate(f); |
| 58 |
|
| 59 |
Array<double> X(3); |
| 60 |
Array<double> value(1); |
| 61 |
|
| 62 |
// Initialize random generator generator (produces same sequence each test). |
| 63 |
srand(1); |
| 64 |
|
| 65 |
for (std::size_t i = 1; i <= num_points; ++i) |
| 66 |
{ |
| 67 |
X[0] = std::rand()/static_cast<double>(RAND_MAX); |
| 68 |
X[1] = std::rand()/static_cast<double>(RAND_MAX); |
| 69 |
X[2] = std::rand()/static_cast<double>(RAND_MAX); |
| 70 |
|
| 71 |
f.eval(value, X); |
| 72 |
} |
| 73 |
|
| 74 |
// Use X variable. |
| 75 |
info("x = %.12e\ty = %.12e\tz = %.12e\tf(x) = %.12e", X[0], X[1], X[2], value[0]); |
| 76 |
} |
| 77 |
info("BENCH %g",toc()); |
| 78 |
|
| 79 |
return 0; |
| 80 |
} |