LCOV - code coverage report
Current view: top level - include/surrogates - PolynomialChaos.h (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: f45d79 Lines: 2 2 100.0 %
Date: 2025-07-25 05:00:46 Functions: 0 0 -
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : #pragma once
      11             : 
      12             : #include "SurrogateModel.h"
      13             : #include "PolynomialQuadrature.h"
      14             : #include "QuadratureSampler.h"
      15             : 
      16             : #include "Distribution.h"
      17             : #include "nlohmann/json.h"
      18             : 
      19             : class PolynomialChaos : public SurrogateModel
      20             : {
      21             : public:
      22             :   static InputParameters validParams();
      23             :   PolynomialChaos(const InputParameters & parameters);
      24             :   using SurrogateModel::evaluate;
      25             :   virtual Real evaluate(const std::vector<Real> & x) const override;
      26             : 
      27             :   /// Access number of dimensions/parameters
      28         358 :   std::size_t getNumberOfParameters() const { return _poly.size(); }
      29             : 
      30             :   /// Number of terms in expansion
      31          80 :   std::size_t getNumberofCoefficients() const { return _tuple.size(); }
      32             : 
      33             :   /// Access polynomial orders from tuple
      34             :   ////@{
      35             :   const std::vector<std::vector<unsigned int>> & getPolynomialOrders() const;
      36             :   unsigned int getPolynomialOrder(const unsigned int dim, const unsigned int i) const;
      37             :   ///@}
      38             : 
      39             :   /// Access computed expansion coefficients
      40             :   const std::vector<Real> & getCoefficients() const;
      41             : 
      42             :   /// Evaluate mean: \mu = E[u]
      43             :   virtual Real computeMean() const;
      44             : 
      45             :   /// Evaluate standard deviation: \sigma = sqrt(E[(u-\mu)^2])
      46             :   virtual Real computeStandardDeviation() const;
      47             : 
      48             :   /// Compute expectation of a certain power of the QoI: E[(u-\mu)^n]
      49             :   Real powerExpectation(const unsigned int n) const;
      50             : 
      51             :   /// Evaluates partial derivative of expansion: du(x)/dx_dim
      52             :   Real computeDerivative(const unsigned int dim, const std::vector<Real> & x) const;
      53             :   /**
      54             :    * Evaluates sum of partial derivative of expansion. Example:
      55             :    * computeGradient({0, 2, 3}, x) = du(x)/dx_0dx_2dx_3
      56             :    */
      57             :   Real computePartialDerivative(const std::vector<unsigned int> & dim,
      58             :                                 const std::vector<Real> & x) const;
      59             : 
      60             :   /// Computes Sobol sensitivities S_{i_1,i_2,...,i_s}, where ind = i_1,i_2,...,i_s
      61             :   Real computeSobolIndex(const std::set<unsigned int> & ind) const;
      62             :   Real computeSobolTotal(const unsigned int dim) const;
      63             : 
      64             :   void store(nlohmann::json & json) const;
      65             : 
      66             : private:
      67             :   /// Variables calculation and for looping over the computed coefficients in parallel
      68             :   ///
      69             :   /// The various utility methods in this class require the coefficients be partitioned in parallel,
      70             :   /// but the data being partitioned is loaded from the trainer so it might not be available. Thus,
      71             :   /// the partitioning is done on demand, if needed.
      72             :   ///
      73             :   /// The methods are marked const because they do not modify the loaded data, to keep this interface
      74             :   /// the partitioning uses mutable variables.
      75             :   ///@{
      76             :   mutable dof_id_type _n_local_coeff = std::numeric_limits<dof_id_type>::max();
      77             :   mutable dof_id_type _local_coeff_begin = 0;
      78             :   mutable dof_id_type _local_coeff_end = 0;
      79             :   void linearPartitionCoefficients() const;
      80             :   ///@}
      81             : 
      82             :   // The following items are loaded from a SurrogateTrainer using getModelData
      83             : 
      84             :   /// Maximum polynomial order. The sum of 1D polynomial orders does not go above this value.
      85             :   const unsigned int & _order;
      86             : 
      87             :   /// Total number of parameters/dimensions
      88             :   const unsigned int & _ndim;
      89             : 
      90             :   /// Total number of coefficient (defined by size of _tuple)
      91             :   const std::size_t & _ncoeff;
      92             : 
      93             :   /// A _ndim-by-_ncoeff matrix containing the appropriate one-dimensional polynomial order
      94             :   const std::vector<std::vector<unsigned int>> & _tuple;
      95             : 
      96             :   /// These are the coefficients we are after in the PC expansion
      97             :   const std::vector<Real> & _coeff;
      98             : 
      99             :   /// The distributions used for sampling
     100             :   const std::vector<std::unique_ptr<const PolynomialQuadrature::Polynomial>> & _poly;
     101             : 
     102             :   friend void to_json(nlohmann::json & json, const PolynomialChaos * const & pc);
     103             : };

Generated by: LCOV version 1.14