www.mooseframework.org
ControllableItem.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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:
35  libMesh::Parameters::Value * value,
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  {
135  libMesh::Parameters::Parameter<T> * param =
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  {
159  libMesh::Parameters::Parameter<T> * param =
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  libMesh::Parameters::Parameter<T> * param =
181  dynamic_cast<libMesh::Parameters::Parameter<T> *>(pair.second);
182  return param != nullptr;
183  });
184 }
185 
190 {
191 public:
193  virtual const MooseObjectParameterName & name() const override;
194  virtual std::string dump(unsigned int indent = 0) const override;
195 
196 private:
198 };
199 
ControllableItem::check
bool check() const
Return true if the template argument is valid for ALL items.
Definition: ControllableItem.h:175
ControllableAlias::dump
virtual std::string dump(unsigned int indent=0) const override
Returns a string displaying the parameter name and current value.
Definition: ControllableItem.C:91
ControlOutput.h
ControllableItem::type
std::string type() const
Return the type of the master parameter.
Definition: ControllableItem.C:67
ConsoleUtils::indent
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:30
ControllableItem
An intermediate object for building a "controllable item", where an "item" can refer to multiple inpu...
Definition: ControllableItem.h:31
ControllableItem::get
std::vector< T > get(bool type_check=true) const
Return a copy of all values for this "item".
Definition: ControllableItem.h:153
MooseObjectParameterName.h
ControllableItem::operator=
ControllableItem & operator=(const ControllableItem &)=delete
ControllableItem::~ControllableItem
virtual ~ControllableItem()=default
mooseError
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition: MooseError.h:210
ControllableItem::isChanged
bool isChanged()
Definition: ControllableItem.h:102
ControllableItem::operator!=
bool operator!=(const ControllableItem &rhs) const
Definition: ControllableItem.h:77
ControllableItem::getExecuteOnFlags
const std::set< ExecFlagType > & getExecuteOnFlags() const
Return the execute flag restrictions, an empty set is un-restricted.
Definition: ControllableItem.h:108
ControllableItem::operator==
bool operator==(const ControllableItem &rhs) const
Use the master name for comparison operators to allow object to work within a set/map.
Definition: ControllableItem.h:76
ControllableAlias::ControllableAlias
ControllableAlias(const MooseObjectParameterName &name, ControllableItem *)
Definition: ControllableItem.C:78
MooseObjectParameterName
A class for storing an input parameter name.
Definition: MooseObjectParameterName.h:26
ControllableItem::_changed
bool _changed
Flag for ControlOutput, allows output objects to keep track of when a parameter is altered.
Definition: ControllableItem.h:123
ControllableItem::ControllableItem
ControllableItem()
Constructor for creating an empty item (see ControllableAlias)
Definition: ControllableItem.C:21
ControllableItem::connect
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.
Definition: ControllableItem.C:24
ControllableAlias
Allows for aliases to be defined via InputParameterWarehouse::addControllableParameterAlias.
Definition: ControllableItem.h:189
ControllableAlias::name
virtual const MooseObjectParameterName & name() const override
Return the name of the master parameter.
Definition: ControllableItem.C:85
ControllableItem::operator<<
friend std::ostream & operator<<(std::ostream &stream, const ControllableItem &obj)
Allows this to be used with std:: cout.
Definition: ControllableItem.C:106
MooseError.h
ControllableItem::dump
virtual std::string dump(unsigned int indent=0) const
Returns a string displaying the parameter name and current value.
Definition: ControllableItem.C:44
ControllableItem::resetChanged
void resetChanged()
Methods for ControlOutput::outputChangedControls, these don't have meaning outside of this function.
Definition: ControllableItem.h:101
ControllableItem::_pairs
std::vector< std::pair< MooseObjectParameterName, libMesh::Parameters::Value * > > _pairs
List of names for this item.
Definition: ControllableItem.h:120
ControllableItem::_execute_flags
std::set< ExecFlagType > _execute_flags
Flags to which the control is restricted (if not set it is unrestricted)
Definition: ControllableItem.h:126
ControllableItem::operator<
bool operator<(const ControllableItem &rhs) const
Definition: ControllableItem.h:78
ControllableAlias::_name
MooseObjectParameterName _name
Definition: ControllableItem.h:197
ControllableItem::set
void set(const T &value, bool type_check=true)
Set the value(s) of the controlled parameters stored in this class.
Definition: ControllableItem.h:131
ControllableItem::name
virtual const MooseObjectParameterName & name() const
Return the name of the master parameter.
Definition: ControllableItem.C:73