https://mooseframework.inl.gov
NearestPointSurrogate.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "NearestPointSurrogate.h"
11 
12 registerMooseObject("StochasticToolsApp", NearestPointSurrogate);
13 
16 {
18  params.addClassDescription("Surrogate that evaluates the value from the nearest point from data "
19  "in [NearestPointTrainer.md]");
20  return params;
21 }
22 
24  : SurrogateModel(parameters),
25  _sample_points(getModelData<std::vector<std::vector<Real>>>("_sample_points")),
26  _sample_results(getModelData<std::vector<std::vector<Real>>>("_sample_results"))
27 {
28 }
29 
30 Real
31 NearestPointSurrogate::evaluate(const std::vector<Real> & x) const
32 {
33  // Check whether input point has same dimensionality as training data
34  mooseAssert(_sample_points.size() == x.size(),
35  "Input point does not match dimensionality of training data.");
36 
38 }
39 
40 void
41 NearestPointSurrogate::evaluate(const std::vector<Real> & x, std::vector<Real> & y) const
42 {
43  mooseAssert(_sample_points.size() == x.size(),
44  "Input point does not match dimensionality of training data.");
45 
46  y.assign(_sample_results.size(), 0.0);
47 
48  unsigned int idx = findNearestPoint(x);
49 
50  for (const auto & r : index_range(y))
51  y[r] = _sample_results[r][idx];
52 }
53 
54 unsigned int
55 NearestPointSurrogate::findNearestPoint(const std::vector<Real> & x) const
56 {
57  unsigned int idx = 0;
58 
59  // Container of current minimum distance during training sample loop
60  Real dist_min = std::numeric_limits<Real>::max();
61 
62  for (dof_id_type p = 0; p < _sample_points[0].size(); ++p)
63  {
64  // Sum over the distance of each point dimension
65  Real dist = 0;
66  for (unsigned int i = 0; i < x.size(); ++i)
67  {
68  Real diff = (x[i] - _sample_points[i][p]);
69  dist += diff * diff;
70  }
71 
72  // Check if this training point distance is smaller than the current minimum
73  if (dist < dist_min)
74  {
75  idx = p;
76  dist_min = dist;
77  }
78  }
79  return idx;
80 }
unsigned int findNearestPoint(const std::vector< Real > &x) const
virtual Real evaluate(const std::vector< Real > &x) const override
Evaluate surrogate model given a row of parameters.
static InputParameters validParams()
const std::vector< double > y
NearestPointSurrogate(const InputParameters &parameters)
static InputParameters validParams()
const std::vector< double > x
const std::vector< std::vector< Real > > & _sample_points
Array containing sample points.
registerMooseObject("StochasticToolsApp", NearestPointSurrogate)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const std::vector< std::vector< Real > > & _sample_results
Array containing results.
void addClassDescription(const std::string &doc_string)
auto index_range(const T &sizable)
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)
uint8_t dof_id_type