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 : /** 42 : * Get a sampler with a given name 43 : * @param name The name of the parameter key of the sampler to retrieve 44 : * @return The sampler with name associated with the parameter 'name' 45 : */ 46 : template <typename T = Sampler> 47 : T & getSampler(const std::string & name); 48 : 49 : /** 50 : * Get a sampler with a given name 51 : * @param name The name of the sampler to retrieve 52 : * @return The sampler with name 'name' 53 : */ 54 : template <typename T = Sampler> 55 : T & getSamplerByName(const SamplerName & name); 56 : 57 : private: 58 : /// Parameters of the object with this interface 59 : const InputParameters & _si_params; 60 : 61 : /// Reference to FEProblemBase instance 62 : FEProblemBase & _si_feproblem; 63 : 64 : /// Thread ID 65 : THREAD_ID _si_tid; 66 : }; 67 : 68 : template <typename T> 69 : T & 70 0 : SamplerInterface::getSampler(const std::string & name) 71 : { 72 0 : return getSamplerByName<T>(_si_params.get<SamplerName>(name)); 73 : } 74 : 75 : template <typename T> 76 : T & 77 0 : SamplerInterface::getSamplerByName(const SamplerName & name) 78 : { 79 0 : Sampler * base_ptr = &_si_feproblem.getSampler(name, _si_tid); 80 0 : T * obj_ptr = dynamic_cast<T *>(base_ptr); 81 0 : if (!obj_ptr) 82 0 : mooseError("Failed to find a Sampler object with the name '", name, "' for the desired type."); 83 0 : return *obj_ptr; 84 : }