exampleSetup.hpp
Go to the documentation of this file.
1/*
2 Copyright 2016 SINTEF ICT, Applied Mathematics.
3 Copyright 2016 Statoil ASA.
4
5 This file is part of the Open Porous Media project (OPM).
6
7 OPM is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 OPM is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with OPM. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#ifndef OPM_EXAMPLESETUP_HEADER_INCLUDED
22#define OPM_EXAMPLESETUP_HEADER_INCLUDED
23
24
25
27
31
38
39#include <exception>
40#include <initializer_list>
41#include <sstream>
42#include <stdexcept>
43#include <string>
44#include <utility>
45#include <vector>
46
47#include <boost/filesystem/path.hpp>
48
49namespace example {
50 template <class FluxCalc>
53 FluxCalc&& getFlux)
54 {
56
57 const auto actPh = G.activePhases();
58
59 auto flux = ConnVals(ConnVals::NumConnections{G.numConnections()},
60 ConnVals::NumPhases{actPh.size()});
61
62 auto phas = ConnVals::PhaseID{0};
63
64 for (const auto& p : actPh) {
65 const auto pflux = getFlux(p);
66
67 if (! pflux.empty()) {
68 assert (pflux.size() == flux.numConnections());
69
70 auto conn = ConnVals::ConnID{0};
71 for (const auto& v : pflux) {
72 flux(conn, phas) = v;
73 conn.id += 1;
74 }
75 }
76
77 phas.id += 1;
78 }
79
80 return flux;
81 }
82
86 const Opm::ECLRestartData& rstrt,
87 const bool compute_fluxes,
88 const bool useEPS)
89 {
90 if (compute_fluxes) {
91 const auto grav = 0.0;
92
93 Opm::ECLFluxCalc calc(G, init, grav, useEPS);
94
95 return extractFluxField(G, [&calc, &rstrt]
96 (const Opm::ECLPhaseIndex p)
97 {
98 return calc.flux(rstrt, p);
99 });
100 }
101
102 return extractFluxField(G, [&G, &rstrt]
103 (const Opm::ECLPhaseIndex p)
104 {
105 return G.flux(rstrt, p);
106 });
107 }
108
109 template <class WellFluxes>
110 std::map<Opm::FlowDiagnostics::CellSetID, Opm::FlowDiagnostics::CellSetValues>
112 const WellFluxes& well_fluxes)
113 {
114 std::map<Opm::FlowDiagnostics::CellSetID, Opm::FlowDiagnostics::CellSetValues> well_flows;
115 for (const auto& well : well_fluxes) {
117 for (const auto& completion : well.completions) {
118 const auto& gridName = completion.gridName;
119 const auto& ijk = completion.ijk;
120 const int cell_index = G.activeCell(ijk, gridName);
121 if (cell_index >= 0) {
122 // Since inflow is a std::map, if the key was not
123 // already present operator[] will insert a
124 // value-initialized value (as in T() for a type
125 // T), which is zero for built-in numerical types,
126 // including double.
127 inflow[cell_index] += completion.reservoir_inflow_rate;
128 }
129 }
130 }
131
132 return well_flows;
133 }
134
135
136
137
138 inline Opm::ECLCaseUtilities::ResultSet
140 {
141 for (const auto* p : { "case", "grid", "init", "restart" })
142 {
143 if (param.has(p)) {
144 return Opm::ECLCaseUtilities::ResultSet {
145 param.get<std::string>(p)
146 };
147 }
148 }
149
150 throw std::invalid_argument {
151 "No Valid Result Set Identified by Input Parameters"
152 };
153 }
154
155
156
157
158 inline std::unordered_set<int>
159 getAvailableSteps(const ::Opm::ECLCaseUtilities::ResultSet& result_set)
160 {
161 const auto steps = result_set.reportStepIDs();
162
163 return { std::begin(steps), std::end(steps) };
164 }
165
166
167
168
169
171 initParam(int argc, char** argv)
172 {
173 // Obtain parameters from command line (possibly specifying a parameter file).
174 const bool verify_commandline_syntax = true;
175 const bool parameter_output = false;
176 Opm::ParameterGroup param(argc, argv, verify_commandline_syntax, parameter_output);
177 return param;
178 }
179
180
181
182 inline double simulationTime(const Opm::ECLRestartData& rstrt)
183 {
184 if (! rstrt.haveKeywordData("DOUBHEAD")) {
185 return -1.0;
186 }
187
188 const auto& doubhead = rstrt.keywordData<double>("DOUBHEAD");
189
190 // First item (.front()) is simulation time in days
191 return doubhead.front();
192 }
193
194
195
198 {
199 const auto connGraph = Opm::FlowDiagnostics::
200 ConnectivityGraph{ static_cast<int>(G.numCells()),
201 G.neighbours() };
202
203 // Create the Toolbox.
204 auto tool = Opm::FlowDiagnostics::Toolbox{ connGraph };
206
207 return tool;
208 }
209
210
211
212
213 struct Setup
214 {
215 Setup(int argc, char** argv)
216 : param (initParam(argc, argv))
218 , init (result_set.initFile())
219 , graph (::Opm::ECLGraph::load(result_set.gridFile(), init))
221 , well_fluxes ()
223 , compute_fluxes_(param.getDefault("compute_fluxes", false))
224 , useEPS_ (param.getDefault("use_ep_scaling", false))
225 {
226 const int step = param.getDefault("step", 0);
227
228 if (! this->selectReportStep(step)) {
229 std::ostringstream os;
230
231 os << "Report Step " << step
232 << " is Not Available in Result Set "
233 << this->result_set.gridFile().stem();
234
235 throw std::domain_error(os.str());
236 }
237 }
238
239 bool selectReportStep(const int step)
240 {
241 if (!this->available_steps.empty() && this->available_steps.count(step) == 0) {
242 // Requested report step not amongst those stored in the
243 // result set.
244 return false;
245 }
246
247 if (! (this->result_set.isUnifiedRestart() &&
248 bool(this->restart)))
249 {
250 // Non-unified (separate) restart files, or first time-step
251 // selection in a unified restart file case.
252 const auto restart_file =
253 this->result_set.restartFile(step);
254
255 this->openRestartFile(restart_file);
256 }
257
258 if (! this->restart->selectReportStep(step)) {
259 return false;
260 }
261
262 {
263 auto wsol = Opm::ECLWellSolution{-1.0, false};
264 well_fluxes = wsol.solution(*restart, graph.activeGrids());
265 }
266
269
271
272 return true;
273 }
274
276 Opm::ECLCaseUtilities::ResultSet result_set;
279 std::unordered_set<int> available_steps;
280 std::vector<Opm::ECLWellSolution::WellData> well_fluxes;
282 bool compute_fluxes_ = false;
283 bool useEPS_ = false;
284
285 std::shared_ptr<Opm::ECLRestartData> restart;
286
287 private:
288 void openRestartFile(const boost::filesystem::path& rstrt)
289 {
290 this->restart.reset(new Opm::ECLRestartData{ rstrt });
291 }
292 };
293
294
295} // namespace example
296
297
298
299#endif // OPM_EXAMPLESETUP_HEADER_INCLUDED
const char *const string
Definition: cJSON.h:170
Class for computing connection fluxes in the absence of flux output.
Definition: ECLFluxCalc.hpp:42
std::vector< double > flux(const ECLRestartData &rstrt, const ECLPhaseIndex phase) const
Definition: ECLGraph.hpp:52
const std::vector< ECLPhaseIndex > & activePhases() const
std::size_t numCells() const
Retrieve number of active cells in graph.
int activeCell(const std::array< int, 3 > &ijk, const std::string &gridID="") const
std::size_t numConnections() const
Retrieve number of connections in graph.
const std::vector< std::string > & activeGrids() const
std::vector< double > flux(const ECLRestartData &rstrt, const ECLPhaseIndex phase) const
std::vector< double > poreVolume() const
std::vector< int > neighbours() const
Definition: ECLResultData.hpp:177
Definition: ECLResultData.hpp:57
bool haveKeywordData(const std::string &vector, const std::string &gridID="") const
std::vector< T > keywordData(const std::string &vector, const std::string &gridID="") const
Definition: ECLWellSolution.hpp:35
Definition: CellSet.hpp:35
Definition: ConnectionValues.hpp:34
Toolbox for running flow diagnostics.
Definition: Toolbox.hpp:40
void assignConnectionFlux(const ConnectionValues &flux)
Assign fluxes associated with each connection.
void assignPoreVolume(const std::vector< double > &pv)
Assign pore volumes associated with each active cell.
void assignInflowFlux(const std::map< CellSetID, CellSetValues > &inflow_flux)
Definition: ParameterGroup.hpp:81
bool has(const std::string &name) const
This method checks if there is something with name name in the parameter gropup.
T getDefault(const std::string &name, const T &default_value) const
This method is used to read a parameter from the parameter group.
Definition: ParameterGroup_impl.hpp:214
T get(const std::string &name) const
This method is used to read a parameter from the parameter group.
Definition: ParameterGroup_impl.hpp:170
#define false
Definition: msvc_stdbool.h:19
not_this_one begin(...)
std::map< int, double > CellSetValues
Definition: CellSetValues.hpp:29
doubhead
Definition: VectorItems/doubhead.hpp:29
RestartValue load(const std::string &filename, int report_step, SummaryState &summary_state, const std::vector< RestartKey > &solution_keys, const EclipseState &es, const EclipseGrid &grid, const Schedule &schedule, const std::vector< RestartKey > &extra_keys={})
std::vector< double > init(const std::string &kewyord, const TableManager &tables, const Phases &phases, const std::vector< double > &cell_depth, const std::vector< int > &num, const std::vector< int > &endnum)
constexpr const double flux
Definition: custom-opm-common/opm-common/opm/parser/eclipse/Units/Units.hpp:183
Definition: A.hpp:4
ECLPhaseIndex
Definition: ECLPhaseIndex.hpp:28
@ end
Definition: ActionValue.hpp:20
Definition: exampleSetup.hpp:49
Opm::ECLCaseUtilities::ResultSet identifyResultSet(const Opm::ParameterGroup &param)
Definition: exampleSetup.hpp:139
std::unordered_set< int > getAvailableSteps(const ::Opm::ECLCaseUtilities::ResultSet &result_set)
Definition: exampleSetup.hpp:159
Opm::ParameterGroup initParam(int argc, char **argv)
Definition: exampleSetup.hpp:171
Opm::FlowDiagnostics::Toolbox initToolbox(const Opm::ECLGraph &G)
Definition: exampleSetup.hpp:197
std::map< Opm::FlowDiagnostics::CellSetID, Opm::FlowDiagnostics::CellSetValues > extractWellFlows(const Opm::ECLGraph &G, const WellFluxes &well_fluxes)
Definition: exampleSetup.hpp:111
Opm::FlowDiagnostics::ConnectionValues extractFluxField(const Opm::ECLGraph &G, FluxCalc &&getFlux)
Definition: exampleSetup.hpp:52
double simulationTime(const Opm::ECLRestartData &rstrt)
Definition: exampleSetup.hpp:182
Definition: exampleSetup.hpp:214
bool compute_fluxes_
Definition: exampleSetup.hpp:282
Setup(int argc, char **argv)
Definition: exampleSetup.hpp:215
std::vector< Opm::ECLWellSolution::WellData > well_fluxes
Definition: exampleSetup.hpp:280
std::unordered_set< int > available_steps
Definition: exampleSetup.hpp:279
bool selectReportStep(const int step)
Definition: exampleSetup.hpp:239
Opm::ECLGraph graph
Definition: exampleSetup.hpp:278
Opm::ECLCaseUtilities::ResultSet result_set
Definition: exampleSetup.hpp:276
std::shared_ptr< Opm::ECLRestartData > restart
Definition: exampleSetup.hpp:285
Opm::FlowDiagnostics::Toolbox toolbox
Definition: exampleSetup.hpp:281
bool useEPS_
Definition: exampleSetup.hpp:283
Opm::ECLInitFileData init
Definition: exampleSetup.hpp:277
Opm::ParameterGroup param
Definition: exampleSetup.hpp:275