dune-grid  2.2.0
skeletonfunction.hh
Go to the documentation of this file.
00001 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002 // vi: set et ts=8 sw=2 sts=2:
00003 
00004 #ifndef DUNE_GRID_IO_FILE_VTK_SKELETONFUNCTION_HH
00005 #define DUNE_GRID_IO_FILE_VTK_SKELETONFUNCTION_HH
00006 
00007 #include <string>
00008 #include <vector>
00009 
00010 #include <dune/common/fvector.hh>
00011 #include <dune/common/shared_ptr.hh>
00012 
00013 #include <dune/grid/io/file/vtk/functionwriter.hh>
00014 #include <dune/grid/io/file/vtk/pvtuwriter.hh>
00015 #include <dune/grid/io/file/vtk/vtuwriter.hh>
00016 
00017 namespace Dune {
00018 
00021 
00027   namespace VTK {
00028 
00030     //
00031     //  Prototype for VTKFunktions on the skeleton
00032     //
00033 
00034     template<typename GV, typename RF>
00035     struct SkeletonFunctionTraits {
00036       typedef GV GridView;
00037       typedef typename GV::Intersection Cell;
00038 
00039       typedef typename GV::ctype DomainField;
00040       static const unsigned dimDomain = GV::dimension-1;
00041       typedef FieldVector<DomainField, dimDomain> Domain;
00042 
00043       typedef RF RangeField;
00044       typedef std::vector<RangeField> Range;
00045     };
00046 
00048     template <typename GV, typename RF>
00049     class SkeletonFunctionInterface {
00050     public:
00051       typedef SkeletonFunctionTraits<GV, RF> Traits;
00052 
00054       unsigned dimRange() const;
00055 
00057 
00063       void evaluate(const typename Traits::Cell& c,
00064                     const typename Traits::Domain& xl,
00065                     typename Traits::Range& result) const;
00066     };
00067 
00069     //
00070     //  Class for writing SkeletonFunctions
00071     //
00072 
00074 
00078     template<typename Func>
00079     class SkeletonFunctionWriter
00080       : public FunctionWriterBase<typename Func::Traits::Cell>
00081     {
00082       typedef typename Func::Traits::RangeField RF;
00083 
00084       shared_ptr<const Func> func;
00085       std::string name_;
00086       unsigned dimR;
00087       shared_ptr<DataArrayWriter<RF> > arraywriter;
00088 
00089     public:
00090       SkeletonFunctionWriter(const shared_ptr<const Func>& func_,
00091                              const std::string& name, unsigned dimR_)
00092         : func(func_), name_(name), dimR(dimR_)
00093       { }
00094 
00095       SkeletonFunctionWriter(const shared_ptr<const Func>& func_,
00096                              const std::string& name)
00097         : func(func_), name_(name), dimR(func->dimRange())
00098       { }
00099 
00101       virtual std::string name() const { return name_; }
00102 
00104       virtual unsigned ncomps() const { return dimR; }
00105 
00107       virtual void addArray(PVTUWriter& writer) {
00108         writer.addArray<RF>(name(), ncomps());
00109       }
00110 
00112       virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
00113         arraywriter.reset(writer.makeArrayWriter<RF>(name(), ncomps(),
00114                                                      nitems));
00115         return !arraywriter->writeIsNoop();
00116       }
00117 
00119       virtual void write(const typename Func::Traits::Cell& cell,
00120                          const typename Func::Traits::Domain& xl) {
00121         typename Func::Traits::Range result;
00122         func->evaluate(cell, xl, result);
00123         for(unsigned d = 0; d < result.size() && d < dimR; ++d)
00124           arraywriter->write(result[d]);
00125         for(unsigned d = result.size(); d < dimR; ++d)
00126           arraywriter->write(0);
00127       }
00128 
00130       virtual void endWrite() {
00131         arraywriter.reset();
00132       }
00133     };
00134 
00135   } // namespace VTK
00136 
00138 
00139 } // namespace Dune
00140 
00141 #endif // DUNE_GRID_IO_FILE_VTK_SKELETONFUNCTION_HH