Skip to content

Commit 6bc78f4

Browse files
committed
Add nonlinearBounded unit test
1 parent b5b3f1d commit 6bc78f4

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

unit/src/NestedSolveTest.C

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "libmesh/vector_value.h"
1616

17+
#include <utility>
18+
1719
TEST(NestedSolve, FixedSize)
1820
{
1921
auto compute = [&](const NestedSolve::Value<2> & guess,
@@ -200,6 +202,50 @@ TEST(NestedSolve, ScalarPowellTight)
200202
EXPECT_NEAR(solution, 2, 1e-6);
201203
}
202204

205+
TEST(NestedSolve, ScalarBounded)
206+
{
207+
auto computeResidual = [&](const Real & guess, Real & residual) { residual = guess - 2.0; };
208+
auto computeJacobian = [&](const Real & /*guess*/, Real & jacobian) { jacobian = 1.0; };
209+
210+
bool bounds_checked = false;
211+
auto computeBounds = [&]()
212+
{
213+
bounds_checked = true;
214+
return std::make_pair(1.0, 3.0);
215+
};
216+
217+
NestedSolve solver;
218+
Real solution = 1.5;
219+
solver.nonlinearBounded(solution, computeResidual, computeJacobian, computeBounds);
220+
221+
EXPECT_TRUE(bounds_checked);
222+
EXPECT_NEAR(solution, 2.0, 1e-10);
223+
EXPECT_GE(solution, 1.0);
224+
EXPECT_LE(solution, 3.0);
225+
}
226+
227+
TEST(NestedSolve, ScalarBoundedAD)
228+
{
229+
auto computeResidual = [&](const ADReal & guess, ADReal & residual) { residual = guess - 2.0; };
230+
auto computeJacobian = [&](const ADReal & /*guess*/, ADReal & jacobian) { jacobian = 1.0; };
231+
232+
bool bounds_checked = false;
233+
auto computeBounds = [&]()
234+
{
235+
bounds_checked = true;
236+
return std::make_pair(1.0, 3.0);
237+
};
238+
239+
ADNestedSolve solver;
240+
ADReal solution = 1.5;
241+
solver.nonlinearBounded(solution, computeResidual, computeJacobian, computeBounds);
242+
243+
EXPECT_TRUE(bounds_checked);
244+
EXPECT_NEAR(MetaPhysicL::raw_value(solution), 2.0, 1e-10);
245+
EXPECT_GE(MetaPhysicL::raw_value(solution), 1.0);
246+
EXPECT_LE(MetaPhysicL::raw_value(solution), 3.0);
247+
}
248+
203249
TEST(NestedSolve, PlacementNew)
204250
{
205251
Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> em(3, 3);

0 commit comments

Comments
 (0)