interpolation.hpp 10.3 KB
Newer Older
incardon's avatar
incardon committed
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
/*
 * mp4_interpolation.hpp
 *
 *  Created on: May 4, 2017
 *      Author: i-bird
 */

#ifndef OPENFPM_NUMERICS_SRC_INTERPOLATION_INTERPOLATION_HPP_
#define OPENFPM_NUMERICS_SRC_INTERPOLATION_INTERPOLATION_HPP_

#include "NN/CellList/MemFast.hpp"
#include "NN/CellList/CellList.hpp"

#define INTERPOLATION_ERROR_OBJECT std::runtime_error("Runtime interpolation error");

constexpr int inte_m2p = 0;
constexpr int inte_p2m = 1;

template<typename T>
struct mul_inte
{
	inline static void value(T & result, const T & coeff, const T & src)
	{
		result += coeff * src;
	}
};

template<typename T, unsigned int N1>
struct mul_inte<T[N1]>
{
	inline static void value(T (& result)[N1], const T & coeff, const T (& src)[N1])
	{
		for (size_t i = 0 ; i < N1 ; i++)
			result[i] += coeff * src[i];
	}
};

template<typename T, unsigned int N1, unsigned int N2>
struct mul_inte<T[N1][N2]>
{
	inline static void value(T (& result)[N1][N2], const T & coeff, const T (& src)[N1][N2])
	{
		for (size_t i = 0 ; i < N1 ; i++)
			for (size_t j = 0 ; j < N2 ; j++)
				result[i][j] += coeff * src[i][j];
	}
};

49
template<unsigned int prp_g, unsigned int prp_v,unsigned int m2p_or_p2m>
incardon's avatar
incardon committed
50 51
struct inte_template
{
52
	template<typename grid, typename vector, typename iterator, typename key_type> inline static void value(grid & gd,
incardon's avatar
incardon committed
53
			                                                          vector & vd,
54
																	  const grid_dist_key_dx<vector::dims> & k_dist,
incardon's avatar
incardon committed
55
																	  iterator & key_p,
56 57
																	  const grid_cpu<vector::dims,aggregate<typename vector::stype>> & a_int,
																	  const key_type & key)
incardon's avatar
incardon committed
58
	{
59
		mul_inte<typename std::remove_reference<decltype(gd.template get<prp_g>(k_dist))>::type>::value(gd.template get<prp_g>(k_dist),a_int.template get<0>(key),vd.template getProp<prp_v>(key_p));
incardon's avatar
incardon committed
60 61 62
	}
};

63 64
template<unsigned int prp_g, unsigned int prp_v>
struct inte_template<prp_g,prp_v,inte_m2p>
incardon's avatar
incardon committed
65
{
66
	template<typename grid, typename vector, typename iterator, typename key_type> inline static void value(grid & gd,
incardon's avatar
incardon committed
67
			                                                          vector & vd,
68
																	  const grid_dist_key_dx<vector::dims> & k_dist,
incardon's avatar
incardon committed
69
																	  iterator & key_p,
70 71
																	  const grid_cpu<vector::dims,aggregate<typename vector::stype>> & a_int,
																	  const key_type & key)
incardon's avatar
incardon committed
72
	{
73
		mul_inte<typename std::remove_reference<decltype(gd.template get<prp_g>(k_dist))>::type>::value(vd.template getProp<prp_v>(key_p),a_int.template get<0>(key),gd.template get<prp_g>(k_dist));
incardon's avatar
incardon committed
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
	}
};


/*! \brief return the sub-domain where this particle must be interpolated
 *
 * \param p particle position
 *
 * \return the sub-domain id
 *
 */
template<typename vector, typename grid>
inline size_t getSub(Point<vector::dims,typename vector::stype> & p,
		             const CellList<vector::dims,typename vector::stype,Mem_fast<vector::dims,typename vector::stype>,shift<vector::dims,typename vector::stype>> & geo_cell,
					 grid & gd)
{
	size_t cell = geo_cell.getCell(p);

	for (size_t i = 0 ; i < geo_cell.getNelements(cell) ; i++)
	{
		size_t ns = geo_cell.get(cell,i);

		if (gd.getDecomposition().getSubDomain(ns).isInside(p))
			return ns;
	}

#ifdef SE_CLASS1
	std::cerr << __FILE__ << ":" << __LINE__ << " Error: " << std::endl;
	ACTION_ON_ERROR(INTERPOLATION_ERROR_OBJECT)
#endif

	return (size_t)-1;
}

108 109 110 111 112 113 114 115 116 117 118 119 120 121

template<unsigned int prp_g, unsigned int prp_v, unsigned int m2p_or_p2m, typename kernel, typename iterator, typename vector, typename grid, typename grid_inte>
																 inline void inte_calc(iterator & it,
		                     	 	 	 	 	 	 	 	 	 vector & vd,
																 const Box<vector::dims,typename vector::stype> & domain,
																 int (& ip)[vector::dims][kernel::np],
																 grid & gd,
																 const typename vector::stype (& dx)[vector::dims],
																 typename vector::stype (& xp)[vector::dims],
																 const grid_inte & a_int,
																 typename vector::stype (& a)[vector::dims][kernel::np],
																 typename vector::stype (& x)[vector::dims][kernel::np],
																 size_t (& sz)[vector::dims],
																 const CellList<vector::dims,typename vector::stype,Mem_fast<vector::dims,typename vector::stype>,shift<vector::dims,typename vector::stype>> & geo_cell)
