main.cpp 8.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
/*
 * ### WIKI 1 ###
 *
 * ## Simple example
 *
 * In this example we show 1D PSE derivative function approximation
 *
 * ### WIKI END ###
 *
 */

#include "Vector/vector_dist.hpp"
#include "Decomposition/CartDecomposition.hpp"
#include "PSE/Kernels.hpp"
#include "Plot/util.hpp"
#include "Plot/GoogleChart.hpp"
#include "data_type/aggregate.hpp"
#include <cmath>

struct animal
{
	typedef boost::fusion::vector<float[2], size_t, size_t, long int> type;

	//! Attributes name
	struct attributes
	{
		static const std::string name[];
	};

	//! type of the positional field
	typedef float s_type;

	//! The data
	type data;

	//! position property id in boost::fusion::vector
	static const unsigned int pos = 0;
	//! genre of animal property id in boost::fusion::vector
	static const unsigned int genre = 1;
	//! state property id in boost::fusion::vector
	static const unsigned int status = 2;
	//! alive time property id in boost::fusion::vector
	static const unsigned int time_a = 3;

	//! total number of properties boost::fusion::vector
	static const unsigned int max_prop = 4;

	animal()
	{
	}

	inline animal(const animal & p)
	{
		boost::fusion::at_c<0>(data)[0] = boost::fusion::at_c<0>(p.data)[0];
		boost::fusion::at_c<0>(data)[1] = boost::fusion::at_c<0>(p.data)[1];
		//boost::fusion::at_c<0>(data)[2] = boost::fusion::at_c<0>(p.data)[2];
		boost::fusion::at_c<1>(data) = boost::fusion::at_c<1>(p.data);
		boost::fusion::at_c<2>(data) = boost::fusion::at_c<2>(p.data);
		boost::fusion::at_c<3>(data) = boost::fusion::at_c<3>(p.data);
	}

	template<unsigned int id> inline auto get() -> decltype(boost::fusion::at_c < id > (data))
	{
		return boost::fusion::at_c<id>(data);
	}

	template<unsigned int id> inline auto get() const -> const decltype(boost::fusion::at_c < id > (data))
	{
		return boost::fusion::at_c<id>(data);
	}

	template<unsigned int dim, typename Mem> inline animal(const encapc<dim, animal, Mem> & p)
	{
		this->operator=(p);
	}

	template<unsigned int dim, typename Mem> inline animal & operator=(const encapc<dim, animal, Mem> & p)
	{
		boost::fusion::at_c<0>(data)[0] = p.template get<0>()[0];
		boost::fusion::at_c<0>(data)[1] = p.template get<0>()[1];
		//boost::fusion::at_c<0>(data)[2] = p.template get<0>()[2];
		boost::fusion::at_c<1>(data) = p.template get<1>();
		boost::fusion::at_c<2>(data) = p.template get<2>();
		boost::fusion::at_c<3>(data) = p.template get<3>();

		return *this;
	}

	static bool noPointers()
	{
		return true;
	}
};

const std::string animal::attributes::name[] = { "pos", "genre", "status", "time_a", "j_repr" };

