Rheolef  7.1
an efficient C++ finite element environment
geo_seq_visu_vtk_paraview.cc
Go to the documentation of this file.
1 //
22 // paraview vtk visualization
23 //
24 // author: Pierre.Saramito@imag.fr
25 //
26 // date: 12 may 1997 update: 23 oct 2011
27 //
28 #include "rheolef/geo.h"
29 #include "rheolef/piola_util.h"
30 #include "rheolef/rheostream.h"
31 #include "rheolef/iorheo.h"
32 #include "rheolef/iofem.h"
33 
34 #include "geo_seq_put_vtk.h"
35 
36 using namespace std;
37 namespace rheolef {
38 
39 
40 // ----------------------------------------------------------------------------
41 // python utils
42 // ----------------------------------------------------------------------------
43 template<class T>
44 static
45 std::string
46 python (const point_basic<T>& x, size_t d=3)
47 {
48  std::ostringstream os;
49  os << "[" << x[0];
50  for (size_t i = 1; i < d; i++)
51  os << ", " << x[i];
52  os << "]" << std::flush;
53  string buffer = os.str();
54  return buffer;
55 }
56 // ----------------------------------------------------------------------------
57 // geo puts
58 // ----------------------------------------------------------------------------
59 template <class T>
60 odiststream&
62 {
63  //
64  // 1) prerequises
65  //
66  using namespace std;
68  typedef point_basic<size_type> ilat;
69  ostream& os = ops.os();
70  bool verbose = iorheo::getverbose(os);
71  bool clean = iorheo::getclean(os);
72  bool execute = iorheo::getexecute(os);
73  bool fill = iorheo::getfill(os); // isocontours or color fill
74  bool elevation = iorheo::getelevation(os);
75  bool color = iorheo::getcolor(os);
76  bool gray = iorheo::getgray(os);
77  bool black_and_white = iorheo::getblack_and_white(os);
78  bool stereo = iorheo::getstereo(os);
79  bool full = iorheo::getfull(os);
80  bool iso = true;
81  bool cut = iorheo::getcut(os);
82  bool volume = iorheo::getvolume(os);
83  bool grid = iorheo::getgrid(os);
84  bool lattice = iorheo::getlattice(os);
85  bool shrink = iorheo::getshrink(os);
86  bool showlabel = iorheo::getshowlabel(os);
87  bool tube = iorheo::gettube(os);
88  bool ball = iorheo::getball(os);
89  string format = iorheo::getimage_format(os);
90  if (format == "tiff") format = "tif";
91  if (format == "jpeg") format = "jpg";
92  string basename = iorheo::getbasename(os);
93  point_basic<T> origin = iofem::getorigin(os);
94  point_basic<T> normal = iofem::getnormal(os);
95  point_basic<size_type> resolution = iofem::getresolution(os);
96  string outfile_fmt = "";
97  string tmp = get_tmpdir() + "/";
98  if (!clean) tmp = "";
99 
100  size_type dim = omega.dimension();
101  size_type map_dim = omega.map_dimension();
102  size_type nv = omega.sizes().ownership_by_dimension[0].size();
103  size_type nedg = omega.sizes().ownership_by_dimension[1].size();
104  size_type nfac = omega.sizes().ownership_by_dimension[2].size();
105  size_type nvol = omega.sizes().ownership_by_dimension[3].size();
107 
108 #if (_RHEOLEF_PARAVIEW_VERSION_MAJOR >= 5) && (_RHEOLEF_PARAVIEW_VERSION_MINOR >= 5)
109  // paraview version >= 5.5 has high order elements
110  bool high_order = omega.order() > 1 && !cut && !shrink;
111 #else
112  bool high_order = false;
113 #endif
114  //
115  // 2) output mesh data
116  //
117  string filelist;
118  string filename = tmp+basename + ".vtk";
119  filelist = filelist + " " + filename;
120  ofstream vtk_os (filename.c_str());
121  odiststream vtk (vtk_os);
122  if (verbose) clog << "! file \"" << filename << "\" created.\n";
123  size_type out_dim = high_order ? std::min (size_type(1),omega.map_dimension()) : omega.map_dimension();
124  geo_put_vtk (vtk, omega, omega.get_piola_basis(), omega.get_nodes(), true, out_dim);
125  vtk.close();
126  //
127  // 3) output domains data
128  //
129  for (size_type idom = 0, ndom = omega.n_domain(); idom < ndom; ++idom) {
130  const geo_basic<T,sequential>& dom = omega.get_domain(idom);
131  string filename = tmp+basename + "." + dom.name() + ".vtk";
132  filelist = filelist + " " + filename;
133  ofstream vtk_os_dom (filename.c_str());
134  odiststream vtk_dom (vtk_os_dom);
135  if (verbose) clog << "! file \"" << filename << "\" created.\n";
136  out_dim = high_order && !fill ? std::min (size_type(1),dom.map_dimension()) : dom.map_dimension();
137  geo_basic<T,sequential> dom_reduced_edges
138  = high_order && !fill && dom.map_dimension() > 1 ?
139  compact(dom) : dom; // strip internal edges in 3D surface domain
140  geo_put_vtk (vtk_dom, dom_reduced_edges, dom_reduced_edges.get_piola_basis(), dom_reduced_edges.get_nodes(), true, out_dim);
141  vtk_dom.close();
142  }
143  //
144  // 3) create python data file
145  //
146  std::string py_name = filename = tmp+basename + ".py";
147  filelist = filelist + " " + filename;
148  ofstream py (filename.c_str());
149  if (verbose) clog << "! file \"" << filename << "\" created.\n";
150  py << "#!/usr/bin/env paraview --script=" << endl
151  << "# This is a paraview script for the visualization of " << basename << ".vtk" << endl
152  << "# automatically generated by rheolef." << endl
153  << endl
154  ;
155  // paraview python library
156  py << "from paraview.simple import *" << endl
157  << "from paraview_rheolef import * # load rheolef specific functions" << endl
158  << endl
159  ;
160  py << "d = [ '" << tmp+basename << "'";
161  for (size_type idom = 0, ndom = omega.n_domain(); idom < ndom; ++idom) {
162  const geo_basic<T,sequential>& dom = omega.get_domain(idom);;
163  py << ", '" << dom.name() << "'";
164  }
165  py << "]" << endl;
166 
167  bool has_origin = (origin[0] != numeric_limits<Float>::max());
168  bool view_2d = (omega.xmax()[2] - omega.xmin()[2] == 0);
169  point xmin = omega.xmin(),
170  xmax = omega.xmax();
171  py << "opt = { \\" << endl
172  << " 'format' : '" << format << "', \\" << endl
173  << " 'resolution' : [" << resolution[0] << "," << resolution[1] << "], \\" << endl
174  << " 'showlabel': " << showlabel << "," << endl
175  << " 'axis' : 1," << endl
176  << " 'view_1d' : 0," << endl
177  << " 'view_2d' : " << view_2d << "," << endl
178  << " 'color' : '" << (color ? "color" : (gray ? "gray" : "black_and_white")) << "'," << endl
179  << " 'stereo' : " << stereo << ", \\" << endl
180  << " 'bbox' : [[" << xmin[0] << "," << xmin[1] << "," << xmin[2] << "],["
181  << xmax[0] << "," << xmax[1] << "," << xmax[2] << "]], \\" << endl
182  << " 'ball' : " << ball << ", \\" << endl
183  << " 'cut' : " << cut << ", \\" << endl
184  << " 'fill' : " << fill << ", \\" << endl
185  << " 'full' : " << full << ", \\" << endl
186  << " 'lattice' : " << lattice << ", \\" << endl
187  << " 'shrink' : " << shrink << ", \\" << endl
188  << " 'tube' : " << tube << ", \\" << endl
189  << " 'volume' : " << 0 << ", \\" << endl
190  << " 'has_origin' : " << has_origin << ", \\" << endl
191  << " 'origin' : " << python(origin) << ", \\" << endl
192  << " 'normal' : " << python(normal) << ", \\" << endl
193  << " 'elevation' : 0, \\" << endl
194  << " 'high_order' : "<< high_order << " \\" << endl
195  << " }" << endl
196  << endl
197  ;
198  py << "paraview_geo(paraview, opt, d)" << endl
199  << endl
200  ;
201  //
202  // 3) run pyton
203  //
204  int status = 0;
205  string command;
206  if (execute) {
207  string prog = (format == "") ? "paraview --script=" : "pvbatch --use-offscreen-rendering ";
208  command = "LANG=C PYTHONPATH=" + string(_RHEOLEF_PKGDATADIR) + " " + prog + py_name;
209  if (format != "") command = "DISPLAY=:0.0 " + command;
210  if (stereo && format == "") command = command + " --stereo";
211  if (verbose) clog << "! " << command << endl;
212  status = system (command.c_str());
213  }
214  //
215  // 4) clear vtk data
216  //
217  if (clean) {
218  command = "/bin/rm -f " + filelist;
219  if (verbose) clog << "! " << command << endl;
220  status = system (command.c_str());
221  }
222  return ops;
223 }
224 // ----------------------------------------------------------------------------
225 // instanciation in library
226 // ----------------------------------------------------------------------------
227 template odiststream& visu_vtk_paraview<Float> (odiststream&, const geo_basic<Float,sequential>&);
228 
229 }// namespace rheolef
field::size_type size_type
Definition: branch.cc:425
see the point page for the full documentation
size_type size(size_type iproc) const
Definition: distributor.h:163
const basis_basic< T > & get_piola_basis() const
Definition: geo.h:1155
const node_type & xmin() const
Definition: geo.h:1157
std::string name() const
Definition: geo.h:1146
size_type map_dimension() const
Definition: geo.h:1149
size_type n_domain() const
Definition: geo.h:1189
size_type dimension() const
Definition: geo.h:1148
const node_type & xmax() const
Definition: geo.h:1158
const disarray< node_type, sequential > & get_nodes() const
Definition: geo.h:1177
geo_basic< T, sequential > get_domain(size_type i) const
size_type order() const
Definition: geo.h:1156
const geo_size & sizes() const
Definition: geo.h:1162
generic mesh with rerefence counting
Definition: geo.h:1089
odiststream: see the diststream page for the full documentation
Definition: diststream.h:126
std::ostream & os()
Definition: diststream.h:236
#define _RHEOLEF_PKGDATADIR
Definition: config.h:234
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
verbose clean transpose logscale grid shrink ball stereo iso volume skipvtk deformation fastfieldload lattice reader_on_stdin gray
string command
Definition: mkgeo_ball.sh:136
This file is part of Rheolef.
odiststream & geo_put_vtk(odiststream &ops, const geo_basic< T, sequential > &omega, const basis_basic< T > &my_numb, const disarray< point_basic< T >, sequential > &my_node, bool append_data, size_t subgeo_dim)
odiststream & visu_vtk_paraview(odiststream &, const field_basic< T, sequential > &)
std::string get_tmpdir()
get_tmpdir: see the rheostream page for the full documentation
Definition: rheostream.cc:50
geo_basic< T, M > compact(const geo_basic< T, M > &gamma)
Definition: geo.cc:219
details::field_expr_v2_nonlinear_terminal_function< details::normal_pseudo_function< Float > > normal()
normal: see the expression page for the full documentation
std::string python(const point_basic< T > &x, size_t d=3)
distributor ownership_by_dimension[4]
Definition: geo_size.h:63