https://mooseframework.inl.gov
Functions
BrentsMethodTest.C File Reference

Go to the source code of this file.

Functions

Real f (Real x)
 Test function for Brents method. More...
 
 TEST (BrentsMethod, bracket)
 
 TEST (BrentsMethod, root)
 

Function Documentation

◆ f()

Real f ( Real  x)

Test function for Brents method.

f(x) = log(1 + x) * tanh(x / 3) + x / 4 - 3 which has a root at 5.170302597.

Note: the implementation of Brents bracketing method restricts the bracketing interval to positive values

Definition at line 21 of file BrentsMethodTest.C.

Referenced by SolidMechanicsPlasticWeakPlaneShear::activeConstraints(), SolidMechanicsPlasticWeakPlaneTensile::activeConstraints(), SolidMechanicsPlasticMeanCapTC::activeConstraints(), SolidMechanicsPlasticTensileMulti::activeConstraints(), SolidMechanicsPlasticMohrCoulombMulti::activeConstraints(), SolidMechanicsPlasticModel::activeConstraints(), ComputeMultiPlasticityStress::applyKuhnTucker(), BrentsMethod::bracket(), MultiPlasticityRawComponentAssembler::buildActiveConstraints(), MultiPlasticityRawComponentAssembler::buildActiveConstraintsJoint(), MultiPlasticityRawComponentAssembler::buildActiveConstraintsRock(), ComputeMultiPlasticityStress::buildDumbOrder(), MultiPlasticityLinearSystem::calculateConstraints(), MultiPlasticityLinearSystem::calculateRHS(), ViewFactorBase::checkAndNormalizeViewFactor(), ComputeMultiPlasticityStress::checkKuhnTucker(), ChemicalReactionsApp__registerAll(), ChemicalReactionsTestApp__registerAll(), CombinedApp__registerAll(), CombinedTestApp__registerAll(), StiffenedGasTwoPhaseFluidProperties::compute_p_sat(), QuadSubChannel1PhaseProblem::computeBeta(), TriSubChannel1PhaseProblem::computeBeta(), ADComputeIncrementalShellStrain::computeGMatrix(), ComputeHomogenizedLagrangianStrainA::ComputeHomogenizedLagrangianStrainA(), ComputeHomogenizedLagrangianStrainS::ComputeHomogenizedLagrangianStrainS(), ComputeLagrangianStressPK1::computeQpCauchyStress(), ComputeSimoHughesJ2PlasticityStress::computeQpPK1Stress(), ADWallHeatTransferCoefficientGnielinskiMaterial::computeQpProperties(), ElectrochemicalSinteringMaterial::computeQpProperties(), GrandPotentialSinteringMaterial::computeQpProperties(), CNSFVHLLC::computeQpResidual(), PCNSFVHLLCBC::computeQpResidual(), CNSFVHLLCBC::computeQpResidual(), PCNSFVHLLC::computeQpResidual(), ContactApp__registerAll(), ContactTestApp__registerAll(), SodiumProperties::drho_dT(), ElectromagneticsApp__registerAll(), ElectromagneticsTestApp__registerAll(), StabilizationUtils::elementAverage(), MultiPlasticityLinearSystem::eliminateLinearDependence(), HSCoupler2D3DUserObject::evaluateTemperatureFunction(), ExternalPetscSolverApp__registerAll(), ExternalPetscSolverTestApp__registerAll(), FluidPropertiesApp__registerAll(), FluidPropertiesTestApp__registerAll(), formFunction(), formFunctionIW(), FormIFunction(), FourierNoise::FourierNoise(), FsiApp__registerAll(), FsiTestApp__registerAll(), FunctionalExpansionToolsApp__registerAll(), FunctionalExpansionToolsTestApp__registerAll(), GeochemistryApp__registerAll(), GeochemistryTestApp__registerAll(), OptimizeSolve::getTaoSolutionStatus(), HeatTransferApp__registerAll(), HeatTransferTestApp__registerAll(), HomogenizationConstraint::HomogenizationConstraint(), HomogenizedTotalLagrangianStressDivergenceA::HomogenizedTotalLagrangianStressDivergenceA(), HomogenizedTotalLagrangianStressDivergenceR::HomogenizedTotalLagrangianStressDivergenceR(), HomogenizedTotalLagrangianStressDivergenceS::HomogenizedTotalLagrangianStressDivergenceS(), Beta::incompleteBetaInv(), Gamma::incompleteGammaInv(), ParameterStudyAction::inferMultiAppMode(), LevelSetApp__registerAll(), LevelSetTestApp__registerAll(), ComputeMultiPlasticityStress::lineSearch(), MiscApp__registerAll(), MiscTestApp__registerAll(), ModulesApp__registerAll(), OptimizeSolve::monitor(), NavierStokesApp__registerAll(), NavierStokesTestApp__registerAll(), FluidPropertiesUtils::NewtonSolve2D(), NSFVDispersePhaseDragFunctorMaterial::NSFVDispersePhaseDragFunctorMaterial(), OptimizationApp__registerAll(), OptimizationTestApp__registerAll(), PorousFlowLineGeometry::parseNextLineReals(), RichardsPolyLineSink::parseNextLineReals(), PeacemanBorehole::parseNextLineReals(), PeridynamicsApp__registerAll(), PeridynamicsTestApp__registerAll(), PhaseFieldApp__registerAll(), PhaseFieldTestApp__registerAll(), PorousFlowApp__registerAll(), PorousFlowTestApp__registerAll(), LevelSetOlssonReinitialization::precomputeQpResidual(), NavierStokesLHDGAssemblyHelper::pressureVolumeResidual(), PorousFlowFluidStateFlash::rachfordRice(), RayTracingApp__registerAll(), RayTracingTestApp__registerAll(), RdgApp__registerAll(), RdgTestApp__registerAll(), ReactorApp__registerAll(), ReactorTestApp__registerAll(), SubChannelTestApp::registerAll(), NavierStokesUnitApp::registerAll(), ReactorUnitApp::registerAll(), ChemicalReactionsApp::registerAll(), FunctionalExpansionToolsApp::registerAll(), SolidPropertiesApp::registerAll(), FunctionalExpansionToolsTestApp::registerAll(), LevelSetApp::registerAll(), PorousFlowApp::registerAll(), RdgTestApp::registerAll(), PeridynamicsTestApp::registerAll(), NavierStokesApp::registerAll(), FluidPropertiesTestApp::registerAll(), MiscApp::registerAll(), ScalarTransportApp::registerAll(), OptimizationTestApp::registerAll(), FsiApp::registerAll(), XFEMTestApp::registerAll(), CombinedApp::registerAll(), XFEMApp::registerAll(), NavierStokesTestApp::registerAll(), ContactApp::registerAll(), PhaseFieldTestApp::registerAll(), OptimizationApp::registerAll(), ContactTestApp::registerAll(), SolidPropertiesTestApp::registerAll(), LevelSetTestApp::registerAll(), GeochemistryTestApp::registerAll(), GeochemistryApp::registerAll(), PeridynamicsApp::registerAll(), StochasticToolsApp::registerAll(), ThermalHydraulicsTestApp::registerAll(), PhaseFieldApp::registerAll(), HeatTransferApp::registerAll(), SubChannelApp::registerAll(), ScalarTransportTestApp::registerAll(), ReactorTestApp::registerAll(), ChemicalReactionsTestApp::registerAll(), PorousFlowTestApp::registerAll(), RdgApp::registerAll(), RayTracingApp::registerAll(), FsiTestApp::registerAll(), FluidPropertiesApp::registerAll(), ElectromagneticsApp::registerAll(), ReactorApp::registerAll(), StochasticToolsTestApp::registerAll(), RichardsTestApp::registerAll(), MiscTestApp::registerAll(), SolidMechanicsTestApp::registerAll(), ElectromagneticsTestApp::registerAll(), SolidMechanicsApp::registerAll(), HeatTransferTestApp::registerAll(), RayTracingTestApp::registerAll(), ExternalPetscSolverTestApp::registerAll(), CombinedTestApp::registerAll(), RichardsApp::registerAll(), ExternalPetscSolverApp::registerAll(), ThermalHydraulicsApp::registerAll(), ModulesApp::registerAll(), ModulesApp::registerAllObjects(), ComputeMultiPlasticityStress::residual2(), FiniteStrainPlasticMaterial::returnMap(), ComputeMultiPlasticityStress::returnMap(), SodiumProperties::rho(), RichardsTestApp__registerAll(), BrentsMethod::root(), ScalarTransportApp__registerAll(), ScalarTransportTestApp__registerAll(), OptimizeSolve::setTaoSolutionStatus(), ComputeMultiPlasticityStress::singleStep(), MultiParameterPlasticityStressUpdate::smoothAllQuantities(), SolidMechanicsApp__registerAll(), SolidMechanicsTestApp__registerAll(), SolidPropertiesApp__registerAll(), SolidPropertiesTestApp__registerAll(), StochasticToolsApp__registerAll(), StochasticToolsTestApp__registerAll(), SubChannelApp__registerAll(), SubChannelTestApp__registerAll(), TemperatureDependentHardeningStressUpdateTempl< is_ad >::TemperatureDependentHardeningStressUpdateTempl(), TEST(), TEST_F(), ThermalHydraulicsApp__registerAll(), ThermalHydraulicsTestApp__registerAll(), FourierNoise::value(), Water97FluidProperties::vaporTemperature_ad(), XFEMApp__registerAll(), XFEMTestApp__registerAll(), IdealRealGasMixtureFluidProperties::xs_prim_from_p_T(), MultiPlasticityRawComponentAssembler::yieldFunction(), SolidMechanicsPlasticMohrCoulombMulti::yieldFunctionEigvals(), SolidMechanicsPlasticTensileMulti::yieldFunctionV(), SolidMechanicsPlasticMohrCoulombMulti::yieldFunctionV(), and SolidMechanicsPlasticModel::yieldFunctionV().