incardon's avatar
incardon committed
122
{
123
	auto key_p = it.get();
incardon's avatar
incardon committed
124

125
	Point<vector::dims,typename vector::stype> p = vd.getPos(key_p);
incardon's avatar
incardon committed
126

127 128
	// On which sub-domain we interpolate the particle
	size_t sub = getSub<vector>(p,geo_cell,gd);
incardon's avatar
incardon committed
129

130
	typename vector::stype x0[vector::dims];
incardon's avatar
incardon committed
131

132 133 134 135
	// calculate the position of the particle in the grid
	// coordinated
	for (size_t i = 0 ; i < vector::dims ; i++)
		x0[i] = (p.get(i)-domain.getLow(i))*dx[i];
incardon's avatar
incardon committed
136

137 138 139 140 141 142
	// convert into integer
	for (size_t i = 0 ; i < vector::dims ; i++)
		ip[i][0] = (int)x0[i];

	// convert the global grid position into local grid position
	grid_key_dx<vector::dims> base;
incardon's avatar
incardon committed
143

144 145
	for (size_t i = 0 ; i < vector::dims ; i++)
		base.set_d(i,ip[i][0] - gd.getLocalGridsInfo().get(sub).origin.get(i) - (long int)kernel::np/2 + 1);
incardon's avatar
incardon committed
146

147 148 149 150
	// convenient grid of points

	for (size_t j = 0 ; j < kernel::np-1 ; j++)
	{
incardon's avatar
incardon committed
151
		for (size_t i = 0 ; i < vector::dims ; i++)
152 153
			ip[i][j+1] = (int)ip[i][j]+1;
	}
incardon's avatar
incardon committed
154

155 156
	for (size_t i = 0 ; i < vector::dims ; i++)
		xp[i] = x0[i] - ip[i][0];
incardon's avatar
incardon committed
157

158 159 160 161 162
	for (long int j = 0 ; j < kernel::np ; j++)
	{
		for (size_t i = 0 ; i < vector::dims ; i++)
			x[i][j] = - xp[i] + typename vector::stype((long int)j - (long int)kernel::np/2 + 1);
	}
incardon's avatar
incardon committed
163

164 165
	for (size_t j = 0 ; j < kernel::np ; j++)
	{
incardon's avatar
incardon committed
166
		for (size_t i = 0 ; i < vector::dims ; i++)
167 168
			a[i][j] = kernel::value(x[i][j],j);
	}
incardon's avatar
incardon committed
169

170 171
	grid_sm<vector::dims,void> gs(sz);
	grid_key_dx_iterator<vector::dims> kit(gs);
incardon's avatar
incardon committed
172

173 174 175
	while (kit.isNext())
	{
		auto key = kit.get();
incardon's avatar
incardon committed
176

177
		a_int.template get<0>(key) = 1;
incardon's avatar
incardon committed
178

179 180
		for (size_t i = 0 ; i < vector::dims ; i++)
			a_int.template get<0>(key) *= a[i][key.get(i)];
incardon's avatar
incardon committed
181

182 183
		++kit;
	}
incardon's avatar
incardon committed
184

185 186 187
	grid_key_dx_iterator<vector::dims> kit2(gs);
	grid_dist_key_dx<vector::dims> k_dist;
	k_dist.setSub(sub);
incardon's avatar
incardon committed
188

189 190 191
	while (kit2.isNext())
	{
		auto key = kit2.get();
incardon's avatar
incardon committed
192

193 194
		for (size_t i = 0 ; i < vector::dims ; i++)
			k_dist.getKeyRef().set_d(i,key.get(i) + base.get(i));
incardon's avatar
incardon committed
195

196 197 198
		inte_template<prp_g,prp_v,m2p_or_p2m>::value(gd,vd,k_dist,key_p,a_int,key);

		++kit2;
incardon's avatar
incardon committed
199
	}
200 201
}

incardon's avatar
incardon committed
202 203 204 205 206 207 208 209 210 211 212 213

template<typename vector,typename grid, typename kernel>
class interpolate
{
	CellList<vector::dims,typename vector::stype,Mem_fast<vector::dims,typename vector::stype>,shift<vector::dims,typename vector::stype>> geo_cell;

	struct Box_vol
	{
		Box<vector::dims,size_t> bv;

		size_t vol;

214
		void operator<(const Box_vol & bv)
incardon's avatar
incardon committed
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
		{
			return vol < bv.vol;
		}
	};

	//! particles
	vector & vd;

	//! grid
	grid & gd;


public:

