elec-1.00
 All Classes Namespaces Files Functions Variables Macros
elecPlot.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 /* This file is part of rl-lib
4  *
5  * Copyright (C) 2010, Herve FREZZA-BUET
6  *
7  * Author : Herve Frezza-Buet
8  *
9  * Contributor :
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public
13  * License (GPL) as published by the Free Software Foundation; either
14  * version 3 of the License, or any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  *
25  * Contact : Herve.Frezza-Buet@supelec.fr
26  *
27  */
28 
29 #include <string>
30 #include <elecPoint.hpp>
31 #include <iostream>
32 #include <iomanip>
33 #include <iterator>
34 
35 
36 namespace elec {
37  namespace plot {
38  class Figure {
39  public:
40 
41  unsigned int frame_id;
42 
43  Figure(const std::string& title,
44  const elec::Point& min, const elec::Point& max)
45  : frame_id(0) {
46  std::cout << title << std::endl
47  << min.x << ' ' << min.y << ' '
48  << max.x << ' ' << max.y << std::endl;
49  }
50 
51  void begin_frame(const std::string& prefix,
52  const std::string& suffix) {
53  std::cout << "filename" << std::endl
54  << prefix << '-'
55  << std::setw(6) << std::setfill('0') << frame_id++
56  << '.' << suffix << std::endl;
57  }
58 
59  void end_frame() {
60  std::cout << "draw" << std::endl;
61  }
62 
63  void set_view(double elev, double azim) {
64  std::cout << "view" << std::endl
65  << elev << ' ' << azim << std::endl;
66  }
67 
68  void __plot_focus(const elec::Point& pos) {
69  std::cout << "focus" << std::endl
70  << pos.x << ' ' << pos.y << std::endl;
71  }
72 
73  template<typename Iter>
74  void __plot_particles(const Iter& begin, const Iter& end) {
75  unsigned int nb_p = 0;
76  unsigned int nb_e = 0;
77  for(auto it = begin; it != end; ++it)
78  if((*it).q > 0)
79  ++nb_p;
80  else
81  ++nb_e;
82 
83  if(nb_p > 0) {
84  std::cout << "protons" << std::endl;
85  for(auto it = begin; it != end; ++it)
86  if((*it).q > 0)
87  std::cout << (*it).pos.x << ' ';
88  std::cout << std::endl;
89  for(auto it = begin; it != end; ++it)
90  if((*it).q > 0)
91  std::cout << (*it).pos.y << ' ';
92  std::cout << std::endl;
93  }
94 
95  if(nb_e > 0) {
96  std::cout << "electrons" << std::endl;
97  for(auto it = begin; it != end; ++it)
98  if((*it).q < 0)
99  std::cout << (*it).pos.x << ' ';
100  std::cout << std::endl;
101  for(auto it = begin; it != end; ++it)
102  if((*it).q < 0)
103  std::cout << (*it).pos.y << ' ';
104  std::cout << std::endl;
105  }
106  }
107 
108  template<typename PosIter,typename ValIter>
109  void __plot_field(const PosIter& pbegin, const PosIter& pend,
110  const ValIter& vbegin, const ValIter& vend,
111  double scale, double max_norm) {
112  std::cout << "vector-field" << std::endl;
113  for(auto it = pbegin; it != pend; ++it)
114  std::cout << (*it).x << ' ';
115  std::cout << std::endl;
116  for(auto it = pbegin; it != pend; ++it)
117  std::cout << (*it).y << ' ';
118  std::cout << std::endl;
119 
120  double n2 = max_norm*max_norm;
121 
122  for(auto it = vbegin; it != vend; ++it)
123  if((*it)*(*it) < n2)
124  std::cout << scale*(*it).x << ' ';
125  else
126  std::cout << 0 << ' ';
127  std::cout << std::endl;
128  for(auto it = vbegin; it != vend; ++it)
129  if((*it)*(*it) < n2)
130  std::cout << scale*(*it).y << ' ';
131  else
132  std::cout << 0 << ' ';
133  std::cout << std::endl;
134  }
135 
136  template<typename PosIter,typename ValIter>
137  void __plot_potential(const PosIter& pbegin, const PosIter& pend,
138  const ValIter& vbegin, const ValIter& vend,
139  double vmin, double vmax, unsigned int nb_contours) {
140  std::cout << "scalar-field" << std::endl;
141  std::cout << vmin << ' ' << vmax << ' ' << nb_contours << std::endl;
142  for(auto it = pbegin; it != pend; ++it)
143  std::cout << (*it).x << ' ';
144  std::cout << std::endl;
145  for(auto it = pbegin; it != pend; ++it)
146  std::cout << (*it).y << ' ';
147  std::cout << std::endl;
148  for(auto it = vbegin; it != vend; ++it)
149  std::cout << *it << ' ';
150  std::cout << std::endl;
151  }
152 
154  std::cout << "end" << std::endl;
155  }
156  };
157 
158  inline Figure figure(const std::string& title,
159  const elec::Point& min, const elec::Point& max) {
160  return Figure(title,min,max);
161  };
162  }
163 }
void end_frame()
Definition: elecPlot.hpp:59
Figure figure(const std::string &title, const elec::Point &min, const elec::Point &max)
Definition: elecPlot.hpp:158
void begin_frame(const std::string &prefix, const std::string &suffix)
Definition: elecPlot.hpp:51
Figure(const std::string &title, const elec::Point &min, const elec::Point &max)
Definition: elecPlot.hpp:43
void __plot_focus(const elec::Point &pos)
Definition: elecPlot.hpp:68
Definition: elecPoint.hpp:35
void __plot_particles(const Iter &begin, const Iter &end)
Definition: elecPlot.hpp:74
Definition: elecPlot.hpp:38
double y
Definition: elecPoint.hpp:37
~Figure()
Definition: elecPlot.hpp:153
unsigned int frame_id
Definition: elecPlot.hpp:41
void __plot_field(const PosIter &pbegin, const PosIter &pend, const ValIter &vbegin, const ValIter &vend, double scale, double max_norm)
Definition: elecPlot.hpp:109
void __plot_potential(const PosIter &pbegin, const PosIter &pend, const ValIter &vbegin, const ValIter &vend, double vmin, double vmax, unsigned int nb_contours)
Definition: elecPlot.hpp:137
double x
Definition: elecPoint.hpp:37
void set_view(double elev, double azim)
Definition: elecPlot.hpp:63