Skip to content

Commit b937392

Browse files
authored
Merge pull request #3571 from jwpeterson/rb_parameters_n_steps
Add RBParameters::set_n_steps() and update RBTheta::evaluate() APIs
2 parents 9f556ac + c780d04 commit b937392

4 files changed

Lines changed: 84 additions & 6 deletions

File tree

include/reduced_basis/rb_parameters.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,17 @@ class RBParameters
4444
{
4545
public:
4646

47+
/**
48+
* Constructor. Initializes the _n_steps parameter to 1 for
49+
* backwards compatibility, but the set_n_steps() function can
50+
* always be called later to update this value.
51+
*/
52+
RBParameters();
53+
4754
/**
4855
* The special functions can be defaulted for this class, as it
4956
* does not manage any memory itself.
5057
*/
51-
RBParameters () = default;
5258
RBParameters (RBParameters &&) = default;
5359
RBParameters (const RBParameters &) = default;
5460
RBParameters & operator= (const RBParameters &) = default;
@@ -257,6 +263,16 @@ class RBParameters
257263
*/
258264
unsigned int n_parameters() const;
259265

266+
/**
267+
* Set the number of steps this RBParameters object is intended to
268+
* represent, in the case that there are no actual parameters stored
269+
* on it. Note: this value will only be used in the no-parameters
270+
* case; if there are actual parameters specified in this class, the
271+
* number set via this API is ignored. All parameters stored within
272+
* the RBParameters object must have n_steps() steps.
273+
*/
274+
void set_n_steps(unsigned int n_steps);
275+
260276
/**
261277
* Returns the number of steps stored for all parameters. For
262278
* simplicity, we require all parameters to store the same number of
@@ -352,6 +368,14 @@ class RBParameters
352368
std::size_t index,
353369
Real value);
354370

371+
/**
372+
* The number of steps represented by this RBParameters object, in
373+
* the case where there are no parameters actually stored on it. If
374+
* there are parameters stored on this RBParameters object, then the
375+
* n_steps() API returns that number of steps instead.
376+
*/
377+
unsigned int _n_steps;
378+
355379
/**
356380
* The map that stores the actual parameter vectors. Each vector is
357381
* indexed by a name.

src/reduced_basis/rb_parameters.C

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
namespace libMesh
2828
{
2929

30+
RBParameters::RBParameters() :
31+
_n_steps(1)
32+
{
33+
}
34+
3035
RBParameters::RBParameters(const std::map<std::string, Real> & parameter_map)
3136
{
3237
// Backwards compatible support for constructing an RBParameters
@@ -38,6 +43,7 @@ RBParameters::RBParameters(const std::map<std::string, Real> & parameter_map)
3843

3944
void RBParameters::clear()
4045
{
46+
_n_steps = 1;
4147
_parameters.clear();
4248
_extra_parameters.clear();
4349
}
@@ -159,11 +165,16 @@ unsigned int RBParameters::n_parameters() const
159165
return cast_int<unsigned int>(_parameters.size());
160166
}
161167

168+
void RBParameters::set_n_steps(unsigned int n_steps)
169+
{
170+
_n_steps = n_steps;
171+
}
172+
162173
unsigned int RBParameters::n_steps() const
163174
{
164175
// Quick return if there are no parameters
165176
if (_parameters.empty())
166-
return 0;
177+
return _n_steps;
167178

168179
// If _parameters is not empty, we can check the number of steps in the first param
169180
auto size_first = _parameters.begin()->second.size();

src/reduced_basis/rb_theta.C

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,37 @@
2525
namespace libMesh
2626
{
2727

28-
Number RBTheta::evaluate(const RBParameters &) { return 1.; }
28+
Number RBTheta::evaluate(const RBParameters & mu)
29+
{
30+
// The RBTheta::evaluate() API is not general enough to handle the
31+
// multi-step RBParameters case, and you must therefore call
32+
// RBTheta::evaluate_vec() instead.
33+
libmesh_error_msg_if(mu.n_steps() > 1,
34+
"You should only call the evaluate_vec() API when using multi-step RBParameters objects.");
35+
36+
return 1.;
37+
}
2938

3039
std::vector<Number>
3140
RBTheta::evaluate_vec(const std::vector<RBParameters> & mus)
3241
{
33-
std::vector<Number> result(mus.size());
34-
for (auto i : index_range(mus))
35-
result[i] = evaluate(mus[i]);
42+
// Eventual return value
43+
std::vector<Number> result;
44+
45+
for (const auto & mu : mus)
46+
{
47+
// Backwards-compatible behavior: for single-step RBParameters objects, we fall back on
48+
// calling the scalar evaluate() function for this RBTheta object, which may have been
49+
// overridden by the user.
50+
if (mu.n_steps() == 1)
51+
result.push_back( this->evaluate(mu) );
52+
else
53+
{
54+
// For multistep RBParameters objects, all we can do is return
55+
// mu.n_steps() copies of 1 here at the base class level.
56+
result.insert(result.end(), /*count=*/mu.n_steps(), /*val=*/1.);
57+
}
58+
}
3659

3760
return result;
3861
}

tests/utils/rb_parameters_test.C

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public:
1414
CPPUNIT_TEST( testOldConstructor );
1515
CPPUNIT_TEST( testIterators );
1616
CPPUNIT_TEST( testAppend );
17+
CPPUNIT_TEST( testNSteps );
1718
CPPUNIT_TEST_SUITE_END();
1819

1920
public:
@@ -117,6 +118,25 @@ public:
117118
for (int i=0; i<3; ++i)
118119
CPPUNIT_ASSERT_EQUAL(params1.get_step_value("b", i), Real(i+3));
119120
}
121+
122+
void testNSteps()
123+
{
124+
LOG_UNIT_TEST;
125+
126+
// A default-constructed RBparameters object has 1 step by definition
127+
RBParameters params;
128+
CPPUNIT_ASSERT_EQUAL(params.n_steps(), static_cast<unsigned int>(1));
129+
130+
// Set the number of steps to use in the no-parameters case
131+
params.set_n_steps(10);
132+
CPPUNIT_ASSERT_EQUAL(params.n_steps(), static_cast<unsigned int>(10));
133+
134+
// Define multiple steps for a single parameter. Now we no longer
135+
// use the set_n_steps() value, since we have actual steps.
136+
params.push_back_value("a", 1.);
137+
params.push_back_value("a", 2.);
138+
CPPUNIT_ASSERT_EQUAL(params.n_steps(), static_cast<unsigned int>(2));
139+
}
120140
};
121141

122142
CPPUNIT_TEST_SUITE_REGISTRATION ( RBParametersTest );

0 commit comments

Comments
 (0)