From 9c4c15e2bd72566f58246bea32240712f363b4a4 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm <philipp@uvos.xyz> Date: Fri, 24 Feb 2023 09:59:21 +0100 Subject: [PATCH] Eistype: improve loading and saveing functions --- eisgenerator/eistype.h | 15 +++++++++++++-- eisgenerator_plot | 12 +++++++++++- eistype.cpp | 35 +++++++++++++++++++---------------- main.cpp | 2 +- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/eisgenerator/eistype.h b/eisgenerator/eistype.h index 116ef3e..a3a2060 100644 --- a/eisgenerator/eistype.h +++ b/eisgenerator/eistype.h @@ -3,6 +3,7 @@ #include <vector> #include <cassert> #include <cmath> +#include <filesystem> typedef double fvalue; @@ -134,9 +135,19 @@ public: } }; -bool saveToDisk(const std::vector<DataPoint>& data, const std::string& fileName, std::string headStr = ""); +struct EisSpectra +{ + std::vector<DataPoint> data; + std::string model; + std::string header; + EisSpectra(const std::vector<DataPoint>& dataIn, const std::string& modelIn, const std::string& headerIn): + data(dataIn), model(modelIn), header(headerIn) {} + EisSpectra(){} +}; + +bool saveToDisk(const EisSpectra& data, const std::filesystem::path& path); -std::pair<std::vector<DataPoint>, std::string> loadFromDisk(const std::string& fileName); +EisSpectra loadFromDisk(const std::filesystem::path& path); fvalue eisDistance(const std::vector<eis::DataPoint>& a, const std::vector<eis::DataPoint>& b); diff --git a/eisgenerator_plot b/eisgenerator_plot index 57eeb0c..6ca0ce9 100755 --- a/eisgenerator_plot +++ b/eisgenerator_plot @@ -10,4 +10,14 @@ then exit fi -eisgenerator_export -q "$@" | gnuplot -p -e "set datafile separator ','; plot '-' using 2:3 w l" +eisgenerator_export -q "$@" | gnuplot -p -e "\ +set terminal qt enhanced font \",15\" title \"EIS Simulation\"; \ +set style line 1 lw 3 lc \"blue\"; \ +set ylabel 'Z_{im}'; \ +set xlabel 'Z_{re}'; \ +set tics font \"Helvetica,12\"; \ +set xlabel font \"Helvetica,15\"; \ +set ylabel font \"Helvetica,15\"; \ +set datafile separator ','; \ +plot '-' using 2:3 notitle w l ls 1; +" diff --git a/eistype.cpp b/eistype.cpp index bf4fce1..2631434 100644 --- a/eistype.cpp +++ b/eistype.cpp @@ -8,49 +8,52 @@ using namespace eis; -bool eis::saveToDisk(const std::vector<DataPoint>& data, const std::string& fileName, std::string headStr) +bool eis::saveToDisk(const EisSpectra& data, const std::filesystem::path& path) { std::fstream file; - file.open(fileName, std::ios_base::out | std::ios_base::trunc); + file.open(path, std::ios_base::out | std::ios_base::trunc); if(!file.is_open()) { - Log(Log::ERROR)<<"can not open "<<fileName<<" for writing\n"; + Log(Log::ERROR)<<"can not open "<<path<<" for writing\n"; return false; } file<<std::scientific; - if(!headStr.empty()) - file<<headStr; + file<<data.model<<(data.header.empty() ? ", " : ""); + file<<data.header; file<<"\nomega, real, im\n"; - for(const eis::DataPoint& point : data) + for(const eis::DataPoint& point : data.data) file<<point.omega<<", "<<point.im.real()<<", "<<point.im.imag()<<'\n'; file.close(); return true; } -std::pair<std::vector<DataPoint>, std::string> eis::loadFromDisk(const std::string& fileName) +EisSpectra eis::loadFromDisk(const std::filesystem::path& path) { + EisSpectra out; std::fstream file; - file.open(fileName, std::ios_base::in); + file.open(path, std::ios_base::in); if(!file.is_open()) - throw file_error("can not open " + fileName + " for reading\n"); - - std::pair<std::vector<DataPoint>, std::string> out; - - std::getline(file, out.second); + throw file_error("can not open " + path.string() + " for reading\n"); std::string line; std::getline(file, line); + std::vector<std::string> tokens = tokenize(line, ','); + out.model = tokens[0]; + line.erase(line.begin(), line.begin()+tokens.size()); + out.header = line; + std::getline(file, line); + while(file.good()) { std::getline(file, line); if(line.empty() || line[0] == '#') continue; - std::vector<std::string> tokens = tokenize(line, ','); + tokens = tokenize(line, ','); if(tokens.size() != 3) - throw file_error("invalid line in " + fileName + ": " + line); - out.first.push_back(DataPoint({std::stod(tokens[1]), std::stod(tokens[2])}, std::stod(tokens[0]))); + throw file_error("invalid line in " + path.string() + ": " + line); + out.data.push_back(DataPoint({std::stod(tokens[1]), std::stod(tokens[2])}, std::stod(tokens[0]))); } file.close(); diff --git a/main.cpp b/main.cpp index 11c10af..aa7a90e 100644 --- a/main.cpp +++ b/main.cpp @@ -124,7 +124,7 @@ static void runParamSweep(const Config& config, eis::Model& model) } } - eis::saveToDisk(data, std::string(PARA_SWEEP_OUTPUT_DIR)+std::string("/")+std::to_string(i)+".csv", model.getModelStrWithParam(i)); + eis::saveToDisk(eis::EisSpectra(data, model.getModelStrWithParam(i), ""), std::string(PARA_SWEEP_OUTPUT_DIR)+std::string("/")+std::to_string(i)+".csv"); eis::Log(eis::Log::INFO, false)<<'.'; } auto end = std::chrono::high_resolution_clock::now(); -- GitLab