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 "ParallelUniqueId.h" 13 : #include "InputParameters.h" 14 : #include "FEProblemBase.h" 15 : 16 : // Forward declarations 17 : class Sampler; 18 : /** 19 : * Interface for objects that need to use samplers. 20 : * 21 : * This practically adds two methods for getting Sampler objects: 22 : * 23 : * 1. Call `getSampler` or `getSamplerByName` without a template parameter and you will get 24 : * a `Sampler` base object (see SamplerInterface.C for the template specialization). 25 : * 2. Call `getSampler<MySampler>` or `getSamplerByName<MySampler>` to perform a cast to the 26 : * desired type, as done for UserObjects. 27 : */ 28 : class SamplerInterface 29 : { 30 : public: 31 : static InputParameters validParams(); 32 : 33 : /** 34 : * @param params The parameters used by the object being instantiated. This 35 : * class needs them so it can get the sampler named in the input file, 36 : * but the object calling getSampler only needs to use the name on the 37 : * left hand side of the statement "sampler = sampler_name" 38 : */ 39 : SamplerInterface(const MooseObject * moose_object); 40 : 41 : #ifdef MOOSE_KOKKOS_ENABLED 42 : /** 43 : * Special constructor used for Kokkos functor copy during parallel dispatch 44 : */ 45 : SamplerInterface(const SamplerInterface & object, const Moose::Kokkos::FunctorCopy & key); 46 : #endif 47 : 48 : /** 49 : * Get a sampler with a given name 50 : * @param name The name of the parameter key of the sampler to retrieve 51 : * @return The sampler with name associated with the parameter 'name' 52 : */ 53 : template <typename T = Sampler> 54 : T & getSampler(const std::string & name); 55 : 56 : /** 57 : * Get a sampler with a given name 58 : * @param name The name of the sampler to retrieve 59 : * @return The sampler with name 'name' 60 : */ 61 : template <typename T = Sampler> 62 : T & getSamplerByName(const SamplerName & name); 63 : 64 : private: 65 : /// Parameters of the object with this interface 66 : const InputParameters & _si_params; 67 : 68 : /// Reference to FEProblemBase instance 69 : FEProblemBase & _si_feproblem; 70 : 71 : /// Thread ID 72 : THREAD_ID _si_tid; 73 : }; 74 : 75 : template <typename T> 76 : T & 77 0 : SamplerInterface::getSampler(const std::string & name) 78 : { 79 0 : return getSamplerByName<T>(_si_params.get<SamplerName>(name)); 80 : } 81 : 82 : template <typename T> 83 : T & 84 0 : SamplerInterface::getSamplerByName(const SamplerName & name) 85 : { 86 0 : Sampler * base_ptr = &_si_feproblem.getSampler(name, _si_tid); 87 0 : T * obj_ptr = dynamic_cast<T *>(base_ptr); 88 0 : if (!obj_ptr) 89 0 : mooseError("Failed to find a Sampler object with the name '", name, "' for the desired type."); 90 0 : return *obj_ptr; 91 : }