new dump file IO class concept

pull/1/head
Thorsten Liebig 2011-04-01 15:08:56 +02:00
parent f732038475
commit 3dc19c1f4d
5 changed files with 497 additions and 21 deletions

View File

@ -42,19 +42,25 @@ win32 {
LIBS += ../tinyxml/libtinyxml.so
LIBS += -lboost_thread
LIBS += -lhdf5 -lhdf5_cpp
### vtk ###
INCLUDEPATH += /usr/include/vtk-5.2 \
/usr/include/vtk-5.4 \
/usr/include/vtk-5.6
LIBS += -lvtkCommon \
-lvtkIO \
-lvtksys
}
QMAKE_LFLAGS += \'-Wl,-rpath,\$$ORIGIN/../CSXCAD\'
QMAKE_LFLAGS += \'-Wl,-rpath,\$$ORIGIN/../fparser\'
QMAKE_LFLAGS += \'-Wl,-rpath,\$$ORIGIN/../tinyxml\'
#### SOURCES ################################################################
SOURCES += main.cpp \
tools/ErrorMsg.cpp \
tools/AdrOp.cpp \
FDTD/engine.cpp \
openems.cpp
# FDTD
SOURCES += FDTD/engine.cpp \
FDTD/operator.cpp \
tools/array_ops.cpp \
openems.cpp \
FDTD/engine_multithread.cpp \
FDTD/operator_cylinder.cpp \
FDTD/engine_sse.cpp \
@ -62,8 +68,6 @@ SOURCES += main.cpp \
FDTD/operator_sse_compressed.cpp \
FDTD/engine_sse_compressed.cpp \
FDTD/operator_multithread.cpp \
tools/global.cpp \
tools/useful.cpp \
FDTD/excitation.cpp \
FDTD/operator_cylindermultigrid.cpp \
FDTD/engine_cylindermultigrid.cpp \
@ -99,16 +103,24 @@ SOURCES += Common/operator_base.cpp \
Common/processfields_td.cpp \
Common/processcurrent.cpp \
Common/processfields_fd.cpp \
Common/processfieldprobe.cpp \
Common/processfields_sar.cpp
Common/processfieldprobe.cpp \
Common/processfields_sar.cpp
HEADERS += tools/ErrorMsg.h \
tools/AdrOp.h \
tools/constants.h \
FDTD/engine.h \
# tools
SOURCES += tools/global.cpp \
tools/useful.cpp \
tools/array_ops.cpp \
tools/ErrorMsg.cpp \
tools/AdrOp.cpp \
tools/vtk_file_io.cpp \
tools/base_file_io.cpp
#### HEADERS ################################################################
HEADERS += openems.h
# FDTD
HEADERS += FDTD/engine.h \
FDTD/operator.h \
tools/array_ops.h \
openems.h \
FDTD/engine_multithread.h \
FDTD/operator_cylinder.h \
FDTD/engine_sse.h \
@ -117,11 +129,8 @@ HEADERS += tools/ErrorMsg.h \
FDTD/operator_sse_compressed.h \
FDTD/engine_sse_compressed.h \
FDTD/operator_multithread.h \
tools/global.h \
tools/useful.h \
FDTD/operator_cylindermultigrid.h \
FDTD/engine_cylindermultigrid.h \
tools/aligned_allocator.h \
FDTD/engine_interface_fdtd.h
# FDTD/extensions header files
@ -154,8 +163,19 @@ HEADERS += Common/operator_base.h \
Common/processcurrent.h \
Common/processmodematch.h \
Common/processfields_fd.h \
Common/processfieldprobe.h \
Common/processfields_sar.h
Common/processfieldprobe.h \
Common/processfields_sar.h
# tools
HEADERS += tools/ErrorMsg.h \
tools/AdrOp.h \
tools/constants.h \
tools/array_ops.h \
tools/global.h \
tools/useful.h \
tools/aligned_allocator.h \
tools/vtk_file_io.h \
tools/base_file_io.h
QMAKE_CXXFLAGS_RELEASE = -O3 \
-g \

32
tools/base_file_io.cpp Normal file
View File

