vector_dist_performance_common.hpp 6.41 KB
Newer Older
incardon's avatar
incardon committed
1 2 3 4 5 6 7 8 9 10
/*
 * vector_dist_performance_common.hpp
 *
 *  Created on: Dec 25, 2016
 *      Author: i-bird
 */

#ifndef SRC_VECTOR_PERFORMANCE_VECTOR_DIST_PERFORMANCE_COMMON_HPP_
#define SRC_VECTOR_PERFORMANCE_VECTOR_DIST_PERFORMANCE_COMMON_HPP_

incardon's avatar
incardon committed
11
#include "Vector/vector_dist.hpp"
incardon's avatar
incardon committed
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136

/*! \brief Calculate and put particles' forces
 *
 * \param NN Cell list
 * \param vd Distributed vector
 * \param r_cut Cut-off radius
 */
template<unsigned int dim, size_t prp = 0, typename T, typename V> void calc_forces(T & NN, V & vd, float r_cut)
{
	auto it_v = vd.getDomainIterator();

	float sum[dim];

	for (size_t i = 0; i < dim; i++)
		sum[i] = 0;

	while (it_v.isNext())
	{
		//key
		vect_dist_key_dx key = it_v.get();

    	// Get the position of the particles
		Point<dim,float> p = vd.getPos(key);

		for (size_t i = 0; i < dim; i++)
			sum[i] = 0;

    	// Get the neighborhood of the particle
    	auto cell_it = NN.template getNNIterator<NO_CHECK>(NN.getCell(p));

    	while(cell_it.isNext())
    	{
    		auto nnp = cell_it.get();

    		// p != q
    		if (nnp == key.getKey())
    		{
    			++cell_it;
    			continue;
    		}

    		Point<dim,float> q = vd.getPos(nnp);

    		if (p.distance2(q) < r_cut*r_cut)
    		{
				//Calculate the forces
    			float num[dim];
    			for (size_t i = 0; i < dim; i++)
    				num[i] = vd.getPos(key)[i] - vd.getPos(nnp)[i];

    			float denom = 0;
    			for (size_t i = 0; i < dim; i++)
					denom += num[i] * num[i];

    			float res[dim];
    			for (size_t i = 0; i < dim; i++)
					res[i] = num[i] / denom;

    			for (size_t i = 0; i < dim; i++)
					sum[i] += res[i];
    		}
			//Next particle in a cell
			++cell_it;
		}

		//Put the forces
		for (size_t i = 0; i < dim; i++)
			vd.template getProp<prp>(key)[i] += sum[i];

		//Next particle in cell list
		++it_v;
	}
}

/*! \brief For each particle of vd calculate the accumulation of the distances of the neighborhood
 *          particles inside vd2
 *
 *
 * \param NN Cell list vd
 * \param NN2 Cell list vd2
 * \param vd Distributed vector
 * \param vd2 Distributed vector 2
 * \param r_cut Cut-off radius
 *
 */
template<unsigned int dim, unsigned int prp, typename T, typename V> void cross_calc(T & NN, T & NN2, V & vd, V & vd2)
{
	auto it_v = vd.getDomainIterator();

	while (it_v.isNext())
	{
		//key
		vect_dist_key_dx key = it_v.get();

    	// Get the position of the particles
		Point<dim,float> p = vd.getPos(key);

    	// Get the neighborhood of the particle
    	auto cell_it = NN2.template getNNIterator<NO_CHECK>(NN2.getCell(p));

    	double sum = 0.0;

    	while(cell_it.isNext())
    	{
    		auto nnp = cell_it.get();

    		Point<dim,float> q = vd2.getPos(nnp);

    		sum += norm(p - q);

			//Next particle in a cell
			++cell_it;
		}

		vd.template getProp<prp>(key) = sum;

		//Next particle in cell list
		++it_v;
	}
}

/*! \brief Initialize a distributed vector
 *
 * \param vd Distributed vector
 */
incardon's avatar
incardon committed
137 138
template<unsigned int dim, typename v_dist>
void vd_initialize(v_dist & vd, Vcluster<HeapMemory> & v_cl)
incardon's avatar
incardon committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152
{
	// The random generator engine
	std::default_random_engine eg(v_cl.getProcessUnitID()*4313);
	std::uniform_real_distribution<float> ud(0.0f, 1.0f);

	//! [Create a vector of random elements on each processor 2D]

	auto it = vd.getIterator();

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

		for (size_t i = 0; i < dim; i++)
incardon's avatar
incardon committed
153
		{vd.getPos(key)[i] = ud(eg);}
incardon's avatar
incardon committed
154 155 156 157 158 159 160

		++it;
	}

	vd.map();
}

