diff --git a/Common/processfields.cpp b/Common/processfields.cpp index 4fdcdfe..5bec9be 100644 --- a/Common/processfields.cpp +++ b/Common/processfields.cpp @@ -18,7 +18,8 @@ #include #include #include "tools/global.h" -#include "tools/vtk_file_io.h" +#include "tools/vtk_file_writer.h" +#include "tools/hdf5_file_writer.h" #include "processfields.h" #include "FDTD/engine_interface_fdtd.h" @@ -28,7 +29,8 @@ ProcessFields::ProcessFields(Engine_Interface_Base* eng_if) : Processing(eng_if) // vtk-file is default m_fileType = VTK_FILETYPE; m_SampleType = NONE; - m_Dump_File = NULL; + m_Vtk_Dump_File = NULL; + m_HDF5_Dump_File = NULL; SetPrecision(6); m_dualTime = false; @@ -47,8 +49,8 @@ ProcessFields::ProcessFields(Engine_Interface_Base* eng_if) : Processing(eng_if) ProcessFields::~ProcessFields() { - delete m_Dump_File; - m_Dump_File = NULL; + delete m_Vtk_Dump_File; + m_Vtk_Dump_File = NULL; for (int n=0; n<3; ++n) { delete[] posLines[n]; @@ -84,16 +86,28 @@ void ProcessFields::InitProcess() if (m_fileType==VTK_FILETYPE) { - delete m_Dump_File; - m_Dump_File = new VTK_File_IO(m_filename,(int)m_Mesh_Type); + delete m_Vtk_Dump_File; + m_Vtk_Dump_File = new VTK_File_Writer(m_filename,(int)m_Mesh_Type); #ifdef OUTPUT_IN_DRAWINGUNITS double discScaling = 1; #else double discScaling = Op->GetGridDelta(); #endif - m_Dump_File->SetMeshLines(discLines,numLines,discScaling); - m_Dump_File->SetNativeDump(g_settings.NativeFieldDumps()); + m_Vtk_Dump_File->SetMeshLines(discLines,numLines,discScaling); + m_Vtk_Dump_File->SetNativeDump(g_settings.NativeFieldDumps()); + } + if (m_fileType==HDF5_FILETYPE) + { + delete m_HDF5_Dump_File; + m_HDF5_Dump_File = new HDF5_File_Writer(m_filename+".h5"); + + #ifdef OUTPUT_IN_DRAWINGUNITS + double discScaling = 1; + #else + double discScaling = Op->GetGridDelta(); + #endif + m_HDF5_Dump_File->WriteRectMesh(numLines,discLines,(int)m_Mesh_Type,discScaling); } } @@ -216,220 +230,6 @@ void ProcessFields::CalcMeshPos() } } -bool ProcessFields::WriteMesh2HDF5(string filename, string groupName, unsigned int const* numLines, double const* const* discLines, MeshType meshT, double discLines_scaling) -{ - H5::H5File file( filename, H5F_ACC_RDWR ); - - H5::Group hdf_group( file.openGroup( groupName )); - - string names[] = {"x","y","z"}; - if (meshT==CYLINDRICAL_MESH) - { - names[0]="rho"; - names[1]="alpha"; - } - - H5::Group* group = new H5::Group( hdf_group.createGroup( "/Mesh" )); - for (int n=0; n<3; ++n) - { - hsize_t dimsf[1]; // dataset dimensions - dimsf[0] = numLines[n]; - H5::DataSpace dataspace( 1, dimsf ); - H5::FloatType datatype( H5::PredType::NATIVE_FLOAT ); - H5::DataSet dataset = group->createDataSet( names[n].c_str(), datatype, dataspace ); - //convert to float... - float* array = new float[numLines[n]]; - for (unsigned int i=0; i const* const* const* const* array, unsigned int const* numLines, float weight, float frequency) -{ - const H5std_string FILE_NAME(filename); - const H5std_string DATASET_NAME_RE( name + "_real"); - const H5std_string DATASET_NAME_IM( name + "_imag"); - - H5::H5File file( FILE_NAME, H5F_ACC_RDWR ); - - H5::Group group( file.openGroup( groupName )); - - hsize_t t_dimsf[] = {1}; - H5::DataSpace t_dataspace( 1, t_dimsf ); - - hsize_t dimsf[4]; // dataset dimensions - dimsf[0] = 3; - dimsf[1] = numLines[2]; - dimsf[2] = numLines[1]; - dimsf[3] = numLines[0]; - - H5::DataSpace dataspace( 4, dimsf ); - H5::FloatType datatype( H5::PredType::NATIVE_FLOAT ); - - //create and write real part - H5::DataSet dataset = group.createDataSet( DATASET_NAME_RE, datatype, dataspace ); - H5::Attribute attr = dataset.createAttribute("frequency",H5::PredType::NATIVE_FLOAT,t_dataspace); - attr.write( H5::PredType::NATIVE_FLOAT , &frequency); - // I have not the slightest idea why this array-copy action is necessary... but it's the only way hdf5 does what it is supposed to do anyway!! - // at least it is save in case FDTD_FLOAT was defined as double... - // why does hdf5 write the dimensions backwards??? or matlab??? - unsigned long pos = 0; - float *hdf5array = new float[3*numLines[0]*numLines[1]*numLines[2]]; - for (int n=0; n<3; ++n) - { - for (unsigned int k=0; k const* const* const* const* array, unsigned int const* numLines, float weight, float frequency); - double CalcTotalEnergyEstimate() const; void SetFileType(FileType fileType) {m_fileType=fileType;} @@ -89,7 +78,8 @@ protected: DumpType m_DumpType; FileType m_fileType; - Base_File_IO* m_Dump_File; + VTK_File_Writer* m_Vtk_Dump_File; + HDF5_File_Writer* m_HDF5_Dump_File; enum SampleType {NONE, SUBSAMPLE, OPT_RESOLUTION} m_SampleType; virtual void CalcMeshPos(); diff --git a/Common/processfields_fd.cpp b/Common/processfields_fd.cpp index a89c608..5cda128 100644 --- a/Common/processfields_fd.cpp +++ b/Common/processfields_fd.cpp @@ -17,7 +17,8 @@ #include "processfields_fd.h" #include "Common/operator_base.h" -#include "tools/vtk_file_io.h" +#include "tools/vtk_file_writer.h" +#include "tools/hdf5_file_writer.h" #include #include #include @@ -50,35 +51,14 @@ void ProcessFieldsFD::InitProcess() //setup the hdf5 file ProcessFields::InitProcess(); - if (m_Dump_File) - m_Dump_File->SetHeader(string("openEMS FD Field Dump -- Interpolation: ")+m_Eng_Interface->GetInterpolationTypeString()); + if (m_Vtk_Dump_File) + m_Vtk_Dump_File->SetHeader(string("openEMS FD Field Dump -- Interpolation: ")+m_Eng_Interface->GetInterpolationTypeString()); - if (m_fileType==HDF5_FILETYPE) + if (m_HDF5_Dump_File) { - //create hdf5 file & necessary groups - m_filename+= ".h5"; - H5::H5File* file = new H5::H5File( m_filename , H5F_ACC_TRUNC ); - H5::Group* group = new H5::Group( file->createGroup( "/FieldData" )); - delete group; - group = new H5::Group( file->createGroup( "/FieldData/FD" )); - - //set number of frequencies - hsize_t t_dimsf[] = {1}; - H5::DataSpace t_dataspace( 1, t_dimsf ); - H5::Attribute attr = group->createAttribute("Number_of_Frequencies",H5::PredType::NATIVE_INT,t_dataspace); - int count = m_FD_Samples.size(); - attr.write( H5::PredType::NATIVE_INT , &count); - - delete group; - delete file; - - //write mesh information in main root-group - #ifdef OUTPUT_IN_DRAWINGUNITS - double discScaling = 1; - #else - double discScaling = Op->GetGridDelta(); - #endif - ProcessFields::WriteMesh2HDF5(m_filename,"/",numLines,discLines,m_Mesh_Type, discScaling); + m_HDF5_Dump_File->SetCurrentGroup("/FieldData/FD"); + int numFreq = m_FD_Samples.size(); + m_HDF5_Dump_File->WriteAtrribute("/FieldData/FD","Number_of_Frequencies",&numFreq,1,H5T_NATIVE_INT); } //create data structures... @@ -164,10 +144,10 @@ void ProcessFieldsFD::DumpFDData() stringstream ss; ss << m_filename << fixed << "_f=" << m_FD_Samples.at(n) << "_p=" << std::setw( 3 ) << std::setfill( '0' ) <<(int)(angle * 180 / M_PI); - m_Dump_File->SetFilename(ss.str()); - m_Dump_File->ClearAllFields(); - m_Dump_File->AddVectorField(GetFieldNameByType(m_DumpType),field,numLines); - if (m_Dump_File->Write()==false) + m_Vtk_Dump_File->SetFilename(ss.str()); + m_Vtk_Dump_File->ClearAllFields(); + m_Vtk_Dump_File->AddVectorField(GetFieldNameByType(m_DumpType),field); + if (m_Vtk_Dump_File->Write()==false) cerr << "ProcessFieldsFD::Process: can't dump to file... abort! " << endl; } @@ -187,10 +167,10 @@ void ProcessFieldsFD::DumpFDData() } stringstream ss; ss << m_filename << fixed << "_f=" << m_FD_Samples.at(n) << "_abs"; - m_Dump_File->SetFilename(ss.str()); - m_Dump_File->ClearAllFields(); - m_Dump_File->AddVectorField(GetFieldNameByType(m_DumpType),field,numLines); - if (m_Dump_File->Write()==false) + m_Vtk_Dump_File->SetFilename(ss.str()); + m_Vtk_Dump_File->ClearAllFields(); + m_Vtk_Dump_File->AddVectorField(GetFieldNameByType(m_DumpType),field); + if (m_Vtk_Dump_File->Write()==false) cerr << "ProcessFieldsFD::Process: can't dump to file... abort! " << endl; } @@ -210,10 +190,10 @@ void ProcessFieldsFD::DumpFDData() } stringstream ss; ss << m_filename << fixed << "_f=" << m_FD_Samples.at(n) << "_arg"; - m_Dump_File->SetFilename(ss.str()); - m_Dump_File->ClearAllFields(); - m_Dump_File->AddVectorField(GetFieldNameByType(m_DumpType),field,numLines); - if (m_Dump_File->Write()==false) + m_Vtk_Dump_File->SetFilename(ss.str()); + m_Vtk_Dump_File->ClearAllFields(); + m_Vtk_Dump_File->AddVectorField(GetFieldNameByType(m_DumpType),field); + if (m_Vtk_Dump_File->Write()==false) cerr << "ProcessFieldsFD::Process: can't dump to file... abort! " << endl; } } @@ -227,10 +207,15 @@ void ProcessFieldsFD::DumpFDData() { stringstream ss; ss << "f" << n; - DumpVectorArray2HDF5(m_filename.c_str(), "/FieldData/FD", ss.str(), m_FD_Fields.at(n),numLines,1.0,m_FD_Samples.at(n)); + size_t datasize[]={numLines[0],numLines[1],numLines[2]}; + if (m_HDF5_Dump_File->WriteVectorField(ss.str(), m_FD_Fields.at(n), datasize)==false) + cerr << "ProcessFieldsFD::Process: can't dump to file...! " << endl; + float freq[1]={m_FD_Samples.at(n)}; + if (m_HDF5_Dump_File->WriteAtrribute("/FieldData/FD/"+ss.str(),"frequency",freq,1)==false) + cerr << "ProcessFieldsFD::Process: can't dump to file...! " << endl; } return; } - cerr << "ProcessFieldsTD::Process: unknown File-Type" << endl; + cerr << "ProcessFieldsFD::Process: unknown File-Type" << endl; } diff --git a/Common/processfields_sar.cpp b/Common/processfields_sar.cpp index 4beb080..efe431e 100644 --- a/Common/processfields_sar.cpp +++ b/Common/processfields_sar.cpp @@ -17,7 +17,8 @@ #include "processfields_sar.h" #include "operator_base.h" -#include "tools/vtk_file_io.h" +#include "tools/vtk_file_writer.h" +#include "tools/hdf5_file_writer.h" ProcessFieldsSAR::ProcessFieldsSAR(Engine_Interface_Base* eng_if) : ProcessFieldsFD(eng_if) { @@ -109,17 +110,22 @@ void ProcessFieldsSAR::DumpFDData() stringstream ss; ss << m_filename << fixed << "_f=" << m_FD_Samples.at(n); - m_Dump_File->SetFilename(ss.str()); - m_Dump_File->ClearAllFields(); - m_Dump_File->AddScalarField(GetFieldNameByType(m_DumpType),SAR,numLines); - if (m_Dump_File->Write()==false) - cerr << "ProcessFieldsSAR::Process: can't dump to file... abort! " << endl; + m_Vtk_Dump_File->SetFilename(ss.str()); + m_Vtk_Dump_File->ClearAllFields(); + m_Vtk_Dump_File->AddScalarField(GetFieldNameByType(m_DumpType),SAR); + if (m_Vtk_Dump_File->Write()==false) + cerr << "ProcessFieldsSAR::Process: can't dump to file...! " << endl; } else if (m_fileType==HDF5_FILETYPE) { stringstream ss; ss << "f" << n; - DumpScalarArray2HDF5(m_filename.c_str(), "/FieldData/FD", ss.str(), SAR,numLines , "frequency", m_FD_Samples.at(n)); + size_t datasize[]={numLines[0],numLines[1],numLines[2]}; + if (m_HDF5_Dump_File->WriteScalarField(ss.str(), SAR, datasize)==false) + cerr << "ProcessFieldsSAR::Process: can't dump to file...! " << endl; + float freq[1]={m_FD_Samples.at(n)}; + if (m_HDF5_Dump_File->WriteAtrribute("/FieldData/FD/"+ss.str(),"frequency",freq,1)==false) + cerr << "ProcessFieldsSAR::Process: can't dump to file...! " << endl; } else cerr << "ProcessFieldsSAR::Process: unknown File-Type" << endl; diff --git a/Common/processfields_td.cpp b/Common/processfields_td.cpp index 671fc8f..353f643 100644 --- a/Common/processfields_td.cpp +++ b/Common/processfields_td.cpp @@ -17,7 +17,8 @@ #include "processfields_td.h" #include "Common/operator_base.h" -#include "tools/vtk_file_io.h" +#include "tools/vtk_file_writer.h" +#include "tools/hdf5_file_writer.h" #include #include #include @@ -38,29 +39,11 @@ void ProcessFieldsTD::InitProcess() ProcessFields::InitProcess(); - if (m_Dump_File) - m_Dump_File->SetHeader(string("openEMS TD Field Dump -- Interpolation: ")+m_Eng_Interface->GetInterpolationTypeString()); + if (m_Vtk_Dump_File) + m_Vtk_Dump_File->SetHeader(string("openEMS TD Field Dump -- Interpolation: ")+m_Eng_Interface->GetInterpolationTypeString()); - #ifdef OUTPUT_IN_DRAWINGUNITS - double discScaling = 1; - #else - double discScaling = Op->GetGridDelta(); - #endif - - if (m_fileType==HDF5_FILETYPE) - { - //create hdf5 file & necessary groups - m_filename+= ".h5"; - H5::H5File* file = new H5::H5File( m_filename, H5F_ACC_TRUNC ); - H5::Group* group = new H5::Group( file->createGroup( "/FieldData" )); - delete group; - group = new H5::Group( file->createGroup( "/FieldData/TD" )); - delete group; - delete file; - - //write mesh information in main root-group - ProcessFields::WriteMesh2HDF5(m_filename,"/",numLines,discLines,m_Mesh_Type, discScaling); - } + if (m_HDF5_Dump_File) + m_HDF5_Dump_File->SetCurrentGroup("/FieldData/TD"); } int ProcessFieldsTD::Process() @@ -71,25 +54,37 @@ int ProcessFieldsTD::Process() string filename = m_filename; float**** field = CalcField(); + bool success = true; if (m_fileType==VTK_FILETYPE) { - m_Dump_File->SetTimestep(m_Eng_Interface->GetNumberOfTimesteps()); - m_Dump_File->ClearAllFields(); - m_Dump_File->AddVectorField(GetFieldNameByType(m_DumpType),field,numLines); - if (m_Dump_File->Write()==false) - cerr << "ProcessFieldsTD::Process: can't dump to file... abort! " << endl; + m_Vtk_Dump_File->SetTimestep(m_Eng_Interface->GetNumberOfTimesteps()); + m_Vtk_Dump_File->ClearAllFields(); + m_Vtk_Dump_File->AddVectorField(GetFieldNameByType(m_DumpType),field); + success &= m_Vtk_Dump_File->Write(); } else if (m_fileType==HDF5_FILETYPE) { stringstream ss; ss << std::setw( pad_length ) << std::setfill( '0' ) << m_Eng_Interface->GetNumberOfTimesteps(); - DumpVectorArray2HDF5(filename.c_str(), "/FieldData/TD", string( ss.str() ), field, numLines, m_Eng_Interface->GetTime(m_dualTime)); + size_t datasize[]={numLines[0],numLines[1],numLines[2]}; + success &= m_HDF5_Dump_File->WriteVectorField(ss.str(), field, datasize); + float time[1]={m_Eng_Interface->GetTime(m_dualTime)}; + success &= m_HDF5_Dump_File->WriteAtrribute("/FieldData/TD/"+ss.str(),"time",time,1); } else + { + success = false; cerr << "ProcessFieldsTD::Process: unknown File-Type" << endl; + } Delete_N_3DArray(field,numLines); + if (success==false) + { + SetEnable(false); + cerr << "ProcessFieldsTD::Process: can't dump to file... disabled! " << endl; + } + return GetNextInterval(); } diff --git a/FDTD/operator.cpp b/FDTD/operator.cpp index cd72696..ec57b19 100644 --- a/FDTD/operator.cpp +++ b/FDTD/operator.cpp @@ -23,7 +23,7 @@ #include "extensions/operator_ext_excitation.h" #include "Common/processfields.h" #include "tools/array_ops.h" -#include "tools/vtk_file_io.h" +#include "tools/vtk_file_writer.h" #include "fparser.hh" Operator* Operator::New() @@ -493,24 +493,24 @@ void Operator::DumpOperator2File(string filename) ii_temp[n][pos[0]][pos[1]][pos[2]] = GetII(n,pos); } - VTK_File_IO* vtk_io = new VTK_File_IO(filename.c_str(), m_MeshType); - vtk_io->SetMeshLines(discLines,numLines,discLines_scaling); - vtk_io->SetHeader("openEMS - Operator dump"); + VTK_File_Writer* vtk_Writer = new VTK_File_Writer(filename.c_str(), m_MeshType); + vtk_Writer->SetMeshLines(discLines,numLines,discLines_scaling); + vtk_Writer->SetHeader("openEMS - Operator dump"); - vtk_io->SetNativeDump(true); + vtk_Writer->SetNativeDump(true); - vtk_io->AddVectorField("vv",vv_temp,numLines); + vtk_Writer->AddVectorField("vv",vv_temp); Delete_N_3DArray(vv_temp,numLines); - vtk_io->AddVectorField("vi",vi_temp,numLines); + vtk_Writer->AddVectorField("vi",vi_temp); Delete_N_3DArray(vi_temp,numLines); - vtk_io->AddVectorField("iv",iv_temp,numLines); + vtk_Writer->AddVectorField("iv",iv_temp); Delete_N_3DArray(iv_temp,numLines); - vtk_io->AddVectorField("ii",ii_temp,numLines); + vtk_Writer->AddVectorField("ii",ii_temp); Delete_N_3DArray(ii_temp,numLines); - vtk_io->AddVectorField("exc",exc,numLines); + vtk_Writer->AddVectorField("exc",exc); Delete_N_3DArray(exc,numLines); - if (vtk_io->Write()==false) + if (vtk_Writer->Write()==false) cerr << "Operator::DumpOperator2File: Error: Can't write file... skipping!" << endl; cout << " done!" << endl; @@ -590,16 +590,16 @@ void Operator::DumpPEC2File( string filename ) scaling = GetGridDelta(); #endif - VTK_File_IO* vtk_io = new VTK_File_IO(filename.c_str(), m_MeshType); - vtk_io->SetMeshLines(discLines,numLines,scaling); - vtk_io->SetHeader("openEMS - PEC dump"); + VTK_File_Writer* vtk_Writer = new VTK_File_Writer(filename.c_str(), m_MeshType); + vtk_Writer->SetMeshLines(discLines,numLines,scaling); + vtk_Writer->SetHeader("openEMS - PEC dump"); - vtk_io->SetNativeDump(true); + vtk_Writer->SetNativeDump(true); - vtk_io->AddVectorField("PEC",pec,numLines); + vtk_Writer->AddVectorField("PEC",pec); Delete_N_3DArray(pec,numLines); - if (vtk_io->Write()==false) + if (vtk_Writer->Write()==false) cerr << "Operator::DumpPEC2File: Error: Can't write file... skipping!" << endl; cout << " done!" << endl; @@ -640,22 +640,22 @@ void Operator::DumpMaterial2File(string filename) } } - VTK_File_IO* vtk_io = new VTK_File_IO(filename.c_str(), m_MeshType); - vtk_io->SetMeshLines(discLines,numLines,discLines_scaling); - vtk_io->SetHeader("openEMS - material dump"); + VTK_File_Writer* vtk_Writer = new VTK_File_Writer(filename.c_str(), m_MeshType); + vtk_Writer->SetMeshLines(discLines,numLines,discLines_scaling); + vtk_Writer->SetHeader("openEMS - material dump"); - vtk_io->SetNativeDump(true); + vtk_Writer->SetNativeDump(true); - vtk_io->AddVectorField("epsilon",epsilon,numLines); + vtk_Writer->AddVectorField("epsilon",epsilon); Delete_N_3DArray(epsilon,numLines); - vtk_io->AddVectorField("mue",mue,numLines); + vtk_Writer->AddVectorField("mue",mue); Delete_N_3DArray(mue,numLines); - vtk_io->AddVectorField("kappa",kappa,numLines); + vtk_Writer->AddVectorField("kappa",kappa); Delete_N_3DArray(kappa,numLines); - vtk_io->AddVectorField("sigma",sigma,numLines); + vtk_Writer->AddVectorField("sigma",sigma); Delete_N_3DArray(sigma,numLines); - if (vtk_io->Write()==false) + if (vtk_Writer->Write()==false) cerr << "Operator::DumpMaterial2File: Error: Can't write file... skipping!" << endl; cout << " done!" << endl; diff --git a/openEMS.pro b/openEMS.pro index df76004..8924dfa 100644 --- a/openEMS.pro +++ b/openEMS.pro @@ -115,8 +115,8 @@ 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 # tools SOURCES += tools/global.cpp \ @@ -124,8 +124,8 @@ SOURCES += Common/operator_base.cpp \ tools/array_ops.cpp \ tools/ErrorMsg.cpp \ tools/AdrOp.cpp \ - tools/vtk_file_io.cpp \ - tools/base_file_io.cpp + tools/vtk_file_writer.cpp \ + tools/hdf5_file_writer.cpp #### HEADERS ################################################################ HEADERS += openems.h @@ -178,7 +178,7 @@ HEADERS += Common/operator_base.h \ Common/processmodematch.h \ Common/processfields_fd.h \ Common/processfieldprobe.h \ - Common/processfields_sar.h + Common/processfields_sar.h # tools HEADERS += tools/ErrorMsg.h \ @@ -188,8 +188,8 @@ HEADERS += tools/ErrorMsg.h \ tools/global.h \ tools/useful.h \ tools/aligned_allocator.h \ - tools/vtk_file_io.h \ - tools/base_file_io.h + tools/vtk_file_writer.h \ + tools/hdf5_file_writer.h QMAKE_CXXFLAGS_RELEASE = -O3 \ -g \ diff --git a/tools/base_file_io.cpp b/tools/base_file_io.cpp deleted file mode 100644 index 08247a2..0000000 --- a/tools/base_file_io.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* -* 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 . -*/ - -#include "base_file_io.h" - -Base_File_IO::Base_File_IO(std::string filename, int meshType) -{ - SetFilename(filename); - m_MeshType = meshType; - m_NativeDump = false; - m_Binary = true; - m_Compress = true; - m_AppendMode = false; - m_ActiveTS = false; -} - -Base_File_IO::~Base_File_IO() -{ -} diff --git a/tools/hdf5_file_reader.cpp b/tools/hdf5_file_reader.cpp new file mode 100644 index 0000000..7467d75 --- /dev/null +++ b/tools/hdf5_file_reader.cpp @@ -0,0 +1,427 @@ +/* +* 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 . +*/ + +#include "hdf5_file_reader.h" +#include "../tools/array_ops.h" +#include + +#include +#include +#include + +using namespace std; + +HDF5_File_Reader::HDF5_File_Reader(string filename) +{ + m_filename = filename; + //suppress hdf5 error output + //H5Eset_auto(NULL, NULL); +} + +HDF5_File_Reader::~HDF5_File_Reader() +{ +} + +bool HDF5_File_Reader::IsValid() +{ + htri_t val = H5Fis_hdf5(m_filename.c_str()); + if (val<0) + { + cerr << "HDF5_File_Reader::IsValid: the given file """ << m_filename << """ is not accessible..." << endl; + return false; + } + if (val==0) + { + cerr << "HDF5_File_Reader::IsValid: the given file """ << m_filename << """ is invalid..." << endl; + return false; + } + if (val==0) + cerr << "HDF5_File_Reader::IsValid: the given file """ << m_filename << """ is valid..." << endl; + return true; +} + +bool HDF5_File_Reader::ReadDataSet(string ds_name, hsize_t &nDim, hsize_t* &dims, float* &data) +{ + if (IsValid()==false) + return false; + + hid_t hdf5_file = H5Fopen( m_filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT ); + if (hdf5_file==-1) + { + cerr << "HDF5_File_Reader::ReadDataSet: opening the given file """ << m_filename << """ failed" << endl; + return false; + } + + hid_t dataset = H5Dopen(hdf5_file, ds_name.c_str() ); + if (dataset<0) + { + cerr << "HDF5_File_Reader::ReadDataSet: dataset not found" << endl; + return false; + } + hid_t type = H5Dget_type(dataset); + if (type<0) + { + cerr << "HDF5_File_Reader::ReadDataSet: dataset type error" << endl; + return false; + } + if (H5Tget_class(type)!=H5T_FLOAT) + { + cerr << "HDF5_File_Reader::ReadDataSet: dataset type not a float" << endl; + return false; + } + + hid_t space = H5Dget_space(dataset); + nDim = H5Sget_simple_extent_ndims(space); + dims = new hsize_t[nDim]; + H5Sget_simple_extent_dims(space, dims, NULL ); + hsize_t data_size = 1; + for (unsigned int d=0;d Mesh_Names; + if (H5Lexists(hdf5_file, "/Mesh/x", H5P_DEFAULT) && H5Lexists(hdf5_file, "/Mesh/y", H5P_DEFAULT) && H5Lexists(hdf5_file, "/Mesh/z", H5P_DEFAULT)) + { + meshType = 0; + Mesh_Names.push_back("/Mesh/x"); + Mesh_Names.push_back("/Mesh/y"); + Mesh_Names.push_back("/Mesh/z"); + } + else if (H5Lexists(hdf5_file, "/Mesh/rho", H5P_DEFAULT) && H5Lexists(hdf5_file, "/Mesh/alpha", H5P_DEFAULT) && H5Lexists(hdf5_file, "/Mesh/z", H5P_DEFAULT)) + { + meshType = 1; + Mesh_Names.push_back("/Mesh/rho"); + Mesh_Names.push_back("/Mesh/alpha"); + Mesh_Names.push_back("/Mesh/z"); + } + else + { + cerr << "HDF5_File_Reader::ReadMesh: no falid mesh information found" << endl; + return false; + } + for (int n=0;n<3;++n) + { + hsize_t nDim; + hsize_t* dims=NULL; + float* data=NULL; + ReadDataSet(Mesh_Names.at(n), nDim, dims, data); + if (nDim!=1) + { + cerr << "HDF5_File_Reader::ReadMesh: mesh dimension error" << endl; + delete[] dims; + return false; + } + numLines[n]=dims[0]; + delete[] dims; + lines[n]=data; + } + return true; +} + +unsigned int HDF5_File_Reader::GetNumTimeSteps() +{ + if (IsValid()==false) + return false; + + hid_t hdf5_file = H5Fopen( m_filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT ); + if (hdf5_file==-1) + { + cerr << "HDF5_File_Reader::GetNumTimeSteps: opening the given file """ << m_filename << """ failed" << endl; + return false; + } + + if (H5Lexists(hdf5_file, "/FieldData/TD", H5P_DEFAULT)<0) + return 0; + + hid_t TD_grp = H5Gopen(hdf5_file, "/FieldData/TD" ); + if (TD_grp<0) + { + cerr << "HDF5_File_Reader::GetNumTimeSteps: can't open group ""/FieldData/TD""" << endl; + return 0; + } + hsize_t numObj; + if (H5Gget_num_objs(TD_grp,&numObj)<0) + { + cerr << "HDF5_File_Reader::GetNumTimeSteps: can't read number of datasets" << endl; + return 0; + } + return numObj; +} + +bool HDF5_File_Reader::ReadTimeSteps(vector ×tep, vector &names) +{ + if (IsValid()==false) + return false; + + hid_t hdf5_file = H5Fopen( m_filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT ); + if (hdf5_file==-1) + { + cerr << "HDF5_File_Reader::ReadTimeSteps: opening the given file """ << m_filename << """ failed" << endl; + return false; + } + + if (H5Lexists(hdf5_file, "/FieldData/TD", H5P_DEFAULT)<0) + { + cerr << "HDF5_File_Reader::ReadTimeSteps: can't open ""/FieldData/TD""" << endl; + return false; + } + hid_t TD_grp = H5Gopen(hdf5_file, "/FieldData/TD" ); + if (TD_grp<0) + { + cerr << "HDF5_File_Reader::ReadTimeSteps: can't open ""/FieldData/TD""" << endl; + return false; + } + hsize_t numObj; + if (H5Gget_num_objs(TD_grp,&numObj)<0) + { + cerr << "HDF5_File_Reader::ReadTimeSteps: can't read number of datasets" << endl; + return false; + } + char name[100]; + timestep.clear(); + timestep.resize(numObj,0); + names.clear(); + names.resize(numObj); + for (hsize_t n=0;n> num) + { + timestep.at(n)=num; + names.at(n)=name; + } + else + { + cerr << "HDF5_File_Reader::ReadTimeSteps: invalid timestep format found!" << endl; + return false; + } + } + return true; +} + +float**** HDF5_File_Reader::GetTDVectorData(size_t idx, float &time, unsigned int data_size[]) +{ + if (IsValid()==false) + return false; + + hid_t hdf5_file = H5Fopen( m_filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT ); + if (hdf5_file==-1) + { + cerr << "HDF5_File_Reader::GetTDVectorData: opening the given file """ << m_filename << """ failed" << endl; + return false; + } + + time = 0; + if (H5Lexists(hdf5_file, "/FieldData/TD", H5P_DEFAULT)<0) + { + cerr << "HDF5_File_Reader::GetTDVectorData: can't open ""/FieldData/TD""" << endl; + return NULL; + } + hid_t TD_grp = H5Gopen(hdf5_file, "/FieldData/TD" ); + if (TD_grp<0) + { + cerr << "HDF5_File_Reader::GetTDVectorData: can't open ""/FieldData/TD""" << endl; + return NULL; + } + + hsize_t numObj; + if (H5Gget_num_objs(TD_grp,&numObj)<0) + { + cerr << "HDF5_File_Reader::GetTDVectorData: can't read number of datasets" << endl; + return NULL; + } + if (idx>=numObj) + return NULL; + + if (H5Gget_objtype_by_idx(TD_grp, idx) != H5G_DATASET) + { + cerr << "HDF5_File_Reader::GetTDVectorData: invalid timestep found!" << endl; + return NULL; + } + + char name[100]; + ssize_t name_size = H5Gget_objname_by_idx(TD_grp, idx, name, 100 ); + string ds_name = "/FieldData/TD/" + string(name); + + hid_t attr = H5Aopen_by_name(hdf5_file, ds_name.c_str(), "time", H5P_DEFAULT, H5P_DEFAULT); + if (attr<0) + { + cerr << "HDF5_File_Reader::GetTDVectorData: time attribute not found!" << endl; + return NULL; + } + if (H5Aread(attr, H5T_NATIVE_FLOAT, &time)<0) + { + cerr << "HDF5_File_Reader::GetTDVectorData: can't read time attribute!" << endl; + return NULL; + } + + hsize_t nDim; + hsize_t* dims=NULL; + float* data=NULL; + ReadDataSet(ds_name, nDim, dims, data); + if (nDim!=4) + { + cerr << "HDF5_File_Reader::GetTDVectorData: data dimension invalid" << endl; + delete[] dims; + return NULL; + } + if (dims[0]!=3) + { + cerr << "HDF5_File_Reader::GetTDVectorData: vector data dimension invalid" << endl; + delete[] dims; + return NULL; + } + data_size[0]=dims[3]; + data_size[1]=dims[2]; + data_size[2]=dims[1]; + delete[] dims; + data_size[3]=3; + size_t pos = 0; + float**** field = Create_N_3DArray(data_size); + for (unsigned int d=0;d<3;++d) + for (unsigned int k=0;k &frequencies) +{ + cerr << "HDF5_File_Reader::ReadFrequencies(): not implemented yet!" << endl; + return false; +} + +complex**** HDF5_File_Reader::GetFDVectorData(size_t idx, float &frequency, unsigned int data_size[]) +{ + cerr << "HDF5_File_Reader::GetFDVectorData(): not implemented yet!" << endl; + return NULL; +} + +bool HDF5_File_Reader::CalcFDVectorData(vector &frequencies, vector****> &FD_data, unsigned int data_size[4]) +{ + FD_data.clear(); + float time; + //read first TD data + float**** field = this->GetTDVectorData(0,time,data_size); + if (field==NULL) + { + cerr << "HDF5_File_Reader::CalcFDVectorData: error, no TD data found..." << endl; + return false; + } + + //init + FD_data.resize(frequencies.size(), NULL); + for (size_t fn=0;fn >(data_size); + + size_t ts=0; + unsigned int pos[3]; + complex PI_2_I(0.0,-2.0*M_PI); + complex exp_jwt_2_dt; + float time_diff=0; + float time_old =0; + complex**** field_fd = NULL; + while (field) + { + if ((ts>1) && abs(time_diff - (time - time_old))>1e15) + { + cerr << "HDF5_File_Reader::CalcFDVectorData: time interval error..." << endl; + for (size_t fn=0;fn)(PI_2_I * frequencies.at(fn) * time) ); + field_fd = FD_data.at(fn); + for (pos[0]=0; pos[0]GetTDVectorData(ts,time,data_size); + } + + // finalize data + time_diff*=2; + for (size_t fn=0;fn. +*/ + +#ifndef HDF5_FILE_READER_H +#define HDF5_FILE_READER_H + +#include +#include +#include +#include + +class HDF5_File_Reader +{ +public: + HDF5_File_Reader(std::string filename); + virtual ~HDF5_File_Reader(); + + bool ReadMesh(float** lines, unsigned int* numLines, int &meshType); + + //! Get the number of timesteps stored at /FieldData/TD/ + unsigned int GetNumTimeSteps(); + bool ReadTimeSteps(std::vector ×tep, std::vector &names); + + /*! + Get time-domain data stored at /FieldData/TD/ + \param[in] ids time step index to extract + \param[out] time time attribute for the given timestep + \param[out] data_size data size found + \return field data found in given timestep, caller must delete array, returns NULL if timestep was not found + */ + float**** GetTDVectorData(size_t idx, float &time, unsigned int data_size[4]); + + unsigned int GetNumFrequencies(); + bool ReadFrequencies(std::vector &frequencies); + std::complex**** GetFDVectorData(size_t idx, float &frequency, unsigned int data_size[4]); + + /*! + Calculate + */ + bool CalcFDVectorData(std::vector &frequencies, std::vector****> &FD_data, unsigned int data_size[4]); + + bool IsValid(); + +protected: + std::string m_filename; + + bool ReadDataSet(std::string ds_name, hsize_t &nDim, hsize_t* &dims, float* &data); +}; + +#endif // HDF5_FILE_READER_H diff --git a/tools/hdf5_file_writer.cpp b/tools/hdf5_file_writer.cpp new file mode 100644 index 0000000..a67b170 --- /dev/null +++ b/tools/hdf5_file_writer.cpp @@ -0,0 +1,335 @@ +/* +* 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 . +*/ + +using namespace std; + +#include "hdf5_file_writer.h" +#include +#include + +#include +#include +#include + +HDF5_File_Writer::HDF5_File_Writer(string filename) +{ + m_filename = filename; + m_Group = "/"; + hid_t hdf5_file = H5Fcreate(m_filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + if (hdf5_file<0) + { + cerr << "HDF5_File_Writer::HDF5_File_Writer: Error, creating the given file """ << m_filename << """ failed" << endl; + } + H5Fclose(hdf5_file); +} + +HDF5_File_Writer::~HDF5_File_Writer() +{ +} + +hid_t HDF5_File_Writer::OpenGroup(hid_t hdf5_file, string group) +{ + if (hdf5_file<0) + { + cerr << "HDF5_File_Writer::CreateGroup: Error, invalid file id" << endl; + return -1; + } + + vector results; + boost::split(results, group, boost::is_any_of("/")); + + hid_t grp=H5Gopen(hdf5_file,"/"); + if (grp<0) + { + cerr << "HDF5_File_Writer::OpenGroup: Error, opening root group " << endl; + return -1; + } + + for (size_t n=0;n0)) //check for theta/phi-direction + array[i] = discLines[n][i]; + else + array[i] = discLines[n][i] * scaling; + } + if (H5Dwrite(dataset, H5T_NATIVE_FLOAT, space, H5P_DEFAULT, H5P_DEFAULT, array)) + { + cerr << "HDF5_File_Writer::WriteRectMesh: Error, writing to dataset failed" << endl; + delete[] array; + return false; + } + delete[] array; + } + H5Fclose(hdf5_file); + return true; +} + +bool HDF5_File_Writer::WriteScalarField(std::string dataSetName, float const* const* const* field, size_t datasize[3]) +{ + size_t pos = 0; + size_t size = datasize[0]*datasize[1]*datasize[2]; + size_t n_size[3]={datasize[2],datasize[1],datasize[0]}; + float* buffer = new float[size]; + for (size_t k=0;k const* const* const* field, size_t datasize[3]) +{ + size_t pos = 0; + size_t size = datasize[0]*datasize[1]*datasize[2]; + size_t n_size[3]={datasize[2],datasize[1],datasize[0]}; + float* buffer = new float[size]; + for (size_t k=0;k const* const* const* const* field, size_t datasize[3]) +{ + size_t pos = 0; + size_t size = datasize[0]*datasize[1]*datasize[2]*3; + size_t n_size[4]={3,datasize[2],datasize[1],datasize[0]}; + float* buffer = new float[size]; + for (int n=0;n<3;++n) + for (size_t k=0;k values) +{ + float val[values.size()]; + for (size_t n=0;n. +*/ + +#ifndef HDF5_FILE_WRITER_H +#define HDF5_FILE_WRITER_H + +#include +#include +#include +#include + +class HDF5_File_Writer +{ +public: + HDF5_File_Writer(std::string filename); + ~HDF5_File_Writer(); + + bool WriteRectMesh(unsigned int const* numLines, double const* const* discLines, int MeshType=0, double scaling=1); + bool WriteRectMesh(unsigned int const* numLines, float const* const* discLines, int MeshType=0, float scaling=1); + + bool WriteScalarField(std::string dataSetName, float const* const* const* field, size_t datasize[3]); + bool WriteScalarField(std::string dataSetName, std::complex const* const* const* field, size_t datasize[3]); + bool WriteVectorField(std::string dataSetName, float const* const* const* const* field, size_t datasize[3]); + bool WriteVectorField(std::string dataSetName, std::complex const* const* const* const* field, size_t datasize[3]); + + bool WriteData(std::string dataSetName, float const* field_buf, size_t dim, size_t* datasize); + + bool WriteAtrribute(std::string locName, std::string attr_name, void const* value, hsize_t size, hid_t mem_type); + bool WriteAtrribute(std::string locName, std::string attr_name, float const* value, hsize_t size); + bool WriteAtrribute(std::string locName, std::string attr_name, std::vector values); + + void SetCurrentGroup(std::string group) {m_Group = group;} + +protected: + std::string m_filename; + std::string m_Group; + + hid_t OpenGroup(hid_t hdf5_file, std::string group); +}; + +#endif // HDF5_FILE_WRITER_H diff --git a/tools/vtk_file_io.h b/tools/vtk_file_io.h deleted file mode 100644 index f23ed63..0000000 --- a/tools/vtk_file_io.h +++ /dev/null @@ -1,54 +0,0 @@ -/* -* 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 . -*/ - -#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; - - std::vector m_MeshLines[3]; - - virtual std::string GetTimestepFilename(int pad_length=10) const; -}; - -#endif // VTK_FILE_IO_H diff --git a/tools/vtk_file_io.cpp b/tools/vtk_file_writer.cpp similarity index 71% rename from tools/vtk_file_io.cpp rename to tools/vtk_file_writer.cpp index 39aaa6f..deb7e92 100644 --- a/tools/vtk_file_io.cpp +++ b/tools/vtk_file_writer.cpp @@ -1,5 +1,5 @@ /* -* Copyright (C) 2011 Thorsten Liebig (Thorsten.Liebig@gmx.de) +* Copyright (C) 2011,2012 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 @@ -17,7 +17,7 @@ using namespace std; -#include "vtk_file_io.h" +#include "vtk_file_writer.h" #include #include @@ -35,34 +35,42 @@ using namespace std; #include -VTK_File_IO::VTK_File_IO(string filename, int meshType) : Base_File_IO(filename, meshType) +VTK_File_Writer::VTK_File_Writer(string filename, int meshType) { + SetFilename(filename); + m_MeshType = meshType; + m_NativeDump = false; + m_Binary = true; + m_Compress = true; + m_AppendMode = false; + m_ActiveTS = false; + 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; + cerr << "VTK_File_Writer::VTK_File_Writer: Error, unknown mesh type: " << m_MeshType << endl; m_GridData=NULL; } } -VTK_File_IO::~VTK_File_IO() +VTK_File_Writer::~VTK_File_Writer() { if (m_GridData) m_GridData->Delete(); m_GridData = NULL; } -void VTK_File_IO::SetMeshLines(double const* const* lines, unsigned int const* count, double scaling) +void VTK_File_Writer::SetMeshLines(double const* const* lines, unsigned int const* count, double scaling) { if (m_MeshType==0) //cartesian mesh { vtkRectilinearGrid* RectGrid = dynamic_cast(m_GridData); if (RectGrid==NULL) { - cerr << "VTK_File_IO::SetMeshLines: Error, grid invalid, this should not have happend! " << endl; + cerr << "VTK_File_Writer::SetMeshLines: Error, grid invalid, this should not have happend! " << endl; exit(1); } RectGrid->SetDimensions(count[0],count[1],count[2]); @@ -89,7 +97,7 @@ void VTK_File_IO::SetMeshLines(double const* const* lines, unsigned int const* c vtkStructuredGrid* StructGrid = dynamic_cast(m_GridData); if (StructGrid==NULL) { - cerr << "VTK_File_IO::SetMeshLines: Error, grid invalid, this should not have happend! " << endl; + cerr << "VTK_File_Writer::SetMeshLines: Error, grid invalid, this should not have happend! " << endl; exit(1); } @@ -123,21 +131,21 @@ void VTK_File_IO::SetMeshLines(double const* const* lines, unsigned int const* c } else { - cerr << "VTK_File_IO::SetMeshLines: Error, unknown mesh type: " << m_MeshType << endl; + cerr << "VTK_File_Writer::SetMeshLines: Error, unknown mesh type: " << m_MeshType << endl; } } -void VTK_File_IO::AddScalarField(string fieldname, double const* const* const* field, unsigned int const* size) +void VTK_File_Writer::AddScalarField(string fieldname, double const* const* const* field) { vtkDoubleArray* array = vtkDoubleArray::New(); - array->SetNumberOfTuples(size[0]*size[1]*size[2]); + array->SetNumberOfTuples(m_MeshLines[0].size()*m_MeshLines[1].size()*m_MeshLines[2].size()); array->SetName(fieldname.c_str()); int id=0; - for (unsigned int k=0;kSetTuple1(id++,field[i][j][k]); } @@ -147,17 +155,17 @@ void VTK_File_IO::AddScalarField(string fieldname, double const* const* const* f array->Delete(); } -void VTK_File_IO::AddScalarField(string fieldname, float const* const* const* field, unsigned int const* size) +void VTK_File_Writer::AddScalarField(string fieldname, float const* const* const* field) { vtkFloatArray* array = vtkFloatArray::New(); - array->SetNumberOfTuples(size[0]*size[1]*size[2]); + array->SetNumberOfTuples(m_MeshLines[0].size()*m_MeshLines[1].size()*m_MeshLines[2].size()); array->SetName(fieldname.c_str()); int id=0; - for (unsigned int k=0;kSetTuple1(id++,field[i][j][k]); } @@ -167,21 +175,21 @@ void VTK_File_IO::AddScalarField(string fieldname, float const* const* const* fi array->Delete(); } -void VTK_File_IO::AddVectorField(string fieldname, double const* const* const* const* field, unsigned int const* size) +void VTK_File_Writer::AddVectorField(string fieldname, double const* const* const* const* field) { vtkDoubleArray* array = vtkDoubleArray::New(); array->SetNumberOfComponents(3); - array->SetNumberOfTuples(size[0]*size[1]*size[2]); + array->SetNumberOfTuples(m_MeshLines[0].size()*m_MeshLines[1].size()*m_MeshLines[2].size()); array->SetName(fieldname.c_str()); int id=0; double out[3]; - for (unsigned int k=0;kSetTuple3(id++,field[0][i][j][k],field[1][i][j][k],field[2][i][j][k]); @@ -199,21 +207,21 @@ void VTK_File_IO::AddVectorField(string fieldname, double const* const* const* c array->Delete(); } -void VTK_File_IO::AddVectorField(string fieldname, float const* const* const* const* field, unsigned int const* size) +void VTK_File_Writer::AddVectorField(string fieldname, float const* const* const* const* field) { vtkFloatArray* array = vtkFloatArray::New(); array->SetNumberOfComponents(3); - array->SetNumberOfTuples(size[0]*size[1]*size[2]); + array->SetNumberOfTuples(m_MeshLines[0].size()*m_MeshLines[1].size()*m_MeshLines[2].size()); array->SetName(fieldname.c_str()); int id=0; float out[3]; - for (unsigned int k=0;kSetTuple3(id++,field[0][i][j][k],field[1][i][j][k],field[2][i][j][k]); @@ -232,12 +240,12 @@ void VTK_File_IO::AddVectorField(string fieldname, float const* const* const* co } -int VTK_File_IO::GetNumberOfFields() const +int VTK_File_Writer::GetNumberOfFields() const { return m_GridData->GetPointData()->GetNumberOfArrays(); } -void VTK_File_IO::ClearAllFields() +void VTK_File_Writer::ClearAllFields() { while (m_GridData->GetPointData()->GetNumberOfArrays()>0) { @@ -246,12 +254,12 @@ void VTK_File_IO::ClearAllFields() } } -bool VTK_File_IO::Write() +bool VTK_File_Writer::Write() { return WriteXML(); } -string VTK_File_IO::GetTimestepFilename(int pad_length) const +string VTK_File_Writer::GetTimestepFilename(int pad_length) const { if (m_ActiveTS==false) return m_filename; @@ -263,7 +271,7 @@ string VTK_File_IO::GetTimestepFilename(int pad_length) const } -bool VTK_File_IO::WriteASCII() +bool VTK_File_Writer::WriteASCII() { vtkDataWriter* writer = NULL; if (m_MeshType==0) //cartesian mesh @@ -272,7 +280,7 @@ bool VTK_File_IO::WriteASCII() writer = vtkStructuredGridWriter::New(); else { - cerr << "VTK_File_IO::WriteASCII: Error, unknown mesh type: " << m_MeshType << endl; + cerr << "VTK_File_Writer::WriteASCII: Error, unknown mesh type: " << m_MeshType << endl; return false; } @@ -291,7 +299,7 @@ bool VTK_File_IO::WriteASCII() return true; } -bool VTK_File_IO::WriteXML() +bool VTK_File_Writer::WriteXML() { vtkXMLStructuredDataWriter* writer = NULL; if (m_MeshType==0) //cartesian mesh @@ -300,7 +308,7 @@ bool VTK_File_IO::WriteXML() writer = vtkXMLStructuredGridWriter::New(); else { - cerr << "VTK_File_IO::WriteXML: Error, unknown mesh type: " << m_MeshType << endl; + cerr << "VTK_File_Writer::WriteXML: Error, unknown mesh type: " << m_MeshType << endl; return false; } diff --git a/tools/base_file_io.h b/tools/vtk_file_writer.h similarity index 67% rename from tools/base_file_io.h rename to tools/vtk_file_writer.h index de3e0b7..428d859 100644 --- a/tools/base_file_io.h +++ b/tools/vtk_file_writer.h @@ -1,5 +1,5 @@ /* -* Copyright (C) 2011 Thorsten Liebig (Thorsten.Liebig@gmx.de) +* Copyright (C) 2011,2012 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 @@ -15,20 +15,23 @@ * along with this program. If not, see . */ -#ifndef BASE_FILE_IO_H -#define BASE_FILE_IO_H +#ifndef VTK_FILE_WRITER_H +#define VTK_FILE_WRITER_H #include #include #include #include #include +#include -//! Abstract base class for dumping scalar or vector field data -class Base_File_IO +class vtkDataSet; + +class VTK_File_Writer { public: - virtual ~Base_File_IO(); + VTK_File_Writer(std::string filename, int meshType=0); + virtual ~VTK_File_Writer(); //! Set the filename virtual void SetFilename(std::string filename) {m_filename=filename;} @@ -42,24 +45,17 @@ public: //! 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 ; + void SetNativeDump(bool val) {m_NativeDump=val;} - void SetNativeDump(bool val) {m_NativeDump=val;}; + virtual void SetMeshLines(double const* const* lines, unsigned int const* count, double scaling=1); - //! 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; + virtual void AddScalarField(std::string fieldname, double const* const* const* field); + virtual void AddScalarField(std::string fieldname, float const* const* const* field); + virtual void AddVectorField(std::string fieldname, double const* const* const* const* field); + virtual void AddVectorField(std::string fieldname, float const* const* const* const* field); - //! Get the number of fields. \sa ClearAllFields - virtual int GetNumberOfFields() const = 0; - //! Clear all included fields. \sa GetNumberOfFields - virtual void ClearAllFields() = 0; + virtual int GetNumberOfFields() const; + virtual void ClearAllFields(); //! Get if timestep file series is active. \sa SetTimestepActive virtual bool GetTimestepActive() {return m_ActiveTS;} @@ -68,23 +64,31 @@ public: //! 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; + virtual bool Write(); + + virtual bool WriteASCII(); + virtual bool WriteXML(); protected: - Base_File_IO(std::string filename, int meshType=0); std::string m_filename; std::string m_header; + //timestep properties bool m_ActiveTS; unsigned int m_timestep; + vtkDataSet* m_GridData; + + //mesh information int m_MeshType; + std::vector m_MeshLines[3]; bool m_NativeDump; bool m_AppendMode; bool m_Binary; bool m_Compress; + + virtual std::string GetTimestepFilename(int pad_length=10) const; }; - -#endif // BASE_FILE_IO_H +#endif // VTK_FILE_Writer_H