main.cpp 5.38 KB
Newer Older
incardon's avatar
incardon committed
1
#include "Grid/grid_dist_id.hpp"
2
#include "data_type/aggregate.hpp"
incardon's avatar
incardon committed
3
4
#include "Decomposition/CartDecomposition.hpp"

Pietro Incardona's avatar
Pietro Incardona committed
5
/*!
incardon's avatar
incardon committed
6
 * \page Grid_1_stencil Stencil example
incardon's avatar
incardon committed
7
8
 *
 *
Pietro Incardona's avatar
Pietro Incardona committed
9
 * # Stencil example and ghost # {#e1_st}
incardon's avatar
incardon committed
10
 *
Pietro Incardona's avatar
Pietro Incardona committed
11
12
13
 * This example show how to move grid_key in order to create a Laplacian stencil,
 * be careful, the function move is convenient, but not the fastest implementation.
 * We also show how to do ghost communications
incardon's avatar
incardon committed
14
15
16
 *
 */

Pietro Incardona's avatar
Pietro Incardona committed
17
/*!
incardon's avatar
incardon committed
18
 * \page Grid_1_stencil Stencil example
incardon's avatar
incardon committed
19
 *
20
 * Define some convenient constants and types
incardon's avatar
incardon committed
21
 *
Pietro Incardona's avatar
Pietro Incardona committed
22
23
 * \snippet Grid/1_stencil/main.cpp useful constant
 *
incardon's avatar
incardon committed
24
 */
Pietro Incardona's avatar
Pietro Incardona committed
25
26
27

//! \cond [useful constant] \endcond

incardon's avatar
incardon committed
28
29
30
31
constexpr size_t x = 0;
constexpr size_t y = 1;
constexpr size_t z = 2;

32
33
34
constexpr size_t A = 0;
constexpr size_t B = 0;

Pietro Incardona's avatar
Pietro Incardona committed
35
//! \cond [useful constant] \endcond
36

incardon's avatar
incardon committed
37
38
int main(int argc, char* argv[])
{
Pietro Incardona's avatar
Pietro Incardona committed
39
	/*!
incardon's avatar
incardon committed
40
	 * \page Grid_1_stencil Stencil example
Pietro Incardona's avatar
Pietro Incardona committed
41
42
43
44
45
46
47
48
49
50
51
52
53
	 *
	 * ## Initialization ## {#e1_st_init}
	 *
	 * Initialize the library and several objects
	 *
	 * \see \ref e0_s_initialization
	 *
	 * \snippet Grid/1_stencil/main.cpp parameters
	 *
	 *
	 */

	//! \cond [parameters] \endcond
incardon's avatar
incardon committed
54

Pietro Incardona's avatar
Pietro Incardona committed
55
	openfpm_init(&argc,&argv);
incardon's avatar
incardon committed
56

Pietro Incardona's avatar
Pietro Incardona committed
57
	// domain
incardon's avatar
incardon committed
58
59
	Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});

Pietro Incardona's avatar
Pietro Incardona committed
60
61
62
63
	// grid sizes
	size_t sz[3] = {100,100,100};

	// ghost extension
incardon's avatar
incardon committed
64
65
	Ghost<3,float> g(0.03);

Pietro Incardona's avatar
Pietro Incardona committed
66
67
68
	//! \cond [parameters] \endcond

	/*!
incardon's avatar
incardon committed
69
	 * \page Grid_1_stencil Stencil example
Pietro Incardona's avatar
Pietro Incardona committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
	 *
	 * ## Grid create ## {#e1_st_inst}
	 *
	 * Create a distributed grid in 3D. With typedef we create an alias name for aggregate<float[3],float[3]>.
	 * In practice the type of grid_point == aggregate<float[3],float[3]>
	 *
	 * \see \ref e0_s_grid_inst
	 *
	 * \snippet Grid/1_stencil/main.cpp grid
	 *
	 */

	//! \cond [grid] \endcond

	// a convenient alias for aggregate<...>
	typedef aggregate<float,float> grid_point;

