opm-simulators
WriteSystemMatrixHelper.hpp
1 /*
2  Copyright 2019 SINTEF Digital, Mathematics and Cybernetics.
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 
20 #ifndef OPM_WRITESYSTEMMATRIXHELPER_HEADER_INCLUDED
21 #define OPM_WRITESYSTEMMATRIXHELPER_HEADER_INCLUDED
22 
23 #include <dune/istl/matrixmarket.hh>
24 
25 #include <opm/simulators/linalg/MatrixMarketSpecializations.hpp>
26 
27 #include <filesystem>
28 #include <iomanip>
29 #include <sstream>
30 #include <string>
31 
32 namespace Opm::Helper {
33 
34  namespace detail {
62  template <class SimulatorType, class LinalgObjectType, class Communicator>
63  void writeMatrixMarketObject(const SimulatorType& simulator,
64  const LinalgObjectType& linalgObject,
65  const std::string& objName,
66  [[maybe_unused]] const Communicator* comm)
67  {
68  namespace fs = std::filesystem;
69 
70  const auto output_dir = fs::path {simulator.problem().outputDir()} / "reports";
71  if (! fs::exists(output_dir)) {
72  fs::create_directory(output_dir);
73  }
74 
75  // Combine and return.
76  const auto& iterCtx = simulator.problem().iterationContext();
77  std::ostringstream oss;
78  oss << "prob_" << simulator.episodeIndex()
79  << "_time_" << std::setprecision(15) << std::setw(12) << std::setfill('0') << simulator.time()
80  << "_nit_" << iterCtx.iteration()
81  << '_' << objName << "_istl";
82 
83  const auto filename = (output_dir / oss.str()).generic_string();
84 
85 #if HAVE_MPI
86  if (comm != nullptr) {
87  Dune::storeMatrixMarket(linalgObject, filename, *comm);
88  }
89  else
90 #endif // HAVE_MPI
91  {
92  Dune::storeMatrixMarket(linalgObject, filename + ".mm");
93  }
94  }
95  } // namespace detail
96 
121  template <class SimulatorType, class VectorType, class Communicator>
122  void writeVector(const SimulatorType& simulator,
123  const VectorType& rhs,
124  const std::string& sysName,
125  const Communicator* comm)
126  {
127  detail::writeMatrixMarketObject(simulator, rhs, sysName + "_vector", comm);
128  }
129 
154  template <class SimulatorType, class MatrixType, class Communicator>
155  void writeMatrix(const SimulatorType& simulator,
156  const MatrixType& matrix,
157  const std::string& sysName,
158  const Communicator* comm)
159  {
160  detail::writeMatrixMarketObject(simulator, matrix, sysName + "_matrix", comm);
161  }
162 
196  template <class SimulatorType, class MatrixType, class VectorType, class Communicator>
197  void writeSystem(const SimulatorType& simulator,
198  const MatrixType& matrix,
199  const VectorType& rhs,
200  const std::string& sysName,
201  const Communicator* comm)
202  {
203  writeMatrix(simulator, matrix, sysName, comm);
204  writeVector(simulator, rhs, sysName, comm);
205  }
206 
238  template <class SimulatorType, class MatrixType, class VectorType, class Communicator>
239  void writeSystem(const SimulatorType& simulator,
240  const MatrixType& matrix,
241  const VectorType& rhs,
242  const Communicator* comm)
243  {
244  writeSystem(simulator, matrix, rhs, "flow_", comm);
245  }
246 
278  template <class SimulatorType, class MatrixType, class VectorType, class Communicator>
279  void writeMechSystem(const SimulatorType& simulator,
280  const MatrixType& matrix,
281  const VectorType& rhs,
282  const Communicator* comm)
283  {
284  writeSystem(simulator, matrix, rhs, "mech_", comm);
285  }
286 
287 } // namespace Opm::Helper
288 
289 #endif // OPM_WRITESYSTEMMATRIXHELPER_HEADER_INCLUDED
Definition: WriteSystemMatrixHelper.hpp:32