Skip to content

Commit da64037

Browse files
committed
Adjust and cleanup per reviewer comments
1 parent 0f16d76 commit da64037

13 files changed

Lines changed: 107 additions & 137 deletions

‎GridKit/Model/ConfigurationChecks.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace GridKit
2222
* condition that does not hold and returns it. The caller decides how
2323
* the messages are reported; passed() is true when there are none.
2424
*/
25-
class ConfigurationChecks
25+
class [[nodiscard]] ConfigurationChecks
2626
{
2727
public:
2828
ConfigurationChecks() = default;

‎GridKit/Model/ParameterReader.hpp‎

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <cmath>
1010
#include <format>
11+
#include <optional>
1112
#include <stdexcept>
1213
#include <string_view>
1314
#include <variant>
@@ -24,12 +25,11 @@ namespace GridKit
2425
/**
2526
* @brief Reads typed parameters out of a model data container.
2627
*
27-
* A load method stores a provided value in its target and returns true.
28-
* An omitted parameter leaves the target untouched, so the target's
29-
* initial value is the default. A require method rejects an omitted
30-
* parameter. A value of the wrong type cannot produce a valid model, so
31-
* it is rejected with std::invalid_argument naming the model and the
32-
* parameter. Real parameters accept integer values and must be finite.
28+
* A read method returns the provided value or std::nullopt when the
29+
* parameter is omitted. The model chooses a default or requires a value.
30+
* Invalid values are rejected with std::invalid_argument naming the
31+
* model and parameter. Real parameters accept integer values and must
32+
* be finite.
3333
*
3434
* @tparam ModelDataT A model data container satisfying
3535
* PhasorDynamics::ModelData.
@@ -52,13 +52,13 @@ namespace GridKit
5252
{
5353
}
5454

55-
/// Load an optional real parameter.
56-
bool loadReal(ParametersT key, RealT& target) const
55+
/// Read an optional real parameter.
56+
[[nodiscard]] std::optional<RealT> readReal(ParametersT key) const
5757
{
5858
const auto* value = find(key);
5959
if (value == nullptr)
6060
{
61-
return false;
61+
return std::nullopt;
6262
}
6363

6464
RealT real{};
@@ -80,57 +80,30 @@ namespace GridKit
8080
reject(key, "must be finite");
8181
}
8282

83-
target = real;
84-
return true;
83+
return real;
8584
}
8685

87-
/// Load an optional non-negative real parameter.
88-
bool loadNonNegative(ParametersT key, RealT& target) const
86+
/// Read an optional non-negative real parameter.
87+
[[nodiscard]] std::optional<RealT> readNonNegative(ParametersT key) const
8988
{
90-
if (!loadReal(key, target))
91-
{
92-
return false;
93-
}
94-
95-
if (target < ZERO<RealT>)
89+
auto value = readReal(key);
90+
if (value && *value < ZERO<RealT>)
9691
{
9792
reject(key, "must be non-negative");
9893
}
99-
return true;
100-
}
101-
102-
/// Load an optional boolean parameter.
103-
bool loadBool(ParametersT key, bool& target) const
104-
{
105-
return loadExact(key, target, "must be boolean");
106-
}
107-
108-
/// Load an optional integer parameter.
109-
bool loadInteger(ParametersT key, IdxT& target) const
110-
{
111-
return loadExact(key, target, "must be an integer");
94+
return value;
11295
}
11396

114-
/// Read a real parameter that must be provided.
115-
RealT requireReal(ParametersT key) const
97+
/// Read an optional boolean parameter.
98+
[[nodiscard]] std::optional<bool> readBool(ParametersT key) const
11699
{
117-
RealT real{};
118-
if (!loadReal(key, real))
119-
{
120-
reject(key, "is required");
121-
}
122-
return real;
100+
return readExact<bool>(key, "must be boolean");
123101
}
124102

125-
/// Read a boolean parameter that must be provided.
126-
bool requireBool(ParametersT key) const
103+
/// Read an optional integer parameter.
104+
[[nodiscard]] std::optional<IdxT> readInteger(ParametersT key) const
127105
{
128-
bool flag{};
129-
if (!loadBool(key, flag))
130-
{
131-
reject(key, "is required");
132-
}
133-
return flag;
106+
return readExact<IdxT>(key, "must be an integer");
134107
}
135108

136109
private:
@@ -148,14 +121,14 @@ namespace GridKit
148121
return &entry->second;
149122
}
150123

151-
/// Load an optional parameter that must hold exactly a T.
124+
/// Read an optional parameter that must hold exactly a T.
152125
template <typename T>
153-
bool loadExact(ParametersT key, T& target, std::string_view reason) const
126+
[[nodiscard]] std::optional<T> readExact(ParametersT key, std::string_view reason) const
154127
{
155128
const auto* value = find(key);
156129
if (value == nullptr)
157130
{
158-
return false;
131+
return std::nullopt;
159132
}
160133

161134
const auto* exact_value = std::get_if<T>(value);
@@ -164,8 +137,7 @@ namespace GridKit
164137
reject(key, reason);
165138
}
166139

167-
target = *exact_value;
168-
return true;
140+
return *exact_value;
169141
}
170142

171143
/// Reject the model data with a message naming the model and parameter.

‎GridKit/Model/PhasorDynamics/Component.hpp‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ namespace GridKit
6262
}
6363
}
6464

65-
/// Report every configuration problem; passed() when the model can initialize.
66-
virtual Model::ConfigurationChecks verify() const = 0;
65+
/// Report configuration problems. Override when the component has checks.
66+
virtual Model::ConfigurationChecks verify() const
67+
{
68+
return {};
69+
}
6770

6871
IdxT size() override final
6972
{
@@ -287,7 +290,7 @@ namespace GridKit
287290
*
288291
* @return true when the configuration has no problems.
289292
*/
290-
bool verifyAndLog() const
293+
[[nodiscard]] bool verifyAndLog() const
291294
{
292295
const auto checks = verify();
293296
for (const auto& error : checks.errors())

‎GridKit/Model/PhasorDynamics/Exciter/ESDC1A/Esdc1aImpl.hpp‎

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -636,26 +636,26 @@ namespace GridKit
636636
{
637637
using Params = typename ModelDataT::Parameters;
638638

639-
Model::ParameterReader reader(data, "Esdc1a");
640-
641-
reader.loadNonNegative(Params::Tr, Tr_);
642-
reader.loadReal(Params::Ka, Ka_);
643-
reader.loadNonNegative(Params::Ta, Ta_);
644-
reader.loadNonNegative(Params::Tb, Tb_);
645-
reader.loadReal(Params::Tc, Tc_);
646-
reader.loadReal(Params::Vrmax, Vrmax_);
647-
reader.loadReal(Params::Vrmin, Vrmin_);
648-
reader.loadReal(Params::Ke, Ke_);
649-
reader.loadNonNegative(Params::Te, Te_);
650-
reader.loadReal(Params::Kf, Kf_);
651-
reader.loadNonNegative(Params::Tf1, Tf1_);
652-
reader.loadBool(Params::Spdmlt, Spdmlt_);
653-
reader.loadReal(Params::E1, E1_);
654-
reader.loadReal(Params::Se1, Se1_);
655-
reader.loadReal(Params::E2, E2_);
656-
reader.loadReal(Params::Se2, Se2_);
657-
reader.loadInteger(Params::UEL, UEL_);
658-
reader.loadBool(Params::exclim, exclim_);
639+
const Model::ParameterReader reader(data, "Esdc1a");
640+
641+
Tr_ = reader.readNonNegative(Params::Tr).value_or(Tr_);
642+
Ka_ = reader.readReal(Params::Ka).value_or(Ka_);
643+
Ta_ = reader.readNonNegative(Params::Ta).value_or(Ta_);
644+
Tb_ = reader.readNonNegative(Params::Tb).value_or(Tb_);
645+
Tc_ = reader.readReal(Params::Tc).value_or(Tc_);
646+
Vrmax_ = reader.readReal(Params::Vrmax).value_or(Vrmax_);
647+
Vrmin_ = reader.readReal(Params::Vrmin).value_or(Vrmin_);
648+
Ke_ = reader.readReal(Params::Ke).value_or(Ke_);
649+
Te_ = reader.readNonNegative(Params::Te).value_or(Te_);
650+
Kf_ = reader.readReal(Params::Kf).value_or(Kf_);
651+
Tf1_ = reader.readNonNegative(Params::Tf1).value_or(Tf1_);
652+
Spdmlt_ = reader.readBool(Params::Spdmlt).value_or(Spdmlt_);
653+
E1_ = reader.readReal(Params::E1).value_or(E1_);
654+
Se1_ = reader.readReal(Params::Se1).value_or(Se1_);
655+
E2_ = reader.readReal(Params::E2).value_or(E2_);
656+
Se2_ = reader.readReal(Params::Se2).value_or(Se2_);
657+
UEL_ = reader.readInteger(Params::UEL).value_or(UEL_);
658+
exclim_ = reader.readBool(Params::exclim).value_or(exclim_);
659659
setDerivedParameters();
660660
}
661661

‎GridKit/Model/PhasorDynamics/Load/LoadZ/LoadZ.hpp‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ namespace GridKit
5656
virtual int evaluateResidual() override final;
5757
virtual int evaluateJacobian() override final;
5858

59-
virtual Model::ConfigurationChecks verify() const override final
60-
{
61-
return {};
62-
}
63-
6459
public:
6560
void setR(RealT R)
6661
{

‎GridKit/Model/PhasorDynamics/Load/LoadZIP/LoadZIP.hpp‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ namespace GridKit
5656
int evaluateResidual() override final;
5757
int evaluateJacobian() override final;
5858

59-
Model::ConfigurationChecks verify() const override final
60-
{
61-
return {};
62-
}
63-
6459
public:
6560
void setPnom(RealT Pnom)
6661
{

‎GridKit/Model/PhasorDynamics/SignalSource/ConstantSignalSource.hpp‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ namespace GridKit
5151
int evaluateResidual() override final;
5252
int evaluateJacobian() override final;
5353

54-
Model::ConfigurationChecks verify() const override final;
55-
5654
SignalPortsT& getPorts()
5755
{
5856
return ports_;

‎GridKit/Model/PhasorDynamics/SignalSource/ConstantSignalSourceImpl.hpp‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ namespace GridKit
8383
return 0;
8484
}
8585

86-
template <typename scalar_type, typename index_type>
87-
Model::ConfigurationChecks ConstantSignalSource<scalar_type, index_type>::verify() const
88-
{
89-
return {};
90-
}
91-
9286
template <typename scalar_type, typename index_type>
9387
int ConstantSignalSource<scalar_type, index_type>::initialize()
9488
{

‎GridKit/Model/PhasorDynamics/SignalSource/FunctionSignalSource.hpp‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ namespace GridKit
5454
int evaluateResidual() override final;
5555
int evaluateJacobian() override final;
5656

57-
Model::ConfigurationChecks verify() const override final;
58-
5957
void updateTime(RealT t, RealT a) override;
6058

6159
SignalPortsT& getPorts()

‎GridKit/Model/PhasorDynamics/SignalSource/FunctionSignalSourceImpl.hpp‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,6 @@ namespace GridKit
109109
return 0;
110110
}
111111

112-
template <typename scalar_type, typename index_type>
113-
Model::ConfigurationChecks FunctionSignalSource<scalar_type, index_type>::verify() const
114-
{
115-
return {};
116-
}
117-
118112
template <typename scalar_type, typename index_type>
119113
int FunctionSignalSource<scalar_type, index_type>::initialize()
120114
{

0 commit comments

Comments
 (0)