[P]arallel [Hi]gh-order [Li]brary for [P]DEs  Latest
Parallel High-Order Library for PDEs through hp-adaptive Discontinuous Galerkin methods
nearest_neighbors.cpp
1 #include "nearest_neighbors.h"
2 #include <iostream>
3 #include <algorithm>
4 #include <numeric>
5 
6 namespace PHiLiP {
7 namespace ProperOrthogonalDecomposition {
8 
10  : snapshot_params()
11  , scaler()
12  , snapshots()
13 {}
14 
15 void NearestNeighbors::update_snapshots(const MatrixXd &snapshot_parameters, dealii::LinearAlgebra::distributed::Vector<double> snapshot){
16  snapshot_params = snapshot_parameters;
17  snapshots.emplace_back(snapshot);
18  if(snapshots.size() > 1){
20  }
21 }
22 
24 
25  MatrixXd midpoints(0, snapshot_params.cols());
26 
27  for(auto snapshot : scaled_snapshot_params.rowwise()){
28 
29  VectorXd distances = (scaled_snapshot_params.rowwise() - snapshot).rowwise().squaredNorm();
30 
31  std::vector<int> index(distances.size());
32  std::iota(index.begin(), index.end(), 0);
33 
34  std::sort(index.begin(), index.end(),
35  [&](const int& a, const int& b) {
36  return distances[a] < distances[b];
37  });
38 
39  for (int i = 1 ; i < snapshot.cols() + 2 ; i++) { //Ignore zeroth index as this would be the same point
40  midpoints.conservativeResize(midpoints.rows()+1, midpoints.cols());
41  midpoints.row(midpoints.rows()-1) = (snapshot_params.row(index[i])+scaler.inverse_transform(snapshot))/2;
42  }
43  }
44  return midpoints;
45 }
46 
47 MatrixXd NearestNeighbors::kNearestNeighborsMidpoint(const RowVectorXd& point){
48  RowVectorXd scaled_point = scaler.transform(point);
49  VectorXd distances = (scaled_snapshot_params.rowwise() - scaled_point).rowwise().squaredNorm();
50 
51  std::vector<int> index(distances.size());
52  std::iota(index.begin(), index.end(), 0);
53 
54  std::sort(index.begin(), index.end(),
55  [&](const int& a, const int& b) {
56  return distances[a] < distances[b];
57  });
58 
59 
60  MatrixXd midpoints(point.cols()+1, point.cols());
61  for (int i = 0 ; i < point.cols()+1 ; i++) {
62  midpoints.row(i) = (snapshot_params.row(index[i+1])+point)/2; //i+1 to ignore zeroth index as this would be the same point
63  }
64 
65  return midpoints;
66 }
67 
68 dealii::LinearAlgebra::distributed::Vector<double> NearestNeighbors::nearestNeighborMidpointSolution(const RowVectorXd& point){
69  RowVectorXd scaled_point = scaler.transform(point);
70  VectorXd distances = (scaled_snapshot_params.rowwise() - scaled_point).rowwise().squaredNorm();
71 
72  std::vector<int> index(distances.size());
73  std::iota(index.begin(), index.end(), 0);
74 
75  std::sort(index.begin(), index.end(),
76  [&](const int& a, const int& b) {
77  return distances[a] < distances[b];
78  });
79 
80  dealii::LinearAlgebra::distributed::Vector<double> interpolated_solution = snapshots[index[0]];
81  interpolated_solution += snapshots[index[1]];
82  interpolated_solution /= 2;
83  return interpolated_solution;
84 }
85 
86 }
87 }
void update_snapshots(const MatrixXd &snapshot_parameters, dealii::LinearAlgebra::distributed::Vector< double > snapshot)
Add snapshot.
Files for the baseline physics.
Definition: ADTypes.hpp:10
MatrixXd scaled_snapshot_params
Scaled snapshot parameters.
std::vector< dealii::LinearAlgebra::distributed::Vector< double > > snapshots
Vector containing all snapshots.
MatrixXd kPairwiseNearestNeighborsMidpoint()
Find midpoint of all snapshot locations.
MatrixXd fit_transform(const MatrixXd &snapshot_parameters)
Fit and transform data.
MatrixXd kNearestNeighborsMidpoint(const RowVectorXd &point)
Given a point, returns midpoint between point and k nearest snapshots, where k is 1+num_parameters...
MatrixXd inverse_transform(const MatrixXd &snapshot_parameters)
Unscale data.
MatrixXd transform(const MatrixXd &snapshot_parameters)
Transform data to previously fitted dataset.