Debian Codegraph @

packages/stk/stk_unit_tests/stk_middle_mesh/test_gauss_newton.cpp

raw
1 #include "gtest/gtest.h"
2 #include "stk_middle_mesh/gauss_newton.hpp"
3
4 using namespace stk::middle_mesh::utils::impl;
5
6 TEST(GaussNewton, Quadratic)
7 {
8
9 auto f = [](const std::vector<double>& x, std::vector<double>& residuals)
10 {
11 for (int i=0; i < 2; ++i)
12 residuals[i] = x[i]*x[i];
13 };
14
15 auto jacTest = [](const std::vector<double>& x, Matrix<double>& jac)
16 {
17 jac.fill(0);
18 for (int i=0; i < 2; ++i)
19 jac(i, i) = 2*x[i];
20 };
21
22 GaussNewton gaussNewton(2, 2, 1e-40, 100);
23
24 std::vector<double> x0 = {1, 2};
25 gaussNewton.solve(f, jacTest, x0);
26
27 EXPECT_NEAR(x0[0], 0, 1e-12);
28 EXPECT_NEAR(x0[1], 0, 1e-12);
29 }
30
31 TEST(GaussNewton, RectangularDegenerate)
32 {
33
34 auto f = [](const std::vector<double>& x, std::vector<double>& residuals)
35 {
36 residuals[0] = x[0] - 0.5;
37 residuals[1] = x[1] - 1;
38 residuals[2] = 1.5 - x[0] - x[1];
39 };
40
41 auto jacTest = [](const std::vector<double>& /*x*/, Matrix<double>& jac)
42 {
43 jac.fill(0);
44 jac(0, 0) = 1;
45 jac(1, 1) = 1;
46 jac(2, 0) = -1; jac(2, 1) = -1;
47 };
48
49 GaussNewton gaussNewton(3, 2, 1e-40, 100);
50
51 std::vector<double> x0 = {1, 2};
52 gaussNewton.solve(f, jacTest, x0);
53
54 EXPECT_NEAR(x0[0], 0.5, 1e-12);
55 EXPECT_NEAR(x0[1], 1.0, 1e-12);
56 }