22#ifndef HYBRID_NEWTON_CONFIG_HPP
23#define HYBRID_NEWTON_CONFIG_HPP
48 double scale(
double raw_value)
const {
54 return (denom == 0.0) ?
min : (raw_value -
min) / denom;
62 double unscale(
double scaled_value)
const {
67 default:
return scaled_value;
89 double apply(
double raw_value)
const {
92 case Type::Log:
return std::log(raw_value);
95 default:
return raw_value;
101 case Type::Log10:
return std::pow(10.0, transformed_value);
102 case Type::Log:
return std::exp(transformed_value);
103 case Type::Log1p:
return std::expm1(transformed_value);
105 default:
return transformed_value;
151 model_path = model_config.
get<std::string>(
"model_path",
"");
153 throw std::runtime_error(
"Missing 'model_path' in HybridNewton config");
157 throw std::runtime_error(
"Missing 'cell_indices_file' in HybridNewton config");
166 throw std::runtime_error(
"Missing 'apply_times' in HybridNewton config");
168 for (
const auto& key : applyTimesOpt->get_child_keys())
169 apply_times.push_back(applyTimesOpt->get_child(key).get<
double>(
""));
172 throw std::runtime_error(
"'apply_times' must contain at least one value");
181 [&](
const auto& p) { return p.first == name; });
186 [&](
const auto& p) { return p.first == name; });
205 if ((hasRsFeature || hasRvFeature) && !compositionSwitchEnabled) {
206 OPM_THROW(std::runtime_error,
207 "HybridNewton: RS or RV features detected but composition support is disabled. "
208 "CompositionSwitch must be enabled for RS/RV features."
220 std::vector<int> loadCellIndicesFromFile(
const std::string& filename)
const {
221 std::vector<int> indices;
222 std::ifstream cellFile(filename);
223 if (!cellFile.is_open())
224 throw std::runtime_error(
"Cannot open cell indices file: " + filename);
228 while (std::getline(cellFile, line)) {
230 if (line.empty() || line[0] ==
'#')
continue;
231 try { indices.push_back(std::stoi(line)); }
233 throw std::runtime_error(
"Invalid cell index at line " +
std::to_string(lineNumber) +
234 " in file " + filename +
": " + line);
239 throw std::runtime_error(
"No valid cell indices found in file: " + filename);
254 void parseFeatures(
const PropertyTree& pt,
const std::string& path,
255 std::vector<std::pair<std::string, FeatureSpec>>& features) {
256 auto subtreeOpt = pt.get_child_optional(path);
257 if (!subtreeOpt)
return;
259 for (
const auto& name : subtreeOpt->get_child_keys()) {
260 const PropertyTree& ft = subtreeOpt->get_child(name);
262 spec.transform = Transform(ft.get<std::string>(
"feature_engineering",
"none"));
264 if (
auto sOpt = ft.get_child_optional(
"scaling_params")) {
265 const PropertyTree& s = *sOpt;
266 if (s.get_child_optional(
"mean") && s.get_child_optional(
"std")) {
268 spec.scaler.mean = s.get<
double>(
"mean", 0.0);
269 spec.scaler.std = s.get<
double>(
"std", 1.0);
270 }
else if (s.get_child_optional(
"min") && s.get_child_optional(
"max")) {
272 spec.scaler.min = s.get<
double>(
"min", 0.0);
273 spec.scaler.max = s.get<
double>(
"max", 1.0);
281 spec.is_delta = name.compare(0, 6,
"DELTA_") == 0;
282 spec.actual_name = spec.is_delta ? name.substr(6) : name;
284 features.emplace_back(name, std::move(spec));
Configuration for a Hybrid Newton ML model.
Definition: HybridNewtonConfig.hpp:130
std::vector< double > apply_times
Definition: HybridNewtonConfig.hpp:136
bool hasInputFeature(const std::string &name) const
Definition: HybridNewtonConfig.hpp:179
std::vector< std::pair< std::string, FeatureSpec > > input_features
Definition: HybridNewtonConfig.hpp:137
std::vector< std::pair< std::string, FeatureSpec > > output_features
Definition: HybridNewtonConfig.hpp:138
HybridNewtonConfig()=default
void validateConfig(bool compositionSwitchEnabled) const
Validate feature compatibility with simulator settings.
Definition: HybridNewtonConfig.hpp:195
std::size_t n_cells
Definition: HybridNewtonConfig.hpp:135
bool hasOutputFeature(const std::string &name) const
Definition: HybridNewtonConfig.hpp:184
std::string cell_indices_file
Definition: HybridNewtonConfig.hpp:133
HybridNewtonConfig(const PropertyTree &model_config)
Construct configuration from a PropertyTree.
Definition: HybridNewtonConfig.hpp:149
std::vector< int > cell_indices
Definition: HybridNewtonConfig.hpp:134
std::string model_path
Definition: HybridNewtonConfig.hpp:132
Hierarchical collection of key/value pairs.
Definition: PropertyTree.hpp:39
T get(const std::string &key) const
std::optional< PropertyTree > get_child_optional(const std::string &key) const
Definition: blackoilbioeffectsmodules.hh:43
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)
Metadata for a single feature (input or output).
Definition: HybridNewtonConfig.hpp:115
Transform transform
Definition: HybridNewtonConfig.hpp:116
bool is_delta
Definition: HybridNewtonConfig.hpp:118
std::string actual_name
Definition: HybridNewtonConfig.hpp:119
Scaler scaler
Definition: HybridNewtonConfig.hpp:117
Represents scaling information for a feature.
Definition: HybridNewtonConfig.hpp:41
double std
Definition: HybridNewtonConfig.hpp:44
double scale(double raw_value) const
Definition: HybridNewtonConfig.hpp:48
enum Opm::Scaler::Type type
double max
Definition: HybridNewtonConfig.hpp:46
double unscale(double scaled_value) const
Definition: HybridNewtonConfig.hpp:62
double min
Definition: HybridNewtonConfig.hpp:45
double mean
Definition: HybridNewtonConfig.hpp:43
Type
Definition: HybridNewtonConfig.hpp:42