Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/Application/vtkpost/readepfile.cpp
3203 views
1
/*****************************************************************************
2
* *
3
* Elmer, A Finite Element Software for Multiphysical Problems *
4
* *
5
* Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland *
6
* *
7
* This program is free software; you can redistribute it and/or *
8
* modify it under the terms of the GNU General Public License *
9
* as published by the Free Software Foundation; either version 2 *
10
* of the License, or (at your option) any later version. *
11
* *
12
* This program is distributed in the hope that it will be useful, *
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
* GNU General Public License for more details. *
16
* *
17
* You should have received a copy of the GNU General Public License *
18
* along with this program (in file fem/GPL-2); if not, write to the *
19
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
20
* Boston, MA 02110-1301, USA. *
21
* *
22
*****************************************************************************/
23
24
/*****************************************************************************
25
* *
26
* ElmerGUI readepfile *
27
* *
28
*****************************************************************************
29
* *
30
* Authors: Mikko Lyly, Juha Ruokolainen and Peter R�back *
31
* Email: [email protected] *
32
* Web: http://www.csc.fi/elmer *
33
* Address: CSC - IT Center for Science Ltd. *
34
* Keilaranta 14 *
35
* 02101 Espoo, Finland *
36
* *
37
* Original Date: 15 Mar 2008 *
38
* *
39
*****************************************************************************/
40
#if WITH_QT5 || WITH_QT6
41
#include <QtWidgets>
42
#endif
43
#include <QtGui>
44
#include <iostream>
45
#include <vtkXMLUnstructuredGridReader.h>
46
#include <vtkUnstructuredGrid.h>
47
#include <vtkDataSet.h>
48
#include <vtkPointData.h>
49
#include <vtkCellData.h>
50
#include "readepfile.h"
51
52
using namespace std;
53
54
ReadEpFile::ReadEpFile(QWidget *parent)
55
: QDialog(parent)
56
{
57
ui.setupUi(this);
58
59
connect(ui.browseButton, SIGNAL(clicked()), this, SLOT(browseButtonClickedSlot()));
60
connect(ui.applyButton, SIGNAL(clicked()), this, SLOT(applyButtonClickedSlot()));
61
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonClickedSlot()));
62
connect(ui.okButton, SIGNAL(clicked()), this, SLOT(okButtonClickedSlot()));
63
connect(ui.allButton, SIGNAL(clicked()), this, SLOT(allButtonClickedSlot()));
64
65
ui.nodesEdit->setEnabled(false);
66
ui.elementsEdit->setEnabled(false);
67
ui.timestepsEdit->setEnabled(false);
68
ui.dofsEdit->setEnabled(false);
69
70
setWindowTitle("Read input file");
71
setWindowIcon(QIcon(":/icons/Mesh3D.png"));
72
}
73
74
ReadEpFile::~ReadEpFile()
75
{
76
}
77
78
void ReadEpFile::browseButtonClickedSlot()
79
{
80
QString fileName = QFileDialog::getOpenFileName(this, tr("Select input file"), "", tr("Postprocessor files (*.ep *.vtu);;ElmerPost files (*.ep);;Paraview files (*.vtu)"));
81
82
ui.fileName->setText(fileName.trimmed());
83
ui.start->setValue(1);
84
ui.end->setValue(1);
85
86
readHeader();
87
}
88
89
void ReadEpFile::applyButtonClickedSlot()
90
{
91
QString fileName = ui.fileName->text().trimmed();
92
93
if(fileName.isEmpty()) return;
94
95
int start = ui.start->value();
96
int end = ui.end->value();
97
int maxSteps = ui.timestepsEdit->text().toInt();
98
99
if(end > maxSteps) {
100
end = maxSteps;
101
ui.end->setValue(maxSteps);
102
}
103
104
if(start > end) {
105
start = end;
106
ui.start->setValue(start);
107
}
108
109
repaint();
110
111
emit(readPostFileSignal(fileName));
112
}
113
114
void ReadEpFile::cancelButtonClickedSlot()
115
{
116
close();
117
}
118
119
void ReadEpFile::okButtonClickedSlot()
120
{
121
applyButtonClickedSlot();
122
cancelButtonClickedSlot();
123
}
124
125
void ReadEpFile::readHeader()
126
{
127
QString fileName = ui.fileName->text().trimmed();
128
129
QFile postFile(fileName);
130
131
if(!postFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
132
ui.fileName->setText("");
133
return;
134
}
135
136
int nodes, elements, timesteps, components;
137
138
if(ui.fileName->text().endsWith(".vtu", Qt::CaseInsensitive)){
139
140
vtkXMLUnstructuredGridReader* reader = vtkXMLUnstructuredGridReader::New();
141
reader->SetFileName(ui.fileName->text().toLatin1().data());
142
reader->Update();
143
144
nodes = reader->GetNumberOfPoints();
145
elements = reader->GetNumberOfCells();
146
components = 1;
147
timesteps = reader->GetNumberOfTimeSteps();
148
if(timesteps == 0) timesteps = 1;
149
components = 0;
150
vtkUnstructuredGrid *output = reader->GetOutput();
151
vtkPointData *pointData = output->GetPointData();
152
vtkCellData *cellData = output->GetCellData();
153
154
for(int i = 0; i < reader->GetNumberOfPointArrays(); i++){
155
components += pointData->GetArray(reader->GetPointArrayName(i))->GetNumberOfComponents();
156
}
157
reader->Delete();
158
159
160
QFileInfo info(fileName);
161
QDir dir = info.dir();
162
QString name = info.fileName();
163
int l = name.length();
164
int i = 4;
165
while(name.at(l-i-1).isNumber() && i>0) i++;
166
QString filter = name.left(l-i) + "*.vtu";
167
cout << ".vtu file: " << filter.toLatin1().data() << endl;
168
169
QStringList filterList;
170
filterList << "*.vtu";
171
vtuFileNameList = dir.entryList(filterList, QDir::Readable|QDir::Files|QDir::NoSymLinks, QDir::SortFlags(QDir::Name | QDir::IgnoreCase));
172
//for(int i=0; i < vtuFileNameList.length(); i++){
173
// cout << vtuFileNameList.at(i).toLatin1().data() << endl;
174
//}
175
vtuFileNameList = vtuFileNameList.mid(vtuFileNameList.indexOf(info.fileName()));
176
timesteps = vtuFileNameList.length();
177
178
}else if(ui.fileName->text().endsWith(".ep", Qt::CaseInsensitive)){
179
180
QTextStream post(&postFile);
181
182
QTextStream txtStream;
183
QString tmpLine = post.readLine().trimmed();
184
while(tmpLine.isEmpty() || (tmpLine.at(0) == '#'))
185
tmpLine = post.readLine().trimmed();
186
txtStream.setString(&tmpLine);
187
188
txtStream >> nodes >> elements >> components >> timesteps;
189
190
postFile.close();
191
}
192
193
ui.nodesEdit->setText(QString::number(nodes));
194
ui.elementsEdit->setText(QString::number(elements));
195
ui.timestepsEdit->setText(QString::number(timesteps));
196
ui.dofsEdit->setText(QString::number(components));
197
}
198
199
void ReadEpFile::allButtonClickedSlot()
200
{
201
ui.start->setValue(1);
202
ui.end->setValue(ui.timestepsEdit->text().toInt());
203
204
repaint();
205
}
206
207