42 enum class Type { None, Standard, MinMax } type = Type::None;
48 double scale(
double raw_value)
const {
51 return (std == 0.0) ? mean : (raw_value - mean) / std;
53 double denom = max - min;
54 return (denom == 0.0) ? min : (raw_value - min) / denom;
62 double unscale(
double scaled_value)
const {
64 case Type::Standard:
return scaled_value * std + mean;
65 case Type::MinMax:
return scaled_value * (max - min) + min;
67 default:
return scaled_value;
79 enum class Type { None, Log, Log10, Log1p } type = Type::None;
81 Transform() =
default;
82 explicit Transform(
const std::string& name) {
83 if (name ==
"log10") type = Type::Log10;
84 else if (name ==
"log") type = Type::Log;
85 else if (name ==
"log1p") type = Type::Log1p;
86 else type = Type::None;
89 double apply(
double raw_value)
const {
91 case Type::Log10:
return std::log10(raw_value);
92 case Type::Log:
return std::log(raw_value);
93 case Type::Log1p:
return std::log1p(raw_value);
95 default:
return raw_value;
99 double applyInverse(
double transformed_value)
const {
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;
130class HybridNewtonConfig {
132 std::string model_path;
133 std::string cell_indices_file;
134 std::vector<int> cell_indices;
135 std::size_t n_cells = 0;
136 std::vector<double> apply_times;
137 std::vector<std::pair<std::string, FeatureSpec>> input_features;
138 std::vector<std::pair<std::string, FeatureSpec>> output_features;
141 HybridNewtonConfig() =
default;
151 model_path = model_config.
get<std::string>(
"model_path",
"");
152 if (model_path.empty())
153 throw std::runtime_error(
"Missing 'model_path' in HybridNewton config");
155 cell_indices_file = model_config.
get<std::string>(
"cell_indices_file",
"");
156 if (cell_indices_file.empty())
157 throw std::runtime_error(
"Missing 'cell_indices_file' in HybridNewton config");
160 cell_indices = loadCellIndicesFromFile(cell_indices_file);
161 n_cells = cell_indices.size();
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>(
""));
171 if (apply_times.empty())
172 throw std::runtime_error(
"'apply_times' must contain at least one value");
175 parseFeatures(model_config,
"features.inputs", input_features);
176 parseFeatures(model_config,
"features.outputs", output_features);
179 bool hasInputFeature(
const std::string& name)
const {
180 return std::any_of(input_features.begin(), input_features.end(),
181 [&](
const auto& p) { return p.first == name; });
184 bool hasOutputFeature(
const std::string& name)
const {
185 return std::any_of(output_features.begin(), output_features.end(),
186 [&](
const auto& p) { return p.first == name; });
197 bool hasRsFeature = hasInputFeature(
"RS") ||
198 hasOutputFeature(
"RS") ||
199 hasOutputFeature(
"DELTA_RS");
201 bool hasRvFeature = hasInputFeature(
"RV") ||
202 hasOutputFeature(
"RV") ||
203 hasOutputFeature(
"DELTA_RV");
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")) {
267 spec.scaler.type = Scaler::Type::Standard;
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")) {
271 spec.scaler.type = Scaler::Type::MinMax;
272 spec.scaler.min = s.get<
double>(
"min", 0.0);
273 spec.scaler.max = s.get<
double>(
"max", 1.0);
275 spec.scaler.type = Scaler::Type::None;
278 spec.scaler.type = Scaler::Type::None;
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));
std::optional< PropertyTree > get_child_optional(const std::string &key) const
Retrieve copy of sub tree rooted at node.
Definition PropertyTree.cpp:90
T get(const std::string &key) const
Retrieve property value given hierarchical property key.
Definition PropertyTree.cpp:59