OutputExtractor.hpp
Go to the documentation of this file.
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 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18
19 Consult the COPYING file in the top-level source directory of this
20 module for the precise wording of the license and the list of
21 copyright holders.
22*/
27#ifndef OPM_OUTPUT_EXTRACTORS_HPP
28#define OPM_OUTPUT_EXTRACTORS_HPP
29
30#include <opm/common/OpmLog/OpmLog.hpp>
31#include <opm/common/utility/Visitor.hpp>
32
33#include <opm/material/common/Valgrind.hpp>
34
39
40#include <algorithm>
41#include <array>
42#include <unordered_map>
43#include <set>
44#include <variant>
45#include <vector>
46
47#include <fmt/format.h>
48
49namespace Opm::detail {
50
52template<class TypeTag>
54{
57 using FluidState = typename IntensiveQuantities::FluidState;
59 static constexpr int numPhases = FluidSystem::numPhases;
60
63 {
70 };
71
73 struct Context
74 {
75 unsigned globalDofIdx;
76 unsigned pvtRegionIdx;
78 const FluidState& fs;
81 };
82
84 using AssignFunc = std::function<void(const Context&)>;
85
88 using ScalarFunc = std::function<Scalar(const Context&)>;
89
92 using PhaseFunc = std::function<Scalar(const unsigned /*phase*/, const Context&)>;
93
94 using ScalarBuffer = std::vector<Scalar>;
95 using PhaseArray = std::array<ScalarBuffer,numPhases>;
96
99 {
102 };
103
106 {
109 };
110
112 struct Entry
113 {
114 std::variant<AssignFunc, ScalarEntry, PhaseEntry> data;
115 bool condition = true;
116 };
117
119 template<std::size_t size>
120 static std::vector<Entry> removeInactive(std::array<Entry,size>& input)
121 {
122 // Setup active extractors
123 std::vector<Entry> filtered_extractors;
124 filtered_extractors.reserve(input.size());
125 std::copy_if(std::move_iterator(input.begin()),
126 std::move_iterator(input.end()),
127 std::back_inserter(filtered_extractors),
128 [](const Entry& e)
129 {
130 if (!e.condition) {
131 return false;
132 }
133 return std::visit(VisitorOverloadSet{
134 [](const AssignFunc&)
135 {
136 return true;
137 },
138 [](const ScalarEntry& v)
139 {
140 return !v.data->empty();
141 },
142 [](const PhaseEntry& v)
143 {
144 return std::any_of(v.data->begin(),
145 v.data->end(),
146 [](const auto& ve)
147 { return !ve.empty(); });
148 }
149 }, e.data);
150 });
151
152 return filtered_extractors;
153 }
154
158 static void process(const Context& ectx,
159 const std::vector<Entry>& extractors)
160 {
161 std::for_each(extractors.begin(), extractors.end(),
162 [&ectx](const auto& entry)
163 {
164 std::visit(VisitorOverloadSet{
165 [&ectx](const ScalarEntry& v)
166 {
167 auto& array = *v.data;
168 array[ectx.globalDofIdx] = v.extract(ectx);
169 Valgrind::CheckDefined(array[ectx.globalDofIdx]);
170 },
171 [&ectx](const PhaseEntry& v)
172 {
173 std::for_each(v.data->begin(), v.data->end(),
174 [phaseIdx = 0, &ectx, &v](auto& array) mutable
175 {
176 if (!array.empty()) {
177 array[ectx.globalDofIdx] = v.extract(phaseIdx, ectx);
178 Valgrind::CheckDefined(array[ectx.globalDofIdx]);
179 }
180 ++phaseIdx;
181 });
182 },
183 [&ectx](const typename Extractor::AssignFunc& extract)
184 { extract(ectx); },
185 }, entry.data);
186 });
187 }
188};
189
191template<class TypeTag>
193{
197 using FluidState = typename IntensiveQuantities::FluidState;
199 static constexpr int numPhases = FluidSystem::numPhases;
200 static constexpr int oilPhaseIdx = FluidSystem::oilPhaseIdx;
201 static constexpr int gasPhaseIdx = FluidSystem::gasPhaseIdx;
202 static constexpr int waterPhaseIdx = FluidSystem::waterPhaseIdx;
203
205 struct Context
206 {
207 unsigned globalDofIdx;
208 unsigned dofIdx;
209 const FluidState& fs;
212 };
213
215 using AssignFunc = std::function<void(const Context&)>;
216
219 using ScalarFunc = std::function<Scalar(const Context&)>;
220
223 using PhaseFunc = std::function<Scalar(const unsigned /*phase*/, const Context&)>;
224
226 {
228 std::variant<std::string_view, std::vector<std::string_view>> kw;
229
232 };
233
235 {
237 std::variant<std::array<std::string_view, numPhases>,
238 std::array<std::array<std::string_view, numPhases>, 2>> kw;
239
242 };
243
245 using Entry = std::variant<ScalarEntry, PhaseEntry>;
246
248 struct Exec
249 {
251 Exec(double* d, ScalarFunc&& e)
252 : data(d), extract(std::move(e))
253 {}
254
255 double* data;
257 };
258
260 using ExecMap = std::unordered_map<int, std::vector<Exec>>;
261
263 template<std::size_t size>
264 static ExecMap setupExecMap(std::map<std::pair<std::string, int>, double>& blockData,
265 const std::array<Entry,size>& handlers)
266 {
267 using PhaseViewArray = std::array<std::string_view, numPhases>;
268 using StringViewVec = std::vector<std::string_view>;
269
270 ExecMap extractors;
271
272 std::for_each(
273 blockData.begin(),
274 blockData.end(),
275 [&handlers, &extractors](auto& bd_info)
276 {
277 unsigned phase{};
278 const auto& [key, cell] = bd_info.first;
279 const auto& handler_info =
280 std::find_if(
281 handlers.begin(),
282 handlers.end(),
283 [&kw_name = bd_info.first.first, &phase](const auto& handler)
284 {
285 // Extract list of keyword names from handler
286 const auto gen_handlers =
287 std::visit(VisitorOverloadSet{
288 [](const ScalarEntry& entry)
289 {
290 return std::visit(VisitorOverloadSet{
291 [](const std::string_view& kw) -> StringViewVec
292 {
293 return {kw};
294 },
295 [](const StringViewVec& kws) -> StringViewVec
296 { return kws; }
297 }, entry.kw);
298 },
299 [](const PhaseEntry& entry)
300 {
301 return std::visit(VisitorOverloadSet{
302 [](const PhaseViewArray& data)
303 {
304 return StringViewVec{data.begin(), data.end()};
305 },
306 [](const std::array<PhaseViewArray,2>& data)
307 {
308 StringViewVec res;
309 res.reserve(2*numPhases);
310 res.insert(res.end(),
311 data[0].begin(),
312 data[0].end());
313 res.insert(res.end(),
314 data[1].begin(),
315 data[1].end());
316 return res;
317 }
318 }, entry.kw);
319 }
320 }, handler);
321
322 const auto found_handler =
323 std::find(gen_handlers.begin(), gen_handlers.end(), kw_name);
324 if (found_handler != gen_handlers.end()) {
325 phase = std::distance(gen_handlers.begin(), found_handler) % numPhases;
326 }
327 return found_handler != gen_handlers.end();
328 }
329 );
330
331 if (handler_info != handlers.end()) {
332 extractors[cell - 1].emplace_back(
333 &bd_info.second,
334 std::visit(VisitorOverloadSet{
335 [](const ScalarEntry& e)
336 {
337 return e.extract;
338 },
339 [phase](const PhaseEntry& e) -> ScalarFunc
340 {
341 return [phase, extract = e.extract]
342 (const Context& ectx)
343 {
344 static constexpr auto phaseMap = std::array{
345 waterPhaseIdx,
346 oilPhaseIdx,
347 gasPhaseIdx,
348 };
349 return extract(phaseMap[phase], ectx);
350 };
351 }
352 }, *handler_info)
353 );
354 }
355 else {
356 OpmLog::warning("Unhandled output keyword",
357 fmt::format("Keyword '{}' is unhandled for output "
358 "to summary file.", key));
359 }
360 }
361 );
362
363 return extractors;
364 }
365
367 static void process(const std::vector<Exec>& blockExtractors,
368 const Context& ectx)
369 {
370 std::for_each(blockExtractors.begin(), blockExtractors.end(),
371 [&ectx](auto& bdata)
372 { *bdata.data = bdata.extract(ectx); });
373 }
374};
375
376} // namespace Opm::detail
377
378#endif // OPM_OUTPUT_EXTRACTORS_HPP
Defines a type tags and some fundamental properties all models.
Declare the properties used by the infrastructure code of the finite volume discretizations.
Defines the common properties required by the porous medium multi-phase models.
Definition: alignedallocator.hh:32
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(....
Definition: propertysystem.hh:233
The Opm property system, traits with inheritance.
Context passed to element extractor functions.
Definition: OutputExtractor.hpp:206
const FluidState & fs
Fluid state for cell.
Definition: OutputExtractor.hpp:209
const ElementContext & elemCtx
Definition: OutputExtractor.hpp:211
const IntensiveQuantities & intQuants
Intensive quantities for cell.
Definition: OutputExtractor.hpp:210
unsigned dofIdx
Definition: OutputExtractor.hpp:208
unsigned globalDofIdx
Global degree-of-freedom index.
Definition: OutputExtractor.hpp:207
Descriptor for extractor execution.
Definition: OutputExtractor.hpp:249
double * data
Where to store output data.
Definition: OutputExtractor.hpp:255
ScalarFunc extract
Extraction function to call.
Definition: OutputExtractor.hpp:256
Exec(double *d, ScalarFunc &&e)
Move constructor.
Definition: OutputExtractor.hpp:251
Definition: OutputExtractor.hpp:235
PhaseFunc extract
Associated extraction lambda.
Definition: OutputExtractor.hpp:241
std::variant< std::array< std::string_view, numPhases >, std::array< std::array< std::string_view, numPhases >, 2 > > kw
One or two lists of names for the keyword for each phase.
Definition: OutputExtractor.hpp:238
Definition: OutputExtractor.hpp:226
std::variant< std::string_view, std::vector< std::string_view > > kw
A single name or a list of names for the keyword.
Definition: OutputExtractor.hpp:228
ScalarFunc extract
Associated extraction lamda.
Definition: OutputExtractor.hpp:231
Wrapping struct holding types used for block-level data extraction.
Definition: OutputExtractor.hpp:193
std::unordered_map< int, std::vector< Exec > > ExecMap
A map of extraction executors, keyed by cartesian cell index.
Definition: OutputExtractor.hpp:260
GetPropType< TypeTag, Properties::ElementContext > ElementContext
Definition: OutputExtractor.hpp:194
typename IntensiveQuantities::FluidState FluidState
Definition: OutputExtractor.hpp:197
std::function< Scalar(const unsigned, const Context &)> PhaseFunc
Definition: OutputExtractor.hpp:223
GetPropType< TypeTag, Properties::FluidSystem > FluidSystem
Definition: OutputExtractor.hpp:198
std::variant< ScalarEntry, PhaseEntry > Entry
Descriptor for extractors.
Definition: OutputExtractor.hpp:245
static ExecMap setupExecMap(std::map< std::pair< std::string, int >, double > &blockData, const std::array< Entry, size > &handlers)
Setup an extractor executor map from a map of evaluations to perform.
Definition: OutputExtractor.hpp:264
std::function< Scalar(const Context &)> ScalarFunc
Definition: OutputExtractor.hpp:219
GetPropType< TypeTag, Properties::IntensiveQuantities > IntensiveQuantities
Definition: OutputExtractor.hpp:195
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: OutputExtractor.hpp:196
static void process(const std::vector< Exec > &blockExtractors, const Context &ectx)
Process a list of block extractors.
Definition: OutputExtractor.hpp:367
std::function< void(const Context &)> AssignFunc
Callback for extractors handling their own assignements.
Definition: OutputExtractor.hpp:215
Context passed to extractor functions.
Definition: OutputExtractor.hpp:74
const HysteresisParams & hParams
Hysteresis parameters for cell.
Definition: OutputExtractor.hpp:80
const IntensiveQuantities & intQuants
Intensive quantities for cell.
Definition: OutputExtractor.hpp:79
unsigned globalDofIdx
Global degree-of-freedom index.
Definition: OutputExtractor.hpp:75
const FluidState & fs
Fluid state for cell.
Definition: OutputExtractor.hpp:78
int episodeIndex
Current report step.
Definition: OutputExtractor.hpp:77
unsigned pvtRegionIdx
pvt region for dof
Definition: OutputExtractor.hpp:76
Descriptor for extractors.
Definition: OutputExtractor.hpp:113
bool condition
Additional condition for enabling extractor.
Definition: OutputExtractor.hpp:115
std::variant< AssignFunc, ScalarEntry, PhaseEntry > data
Extractor.
Definition: OutputExtractor.hpp:114
Struct holding hysteresis parameters.
Definition: OutputExtractor.hpp:63
Scalar somin
Min oil saturation.
Definition: OutputExtractor.hpp:69
Scalar swmin
Min water saturation.
Definition: OutputExtractor.hpp:66
Scalar swmax
Max water saturation.
Definition: OutputExtractor.hpp:65
Scalar shmax
Max something.
Definition: OutputExtractor.hpp:68
Scalar sgmax
Max gas saturation.
Definition: OutputExtractor.hpp:67
Scalar somax
Max oil saturation.
Definition: OutputExtractor.hpp:64
A phase buffer extractor descriptor.
Definition: OutputExtractor.hpp:106
PhaseArray * data
Array of buffers to store data in.
Definition: OutputExtractor.hpp:107
PhaseFunc extract
Function to call for extraction.
Definition: OutputExtractor.hpp:108
A scalar extractor descriptor.
Definition: OutputExtractor.hpp:99
ScalarBuffer * data
Buffer to store data in.
Definition: OutputExtractor.hpp:100
ScalarFunc extract
Function to call for extraction.
Definition: OutputExtractor.hpp:101
Wrapping struct holding types used for element-level data extraction.
Definition: OutputExtractor.hpp:54
std::array< ScalarBuffer, numPhases > PhaseArray
An array of buffers, one for each phase.
Definition: OutputExtractor.hpp:95
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: OutputExtractor.hpp:56
std::vector< Scalar > ScalarBuffer
A scalar buffer.
Definition: OutputExtractor.hpp:94
std::function< void(const Context &)> AssignFunc
Callback for extractors handling their own assignements.
Definition: OutputExtractor.hpp:84
GetPropType< TypeTag, Properties::FluidSystem > FluidSystem
Definition: OutputExtractor.hpp:58
std::function< Scalar(const unsigned, const Context &)> PhaseFunc
Definition: OutputExtractor.hpp:92
GetPropType< TypeTag, Properties::IntensiveQuantities > IntensiveQuantities
Definition: OutputExtractor.hpp:55
std::function< Scalar(const Context &)> ScalarFunc
Definition: OutputExtractor.hpp:88
static void process(const Context &ectx, const std::vector< Entry > &extractors)
Process the given extractor entries.
Definition: OutputExtractor.hpp:158
typename IntensiveQuantities::FluidState FluidState
Definition: OutputExtractor.hpp:57
static std::vector< Entry > removeInactive(std::array< Entry, size > &input)
Obtain vector of active extractors from an array of extractors.
Definition: OutputExtractor.hpp:120
static constexpr int numPhases
Definition: OutputExtractor.hpp:59