Skip to content

Commit 6906691

Browse files
committed
- Fixed plotting of CSV result files. git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@19579 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent dc62df9 commit 6906691

File tree

4 files changed

+51
-55
lines changed

4 files changed

+51
-55
lines changed

OMPlot/OMPlotGUI/OMPlotGUI.pro

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ CONFIG += console
1212

1313
SOURCES += main.cpp \
1414
../../SimulationRuntime/c/util/read_matlab4.c \
15+
../../SimulationRuntime/c/util/libcsv.c \
16+
../../SimulationRuntime/c/util/read_csv.c \
1517
Plot.cpp \
1618
PlotZoomer.cpp \
1719
Legend.cpp \
@@ -24,6 +26,8 @@ SOURCES += main.cpp \
2426
PlotMainWindow.cpp
2527

2628
HEADERS += ../../SimulationRuntime/c/util/read_matlab4.h \
29+
../../SimulationRuntime/c/util/libcsv.h \
30+
../../SimulationRuntime/c/util/read_csv.h \
2731
Plot.h \
2832
PlotZoomer.h \
2933
Legend.h \
@@ -38,12 +42,15 @@ HEADERS += ../../SimulationRuntime/c/util/read_matlab4.h \
3842
win32 {
3943
QMAKE_LFLAGS += -enable-auto-import
4044
CONFIG(debug, debug|release){
41-
LIBS += -L$$(OMDEV)/lib/qwt-6.1.0-mingw/lib -lqwtd
45+
LIBS += -L$$(OMDEV)/lib/qwt-6.1.0-mingw/lib -lqwtd \
46+
-L../../3rdParty/gc-7.2/.libs -lgc
4247
}
4348
else {
44-
LIBS += -L$$(OMDEV)/lib/qwt-6.1.0-mingw/lib -lqwt
49+
LIBS += -L$$(OMDEV)/lib/qwt-6.1.0-mingw/lib -lqwt \
50+
-L../../3rdParty/gc-7.2/.libs -lgc
4551
}
46-
INCLUDEPATH += $$(OMDEV)/lib/qwt-6.1.0-mingw/include
52+
INCLUDEPATH += $$(OMDEV)/lib/qwt-6.1.0-mingw/include \
53+
../../3rdParty/gc-7.2/include
4754
} else {
4855
include(OMPlotGUI.config)
4956
}

OMPlot/OMPlotGUI/OMPlotLib.pro

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ QMAKE_LFLAGS += -enable-auto-import
1414

1515
SOURCES += main.cpp \
1616
../../SimulationRuntime/c/util/read_matlab4.c \
17+
../../SimulationRuntime/c/util/libcsv.c \
18+
../../SimulationRuntime/c/util/read_csv.c \
1719
Plot.cpp \
1820
PlotZoomer.cpp \
1921
Legend.cpp \
@@ -26,6 +28,8 @@ SOURCES += main.cpp \
2628
PlotMainWindow.cpp
2729

2830
HEADERS += ../../SimulationRuntime/c/util/read_matlab4.h \
31+
../../SimulationRuntime/c/util/libcsv.h \
32+
../../SimulationRuntime/c/util/read_csv.h \
2933
Plot.h \
3034
PlotZoomer.h \
3135
Legend.h \
@@ -39,12 +43,15 @@ HEADERS += ../../SimulationRuntime/c/util/read_matlab4.h \
3943

4044
win32 {
4145
CONFIG(debug, debug|release){
42-
LIBS += -L$$(OMDEV)/lib/qwt-6.1.0-mingw/lib -lqwtd
46+
LIBS += -L$$(OMDEV)/lib/qwt-6.1.0-mingw/lib -lqwtd \
47+
-L../../3rdParty/gc-7.2/.libs -lgc
4348
}
4449
else {
45-
LIBS += -L$$(OMDEV)/lib/qwt-6.1.0-mingw/lib -lqwt
50+
LIBS += -L$$(OMDEV)/lib/qwt-6.1.0-mingw/lib -lqwt \
51+
-L../../3rdParty/gc-7.2/.libs -lgc
4652
}
47-
INCLUDEPATH += $$(OMDEV)/lib/qwt-6.1.0-mingw/include
53+
INCLUDEPATH += $$(OMDEV)/lib/qwt-6.1.0-mingw/include \
54+
../../3rdParty/gc-7.2/include
4855
} else {
4956
include(OMPlotGUI.config)
5057
}