22 {
23  return std::log(1.0 + x) * std::tanh(x / 3.0) + x / 4.0 - 3;
24 }
const std::vector< double > x

◆ TEST() [1/2]

TEST ( BrentsMethod  ,
bracket   
)

Definition at line 26 of file BrentsMethodTest.C.

27 {
28  // Initial guess for bracketing interval does not bracket root
29  Real x1 = 0.5;
30  Real x2 = 1.0;
31 
32  auto func = [](Real x) { return f(x); };
33 
34  // Call bracket to determine the bracketing interval
35  BrentsMethod::bracket(func, x1, x2);
36 
37  // The product of the function f(x) at the bracketing interval (x1, x2) must
38  // be negative to bracket a root
39  EXPECT_LT(f(x1) * f(x2), 0.0);
40 
41  // Test that a warning is thrown if the initial guesses are equal
42  try
43  {
44  // Trigger identical initial guess error
45  BrentsMethod::bracket(func, x1, x1);
46  FAIL() << "missing expected exception";
47  }
48  catch (const std::exception & e)
49  {
50  std::string msg(e.what());
51  ASSERT_TRUE(msg.find("Bad initial range (0) used in BrentsMethod::bracket") !=
52  std::string::npos)
53  << "failed with unexpected error: " << msg;
54  }
55 
56  // Test that a warning is thrown if no bracketing interval is found after 50 iterations.
57  try
58  {
59  // Trigger no bracketing interval warning by adding 4 to f(x), whereby no real root exists
60  auto func2 = [](Real x) { return f(x) + 4.0; };
61 
62  BrentsMethod::bracket(func2, x1, x2);
63  FAIL() << "missing expected exception";
64  }
65  catch (const std::exception & e)
66  {
67  std::string msg(e.what());
68  ASSERT_TRUE(
69  msg.find("No bracketing interval found by BrentsMethod::bracket after 50 iterations") !=
70  std::string::npos)
71  << "failed with unexpected error: " << msg;
72  }
73 }
const std::vector< double > x
Real f(Real x)
Test function for Brents method.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void bracket(std::function< Real(Real)> const &f, Real &x1, Real &x2)
Function to bracket a root of a given function.
Definition: BrentsMethod.C:17

