Page MenuHomec4science

paraview_helper.hh
No OneTemporary

File Metadata

Created
Mon, Jun 3, 19:31

paraview_helper.hh

/*
Copyright 2008 Guillaume ANCIAUX (guillaume.anciaux@epfl.ch)
This file is part of ParaViewHelper.
ParaViewHelper 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.
ParaViewHelper 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 ParaViewHelper. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __IOHELPER_PARAVIEW_HELPER_H__
#define __IOHELPER_PARAVIEW_HELPER_H__
/* -------------------------------------------------------------------------- */
#include "base64.hh"
#include <iomanip>
/* -------------------------------------------------------------------------- */
__BEGIN_IOHELPER__
class ParaviewHelper
{
/* ------------------------------------------------------------------------ */
/* Constructors/Destructors */
/* ------------------------------------------------------------------------ */
public:
ParaviewHelper(File & f, UInt mode);
virtual ~ParaviewHelper();
/* ------------------------------------------------------------------------ */
/* Methods */
/* ------------------------------------------------------------------------ */
void writeHeader(int nb_nodes,int nb_elems);
template <typename T1, typename T2>
void writePHeader(T1 & per_node_data, T2 & per_elem_data,
Int world_size, const std::string & base_name);
void startDofList(int dimension);
void endDofList();
void startCellsConnectivityList();
void endCellsConnectivityList();
void startCellsoffsetsList();
void endCellsoffsetsList();
void startCellstypesList();
void endCellstypesList();
void startPointDataList();
void endPointDataList();
void startCellDataList();
void endCellDataList();
void startData(const std::string & name,int nb_components);
void PDataArray(const std::string & name,int nb_components);
void endData();
void write_conclusion();
void pushDouble(double x);
void pushInteger(int i);
private:
void SetMode(int mode);
/* ------------------------------------------------------------------------ */
/* Class Members */
/* ------------------------------------------------------------------------ */
protected:
Base64Writer b64;
int bflag;
File & file;
long header_offset;
unsigned int compteur;
};
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::SetMode(int mode){
bflag = BASE64 & mode;
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::writeHeader(int nb_nodes,int nb_elems){
file << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" " ;
/* #ifdef BIG_ENDIAN */
/* file.printf("byte_order=\"BigEndian\">\n"); */
/* #else */
file << "byte_order=\"LittleEndian\">" << std::endl;
/* #endif */
file << "<UnstructuredGrid>\n<Piece NumberOfPoints= \""
<< nb_nodes << "\" NumberOfCells=\""
<< nb_elems << "\">" << std::endl;
}
/* -------------------------------------------------------------------------- */
template <typename T1, typename T2>
inline void ParaviewHelper::writePHeader(T1 & per_node_data, T2 & per_elem_data,
Int world_size, const std::string & base_name){
file << "<VTKFile type=\"PUnstructuredGrid\" version=\"0.1\" " << std::endl;
file << "byte_order=\"LittleEndian\">" << std::endl;
file << "<PUnstructuredGrid GhostLevel=\"0\">" << std::endl;
file << "<PPointData>" << std::endl;
typename T1::iterator itNodeField = per_node_data.begin();
typename T1::iterator endNodeField = per_node_data.end();
for ( ; itNodeField != endNodeField ; ++itNodeField){
int dim = (*itNodeField).second->getDim();
if ((*itNodeField).second->getFlagEmbedded()) dim = 3;
PDataArray((*itNodeField).second->getName(),dim);
}
file << "</PPointData>" << std::endl;
file << "<PCellData>" << std::endl;
typename T2::iterator itElemField = per_elem_data.begin();
typename T2::iterator endElemField = per_elem_data.end();
for (; itElemField != endElemField ; ++itElemField){
PDataArray((*itElemField).second->getName(),
(*itElemField).second->getDim());
}
file << "</PCellData>" << std::endl;
file << "<PPoints>" << std::endl;
file << "<PDataArray type=\"Float64\" NumberOfComponents=\"3\" format=\"ascii\"/>" << std::endl;
file << "</PPoints>" << std::endl;
for (int l = 0 ; l < world_size ; ++l)
file << "<Piece Source=\"" << base_name << ".proc" <<
std::setfill('0') << std::setw(4) << l << ".vtu\"/>" << std::endl;
file << "</PUnstructuredGrid></VTKFile>" << std::endl;
file.close();
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::startDofList(int dimension){
file << "<Points>" << std::endl;
file << "<DataArray type = \"Float64\" NumberOfComponents=\"" << dimension << "\" " ;
if (bflag == BASE64) file << "format=\"binary\">" << std::endl;
else file << "format=\"ascii\">" << std::endl;
if (bflag == BASE64) b64.CreateHeader();
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::endDofList(){
if (bflag == BASE64)
b64.WriteHeader();
file << "</DataArray>" << std::endl;
file << "</Points>" << std::endl;
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::startCellsConnectivityList(){
file << "<Cells>" << std::endl;
file << "<DataArray type=\"Int32\" Name=\"connectivity\" ";
if (bflag == BASE64) file << "format=\"binary\">" << std::endl;
else file << "format=\"ascii\">" << std::endl;
if (bflag == BASE64) b64.CreateHeader();
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::endCellsConnectivityList(){
if (bflag == BASE64) b64.WriteHeader();
file << std::endl << "</DataArray>" << std::endl;
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::startCellsoffsetsList(){
file << "<DataArray type=\"Int32\" Name=\"offsets\" " ;
if (bflag == BASE64) file << "format=\"binary\">" << std::endl;
else file << "format=\"ascii\">" << std::endl;
if (bflag == BASE64) b64.CreateHeader();
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::endCellsoffsetsList(){
if (bflag == BASE64) b64.WriteHeader();
file << std::endl << "</DataArray>" << std::endl;
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::startCellstypesList(){
file << "<DataArray type=\"UInt32\" Name=\"types\" ";
if (bflag == BASE64) file << "format=\"binary\">" << std::endl;
else file << "format=\"ascii\">" << std::endl;
if (bflag == BASE64) b64.CreateHeader();
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::endCellstypesList(){
if (bflag == BASE64)
b64.WriteHeader();
file << std::endl << "</DataArray>" << std::endl;
file << "</Cells>" << std::endl;
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::startPointDataList(){
file << "<PointData>" << std::endl;
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::endPointDataList(){
file << "</PointData>" << std::endl;
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::startCellDataList(){
file << "<CellData>" << std::endl;
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::endCellDataList(){
file << "</CellData>" << std::endl;
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::startData(const std::string & name,int nb_components){
file << "<DataArray type=\"Float64\" NumberOfComponents=\""
<< nb_components << "\" Name=\"" << name << "\" " << std::endl;
if (bflag == BASE64) file << "format=\"binary\">" << std::endl;
else file << "format=\"ascii\">" << std::endl;
if (bflag == BASE64) b64.CreateHeader();
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::endData(){
if (bflag == BASE64) b64.WriteHeader();
file << "</DataArray>" << std::endl;
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::PDataArray(const std::string & name,int nb_components){
file << "<PDataArray type=\"Float64\" NumberOfComponents=\""
<< nb_components << "\" Name=\"" << name << "\" " << std::endl;
if (bflag == BASE64) file << "format=\"binary\"></PDataArray>" <<std::endl;
else file << "format=\"ascii\"></PDataArray>" << std::endl;
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::write_conclusion(){
file << "</Piece>" << std::endl;
file << "</UnstructuredGrid>" << std::endl;
file << "</VTKFile>" << std::endl;
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::pushDouble(double d){
if (bflag == BASE64)
b64.PushDoubleInBase64(d);
else{
file << std::setprecision(15) << std::scientific << d << " ";
++compteur;
if (compteur%3 == 0)
file << std::endl;
}
}
/* -------------------------------------------------------------------------- */
inline void ParaviewHelper::pushInteger(int i){
if (bflag == BASE64)
b64.PushIntegerInBase64(i);
else{
file << i << " ";
}
}
/* -------------------------------------------------------------------------- */
inline ParaviewHelper::ParaviewHelper(File & f, UInt mode):
file(f), b64(f){
bflag = BASE64;
compteur = 0;
SetMode(mode);
}
/* -------------------------------------------------------------------------- */
inline ParaviewHelper::~ParaviewHelper(){
}
/* -------------------------------------------------------------------------- */
__END_IOHELPER__
#endif /* __IOHELPER_PARAVIEW_HELPER_H__ */

Event Timeline