MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
walls.cc
Go to the documentation of this file.
1 
7 #include "globals.hh"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "allocation.hh"
11 using namespace globals;
14 #define COUNT(i,j,k,a) (((V_CELL(0,(i),(j),(k)) == (a)) ? 1 : 0) + \
15  ((V_CELL(1,(i),(j),(k)) == (a)) ? 1 : 0) + \
16  ((V_CELL(2,(i),(j),(k)) == (a)) ? 1 : 0) + \
17  ((V_CELL(3,(i),(j),(k)) == (a)) ? 1 : 0) + \
18  ((V_CELL(4,(i),(j),(k)) == (a)) ? 1 : 0) + \
19  ((V_CELL(5,(i),(j),(k)) == (a)) ? 1 : 0) + \
20  ((V_CELL(6,(i),(j),(k)) == (a)) ? 1 : 0) + \
21  ((V_CELL(7,(i),(j),(k)) == (a)) ? 1 : 0) )
22 void makeWalls(\
24  int ***amat ,\
25  int ncell ,\
26  int *center_x ,\
27  int *center_y ,\
28  int *center_z ,\
29  bool report )
30 {
31  // Make cell walls based on position of Voronoi seeds. Update `amat`.
32  int i,j,k,m;
33  int min_distance, m_cell;
34  int dx, dy, dz, mx, my, mz;
35  int ****v_cell, *v;
36  if (report) {
37  printf("creating walls\n");
38  }
39  // v = working array, distances from centers of cells.
40  v = (int *)calloc((size_t)ncell, sizeof(int));
41  /*
42  * Allocate Voronoi set v_cell[8][i][j][k].
43  */
44  v_cell = (int ****)calloc((size_t)8, sizeof(int ***));
45  for (m = 0; m < 8; m++) {
46  v_cell[m] = alloc_3Dmatrix (nx, ny, nz);
47  if (v_cell[m] == NULL) {
48  fprintf (stderr, "Insufficient memory.\n");
49  exit(9);
50  }
51  }
52  /*
53  * Initialize the voronoi set v_cell[8][i][j][k].
54  */
55  for (m = 0; m < 8 ; m++)
56  for (i = 0; i < nx; i++)
57  for (j = 0; j < ny; j++)
58  for (k = 0; k < nz; k++)
59  v_cell[m][i][j][k] = -1;
60 
61  /*
62  * Set the Voronoi set v_cell[8][i][j][k].
63  */
64  for (i = 0; i < nx; i++)
65  for (j = 0; j < ny; j++)
66  for (k = 0; k < nz; k++) {
67  /*
68  * Store the distance of [i,j,k] from all centers of cells
69  * in vector v[m].
70  */
71  for (m = 0; m < ncell; m++) {
72  mx = center_x[m];
73  my = center_y[m];
74  mz = center_z[m];
75  dx = MIN(ABS(i - mx), nx - ABS(i - mx));
76  dy = MIN(ABS(j - my), ny - ABS(j - my));
77  dz = MIN(ABS(k - mz), nz - ABS(k - mz));
78  v[m] = dx * dx + dy * dy + dz * dz;
79  }
80  /*
81  * Find the shortest distance from all centers of cells.
82  */
83  min_distance = 3 * nx * ny * nz;
84  for (m = 0; m < ncell; m++)
85  if (v[m] < min_distance)
86  min_distance = v[m];
87  /*
88  * Set the Voronoi set v_cell[8][i][j][k] .
89  */
90  m_cell = 0;
91  for (m = 0; m < ncell; m++)
92  if (v[m] == min_distance) {
93  v_cell[m_cell][i][j][k] = m;
94  m_cell++;
95  if (m_cell == 8)
96  break;
97  }
98  }
99 
100  /*
101  * Set all elements with equal distance from two or more
102  * centers of cells to be solid phase.
103  */
104  m_cell = 0;
105  for (i = 0; i < nx; i++)
106  for (j = 0; j < ny; j++)
107  for (k = 0; k < nz; k++)
108  if (v_cell[1][i][j][k] != -1) {
109  amat[i][j][k] = 8 - COUNT(i,j,k,-1);
110  m_cell++;
111  }
112  if (report) {
113  printf("Voxels with multiplicity: m_cell = %d\n", m_cell);
114  }
115 
116  /*
117  * Set all elements where at least one of the 6 nearest
118  * neighbors is from another Voronoi set to be also wall
119  * elements.
120  */
121 
122  for (i = 0; i < nx; i++)
123  for (j = 0; j < ny; j++)
124  for (k = 0; k < nz; k++) {
125  if (amat[i][j][k] > 1) {
126  continue;
127  } else if (AMAT(i+1,j,k) > 1 ||
128  V_CELL(0,i,j,k) != V_CELL(0,i+1,j,k) ) {
129  amat[i][j][k] = 1;
130  } else if (AMAT(i-1,j,k) > 1 ||
131  V_CELL(0,i,j,k) != V_CELL(0,i-1,j,k) ) {
132  amat[i][j][k] = 1;
133  } else if (AMAT(i,j+1,k) > 1 ||
134  V_CELL(0,i,j,k) != V_CELL(0,i,j+1,k) ) {
135  amat[i][j][k] = 1;
136  } else if (AMAT(i,j-1,k) > 1 ||
137  V_CELL(0,i,j,k) != V_CELL(0,i,j-1,k) ) {
138  amat[i][j][k] = 1;
139  } else if (AMAT(i,j,k+1) > 1 ||
140  V_CELL(0,i,j,k) != V_CELL(0,i,j,k+1) ) {
141  amat[i][j][k] = 1;
142  } else if (AMAT(i,j,k-1) > 1 ||
143  V_CELL(0,i,j,k) != V_CELL(0,i,j,k-1) ) {
144  amat[i][j][k] = 1;
145  }
146  }
147  free(v);
148  for (i=0; i<8; i++) {
149  v_cell[i]=free_3Dmatrix(v_cell[i]);
150  }
151  free(v_cell);
152 }
int nz
domain size in Z
Definition: globals.cc:21
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
real(dp), dimension(:), allocatable dz
spatial discretization
Definition: globals.f90:106
int *** free_3Dmatrix(int ***amat)
free 3D integer matrix
Definition: allocation.cc:111
#define AMAT(i, j, k)
Respect periodic boundary conditions in matrix.
Definition: globals.hh:19
#define MIN(x, y)
Lesser of two values.
Definition: globals.hh:11
Defines global variables, macros, templates and namespace.
m
Bubble Growth Application Recipe.
Definition: bubbleGrowth.py:59
int nx
domain size in X
Definition: globals.cc:19
#define COUNT(i, j, k, a)
Definition: walls.cc:14
integer ncell
number of cells
Definition: globals.f90:15
namespace with global variables
Definition: globals.f90:8
int *** alloc_3Dmatrix(int nx, int ny, int nz)
allocate 3D integer matrix
Definition: allocation.cc:83
#define ABS(x)
Absolute value.
Definition: globals.hh:9
#define V_CELL(m, i, j, k)
Respect periodic boundary conditions cell volume matrix.
Definition: globals.hh:21
int ny
domain size in Y
Definition: globals.cc:20