NDDEM
Vtk.h
Go to the documentation of this file.
1 #include "Boundaries.h"
2 namespace vtkwriter {
3 
4 FILE * open (char path[], int d) {
5  FILE *out ;
6  static bool warn ;
7  out=fopen(path, "w") ; if (out==NULL) {printf("Cannot open out file\n") ; return nullptr;}
8  if (d>3 && warn==false) {
9  printf("WARN: writevtk might misbehave with dimension higher than 3. The 3d projection is always centered in all other dimensions\n") ;
10  warn=true ;
11  }
12  fprintf(out, "# vtk DataFile Version 2.0\nMost Useless DEM (tm) output file\nASCII\nDATASET POLYDATA\n") ;
13  return out ;
14 }
15 //------------------------------------------------------------------------------
16 int write_points (FILE * out, cv2d & X, int d)
17 {
18  fprintf(out, "POINTS %ld float\n", X.size()) ;
19  for (uint i=0 ; i<X.size() ; i++) fprintf(out, "%g %g %g\n", X[i][0], X[i][1], d<3?0:X[i][2]) ;
20  fprintf(out, "VERTICES %ld %ld\n", X.size(), 2*X.size()) ;
21  for (uint i=0 ; i<X.size() ; i++) fprintf(out, "1 %d\n", i) ;
22  return 0;
23 }
24 //------------------------------------------------------------------------------
25 int write_contactlines (FILE *out, cv2d & ids)
26 {
27 
28 fprintf(out, "LINES %ld %ld\n", ids.size(), ids.size()*3) ;
29 for (uint i=0 ; i<ids.size() ; i++) fprintf(out, "2 %g %g\n", ids[i][0], ids[i][1]) ;
30 return 0 ;
31 }
32 
33 //------------------------------------------------------------------------------
34 int start_pointdata (FILE *out, cv2d & X)
35 {
36  fprintf(out, "\nPOINT_DATA %ld", X.size()) ;
37  return 0;
38 }
39 //------------------------------------------------------------------------------
40 int start_celldata (FILE *out, int N, int Ncf)
41 {
42  fprintf(out, "\nCELL_DATA %d\n", N+Ncf) ;
43  return 0 ;
44 }
45 //------------------------------------------------------------------------------
46 template <int dd>
47 int write_dimension_data (FILE *out, cv2d &X, cv1d &r, int d, vector < Boundary<dd> > boundaries)
48 {
49  vector <float> projectioncenter ;
50  if (boundaries[0].Type==WallType::ROTATINGSPHERE)
51  for (int i=3 ; i<d ; i++) projectioncenter.push_back((boundaries[0].center[i])/2) ;
52  else
53  for (int i=3 ; i<d ; i++) projectioncenter.push_back((boundaries[i].xmax+boundaries[i].xmin)/2) ;
54 
55  for (uint j=3 ; j<X[0].size() ; j++)
56  {
57  fprintf(out, "\nSCALARS Dimension%d float 1 \nLOOKUP_TABLE default \n", j) ;
58  for (uint i=0 ; i<X.size() ; i++)
59  fprintf(out, "%g ", X[i][j]) ;
60  }
61 
62  fprintf(out, "\n\nSCALARS RadiusProjected float 1 \nLOOKUP_TABLE default\n");
63  for (uint i=0 ; i<X.size() ; i++)
64  {
65  float value = r[i]*r[i] ;
66  for (int j=3 ; j<d ; j++) value-=(X[i][j]-projectioncenter[j-3])*(X[i][j]-projectioncenter[j-3]) ;
67  if (value<0) fprintf(out, "%g ", 0.0) ;
68  else fprintf(out, "%g ", sqrt(value)) ;
69  }
70  return 0;
71 }
72 //------------------------------------------------------------------------------
73 int write_data (FILE *out, TensorInfos v, int d)
74 {
75  switch (v.order) {
76  case TensorType::SCALAR: fprintf(out, "\nSCALARS %s double 1 \nLOOKUP_TABLE default \n", v.name.c_str()) ;//scalar
77  for (uint i=0 ; i<(*v.data)[0].size() ; i++)
78  fprintf(out, "%g ", (*v.data)[0][i]) ;
79  break ;
80  case TensorType::VECTOR: fprintf(out, "\nVECTORS %s double \n", v.name.c_str()) ;//vector
81  for (auto i : (*v.data))
82  fprintf(out, "%g %g %g\n", i[0], i[1], d<3?0:i[2]) ;
83  break ;
84  case TensorType::TENSOR: fprintf(out, "\nTENSORS %s double \n", v.name.c_str()) ;//tensor
85  for (auto i : (*v.data))
86  fprintf(out, "%g %g %g %g %g %g %g %g %g\n", i[0], i[1], i[2], i[d], i[d+1], i[d+2], i[2*d], i[2*d+1], i[2*d+2]) ;
87  break ;
88  case TensorType::SYMTENSOR: fprintf(out, "\nTENSORS %ssym double \n", v.name.c_str()) ;//tensor
89  for (auto i : (*v.data))
90  fprintf(out, "%g %g %g %g %g %g %g %g %g\n", i[0], i[1], i[2], i[1], i[d], i[d+1], i[2], i[d+1], i[2*d-1]) ;
91  break ;
92  case TensorType::SKEWTENSOR: fprintf(out, "\nTENSORS %sskew double \n", v.name.c_str()) ;//tensor
93  for (v1d i : (*v.data))
94  fprintf(out, "%g %g %g %g %g %g %g %g %g\n", 0.0, i[0], i[1], -i[0], 0.0, i[d-1], -i[1], -i[d-1], 0.0) ;
95  break ;
96  default: break ; /*fprintf(out, "\nPOINT_DATA %ld\nSCALARS %s double 1 \nLOOKUP_TABLE default \n",(*data.data).size(), data.name.c_str()) ;//scalar norm
97  for (uint i=0 ; i<(*data.data).size() ; i++)
98  fprintf(out, "%g ", Tools<d>::norm((*data.data)[i])) ;*/
99  }
100  return 0 ;
101 }
102 //------------------------------------------------------------------------------
103 int write_celldata (FILE *out, std::string name, cv2d & v, TensorType order, int idx, int N, int d)
104 {
105  switch (order) {
106  case TensorType::VECTOR: fprintf(out, "\nVECTORS %s double \n", name.c_str()) ;//vector
107  for (int i=0 ; i<N ; i++) fprintf(out, "0 0 0\n") ;
108  for (auto i : v)
109  fprintf(out, "%g %g %g\n", i[idx], i[idx+1], d<3?0:i[idx+2]) ;
110  break ;
111  case TensorType::SCALARMASK: fprintf(out, "\nSCALARS %s long 1 \nLOOKUP_TABLE default \n", name.c_str()) ;//tensor
112  for (int i=0 ; i<N ; i++) {fprintf(out, "0 ") ; } fprintf(out, "\n") ;
113  for (auto i : v)
114  fprintf(out, "%ld \n", static_cast<long int>(i[idx])) ;
115  break ;
116  default: break ; /*fprintf(out, "\nPOINT_DATA %ld\nSCALARS %s double 1 \nLOOKUP_TABLE default \n",(*data.data).size(), data.name.c_str()) ;//scalar norm
117  for (uint i=0 ; i<(*data.data).size() ; i++)
118  fprintf(out, "%g ", Tools<d>::norm((*data.data)[i])) ;*/
119  }
120  return 0 ;
121 }
122 //------------------------------------------------------------------------------
123 void close(FILE *out ) { fclose(out) ;}
124 } ;
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
Definition: Boundaries.h:8
Limited use: used to transfer data to the VTK writer.
Definition: Tools.h:50
TensorType order
Definition: Tools.h:53
string name
Definition: Tools.h:52
v2d * data
Definition: Tools.h:54
TensorType
Definition: Tools.h:46
unsigned int uint
Definition: Typedefs.h:8
const vector< double > cv1d
Definition: Typedefs.h:13
const vector< vector< double > > cv2d
Definition: Typedefs.h:14
vector< double > v1d
Definition: Typedefs.h:9
@ ROTATINGSPHERE
uint d
int N
Definition: Vtk.h:2
int write_data(FILE *out, TensorInfos v, int d)
Definition: Vtk.h:73
FILE * open(char path[], int d)
Definition: Vtk.h:4
int write_points(FILE *out, cv2d &X, int d)
Definition: Vtk.h:16
int write_contactlines(FILE *out, cv2d &ids)
Definition: Vtk.h:25
int write_celldata(FILE *out, std::string name, cv2d &v, TensorType order, int idx, int N, int d)
Definition: Vtk.h:103
int write_dimension_data(FILE *out, cv2d &X, cv1d &r, int d, vector< Boundary< dd > > boundaries)
Definition: Vtk.h:47
void close(FILE *out)
Definition: Vtk.h:123
int start_celldata(FILE *out, int N, int Ncf)
Definition: Vtk.h:40
int start_pointdata(FILE *out, cv2d &X)
Definition: Vtk.h:34
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1282
Type
Type of JSON value.
Definition: rapidjson.h:644