From 33fd35a65afc85f344a85f0f4da1ac8079e8c443 Mon Sep 17 00:00:00 2001 From: Hannes Signer Date: Wed, 2 Aug 2023 10:35:36 +0200 Subject: [PATCH] add csv2matrix function --- examples/csv2eigen.hpp | 28 +++++++++++++++++++++++++++ examples/reference-FTCS_2D_closed.cpp | 10 +++++++++- src/Simulation.cpp | 2 +- 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 examples/csv2eigen.hpp diff --git a/examples/csv2eigen.hpp b/examples/csv2eigen.hpp new file mode 100644 index 0000000..bc0c0a0 --- /dev/null +++ b/examples/csv2eigen.hpp @@ -0,0 +1,28 @@ +#include +#include +#include + +using namespace std; +using namespace Eigen; + +inline MatrixXd CSV2Eigen(string file2Convert){ + + vector matrixEntries; + + ifstream matrixDataFile(file2Convert); + + string matrixRowString; + string matrixEntry; + int matrixRowNumber = 0; + + while(getline(matrixDataFile, matrixRowString)){ + stringstream matrixRowStringStream(matrixRowString); + while(getline(matrixRowStringStream, matrixEntry, ' ')){ + matrixEntries.push_back(stod(matrixEntry)); + } + matrixRowNumber++; + } + + return Map>(matrixEntries.data(), matrixRowNumber, matrixEntries.size() / matrixRowNumber); + +} \ No newline at end of file diff --git a/examples/reference-FTCS_2D_closed.cpp b/examples/reference-FTCS_2D_closed.cpp index 89c3b29..165d00e 100644 --- a/examples/reference-FTCS_2D_closed.cpp +++ b/examples/reference-FTCS_2D_closed.cpp @@ -1,4 +1,6 @@ #include +#include "csv2eigen.hpp" +#include "Eigen/Core" int main(int argc, char *argv[]) { int row = 11; @@ -42,8 +44,14 @@ int main(int argc, char *argv[]) { Simulation sim = Simulation(grid, bc, FTCS_APPROACH); //sim.setTimestep(0.001); sim.setIterations(2); - sim.setOutputCSV(CSV_OUTPUT_VERBOSE); + sim.setOutputCSV(CSV_OUTPUT_ON); + MatrixXd mymatrix = CSV2Eigen("/Users/hannessigner/Documents/tug/build/examples/FTCS_11_11_2-1.csv"); + + cout << "Matrix start:" << endl; + cout << mymatrix << endl; + + //bool r = grid.isApprox(mymatrix); // RUN sim.run(); diff --git a/src/Simulation.cpp b/src/Simulation.cpp index 07ec676..3a1b0a2 100644 --- a/src/Simulation.cpp +++ b/src/Simulation.cpp @@ -152,7 +152,7 @@ void Simulation::run() { } if (this->csv_output > CSV_OUTPUT_OFF) { filename = createCSVfile(); - printConcentrationsCSV(filename); + // printConcentrationsCSV(filename); } if (approach == FTCS_APPROACH) {