main.cpp 9.83 KB
Newer Older
Pietro Incardona's avatar
Pietro Incardona committed
1
2
3
4
#include "Grid/grid_dist_id.hpp"
#include "data_type/aggregate.hpp"

/*!
incardon's avatar
incardon committed
5
 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
6
7
8
9
10
11
12
13
14
15
16
 *
 * [TOC]
 *
 * # Simple example of grid usage to solve an equation # {#e2_solve_eq}
 *
 * This example show the usage of grid to solve the following equation
 *
 * \f$\frac{\partial^2 u}{\partial^2 x} + \frac{\partial^2 u}{\partial^2 y} = 1\f$
 * 
 * \f$u(x,y) = 0 \f$
 *
incardon's avatar
incardon committed
17
18
19
20
21
 * at the boundary. This lead to the solution shown in the picture
 *
 * \htmlonly
 * <img src="http://ppmcore.mpi-cbg.de/web/images/examples/2_solve_eq/solution.jpg"/>
 * \endhtmlonly
Pietro Incardona's avatar
Pietro Incardona committed
22
23
24
25
26
27
28
29
30
31
32
 * 
 *
 * ## Field initialization ## {#e2_se_finit}
 *
 */

//! \cond [field init] \endcond

void init(grid_dist_id<2,double,aggregate<double> > & g_dist, const size_t (& sz)[2])
{
	/*!
incardon's avatar
incardon committed
33
	 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	 *
	 * In order to initialize the field U, first we get an iterator that cover
	 *  domain + Ghost to iterate all the grid points.
	 * Inside the cycle we initialize domain and border (Ghost part to 0.0)
	 *
	 * \see \ref e0_s_loop_gp
	 *
	 */

	//! \cond [iterator] \endcond

	// Get the iterator
	auto it = g_dist.getDomainGhostIterator();

	// For each point in the grid
	while (it.isNext())
	{
		//! \cond [iterator] \endcond

		/*!
incardon's avatar
incardon committed
54
		 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
		 *
		 * Get the local grid key
		 *
		 * \see \ref e0_s_grid_coord
		 *
		 * \snippet Grid/2_solve_eq/main.cpp local key
		 *
		 */

		//! \cond [local key] \endcond

		auto key = it.get();

		//! \cond [local key] \endcond

		/*!
incardon's avatar
incardon committed
71
		 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
		 *
		 *
		 * Here 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/2_solve_eq/main.cpp global key
		 *
		 */

		//! \cond [global key] \endcond

		auto key_g = g_dist.getGKey(key);

		//! \cond [global key] \endcond

		/*!
incardon's avatar
incardon committed
91
		 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
		 *
		 * Initialize to 0, domain + boundary
		 *
		 * \snippet Grid/2_solve_eq/main.cpp init field zero
		 *
		 * The full function look like this
		 *
		 * \snippet Grid/2_solve_eq/main.cpp field init
		 *
		 */

		//! \cond [init field zero] \endcond

		if (key_g.get(0) == 0 || key_g.get(0) == sz[0] ||
			key_g.get(1) == 0 || key_g.get(1) == sz[1])
		{
			// Boundary part
			g_dist.template get<0>(key) = 0.0;
		}
		else
		{
			// Internal part
			g_dist.template get<0>(key) = 0.0;
		}

		//! \cond [init field zero] \endcond

		//! \cond [iterator2] \endcond

		++it;

	}

	//! \cond [iterator2] \endcond
}

//! \cond [field init] \endcond

constexpr int x = 0;
constexpr int y = 1;

