Rheolef  7.1
an efficient C++ finite element environment
branch_seq_visu_vtk_paraview.cc
Go to the documentation of this file.
1 //
22 // animations using paraview
23 //
24 // author: Pierre.Saramito@imag.fr
25 //
26 // date: 25 janv 2020
27 //
28 #include "rheolef/branch.h"
29 #include "rheolef/field_expr.h"
30 #include "rheolef/piola_util.h"
31 #include "rheolef/rheostream.h"
32 #include "rheolef/iorheo.h"
33 #include "rheolef/iofem.h"
34 #include "rheolef/interpolate.h"
35 #include "rheolef/render_option.h"
36 
37 namespace rheolef {
38 using namespace std;
39 
40 // extern: in "field_seq_visu_vtk_paraview.cc"
41 template<class T> std::string python (const point_basic<T>& x, size_t d=3);
42 
43 template<class T>
44 void
46 {
47  ostream& os = out.os();
48  iorheo::setbranch_counter(os, 0);
49  b._u_range = std::make_pair( std::numeric_limits<T>::max(),
50  -std::numeric_limits<T>::max());
51  b._have_u_range = std::make_pair(false,false);
52 }
53 template<class T>
54 void
56 {
57  ostream& os = out.os();
58  // ---------------
59  // update _u_range
60  // ---------------
61  size_t idx = 0;
62  string mark = iorheo::getmark(os);
63  if (mark == "") {
64  mark = b[0].first;
65  } else {
66  for (; idx < b.n_field(); ++idx) if (mark == b[idx].first) break;
67  check_macro (idx < b.n_field(), "mark `" << mark << "' not found in branch");
68  }
69  const field_basic<T,sequential>& uh = b[idx].second;
70  b._u_range.first = std::min (b._u_range.first, uh.min());
71  b._u_range.second = std::max (b._u_range.second, uh.max());
72  // ---------------
73  // put vtk
74  // ---------------
75  size_t branch_counter = iorheo::getbranch_counter(os);
76  iorheo::setbranch_counter(os, branch_counter+1);
77  bool skipvtk = iorheo::getskipvtk(os);
78  if (skipvtk) return;
79  bool clean = iorheo::getclean(os);
80  string basename = iorheo::getbasename(os);
81  if (basename == "") basename = "output";
82  string tmp = get_tmpdir() + "/";
83  if (!clean) tmp = "";
84  string data_file_name = tmp + basename + "-" + itos(b._count_value) + ".vtk";
85  ofstream vtk (data_file_name.c_str());
86  odiststream out_vtk (vtk);
87  bool verbose = iorheo::getverbose(clog);
88  verbose && clog << "! file `" << data_file_name << "' created" << endl;
89  put_event_vtk_stream (out_vtk, b);
90  vtk.close();
91 }
92 // ----------------------------------------------------------------------------
93 // paraview render call
94 // ----------------------------------------------------------------------------
95 template<class T>
96 void
98 {
99  // TODO: 99% of code similar with field_seq_visu_vtk_paraview.cc => merge
100  // also support anim for vector & tensor
101  //
102  // 0) prerequises
103  //
104  using namespace std;
107  typedef point_basic<size_type> ilat;
108  if (b.n_field() == 0) {
109  warning_macro ("empty branch: animation skipped");
110  return;
111  }
112  ostream& os = out.os();
113  //
114  // 1) set render options
115  //
116  render_option popt;
117  popt.mark = iorheo::getmark(os);
118  size_t idx = 0;
119  if (popt.mark == "") {
120  popt.mark = b[0].first;
121  } else {
122  for (; idx < b.n_field(); ++idx) if (popt.mark == b[idx].first) break;
123  check_macro (idx < b.n_field(), "mark `" << popt.mark << "' not found in branch");
124  }
125  const field_basic<T,sequential>& uh = b[idx].second;
126  string valued = uh.get_space().valued();
127  bool is_scalar = (valued == "scalar");
128  if (popt.mark != "" && !is_scalar) popt.mark = "|"+popt.mark +"|";
129  popt.fill = iorheo::getfill(os); // isocontours or color fill
130  popt.elevation = iorheo::getelevation(os);
131  popt.color = iorheo::getcolor(os);
132  popt.gray = iorheo::getgray(os);
133  popt.black_and_white = iorheo::getblack_and_white(os);
134  popt.showlabel = iorheo::getshowlabel(os);
135  popt.stereo = iorheo::getstereo(os);
136  popt.volume = iorheo::getvolume(os);
137  popt.iso = true;
138  popt.cut = iorheo::getcut(os);
139  popt.grid = iorheo::getgrid(os);
140  popt.format = iorheo::getimage_format(os);
141  popt.scale = iorheo::getvectorscale(os);
142  popt.origin = iofem::getorigin(os);
143  popt.normal = iofem::getnormal(os);
144  popt.resolution = iofem::getresolution(os);
145  popt.n_isovalue = iorheo::getn_isovalue(os);
146  popt.n_isovalue_negative = iorheo::getn_isovalue_negative(os);
147  popt.isovalue = iorheo::getisovalue(os); // isovalue is always a Float
148  popt.label = iorheo::getlabel(os);
149 
150  const geo_basic<float_type,sequential>& omega = uh.get_geo();
151  size_type dim = omega.dimension();
152  size_type map_dim = omega.map_dimension();
153  popt.view_2d = (dim == 2);
154  popt.view_map = (dim > map_dim);
155 #if (_RHEOLEF_PARAVIEW_VERSION_MAJOR >= 5) && (_RHEOLEF_PARAVIEW_VERSION_MINOR >= 5)
156  // paraview version >= 5.5 has high order elements
157  popt.high_order = (omega.order() > 1 || uh.get_space().degree() > 1);
158 #else
159  popt.high_order = false;
160 #endif
161  popt.xmin = uh.get_geo().xmin();
162  popt.xmax = uh.get_geo().xmax();
163  popt.branch_size = iorheo::getbranch_counter(os);
164  // min and max over all the branch
165  popt.f_min = b._u_range.first;
166  popt.f_max = b._u_range.second;
167  //
168  // 2) create python data file
169  //
170  bool verbose = iorheo::getverbose(os);
171  bool clean = iorheo::getclean(os);
172  bool execute = iorheo::getexecute(os);
173  bool skipvtk = iorheo::getskipvtk(os);
174  string tmp = get_tmpdir() + "/";
175  if (skipvtk || !clean) tmp = "";
176  string clean_filelist;
177  string basename = iorheo::getbasename(os);
178  if (basename == "") basename = "output";
179  std::string py_name = tmp+basename + ".py";
180  clean_filelist = clean_filelist + " " + py_name;
181  ofstream py (py_name.c_str());
182  if (verbose) clog << "! file \"" << py_name << "\" created.\n";
183  py << popt
184  << "filelist = [";
185  for (size_type i = 0; i < popt.branch_size; ++i) {
186  string i_vtk_name = tmp + basename + "-" + itos(i) + ".vtk";
187  string opt_coma = (i+1 != popt.branch_size) ? ", " : "";
188  py << "\"" << i_vtk_name + "\"" + opt_coma;
189  if (!skipvtk) clean_filelist = clean_filelist + " " + i_vtk_name;
190  }
191  py << "]" << endl
192  << endl
193  << "paraview_branch_" << valued << "(paraview, \"" << tmp+basename << "\", filelist, opt)" << endl
194  << endl
195  ;
196  py.close();
197  //
198  // 3) run pyton
199  //
200  int status = 0;
201  string command;
202  if (execute) {
203  string prog = (popt.format == "") ? "paraview --script=" : "pvbatch --use-offscreen-rendering ";
204  command = "LANG=C PYTHONPATH=" + string(_RHEOLEF_PKGDATADIR) + " " + prog + py_name;
205  if (popt.format != "") command = "DISPLAY=:0.0 " + command;
206  if (popt.stereo && popt.format == "") command = command + " --stereo";
207  if (verbose) clog << "! " << command << endl;
208  status = system (command.c_str());
209  }
210  //
211  // 4) clear vtk and py files
212  //
213  if (clean) {
214  command = "/bin/rm -f " + clean_filelist;
215  if (verbose) clog << "! " << command << endl;
216  status = system (command.c_str());
217  }
218 }
219 // ----------------------------------------------------------------------------
220 // instanciation in library
221 // ----------------------------------------------------------------------------
225 
226 } // namespace rheolef
field::size_type size_type
Definition: branch.cc:425
const space_type & get_space() const
Definition: field.h:300
const geo_type & get_geo() const
Definition: field.h:301
T min() const
Definition: field.h:683
T max() const
Definition: field.h:699
typename float_traits< T >::type float_type
Definition: field.h:245
generic mesh with rerefence counting
Definition: geo.h:1089
odiststream: see the diststream page for the full documentation
Definition: diststream.h:126
typename float_traits< value_type >::type float_type
size_t size_type
Definition: basis_get.cc:76
#define _RHEOLEF_PKGDATADIR
Definition: config.h:240
#define warning_macro(message)
Definition: dis_macros.h:53
check_macro(expr1.have_homogeneous_space(Xh1), "dual(expr1,expr2); expr1 should have homogeneous space. HINT: use dual(interpolate(Xh, expr1),expr2)")
verbose clean transpose logscale grid shrink ball stereo iso volume skipvtk deformation fastfieldload lattice reader_on_stdin color format format format format format format format format format format format format format format format format vtk
string command
Definition: mkgeo_ball.sh:136
This file is part of Rheolef.
void put_event_paraview(odiststream &out, const branch_basic< T, sequential > &b)
void put_event_vtk_stream(odiststream &out_vtk, const branch_basic< T, sequential > &b)
void put_header_paraview(odiststream &out, const branch_basic< T, sequential > &b)
std::string itos(std::string::size_type i)
itos: see the rheostream page for the full documentation
std::string get_tmpdir()
get_tmpdir: see the rheostream page for the full documentation
Definition: rheostream.cc:50
std::string python(const point_basic< T > &x, size_t d=3)
void put_finalize_paraview(odiststream &out, const branch_basic< T, sequential > &b)
point_basic< size_t > resolution
Definition: render_option.h:48