@ -0,0 +1,32 @@
/*
* Copyright (C) 2011 Thorsten Liebig (Thorsten.Liebig@gmx.de)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base_file_io.h"
Base_File_IO::Base_File_IO(std::string filename, int meshType)
{
SetFilename(filename);
m_MeshType = meshType;
m_Binary = true;
m_Compress = true;
m_AppendMode = false;
m_ActiveTS = false;
}
Base_File_IO::~Base_File_IO()
{
}

87
tools/base_file_io.h Normal file
View File

@ -0,0 +1,87 @@
/*
* Copyright (C) 2011 Thorsten Liebig (Thorsten.Liebig@gmx.de)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BASE_FILE_IO_H
#define BASE_FILE_IO_H
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
//! Abstract base class for dumping scalar or vector field data
class Base_File_IO
{
public:
virtual ~Base_File_IO();
//! Set the filename
virtual void SetFilename(std::string filename) {m_filename=filename;}
//! Set the header information. May not be supported by all file types or setting.
virtual void SetHeader(std::string header) {m_header=header;}
//! Tell write to append data. May fail if filename has changed or filetype doesn't support this.
virtual void SetAppendMode(bool val) {m_AppendMode=val;}
//! Set binary flag (if the file type supports it)
virtual void SetBinary(bool val) {m_Binary=val;}
//! Set compression flag (if the file type supports it)
virtual void SetCompress(bool val) {m_Compress=val;}
//! Set the mesh lines for the given mesh type.
virtual void SetMeshLines(double const* const* lines, unsigned int const* count, double scaling=1) = 0 ;
//! Add a scalar field. \sa GetNumberOfFields \sa ClearAllFields
virtual void AddScalarField(std::string fieldname, double const* const* const* field, unsigned int const* size) = 0;
//! Add a scalar field. \sa GetNumberOfFields \sa ClearAllFields
virtual void AddScalarField(std::string fieldname, float const* const* const* field, unsigned int const* size) = 0;
//! Add a vector field. \sa GetNumberOfFields \sa ClearAllFields
virtual void AddVectorField(std::string fieldname, double const* const* const* const* field, unsigned int const* size) = 0;
//! Add a vector field. \sa GetNumberOfFields \sa ClearAllFields
virtual void AddVectorField(std::string fieldname, float const* const* const* const* field, unsigned int const* size) = 0;
//! Get the number of fields. \sa ClearAllFields
virtual int GetNumberOfFields() const = 0;
//! Clear all included fields. \sa GetNumberOfFields
virtual void ClearAllFields() = 0;
//! Get if timestep file series is active. \sa SetTimestepActive
virtual bool GetTimestepActive() {return m_ActiveTS;}
//! Set the timestep file series flag. \sa GetTimestepActive \sa SetTimestep
virtual void SetTimestepActive(bool val) {m_ActiveTS = val;}
//! Set the current timestep, this will set the timestep flag to true. \sa SetTimestepActive
virtual void SetTimestep(unsigned int ts) {m_timestep=ts;SetTimestepActive(true);}
virtual bool Write() = 0;
protected:
Base_File_IO(std::string filename, int meshType=0);
std::string m_filename;
std::string m_header;
bool m_ActiveTS;
unsigned int m_timestep;
int m_MeshType;
bool m_AppendMode;
bool m_Binary;
bool m_Compress;
};
#endif // BASE_FILE_IO_H

285
tools/vtk_file_io.cpp Normal file
View File

@ -0,0 +1,285 @@
/*
* Copyright (C) 2011 Thorsten Liebig (Thorsten.Liebig@gmx.de)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using namespace std;
#include "vtk_file_io.h"
#include <vtkRectilinearGrid.h>
#include <vtkRectilinearGridWriter.h>
#include <vtkXMLRectilinearGridWriter.h>
#include <vtkStructuredGrid.h>
#include <vtkStructuredGridWriter.h>
#include <vtkXMLStructuredGridWriter.h>
#include <vtkZLibDataCompressor.h>
#include <vtkFloatArray.h>
#include <vtkDoubleArray.h>
#include <vtkFieldData.h>
#include <vtkPointData.h>
#include <sstream>
#include <iomanip>
VTK_File_IO::VTK_File_IO(string filename, int meshType) : Base_File_IO(filename, meshType)
{
if (m_MeshType==0) //cartesian mesh
m_GridData = vtkRectilinearGrid::New();
else if (m_MeshType==1) //cylindrical mesh
m_GridData = vtkStructuredGrid::New();
else
{
cerr << "VTK_File_IO::VTK_File_IO: Error, unknown mesh type: " << m_MeshType << endl;
m_GridData=NULL;
}
}
VTK_File_IO::~VTK_File_IO()
{
if (m_GridData)
m_GridData->Delete();
m_GridData = NULL;
}
void VTK_File_IO::SetMeshLines(double const* const* lines, unsigned int const* count, double scaling)
{
if (m_MeshType==0) //cartesian mesh
{
vtkRectilinearGrid* RectGrid = dynamic_cast<vtkRectilinearGrid*>(m_GridData);
if (RectGrid==NULL)
{
cerr << "VTK_File_IO::SetMeshLines: Error, grid invalid, this should not have happend! " << endl;
exit(1);
}
RectGrid->SetDimensions(count[0],count[1],count[2]);
vtkDoubleArray *Coords[3];
for (int n=0;n<3;++n)
{
Coords[n] = vtkDoubleArray::New();
for (unsigned int i=0; i<count[n]; i++)
Coords[n]->InsertNextValue(lines[n][i]*scaling);
}
RectGrid->SetXCoordinates(Coords[0]);
RectGrid->SetYCoordinates(Coords[1]);
RectGrid->SetZCoordinates(Coords[2]);
for (int n=0;n<3;++n)
Coords[n]->Delete();
}
else if (m_MeshType==1) //cylindrical mesh
{
vtkStructuredGrid* StructGrid = dynamic_cast<vtkStructuredGrid*>(m_GridData);
if (StructGrid==NULL)
{
cerr << "VTK_File_IO::SetMeshLines: Error, grid invalid, this should not have happend! " << endl;
exit(1);
}
StructGrid->SetDimensions(count[0],count[1],count[2]);
vtkPoints *points = vtkPoints::New();
points->SetNumberOfPoints(count[0]*count[1]*count[2]);
double r[3];
int id=0;
for (unsigned int k=0; k<count[2]; ++k)
for (unsigned int j=0; j<count[1]; ++j)
for (unsigned int i=0; i<count[0]; ++i)
{
r[0] = lines[0][i] * cos(lines[1][j]) * scaling;
r[1] = lines[0][i] * sin(lines[1][j]) * scaling;
r[2] = lines[2][k] * scaling;
points->SetPoint(id++,r);
}
StructGrid->SetPoints(points);
points->Delete();
}
else
{
cerr << "VTK_File_IO::SetMeshLines: Error, unknown mesh type: " << m_MeshType << endl;
}
}
void VTK_File_IO::AddScalarField(string fieldname, double const* const* const* field, unsigned int const* size)
{
vtkDoubleArray* array = vtkDoubleArray::New();
array->SetNumberOfTuples(size[0]*size[1]*size[2]);
array->SetName(fieldname.c_str());
int id=0;
for (unsigned int k=0;k<size[2];++k)
{
for (unsigned int j=0;j<size[1];++j)
{
for (unsigned int i=0;i<size[0];++i)
{
array->SetTuple1(id++,field[i][j][k]);
}
}
}
m_GridData->GetPointData()->AddArray(array);
array->Delete();
}
void VTK_File_IO::AddScalarField(string fieldname, float const* const* const* field, unsigned int const* size)
{
vtkFloatArray* array = vtkFloatArray::New();
array->SetNumberOfTuples(size[0]*size[1]*size[2]);
array->SetName(fieldname.c_str());
int id=0;
for (unsigned int k=0;k<size[2];++k)
{
for (unsigned int j=0;j<size[1];++j)
{
for (unsigned int i=0;i<size[0];++i)
{
array->SetTuple1(id++,field[i][j][k]);
}
}
}
m_GridData->GetPointData()->AddArray(array);
array->Delete();
}
void VTK_File_IO::AddVectorField(string fieldname, double const* const* const* const* field, unsigned int const* size)
{
vtkDoubleArray* array = vtkDoubleArray::New();
array->SetNumberOfComponents(3);
array->SetNumberOfTuples(size[0]*size[1]*size[2]);
array->SetName(fieldname.c_str());
int id=0;
for (unsigned int k=0;k<size[2];++k)
{
for (unsigned int j=0;j<size[1];++j)
{
for (unsigned int i=0;i<size[0];++i)
{
array->SetTuple3(id++,field[0][i][j][k],field[1][i][j][k],field[2][i][j][k]);
}
}
}
m_GridData->GetPointData()->AddArray(array);
array->Delete();
}
void VTK_File_IO::AddVectorField(string fieldname, float const* const* const* const* field, unsigned int const* size)
{
vtkFloatArray* array = vtkFloatArray::New();
array->SetNumberOfComponents(3);
array->SetNumberOfTuples(size[0]*size[1]*size[2]);
array->SetName(fieldname.c_str());
int id=0;
for (unsigned int k=0;k<size[2];++k)
{
for (unsigned int j=0;j<size[1];++j)
{
for (unsigned int i=0;i<size[0];++i)
{
array->SetTuple3(id++,field[0][i][j][k],field[1][i][j][k],field[2][i][j][k]);
}
}
}
m_GridData->GetPointData()->AddArray(array);
array->Delete();
}
int VTK_File_IO::GetNumberOfFields() const
{
return m_GridData->GetPointData()->GetNumberOfArrays();
}
void VTK_File_IO::ClearAllFields()
{
while (m_GridData->GetPointData()->GetNumberOfArrays()>0)
{
const char* name = m_GridData->GetPointData()->GetArrayName(0);
m_GridData->GetPointData()->RemoveArray(name);
}
}
bool VTK_File_IO::Write()
{
return WriteXML();
}
string VTK_File_IO::GetTimestepFilename(int pad_length) const
{
if (m_ActiveTS==false)
return m_filename;
stringstream ss;
ss << m_filename << "_" << std::setw( pad_length ) << std::setfill( '0' ) << m_timestep;
return ss.str();
}
bool VTK_File_IO::WriteASCII()
{
vtkDataWriter* writer = NULL;
if (m_MeshType==0) //cartesian mesh
writer = vtkRectilinearGridWriter::New();
else if (m_MeshType==1) //cylindrical mesh
writer = vtkStructuredGridWriter::New();
else
{
cerr << "VTK_File_IO::WriteASCII: Error, unknown mesh type: " << m_MeshType << endl;
return false;
}
writer->SetHeader(m_header.c_str());
writer->SetInput(m_GridData);
string filename = GetTimestepFilename() + ".vtk";
writer->SetFileName(filename.c_str());
if (m_Binary)
writer->SetFileTypeToBinary();
else
writer->SetFileTypeToASCII();
writer->Write();
writer->Delete();
return true;
}
bool VTK_File_IO::WriteXML()
{
vtkXMLStructuredDataWriter* writer = NULL;
if (m_MeshType==0) //cartesian mesh
writer = vtkXMLRectilinearGridWriter::New();
else if (m_MeshType==1) //cylindrical mesh
writer = vtkXMLStructuredGridWriter::New();
else
{
cerr << "VTK_File_IO::WriteXML: Error, unknown mesh type: " << m_MeshType << endl;
return false;
}
writer->SetInput(m_GridData);
string filename = GetTimestepFilename() + "." + writer->GetDefaultFileExtension();
writer->SetFileName(filename.c_str());
if (m_Compress)
writer->SetCompressor(vtkZLibDataCompressor::New());
else
writer->SetCompressor(NULL);
if (m_Binary)
writer->SetDataModeToBinary();
else
writer->SetDataModeToAscii();
writer->Write();
writer->Delete();
return true;
}

52
tools/vtk_file_io.h Normal file
View File

@ -0,0 +1,52 @@
/*
* Copyright (C) 2011 Thorsten Liebig (Thorsten.Liebig@gmx.de)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef VTK_FILE_IO_H
#define VTK_FILE_IO_H
#include "base_file_io.h"
class vtkDataSet;
class VTK_File_IO : public Base_File_IO
{
public:
VTK_File_IO(std::string filename, int meshType=0);
virtual ~VTK_File_IO();
virtual void SetMeshLines(double const* const* lines, unsigned int const* count, double scaling=1);
virtual void AddScalarField(std::string fieldname, double const* const* const* field, unsigned int const* size);
virtual void AddScalarField(std::string fieldname, float const* const* const* field, unsigned int const* size);
virtual void AddVectorField(std::string fieldname, double const* const* const* const* field, unsigned int const* size);
virtual void AddVectorField(std::string fieldname, float const* const* const* const* field, unsigned int const* size);
virtual int GetNumberOfFields() const;
virtual void ClearAllFields();
virtual bool Write();
virtual bool WriteASCII();
virtual bool WriteXML();
protected:
vtkDataSet* m_GridData;
virtual std::string GetTimestepFilename(int pad_length=10) const;
};
#endif // VTK_FILE_IO_H