https://mooseframework.inl.gov
Loading...
Searching...
No Matches
ControllableParameter.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// MOOSE includes
13#include "InputParameters.h"
14#include "MooseObjectName.h"
16#include "ControllableItem.h"
17
27{
28public:
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
81private:
83 std::vector<ControllableItem *> _items;
84};
85
86template <typename T>
87void
88ControllableParameter::set(const T & value, bool type_check /*=true*/)
89{
90 for (ControllableItem * item : _items)
91 item->set<T>(value, type_check);
92}
93
94template <typename T>
95std::vector<T>
96ControllableParameter::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
127template <typename T>
128bool
130{
131 bool type = std::all_of(
132 _items.begin(), _items.end(), [](ControllableItem * item) { return item->check<T>(); });
133 return type && !empty();
134}
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition MooseError.h:345
An intermediate object for building a "controllable item", where an "item" can refer to multiple inpu...
The ControllableParameter class is simply a set of ControllableItem objects.
virtual ~ControllableParameter()=default
void add(ControllableItem *item)
Adds the supplied item with the other items within this object.
ControllableParameter & operator=(const ControllableParameter &)=delete
friend std::ostream & operator<<(std::ostream &stream, const ControllableParameter &obj)
Allows this to be used with std:: cout.
void set(const T &value, bool type_check=true)
Set the value(s) of the controlled parameters stored in this class.
ControllableParameter()=default
ControllableParameter & operator=(ControllableParameter &&)=delete
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.
std::string dump() const
Return a string that lists the parameters stored by this object.
ControllableParameter(const ControllableParameter &)=delete
bool empty()
Return true if the container is empty.
std::vector< ControllableItem * > _items
Storage for the ControllableItems, these are stored as pointers to avoid copies.
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.
ControllableParameter(ControllableParameter &&)=default
Class for containing MooseEnum item information.