https://mooseframework.inl.gov
ControllableItem.h
Go to the documentation of this file.
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 "libmesh/parameters.h"
14 #include "MooseError.h"
15 #include "ControlOutput.h"
16 
32 {
33 public:
36  const std::set<ExecFlagType> & flags = {});
37  virtual ~ControllableItem() = default;
38 
39  ControllableItem(const ControllableItem &) = default;
40  ControllableItem(ControllableItem &&) = default;
41 
42  ControllableItem & operator=(const ControllableItem &) = delete;
44 
49  void connect(ControllableItem * item, bool type_check = true);
50 
57  template <typename T>
58  void set(const T & value, bool type_check = true);
59 
63  template <typename T>
64  std::vector<T> get(bool type_check = true) const;
65 
69  template <typename T>
70  bool check() const;
71 
73 
76  bool operator==(const ControllableItem & rhs) const { return name() == rhs.name(); }
77  bool operator!=(const ControllableItem & rhs) const { return name() != rhs.name(); }
78  bool operator<(const ControllableItem & rhs) const { return name() < rhs.name(); }
80 
84  virtual const MooseObjectParameterName & name() const;
85 
89  std::string type() const;
90 
94  virtual std::string dump(unsigned int indent = 0) const;
95 
97 
101  void resetChanged() { _changed = false; }
102  bool isChanged() { return _changed; }
104 
108  const std::set<ExecFlagType> & getExecuteOnFlags() const { return _execute_flags; }
109 
111  friend std::ostream & operator<<(std::ostream & stream, const ControllableItem & obj);
112 
113 protected:
118 
120  std::vector<std::pair<MooseObjectParameterName, libMesh::Parameters::Value *>> _pairs;
121 
123  bool _changed = false;
124 
126  std::set<ExecFlagType> _execute_flags;
127 };
128 
129 template <typename T>
130 void
131 ControllableItem::set(const T & value, bool type_check /*=true*/)
132 {
133  for (auto & pair : _pairs)
134  {
136  dynamic_cast<libMesh::Parameters::Parameter<T> *>(pair.second);
137  if (type_check && param == nullptr)
138  mooseError("Failed to set the '",
139  pair.first,
140  "' parameter the supplied template argument must be of type '",
141  pair.second->type(),
142  "'.");
143  else if (param != nullptr)
144  {
145  param->set() = value;
146  _changed = true;
147  }
148  }
149 }
150 
151 template <typename T>
152 std::vector<T>
153 ControllableItem::get(bool type_check /*=true*/) const
154 {
155  std::vector<T> output;
156  output.reserve(_pairs.size());
157  for (const auto & pair : _pairs)
158  {
160  dynamic_cast<libMesh::Parameters::Parameter<T> *>(pair.second);
161  if (type_check && param == nullptr)
162  mooseError("Failed to get the '",
163  pair.first,
164  "' parameter the supplied template argument must be of type '",
165  pair.second->type(),
166  "'.");
167  else if (param != nullptr)
168  output.push_back(param->get());
169  }
170  return output;
171 }
172 
173 template <typename T>
174 bool
176 {
177  return std::all_of(_pairs.begin(),
178  _pairs.end(),
179  [](std::pair<MooseObjectParameterName, libMesh::Parameters::Value *> pair)
180  {
182  dynamic_cast<libMesh::Parameters::Parameter<T> *>(pair.second);
183  return param != nullptr;
184  });
185 }
186 
191 {
192 public:
194  virtual const MooseObjectParameterName & name() const override;
195  virtual std::string dump(unsigned int indent = 0) const override;
196 
197 private:
199 };
virtual ~ControllableItem()=default
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:41
std::set< ExecFlagType > _execute_flags
Flags to which the control is restricted (if not set it is unrestricted)
bool operator==(const ControllableItem &rhs) const
Use the master name for comparison operators to allow object to work within a set/map.
friend std::ostream & operator<<(std::ostream &stream, const ControllableItem &obj)
Allows this to be used with std:: cout.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
bool operator!=(const ControllableItem &rhs) const
const std::set< ExecFlagType > & getExecuteOnFlags() const
Return the execute flag restrictions, an empty set is un-restricted.
bool operator<(const ControllableItem &rhs) const
void connect(ControllableItem *item, bool type_check=true)
Connects the supplied item with this item to allow for multiple parameters to be changed by one...
virtual const MooseObjectParameterName & name() const
Return the name of the master parameter.
ControllableAlias(const MooseObjectParameterName &name, ControllableItem *)
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
virtual std::string dump(unsigned int indent=0) const
Returns a string displaying the parameter name and current value.
void resetChanged()
Methods for ControlOutput::outputChangedControls, these don&#39;t have meaning outside of this function...
std::vector< std::pair< MooseObjectParameterName, libMesh::Parameters::Value * > > _pairs
List of names for this item.
bool check() const
Return true if the template argument is valid for ALL items.
std::string type() const
Return the type of the master parameter.
void set(const T &value, bool type_check=true)
Set the value(s) of the controlled parameters stored in this class.
An intermediate object for building a "controllable item", where an "item" can refer to multiple inpu...
ControllableItem()
Constructor for creating an empty item (see ControllableAlias)
A class for storing an input parameter name.
virtual std::string dump(unsigned int indent=0) const override
Returns a string displaying the parameter name and current value.
virtual const MooseObjectParameterName & name() const override
Return the name of the master parameter.
bool _changed
Flag for ControlOutput, allows output objects to keep track of when a parameter is altered...
MooseObjectParameterName _name
ControllableItem & operator=(const ControllableItem &)=delete
Allows for aliases to be defined via InputParameterWarehouse::addControllableParameterAlias.
std::vector< T > get(bool type_check=true) const
Return a copy of all values for this "item".