Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)

graphics/vtkGraphics.hh
Go to the documentation of this file.
00001 /*
00002  * VtkGraphics.hh
00003  *
00004  *  Created on: 21.09.2011
00005  *      Author: Robert Gruhlke und Christian Heier
00006  */
00007 
00008 #ifndef VtkGraphics_HH_
00009 #define VtkGraphics_HH_
00010 #include "basics.hh"
00011 #include "matrixBasis.hh"
00012 #include "basis.hh"
00013 
00014 namespace graphics {
00015 
00016   class VtkGraphics: public OutputBase {
00017   public:
00018     VtkGraphics();
00019 
00027     template<class F, class G>
00028     VtkGraphics(const concepts::Space<G>& spc, const std::string filename,
00029         const concepts::Vector<F>& sol,
00030         const concepts::ElementFunction<F, G>* fun = 0, const uint dim = 2);
00031 
00038     template<class F, class G>
00039     VtkGraphics(const concepts::Space<G>& spc, const std::string filename,
00040         const concepts::ElementFormula<F, G>& frm, const uint dim = 2);
00041 
00047     template<class G>
00048     VtkGraphics(const concepts::Space<G>& spc, const std::string filename,
00049         const uint dim = 2);
00050 
00051     VtkGraphics(concepts::Mesh& msh, const std::string filename,
00052         const uint dim = 2, const uint points = 5);
00053 
00054     virtual ~VtkGraphics();
00055 
00061     template<class G>
00062     inline void clearMapping(
00063         std::map<std::string, concepts::DenseMatrix<G>*>& dense_ptr_) {
00064 
00065       delete (dense_ptr_["x"]);
00066 
00067       if (dim_ > 1)
00068         delete (dense_ptr_["y"]);
00069       if (dim_ > 2)
00070         delete (dense_ptr_["z"]);
00071 
00072       if (dim_ > 1) {
00073         delete (dense_ptr_["edgemsh"]);
00074         delete (dense_ptr_["vtxmsh"]);
00075       }
00076       delete (dense_ptr_["Attr"]);
00077       delete (dense_ptr_["edgAttr"]);
00078 
00079       //stores the gradient
00080       //if (gradAndValue_)
00081       //  delete (dense_ptr_["grad"]);
00082 
00083       //store informations to plot solution
00084       if (data_) {
00085         delete (dense_ptr_["p"]);
00086         delete (dense_ptr_["u"]);
00087         // Cell attribute on each quadrature points
00088         delete (dense_ptr_["pattr"]);
00089         delete (dense_ptr_["w"]);
00090         delete (dense_ptr_["msh"]);
00091       }
00092       dense_ptr_.clear();
00093     }
00094     ;
00095 
00096   private:
00097 
00098     // Create the matrices
00099     template<class G>
00100     void createMatrices_(bool data, uint count[], std::map<std::string,
00101         concepts::DenseMatrix<G>*>& dense_ptr_);
00102 
00103     //Store data as .mat file
00104     template<class G, class F>
00105     void storeData_(bool data, std::string filename, std::map<std::string,
00106         concepts::DenseMatrix<G>*>& dense_ptr_,
00107         concepts::DenseMatrix<F>* functionValue = 0);
00108 
00109     void writeHead_(std::string&, std::ostringstream& stream);
00110     template<class G, class F>
00111     void writePoints_(std::ostringstream& stream, std::map<std::string,
00112         concepts::DenseMatrix<G>*>& dense_ptr_,
00113         concepts::DenseMatrix<F>* functionValue = 0);
00114     template<class G>
00115     void writeMesh_(std::ostringstream& stream, std::map<std::string,
00116         concepts::DenseMatrix<G>*>& dense_ptr_);
00117     template<class G>
00118     void writeEdgeMesh_(std::ostringstream& stream, std::map<std::string,
00119         concepts::DenseMatrix<G>*>& dense_ptr_);
00120     template<class F>
00121     void writeValues_(std::ostringstream& stream,
00122         concepts::DenseMatrix<F>* functionValue = 0);
00123 
00124     template<class G>
00125     void writeWeightsAndAttr_(std::ostringstream& stream, std::map<std::string,
00126         concepts::DenseMatrix<G>*>& dense_ptr_);
00127 
00131     std::string vtkEnding_(const std::string& filename) {
00132       uint size = filename.size();
00133       if (size < 4 || std::string(filename, size - 4, 4) != ".vtk")
00134         return (filename + ".vtk");
00135       return filename;
00136     }
00137 
00138     // for destructor
00139     uint data_;
00140 
00141     // Spatial dimension of the space
00142     uint dim_;
00143 
00144     bool isComplex_;
00145 
00146     // dimension of the output: 1 if output is solution | dim_ if output is gradient
00147     uint outputDimension_;
00148 
00149   };
00150 
00151   template<class F, class G>
00152   VtkGraphics::VtkGraphics(const concepts::Space<G>& spc,
00153       const std::string filename, const concepts::Vector<F>& sol,
00154       const concepts::ElementFunction<F, G>* fun, const uint dim) :
00155     OutputBase(vtkEnding_(filename)) {
00156 
00157     dim_ = dim;
00158 
00159     // Post processor (global)
00160     concepts::GlobalPostprocess<G> postProcess(spc);
00161 
00162     //output dimension for ex. n = 1 if fun = Value or n = 2 if fun = Grad
00163     if (fun)
00164       outputDimension_ = fun->n();
00165     else
00166       outputDimension_ = 1;
00167 
00168     //is functionValue Complex or Real?
00169     if (typeid(F) == typeid(concepts::Cmplx))
00170       isComplex_ = true;
00171     if (typeid(F) == typeid(concepts::Real))
00172       isComplex_ = false;
00173 
00174     //declare mapping
00175     std::map<std::string, concepts::DenseMatrix<G>*> dense_ptr_;
00176 
00177     //declare FunctionValues u
00178     concepts::DenseMatrix<F>* functionValue;
00179 
00180     // Post processor on cell
00181     MatrixBaseDataCell<F, G> bdc(dense_ptr_, functionValue, sol, fun);
00182 
00183     //calculate size of the matrices / size of data
00184     postProcess(bdc);
00185 
00186     //Now the we know the size of the matrices, so create them.
00187     createMatrices_(true, bdc.dimensionsOfMatrices, dense_ptr_);
00188 
00189     //Now the we know the number of evaluations of the function, so create the valuevector.
00190     functionValue = new concepts::DenseMatrix<F>(outputDimension_,
00191         bdc.dimensionsOfMatrices[0]);
00192 
00193     //the MatrixBaseDataCell should know that everything is counted i.e. it can read data in the matrices
00194     bdc.counted = true;
00195 
00196     //run GlobalPostprocess with bdc to fill the concepts::DenseMatrix<> with mesh/solution informations.
00197     postProcess(bdc);
00198 
00199     //store the matrices
00200     storeData_(true, filename, dense_ptr_, functionValue);
00201 
00202     //deletes dense matrix pointer
00203     delete (functionValue);
00204 
00205     //Deletes the Dense matrices in the mapping
00206     clearMapping(dense_ptr_);
00207   }
00208 
00209   template<class F, class G>
00210   VtkGraphics::VtkGraphics(const concepts::Space<G>& spc,
00211       const std::string filename, const concepts::ElementFormula<F, G>& frm,
00212       const uint dim) :
00213     OutputBase(vtkEnding_(filename)) {
00214 
00215     //save dimension
00216     dim_ = dim;
00217 
00218     if (typeid(F) == typeid(concepts::Cmplx))
00219       isComplex_ = true;
00220     else
00221       isComplex_ = false;
00222 
00223     //output dimension for ex. n = 1 if fun = Value or n = 2 if fun = Grad
00224     outputDimension_ = Size<F>::n();
00225 
00226     // Post processor (global)
00227     concepts::GlobalPostprocess<G> postProcess(spc);
00228 
00229     //declare mapping
00230     std::map<std::string, concepts::DenseMatrix<G>*> dense_ptr_;
00231 
00232     //declare FunctionValues u
00233     concepts::DenseMatrix<F>* functionValue;
00234 
00235     // Post processor on cell
00236     MatrixBaseElementFormulaCell<F, G>
00237         bdc(dense_ptr_, functionValue, dim_, frm);
00238 
00239     //calculate size of the matrices / size of data
00240     postProcess(bdc);
00241 
00242     //Now we know the size of the matrices, so lets inatialize them.
00243     createMatrices_(true, bdc.dimensionsOfMatrices, dense_ptr_);
00244 
00245     //Now the we know the number of evaluations of the function, so create the valuevector.
00246     functionValue = new concepts::DenseMatrix<F>(outputDimension_,
00247         bdc.dimensionsOfMatrices[0]);
00248 
00249     //the MatrixBaseMeshCell should know that everything is counted i.e. it can read data in the matrices
00250     bdc.counted = true;
00251 
00252     //run GlobalPostprocess with bdc to fill the concepts::DenseMatrix<> with mesh/solution informations.
00253     postProcess(bdc);
00254 
00255     //store the matrices
00256     storeData_(true, filename, dense_ptr_, functionValue);
00257 
00258     //Delete the Dense matrices in the mapping and function value
00259     delete (functionValue);
00260     clearMapping(dense_ptr_);
00261 
00262   }
00263 
00264   template<class G>
00265   VtkGraphics::VtkGraphics(const concepts::Space<G>& spc,
00266       const std::string filename, const uint dim) :
00267     OutputBase(vtkEnding_(filename)) {
00268 
00269     dim_ = dim;
00270     isComplex_ = false;
00271     outputDimension_ = 0;
00272 
00273     // Post processor (global)
00274     concepts::GlobalPostprocess<G> postProcess(spc);
00275 
00276     //declare mapping
00277     std::map<std::string, concepts::DenseMatrix<G>*> dense_ptr_;
00278 
00279     // Post processor on cell
00280     MatrixBaseMeshCell bdc(dense_ptr_, dim_);
00281 
00282     //calculate size of the matrices / size of data
00283     postProcess(bdc);
00284 
00285     //Now the we know the size of the matrices, so create them.
00286     createMatrices_(false, bdc.dimensionsOfMatrices, dense_ptr_);
00287 
00288     //the MatrixBaseDataCell should know that everything is counted i.e. it can read data in the matrices
00289     bdc.counted = true;
00290 
00291     //run GlobalPostprocess with bdc to fill the concepts::DenseMatrix<> with mesh/solution informations.
00292     postProcess(bdc);
00293 
00294     //store the matrices
00295     storeData_<concepts::Real, concepts::Real> (false, filename, dense_ptr_);
00296 
00297     //Deletes the Dense matrices in the mapping
00298     clearMapping(dense_ptr_);
00299   }
00300 
00305   template<class G>
00306   void VtkGraphics::createMatrices_(bool data, uint count[], std::map<
00307       std::string, concepts::DenseMatrix<G>*>& dense_ptr_) {
00308 
00309     data_ = data;
00310     // coordinates
00311 
00312     dense_ptr_.insert(std::pair<std::string, concepts::DenseMatrix<G>*>("x",
00313         new concepts::DenseMatrix<G>(1, count[0])));
00314     if (dim_ > 1)
00315       dense_ptr_.insert(std::pair<std::string, concepts::DenseMatrix<G>*>("y",
00316           new concepts::DenseMatrix<G>(1, count[0])));
00317     if (dim_ > 2)
00318       dense_ptr_.insert(std::pair<std::string, concepts::DenseMatrix<G>*>("z",
00319           new concepts::DenseMatrix<G>(1, count[0])));
00320 
00321     //TODO add FaceMesh
00322 
00323     dense_ptr_.insert(std::pair<std::string, concepts::DenseMatrix<G>*>(
00324         "edgAttr", new concepts::DenseMatrix<G>(1, count[2])));
00325 
00326     if (dim_ > 1) {
00327       dense_ptr_.insert(std::pair<std::string, concepts::DenseMatrix<G>*>(
00328           "edgemsh", new concepts::DenseMatrix<G>(2, count[2])));
00329       dense_ptr_.insert(std::pair<std::string, concepts::DenseMatrix<G>*>(
00330           "vtxmsh", new concepts::DenseMatrix<G>(5, count[3])));
00331     }
00332     dense_ptr_.insert(std::pair<std::string, concepts::DenseMatrix<G>*>("Attr",
00333         new concepts::DenseMatrix<G>(1, count[3])));
00334 
00335     //stores the gradient
00336     //if (gradAndValue_)
00337     //  dense_ptr_.insert(std::pair<std::string, concepts::DenseMatrix<G>*>("grad",
00338     //      new concepts::DenseMatrix<G> (dim_, count[0])));
00339 
00340     //store informations to plot solution
00341     if (data) {
00342       dense_ptr_.insert(std::pair<std::string, concepts::DenseMatrix<G>*>("p",
00343           new concepts::DenseMatrix<G>(2, count[3])));
00344       dense_ptr_.insert(std::pair<std::string, concepts::DenseMatrix<G>*>("u",
00345           new concepts::DenseMatrix<G>(outputDimension_, count[0])));
00346       // Cell attribute on each quadrature points
00347       dense_ptr_.insert(std::pair<std::string, concepts::DenseMatrix<G>*>(
00348           "pattr", new concepts::DenseMatrix<G>(1, count[0])));
00349       dense_ptr_.insert(std::pair<std::string, concepts::DenseMatrix<G>*>("w",
00350           new concepts::DenseMatrix<G>(1, count[0])));
00351       dense_ptr_.insert(std::pair<std::string, concepts::DenseMatrix<G>*>(
00352           "msh", new concepts::DenseMatrix<G>(5, count[1])));
00353     }
00354 
00355   }//end Create matrices
00356 
00357 
00358   //Save the matrices that hold informations
00359   template<class G, class F>
00360   void VtkGraphics::storeData_(bool data, std::string filename, std::map<
00361       std::string, concepts::DenseMatrix<G>*>& dense_ptr_,
00362       concepts::DenseMatrix<F>* functionValue) {
00363 
00364     std::ostringstream stream;
00365 
00366     //write head of vtk file
00367     writeHead_(filename, stream);
00368 
00369     //write the figure-points in the vtk file
00370     writePoints_(stream, dense_ptr_, functionValue);
00371 
00372     //writes the mesh in the vtk file
00373     //(i.e. the quads = [point1, point2, point3, point4] that builds the mesh)
00374     if (data)
00375       writeMesh_(stream, dense_ptr_);
00376     //writes data for the edge
00377     writeEdgeMesh_(stream, dense_ptr_);
00378 
00379     //writes data in the vtk file. The value/gradient/laplace of the solution,
00380     //the integration weights of the integration rule and attributes
00381     if (data) {
00382       writeValues_(stream, functionValue);
00383       writeWeightsAndAttr_(stream, dense_ptr_);
00384     }
00385 
00386     //store the written data in ofs
00387     *ofs_ << stream.str();
00388   }
00389 
00390   template<class G>
00391   void VtkGraphics::writeMesh_(std::ostringstream& stream, std::map<
00392       std::string, concepts::DenseMatrix<G>*>& dense_ptr_) {
00393     stream << "POLYGONS" << " " << dense_ptr_["msh"]->dimY() << " "
00394         << dense_ptr_["msh"]->dimY() * 5 << "\n";
00395 
00396     for (uint i = 0; i < dense_ptr_["msh"]->dimY(); ++i)
00397       stream << "4 " << dense_ptr_["msh"]->operator()(0, i) - 1 << " "
00398           << dense_ptr_["msh"]->operator()(1, i) - 1 << " "
00399           << dense_ptr_["msh"]->operator()(2, i) - 1 << " "
00400           << dense_ptr_["msh"]->operator()(3, i) - 1 << '\n';
00401   }
00402 
00403   template<class G>
00404   void VtkGraphics::writeEdgeMesh_(std::ostringstream& stream, std::map<
00405       std::string, concepts::DenseMatrix<G>*>& dense_ptr_) {
00406     stream << "LINES" << " " << dense_ptr_["edgemsh"]->dimY() << " "
00407         << dense_ptr_["edgemsh"]->dimY() * 3 << "\n";
00408 
00409     for (uint i = 0; i < dense_ptr_["edgemsh"]->dimY(); ++i)
00410       stream << "2 " << dense_ptr_["edgemsh"]->operator()(0, i) - 1 << " "
00411           << dense_ptr_["edgemsh"]->operator()(1, i) - 1 << "\n";
00412   }
00413 
00414   template<class F>
00415   void VtkGraphics::writeValues_(std::ostringstream& stream,
00416       concepts::DenseMatrix<F>* functionValue) {
00417     stream << "POINT_DATA " << functionValue->dimY() << "\n";
00418     if (isComplex_) {
00419       stream << "VECTORS solution FLOAT\n";
00420     } else {
00421       stream << "SCALARS solution FLOAT\n";
00422       stream << "LOOKUP_TABLE default\n";
00423     }
00424     //stream << "LOOKUP_TABLE default\n";
00425 
00426     if (isComplex_) {
00427       concepts::DenseMatrix<concepts::Cmplx>* functionValueCmplx =
00428           dynamic_cast<concepts::DenseMatrix<concepts::Cmplx>*> (functionValue);
00429 
00430       for (uint i = 0; i < functionValue->dimY(); ++i)
00431         stream << (functionValueCmplx->operator()(0, i)).real() << " "
00432             << (functionValueCmplx->operator()(0, i)).imag() << " 0\n";
00433     } else {
00434       for (uint i = 0; i < functionValue->dimY(); ++i)
00435         stream << functionValue->operator()(0, i) << "\n";
00436     }
00437 
00438   }
00439 
00440   template<class G>
00441   void VtkGraphics::writeWeightsAndAttr_(std::ostringstream& stream, std::map<
00442       std::string, concepts::DenseMatrix<G>*>& dense_ptr_) {
00443     stream << "SCALARS weights FLOAT\n";
00444     stream << "LOOKUP_TABLE default\n";
00445     for (uint i = 0; i < dense_ptr_["w"]->dimY(); ++i)
00446       stream << dense_ptr_["w"]->operator()(0, i) << "\n";
00447 
00448     stream << "SCALARS pointAttributes FLOAT\n";
00449     stream << "LOOKUP_TABLE default\n";
00450     for (uint i = 0; i < dense_ptr_["pattr"]->dimY(); ++i)
00451       stream << dense_ptr_["pattr"]->operator()(0, i) << "\n";
00452 
00453   }
00454 
00455   template<class G, class F>
00456   void VtkGraphics::writePoints_(std::ostringstream& stream, std::map<
00457       std::string, concepts::DenseMatrix<G>*>& dense_ptr_,
00458       concepts::DenseMatrix<F>* functionValue) {
00459 
00460     stream << "POINTS " << dense_ptr_["x"]->dimY() << " float\n";
00461     if (isComplex_ && functionValue != 0) {
00462       //std::cout << "isComplex" << std::endl;
00463       //concepts::DenseMatrix<concepts::Cmplx> * functionValueCmplx =
00464       //    dynamic_cast<concepts::DenseMatrix<concepts::Cmplx>*> (functionValue);
00465       for (uint i = 0; i < dense_ptr_["x"]->dimY(); ++i) {
00466         stream << dense_ptr_["x"]->operator()(0, i) << " "
00467             << dense_ptr_["y"]->operator()(0, i);
00468         // if (outputDimension_ == 1) {
00469         //   stream << " " << (functionValueCmplx->operator()(0, i)).real()
00470         //       << "\n";
00471         // } else
00472         stream << " " << 0 << "\n";
00473       }
00474     } else {
00475       for (uint i = 0; i < dense_ptr_["x"]->dimY(); ++i) {
00476         stream << dense_ptr_["x"]->operator()(0, i) << " "
00477             << dense_ptr_["y"]->operator()(0, i);
00478         if (outputDimension_ == 1 && functionValue != 0)
00479           stream << " " << functionValue->operator()(0, i) << "\n";
00480         else
00481           stream << " " << 0 << "\n";
00482       }
00483 
00484     }
00485 
00486   }
00487 
00488 } // namespace graphics
00489 
00490 
00491 #endif /* VtkGraphics_HH_ */

Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)