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