OpenLB 1.7
Loading...
Searching...
No Matches
CSVWriter.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2021 Simon Großmann
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#ifndef CSV_WRITER_HH
25#define CSV_WRITER_HH
26
27#include <fstream>
28#include <iostream>
29#include <unistd.h>
30#include "core/singleton.h"
31#include "io/fileName.h"
32#include "CSVWriter.h"
34
35namespace olb {
38template< typename T >
39CSV<T>::CSV(std::string name, char separator, std::vector<std::string> columnTags)
40 : _name(name),
41 _dataFile(singleton::directories().getGnuplotOutDir()+"data/"+_name+".dat"),
42 _dir(singleton::directories().getGnuplotOutDir()),
43 _separator(separator),
44 _columnTags(columnTags)
45{
46 if (singleton::mpi().getRank() == _rank) {
47 std::ofstream fout;
50 fout.open(_dataFile.c_str(), std::ios::trunc);
51 if(columnTags.size()>0)
52 {
53 setColumnTags(columnTags);
54 }
55 fout.close();
56 }
58
59template< typename T >
60CSV<T>::CSV(std::string name, std::vector<std::string> columnTags) : CSV(name, ' ', columnTags){}
62template< typename T >
63CSV<T>::CSV(std::string name) : CSV(name, ' ', std::vector<std::string> {}){}
64
65template< typename T >
66CSV<T>::CSV(std::string name, char separator) : CSV(name, separator, std::vector<std::string> {}){}
67
69template< typename T >
70void CSV<T>::writeDataFile(T xValue, const std::vector<T>& yValues,const std::string& plotNameFile, int precision)
71{
72 if (singleton::mpi().getRank() == _rank) {
73 std::ofstream fout;
74 std::string DATAF;
75 DATAF = singleton::directories().getGnuplotOutDir()+"data/"+plotNameFile+".dat";
76 fout.precision(precision);
77 fout.open(DATAF.c_str(), std::ios::out | std::ios::app);
78 fout << BaseType<T>(xValue);
79 for (unsigned int i = 0; i < yValues.size(); i++) {
80 fout << _separator << BaseType<T>(yValues[i]);
81 }
82 fout << std::endl;
83 fout.close();
84 }
85 return;
86}
87
88
90template< typename T >
91void CSV<T>::writeDataFile(T xValue, T yValue, const std::string& plotNameFile, int precision)
92{
93 std::vector<T> yValues{yValue};
94 writeDataFile(xValue, yValues, plotNameFile, precision);
95 return;
96}
97
98
100template< typename T >
101void CSV<T>::writeDataFile(T xValue, const std::vector<T>& yValues, int precision)
102{
103 writeDataFile(xValue, yValues, _name, precision);
104 return;
105}
106
108template< typename T >
109void CSV<T>::writeDataFile(T xValue, T yValue, int precision)
110{
111 std::vector<T> yValues{yValue};
112 writeDataFile(xValue, yValues, _name,precision);
113 return;
114}
115
118template< typename T >
119void CSV<T>::setColumnTags(const std::vector<std::string> columnTags, std::string& plotFileName)
120{
121 OstreamManager clout(std::cout,"setColumnTags");
122
123 std::ofstream fout(singleton::directories().getGnuplotOutDir() + "data/" + plotFileName + ".dat",std::ios::out);
124
125 if(fout.is_open())
126 {
127 unsigned int k = 0;
128 for(;k < columnTags.size() -1; k++)
129 {
130 fout << columnTags.at(k) << _separator;
131 }
133 fout << columnTags.at(k) << std::endl;
134 }
135 fout.close();
136}
137
138template< typename T >
139void CSV<T>::setColumnTags(const std::vector<std::string> columnTags)
140{
141 setColumnTags(columnTags, _name);
142}
143
144template< typename T >
145void CSV<T>::clearFile(std::string filename)
146{
148 std::ofstream fout(singleton::directories().getGnuplotOutDir() + "data/" + filename + ".dat",std::ios::out);
149 fout.close();
150}
151
152template< typename T>
154{
155 clearFile(_name);
156}
157
158
159} // namespace olb
160
161#endif
void clearFile()
Definition CSVWriter.hh:153
void writeDataFile(T xValue, T yValue, const std::string &plotFileName, int precision=16)
former datFileOut functions these functions create a datafile in the csv format in order to write in ...
Definition CSVWriter.hh:91
void setColumnTags(const std::vector< std::string > columnTags, std::string &plotFileName)
adds column tags at the beginning of the csv data file.
Definition CSVWriter.hh:119
CSV(std::string name, char separator, std::vector< std::string > columnTags)
Constructor with name for output file.
Definition CSVWriter.hh:39
class for marking output with some text
std::string getGnuplotOutDir() const
Definition singleton.h:101
These functions help you to create file names.
MpiManager & mpi()
Directories & directories()
Definition singleton.h:150
Top level namespace for all of OpenLB.
Definition of singletons: global, publicly available information.