Debian Codegraph @

packages/stk/stk_unit_tests/stk_middle_mesh/test_patch_objective.cpp

raw
1 #include "gtest/gtest.h"
2
3 #include "stk_middle_mesh/create_mesh.hpp"
4 #include "stk_middle_mesh/patch_distortion_objective.hpp"
5 #include "stk_middle_mesh/patch_energy_objective.hpp"
6 #include "stk_middle_mesh/regularized_distortion_metric.hpp"
7
8 namespace stk {
9 namespace middle_mesh {
10 namespace impl {
11
12 using namespace mesh;
13 using namespace mesh::impl;
14
15 namespace {
16
17 MeshEntityPtr find_closest_vert(std::shared_ptr<Mesh> mesh, const utils::Point& pt)
18 {
19 double minDist = std::numeric_limits<double>::max();
20 MeshEntityPtr minVert = nullptr;
21 for (auto& vert : mesh->get_vertices())
22 if (vert)
23 {
24 auto pt2 = vert->get_point_orig(0);
25 auto disp = pt - pt2;
26 double dist = dot(disp, disp);
27 if (dist < minDist)
28 {
29 minDist = dist;
30 minVert = vert;
31 }
32 }
33
34 return minVert;
35 }
36
37 void expect_float_eq2(const utils::Point& pt1, const utils::Point& pt2)
38 {
39 EXPECT_FLOAT_EQ(pt1.x, pt2.x);
40 EXPECT_FLOAT_EQ(pt1.y, pt2.y);
41 }
42
43 void test_gradient(opt::impl::PatchObjective& obj, opt::impl::ActiveVertData& active, utils::Point& pt,
44 const double tol)
45 {
46 obj.set_active_patch(active);
47 utils::Point grad = obj.compute_quality_rev(pt);
48
49 // check finite difference
50 double eps = 1e-7;
51 double q0 = obj.compute_quality(pt);
52 // std::cout << "q0 = " << q0 << std::endl;
53
54 pt.x += eps;
55 double q1 = obj.compute_quality(pt);
56 // std::cout << "q1 = " << q1 << std::endl;
57 double dqDx1 = (q1 - q0) / eps;
58 pt.x -= eps;
59
60 pt.y += eps;
61 double q2 = obj.compute_quality(pt);
62 double dqDx2 = (q2 - q0) / eps;
63 pt.y -= eps;
64
65 // std::cout << "deriv_fd = " << utils::Point(dq_dx1, dq_dx2) << std::endl;
66 // std::cout << "deriv = " << pt << std::endl;
67 EXPECT_NEAR(dqDx1, grad.x, tol);
68 EXPECT_NEAR(dqDx2, grad.y, tol);
69 }
70
71 void test_hessian(opt::impl::PatchObjective& obj, opt::impl::ActiveVertData& active, utils::Point& pt, const double /*tol*/)
72
73 {
74 obj.set_active_patch(active);
75 auto hessian = obj.compute_hessian(pt);
76
77 utils::impl::Mat2x2<double> hessian2;
78 double eps = 1e-7;
79 auto col0 = obj.compute_quality_rev(pt, 1);
80
81 pt.x += eps;
82 auto col1 = obj.compute_quality_rev(pt, 1);
83 pt.x -= eps;
84
85 pt.y += eps;
86 auto col2 = obj.compute_quality_rev(pt, 1);
87 pt.y -= eps;
88
89 hessian2(0, 0) = (col1.x - col0.x) / eps;
90 hessian2(1, 0) = (col1.y - col0.y) / eps;
91 hessian2(0, 1) = (col2.x - col0.x) / eps;
92 hessian2(1, 1) = (col2.y - col0.y) / eps;
93
94 std::cout << "hessian = \n" << hessian << std::endl;
95 // std::cout << "hessian2 = \n" << hessian2 << std::endl;
96 for (int i = 0; i < 2; ++i)
97 for (int j = 0; j < 2; ++j)
98 // assert(std::abs(hessian(i, j) - hessian2(i, j)) < 5e-5);
99 EXPECT_NEAR(hessian(i, j), hessian2(i, j), 5e-5);
100 }
101
102 } // namespace
103
104 TEST(PatchObjective, computeDeriv)
105 {
106 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
107 GTEST_SKIP();
108
109 MeshSpec spec;
110 spec.numelX = 2;
111 spec.numelY = 2;
112 spec.xmin = 0;
113 spec.xmax = 1;
114 spec.ymin = 0;
115 spec.ymax = 1;
116
117 auto func = [&](const utils::Point& pt) { return pt; };
118
119 std::shared_ptr<Mesh> mesh = create_mesh(spec, func);
120 MeshEntityPtr vert = find_closest_vert(mesh, utils::Point(0.5, 0.5));
121 opt::impl::ActiveVertData active(mesh, vert);
122
123 auto metric = std::make_shared<RegularizedDistortionMetric<double>>();
124 PatchDistortionObjective obj(metric);
125 opt::impl::PatchEnergyObjective obj2;
126 // evaluate somewhere other than the initial position
127 // (where the W matrix was calculated -> quality = 1 -> gradient = 0)
128 // vert->set_point_orig(0, utils::Point(0.55, 0.65));
129 utils::Point pt = utils::Point(0.55, 0.65);
130
131 test_gradient(obj, active, pt, 2e-6);
132 test_gradient(obj2, active, pt, 2e-6);
133 }
134
135 TEST(PatchObjective, computeHessian)
136 {
137 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
138 GTEST_SKIP();
139
140 MeshSpec spec;
141 spec.numelX = 2;
142 spec.numelY = 2;
143 spec.xmin = 0;
144 spec.xmax = 1;
145 spec.ymin = 0;
146 spec.ymax = 1;
147
148 auto func = [&](const utils::Point& pt) { return pt; };
149
150 std::shared_ptr<Mesh> mesh = create_mesh(spec, func);
151 MeshEntityPtr vert = find_closest_vert(mesh, utils::Point(0.5, 0.5));
152 opt::impl::ActiveVertData active(mesh, vert);
153
154 auto metric = std::make_shared<RegularizedDistortionMetric<double>>();
155 PatchDistortionObjective obj(metric);
156
157 opt::impl::PatchEnergyObjective obj2;
158 // evaluate somewhere other than the initial position
159 // (where the W matrix was calculated -> quality = 1 -> gradient = 0)
160 utils::Point pt = utils::Point(0.55, 0.65);
161
162 test_hessian(obj, active, pt, 5e-5);
163 test_hessian(obj2, active, pt, 5e-5);
164 }
165
166 TEST(PatchObjective, Parameterization)
167 {
168 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
169 GTEST_SKIP();
170
171 MeshSpec spec;
172 spec.numelX = 2;
173 spec.numelY = 2;
174 spec.xmin = 0;
175 spec.xmax = 1;
176 spec.ymin = 0;
177 spec.ymax = 1;
178
179 auto func = [&](const utils::Point& pt) { return pt; };
180
181 std::shared_ptr<Mesh> mesh = create_mesh(spec, func);
182 MeshEntityPtr vert = find_closest_vert(mesh, utils::Point(0.5, 0.5));
183 opt::impl::ActiveVertData active(mesh, vert);
184
185 auto metric = std::make_shared<RegularizedDistortionMetric<double>>();
186 // auto f = [](MeshEntityPtr) { return true; };
187 PatchDistortionObjective obj(metric);
188 obj.set_active_patch(active);
189
190 expect_float_eq2(obj.compute_parameterization(utils::Point(0.5, 0.5)), utils::Point(0.5, 0.5));
191 expect_float_eq2(obj.compute_parameterization(utils::Point(0.6, 0.7)), utils::Point(0.6, 0.7));
192
193 auto pt1 = obj.compute_inverse_parameterization(utils::Point(0.5, 0.5));
194 expect_float_eq2(pt1, utils::Point(0.5, 0.5));
195 expect_float_eq2(obj.compute_parameterization(utils::Point(0.6, 0.7)), utils::Point(0.6, 0.7));
196 }
197
198 // TODO: test compute_parameterization and compute_inverse_parameterization
199
200 } // namespace impl
201 } // namespace middle_mesh
202 } // namespace stk