Rheolef  7.1
an efficient C++ finite element environment
basis_fem_visu_gnuplot.cc
Go to the documentation of this file.
1 // file automatically generated by: ../../../rheolef/nfem/pbasis/make_basis_list_cxx.sh
22 #include "basis.h"
23 #include "equispaced.icc"
24 #include "basis_visu_gnuplot.icc"
26 #include "rheolef/iorheo.h"
27 namespace rheolef {
28 using namespace std;
29 
30 // --------------------------------------------------------------------------
31 // show polynomials
32 // --------------------------------------------------------------------------
33 template<class T>
34 void
35 basis_rep<T>::put (ostream& os, reference_element hat_K) const
36 {
37  switch (valued_tag()) {
38  case space_constant::scalar: put_scalar_valued (os, hat_K); break;
39  case space_constant::vector: put_vector_valued (os, hat_K); break;
40  default: error_macro ("unimplemented gnuplot visualization of "<<valued()<<"-valued polynomials");
41  }
42 }
43 template<class T>
44 void
45 basis_rep<T>::put_scalar_valued (ostream& os, reference_element hat_K) const
46 {
47  details::put(*this,os,hat_K); // shared with basis_raw
48 }
49 template<class T>
50 void
52 {
54  bool verbose = iorheo::getverbose(os);
55  bool clean = iorheo::getclean(os);
56  bool execute = iorheo::getexecute(os);
57  size_type nsub = iorheo::getsubdivide(os);
58  if (nsub <= 1) nsub = 20; // default value
59 
60  size_type loc_ndof = ndof(hat_K);
61  string basename = "basis-" + name() + "-" + hat_K.name();
62  string filelist;
63  // --------------------
64  // .plot
65  // --------------------
66  string plot_name = basename + ".plot";
67  string gdat_name = basename + ".gdat";
68  ofstream plot (plot_name.c_str());
69  cerr << "! file \"" << plot_name << "\" created" << endl;
70  filelist += " \"" + plot_name + "\"";
71  size_t d = hat_K.dimension();
72  check_macro (d==1 || d==2, "unsupported dimension " << d);
73  plot << "gdat = \"" << gdat_name << "\"" << endl
74  << "set colors classic" << endl
75  << "set size square" << endl
76  ;
77  // draw all edges
78  point_basic<T> xmin, xmax;
79  for (size_type mu = 0; mu < d; ++mu) {
80  xmin[mu] = 1;
81  xmax[mu] = -1;
82  }
83  for (size_type loc_isid = 0, loc_nsid = hat_K.n_subgeo(1); loc_isid < loc_nsid; ++loc_isid) {
84  size_type loc_ia = hat_K.subgeo_local_vertex (1, loc_isid, 0);
85  size_type loc_ib = hat_K.subgeo_local_vertex (1, loc_isid, 1);
86  const point& a = hat_K.vertex (loc_ia);
87  const point& b = hat_K.vertex (loc_ib);
88  plot << "set arrow from ";
89  size_type d2 = max(d,size_type(2));
90  for (size_type mu = 0; mu < d2; ++mu) {
91  plot << a[mu];
92  if (mu+1 != d2) plot << ", ";
93  xmin[mu] = min(xmin[mu],T(a[mu]));
94  xmax[mu] = max(xmax[mu],T(a[mu]));
95  }
96  plot << " to ";
97  for (size_type mu = 0; mu < d2; ++mu) {
98  plot << b[mu];
99  if (mu+1 != d2) plot << ", ";
100  xmin[mu] = min(xmin[mu],T(b[mu]));
101  xmax[mu] = max(xmax[mu],T(b[mu]));
102  }
103  plot << " nohead lc 0 lw 1" << endl;
104  }
105  T delta = 0.1;
106  plot << endl
107  << "set xrange ["<<xmin[0]-delta<<":"<<xmax[0]+delta<<"]"<<endl;
108  if (d >= 2) {
109  plot << "set yrange ["<<xmin[1]-delta<<":"<<xmax[1]+delta<<"]"<<endl;
110  } else {
111  plot << "set yrange [-1:1]"<<endl;
112  }
113  if (d >= 3) {
114  plot << "set zrange ["<<xmin[2]-delta<<":"<<xmax[2]+delta<<"]"<<endl
115  << "set xyplane at "<< xmin[2]-delta << endl;
116  }
117  if (d == 1) {
118  plot << "plot \\" << endl
119  ;
120  for (size_t loc_idof = 0; loc_idof < loc_ndof; ++loc_idof) {
121  plot << " gdat u 1:"<<loc_idof+2 << " t \"L" << loc_idof+1 << "\" w l";
122  if (loc_idof+1 != loc_ndof) { plot << ",\\"; }
123  plot << endl;
124  }
125  } else if (d == 2) {
126  plot << "pause_duration = 0.5" << endl
127  << "d = 2" << endl
128  << "scale= 1./" << nsub << endl
129  << "idx = 0" << endl
130  << "idxmax = " << loc_ndof << endl
131  << "plot gdat i idx u 1:2:(scale*$3):(scale*$4) t sprintf(\"P%d\",idx+1) w vec lc 0 lw 2" << endl
132  << "load \"" << basename << ".loop\"" << endl
133  ;
134  string loop_name = basename + ".loop";
135  ofstream loop (loop_name.c_str());
136  cerr << "! file \"" << loop_name << "\" created" << endl;
137  filelist += " \"" + loop_name + "\"";
138  loop << "idx = idx+1" << endl
139  << "if (idx < idxmax) \\" << endl
140  << " pause -1 \"<return>\"; \\" << endl
141  << " replot; \\" << endl
142  << " reread" << endl
143  ;
144  }
145  plot << "pause -1 \"<return>\"" << endl;
146  plot.close();
147 
148  // --------------------
149  // .gdat
150  // --------------------
151  ofstream gdat (gdat_name.c_str());
152  cerr << "! file \"" << gdat_name << "\" created" << endl;
153  filelist += " \"" + gdat_name + "\"";
154  gdat << setprecision(std::numeric_limits<T>::digits10)
155  << "# basis " << name() << endl
156  << "# element " << hat_K.name() << endl
157  << "# degree " << degree() << endl
158  ;
159  switch (hat_K.variant()) {
160 #ifdef TODO
161  case reference_element::p: {
162  break;
163  }
164  case reference_element::e: {
165  gdat << "# ndof " << loc_ndof << endl
166  << "# x";
167  for (size_t loc_idof = 0; loc_idof < loc_ndof; ++loc_idof) {
168  gdat << " L" << loc_idof+1;
169  }
170  gdat << endl;
171  for (size_type i = 0; i <= nsub; i++) {
172  point_basic<T> hat_x (T(i)/T(nsub));
173  eval (hat_K, hat_x, value);
174  gdat << hat_x[0];
175  for (size_t loc_idof = 0, loc_ndof = value.size(); loc_idof < loc_ndof; ++loc_idof) {
176  for (size_t mu = 0; mu < d; ++mu) {
177  gdat << " " << value[loc_idof][mu];
178  }
179  }
180  gdat << endl;
181  }
182  break;
183  }
184 #endif // TODO
186  case reference_element::q: {
187  Eigen::Matrix<point_basic<T>,Eigen::Dynamic,1> hat_node;
188  Eigen::Matrix<point_basic<T>,Eigen::Dynamic,Eigen::Dynamic> value;
189  pointset_lagrange_equispaced (hat_K, nsub, hat_node);
190  details::basis_on_pointset_evaluate (*this, hat_K, hat_node, value);
191  size_t loc_nnod = value.rows();
192  size_t loc_ndof = value.cols();
193  for (size_t loc_idof = 0; loc_idof < loc_ndof; ++loc_idof) {
194  for (size_t loc_inod = 0; loc_inod < loc_nnod; ++loc_inod) {
195  for (size_t mu = 0; mu < d; ++mu) {
196  gdat << hat_node[loc_inod][mu] << " ";
197  }
198  for (size_t mu = 0; mu < d; ++mu) {
199  gdat << value(loc_inod,loc_idof)[mu] << " ";
200  }
201  gdat << endl;
202  }
203  gdat << endl << endl;
204  }
205  break;
206  }
207 #ifdef TODO
208  case reference_element::T: {
209  for (size_type k = 0; k <= nsub; k++) {
210  for (size_type j = 0; j+k <= nsub; j++) {
211  for (size_type i = 0; i+j+k <= nsub; i++) {
212  point_basic<T> hat_x (T(i)/T(nsub), T(j)/T(nsub), T(k)/T(nsub));
213  eval_lagrange (hat_x, hat_K, degree, sopt, inv_vdm, value);
214  Lambda = max(Lambda, norm(value,1));
215  }
216  }
217  }
218  return Lambda;
219  }
220  case reference_element::P: {
221  for (size_type k = 0; k <= degree; k++) {
222  for (size_type j = 0; j <= degree; j++) {
223  for (size_type i = 0; i+j <= degree; i++) {
224  point_basic<T> hat_x (T(i)/T(nsub), T(j)/T(nsub), 2*T(k)/T(nsub)-1);
225  eval_lagrange (hat_x, hat_K, degree, sopt, inv_vdm, value);
226  Lambda = max(Lambda, norm(value,1));
227  }
228  }
229  }
230  break;
231  }
232  case reference_element::H: {
233  for (size_type k = 0; k <= degree; k++) {
234  for (size_type j = 0; j <= degree; j++) {
235  for (size_type i = 0; i <= degree; i++) {
236  point_basic<T> hat_x (2*T(i)/T(nsub)-1, 2*T(j)/T(nsub), 2*T(k)/T(nsub)-1);
237  eval_lagrange (hat_x, hat_K, degree, sopt, inv_vdm, value);
238  Lambda = max(Lambda, norm(value,1));
239  }
240  }
241  }
242  break;
243  }
244 #endif // TODO
245  default:
246  error_macro ("unexpected element type `"<<hat_K.name()<<"'");
247  }
248  // -----------
249  if (execute) {
250  // -----------
251  string command = "gnuplot \"" + plot_name + "\"";
252  if (verbose) clog << "! " << command << endl;
253  int status = system (command.c_str());
254  }
255  // -----------
256  if (clean) {
257  // -----------
258  string command = "/bin/rm -f " + filelist;
259  if (verbose) clog << "! " << command << endl;
260  int status = system (command.c_str());
261  }
262 }
263 // --------------------------------------------------------------------------
264 // show nodes
265 // --------------------------------------------------------------------------
266 template<class T>
267 static
268 void
269 put_hat_node_header (std::ostream& plot, reference_element hat_K)
270 {
272  size_type d = hat_K.dimension();
273  plot << "set colors classic" << endl
274  << "set nokey" << endl
275  << "set noborder" << endl
276  << "set noxtics" << endl
277  << "set noytics" << endl
278  << "set noztics" << endl
279  ;
280  if (d == 2){
281  plot << "set size ratio -1 # 2d equal scales" << endl;
282  } else if (d == 3) {
283  plot << "set view 70, 140 # x to left, y to right, z to top" << endl;
284  plot << "set view equal xyz # 3d equal scales" << endl;
285  }
286  plot << endl;
287 
288  // draw all edges
289  point_basic<T> xmin, xmax;
290  for (size_type mu = 0; mu < d; ++mu) {
291  xmin[mu] = 1;
292  xmax[mu] = -1;
293  }
294  for (size_type loc_isid = 0, loc_nsid = hat_K.n_subgeo(1); loc_isid < loc_nsid; ++loc_isid) {
295  size_type loc_ia = hat_K.subgeo_local_vertex (1, loc_isid, 0);
296  size_type loc_ib = hat_K.subgeo_local_vertex (1, loc_isid, 1);
297  const point& a = hat_K.vertex (loc_ia);
298  const point& b = hat_K.vertex (loc_ib);
299  plot << "set arrow from ";
300  size_type d2 = max(d,size_type(2));
301  for (size_type mu = 0; mu < d2; ++mu) {
302  plot << a[mu];
303  if (mu+1 != d2) plot << ", ";
304  xmin[mu] = min(xmin[mu],T(a[mu]));
305  xmax[mu] = max(xmax[mu],T(a[mu]));
306  }
307  plot << " to ";
308  for (size_type mu = 0; mu < d2; ++mu) {
309  plot << b[mu];
310  if (mu+1 != d2) plot << ", ";
311  xmin[mu] = min(xmin[mu],T(b[mu]));
312  xmax[mu] = max(xmax[mu],T(b[mu]));
313  }
314  plot << " nohead lc 0 lw 1" << endl;
315  }
316  T delta = 0.1;
317  plot << endl
318  << "set xrange ["<<xmin[0]-delta<<":"<<xmax[0]+delta<<"]"<<endl;
319  if (d >= 2) {
320  plot << "set yrange ["<<xmin[1]-delta<<":"<<xmax[1]+delta<<"]"<<endl;
321  } else {
322  plot << "set yrange [-1:1]"<<endl;
323  }
324  if (d >= 3) {
325  plot << "set zrange ["<<xmin[2]-delta<<":"<<xmax[2]+delta<<"]"<<endl
326  << "set xyplane at "<< xmin[2]-delta << endl;
327  }
328 }
329 template<class T>
330 void
331 basis_rep<T>::put_hat_node (std::ostream& os, reference_element hat_K) const
332 {
333  using namespace std;
334  bool verbose = iorheo::getverbose(os);
335  bool clean = iorheo::getclean(os);
336  bool execute = iorheo::getexecute(os);
337 
338  _initialize_data_guard (hat_K);
339  const Eigen::Matrix<point_basic<T>,Eigen::Dynamic,1>& hat_x = hat_node(hat_K);
340  size_type d = hat_K.dimension();
341 
342  string basename = "basis-" + name() + "-" + hat_K.name();
343  string filelist;
344  // --------------------
345  // .gdat
346  // --------------------
347  string gdat_name = basename + ".gdat";
348  ofstream gdat (gdat_name.c_str());
349  cerr << "! file \"" << gdat_name << "\" created" << endl;
350  filelist += " " + gdat_name;
351  gdat << setprecision(std::numeric_limits<T>::digits10)
352  << "# basis " << name() << endl
353  << "# element " << hat_K.name() << endl
354  << "# degree " << degree() << endl
355  << "# ndof " << ndof(hat_K) << endl
356  << "# raw_poly " << _sopt.get_raw_polynomial_name() << endl
357  << "# nodeset " << _sopt.get_node_name() << endl
358  << "# nnode " << hat_x.size() << endl
359  ;
360  if (! option().is_restricted_to_sides()) {
361  for (size_type dim = 0, d = hat_K.dimension(); dim <= d; ++dim) {
362  for (size_type loc_inod = first_inod_by_dimension(hat_K,dim), loc_nnod = first_inod_by_dimension(hat_K,dim+1); loc_inod < loc_nnod; ++loc_inod) {
363  hat_x [loc_inod].put (gdat, max(d,size_type(2)));
364  gdat << " " << loc_inod << endl;
365  }
366  gdat << endl << endl;
367  }
368  } else {
369  // basis_sides has no ordered nodes by dimension and no first_inod(K,dim)
370  for (size_type loc_inod = 0, loc_nnod = nnod(hat_K); loc_inod < loc_nnod; ++loc_inod) {
371  hat_x [loc_inod].put (gdat, max(d,size_type(2)));
372  gdat << " " << loc_inod << endl;
373  }
374  }
375  // --------------------
376  // .plot
377  // --------------------
378  string plot_name = basename + ".plot";
379  ofstream plot (plot_name.c_str());
380  cerr << "! file \"" << plot_name << "\" created" << endl;
381  filelist += " " + plot_name;
382  plot << "gdat = \"" << gdat_name << "\"" << endl
383  << "set title \"" << name() << ": nodes\"" << endl
384  ;
385  put_hat_node_header<T> (plot, hat_K) ;
386  string color[4] = {"#ff0000", "#008800", "#0000ff", "#ff00ff"};
387  bool need_coma = false;
388  size_type index = 0;
389  plot << ((d==3) ? "s" : "") << "plot \\" << endl;
390  if (! option().is_restricted_to_sides()) {
391  for (size_type dim = 0, d = hat_K.dimension(); dim <= d; ++dim) {
392  size_type nnod_dim = first_inod_by_dimension (hat_K, dim+1) - first_inod_by_dimension (hat_K, dim);
393  if (nnod_dim == 0) continue;
394  plot << " " << (need_coma ? ", " : " ") << "gdat \\" << endl
395  << " index " << index << " \\" << endl
396  << " w p ps 2 pt 7 lc rgb \"" << color[dim] << "\",\\" << endl
397  << " gdat \\" << endl
398  << " index " << index << " \\" << endl
399  << " u 1:2:3" << ((d==3) ? ":4" : "") << " w labels offset char 2 lc 0 \\" << endl
400  ;
401  need_coma = true;
402  index++;
403  }
404  } else {
405  plot << " gdat \\" << endl
406  << " w p ps 2 pt 7 lc rgb \"" << color[0] << "\",\\" << endl
407  << " gdat \\" << endl
408  << " u 1:2:3" << ((d==3) ? ":4" : "") << " w labels offset char 2 lc 0" << endl
409  ;
410  }
411  plot << endl
412  << "pause -1 \"<return>\"" << endl
413  ;
414  // -----------
415  if (execute) {
416  // -----------
417  string command = "gnuplot \"" + plot_name + "\"";
418  if (verbose) clog << "! " << command << endl;
419  int status = system (command.c_str());
420  }
421  // -----------
422  if (clean) {
423  // -----------
424  string command = "/bin/rm -f " + filelist;
425  if (verbose) clog << "! " << command << endl;
426  int status = system (command.c_str());
427  }
428 }
429 // --------------------------------------------------------------------------
430 // show nodes
431 // --------------------------------------------------------------------------
432 template<class T>
433 void
435  std::ostream& os,
436  reference_element hat_K,
437  const side_information_type& sid) const
438 {
439  using namespace std;
440  check_macro (is_nodal(), "side-node visualization: only nodal basis supported");
441  Eigen::Matrix<size_type,Eigen::Dynamic,1> loc_inod;
442  local_idof_on_side (hat_K, sid, loc_inod);
443 
444  bool verbose = iorheo::getverbose(os);
445  bool clean = iorheo::getclean(os);
446  bool execute = iorheo::getexecute(os);
447 
448  _initialize_data_guard (hat_K);
449  const Eigen::Matrix<point_basic<T>,Eigen::Dynamic,1>& hat_x = hat_node (hat_K);
450  size_type d = hat_K.dimension();
451 
452  string basename = "basis-" + name() + "-" + hat_K.name();
453  string filelist;
454  // --------------------
455  // .gdat
456  // --------------------
457  string gdat_name = basename + ".gdat";
458  ofstream gdat (gdat_name.c_str());
459  cerr << "! file \"" << gdat_name << "\" created" << endl;
460  filelist += " " + gdat_name;
461  gdat << setprecision(std::numeric_limits<T>::digits10)
462  << "# basis " << name() << endl
463  << "# element " << hat_K.name() << endl
464  << "# degree " << degree() << endl
465  << "# ndof " << ndof(hat_K) << endl
466  << "# raw_poly " << _sopt.get_raw_polynomial_name() << endl
467  << "# nodeset " << _sopt.get_node_name() << endl
468  << "# nnode " << hat_x.size() << endl
469  ;
470  for (size_type loc_sid_inod = 0, loc_sid_nnod = loc_inod.size(); loc_sid_inod < loc_sid_nnod; ++loc_sid_inod) {
471  hat_x [loc_inod[loc_sid_inod]].put (gdat, max(d,size_type(2)));
472  gdat << " " << loc_inod[loc_sid_inod] << endl;
473  }
474  // --------------------
475  // .plot
476  // --------------------
477  string plot_name = basename + ".plot";
478  ofstream plot (plot_name.c_str());
479  cerr << "! file \"" << plot_name << "\" created" << endl;
480  filelist += " " + plot_name;
481  plot << "gdat = \"" << gdat_name << "\"" << endl
482  << "set title \"" << name() << ": nodes\"" << endl
483  ;
484  put_hat_node_header<T> (plot, hat_K) ;
485  string color[4] = {"#ff0000", "#008800", "#0000ff", "#ff00ff"};
486  bool need_coma = false;
487  size_type index = 0;
488  plot << ((d==3) ? "s" : "") << "plot \\" << endl
489  << " gdat \\" << endl
490  << " w p ps 2 pt 7 lc rgb \"" << color[d] << "\",\\" << endl
491  << " gdat \\" << endl
492  << " u 1:2:3" << ((d==3) ? ":4" : "") << " w labels offset char 2 lc 0" << endl
493  << "pause -1 \"<return>\"" << endl
494  ;
495  // -----------
496  if (execute) {
497  // -----------
498  string command = "gnuplot \"" + plot_name + "\"";
499  if (verbose) clog << "! " << command << endl;
500  int status = system (command.c_str());
501  }
502  // -----------
503  if (clean) {
504  // -----------
505  string command = "/bin/rm -f " + filelist;
506  if (verbose) clog << "! " << command << endl;
507  int status = system (command.c_str());
508  }
509 }
510 // ----------------------------------------------------------------------------
511 // instanciation in library
512 // ----------------------------------------------------------------------------
513 #define _RHEOLEF_instanciation(T) \
514 template void basis_rep<Float>::put (ostream& os, reference_element hat_K) const; \
515 template void basis_rep<Float>::put_scalar_valued (ostream& os, reference_element hat_K) const; \
516 template void basis_rep<Float>::put_vector_valued (ostream& os, reference_element hat_K) const; \
517 template void basis_rep<Float>::put_hat_node (ostream& os, reference_element hat_K) const; \
518 template void basis_rep<Float>::put_hat_node_on_side ( \
519  std::ostream&, reference_element, const side_information_type&) const;
520 
522 
523 } // namespace rheolef
basis - finite element method
void put(idiststream &in, odiststream &out, bool do_proj, bool do_lumped_mass, bool def_fill_opt, size_type extract_id, const Float &scale_value, const std::pair< Float, Float > &u_range, render_type render)
Definition: branch.cc:500
field::size_type size_type
Definition: branch.cc:425
see the Float page for the full documentation
see the point page for the full documentation
reference_element::size_type size_type
Definition: basis.h:214
void put_hat_node_on_side(std::ostream &os, reference_element hat_K, const side_information_type &sid) const
void put_hat_node(std::ostream &os, reference_element hat_K) const
virtual void put_vector_valued(std::ostream &os, reference_element hat_K) const
see the reference_element page for the full documentation
const point_basic< Float > & vertex(size_type iloc) const
static const variant_type H
static const variant_type q
static const variant_type e
static const variant_type p
variant_type variant() const
size_type subgeo_local_vertex(size_type subgeo_dim, size_type loc_isid, size_type loc_jsidvert) const
std::vector< int >::size_type size_type
size_type n_subgeo(size_type subgeo_dim) const
static const variant_type T
static const variant_type P
static const variant_type t
size_t size_type
Definition: basis_get.cc:76
point_basic< T >
Definition: piola_fem.h:135
rheolef::std value
#define error_macro(message)
Definition: dis_macros.h:49
Expr1::float_type T
Definition: field_expr.h:261
check_macro(expr1.have_homogeneous_space(Xh1), "dual(expr1,expr2); expr1 should have homogeneous space. HINT: use dual(interpolate(Xh, expr1),expr2)")
Float delta(Float f, Float g)
string command
Definition: mkgeo_ball.sh:136
void put(const Basis &b, ostream &os, reference_element hat_K)
void basis_on_pointset_evaluate(const Basis &b, const reference_element &hat_K, const Eigen::Matrix< point_basic< T >, Eigen::Dynamic, 1 > &hat_x, Eigen::Matrix< Value, Eigen::Dynamic, Eigen::Dynamic > &vdm)
size_type nnod(const basis_basic< T > &b, const geo_size &gs, size_type map_dim)
size_type ndof(const basis_basic< T > &b, const geo_size &gs, size_type map_dim)
This file is part of Rheolef.
void pointset_lagrange_equispaced(reference_element hat_K, size_t order_in, Eigen::Matrix< point_basic< T >, Eigen::Dynamic, 1 > &hat_xnod, size_t internal=0)
Definition: equispaced.icc:44
T norm(const vec< T, M > &x)
norm(x): see the expression page for the full documentation
Definition: vec.h:387
_RHEOLEF_instanciation(Float, sequential, std::allocator< Float >) _RHEOLEF_instanciation(Float
space_constant::valued_type valued_tag() const