exportSystem.hpp
Go to the documentation of this file.
1/*
2 Copyright 2025 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
20#ifndef OPM_EXPORT_SYSTEM_HEADER_INCLUDED
21#define OPM_EXPORT_SYSTEM_HEADER_INCLUDED
22
23#include <cstdio>
24#include <memory>
25
26namespace Opm
27{
28
29 // Forward declaring as they will be called from exportSystem().
30 template <class IstlMatrix>
31 void exportSparsity(const IstlMatrix& A, const char *path=".");
32 template <class IstlMatrix>
33 void exportNonzeros(const IstlMatrix& A, const char *tag="", const char *path=".");
34 template <class GlobalEqVector>
35 void exportVector(const GlobalEqVector& x, const char *tag="", const char *name="export/x");
36
40 template <class IstlMatrix, class GlobalEqVector>
41 void
42 exportSystem(const IstlMatrix& jacobian,
43 const GlobalEqVector& residual,
44 const bool export_sparsity,
45 const char* tag,
46 const char* path = "export")
47 {
48 // export sparsity only if requested
49 if (export_sparsity) {
50 exportSparsity(jacobian,path);
51 }
52
53 // export matrix
54 exportNonzeros(jacobian,tag,path);
55
56 // export residual
57 constexpr size_t bufsize = 256;
58 char name[bufsize];
59 std::snprintf(name,bufsize,"%s/r",path);
60 exportVector(residual,tag,name);
61 }
62
66 template <class GlobalEqVector>
67 void exportVector(const GlobalEqVector& x, const char *tag, const char *name)
68 {
69 // assume double precision and contiguous data
70 const double *data = &x[0][0];
71
72 constexpr size_t bufsize = 512;
73 char filename[bufsize];
74 std::snprintf(filename,bufsize,"%s%s.f64",name,tag);
75 FILE *out =fopen(filename,"w");
76 std::fwrite(data, sizeof(double), x.dim(),out);
77 std::fclose(out);
78 }
79
83 template <class IstlMatrix>
84 void exportNonzeros(const IstlMatrix& A, const char *tag, const char *path)
85 {
86 // assume double precision and contiguous data
87 const double *data = &A[0][0][0][0];
88 size_t dim = A[0][0].N()*A[0][0].M()*A.nonzeroes();
89
90 constexpr size_t bufsize = 256;
91 char filename[bufsize];
92 std::snprintf(filename,bufsize,"%s/data%s.f64",path,tag);
93 FILE *out =fopen(filename,"w");
94 std::fwrite(data, sizeof(double), dim,out);
95 std::fclose(out);
96 }
97
101 template <class IstlMatrix>
102 void exportSparsity(const IstlMatrix& A, const char *path)
103 {
104 //assemble csr graph
105 auto rows = std::make_unique<int[]>(A.N()+1);
106 auto cols = std::make_unique<int[]>(A.nonzeroes());
107
108 int irow=0;
109 int icol=0;
110 rows[0]=0;
111 for(auto row=A.begin(); row!=A.end(); row++)
112 {
113 for(unsigned int i=0;i<row->getsize();i++)
114 {
115 cols[icol++]=row->getindexptr()[i];
116 }
117 rows[irow+1]= rows[irow]+row->getsize();
118 irow++;
119 }
120
121 //export arrays
122 FILE *out;
123 constexpr size_t bufsize = 256;
124 char filename[bufsize];
125
126 std::snprintf(filename,bufsize,"%s/rows.i32",path);
127 out=std::fopen(filename,"w");
128 std::fwrite(rows.get(), sizeof(int), A.N()+1,out);
129 std::fclose(out);
130
131 std::snprintf(filename,bufsize,"%s/cols.i32",path);
132 out=std::fopen(filename,"w");
133 std::fwrite(cols.get(), sizeof(int), A.nonzeroes(),out);
134 std::fclose(out);
135 }
136
137} // namespace Opm
138
139#endif // OPM_EXPORT_SYSTEM_HEADER_INCLUDED
static constexpr int dim
Definition: structuredgridvanguard.hh:68
Definition: blackoilbioeffectsmodules.hh:43
void exportNonzeros(const IstlMatrix &A, const char *tag="", const char *path=".")
Export nonzero blocks of jacobian block-sparse matrix.
Definition: exportSystem.hpp:84
void exportSparsity(const IstlMatrix &A, const char *path=".")
Export sparsity pattern of jacobian block-sparse matrix.
Definition: exportSystem.hpp:102
void exportVector(const GlobalEqVector &x, const char *tag="", const char *name="export/x")
Export block vector.
Definition: exportSystem.hpp:67
void exportSystem(const IstlMatrix &jacobian, const GlobalEqVector &residual, const bool export_sparsity, const char *tag, const char *path="export")
Export blocks-sparse linear system.
Definition: exportSystem.hpp:42