OMPlot/OMPlotGUI/PlotWindow.cpp

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -295,65 +295,46 @@ void PlotWindow::plot()
295295
//PLOT CSV
296296
else if (mFile.fileName().endsWith("csv"))
297297
{
298-
// open the file
299-
mFile.open(QIODevice::ReadOnly);
300-
mpTextStream = new QTextStream(&mFile);
301-
currentLine = mpTextStream->readLine();
302-
currentLine.remove(QChar('"'));
303-
QStringList allVariablesInFile = currentLine.split(",");
304-
allVariablesInFile.removeLast();
305-
298+
/* open the file */
306299
QStringList variablesPlotted;
307-
QVector<int> variablesToPlotIndex;
308-
for (int j = 0; j < allVariablesInFile.length(); j++)
309-
{
310-
if ((mVariablesList.contains(allVariablesInFile[j])) or (getPlotType() == PlotWindow::PLOTALL))
311-
{
312-
variablesToPlotIndex.push_back(j);
313-
variablesPlotted.append(allVariablesInFile[j]);
314-
}
315-
}
316-
317-
// create plot curves
318-
PlotCurve *pPlotCurve[variablesToPlotIndex.size()];
319-
for(int i = 0; i < variablesToPlotIndex.size(); i++)
320-
{
321-
pPlotCurve[i] = new PlotCurve(mpPlot);
322-
pPlotCurve[i]->setFileName(QFileInfo(mFile).fileName());
323-
mpPlot->addPlotCurve(pPlotCurve[i]);
324-
pPlotCurve[i]->setTitle(variablesPlotted.at(i));
325-
}
300+
struct csv_data *csvReader;
301+
csvReader = read_csv(mFile.fileName().toStdString().c_str());
302+
if (csvReader == NULL)
303+
throw PlotException(tr("Failed to open simulation result file %1").arg(mFile.fileName()));
326304

327-
//Assign Values
328-
while(!mpTextStream->atEnd())
305+
//Read in timevector
306+
double *timeVals = read_csv_dataset(csvReader, "time");
307+
if (timeVals == NULL)
308+
throw NoVariableException("Variable doesnt exist: time");
309+
// read in all values
310+
for (int i = 0; i < csvReader->numvars; i++)
329311
{
330-
currentLine = mpTextStream->readLine();
331-
QStringList values = currentLine.split(",");
332-
values.removeLast();
333-
334-
for(int i = 0; i < variablesToPlotIndex.size(); i++)
312+
if (mVariablesList.contains(csvReader->variables[i]) or getPlotType() == PlotWindow::PLOTALL)
335313
{
336-
QString valuesString = values[0];
337-
pPlotCurve[i]->addXAxisValue(valuesString.toDouble());
338-
QString valuesString2 = values[variablesToPlotIndex[i]];
339-
pPlotCurve[i]->addYAxisValue(valuesString2.toDouble());
314+
variablesPlotted.append(csvReader->variables[i]);
315+
double *vals = read_csv_dataset(csvReader, csvReader->variables[i]);
316+
if (vals == NULL)
317+
throw NoVariableException(tr("Variable doesnt exist: %1").arg(csvReader->variables[i]).toStdString().c_str());
318+
PlotCurve *pPlotCurve = new PlotCurve(mpPlot);
319+
pPlotCurve = new PlotCurve(mpPlot);
320+
pPlotCurve->setFileName(QFileInfo(mFile).fileName());
321+
mpPlot->addPlotCurve(pPlotCurve);
322+
pPlotCurve->setTitle(csvReader->variables[i]);
323+
for (int i = 0 ; i < csvReader->numsteps ; i++)
324+
{
325+
pPlotCurve->addXAxisValue(timeVals[i]);
326+
pPlotCurve->addYAxisValue(vals[i]);
327+
}
328+
pPlotCurve->setData(timeVals, vals, csvReader->numsteps);
329+
pPlotCurve->attach(mpPlot);
330+
mpPlot->replot();
340331
}
341332
}
342-
343-
// plot the curves
344-
for(int i = 0; i < variablesToPlotIndex.size(); i++)
345-
{
346-
pPlotCurve[i]->setData(pPlotCurve[i]->getXAxisVector(), pPlotCurve[i]->getYAxisVector(),
347-
pPlotCurve[i]->getSize());
348-
pPlotCurve[i]->attach(mpPlot);
349-
mpPlot->replot();
350-
}
351-
352333
// if plottype is PLOT then check which requested variables are not found in the file
353334
if (getPlotType() == PlotWindow::PLOT)
354335
checkForErrors(mVariablesList, variablesPlotted);
355336
// close the file
356-
mFile.close();
337+
omc_free_csv_reader(csvReader);
357338
}
358339
//PLOT MAT
359340
else if(mFile.fileName().endsWith("mat"))

OMPlot/OMPlotGUI/PlotWindow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#endif
4242
#include <stdexcept>
4343
#include "../../SimulationRuntime/c/util/read_matlab4.h"
44+
#include "../../SimulationRuntime/c/util/read_csv.h"
4445
#include "Plot.h"
4546

4647
namespace OMPlot

0 commit comments

Comments
 (0)