◆ TEST() [2/2]

TEST ( BrentsMethod  ,
root   
)

Definition at line 75 of file BrentsMethodTest.C.

76 {
77  // Bracketing interval that does bracket root
78  Real x1 = 0.5;
79  Real x2 = 10.0;
80 
81  auto func = [](Real x) { return f(x); };
82 
83  // Check that the root is 5.170302597
84  EXPECT_NEAR(5.170302597, BrentsMethod::root(func, x1, x2), 1e-8);
85 
86  // Test that a warning is thrown if the supplied interval does not bracket the root
87  try
88  {
89  // Trigger no bracketing interval error
90  x2 = 1.0;
91  BrentsMethod::root(func, x1, x2);
92  FAIL() << "missing expected exception";
93  }
94  catch (const std::exception & e)
95  {
96  std::string msg(e.what());
97  ASSERT_TRUE(msg.find("Root must be bracketed in BrentsMethod::root") != std::string::npos)
98  << "failed with unexpected error: " << msg;
99  }
100 }
const std::vector< double > x
Real f(Real x)
Test function for Brents method.
Real root(std::function< Real(Real)> const &f, Real x1, Real x2, Real tol=1.0e-12)
Finds the root of a function using Brent&#39;s method.
Definition: BrentsMethod.C:66
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real