87
	grid_dist_id<3, float, grid_point> g_dist(sz,domain,g);
incardon's avatar
incardon committed
88

Pietro Incardona's avatar
Pietro Incardona committed
89
90
91
	//! \cond [grid] \endcond

	/*!
incardon's avatar
incardon committed
92
	 * \page Grid_1_stencil Stencil example
Pietro Incardona's avatar
Pietro Incardona committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
	 *
	 * ## Loop over grid points ## {#e1_s_loop_gp}
	 *
	 * Get an iterator that go through the point of the domain (No ghost)
	 *
	 * \see \ref e0_s_loop_gp
	 *
	 * \snippet Grid/1_stencil/main.cpp iterator
	 * \snippet Grid/1_stencil/main.cpp iterator2
	 *
	 */

	//! \cond [iterator] \endcond

incardon's avatar
incardon committed
107
108
109
110
	auto dom = g_dist.getDomainIterator();

	while (dom.isNext())
	{
Pietro Incardona's avatar
Pietro Incardona committed
111
112
113
114

		//! \cond [iterator] \endcond

		/*!
incardon's avatar
incardon committed
115
		 * \page Grid_1_stencil Stencil example
Pietro Incardona's avatar
Pietro Incardona committed
116
117
118
119
120
121
122
123
124
125
126
		 *
		 * Inside the cycle we get the local grid key
		 *
		 * \see \ref e0_s_grid_coord
		 *
		 * \snippet Grid/1_stencil/main.cpp local key
		 *
		 */

		//! \cond [local key] \endcond

incardon's avatar
incardon committed
127
128
		auto key = dom.get();

Pietro Incardona's avatar
Pietro Incardona committed
129
130
131
		//! \cond [local key] \endcond

		/*!
incardon's avatar
incardon committed
132
		 * \page Grid_1_stencil Stencil example
Pietro Incardona's avatar
Pietro Incardona committed
133
134
135
136
137
138
139
140
141
142
143
144
		 *
		 * We convert the local grid position, into global position, key_g contain 3 integers that identify the position
		 * of the grid point in global coordinates
		 *
		 * \see \ref e0_s_grid_coord
		 *
		 * \snippet Grid/1_stencil/main.cpp global key
		 *
		 */

		//! \cond [global key] \endcond

incardon's avatar
incardon committed
145
146
		auto key_g = g_dist.getGKey(key);

Pietro Incardona's avatar
Pietro Incardona committed
147
		//! \cond [global key] \endcond
incardon's avatar
incardon committed
148

Pietro Incardona's avatar
Pietro Incardona committed
149
		/*!
incardon's avatar
incardon committed
150
		 * \page Grid_1_stencil Stencil example
Pietro Incardona's avatar
Pietro Incardona committed
151
152
153
154
155
156
157
158
159
		 *
		 * we write on the grid point of position (i,j,k) the value i*i + j*j + k*k on the property A.
		 * Mathematically is equivalent to the function
		 *
		 * \f$ f(x,y,z) = x^2 + y^2 + z^2 \f$
		 *
		 * \snippet Grid/1_stencil/main.cpp function
		 *
		 */
incardon's avatar
incardon committed
160

Pietro Incardona's avatar
Pietro Incardona committed
161
162
163
164
165
		//! \cond [function] \endcond

		g_dist.template get<A>(key) = key_g.get(0)*key_g.get(0) + key_g.get(1)*key_g.get(1) + key_g.get(2)*key_g.get(2);

		//! \cond [function] \endcond
incardon's avatar
incardon committed
166

Pietro Incardona's avatar
Pietro Incardona committed
167
168
169
		//! \cond [iterator2] \endcond

		++dom;
incardon's avatar
incardon committed
170
171
	}

