| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Trilinos: An Object-Oriented Solver Framework |
| 4 |
// |
| 5 |
// Copyright 2001-2024 NTESS and the Trilinos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
// 2016/09/23 |
| 11 |
|
| 12 |
#ifndef _TRILINOS_COUPLINGS_FENL_VPS_H_ |
| 13 |
#define _TRILINOS_COUPLINGS_FENL_VPS_H_ |
| 14 |
|
| 15 |
#include <cmath> |
| 16 |
#include <vector> |
| 17 |
#include <list> |
| 18 |
#include <utility> |
| 19 |
#include <stack> |
| 20 |
#include <map> |
| 21 |
#include <set> |
| 22 |
#include <algorithm> |
| 23 |
#include <iterator> |
| 24 |
#include <stdexcept> |
| 25 |
#include <cstddef> |
| 26 |
#include <cstdlib> |
| 27 |
#include <limits> |
| 28 |
|
| 29 |
#include <iostream> |
| 30 |
#include <fstream> |
| 31 |
#include <sstream> |
| 32 |
#include <string.h> |
| 33 |
|
| 34 |
#include <time.h> |
| 35 |
|
| 36 |
#include <cfloat> // ETP |
| 37 |
|
| 38 |
class VPS |
| 39 |
{ |
| 40 |
public: |
| 41 |
|
| 42 |
const double DST_TOL = 1E-10; |
| 43 |
const double PI = 3.141592653589793; |
| 44 |
|
| 45 |
VPS(); |
| 46 |
|
| 47 |
~VPS(); |
| 48 |
|
| 49 |
int clear_memory(); |
| 50 |
|
| 51 |
|
| 52 |
enum surrogate_method{ Interpolation, Regression, Integration }; |
| 53 |
|
| 54 |
enum surrogate_basis{ monomials, legendre, chebyshev, radial }; |
| 55 |
|
| 56 |
int get_initial_well_spaced_samples(size_t num_dim, double* xmin, double* xmax, size_t num_samples, double** x); |
| 57 |
|
| 58 |
int build_surrogate(size_t num_dim, |
| 59 |
double* xmin, double* xmax, |
| 60 |
size_t num_functions, |
| 61 |
surrogate_method method, |
| 62 |
surrogate_basis basis, size_t desired_order, |
| 63 |
size_t budget, size_t num_samples, |
| 64 |
double** x, |
| 65 |
double** f, |
| 66 |
double*** g, |
| 67 |
double**** h); |
| 68 |
|
| 69 |
int evaluate_surrogate(double* x, double* fs); |
| 70 |
|
| 71 |
int evaluate_surrogate(size_t cell_index, double* x, double* fs); |
| 72 |
|
| 73 |
int suggest_new_sample(double* x, double &r, double &err_est); |
| 74 |
|
| 75 |
int get_ensemble(size_t num_ensemble_points, double** ensemble_points, |
| 76 |
int proc_rank = 0); |
| 77 |
|
| 78 |
int get_stats(size_t num_mc_points, size_t function_index, double& mean, double& var); |
| 79 |
|
| 80 |
private: |
| 81 |
|
| 82 |
int sample_voronoi_vertex(size_t seed_index, double* xmin, double* xmax, double diag, double* v); |
| 83 |
|
| 84 |
int sample_voronoi_facet(size_t seed_index, double* xmin, double* xmax, double diag, double* v); |
| 85 |
|
| 86 |
int get_normal_component(size_t num_dim, size_t num_basis, double** basis, double* vect, double &norm); |
| 87 |
|
| 88 |
bool trim_spoke(size_t num_dim, double* xst, double* xend, double* p, double* q); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 93 |
/////////// RNG Methods ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 94 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 95 |
void initiate_random_number_generator(unsigned long x); |
| 96 |
double generate_a_random_number(); |
| 97 |
|
| 98 |
int sample_uniformly_from_unit_sphere(double* dart, size_t num_dim); |
| 99 |
|
| 100 |
//////////////////////////////////////////////////////////////////////////////// |
| 101 |
/////////// RNG Variables ///////////////////////////////////////////////////// |
| 102 |
//////////////////////////////////////////////////////////////////////////////// |
| 103 |
double Q[1220]; |
| 104 |
int indx; |
| 105 |
double cc; |
| 106 |
double c; /* current CSWB */ |
| 107 |
double zc; /* current SWB `borrow` */ |
| 108 |
double zx; /* SWB seed1 */ |
| 109 |
double zy; /* SWB seed2 */ |
| 110 |
size_t qlen;/* length of Q array */ |
| 111 |
|
| 112 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 113 |
/////////// kd-tree Methods ///////////////////////////////////////////////////////////////////////////////////////////// |
| 114 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 115 |
|
| 116 |
int kd_tree_build_balanced(); |
| 117 |
|
| 118 |
int kd_tree_balance_quicksort(size_t target_pos, size_t left, size_t right, size_t active_dim, size_t* tree_nodes_sorted); |
| 119 |
|
| 120 |
int kd_tree_quicksort_adjust_target_position(size_t target_pos, size_t left, size_t right, size_t active_dim, size_t* tree_nodes_sorted); |
| 121 |
|
| 122 |
int kd_tree_add_point(size_t point_index); |
| 123 |
|
| 124 |
int kd_tree_get_seeds_in_sphere(double* x, // Sphere center |
| 125 |
double r, // Sphere radius |
| 126 |
size_t d_index, size_t node_index, // indices to traverse the kd-tree |
| 127 |
size_t &num_points_in_sphere, size_t* &points_in_sphere, // number of points in sphere and their indices |
| 128 |
size_t &capacity // Size of points in sphere array |
| 129 |
); |
| 130 |
|
| 131 |
int kd_tree_get_closest_seed(size_t seed_index, // seed index |
| 132 |
size_t d_index, size_t node_index, // indices to traverse the kd-tree |
| 133 |
size_t &closest_seed, double &closest_distance // index of closest seed and distance from it |
| 134 |
); |
| 135 |
|
| 136 |
int kd_tree_get_closest_seed(double* x, // a point in space |
| 137 |
size_t d_index, size_t node_index, // indices to traverse the kd-tree |
| 138 |
size_t &closest_seed, double &closest_distance // index of closest seed and distance from it |
| 139 |
); |
| 140 |
|
| 141 |
int kd_tree_get_closest_seed(double* x, // a point in space |
| 142 |
size_t d_index, size_t node_index, // indices to traverse the kd-tree |
| 143 |
size_t num_exculded_seeds, size_t* exculded_seeds, // Number of excluded seeds and their indices |
| 144 |
size_t &closest_seed, double &closest_distance // index of closest seed and distance from it |
| 145 |
); |
| 146 |
|
| 147 |
int get_closest_seed_tree(size_t seed_index, size_t &closest_seed, double &closest_distance); |
| 148 |
|
| 149 |
int get_closest_seed_tree(double* x, size_t &closest_seed, double &closest_distance); |
| 150 |
|
| 151 |
int get_closest_seed_tree(double* x, size_t num_exculded_seeds, size_t* exculded_seeds, size_t &closest_seed, double &closest_distance); |
| 152 |
|
| 153 |
int get_seeds_in_sphere_tree(double* x, double r, size_t &num_points_in_sphere, size_t* &points_in_sphere, size_t &capacity); |
| 154 |
|
| 155 |
int get_closest_seed_brute(double* x, size_t num_significant_neighbors, size_t* significant_neighbors, size_t num_exculded_seeds, size_t* exculded_seeds, size_t &closest_seed); |
| 156 |
|
| 157 |
|
| 158 |
//////////////////////////////////////////////////////////////////////////////// |
| 159 |
/////////// kd-tree Variables ///////////////////////////////////////////////// |
| 160 |
//////////////////////////////////////////////////////////////////////////////// |
| 161 |
size_t _tree_origin; // index of tree root |
| 162 |
size_t _tree_max_height; // Max height of a tree branch |
| 163 |
size_t* _tree_left; // left pointer of a tree node |
| 164 |
size_t* _tree_right; // right pointer of a tree node |
| 165 |
|
| 166 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 167 |
/////////// General Methods ////////////////////////////////////////////////////////////////////////////////////////////// |
| 168 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 169 |
|
| 170 |
int quicksort(size_t* I, size_t left, size_t right); |
| 171 |
|
| 172 |
int quicksort(double* V, size_t* I, size_t left, size_t right); |
| 173 |
|
| 174 |
bool find_binary(size_t entry, size_t* I, size_t left, size_t right); |
| 175 |
|
| 176 |
bool find_brute(size_t entry, size_t* I, size_t num_entries); |
| 177 |
|
| 178 |
int add_entry(size_t entry, size_t &num_entries, size_t* &I, size_t &capacity); |
| 179 |
|
| 180 |
size_t retrieve_num_permutations(size_t num_dim, size_t upper_bound, bool force_sum_constraint, size_t sum_constraint); |
| 181 |
|
| 182 |
int retrieve_permutations(size_t &num_perm, size_t** &perm, size_t num_dim, size_t upper_bound, bool force_sum_constraint, size_t sum_constraint); |
| 183 |
|
| 184 |
void plot_graph(std::string file_name, size_t num_points, double* x, size_t num_functions, double** f); |
| 185 |
|
| 186 |
void plot_polynomial(std::string file_name, size_t num_basis, double* c, double xmin, double xmax, size_t num_points, double* px, double* py); |
| 187 |
|
| 188 |
void plot_piecewise_polynomial(std::string file_name, size_t num_pieces, double* pmin, double* pmax, size_t* num_basis, double** c, size_t num_points, double* px, double* py); |
| 189 |
|
| 190 |
void plot_FourierExpansion(std::string file_name, size_t num_basis, double xmin, double xmax, double* a, double* b, size_t num_points, double* px, double* py, size_t* num_cbasis, double** c); |
| 191 |
|
| 192 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 193 |
/////////// VPS General Methods ////////////////////////////////////////////////////////////////////////////////////////// |
| 194 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 195 |
|
| 196 |
int find_domain_diagonal(); |
| 197 |
|
| 198 |
// Plot Delaunay Mesh in a postscript |
| 199 |
void plot_delaunay_graph(const std::string outFile); |
| 200 |
|
| 201 |
// Plot 2d Function |
| 202 |
void plot_vps_surrogate(std::string file_name, size_t function_index, size_t num_contours, bool plot_graph); |
| 203 |
|
| 204 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 205 |
/////////// Delaunay Graph Methods /////////////////////////////////////////////////////////////////////////////////////// |
| 206 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 207 |
|
| 208 |
int construct_delaunay_graph(); |
| 209 |
|
| 210 |
int update_delaunay_graph(size_t seed_index); |
| 211 |
|
| 212 |
int sample_voronoi_vertex(double* v); |
| 213 |
|
| 214 |
int connect_seeds(size_t seed_i, size_t seed_j); |
| 215 |
|
| 216 |
int disconnect_seeds(size_t seed_i, size_t seed_j); |
| 217 |
|
| 218 |
int get_num_seed_neighbors(size_t seed_index, size_t &num_seed_neighbors); |
| 219 |
|
| 220 |
//////////////////////////////////////////////////////////////////////////////// |
| 221 |
/////////// Delaunay Graph Variables ////////////////////////////////////////// |
| 222 |
//////////////////////////////////////////////////////////////////////////////// |
| 223 |
|
| 224 |
size_t** _seed_neighbors; // indices of delaunay neighbors around a given seed |
| 225 |
bool*** _seed_disc_neighbors; // boolean to indicate discontinuous neighbors |
| 226 |
double** _seed_box; // A box around every seed |
| 227 |
double* _seed_rf; // Seed disk-free Radius |
| 228 |
double* _seed_rc; // Seed coverage Radius |
| 229 |
|
| 230 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 231 |
/////////// Fitting Methods ////////////////////////////////////////////////////////////////////////// |
| 232 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 233 |
|
| 234 |
// Data Fit |
| 235 |
int LeastSquare_QR(size_t num_data_points, double xmin, double xmax, double* x, double* f, size_t num_basis, double* c); |
| 236 |
|
| 237 |
int NaturalCubicSplineInterpolation(size_t num_data_points, double* x, double* y, double* co, double* c1, double* c2, double* c3); |
| 238 |
|
| 239 |
int FourierExpansion(size_t num_data_points, double xmin, double xmax, double* x, double* f, double** c, size_t num_basis, double* a, double* b); |
| 240 |
|
| 241 |
// Linear Solvers |
| 242 |
|
| 243 |
int solve_LU_Doolittle(size_t num_eq, double* LD, double* DD, double* UD, double* b, double* x); |
| 244 |
|
| 245 |
void LS_QR_Solver(size_t nrow, size_t ncol, double** A, double* b, double* x); |
| 246 |
|
| 247 |
void GramSchmidt(size_t nrow, size_t ncol, double** A, double** Q); |
| 248 |
|
| 249 |
void orthonormalize_vector(size_t nbasis, size_t ndim, double** e_basis, double* v, double &norm); |
| 250 |
|
| 251 |
void QR_Factorization(size_t nrow, size_t ncol, double** A, double** Q, double** R); |
| 252 |
|
| 253 |
void MAT_MAT(size_t nrow, size_t ncol, double** A, size_t mcol, double** B, double** C); |
| 254 |
|
| 255 |
void MAT_Transpose(size_t nrow, size_t ncol, double** A, double** AT); |
| 256 |
|
| 257 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 258 |
/////////// Surrogate Methods /////////////////////////////////////////////////////////////////////////////////////// |
| 259 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 260 |
|
| 261 |
int construct_local_surrogates(); |
| 262 |
|
| 263 |
int init_vps_containers(); |
| 264 |
|
| 265 |
int add_neighbor_layer(size_t cell_index, size_t function_index, size_t &num_neighbors, size_t* &neighbors, size_t &neighbors_capacity); |
| 266 |
|
| 267 |
int retrieve_weights_regression(size_t cell_index); |
| 268 |
|
| 269 |
double evaluate_basis_function(double* x, size_t cell_index, size_t ibasis); |
| 270 |
|
| 271 |
double evaluate_one_dimensional_basis_function(double x, double xo, double xmin, double xmax, size_t ibasis); |
| 272 |
|
| 273 |
int detect_discontinuities(); |
| 274 |
|
| 275 |
void plot_vps_frames(std::string file_name, size_t function_index, size_t nx, size_t ny, size_t num_contours, bool plot_graph); |
| 276 |
|
| 277 |
void create_ps_file(std::string file_name, size_t nx, size_t ny, std::fstream &file, double &scale); |
| 278 |
|
| 279 |
void plot_vps_surrogate_frame(std::fstream &file, double scale, size_t function_index, double* xsec, size_t dim_i, size_t dim_j, |
| 280 |
size_t frame_i, size_t frame_j, size_t frame_ni, size_t frame_nj, bool plot_graph, std::vector<double> &contours); |
| 281 |
|
| 282 |
|
| 283 |
//////////////////////////////////////////////////////////////////////////////// |
| 284 |
/////////// Surrogate Variables ////////////////////////////////////////// |
| 285 |
//////////////////////////////////////////////////////////////////////////////// |
| 286 |
surrogate_method _method; |
| 287 |
surrogate_basis _basis; |
| 288 |
|
| 289 |
size_t _pool_order; // order of basis pool |
| 290 |
size_t _num_basis_pool; // number of global basis |
| 291 |
size_t** _p; // global basis indicator |
| 292 |
|
| 293 |
size_t _num_basis; // number of global basis |
| 294 |
double*** _basis_coef; // basis coeficients |
| 295 |
size_t*** _basis_index; // basis index |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
size_t _num_dim; |
| 300 |
size_t _num_functions; |
| 301 |
size_t _desired_order; |
| 302 |
size_t _budget; |
| 303 |
size_t _num_samples; |
| 304 |
|
| 305 |
double _diag; // domain diagonal |
| 306 |
|
| 307 |
// pointers |
| 308 |
double* _xmin; // domain lower bound |
| 309 |
double* _xmax; // domain upper bound |
| 310 |
double** _x; // sample locations |
| 311 |
double** _f; // function values |
| 312 |
double*** _g; // function gradients |
| 313 |
double**** _h; // functions hessians |
| 314 |
|
| 315 |
|
| 316 |
size_t _num_vs; |
| 317 |
double** _vs; |
| 318 |
|
| 319 |
|
| 320 |
}; |
| 321 |
|
| 322 |
#endif |