https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
11
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
30Real
31NearestPointSurrogate::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
40void
41NearestPointSurrogate::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
54unsigned int
55NearestPointSurrogate::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}
const std::vector< double > y
const std::vector< double > x
const Real p
registerMooseObject("StochasticToolsApp", NearestPointSurrogate)
void addClassDescription(const std::string &doc_string)
const std::vector< std::vector< Real > > & _sample_points
Array containing sample points.
NearestPointSurrogate(const InputParameters &parameters)
const std::vector< std::vector< Real > > & _sample_results
Array containing results.
virtual Real evaluate(const std::vector< Real > &x) const override
Evaluate surrogate model given a row of parameters.
unsigned int findNearestPoint(const std::vector< Real > &x) const
static InputParameters validParams()
static InputParameters validParams()