int main(int argc, char* argv[])
{
	init_global_v_cluster(&argc,&argv);

	Vcluster & v_cl = *global_v_cluster;

	//time the animal stays alive without eating
	size_t PRED_TIME_A = 14;

	size_t PREY_TIME_A = 7;

	size_t PREDATOR = 1, PREY = 0;
	size_t ALIVE = 1, DEAD = 0;

	// Predators reproducing probability
	float PRED_REPR = 0.2;

	// Predators eating probability
	float PRED_EAT = 0.6;

	// Prey reproducing probability
	float PREY_REPR = 0.5;

	// set the seed
	// create the random generator engine
	std::srand(v_cl.getProcessUnitID());
123
124
	unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
	std::default_random_engine eg(seed);
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
181
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
209
210
211
212
213
214
215
216
217
218
219
220
	std::uniform_real_distribution<float> ud(0.0f, 1.0f);
	std::uniform_real_distribution<float> md(-1.0f, 1.0f);
	std::uniform_real_distribution<float> uc(0.0f, 0.7f);
	std::uniform_real_distribution<float> lc(0.3f, 1.0f);

	size_t k = 100000;

	Box<2, float> box( { 0.0, 0.0 }, { 1.0, 1.0 });

	// Grid info
	grid_sm<2, void> info( { 8, 8 });

	// Boundary conditions
	size_t bc[2] = { PERIODIC, PERIODIC };

	// factor
	float factor = pow(global_v_cluster->getProcessingUnits() / 2.0f, 1.0f / 3.0f);

	// interaction radius
	float r_cut = 0.01 / factor;

	// ghost
	Ghost<2, float> ghost(r_cut);

	// Distributed vector
	vector_dist<2, float, animal, CartDecomposition<2, float, HeapMemory, ParMetisDistribution<2, float>>> vd(k,box,bc,ghost);

	// Init DLB tool
	DLB dlb(v_cl);

	// Set unbalance threshold
	dlb.setHeurisitc(DLB::Heuristic::UNBALANCE_THRLD);
	dlb.setThresholdLevel(DLB::ThresholdLevel::THRLD_MEDIUM);

	auto it = vd.getIterator();

	while (it.isNext())
	{
		auto key = it.get();
		if(ud(eg) < 0.7 )
		{
			vd.template getPos<animal::pos>(key)[0] = lc(eg);
			vd.template getPos<animal::pos>(key)[1] = lc(eg);
			vd.template getProp<animal::genre>(key) = PREY;
			vd.template getProp<animal::status>(key) = ALIVE;
			vd.template getProp<animal::time_a>(key) = PREY_TIME_A;
		}
		else
		{
			vd.template getPos<animal::pos>(key)[0] = uc(eg);
			vd.template getPos<animal::pos>(key)[1] = uc(eg);
			vd.template getProp<animal::genre>(key) = PREDATOR;
			vd.template getProp<animal::status>(key) = ALIVE;
			vd.template getProp<animal::time_a>(key) = PRED_TIME_A;
		}
		++it;
	}

	vd.map();

	vd.addComputationCosts();

	vd.getDecomposition().rebalance(dlb);

	vd.map();

	//vd.getDecomposition().getDistribution().write("parmetis_prey_predators_" + std::to_string(0) + ".vtk");
	//vd.write("particles_", 0, NO_GHOST);

	// 100 step random walk
	for (size_t j = 0; j < 100; j++)
	{
		size_t prey = 0, predators = 0;

		auto it = vd.getDomainIterator();

		while (it.isNext())
		{
			auto key = it.get();

			vd.template getPos<animal::pos>(key)[0] += 0.01 * md(eg);
			vd.template getPos<animal::pos>(key)[1] += 0.01 * md(eg);

			if(vd.template getProp<animal::genre>(key) == PREY)
			prey++;
			else
			predators++;

			++it;
		}

		vd.map();


		/////// Interactions ///
		// get ghosts
221
222

		vd.ghost_get<0>();
223
224
225
226
227
228
229
230
231
232

		// vector of dead animals
		openfpm::vector<size_t> deads;
		openfpm::vector<vect_dist_key_dx> reps_prey;
		openfpm::vector<vect_dist_key_dx> reps_pred;

		// get the cell list with a cutoff radius

		bool error = false;

233
		auto NN = vd.getCellList(0.01/factor);
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253

		// iterate across the domain particle

		auto it2 = vd.getDomainIterator();

		while (it2.isNext())
		{
			auto p = it2.get();

			Point<2,float> xp = vd.getPos<0>(p);

			size_t gp = vd.getProp<animal::genre>(p);
			size_t sp = vd.getProp<animal::status>(p);

			if(sp == ALIVE)
			{
				if(gp == PREY)
				{
					if( prey < k/1.5 && ud(eg) < PREY_REPR )
						reps_prey.add(p);
254

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
					vd.getProp<animal::time_a>(p)--;

					if(vd.getProp<animal::time_a>(p) <= 0)
					{
						vd.getProp<animal::status>(p) = DEAD;
						prey--;
					}
				}
				else if(gp == PREDATOR)
				{
					vd.getProp<animal::time_a>(p)--;

					if(vd.getProp<animal::time_a>(p) <= 0)
					{
						vd.getProp<animal::status>(p) = DEAD;
					}
					else
					{
273
						auto Np = NN.getIterator(NN.getCell(xp));
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353

						while (Np.isNext())
						{
							auto q = Np.get();

							size_t gq = vd.getProp<animal::genre>(q);
							size_t sq = vd.getProp<animal::status>(q);

							Point<2,float> xq = vd.getPos<0>(q);
							Point<2,float> f = (xp - xq);

							float distance = f.norm();

							if (distance < 2*r_cut*sqrt(2) && gq == PREY && sq == ALIVE)
							{
								if( ud(eg) < PRED_EAT )
								{
									vd.getProp<animal::status>(q) = DEAD;
									vd.getProp<animal::time_a>(p) = PRED_TIME_A;

									if( ud(eg) < PRED_REPR )
										reps_pred.add(p);
								}
							}
							++Np;
						}
					}
				}

			}

			++it2;
		}

		vd.deleteGhost();

		// Replicate

		for (size_t i = 0 ; i < reps_prey.size() ; i++)
		{
			vd.add();
			vd.getLastPos<animal::pos>()[0] = vd.getPos<0>(reps_prey.get(i))[0];
			vd.getLastPos<animal::pos>()[1] = vd.getPos<0>(reps_prey.get(i))[1];
			vd.getLastProp<animal::genre>() = PREY;
			vd.getLastProp<animal::status>() = ALIVE;
			vd.getLastProp<animal::time_a>() = PREY_TIME_A;
		}

		for (size_t i = 0 ; i < reps_pred.size() ; i++)
		{
			vd.add();
			vd.getLastPos<animal::pos>()[0] = vd.getPos<0>(reps_pred.get(i))[0];
			vd.getLastPos<animal::pos>()[1] = vd.getPos<0>(reps_pred.get(i))[1];
			vd.getLastProp<animal::genre>() = PREDATOR;
			vd.getLastProp<animal::status>() = ALIVE;
			vd.getLastProp<animal::time_a>() = PRED_TIME_A;
		}

		auto it3 = vd.getDomainIterator();
		while (it3.isNext())
		{
			auto key = it3.get();
			if(vd.getProp<animal::status>(key.getKey()) == DEAD)
			{
				deads.add(key.getKey());
			}
			++it3;
		}

		deads.sort();
		vd.remove(deads, 0);

		deads.resize(0);

		vd.deleteGhost();

		////////////////////////

		vd.addComputationCosts();

354
		vd.getDecomposition().rebalance(dlb);
355
356
357
358
359
360
361
362
363
364
365

		vd.map();
	}

	//
	// ### WIKI 10 ###
	//
	// Deinitialize the library
	//
	delete_global_v_cluster();
}