LCOV - code coverage report
Current view: top level - src/systems - MooseEigenSystem.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 102 125 81.6 %
Date: 2025-07-17 01:28:37 Functions: 13 14 92.9 %
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 "MooseEigenSystem.h"
      11             : 
      12             : #include "MaterialData.h"
      13             : #include "Factory.h"
      14             : #include "EigenKernel.h"
      15             : #include "FEProblemBase.h"
      16             : 
      17         106 : MooseEigenSystem::MooseEigenSystem(FEProblemBase & fe_problem, const std::string & name)
      18             :   : NonlinearSystem(fe_problem, name),
      19         106 :     _all_eigen_vars(false),
      20         106 :     _active_on_old(false),
      21         106 :     _eigen_kernel_counter(0)
      22             : {
      23         106 : }
      24             : 
      25         106 : MooseEigenSystem::~MooseEigenSystem() {}
      26             : 
      27             : void
      28         320 : MooseEigenSystem::addKernel(const std::string & kernel_name,
      29             :                             const std::string & name,
      30             :                             InputParameters & parameters)
      31             : {
      32         672 :   for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
      33             :   {
      34             :     // In the case of EigenKernels, we might need to add two to the system
      35         352 :     if (parameters.have_parameter<bool>("eigen"))
      36             :     {
      37             :       {
      38             :         // EigenKernel
      39         156 :         parameters.set<bool>("implicit") = true;
      40             :         std::shared_ptr<KernelBase> ekernel =
      41         156 :             _factory.create<KernelBase>(kernel_name, name, parameters, tid);
      42         156 :         if (parameters.get<bool>("eigen"))
      43         156 :           markEigenVariable(parameters.get<NonlinearVariableName>("variable"));
      44         156 :         _kernels.addObject(ekernel, tid);
      45         156 :       }
      46         156 :       if (parameters.get<bool>("eigen"))
      47             :       {
      48             :         // EigenKernel_old
      49         156 :         parameters.set<bool>("implicit") = false;
      50         156 :         std::string old_name(name + "_old");
      51             : 
      52             :         std::shared_ptr<KernelBase> ekernel =
      53         156 :             _factory.create<KernelBase>(kernel_name, old_name, parameters, tid);
      54         156 :         _eigen_var_names.insert(parameters.get<NonlinearVariableName>("variable"));
      55         156 :         _kernels.addObject(ekernel, tid);
      56         156 :         ++_eigen_kernel_counter;
      57         156 :       }
      58             :     }
      59             :     else // Standard nonlinear system kernel
      60             :     {
      61             :       // Create the kernel object via the factory
      62             :       std::shared_ptr<KernelBase> kernel =
      63         196 :           _factory.create<KernelBase>(kernel_name, name, parameters, tid);
      64         196 :       _kernels.addObject(kernel, tid);
      65         196 :     }
      66             :   }
      67             : 
      68         320 :   if (parameters.get<std::vector<AuxVariableName>>("save_in").size() > 0)
      69           0 :     _has_save_in = true;
      70         320 :   if (parameters.get<std::vector<AuxVariableName>>("diag_save_in").size() > 0)
      71          12 :     _has_diag_save_in = true;
      72         320 : }
      73             : 
      74             : void
      75         166 : MooseEigenSystem::markEigenVariable(const VariableName & var_name)
      76             : {
      77         166 :   _eigen_var_names.insert(var_name);
      78         166 : }
      79             : 
      80             : void
      81         109 : MooseEigenSystem::scaleSystemSolution(SYSTEMTAG tag, Real scaling_factor)
      82             : {
      83         109 :   if (tag == ALL)
      84             :   {
      85           0 :     solution().scale(scaling_factor);
      86             :   }
      87         109 :   else if (tag == EIGEN)
      88             :   {
      89         109 :     if (_all_eigen_vars)
      90             :     {
      91          99 :       solution().scale(scaling_factor);
      92             :     }
      93             :     else
      94             :     {
      95         577 :       for (const auto & dof : _eigen_var_indices)
      96         567 :         solution().set(dof, solution()(dof) * scaling_factor);
      97             :     }
      98             :   }
      99         109 :   solution().close();
     100         109 :   update();
     101         109 : }
     102             : 
     103             : void
     104        1892 : MooseEigenSystem::combineSystemSolution(SYSTEMTAG tag, const std::vector<Real> & coefficients)
     105             : {
     106             :   mooseAssert(coefficients.size() > 0 && coefficients.size() <= 3, "Size error on coefficients");
     107        1892 :   if (tag == ALL)
     108             :   {
     109           0 :     solution().scale(coefficients[0]);
     110           0 :     if (coefficients.size() > 1)
     111           0 :       solution().add(coefficients[1], solutionOld());
     112           0 :     if (coefficients.size() > 2)
     113           0 :       solution().add(coefficients[2], solutionOlder());
     114             :   }
     115        1892 :   else if (tag == EIGEN)
     116             :   {
     117        1892 :     if (_all_eigen_vars)
     118             :     {
     119        1892 :       solution().scale(coefficients[0]);
     120        1892 :       if (coefficients.size() > 1)
     121        1892 :         solution().add(coefficients[1], solutionOld());
     122        1892 :       if (coefficients.size() > 2)
     123        1678 :         solution().add(coefficients[2], solutionOlder());
     124             :     }
     125             :     else
     126             :     {
     127           0 :       if (coefficients.size() > 2)
     128             :       {
     129           0 :         for (const auto & dof : _eigen_var_indices)
     130             :         {
     131           0 :           Real t = solution()(dof) * coefficients[0];
     132           0 :           t += solutionOld()(dof) * coefficients[1];
     133           0 :           t += solutionOlder()(dof) * coefficients[2];
     134           0 :           solution().set(dof, t);
     135             :         }
     136             :       }
     137           0 :       else if (coefficients.size() > 1)
     138             :       {
     139           0 :         for (const auto & dof : _eigen_var_indices)
     140             :         {
     141           0 :           Real t = solution()(dof) * coefficients[0];
     142           0 :           t += solutionOld()(dof) * coefficients[1];
     143           0 :           solution().set(dof, t);
     144             :         }
     145             :       }
     146             :       else
     147             :       {
     148           0 :         for (const auto & dof : _eigen_var_indices)
     149             :         {
     150           0 :           Real t = solution()(dof) * coefficients[0];
     151           0 :           solution().set(dof, t);
     152             :         }
     153             :       }
     154             :     }
     155             :   }
     156        1892 :   solution().close();
     157        1892 :   update();
     158        1892 : }
     159             : 
     160             : void
     161          95 : MooseEigenSystem::initSystemSolution(SYSTEMTAG tag, Real v)
     162             : {
     163          95 :   if (tag == ALL)
     164             :   {
     165           0 :     solution() = v;
     166             :   }
     167          95 :   else if (tag == EIGEN)
     168             :   {
     169          95 :     if (_all_eigen_vars)
     170             :     {
     171          85 :       solution() = v;
     172             :     }
     173             :     else
     174             :     {
     175         577 :       for (const auto & dof : _eigen_var_indices)
     176         567 :         solution().set(dof, v);
     177             :     }
     178             :   }
     179          95 :   solution().close();
     180          95 :   update();
     181          95 : }
     182             : 
     183             : void
     184         105 : MooseEigenSystem::initSystemSolutionOld(SYSTEMTAG tag, Real v)
     185             : {
     186         105 :   if (tag == ALL)
     187             :   {
     188           0 :     solutionOld() = v;
     189             :   }
     190         105 :   else if (tag == EIGEN)
     191             :   {
     192         105 :     if (_all_eigen_vars)
     193             :     {
     194          95 :       solutionOld() = v;
     195             :     }
     196             :     else
     197             :     {
     198         577 :       for (const auto & dof : _eigen_var_indices)
     199         567 :         solutionOld().set(dof, v);
     200             :     }
     201             :   }
     202         105 :   solutionOld().close();
     203         105 :   update();
     204         105 : }
     205             : 
     206             : void
     207         105 : MooseEigenSystem::eigenKernelOnOld()
     208             : {
     209         105 :   _active_on_old = true;
     210         105 :   _fe_problem.updateActiveObjects(); // update warehouse active objects
     211         105 : }
     212             : 
     213             : void
     214          95 : MooseEigenSystem::eigenKernelOnCurrent()
     215             : {
     216          95 :   _active_on_old = false;
     217          95 :   _fe_problem.updateActiveObjects(); // update warehouse active objects
     218          95 : }
     219             : 
     220             : bool
     221        2968 : MooseEigenSystem::activeOnOld()
     222             : {
     223        2968 :   return _active_on_old;
     224             : }
     225             : 
     226             : void
     227         105 : MooseEigenSystem::buildSystemDoFIndices(SYSTEMTAG tag)
     228             : {
     229         105 :   if (tag == ALL)
     230             :   {
     231             :   }
     232         105 :   else if (tag == EIGEN)
     233             :   {
     234             :     // build DoF indices for the eigen system
     235         105 :     _eigen_var_indices.clear();
     236         105 :     _all_eigen_vars = getEigenVariableNames().size() == getVariableNames().size();
     237         105 :     if (!_all_eigen_vars)
     238             :     {
     239          10 :       for (std::set<VariableName>::const_iterator it = getEigenVariableNames().begin();
     240          20 :            it != getEigenVariableNames().end();
     241          10 :            it++)
     242             :       {
     243          10 :         unsigned int i = sys().variable_number(*it);
     244          10 :         std::set<dof_id_type> var_indices;
     245          10 :         sys().local_dof_indices(i, var_indices);
     246          10 :         _eigen_var_indices.insert(var_indices.begin(), var_indices.end());
     247          10 :       }
     248             :     }
     249             :   }
     250         105 : }
     251             : 
     252             : bool
     253         105 : MooseEigenSystem::containsEigenKernel() const
     254             : {
     255         105 :   return _eigen_kernel_counter > 0;
     256             : }

Generated by: LCOV version 1.14