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 "baseoutputwriter.hh"
31 
33 
35 
36 #include <opm/common/ErrorMacros.hpp>
37 #include <opm/common/Exceptions.hpp>
38 
39 #include <dune/istl/bvector.hh>
40 #include <dune/common/fvector.hh>
41 
42 #include <vector>
43 #include <sstream>
44 #include <string>
45 #include <array>
46 
47 #include <cstdio>
48 
49 namespace Ewoms {
50 namespace Properties {
51 // forward definition of property tags
52 NEW_PROP_TAG(NumPhases);
53 NEW_PROP_TAG(NumComponents);
54 NEW_PROP_TAG(NumEq);
55 
56 NEW_PROP_TAG(Model);
57 NEW_PROP_TAG(Simulator);
58 NEW_PROP_TAG(Scalar);
59 NEW_PROP_TAG(Evaluation);
60 NEW_PROP_TAG(GridView);
61 NEW_PROP_TAG(ElementContext);
62 NEW_PROP_TAG(FluidSystem);
63 NEW_PROP_TAG(DiscBaseOutputModule);
64 } // namespace Properties
65 
66 #if __GNUC__ || __clang__
67 #pragma GCC diagnostic push
68 #pragma GCC diagnostic ignored "-Wpragmas"
69 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
70 #endif
71 
79 template<class TypeTag>
81 {
82  typedef typename GET_PROP_TYPE(TypeTag, Simulator) Simulator;
83  typedef typename GET_PROP_TYPE(TypeTag, Model) Model;
84  typedef typename GET_PROP_TYPE(TypeTag, Scalar) Scalar;
85  typedef typename GET_PROP_TYPE(TypeTag, GridView) GridView;
86  typedef typename GET_PROP_TYPE(TypeTag, ElementContext) ElementContext;
87  typedef typename GET_PROP_TYPE(TypeTag, FluidSystem) FluidSystem;
88  typedef typename GET_PROP_TYPE(TypeTag, DiscBaseOutputModule) DiscBaseOutputModule;
89 
90  enum { numPhases = GET_PROP_VALUE(TypeTag, NumPhases) };
91  enum { numComponents = GET_PROP_VALUE(TypeTag, NumComponents) };
92  enum { numEq = GET_PROP_VALUE(TypeTag, NumEq) };
93  enum { dim = GridView::dimension };
94  enum { dimWorld = GridView::dimensionworld };
95 
96  typedef BaseOutputWriter::Tensor Tensor;
97 
98 public:
99  typedef BaseOutputWriter::ScalarBuffer ScalarBuffer;
100  typedef BaseOutputWriter::VectorBuffer VectorBuffer;
101  typedef BaseOutputWriter::TensorBuffer TensorBuffer;
102 
103  typedef std::array<ScalarBuffer, numEq> EqBuffer;
104  typedef std::array<ScalarBuffer, numPhases> PhaseBuffer;
105  typedef std::array<ScalarBuffer, numComponents> ComponentBuffer;
106  typedef std::array<std::array<ScalarBuffer, numComponents>, numPhases> PhaseComponentBuffer;
107 
108  typedef std::array<VectorBuffer, numPhases> PhaseVectorBuffer;
109 
110  BaseOutputModule(const Simulator& simulator)
111  : simulator_(simulator)
112  {}
113 
114  virtual ~BaseOutputModule()
115  {}
116 
125  virtual void allocBuffers() = 0;
126 
135  virtual void processElement(const ElementContext& elemCtx) = 0;
136 
140  virtual void commitBuffers(BaseOutputWriter& writer) = 0;
141 
152  virtual bool needExtensiveQuantities() const
153  { return false; }
154 
155 protected:
156  enum BufferType {
159 
162 
165  };
166 
170  void resizeScalarBuffer_(ScalarBuffer& buffer,
171  BufferType bufferType = DofBuffer)
172  {
173  size_t n;
174  if (bufferType == VertexBuffer)
175  n = static_cast<size_t>(simulator_.gridView().size(dim));
176  else if (bufferType == ElementBuffer)
177  n = static_cast<size_t>(simulator_.gridView().size(0));
178  else if (bufferType == DofBuffer)
179  n = simulator_.model().numGridDof();
180  else
181  OPM_THROW(std::logic_error, "bufferType must be one of Dof, Vertex or Element");
182 
183  buffer.resize(n);
184  std::fill(buffer.begin(), buffer.end(), 0.0);
185  }
186 
190  void resizeTensorBuffer_(TensorBuffer& buffer,
191  BufferType bufferType = DofBuffer)
192  {
193  size_t n;
194  if (bufferType == VertexBuffer)
195  n = static_cast<size_t>(simulator_.gridView().size(dim));
196  else if (bufferType == ElementBuffer)
197  n = static_cast<size_t>(simulator_.gridView().size(0));
198  else if (bufferType == DofBuffer)
199  n = simulator_.model().numGridDof();
200  else
201  OPM_THROW(std::logic_error, "bufferType must be one of Dof, Vertex or Element");
202 
203  buffer.resize(n);
204  Tensor nullMatrix(dimWorld, dimWorld, 0.0);
205  std::fill(buffer.begin(), buffer.end(), nullMatrix);
206  }
207 
212  void resizeEqBuffer_(EqBuffer& buffer,
213  BufferType bufferType = DofBuffer)
214  {
215  size_t n;
216  if (bufferType == VertexBuffer)
217  n = static_cast<size_t>(simulator_.gridView().size(dim));
218  else if (bufferType == ElementBuffer)
219  n = static_cast<size_t>(simulator_.gridView().size(0));
220  else if (bufferType == DofBuffer)
221  n = simulator_.model().numGridDof();
222  else
223  OPM_THROW(std::logic_error, "bufferType must be one of Dof, Vertex or Element");
224 
225  for (unsigned i = 0; i < numEq; ++i) {
226  buffer[i].resize(n);
227  std::fill(buffer[i].begin(), buffer[i].end(), 0.0);
228  }
229  }
230 
235  void resizePhaseBuffer_(PhaseBuffer& buffer,
236  BufferType bufferType = DofBuffer)
237  {
238  size_t n;
239  if (bufferType == VertexBuffer)
240  n = static_cast<size_t>(simulator_.gridView().size(dim));
241  else if (bufferType == ElementBuffer)
242  n = static_cast<size_t>(simulator_.gridView().size(0));
243  else if (bufferType == DofBuffer)
244  n = simulator_.model().numGridDof();
245  else
246  OPM_THROW(std::logic_error, "bufferType must be one of Dof, Vertex or Element");
247 
248  for (unsigned i = 0; i < numPhases; ++i) {
249  buffer[i].resize(n);
250  std::fill(buffer[i].begin(), buffer[i].end(), 0.0);
251  }
252  }
253 
258  void resizeComponentBuffer_(ComponentBuffer& buffer,
259  BufferType bufferType = DofBuffer)
260  {
261  size_t n;
262  if (bufferType == VertexBuffer)
263  n = static_cast<size_t>(simulator_.gridView().size(dim));
264  else if (bufferType == ElementBuffer)
265  n = static_cast<size_t>(simulator_.gridView().size(0));
266  else if (bufferType == DofBuffer)
267  n = simulator_.model().numGridDof();
268  else
269  OPM_THROW(std::logic_error, "bufferType must be one of Dof, Vertex or Element");
270 
271  for (unsigned i = 0; i < numComponents; ++i) {
272  buffer[i].resize(n);
273  std::fill(buffer[i].begin(), buffer[i].end(), 0.0);
274  }
275  }
276 
281  void resizePhaseComponentBuffer_(PhaseComponentBuffer& buffer,
282  BufferType bufferType = DofBuffer)
283  {
284  size_t n;
285  if (bufferType == VertexBuffer)
286  n = static_cast<size_t>(simulator_.gridView().size(dim));
287  else if (bufferType == ElementBuffer)
288  n = static_cast<size_t>(simulator_.gridView().size(0));
289  else if (bufferType == DofBuffer)
290  n = simulator_.model().numGridDof();
291  else
292  OPM_THROW(std::logic_error, "bufferType must be one of Dof, Vertex or Element");
293 
294  for (unsigned i = 0; i < numPhases; ++i) {
295  for (unsigned j = 0; j < numComponents; ++j) {
296  buffer[i][j].resize(n);
297  std::fill(buffer[i][j].begin(), buffer[i][j].end(), 0.0);
298  }
299  }
300  }
301 
306  const char *name,
307  ScalarBuffer& buffer,
308  BufferType bufferType = DofBuffer)
309  {
310  if (bufferType == DofBuffer)
311  DiscBaseOutputModule::attachScalarDofData_(baseWriter, buffer, name);
312  else if (bufferType == VertexBuffer)
313  attachScalarVertexData_(baseWriter, buffer, name);
314  else if (bufferType == ElementBuffer)
315  attachScalarElementData_(baseWriter, buffer, name);
316  else
317  OPM_THROW(std::logic_error, "bufferType must be one of Dof, Vertex or Element");
318  }
319 
324  const char *name,
325  VectorBuffer& buffer,
326  BufferType bufferType = DofBuffer)
327  {
328  if (bufferType == DofBuffer)
329  DiscBaseOutputModule::attachVectorDofData_(baseWriter, buffer, name);
330  else if (bufferType == VertexBuffer)
331  attachVectorVertexData_(baseWriter, buffer, name);
332  else if (bufferType == ElementBuffer)
333  attachVectorElementData_(baseWriter, buffer, name);
334  else
335  OPM_THROW(std::logic_error, "bufferType must be one of Dof, Vertex or Element");
336  }
337 
342  const char *name,
343  TensorBuffer& buffer,
344  BufferType bufferType = DofBuffer)
345  {
346  if (bufferType == DofBuffer)
347  DiscBaseOutputModule::attachTensorDofData_(baseWriter, buffer, name);
348  else if (bufferType == VertexBuffer)
349  attachTensorVertexData_(baseWriter, buffer, name);
350  else if (bufferType == ElementBuffer)
351  attachTensorElementData_(baseWriter, buffer, name);
352  else
353  OPM_THROW(std::logic_error, "bufferType must be one of Dof, Vertex or Element");
354  }
355 
360  const char *pattern,
361  EqBuffer& buffer,
362  BufferType bufferType = DofBuffer)
363  {
364  char name[512];
365  for (unsigned i = 0; i < numEq; ++i) {
366  std::string eqName = simulator_.model().primaryVarName(i);
367  snprintf(name, 512, pattern, eqName.c_str());
368 
369  if (bufferType == DofBuffer)
370  DiscBaseOutputModule::attachScalarDofData_(baseWriter, buffer[i], name);
371  else if (bufferType == VertexBuffer)
372  attachScalarVertexData_(baseWriter, buffer[i], name);
373  else if (bufferType == ElementBuffer)
374  attachScalarElementData_(baseWriter, buffer[i], name);
375  else
376  OPM_THROW(std::logic_error, "bufferType must be one of Dof, Vertex or Element");
377  }
378  }
379 
384  const char *pattern,
385  EqBuffer& buffer,
386  BufferType bufferType = DofBuffer)
387  {
388  char name[512];
389  for (unsigned i = 0; i < numEq; ++i) {
390  std::ostringstream oss;
391  oss << i;
392  snprintf(name, 512, pattern, oss.str().c_str());
393 
394  if (bufferType == DofBuffer)
395  DiscBaseOutputModule::attachScalarDofData_(baseWriter, buffer[i], name);
396  else if (bufferType == VertexBuffer)
397  attachScalarVertexData_(baseWriter, buffer[i], name);
398  else if (bufferType == ElementBuffer)
399  attachScalarElementData_(baseWriter, buffer[i], name);
400  else
401  OPM_THROW(std::logic_error, "bufferType must be one of Dof, Vertex or Element");
402  }
403  }
404 
409  const char *pattern,
410  PhaseBuffer& buffer,
411  BufferType bufferType = DofBuffer)
412  {
413  char name[512];
414  for (unsigned i = 0; i < numPhases; ++i) {
415  snprintf(name, 512, pattern, FluidSystem::phaseName(i));
416 
417  if (bufferType == DofBuffer)
418  DiscBaseOutputModule::attachScalarDofData_(baseWriter, buffer[i], name);
419  else if (bufferType == VertexBuffer)
420  attachScalarVertexData_(baseWriter, buffer[i], name);
421  else if (bufferType == ElementBuffer)
422  attachScalarElementData_(baseWriter, buffer[i], name);
423  else
424  OPM_THROW(std::logic_error, "bufferType must be one of Dof, Vertex or Element");
425  }
426  }
427 
432  const char *pattern,
433  ComponentBuffer& buffer,
434  BufferType bufferType = DofBuffer)
435  {
436  char name[512];
437  for (unsigned i = 0; i < numComponents; ++i) {
438  snprintf(name, 512, pattern, FluidSystem::componentName(i));
439 
440  if (bufferType == DofBuffer)
441  DiscBaseOutputModule::attachScalarDofData_(baseWriter, buffer[i], name);
442  else if (bufferType == VertexBuffer)
443  attachScalarVertexData_(baseWriter, buffer[i], name);
444  else if (bufferType == ElementBuffer)
445  attachScalarElementData_(baseWriter, buffer[i], name);
446  else
447  OPM_THROW(std::logic_error, "bufferType must be one of Dof, Vertex or Element");
448  }
449  }
450 
455  const char *pattern,
456  PhaseComponentBuffer& buffer,
457  BufferType bufferType = DofBuffer)
458  {
459  char name[512];
460  for (unsigned i= 0; i < numPhases; ++i) {
461  for (unsigned j = 0; j < numComponents; ++j) {
462  snprintf(name, 512, pattern,
463  FluidSystem::phaseName(i),
464  FluidSystem::componentName(j));
465 
466  if (bufferType == DofBuffer)
467  DiscBaseOutputModule::attachScalarDofData_(baseWriter, buffer[i][j], name);
468  else if (bufferType == VertexBuffer)
469  attachScalarVertexData_(baseWriter, buffer[i][j], name);
470  else if (bufferType == ElementBuffer)
471  attachScalarElementData_(baseWriter, buffer[i][j], name);
472  else
473  OPM_THROW(std::logic_error,
474  "bufferType must be one of Dof, Vertex or Element");
475  }
476  }
477  }
478 
479  void attachScalarElementData_(BaseOutputWriter& baseWriter,
480  ScalarBuffer& buffer,
481  const char *name)
482  { baseWriter.attachScalarElementData(buffer, name); }
483 
484  void attachScalarVertexData_(BaseOutputWriter& baseWriter,
485  ScalarBuffer& buffer,
486  const char *name)
487  { baseWriter.attachScalarVertexData(buffer, name); }
488 
489  void attachVectorElementData_(BaseOutputWriter& baseWriter,
490  VectorBuffer& buffer,
491  const char *name)
492  { baseWriter.attachVectorElementData(buffer, name); }
493 
494  void attachVectorVertexData_(BaseOutputWriter& baseWriter,
495  VectorBuffer& buffer,
496  const char *name)
497  { baseWriter.attachVectorVertexData(buffer, name); }
498 
499  void attachTensorElementData_(BaseOutputWriter& baseWriter,
500  TensorBuffer& buffer,
501  const char *name)
502  { baseWriter.attachTensorElementData(buffer, name); }
503 
504  void attachTensorVertexData_(BaseOutputWriter& baseWriter,
505  TensorBuffer& buffer,
506  const char *name)
507  { baseWriter.attachTensorVertexData(buffer, name); }
508 
509  const Simulator& simulator_;
510 };
511 
512 #if __GNUC__ || __clang__
513 #pragma GCC diagnostic pop
514 #endif
515 
516 } // namespace Ewoms
517 
518 #endif
virtual void allocBuffers()=0
Allocate memory for the scalar fields we would like to write to disk.
void commitEqBuffer_(BaseOutputWriter &baseWriter, const char *pattern, EqBuffer &buffer, BufferType bufferType=DofBuffer)
Add a buffer with as many variables as PDEs to the result file.
Definition: baseoutputmodule.hh:383
The base class for all output writers.
Definition: baseoutputwriter.hh:43
Definition: baseauxiliarymodule.hh:37
Model & model()
Return the physical model used in the simulation.
Definition: simulator.hh:189
void commitPhaseBuffer_(BaseOutputWriter &baseWriter, const char *pattern, PhaseBuffer &buffer, BufferType bufferType=DofBuffer)
Add a phase-specific buffer to the result file.
Definition: baseoutputmodule.hh:408
void commitVectorBuffer_(BaseOutputWriter &baseWriter, const char *name, VectorBuffer &buffer, BufferType bufferType=DofBuffer)
Add a buffer containing vectorial quantities to the result file.
Definition: baseoutputmodule.hh:323
void commitScalarBuffer_(BaseOutputWriter &baseWriter, const char *name, ScalarBuffer &buffer, BufferType bufferType=DofBuffer)
Add a buffer containing scalar quantities to the result file.
Definition: baseoutputmodule.hh:305
#define GET_PROP_VALUE(TypeTag, PropTagName)
Access the value attribute of a property for a type tag.
Definition: propertysystem.hh:469
virtual void processElement(const ElementContext &elemCtx)=0
Modify the internal buffers according to the intensive quanties relevant for an element.
Buffer contains data associated with the degrees of freedom.
Definition: baseoutputmodule.hh:158
void resizeTensorBuffer_(TensorBuffer &buffer, BufferType bufferType=DofBuffer)
Allocate the space for a buffer storing a tensorial quantity.
Definition: baseoutputmodule.hh:190
virtual void attachScalarElementData(ScalarBuffer &buf, std::string name)=0
Add a scalar element centered quantity to the output.
void commitTensorBuffer_(BaseOutputWriter &baseWriter, const char *name, TensorBuffer &buffer, BufferType bufferType=DofBuffer)
Add a buffer containing tensorial quantities to the result file.
Definition: baseoutputmodule.hh:341
BufferType
Definition: baseoutputmodule.hh:156
virtual void commitBuffers(BaseOutputWriter &writer)=0
Add all buffers to the VTK output writer.
void commitPriVarsBuffer_(BaseOutputWriter &baseWriter, const char *pattern, EqBuffer &buffer, BufferType bufferType=DofBuffer)
Add a buffer with as many variables as PDEs to the result file.
Definition: baseoutputmodule.hh:359
Buffer contains data associated with the grid&#39;s vertices.
Definition: baseoutputmodule.hh:161
Buffer contains data associated with the grid&#39;s elements.
Definition: baseoutputmodule.hh:164
void resizeEqBuffer_(EqBuffer &buffer, BufferType bufferType=DofBuffer)
Allocate the space for a buffer storing a equation specific quantity.
Definition: baseoutputmodule.hh:212
This file provides the infrastructure to retrieve run-time parameters.
void resizeComponentBuffer_(ComponentBuffer &buffer, BufferType bufferType=DofBuffer)
Allocate the space for a buffer storing a component specific quantity.
Definition: baseoutputmodule.hh:258
The base class for writer modules.
Definition: baseoutputmodule.hh:80
void resizePhaseComponentBuffer_(PhaseComponentBuffer &buffer, BufferType bufferType=DofBuffer)
Allocate the space for a buffer storing a phase and component specific buffer.
Definition: baseoutputmodule.hh:281
const GridView & gridView() const
Return the grid view for which the simulation is done.
Definition: simulator.hh:183
void commitComponentBuffer_(BaseOutputWriter &baseWriter, const char *pattern, ComponentBuffer &buffer, BufferType bufferType=DofBuffer)
Add a component-specific buffer to the result file.
Definition: baseoutputmodule.hh:431
The base class for all output writers.
void resizePhaseBuffer_(PhaseBuffer &buffer, BufferType bufferType=DofBuffer)
Allocate the space for a buffer storing a phase-specific quantity.
Definition: baseoutputmodule.hh:235
Provides the magic behind the eWoms property system.
void resizeScalarBuffer_(ScalarBuffer &buffer, BufferType bufferType=DofBuffer)
Allocate the space for a buffer storing a scalar quantity.
Definition: baseoutputmodule.hh:170
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:152
Manages the initializing and running of time dependent problems.
Definition: simulator.hh:75
#define NEW_PROP_TAG(PTagName)
Define a property tag.
Definition: propertysystem.hh:247
void commitPhaseComponentBuffer_(BaseOutputWriter &baseWriter, const char *pattern, PhaseComponentBuffer &buffer, BufferType bufferType=DofBuffer)
Add a phase and component specific quantities to the output.
Definition: baseoutputmodule.hh:454