22 params.
addRequiredParam<SamplerName>(
"sampler",
"Morris sampler used to generate samples.");
23 params.
addParam<std::vector<VectorPostprocessorName>>(
24 "vectorpostprocessors",
25 "List of VectorPostprocessor(s) to utilized for statistic computations.");
26 params.
addParam<std::vector<ReporterName>>(
27 "reporters", {},
"List of Reporter values to utilized for statistic computations.");
33 "A vector of confidence levels to consider, values must be in (0, 1).");
34 params.
addParam<
unsigned int>(
"ci_replicates",
36 "The number of replicates to use when computing confidence level " 37 "intervals. This is basically the number of times the statistics " 38 "are recomputed with a random selection of indices.");
39 params.
addParam<
unsigned int>(
"ci_seed",
41 "The random number generator seed used for creating replicates " 42 "while computing confidence level intervals.");
48 _sampler(getSampler(
"sampler")),
49 _ci_levels(getParam<
std::vector<
Real>>(
"ci_levels")),
50 _ci_replicates(getParam<unsigned
int>(
"ci_replicates")),
51 _ci_seed(getParam<unsigned
int>(
"ci_seed")),
54 if (!dynamic_cast<MorrisSampler *>(&
_sampler))
55 paramError(
"sampler",
"Computing Morris sensitivities requires the use of a Morris sampler.");
61 paramError(
"ci_levels",
"The supplied levels must be greater than zero.");
63 paramError(
"ci_levels",
"The supplied levels must be less than 1.0");
67 (
getParam<std::vector<ReporterName>>(
"reporters").empty() &&
68 getParam<std::vector<VectorPostprocessorName>>(
"vectorpostprocessors").empty()))
70 "The 'vectorpostprocessors' and/or 'reporters' parameters must be defined and non-empty.");
82 std::vector<std::string> unsupported_types;
83 for (
const auto & r_name :
getParam<std::vector<ReporterName>>(
"reporters"))
86 declareValueHelper<Real>(r_name);
90 unsupported_types.push_back(r_name.getCombinedName());
93 if (!unsupported_types.empty())
95 "The following reporter value(s) do not have a type supported by the " 102 for (
const auto & vpp_name :
103 getParam<std::vector<VectorPostprocessorName>>(
"vectorpostprocessors"))
104 for (
const auto & vec_name :
106 declareValueHelper<Real>(
ReporterName(vpp_name, vec_name));
115 json[
"confidence_intervals"] = {{
"method",
"percentile"},
125 template <
typename DataType>
129 const auto & data = getReporterValueByName<std::vector<DataType>>(r_name);
146 template <
typename DataType>
151 const std::vector<DataType> & data)
156 MultiMooseEnum items(
"mean meanabs stddev",
"mean meanabs stddev",
true);
157 _mu_calc = StochasticTools::makeCalculator<std::vector<DataType>, DataType>(items[0], other);
158 _mustar_calc = StochasticTools::makeCalculator<std::vector<DataType>, DataType>(items[1], other);
159 _sig_calc = StochasticTools::makeCalculator<std::vector<DataType>, DataType>(items[2], other);
164 auto & mu_star = this->
_state.
value()[
"mu_star"].first;
170 template <
typename DataType>
175 const std::vector<DataType> & data,
177 const std::vector<Real> & ci_levels,
178 unsigned int ci_replicates,
179 unsigned int ci_seed)
182 _ci_mu_calc = StochasticTools::makeBootstrapCalculator<std::vector<DataType>, DataType>(
183 ci_method, other, ci_levels, ci_replicates, ci_seed, *
_mu_calc);
184 _ci_mustar_calc = StochasticTools::makeBootstrapCalculator<std::vector<DataType>, DataType>(
185 ci_method, other, ci_levels, ci_replicates, ci_seed, *
_mustar_calc);
186 _ci_sig_calc = StochasticTools::makeBootstrapCalculator<std::vector<DataType>, DataType>(
187 ci_method, other, ci_levels, ci_replicates, ci_seed, *
_sig_calc);
192 auto & mu_star = this->
_state.
value()[
"mu_star"].second;
198 template <
typename DataType>
203 if (_data.size() == _sampler.getNumberOfRows())
204 offset = _sampler.getLocalRowBegin();
205 else if (_data.size() == _sampler.getNumberOfLocalRows())
208 mooseError(
"Data size inconsistency. Expected data vector to have ",
209 _sampler.getNumberOfLocalRows(),
211 _sampler.getNumberOfRows(),
212 " (global) elements, but actually has ",
214 " elements. Are you using the same sampler?");
217 if (_sampler.getNumberOfLocalRows() % (nc + 1) > 0)
219 "Sampler does not have an appropriate number of rows. Are you using a Morris sampler?");
221 std::vector<std::vector<DataType>> elem_effects(
222 nc, std::vector<DataType>(_sampler.getNumberOfLocalRows() / (nc + 1)));
224 std::vector<DataType>
y(nc + 1);
226 for (
dof_id_type p = 0; p < _sampler.getNumberOfLocalRows(); ++p)
230 const std::vector<Real> row = _sampler.getNextLocalRow();
231 for (
unsigned int k = 0;
k < nc; ++
k)
233 y[tind] = _data[p + offset];
237 const std::vector<DataType> ee = computeElementaryEffects(
x,
y);
238 for (
unsigned int k = 0;
k < nc; ++
k)
239 elem_effects[
k][traj] = ee[
k];
243 auto &
mu = this->_state.value()[
"mu"];
244 auto & mustar = this->_state.value()[
"mu_star"];
245 auto & sig = this->_state.value()[
"sigma"];
246 for (
unsigned int k = 0;
k < nc; ++
k)
248 mu.first[
k] = _mu_calc->compute(elem_effects[
k],
true);
249 mustar.first[
k] = _mustar_calc->compute(elem_effects[
k],
true);
250 sig.first[
k] = _sig_calc->compute(elem_effects[
k],
true);
253 mu.second[
k] = _ci_mu_calc->compute(elem_effects[
k],
true);
255 mustar.second[
k] = _ci_mustar_calc->compute(elem_effects[
k],
true);
257 sig.second[
k] = _ci_sig_calc->compute(elem_effects[
k],
true);
264 const std::vector<Real> &
y)
const 266 const auto k =
y.size() - 1;
269 for (
unsigned int j = 0;
j <
k; ++
j)
270 dy(
j) =
y[
j + 1] -
y[
j];
273 return std::vector<Real>(u.data(), u.data() + u.size());
277 std::vector<std::vector<Real>>
281 const auto k =
y.size() - 1;
283 const auto solver = dx.fullPivLu();
285 std::vector<std::vector<Real>> ee(
k, std::vector<Real>(
y[0].size()));
286 for (
unsigned int i = 0; i <
y[0].size(); ++i)
288 for (
unsigned int j = 0;
j <
k; ++
j)
289 dy(
j) =
y[
j + 1][i] -
y[
j][i];
291 for (
unsigned int j = 0;
j <
k; ++
j)
297 template void MorrisReporter::declareValueHelper<Real>(
const ReporterName & r_name);
299 template void MorrisReporter::declareValueHelper<std::vector<Real>>(
const ReporterName & r_name);
std::string join(Iterator begin, Iterator end, const std::string &delimiter)
T & declareValueByName(const ReporterValueName &value_name, Args &&... args)
virtual void finalize() override
void mooseError(Args &&... args)
const ReporterMode REPORTER_MODE_ROOT
static InputParameters validParams()
const unsigned int & _ci_seed
Random seed for producing CI replicates.
ReporterState< MorrisState< DataType > > & _state
std::unique_ptr< StochasticTools::BootstrapCalculator< std::vector< DataType >, DataType > > _ci_mustar_calc
void declareValueHelper(const ReporterName &r_name)
Helper for adding Morris reporter values.
const std::vector< double > y
static InputParameters validParams()
const std::vector< Real > & _ci_levels
CI levels to be computed.
std::vector< DataType > computeElementaryEffects(const RealEigenMatrix &x, const std::vector< DataType > &y) const
Function for computing elementary effects for a single set of trajectories This is meant to be specia...
virtual void store(nlohmann::json &json) const
bool isParamValid(const std::string &name) const
const unsigned int & _ci_replicates
Number of CI replicates to use in Bootstrap methods.
const std::vector< double > x
virtual void initialize() override final
Sampler & _sampler
Morris sampler (don't need any specific functions, but should be this type)
static const std::string mu
std::map< std::string, std::pair< std::vector< DataType >, std::vector< std::vector< DataType > >> > MorrisState
const T & getParam(const std::string &name) const
const VectorPostprocessor & getVectorPostprocessorObjectByName(const std::string &object_name, const THREAD_ID tid=0) const
void paramError(const std::string ¶m, Args... args) const
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic > RealEigenMatrix
const std::string & getObjectName() const
dof_id_type getNumberOfRows() const
MorrisReporterContext(const libMesh::ParallelObject &other, const MooseObject &producer, ReporterState< MorrisState< DataType >> &state, Sampler &sampler, const std::vector< DataType > &data)
std::unique_ptr< StochasticTools::Calculator< std::vector< DataType >, DataType > > _mu_calc
Storage for the Calculator object for the desired stat, this is created in constructor.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
bool _initialized
Whether or not initialize() has been called for reporter value declaration.
FEProblemBase & _fe_problem
void mooseError(Args &&... args) const
registerMooseObject("StochasticToolsApp", MorrisReporter)
const InputParameters & parameters() const
Sampler & _sampler
Morris sampler (don't need any specific functions, but should be this type)
std::unique_ptr< StochasticTools::Calculator< std::vector< DataType >, DataType > > _sig_calc
T & value(const std::size_t time_index=0)
static const std::complex< double > j(0, 1)
Complex number "j" (also known as "i")
Eigen::Matrix< Real, Eigen::Dynamic, 1 > RealEigenVector
MorrisReporter(const InputParameters ¶meters)
const std::string & getValueName() const
static const std::string k
const std::set< std::string > & getVectorNames() const
void ErrorVector unsigned int
dof_id_type getNumberOfCols() const
virtual void store(nlohmann::json &json) const override
bool hasReporterValueByName(const ReporterName &reporter_name) const
std::unique_ptr< StochasticTools::BootstrapCalculator< std::vector< DataType >, DataType > > _ci_sig_calc
std::unique_ptr< StochasticTools::BootstrapCalculator< std::vector< DataType >, DataType > > _ci_mu_calc
Storage for the BootstrapCalculator for the desired confidence interval calculations (optional) ...
std::unique_ptr< StochasticTools::Calculator< std::vector< DataType >, DataType > > _mustar_calc