LCOV - code coverage report
Current view: top level - src/reporters - SingularTripletReporter.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #31405 (292dce) with base fef103 Lines: 41 45 91.1 %
Date: 2025-09-04 07:57:52 Functions: 4 4 100.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             : #include "SingularTripletReporter.h"
      11             : #include "JsonIO.h"
      12             : 
      13             : registerMooseObject("StochasticToolsApp", SingularTripletReporter);
      14             : 
      15             : InputParameters
      16         110 : SingularTripletReporter::validParams()
      17             : {
      18         110 :   InputParameters params = GeneralReporter::validParams();
      19         110 :   params += MappingInterface::validParams();
      20             : 
      21         110 :   params.addClassDescription("Tool for accessing and outputting the singular triplets of a "
      22             :                              "singular value decomposition in PODMapping.");
      23             : 
      24         220 :   params.addRequiredParam<UserObjectName>(
      25             :       "pod_mapping", "The PODMapping object whose singular triplets should be printed.");
      26             : 
      27         220 :   params.addRequiredParam<std::vector<VariableName>>(
      28             :       "variables", "The names of the variables whose SVD should be printed.");
      29             : 
      30         110 :   return params;
      31           0 : }
      32             : 
      33          55 : SingularTripletReporter::SingularTripletReporter(const InputParameters & parameters)
      34             :   : GeneralReporter(parameters),
      35             :     MappingInterface(this),
      36          55 :     _variable_names(getParam<std::vector<VariableName>>("variables")),
      37          55 :     _left_singular_vectors(
      38         220 :         declareValueByName<std::map<VariableName, std::vector<DenseVector<Real>>>>(
      39             :             "left_singular_vectors", REPORTER_MODE_ROOT)),
      40          55 :     _right_singular_vectors(
      41         165 :         declareValueByName<std::map<VariableName, std::vector<DenseVector<Real>>>>(
      42             :             "right_singular_vectors", REPORTER_MODE_ROOT)),
      43          55 :     _singular_values(declareValueByName<std::map<VariableName, std::vector<Real>>>(
      44          55 :         "singular_values", REPORTER_MODE_ROOT))
      45             : 
      46             : {
      47         110 :   for (const auto & vname : _variable_names)
      48             :   {
      49          55 :     _left_singular_vectors.emplace(vname, std::vector<DenseVector<Real>>());
      50          55 :     _right_singular_vectors.emplace(vname, std::vector<DenseVector<Real>>());
      51          55 :     _singular_values.emplace(vname, std::vector<Real>());
      52             :   }
      53          55 : }
      54             : 
      55             : void
      56          55 : SingularTripletReporter::initialSetup()
      57             : {
      58          55 :   _pod_mapping = dynamic_cast<PODMapping *>(&getMapping("pod_mapping"));
      59             : 
      60          55 :   if (!_pod_mapping)
      61           0 :     paramError("pod_mapping", "The given mapping is not a PODMapping!");
      62             : 
      63          55 :   const auto & pod_mapping_variables = _pod_mapping->getVariableNames();
      64         110 :   for (const auto & vname : _variable_names)
      65          55 :     if (std::find(pod_mapping_variables.begin(), pod_mapping_variables.end(), vname) ==
      66             :         pod_mapping_variables.end())
      67           0 :       paramError("variables",
      68           0 :                  "The SVD of the requested variable " + vname +
      69             :                      " is not in the PODMapping object!");
      70             : 
      71         110 :   for (const auto & vname : _variable_names)
      72             :   {
      73          55 :     _left_singular_vectors[vname].clear();
      74          55 :     _right_singular_vectors[vname].clear();
      75          55 :     _singular_values[vname].clear();
      76             :   }
      77          55 : }
      78             : 
      79             : void
      80          55 : SingularTripletReporter::execute()
      81             : {
      82         110 :   for (const auto & vname : _variable_names)
      83             :   {
      84          55 :     _pod_mapping->buildMapping(vname);
      85          55 :     _singular_values[vname] = _pod_mapping->singularValues(vname);
      86             : 
      87          55 :     const auto & left_modes = _pod_mapping->leftBasis(vname);
      88          55 :     const auto & right_modes = _pod_mapping->rightBasis(vname);
      89             : 
      90         165 :     for (auto mode_i : index_range(left_modes))
      91             :     {
      92         110 :       _left_singular_vectors[vname].push_back(left_modes[mode_i]);
      93         110 :       _right_singular_vectors[vname].push_back(right_modes[mode_i]);
      94             :     }
      95             :   }
      96          55 : }

Generated by: LCOV version 1.14