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

Generated by: LCOV version 1.14