MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
foams.cc
Go to the documentation of this file.
1 
10 #include "globals.hh"
11 #include <iostream>
12 #include <string.h>
13 #include "inout.hh"
14 #include "allocation.hh"
15 #include "geometry.hh"
16 #include "skeleton.hh"
17 #include "walls.hh"
18 #include "seeds.hh"
19 #include "struts.hh"
20 using namespace std;
21 using namespace globals;
23 int main(int argc,char *argv[])
24 {
25  int ncell; // number of seeds for tessellation
26  int i, j, k;
27  int ***amat,***smat; // 3D matrix of voxels
28  int *center_x, *center_y, *center_z; // position of seeds
29  int vmax=10000; // maximum number of vertices
30  int incmax=8; // maximum number of incident vertices to a vertex
31  double **vert; // vertex coordinates
32  int **vinc; // incident vertices for each vertex
33  int sv=0; // final number of vertices
34  double por=0; // porosity
35  double por_s=0; // porosity of struts only
36  double fs=0; // strut content
37  int gsl_status; // status return by GSL function
38  double mini,maxi; // bounds of optimization interval
39  int maxit=3; // maximum number of restarts of optimization
40  string inputsFilename="foamreconstr.in"; // Name of input file with
41  // parameters that determine what will be done
42  string outputFilename; // Name of output file with morphology
43  // without extension
44  string VTKInputFilename; // Name of input VTK file with morphology
45  string GnuplotSkeletonFilename; // Name of input/output file with
46  // tessellation - gnuplot diagram
47  string GnuplotAltSkeletonFilename; // Name of output file with
48  // tessellation - alternative gnuplot diagram
49  string descriptorsFilename; // Name of output file with
50  // morphology descriptors
51  string parametersFilename; // Name of output file with parameters
52 
53  // read inputs and save them to global variables
54  readParameters(inputsFilename,outputFilename,VTKInputFilename,\
55  GnuplotSkeletonFilename,GnuplotAltSkeletonFilename,descriptorsFilename,\
56  parametersFilename);
57  // define names of output files with morphology
58  string VTKOutputFilename=outputFilename+".vtk";
59  string DXOutputFilename=outputFilename+".dat";
60  if (import_vtk) {
61  // only allocate the 3D matrix
62  amat=allocateFromVTK(VTKInputFilename,amat,progress_report);
63  } else {
64  // allocate the 3D matrix and make seeds for the tessellation
65  ncell = 1 + (nx - 1) / sx ;
66  ncell = ncell * (1 + (ny - 1) / sy);
67  ncell = ncell * (1 + (nz - 1) / sz);
68  /*
69  * Allocate memory:
70  * center_x, center_y, center_z = distance from centers of cells.
71  */
72  center_x = (int *)calloc((size_t)ncell, sizeof(int));
73  center_y = (int *)calloc((size_t)ncell, sizeof(int));
74  center_z = (int *)calloc((size_t)ncell, sizeof(int));
75  if (center_x == NULL || center_y == NULL || center_z == NULL) {
76  fprintf (stderr, "Insufficient memory.\n");
77  return 9;
78  }
79  /*
80  * Allocate the 3D matrix of voxels.
81  * voxel = 0 (pore phase).
82  * voxel > 1 (solid phase).
83  */
84  amat = alloc_3Dmatrix (nx, ny, nz);
85  if (amat == NULL) {
86  fprintf (stderr, "Insufficient memory.\n");
87  return 9;
88  }
89  for (i = 0; i < nx; i++)
90  for (j = 0; j < ny; j++)
91  for (k = 0; k < nz; k++)
92  amat[i][j][k] = 0;
93  createSeeds(ncell,center_x,center_y,center_z,progress_report);
94  }
95  if (createNodes || createEdges) {
96  if (!import_vtk) {
97  // geometrical voronoi tesselation by voro++
98  // creates gnuplot diagram with the tessellation
99  makeFoamSkeleton(GnuplotSkeletonFilename,ncell,center_x,center_y,\
100  center_z,progress_report);
101  }
102  vert=alloc_dmatrix(vmax,3);
103  vinc=alloc_matrix(vmax,incmax);
104  // read the gnuplot diagram with the tessellation
105  // make the alternative diagram with the tessellation
106  importFoamSkeleton(GnuplotSkeletonFilename,vert,vinc,vmax,incmax,sv,\
108  if (!save_voro_diag1) {
109  // delete the gnuplot diagram with the tessellation from disk
110  remove(GnuplotSkeletonFilename.c_str());
111  }
112  // save alternative gnuplot diagram of the tesselation
113  if (save_voro_diag2) {
114  saveToGnuplot(GnuplotAltSkeletonFilename,sv,incmax,vert,vinc,\
116  }
117  // allocate matrix for struts
118  smat = alloc_3Dmatrix (nx, ny, nz);
119  if (smat == NULL) {
120  fprintf (stderr, "Insufficient memory.\n");
121  return 9;
122  }
123  // struct for passing variables to GSL function
124  struct fn1_params params = {sv,incmax,vmax,vert,vinc,smat,\
125  progress_report};
126  // initial interval where we are looking for optimal dedge
127  if (dedge == 0) {
128  dedge=2;
129  }
130  mini=0.5*dedge;
131  maxi=1.5*dedge;
132  for (i=0;i<maxit;i++) {
133  // finds dedge, which gives desired porosity of struts
134  gsl_status = optim(&params,dedge,mini,maxi,progress_report);
135  if (gsl_status==0) {
136  break;
137  } else {
138  mini=0.5*mini;
139  maxi=1.5*maxi;
140  if (i==maxit-1) {
141  cout << "Didn't find initial interval" << endl;
142  exit(1);
143  }
144  }
145  }
146  // save dedge so that we can use it as initial guess next time
147  saveParameters(parametersFilename,dedge,progress_report);
148  }
149  por_s=porosity(smat);
150  if (progress_report) {
151  cout << "porosity of struts only " << por_s << endl;
152  }
153  vert=free_dmatrix(vert);
154  vinc=free_matrix(vinc);
155  if (!openCell) {
156  // create walls
157  if (import_vtk) {
158  // walls from file
159  importFromVTK(VTKInputFilename,amat,progress_report);
160  } else {
161  // walls from Voronoi tessellation
162  makeWalls(amat,ncell,center_x,center_y,center_z,progress_report);
163  free(center_x);
164  free(center_y);
165  free(center_z);
166  }
167  }
168  for (i = 0; i < nx; i++)
169  for (j = 0; j < ny; j++)
170  for (k = 0; k < nz; k++)
171  smat[i][j][k] = smat[i][j][k] + amat[i][j][k];
172  por=porosity(smat);
173  fs=(1-por_s)/(1-por);
174  if (progress_report) {
175  cout << "porosity " << por << endl;
176  cout << "polymer in struts " << fs << endl;
177  }
178  // save and exit
179  saveDescriptors(descriptorsFilename,por,fs,progress_report);
180  if (save_dat) {
181  saveToDX(DXOutputFilename.c_str(),smat,progress_report);
182  }
183  if (save_vtk) {
184  saveToVTK(VTKOutputFilename.c_str(),smat,progress_report);
185  }
186  amat=free_3Dmatrix(amat);
187  amat=free_3Dmatrix(smat);
188  exit(0);
189 }
bool openCell
make open cell foam
Definition: globals.cc:9
double porosity(int ***amat)
Calculates porosity.
Definition: geometry.cc:70
void importFoamSkeleton(string filename, double **vert, int **vinc, int vmax, int incmax, int &sv, bool report)
Load geometric tessellation, move it to the domain and store it.
Definition: skeleton.cc:47
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 makeWalls(int ***amat, int ncell, int *center_x, int *center_y, int *center_z, bool report)
Creates walls based on position of seeds and Voronoi tessellation.
Definition: walls.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
int *** free_3Dmatrix(int ***amat)
free 3D integer matrix
Definition: allocation.cc:111
bool save_voro_diag2
alternative gnuplot Voronoi diagram
Definition: globals.cc:26
double ** alloc_dmatrix(int nx, int ny)
allocate 2D double matrix
Definition: allocation.cc:120
int optim(void *params, double &m, double &a, double &b, bool report)
GSL minimizer for univariate scalar function.
Definition: struts.cc:46
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.
int ** alloc_matrix(int nx, int ny)
allocate 2D integer matrix
Definition: allocation.cc:184
bool save_dat
save file in old dx style
Definition: globals.cc:10
void saveParameters(string filename, double dedge, bool report)
Saves parameters (dedge) to a file.
Definition: inout.cc:315
int main(int argc, char *argv[])
Reads parameters. Creates struts and walls. Saves foam morphology to a file.
Definition: foams.cc:23
int ** free_matrix(int **amat)
free 2D integer matrix
Definition: allocation.cc:208
real(dp) por
porosity
Definition: constants.f90:42
int nx
domain size in X
Definition: globals.cc:19
integer ncell
number of cells
Definition: globals.f90:15
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
struct for passing paramaters to GSL function
Definition: globals.hh:27
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
double ** free_dmatrix(double **amat)
free 2D double matrix
Definition: allocation.cc:144
void createSeeds(int &ncell, int *center_x, int *center_y, int *center_z, bool report)
Initialization of seeds. Stores positions of seeds.
Definition: seeds.cc:12
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
void makeFoamSkeleton(string filename, int ncell, int *center_x, int *center_y, int *center_z, bool report)
Make geometric tessellation using voro++ and store it to a file.
Definition: skeleton.cc:16
int ny
domain size in Y
Definition: globals.cc:20