MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
inout.cc
Go to the documentation of this file.
1 
6 #include "globals.hh"
7 #include <fstream>
8 #include <iostream>
9 #include <string.h>
10 #include "allocation.hh"
11 #include "geometry.hh"
12 using namespace std;
13 using namespace globals;
20  string filename,\
22  string &outputFilename,\
24  string &VTKInputFilename,\
26  string &GnuplotSkeletonFilename,\
28  string &GnuplotAltSkeletonFilename,\
30  string &descriptorsFilename,\
32  string &parametersFilename)
33 {
34  // Reads inputs from text file. Returns filenames. Rest is put into global
35  // variables.
36  ifstream fin;
37  fin.open(filename);
38  if (!fin.is_open()) {
39  cout << "Can't open file " << filename << endl;
40  exit(1);
41  }
42  fin >> createNodes; fin.ignore(256,'\n');
43  fin >> createEdges; fin.ignore(256,'\n');
44  fin >> openCell; fin.ignore(256,'\n');
45  fin >> save_dat; fin.ignore(256,'\n');
46  fin >> save_vtk; fin.ignore(256,'\n');
47  fin >> dstrut; fin.ignore(256,'\n');
48  fin >> dedge; fin.ignore(256,'\n');
49  fin >> strutPorosity; fin.ignore(256,'\n');
50  fin >> RANDOM; fin.ignore(256,'\n');
51  fin >> grid; fin.ignore(256,'\n');
52  fin >> nx; fin.ignore(256,'\n');
53  fin >> ny; fin.ignore(256,'\n');
54  fin >> nz; fin.ignore(256,'\n');
55  fin >> sx; fin.ignore(256,'\n');
56  fin >> sy; fin.ignore(256,'\n');
57  fin >> sz; fin.ignore(256,'\n');
58  fin >> save_voro_diag1; fin.ignore(256,'\n');
59  fin >> save_voro_diag2; fin.ignore(256,'\n');
60  fin >> import_vtk; fin.ignore(256,'\n');
61  fin >> progress_report; fin.ignore(256,'\n');
62  fin >> outputFilename; fin.ignore(256,'\n');
63  fin >> VTKInputFilename; fin.ignore(256,'\n');
64  fin >> GnuplotSkeletonFilename; fin.ignore(256,'\n');
65  fin >> GnuplotAltSkeletonFilename; fin.ignore(256,'\n');
66  fin >> descriptorsFilename; fin.ignore(256,'\n');
67  fin >> parametersFilename; fin.ignore(256,'\n');
68  fin.close();
69 }
72  string filename ,\
73  int ***amat ,\
74  bool report )
75 {
76  int i,j,k;
77  ifstream fin;
78  string line;
79  // read vtk file
80  if (report) {
81  cout << "reading vtk file" << endl;
82  }
83  fin.open(filename);
84  // read header
85  int vtkx,vtky,vtkz;
86  for (i=0;i<9;i++) {
87  getline(fin, line);
88  if (i==4) {
89  char word[20];
90  sscanf(line.c_str(),"%s %d %d %d",word,&vtkx,&vtky,&vtkz);
91  }
92  }
93  if (report) {
94  cout << "dimensions: " << vtkx << ", " << vtky << ", " << vtkz << endl;
95  cout << "allocating" << endl;
96  }
97  amat = alloc_3Dmatrix (vtkx, vtky, vtkz);
98  for (i = 0; i < nx; i++)
99  for (j = 0; j < ny; j++)
100  for (k = 0; k < nz; k++)
101  amat[i][j][k] = 0;
102  nx=vtkx;
103  ny=vtky;
104  nz=vtkz;
105  return amat;
106 }
109  string filename ,\
110  int ***amat ,\
111  bool report )
112 {
113  int i,j,k,l;
114  ifstream fin;
115  string line;
116  int *bmat;
117  float a,b,c;
118  // read vtk file
119  if (report) {
120  cout << "reading vtk file" << endl;
121  }
122  fin.open(filename);
123  // read header
124  int vtkx,vtky,vtkz;
125  for (i=0;i<9;i++) {
126  getline(fin, line);
127  if (i==4) {
128  char word[20];
129  sscanf(line.c_str(),"%s %d %d %d",word,&vtkx,&vtky,&vtkz);
130  }
131  }
132  if (report) {
133  cout << "dimensions: " << vtkx << ", " << vtky << ", " << vtkz << endl;
134  cout << "loading data file" << endl;
135  }
136  bmat = (int *)calloc((size_t)vtkx*vtky*vtkz, sizeof(int));
137  // read data
138  getline(fin, line);
139  sscanf(line.c_str(),"%e %e %e",&a,&b,&c);
140  // 3 values on first data line
141  bmat[0]=int(a+0.5);
142  bmat[1]=int(b+0.5);
143  bmat[2]=int(c+0.5);
144  l=3;
145  for (i=1;i<vtkx*vtky*vtkz/2-1;i++) {
146  // 2 values on next lines
147  getline(fin, line);
148  sscanf(line.c_str(),"%e %e",&a,&b);
149  bmat[2*i+1]=int(a+0.5);
150  bmat[2*i+2]=int(b+0.5);
151  l=l+2;
152  }
153  //check that we read the right amount of values
154  // cout << vtkx*vtky*vtkz << " " << l << endl;
155  // 1 value on last line
156  getline(fin, line);
157  sscanf(line.c_str(),"%e",&a);
158  bmat[vtkx*vtky*vtkz-1]=int(a+0.5);
159  fin.close();
160  // make 3D array from 1D array
161  l=0;
162  for (i=0;i<vtkz;i++) {
163  for (j=0;j<vtky;j++) {
164  for (k=0;k<vtkx;k++) {
165  if (amat[i][j][k]==0) {
166  amat[i][j][k]=bmat[l];
167  }
168  l++;
169  }
170  }
171  }
172  free(bmat);
173  nx=vtkx;
174  ny=vtky;
175  nz=vtkz;
176 }
178 void saveToVTK(\
179  const char* filename ,\
180  int ***amat ,\
181  bool report )
182 {
183  int i,j,k;
184  FILE *strmo;
185  // Output to file in Paraview style.
186  if (report) {
187  cout << "saving in Paraview style..." << endl;
188  }
189  strmo = fopen(filename, "w");
190  if (strmo == NULL) {
191  fprintf (stderr, "Can't open file %s\n",filename);
192  exit(13);
193  }
194 
195  fprintf (strmo, "# vtk DataFile Version 3.0\n");
196  fprintf (strmo, "vtkfile\n");
197  fprintf (strmo, "ASCII\n");
198  fprintf (strmo, "DATASET STRUCTURED_POINTS\n");
199  fprintf (strmo, "DIMENSIONS %d %d %d\n",nx,ny,nz);
200  fprintf (strmo, "ORIGIN 1 1 1\n");
201  fprintf (strmo, "SPACING %g %g %g\n",1.0/nx,1.0/ny,1.0/nz);
202  fprintf (strmo, "POINT_DATA %d\n",nx*ny*nz);
203  fprintf (strmo, "SCALARS values int\n");
204  fprintf (strmo, "LOOKUP_TABLE default\n");
205 
206  for (i = 0; i < nx; i++) {
207  for (j = 0; j < ny; j++) {
208  for (k = 0; k < nz; k++)
209  fprintf (strmo, " %c", (amat[i][j][k] >= 1) ? '0' : '1');
210  //fprintf (strmo, " %d", amat[i][j][k]);
211  fprintf (strmo, "\n");
212  }
213  fprintf (strmo, "\n");
214  }
215  fprintf (strmo, "\n");
216 
217  fclose (strmo);
218 }
220 void saveToDX(\
221  const char* filename ,\
222  int ***amat ,\
223  bool report )
224 {
225  int i,j,k;
226  FILE *strmo;
227  // Output to file in DX style.
228  if (report) {
229  cout << "saving in DX style..." << endl;
230  }
231  strmo = fopen(filename, "w");
232  if (strmo == NULL) {
233  fprintf (stderr, "Can't open file %s\n",filename);
234  exit(13);
235  }
236 
237  fprintf (strmo, "NCX = %d ;\n", nx);
238  fprintf (strmo, "NCY = %d ;\n", ny);
239  fprintf (strmo, "NCZ = %d ;\n", nz);
240 
241  fprintf (strmo, "A = [\n");
242  for (i = 0; i < nx; i++) {
243  for (j = 0; j < ny; j++) {
244  for (k = 0; k < nz; k++)
245  fprintf (strmo, " %c", (amat[i][j][k] >= 1) ? '0' : '1');
246  //fprintf (strmo, " %d", amat[i][j][k]);
247  fprintf (strmo, "\n");
248  }
249  fprintf (strmo, "\n");
250  }
251  fprintf (strmo, "];\n");
252 
253  fclose (strmo);
254 }
257  string filename ,\
258  int sv ,\
259  int incmax ,\
260  double **vert ,\
261  int **vinc ,\
262  bool report )
263 {
264  // Stores cell edges incident to voronoi vertices in the domain
265  ofstream fout;
266  int i,j;
267  if (report) {
268  cout << "saving shifted Voronoi diagram" << endl;
269  }
270  fout.open(filename);
271  if (!fout.is_open()) {
272  cout << "can't open file " << filename << endl;
273  exit(1);
274  }
275  for (i=0; i<sv; i++) {
276  if (in_domain(vert[i][0],vert[i][1],vert[i][2])) {
277  for (j=0; j<incmax; j++) {
278  if (vinc[i][j] != -1) {
279  fout << vert[i][0] << " " << vert[i][1] << " " << \
280  vert[i][2] << endl;
281  fout << vert[vinc[i][j]][0] << " " << \
282  vert[vinc[i][j]][1] << " " << \
283  vert[vinc[i][j]][2] << endl;
284  fout << endl;
285  fout << endl;
286  } else {
287  break;
288  }
289  }
290  }
291  }
292  fout.close();
293 }
296  string filename ,\
297  double por ,\
298  double fs ,\
299  bool report )
300 {
301  ofstream fout;
302  if (report) {
303  cout << "saving descriptors" << endl;
304  }
305  fout.open(filename);
306  if (!fout.is_open()) {
307  cout << "can't open file " << filename << endl;
308  exit(1);
309  }
310  fout << por << endl;
311  fout << fs << endl;
312  fout.close();
313 }
316  string filename ,\
317  double dedge ,\
318  bool report )
319 {
320  ofstream fout;
321  if (report) {
322  cout << "saving parameters" << endl;
323  }
324  fout.open(filename);
325  if (!fout.is_open()) {
326  cout << "can't open file " << filename << endl;
327  exit(1);
328  }
329  fout << dedge << endl;
330  fout.close();
331 }
bool openCell
make open cell foam
Definition: globals.cc:9
int grid
Definition: globals.cc:17
void saveDescriptors(string filename, double por, double fs, bool report)
Saves morphology descriptors (porosity and strut content) to a file.
Definition: inout.cc:295
bool progress_report
show detailed progress report
Definition: globals.cc:28
double dedge
parameter influencing size of struts in cell edges
Definition: globals.cc:13
int sy
size of cells in Y
Definition: globals.cc:23
void readParameters(string filename, string &outputFilename, string &VTKInputFilename, string &GnuplotSkeletonFilename, string &GnuplotAltSkeletonFilename, string &descriptorsFilename, string &parametersFilename)
Definition: inout.cc:18
bool save_voro_diag1
gnuplot Voronoi diagram
Definition: globals.cc:25
bool createEdges
create struts at cell edges
Definition: globals.cc:8
int sz
size of cells in Z
Definition: globals.cc:24
void saveToGnuplot(string filename, int sv, int incmax, double **vert, int **vinc, bool report)
Saves tessellation diagram to gnuplot file.
Definition: inout.cc:256
bool save_vtk
save file in new paraview style
Definition: globals.cc:11
bool save_voro_diag2
alternative gnuplot Voronoi diagram
Definition: globals.cc:26
int *** allocateFromVTK(string filename, int ***amat, bool report)
Allocates the matrix from dimensions read from VTK file.
Definition: inout.cc:71
int sx
size of cells in X
Definition: globals.cc:22
void saveToDX(const char *filename, int ***amat, bool report)
Saves morphology to DX file.
Definition: inout.cc:220
Defines global variables, macros, templates and namespace.
bool save_dat
save file in old dx style
Definition: globals.cc:10
real(dp) dstrut
strut diameter
Definition: constants.f90:42
void saveParameters(string filename, double dedge, bool report)
Saves parameters (dedge) to a file.
Definition: inout.cc:315
bool in_domain(double x, double y, double z)
True if the point is in the domain, False otherwise.
Definition: geometry.cc:9
double RANDOM
Definition: globals.cc:15
int nx
domain size in X
Definition: globals.cc:19
void saveToVTK(const char *filename, int ***amat, bool report)
Saves morphology to VTK file.
Definition: inout.cc:178
bool createNodes
create struts at cell vertices
Definition: globals.cc:7
integer nz
spatial discretization
Definition: constants.f90:32
namespace with global variables
Definition: globals.f90:8
void importFromVTK(string filename, int ***amat, bool report)
Reads morphology from VTK file. Changes amat.
Definition: inout.cc:108
bool import_vtk
import morphology from vtk
Definition: globals.cc:27
int *** alloc_3Dmatrix(int nx, int ny, int nz)
allocate 3D integer matrix
Definition: allocation.cc:83
double strutPorosity
Definition: globals.cc:14
int ny
domain size in Y
Definition: globals.cc:20