libMesh
rb_construction.C
Go to the documentation of this file.
1 // rbOOmit: An implementation of the Certified Reduced Basis method.
2 // Copyright (C) 2009, 2010 David J. Knezevic
3 
4 // This file is part of rbOOmit.
5 
6 // rbOOmit is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 
11 // rbOOmit is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20 // rbOOmit includes
21 #include "libmesh/rb_construction_base.h"
22 #include "libmesh/rb_construction.h"
23 #include "libmesh/rb_assembly_expansion.h"
24 #include "libmesh/rb_evaluation.h"
25 #include "libmesh/elem_assembly.h"
26 
27 // LibMesh includes
28 #include "libmesh/numeric_vector.h"
29 #include "libmesh/sparse_matrix.h"
30 #include "libmesh/dof_map.h"
31 #include "libmesh/libmesh_logging.h"
32 #include "libmesh/equation_systems.h"
33 #include "libmesh/exodusII_io.h"
34 #include "libmesh/gmv_io.h"
35 #include "libmesh/linear_solver.h"
36 #include "libmesh/getpot.h"
37 #include "libmesh/int_range.h"
38 #include "libmesh/mesh_base.h"
39 #include "libmesh/parallel.h"
40 #include "libmesh/xdr_cxx.h"
41 #include "libmesh/timestamp.h"
42 #include "libmesh/petsc_linear_solver.h"
43 #include "libmesh/dg_fem_context.h"
44 #include "libmesh/dirichlet_boundaries.h"
45 #include "libmesh/zero_function.h"
46 #include "libmesh/coupling_matrix.h"
47 #include "libmesh/face_tri3_subdivision.h"
48 #include "libmesh/quadrature.h"
49 #include "libmesh/utility.h"
50 
51 // C++ includes
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #include <errno.h>
55 #include <fstream>
56 #include <sstream>
57 #include <limits>
58 #include <stdlib.h> // mkstemps on Linux
59 #ifdef LIBMESH_HAVE_UNISTD_H
60 #include <unistd.h> // mkstemps on MacOS
61 #endif
62 
63 namespace libMesh
64 {
65 
67  const std::string & name_in,
68  const unsigned int number_in)
69  : Parent(es, name_in, number_in),
70  inner_product_solver(LinearSolver<Number>::build(es.comm())),
71  extra_linear_solver(nullptr),
72  inner_product_matrix(SparseMatrix<Number>::build(es.comm())),
73  skip_residual_in_train_reduced_basis(false),
74  exit_on_repeated_greedy_parameters(true),
75  impose_internal_fluxes(false),
76  skip_degenerate_sides(true),
77  compute_RB_inner_product(false),
78  store_non_dirichlet_operators(false),
79  store_untransformed_basis(false),
80  use_empty_rb_solve_in_greedy(true),
81  Fq_representor_innerprods_computed(false),
82  Nmax(0),
83  delta_N(1),
84  output_dual_innerprods_computed(false),
85  assert_convergence(true),
86  rb_eval(nullptr),
87  inner_product_assembly(nullptr),
88  use_energy_inner_product(false),
89  rel_training_tolerance(1.e-4),
90  abs_training_tolerance(1.e-12),
91  normalize_rb_bound_in_greedy(false),
92  RB_training_type("Greedy"),
93  _preevaluate_thetas_flag(false),
94  _preevaluate_thetas_completed(false)
95 {
96  // set assemble_before_solve flag to false
97  // so that we control matrix assembly.
98  assemble_before_solve = false;
99 }
100 
102 
104 {
105  LOG_SCOPE("clear()", "RBConstruction");
106 
107  Parent::clear();
108 
109  Aq_vector.clear();
110  Fq_vector.clear();
111  outputs_vector.clear();
112 
114  {
115  non_dirichlet_Aq_vector.clear();
116  non_dirichlet_Fq_vector.clear();
118  }
119 
120  // Also delete the Fq representors
121  Fq_representor.clear();
122 
123  // Set Fq_representor_innerprods_computed flag to false now
124  // that we've cleared the Fq representors
126 }
127 
128 std::string RBConstruction::system_type () const
129 {
130  return "RBConstruction";
131 }
132 
134  SparseMatrix<Number> & input_matrix,
135  NumericVector<Number> & input_rhs)
136 {
137  // This is similar to LinearImplicitSysmte::solve()
138 
139  // Get a reference to the EquationSystems
140  const EquationSystems & es =
141  this->get_equation_systems();
142 
143  // If the linear solver hasn't been initialized, we do so here.
144  input_solver.init();
145 
146  // Get the user-specifiied linear solver tolerance
147  const double tol =
148  double(es.parameters.get<Real>("linear solver tolerance"));
149 
150  // Get the user-specified maximum # of linear solver iterations
151  const unsigned int maxits =
152  es.parameters.get<unsigned int>("linear solver maximum iterations");
153 
154  // It's good practice to clear the solution vector first since it can
155  // affect convergence of iterative solvers
156  solution->zero();
157 
158  // Solve the linear system.
159  // Store the number of linear iterations required to
160  // solve and the final residual.
162  input_solver.solve (input_matrix, *solution, input_rhs, tol, maxits);
163 
165 
166  // Update the system after the solve
167  this->update();
168 }
169 
171 {
172  rb_eval = &rb_eval_in;
173 }
174 
176 {
177  libmesh_error_msg_if(!rb_eval, "Error: RBEvaluation object hasn't been initialized yet");
178 
179  return *rb_eval;
180 }
181 
183 {
184  libmesh_error_msg_if(!rb_eval, "Error: RBEvaluation object hasn't been initialized yet");
185 
186  return *rb_eval;
187 }
188 
190 {
191  return (rb_eval != nullptr);
192 }
193 
195 {
197 }
198 
200 {
202 }
203 
204 void RBConstruction::process_parameters_file (const std::string & parameters_filename)
205 {
206  // First read in data from input_filename
207  GetPot infile(parameters_filename);
208 
209  const unsigned int n_training_samples = infile("n_training_samples",0);
210  const bool deterministic_training = infile("deterministic_training",false);
211  unsigned int training_parameters_random_seed_in =
212  static_cast<unsigned int>(-1);
213  training_parameters_random_seed_in = infile("training_parameters_random_seed",
214  training_parameters_random_seed_in);
215  const bool quiet_mode_in = infile("quiet_mode", quiet_mode);
216  const unsigned int Nmax_in = infile("Nmax", Nmax);
217  const Real rel_training_tolerance_in = infile("rel_training_tolerance",
219  const Real abs_training_tolerance_in = infile("abs_training_tolerance",
221 
222  // Initialize value to false, let the input file value override.
223  const bool normalize_rb_bound_in_greedy_in = infile("normalize_rb_bound_in_greedy",
224  false);
225 
226  const std::string RB_training_type_in = infile("RB_training_type", "Greedy");
227 
228  // Read in the parameters from the input file too
229  unsigned int n_continuous_parameters = infile.vector_variable_size("parameter_names");
230  RBParameters mu_min_in;
231  RBParameters mu_max_in;
232  for (unsigned int i=0; i<n_continuous_parameters; i++)
233  {
234  // Read in the parameter names
235  std::string param_name = infile("parameter_names", "NONE", i);
236 
237  {
238  Real min_val = infile(param_name, 0., 0);
239  mu_min_in.set_value(param_name, min_val);
240  }
241 
242  {
243  Real max_val = infile(param_name, 0., 1);
244  mu_max_in.set_value(param_name, max_val);
245  }
246  }
247 
248  std::map<std::string, std::vector<Real>> discrete_parameter_values_in;
249 
250  unsigned int n_discrete_parameters = infile.vector_variable_size("discrete_parameter_names");
251  for (unsigned int i=0; i<n_discrete_parameters; i++)
252  {
253  std::string param_name = infile("discrete_parameter_names", "NONE", i);
254 
255  unsigned int n_vals_for_param = infile.vector_variable_size(param_name);
256  std::vector<Real> vals_for_param(n_vals_for_param);
257  for (auto j : make_range(vals_for_param.size()))
258  vals_for_param[j] = infile(param_name, 0., j);
259 
260  discrete_parameter_values_in[param_name] = vals_for_param;
261  }
262 
263  std::map<std::string,bool> log_scaling_in;
264  // For now, just set all entries to false.
265  // TODO: Implement a decent way to specify log-scaling true/false
266  // in the input text file
267  for (const auto & pr : mu_min_in)
268  log_scaling_in[pr.first] = false;
269 
270  // Set the parameters that have been read in
271  set_rb_construction_parameters(n_training_samples,
272  deterministic_training,
273  static_cast<int>(training_parameters_random_seed_in),
274  quiet_mode_in,
275  Nmax_in,
276  rel_training_tolerance_in,
277  abs_training_tolerance_in,
278  normalize_rb_bound_in_greedy_in,
279  RB_training_type_in,
280  mu_min_in,
281  mu_max_in,
282  discrete_parameter_values_in,
283  log_scaling_in);
284 }
285 
287  unsigned int n_training_samples_in,
288  bool deterministic_training_in,
289  int training_parameters_random_seed_in,
290  bool quiet_mode_in,
291  unsigned int Nmax_in,
292  Real rel_training_tolerance_in,
293  Real abs_training_tolerance_in,
294  bool normalize_rb_bound_in_greedy_in,
295  const std::string & RB_training_type_in,
296  const RBParameters & mu_min_in,
297  const RBParameters & mu_max_in,
298  const std::map<std::string, std::vector<Real>> & discrete_parameter_values_in,
299  const std::map<std::string,bool> & log_scaling_in,
300  std::map<std::string, std::vector<RBParameter>> * training_sample_list)
301 {
302  // Read in training_parameters_random_seed value. This is used to
303  // seed the RNG when picking the training parameters. By default the
304  // value is -1, which means use std::time to seed the RNG.
305  set_training_random_seed(training_parameters_random_seed_in);
306 
307  // Set quiet mode
308  set_quiet_mode(quiet_mode_in);
309 
310  // Initialize RB parameters
311  set_Nmax(Nmax_in);
312 
313  set_rel_training_tolerance(rel_training_tolerance_in);
314  set_abs_training_tolerance(abs_training_tolerance_in);
315 
316  set_normalize_rb_bound_in_greedy(normalize_rb_bound_in_greedy_in);
317 
318  set_RB_training_type(RB_training_type_in);
319 
320  // Initialize the parameter ranges and the parameters themselves
321  initialize_parameters(mu_min_in, mu_max_in, discrete_parameter_values_in);
322 
323  bool updated_deterministic_training = deterministic_training_in;
324  if (training_sample_list && (this->get_parameters_min().n_parameters() > 3))
325  {
326  // In this case we force deterministic_training to be false because
327  // a) deterministic training samples are not currrently supported with
328  // more than 3 parameters, and
329  // b) we will overwrite the training samples anyway in the call to
330  // load_training_set() below, so we do not want to generate an
331  // error due to deterministic training sample generation when
332  // the samples will be overwritten anyway.
333  updated_deterministic_training = false;
334  }
335 
337  this->get_parameters_max(),
338  n_training_samples_in,
339  log_scaling_in,
340  updated_deterministic_training); // use deterministic parameters
341 
342  if (training_sample_list)
343  {
344  // Note that we must call initialize_training_parameters() before
345  // load_training_set() in order to initialize the parameter vectors.
346  load_training_set(*training_sample_list);
347  }
348 }
349 
351 {
352  // Print out info that describes the current setup
353  libMesh::out << std::endl << "RBConstruction parameters:" << std::endl;
354  libMesh::out << "system name: " << this->name() << std::endl;
355  libMesh::out << "Nmax: " << Nmax << std::endl;
356  libMesh::out << "Greedy relative error tolerance: " << get_rel_training_tolerance() << std::endl;
357  libMesh::out << "Greedy absolute error tolerance: " << get_abs_training_tolerance() << std::endl;
358  libMesh::out << "Do we normalize RB error bound in greedy? " << get_normalize_rb_bound_in_greedy() << std::endl;
359  libMesh::out << "RB training type: " << get_RB_training_type() << std::endl;
361  {
362  libMesh::out << "Aq operators attached: " << get_rb_theta_expansion().get_n_A_terms() << std::endl;
363  libMesh::out << "Fq functions attached: " << get_rb_theta_expansion().get_n_F_terms() << std::endl;
364  libMesh::out << "n_outputs: " << get_rb_theta_expansion().get_n_outputs() << std::endl;
365  for (unsigned int n=0; n<get_rb_theta_expansion().get_n_outputs(); n++)
366  libMesh::out << "output " << n << ", Q_l = " << get_rb_theta_expansion().get_n_output_terms(n) << std::endl;
367  }
368  else
369  {
370  libMesh::out << "RBThetaExpansion member is not set yet" << std::endl;
371  }
372  libMesh::out << "Number of parameters: " << get_n_params() << std::endl;
373  for (const auto & pr : get_parameters())
374  if (!is_discrete_parameter(pr.first))
375  {
376  libMesh::out << "Parameter " << pr.first
377  << ": Min = " << get_parameter_min(pr.first)
378  << ", Max = " << get_parameter_max(pr.first) << std::endl;
379  }
380 
382  libMesh::out << "n_training_samples: " << get_n_training_samples() << std::endl;
383  libMesh::out << "quiet mode? " << is_quiet() << std::endl;
384  libMesh::out << std::endl;
385 }
386 
388 {
389  std::unique_ptr<NumericVector<Number>> temp = solution->clone();
390 
391  for (unsigned int i=0; i<get_rb_evaluation().get_n_basis_functions(); i++)
392  {
393  for (unsigned int j=0; j<get_rb_evaluation().get_n_basis_functions(); j++)
394  {
396  Number value = temp->dot( get_rb_evaluation().get_basis_function(i) );
397 
398  libMesh::out << value << " ";
399  }
400  libMesh::out << std::endl;
401  }
402  libMesh::out << std::endl;
403 }
404 
406 {
407  rb_assembly_expansion = &rb_assembly_expansion_in;
408 }
409 
411 {
412  libmesh_error_msg_if(!rb_assembly_expansion, "Error: RBAssemblyExpansion object hasn't been initialized yet");
413 
414  return *rb_assembly_expansion;
415 }
416 
418 {
419  use_energy_inner_product = false;
420  inner_product_assembly = &inner_product_assembly_in;
421 }
422 
424 {
425  libmesh_error_msg_if(use_energy_inner_product,
426  "Error: inner_product_assembly not available since we're using energy inner-product");
427 
428  libmesh_error_msg_if(!inner_product_assembly,
429  "Error: inner_product_assembly hasn't been initialized yet");
430 
431  return *inner_product_assembly;
432 }
433 
434 void RBConstruction::set_energy_inner_product(const std::vector<Number> & energy_inner_product_coeffs_in)
435 {
437  energy_inner_product_coeffs = energy_inner_product_coeffs_in;
438 }
439 
441 {
442 #ifdef LIBMESH_ENABLE_CONSTRAINTS
443  const DofMap & dof_map = get_dof_map();
444 
445  for (dof_id_type i=dof_map.first_dof(); i<dof_map.end_dof(); i++)
446  {
448  {
449  vector.set(i, 0.);
450  }
451  }
452 #endif
453 
454  vector.close();
455 }
456 
458 {
459  return (solution->l2_norm() == 0.);
460 }
461 
462 void RBConstruction::initialize_rb_construction(bool skip_matrix_assembly,
463  bool skip_vector_assembly)
464 {
465  if (!skip_matrix_assembly && !skip_vector_assembly)
466  {
467  // Check that the theta and assembly objects are consistently sized
468  libmesh_assert_equal_to (get_rb_theta_expansion().get_n_A_terms(), get_rb_assembly_expansion().get_n_A_terms());
469  libmesh_assert_equal_to (get_rb_theta_expansion().get_n_F_terms(), get_rb_assembly_expansion().get_n_F_terms());
470  libmesh_assert_equal_to (get_rb_theta_expansion().get_n_outputs(), get_rb_assembly_expansion().get_n_outputs());
471  for (unsigned int i=0; i<get_rb_theta_expansion().get_n_outputs(); i++)
472  {
473  libmesh_assert_equal_to (get_rb_theta_expansion().get_n_output_terms(i),
474  get_rb_assembly_expansion().get_n_output_terms(i));
475  }
476  }
477 
478  // Perform the initialization
480  assemble_affine_expansion(skip_matrix_assembly, skip_vector_assembly);
481 
482  // inner_product_solver performs solves with the same matrix every time
483  // hence we can set reuse_preconditioner(true).
484  inner_product_solver->reuse_preconditioner(true);
485 
486  // The primary solver is used for truth solves and other solves that
487  // require different matrices, so set reuse_preconditioner(false).
489 
490 }
491 
492 void RBConstruction::assemble_affine_expansion(bool skip_matrix_assembly,
493  bool skip_vector_assembly)
494 {
495  if (!skip_matrix_assembly)
496  {
497  // Assemble and store all of the matrices
498  this->assemble_misc_matrices();
500  }
501 
502  if (!skip_vector_assembly)
503  {
504  // Assemble and store all of the vectors
507  }
508 }
509 
511 {
512  // Resize vectors for storing mesh-dependent data
513  Aq_vector.resize(get_rb_theta_expansion().get_n_A_terms());
514  Fq_vector.resize(get_rb_theta_expansion().get_n_F_terms());
515 
516  // Resize the Fq_representors and initialize each to nullptr.
517  // These are basis independent and hence stored here, whereas
518  // the Aq_representors are stored in RBEvaluation
519  Fq_representor.resize(get_rb_theta_expansion().get_n_F_terms());
520 
521  // Initialize vectors for the inner products of the Fq representors
522  // These are basis independent and therefore stored here.
523  unsigned int Q_f_hat = get_rb_theta_expansion().get_n_F_terms()*(get_rb_theta_expansion().get_n_F_terms()+1)/2;
524  Fq_representor_innerprods.resize(Q_f_hat);
525 
526  // Resize the output vectors
527  outputs_vector.resize(get_rb_theta_expansion().get_n_outputs());
528  for (unsigned int n=0; n<get_rb_theta_expansion().get_n_outputs(); n++)
530 
531  // Resize the output dual norm vectors
532  output_dual_innerprods.resize(get_rb_theta_expansion().get_n_outputs());
533  for (unsigned int n=0; n<get_rb_theta_expansion().get_n_outputs(); n++)
534  {
536  output_dual_innerprods[n].resize(Q_l_hat);
537  }
538 
539  {
540  DofMap & dof_map = this->get_dof_map();
541 
543  inner_product_matrix->init();
544  inner_product_matrix->zero();
545 
547  {
548  // We also need a non-Dirichlet inner-product matrix
553  }
554 
555  for (unsigned int q=0; q<get_rb_theta_expansion().get_n_A_terms(); q++)
556  {
557  // Initialize the memory for the matrices
559  dof_map.attach_matrix(*Aq_vector[q]);
560  Aq_vector[q]->init();
561  Aq_vector[q]->zero();
562  }
563 
564  // We also need to initialize a second set of non-Dirichlet operators
566  {
567  non_dirichlet_Aq_vector.resize(get_rb_theta_expansion().get_n_A_terms());
568  for (unsigned int q=0; q<get_rb_theta_expansion().get_n_A_terms(); q++)
569  {
570  // Initialize the memory for the matrices
573  non_dirichlet_Aq_vector[q]->init();
574  non_dirichlet_Aq_vector[q]->zero();
575  }
576  }
577  }
578 
579  // Initialize the vectors
580  for (unsigned int q=0; q<get_rb_theta_expansion().get_n_F_terms(); q++)
581  {
582  // Initialize the memory for the vectors
584  Fq_vector[q]->init (this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
585  }
586 
587  // We also need to initialize a second set of non-Dirichlet operators
589  {
590  non_dirichlet_Fq_vector.resize(get_rb_theta_expansion().get_n_F_terms());
591  for (unsigned int q=0; q<get_rb_theta_expansion().get_n_F_terms(); q++)
592  {
593  // Initialize the memory for the vectors
595  non_dirichlet_Fq_vector[q]->init (this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
596  }
597  }
598 
599  for (unsigned int n=0; n<get_rb_theta_expansion().get_n_outputs(); n++)
600  for (unsigned int q_l=0; q_l<get_rb_theta_expansion().get_n_output_terms(n); q_l++)
601  {
602  // Initialize the memory for the truth output vectors
604  outputs_vector[n][q_l]->init (this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
605  }
606 
608  {
609  non_dirichlet_outputs_vector.resize(get_rb_theta_expansion().get_n_outputs());
610  for (unsigned int n=0; n<get_rb_theta_expansion().get_n_outputs(); n++)
611  {
612  non_dirichlet_outputs_vector[n].resize( get_rb_theta_expansion().get_n_output_terms(n) );
613  for (unsigned int q_l=0; q_l<get_rb_theta_expansion().get_n_output_terms(n); q_l++)
614  {
615  // Initialize the memory for the truth output vectors
617  non_dirichlet_outputs_vector[n][q_l]->init (this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
618  }
619  }
620  }
621 
622  // Resize truth_outputs vector
623  truth_outputs.resize(this->get_rb_theta_expansion().get_n_outputs());
624 }
625 
626 std::unique_ptr<DGFEMContext> RBConstruction::build_context ()
627 {
628  return std::make_unique<DGFEMContext>(*this);
629 }
630 
632  ElemAssembly * elem_assembly,
633  SparseMatrix<Number> * input_matrix,
634  NumericVector<Number> * input_vector,
635  bool symmetrize,
636  bool apply_dof_constraints)
637 {
638  LOG_SCOPE("add_scaled_matrix_and_vector()", "RBConstruction");
639 
640  bool assemble_matrix = (input_matrix != nullptr);
641  bool assemble_vector = (input_vector != nullptr);
642 
643  if (!assemble_matrix && !assemble_vector)
644  return;
645 
646  const MeshBase & mesh = this->get_mesh();
647 
648  // First add any node-based terms (e.g. point loads)
649 
650  // Make a std::set of all the nodes that are in 1 or more
651  // nodesets. We only want to call get_nodal_values() once per Node
652  // per ElemAssembly object, regardless of how many nodesets it
653  // appears in.
654  std::set<dof_id_type> nodes_with_nodesets;
655  for (const auto & t : mesh.get_boundary_info().build_node_list())
656  nodes_with_nodesets.insert(std::get<0>(t));
657 
658  // It's possible for the node assembly loop below to throw an
659  // exception on one (or some subset) of the processors. In that
660  // case, we stop assembling on the processor(s) that threw and let
661  // the other processors finish assembly. Then we synchronize to
662  // check whether an exception was thrown on _any_ processor, and if
663  // so, re-throw it on _all_ processors.
664  int nodal_assembly_threw = 0;
665 
666  libmesh_try
667  {
668 
669  for (const auto & id : nodes_with_nodesets)
670  {
671  const Node & node = mesh.node_ref(id);
672 
673  // If node is on this processor, then all dofs on node are too
674  // so we can do the add below safely
675  if (node.processor_id() == this->comm().rank())
676  {
677  // Get the values to add to the rhs vector
678  std::vector<dof_id_type> nodal_dof_indices;
679  DenseMatrix<Number> nodal_matrix;
680  DenseVector<Number> nodal_rhs;
681  elem_assembly->get_nodal_values(nodal_dof_indices,
682  nodal_matrix,
683  nodal_rhs,
684  *this,
685  node);
686 
687  // Perform any required user-defined postprocessing on
688  // the matrix and rhs.
689  //
690  // TODO: We need to postprocess node matrices and vectors
691  // in some cases (e.g. when rotations are applied to
692  // nodes), but since we don't have a FEMContext at this
693  // point we would need to have a different interface
694  // taking the DenseMatrix, DenseVector, and probably the
695  // current node that we are on...
696  // this->post_process_elem_matrix_and_vector(nodal_matrix, nodal_rhs);
697 
698  if (!nodal_dof_indices.empty())
699  {
700  if (apply_dof_constraints)
701  {
702  // Apply constraints, e.g. Dirichlet and periodic constraints
704  nodal_matrix,
705  nodal_rhs,
706  nodal_dof_indices,
707  /*asymmetric_constraint_rows*/ false);
708  }
709 
710  // Scale and add to global matrix and/or vector
711  nodal_matrix *= scalar;
712  nodal_rhs *= scalar;
713 
714  if (assemble_vector)
715  input_vector->add_vector(nodal_rhs, nodal_dof_indices);
716 
717  if (assemble_matrix)
718  input_matrix->add_matrix(nodal_matrix, nodal_dof_indices);
719  }
720  }
721  }
722  } // libmesh_try
723  libmesh_catch(...)
724  {
725  nodal_assembly_threw = 1;
726  }
727 
728  // Check for exceptions on any procs and if there is one, re-throw
729  // it on all procs.
730  this->comm().max(nodal_assembly_threw);
731 
732  if (nodal_assembly_threw)
733  libmesh_error_msg("Error during assembly in RBConstruction::add_scaled_matrix_and_vector()");
734 
735  std::unique_ptr<DGFEMContext> c = this->build_context();
736  DGFEMContext & context = cast_ref<DGFEMContext &>(*c);
737 
738  this->init_context(context);
739 
740  // It's possible for the assembly loop below to throw an exception
741  // on one (or some subset) of the processors. This can happen when
742  // e.g. the mesh contains one or more elements with negative
743  // Jacobian. In that case, we stop assembling on the processor(s)
744  // that threw and let the other processors finish assembly. Then we
745  // synchronize to check whether an exception was thrown on _any_
746  // processor, and if so, re-throw it on _all_ processors. This way,
747  // we make it easier for callers to handle exceptions in parallel
748  // during assembly: they can simply assume that the code either
749  // throws on all procs or on no procs.
750  int assembly_threw = 0;
751 
752  libmesh_try
753  {
754 
755  for (const auto & elem : mesh.active_local_element_ptr_range())
756  {
757  const ElemType elemtype = elem->type();
758 
759  if(elemtype == NODEELEM)
760  {
761  // We assume that we do not perform any assembly directly on
762  // NodeElems, so we skip the assembly calls.
763 
764  // However, in a spline basis with Dirichlet constraints on
765  // spline nodes, a constrained matrix has to take those
766  // nodes into account.
767  if (!apply_dof_constraints)
768  continue;
769  }
770 
771  // Subdivision elements need special care:
772  // - skip ghost elements
773  // - init special quadrature rule
774  std::unique_ptr<QBase> qrule;
775  if (elemtype == TRI3SUBDIVISION)
776  {
777  const Tri3Subdivision * gh_elem = static_cast<const Tri3Subdivision *> (elem);
778  if (gh_elem->is_ghost())
779  continue ;
780  // A Gauss quadrature rule for numerical integration.
781  // For subdivision shell elements, a single Gauss point per
782  // element is sufficient, hence we use extraorder = 0.
783  const int extraorder = 0;
784  FEBase * elem_fe = nullptr;
785  context.get_element_fe( 0, elem_fe );
786 
787  qrule = elem_fe->get_fe_type().default_quadrature_rule (2, extraorder);
788 
789  // Tell the finite element object to use our quadrature rule.
790  elem_fe->attach_quadrature_rule (qrule.get());
791  }
792 
793  context.pre_fe_reinit(*this, elem);
794 
795  // Do nothing in case there are no dof_indices on the current element
796  if ( context.get_dof_indices().empty() )
797  continue;
798 
799  context.elem_fe_reinit();
800 
801  if (elemtype != NODEELEM)
802  {
803  elem_assembly->interior_assembly(context);
804 
805  const unsigned char n_sides = context.get_elem().n_sides();
806  for (context.side = 0; context.side != n_sides; ++context.side)
807  {
808  // May not need to apply fluxes on non-boundary elements
809  if ((context.get_elem().neighbor_ptr(context.get_side()) != nullptr) && !impose_internal_fluxes)
810  continue;
811 
812  // skip degenerate sides with zero area
813  if( (context.get_elem().side_ptr(context.get_side())->volume() <= 0.) && skip_degenerate_sides)
814  continue;
815 
816  context.side_fe_reinit();
817  elem_assembly->boundary_assembly(context);
818 
819  if (context.dg_terms_are_active())
820  {
821  input_matrix->add_matrix (context.get_elem_elem_jacobian(),
822  context.get_dof_indices(),
823  context.get_dof_indices());
824 
825  input_matrix->add_matrix (context.get_elem_neighbor_jacobian(),
826  context.get_dof_indices(),
827  context.get_neighbor_dof_indices());
828 
829  input_matrix->add_matrix (context.get_neighbor_elem_jacobian(),
830  context.get_neighbor_dof_indices(),
831  context.get_dof_indices());
832 
833  input_matrix->add_matrix (context.get_neighbor_neighbor_jacobian(),
834  context.get_neighbor_dof_indices(),
835  context.get_neighbor_dof_indices());
836  }
837  }
838  }
839 
840  // Do any required user post-processing before symmetrizing and/or applying
841  // constraints.
842  //
843  // We only do this if apply_dof_constraints is true because we want to be
844  // able to set apply_dof_constraints=false in order to obtain a matrix
845  // A with no dof constraints or dof transformations, as opposed to C^T A C,
846  // which includes constraints and/or dof transformations. Here C refers to
847  // the matrix that imposes dof constraints and transformations on the
848  // solution u.
849  //
850  // Matrices such as A are what we store in our "non_dirichlet" operators, and
851  // they are useful for computing terms such as (C u_i)^T A (C u_j) (e.g. see
852  // update_RB_system_matrices()), where C u is the result of a "truth_solve",
853  // which includes calls to both enforce_constraints_exactly() and
854  // post_process_truth_solution(). If we use C^T A C to compute these terms then
855  // we would "double apply" the matrix C, which can give incorrect results.
856  if (apply_dof_constraints)
858 
859  // Need to symmetrize before imposing
860  // periodic constraints
861  if (assemble_matrix && symmetrize)
862  {
863  DenseMatrix<Number> Ke_transpose;
864  context.get_elem_jacobian().get_transpose(Ke_transpose);
865  context.get_elem_jacobian() += Ke_transpose;
866  context.get_elem_jacobian() *= 0.5;
867  }
868 
869  // As discussed above, we can set apply_dof_constraints=false to
870  // get A instead of C^T A C
871  if (apply_dof_constraints)
872  {
873  // Apply constraints, e.g. Dirichlet and periodic constraints
875  (context.get_elem_jacobian(),
876  context.get_elem_residual(),
877  context.get_dof_indices(),
878  /*asymmetric_constraint_rows*/ false );
879  }
880 
881  // Scale and add to global matrix and/or vector
882  context.get_elem_jacobian() *= scalar;
883  context.get_elem_residual() *= scalar;
884 
885  if (assemble_matrix)
886  {
887 
888  CouplingMatrix * coupling_matrix = get_dof_map()._dof_coupling;
889  if (!coupling_matrix)
890  {
891  // If we haven't defined a _dof_coupling matrix then just add
892  // the whole matrix
893  input_matrix->add_matrix (context.get_elem_jacobian(),
894  context.get_dof_indices() );
895  }
896  else
897  {
898  // Otherwise we should only add the relevant submatrices
899  for (unsigned int var1=0; var1<n_vars(); var1++)
900  {
901  ConstCouplingRow ccr(var1, *coupling_matrix);
902  for (const auto & var2 : ccr)
903  {
904  unsigned int sub_m = context.get_elem_jacobian( var1, var2 ).m();
905  unsigned int sub_n = context.get_elem_jacobian( var1, var2 ).n();
906  DenseMatrix<Number> sub_jac(sub_m, sub_n);
907  for (unsigned int row=0; row<sub_m; row++)
908  for (unsigned int col=0; col<sub_n; col++)
909  {
910  sub_jac(row,col) = context.get_elem_jacobian( var1, var2 ).el(row,col);
911  }
912  input_matrix->add_matrix (sub_jac,
913  context.get_dof_indices(var1),
914  context.get_dof_indices(var2) );
915  }
916  }
917  }
918 
919  }
920 
921  if (assemble_vector)
922  input_vector->add_vector (context.get_elem_residual(),
923  context.get_dof_indices() );
924  } // end for (elem)
925 
926  } // libmesh_try
927  libmesh_catch(...)
928  {
929  assembly_threw = 1;
930  }
931 
932  // Note: regardless of whether any procs threw during assembly (and
933  // thus didn't finish assembling), we should not leave the matrix
934  // and vector in an inconsistent state, since it may be possible to
935  // recover from the exception. Therefore, we close them now. The
936  // assumption here is that the nature of the exception does not
937  // prevent the matrix and vector from still being assembled (albeit
938  // with incomplete data).
939  if (assemble_matrix)
940  input_matrix->close();
941  if (assemble_vector)
942  input_vector->close();
943 
944  // Check for exceptions on any procs and if there is one, re-throw
945  // it on all procs.
946  this->comm().max(assembly_threw);
947 
948  if (assembly_threw)
949  libmesh_error_msg("Error during assembly in RBConstruction::add_scaled_matrix_and_vector()");
950 }
951 
953 {
954  // Set current_local_solution = vec so that we can access
955  // vec from DGFEMContext during assembly
956  vec.localize
957  (*current_local_solution, this->get_dof_map().get_send_list());
958 }
959 
961 {
962  LOG_SCOPE("truth_assembly()", "RBConstruction");
963 
964  const RBParameters & mu = get_parameters();
965 
966  this->matrix->zero();
967  this->rhs->zero();
968 
969  this->matrix->close();
970  this->rhs->close();
971 
972  {
973  // We should have already assembled the matrices
974  // and vectors in the affine expansion, so
975  // just use them
976 
977  for (unsigned int q_a=0; q_a<get_rb_theta_expansion().get_n_A_terms(); q_a++)
978  {
979  matrix->add(get_rb_theta_expansion().eval_A_theta(q_a, mu), *get_Aq(q_a));
980  }
981 
982  std::unique_ptr<NumericVector<Number>> temp_vec = NumericVector<Number>::build(this->comm());
983  temp_vec->init (this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
984  for (unsigned int q_f=0; q_f<get_rb_theta_expansion().get_n_F_terms(); q_f++)
985  {
986  *temp_vec = *get_Fq(q_f);
987  temp_vec->scale( get_rb_theta_expansion().eval_F_theta(q_f, mu) );
988  rhs->add(*temp_vec);
989  }
990  }
991 
992  this->matrix->close();
993  this->rhs->close();
994 }
995 
997  bool apply_dof_constraints)
998 {
999  input_matrix->zero();
1000 
1002  {
1005  input_matrix,
1006  nullptr,
1007  false, /* symmetrize */
1008  apply_dof_constraints);
1009  }
1010  else
1011  {
1012  libmesh_error_msg_if(energy_inner_product_coeffs.size() != get_rb_theta_expansion().get_n_A_terms(),
1013  "Error: invalid number of entries in energy_inner_product_coeffs.");
1014 
1015  // We symmetrize below so that we may use the energy inner-product even in cases
1016  // where the A_q are not symmetric.
1017  for (unsigned int q_a=0; q_a<get_rb_theta_expansion().get_n_A_terms(); q_a++)
1018  {
1021  input_matrix,
1022  nullptr,
1023  true, /* symmetrize */
1024  apply_dof_constraints);
1025  }
1026  }
1027 }
1028 
1030  SparseMatrix<Number> * input_matrix,
1031  bool apply_dof_constraints)
1032 {
1033  libmesh_error_msg_if(q >= get_rb_theta_expansion().get_n_A_terms(),
1034  "Error: We must have q < Q_a in assemble_Aq_matrix.");
1035 
1036  input_matrix->zero();
1037 
1040  input_matrix,
1041  nullptr,
1042  false, /* symmetrize */
1043  apply_dof_constraints);
1044 }
1045 
1047  unsigned int q_a,
1048  SparseMatrix<Number> * input_matrix,
1049  bool symmetrize)
1050 {
1051  LOG_SCOPE("add_scaled_Aq()", "RBConstruction");
1052 
1053  libmesh_error_msg_if(q_a >= get_rb_theta_expansion().get_n_A_terms(),
1054  "Error: We must have q < Q_a in add_scaled_Aq.");
1055 
1056  if (!symmetrize)
1057  {
1058  input_matrix->add(scalar, *get_Aq(q_a));
1059  input_matrix->close();
1060  }
1061  else
1062  {
1065  input_matrix,
1066  nullptr,
1067  symmetrize);
1068  }
1069 }
1070 
1072 {
1073  libMesh::out << "Assembling inner product matrix" << std::endl;
1075 
1077  {
1078  libMesh::out << "Assembling non-Dirichlet inner product matrix" << std::endl;
1080  }
1081 }
1082 
1084 {
1085  for (unsigned int q_a=0; q_a<get_rb_theta_expansion().get_n_A_terms(); q_a++)
1086  {
1087  libMesh::out << "Assembling affine operator " << (q_a+1) << " of "
1088  << get_rb_theta_expansion().get_n_A_terms() << std::endl;
1089  assemble_Aq_matrix(q_a, get_Aq(q_a));
1090  }
1091 
1093  {
1094  for (unsigned int q_a=0; q_a<get_rb_theta_expansion().get_n_A_terms(); q_a++)
1095  {
1096  libMesh::out << "Assembling non-Dirichlet affine operator " << (q_a+1) << " of "
1097  << get_rb_theta_expansion().get_n_A_terms() << std::endl;
1098  assemble_Aq_matrix(q_a, get_non_dirichlet_Aq(q_a), false);
1099  }
1100  }
1101 }
1102 
1104 {
1105  for (unsigned int q_f=0; q_f<get_rb_theta_expansion().get_n_F_terms(); q_f++)
1106  {
1107  libMesh::out << "Assembling affine vector " << (q_f+1) << " of "
1108  << get_rb_theta_expansion().get_n_F_terms() << std::endl;
1109  assemble_Fq_vector(q_f, get_Fq(q_f));
1110  }
1111 
1113  {
1114  for (unsigned int q_f=0; q_f<get_rb_theta_expansion().get_n_F_terms(); q_f++)
1115  {
1116  libMesh::out << "Assembling non-Dirichlet affine vector " << (q_f+1) << " of "
1117  << get_rb_theta_expansion().get_n_F_terms() << std::endl;
1118  assemble_Fq_vector(q_f, get_non_dirichlet_Fq(q_f), false);
1119  }
1120  }
1121 
1122 }
1123 
1125  NumericVector<Number> * input_vector,
1126  bool apply_dof_constraints)
1127 {
1128  libmesh_error_msg_if(q >= get_rb_theta_expansion().get_n_F_terms(),
1129  "Error: We must have q < Q_f in assemble_Fq_vector.");
1130 
1131  input_vector->zero();
1132 
1135  nullptr,
1136  input_vector,
1137  false, /* symmetrize */
1138  apply_dof_constraints /* apply_dof_constraints */);
1139 }
1140 
1142 {
1143  for (unsigned int n=0; n<get_rb_theta_expansion().get_n_outputs(); n++)
1144  for (unsigned int q_l=0; q_l<get_rb_theta_expansion().get_n_output_terms(n); q_l++)
1145  {
1146  libMesh::out << "Assembling output vector, (" << (n+1) << "," << (q_l+1)
1147  << ") of (" << get_rb_theta_expansion().get_n_outputs()
1148  << "," << get_rb_theta_expansion().get_n_output_terms(n) << ")"
1149  << std::endl;
1150  get_output_vector(n, q_l)->zero();
1152  nullptr,
1153  get_output_vector(n,q_l),
1154  false, /* symmetrize */
1155  true /* apply_dof_constraints */);
1156  }
1157 
1159  {
1160  for (unsigned int n=0; n<get_rb_theta_expansion().get_n_outputs(); n++)
1161  for (unsigned int q_l=0; q_l<get_rb_theta_expansion().get_n_output_terms(n); q_l++)
1162  {
1163  libMesh::out << "Assembling non-Dirichlet output vector, (" << (n+1) << "," << (q_l+1)
1164  << ") of (" << get_rb_theta_expansion().get_n_outputs()
1165  << "," << get_rb_theta_expansion().get_n_output_terms(n) << ")"
1166  << std::endl;
1169  nullptr,
1171  false, /* symmetrize */
1172  false /* apply_dof_constraints */);
1173  }
1174  }
1175 }
1176 
1177 Real RBConstruction::train_reduced_basis(const bool resize_rb_eval_data)
1178 {
1179  if(get_RB_training_type() == "Greedy")
1180  {
1181  return train_reduced_basis_with_greedy(resize_rb_eval_data);
1182  }
1183  else if (get_RB_training_type() == "POD")
1184  {
1186  return 0.;
1187  }
1188  else
1189  {
1190  libmesh_error_msg("RB training type not recognized: " + get_RB_training_type());
1191  }
1192 
1193  return 0.;
1194 }
1195 
1197 {
1198  LOG_SCOPE("train_reduced_basis_with_greedy()", "RBConstruction");
1199 
1200  int count = 0;
1201 
1202  RBEvaluation & rbe = get_rb_evaluation();
1203 
1204  // initialize rbe's parameters
1205  rbe.initialize_parameters(*this);
1206 
1207  // possibly resize data structures according to Nmax
1208  if (resize_rb_eval_data)
1210 
1211  // Clear the Greedy param list
1212  for (auto & plist : rbe.greedy_param_list)
1213  plist.clear();
1214 
1215  rbe.greedy_param_list.clear();
1216 
1217  Real training_greedy_error = 0.;
1218 
1219 
1220  // If we are continuing from a previous training run,
1221  // we might already be at the max number of basis functions.
1222  // If so, we can just return.
1223  if (rbe.get_n_basis_functions() >= get_Nmax())
1224  {
1225  libMesh::out << "Maximum number of basis functions reached: Nmax = "
1226  << get_Nmax() << std::endl;
1227  return 0.;
1228  }
1229 
1230  // Optionally pre-evaluate the theta functions on the entire (local) training parameter set.
1233 
1235  {
1236  // Compute the dual norms of the outputs if we haven't already done so.
1238 
1239  // Compute the Fq Riesz representor dual norms if we haven't already done so.
1241  }
1242 
1243  libMesh::out << std::endl << "---- Performing Greedy basis enrichment ----" << std::endl;
1244  Real initial_greedy_error = 0.;
1245  bool initial_greedy_error_initialized = false;
1246  while (true)
1247  {
1248  libMesh::out << std::endl << "---- Basis dimension: "
1249  << rbe.get_n_basis_functions() << " ----" << std::endl;
1250 
1251  if (count > 0 || (count==0 && use_empty_rb_solve_in_greedy))
1252  {
1253  libMesh::out << "Performing RB solves on training set" << std::endl;
1254  training_greedy_error = compute_max_error_bound();
1255 
1256  libMesh::out << "Maximum error bound is " << training_greedy_error << std::endl << std::endl;
1257 
1258  // record the initial error
1259  if (!initial_greedy_error_initialized)
1260  {
1261  initial_greedy_error = training_greedy_error;
1262  initial_greedy_error_initialized = true;
1263  }
1264 
1265  // Break out of training phase if we have reached Nmax
1266  // or if the training_tolerance is satisfied.
1267  if (greedy_termination_test(training_greedy_error, initial_greedy_error, count))
1268  break;
1269  }
1270 
1271  libMesh::out << "Performing truth solve at parameter:" << std::endl;
1272  print_parameters();
1273 
1274  // Update the list of Greedily selected parameters
1275  this->update_greedy_param_list();
1276 
1277  // Perform an Offline truth solve for the current parameter
1278  truth_solve(-1);
1279 
1281  {
1282  libMesh::out << "Zero basis function encountered hence ending basis enrichment" << std::endl;
1283  break;
1284  }
1285 
1286  // Add orthogonal part of the snapshot to the RB space
1287  libMesh::out << "Enriching the RB space" << std::endl;
1288  enrich_RB_space();
1289 
1290  update_system();
1291 
1292  // Check if we've reached Nmax now. We do this before calling
1293  // update_residual_terms() since we can skip that step if we've
1294  // already reached Nmax.
1295  if (rbe.get_n_basis_functions() >= this->get_Nmax())
1296  {
1297  libMesh::out << "Maximum number of basis functions reached: Nmax = "
1298  << get_Nmax() << std::endl;
1299  break;
1300  }
1301 
1303  {
1305  }
1306 
1307  // Increment counter
1308  count++;
1309  }
1310  this->update_greedy_param_list();
1311 
1312  return training_greedy_error;
1313 }
1314 
1315 void RBConstruction::enrich_basis_from_rhs_terms(const bool resize_rb_eval_data)
1316 {
1317  LOG_SCOPE("enrich_basis_from_rhs_terms()", "RBConstruction");
1318 
1319  // initialize rb_eval's parameters
1321 
1322  // possibly resize data structures according to Nmax
1323  if (resize_rb_eval_data)
1324  {
1326  }
1327 
1328  libMesh::out << std::endl << "---- Enriching basis from rhs terms ----" << std::endl;
1329 
1330  truth_assembly();
1331 
1332  for (unsigned int q_f=0; q_f<get_rb_theta_expansion().get_n_F_terms(); q_f++)
1333  {
1334  libMesh::out << std::endl << "Performing truth solve with rhs from rhs term " << q_f << std::endl;
1335 
1336  *rhs = *get_Fq(q_f);
1337 
1338  if (rhs->l2_norm() == 0)
1339  {
1340  // Skip enrichment if the rhs is zero
1341  continue;
1342  }
1343 
1344  // truth_assembly assembles into matrix and rhs, so use those for the solve
1345  if (extra_linear_solver)
1346  {
1347  // If extra_linear_solver has been initialized, then we use it for the
1348  // truth solves.
1350 
1351  if (assert_convergence)
1353  }
1354  else
1355  {
1357 
1358  if (assert_convergence)
1360  }
1361 
1362  // Debugging: enable this code to print the rhs that was used in
1363  // the most recent truth solve to a uniquely-named file.
1364 #if 0
1365  {
1366  char temp_file[] = "truth_rhs_XXXXXX.dat";
1367  int fd = mkstemps(temp_file, 4);
1368  if (fd != -1)
1369  {
1370  libMesh::out << "Writing truth system rhs to file: " << temp_file << std::endl;
1371  rhs->print_matlab(std::string(temp_file));
1372  }
1373  }
1374 #endif // 0
1375 
1376  // Debugging: enable this code to print the most recent truth
1377  // solution to a uniquely-named file.
1378 #ifdef LIBMESH_HAVE_EXODUS_API
1379 #if 0
1380  {
1381  // Note: mkstemps creates a file and returns an open file descriptor to it.
1382  // The filename is created from a template which must have 6 'X' characters followed
1383  // by a suffix having the specified length (in this case 4, for ".exo").
1384  char temp_file[] = "truth_XXXXXX.exo";
1385  int fd = mkstemps(temp_file, 4);
1386  if (fd != -1)
1387  {
1388  libMesh::out << "Writing truth solution to file: " << temp_file << std::endl;
1389  ExodusII_IO exo_io(this->get_mesh());
1390  std::set<std::string> system_names = {this->name()};
1391  exo_io.write_equation_systems(std::string(temp_file),
1392  this->get_equation_systems(), &system_names);
1393  }
1394  }
1395 #endif // 0
1396 #endif // LIBMESH_HAVE_EXODUS_API
1397 
1398  // Call user-defined post-processing routines on the truth solution.
1400 
1401  // Add orthogonal part of the snapshot to the RB space
1402  libMesh::out << "Enriching the RB space" << std::endl;
1403  enrich_RB_space();
1404 
1405  update_system();
1406  }
1407 }
1408 
1410 {
1411  // We need to use the same training set on all processes so that
1412  // the truth solves below work correctly in parallel.
1413  libmesh_error_msg_if(!serial_training_set, "We must use a serial training set with POD");
1414  libmesh_error_msg_if(get_rb_evaluation().get_n_basis_functions() > 0, "Basis should not already be initialized");
1415 
1418 
1419  // Storage for the POD snapshots
1420  unsigned int n_snapshots = get_n_training_samples();
1421 
1422  if (get_n_params() == 0)
1423  {
1424  // In this case we should have generated an empty training set
1425  // so assert this
1426  libmesh_assert(n_snapshots == 0);
1427 
1428  // If we have no parameters, then we should do exactly one "truth solve"
1429  n_snapshots = 1;
1430  }
1431 
1432  std::vector<std::unique_ptr<NumericVector<Number>>> POD_snapshots(n_snapshots);
1433  for (unsigned int i=0; i<n_snapshots; i++)
1434  {
1435  POD_snapshots[i] = NumericVector<Number>::build(this->comm());
1436  POD_snapshots[i]->init (this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
1437  }
1438 
1439  // We use the same training set on all processes
1440  libMesh::out << std::endl;
1441  for (unsigned int i=0; i<n_snapshots; i++)
1442  {
1443  if (get_n_params() > 0)
1444  {
1446  }
1447 
1448  libMesh::out << "Truth solve " << (i+1) << " of " << n_snapshots << std::endl;
1449 
1450  truth_solve(-1);
1451 
1452  *POD_snapshots[i] = *solution;
1453  }
1454  libMesh::out << std::endl;
1455 
1457  {
1458  libMesh::out << "Normalizing solution snapshots" << std::endl;
1459  for (unsigned int i=0; i<n_snapshots; i++)
1460  {
1462  *inner_product_storage_vector, *POD_snapshots[i]);
1463  Real norm = std::sqrt(std::real(POD_snapshots[i]->dot(*inner_product_storage_vector)));
1464 
1465  if (norm > 0.)
1466  POD_snapshots[i]->scale(1./norm);
1467  }
1468  }
1469 
1470  // Set up the "correlation matrix"
1471  DenseMatrix<Number> correlation_matrix(n_snapshots,n_snapshots);
1472  for (unsigned int i=0; i<n_snapshots; i++)
1473  {
1475  *inner_product_storage_vector, *POD_snapshots[i]);
1476 
1477  for (unsigned int j=0; j<=i; j++)
1478  {
1479  Number inner_prod = (POD_snapshots[j]->dot(*inner_product_storage_vector));
1480 
1481  correlation_matrix(i,j) = inner_prod;
1482  if(i != j)
1483  {
1484  correlation_matrix(j,i) = libmesh_conj(inner_prod);
1485  }
1486  }
1487  }
1488 
1489  // compute SVD of correlation matrix
1490  DenseVector<Real> sigma( n_snapshots );
1491  DenseMatrix<Number> U( n_snapshots, n_snapshots );
1492  DenseMatrix<Number> VT( n_snapshots, n_snapshots );
1493  correlation_matrix.svd(sigma, U, VT );
1494 
1495  if (sigma(0) == 0.)
1496  return;
1497 
1498  // Add dominant vectors from the POD as basis functions.
1499  unsigned int j = 0;
1500  while (true)
1501  {
1502  if (j >= get_Nmax() || j >= n_snapshots)
1503  {
1504  libMesh::out << "Maximum number of basis functions (" << j << ") reached." << std::endl;
1505  break;
1506  }
1507 
1508  // The "energy" error in the POD approximation is determined by the first omitted
1509  // singular value, i.e. sigma(j). We normalize by sigma(0), which gives the total
1510  // "energy", in order to obtain a relative error.
1511  const Real rel_err = std::sqrt(sigma(j)) / std::sqrt(sigma(0));
1512 
1513  libMesh::out << "Number of basis functions: " << j
1514  << ", POD error norm: " << rel_err << std::endl;
1515 
1516  if (rel_err < this->rel_training_tolerance)
1517  {
1518  libMesh::out << "Training tolerance reached." << std::endl;
1519  break;
1520  }
1521 
1522  std::unique_ptr< NumericVector<Number> > v = POD_snapshots[j]->zero_clone();
1523  for ( unsigned int i=0; i<n_snapshots; ++i )
1524  {
1525  v->add( U.el(i, j), *POD_snapshots[i] );
1526  }
1527 
1528  Real norm_v = std::sqrt(sigma(j));
1529  v->scale( 1./norm_v );
1530 
1531  get_rb_evaluation().basis_functions.emplace_back( std::move(v) );
1532 
1533  j++;
1534  }
1535  libMesh::out << std::endl;
1536 
1538  update_system();
1539 
1540  // We now compute all terms required to evaluate the RB error indicator.
1541  // Unlike in the case of the RB Greedy algorithm, for the POD approach
1542  // we do not need this data in order to compute the basis. However, we
1543  // do need this data in order to evaluate error indicator quantities in
1544  // the Online stage, so we compute it now so that it can be saved in
1545  // the training data.
1547 }
1548 
1550  Real initial_error,
1551  int)
1552 {
1553  if (abs_greedy_error < this->abs_training_tolerance)
1554  {
1555  libMesh::out << "Absolute error tolerance reached." << std::endl;
1556  return true;
1557  }
1558 
1559  Real rel_greedy_error = abs_greedy_error/initial_error;
1560  if (rel_greedy_error < this->rel_training_tolerance)
1561  {
1562  libMesh::out << "Relative error tolerance reached." << std::endl;
1563  return true;
1564  }
1565 
1566  RBEvaluation & rbe = get_rb_evaluation();
1567 
1568  if (rbe.get_n_basis_functions() >= this->get_Nmax())
1569  {
1570  libMesh::out << "Maximum number of basis functions reached: Nmax = "
1571  << get_Nmax() << std::endl;
1572  return true;
1573  }
1574 
1576  for (auto & plist : rbe.greedy_param_list)
1577  if (plist == get_parameters())
1578  {
1579  libMesh::out << "Exiting greedy because the same parameters were selected twice" << std::endl;
1580  return true;
1581  }
1582 
1583  return false;
1584 }
1585 
1587 {
1589 }
1590 
1592 {
1593  libmesh_error_msg_if(i >= get_rb_evaluation().greedy_param_list.size(),
1594  "Error: Argument in RBConstruction::get_greedy_parameter is too large.");
1595 
1597 }
1598 
1600 {
1601  LOG_SCOPE("truth_solve()", "RBConstruction");
1602 
1604  {
1605  _untransformed_solution = solution->zero_clone();
1606  }
1607 
1608  truth_assembly();
1609 
1610  // truth_assembly assembles into matrix and rhs, so use those for the solve
1611  if (extra_linear_solver)
1612  {
1613  // If extra_linear_solver has been initialized, then we use it for the
1614  // truth solves.
1616 
1617  if (assert_convergence)
1619  }
1620  else
1621  {
1623 
1624  if (assert_convergence)
1626  }
1627 
1629  {
1631  }
1632 
1633  // Call user-defined post-processing routines on the truth solution.
1635 
1636  const RBParameters & mu = get_parameters();
1637 
1638  for (unsigned int n=0; n<get_rb_theta_expansion().get_n_outputs(); n++)
1639  {
1640  truth_outputs[n] = 0.;
1641  for (unsigned int q_l=0; q_l<get_rb_theta_expansion().get_n_output_terms(n); q_l++)
1643  get_output_vector(n,q_l)->dot(*solution);
1644  }
1645 
1646 #ifdef LIBMESH_HAVE_EXODUS_API
1647  if (plot_solution > 0)
1648  {
1649  ExodusII_IO exo_io(this->get_mesh());
1650  std::set<std::string> system_names = {this->name()};
1651  exo_io.write_equation_systems("truth.exo", this->get_equation_systems(), &system_names);
1652  }
1653 #else
1654  libmesh_ignore(plot_solution);
1655 #endif
1656 
1657  // Get the X norm of the truth solution
1658  // Useful for normalizing our true error data
1660  Number truth_X_norm = std::sqrt(inner_product_storage_vector->dot(*solution));
1661 
1662  return libmesh_real(truth_X_norm);
1663 }
1664 
1665 bool RBConstruction::is_serial_training_type(const std::string & RB_training_type_in)
1666 {
1667  return (RB_training_type_in == "POD");
1668 }
1669 
1670 void RBConstruction::set_RB_training_type(const std::string & RB_training_type_in)
1671 {
1672  this->RB_training_type = RB_training_type_in;
1673 
1674  if(is_serial_training_type(RB_training_type_in))
1675  {
1676  // We need to use a serial training set (so that the training
1677  // set is the same on all processes) if we're using POD
1678  this->serial_training_set = true;
1679  }
1680 }
1681 
1682 const std::string & RBConstruction::get_RB_training_type() const
1683 {
1684  return RB_training_type;
1685 }
1686 
1687 void RBConstruction::set_Nmax(unsigned int Nmax_in)
1688 {
1689  this->Nmax = Nmax_in;
1690 }
1691 
1693 {
1694  LOG_SCOPE("load_basis_function()", "RBConstruction");
1695 
1696  libmesh_assert_less (i, get_rb_evaluation().get_n_basis_functions());
1697 
1699 
1700  this->update();
1701 }
1702 
1704 {
1705  LOG_SCOPE("enrich_RB_space()", "RBConstruction");
1706 
1707  auto new_bf = solution->clone();
1708 
1709  std::unique_ptr<NumericVector<Number>> new_untransformed_bf;
1711  {
1712  new_untransformed_bf = _untransformed_solution->clone();
1713  libmesh_assert_equal_to(_untransformed_basis_functions.size(), get_rb_evaluation().get_n_basis_functions());
1714  }
1715 
1716  for (unsigned int index=0; index<get_rb_evaluation().get_n_basis_functions(); index++)
1717  {
1719 
1720  Number scalar =
1721  inner_product_storage_vector->dot(get_rb_evaluation().get_basis_function(index));
1722  new_bf->add(-scalar, get_rb_evaluation().get_basis_function(index));
1723 
1725  {
1726  new_untransformed_bf->add(-scalar, *_untransformed_basis_functions[index]);
1727  }
1728  }
1729 
1730  // Normalize new_bf
1732  Number new_bf_norm = std::sqrt( inner_product_storage_vector->dot(*new_bf) );
1733 
1734  if (new_bf_norm == 0.)
1735  {
1736  new_bf->zero(); // avoid potential nan's
1737 
1739  {
1740  new_untransformed_bf->zero();
1741  }
1742  }
1743  else
1744  {
1745  new_bf->scale(1./new_bf_norm);
1746 
1748  {
1749  new_untransformed_bf->scale(1./new_bf_norm);
1750  }
1751  }
1752 
1753  // load the new basis function into the basis_functions vector.
1754  get_rb_evaluation().basis_functions.emplace_back( std::move(new_bf) );
1755 
1757  {
1758  _untransformed_basis_functions.emplace_back( std::move(new_untransformed_bf) );
1759  }
1760 }
1761 
1763 {
1764  libMesh::out << "Updating RB matrices" << std::endl;
1766 }
1767 
1769 {
1771 
1772  Real error_bound = 0.;
1774  {
1775  // Obtain the pre-evaluated theta functions from the current training parameter index
1776  const auto & evaluated_thetas = get_evaluated_thetas(get_current_training_parameter_index());
1777  error_bound = get_rb_evaluation().rb_solve(get_rb_evaluation().get_n_basis_functions(),
1778  &evaluated_thetas);
1779  }
1780  else
1781  error_bound = get_rb_evaluation().rb_solve(get_rb_evaluation().get_n_basis_functions());
1782 
1783 
1785  {
1786  Real error_bound_normalization = get_rb_evaluation().get_error_bound_normalization();
1787 
1788  if ((error_bound < abs_training_tolerance) ||
1789  (error_bound_normalization < abs_training_tolerance))
1790  {
1791  // We don't want to normalize this error bound if the bound or the
1792  // normalization value are below the absolute tolerance. Hence do nothing
1793  // in this case.
1794  }
1795  else
1796  error_bound /= error_bound_normalization;
1797  }
1798 
1799  return error_bound;
1800 }
1801 
1802 void RBConstruction::recompute_all_residual_terms(bool compute_inner_products)
1803 {
1804  // Compute the basis independent terms
1806  compute_Fq_representor_innerprods(compute_inner_products);
1807 
1808  // and all the basis dependent terms
1809  unsigned int saved_delta_N = delta_N;
1811 
1812  update_residual_terms(compute_inner_products);
1813 
1814  delta_N = saved_delta_N;
1815 }
1816 
1818 {
1819  LOG_SCOPE("compute_max_error_bound()", "RBConstruction");
1820 
1821  // Treat the case with no parameters in a special way
1822  if (get_n_params() == 0)
1823  {
1824  Real max_val;
1825  if (std::numeric_limits<Real>::has_infinity)
1826  {
1827  max_val = std::numeric_limits<Real>::infinity();
1828  }
1829  else
1830  {
1831  max_val = std::numeric_limits<Real>::max();
1832  }
1833 
1834  // Make sure we do at least one solve, but otherwise return a zero error bound
1835  // when we have no parameters
1836  return (get_rb_evaluation().get_n_basis_functions() == 0) ? max_val : 0.;
1837  }
1838 
1840 
1841  // keep track of the maximum error
1842  unsigned int max_err_index = 0;
1843  Real max_err = 0.;
1844 
1846  for (unsigned int i=0; i<get_local_n_training_samples(); i++)
1847  {
1848  // Load training parameter i, this is only loaded
1849  // locally since the RB solves are local.
1850  set_params_from_training_set( first_index+i );
1851 
1852  // In case we pre-evaluate the theta functions,
1853  // also keep track of the current training parameter index.
1855  set_current_training_parameter_index(first_index+i);
1856 
1857 
1859 
1860  if (training_error_bounds[i] > max_err)
1861  {
1862  max_err_index = i;
1863  max_err = training_error_bounds[i];
1864  }
1865  }
1866 
1867  std::pair<numeric_index_type, Real> error_pair(first_index+max_err_index, max_err);
1868  get_global_max_error_pair(this->comm(),error_pair);
1869 
1870  // If we have a serial training set (i.e. a training set that is the same on all processors)
1871  // just set the parameters on all processors
1872  if (serial_training_set)
1873  {
1874  set_params_from_training_set( error_pair.first );
1875  }
1876  // otherwise, broadcast the parameter that produced the maximum error
1877  else
1878  {
1879  unsigned int root_id=0;
1880  if ((get_first_local_training_index() <= error_pair.first) &&
1881  (error_pair.first < get_last_local_training_index()))
1882  {
1883  set_params_from_training_set( error_pair.first );
1884  root_id = this->processor_id();
1885  }
1886 
1887  this->comm().sum(root_id); // root_id is only non-zero on one processor
1888  broadcast_parameters(root_id);
1889  }
1890 
1891  return error_pair.second;
1892 }
1893 
1895 {
1896  LOG_SCOPE("update_RB_system_matrices()", "RBConstruction");
1897 
1898  unsigned int RB_size = get_rb_evaluation().get_n_basis_functions();
1899 
1900  std::unique_ptr<NumericVector<Number>> temp = NumericVector<Number>::build(this->comm());
1901  temp->init (this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
1902 
1903  for (unsigned int q_f=0; q_f<get_rb_theta_expansion().get_n_F_terms(); q_f++)
1904  {
1905  for (unsigned int i=(RB_size-delta_N); i<RB_size; i++)
1906  {
1907  get_rb_evaluation().RB_Fq_vector[q_f](i) = get_non_dirichlet_Fq_if_avail(q_f)->dot(get_rb_evaluation().get_basis_function(i));
1908  }
1909  }
1910 
1911  for (unsigned int i=(RB_size-delta_N); i<RB_size; i++)
1912  {
1913  for (unsigned int n=0; n<get_rb_theta_expansion().get_n_outputs(); n++)
1914  for (unsigned int q_l=0; q_l<get_rb_theta_expansion().get_n_output_terms(n); q_l++)
1915  {
1916  get_rb_evaluation().RB_output_vectors[n][q_l](i) =
1917  get_output_vector(n,q_l)->dot(get_rb_evaluation().get_basis_function(i));
1918  }
1919 
1920  for (unsigned int j=0; j<RB_size; j++)
1921  {
1922  Number value = 0.;
1923 
1925  {
1926  // Compute reduced inner_product_matrix
1927  temp->zero();
1929 
1930  value = temp->dot( get_rb_evaluation().get_basis_function(i) );
1932  if (i!=j)
1933  {
1934  // The inner product matrix is assumed
1935  // to be hermitian
1937  }
1938  }
1939 
1940  for (unsigned int q_a=0; q_a<get_rb_theta_expansion().get_n_A_terms(); q_a++)
1941  {
1942  // Compute reduced Aq matrix
1943  temp->zero();
1944  get_non_dirichlet_Aq_if_avail(q_a)->vector_mult(*temp, get_rb_evaluation().get_basis_function(j));
1945 
1946  value = (*temp).dot(get_rb_evaluation().get_basis_function(i));
1947  get_rb_evaluation().RB_Aq_vector[q_a](i,j) = value;
1948 
1949  if (i!=j)
1950  {
1951  temp->zero();
1952  get_non_dirichlet_Aq_if_avail(q_a)->vector_mult(*temp, get_rb_evaluation().get_basis_function(i));
1953 
1954  value = (*temp).dot(get_rb_evaluation().get_basis_function(j));
1955  get_rb_evaluation().RB_Aq_vector[q_a](j,i) = value;
1956  }
1957  }
1958  }
1959  }
1960 }
1961 
1962 
1963 void RBConstruction::update_residual_terms(bool compute_inner_products)
1964 {
1965  LOG_SCOPE("update_residual_terms()", "RBConstruction");
1966 
1967  libMesh::out << "Updating RB residual terms" << std::endl;
1968 
1969  unsigned int RB_size = get_rb_evaluation().get_n_basis_functions();
1970 
1972  {
1973  libmesh_assert_equal_to(_untransformed_basis_functions.size(), get_rb_evaluation().get_n_basis_functions());
1974  }
1975 
1976  for (unsigned int q_a=0; q_a<get_rb_theta_expansion().get_n_A_terms(); q_a++)
1977  {
1978  for (unsigned int i=(RB_size-delta_N); i<RB_size; i++)
1979  {
1980  // Initialize the vector in which we'll store the representor
1981  if (!get_rb_evaluation().Aq_representor[q_a][i])
1982  {
1984  get_rb_evaluation().Aq_representor[q_a][i]->init(this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
1985  }
1986 
1987  libmesh_assert(get_rb_evaluation().Aq_representor[q_a][i]->size() == this->n_dofs() &&
1988  get_rb_evaluation().Aq_representor[q_a][i]->local_size() == this->n_local_dofs() );
1989 
1990  rhs->zero();
1992  {
1993  get_Aq(q_a)->vector_mult(*rhs, get_rb_evaluation().get_basis_function(i));
1994  }
1995  else
1996  {
1998  }
1999  rhs->scale(-1.);
2000 
2001  if (!is_quiet())
2002  {
2003  libMesh::out << "Starting solve [q_a][i]=[" << q_a <<"]["<< i << "] in RBConstruction::update_residual_terms() at "
2004  << Utility::get_timestamp() << std::endl;
2005  }
2006 
2008 
2009  if (assert_convergence)
2011 
2012  if (!is_quiet())
2013  {
2014  libMesh::out << "Finished solve [q_a][i]=[" << q_a <<"]["<< i << "] in RBConstruction::update_residual_terms() at "
2015  << Utility::get_timestamp() << std::endl;
2016  libMesh::out << this->n_linear_iterations() << " iterations, final residual "
2017  << this->final_linear_residual() << std::endl;
2018  }
2019 
2020  // Store the representor
2022  }
2023  }
2024 
2025  // Now compute and store the inner products (if requested)
2026  if (compute_inner_products)
2027  {
2028 
2029  for (unsigned int q_f=0; q_f<get_rb_theta_expansion().get_n_F_terms(); q_f++)
2030  {
2032 
2033  for (unsigned int q_a=0; q_a<get_rb_theta_expansion().get_n_A_terms(); q_a++)
2034  {
2035  for (unsigned int i=(RB_size-delta_N); i<RB_size; i++)
2036  {
2038  inner_product_storage_vector->dot(*get_rb_evaluation().Aq_representor[q_a][i]);
2039  }
2040  }
2041  }
2042 
2043  unsigned int q=0;
2044  for (unsigned int q_a1=0; q_a1<get_rb_theta_expansion().get_n_A_terms(); q_a1++)
2045  {
2046  for (unsigned int q_a2=q_a1; q_a2<get_rb_theta_expansion().get_n_A_terms(); q_a2++)
2047  {
2048  for (unsigned int i=(RB_size-delta_N); i<RB_size; i++)
2049  {
2050  for (unsigned int j=0; j<RB_size; j++)
2051  {
2054  inner_product_storage_vector->dot(*get_rb_evaluation().Aq_representor[q_a1][i]);
2055 
2056  if (i != j)
2057  {
2060  inner_product_storage_vector->dot(*get_rb_evaluation().Aq_representor[q_a1][j]);
2061  }
2062  }
2063  }
2064  q++;
2065  }
2066  }
2067  } // end if (compute_inner_products)
2068 }
2069 
2071 {
2072  return *inner_product_matrix;
2073 }
2074 
2076 {
2077  // Skip calculations if we've already computed the output dual norms
2079  {
2080  // Short circuit if we don't have any outputs
2081  if (get_rb_theta_expansion().get_n_outputs() == 0)
2082  {
2084  return;
2085  }
2086 
2087  // Only log if we get to here
2088  LOG_SCOPE("compute_output_dual_innerprods()", "RBConstruction");
2089 
2090  libMesh::out << "Compute output dual inner products" << std::endl;
2091 
2092  // Find out the largest value of Q_l
2093  unsigned int max_Q_l = 0;
2094  for (unsigned int n=0; n<get_rb_theta_expansion().get_n_outputs(); n++)
2095  max_Q_l = (get_rb_theta_expansion().get_n_output_terms(n) > max_Q_l) ? get_rb_theta_expansion().get_n_output_terms(n) : max_Q_l;
2096 
2097  std::vector<std::unique_ptr<NumericVector<Number>>> L_q_representor(max_Q_l);
2098  for (unsigned int q=0; q<max_Q_l; q++)
2099  {
2100  L_q_representor[q] = NumericVector<Number>::build(this->comm());
2101  L_q_representor[q]->init (this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
2102  }
2103 
2104  for (unsigned int n=0; n<get_rb_theta_expansion().get_n_outputs(); n++)
2105  {
2106  for (unsigned int q_l=0; q_l<get_rb_theta_expansion().get_n_output_terms(n); q_l++)
2107  {
2108  rhs->zero();
2109  rhs->add(1., *get_output_vector(n,q_l));
2110 
2111  if (!is_quiet())
2112  libMesh::out << "Starting solve n=" << n << ", q_l=" << q_l
2113  << " in RBConstruction::compute_output_dual_innerprods() at "
2114  << Utility::get_timestamp() << std::endl;
2115 
2116  // Use the main linear solver here instead of the inner_product solver, since
2117  // get_matrix_for_output_dual_solves() may not return the inner product matrix.
2119 
2120  // We possibly perform multiple solves here with the same matrix, hence
2121  // set reuse_preconditioner(true) (and set it back to false again below
2122  // at the end of this function).
2123  linear_solver->reuse_preconditioner(true);
2124 
2125  if (assert_convergence)
2127 
2128  if (!is_quiet())
2129  {
2130  libMesh::out << "Finished solve n=" << n << ", q_l=" << q_l
2131  << " in RBConstruction::compute_output_dual_innerprods() at "
2132  << Utility::get_timestamp() << std::endl;
2133 
2135  << " iterations, final residual "
2136  << this->final_linear_residual() << std::endl;
2137  }
2138 
2139  *L_q_representor[q_l] = *solution;
2140  }
2141 
2142  unsigned int q=0;
2143  for (unsigned int q_l1=0; q_l1<get_rb_theta_expansion().get_n_output_terms(n); q_l1++)
2144  {
2146 
2147  for (unsigned int q_l2=q_l1; q_l2<get_rb_theta_expansion().get_n_output_terms(n); q_l2++)
2148  {
2149  output_dual_innerprods[n][q] = L_q_representor[q_l2]->dot(*inner_product_storage_vector);
2150  libMesh::out << "output_dual_innerprods[" << n << "][" << q << "] = " << output_dual_innerprods[n][q] << std::endl;
2151 
2152  q++;
2153  }
2154  }
2155  }
2156 
2157  // We may not need to use linear_solver again (e.g. this would happen if we use
2158  // extra_linear_solver for the truth_solves). As a result, let's clear linear_solver
2159  // to release any memory it may be taking up. If we do need it again, it will
2160  // be initialized when necessary.
2161  linear_solver->clear();
2162  linear_solver->reuse_preconditioner(false);
2163 
2165  }
2166 
2168 }
2169 
2170 void RBConstruction::compute_Fq_representor_innerprods(bool compute_inner_products)
2171 {
2172 
2173  // Skip calculations if we've already computed the Fq_representors
2175  {
2176  // Only log if we get to here
2177  LOG_SCOPE("compute_Fq_representor_innerprods()", "RBConstruction");
2178 
2179  for (unsigned int q_f=0; q_f<get_rb_theta_expansion().get_n_F_terms(); q_f++)
2180  {
2181  if (!Fq_representor[q_f])
2182  {
2184  Fq_representor[q_f]->init (this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
2185  }
2186 
2187  libmesh_assert(Fq_representor[q_f]->size() == this->n_dofs() &&
2188  Fq_representor[q_f]->local_size() == this->n_local_dofs() );
2189 
2190  rhs->zero();
2191  rhs->add(1., *get_Fq(q_f));
2192 
2193  if (!is_quiet())
2194  libMesh::out << "Starting solve q_f=" << q_f
2195  << " in RBConstruction::update_residual_terms() at "
2196  << Utility::get_timestamp() << std::endl;
2197 
2199 
2200  if (assert_convergence)
2202 
2203  if (!is_quiet())
2204  {
2205  libMesh::out << "Finished solve q_f=" << q_f
2206  << " in RBConstruction::update_residual_terms() at "
2207  << Utility::get_timestamp() << std::endl;
2208 
2210  << " iterations, final residual "
2211  << this->final_linear_residual() << std::endl;
2212  }
2213 
2214  *Fq_representor[q_f] = *solution;
2215  }
2216 
2217  if (compute_inner_products)
2218  {
2219  unsigned int q=0;
2220 
2221  for (unsigned int q_f1=0; q_f1<get_rb_theta_expansion().get_n_F_terms(); q_f1++)
2222  {
2224 
2225  for (unsigned int q_f2=q_f1; q_f2<get_rb_theta_expansion().get_n_F_terms(); q_f2++)
2226  {
2228 
2229  q++;
2230  }
2231  }
2232  } // end if (compute_inner_products)
2233 
2235  }
2236 
2238 }
2239 
2241 {
2242  LOG_SCOPE("load_rb_solution()", "RBConstruction");
2243 
2244  solution->zero();
2245 
2246  libmesh_error_msg_if(get_rb_evaluation().RB_solution.size() > get_rb_evaluation().get_n_basis_functions(),
2247  "ERROR: System contains " << get_rb_evaluation().get_n_basis_functions() << " basis functions."
2248  << " RB_solution vector contains " << get_rb_evaluation().RB_solution.size() << " entries."
2249  << " RB_solution in RBConstruction::load_rb_solution is too long!");
2250 
2251  for (auto i : make_range(get_rb_evaluation().RB_solution.size()))
2252  solution->add(get_rb_evaluation().RB_solution(i), get_rb_evaluation().get_basis_function(i));
2253 
2254  update();
2255 }
2256 
2257 // The slow (but simple, non-error prone) way to compute the residual dual norm
2258 // Useful for error checking
2260 {
2261  LOG_SCOPE("compute_residual_dual_norm_slow()", "RBConstruction");
2262 
2263  // Put the residual in rhs in order to compute the norm of the Riesz representor
2264  // Note that this only works in serial since otherwise each processor will
2265  // have a different parameter value during the Greedy training.
2266 
2267  std::unique_ptr<NumericVector<Number>> RB_sol = NumericVector<Number>::build(comm());
2268  RB_sol->init (this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
2269 
2270  std::unique_ptr<NumericVector<Number>> temp = NumericVector<Number>::build(comm());
2271  temp->init (this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
2272 
2274  {
2275  libmesh_assert_equal_to(_untransformed_basis_functions.size(), get_rb_evaluation().get_n_basis_functions());
2276  }
2277 
2278  for (unsigned int i=0; i<N; i++)
2279  {
2281  {
2282  RB_sol->add(get_rb_evaluation().RB_solution(i), get_rb_evaluation().get_basis_function(i));
2283  }
2284  else
2285  {
2286  RB_sol->add(get_rb_evaluation().RB_solution(i), *_untransformed_basis_functions[i]);
2287  }
2288  }
2289 
2290  this->truth_assembly();
2291  matrix->vector_mult(*temp, *RB_sol);
2292  rhs->add(-1., *temp);
2293 
2294  // Then solve to get the Reisz representor
2295  matrix->zero();
2297 
2300  Number slow_residual_norm_sq = solution->dot(*inner_product_storage_vector);
2301 
2302  return std::sqrt( libmesh_real(slow_residual_norm_sq) );
2303 }
2304 
2306 {
2307  return inner_product_matrix.get();
2308 }
2309 
2311 {
2312  return inner_product_matrix.get();
2313 }
2314 
2316 {
2317  libmesh_error_msg_if(!store_non_dirichlet_operators,
2318  "Error: Must have store_non_dirichlet_operators==true to access non_dirichlet_inner_product_matrix.");
2319 
2321 }
2322 
2324 {
2325  libmesh_error_msg_if(!store_non_dirichlet_operators,
2326  "Error: Must have store_non_dirichlet_operators==true to access non_dirichlet_inner_product_matrix.");
2327 
2329 }
2330 
2332 {
2334  {
2336  }
2337 
2338  return get_inner_product_matrix();
2339 }
2340 
2342 {
2344  {
2346  }
2347 
2348  return get_inner_product_matrix();
2349 }
2350 
2352 {
2353  libmesh_error_msg_if(q >= get_rb_theta_expansion().get_n_A_terms(),
2354  "Error: We must have q < Q_a in get_Aq.");
2355 
2356  return Aq_vector[q].get();
2357 }
2358 
2360 {
2361  libmesh_error_msg_if(!store_non_dirichlet_operators,
2362  "Error: Must have store_non_dirichlet_operators==true to access non_dirichlet_Aq.");
2363 
2364  libmesh_error_msg_if(q >= get_rb_theta_expansion().get_n_A_terms(),
2365  "Error: We must have q < Q_a in get_Aq.");
2366 
2367  return non_dirichlet_Aq_vector[q].get();
2368 }
2369 
2371 {
2373  {
2374  return get_non_dirichlet_Aq(q);
2375  }
2376 
2377  return get_Aq(q);
2378 }
2379 
2381 {
2382  libmesh_error_msg_if(q >= get_rb_theta_expansion().get_n_F_terms(),
2383  "Error: We must have q < Q_f in get_Fq.");
2384 
2385  return Fq_vector[q].get();
2386 }
2387 
2389 {
2390  libmesh_error_msg_if(!store_non_dirichlet_operators,
2391  "Error: Must have store_non_dirichlet_operators==true to access non_dirichlet_Fq.");
2392 
2393  libmesh_error_msg_if(q >= get_rb_theta_expansion().get_n_F_terms(),
2394  "Error: We must have q < Q_f in get_Fq.");
2395 
2396  return non_dirichlet_Fq_vector[q].get();
2397 }
2398 
2400 {
2402  {
2403  return get_non_dirichlet_Fq(q);
2404  }
2405 
2406  return get_Fq(q);
2407 }
2408 
2410 {
2411  libmesh_error_msg_if((n >= get_rb_theta_expansion().get_n_outputs()) ||
2412  (q_l >= get_rb_theta_expansion().get_n_output_terms(n)),
2413  "Error: We must have n < n_outputs and "
2414  "q_l < get_rb_theta_expansion().get_n_output_terms(n) in get_output_vector.");
2415 
2416  return outputs_vector[n][q_l].get();
2417 }
2418 
2420 {
2421  libmesh_error_msg_if((n >= get_rb_theta_expansion().get_n_outputs()) ||
2422  (q_l >= get_rb_theta_expansion().get_n_output_terms(n)),
2423  "Error: We must have n < n_outputs and "
2424  "q_l < get_rb_theta_expansion().get_n_output_terms(n) in get_non_dirichlet_output_vector.");
2425 
2426  return non_dirichlet_outputs_vector[n][q_l].get();
2427 }
2428 
2429 void RBConstruction::get_all_matrices(std::map<std::string, SparseMatrix<Number> *> & all_matrices)
2430 {
2431  all_matrices.clear();
2432 
2433  all_matrices["inner_product"] = get_inner_product_matrix();
2434 
2436  {
2437  all_matrices["non_dirichlet_inner_product"] = get_non_dirichlet_inner_product_matrix();
2438  }
2439 
2440  for (unsigned int q_a=0; q_a<get_rb_theta_expansion().get_n_A_terms(); q_a++)
2441  {
2442  std::stringstream matrix_name;
2443  matrix_name << "A" << q_a;
2444  all_matrices[matrix_name.str()] = get_Aq(q_a);
2445 
2447  {
2448  matrix_name << "_non_dirichlet";
2449  all_matrices[matrix_name.str()] = get_non_dirichlet_Aq(q_a);
2450  }
2451  }
2452 }
2453 
2454 void RBConstruction::get_all_vectors(std::map<std::string, NumericVector<Number> *> & all_vectors)
2455 {
2456  all_vectors.clear();
2457 
2458  get_output_vectors(all_vectors);
2459 
2460  for (unsigned int q_f=0; q_f<get_rb_theta_expansion().get_n_F_terms(); q_f++)
2461  {
2462  std::stringstream F_vector_name;
2463  F_vector_name << "F" << q_f;
2464  all_vectors[F_vector_name.str()] = get_Fq(q_f);
2465 
2467  {
2468  F_vector_name << "_non_dirichlet";
2469  all_vectors[F_vector_name.str()] = get_non_dirichlet_Fq(q_f);
2470  }
2471  }
2472 }
2473 
2474 void RBConstruction::get_output_vectors(std::map<std::string, NumericVector<Number> *> & output_vectors)
2475 {
2476  output_vectors.clear();
2477 
2478  for (unsigned int n=0; n<get_rb_theta_expansion().get_n_outputs(); n++)
2479  for (unsigned int q_l=0; q_l<get_rb_theta_expansion().get_n_output_terms(n); q_l++)
2480  {
2481  std::stringstream output_name;
2482  output_name << "output_" << n << "_"<< q_l;
2483  output_vectors[output_name.str()] = get_output_vector(n,q_l);
2484 
2486  {
2487  output_name << "_non_dirichlet";
2488  output_vectors[output_name.str()] = get_non_dirichlet_output_vector(n,q_l);
2489  }
2490  }
2491 }
2492 
2493 #ifdef LIBMESH_ENABLE_DIRICHLET
2494 
2495 std::unique_ptr<DirichletBoundary> RBConstruction::build_zero_dirichlet_boundary_object()
2496 {
2497  ZeroFunction<> zf;
2498 
2499  std::set<boundary_id_type> dirichlet_ids;
2500  std::vector<unsigned int> variables;
2501 
2502  // The DirichletBoundary constructor clones zf, so it's OK that zf is only in local scope
2503  return std::make_unique<DirichletBoundary>(dirichlet_ids, variables, &zf);
2504 }
2505 
2506 #endif
2507 
2508 void RBConstruction::write_riesz_representors_to_files(const std::string & riesz_representors_dir,
2509  const bool write_binary_residual_representors)
2510 {
2511  LOG_SCOPE("write_riesz_representors_to_files()", "RBConstruction");
2512 
2513  // Write out Riesz representors. These are useful to have when restarting,
2514  // so you don't have to recompute them all over again.
2515 
2516  // First we write out the Fq representors, these are independent of an RBEvaluation object.
2517  libMesh::out << "Writing out the Fq_representors..." << std::endl;
2518 
2519  std::ostringstream file_name;
2520  const std::string riesz_representor_suffix = (write_binary_residual_representors ? ".xdr" : ".dat");
2521  struct stat stat_info;
2522 
2523  // Residual representors written out to their own separate directory
2524  if ( this->processor_id() == 0)
2525  if ( Utility::mkdir(riesz_representors_dir.c_str()) != 0)
2526  libMesh::out << "Skipping creating residual_representors directory: " << strerror(errno) << std::endl;
2527 
2528  for (auto i : index_range(Fq_representor))
2529  {
2530  if (Fq_representor[i])
2531  {
2532  file_name.str(""); // reset filename
2533  file_name << riesz_representors_dir << "/Fq_representor" << i << riesz_representor_suffix;
2534 
2535  // Check to see if file exists, if so, don't overwrite it, we assume it was
2536  // there from a previous call to this function. Note: if stat returns zero
2537  // it means it successfully got the file attributes (and therefore the file
2538  // exists). Because of the following factors:
2539  // 1.) write_serialized_data takes longer for proc 0 than it does for others,
2540  // so processors can get out of sync.
2541  // 2.) The constructor for Xdr opens a (0 length) file on *all* processors,
2542  // there are typically hundreds of 0-length files created during this loop,
2543  // and that screws up checking for the existence of files. One way to stay
2544  // in sync is to but a barrier at each iteration of the loop -- not sure how
2545  // bad this will affect performance, but it can't be much worse than serialized
2546  // I/O already is :)
2547  int stat_result = stat(file_name.str().c_str(), &stat_info);
2548 
2549  if ( (stat_result != 0) || // file definitely doesn't already exist
2550  (stat_info.st_size == 0)) // file exists, but has zero length (can happen if another proc already opened it!)
2551  {
2552  // No need to copy!
2553  // *solution = *(Fq_representor[i]);
2554  // std::swap doesn't work on pointers
2555  //std::swap(solution.get(), Fq_representor[i]);
2556  Fq_representor[i]->swap(*solution);
2557 
2558  Xdr fqr_data(file_name.str(),
2559  write_binary_residual_representors ? ENCODE : WRITE);
2560 
2561  write_serialized_data(fqr_data, false);
2562 
2563  // Synchronize before moving on
2564  this->comm().barrier();
2565 
2566  // Swap back.
2567  Fq_representor[i]->swap(*solution);
2568 
2569  // TODO: bzip the resulting file? See $LIBMESH_DIR/src/mesh/unstructured_mesh.C
2570  // for the system call, be sure to do it only on one processor, etc.
2571  }
2572  }
2573  }
2574 
2575 
2576  // Next, write out the Aq representors associated with rb_eval.
2577  libMesh::out << "Writing out the Aq_representors..." << std::endl;
2578 
2579  const unsigned int jstop = get_rb_evaluation().get_n_basis_functions();
2580  const unsigned int jstart = jstop-get_delta_N();
2581 
2582  RBEvaluation & rbe = get_rb_evaluation();
2583  for (auto i : index_range(rbe.Aq_representor))
2584  for (unsigned int j=jstart; j<jstop; ++j)
2585  {
2586  libMesh::out << "Writing out Aq_representor[" << i << "][" << j << "]..." << std::endl;
2587  libmesh_assert(get_rb_evaluation().Aq_representor[i][j]);
2588 
2589  file_name.str(""); // reset filename
2590  file_name << riesz_representors_dir
2591  << "/Aq_representor" << i << "_" << j << riesz_representor_suffix;
2592 
2593  {
2594  // No need to copy! Use swap instead.
2595  // *solution = *(Aq_representor[i][j]);
2596  rbe.Aq_representor[i][j]->swap(*solution);
2597 
2598  Xdr aqr_data(file_name.str(),
2599  write_binary_residual_representors ? ENCODE : WRITE);
2600 
2601  write_serialized_data(aqr_data, false);
2602 
2603  // Synchronize before moving on
2604  this->comm().barrier();
2605 
2606  // Swap back.
2607  rbe.Aq_representor[i][j]->swap(*solution);
2608 
2609  // TODO: bzip the resulting file? See $LIBMESH_DIR/src/mesh/unstructured_mesh.C
2610  // for the system call, be sure to do it only on one processor, etc.
2611  }
2612  }
2613 }
2614 
2615 
2616 
2617 void RBConstruction::read_riesz_representors_from_files(const std::string & riesz_representors_dir,
2618  const bool read_binary_residual_representors)
2619 {
2620  LOG_SCOPE("read_riesz_representors_from_files()", "RBConstruction");
2621 
2622  libMesh::out << "Reading in the Fq_representors..." << std::endl;
2623 
2624  const std::string riesz_representor_suffix = (read_binary_residual_representors ? ".xdr" : ".dat");
2625  std::ostringstream file_name;
2626  struct stat stat_info;
2627 
2628  // Read in the Fq_representors. There should be Q_f of these. FIXME:
2629  // should we be worried about leaks here?
2630  for (const auto & rep : Fq_representor)
2631  libmesh_error_msg_if(rep, "Error, must delete existing Fq_representor before reading in from file.");
2632 
2633  for (auto i : index_range(Fq_representor))
2634  {
2635  file_name.str(""); // reset filename
2636  file_name << riesz_representors_dir
2637  << "/Fq_representor" << i << riesz_representor_suffix;
2638 
2639  // On processor zero check to be sure the file exists
2640  if (this->processor_id() == 0)
2641  {
2642  int stat_result = stat(file_name.str().c_str(), &stat_info);
2643 
2644  libmesh_error_msg_if(stat_result != 0, "File does not exist: " << file_name.str());
2645  }
2646 
2647  Xdr fqr_data(file_name.str(),
2648  read_binary_residual_representors ? DECODE : READ);
2649 
2650  read_serialized_data(fqr_data, false);
2651 
2653  Fq_representor[i]->init (this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
2654 
2655  // No need to copy, just swap
2656  // *Fq_representor[i] = *solution;
2657  Fq_representor[i]->swap(*solution);
2658  }
2659 
2660  // Alert the update_residual_terms() function that we don't need to recompute
2661  // the Fq_representors as we have already read them in from file!
2663 
2664 
2665  libMesh::out << "Reading in the Aq_representors..." << std::endl;
2666 
2667  // Read in the Aq representors. The class makes room for [Q_a][Nmax] of these. We are going to
2668  // read in [Q_a][get_rb_evaluation().get_n_basis_functions()]. FIXME:
2669  // should we be worried about leaks in the locations where we're about to fill entries?
2670  RBEvaluation & rbe = get_rb_evaluation();
2671  for (const auto & row : rbe.Aq_representor)
2672  for (const auto & rep : row)
2673  libmesh_error_msg_if(rep, "Error, must delete existing Aq_representor before reading in from file.");
2674 
2675  // Now ready to read them in from file!
2676  for (auto i : index_range(rbe.Aq_representor))
2677  for (unsigned int j=0; j<rbe.get_n_basis_functions(); ++j)
2678  {
2679  file_name.str(""); // reset filename
2680  file_name << riesz_representors_dir
2681  << "/Aq_representor" << i << "_" << j << riesz_representor_suffix;
2682 
2683  // On processor zero check to be sure the file exists
2684  if (this->processor_id() == 0)
2685  {
2686  int stat_result = stat(file_name.str().c_str(), &stat_info);
2687 
2688  libmesh_error_msg_if(stat_result != 0, "File does not exist: " << file_name.str());
2689  }
2690 
2691  Xdr aqr_data(file_name.str(), read_binary_residual_representors ? DECODE : READ);
2692 
2693  read_serialized_data(aqr_data, false);
2694 
2695  rbe.Aq_representor[i][j] = NumericVector<Number>::build(this->comm());
2696  rbe.Aq_representor[i][j]->init (n_dofs(), n_local_dofs(),
2697  false, PARALLEL);
2698 
2699  // No need to copy, just swap
2700  rbe.Aq_representor[i][j]->swap(*solution);
2701  }
2702 }
2703 
2705 {
2707 
2708  conv_flag = input_solver.get_converged_reason();
2709 
2710  libmesh_error_msg_if(conv_flag < 0, "Convergence error. Error id: " << conv_flag);
2711 }
2712 
2714 {
2715  return assert_convergence;
2716 }
2717 
2719 {
2720  assert_convergence = flag;
2721 }
2722 
2724 {
2725  return _preevaluate_thetas_flag;
2726 }
2727 
2729 {
2730  _preevaluate_thetas_flag = flag;
2731 }
2732 
2734 {
2736 }
2737 
2739 {
2741 }
2742 
2743 const std::vector<Number> &
2744 RBConstruction::get_evaluated_thetas(unsigned int training_parameter_index) const
2745 {
2746  const numeric_index_type first_index = get_first_local_training_index();
2747  libmesh_assert(training_parameter_index >= first_index);
2748 
2749  const numeric_index_type local_index = training_parameter_index - first_index;
2750  libmesh_assert(local_index < _evaluated_thetas.size());
2751 
2752  return _evaluated_thetas[local_index];
2753 }
2754 
2756 {
2757  LOG_SCOPE("preevaluate_thetas()", "RBConstruction");
2758 
2760 
2761  // Early return if we've already preevaluated thetas.
2763  return;
2764 
2765  if ( get_local_n_training_samples() == 0 )
2766  return;
2767 
2768  auto & rb_theta_expansion = get_rb_evaluation().get_rb_theta_expansion();
2769  const unsigned int n_A_terms = rb_theta_expansion.get_n_A_terms();
2770  const unsigned int n_F_terms = rb_theta_expansion.get_n_F_terms();
2771  const unsigned int n_outputs = rb_theta_expansion.get_total_n_output_terms();
2772 
2773  // Collect all training parameters
2774  // TODO: Here instead of using a vector of RBParameters objects,
2775  // we could use a single RBParameters object with multiple samples.
2776  // This would save memory over the current approach, but that may
2777  // not be a big deal in practice unless the number of training samples
2778  // is very large for some reason.
2779  std::vector<RBParameters> mus(get_local_n_training_samples());
2780  const numeric_index_type first_index = get_first_local_training_index();
2781  for (unsigned int i=0; i<get_local_n_training_samples(); i++)
2782  {
2783  // Load training parameter i, this is only loaded
2784  // locally since the RB solves are local.
2785  set_params_from_training_set( first_index+i );
2786  mus[i] = get_parameters();
2787  _evaluated_thetas[i].resize(n_A_terms + n_F_terms + n_outputs);
2788  }
2789 
2790  // Evaluate thetas for all training parameters simultaneously
2791  for (unsigned int q_a=0; q_a<n_A_terms; q_a++)
2792  {
2793  const auto A_vals = rb_theta_expansion.eval_A_theta(q_a, mus);
2794  for (auto i : make_range(get_local_n_training_samples()))
2795  _evaluated_thetas[i][q_a] = A_vals[i];
2796  }
2797 
2798  for (unsigned int q_f=0; q_f<n_F_terms; q_f++)
2799  {
2800  const auto F_vals = rb_theta_expansion.eval_F_theta(q_f, mus);
2801  for (auto i : make_range(get_local_n_training_samples()))
2802  _evaluated_thetas[i][n_A_terms + q_f] = F_vals[i];
2803  }
2804 
2805  {
2806  unsigned int output_counter = 0;
2807  for (unsigned int n=0; n<rb_theta_expansion.get_n_outputs(); n++)
2808  for (unsigned int q_l=0; q_l<rb_theta_expansion.get_n_output_terms(n); q_l++)
2809  {
2810  // Evaluate the current output functional term for all
2811  // training parameters simultaneously.
2812  const auto output_vals = rb_theta_expansion.eval_output_theta(n, q_l, mus);
2813 
2814  // TODO: the size of _evaluated_thetas is currently assumed to be
2815  // the same as get_local_n_training_samples(), but this won't be
2816  // the case if we use RBParameters objects that have multiple samples.
2817  // So just make sure that's the case for now.
2818  libmesh_error_msg_if(output_vals.size() != get_local_n_training_samples(),
2819  "We currently only support single-sample RBParameters "
2820  "objects during the training stage.");
2821 
2822  for (auto i : index_range(output_vals))
2823  _evaluated_thetas[i][n_A_terms + n_F_terms + output_counter] = output_vals[i];
2824 
2825  // Go to next output term
2826  output_counter++;
2827  }
2828  }
2829 
2831 }
2832 
2834 {
2836 }
2837 
2838 } // namespace libMesh
virtual void read_riesz_representors_from_files(const std::string &riesz_representors_dir, const bool write_binary_residual_representors)
Read in all the Riesz representor data from files.
T libmesh_real(T a)
Real compute_residual_dual_norm_slow(const unsigned int N)
The slow (but simple, non-error prone) way to compute the residual dual norm.
numeric_index_type get_first_local_training_index() const
Get the first local index of the training parameters.
bool normalize_rb_bound_in_greedy
This boolean indicates if we normalize the RB error in the greedy using RBEvaluation::get_error_bound...
std::vector< std::vector< std::unique_ptr< NumericVector< Number > > > > Aq_representor
Vector storing the residual representors associated with the left-hand side.
virtual void initialize_rb_construction(bool skip_matrix_assembly=false, bool skip_vector_assembly=false)
Allocate all the data structures necessary for the construction stage of the RB method.
ElemType
Defines an enum for geometric element types.
void write_serialized_data(Xdr &io, const bool write_additional_data=true) const
Writes additional data, namely vectors, for this System.
Definition: system_io.C:1668
dof_id_type end_dof(const processor_id_type proc) const
Definition: dof_map_base.h:191
This is the EquationSystems class.
virtual Number eval_output_theta(unsigned int output_index, unsigned int q_l, const RBParameters &mu) const
Evaluate theta_q_l at the current parameter.
const DenseMatrix< Number > & get_elem_jacobian() const
Const accessor for element Jacobian.
Definition: diff_context.h:274
std::vector< Real > training_error_bounds
Vector storing the values of the error bound for each parameter in the training set — the parameter ...
bool get_normalize_rb_bound_in_greedy() const
bool quiet_mode
Flag to indicate whether we print out extra information during the Offline stage. ...
A Node is like a Point, but with more information.
Definition: node.h:52
virtual void truth_assembly()
Assemble the truth matrix and right-hand side for current_parameters.
void set_RB_training_type(const std::string &RB_training_type_in)
Get/set the string that determines the training type.
std::vector< Number > Fq_representor_innerprods
Vectors storing the residual representor inner products to be used in computing the residuals online...
virtual void pre_fe_reinit(const System &, const Elem *e)
Reinitializes local data vectors/matrices on the current geometric element.
Definition: fem_context.C:1683
void zero_constrained_dofs_on_vector(NumericVector< Number > &vector) const
It is sometimes useful to be able to zero vector entries that correspond to constrained dofs...
boost::multiprecision::float128 real(const boost::multiprecision::float128 in)
void set_energy_inner_product(const std::vector< Number > &energy_inner_product_coeffs_in)
Specify the coefficients of the A_q operators to be used in the energy inner-product.
std::vector< std::vector< Number > > _evaluated_thetas
Storage of evaluated theta functions at a set of parameters.
std::vector< std::unique_ptr< NumericVector< Number > > > Fq_representor
Vector storing the residual representors associated with the right-hand side.
T libmesh_conj(T a)
ElemAssembly & get_F_assembly(unsigned int q)
Return a reference to the specified F_assembly object.
virtual void post_process_elem_matrix_and_vector(DGFEMContext &)
This function is called from add_scaled_matrix_and_vector() before each element matrix and vector are...
unsigned int get_n_F_terms() const
Get Q_f, the number of terms in the affine expansion for the right-hand side.
bool assert_convergence
A boolean flag to indicate whether to check for proper convergence after each solve.
ConstFunction that simply returns 0.
Definition: zero_function.h:38
virtual Real train_reduced_basis(const bool resize_rb_eval_data=true)
Train the reduced basis.
virtual void resize_data_structures(const unsigned int Nmax, bool resize_error_bound_data=true)
Resize and clear the data vectors corresponding to the value of Nmax.
bool _normalize_solution_snapshots
Set this boolean to true if we want to normalize solution snapshots used in training to have norm of ...
virtual void get_all_matrices(std::map< std::string, SparseMatrix< Number > *> &all_matrices)
Get a map that stores pointers to all of the matrices.
std::vector< std::unique_ptr< NumericVector< Number > > > _untransformed_basis_functions
In cases where we have dof transformations such as a change of coordinates at some nodes we need to s...
void constrain_element_matrix_and_vector(DenseMatrix< Number > &matrix, DenseVector< Number > &rhs, std::vector< dof_id_type > &elem_dofs, bool asymmetric_constraint_rows=true) const
Constrains the element matrix and vector.
Definition: dof_map.h:2330
DenseVector< Number > RB_solution
The RB solution vector.
void set_preevaluate_thetas_flag(bool flag)
virtual Real truth_solve(int plot_solution)
Perform a "truth" solve, i.e.
RBConstruction(EquationSystems &es, const std::string &name, const unsigned int number)
Constructor.
unsigned int _n_linear_iterations
The number of linear iterations required to solve the linear system Ax=b.
void vector_mult(NumericVector< T > &dest, const NumericVector< T > &arg) const
Multiplies the matrix by the NumericVector arg and stores the result in NumericVector dest...
const Elem & get_elem() const
Accessor for current Elem object.
Definition: fem_context.h:908
std::string get_timestamp()
Definition: timestamp.C:37
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition: exodusII_io.h:52
Real get_parameter_min(const std::string &param_name) const
Get minimum allowable value of parameter param_name.
DenseMatrix< Number > RB_inner_product_matrix
The inner product matrix.
Real get_parameter_max(const std::string &param_name) const
Get maximum allowable value of parameter param_name.
SparseMatrix< Number > * get_Aq(unsigned int q)
Get a pointer to Aq.
bool compute_RB_inner_product
Boolean flag to indicate whether we compute the RB_inner_product_matrix.
virtual void side_fe_reinit() override
Override side_fe_reinit to set a boolean flag so that by default DG terms are assumed to be inactive...
virtual void set_context_solution_vec(NumericVector< Number > &vec)
Set current_local_solution = vec so that we can access vec from FEMContext during assembly...
virtual void add_vector(const T *v, const std::vector< numeric_index_type > &dof_indices)
Computes , where v is a pointer and each dof_indices[i] specifies where to add value v[i]...
const std::vector< dof_id_type > & get_neighbor_dof_indices() const
Accessor for neighbor dof indices.
virtual void interior_assembly(FEMContext &)
Perform the element interior assembly.
Definition: elem_assembly.h:55
std::vector< std::unique_ptr< NumericVector< Number > > > basis_functions
The libMesh vectors storing the finite element coefficients of the RB basis functions.
const RBParameters & get_parameters_max() const
Get an RBParameters object that specifies the maximum allowable value for each parameter.
const EquationSystems & get_equation_systems() const
Definition: system.h:721
void sum(T &r) const
void attach_matrix(SparseMatrix< Number > &matrix)
Additional matrices may be attached to this DofMap.
Definition: dof_map.C:274
void enrich_basis_from_rhs_terms(const bool resize_rb_eval_data=true)
This function computes one basis function for each rhs term.
std::vector< std::vector< Number > > output_dual_innerprods
The vector storing the dual norm inner product terms for each output.
void barrier() const
MeshBase & mesh
ElemAssembly & get_inner_product_assembly()
void set_quiet_mode(bool quiet_mode_in)
Set the quiet_mode flag.
processor_id_type rank() const
NumericVector< Number > * rhs
The system matrix.
LinearConvergenceReason
Linear solver convergence flags (taken from the PETSc flags).
unsigned int get_n_A_terms() const
Get Q_a, the number of terms in the affine expansion for the bilinear form.
bool get_convergence_assertion_flag() const
Getter for the flag determining if convergence should be checked after each solve.
This proxy class acts like a container of indices from a single coupling row.
const Parallel::Communicator & comm() const
bool is_quiet() const
Is the system in quiet mode?
bool skip_degenerate_sides
In some cases meshes are intentionally created with degenerate sides as a way to represent, say, triangles using a hex-only mesh.
virtual bool greedy_termination_test(Real abs_greedy_error, Real initial_greedy_error, int count)
Function that indicates when to terminate the Greedy basis training.
unsigned int m() const
ElemAssembly & get_output_assembly(unsigned int output_index, unsigned int q_l)
Return a reference to the specified output assembly object.
void add_scaled_Aq(Number scalar, unsigned int q_a, SparseMatrix< Number > *input_matrix, bool symmetrize)
Add the scaled q^th affine matrix to input_matrix.
const RBParameters & get_greedy_parameter(unsigned int i)
Return the parameters chosen during the i^th step of the Greedy algorithm.
int mkdir(const char *pathname)
Create a directory.
Definition: utility.C:152
std::vector< std::vector< Number > > output_dual_innerprods
The vector storing the dual norm inner product terms for each output.
std::string RB_training_type
This string indicates the type of training that we will use.
SparseMatrix< Number > * get_non_dirichlet_inner_product_matrix()
Get the non-Dirichlet (or more generally no-constraints) version of the inner-product matrix...
static void get_global_max_error_pair(const Parallel::Communicator &communicator, std::pair< numeric_index_type, Real > &error_pair)
Static function to return the error pair (index,error) that is corresponds to the largest error on al...
NumericVector< Number > * get_non_dirichlet_Fq_if_avail(unsigned int q)
Get a pointer to non_dirichlet_Fq if it&#39;s available, otherwise get Fq.
virtual std::unique_ptr< DGFEMContext > build_context()
Builds a DGFEMContext object with enough information to do evaluations on each element.
virtual LinearSolver< Number > * get_linear_solver() const override
std::unique_ptr< SparseMatrix< Number > > inner_product_matrix
The inner product matrix.
virtual Real get_error_bound_normalization()
const DenseMatrix< Number > & get_neighbor_neighbor_jacobian() const
Const accessor for element-neighbor Jacobian.
const std::string & get_RB_training_type() const
The libMesh namespace provides an interface to certain functionality in the library.
NumericVector< Number > * get_Fq(unsigned int q)
Get a pointer to Fq.
virtual T dot(const NumericVector< T > &v) const =0
dof_id_type n_local_dofs() const
Definition: system.C:158
std::vector< Number > Fq_representor_innerprods
Vectors storing the residual representor inner products to be used in computing the residuals online...
std::vector< RBParameters > greedy_param_list
The list of parameters selected by the Greedy algorithm in generating the Reduced Basis associated wi...
unsigned int get_current_training_parameter_index() const
Get/set the current training parameter index.
virtual void get_nodal_values(std::vector< dof_id_type > &, DenseMatrix< Number > &, DenseVector< Number > &, const System &, const Node &)
Get values to add to the matrix or rhs vector based on node.
Definition: elem_assembly.h:67
unsigned int get_n_outputs() const
Get n_outputs, the number output functionals.
Real get_rel_training_tolerance() const
unsigned char get_side() const
Accessor for current side of Elem object.
Definition: fem_context.h:922
const MeshBase & get_mesh() const
Definition: system.h:2358
void broadcast_parameters(const unsigned int proc_id)
Broadcasts parameters from processor proc_id to all processors.
void reset_preevaluate_thetas_completed()
Reset the _preevaluate_thetas_completed flag to false.
static std::unique_ptr< SparseMatrix< T > > build(const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package(), const MatrixBuildType matrix_build_type=MatrixBuildType::AUTOMATIC)
Builds a SparseMatrix<T> using the linear solver package specified by solver_package.
virtual void print_info() const
Print out info that describes the current setup of this RBConstruction.
This class stores the set of RBTheta functor objects that define the "parameter-dependent expansion" ...
static std::unique_ptr< DirichletBoundary > build_zero_dirichlet_boundary_object()
It&#39;s helpful to be able to generate a DirichletBoundary that stores a ZeroFunction in order to impose...
std::vector< Number > energy_inner_product_coeffs
We may optionally want to use the "energy inner-product" rather than the inner-product assembly speci...
dof_id_type n_dofs() const
Definition: system.C:121
virtual void init_context(FEMContext &)
Initialize the FEMContext prior to performing an element loop.
virtual Real compute_max_error_bound()
(i) Compute the a posteriori error bound for each set of parameters in the training set...
This is the MeshBase class.
Definition: mesh_base.h:75
virtual void zero()=0
Set all entries to zero.
virtual void elem_fe_reinit(const std::vector< Point > *const pts=nullptr)
Reinitializes interior FE objects on the current geometric element.
Definition: fem_context.C:1477
std::unique_ptr< NumericVector< Number > > inner_product_storage_vector
We keep an extra temporary vector that is useful for performing inner products (avoids unnecessary me...
void print_basis_function_orthogonality() const
Print out a matrix that shows the orthogonality of the RB basis functions.
std::vector< std::unique_ptr< NumericVector< Number > > > non_dirichlet_Fq_vector
virtual void get_output_vectors(std::map< std::string, NumericVector< Number > *> &all_vectors)
Get a map that stores pointers to all of the vectors.
virtual void add(const numeric_index_type i, const numeric_index_type j, const T value)=0
Add value to the element (i,j).
virtual void load_training_set(const std::map< std::string, std::vector< RBParameter >> &new_training_set)
Overwrite the training parameters with new_training_set.
void set_rb_construction_parameters(unsigned int n_training_samples_in, bool deterministic_training_in, int training_parameters_random_seed_in, bool quiet_mode_in, unsigned int Nmax_in, Real rel_training_tolerance_in, Real abs_training_tolerance_in, bool normalize_rb_error_bound_in_greedy_in, const std::string &RB_training_type_in, const RBParameters &mu_min_in, const RBParameters &mu_max_in, const std::map< std::string, std::vector< Real >> &discrete_parameter_values_in, const std::map< std::string, bool > &log_scaling, std::map< std::string, std::vector< RBParameter >> *training_sample_list=nullptr)
Set the state of this RBConstruction object based on the arguments to this function.
void assemble_inner_product_matrix(SparseMatrix< Number > *input_matrix, bool apply_dof_constraints=true)
Assemble the inner product matrix and store it in input_matrix.
LinearSolver< Number > * extra_linear_solver
Also, we store a pointer to an extra linear solver.
FEType get_fe_type() const
Definition: fe_abstract.h:520
virtual void boundary_assembly(FEMContext &)
Perform the element boundary assembly.
Definition: elem_assembly.h:60
Generic sparse matrix.
Definition: vector_fe_ex5.C:46
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:179
virtual void scale(const T factor)=0
Scale each element of the vector by the given factor.
virtual void recompute_all_residual_terms(const bool compute_inner_products=true)
This function computes all of the residual representors, can be useful when restarting a basis traini...
virtual void load_basis_function(unsigned int i)
Load the i^th RB function into the RBConstruction solution vector.
std::unique_ptr< QBase > default_quadrature_rule(const unsigned int dim, const int extraorder=0) const
Definition: fe_type.C:34
This base class can be inherited from to provide interfaces to linear solvers from different packages...
std::vector< DenseVector< Number > > RB_Fq_vector
Dense vector for the RHS.
virtual void initialize_training_parameters(const RBParameters &mu_min, const RBParameters &mu_max, const unsigned int n_global_training_samples, const std::map< std::string, bool > &log_param_scale, const bool deterministic=true)
Initialize the parameter ranges and indicate whether deterministic or random training parameters shou...
virtual void solve_for_matrix_and_rhs(LinearSolver< Number > &input_solver, SparseMatrix< Number > &input_matrix, NumericVector< Number > &input_rhs)
Assembles & solves the linear system A*x=b for the specified matrix input_matrix and right-hand side ...
bool impose_internal_fluxes
Boolean flag to indicate whether we impose "fluxes" (i.e.
bool Fq_representor_innerprods_computed
A boolean flag to indicate whether or not the Fq representor norms have already been computed — used...
void set_training_random_seed(int seed)
Set the seed that is used to randomly generate training parameters.
bool use_empty_rb_solve_in_greedy
A boolean flag to indicate whether or not we initialize the Greedy algorithm by performing rb_solves ...
bool use_energy_inner_product
Boolean to indicate whether we&#39;re using the energy inner-product.
void libmesh_ignore(const Args &...)
virtual void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols)=0
Add the full matrix dm to the SparseMatrix.
RBThetaExpansion & get_rb_theta_expansion()
Get a reference to the RBThetaExpansion object that that belongs to rb_eval.
bool get_preevaluate_thetas_flag() const
Get/set flag to pre-evaluate the theta functions.
const T & get(std::string_view) const
Definition: parameters.h:426
void set_rb_assembly_expansion(RBAssemblyExpansion &rb_assembly_expansion_in)
Set the rb_assembly_expansion object.
virtual Real l2_norm() const =0
virtual void zero()=0
Set all entries to 0.
const RBParameters & get_parameters_min() const
Get an RBParameters object that specifies the minimum allowable value for each parameter.
std::unique_ptr< SparseMatrix< Number > > non_dirichlet_inner_product_matrix
dof_id_type numeric_index_type
Definition: id_types.h:99
virtual void init(const char *name=nullptr)=0
Initialize data structures if not done so already.
virtual T el(const unsigned int i, const unsigned int j) const override final
Definition: dense_matrix.h:126
void update_greedy_param_list()
Update the list of Greedily chosen parameters with current_parameters.
Real rel_training_tolerance
Relative and absolute tolerances for training reduced basis using the Greedy scheme.
unsigned int _current_training_parameter_index
The current training parameter index during reduced basis training.
unsigned int n_linear_iterations() const
void svd(DenseVector< Real > &sigma)
Compute the singular value decomposition of the matrix.
unsigned int get_Nmax() const
Get/set Nmax, the maximum number of RB functions we are willing to compute.
RBAssemblyExpansion & get_rb_assembly_expansion()
virtual void write_equation_systems(const std::string &fname, const EquationSystems &es, const std::set< std::string > *system_names=nullptr) override
Writes out the solution for no specific time or timestep.
Definition: exodusII_io.C:2033
SparseMatrix< Number > * get_inner_product_matrix()
Get a pointer to inner_product_matrix.
void get_transpose(DenseMatrix< T > &dest) const
Put the tranposed matrix into dest.
virtual void update_RB_system_matrices()
Compute the reduced basis matrices for the current basis.
NumericVector< Number > & get_basis_function(unsigned int i)
Get a reference to the i^th basis function.
virtual void update_residual_terms(bool compute_inner_products=true)
Compute the terms that are combined ‘online’ to determine the dual norm of the residual.
bool store_untransformed_basis
Boolean flag to indicate whether we store a second copy of the basis without constraints or dof trans...
void assemble_Fq_vector(unsigned int q, NumericVector< Number > *input_vector, bool apply_dof_constraints=true)
Assemble the q^th affine vector and store it in input_matrix.
numeric_index_type get_last_local_training_index() const
Get the last local index of the training parameters.
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition: system.h:1593
const std::vector< Number > & get_evaluated_thetas(unsigned int training_parameter_index) const
Return the evaluated theta functions at the given training parameter index.
numeric_index_type get_local_n_training_samples() const
Get the total number of training samples local to this processor.
virtual void update_system()
Update the system after enriching the RB space; this calls a series of functions to update the system...
Real get_abs_training_tolerance() const
This class extends FEMContext in order to provide extra data required to perform local element residu...
libmesh_assert(ctx)
bool is_constrained_dof(const dof_id_type dof) const
Definition: dof_map.h:2258
virtual SparseMatrix< Number > & get_matrix_for_output_dual_solves()
Return the matrix for the output residual dual norm solves.
The Tri3Subdivision element is a three-noded subdivision surface shell element used in mechanics calc...
NumericVector< Number > * get_non_dirichlet_Fq(unsigned int q)
Get a pointer to non-Dirichlet Fq.
virtual Real get_RB_error_bound()
const std::vector< dof_id_type > & get_dof_indices() const
Accessor for element dof indices.
Definition: diff_context.h:363
unsigned int get_delta_N() const
Get delta_N, the number of basis functions we add to the RB space per iteration of the greedy algorit...
CouplingMatrix * _dof_coupling
Degree of freedom coupling.
Definition: dof_map.h:1613
const RBParameters & get_parameters() const
Get the current parameters.
void print_parameters() const
Print the current parameters.
SparseMatrix< Number > * get_non_dirichlet_Aq(unsigned int q)
Get a pointer to non_dirichlet_Aq.
void set_inner_product_assembly(ElemAssembly &inner_product_assembly_in)
Set the rb_assembly_expansion object.
std::vector< std::unique_ptr< SparseMatrix< Number > > > Aq_vector
Vector storing the Q_a matrices from the affine expansion.
void add_scaled_matrix_and_vector(Number scalar, ElemAssembly *elem_assembly, SparseMatrix< Number > *input_matrix, NumericVector< Number > *input_vector, bool symmetrize=false, bool apply_dof_constraints=true)
This function loops over the mesh and applies the specified interior and/or boundary assembly routine...
bool skip_residual_in_train_reduced_basis
Boolean flag to indicate if we skip residual calculations in train_reduced_basis. ...
void set_convergence_assertion_flag(bool flag)
Setter for the flag determining if convergence should be checked after each solve.
This class is part of the rbOOmit framework.
Definition: rb_parameters.h:52
auto norm(const T &a) -> decltype(std::abs(a))
Definition: tensor_tools.h:74
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const std::optional< double > tol=std::nullopt, const std::optional< unsigned int > m_its=std::nullopt)=0
This function calls the solver _solver_type preconditioned with the _preconditioner_type precondition...
bool is_rb_eval_initialized() const
unsigned int delta_N
The number of basis functions that we add at each greedy step.
Real train_reduced_basis_with_greedy(const bool resize_rb_eval_data)
Train the reduced basis using the "Greedy algorithm.".
RBEvaluation * rb_eval
The current RBEvaluation object we are using to perform the Evaluation stage of the reduced basis met...
Real volume(const MeshBase &mesh, unsigned int dim=libMesh::invalid_uint)
Find the total volume of a mesh (interpreting that as area for dim = 2, or total arc length for dim =...
Definition: mesh_tools.C:985
std::unique_ptr< NumericVector< Number > > _untransformed_solution
We also store a copy of the untransformed solution in order to create _untransformed_basis_functions...
numeric_index_type get_n_training_samples() const
Get the number of global training samples.
const DenseMatrix< Number > & get_elem_neighbor_jacobian() const
Const accessor for element-neighbor Jacobian.
virtual void close()=0
Calls the NumericVector&#39;s internal assembly routines, ensuring that the values are consistent across ...
This class implements a C++ interface to the XDR (eXternal Data Representation) format.
Definition: xdr_cxx.h:67
bool dg_terms_are_active() const
Are the DG terms active, i.e.
Real _final_linear_residual
The final residual for the linear system Ax=b.
std::vector< std::vector< std::vector< Number > > > Fq_Aq_representor_innerprods
Vectors storing the residual representor inner products to be used in computing the residuals online...
void set_params_from_training_set(unsigned int global_index)
Set parameters to the RBParameters stored in index global_index of the global training set...
virtual Real rb_solve(unsigned int N)
Perform online solve with the N RB basis functions, for the set of parameters in current_params, where 0 <= N <= RB_size.
This class is part of the rbOOmit framework.
Definition: rb_evaluation.h:50
virtual void post_process_truth_solution()
Similarly, provide an opportunity to post-process the truth solution after the solve is complete...
virtual unsigned int n_sides() const =0
bool set_parameters(const RBParameters &params)
Set the current parameters to params The parameters are checked for validity; an error is thrown if t...
virtual LinearConvergenceReason get_converged_reason() const =0
const DenseMatrix< Number > & get_neighbor_elem_jacobian() const
Const accessor for element-neighbor Jacobian.
const Elem * neighbor_ptr(unsigned int i) const
Definition: elem.h:2598
void set_current_training_parameter_index(unsigned int index)
const DenseVector< Number > & get_elem_residual() const
Const accessor for element residual.
Definition: diff_context.h:242
virtual void update()
Update the local values to reflect the solution on neighboring processors.
Definition: system.C:510
void set_abs_training_tolerance(Real new_training_tolerance)
Get/set the absolute tolerance for the basis training.
std::vector< DenseMatrix< Number > > RB_Aq_vector
Dense matrices for the RB computations.
virtual void close()=0
Calls the SparseMatrix&#39;s internal assembly routines, ensuring that the values are consistent across p...
virtual void print_matlab(const std::string &filename="") const
Print the contents of the vector in Matlab&#39;s sparse matrix format.
bool store_non_dirichlet_operators
Boolean flag to indicate whether we store a second copy of each affine operator and vector which does...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void clear() override
Clear all the data structures associated with the system.
virtual std::unique_ptr< Elem > side_ptr(unsigned int i)=0
unsigned int get_n_output_terms(unsigned int output_index) const
Get the number of affine terms associated with the specified output.
ElemAssembly * inner_product_assembly
Pointer to inner product assembly.
virtual void reuse_preconditioner(bool)
Set the same_preconditioner flag, which indicates if we reuse the same preconditioner for subsequent ...
unsigned char side
Current side for side_* to examine.
Definition: fem_context.h:1013
virtual std::string system_type() const override
void check_convergence(LinearSolver< Number > &input_solver)
Check if the linear solver reports convergence.
void max(const T &r, T &o, Request &req) const
std::unique_ptr< LinearSolver< Number > > inner_product_solver
We store an extra linear solver object which we can optionally use for solving all systems in which t...
void set_value(const std::string &param_name, Real value)
Set the value of the specified parameter.
void set_normalize_rb_bound_in_greedy(bool normalize_rb_bound_in_greedy_in)
Get/set the boolean to indicate if we normalize the RB error in the greedy.
std::vector< std::unique_ptr< NumericVector< Number > > > Fq_vector
Vector storing the Q_f vectors in the affine decomposition of the right-hand side.
This class stores the set of ElemAssembly functor objects that define the "parameter-independent expa...
OStreamProxy out
virtual bool is_serial_training_type(const std::string &RB_training_type_in)
SparseMatrix< Number > * matrix
The system matrix.
virtual unsigned int get_n_basis_functions() const
Get the current number of basis functions.
ElemAssembly & get_A_assembly(unsigned int q)
Return a reference to the specified A_assembly object.
ElemAssembly provides a per-element (interior and boundary) assembly functionality.
Definition: elem_assembly.h:38
virtual void preevaluate_thetas()
std::vector< Number > truth_outputs
Vector storing the truth output values from the most recent truth solve.
SparseMatrix< Number > * get_non_dirichlet_inner_product_matrix_if_avail()
Get the non-Dirichlet inner-product matrix if it&#39;s available, otherwise get the inner-product matrix ...
static const bool value
Definition: xdr_io.C:54
virtual void compute_Fq_representor_innerprods(bool compute_inner_products=true)
Compute the terms that are combined ‘online’ to determine the dual norm of the residual.
virtual void set_Nmax(unsigned int Nmax)
std::vector< std::unique_ptr< SparseMatrix< Number > > > non_dirichlet_Aq_vector
We may also need a second set of matrices/vectors that do not have the Dirichlet boundary conditions ...
void set_rel_training_tolerance(Real new_training_tolerance)
Get/set the relative tolerance for the basis training.
std::vector< std::vector< std::unique_ptr< NumericVector< Number > > > > non_dirichlet_outputs_vector
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
Definition: int_range.h:140
virtual void write_riesz_representors_to_files(const std::string &riesz_representors_dir, const bool write_binary_residual_representors)
Write out all the Riesz representor data to files.
virtual void assemble_all_output_vectors()
Assemble and store the output vectors.
static std::unique_ptr< NumericVector< T > > build(const Parallel::Communicator &comm, SolverPackage solver_package=libMesh::default_solver_package(), ParallelType parallel_type=AUTOMATIC)
Builds a NumericVector on the processors in communicator comm using the linear solver package specifi...
Parameters parameters
Data structure holding arbitrary parameters.
virtual unsigned int size() const override final
Definition: dense_vector.h:104
bool _preevaluate_thetas_flag
Flag to indicate if we preevaluate the theta functions.
std::unique_ptr< NumericVector< Number > > current_local_solution
All the values I need to compute my contribution to the simulation at hand.
Definition: system.h:1605
bool serial_training_set
This boolean flag indicates whether or not the training set should be the same on all processors...
void assemble_Aq_matrix(unsigned int q, SparseMatrix< Number > *input_matrix, bool apply_dof_constraints=true)
Assemble the q^th affine matrix and store it in input_matrix.
virtual void process_parameters_file(const std::string &parameters_filename)
Read in from the file specified by parameters_filename and set the this system&#39;s member variables acc...
RBAssemblyExpansion * rb_assembly_expansion
This member holds the (parameter independent) assembly functors that define the "affine expansion" of...
void get_element_fe(unsigned int var, FEGenericBase< OutputShape > *&fe) const
Accessor for interior finite element object for variable var for the largest dimension in the mesh...
Definition: fem_context.h:277
NumericVector< Number > * get_output_vector(unsigned int n, unsigned int q_l)
Get a pointer to the n^th output.
virtual bool check_if_zero_truth_solve() const
RBEvaluation & get_rb_evaluation()
Get a reference to the RBEvaluation object.
virtual void assemble_misc_matrices()
Assemble and store all the inner-product matrix, the constraint matrix (for constrained problems) and...
virtual void get_all_vectors(std::map< std::string, NumericVector< Number > *> &all_vectors)
Get a map that stores pointers to all of the vectors.
virtual void set(const numeric_index_type i, const T value)=0
Sets v(i) = value.
const DenseMatrix< Number > & get_elem_elem_jacobian() const
Const accessor for element-element Jacobian.
const std::string & name() const
Definition: system.h:2342
dof_id_type first_dof(const processor_id_type proc) const
Definition: dof_map_base.h:185
virtual void load_rb_solution()
Load the RB solution from the most recent solve with rb_eval into this system&#39;s solution vector...
void set_rb_evaluation(RBEvaluation &rb_eval_in)
Set the RBEvaluation object.
unsigned int n_vars() const
Definition: system.h:2430
unsigned int n() const
virtual void add(const numeric_index_type i, const T value)=0
Adds value to the vector entry specified by i.
virtual void compute_output_dual_innerprods()
Compute and store the dual norm of each output functional.
bool assemble_before_solve
Flag which tells the system to whether or not to call the user assembly function during each call to ...
Definition: system.h:1547
processor_id_type processor_id() const
void initialize_parameters(const RBParameters &mu_min_in, const RBParameters &mu_max_in, const std::map< std::string, std::vector< Real >> &discrete_parameter_values)
Initialize the parameter ranges and set current_parameters.
bool exit_on_repeated_greedy_parameters
Boolean flag to indicate whether we exit the greedy if we select the same parameters twice in a row...
std::unique_ptr< LinearSolver< Number > > linear_solver
This class handles all the details of interfacing with various linear algebra packages like PETSc or ...
bool output_dual_innerprods_computed
A boolean flag to indicate whether or not the output dual norms have already been computed — used to...
const DofMap & get_dof_map() const
Definition: system.h:2374
processor_id_type processor_id() const
Definition: dof_object.h:905
void train_reduced_basis_with_POD()
Train the reduced basis using Proper Orthogonal Decomposition (POD).
unsigned int get_n_params() const
Get the number of parameters.
virtual void enrich_RB_space()
Add a new basis function to the RB space.
virtual void assemble_all_affine_vectors()
Assemble and store the affine RHS vectors.
void read_serialized_data(Xdr &io, const bool read_additional_data=true)
Reads additional data, namely vectors, for this System.
Definition: system_io.C:680
virtual void attach_quadrature_rule(QBase *q)=0
Provides the class with the quadrature rule.
NumericVector< Number > * get_non_dirichlet_output_vector(unsigned int n, unsigned int q_l)
Get a pointer to non-Dirichlet output vector.
virtual void allocate_data_structures()
Helper function that actually allocates all the data structures required by this class.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition: int_range.h:117
RBThetaExpansion & get_rb_theta_expansion()
Get a reference to the rb_theta_expansion.
Definition: rb_evaluation.C:85
bool _preevaluate_thetas_completed
Flag to indicate if the preevaluate_thetas function has been called, since this allows us to avoid ca...
This class forms the foundation from which generic finite elements may be derived.
SparseMatrix< Number > * get_non_dirichlet_Aq_if_avail(unsigned int q)
Get a pointer to non_dirichlet_Aq if it&#39;s available, otherwise get Aq.
virtual void clear()
Clear all the data structures associated with the system.
bool is_discrete_parameter(const std::string &mu_name) const
Is parameter mu_name discrete?
unsigned int Nmax
Maximum number of reduced basis functions we are willing to use.
virtual void assemble_affine_expansion(bool skip_matrix_assembly, bool skip_vector_assembly)
Assemble the matrices and vectors for this system.
std::vector< std::vector< std::vector< Number > > > Aq_Aq_representor_innerprods
void print_discrete_parameter_values() const
Print out all the discrete parameter values.
uint8_t dof_id_type
Definition: id_types.h:67
void enforce_constraints_exactly(const System &system, NumericVector< Number > *v=nullptr, bool homogeneous=false) const
Constrains the numeric vector v, which represents a solution defined on the mesh. ...
Definition: dof_map.h:2350
std::vector< std::vector< std::unique_ptr< NumericVector< Number > > > > outputs_vector
The libMesh vectors that define the output functionals.
This class defines a coupling matrix.
virtual void assemble_all_affine_operators()
Assemble and store all Q_a affine operators as well as the inner-product matrix.
virtual void localize(std::vector< T > &v_local) const =0
Creates a copy of the global vector in the local vector v_local.
std::vector< std::vector< DenseVector< Number > > > RB_output_vectors
The vectors storing the RB output vectors.