incardon's avatar
incardon committed
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
/*! \brief Initialize a distributed vector
 *
 * \param vd Distributed vector
 * \param box where to initialize
 * \param npart number of particles to add
 *
 */
template<unsigned int dim, typename v_dist>
void vd_initialize_box_nomap(v_dist & vd, const Box<dim,typename v_dist::stype> & box, Vcluster<HeapMemory> & v_cl, int npart)
{
	// The random generator engine
	std::default_random_engine eg(v_cl.getProcessUnitID()*4313);
	std::uniform_real_distribution<float> ud(0.0f, 1.0f);

	size_t n = vd.size_local();
	size_t n_stop = n + npart;

	auto it = vd.getIterator(n,n_stop);

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

		for (size_t i = 0; i < dim; i++)
		{vd.getPos(key)[i] = (box.getHigh(i) - box.getLow(i))*ud(eg) + box.getLow(i);}

		++it;
	}
}

incardon's avatar
incardon committed
191 192 193 194 195 196 197

/*! \brief Initialize 2 distributed vectors with equally positioned particles
 *
 * \param vd, vd2 Distributed vectors
 * \param v_cl Global vcluster
 * \param k_int Number of particles
 */
198
template<unsigned int dim, typename v_dist> void vd_initialize_double(v_dist & vd,v_dist & vd2, Vcluster<> & v_cl, size_t k_int)
incardon's avatar
incardon committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 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
{
	// The random generator engine
	std::default_random_engine eg(v_cl.getProcessUnitID()*4313);
	std::uniform_real_distribution<float> ud(0.0f, 1.0f);

	//! [Create a vector of random elements on each processor 2D]

	auto it = vd.getIterator();

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

		for (size_t i = 0; i < dim; i++)
		{
			vd.getPos(key)[i] = ud(eg);
			vd2.getPos(key)[i] = vd.getPos(key)[i];
		}

		++it;
	}

	vd.map();
	vd2.map();
}



/*! \brief Calculate and put particles' forces
 *
 * \param NN Cell list hilbert
 * \param vd Distributed vector
 * \param r_cut Cut-off radius
 */
template<unsigned int dim, size_t prp = 0, typename T, typename V> void calc_forces_hilb(T & NN, V & vd, float r_cut)
{
	auto it_cl = NN.getIterator();

	float sum[dim];

	for (size_t i = 0; i < dim; i++)
		sum[i] = 0;

	while (it_cl.isNext())
	{
		//key
		auto key = it_cl.get();

    	// Get the position of the particles
		Point<dim,float> p = vd.getPos(key);

		for (size_t i = 0; i < dim; i++)
			sum[i] = 0;

    	// Get the neighborhood of the particle
    	auto cell_it = NN.template getNNIterator<NO_CHECK>(NN.getCell(p));

    	while(cell_it.isNext())
    	{
    		auto nnp = cell_it.get();

    		// p != q
    		if (nnp == key)
    		{
    			++cell_it;
    			continue;
    		}

    		Point<dim,float> q = vd.getPos(nnp);

    		if (p.distance2(q) < r_cut*r_cut)
    		{
				//Calculate the forces
    			float num[dim];
    			for (size_t i = 0; i < dim; i++)
    				num[i] = vd.getPos(key)[i] - vd.getPos(nnp)[i];

    			float denom = 0;
    			for (size_t i = 0; i < dim; i++)
					denom += num[i] * num[i];

    			float res[dim];
    			for (size_t i = 0; i < dim; i++)
					res[i] = num[i] / denom;

    			for (size_t i = 0; i < dim; i++)
					sum[i] += res[i];
    		}
			//Next particle in a cell
			++cell_it;
		}

		//Put the forces
		for (size_t i = 0; i < dim; i++)
			vd.template getProp<prp>(key)[i] += sum[i];

		//Next particle in cell list
		++it_cl;
	}
}

#endif /* SRC_VECTOR_PERFORMANCE_VECTOR_DIST_PERFORMANCE_COMMON_HPP_ */