File: ASTNode.cpp

package info (click to toggle)
opm-common 2024.10%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 98,420 kB
  • sloc: cpp: 263,013; python: 3,155; sh: 198; xml: 174; pascal: 136; makefile: 12
file content (255 lines) | stat: -rw-r--r-- 7,202 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
  Copyright 2019 Equinor ASA.

  This file is part of the Open Porous Media project (OPM).

  OPM is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  OPM is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with OPM.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <opm/input/eclipse/Schedule/Action/ASTNode.hpp>

#include <opm/input/eclipse/Schedule/Action/ActionContext.hpp>
#include <opm/input/eclipse/Schedule/Action/ActionValue.hpp>
#include <opm/input/eclipse/Schedule/Well/WList.hpp>
#include <opm/input/eclipse/Schedule/Well/WListManager.hpp>

#include <opm/common/utility/shmatch.hpp>

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <stdexcept>
#include <string>
#include <unordered_set>
#include <vector>

#include <fmt/format.h>
#include <fmt/ranges.h>

namespace {
    std::string strip_quotes(const std::string& s)
    {
        if (s.front() == '\'') {
            return s.substr(1, s.size() - 2);
        }
        else {
            return s;
        }
    }

    std::vector<std::string>
    strip_quotes(const std::vector<std::string>& quoted_strings)
    {
        std::vector<std::string> strings;

        std::transform(quoted_strings.begin(), quoted_strings.end(),
                       std::back_inserter(strings),
                       [](const std::string& qs)
                       {
                           return strip_quotes(qs);
                       });

        return strings;
    }
} // Anonymous namespace

Opm::Action::ASTNode::ASTNode()
    : ASTNode { TokenType::error }
{}

Opm::Action::ASTNode::ASTNode(const TokenType type_arg)
    : ASTNode { type_arg, FuncType::none, "", {} }
{}

Opm::Action::ASTNode::ASTNode(const double value)
    : ASTNode { TokenType::number }
{
    this->number = value;
}

Opm::Action::ASTNode::ASTNode(const TokenType                 type_arg,
                              const FuncType                  func_type_arg,
                              const std::string&              func_arg,
                              const std::vector<std::string>& arg_list_arg)
    : type     (type_arg)
    , func_type(func_type_arg)
    , func     (func_arg)
    , arg_list (strip_quotes(arg_list_arg))
{}

Opm::Action::ASTNode
Opm::Action::ASTNode::serializationTestObject()
{
    ASTNode result;

    result.type = TokenType::number;
    result.func_type = FuncType::field;
    result.func = "test1";
    result.arg_list = {"test2"};
    result.number = 1.0;
    ASTNode child = result;
    result.children = {child};

    return result;
}

std::size_t Opm::Action::ASTNode::size() const
{
    return this->children.size();
}

bool Opm::Action::ASTNode::empty() const
{
    return this->size() == 0;
}

void Opm::Action::ASTNode::add_child(const ASTNode& child)
{
    this->children.push_back(child);
}

Opm::Action::Value
Opm::Action::ASTNode::value(const Action::Context& context) const
{
    if (! this->children.empty()) {
        throw std::invalid_argument {
            "value() method should only reach leafnodes"
        };
    }

    if (this->type == TokenType::number) {
        return Value { this->number };
    }

    if (this->arg_list.empty()) {
        return Value { context.get(this->func) };
    }

    // The matching code is special case to handle one-argument cases with
    // well patterns like 'P*'.
    if ((this->arg_list.size() == 1) &&
        (this->arg_list.front().find("*") != std::string::npos))
    {
        if (this->func_type != FuncType::well) {
            throw std::logic_error {
                ": attempted to action-evaluate list not of type well."
            };
        }

        const auto& well_arg = this->arg_list[0];

        auto well_values = Value{};
        auto wnames = std::vector<std::string>{};

        if ((well_arg.front() == '*') && (well_arg.size() > 1)) {
            const auto& wlm = context.wlist_manager();
            wnames = wlm.wells(well_arg);
        }
        else {
            const auto& wells = context.wells(this->func);
            std::copy_if(wells.begin(), wells.end(), std::back_inserter(wnames),
                        [&well_arg](const auto& well)
                        {
                            return shmatch(well_arg, well);
                        });
        }

        for (const auto& wname : wnames) {
            well_values.add_well(wname, context.get(this->func, wname));
        }

        return well_values;
    }
    else {
        const auto arg_key = fmt::format("{}", fmt::join(this->arg_list, ":"));
        const auto scalar_value = context.get(this->func, arg_key);

        if (this->func_type == FuncType::well) {
            return Value { this->arg_list.front(), scalar_value };
        }

        return Value { scalar_value };
    }
}

Opm::Action::Result
Opm::Action::ASTNode::eval(const Action::Context& context) const
{
    if (this->empty()) {
        throw std::invalid_argument {
            "ASTNode::eval() should not reach leafnodes"
        };
    }

    if ((this->type == TokenType::op_or) ||
        (this->type == TokenType::op_and))
    {
        auto result = Result { this->type == TokenType::op_and };

        for (const auto& child : this->children) {
            if (this->type == TokenType::op_or) {
                result |= child.eval(context);
            }
            else {
                result &= child.eval(context);
            }
        }

        return result;
    }

    auto v2 = Value {};

    // Special casing of MONTH comparisons where in addition symbolic month
    // names we can compare with numeric months, in the case of numeric
    // months the numerical value should be rounded before comparison - i.e.
    //
    //   MNTH = 4.3
    //
    // should evaluate to true for the month of April (4).
    if (this->children.front().func_type == FuncType::time_month) {
        const auto& rhs = this->children[1];

        v2 = (rhs.type == TokenType::number)
            ? Value { std::round(rhs.number) }
            : rhs.value(context);
    }
    else {
        v2 = this->children[1].value(context);
    }

    return this->children.front().value(context).eval_cmp(this->type, v2);
}

bool Opm::Action::ASTNode::operator==(const ASTNode& data) const
{
    return (type == data.type)
        && (func_type == data.func_type)
        && (func == data.func)
        && (arg_list == data.arg_list)
        && (number == data.number)
        && (children == data.children)
        ;
}

void Opm::Action::ASTNode::required_summary(std::unordered_set<std::string>& required_summary) const
{
    if (this->type == TokenType::ecl_expr) {
        required_summary.insert(this->func);
    }

    for (const auto& node : this->children) {
        node.required_summary(required_summary);
    }
}