~ubuntu-branches/ubuntu/trusty/rheolef/trusty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
///
/// This file is part of Rheolef.
///
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
///
/// Rheolef is free software; you can redistribute it and/or modify
/// it under the terms of the GNU General Public License as published by
/// the Free Software Foundation; either version 2 of the License, or
/// (at your option) any later version.
///
/// Rheolef is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with Rheolef; if not, write to the Free Software
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
///
/// =========================================================================
//
// visualization for vector fields
//  - with velocity arrow  (fluid)
//  - with deformed meshes (elasticity)
//
// author: Pierre.Saramito@imag.fr
//
// date: 12 february 2001
//
#include "rheolef/field.h"
#include "rheolef/geo-visu-aux.h"
using namespace std;
namespace rheolef {

Float maxi(Float a,Float b) { return (a>b)?a:b; }
int
field::gnuplot2d_velocity (const string& basename, plot_options& opt) const
{
  char filename [1000];
  sprintf (filename, "%s.plot", basename.c_str());
  ofstream plot (filename);
  space V0 = space(_V[0]);
  space V1 = space(_V[1]);	// TODO: can be better: avoid memory
  field u0 = get_comp(V0, 0);
  field u1 = get_comp(V1, 1);	// TODO: can be better: avoid memory 

  const geo& g = get_geo();
  if (opt.verbose) clog << "! file \"" << filename << "\" created.\n";
  plot << "#!gnuplot\n";
  if (!opt.bare)
  	plot << "set title \"" << basename << "\"" << endl;
  else
  	plot << "set nokey" << endl;
  if (opt.empty) 
   {
   	plot << "set noborder" << endl;
   	plot << "set noxtics" << endl;
   	plot << "set noytics" << endl;
   }
  if (opt.xmax==opt.xmin) 
   { 
   	opt.xmin=g.xmin(); 
	opt.xmax=g.xmax(); 
	point diag = opt.xmax-opt.xmin;
	Float w=opt.border*::sqrt(sqr(diag[0])+sqr(diag[1]));
	Float al=maxi(0,-u0.min());
	Float ar=maxi(0,u0.max());
	Float ab=maxi(0,-u1.min());
	Float at=maxi(0,u1.max());
	opt.xmax[0]+=w+ar*opt.vscale;
	opt.xmax[1]+=w+at*opt.vscale;
	opt.xmin[0]-=w+al*opt.vscale;
	opt.xmin[1]-=w+ab*opt.vscale;
	if (opt.xmirror) opt.xmin[0]=-opt.xmax[0];
	if (opt.ymirror) opt.xmin[1]=-opt.xmax[1];
   }
  if (opt.xmin[0]==opt.xmax[0])
   { 
	point diag = opt.xmax-opt.xmin;
	Float w=.05*::sqrt(sqr(diag[0])+sqr(diag[1]));
	Float al=maxi(0,-u0.min());
	Float ar=maxi(0,u0.max());
	opt.xmax[0]+=w+ar*opt.vscale;
	opt.xmin[0]-=w+al*opt.vscale;
   }
  if (opt.xmin[1]==opt.xmax[1])
   { 
	point diag = opt.xmax-opt.xmin;
	Float w=.05*::sqrt(sqr(diag[0])+sqr(diag[1]));
	Float al=maxi(0,-u1.min());
	Float ar=maxi(0,u1.max());
	opt.xmax[1]+=w+ar*opt.vscale;
	opt.xmin[1]-=w+al*opt.vscale;
   }
  if (opt.equal_scales)
   {
   	plot << "set size ratio -1" << endl;
   }

  if (opt.postscript && opt.noxplot)  
   {
	plot << "set output '" << basename << ".eps'" << endl;
	g.write_gnuplot_postscript_options(plot,opt);
   }
  ofstream gdat;
  string coma=",";
  plot << "vscale=" << opt.vscale << endl;
  if (opt.meshplot) plot << "load \"" << basename << ".mesh.plot\"" << endl << "replot "; 
  else 
   {
     if (!opt.keep_user_range) 
      {
     	plot << "set xrange ["<<opt.xmin[0]<<":"<<opt.xmax[0]<<"]" << endl;
     	plot << "set yrange ["<<opt.xmin[1]<<":"<<opt.xmax[1]<<"]" << endl;
      }
     plot << "plot ";
   }
  plot << "\"" << basename 
  		<< ".gdat\" using 1:2:(vscale*$4):(vscale*$5) title \"" 
		<< basename << "\" with vectors linetype 3 \\\n";
  if (opt.xmirror) plot << ", \"" << basename 
		<< ".gdat\" using (-$1):2:(-vscale*$4):(vscale*$5) notitle with vectors linetype 3 \\\n";
  if (opt.ymirror) plot << ", \"" << basename 
  	<< ".gdat\" using 1:(-$2):(vscale*$4):(-vscale*$5) notitle with vectors linetype 3 \\\n";
  if (opt.xmirror && opt.ymirror) plot << ", \"" << basename 
  	<< ".gdat\" using (-$1):(-$2):(-vscale*$4):(-vscale*$5) notitle with vectors linetype 3 \\\n";
  plot << endl;
   
  sprintf (filename, "%s.gdat", basename.c_str());
  gdat.open (filename);
  if (opt.verbose) clog << "! file \"" << filename << "\" created.\n";
  //
  // plot vectors
  //
  tiny_vector<size_type> dof0;
  tiny_vector<size_type> dof1;
  geo::const_iterator last_K = g.end();
  vector<bool> done(V0.size(), false);
  for (geo::const_iterator iter_K = g.begin(); iter_K != last_K; iter_K++) {
      const geo_element& K = *iter_K;
      V0.set_dof(K, dof0);
      V1.set_dof(K, dof1);
      for (geo::size_type i = 0; i < dof0.size(); i++) {
	  if (done[dof0(i)]) {
	     continue;
	  }
          point x_i = V0.x_dof(K, i);
          Float u0_i = u0.at(dof0(i));
          Float u1_i = u1.at(dof1(i));
          gdat << x_i << "\t" << u0_i << " " << u1_i << endl;
          done[dof0(i)] = true;
      }
  }
  //
  // end of plot
  //
  plot << endl; // because of the trailing "\"
  plot << endl; 
  if (opt.pause) plot << "pause -1 \"<return>\"\n";
  if (opt.postscript && (!opt.noxplot)) 
   {
	plot << "set output '" << basename << ".eps'" << endl;
   	g.write_gnuplot_postscript_options(plot,opt);
	plot << "replot";
   }
  plot.close();
  //
  // run gnuplot
  //
  if (opt.meshplot) 
   {
	plot_options optm(opt);
	optm.execute=false; 
	optm.postscript=false;
	optm.bare=true; 
	optm.clean=false;
	g.gnuplot2d (basename+".mesh", optm);
   }
  int status = 0;
  char command [1000];
  if (opt.execute) {
      sprintf (command, "gnuplot -persist %s.plot", basename.c_str());
      if (opt.verbose) clog << "! " << command << endl;
      status = system (command);
  }
  //
  // clear gnuplot data
  //
  if (opt.clean) {
      sprintf (command, "/bin/rm -f %s.plot %s.gdat", basename.c_str(), basename.c_str());
      if (opt.verbose) clog << "! " << command << endl;
      status |= system (command);
      if (opt.meshplot)
      {
      	sprintf (command, "/bin/rm -f %s.mesh.plot %s.mesh.gdat", basename.c_str(), basename.c_str());
      	if (opt.verbose) clog << "! " << command << endl;
      	status |= system (command);
      }
  }
  return status;
}

int
field::plotmtv_velocity (const string& basename, 
	bool execute, bool clean, bool verbose,
	const Float& vscale, bool put_domains) const
{
  string mtv_name = basename + ".mtv";
  ofstream mtv (mtv_name.c_str());
  if (verbose) clog << "! file \"" << mtv_name << "\" created.\n";
  int digits10 = numeric_limits<Float>::digits10;
  mtv << setprecision(digits10);
  //
  // header
  //
  mtv << "#!plotmtv\n"
      << "$ DATA = VECTOR\n"
      << "% comment    = \"rheolef\"\n"
      << "% toplabel   = \"" << basename << "\"\n"
      << "% subtitle   = \"\"\n"
      << "% equalscale = on\n"
      << "% vscale     = " << vscale << "\n"
      ;
  //
  // data
  //
  const geo& g = get_geo();
  space V0 = space(_V[0]);
  space V1 = space(_V[1]);	// TODO: can be better: avoid memory
  space V2;
  if (g.dimension() == 3) {
        V2 = _V[2];
  }
  field u0 = get_comp(V0, 0);
  field u1 = get_comp(V1, 1);	// TODO: can be better: avoid memory 
  field u2;
  if (g.dimension() == 3) {
        u2 = get_comp(V2, 2);
  }
  tiny_vector<size_type> dof0;
  tiny_vector<size_type> dof1;
  tiny_vector<size_type> dof2;
  geo::const_iterator last_K = g.end();
  vector<bool> done(V0.size(), false);
  for (geo::const_iterator iter_K = g.begin(); iter_K != last_K; iter_K++) {
      const geo_element& K = *iter_K;
      V0.set_dof(K, dof0);
      V1.set_dof(K, dof1);
      if (g.dimension() == 3) {
          V2.set_dof(K, dof2);
      }
      for (geo::size_type i = 0; i < dof0.size(); i++) {
	  if (done[dof0(i)]) {
	     continue;
	  }
          point x_i = V0.x_dof(K, i);
          Float u0_i = u0.at(dof0(i));
          Float u1_i = u1.at(dof1(i));
          Float u2_i = 0;
          if (g.dimension() == 3) {
                u2_i = u2.at(dof2(i));
	  }
          mtv << x_i << "\t" << u0_i << " " << u1_i << " " << u2_i << endl;
          done[dof0(i)] = true;
      }
  }
  //
  // plot domains if required
  //
  if (put_domains) 
   {
      mtv << "$ DATA = CURVE2D" << endl;
      g.put_mtv_domains(mtv,1);
   }
  mtv.close();
  //
  // run plotmtv
  //
  int status = 0;
  if (execute) {
      string option;
      if (g.dimension() == 3) {
 	 option = "-3d ";
      }
      if (put_domains) option += "-plotall ";
      string command;
      if (verbose) {
	  command = "plotmtv " + option + basename + ".mtv 1>&2";
          clog << "! " << command << endl;
      } else {
	  command = "plotmtv " + option + basename + ".mtv >/dev/null";
      }
      status = system (command.c_str());
  }
  //
  // clear plotmtv data
  //
  if (clean) {
      string command = "/bin/rm -f " + basename + ".mtv";
      if (verbose) clog << "! " << command << endl;
      status |= system (command.c_str());
  }
  return status;
}

ostream&
field::put_vtk_vector (ostream& vtk, const string& name, bool put_header) const
{
  const geo& g = get_geo();
  space V0 = space(_V[0]);
  space V1 = space(_V[1]);	// TODO: can be better: avoid memory
  space V2;
  if (g.dimension() == 3) {
        V2 = space(_V[2]);
  }
  field u0 = get_comp(V0, 0);
  field u1 = get_comp(V1, 1);	// TODO: can be better: avoid memory 
  field u2;
  //
  // put norm(u) as required scalar field for vector color
  //
  field norm_u;
  if (g.dimension() == 3) {
        u2 = get_comp(V2, 2);
	norm_u = sqrt(u0*u0+u1*u1+u2*u2);
  } else {
	norm_u = sqrt(u0*u0+u1*u1);
  }
  norm_u.put_vtk(vtk, name+"_norm", put_header);
  //
  // put vector data
  //
  vtk << "VECTORS " << name << " float" << endl;
  for (size_type i = 0; i < u0.size(); i++) {
      Float u0_i = u0.at(i);
      Float u1_i = u1.at(i);
      Float u2_i = 0;
      if (g.dimension() == 3) {
            u2_i = u2.at(i);
      }
      vtk << u0_i << " " << u1_i << " " << u2_i << endl;
  }
  vtk << endl;
  return vtk;
}
ostream&
field::put_vtk_tensor (ostream& vtk, const string& name, bool put_header) const
{
  const geo& g = get_geo();
  //
  // put norm(tau) as required scalar field for tensor color
  //
  field norm_tau;
  switch (g.dimension()) {
  case 1:
	norm_tau = abs(operator()(0,0)); 
	break;
  case 2:
	norm_tau = sqrt(
	            sqr(operator()(0,0)) 
		  + sqr(operator()(0,1)) 
		  + sqr(operator()(1,1)) 
		  + sqr(operator()(1,0)) 
	         );
	break;
  default:
	norm_tau = sqrt(
	            sqr(operator()(0,0)) 
		  + sqr(operator()(0,1)) 
		  + sqr(operator()(1,1)) 
		  + sqr(operator()(1,0)) 
		  + sqr(operator()(2,0)) 
		  + sqr(operator()(2,1)) 
		  + sqr(operator()(2,2)) 
		  + sqr(operator()(0,2)) 
		  + sqr(operator()(1,2)) 
	         );
	break;
  }
  norm_tau.put_vtk(vtk, name+"_norm", put_header);
  //
  // put tensors data
  //
  vtk << "TENSORS " << name << " float" << endl;
  switch (g.dimension()) {
  case 1: {
    field t00 = operator()(0,0);
    for (size_type i = 0; i < norm_tau.size(); i++) {
      vtk << t00.at(i) << " 0 0" << endl
          << "0 0 0" << endl
          << "0 0 0" << endl;
    }
    break;
  }
  case 2: {
    field t00 = operator()(0,0);
    field t01 = operator()(0,1);
    field t11 = operator()(1,1);
    field t10 = operator()(1,0);
    for (size_type i = 0; i < norm_tau.size(); i++) {
      vtk << t00.at(i) << " " << t01.at(i) << " 0" << endl
          << t10.at(i) << " " << t11.at(i) << " 0" << endl
          << "0 0 0" << endl;
    }
    break;
  }
  default: {
    field t00 = operator()(0,0);
    field t01 = operator()(0,1);
    field t11 = operator()(1,1);
    field t10 = operator()(1,0);
    field t02 = operator()(0,2);
    field t12 = operator()(1,2);
    field t22 = operator()(2,2);
    field t20 = operator()(2,0);
    field t21 = operator()(2,1);
    for (size_type i = 0; i < norm_tau.size(); i++) {
      vtk << t00.at(i) << " " << t01.at(i) << " " << t02.at(i) << endl
          << t10.at(i) << " " << t11.at(i) << " " << t12.at(i) << endl
          << t20.at(i) << " " << t21.at(i) << " " << t22.at(i) << endl;
    }
    break;
  }}
  vtk << endl;
  return vtk;
}

int
field::vtk_velocity (const string& basename, 
	bool execute, bool clean, bool verbose,
	const Float& vscale) const
{
  string vtk_name = basename + ".vtk";
  ofstream vtk (vtk_name.c_str());
  if (verbose) clog << "! file \"" << vtk_name << "\" created.\n";
  int digits10 = numeric_limits<Float>::digits10;
  vtk << setprecision(digits10);
  //
  // header
  //
  //
  // output geometry and data
  //
  const geo& g = get_geo();
  if (get_approx() == "P1") {
     vtk << vtkdata << get_geo();
  } else if (get_approx() == "P2") {
     vtk << lattice << vtkdata << get_geo();
  } else {
     fatal_macro ("vtk_vector: P1 or P2 approximation expected");
  }
  put_vtk_vector (vtk);
  vtk.close();
  //
  // output tcl script
  //
  string tcl_name = basename + ".tcl";
  ofstream tcl (tcl_name.c_str());
  if (verbose) clog << "! file \"" << tcl_name << "\" created.\n";
  tcl << setprecision(digits10);
 
  tcl << "# this is a tcl version of " << basename << ".vtk visualization\n"
      << "source \"" << _RHEOLEF_PKGDATADIR << "/vtk_interactor.tcl\"\n"
      << "#\n"
      << "# edit parameters:\n"
      << "#\n"
      << "set vscale          " << vscale << ";\n"
      << "set axis_color       \"0. 0. 0.\";\n"
      << "set outline_color    \"0. 0. 0.\";\n"
      << "set background_color \"1. 1. 1.\";\n"
      << "#\n"
      << "# Create rendering stuff\n"
      << "#\n"
      << "vtkRenderer ren1;\n"
      << "vtkRenderWindow render_window;\n"
      << "    render_window AddRenderer ren1;\n"
      << "vtkRenderWindowInteractor iren;\n"
      << "    iren SetRenderWindow render_window;\n"
      << "#\n"
      << "# load data\n"
      << "#\n"
      << "vtkUnstructuredGridReader reader;\n"
      << "    reader SetFileName \"" << basename << ".vtk\";\n"
      << "    reader Update;\n"
      << "#\n"
      << "# vector actor\n"
      << "#\n"
      << "vtkHedgeHog vector_filter;\n"
      << "    vector_filter SetInput [reader GetOutput];\n"
      << "    vector_filter SetScaleFactor $vscale;\n"
      << "\n"
      << "vtkLookupTable lookup_table;\n"
      << "    lookup_table Build;\n"
      << "\n"
      << "vtkPolyDataMapper vector_mapper;\n"
      << "    vector_mapper SetInput [vector_filter GetOutput];\n"
      << "    vector_mapper SetLookupTable lookup_table;\n"
      << "    vector_mapper ImmediateModeRenderingOn;\n"
      << "    eval vector_mapper SetScalarRange [[reader GetOutput] GetScalarRange];\n"
      << "\n"
      << "vtkActor vector_actor;\n"
      << "    vector_actor SetMapper vector_mapper;\n"
      << "    # eval [vector_actor GetProperty] SetColor 1 0.49 0.25;\n"
      << "\n"
      << "ren1 AddActor vector_actor;\n"
      << "#\n"
      << "# outline actor\n"
      << "#\n"
      << "vtkOutlineFilter outline_filter;\n"
      << "    outline_filter SetInput [reader GetOutput];\n"
      << "vtkPolyDataMapper outline_mapper;\n"
      << "    outline_mapper SetInput [outline_filter GetOutput];\n"
      << "vtkActor outline_actor;\n"
      << "    outline_actor SetMapper outline_mapper;\n"
      << "    eval [outline_actor GetProperty] SetColor $outline_color\n"
      << "set outline_prop [outline_actor GetProperty];\n"
      << "\n"
      << "ren1 AddActor outline_actor\n"
      << "#\n"
      << "# axis actor\n"
      << "#\n"
      << "vtkCubeAxesActor2D axis_actor;\n"
      << "    axis_actor SetInput [reader GetOutput];\n"
      << "    axis_actor SetCamera [ren1 GetActiveCamera];\n"
      << "    axis_actor SetLabelFormat \"%6.4g\";\n"
      << "    if {[get_vtk_major_version] >= 4 && [get_vtk_minor_version] > 0} {\n"
      << "        vtkTextProperty axis_actor_property\n"
      << "        axis_actor_property ShadowOn\n"
      << "        axis_actor SetAxisLabelTextProperty axis_actor_property\n"
      << "        axis_actor SetAxisTitleTextProperty axis_actor_property\n"
      << "    } else {\n"
      << "        axis_actor ShadowOn;\n"
      << "    }\n"
      << "    axis_actor SetFlyModeToOuterEdges;\n"
      << "    axis_actor SetFontFactor 0.8;\n"
      << "    eval [axis_actor GetProperty] SetColor $axis_color;\n"
      << "\n"
      << "ren1 AddProp  axis_actor\n"
      << "#\n"
      << "# color bar\n"
      << "#\n"
      << "vtkScalarBarActor bar_actor\n"
      << "    bar_actor SetLookupTable [vector_mapper GetLookupTable]\n"
      << "    bar_actor SetOrientationToHorizontal\n"
      << "    [bar_actor GetProperty] SetColor 0 0 0\n"
      << "    [bar_actor GetPositionCoordinate] SetCoordinateSystemToNormalizedViewport\n"
      << "    [bar_actor GetPositionCoordinate] SetValue 0.1 0.01\n"
      << "    bar_actor SetOrientationToHorizontal\n"
      << "    bar_actor SetWidth  0.8\n"
      << "    bar_actor SetHeight 0.10\n"
      << "    bar_actor SetTitle \"velocity module\"\n"
      << "    if {[get_vtk_major_version] >= 4 && [get_vtk_minor_version] > 0} {\n"
      << "        vtkTextProperty bar_text_property\n"
      << "        bar_text_property BoldOn\n"
      << "        bar_actor SetLabelTextProperty bar_text_property\n"
      << "        bar_actor SetTitleTextProperty bar_text_property\n"
      << "    } else {\n"
      << "        bar_actor BoldOn\n"
      << "    }\n"
      << "    eval [bar_actor GetProperty] SetColor $axis_color;\n"
      << "\n"
      << "ren1 AddActor bar_actor;\n"
      << "#\n"
      << "# render the image\n"
      << "#\n"
      << "render_window SetSize 500 500;\n"
      << "eval ren1 SetBackground $background_color;\n"
      << "if { [get_vtk_major_version] >= 4 && [get_vtk_minor_version] > 0} {\n"
      << "    iren AddObserver UserEvent {wm deiconify .vtkInteract};\n"
      << "} else {\n"
      << "    iren SetUserMethod {wm deiconify .vtkInteract};\n"
      << "}\n"
      << "render_window Render;\n"
      << "#\n"
      << "# prevent the tk window from showing up then start the event loop\n"
      << "#\n"
      << "wm withdraw .\n"
    ;
  tcl.close();
  //
  // run vtk
  //
  int status = 0;
  string command = "vtk " + tcl_name;
  if (execute) {
      if (verbose) clog << "! " << command << endl;
      status = system (command.c_str());
  }
  //
  // clear vtk data
  //
  command = "/bin/rm -f " + vtk_name + " " + tcl_name;
  if (clean) {
      if (verbose) clog << "! " << command << endl;
      status |= system (command.c_str());
  }
  return status;
}

int
field::plotmtv_deformation_P1 (const string& basename, 
	bool execute, bool clean, bool verbose,
	const Float& vscale) const
{
  if (get_approx() != "P1") {
      error_macro("field: " << get_approx()
                            << " plotmtv deformation output not implemented.");
  }
  string mtv_name = basename + ".mtv";
  ofstream mtv (mtv_name.c_str());
  if (verbose) clog << "! file \"" << mtv_name << "\" created.\n";
  int digits10 = numeric_limits<Float>::digits10;
  mtv << setprecision(digits10);
  //
  // header
  //
  const geo& g = get_geo();
  mtv << "#!plotmtv\n"
      << "$ DATA = CURVE" << g.dimension() << "D\n"
      << "% comment    = \"rheolef\"\n"
      << "% toplabel   = \"" << basename << "\"\n"
      << "% subtitle   = \"\"\n"
      << "% equalscale = on\n"
      ;
  //
  // plot elements
  //
  geo::const_iterator iter_e = g.begin();
  geo::const_iterator last_e = g.end();
  while (iter_e != last_e)
      plotmtv_element (mtv, *iter_e++, g.begin_node(), g.dimension());
  //
  // data
  //
  space V0 = space(_V[0]);
  space V1 = space(_V[1]);	// TODO: can be better: avoid memory
  space V2;
  if (g.dimension() == 3) {
        V2 = _V[2];
  }
  field u0 = get_comp(V0, 0);
  field u1 = get_comp(V1, 1);	// TODO: can be better: avoid memory 
  field u2;
  if (g.dimension() == 3) {
        u2 = get_comp(V2, 2);
  }
  tiny_vector<size_type> dof0;
  tiny_vector<size_type> dof1;
  tiny_vector<size_type> dof2;
  geo::const_iterator last_K = g.end();
  vector<bool>  done    (V0.size(), false);
  vector<point> new_node(V0.size());
  for (geo::const_iterator iter_K = g.begin(); iter_K != last_K; iter_K++) {
      const geo_element& K = *iter_K;
      V0.set_dof(K, dof0);
      V1.set_dof(K, dof1);
      if (g.dimension() == 3) {
          V2.set_dof(K, dof2);
      }
      for (geo::size_type i = 0; i < dof0.size(); i++) {
	  if (done[dof0(i)]) {
	     continue;
	  }
          point x_i = V0.x_dof(K, i);
          Float u0_i = u0.at(dof0(i));
          Float u1_i = u1.at(dof1(i));
          Float u2_i = 0;
          if (g.dimension() == 3) {
                u2_i = u2.at(dof2(i));
	  }
          point deplacement (u0_i, u1_i, u2_i);
          new_node [dof0(i)] = x_i + vscale*deplacement;
          done[dof0(i)] = true;
      }
  }
  //
  // plot deplaced mesh (P1 only)
  //
  iter_e = g.begin();
  last_e = g.end();
  while (iter_e != last_e)
      plotmtv_element (mtv, *iter_e++, new_node.begin(), g.dimension(), 2, 2);

  mtv.close();
  //
  // run plotmtv
  //
  int status = 0;
  if (execute) {
      string option;
      if (g.dimension() == 3) {
 	 option = "-3d ";
      }
      string command;
      if (verbose) {
	  command = "plotmtv " + option + basename + ".mtv 1>&2";
          clog << "! " << command << endl;
      } else {
	  command = "plotmtv " + option + basename + ".mtv >/dev/null";
      }
      status = system (command.c_str());
  }
  //
  // clear plotmtv data
  //
  if (clean) {
      string command = "/bin/rm -f " + basename + ".mtv";
      if (verbose) clog << "! " << command << endl;
      status |= system (command.c_str());
  }
  return status;
}

int
field::vtk_deformation (const string& basename, 
	bool execute, bool clean, bool verbose,
	const Float& vscale) const
{
  string vtk_name = basename + ".vtk";
  ofstream vtk (vtk_name.c_str());
  if (verbose) clog << "! file \"" << vtk_name << "\" created.\n";
  int digits10 = numeric_limits<Float>::digits10;
  vtk << setprecision(digits10);
  //
  // header
  //
  //
  // output geometry and data
  //
  const geo& g = get_geo();
  if (get_approx() == "P1") {
     vtk << vtkdata << get_geo();
  } else if (get_approx() == "P2") {
     vtk << lattice << vtkdata << get_geo();
  } else {
     fatal_macro ("vtk_vector: P1 or P2 approximation expected");
  }
  put_vtk_vector (vtk);
  vtk.close();
  //
  // output tcl script
  //
  string tcl_name = basename + ".tcl";
  ofstream tcl (tcl_name.c_str());
  if (verbose) clog << "! file \"" << tcl_name << "\" created.\n";
  tcl << setprecision(digits10);
  tcl << "# this is a tcl version of cube.vtk visualization\n"
      << "source \"" << _RHEOLEF_PKGDATADIR << "/vtk_interactor.tcl\"\n"
      << "#\n"
      << "# edit parameters:\n"
      << "#\n"
      << "set vscale           " << vscale << ";\n"
      << "set axis_color       \"0. 0. 0.\";\n"
      << "set outline_color    \"0. 0. 0.\";\n"
      << "set background_color \"1. 1. 1.\";\n"
      << "#\n"
      << "# Create rendering stuff\n"
      << "#\n"
      << "vtkRenderer ren1\n"
      << "vtkRenderWindow render_window\n"
      << "    render_window AddRenderer ren1\n"
      << "vtkRenderWindowInteractor iren\n"
      << "    iren SetRenderWindow render_window\n"
      << "#\n"
      << "#\n"
      << "# load data\n"
      << "#\n"
      << "vtkUnstructuredGridReader reader;\n"
      << "    reader SetFileName \"" << basename << ".vtk\";\n"
      << "    reader Update;\n"
      << "\n"
      << "vtkLookupTable lookup_table;\n"
      << "    lookup_table Build;\n"
      << "\n"
      << "\n"
      << "vtkWarpVector translate_filter\n"
      << "    translate_filter SetInput [reader GetOutput]\n"
      << "    translate_filter SetScaleFactor $vscale\n"
      << "vtkVectorNorm norm_filter\n"
      << "    norm_filter SetInput [translate_filter GetOutput]\n"
      << "vtkDataSetMapper deformation_mapper\n"
      << "    deformation_mapper SetInput [translate_filter GetOutput]\n"
      << "    deformation_mapper SetInput [norm_filter GetOutput]\n"
      << "    deformation_mapper SetLookupTable lookup_table;\n"
      << "    deformation_mapper ImmediateModeRenderingOn;\n"
      << "    eval deformation_mapper SetScalarRange [[reader GetOutput] GetScalarRange]\n"
      << "vtkActor deformation_actor\n"
      << "    deformation_actor SetMapper deformation_mapper\n"
      << "\n"
      << "ren1 AddActor deformation_actor\n"
      << "#\n"
      << "# create the outline\n"
      << "#\n"
      << "vtkOutlineFilter outline_filter\n"
      << "    outline_filter SetInput [reader GetOutput]\n"
      << "vtkPolyDataMapper outline_mapper\n"
      << "    outline_mapper SetInput [outline_filter GetOutput]\n"
      << "vtkActor outline_actor\n"
      << "    outline_actor SetMapper outline_mapper\n"
      << "    eval [outline_actor GetProperty] SetColor $outline_color\n"
      << "\n"
      << "ren1 AddActor outline_actor\n"
      << "#\n"
      << "# axis actor\n"
      << "#\n"
      << "vtkCubeAxesActor2D axis_actor;\n"
      << "    axis_actor SetInput [reader GetOutput];\n"
      << "    axis_actor SetCamera [ren1 GetActiveCamera];\n"
      << "    axis_actor SetLabelFormat \"%6.4g\";\n"
      << "    if {[get_vtk_major_version] >= 4 && [get_vtk_minor_version] > 0} {\n"
      << "        vtkTextProperty axis_actor_property\n"
      << "        axis_actor_property ShadowOn\n"
      << "        axis_actor SetAxisLabelTextProperty axis_actor_property\n"
      << "        axis_actor SetAxisTitleTextProperty axis_actor_property\n"
      << "    } else {\n"
      << "        axis_actor ShadowOn;\n"
      << "    }\n"
      << "    axis_actor SetFlyModeToOuterEdges;\n"
      << "    axis_actor SetFontFactor 0.8;\n"
      << "    eval [axis_actor GetProperty] SetColor $axis_color;\n"
      << "\n"
      << "ren1 AddProp  axis_actor\n"
      << "#\n"
      << "# color bar\n"
      << "#\n"
      << "vtkScalarBarActor bar_actor\n"
      << "    bar_actor SetLookupTable [deformation_mapper GetLookupTable]\n"
      << "    bar_actor SetOrientationToHorizontal\n"
      << "    [bar_actor GetProperty] SetColor 0 0 0\n"
      << "    [bar_actor GetPositionCoordinate] SetCoordinateSystemToNormalizedViewport\n"
      << "    [bar_actor GetPositionCoordinate] SetValue 0.1 0.01\n"
      << "    bar_actor SetOrientationToHorizontal\n"
      << "    bar_actor SetWidth  0.8\n"
      << "    bar_actor SetHeight 0.10\n"
      << "    bar_actor SetTitle \"deformation module\"\n"

      //
      // Version 4.2 of VTK introduces several modifications
      // on a couple of classes. Now for instance, fonts are
      // managed with a single class. In order to stay compatible
      // with older and newer versions, we use the code below
      //

      << "    if {[get_vtk_major_version] >= 4 && [get_vtk_minor_version] > 0} {\n"
      << "        vtkTextProperty bar_text_property\n"
      << "        bar_text_property BoldOn\n"
      << "        bar_actor SetLabelTextProperty bar_text_property\n"
      << "        bar_actor SetTitleTextProperty bar_text_property\n"
      << "    } else {\n"
      << "        bar_actor BoldOn\n"
      << "    }\n"
      << "    eval [bar_actor GetProperty] SetColor $axis_color;\n"
      << "\n"
      << "ren1 AddActor bar_actor;\n"
      << "#\n"
      << "# render the image\n"
      << "#\n"
      << "render_window SetSize 500 500;\n"
      << "eval ren1 SetBackground $background_color;\n"
      << "if { [get_vtk_major_version] >= 4 && [get_vtk_minor_version] > 0} {\n"
      << "    iren AddObserver UserEvent {wm deiconify .vtkInteract};\n"
      << "} else {\n"
      << "    iren SetUserMethod {wm deiconify .vtkInteract};\n"
      << "}\n"
      << "render_window Render;\n"
      << "#\n"
      << "# prevent the tk window from showing up then start the event loop\n"
      << "#\n"
      << "wm withdraw .\n"
     ;
  tcl.close();
  //
  // run vtk
  //
  int status = 0;
  string command = "vtk " + tcl_name;
  if (execute) {
      if (verbose) clog << "! " << command << endl;
      status = system (command.c_str());
  }
  //
  // clear vtk data
  //
  command = "/bin/rm -f " + vtk_name + " " + tcl_name;
  if (clean) {
      if (verbose) clog << "! " << command << endl;
      status |= system (command.c_str());
  }
  return status;
}
} // namespace rheolef