LCOV - code coverage report
Current view: top level - src/ics - RandomICBase.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 37 46 80.4 %
Date: 2025-07-17 01:28:37 Functions: 5 5 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 "RandomICBase.h"
      11             : #include "MooseRandom.h"
      12             : 
      13             : #include "libmesh/point.h"
      14             : 
      15             : namespace
      16             : {
      17             : /**
      18             :  * Method for retrieving or generating and caching a value in a map.
      19             :  */
      20             : inline Real
      21      306188 : valueHelper(dof_id_type id, MooseRandom & generator, std::map<dof_id_type, Real> & map)
      22             : {
      23      306188 :   auto it_pair = map.lower_bound(id);
      24             : 
      25             :   // Do we need to generate a new number?
      26      306188 :   if (it_pair == map.end() || it_pair->first != id)
      27       82425 :     it_pair = map.emplace_hint(it_pair, id, generator.rand(id));
      28             : 
      29      306188 :   return it_pair->second;
      30             : }
      31             : }
      32             : 
      33             : InputParameters
      34       15207 : RandomICBase::validParams()
      35             : {
      36       15207 :   InputParameters params = InitialCondition::validParams();
      37       15207 :   params.addParam<unsigned int>("seed", 0, "Seed value for the random number generator");
      38       45621 :   params.addParam<bool>(
      39             :       "legacy_generator",
      40       30414 :       false,
      41             :       "Determines whether or not the legacy generator (deprecated) should be used.");
      42             : 
      43       15207 :   params.addClassDescription("Base class for generating a random field for a variable.");
      44       15207 :   return params;
      45           0 : }
      46             : 
      47         496 : RandomICBase::RandomICBase(const InputParameters & parameters)
      48             :   : InitialCondition(parameters),
      49         992 :     _is_nodal(_var.isNodal()),
      50         496 :     _use_legacy(getParam<bool>("legacy_generator")),
      51         496 :     _elem_random_generator(nullptr),
      52         992 :     _node_random_generator(nullptr)
      53             : {
      54         496 :   unsigned int processor_seed = getParam<unsigned int>("seed");
      55         496 :   MooseRandom::seed(processor_seed);
      56             : 
      57         496 :   if (_use_legacy)
      58             :   {
      59           0 :     auto proc_id = processor_id();
      60           0 :     if (proc_id > 0)
      61             :     {
      62           0 :       for (processor_id_type i = 0; i < proc_id; ++i)
      63           0 :         processor_seed = MooseRandom::randl();
      64           0 :       MooseRandom::seed(processor_seed);
      65             :     }
      66             :   }
      67             :   else
      68             :   {
      69             :     _elem_random_data =
      70         496 :         std::make_unique<RandomData>(_fe_problem, false, EXEC_INITIAL, MooseRandom::randl());
      71             :     _node_random_data =
      72         496 :         std::make_unique<RandomData>(_fe_problem, true, EXEC_INITIAL, MooseRandom::randl());
      73             : 
      74         496 :     _elem_random_generator = &_elem_random_data->getGenerator();
      75         496 :     _node_random_generator = &_node_random_data->getGenerator();
      76             :   }
      77         496 : }
      78             : 
      79             : void
      80         485 : RandomICBase::initialSetup()
      81             : {
      82         485 :   if (!_use_legacy)
      83             :   {
      84         485 :     _elem_random_data->updateSeeds(EXEC_INITIAL);
      85         485 :     _node_random_data->updateSeeds(EXEC_INITIAL);
      86             :   }
      87         485 : }
      88             : 
      89             : Real
      90      306188 : RandomICBase::generateRandom()
      91             : {
      92             :   Real rand_num;
      93             : 
      94      306188 :   if (_use_legacy)
      95             :   {
      96           0 :     mooseDeprecated("legacy_generator is deprecated. Please set \"legacy_generator = false\". This "
      97             :                     "capability will be removed after 11/01/2018");
      98             : 
      99             :     // Random number between 0 and 1
     100           0 :     rand_num = MooseRandom::rand();
     101             :   }
     102             :   else
     103             :   {
     104      306188 :     if (_current_node)
     105      300296 :       rand_num = valueHelper(_current_node->id(), *_node_random_generator, _node_numbers);
     106        5892 :     else if (_current_elem)
     107        5892 :       rand_num = valueHelper(_current_elem->id(), *_elem_random_generator, _elem_numbers);
     108             :     else
     109           0 :       mooseError("We can't generate parallel consistent random numbers for this kind of variable "
     110             :                  "yet. Please contact the MOOSE team for assistance");
     111             :   }
     112             : 
     113      306188 :   return rand_num;
     114             : }

Generated by: LCOV version 1.14