OpenLB 1.7
Loading...
Searching...
No Matches
plainWriter.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2022 Nicolas Hafen, Mathias J. Krause
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24
25#ifndef PLAIN_WRITER_HH
26#define PLAIN_WRITER_HH
27
28namespace olb {
29
30//Write scalar data (single core only)
31template <typename ARRAYTYPE>
32void writeScalarData( std::ofstream& dataWriterOpened,
33 std::string fullFileName, std::string headLine,
34 ARRAYTYPE& dataVector, int iE, int iEinit )
35{
36 //Write headline if first element
37 if (iE == iEinit){
38 dataWriterOpened << headLine << std::endl;
39 }
40 //Write Data
41 dataWriterOpened << dataVector[0];
42 for (unsigned int i=1; i<dataVector.size(); ++i){
43 dataWriterOpened << " " << dataVector[i];
44 }
45 dataWriterOpened << std::endl;
46}
47
48//Write scalar data
49template <typename ARRAYTYPE>
50void writeScalarData( std::string fullFileName, std::string headLine,
51 ARRAYTYPE& dataVector, int iE, int iEinit )
52{
53#ifdef PARALLEL_MODE_MPI
54 if (singleton::mpi().isMainProcessor()){
55#endif
56 //Instantiate data writer
57 std::ofstream dataWriter;
58 dataWriter.open( fullFileName, std::ofstream::app );
59 //Write scalar data
60 writeScalarData( dataWriter, fullFileName, headLine,
61 dataVector, iE, iEinit );
62 //Close File
63 dataWriter.close();
64#ifdef PARALLEL_MODE_MPI
65 }
66#endif
67}
68
69//Write scalar data (including sanity check)
70template <typename ARRAYTYPE>
71void writeScalarData( std::string fullFileName, std::vector<std::string>& headLineVector,
72 ARRAYTYPE& dataVector, int iT, int iTinit )
73{
74 //Perform sanity check
75#ifdef PARALLEL_MODE_MPI
76 if (singleton::mpi().isMainProcessor()){
77#endif
78 if (headLineVector.size()!=dataVector.size()){
79 std::cerr << "WARNING (" << fullFileName << "): DataVector does not match provided headline!" << std::endl;
80 }
81#ifdef PARALLEL_MODE_MPI
82 }
83#endif
84 //Set up headLine string
85 std::string headLineStringScalar;
86 for ( unsigned int iQ=0; iQ<headLineVector.size(); ++iQ ){
87 if (iQ>0){ headLineStringScalar+=" "; };
88 headLineStringScalar += headLineVector[iQ];
89 }
90 //Call write scalar data
91 writeScalarData( fullFileName, headLineStringScalar, dataVector, iT, iTinit );
92}
93
94
95
96
97//Write array data
98void writeArrayData( std::string fullFileName, std::string headLine,
99 std::vector<std::string>& dataVector )
100{
101#ifdef PARALLEL_MODE_MPI
102 if (singleton::mpi().isMainProcessor()){
103#endif
104 //Instantiate data writer
105 std::ofstream dataWriter;
106 dataWriter.open( fullFileName );
107 //Write headline
108 dataWriter << headLine << std::endl;
109 //Write Data
110 for (unsigned int i=0; i<dataVector.size(); ++i){
111 dataWriter << dataVector[i] << std::endl;
112 }
113 //Close File
114 dataWriter.close();
115#ifdef PARALLEL_MODE_MPI
116 }
117#endif
118}
119
120//Write array data
121template <typename ARRAYTYPE>
122void writeArrayData( std::string fullFileName, std::string headLine,
123 std::vector<ARRAYTYPE>& dataVector )
124{
125#ifdef PARALLEL_MODE_MPI
126 if (singleton::mpi().isMainProcessor()){
127#endif
128 //Instantiate data writer
129 std::ofstream dataWriter;
130 dataWriter.open( fullFileName );
131 //Write scalar data for each array entry
132 for (unsigned int i=0; i<dataVector.size(); ++i){
133 writeScalarData( dataWriter, fullFileName, headLine, dataVector[i], i );
134 }
135 //Close File
136 dataWriter.close();
137#ifdef PARALLEL_MODE_MPI
138 }
139#endif
140}
141
142//Write array data (including sanity check)
143template <typename ARRAYTYPE>
144void writeArrayData( std::string fullFileName, std::vector<std::string>& headLineVector,
145 std::vector<ARRAYTYPE>& dataVector )
146{
147 //Perform sanity check
148#ifdef PARALLEL_MODE_MPI
149 if (singleton::mpi().isMainProcessor()){
150#endif
151 if (dataVector.size()==0){
152 std::cerr << "WARNING: DataVector is empty!" << std::endl;
153 } else {
154 if (headLineVector.size()!=dataVector[0].size()){
155 std::cerr << "WARNING (" << fullFileName << "): DataVector does not match provided headline!" << std::endl;
156 }
157 }
158#ifdef PARALLEL_MODE_MPI
159 }
160#endif
161 //Set up headLine string
162 std::string headLineStringArray;
163 for ( unsigned int iQ=0; iQ<headLineVector.size(); ++iQ ){
164 if (iQ>0){ headLineStringArray+=" "; };
165 headLineStringArray += headLineVector[iQ];
166 }
167 //Call write array data
168 writeArrayData( fullFileName, headLineStringArray, dataVector);
169}
170
171} //namespace olb
172
173#endif
MpiManager & mpi()
Top level namespace for all of OpenLB.
void writeArrayData(std::string fullFileName, std::string headLine, std::vector< std::string > &dataVector)
Write array data.
void writeScalarData(std::ofstream &dataWriterOpened, std::string fullFileName, std::string headLine, ARRAYTYPE &dataVector, int iE, int iE0=0)
Write functions for scalar and array data.