|
| 1 | +/*! |
| 2 | + * \file CQuasiNewtonInvLeastSquares_tests.cpp |
| 3 | + * \brief Unit tests for the CQuasiNewtonInvLeastSquares class. |
| 4 | + * Which should find the root of a n-d linear problem in n+1 iterations. |
| 5 | + * \author P. Gomes |
| 6 | + * \version 7.0.5 "Blackbird" |
| 7 | + * |
| 8 | + * SU2 Project Website: https://su2code.github.io |
| 9 | + * |
| 10 | + * The SU2 Project is maintained by the SU2 Foundation |
| 11 | + * (http://su2foundation.org) |
| 12 | + * |
| 13 | + * Copyright 2012-2020, SU2 Contributors (cf. AUTHORS.md) |
| 14 | + * |
| 15 | + * SU2 is free software; you can redistribute it and/or |
| 16 | + * modify it under the terms of the GNU Lesser General Public |
| 17 | + * License as published by the Free Software Foundation; either |
| 18 | + * version 2.1 of the License, or (at your option) any later version. |
| 19 | + * |
| 20 | + * SU2 is distributed in the hope that it will be useful, |
| 21 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | + * Lesser General Public License for more details. |
| 24 | + * |
| 25 | + * You should have received a copy of the GNU Lesser General Public |
| 26 | + * License along with SU2. If not, see <http://www.gnu.org/licenses/>. |
| 27 | + */ |
| 28 | + |
| 29 | +#include "catch.hpp" |
| 30 | +#include <sstream> |
| 31 | +#include <iomanip> |
| 32 | +#include "../../../Common/include/toolboxes/CQuasiNewtonInvLeastSquares.hpp" |
| 33 | + |
| 34 | +struct Problem { |
| 35 | + static constexpr int N = 4; |
| 36 | + static constexpr passivedouble coeffs[][N] = {{0.5, -0.7, 0.2, 3.0}, |
| 37 | + {1.0, -0.2, -0.6, 0.0}, |
| 38 | + {0.1, 0.2, 3.14, -1.0}, |
| 39 | + {-1.0, -0.4, 0.0, 1.6}}; |
| 40 | + /*--- Row sum, sol should be {1.0}. ---*/ |
| 41 | + static constexpr passivedouble rhs[] = {3.0, 0.2, 2.44, 0.2}; |
| 42 | + passivedouble sol[N] = {0.0}; |
| 43 | + |
| 44 | + template<class T> |
| 45 | + void iterate(const T& x) { |
| 46 | + for(int i=0; i<N; ++i) { |
| 47 | + sol[i] = x(i,0) + rhs[i]; |
| 48 | + for(int j=0; j<N; ++j) |
| 49 | + sol[i] -= coeffs[i][j] * x(j,0); |
| 50 | + } |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +template<class P, class Q> |
| 55 | +void iterate(P& p, Q& q) { |
| 56 | + p.iterate(q); |
| 57 | + for(int i=0; i<P::N; ++i) |
| 58 | + q.FPresult(i,0) = p.sol[i]; |
| 59 | + q.compute(); |
| 60 | +} |
| 61 | + |
| 62 | +TEST_CASE("QN-ILS", "[Toolboxes]") { |
| 63 | + Problem p; |
| 64 | + CQuasiNewtonInvLeastSquares<passivedouble> qnils(Problem::N+1, Problem::N, 1); |
| 65 | + |
| 66 | + /*--- Solve ---*/ |
| 67 | + for(int i=0; i<=Problem::N; ++i) |
| 68 | + iterate(p, qnils); |
| 69 | + |
| 70 | + /*--- Check we solved in N+1 iterations. ---*/ |
| 71 | + for(int i=0; i<Problem::N; ++i) |
| 72 | + CHECK(qnils(i,0) == Approx(1.0)); |
| 73 | + |
| 74 | + /*--- Check we don't break a converged problem. ---*/ |
| 75 | + iterate(p, qnils); |
| 76 | + |
| 77 | + for(int i=0; i<Problem::N; ++i) |
| 78 | + CHECK(qnils(i,0) == Approx(1.0)); |
| 79 | +} |
| 80 | + |
0 commit comments