12 #include "libmesh/petsc_vector.h" 18 Real _namespaced_tol = 1e-6;
20 differenceBetweenTwoVectors(std::vector<Real>
a, std::vector<Real>
b)
22 std::transform(
a.cbegin(),
a.cend(),
b.cbegin(),
b.begin(), std::minus<>{});
23 return std::abs(std::accumulate(
b.begin(),
b.end(), 0.0));
26 errorCheckMsg(
const std::vector<Real> & optReporterBaseBounds,
27 const std::vector<Real> & optReporterTestBounds,
31 if (optReporterBaseBounds.size() != ndof || optReporterTestBounds.size() != ndof)
33 out +=
"There should be one bound per parameter read from the OptimizationDataBase object. ";
34 out +=
"\n Number of parameter values on OptimizationDataBase object: ";
35 out += std::to_string(ndof);
36 out +=
"\n Number of bounds values read from OptimizationDataBase object: ";
37 out += std::to_string(optReporterBaseBounds.size());
38 out +=
"\n Number of bounds values read from OptimizationReporterTest object: ";
39 out += std::to_string(optReporterTestBounds.size());
41 Real diff = differenceBetweenTwoVectors(optReporterBaseBounds, optReporterTestBounds);
42 if (diff > _namespaced_tol)
44 std::string
out(
"\n OptimizationDataBase object bounds: ");
45 for (
const auto & val : optReporterBaseBounds)
46 out += std::to_string(val) +
" ";
47 out +=
"\n OptimizationReporterTest object bounds: ";
48 for (
const auto & val : optReporterTestBounds)
49 out += std::to_string(val) +
" ";
62 "Testing that OptimizationReporter values can be set");
63 params.
addParam<std::vector<Real>>(
"expected_lower_bounds",
64 "Testing that OptimizationReporter lower bounds " 65 "can be set and used in the objective calculations");
66 params.
addParam<std::vector<Real>>(
"expected_upper_bounds",
67 "Testing that OptimizationReporter upper bounds " 68 "can be set and used in the objective calculations");
74 _my_comm(MPI_COMM_SELF),
75 _optSolverParameters(
std::make_unique<
libMesh::PetscVector<
Number>>(_my_comm))
83 mooseError(
"No optimization reporter object found.");
88 std::vector<Real> valuesToSetOnOptRepParams(
89 getParam<std::vector<Real>>(
"values_to_set_parameters_to"));
90 if (ndof != valuesToSetOnOptRepParams.size())
93 " and OptimizationReporterTest contains ",
94 valuesToSetOnOptRepParams.size(),
95 " parameters. They must be the same.");
97 std::vector<Real> lower_bounds(ndof);
98 std::vector<Real> upper_bounds(ndof);
105 std::vector<Real> expectedLowerBounds =
isParamValid(
"expected_lower_bounds")
106 ? getParam<std::vector<Real>>(
"expected_lower_bounds")
107 : std::vector<Real>(ndof, 0.0);
108 std::vector<Real> expectedUpperBounds =
isParamValid(
"expected_upper_bounds")
109 ? getParam<std::vector<Real>>(
"expected_upper_bounds")
110 : std::vector<Real>(ndof, 0.0);
112 std::string errorCheckLowerBounds(errorCheckMsg(expectedLowerBounds, lower_bounds, ndof));
113 if (!errorCheckLowerBounds.empty())
114 mooseError(
"Error in lower bounds: ", errorCheckLowerBounds);
116 std::string errorCheckUpperBounds(errorCheckMsg(expectedUpperBounds, upper_bounds, ndof));
117 if (!errorCheckUpperBounds.empty())
118 mooseError(
"Error in upper bounds: ", errorCheckUpperBounds);
125 std::vector<Real> valuesToSetOnOptRepParams(
126 getParam<std::vector<Real>>(
"values_to_set_parameters_to"));
128 for (
auto & val : valuesToSetOnOptRepParams)
static InputParameters validParams()
T & getUserObject(const std::string &name, unsigned int tid=0) const
static InputParameters validParams()
Real getLowerBound(dof_id_type i) const
bool hasUserObject(const std::string &name) const
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
Base class for optimization objects, implements routines for calculating misfit.
bool isParamValid(const std::string &name) const
std::unique_ptr< libMesh::PetscVector< Number > > _optSolverParameters
void setInitialCondition(libMesh::PetscVector< Number > ¶m)
Function to initialize petsc vectors from vpp data.
OptimizationReporterBase * _optReporter
const T & getParam(const std::string &name) const
A UserObject that tests the requesting of Reporter values that are actually correct.
OptimizationReporterTest(const InputParameters ¶ms)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
registerMooseObject("OptimizationTestApp", OptimizationReporterTest)
FEProblemBase & _fe_problem
IntRange< T > make_range(T beg, T end)
virtual void updateParameters(const libMesh::PetscVector< Number > &x)
Function to set parameters.
void mooseError(Args &&... args) const
Real getUpperBound(dof_id_type i) const
Upper and lower bounds for each parameter being controlled.
void initialSetup() override