LCOV - code coverage report
Current view: top level - include/reduced_basis - rb_evaluation.h (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4229 (6a9aeb) with base 727f46 Lines: 3 3 100.0 %
Date: 2025-08-19 19:27:09 Functions: 2 3 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : #ifndef LIBMESH_RB_EVALUATION_H
      21             : #define LIBMESH_RB_EVALUATION_H
      22             : 
      23             : // rbOOmit includes
      24             : #include "libmesh/rb_parametrized.h"
      25             : 
      26             : // libMesh includes
      27             : #include "libmesh/dense_matrix.h"
      28             : #include "libmesh/dense_vector.h"
      29             : #include "libmesh/parallel_object.h"
      30             : 
      31             : // C++ includes
      32             : #include <memory>
      33             : 
      34             : namespace libMesh
      35             : {
      36             : 
      37             : class System;
      38             : template <typename T> class NumericVector;
      39             : class RBThetaExpansion;
      40             : 
      41             : /**
      42             :  * This class is part of the rbOOmit framework.
      43             :  *
      44             :  * RBEvaluation encapsulates the functionality required
      45             :  * to _evaluate_ a given reduced basis model.
      46             :  *
      47             :  * \author David J. Knezevic
      48             :  * \date 2011
      49             :  */
      50          64 : class RBEvaluation : public RBParametrized,
      51             :                      public ParallelObject
      52             : {
      53             : public:
      54             : 
      55             :   /**
      56             :    * Constructor.
      57             :    */
      58             :   RBEvaluation (const Parallel::Communicator & comm);
      59             : 
      60             :   /**
      61             :    * Special functions.
      62             :    * - This class contains unique_ptrs, so it can't be default copy
      63             :    *   constructed/assigned.
      64             :    * - The destructor is defaulted out of line.
      65             :    */
      66             :   RBEvaluation (RBEvaluation &&) = default;
      67             :   RBEvaluation (const RBEvaluation &) = delete;
      68             :   RBEvaluation & operator= (const RBEvaluation &) = delete;
      69             :   RBEvaluation & operator= (RBEvaluation &&) = default;
      70             :   virtual ~RBEvaluation ();
      71             : 
      72             :   /**
      73             :    * Clear this RBEvaluation object. Delete the basis functions
      74             :    * and clear and extra data in subclasses.
      75             :    */
      76             :   virtual void clear() override;
      77             : 
      78             :   /**
      79             :    * Set the RBThetaExpansion object.
      80             :    */
      81             :   void set_rb_theta_expansion(RBThetaExpansion & rb_theta_expansion_in);
      82             : 
      83             :   /**
      84             :    * Get a reference to the rb_theta_expansion.
      85             :    */
      86             :   RBThetaExpansion & get_rb_theta_expansion();
      87             :   const RBThetaExpansion & get_rb_theta_expansion() const;
      88             : 
      89             :   /**
      90             :    * \returns \p true if the theta expansion has been initialized.
      91             :    */
      92             :   bool is_rb_theta_expansion_initialized() const;
      93             : 
      94             :   /**
      95             :    * Resize and clear the data vectors corresponding to the value of
      96             :    * \p Nmax. Optionally resize the data structures required for the
      97             :    * error bound.  Override to also clear and resize any extra data in
      98             :    * subclasses.
      99             :    */
     100             :   virtual void resize_data_structures(const unsigned int Nmax,
     101             :                                       bool resize_error_bound_data=true);
     102             : 
     103             :   /**
     104             :    * Get a reference to the i^th basis function.
     105             :    */
     106             :   NumericVector<Number> & get_basis_function(unsigned int i);
     107             :   const NumericVector<Number> & get_basis_function(unsigned int i) const;
     108             : 
     109             :   /**
     110             :    * Perform online solve with the N RB basis functions, for the
     111             :    * set of parameters in current_params, where 0 <= N <= RB_size.
     112             :    * \returns The (absolute) error bound associated with
     113             :    * the RB approximation.
     114             :    * With an empty RB space (N=0), our RB solution is zero, but we
     115             :    * still obtain a meaningful error bound associated with the
     116             :    * forcing terms.
     117             :    */
     118             :   virtual Real rb_solve(unsigned int N);
     119             : 
     120             :   /**
     121             :    * The same as above, except that we pass in evaluated_thetas
     122             :    * instead of recomputing the theta values.
     123             :    */
     124             :   virtual Real rb_solve(unsigned int N,
     125             :                         const std::vector<Number> * evaluated_thetas);
     126             : 
     127             :   /**
     128             :    * \returns A scaling factor that we can use to provide a consistent
     129             :    * scaling of the RB error bound across different parameter values.
     130             :    */
     131             :   virtual Real get_error_bound_normalization();
     132             : 
     133             :   /**
     134             :    * Compute the dual norm of the residual for the solution
     135             :    * saved in RB_solution_vector.
     136             :    */
     137             :   virtual Real compute_residual_dual_norm(const unsigned int N);
     138             : 
     139             :   /**
     140             :    * The same as above, except that we pass in evaluated thetas
     141             :    * instead of recomputing the theta values.
     142             :    */
     143             :   virtual Real compute_residual_dual_norm(const unsigned int N,
     144             :                                           const std::vector<Number> * evaluated_thetas);
     145             : 
     146             :   /**
     147             :    * Specifies the residual scaling on the denominator to
     148             :    * be used in the a posteriori error bound. Override
     149             :    * in subclass in order to obtain the desired error bound.
     150             :    */
     151             :   virtual Real residual_scaling_denom(Real alpha_LB);
     152             : 
     153             : #ifdef LIBMESH_ENABLE_DEPRECATED
     154             :   /**
     155             :    * Evaluate the dual norm of output \p n for the current parameters.
     156             :    *
     157             :    * This function is \deprecated, since you should either evaluate
     158             :    * the output dual norm using a vector of pre-evaluated thetas, or
     159             :    * by using the RBParameters object returned by calling
     160             :    * get_parameters() on this class. Instead call the version of this
     161             :    * function that takes an option pointer to a vector of evalauted
     162             :    * theta values.
     163             :    */
     164             :   Real eval_output_dual_norm(unsigned int n, const RBParameters & mu);
     165             : #endif // LIBMESH_ENABLE_DEPRECATED
     166             : 
     167             :   /**
     168             :    * Evaluate the dual norm of output \p n for the current parameters,
     169             :    * or using the pre-evaluted theta values provided in the "evaluated_thetas"
     170             :    * array.
     171             :    */
     172             :   Real eval_output_dual_norm(unsigned int n, const std::vector<Number> * evaluated_thetas);
     173             : 
     174             :   /**
     175             :    * Get a lower bound for the stability constant (e.g. coercivity constant or
     176             :    * inf-sup constant) at the current parameter value.
     177             :    */
     178             :   virtual Real get_stability_lower_bound();
     179             : 
     180             :   /**
     181             :    * Get the current number of basis functions.
     182             :    */
     183     1105790 :   virtual unsigned int get_n_basis_functions() const
     184     1184354 :   { return cast_int<unsigned int>(basis_functions.size()); }
     185             : 
     186             :   /**
     187             :    * Set the number of basis functions. Useful when reading in
     188             :    * stored data.
     189             :    */
     190             :   virtual void set_n_basis_functions(unsigned int n_bfs);
     191             : 
     192             :   /**
     193             :    * Clear all the Riesz representors that are used to compute the RB residual
     194             :    * (and hence error bound). This is useful since once we complete the Greedy
     195             :    * we may not need the representors any more.
     196             :    */
     197             :   virtual void clear_riesz_representors();
     198             : 
     199             :   /**
     200             :    * Write out all the data to text files in order to segregate the
     201             :    * Offline stage from the Online stage.
     202             :    *
     203             :    * \note This is a legacy method, use RBDataSerialization instead.
     204             :    */
     205             :   virtual void legacy_write_offline_data_to_files(const std::string & directory_name = "offline_data",
     206             :                                                   const bool write_binary_data=true);
     207             : 
     208             :   /**
     209             :    * Read in the saved Offline reduced basis data
     210             :    * to initialize the system for Online solves.
     211             :    *
     212             :    * \note This is a legacy method, use RBDataSerialization instead.
     213             :    */
     214             :   virtual void legacy_read_offline_data_from_files(const std::string & directory_name = "offline_data",
     215             :                                                    bool read_error_bound_data=true,
     216             :                                                    const bool read_binary_data=true);
     217             : 
     218             :   /**
     219             :    * Write out all the basis functions to file.
     220             :    * \p sys is used for file IO
     221             :    * \p directory_name specifies which directory to write files to
     222             :    * \p read_binary_basis_functions indicates whether to expect
     223             :    * binary or ASCII data
     224             :    */
     225             :   virtual void write_out_basis_functions(System & sys,
     226             :                                          const std::string & directory_name = "offline_data",
     227             :                                          const bool write_binary_basis_functions = true);
     228             : 
     229             :   /**
     230             :    * Same as write_out_basis_functions, except in this case we pass in the vectors to be
     231             :    * written.
     232             :    */
     233             :   static void write_out_vectors(System & sys,
     234             :                                 std::vector<NumericVector<Number>*> & vectors,
     235             :                                 const std::string & directory_name = "offline_data",
     236             :                                 const std::string & data_name = "bf",
     237             :                                 const bool write_binary_basis_functions = true);
     238             : 
     239             :   /**
     240             :    * Read in all the basis functions from file.
     241             :    *
     242             :    * \param sys Used for file IO.
     243             :    * \param directory_name Specifies which directory to write files to.
     244             :    * \param read_binary_basis_functions Indicates whether to expect binary or ASCII data.
     245             :    */
     246             :   virtual void read_in_basis_functions(System & sys,
     247             :                                        const std::string & directory_name = "offline_data",
     248             :                                        const bool read_binary_basis_functions = true);
     249             : 
     250             :   /**
     251             :    * Same as read_in_basis_functions, except in this case we pass in the vectors to be
     252             :    * written. We assume that the size of vectors indicates the number of vectors
     253             :    * that need to be read in.
     254             :    */
     255             :   static void read_in_vectors(System & sys,
     256             :                               std::vector<std::unique_ptr<NumericVector<Number>>> & vectors,
     257             :                               const std::string & directory_name,
     258             :                               const std::string & data_name,
     259             :                               const bool read_binary_vectors);
     260             : 
     261             :   /**
     262             :    * Performs read_in_vectors for a list of directory names and data names.
     263             :    * Reading in vectors requires us to renumber the dofs in a partition-independent
     264             :    * way. This function only renumbers the dofs once at the start (and reverts
     265             :    * it at the end), which can save a lot of work compared to renumbering on every read.
     266             :    */
     267             :   static void read_in_vectors_from_multiple_files(System & sys,
     268             :                                                   std::vector<std::vector<std::unique_ptr<NumericVector<Number>>> *> multiple_vectors,
     269             :                                                   const std::vector<std::string> & multiple_directory_names,
     270             :                                                   const std::vector<std::string> & multiple_data_names,
     271             :                                                   const bool read_binary_vectors);
     272             : 
     273             :   //----------- PUBLIC DATA MEMBERS -----------//
     274             : 
     275             :   /**
     276             :    * The libMesh vectors storing the finite element coefficients
     277             :    * of the RB basis functions.
     278             :    */
     279             :   std::vector<std::unique_ptr<NumericVector<Number>>> basis_functions;
     280             : 
     281             :   /**
     282             :    * The list of parameters selected by the Greedy algorithm in generating
     283             :    * the Reduced Basis associated with this RBEvaluation object.
     284             :    */
     285             :   std::vector<RBParameters> greedy_param_list;
     286             : 
     287             :   /**
     288             :    * The inner product matrix. This should be close to the identity,
     289             :    * we need to calculate this rather than assume diagonality in order
     290             :    * to accurately perform projections since orthogonality degrades
     291             :    * with increasing N.
     292             :    */
     293             :   DenseMatrix<Number> RB_inner_product_matrix;
     294             : 
     295             :   /**
     296             :    * Dense matrices for the RB computations.
     297             :    */
     298             :   std::vector<DenseMatrix<Number>> RB_Aq_vector;
     299             : 
     300             :   /**
     301             :    * Dense vector for the RHS.
     302             :    */
     303             :   std::vector<DenseVector<Number>> RB_Fq_vector;
     304             : 
     305             :   /**
     306             :    * The RB solution vector.
     307             :    */
     308             :   DenseVector<Number> RB_solution;
     309             : 
     310             :   /**
     311             :    * The vectors storing the RB output vectors.
     312             :    */
     313             :   std::vector<std::vector<DenseVector<Number>>> RB_output_vectors;
     314             : 
     315             :   /**
     316             :    * The vectors storing the RB output values and
     317             :    * corresponding error bounds.
     318             :    */
     319             :   std::vector<Number > RB_outputs;
     320             :   std::vector<Real > RB_output_error_bounds;
     321             : 
     322             :   /**
     323             :    * Vectors storing the residual representor inner products
     324             :    * to be used in computing the residuals online.
     325             :    * These values are independent of a basis, hence they can
     326             :    * be copied over directly from an RBSystem.
     327             :    */
     328             :   std::vector<Number> Fq_representor_innerprods;
     329             : 
     330             :   /**
     331             :    * Vectors storing the residual representor inner products
     332             :    * to be used in computing the residuals online.
     333             :    * We store the Aq-dependent representor inner products because they depend
     334             :    * on a reduced basis space. The basis independent representors
     335             :    * are stored in RBSystem.
     336             :    */
     337             :   std::vector<std::vector<std::vector<Number>>> Fq_Aq_representor_innerprods;
     338             :   std::vector<std::vector<std::vector<Number>>> Aq_Aq_representor_innerprods;
     339             : 
     340             :   /**
     341             :    * The vector storing the dual norm inner product terms
     342             :    * for each output.
     343             :    * These values are independent of a basis, hence they can
     344             :    * be copied over directly from an RBSystem.
     345             :    */
     346             :   std::vector<std::vector<Number >> output_dual_innerprods;
     347             : 
     348             :   /**
     349             :    * Vector storing the residual representors associated with the
     350             :    * left-hand side.
     351             :    * These are basis dependent and hence stored here, whereas
     352             :    * the Fq_representors are stored in RBSystem.
     353             :    */
     354             :   std::vector<std::vector<std::unique_ptr<NumericVector<Number>>>> Aq_representor;
     355             : 
     356             :   /**
     357             :    * Boolean to indicate whether we evaluate a posteriori error bounds
     358             :    * when rb_solve is called.
     359             :    */
     360             :   bool evaluate_RB_error_bound;
     361             : 
     362             :   /**
     363             :    * Boolean flag to indicate whether we compute the RB_inner_product_matrix.
     364             :    */
     365             :   bool compute_RB_inner_product;
     366             : 
     367             : protected:
     368             : 
     369             :   /**
     370             :    * Helper function that checks if \p file_name exists.
     371             :    */
     372             :   static void assert_file_exists(const std::string & file_name);
     373             : 
     374             : private:
     375             : 
     376             :   /**
     377             :    * A pointer to to the object that stores the theta expansion.
     378             :    * This is not a std::unique_ptr since we may want to share it.
     379             :    *
     380             :    * \note A \p shared_ptr would be a good option here.
     381             :    */
     382             :   RBThetaExpansion * rb_theta_expansion;
     383             : 
     384             :   /**
     385             :    * For interfaces like rb_solve() and compute_residual_dual_norm() that optinally
     386             :    * take a vector of "pre-evaluated" theta values, this function checks to make sure
     387             :    * that, when provided, it is the right size.
     388             :    */
     389             :   void check_evaluated_thetas_size(const std::vector<Number> * evaluated_thetas) const;
     390             : };
     391             : 
     392             : }
     393             : 
     394             : #endif // LIBMESH_RB_EVALUATION_H

Generated by: LCOV version 1.14