int main(int argc, char* argv[])
{
	/*!
incardon's avatar
incardon committed
136
	 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	 *
	 * ## Initialization ##
	 *
	 * Initialize the library
	 *
	 * \see \ref e0_s_initialization
	 *
	 * \snippet Grid/2_solve_eq/main.cpp ofp_init
	 *
	 */

	//! \cond [ofp_init] \endcond

	openfpm_init(&argc,&argv);
	
	//! \cond [ofp_init] \endcond

	/*!
incardon's avatar
incardon committed
155
	 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
	 *
	 * ## Grid instantiation and initialization ##
	 *
	 * Create
	 * * A 2D box that define the domain
	 * * an array of 2 unsigned integer that will define the size of the grid on each dimension
	 * * A Ghost object that will define the extension of the ghost part for each sub-domain in grid point unit
	 *
	 * \snippet Grid/2_solve_eq/main.cpp ofp_par
	 *
	 */

	//! \cond [ofp_par] \endcond

	Box<2,double> domain({-1.0,-1.0},{1.0,1.0});
	size_t sz[2] = {64,64};
	
	periodicity<2> bc = {NON_PERIODIC,NON_PERIODIC};
	
	// Ghost in grid unit
	Ghost<2,long int> g(1);
	
	//! \cond [ofp_par] \endcond

	/*!
incardon's avatar
incardon committed
181
	 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
	 *
	 * Create a distributed grid in 2D (1° template parameter) space in with double precision (2° template parameter)
	 * each grid point contain a scalar (double),
	 *
	 * Constructor parameters:
	 *
	 * * sz: size of the grid on each dimension
	 * * domain: where the grid is defined
	 * * g: ghost extension
	 * * bc: boundary conditions
	 *
	 * \see \ref e0_s_grid_inst
	 *
	 * \snippet Grid/2_solve_eq/main.cpp grid inst
	 *
	 */

	//! \cond [grid inst] \endcond

	grid_dist_id<2, double, aggregate<double>> g_dist(sz,domain,g,bc);
	
	// spacing between two points
	double spacing[2] = {g_dist.spacing(0),g_dist.spacing(1)};

	//! \cond [grid inst] \endcond

	/*!
incardon's avatar
incardon committed
209
	 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
	 *
	 * Initialize U and fill the boundary conditions
	 *
	 * \see \ref e2_se_field_init
	 *
	 * \snippet Grid/2_solve_eq/main.cpp grid init
	 *
	 */

	//! \cond [grid init] \endcond

	init(g_dist,sz);

	//! \cond [grid init] \endcond

	/*!
incardon's avatar
incardon committed
226
	 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
	 *
	 * ## %Ghost synchronization ##
	 *
	 * Before the computation read the ghost point we have to guarantee that they have
	 *  updated values.
	 *
	 *  \snippet Grid/2_solve_eq/main.cpp ghost sync
	 *
	 */

	//! \cond [ghost sync] \endcond

	// sync the ghost property 0
	g_dist.template ghost_get<0>();

	//! \cond [ghost sync] \endcond

	/*!
incardon's avatar
incardon committed
245
	 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
	 *
	 * ## Red-Black alghorithm ##
	 *
	 * Do 10000 iteration of Red-Black Gauss-Siedel iterations
	 *
	 *
	 */

	//! \cond [gs_alg] \endcond

	// flag that indicate if we are processing red or black
	// we start from red
	bool red_black = true;

	// 10000 iterations
	for (size_t i = 0 ; i < 10000 ; i++)
	{
		/*!
incardon's avatar
incardon committed
264
		 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
		 *
		 * Get an iterator that go through the points of the grid (No ghost)
		 * To compute one iteration.
		 *
		 * \see \ref e0_s_loop_gp
		 * \see \ref e0_s_grid_coord
		 *
		 * \snippet Grid/2_solve_eq/main.cpp gs_it
		 *
		 */

		//! \cond [gs_it] \endcond

		auto dom = g_dist.getDomainIterator();
		
		// Iterate over all the points
		while (dom.isNext())
		{

			// Get the local grid key
			auto key = dom.get();

			// Here 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
			auto key_g = g_dist.getGKey(key);


			//
			// If we are processing red and is odd jump to the next point
			// If we are processing black and is even jump to the next point
			//
			if (red_black == false && (key_g.get(0) + key_g.get(1)) % 2 == 0)
			{++dom; continue;}
			else if (red_black == true && (key_g.get(0) + key_g.get(1)) % 2 == 1)
			{++dom; continue;}

			//
			// Update the grid values
			//
			// P.S. The keyword template is removed, it is possible only if we are in a function
			// without template parameters (if you are unsure use the keyword template)
			//
			g_dist.get<0>(key) = (g_dist.get<0>(key.move(x,1)) + g_dist.template get<0>(key.move(x,-1)) +
                    			 g_dist.get<0>(key.move(y,1)) + g_dist.template get<0>(key.move(y,-1)) +
								 - 1.0)/4.0;

			//
			// next point (red/black)
			++dom;
		}

		//! \cond [gs_it] \endcond

		/*!
incardon's avatar
incardon committed
319
		 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
		 *
		 *
		 * Once an iteration is done we have to synchronize the ghosts
		 * to start a new iteration. Consider that we calculated the red points
		 * in the next iteration the red points in the ghost are used in reading.
		 * This mean that the ghost must have the updated values
		 *
		 * \snippet Grid/2_solve_eq/main.cpp ghost sync2
		 *
		 *
		 */

		//! \cond [ghost sync2] \endcond

		g_dist.template ghost_get<0>();

		// switch from red to black or black to red
		red_black = !red_black;

		//! \cond [ghost sync2] \endcond
	}
	
	/*!
incardon's avatar
incardon committed
343
	 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
	 *
	 * The full Algorithm look like this
	 *
	 * \snippet Grid/2_solve_eq/main.cpp gs_alg
	 */

	//! \cond [gs_alg] \endcond

	/*!
	 * \page Grid_2_solve_eq Grid 2 solve eq
	 *
	 * ## Solution statistic ##
	 *
	 * Once we got the solution we want to check if it really satisfy the equation,
	 * and calculate the error (norm infinity)
	 *
	 * \snippet Grid/2_solve_eq/main.cpp sol stat
	 *
	 */

	//! \cond [sol stat] \endcond

	// It contain the error
	double error = 0.0;

	// Get the iterator
	auto dom = g_dist.getDomainIterator();

	// Iterate over all the points
	while (dom.isNext())
	{
		// same the the grid point and the global grid point
		auto key = dom.get();

		// Calculate the error on each point
		// The error is how much the solution does not respect the equation
		double error_tmp = abs((g_dist.get<0>(key.move(x,1)) + g_dist.get<0>(key.move(x,-1)) +
                		   g_dist.get<0>(key.move(y,1)) + g_dist.get<0>(key.move(y,-1)) +
						   - 4.0*g_dist.get<0>(key)) - 1.0);

		// In the norm infinity the maximum error across all the point is
		// important
		if (error_tmp > error)
			error = error_tmp;

		// next point
		++dom;

	}

	// Get the maximum across processor to calculate the norm infinity of the error
	// Norm infinity of the error is the maximum error across all the grid points
	Vcluster & v_cl = create_vcluster();
	v_cl.max(error);
	v_cl.execute();

	// Only master print the norm infinity
	if (v_cl.getProcessUnitID() == 0)
			std::cout << "Error norm infinity: " << error << std::endl;

	//! \cond [sol stat] \endcond

	/*!
incardon's avatar
incardon committed
407
	 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
	 *
	 * ## VTK Write and visualization ##
	 *
	 * Finally we want a nice output to visualize the information stored by the distributed grid.
	 * The function write by default produce VTK files. One for each processor that can be visualized
	 * with the programs like paraview
	 *
	 * \see \ref e0_s_VTK_vis
	 *
	 * \snippet Grid/2_solve_eq/main.cpp write
	 *
	 */

	//! \cond [write] \endcond

	g_dist.write("output");

	//! \cond [write] \endcond
	

	/*!
incardon's avatar
incardon committed
429
	 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
	 *
	 * ## Finalize ##
	 *
	 *  At the very end of the program we have always to de-initialize the library
	 *
	 * \snippet Grid/2_solve_eq/main.cpp finalize
	 *
	 */

	//! \cond [finalize] \endcond

	openfpm_finalize();

	//! \cond [finalize] \endcond

	/*!
incardon's avatar
incardon committed
446
	 * \page Grid_2_solve_eq Solve equation
Pietro Incardona's avatar
Pietro Incardona committed
447
	 *
incardon's avatar
incardon committed
448
	 * # Full code # {#e2_solve_eq_code}
Pietro Incardona's avatar
Pietro Incardona committed
449
450
451
452
453
	 *
	 * \include Grid/2_solve_eq/main.cpp
	 *
	 */
}