My Project
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 #include <opm/simulators/linalg/MatrixMarketSpecializations.hpp>
25 
26 
27 namespace Opm
28 {
29 namespace Helper
30 {
31  template <class SimulatorType, class MatrixType, class VectorType, class Communicator>
32  void writeSystem(const SimulatorType& simulator,
33  const MatrixType& matrix,
34  const VectorType& rhs,
35  [[maybe_unused]] const Communicator* comm)
36  {
37  std::string dir = simulator.problem().outputDir();
38  if (dir == ".") {
39  dir = "";
40  } else if (!dir.empty() && dir.back() != '/') {
41  dir += "/";
42  }
43  namespace fs = ::Opm::filesystem;
44  fs::path output_dir(dir);
45  fs::path subdir("reports");
46  output_dir = output_dir / subdir;
47  if (!(fs::exists(output_dir))) {
48  fs::create_directory(output_dir);
49  }
50  // Combine and return.
51  std::ostringstream oss;
52  oss << "prob_" << simulator.episodeIndex() << "_time_";
53  oss << std::setprecision(15) << std::setw(12) << std::setfill('0') << simulator.time() << "_";
54  int nit = simulator.model().newtonMethod().numIterations();
55  oss << "_nit_" << nit << "_";
56  std::string output_file(oss.str());
57  fs::path full_path = output_dir / output_file;
58  std::string prefix = full_path.string();
59  {
60  std::string filename = prefix + "matrix_istl";
61 #if HAVE_MPI
62  if (comm != nullptr) { // comm is not set in serial runs
63  Dune::storeMatrixMarket(matrix, filename, *comm, true);
64  } else
65 #endif
66  {
67  Dune::storeMatrixMarket(matrix, filename + ".mm");
68  }
69  }
70  {
71  std::string filename = prefix + "rhs_istl";
72 #if HAVE_MPI
73  if (comm != nullptr) { // comm is not set in serial runs
74  Dune::storeMatrixMarket(rhs, filename, *comm, true);
75  } else
76 #endif
77  {
78  Dune::storeMatrixMarket(rhs, filename + ".mm");
79  }
80  }
81  }
82 
83 
84 } // namespace Helper
85 } // namespace Opm
86 #endif
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: BlackoilPhases.hpp:26