opm-simulators
baseoutputmodule.hh
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 EWOMS_BASE_OUTPUT_MODULE_HH
28 #define EWOMS_BASE_OUTPUT_MODULE_HH
29 
30 #include <dune/common/fvector.hh>
31 
32 #include <dune/istl/bvector.hh>
33 
35 
37 
39 
43 
44 #include <algorithm>
45 #include <array>
46 #include <cstdio>
47 #include <stdexcept>
48 #include <string>
49 
50 namespace Opm::Properties {
51 
52 template <class TypeTag, class MyTypeTag>
53 struct FluidSystem;
54 
55 } // namespace Opm::Properties
56 
57 namespace Opm {
58 
66 template<class TypeTag>
68 {
75  using DiscBaseOutputModule = GetPropType<TypeTag, Properties::DiscBaseOutputModule>;
76 
77  enum { numPhases = getPropValue<TypeTag, Properties::NumPhases>() };
78  enum { numComponents = getPropValue<TypeTag, Properties::NumComponents>() };
79  enum { numEq = getPropValue<TypeTag, Properties::NumEq>() };
80  enum { dim = GridView::dimension };
81  enum { dimWorld = GridView::dimensionworld };
82  using Vector = BaseOutputWriter::Vector;
83  using Tensor = BaseOutputWriter::Tensor;
84 
85 public:
86  using ScalarBuffer = BaseOutputWriter::ScalarBuffer;
87  using VectorBuffer = BaseOutputWriter::VectorBuffer;
88  using TensorBuffer = BaseOutputWriter::TensorBuffer;
89 
90  using EqBuffer = std::array<ScalarBuffer, numEq>;
91  using PhaseBuffer = std::array<ScalarBuffer, numPhases>;
92  using ComponentBuffer = std::array<ScalarBuffer, numComponents>;
93  using PhaseComponentBuffer = std::array<std::array<ScalarBuffer, numComponents>, numPhases>;
94 
95  using PhaseVectorBuffer = std::array<VectorBuffer, numPhases>;
96 
97  BaseOutputModule(const Simulator& simulator)
98  : simulator_(simulator)
99  {}
100 
101  virtual ~BaseOutputModule()
102  {}
103 
112  virtual void allocBuffers() = 0;
113 
122  virtual void processElement(const ElementContext& elemCtx) = 0;
123 
127  virtual void commitBuffers(BaseOutputWriter& writer) = 0;
128 
139  virtual bool needExtensiveQuantities() const
140  { return false; }
141 
142 protected:
143  enum class BufferType {
145  Dof,
146 
148  Vertex,
149 
151  Element,
152  };
153 
157  void resizeScalarBuffer_(ScalarBuffer& buffer, BufferType bufferType)
158  {
159  buffer.resize(this->getBufferSize(bufferType));
160  std::ranges::fill(buffer, 0.0);
161  }
162 
166  void resizeTensorBuffer_(TensorBuffer& buffer, BufferType bufferType)
167  {
168  buffer.resize(this->getBufferSize(bufferType));
169  const Tensor nullMatrix(dimWorld, dimWorld, 0.0);
170  std::ranges::fill(buffer, nullMatrix);
171  }
172 
173  void resizeVectorBuffer_(VectorBuffer& buffer, BufferType bufferType)
174  {
175  buffer.resize(this->getBufferSize(bufferType));
176  Vector zerovector(dimWorld,0.0);
177  zerovector = 0.0;
178  std::ranges::fill(buffer, zerovector);
179  }
180 
185  void resizeEqBuffer_(EqBuffer& buffer, BufferType bufferType)
186  {
187  const std::size_t n = this->getBufferSize(bufferType);
188  for (unsigned i = 0; i < numEq; ++i) {
189  buffer[i].resize(n);
190  std::ranges::fill(buffer[i], 0.0);
191  }
192  }
193 
198  void resizePhaseBuffer_(PhaseBuffer& buffer, BufferType bufferType)
199  {
200  const std::size_t n = this->getBufferSize(bufferType);
201  for (unsigned i = 0; i < numPhases; ++i) {
202  buffer[i].resize(n);
203  std::ranges::fill(buffer[i], 0.0);
204  }
205  }
206 
211  void resizeComponentBuffer_(ComponentBuffer& buffer, BufferType bufferType)
212  {
213  const std::size_t n = this->getBufferSize(bufferType);
214  for (unsigned i = 0; i < numComponents; ++i) {
215  buffer[i].resize(n);
216  std::ranges::fill(buffer[i], 0.0);
217  }
218  }
219 
224  void resizePhaseComponentBuffer_(PhaseComponentBuffer& buffer, BufferType bufferType)
225  {
226  const std::size_t n = this->getBufferSize(bufferType);
227  for (unsigned i = 0; i < numPhases; ++i) {
228  for (unsigned j = 0; j < numComponents; ++j) {
229  buffer[i][j].resize(n);
230  std::ranges::fill(buffer[i][j], 0.0);
231  }
232  }
233  }
234 
239  const char *name,
240  ScalarBuffer& buffer,
241  BufferType bufferType)
242  {
243  switch (bufferType) {
244  case BufferType::Dof:
245  DiscBaseOutputModule::attachScalarDofData_(baseWriter, buffer, name);
246  break;
247  case BufferType::Vertex:
248  attachScalarVertexData_(baseWriter, buffer, name);
249  break;
250  case BufferType::Element:
251  attachScalarElementData_(baseWriter, buffer, name);
252  break;
253  default: throw std::logic_error("bufferType must be one of Dof, Vertex or Element");
254  }
255  }
256 
261  const char *name,
262  VectorBuffer& buffer,
263  BufferType bufferType)
264  {
265  switch (bufferType) {
266  case BufferType::Dof:
267  DiscBaseOutputModule::attachVectorDofData_(baseWriter, buffer, name);
268  break;
269  case BufferType::Vertex:
270  attachVectorVertexData_(baseWriter, buffer, name);
271  break;
272  case BufferType::Element:
273  attachVectorElementData_(baseWriter, buffer, name);
274  break;
275  default: throw std::logic_error("bufferType must be one of Dof, Vertex or Element");
276  }
277  }
278 
283  const char *name,
284  TensorBuffer& buffer,
285  BufferType bufferType)
286  {
287  switch (bufferType) {
288  case BufferType::Dof:
289  DiscBaseOutputModule::attachTensorDofData_(baseWriter, buffer, name);
290  break;
291  case BufferType::Vertex:
292  attachTensorVertexData_(baseWriter, buffer, name);
293  break;
294  case BufferType::Element:
295  attachTensorElementData_(baseWriter, buffer, name);
296  break;
297  default: throw std::logic_error("bufferType must be one of Dof, Vertex or Element");
298  }
299  }
300 
305  const char *pattern,
306  EqBuffer& buffer,
307  BufferType bufferType)
308  {
309  char name[512];
310  for (unsigned i = 0; i < numEq; ++i) {
311  const std::string eqName = simulator_.model().primaryVarName(i);
312  snprintf(name, 512, pattern, eqName.c_str());
313 
314  this->commitScalarBuffer_(baseWriter, name, buffer[i], bufferType);
315  }
316  }
317 
322  const char *pattern,
323  EqBuffer& buffer,
324  BufferType bufferType)
325  {
326  char name[512];
327  for (unsigned i = 0; i < numEq; ++i) {
328  snprintf(name, 512, pattern, std::to_string(i).c_str());
329 
330  this->commitScalarBuffer_(baseWriter, name, buffer[i], bufferType);
331  }
332  }
333 
338  const char *pattern,
339  PhaseBuffer& buffer,
340  BufferType bufferType)
341  {
342  char name[512];
343  for (unsigned i = 0; i < numPhases; ++i) {
344  snprintf(name, 512, pattern, FluidSystem::phaseName(i).data());
345 
346  this->commitScalarBuffer_(baseWriter, name, buffer[i], bufferType);
347  }
348  }
349 
354  const char *pattern,
355  ComponentBuffer& buffer,
356  BufferType bufferType)
357  {
358  char name[512];
359  for (unsigned i = 0; i < numComponents; ++i) {
360  snprintf(name, 512, pattern, FluidSystem::componentName(i).data());
361 
362  this->commitScalarBuffer_(baseWriter, name, buffer[i], bufferType);
363  }
364  }
365 
370  const char *pattern,
371  PhaseComponentBuffer& buffer,
372  BufferType bufferType)
373  {
374  char name[512];
375  for (unsigned i = 0; i < numPhases; ++i) {
376  for (unsigned j = 0; j < numComponents; ++j) {
377  snprintf(name, 512, pattern,
378  FluidSystem::phaseName(i).data(),
379  FluidSystem::componentName(j).data());
380 
381  this->commitScalarBuffer_(baseWriter, name, buffer[i][j], bufferType);
382  }
383  }
384  }
385 
386  void attachScalarElementData_(BaseOutputWriter& baseWriter,
387  ScalarBuffer& buffer,
388  const char *name)
389  { baseWriter.attachScalarElementData(buffer, name); }
390 
391  void attachScalarVertexData_(BaseOutputWriter& baseWriter,
392  ScalarBuffer& buffer,
393  const char *name)
394  { baseWriter.attachScalarVertexData(buffer, name); }
395 
396  void attachVectorElementData_(BaseOutputWriter& baseWriter,
397  VectorBuffer& buffer,
398  const char *name)
399  { baseWriter.attachVectorElementData(buffer, name); }
400 
401  void attachVectorVertexData_(BaseOutputWriter& baseWriter,
402  VectorBuffer& buffer,
403  const char *name)
404  { baseWriter.attachVectorVertexData(buffer, name); }
405 
406  void attachTensorElementData_(BaseOutputWriter& baseWriter,
407  TensorBuffer& buffer,
408  const char *name)
409  { baseWriter.attachTensorElementData(buffer, name); }
410 
411  void attachTensorVertexData_(BaseOutputWriter& baseWriter,
412  TensorBuffer& buffer,
413  const char *name)
414  { baseWriter.attachTensorVertexData(buffer, name); }
415 
416  std::size_t getBufferSize(BufferType bufferType) const
417  {
418  switch (bufferType) {
419  case BufferType::Vertex: return simulator_.gridView().size(dim);
420  case BufferType::Element: return simulator_.gridView().size(0);
421  case BufferType::Dof: return simulator_.model().numGridDof();
422  default: throw std::logic_error("bufferType must be one of Dof, Vertex or Element");
423  }
424  }
425 
426  const Simulator& simulator_;
427 };
428 
429 } // namespace Opm
430 
431 #endif
void commitPhaseComponentBuffer_(BaseOutputWriter &baseWriter, const char *pattern, PhaseComponentBuffer &buffer, BufferType bufferType)
Add a phase and component specific quantities to the output.
Definition: baseoutputmodule.hh:369
void commitPriVarsBuffer_(BaseOutputWriter &baseWriter, const char *pattern, EqBuffer &buffer, BufferType bufferType)
Add a buffer with as many variables as PDEs to the result file.
Definition: baseoutputmodule.hh:304
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
void commitTensorBuffer_(BaseOutputWriter &baseWriter, const char *name, TensorBuffer &buffer, BufferType bufferType)
Add a buffer containing tensorial quantities to the result file.
Definition: baseoutputmodule.hh:282
void resizePhaseBuffer_(PhaseBuffer &buffer, BufferType bufferType)
Allocate the space for a buffer storing a phase-specific quantity.
Definition: baseoutputmodule.hh:198
void commitScalarBuffer_(BaseOutputWriter &baseWriter, const char *name, ScalarBuffer &buffer, BufferType bufferType)
Add a buffer containing scalar quantities to the result file.
Definition: baseoutputmodule.hh:238
This file provides the infrastructure to retrieve run-time parameters.
The base class for writer modules.
Definition: baseoutputmodule.hh:67
virtual void processElement(const ElementContext &elemCtx)=0
Modify the internal buffers according to the intensive quanties relevant for an element.
Defines the common properties required by the porous medium multi-phase models.
BufferType
Definition: baseoutputmodule.hh:143
virtual void commitBuffers(BaseOutputWriter &writer)=0
Add all buffers to the VTK output writer.
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
void resizePhaseComponentBuffer_(PhaseComponentBuffer &buffer, BufferType bufferType)
Allocate the space for a buffer storing a phase and component specific buffer.
Definition: baseoutputmodule.hh:224
The base class for all output writers.
Definition: baseoutputwriter.hh:45
virtual bool needExtensiveQuantities() const
Returns true iff the module needs to access the extensive quantities of a context to do its job...
Definition: baseoutputmodule.hh:139
void resizeComponentBuffer_(ComponentBuffer &buffer, BufferType bufferType)
Allocate the space for a buffer storing a component specific quantity.
Definition: baseoutputmodule.hh:211
Declare the properties used by the infrastructure code of the finite volume discretizations.
Buffer contains data associated with the grid&#39;s elements.
void commitComponentBuffer_(BaseOutputWriter &baseWriter, const char *pattern, ComponentBuffer &buffer, BufferType bufferType)
Add a component-specific buffer to the result file.
Definition: baseoutputmodule.hh:353
The base class for all output writers.
Buffer contains data associated with the degrees of freedom.
void commitPhaseBuffer_(BaseOutputWriter &baseWriter, const char *pattern, PhaseBuffer &buffer, BufferType bufferType)
Add a phase-specific buffer to the result file.
Definition: baseoutputmodule.hh:337
void resizeEqBuffer_(EqBuffer &buffer, BufferType bufferType)
Allocate the space for a buffer storing a equation specific quantity.
Definition: baseoutputmodule.hh:185
void resizeTensorBuffer_(TensorBuffer &buffer, BufferType bufferType)
Allocate the space for a buffer storing a tensorial quantity.
Definition: baseoutputmodule.hh:166
void resizeScalarBuffer_(ScalarBuffer &buffer, BufferType bufferType)
Allocate the space for a buffer storing a scalar quantity.
Definition: baseoutputmodule.hh:157
The Opm property system, traits with inheritance.
void commitVectorBuffer_(BaseOutputWriter &baseWriter, const char *name, VectorBuffer &buffer, BufferType bufferType)
Add a buffer containing vectorial quantities to the result file.
Definition: baseoutputmodule.hh:260
void commitEqBuffer_(BaseOutputWriter &baseWriter, const char *pattern, EqBuffer &buffer, BufferType bufferType)
Add a buffer with as many variables as PDEs to the result file.
Definition: baseoutputmodule.hh:321
virtual void allocBuffers()=0
Allocate memory for the scalar fields we would like to write to disk.
virtual void attachScalarElementData(ScalarBuffer &buf, std::string_view name)=0
Add a scalar element centered quantity to the output.
Definition: blackoilmodel.hh:80
Defines a type tags and some fundamental properties all models.
Buffer contains data associated with the grid&#39;s vertices.