Skip to content

Commit f71148b

Browse files
committed
add unit test
1 parent 88fc0f7 commit f71148b

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+

UnitTests/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# Direct-mode tests:
77
su2_cfd_tests = files(['Common/geometry/primal_grid/CPrimalGrid_tests.cpp',
88
'Common/geometry/dual_grid/CDualGrid_tests.cpp',
9+
'Common/toolboxes/CQuasiNewtonInvLeastSquares_tests.cpp',
910
'SU2_CFD/numerics/CNumerics_tests.cpp'])
1011

1112
# Reverse-mode (algorithmic differentiation) tests:

0 commit comments

Comments
 (0)