www.mooseframework.org
ControllableParameter.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 // MOOSE includes
13 #include "InputParameters.h"
14 #include "MooseObjectName.h"
16 #include "ControllableItem.h"
17 
27 {
28 public:
29  ControllableParameter() = default;
30  virtual ~ControllableParameter() = default;
31 
36 
40  bool empty() { return _items.size() == 0; }
41 
47  std::string dump() const;
48 
53  template <typename T>
54  void set(const T & value, bool type_check = true);
55 
59  template <typename T>
60  std::vector<T> get(bool type_check = true, bool warn_when_values_difffer = false) const;
61 
65  template <typename T>
66  bool check();
67 
71  void checkExecuteOnType(const ExecFlagType & current) const;
72 
76  void add(ControllableItem * item);
77 
79  friend std::ostream & operator<<(std::ostream & stream, const ControllableParameter & obj);
80 
81 private:
83  std::vector<ControllableItem *> _items;
84 };
85 
86 template <typename T>
87 void
88 ControllableParameter::set(const T & value, bool type_check /*=true*/)
89 {
90  for (ControllableItem * item : _items)
91  item->set<T>(value, type_check);
92 }
93 
94 template <typename T>
95 std::vector<T>
96 ControllableParameter::get(bool type_check /*=true*/, bool warn_when_values_differ) const
97 {
98  std::vector<T> output;
99  for (const ControllableItem * const item : _items)
100  {
101  std::vector<T> local = item->get<T>(type_check);
102  output.insert(output.end(), local.begin(), local.end());
103  }
104 
105  // Produce a warning, if the flag is true, when multiple parameters have different values
106  if (warn_when_values_differ && _items.size() > 1)
107  {
108  // The first parameter to test against
109  const T value0 = output[0];
110 
111  // Loop over all other parameter values
112  for (T value : output)
113  {
114  if (value0 != value)
115  {
116  std::ostringstream oss;
117  oss << "The following controlled parameters are being retrieved, but the values differ:\n";
118  oss << dump();
119  mooseWarning(oss.str());
120  }
121  }
122  }
123 
124  return output;
125 }
126 
127 template <typename T>
128 bool
130 {
131  bool type = std::all_of(
132  _items.begin(), _items.end(), [](ControllableItem * item) { return item->check<T>(); });
133  return type && !empty();
134 }
bool empty()
Return true if the container is empty.
void set(const T &value, bool type_check=true)
Set the value(s) of the controlled parameters stored in this class.
virtual ~ControllableParameter()=default
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:333
The ControllableParameter class is simply a set of ControllableItem objects.
ControllableParameter & operator=(const ControllableParameter &)=delete
bool check()
Check size() and the type of the stored items, i.e., there must be items with the given type...
void checkExecuteOnType(const ExecFlagType &current) const
Check the execute flags.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
void add(ControllableItem *item)
Adds the supplied item with the other items within this object.
std::vector< ControllableItem * > _items
Storage for the ControllableItems, these are stored as pointers to avoid copies.
friend std::ostream & operator<<(std::ostream &stream, const ControllableParameter &obj)
Allows this to be used with std:: cout.
std::vector< T > get(bool type_check=true, bool warn_when_values_difffer=false) const
Return a copy of the values of the given type.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
An intermediate object for building a "controllable item", where an "item" can refer to multiple inpu...
ControllableParameter()=default
std::string dump() const
Return a string that lists the parameters stored by this object.