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>
24
26
27#include <filesystem>
28#include <iomanip>
29#include <sstream>
30#include <string>
31
32namespace 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 std::ostringstream oss;
77 oss << "prob_" << simulator.episodeIndex()
78 << "_time_" << std::setprecision(15) << std::setw(12) << std::setfill('0') << simulator.time()
79 << "_nit_" << simulator.model().newtonMethod().numIterations()
80 << '_' << objName << "_istl";
81
82 const auto filename = (output_dir / oss.str()).generic_string();
83
84#if HAVE_MPI
85 if (comm != nullptr) {
86 Dune::storeMatrixMarket(linalgObject, filename, *comm);
87 }
88 else
89#endif // HAVE_MPI
90 {
91 Dune::storeMatrixMarket(linalgObject, filename + ".mm");
92 }
93 }
94 } // namespace detail
95
120 template <class SimulatorType, class VectorType, class Communicator>
121 void writeVector(const SimulatorType& simulator,
122 const VectorType& rhs,
123 const std::string& sysName,
124 const Communicator* comm)
125 {
126 detail::writeMatrixMarketObject(simulator, rhs, sysName + "_vector", comm);
127 }
128
153 template <class SimulatorType, class MatrixType, class Communicator>
154 void writeMatrix(const SimulatorType& simulator,
155 const MatrixType& matrix,
156 const std::string& sysName,
157 const Communicator* comm)
158 {
159 detail::writeMatrixMarketObject(simulator, matrix, sysName + "_matrix", comm);
160 }
161
195 template <class SimulatorType, class MatrixType, class VectorType, class Communicator>
196 void writeSystem(const SimulatorType& simulator,
197 const MatrixType& matrix,
198 const VectorType& rhs,
199 const std::string& sysName,
200 const Communicator* comm)
201 {
202 writeMatrix(simulator, matrix, sysName, comm);
203 writeVector(simulator, rhs, sysName, comm);
204 }
205
237 template <class SimulatorType, class MatrixType, class VectorType, class Communicator>
238 void writeSystem(const SimulatorType& simulator,
239 const MatrixType& matrix,
240 const VectorType& rhs,
241 const Communicator* comm)
242 {
243 writeSystem(simulator, matrix, rhs, "flow_", comm);
244 }
245
277 template <class SimulatorType, class MatrixType, class VectorType, class Communicator>
278 void writeMechSystem(const SimulatorType& simulator,
279 const MatrixType& matrix,
280 const VectorType& rhs,
281 const Communicator* comm)
282 {
283 writeSystem(simulator, matrix, rhs, "mech_", comm);
284 }
285
286} // namespace Opm::Helper
287
288#endif // OPM_WRITESYSTEMMATRIXHELPER_HEADER_INCLUDED
void writeMatrixMarketObject(const SimulatorType &simulator, const LinalgObjectType &linalgObject, const std::string &objName, const Communicator *comm)
Definition: WriteSystemMatrixHelper.hpp:63
Definition: WriteSystemMatrixHelper.hpp:32
void writeSystem(const SimulatorType &simulator, const MatrixType &matrix, const VectorType &rhs, const std::string &sysName, const Communicator *comm)
Definition: WriteSystemMatrixHelper.hpp:196
void writeMechSystem(const SimulatorType &simulator, const MatrixType &matrix, const VectorType &rhs, const Communicator *comm)
Definition: WriteSystemMatrixHelper.hpp:278
void writeVector(const SimulatorType &simulator, const VectorType &rhs, const std::string &sysName, const Communicator *comm)
Definition: WriteSystemMatrixHelper.hpp:121
void writeMatrix(const SimulatorType &simulator, const MatrixType &matrix, const std::string &sysName, const Communicator *comm)
Definition: WriteSystemMatrixHelper.hpp:154