opm-simulators
vtktpsamodule.hpp
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  Copyright 2025 NORCE AS
5 
6  This file is part of the Open Porous Media project (OPM).
7 
8  OPM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 2 of the License, or
11  (at your option) any later version.
12 
13  OPM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OPM. If not, see <http://www.gnu.org/licenses/>.
20 
21  Consult the COPYING file in the top-level source directory of this
22  module for the precise wording of the license and the list of
23  copyright holders.
24 */
25 #ifndef VTK_TPSA_MODULE_HPP
26 #define VTK_TPSA_MODULE_HPP
27 
28 #include <dune/common/dynmatrix.hh>
29 #include <dune/common/fvector.hh>
30 
31 #include <opm/material/densead/Math.hpp>
32 
35 #include <opm/models/io/vtktpsaparams.hpp>
36 #include <opm/models/tpsa/tpsabaseproperties.hpp>
39 
40 namespace Opm {
41 
47 template <class TypeTag>
48 class VtkTpsaModule : public BaseOutputModule<TypeTag>
49 {
51 
56 
57  static constexpr auto vtkFormat = getPropValue<TypeTag, Properties::VtkOutputFormat>();
59 
60  enum { enableMech = getPropValue<TypeTag, Properties::EnableMech>() };
61 
62  using BufferType = typename ParentType::BufferType;
63  using ScalarBuffer = typename ParentType::ScalarBuffer;
64  using VectorBuffer = typename ParentType::VectorBuffer;
65  using TensorBuffer = typename ParentType::TensorBuffer;
66 
67  using SymTensor = Dune::FieldVector<Scalar, 6>;
68  using Tensor = Dune::DynamicMatrix<Scalar>;
69 
70 public:
76  explicit VtkTpsaModule(const Simulator& simulator)
77  : ParentType(simulator)
78  {
79  // Read runtime parameters
80  if constexpr(enableMech) {
81  params_.read();
82  }
83  }
84 
88  static void registerParameters()
89  {
90  if constexpr(enableMech) {
92  }
93  }
94 
98  void allocBuffers() override
99  {
100  // Check if vtk output is enabled
101  if (!Parameters::Get<Parameters::EnableVtkOutput>()) {
102  return;
103  }
104 
105  // Allocate buffers
106  if constexpr(enableMech) {
107  // Displacement
108  if (params_.displacementOutput_) {
109  this->resizeVectorBuffer_(displacement_, BufferType::Dof);
110  }
111 
112  // Rotation
113  if (params_.rotationOutput_) {
114  this->resizeVectorBuffer_(rotation_, BufferType::Dof);
115  }
116 
117  // Solid pressure
118  if (params_.solidPressureOutput_) {
119  this->resizeScalarBuffer_(solidPres_, BufferType::Dof);
120  }
121 
122  // Stress
123  if (params_.stressOutput_) {
124  this->resizeTensorBuffer_(stress_, BufferType::Dof);
125  }
126  }
127  }
128 
134  void processElement(const ElementContext& elemCtx) override
135  {
136  // Check if vtk output is enabled
137  if (!Parameters::Get<Parameters::EnableVtkOutput>()) {
138  return;
139  }
140 
141  if constexpr(enableMech) {
142  // Assign quantities from material state
143  const auto& problem = elemCtx.problem();
144  const auto& geoMechModel = problem.geoMechModel();
145  const auto& faceStressInfo = geoMechModel.linearizer().getStressInfo();
146  for (unsigned dofIdx = 0; dofIdx < elemCtx.numPrimaryDof(/*timeIdx=*/0); ++dofIdx) {
147  const unsigned globalDofIdx = elemCtx.globalSpaceIndex(dofIdx, /*timeIdx=*/0);
148  const auto& materialState = geoMechModel.materialState(globalDofIdx, /*timeIdx=*/0);
149 
150  // Loop over x-, y- and z-dir (corresponding to dirIdx = 0, 1, 2)
151  for (int dirIdx = 0; dirIdx < 3; ++dirIdx) {
152  // Displacement
153  if (params_.displacementOutput_) {
154  displacement_[globalDofIdx][dirIdx] = scalarValue(materialState.displacement(dirIdx));
155  }
156 
157  // Rotation
158  if (params_.rotationOutput_) {
159  rotation_[globalDofIdx][dirIdx] = scalarValue(materialState.rotation(dirIdx));
160  }
161  }
162 
163  // Solid pressure
164  if (params_.solidPressureOutput_) {
165  solidPres_[globalDofIdx] = scalarValue(materialState.solidPressure());
166  }
167 
168  // Stress
169  if (params_.stressOutput_ && !faceStressInfo.empty()) {
170  SymTensor stressSymTensor = geoMechModel.stress(globalDofIdx, false);
171  Tensor& stressTensor = stress_[globalDofIdx];
172  setTensorFromVoigt_(stressTensor, stressSymTensor);
173  }
174  }
175  }
176  }
177 
183  void commitBuffers(BaseOutputWriter& baseWriter) override
184  {
185  // Check if writer exists or mechanics output is enabled
186  if (!dynamic_cast<VtkMultiWriter*>(&baseWriter)) {
187  return;
188  }
189 
190  if constexpr(enableMech) {
191  // Displacement
192  if (params_.displacementOutput_) {
193  this->commitVectorBuffer_(baseWriter, "displacement", displacement_, BufferType::Dof);
194  }
195 
196  // Rotation
197  if (params_.rotationOutput_) {
198  this->commitVectorBuffer_(baseWriter, "rotation", rotation_, BufferType::Dof);
199  }
200 
201  // Solid pressure
202  if (params_.solidPressureOutput_) {
203  this->commitScalarBuffer_(baseWriter, "solid_pressure", solidPres_, BufferType::Dof);
204  }
205 
206  if (params_.stressOutput_) {
207  this->commitTensorBuffer_(baseWriter, "stress", stress_, BufferType::Dof);
208  }
209  }
210  }
211 
212 private:
213  static void setTensorFromVoigt_(Tensor& tensor, const SymTensor& symTensor)
214  {
215  // Diagonal terms
216  for (std::size_t i = 0; i < 3; ++i) {
217  tensor[i][i] = symTensor[i];
218  }
219 
220  // Off-diagonal terms
221  tensor[0][1] = symTensor[5];
222  tensor[0][2] = symTensor[4];
223  tensor[1][2] = symTensor[3];
224  for (std::size_t i = 0; i < 3; ++i) {
225  for (std::size_t j = 0; j < 3; ++j) {
226  if (i > j) {
227  tensor[i][j] = tensor[j][i];
228  }
229  }
230  }
231  }
232 
233  VtkTpsaParams params_{};
234 
235  VectorBuffer displacement_{};
236  VectorBuffer rotation_{};
237  ScalarBuffer solidPres_{};
238  TensorBuffer stress_{};
239 }; // class VtkTpsaModule
240 
241 } // namespace Opm
242 
243 #endif
VtkTpsaModule(const Simulator &simulator)
Constructor.
Definition: vtktpsamodule.hpp:76
VTK output module for TPSA quantities.
Definition: vtktpsamodule.hpp:48
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 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
The base class for writer modules.
BufferType
Definition: baseoutputmodule.hh:143
static void registerParameters()
Register runtime parameters.
Definition: vtktpsamodule.hpp:88
Simplifies writing multi-file VTK datasets.
Definition: vtkmultiwriter.hh:64
Structs needed for tpfalinearizer and its gpuparams struct extracted to be defined in one place that ...
Definition: blackoilbioeffectsmodules.hh:45
The base class for all output writers.
Definition: baseoutputwriter.hh:45
void commitBuffers(BaseOutputWriter &baseWriter) override
Add buffers to VTK writer.
Definition: vtktpsamodule.hpp:183
void read()
Read runtime parameters.
Definition: vtktpsaparams.cpp:49
static void registerParameters()
Register runtime parameters.
Definition: vtktpsaparams.cpp:34
void allocBuffers() override
Allocate memory for output.
Definition: vtktpsamodule.hpp:98
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.
Simplifies writing multi-file VTK datasets.
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 processElement(const ElementContext &elemCtx) override
Assign quantities to output buffers.
Definition: vtktpsamodule.hpp:134