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

graphics/matlab.hh
Go to the documentation of this file.
00001 /* graphics in Matlab
00002  */
00003 
00004 #ifndef graphMatlab_hh
00005 #define graphMatlab_hh
00006 
00007 #include "basis.hh"
00008 #include "basics/outputMatlab.hh"
00009 #include "basics/vectorsMatrices.hh"
00010 #include "basics/vectorsMatricesForward.hh"
00011 
00012 #define MatlabGraphConstr_D 0
00013 
00014 namespace concepts {
00015   
00016   // forward declaration
00017   template<class F>
00018   class Formula;
00019 }
00020 
00021 
00022 namespace graphics {
00023 
00024   using concepts::Real;
00025   using concepts::Real2d;
00026 
00027   // ********************************************************** matlabEnding **
00028 
00030   std::string matlabEnding(const std::string& filename);
00031 
00032   // ******************************************************** MatlabGraphics **
00033 
00112   class MatlabGraphics : public OutputBase {
00113   public:
00119     template<class G>
00120     MatlabGraphics(const concepts::Space<G>& spc, const std::string filename,
00121                    const uint dim = 2);
00127     MatlabGraphics(concepts::Mesh& msh, const std::string filename,
00128        const uint dim = 2, const uint points = noPoints);
00129 
00137     template<class F, class G>
00138     MatlabGraphics(const concepts::Space<G>& spc, const std::string filename,
00139                    const concepts::Vector<F>& sol, const uint dim = 2,
00140                    const concepts::ElementFunction<F,G>* fun = 0);
00141 
00148     template<class F, class G>
00149     MatlabGraphics(const concepts::Space<G>& spc, const std::string filename,
00150                    const concepts::ElementFormula<F,G>& frm,
00151                    const uint dim = 2);
00159     template<class F>
00160     MatlabGraphics(concepts::Mesh& msh, const std::string filename,
00161                    const concepts::PiecewiseFormulaBase<F>& frm, 
00162                    const uint dim = 2, const uint points = noPoints);
00163 
00164     virtual ~MatlabGraphics() {}
00165 
00172     template<class F, class G>
00173     void operator()(const concepts::Space<G>& spc,
00174                     const concepts::Vector<F>& sol,
00175                     const concepts::ElementFunction<F,G>* fun = 0);
00176 
00182     template<class F, class G>
00183     void operator()(const concepts::Space<G>& spc,
00184                     const concepts::Formula<F>& frm);
00190     template<class F, class G>
00191     void operator()(concepts::Mesh& msh, const concepts::Formula<F>& frm,
00192                     const uint points = 5);
00193 
00194     static uint noPoints;
00195   protected:
00196     virtual std::ostream& info(std::ostream& os) const;
00197   private:
00201     template<class F>
00202     void write_(concepts::GlobalPostprocess<F>& postProcess,
00203     BaseOutputCell<F>& matlab, bool data = false);
00205     void createStreams_(bool data);
00207     void closeStreams_(const uint& n);
00208 
00210     std::map<std::string, std::ostream*> s_ptr_;
00212     uint dim_;
00214     uint cnt_;
00215   };
00216 
00217   template<class G>
00218   MatlabGraphics::MatlabGraphics(const concepts::Space<G>& spc,
00219          const std::string filename, const uint dim)
00220     : OutputBase(matlabEnding(filename)), dim_(dim), cnt_(0)
00221   {
00222     // Post processor (global)
00223     concepts::GlobalPostprocess<G> postProcess(spc);
00224     // Post processor on cell
00225     BaseMeshCell matlab(s_ptr_, dim, concepts::ElementGraphicsBase::MATLAB);
00226     // Creates streams, writes to streams, writes to file and clear
00227     // and delete all streams.
00228     write_(postProcess, matlab);
00229   }
00230 
00231   template<class F, class G>
00232   MatlabGraphics::MatlabGraphics(const concepts::Space<G>& spc,
00233                                  const std::string filename,
00234                                  const concepts::Vector<F>& sol,
00235                                  const uint dim,
00236                                  const concepts::ElementFunction<F,G>* fun)
00237     : OutputBase(matlabEnding(filename)), dim_(dim), cnt_(0) {
00238     // Post processor (global)
00239     concepts::GlobalPostprocess<G> postProcess(spc);
00240     // Post processor on cell
00241     BaseDataCell<F,G> matlab(s_ptr_, dim,
00242            concepts::ElementGraphicsBase::MATLAB, sol, fun);
00243     // Creates streams, writes to streams, writes to file and clear
00244     // and delete all streams.
00245     write_(postProcess, matlab, true);
00246   }
00247 
00248   template<class F, class G>
00249   MatlabGraphics::MatlabGraphics(const concepts::Space<G>& spc,
00250          const std::string filename,
00251          const concepts::ElementFormula<F,G>& frm,
00252          const uint dim)
00253     : OutputBase(matlabEnding(filename)), dim_(dim), cnt_(0) {
00254     // Post processor (global)
00255     concepts::GlobalPostprocess<G> postProcess(spc);
00256     // Post processor on cell
00257     BaseElementFormulaCell<F,G> matlab
00258       (s_ptr_, dim, concepts::ElementGraphicsBase::MATLAB, frm);
00259     // Creates streams, writes to streams, writes to file and clear
00260     // and delete all streams.
00261     write_(postProcess, matlab, true);
00262   }
00263 
00264   template<class F>
00265   MatlabGraphics::MatlabGraphics(concepts::Mesh& msh, const std::string filename,
00266                                  const concepts::PiecewiseFormulaBase<F>& frm, 
00267                                  const uint dim, const uint points)
00268     : OutputBase(matlabEnding(filename)), dim_(dim), cnt_(0) {
00269     // Post processor (global)
00270     concepts::GlobalPostprocess<Real> postProcess(msh);
00271     // Post processor on cell
00272     BaseFormulaCell<F,Real> matlab(s_ptr_, dim,
00273                                    concepts::ElementGraphicsBase::MATLAB, frm,
00274                                    points);
00275     // Creates streams, writes to streams, writes to file and clear
00276     // and delete all streams.
00277     write_(postProcess, matlab, true);
00278   }
00279 
00280   template<class F, class G>
00281   void MatlabGraphics::operator()(const concepts::Space<G>& spc,
00282                                   const concepts::Vector<F>& sol,
00283                                   const concepts::ElementFunction<F,G>* fun) {
00284     
00285   }
00286 
00287   template<class F, class G>
00288   void MatlabGraphics::operator()(const concepts::Space<G>& spc,
00289           const concepts::Formula<F>& frm) {
00290     
00291   }
00292 
00293   template<class F, class G>
00294   void MatlabGraphics::operator()(concepts::Mesh& msh,
00295           const concepts::Formula<F>& frm,
00296           const uint points) {
00297     
00298   }
00299 
00300   template<class F>
00301   void MatlabGraphics::write_(concepts::GlobalPostprocess<F>& postProcess,
00302                               BaseOutputCell<F>& matlab, bool data) 
00303   {
00304     // Create the streams
00305     createStreams_(data);
00306     // set counter to zero
00307     matlab.count().assign(1, 0);
00308     // Post processor on cell
00309     DEBUGL(MatlabGraphConstr_D, matlab);
00310     // loop over cells or elements and write into streams
00311     postProcess(matlab);
00312     // write to file, clear and delete streams
00313     closeStreams_(matlab.n());
00314   }
00315 
00316  // ************************************************************* MatlabMesh **
00317 
00323   class MatlabMesh : public concepts::OutputOperator {
00324   public:
00335     static void writeMesh(const concepts::Cell& cell, 
00336         std::map<std::string, std::ostream*>& os,
00337         const concepts::Array<uint>& np, uint& cnt,
00338         bool boundary = false, bool coord = true);
00348     template<class F>
00349     static void writeFormula(const concepts::Cell& cell, 
00350                              std::map<std::string, std::ostream*>& os,
00351                              const concepts::Array<uint>& np, uint& cnt,
00352                              const concepts::PiecewiseFormulaBase<F>* frm);
00360     static void writeAttributes(const concepts::Connector& cntr,
00361         std::map<std::string, std::ostream*>& os);
00367     template<uint dim>
00368     static void writeCoord(typename concepts::Point<Real, dim> x,
00369                            std::map<std::string, std::ostream*>& os);
00370     static void writeCoord(concepts::Point<Real, 2> x,
00371          std::map<std::string, std::ostream*>& os);
00372   protected:
00373     virtual std::ostream& info(std::ostream& os) const;
00374   private:
00375     static void writeMesh_(const concepts::Quad2d& quad, 
00376                            std::map<std::string, std::ostream*>& os,
00377                            const concepts::Array<uint>& np, uint& cnt,
00378                            bool boundary = false, bool coord = true);
00379     static void writeMesh_(const concepts::InfiniteQuad2d& quad, 
00380                            std::map<std::string, std::ostream*>& os,
00381                            const concepts::Array<uint>& np, uint& cnt,
00382                            bool boundary = false, bool coord = true);
00383     static void writeMesh_(const concepts::Edge2d& quad, 
00384                            std::map<std::string, std::ostream*>& os,
00385                            const concepts::Array<uint>& np, uint& cnt,
00386                            bool boundary = false, bool coord = true);
00387   };
00388 
00389 
00390   template<uint dim>
00391   void MatlabMesh::writeCoord(typename concepts::Point<Real, dim> x,
00392             std::map<std::string, std::ostream*>& os) {
00393     *os["x"] << x[0] << " ";
00394     if (dim > 1) {
00395       *os["y"] << x[1] << " ";
00396       if (dim > 2)
00397   *os["z"] << x[2] << " ";
00398     }
00399   }
00400 
00401   template<class F>
00402   void MatlabMesh::writeFormula(const concepts::Cell& cell, 
00403                                 std::map<std::string, std::ostream*>& os,
00404                                 const concepts::Array<uint>& np, uint& cnt,
00405                                 const concepts::PiecewiseFormulaBase<F>* frm) {
00406     DEBUGL(MatlabMeshWriteFormula_D, "cell = " << cell);
00407 
00408     // writes mesh, but not the coordinates
00409     writeMesh(cell, os, np, cnt, false, false);
00410 
00411     F f;       // formula value    
00412 
00413     // 2D quadrilaterial
00414     const concepts::Quad2d* cellQ = 
00415       dynamic_cast<const concepts::Quad2d*>(&cell);
00416     if (cellQ) {
00417       Real2d x;  // coordinates
00418       for (uint j = 0; j < np[1]; ++j) {
00419         Real py = (Real)j / (np[1]-1);
00420         for (uint i = 0; i < np[0]; ++i) {
00421           x = cellQ->chi((Real)i/(np[0]-1), py);
00422           // write coordinates
00423           writeCoord(x, os);
00424           // write value (particularly components)
00425           f = (*frm)(cell.connector(), x);
00426           DEBUGL(MatlabMeshWriteFormula_D, "f = " << f);
00427           *os["u"] << concepts::OutputMatlab<F>(f) << ' ';
00428         } // for i
00429       } // for j
00430     } // cellQ
00431   }
00432 
00433 } // namespace graphics
00434 
00435 #endif // graphMatlab_hh
00436 

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