Skip to content
Snippets Groups Projects
GNUPlotWriter.cc 2.17 KiB
Newer Older
//
// 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 "GNUPlotWriter.h"
#include "FiniteElemSpace.h"
#include "DOFVector.h"
#include "Mesh.h"
#include "ElInfo.h"
#include "Traverse.h"
#include "Global.h"
#include "AdaptInfo.h"

namespace AMDiS {

  GNUPlotWriter::GNUPlotWriter(std::string fn,
			       FiniteElemSpace *feSpace,
			       std::vector<DOFVector<double>*> &dofVectors)
    : feSpace_(feSpace),
      dofVectors_(dofVectors),
      filename_(filename)
  {
    filename = fn;
  }

  void GNUPlotWriter::writeFiles(AdaptInfo *adaptInfo, bool force)
  {
    DOFVector<WorldVector<double> > coords(feSpace_, "coords");
    Mesh *mesh = feSpace_->getMesh();
    int dow = Global::getGeo(WORLD);
    const BasisFunction *basFcts = feSpace_->getBasisFcts();
    int numFcts = basFcts->getNumber();
    DegreeOfFreedom *localDOFs = new DegreeOfFreedom[numFcts];

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, 
					 Mesh::CALL_LEAF_EL | 
					 Mesh::FILL_COORDS);
    while (elInfo) {
      basFcts->getLocalIndices(elInfo->getElement(),
			       feSpace_->getAdmin(),
			       localDOFs);
      for (int i = 0; i < numFcts; i++)
	coords[localDOFs[i]] = elInfo->getCoord(i);
      elInfo = stack.traverseNext(elInfo);
    }

    delete [] localDOFs;

    FILE *file = NULL;
    if (!(file = fopen(filename_.c_str(), "w")))
      ERROR("could not open file %s for writing\n", filename_.c_str());

    fprintf(file, "# line format: time x y z val1[x,y,z] val2[x,y,z] ...\n");

    DOFVector<WorldVector<double> >::Iterator coordsIt(&coords, USED_DOFS);

    int index, numVecs = static_cast<int>(dofVectors_.size());
    for (coordsIt.reset(); !coordsIt.end(); ++coordsIt) {
      index = coordsIt.getDOFIndex();
      fprintf(file, "%e ", adaptInfo->getTime());
      for (int i = 0; i < dow; i++)
	fprintf(file, "%e ", (*coordsIt)[i]);
      for (int i = 0; i < numVecs; i++)
	fprintf(file, "%e ", (*(dofVectors_[i]))[index]);
      fprintf(file, "\n");
    }
  }

}