	interpolate(vector & vd, grid & gd)
	:vd(vd),gd(gd)
	{
		// get the processor bounding box in grid units
		Box<vector::dims,typename vector::stype> bb = gd.getDecomposition().getProcessorBounds();
		Box<vector::dims,typename vector::stype> bunit = gd.getDecomposition().getCellDecomposer().getCellBox();

		size_t div[vector::dims];

		for (size_t i = 0 ; i < vector::dims ; i++)
			div[i] = (bb.getHigh(i) - bb.getLow(i)) / bunit.getHigh(i);

		geo_cell.Initialize(bb,div);

		// Now draw the domain into the cell list

		auto & dec = gd.getDecomposition();

		for (size_t i = 0 ; i < dec.getNSubDomain() ; i++)
		{
			const Box<vector::dims,typename vector::stype> & bx = dec.getSubDomain(i);

			// get the cells this box span
			const grid_key_dx<vector::dims> p1 = geo_cell.getCellGrid(bx.getP1());
			const grid_key_dx<vector::dims> p2 = geo_cell.getCellGrid(bx.getP2());

			// Get the grid and the sub-iterator
			auto & gi = geo_cell.getGrid();
			grid_key_dx_iterator_sub<vector::dims> g_sub(gi,p1,p2);

			// add the box-id to the cell list
			while (g_sub.isNext())
			{
				auto key = g_sub.get();
				geo_cell.addCell(gi.LinId(key),i);
				++g_sub;
			}
		}
	};

	template<unsigned int prp_v, unsigned int prp_g> void p2m(vector & vd, grid & gd)
	{
#ifdef SE_CLASS1

		if (!vd.getDecomposition().is_equal_ng(gd.getDecomposition()) )
		{
			std::cerr << __FILE__ << ":" << __LINE__ << " Error: the distribution of the vector of particles" <<
					" and the grid is different. In order to interpolate the two data structure must have the" <<
					" same decomposition" << std::endl;

			ACTION_ON_ERROR(INTERPOLATION_ERROR_OBJECT)
		}

#endif

		Box<vector::dims,typename vector::stype> domain = vd.getDecomposition().getDomain();

		// grid spacing
		typename vector::stype dx[vector::dims];

		for (size_t i = 0 ; i < vector::dims ; i++)
			dx[i] = 1.0/gd.spacing(i);

		// point position
		typename vector::stype xp[vector::dims];

		int ip[vector::dims][kernel::np];
		typename vector::stype x[vector::dims][kernel::np];
		typename vector::stype a[vector::dims][kernel::np];

299 300 301 302 303 304 305
		size_t sz[vector::dims];

		for (size_t i = 0 ; i < vector::dims ; i++)
			sz[i] = kernel::np;

		grid_cpu<vector::dims,aggregate<typename vector::stype>> a_int(sz);
		a_int.setMemory();
incardon's avatar
incardon committed
306 307 308 309 310

		auto it = vd.getDomainIterator();

		while (it.isNext() == true)
		{
311
			inte_calc<prp_g,prp_v,inte_p2m,kernel>(it,vd,domain,ip,gd,dx,xp,a_int,a,x,sz,geo_cell);
incardon's avatar
incardon committed
312 313 314 315 316

			++it;
		}
	}

incardon's avatar
incardon committed
317
	template<unsigned int prp_g, unsigned int prp_v> void m2p(grid & gd, vector & vd)
incardon's avatar
incardon committed
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
	{
#ifdef SE_CLASS1

		if (!vd.getDecomposition().is_equal_ng(gd.getDecomposition()) )
		{
			std::cerr << __FILE__ << ":" << __LINE__ << " Error: the distribution of the vector of particles" <<
					" and the grid is different. In order to interpolate the two data structure must have the" <<
					" same decomposition" << std::endl;

			ACTION_ON_ERROR(INTERPOLATION_ERROR_OBJECT)
		}

#endif

		Box<vector::dims,typename vector::stype> domain = vd.getDecomposition().getDomain();

		// grid spacing
		typename vector::stype dx[vector::dims];

		for (size_t i = 0 ; i < vector::dims ; i++)
			dx[i] = 1.0/gd.spacing(i);

		// point position
		typename vector::stype xp[vector::dims];

		int ip[vector::dims][kernel::np];
		typename vector::stype x[vector::dims][kernel::np];
		typename vector::stype a[vector::dims][kernel::np];

		size_t sz[vector::dims];

		for (size_t i = 0 ; i < vector::dims ; i++)
			sz[i] = kernel::np;

352 353
		grid_cpu<vector::dims,aggregate<typename vector::stype>> a_int(sz);
		a_int.setMemory();
incardon's avatar
incardon committed
354 355 356 357 358

		auto it = vd.getDomainIterator();

		while (it.isNext() == true)
		{
359
			inte_calc<prp_g,prp_v,inte_m2p,kernel>(it,vd,domain,ip,gd,dx,xp,a_int,a,x,sz,geo_cell);
incardon's avatar
incardon committed
360 361 362 363 364 365 366 367

			++it;
		}
	}

};

#endif /* OPENFPM_NUMERICS_SRC_INTERPOLATION_INTERPOLATION_HPP_ */