libROM  v1.0
Data-driven physical simulation library
GreedySampler.cpp
1 
11 // Description: This class greedily selects parameter points
12 // for the construction of a ROM database.
13 
14 #include "GreedySampler.h"
15 #include "utils/HDFDatabase.h"
16 #include "mpi.h"
17 #include <cmath>
18 #include <algorithm>
19 #include <limits.h>
20 #include <fstream>
21 
22 namespace CAROM {
23 
26 {
27  struct GreedyErrorIndicatorPoint result;
28  result.point = std::shared_ptr<Vector>(point);
29  result.localROM = std::shared_ptr<Vector>(localROM);
30  return result;
31 }
32 
35  std::shared_ptr<Vector>& localROM)
36 {
37  struct GreedyErrorIndicatorPoint result;
38  result.point = std::shared_ptr<Vector>(point);
39  result.localROM = localROM;
40  return result;
41 }
42 
43 Vector
44 getNearestPoint(std::vector<Vector> param_points, Vector point)
45 {
46 
47  int closest_point_index = getNearestPointIndex(param_points, point);
48  return param_points[closest_point_index];
49 }
50 
51 int
52 getNearestPointIndex(std::vector<Vector> param_points, Vector point)
53 {
54 
55  double closest_dist_to_points = INT_MAX;
56  int closest_point_index = -1;
57 
58  for (int i = 0; i < param_points.size(); i++)
59  {
60  Vector diff;
61  point.minus(param_points[i], diff);
62  double dist = diff.norm();
63  if (dist < closest_dist_to_points)
64  {
65  closest_dist_to_points = dist;
66  closest_point_index = i;
67  }
68  }
69 
70  return closest_point_index;
71 }
72 
73 double
74 getNearestPoint(std::vector<double> param_points, double point)
75 {
76 
77  int closest_point_index = getNearestPointIndex(param_points, point);
78  return param_points[closest_point_index];
79 }
80 
81 int
82 getNearestPointIndex(std::vector<double> param_points, double point)
83 {
84 
85  double closest_dist_to_points = INT_MAX;
86  int closest_point_index = -1;
87 
88  for (int i = 0; i < param_points.size(); i++)
89  {
90  double dist = std::abs(point - param_points[i]);
91  if (dist < closest_dist_to_points)
92  {
93  closest_dist_to_points = dist;
94  closest_point_index = i;
95  }
96  }
97 
98  return closest_point_index;
99 }
100 
102  std::vector<Vector> parameter_points,
103  bool check_local_rom,
104  double relative_error_tolerance,
105  double alpha,
106  double max_clamp,
107  int subset_size,
108  int convergence_subset_size,
109  std::string output_log_path,
110  std::string warm_start_file_name,
111  bool use_centroid,
112  int random_seed,
113  bool debug_algorithm)
114 {
115 
116  d_num_parameter_points = parameter_points.size();
117  CAROM_VERIFY(d_num_parameter_points >= 1);
118  d_parameter_points = parameter_points;
119 
120  constructObject(check_local_rom, relative_error_tolerance, alpha, max_clamp,
121  subset_size, convergence_subset_size, output_log_path, use_centroid,
122  random_seed, debug_algorithm);
123 }
124 
126  std::vector<double> parameter_points,
127  bool check_local_rom,
128  double relative_error_tolerance,
129  double alpha,
130  double max_clamp,
131  int subset_size,
132  int convergence_subset_size,
133  std::string output_log_path,
134  std::string warm_start_file_name,
135  bool use_centroid,
136  int random_seed,
137  bool debug_algorithm)
138 {
139 
140  d_num_parameter_points = parameter_points.size();
141 
142  std::vector<Vector> parameter_points_vec;
143  for (int i = 0; i < parameter_points.size(); i++)
144  {
145  Vector vec(1, false);
146  vec.item(0) = parameter_points[i];
147  parameter_points_vec.push_back(vec);
148  }
149  CAROM_VERIFY(d_num_parameter_points >= 1);
150  d_parameter_points = parameter_points_vec;
151 
152  constructObject(check_local_rom, relative_error_tolerance, alpha, max_clamp,
153  subset_size, convergence_subset_size, output_log_path, use_centroid,
154  random_seed, debug_algorithm);
155 }
156 
158  Vector param_space_min,
159  Vector param_space_max,
160  int num_parameter_points,
161  bool check_local_rom,
162  double relative_error_tolerance,
163  double alpha,
164  double max_clamp,
165  int subset_size,
166  int convergence_subset_size,
167  std::string output_log_path,
168  std::string warm_start_file_name,
169  bool use_centroid,
170  int random_seed,
171  bool debug_algorithm)
172 {
173 
174  d_min_param_point = param_space_min;
175  d_max_param_point = param_space_max;
176  d_num_parameter_points = num_parameter_points;
177 
178  constructObject(check_local_rom, relative_error_tolerance, alpha, max_clamp,
179  subset_size,
180  convergence_subset_size, output_log_path, use_centroid, random_seed,
181  debug_algorithm);
182 }
183 
185  double param_space_min,
186  double param_space_max,
187  int num_parameter_points,
188  bool check_local_rom,
189  double relative_error_tolerance,
190  double alpha,
191  double max_clamp,
192  int subset_size,
193  int convergence_subset_size,
194  std::string output_log_path,
195  std::string warm_start_file_name,
196  bool use_centroid,
197  int random_seed,
198  bool debug_algorithm)
199 {
200 
201  Vector param_space_min_vec(1, false);
202  param_space_min_vec.item(0) = param_space_min;
203  Vector param_space_max_vec(1, false);
204  param_space_max_vec.item(0) = param_space_max;
205 
206  d_min_param_point = param_space_min_vec;
207  d_max_param_point = param_space_max_vec;
208  d_num_parameter_points = num_parameter_points;
209 
210  constructObject(check_local_rom, relative_error_tolerance, alpha, max_clamp,
211  subset_size,
212  convergence_subset_size, output_log_path, use_centroid, random_seed,
213  debug_algorithm);
214 }
215 
217  std::string base_file_name,
218  std::string output_log_path)
219 {
220  CAROM_ASSERT(!base_file_name.empty());
221 
222  int mpi_init;
223  MPI_Initialized(&mpi_init);
224  if (mpi_init) {
225  MPI_Comm_rank(MPI_COMM_WORLD, &d_rank);
226  }
227  else {
228  d_rank = 0;
229  }
230 
231  d_output_log_path = output_log_path;
232 
233  load(base_file_name);
234 }
235 
236 void
238  std::string const& warm_start_file_name)
239 {
240  char tmp[100];
241  std::string full_file_name = warm_start_file_name;
242  HDFDatabase database;
243  database.open(full_file_name, "r");
244 
245  sprintf(tmp, "num_parameter_sampled_indices");
246  int num_parameter_sampled_indices;
247  database.getInteger(tmp, num_parameter_sampled_indices);
248  if (num_parameter_sampled_indices > 0)
249  {
250  int temp_parameter_sampled_indices[num_parameter_sampled_indices];
251  sprintf(tmp, "parameter_sampled_indices");
252  database.getIntegerArray(tmp, &temp_parameter_sampled_indices[0],
253  num_parameter_sampled_indices);
254  for (int i = 0; i < num_parameter_sampled_indices; i++)
255  {
256  std::string vec_path = warm_start_file_name + "_" + std::to_string(i);
257  Vector point;
258  point.local_read(vec_path, 0);
259  for (int j = 0; j < d_parameter_points.size(); j++)
260  {
261  Vector diff;
262  point.minus(d_parameter_points[j], diff);
263  double dist = diff.norm();
264  if (dist > 1e-12)
265  {
266  d_parameter_points.push_back(point);
268  }
269  }
270  }
271  }
272 }
273 
274 void
276  std::string base_file_name)
277 {
278  char tmp[100];
279  std::string full_file_name = base_file_name;
280  HDFDatabase database;
281  database.open(full_file_name, "r");
282 
283  sprintf(tmp, "num_parameter_points");
284  int num_parameter_points;
285  database.getInteger(tmp, num_parameter_points);
286  for (int i = 0; i < num_parameter_points; i++)
287  {
288  std::string vec_path = base_file_name + "_" + std::to_string(i);
289  Vector point;
290  point.local_read(vec_path, 0);
291  d_parameter_points.push_back(point);
292  }
293 
294  std::string vec_path = base_file_name + "_min_point";
295  d_min_param_point.local_read(vec_path, 0);
296 
297  vec_path = base_file_name + "_max_point";
298  d_max_param_point.local_read(vec_path, 0);
299 
300  sprintf(tmp, "num_parameter_sampled_indices");
301  int num_parameter_sampled_indices;
302  database.getInteger(tmp, num_parameter_sampled_indices);
303  if (num_parameter_sampled_indices > 0)
304  {
305  int temp_parameter_sampled_indices[num_parameter_sampled_indices];
306  sprintf(tmp, "parameter_sampled_indices");
307  database.getIntegerArray(tmp, &temp_parameter_sampled_indices[0],
308  num_parameter_sampled_indices);
309  for (int i = 0; i < num_parameter_sampled_indices; i++)
310  {
311  d_parameter_sampled_indices.insert(temp_parameter_sampled_indices[i]);
312  }
313  }
314 
315  int bool_int_temp;
316  sprintf(tmp, "procedure_completed");
317  database.getInteger(tmp, bool_int_temp);
318  d_procedure_completed = (bool) bool_int_temp;
319 
321  {
322  sprintf(tmp, "max_error");
323  database.getDouble(tmp, d_max_error);
324  sprintf(tmp, "curr_relative_error");
325  database.getDouble(tmp, d_curr_relative_error);
326  sprintf(tmp, "error_indicator_tol");
327  database.getDouble(tmp, d_error_indicator_tol);
328  sprintf(tmp, "relative_error_tol");
329  database.getDouble(tmp, d_relative_error_tol);
330  sprintf(tmp, "alpha");
331  database.getDouble(tmp, d_alpha);
332  sprintf(tmp, "max_clamp");
333  database.getDouble(tmp, d_max_clamp);
334  sprintf(tmp, "max_num_parameter_points");
335  database.getInteger(tmp, d_num_parameter_points);
336  sprintf(tmp, "subset_size");
337  database.getInteger(tmp, d_subset_size);
338  sprintf(tmp, "convergence_subset_size");
339  database.getInteger(tmp, d_convergence_subset_size);
340  sprintf(tmp, "next_point_to_sample");
341  database.getInteger(tmp, d_next_point_to_sample);
342  sprintf(tmp, "next_point_requiring_error_indicator");
344  sprintf(tmp, "check_local_rom");
345  database.getInteger(tmp, bool_int_temp);
346  d_check_local_rom = (bool) bool_int_temp;
347  sprintf(tmp, "use_centroid");
348  database.getInteger(tmp, bool_int_temp);
349  d_use_centroid = (bool) bool_int_temp;
350  sprintf(tmp, "iteration_started");
351  database.getInteger(tmp, bool_int_temp);
352  d_iteration_started = (bool) bool_int_temp;
353  sprintf(tmp, "convergence_started");
354  database.getInteger(tmp, bool_int_temp);
355  d_convergence_started = (bool) bool_int_temp;
356  sprintf(tmp, "next_parameter_point_computed");
357  database.getInteger(tmp, bool_int_temp);
358  d_next_parameter_point_computed = (bool) bool_int_temp;
359  sprintf(tmp, "point_requiring_error_indicator_computed");
360  database.getInteger(tmp, bool_int_temp);
361  d_point_requiring_error_indicator_computed = (bool) bool_int_temp;
362  sprintf(tmp, "subset_created");
363  database.getInteger(tmp, bool_int_temp);
364  d_subset_created = (bool) bool_int_temp;
365  sprintf(tmp, "debug_algorithm");
366  database.getInteger(tmp, bool_int_temp);
367  d_debug_algorithm = (bool) bool_int_temp;
368  sprintf(tmp, "counter");
369  database.getInteger(tmp, d_counter);
370  sprintf(tmp, "subset_counter");
371  database.getInteger(tmp, d_subset_counter);
372  sprintf(tmp, "random_seed");
373  database.getInteger(tmp, d_random_seed);
374 
375  sprintf(tmp, "parameter_point_random_indices");
378  d_parameter_points.size());
379 
380  sprintf(tmp, "parameter_point_errors");
382  database.getDoubleArray(tmp, &d_parameter_point_errors[0],
383  d_parameter_points.size());
384 
385  sprintf(tmp, "parameter_point_local_rom");
388  d_parameter_points.size());
389 
390  for (int i = 0; i < d_convergence_subset_size; i++)
391  {
392  std::string vec_path = base_file_name + "_conv_" + std::to_string(i);
393  Vector point;
394  point.local_read(vec_path, 0);
395  d_convergence_points.push_back(point);
396  }
397  }
398  database.close();
399 
401 }
402 
403 void
405 {
406  CAROM_VERIFY(d_min_param_point.dim() == d_max_param_point.dim());
407  CAROM_VERIFY(d_num_parameter_points >= 1);
408 
409  bool isGreater = false;
410  for (int i = 0; i < d_min_param_point.dim(); i++)
411  {
412  if (d_num_parameter_points == 1)
413  {
415  {
416  isGreater = true;
417  break;
418  }
419  }
420  else
421  {
423  {
424  isGreater = true;
425  break;
426  }
427  }
428  }
429  CAROM_VERIFY(isGreater);
430 
431  if (d_rank == 0)
432  {
433  std::string str;
434  str += "Total number of sample points: " + std::to_string(
435  d_num_parameter_points) + "\n";
436  str += "Parameter space minimum: [ ";
437  for (int i = 0 ; i < d_min_param_point.dim(); i++)
438  {
439  str += std::to_string(d_min_param_point.item(i)) + " ";
440  }
441  str += "]\n";
442  str += "Parameter space maximum: [ ";
443  for (int i = 0 ; i < d_max_param_point.dim(); i++)
444  {
445  str += std::to_string(d_max_param_point.item(i)) + " ";
446  }
447  str += "]\n";
448  agnosticPrint(str);
449  }
450 }
451 
452 void
454  bool check_local_rom,
455  double relative_error_tolerance,
456  double alpha,
457  double max_clamp,
458  int subset_size,
459  int convergence_subset_size,
460  std::string output_log_path,
461  bool use_centroid,
462  int random_seed,
463  bool debug_algorithm)
464 {
465  CAROM_VERIFY(relative_error_tolerance > 0.0);
466  CAROM_VERIFY(alpha >= 1.0);
467  CAROM_VERIFY(max_clamp >= 1.0);
468  CAROM_VERIFY(subset_size > 0);
469  CAROM_VERIFY(convergence_subset_size > 0);
470  CAROM_VERIFY(subset_size < convergence_subset_size);
471  CAROM_VERIFY(random_seed > 0);
472 
473  int mpi_init;
474  MPI_Initialized(&mpi_init);
475  if (mpi_init) {
476  MPI_Comm_rank(MPI_COMM_WORLD, &d_rank);
477  }
478  else {
479  d_rank = 0;
480  }
481 
482  d_check_local_rom = check_local_rom;
483  d_error_indicator_tol = 0.0;
484  d_relative_error_tol = relative_error_tolerance;
485  d_alpha = alpha;
486  d_max_clamp = max_clamp;
487  d_subset_size = subset_size;
488  d_convergence_subset_size = convergence_subset_size;
489  d_output_log_path = output_log_path;
490  d_use_centroid = use_centroid;
491  d_max_error = 0;
496  d_iteration_started = false;
497  d_convergence_started = false;
498  d_subset_created = false;
499  d_procedure_completed = false;
500  d_subset_counter = 0;
501  d_counter = -1;
502  d_debug_algorithm = debug_algorithm;
503  d_random_seed = random_seed;
504 
505  rng.seed(d_random_seed);
506 
507  if (d_rank == 0)
508  {
509  std::string str;
510  str += "Relative error tolerance: " + std::to_string(d_relative_error_tol) +
511  "\n";
512  str += "Alpha constant: " + std::to_string(d_alpha) + "\n";
513  str += "Max clamp constant: " + std::to_string(d_max_clamp) + "\n";
514  str += "Iteration subset size: " + std::to_string(d_subset_size) + "\n";
515  str += "Convergence subset size: " + std::to_string(d_convergence_subset_size) +
516  "\n";
517  agnosticPrint(str);
518  }
519 }
520 
521 void
523 {
524  CAROM_VERIFY(d_parameter_points.size() > 0);
525  CAROM_VERIFY(d_subset_size <= d_parameter_points.size());
526  CAROM_VERIFY(d_convergence_subset_size <= d_parameter_points.size());
527 
528  for (int i = 0; i < d_parameter_points.size() - 1; i++) {
529  CAROM_VERIFY(d_parameter_points[i].dim() == d_parameter_points[i + 1].dim());
530  }
531 
532  for (int i = 0 ; i < d_parameter_points.size(); i++) {
533  d_parameter_point_errors.push_back(INT_MAX);
534  d_parameter_point_local_rom.push_back(-1);
536  }
537 
539 }
540 
541 std::shared_ptr<Vector>
543 {
544  if (isComplete())
545  {
546  return std::shared_ptr<Vector>(nullptr);
547  }
549  {
550  return std::shared_ptr<Vector>(nullptr);
551  }
553  {
555  return std::shared_ptr<Vector>(result);
556  }
557 
558  if (d_parameter_sampled_indices.size() == 0)
559  {
561  }
562 
563  d_max_error = 0;
564 
565  int curr_point_to_sample = d_next_point_to_sample;
566 
567  d_convergence_started = false;
568  d_subset_created = false;
569  d_subset_counter = 0;
570  d_counter = -1;
571 
572  auto search = d_parameter_sampled_indices.find(curr_point_to_sample);
573  CAROM_VERIFY(search == d_parameter_sampled_indices.end());
574 
575  d_parameter_sampled_indices.insert(curr_point_to_sample);
576 
577  if (d_rank == 0)
578  {
579  std::string str;
580  str += "\nPoint sampled at [ ";
581  for (int i = 0 ; i < d_parameter_points[curr_point_to_sample].dim(); i++)
582  {
583  str += std::to_string(d_parameter_points[curr_point_to_sample].item(i)) + " ";
584  }
585  str += "]\n";
586  agnosticPrint(str);
587  }
588 
590 
591  Vector* result = new Vector(d_parameter_points[curr_point_to_sample]);
592  return std::shared_ptr<Vector>(result);
593 }
594 
596 GreedySampler::getNextPointRequiringRelativeError()
597 {
598  if (isComplete())
599  {
600  return createGreedyErrorIndicatorPoint(nullptr, nullptr);
601  }
602  if (!d_next_parameter_point_computed)
603  {
604  return createGreedyErrorIndicatorPoint(nullptr, nullptr);
605  }
606 
607  Vector* result1 = new Vector(d_parameter_points[d_next_point_to_sample]);
608  Vector* result2 = NULL;
609 
610  if (d_parameter_sampled_indices.size() == 1)
611  {
612  result2 = new Vector(d_parameter_points[getNearestROMIndexToParameterPoint(
613  d_next_point_to_sample, false)]);
614  }
615  else
616  {
617  result2 = new Vector(d_parameter_points[getNearestROMIndexToParameterPoint(
618  d_next_point_to_sample, true)]);
619  }
620 
621  return createGreedyErrorIndicatorPoint(result1, result2);
622 }
623 
624 struct GreedyErrorIndicatorPoint
625 GreedySampler::getNextPointRequiringErrorIndicator()
626 {
627  if (isComplete())
628  {
629  return createGreedyErrorIndicatorPoint(nullptr, nullptr);
630  }
631  if (!d_iteration_started)
632  {
633  return createGreedyErrorIndicatorPoint(nullptr, nullptr);
634  }
635 
636  if (d_convergence_started)
637  {
638  return getNextConvergencePointRequiringErrorIndicator();
639  }
640  else
641  {
642  return getNextSubsetPointRequiringErrorIndicator();
643  }
644 }
645 
646 struct GreedyErrorIndicatorPoint
647 GreedySampler::getNextSubsetPointRequiringErrorIndicator()
648 {
649  if (d_point_requiring_error_indicator_computed)
650  {
651  Vector* result1 = new Vector(
652  d_parameter_points[d_next_point_requiring_error_indicator]);
653  Vector* result2 = new Vector(
654  d_parameter_points[getNearestROMIndexToParameterPoint(
655  d_next_point_requiring_error_indicator, false)]);
656  return createGreedyErrorIndicatorPoint(result1, result2);
657  }
658  if (d_subset_counter == d_subset_size)
659  {
660  return createGreedyErrorIndicatorPoint(nullptr, nullptr);
661  }
662 
663  if(!d_subset_created)
664  {
665  // generate random shuffle
666  if (!d_debug_algorithm)
667  {
668  std::shuffle(d_parameter_point_random_indices.begin(),
669  d_parameter_point_random_indices.end(), rng);
670  }
671  d_subset_created = true;
672  }
673 
674  d_next_point_requiring_error_indicator = -1;
675 
676  while (d_counter < (int) d_parameter_points.size() - 1)
677  {
678  d_counter++;
679  if (d_subset_counter == d_subset_size)
680  {
681  break;
682  }
683  auto search = d_parameter_sampled_indices.find(
684  d_parameter_point_random_indices[d_counter]);
685  if (search == d_parameter_sampled_indices.end())
686  {
687  d_subset_counter++;
688  double curr_error =
689  d_parameter_point_errors[d_parameter_point_random_indices[d_counter]];
690  if (curr_error > d_max_error)
691  {
692  // if we have already computed this error indicator at the same local rom, the error indicator will not improve
693  // no need to calculate the error indicator again
694  if (d_parameter_point_local_rom[d_parameter_point_random_indices[d_counter]] ==
695  getNearestROMIndexToParameterPoint(d_parameter_point_random_indices[d_counter],
696  false))
697  {
698  d_max_error = curr_error;
699  d_next_point_to_sample = d_parameter_point_random_indices[d_counter];
700  if (d_rank == 0)
701  {
702  std::string str;
703  str += "Error indicator at [ ";
704  for (int i = 0 ;
705  i < d_parameter_points[d_parameter_point_random_indices[d_counter]].dim(); i++)
706  {
707  str += std::to_string(
708  d_parameter_points[d_parameter_point_random_indices[d_counter]].item(i)) + " ";
709  }
710  str += "] skipped.\n";
711  str += "Error indicator " + std::to_string(curr_error) +
712  " already computed at the same local ROM.\n";
713  agnosticPrint(str);
714  }
715  }
716  else
717  {
718  d_next_point_requiring_error_indicator =
719  d_parameter_point_random_indices[d_counter];
720  d_point_requiring_error_indicator_computed = true;
721  Vector* result1 = new Vector(
722  d_parameter_points[d_next_point_requiring_error_indicator]);
723  Vector* result2 = new Vector(
724  d_parameter_points[getNearestROMIndexToParameterPoint(
725  d_next_point_requiring_error_indicator, false)]);
726  return createGreedyErrorIndicatorPoint(result1, result2);
727  }
728  }
729  else
730  {
731  if (d_rank == 0)
732  {
733  std::string str;
734  str += "Error indicator at [ ";
735  for (int i = 0 ;
736  i < d_parameter_points[d_parameter_point_random_indices[d_counter]].dim(); i++)
737  {
738  str += std::to_string(
739  d_parameter_points[d_parameter_point_random_indices[d_counter]].item(i)) + " ";
740  }
741  str += "] skipped.\n";
742  str += "Error indicator " + std::to_string(curr_error) +
743  " is less than current max error " + std::to_string(d_max_error) + "\n";
744  agnosticPrint(str);
745  }
746  }
747  }
748  }
749  if (d_next_point_requiring_error_indicator == -1)
750  {
751  if (d_rank == 0)
752  {
753  std::string str;
754  str += "Ran out of points to calculate error indicator for in this iteration.\n";
755  agnosticPrint(str);
756  }
757  if (d_max_error < d_error_indicator_tol)
758  {
759  startConvergence();
760  }
761  else
762  {
763  printErrorIndicatorToleranceNotMet();
764  d_iteration_started = false;
765  }
766  }
767 
768  return createGreedyErrorIndicatorPoint(nullptr, nullptr);
769 }
770 
771 struct GreedyErrorIndicatorPoint
772 GreedySampler::getNextConvergencePointRequiringErrorIndicator()
773 {
774  if (d_point_requiring_error_indicator_computed)
775  {
776  Vector* result1 = new Vector(
777  d_convergence_points[d_next_point_requiring_error_indicator]);
778  std::shared_ptr<Vector> result2 = getNearestROM(
779  d_convergence_points[d_next_point_requiring_error_indicator]);
780  return createGreedyErrorIndicatorPoint(result1, result2);
781  }
782  if (d_counter == d_convergence_subset_size)
783  {
784  return createGreedyErrorIndicatorPoint(nullptr, nullptr);
785  }
786 
787  d_next_point_requiring_error_indicator = -1;
788 
789  //get next point requiring error indicator
790  if (d_counter < (int) d_convergence_points.size())
791  {
792  d_next_point_requiring_error_indicator = d_counter;
793  d_point_requiring_error_indicator_computed = true;
794  Vector* result1 = new Vector(
795  d_convergence_points[d_next_point_requiring_error_indicator]);
796  std::shared_ptr<Vector> result2 = getNearestROM(
797  d_convergence_points[d_next_point_requiring_error_indicator]);
798  return createGreedyErrorIndicatorPoint(result1, result2);
799  }
800 
801  if (d_next_point_requiring_error_indicator == -1)
802  {
803  printConvergenceAchieved();
804  d_procedure_completed = true;
805  }
806 
807  return createGreedyErrorIndicatorPoint(nullptr, nullptr);
808 }
809 
810 void
811 GreedySampler::printSamplingType(std::string sampling_type)
812 {
813  if (d_rank == 0)
814  {
815  std::string str;
816  str += "Sampling type: " + sampling_type + "\n";
817  agnosticPrint(str);
818  }
819 }
820 
821 void
823 {
824  if (d_rank == 0)
825  {
826  std::string str;
827  str += "Convergence achieved.\n";
828  agnosticPrint(str);
829 
830  str = "\nSampled Parameter Points\n";
831  std::vector<std::pair<double, int>> first_dim_of_sampled_points;
832  for (auto itr = d_parameter_sampled_indices.begin();
833  itr != d_parameter_sampled_indices.end(); ++itr) {
834  first_dim_of_sampled_points.push_back(std::make_pair(
835  d_parameter_points[*itr].item(0), *itr));
836  }
837  sort(first_dim_of_sampled_points.begin(), first_dim_of_sampled_points.end());
838  for (int i = 0; i < first_dim_of_sampled_points.size(); i++)
839  {
840  str += "[ ";
841  for (int j = 0 ;
842  j < d_parameter_points[first_dim_of_sampled_points[i].second].dim(); j++)
843  {
844  str += std::to_string(
845  d_parameter_points[first_dim_of_sampled_points[i].second].item(j)) + " ";
846  }
847  str += "]\n";
848  }
849  agnosticPrint(str);
850  }
851 }
852 
853 void
855 {
856  CAROM_VERIFY(error >= 0);
857  CAROM_VERIFY(d_next_parameter_point_computed);
858 
859  if (!std::isfinite(error))
860  {
861  error = INT_MAX;
862  }
863 
864  if (d_rank == 0)
865  {
866  if (d_output_log_path == "")
867  {
868  std::string str;
869  str += "Relative error computed at [ ";
870  for (int i = 0 ; i < d_parameter_points[d_next_point_to_sample].dim(); i++)
871  {
872  str += std::to_string(d_parameter_points[d_next_point_to_sample].item(i)) + " ";
873  }
874  str += "]\n";
875  str += "Relative error: " + std::to_string(error) + "\n";
876  agnosticPrint(str);
877  }
878  }
879 
880  d_curr_relative_error = error;
882  d_iteration_started = true;
883 
884  double old_error_indicator_tol = d_error_indicator_tol;
885 
886  if (d_parameter_sampled_indices.size() > 1)
887  {
888  double max1 = d_alpha * d_error_indicator_tol;
890  double min2 = d_relative_error_tol *
892 
894  {
895  if (d_rank == 0)
896  {
897  std::string str;
898  str += "The relative error was smaller than the relative error tolerance. The error indicator tolerance must increase.\n";
899  str += "Alpha constant * relative error tolerance: " + std::to_string(
900  max1) + "\n";
901  str += "The minimum value the error indicator tolerance can take is: " +
902  std::to_string(max1) + "\n";
903  str += "Max clamp constant * current max error indicator: " + std::to_string(
904  min1) + "\n";
905  str += "Relative error tolerance * current max error indicator / current relative error: "
906  + std::to_string(min2) + "\n";
907  str += "The maximum value the error indicator tolerance can take is the minimum of the previous two values: "
908  + std::to_string(std::min(min1, min2)) + "\n";
909  agnosticPrint(str);
910  }
911  d_error_indicator_tol = std::max(max1, std::min(min1, min2));
912  }
913  else
914  {
915  double min1 = d_relative_error_tol *
917 
918  if (d_rank == 0)
919  {
920  std::string str;
921  str += "The relative error was larger than the relative error tolerance. The error indicator tolerance must decrease.\n";
922  str += "Current error indicator tolerance: " + std::to_string(
923  d_error_indicator_tol) + "\n";
924  str += "Relative error tolerance * current max error indicator / current relative error: "
925  + std::to_string(min1) + "\n";
926  str += "The minimum value the error indicator tolerance can take is: " +
927  std::to_string(std::min(d_error_indicator_tol, min1)) + "\n";
928  agnosticPrint(str);
929  }
931  }
932  if (d_rank == 0)
933  {
934  if (old_error_indicator_tol != d_error_indicator_tol)
935  {
936  std::string str;
937  str += "Error indicator tolerance was adaptively changed from " +
938  std::to_string(old_error_indicator_tol) + " to " + std::to_string(
939  d_error_indicator_tol) + "\n";
940  agnosticPrint(str);
941  }
942  }
943  }
944 
947 
948  if (d_parameter_sampled_indices.size() > 1
950  {
952  }
953  else
954  {
956  {
959  }
960  else
961  {
962  // Precompute next error indicator point
963  // This will allow us to figure out if the greedy algorithm has terminated
964  // early without needing an extra call to the error indicator function.
966  }
967  }
968 }
969 
970 void
971 GreedySampler::setPointErrorIndicator(double error, int vec_size)
972 {
973  CAROM_VERIFY(error >= 0);
975 
976  double proc_errors = pow(error, 2);
977  int total_vec_size = vec_size;
978  CAROM_VERIFY(MPI_Allreduce(MPI_IN_PLACE,
979  &proc_errors,
980  1,
981  MPI_DOUBLE,
982  MPI_SUM,
983  MPI_COMM_WORLD) == MPI_SUCCESS);
984  CAROM_VERIFY(MPI_Allreduce(MPI_IN_PLACE,
985  &total_vec_size,
986  1,
987  MPI_INT,
988  MPI_SUM,
989  MPI_COMM_WORLD) == MPI_SUCCESS);
990  proc_errors = sqrt(proc_errors);
991  proc_errors /= total_vec_size;
992 
993  if (!std::isfinite(proc_errors))
994  {
995  proc_errors = INT_MAX;
996  }
997 
999  {
1000  setConvergenceErrorIndicator(proc_errors);
1001  }
1002  else
1003  {
1004  setSubsetErrorIndicator(proc_errors);
1005  }
1006 }
1007 
1008 void
1010  double proc_errors)
1011 {
1012  if (d_rank == 0)
1013  {
1014  std::string str;
1015  str += "Error indicator computed at [ ";
1016  for (int i = 0 ; i < errorIndicatorPoint.dim(); i++)
1017  {
1018  str += std::to_string(errorIndicatorPoint.item(i)) + " ";
1019  }
1020  str += "]\n";
1021  str += "Error indicator: " + std::to_string(proc_errors) + "\n";
1022  agnosticPrint(str);
1023  }
1024 }
1025 
1026 void
1028 {
1029  if (d_output_log_path == "")
1030  {
1031  std::cout << str;
1032  }
1033  else
1034  {
1035  std::ofstream database_history;
1036  database_history.open(d_output_log_path, std::ios::app);
1037  database_history << str;
1038  database_history.close();
1039  }
1040 }
1041 
1042 void
1044 {
1045  if (d_rank == 0)
1046  {
1047  std::string str;
1048  str += "Error indicator tolerance " + std::to_string(d_error_indicator_tol) +
1049  " not met.\n";
1050  agnosticPrint(str);
1051  }
1052 }
1053 
1054 void
1056 {
1058  {
1059  auto search = d_parameter_sampled_indices.find(
1061  if (search != d_parameter_sampled_indices.end())
1062  {
1066 
1067  double old_error_indicator_tol = d_error_indicator_tol;
1068  if (d_parameter_sampled_indices.size() == 1)
1069  {
1070  d_error_indicator_tol = std::max(d_error_indicator_tol, proc_errors);
1071  }
1072  if (d_rank == 0)
1073  {
1074  std::string str;
1075  str += "Local ROM error indicator computed at [ ";
1076  for (int i = 0 ;
1078  {
1079  str += std::to_string(
1081  }
1082  str += "]\n";
1083  str += "Local ROM error indicator (tolerance unchecked): " + std::to_string(
1084  proc_errors) + "\n";
1085  if (old_error_indicator_tol != d_error_indicator_tol)
1086  {
1087  str += "Error indicator at the local ROM was higher than the previous tolerance.\n";
1088  str += "The error indicator tolerance should always be at least the error indicator at the local ROM.\n";
1089  str += "Error indicator tolerance was adaptively changed from " +
1090  std::to_string(old_error_indicator_tol) + " to " + std::to_string(
1091  d_error_indicator_tol) + "\n";
1092  }
1093  agnosticPrint(str);
1094  }
1095 
1097 
1098  // Precompute next error indicator point
1099  // This will allow us to figure out if the greedy algorithm has terminated
1100  // early without needing an extra call to the error indicator function.
1102 
1103  return;
1104  }
1105  }
1106 
1107  if (proc_errors <
1109  {
1111  proc_errors;
1114  false);
1115  }
1116 
1119 
1121 
1122  if (proc_errors > d_max_error)
1123  {
1124  d_max_error = proc_errors;
1126  }
1127 
1129  || d_counter == (int) d_parameter_points.size() - 1)
1130  {
1132  {
1133  startConvergence();
1134  }
1135  else
1136  {
1137  d_iteration_started = false;
1139  }
1140  }
1141 
1142  // Precompute next error indicator point
1143  // This will allow us to figure out if the greedy algorithm has terminated
1144  // early without needing an extra call to the error indicator function.
1146 
1147  return;
1148 }
1149 
1150 void
1152 {
1154 
1156 
1157  if (proc_errors > d_max_error)
1158  {
1159  d_max_error = proc_errors;
1160  }
1161 
1162  if (proc_errors >= d_error_indicator_tol)
1163  {
1164  d_iteration_started = false;
1168  }
1169  else
1170  {
1171  d_counter++;
1172  }
1173 
1175  {
1177  d_procedure_completed = true;
1178  }
1179 
1180  // Precompute next error indicator point
1181  // This will allow us to figure out if the greedy algorithm has terminated
1182  // early without needing an extra call to the error indicator function.
1184 }
1185 
1186 void
1188 {
1189  d_convergence_points.clear();
1191 }
1192 
1193 void
1195 {
1196  d_convergence_started = true;
1197  d_max_error = 0;
1198  d_counter = 0;
1199  d_subset_counter = 0;
1200 
1201  if (d_rank == 0)
1202  {
1203  std::string str;
1204  str += "Error indicator tolerance " + std::to_string(d_error_indicator_tol) +
1205  " met. Computing convergence.\n";
1206  agnosticPrint(str);
1207  }
1208 
1209  // Precompute next error indicator point
1210  // This will allow us to figure out if the greedy algorithm has terminated
1211  // early without needing an extra call to the error indicator function.
1213 }
1214 
1215 std::vector<Vector>
1217 {
1218  std::vector<Vector> random_points;
1219 
1220  std::vector<std::uniform_real_distribution<double>> unif;
1221  for (int i = 0; i < d_min_param_point.dim(); i++)
1222  {
1223  unif.push_back(std::uniform_real_distribution<double>(d_min_param_point.item(i),
1224  d_max_param_point.item(i)));
1225  }
1226 
1227  for (int i = 0; i < num_points; i++)
1228  {
1229  Vector point(d_min_param_point.dim(), false);
1230  for (int j = 0; j < point.dim(); j++)
1231  {
1232  point.item(j) = unif[j](rng);
1233  }
1234  random_points.push_back(point);
1235  }
1236  return random_points;
1237 }
1238 
1239 std::shared_ptr<Vector>
1241 {
1242 
1243  CAROM_VERIFY(point.dim() == d_parameter_points[0].dim());
1244 
1245  double closest_dist_to_points = INT_MAX;
1246  int closest_point_index = -1;
1247 
1248  for (auto itr = d_parameter_sampled_indices.begin();
1249  itr != d_parameter_sampled_indices.end(); ++itr) {
1250  Vector diff;
1251  point.minus(d_parameter_points[*itr], diff);
1252  double dist = diff.norm();
1253  if (dist < closest_dist_to_points)
1254  {
1255  closest_dist_to_points = dist;
1256  closest_point_index = *itr;
1257  }
1258  }
1259 
1260  if (closest_point_index == -1)
1261  {
1262  return std::shared_ptr<Vector>(nullptr);
1263  }
1264 
1265  Vector* result = new Vector(d_parameter_points[closest_point_index]);
1266  return std::shared_ptr<Vector>(result);
1267 }
1268 
1269 int
1271 {
1272  double closest_dist_to_points = INT_MAX;
1273  int closest_point_index = -1;
1274 
1275  for (int i = 0; i < d_parameter_points.size(); i++)
1276  {
1277  auto search = d_parameter_sampled_indices.find(i);
1278  if (search == d_parameter_sampled_indices.end())
1279  {
1280  Vector diff;
1281  point.minus(d_parameter_points[i], diff);
1282  double dist = diff.norm();
1283  if (dist < closest_dist_to_points)
1284  {
1285  closest_dist_to_points = dist;
1286  closest_point_index = i;
1287  }
1288  }
1289  }
1290 
1291  return closest_point_index;
1292 }
1293 
1294 int
1296 {
1297 
1298  CAROM_VERIFY(index >= 0 && index < d_parameter_points.size());
1299 
1300  double closest_dist_to_points = INT_MAX;
1301  int closest_point_index = -1;
1302 
1303  for (auto itr = d_parameter_sampled_indices.begin();
1304  itr != d_parameter_sampled_indices.end(); ++itr) {
1305  if (index != *itr)
1306  {
1307  Vector diff;
1308  d_parameter_points[index].minus(d_parameter_points[*itr], diff);
1309  double dist = diff.norm();
1310  if (dist < closest_dist_to_points ||
1311  (dist == closest_dist_to_points && *itr == d_parameter_point_local_rom[index]))
1312  {
1313  closest_dist_to_points = dist;
1314  closest_point_index = *itr;
1315  }
1316  }
1317  else if (!ignore_self)
1318  {
1319  closest_dist_to_points = 0;
1320  closest_point_index = *itr;
1321  break;
1322  }
1323  }
1324 
1325  return closest_point_index;
1326 }
1327 
1328 std::vector<Vector>
1330 {
1331  return d_parameter_points;
1332 }
1333 
1334 std::vector<Vector>
1336 {
1337  std::vector<Vector> sampled_points;
1338  for (auto itr = d_parameter_sampled_indices.begin();
1339  itr != d_parameter_sampled_indices.end(); ++itr) {
1340  sampled_points.push_back(d_parameter_points[*itr]);
1341  }
1342  return sampled_points;
1343 }
1344 
1345 void
1346 GreedySampler::save(std::string base_file_name)
1347 {
1348  CAROM_ASSERT(!base_file_name.empty());
1349 
1350  if (d_rank == 0)
1351  {
1352  char tmp[100];
1353  std::string full_file_name = base_file_name;
1354  HDFDatabase database;
1355  database.create(full_file_name);
1356 
1357  sprintf(tmp, "num_parameter_points");
1358  database.putInteger(tmp, d_parameter_points.size());
1359  for (int i = 0; i < d_parameter_points.size(); i++)
1360  {
1361  std::string vec_path = base_file_name + "_" + std::to_string(i);
1362  d_parameter_points[i].write(vec_path);
1363  }
1364  for (int i = 0; i < d_convergence_points.size(); i++)
1365  {
1366  std::string vec_path = base_file_name + "_conv_" + std::to_string(i);
1367  d_convergence_points[i].write(vec_path);
1368  }
1369 
1370  std::string vec_path = base_file_name + "_min_point";
1371  d_min_param_point.write(vec_path);
1372 
1373  vec_path = base_file_name + "_max_point";
1374  d_max_param_point.write(vec_path);
1375 
1376  sprintf(tmp, "num_parameter_sampled_indices");
1377  database.putInteger(tmp, d_parameter_sampled_indices.size());
1378  if (d_parameter_sampled_indices.size() > 0)
1379  {
1380  sprintf(tmp, "parameter_sampled_indices");
1381  std::vector<int> d_parameter_sampled_indices_vec(
1383  database.putIntegerArray(tmp, &d_parameter_sampled_indices_vec[0],
1385  }
1386 
1387  sprintf(tmp, "procedure_completed");
1388  database.putInteger(tmp, d_procedure_completed);
1389 
1390  if (!d_procedure_completed)
1391  {
1392  sprintf(tmp, "max_error");
1393  database.putDouble(tmp, d_max_error);
1394  sprintf(tmp, "curr_relative_error");
1395  database.putDouble(tmp, d_curr_relative_error);
1396  sprintf(tmp, "error_indicator_tol");
1397  database.putDouble(tmp, d_error_indicator_tol);
1398  sprintf(tmp, "relative_error_tol");
1399  database.putDouble(tmp, d_relative_error_tol);
1400  sprintf(tmp, "alpha");
1401  database.putDouble(tmp, d_alpha);
1402  sprintf(tmp, "max_clamp");
1403  database.putDouble(tmp, d_max_clamp);
1404  sprintf(tmp, "max_num_parameter_points");
1405  database.putInteger(tmp, d_num_parameter_points);
1406  sprintf(tmp, "subset_size");
1407  database.putInteger(tmp, d_subset_size);
1408  sprintf(tmp, "convergence_subset_size");
1409  database.putInteger(tmp, d_convergence_subset_size);
1410  sprintf(tmp, "next_point_to_sample");
1411  database.putInteger(tmp, d_next_point_to_sample);
1412  sprintf(tmp, "next_point_requiring_error_indicator");
1414  sprintf(tmp, "check_local_rom");
1415  database.putInteger(tmp, d_check_local_rom);
1416  sprintf(tmp, "use_centroid");
1417  database.putInteger(tmp, d_use_centroid);
1418  sprintf(tmp, "iteration_started");
1419  database.putInteger(tmp, d_iteration_started);
1420  sprintf(tmp, "convergence_started");
1421  database.putInteger(tmp, d_convergence_started);
1422  sprintf(tmp, "next_parameter_point_computed");
1424  sprintf(tmp, "point_requiring_error_indicator_computed");
1426  sprintf(tmp, "subset_created");
1427  database.putInteger(tmp, d_subset_created);
1428  sprintf(tmp, "debug_algorithm");
1429  database.putInteger(tmp, d_debug_algorithm);
1430  sprintf(tmp, "counter");
1431  database.putInteger(tmp, d_counter);
1432  sprintf(tmp, "subset_counter");
1433  database.putInteger(tmp, d_subset_counter);
1434  sprintf(tmp, "random_seed");
1435  database.putInteger(tmp, d_random_seed);
1436 
1437  sprintf(tmp, "parameter_point_random_indices");
1440  sprintf(tmp, "parameter_point_errors");
1441  database.putDoubleArray(tmp, &d_parameter_point_errors[0],
1442  d_parameter_point_errors.size());
1443  sprintf(tmp, "parameter_point_local_rom");
1444  database.putIntegerArray(tmp, &d_parameter_point_local_rom[0],
1446  }
1447  database.close();
1448  }
1449  MPI_Barrier(MPI_COMM_WORLD);
1450 }
1451 
1452 bool
1454 {
1457  {
1458  if (d_rank == 0)
1459  {
1460  std::string str;
1461  str += "Maximum number of parameter points reached. Stopping now.\n";
1462  agnosticPrint(str);
1463  }
1464  d_procedure_completed = true;
1465  }
1466  return d_procedure_completed;
1467 }
1468 
1470 {
1471 }
1472 
1473 }
void getInteger(const std::string &key, int &data)
Reads an integer associated with the supplied key from the currently open database file.
Definition: Database.h:182
void putDouble(const std::string &key, double data)
Writes a double associated with the supplied key to currently open database file.
Definition: Database.h:129
void putInteger(const std::string &key, int data)
Writes an integer associated with the supplied key to currently open database file.
Definition: Database.h:95
void getDouble(const std::string &key, double &data)
Reads a double associated with the supplied key from the currently open database file.
Definition: Database.h:216
void printConvergenceAchieved()
Print that convergence was achieved.
void setPointRelativeError(double error)
Set the relative error of the specified point.
GreedySampler(std::vector< Vector > parameter_points, bool check_local_rom, double relative_error_tolerance, double alpha, double max_clamp, int subset_size, int convergence_subset_size, std::string output_log_path="", std::string warm_start_file_name="", bool use_centroid=true, int random_seed=1, bool debug_algorithm=false)
Constructor.
void generateConvergenceSubset()
Generate a new set of convergence points.
double d_max_error
The current max error of the parameter points of the current iteration.
virtual void save(std::string base_file_name)
Save the object state to a file.
bool d_subset_created
Whether the database has already created a random subset for this iteration.
int d_next_point_requiring_error_indicator
The next parameter point requiring a error indicator.
void printSamplingType(std::string sampling_type)
Print the sampling type.
void setSubsetErrorIndicator(double proc_errors)
Set the error indicator for a subset point.
int d_subset_counter
An internal subset counter.
double d_max_clamp
The max_clamp constant.
bool d_use_centroid
Whether the use the centroid heuristic for obtaining the first parameter point.
bool d_point_requiring_error_indicator_computed
Whether the database has already computed a new paramter point requiring a error indicator.
void setConvergenceErrorIndicator(double proc_errors)
Set the error indicator for a convergence point.
bool isComplete()
Check if the greedy algorithm procedure is complete.
int d_random_seed
Then random seed used to generate subsets.
int d_subset_size
The size of the subset of parameter points used per iteration.
bool d_debug_algorithm
Turn off randomness for debugging purposes.
std::vector< Vector > getParameterPointDomain()
Get the domain of the parameter points.
double d_relative_error_tol
The relative error tolerance used to terminate the greedy procedure.
std::string d_output_log_path
Output log path.
void constructObject(bool check_local_rom, double relative_error_tolerance, double alpha, double max_clamp, int subset_size, int convergence_subset_size, std::string output_log_path, bool use_centroid, int random_seed, bool debug_algorithm)
Construct the GreedySampler object.
bool d_check_local_rom
Whether the check the last sampled local ROM's error indicator after each iteration.
std::vector< int > d_parameter_point_random_indices
The parameter point indices (used to generate the random subsets).
virtual void getNextParameterPointAfterConvergenceFailure()=0
Get the next parameter point to sample after a convergence failure.
std::vector< Vector > d_convergence_points
The convergence parameter points to explore.
std::vector< Vector > d_parameter_points
The parameter points to explore.
int d_rank
The rank of the given processor.
double d_curr_relative_error
The current relative error of the current iteration.
std::vector< double > d_parameter_point_errors
The current errors of the parameter points.
void initializeParameterPoints()
Initialize the list of parameter point candidates to sample.
int d_counter
An internal counter.
bool d_next_parameter_point_computed
Whether the database has already computed a new parameter point to sample.
~GreedySampler()
Destructor.
void setPointErrorIndicator(double error, int vec_size)
Set the error indicator error of the specified parameter point.
std::default_random_engine rng
Random engine used to generate subsets.
std::vector< int > d_parameter_point_local_rom
The local ROMs used to obtain the current errors of the parameter points.
int d_next_point_to_sample
The next parameter point to sample.
void agnosticPrint(std::string str)
Print to output_log_file or cout.
int d_num_parameter_points
The maximum number of parameter points to sample.
std::shared_ptr< Vector > getNearestROM(Vector point)
Returns the nearest local ROM to the specified parameter point.
double d_error_indicator_tol
The error indicator tolerance used to terminate the greedy procedure.
virtual void load(std::string base_file_name)
Load the object state from a file.
int getNearestROMIndexToParameterPoint(int index, bool ignore_self)
Returns the index to the nearest local ROM to the specified parameter point index in the parameter po...
bool d_procedure_completed
Whether the greedy procedure has completed.
int d_convergence_subset_size
The size of the subset of parameter points used to check convergence.
bool d_convergence_started
Whether the database is in the convergence verifying phase.
void printErrorIndicatorToleranceNotMet()
Print that the error indicator was not met.
double d_alpha
The alpha constant.
std::vector< Vector > getSampledParameterPoints()
Get the sampled parameter points.
Vector d_max_param_point
The maximum value of the parameter space.
std::set< int > d_parameter_sampled_indices
The parameter points that were already sampled.
void startConvergence()
Switch to convergence mode.
Vector d_min_param_point
The minimum value of the parameter space.
void addDatabaseFromFile(std::string const &warm_start_file_name)
Do a warm start by adding the sampled parameters from the database in a file to the current database.
std::shared_ptr< Vector > getNextParameterPoint()
Returns the next parameter point for which sampling is required.
void printErrorIndicator(Vector errorIndicatorPoint, double proc_errors)
Print the error indicator.
struct GreedyErrorIndicatorPoint getNextPointRequiringErrorIndicator()
Returns the next parameter point for which an error indicator is required.
bool d_iteration_started
Whether the database has already computed a new parameter point for the current iteration.
void checkParameterPointInput()
Construct the list of parameter point candidates to sample.
std::vector< Vector > generateRandomPoints(int num_points)
Generate a vector of random points.
int getNearestNonSampledPoint(Vector point)
Returns the index of the nearest non-sampled parameter point to the given point.
virtual void getIntegerArray(const std::string &key, int *data, int nelements, const bool distributed=false)
Reads an array of integers associated with the supplied key from the currently open HDF5 database fil...
virtual bool create(const std::string &file_name, const MPI_Comm comm=MPI_COMM_NULL) override
Creates a new HDF5 database file with the supplied name.
Definition: HDFDatabase.cpp:45
virtual bool open(const std::string &file_name, const std::string &type, const MPI_Comm comm=MPI_COMM_NULL) override
Opens an existing HDF5 database file with the supplied name.
Definition: HDFDatabase.cpp:76
virtual void getDoubleArray(const std::string &key, double *data, int nelements, const bool distributed=false)
Reads an array of doubles associated with the supplied key from the currently open HDF5 database file...
virtual void putDoubleArray(const std::string &key, const double *const data, int nelements, const bool distributed=false)
Writes an array of doubles associated with the supplied key to the currently open HDF5 database file.
virtual void putIntegerArray(const std::string &key, const int *const data, int nelements, const bool distributed=false)
Writes an array of integers associated with the supplied key to the currently open HDF5 database file...
virtual bool close()
Closes the currently open HDF5 database file.
double norm() const
Form the norm of this.
Definition: Vector.cpp:242
Vector * minus(const Vector &other) const
Subtracts other and this and returns the result, reference version.
Definition: Vector.h:538
void write(const std::string &base_file_name)
write Vector into (a) HDF file(s).
Definition: Vector.cpp:446
const double & item(int i) const
Const Vector member access.
Definition: Vector.h:656
void local_read(const std::string &base_file_name, int rank)
read read a single rank of a distributed Vector from (a) HDF file(s).
Definition: Vector.cpp:533
int dim() const
Returns the dimension of the Vector on this processor.
Definition: Vector.h:266
Vector getNearestPoint(std::vector< Vector > param_points, Vector point)
Given a vector, find the nearest point in a domain.
struct GreedyErrorIndicatorPoint createGreedyErrorIndicatorPoint(Vector *point, Vector *localROM)
Create a greedy error indicator point.
int getNearestPointIndex(std::vector< Vector > param_points, Vector point)
Given a vector, find the nearest point in a domain.
int getCenterPoint(std::vector< Vector * > &points, bool use_centroid)
Get center point of a group of points.
Definition: Vector.cpp:580
std::shared_ptr< Vector > localROM
The parameter point of the closest local ROM.
Definition: GreedySampler.h:65
std::shared_ptr< Vector > point
The parameter point to calculate the error indicator.
Definition: GreedySampler.h:60