opm-simulators
HybridNewtonConfig.hpp
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 /*
4  Copyright 2025 NORCE Research AS
5 
6  This file is part of the Open Porous Media project (OPM).
7 
8  OPM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  OPM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OPM. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #ifndef HYBRID_NEWTON_CONFIG_HPP
23 #define HYBRID_NEWTON_CONFIG_HPP
24 
25 #include <cmath>
26 #include <string>
27 #include <vector>
28 
29 namespace Opm {
30 
31 class PropertyTree;
32 
38 struct Scaler
39 {
40  enum class Type { None, Standard, MinMax } type = Type::None;
41  double mean = 0.0;
42  double std = 1.0;
43  double min = 0.0;
44  double max = 1.0;
45 
46  double scale(double raw_value) const
47  {
48  switch (type) {
49  case Type::Standard:
50  return (std == 0.0) ? mean : (raw_value - mean) / std;
51  case Type::MinMax: {
52  double denom = max - min;
53  return (denom == 0.0) ? min : (raw_value - min) / denom;
54  }
55  case Type::None:
56  default:
57  return raw_value;
58  }
59  }
60 
61  double unscale(double scaled_value) const
62  {
63  switch (type) {
64  case Type::Standard: return scaled_value * std + mean;
65  case Type::MinMax: return scaled_value * (max - min) + min;
66  case Type::None:
67  default: return scaled_value;
68  }
69  }
70 };
71 
78 struct Transform
79 {
80  enum class Type { None, Log, Log10, Log1p } type = Type::None;
81 
82  Transform() = default;
83  explicit Transform(const std::string& name)
84  {
85  if (name == "log10") type = Type::Log10;
86  else if (name == "log") type = Type::Log;
87  else if (name == "log1p") type = Type::Log1p;
88  else type = Type::None;
89  }
90 
91  double apply(double raw_value) const
92  {
93  switch (type) {
94  case Type::Log10: return std::log10(raw_value);
95  case Type::Log: return std::log(raw_value);
96  case Type::Log1p: return std::log1p(raw_value);
97  case Type::None:
98  default: return raw_value;
99  }
100  }
101 
102  double applyInverse(double transformed_value) const
103  {
104  switch (type) {
105  case Type::Log10: return std::pow(10.0, transformed_value);
106  case Type::Log: return std::exp(transformed_value);
107  case Type::Log1p: return std::expm1(transformed_value);
108  case Type::None:
109  default: return transformed_value;
110  }
111  }
112 };
113 
120 {
121  Transform transform;
122  Scaler scaler;
123  bool is_delta = false;
124  std::string actual_name;
125 
126  FeatureSpec() = default;
127 };
128 
136 {
137 public:
138  std::string model_path;
139  std::string cell_indices_file;
140  std::vector<int> cell_indices;
141  std::size_t n_cells = 0;
142  std::vector<double> apply_times;
143  std::vector<std::pair<std::string, FeatureSpec>> input_features;
144  std::vector<std::pair<std::string, FeatureSpec>> output_features;
145 
146  // Default constructor
147  HybridNewtonConfig() = default;
148 
155  explicit HybridNewtonConfig(const PropertyTree& model_config);
156 
157  bool hasInputFeature(const std::string& name) const;
158 
159  bool hasOutputFeature(const std::string& name) const;
160 
167  void validateConfig(bool compositionSwitchEnabled) const;
168 
169 private:
176  std::vector<int> loadCellIndicesFromFile(const std::string& filename) const;
177 
188  void parseFeatures(const PropertyTree& pt, const std::string& path,
189  std::vector<std::pair<std::string, FeatureSpec>>& features);
190 };
191 
192 } // namespace Opm
193 
194 #endif
Represents scaling information for a feature.
Definition: HybridNewtonConfig.hpp:38
void validateConfig(bool compositionSwitchEnabled) const
Validate feature compatibility with simulator settings.
Definition: HybridNewtonConfig.cpp:82
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
Metadata for a single feature (input or output).
Definition: HybridNewtonConfig.hpp:119
Represents a transformation applied to a feature.
Definition: HybridNewtonConfig.hpp:78
Hierarchical collection of key/value pairs.
Definition: PropertyTree.hpp:38
Configuration for a Hybrid Newton ML model.
Definition: HybridNewtonConfig.hpp:135