Skip to content

Commit 9d582e4

Browse files
author
Dmitri Naumov
committed
Merge branch 'opt-inline-params-satwtd_TC' into 'master'
Enable optional inline parameters for MPL property SaturationWeightedThermalConductivity See merge request ogs/ogs!5392
2 parents 8b4650a + bf26a6e commit 9d582e4

File tree

19 files changed

+412
-74
lines changed

19 files changed

+412
-74
lines changed

BaseLib/ConfigTree-impl.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <utility>
1515

1616
#include "ConfigTree.h"
17+
#include "StringTools.h"
1718

1819
namespace BaseLib
1920
{
@@ -87,24 +88,20 @@ std::optional<std::vector<T>> ConfigTree::getConfigParameterOptionalImpl(
8788
{
8889
if (auto p = getConfigSubtreeOptional(param))
8990
{
90-
std::istringstream sstr{p->getValue<std::string>()};
91-
std::vector<T> result;
92-
T value;
93-
while (sstr >> value)
94-
{
95-
result.push_back(value);
96-
}
97-
if (!sstr.eof()) // The stream is not read until the end, must be an
98-
// error. result contains number of read values.
91+
std::string const raw = p->getValue<std::string>();
92+
93+
std::size_t bad_idx = 0;
94+
if (auto parsed = BaseLib::tryParseVector<T>(raw, &bad_idx))
9995
{
100-
error("Value for key <" + param + "> `" + shortString(sstr.str()) +
101-
"' not convertible to a vector of the desired type."
102-
" Could not convert token no. " +
103-
std::to_string(result.size() + 1) + ".");
104-
return std::nullopt;
96+
return parsed; // OK
10597
}
10698

107-
return std::make_optional(result);
99+
// preserve original error wording & token index
100+
error("Value for key <" + param + "> `" + shortString(raw) +
101+
"' not convertible to a vector of the desired type."
102+
" Could not convert token no. " +
103+
std::to_string(bad_idx) + ".");
104+
return std::nullopt; // not reached
108105
}
109106

110107
return std::nullopt;

BaseLib/StringTools.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
#include <cstddef>
1818
#include <list>
19+
#include <range/v3/range/conversion.hpp>
20+
#include <range/v3/view/istream.hpp>
1921
#include <sstream>
2022
#include <string>
23+
#include <string_view>
2124
#include <vector>
2225

2326
namespace BaseLib
@@ -83,4 +86,31 @@ std::string randomString(std::size_t length);
8386
//! Append '-' and a number such that the name is unique.
8487
std::string getUniqueName(std::vector<std::string> const& existing_names,
8588
std::string const& input_name);
89+
90+
/**
91+
* Tries to parse a whitespace-separated list of values from a string.
92+
* Returns std::nullopt if parsing fails and sets the bad token index.
93+
*/
94+
template <typename T>
95+
std::optional<std::vector<T>> tryParseVector(std::string const& raw,
96+
std::size_t* bad_token_idx)
97+
{
98+
std::istringstream iss{raw};
99+
100+
// Create a range that reads T values from the stream
101+
auto values = ranges::istream_view<T>(iss);
102+
std::vector<T> out = ranges::to<std::vector>(values);
103+
104+
// Check if we consumed the entire input
105+
if (!iss.eof())
106+
{
107+
if (bad_token_idx)
108+
{
109+
*bad_token_idx = out.size() + 1;
110+
}
111+
return std::nullopt;
112+
}
113+
return out;
114+
}
115+
86116
} // end namespace BaseLib
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
\copydoc MaterialPropertyLib::SaturationWeightedThermalConductivity::dry_thermal_conductivity_
2-
The values must be given as Parameter type.
2+
The values can be given either as a reference to a predefined `<parameter>`
3+
or directly as inline constant values, e.g.,
4+
`<dry_thermal_conductivity>0.5 0.3 0.2</dry_thermal_conductivity>`.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
\copydoc MaterialPropertyLib::SaturationWeightedThermalConductivity::wet_thermal_conductivity_
2-
The values must be given as Parameter type.
2+
The values can be given either as a reference to a predefined `<parameter>`
3+
or directly as inline constant values, e.g.,
4+
`<wet_thermal_conductivity>0.9 0.6 0.7</wet_thermal_conductivity>`.

MaterialLib/MPL/CreateComponent.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace
2424
std::unique_ptr<MaterialPropertyLib::Component> createComponent(
2525
int const geometry_dimension,
2626
BaseLib::ConfigTree const& config,
27-
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
27+
std::vector<std::unique_ptr<ParameterLib::ParameterBase>>& parameters,
2828
ParameterLib::CoordinateSystem const* const local_coordinate_system,
2929
std::map<std::string,
3030
std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&
@@ -75,7 +75,7 @@ namespace MaterialPropertyLib
7575
std::vector<std::unique_ptr<Component>> createComponents(
7676
int const geometry_dimension,
7777
std::optional<BaseLib::ConfigTree> const& config,
78-
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
78+
std::vector<std::unique_ptr<ParameterLib::ParameterBase>>& parameters,
7979
ParameterLib::CoordinateSystem const* const local_coordinate_system,
8080
std::map<std::string,
8181
std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&

MaterialLib/MPL/CreateComponent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace MaterialPropertyLib
4646
std::vector<std::unique_ptr<Component>> createComponents(
4747
int const geometry_dimension,
4848
std::optional<BaseLib::ConfigTree> const& config,
49-
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
49+
std::vector<std::unique_ptr<ParameterLib::ParameterBase>>& parameters,
5050
ParameterLib::CoordinateSystem const* const local_coordinate_system,
5151
std::map<std::string,
5252
std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&

MaterialLib/MPL/CreateMedium.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ std::unique_ptr<Medium> createMedium(
2525
int const material_id,
2626
int const geometry_dimension,
2727
BaseLib::ConfigTree const& config,
28-
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
28+
std::vector<std::unique_ptr<ParameterLib::ParameterBase>>& parameters,
2929
ParameterLib::CoordinateSystem const* const local_coordinate_system,
3030
std::map<std::string,
3131
std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&

MaterialLib/MPL/CreateMedium.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ std::unique_ptr<Medium> createMedium(
4545
int const material_id,
4646
int const geometry_dimension,
4747
BaseLib::ConfigTree const& config,
48-
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
48+
std::vector<std::unique_ptr<ParameterLib::ParameterBase>>& parameters,
4949
ParameterLib::CoordinateSystem const* const local_coordinate_system,
5050
std::map<std::string,
5151
std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&

MaterialLib/MPL/CreatePhase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace
2727
std::unique_ptr<MaterialPropertyLib::Phase> createPhase(
2828
int const geometry_dimension,
2929
BaseLib::ConfigTree const& config,
30-
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
30+
std::vector<std::unique_ptr<ParameterLib::ParameterBase>>& parameters,
3131
ParameterLib::CoordinateSystem const* const local_coordinate_system,
3232
std::map<std::string,
3333
std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&
@@ -91,7 +91,7 @@ namespace MaterialPropertyLib
9191
std::vector<std::unique_ptr<Phase>> createPhases(
9292
int const geometry_dimension,
9393
std::optional<BaseLib::ConfigTree> const& config,
94-
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
94+
std::vector<std::unique_ptr<ParameterLib::ParameterBase>>& parameters,
9595
ParameterLib::CoordinateSystem const* const local_coordinate_system,
9696
std::map<std::string,
9797
std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&

MaterialLib/MPL/CreatePhase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace MaterialPropertyLib
5151
std::vector<std::unique_ptr<Phase>> createPhases(
5252
int const geometry_dimension,
5353
std::optional<BaseLib::ConfigTree> const& config,
54-
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
54+
std::vector<std::unique_ptr<ParameterLib::ParameterBase>>& parameters,
5555
ParameterLib::CoordinateSystem const* const local_coordinate_system,
5656
std::map<std::string,
5757
std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&

0 commit comments

Comments
 (0)