OpenLB 1.8.1
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, std::string format)
40 : _name(name),
41 _dataFile(singleton::directories().getGnuplotOutDir()+"data/"+_name+format),
42 _dir(singleton::directories().getGnuplotOutDir()),
43 _separator(separator),
44 _columnTags(columnTags),
45 _format(format)
46{
47 if (singleton::mpi().getRank() == _rank) {
48 std::ofstream fout;
49
51 fout.open(_dataFile.c_str(), std::ios::trunc);
52 if(columnTags.size()>0)
53 {
54 setColumnTags(columnTags);
55 }
56 fout.close();
57 }
58}
59
60template< typename T >
61CSV<T>::CSV(std::string name, std::vector<std::string> columnTags) : CSV(name, ' ', columnTags){}
62
63template< typename T >
64CSV<T>::CSV(std::string name) : CSV(name, ' ', std::vector<std::string> {}){}
65
66template< typename T >
67CSV<T>::CSV(std::string name, char separator) : CSV(name, separator, std::vector<std::string> {}){}
68
70template< typename T >
71void CSV<T>::writeDataFile(T xValue, const std::vector<T>& yValues,const std::string& plotNameFile, int precision)
72{
73 if (singleton::mpi().getRank() == _rank) {
74 std::ofstream fout;
75 std::string DATAF;
76 DATAF = singleton::directories().getGnuplotOutDir()+"data/"+plotNameFile+_format;
77 fout.precision(precision);
78 fout.open(DATAF.c_str(), std::ios::out | std::ios::app);
79 fout << BaseType<T>(xValue);
80 for (unsigned int i = 0; i < yValues.size(); i++) {
81 fout << _separator << BaseType<T>(yValues[i]);
82 }
83 fout << std::endl;
84 fout.close();
85 }
86 return;
87}
88
89
91template< typename T >
92void CSV<T>::writeDataFile(T xValue, T yValue, const std::string& plotNameFile, int precision)
93{
94 std::vector<T> yValues{yValue};
95 writeDataFile(xValue, yValues, plotNameFile, precision);
96 return;
97}
98
99
101template< typename T >
102void CSV<T>::writeDataFile(T xValue, const std::vector<T>& yValues, int precision)
103{
104 writeDataFile(xValue, yValues, _name, precision);
105 return;
106}
107
109template< typename T >
110void CSV<T>::writeDataFile(T xValue, T yValue, int precision)
111{
112 std::vector<T> yValues{yValue};
113 writeDataFile(xValue, yValues, _name,precision);
114 return;
115}
116
119template< typename T >
120void CSV<T>::setColumnTags(const std::vector<std::string> columnTags, std::string& plotFileName)
121{
122 OstreamManager clout(std::cout,"setColumnTags");
123
124 std::ofstream fout(singleton::directories().getGnuplotOutDir() + "data/" + plotFileName + _format,std::ios::out);
125
126 if(fout.is_open())
127 {
128 unsigned int k = 0;
129 for(;k < columnTags.size() -1; k++)
130 {
131 fout << columnTags.at(k) << _separator;
132 }
134 fout << columnTags.at(k) << std::endl;
135 }
136 fout.close();
137}
138
139template< typename T >
140void CSV<T>::setColumnTags(const std::vector<std::string> columnTags)
141{
142 setColumnTags(columnTags, _name);
143}
144
145template< typename T >
146void CSV<T>::clearFile(std::string filename)
147{
149 std::ofstream fout(singleton::directories().getGnuplotOutDir() + "data/" + filename + _format,std::ios::out);
150 fout.close();
151}
152
153template< typename T>
155{
156 clearFile(_name);
157}
158
159
160} // namespace olb
161
162#endif
void clearFile()
Definition CSVWriter.hh:154
CSV(std::string name, char separator, std::vector< std::string > columnTags, std::string format=".dat")
Constructor with name for output file.
Definition CSVWriter.hh:39
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:92
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:120
class for marking output with some text
std::string getGnuplotOutDir() const
Definition singleton.h:107
These functions help you to create file names.
MpiManager & mpi()
Directories & directories()
Definition singleton.h:162
Top level namespace for all of OpenLB.
Definition of singletons: global, publicly available information.