VtkVectorWriter.cc 5.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


#include <stdio.h>
#include <string>
#include <fstream>
#include <sstream>
#include <cmath>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/convenience.hpp>
#include <boost/lexical_cast.hpp>

#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
#include <mpi.h>
#endif

#include "VtkVectorWriter.h"
#include "DataCollector.h"
#include "DOFVector.h"
#include "SurfaceRegion_ED.h"
#include "ElementRegion_ED.h"

namespace AMDiS { 

  int VtkVectorWriter::updateAnimationFile(std::string valueFilename,
				     std::vector< std::string > *paraViewAnimationFrames,
				     std::string animationFilename)
  {
    FUNCNAME("VtkVectorWriter::Impl<S>::updateAnimationFile()");

    size_t found = valueFilename.find_last_of("/\\");
    paraViewAnimationFrames->push_back(valueFilename.substr(found + 1));

    boost::iostreams::filtering_ostream file;
    {
      std::ofstream swapfile(animationFilename.c_str(),
			     std::ios::out | std::ios::trunc);
      TEST_EXIT(swapfile.is_open())
	("Cannot open file %s for writing!\n", animationFilename.c_str());
      swapfile.close();
    }
    file.push(boost::iostreams::file_descriptor_sink(animationFilename,
						     std::ios::trunc));

    file << "<?xml version=\"1.0\"?>\n";
    file << "<VTKFile type=\"Collection\" version=\"0.1\" >"  << "\n";
    file << "<Collection>\n";

    int counter = 0;
    std::vector< std::string >::iterator it;
    for (it = paraViewAnimationFrames->begin();
	 it < paraViewAnimationFrames->end();
	 ++it, counter++) {
      file << "<DataSet timestep=\"" << counter
	   << "\" part=\"0\" file=\"" << (*it) << "\"/>\n";
    }

    file << "</Collection>\n";
    file << "</VTKFile>\n";

    return 0;
  }


  void VtkVectorWriter::writeFile(std::vector<DOFVector<double>* > &values,
			    std::string filename,
76 77
			    bool writeParallel,
			    bool writeAs3dVector)
78
  {
79
    DOFVector<std::vector<double> > *newValues = new DOFVector<std::vector<double> >(values[0]->getFeSpace(), values[0]->getName());
80 81 82 83 84 85 86 87 88
    std::vector<DOFIterator<double>* > iterators;
    for (size_t i = 0; i < values.size(); i++)
      iterators.push_back(new DOFIterator<double>(values[i],USED_DOFS));
    for (size_t i = 0; i < iterators.size(); i++)
      iterators[i]->reset();
    DOFIterator<std::vector<double> > resultIter(newValues, USED_DOFS);

    for(resultIter.reset(); !resultIter.end(); resultIter++)
    {
89
      std::vector<double> val(0);
90
      for (size_t i = 0; i < static_cast<size_t>(iterators.size()); i++)
91 92 93
	val.push_back(*(*(iterators[i])));

      *resultIter = val;
94 95 96
      
      for (size_t i = 0; i < static_cast<size_t>(iterators.size()); i++)
	(*(iterators[i]))++;
97 98
    }

99
    writeFile(newValues, filename, writeParallel, writeAs3dVector);
100 101 102 103 104 105 106 107
    for (size_t i = 0; i < iterators.size(); i++)
      delete iterators[i];
    delete newValues;
  }


  void VtkVectorWriter::writeFile(WorldVector<DOFVector<double>* > &values,
			    std::string filename,
108 109
			    bool writeParallel,
			    bool writeAs3dVector)
110
  {
111
    DOFVector<WorldVector<double> > *newValues = new DOFVector<WorldVector<double> >(values[0]->getFeSpace(), values[0]->getName());
112 113 114 115 116 117 118 119 120 121 122
    WorldVector<DOFIterator<double>* > iterators;
    for (size_t i = 0; i < static_cast<size_t>(values.getSize()); i++)
      iterators[i] = new DOFIterator<double>(values[i],USED_DOFS);
    for (size_t i = 0; i < static_cast<size_t>(iterators.getSize()); i++)
      iterators[i]->reset();
    DOFIterator<WorldVector<double> > resultIter(newValues, USED_DOFS);

    for(resultIter.reset(); !resultIter.end(); resultIter++)
    {
      for (size_t i = 0; i < static_cast<size_t>(iterators.getSize()); i++)
	(*resultIter)[i] = *(*(iterators[i]));
123 124 125

      for (size_t i = 0; i < static_cast<size_t>(iterators.size()); i++)
	(*(iterators[i]))++;
126 127
    }

128
    writeFile(newValues, filename, writeParallel, writeAs3dVector);
129 130 131 132 133 134 135 136
    for (size_t i = 0; i < static_cast<size_t>(iterators.getSize()); i++)
      delete iterators[i];
    delete newValues;
  }


  void VtkVectorWriter::writeFile(SystemVector *values,
			    std::string filename,
137 138
			    bool writeParallel,
			    bool writeAs3dVector)
139
  {
140
    DOFVector<std::vector<double> > *newValues = new DOFVector<std::vector<double> >(values->getDOFVector(0)->getFeSpace(), values->getName());
141 142 143 144 145 146 147 148 149
    std::vector<DOFIterator<double>* > iterators;
    for (size_t i = 0; i < static_cast<size_t>(values->getSize()); i++)
      iterators.push_back(new DOFIterator<double>(values->getDOFVector(i),USED_DOFS));
    for (size_t i = 0; i < iterators.size(); i++)
      iterators[i]->reset();
    DOFIterator<std::vector<double> > resultIter(newValues, USED_DOFS);

    for(resultIter.reset(); !resultIter.end(); resultIter++)
    {
150
      std::vector<double> val(0);
151
      for (size_t i = 0; i < static_cast<size_t>(iterators.size()); i++)
152 153 154
	val.push_back(*(*(iterators[i])));

      *resultIter = val;
155 156 157

      for (size_t i = 0; i < static_cast<size_t>(iterators.size()); i++)
	(*(iterators[i]))++;
158 159
    }

160
    writeFile(newValues, filename, writeParallel, writeAs3dVector);
161 162 163 164 165 166
    for (size_t i = 0; i < iterators.size(); i++)
      delete iterators[i];
    delete newValues;
  }
  
}