Skip to content

Commit fb4a94c

Browse files
authored
Merge pull request #3543 from jwpeterson/rb_parameters_update
Updates to RBParameters interface
2 parents c485e08 + f1f79bb commit fb4a94c

File tree

4 files changed

+74
-16
lines changed

4 files changed

+74
-16
lines changed

include/reduced_basis/rb_parameters.h

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class RBParameters
5656

5757
/**
5858
* Constructor. Set parameters based on the std::map \p parameter_map.
59+
*
60+
* This constructor will still be supported once we switch over to
61+
* the vector-based storage for RBParameters objects. It will just set
62+
* the 0th entry of the vector corresponding to each parameter name.
5963
*/
6064
RBParameters(const std::map<std::string, Real> & parameter_map);
6165

@@ -69,30 +73,64 @@ class RBParameters
6973

7074
/**
7175
* Get a const reference to the map that stores all of the values.
76+
*
77+
* This interface is \deprecated and will be removed soon. To iterate over
78+
* the parameters map, you should instead use the begin/end APIs provided
79+
* by this class.
7280
*/
7381
const std::map<std::string, Real> & get_parameters_map() const;
7482

7583
/**
7684
* Get a const reference to the map that stores all of the "extra" values.
85+
*
86+
* This interface is \deprecated and will be removed soon. To iterate over
87+
* the parameters map, you should instead use the begin/end APIs provided
88+
* by this class.
7789
*/
7890
const std::map<std::string, Real> & get_extra_parameters_map() const;
7991

8092
/**
81-
* Get the value of the specific parameter.
93+
* \returns true if there is a parameter named "param_name" present
94+
* in this class, false otherwise.
95+
*/
96+
bool has_value(const std::string & param_name) const;
97+
98+
/**
99+
* \returns true if there is an extra parameter named "param_name" present
100+
* in this class, false otherwise.
101+
*/
102+
bool has_extra_value(const std::string & param_name) const;
103+
104+
/**
105+
* Get the value of the specified parameter, throwing an error if it
106+
* does not exist.
82107
*/
83108
Real get_value(const std::string & param_name) const;
84109

110+
/**
111+
* Get the value of the specified parameter, returning the provided
112+
* default value if it does not exist.
113+
*/
114+
Real get_value(const std::string & param_name, const Real & default_val) const;
115+
85116
/**
86117
* Set the value of the specified parameter. If param_name
87118
* doesn't already exist, it is added to the RBParameters object.
88119
*/
89120
void set_value(const std::string & param_name, Real value);
90121

91122
/**
92-
* Get the value of the specific extra parameter.
123+
* Get the value of the specified extra parameter, throwing an error
124+
* if it does not exist.
93125
*/
94126
Real get_extra_value(const std::string & param_name) const;
95127

128+
/**
129+
* Get the value of the specified extra parameter, returning the
130+
* provided default value if it does not exist.
131+
*/
132+
Real get_extra_value(const std::string & param_name, const Real & default_val) const;
133+
96134
/**
97135
* Set the value of the specified extra parameter. If param_name
98136
* doesn't already exist, it is added to the extra parameters.

src/reduced_basis/rb_data_serialization.C

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ void add_parameter_ranges_to_builder(const RBParametrized & rb_evaluation,
258258

259259
// We could loop over either parameters_min or parameters_max, they should have the same keys.
260260
unsigned int count = 0;
261-
for (const auto & [key, val] : parameters_min.get_parameters_map())
261+
for (const auto & [key, val] : parameters_min)
262262
if (!rb_evaluation.is_discrete_parameter(key))
263263
{
264264
names.set(count, key);

src/reduced_basis/rb_parameters.C

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717
// License along with this library; if not, write to the Free Software
1818
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1919

20-
// C++ includes
21-
#include <sstream>
22-
2320
// libmesh includes
2421
#include "libmesh/rb_parameters.h"
2522
#include "libmesh/utility.h"
2623

24+
// C++ includes
25+
#include <sstream>
26+
2727
namespace libMesh
2828
{
2929

30-
RBParameters::RBParameters(const std::map<std::string, Real> & parameter_map)
30+
RBParameters::RBParameters(const std::map<std::string, Real> & parameter_map) :
31+
_parameters(parameter_map)
3132
{
32-
_parameters = parameter_map;
3333
}
3434

3535
void RBParameters::clear()
@@ -40,20 +40,38 @@ void RBParameters::clear()
4040

4141
const std::map<std::string, Real> & RBParameters::get_parameters_map() const
4242
{
43+
libmesh_deprecated();
4344
return _parameters;
4445
}
4546

4647
const std::map<std::string, Real> & RBParameters::get_extra_parameters_map() const
4748
{
49+
libmesh_deprecated();
4850
return _extra_parameters;
4951
}
5052

53+
bool RBParameters::has_value(const std::string & param_name) const
54+
{
55+
return _parameters.count(param_name);
56+
}
57+
58+
bool RBParameters::has_extra_value(const std::string & param_name) const
59+
{
60+
return _extra_parameters.count(param_name);
61+
}
62+
5163
Real RBParameters::get_value(const std::string & param_name) const
5264
{
5365
// find the parameter value, throwing an error if it doesn't exist.
5466
return libmesh_map_find(_parameters, param_name);
5567
}
5668

69+
Real RBParameters::get_value(const std::string & param_name, const Real & default_val) const
70+
{
71+
auto it = _parameters.find(param_name);
72+
return (it != _parameters.end() ? it->second : default_val);
73+
}
74+
5775
void RBParameters::set_value(const std::string & param_name, Real value)
5876
{
5977
_parameters[param_name] = value;
@@ -65,6 +83,12 @@ Real RBParameters::get_extra_value(const std::string & param_name) const
6583
return libmesh_map_find(_extra_parameters, param_name);
6684
}
6785

86+
Real RBParameters::get_extra_value(const std::string & param_name, const Real & default_val) const
87+
{
88+
auto it = _extra_parameters.find(param_name);
89+
return (it != _extra_parameters.end() ? it->second : default_val);
90+
}
91+
6892
void RBParameters::set_extra_value(const std::string & param_name, Real value)
6993
{
7094
_extra_parameters[param_name] = value;
@@ -140,12 +164,9 @@ std::string RBParameters::get_string(unsigned int precision) const
140164
std::stringstream param_stringstream;
141165
param_stringstream.precision(precision);
142166

143-
const_iterator it = _parameters.begin();
144-
const_iterator it_end = _parameters.end();
145-
for ( ; it != it_end; ++it)
146-
{
147-
param_stringstream << it->first << ": " << std::scientific << it->second << std::endl;
148-
}
167+
for (const auto & [key, value] : _parameters)
168+
param_stringstream << key << ": " << std::scientific << value << std::endl;
169+
149170
return param_stringstream.str();
150171
}
151172

src/reduced_basis/rb_parametrized.C

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ std::set<std::string> RBParametrized::get_parameter_names() const
124124
libmesh_error_msg_if(!parameters_initialized, "Error: parameters not initialized in RBParametrized::get_parameter_names");
125125

126126
std::set<std::string> parameter_names;
127-
const auto & params_map = parameters_min.get_parameters_map();
128-
for (const auto & pr : params_map)
127+
for (const auto & pr : parameters_min)
129128
parameter_names.insert(pr.first);
130129

131130
return parameter_names;

0 commit comments

Comments
 (0)