Pietro Incardona's avatar
Pietro Incardona committed
172
173
174
	//! \cond [iterator2] \endcond

	/*!
incardon's avatar
incardon committed
175
	 * \page Grid_1_stencil Stencil example
Pietro Incardona's avatar
Pietro Incardona committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
	 *
	 * ## Ghost ## {#e1_s_ghost}
	 *
	 * Each sub-domain has an extended part, that is materially contained into another processor.
	 * In general is not synchronized
	 * ghost_get<A> synchronize the property A in the ghost part
	 *
	 * \snippet Grid/1_stencil/main.cpp ghost
	 *
	 */

	//! \cond [ghost] \endcond

	g_dist.template ghost_get<A>();
incardon's avatar
incardon committed
190
	
Pietro Incardona's avatar
Pietro Incardona committed
191
192
193
	//! \cond [ghost] \endcond

	/*!
incardon's avatar
incardon committed
194
	 * \page Grid_1_stencil Stencil example
Pietro Incardona's avatar
Pietro Incardona committed
195
196
197
198
199
200
201
202
203
204
	 *
	 * Get again another iterator, iterate across all the domain points, calculating a Laplace stencil. Write the
	 * result on B
	 *
	 * \snippet Grid/1_stencil/main.cpp laplacian
	 *
	 */

	//! \cond [laplacian] \endcond

Pietro Incardona's avatar
Pietro Incardona committed
205
	auto dom2 = g_dist.getDomainIterator();
incardon's avatar
incardon committed
206
	
Pietro Incardona's avatar
Pietro Incardona committed
207
	while (dom2.isNext())
incardon's avatar
incardon committed
208
	{
Pietro Incardona's avatar
Pietro Incardona committed
209
		auto key = dom2.get();
incardon's avatar
incardon committed
210
211

		// Laplace stencil
Pietro Incardona's avatar
Pietro Incardona committed
212
213
214
215
		g_dist.template get<B>(key) = g_dist.template get<A>(key.move(x,1)) + g_dist.template get<A>(key.move(x,-1)) +
		                                 g_dist.template get<A>(key.move(y,1)) + g_dist.template get<A>(key.move(y,-1)) +
										 g_dist.template get<A>(key.move(z,1)) + g_dist.template get<A>(key.move(z,-1)) -
										 6*g_dist.template get<A>(key);
incardon's avatar
incardon committed
216

Pietro Incardona's avatar
Pietro Incardona committed
217
		++dom2;
incardon's avatar
incardon committed
218
219
	}

Pietro Incardona's avatar
Pietro Incardona committed
220
221
222
	//! \cond [laplacian] \endcond

	/*!
incardon's avatar
incardon committed
223
	 * \page Grid_1_stencil Stencil example
Pietro Incardona's avatar
Pietro Incardona committed
224
225
226
227
	 *
	 *
	 * Finally we want a nice output to visualize the information stored by the distributed grid
	 *
Pietro Incardona's avatar
Pietro Incardona committed
228
229
	 * \see \ref e0_s_VTK_vis
	 *
Pietro Incardona's avatar
Pietro Incardona committed
230
231
232
233
234
235
	 * \snippet Grid/1_stencil/main.cpp output
	 *
	 */

	//! \cond [output] \endcond

incardon's avatar
incardon committed
236
237
	g_dist.write("output");

Pietro Incardona's avatar
Pietro Incardona committed
238
239
240
	//! \cond [output] \endcond

	/*!
incardon's avatar
incardon committed
241
	 * \page Grid_1_stencil Stencil example
Pietro Incardona's avatar
Pietro Incardona committed
242
243
244
245
246
247
248
249
250
	 *
	 * Deinitialize the library
	 *
	 * \snippet Grid/1_stencil/main.cpp finalize
	 *
	 */

	//! \cond [finalize] \endcond

Pietro Incardona's avatar
Pietro Incardona committed
251
	openfpm_finalize();
Pietro Incardona's avatar
Pietro Incardona committed
252
253
254
255

	//! \cond [finalize] \endcond

	/*!
incardon's avatar
incardon committed
256
	 * \page Grid_1_stencil Stencil example
Pietro Incardona's avatar
Pietro Incardona committed
257
258
259
260
261
262
	 *
	 * # Full code # {#code}
	 *
	 * \include Grid/1_stencil/main.cpp
	 *
	 */
incardon's avatar
incardon committed
263
}