opm-simulators
PyMaterialState_impl.hpp
1 /*
2  Copyright 2020 Equinor ASA.
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 3 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 #ifndef OPM_PY_MATERIAL_STATE_IMPL_HEADER_INCLUDED
20 #define OPM_PY_MATERIAL_STATE_IMPL_HEADER_INCLUDED
21 
22 // Improve IDE experience
23 #ifndef OPM_PY_MATERIAL_STATE_HEADER_INCLUDED
24 #include <config.h>
25 #include <opm/simulators/flow/python/PyMaterialState.hpp>
26 #endif
27 
28 #include <fmt/format.h>
29 
30 namespace Opm::Pybind {
31 
32 template <class TypeTag>
33 std::vector<double>
34 PyMaterialState<TypeTag>::
35 getCellVolumes()
36 {
37  Model& model = this->simulator_->model();
38  const auto size = model.numGridDof();
39  std::vector<double> array(size);
40  for (unsigned dof_idx = 0; dof_idx < size; ++dof_idx) {
41  array[dof_idx] = model.dofTotalVolume(dof_idx);
42  }
43  return array;
44 }
45 
46 template <class TypeTag>
47 std::vector<double>
48 PyMaterialState<TypeTag>::
49 getPorosity()
50 {
51  Problem& problem = this->simulator_->problem();
52  Model& model = this->simulator_->model();
53  const auto size = model.numGridDof();
54  std::vector<double> array(size);
55  for (unsigned dof_idx = 0; dof_idx < size; ++dof_idx) {
56  array[dof_idx] = problem.referencePorosity(dof_idx, /*timeIdx*/0);
57  }
58  return array;
59 }
60 
61 template <class TypeTag>
62 void
63 PyMaterialState<TypeTag>::
64 setPorosity(const double* poro, std::size_t size)
65 {
66  Problem& problem = this->simulator_->problem();
67  Model& model = this->simulator_->model();
68  const auto model_size = model.numGridDof();
69  if (model_size != size) {
70  const std::string msg = fmt::format(
71  "Cannot set porosity. Expected array of size: {}, got array of size: ",
72  model_size, size
73  );
74  throw std::runtime_error(msg);
75  }
76  for (unsigned dof_idx = 0; dof_idx < size; ++dof_idx) {
77  problem.setPorosity(poro[dof_idx], dof_idx);
78  }
79 }
80 
81 } // namespace Opm::Pybind
82 
83 #endif // OPM_PY_MATERIAL_STATE_IMPL_HEADER_INCLUDED
Definition: PyBaseSimulator.hpp:41