FChebInterpolator.hpp 19.4 KB
Newer Older
1 2 3 4 5 6 7 8
#ifndef FCHEBINTERPOLATOR_HPP
#define FCHEBINTERPOLATOR_HPP


#include "./FChebMapping.hpp"
#include "./FChebTensor.hpp"
#include "./FChebRoots.hpp"

9
#include "../../Utils/FBlas.hpp"
10

11 12 13 14 15 16 17 18 19


/**
 * @author Matthias Messner (matthias.matthias@inria.fr)
 * Please read the license
 */

/**
 * @class FChebInterpolator
20
 *
21
 * The class @p FChebInterpolator defines the anterpolation (M2M) and
22
 * interpolation (L2L) concerning operations.
23 24 25 26 27 28 29 30 31 32 33
 */
template <int ORDER>
class FChebInterpolator : FNoCopyable
{
  // compile time constants and types
  enum {nnodes = TensorTraits<ORDER>::nnodes};
  typedef FChebRoots< ORDER>  BasisType;
  typedef FChebTensor<ORDER> TensorType;

  FReal T_of_roots[ORDER][ORDER];
	unsigned int node_ids[nnodes][3];
34 35 36 37 38 39 40
	FReal* ChildParentInterpolator[8];

	/**
	 * Initialize the child - parent - interpolator, it is basically the matrix
	 * S which is precomputed and reused for all M2M and L2L operations, ie for
	 * all non leaf inter/anterpolations.
	 */
41
	void initM2MandL2L()
42
	{
COULAUD Olivier's avatar
COULAUD Olivier committed
43
		FPoint ParentRoots[nnodes], ChildRoots[nnodes];
44
		const FReal ParentWidth(2.);
COULAUD Olivier's avatar
COULAUD Olivier committed
45
		const FPoint ParentCenter(0., 0., 0.);
46 47
		FChebTensor<ORDER>::setRoots(ParentCenter, ParentWidth, ParentRoots);

COULAUD Olivier's avatar
COULAUD Olivier committed
48
		FPoint ChildCenter;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
		const FReal ChildWidth(1.);
		
		// loop: child cells
		for (unsigned int child=0; child<8; ++child) {

			// allocate memory
			ChildParentInterpolator[child] = new FReal [nnodes * nnodes];

			// set child info
			FChebTensor<ORDER>::setRelativeChildCenter(child, ChildCenter);
			FChebTensor<ORDER>::setRoots(ChildCenter, ChildWidth, ChildRoots);

			// assemble child - parent - interpolator
			assembleInterpolator(nnodes, ChildRoots, ChildParentInterpolator[child]);
		}
	}

66 67 68 69


public:
	/**
70
	 * Constructor: Initialize the Chebyshev polynomials at the Chebyshev
71 72 73 74 75 76 77
	 * roots/interpolation point
	 */
	explicit FChebInterpolator()
	{
		// initialize chebyshev polynomials of root nodes: T_o(x_j)
    for (unsigned int o=1; o<ORDER; ++o)
      for (unsigned int j=0; j<ORDER; ++j)
messner's avatar
messner committed
78
        T_of_roots[o][j] = FReal(BasisType::T(o, FReal(BasisType::roots[j])));
79 80 81

		// initialize root node ids
		TensorType::setNodeIds(node_ids);
82 83 84

		// initialize interpolation operator for non M2M and L2L (non leaf
		// operations)
85
		this -> initM2MandL2L();
86 87 88 89 90 91 92 93 94 95
	}

	
	/**
	 * Destructor: Delete dynamically allocated memory for M2M and L2L operator
	 */
	~FChebInterpolator()
	{
		for (unsigned int child=0; child<8; ++child)
			delete [] ChildParentInterpolator[child];
96 97 98
	}


99 100 101 102 103 104 105 106 107 108
	/**
	 * Assembles the interpolator \f$S_\ell\f$ of size \f$N\times
	 * \ell^3\f$. Here local points is meant as points whose global coordinates
	 * have already been mapped to the reference interval [-1,1].
	 *
	 * @param[in] NumberOfLocalPoints
	 * @param[in] LocalPoints
	 * @param[out] Interpolator
	 */
	void assembleInterpolator(const unsigned int NumberOfLocalPoints,
COULAUD Olivier's avatar
COULAUD Olivier committed
109
				  const FPoint *const LocalPoints,
110
				  FReal *const Interpolator) const
COULAUD Olivier's avatar
COULAUD Olivier committed
111
	{
112 113
		// values of chebyshev polynomials of source particle: T_o(x_i)
		FReal T_of_x[ORDER][3];
114 115 116 117 118 119
		FReal c0, c1, c2 ;
		c0 = FReal(0.0) ;
		c1 = FReal(1.) / ORDER;
		c2 = FReal(2.) *c1 ;
		  // loop: local points (mapped in [-1,1])
		  for (unsigned int m=0; m<NumberOfLocalPoints; ++m) {
120 121 122 123 124 125 126 127 128 129 130 131
			// evaluate chebyshev polynomials at local points
			for (unsigned int o=1; o<ORDER; ++o) {
				T_of_x[o][0] = BasisType::T(o, LocalPoints[m].getX());
				T_of_x[o][1] = BasisType::T(o, LocalPoints[m].getY());
				T_of_x[o][2] = BasisType::T(o, LocalPoints[m].getZ());
			}
			
			// assemble interpolator
			for (unsigned int n=0; n<nnodes; ++n) {
				Interpolator[n*nnodes + m] = FReal(1.);
				for (unsigned int d=0; d<3; ++d) {
					const unsigned int j = node_ids[n][d];
132 133 134 135 136
					// FReal S_d = FReal(1.) / ORDER;
					// for (unsigned int o=1; o<ORDER; ++o)
					// 	S_d += FReal(2.) / ORDER * T_of_x[o][d] * T_of_roots[o][j];
					// Interpolator[n*nnodes + m] *= S_d;
					FReal S_d = c0 ;
137
					for (unsigned int o=1; o<ORDER; ++o)
138 139
						S_d +=  T_of_x[o][d] * T_of_roots[o][j];
					S_d = c1 + c2*S_d ; 
140 141 142 143 144 145 146 147
					Interpolator[n*nnodes + m] *= S_d;
				}
			}
			
		}
		
	}

148 149
	
	/**
150 151
	 * Particle to moment: application of \f$S_\ell(y,\bar y_n)\f$
	 * (anterpolation, it is the transposed interpolation)
152 153
	 */
	template <class ContainerClass>
COULAUD Olivier's avatar
COULAUD Olivier committed
154
	void applyP2M(const FPoint& center,
155 156
								const FReal width,
								FReal *const multipoleExpansion,
157
								const ContainerClass *const sourceParticles) const;
158 159 160 161


	
	/**
162
	 * Local to particle operation: application of \f$S_\ell(x,\bar x_m)\f$ (interpolation)
163 164
	 */
	template <class ContainerClass>
COULAUD Olivier's avatar
COULAUD Olivier committed
165
	void applyL2P(const FPoint& center,
166 167
								const FReal width,
								const FReal *const localExpansion,
168
								ContainerClass *const localParticles) const;
169

170 171 172 173 174

	/**
	 * Local to particle operation: application of \f$\nabla_x S_\ell(x,\bar x_m)\f$ (interpolation)
	 */
	template <class ContainerClass>
COULAUD Olivier's avatar
COULAUD Olivier committed
175
	void applyL2PGradient(const FPoint& center,
176 177
												const FReal width,
												const FReal *const localExpansion,
178
												ContainerClass *const localParticles) const;
179

180 181 182 183 184
	/**
	 * Local to particle operation: application of \f$S_\ell(x,\bar x_m)\f$ and
	 * \f$\nabla_x S_\ell(x,\bar x_m)\f$ (interpolation)
	 */
	template <class ContainerClass>
COULAUD Olivier's avatar
COULAUD Olivier committed
185
	void applyL2PTotal(const FPoint& center,
186 187 188 189
										 const FReal width,
										 const FReal *const localExpansion,
										 ContainerClass *const localParticles) const;
	
190 191 192 193 194 195 196
	
	void applyM2M(const unsigned int ChildIndex,
								const FReal *const ChildExpansion,
								FReal *const ParentExpansion) const
	{
		FBlas::gemtva(nnodes, nnodes, FReal(1.),
									ChildParentInterpolator[ChildIndex],
messner's avatar
messner committed
197
									const_cast<FReal*>(ChildExpansion), ParentExpansion);
198
	}
199

200 201 202 203 204 205
	void applyL2L(const unsigned int ChildIndex,
								const FReal *const ParentExpansion,
								FReal *const ChildExpansion) const
	{
		FBlas::gemva(nnodes, nnodes, FReal(1.),
								 ChildParentInterpolator[ChildIndex],
messner's avatar
messner committed
206
								 const_cast<FReal*>(ParentExpansion), ChildExpansion);
207 208 209
	}
	
};
210 211 212



213 214 215 216 217 218 219 220 221 222




/**
 * Particle to moment: application of \f$S_\ell(y,\bar y_n)\f$
 * (anterpolation, it is the transposed interpolation)
 */
template <int ORDER>
template <class ContainerClass>
COULAUD Olivier's avatar
COULAUD Olivier committed
223
inline void FChebInterpolator<ORDER>::applyP2M(const FPoint& center,
224 225 226 227 228 229 230 231 232
																							 const FReal width,
																							 FReal *const multipoleExpansion,
																							 const ContainerClass *const sourceParticles) const
{
	// set all multipole expansions to zero
	FBlas::setzero(nnodes, multipoleExpansion);

	// allocate stuff
	const map_glob_loc map(center, width);
COULAUD Olivier's avatar
COULAUD Olivier committed
233
	FPoint localPosition;
234
	FReal T_of_x[ORDER][3];
235 236 237 238
	FReal S[3],c1;
	//
	FReal xpx,ypy,zpz ;
	c1 = FReal(8.) / nnodes ;
239 240 241 242 243 244 245 246 247 248 249
	// loop over source particles
	typename ContainerClass::ConstBasicIterator iter(*sourceParticles);
	while(iter.hasNotFinished()){

		// map global position to [-1,1]
		map(iter.data().getPosition(), localPosition);

		// evaluate chebyshev polynomials of source particle: T_o(x_i)
		T_of_x[0][0] = FReal(1.);	T_of_x[1][0] = localPosition.getX();
		T_of_x[0][1] = FReal(1.);	T_of_x[1][1] = localPosition.getY();
		T_of_x[0][2] = FReal(1.);	T_of_x[1][2] = localPosition.getZ();
250 251 252 253
		xpx = FReal(2.) * localPosition.getX() ;
		ypy = FReal(2.) * localPosition.getY() ;
		zpz = FReal(2.) * localPosition.getZ() ;

254
		for (unsigned int o=2; o<ORDER; ++o) {
255 256 257
			T_of_x[o][0] = xpx * T_of_x[o-1][0] - T_of_x[o-2][0];
			T_of_x[o][1] = ypy * T_of_x[o-1][1] - T_of_x[o-2][1];
			T_of_x[o][2] = zpz * T_of_x[o-1][2] - T_of_x[o-2][2];
258 259 260 261 262 263
		}
		
		// anterpolate
		const FReal sourceValue = iter.data().getPhysicalValue();
		for (unsigned int n=0; n<nnodes; ++n) {
			const unsigned int j[3] = {node_ids[n][0], node_ids[n][1], node_ids[n][2]};
COULAUD Olivier's avatar
COULAUD Olivier committed
264 265 266
			S[0] = FReal(0.5) + T_of_x[1][0] * T_of_roots[1][j[0]];
			S[1] = FReal(0.5) + T_of_x[1][1] * T_of_roots[1][j[1]];
			S[2] = FReal(0.5) + T_of_x[1][2] * T_of_roots[1][j[2]];
267 268 269 270 271 272
			for (unsigned int o=2; o<ORDER; ++o) {
				S[0] += T_of_x[o][0] * T_of_roots[o][j[0]];
				S[1] += T_of_x[o][1] * T_of_roots[o][j[1]];
				S[2] += T_of_x[o][2] * T_of_roots[o][j[2]];
			}
			// gather contributions
COULAUD Olivier's avatar
COULAUD Olivier committed
273
			//
274
			multipoleExpansion[n]	+= c1 *	S[0] * S[1] * S[2] *	sourceValue;
275 276 277 278 279 280 281 282 283 284 285 286
		}
		// increment source iterator
		iter.gotoNext();
	}
}


/**
 * Local to particle operation: application of \f$S_\ell(x,\bar x_m)\f$ (interpolation)
 */
template <int ORDER>
template <class ContainerClass>
COULAUD Olivier's avatar
COULAUD Olivier committed
287
inline void FChebInterpolator<ORDER>::applyL2P(const FPoint& center,
288 289 290 291 292 293
																							 const FReal width,
																							 const FReal *const localExpansion,
																							 ContainerClass *const localParticles) const
{
	// allocate stuff
	const map_glob_loc map(center, width);
COULAUD Olivier's avatar
COULAUD Olivier committed
294
	FPoint localPosition;
295
	FReal T_of_x[ORDER][3];
296 297 298 299
	FReal xpx,ypy,zpz ;
	FReal S[3],c1;
	//
	c1 = FReal(8.) / nnodes ;
300 301 302 303 304 305 306 307 308 309
	typename ContainerClass::BasicIterator iter(*localParticles);
	while(iter.hasNotFinished()){
			
		// map global position to [-1,1]
		map(iter.data().getPosition(), localPosition);

		// evaluate chebyshev polynomials of source particle: T_o(x_i)
		T_of_x[0][0] = FReal(1.);	T_of_x[1][0] = localPosition.getX();
		T_of_x[0][1] = FReal(1.);	T_of_x[1][1] = localPosition.getY();
		T_of_x[0][2] = FReal(1.);	T_of_x[1][2] = localPosition.getZ();
310 311 312
		xpx = FReal(2.) * localPosition.getX() ;
		ypy = FReal(2.) * localPosition.getY() ;
		zpz = FReal(2.) * localPosition.getZ() ;
313
		for (unsigned int o=2; o<ORDER; ++o) {
314 315 316
			T_of_x[o][0] = xpx * T_of_x[o-1][0] - T_of_x[o-2][0];
			T_of_x[o][1] = ypy * T_of_x[o-1][1] - T_of_x[o-2][1];
			T_of_x[o][2] = zpz * T_of_x[o-1][2] - T_of_x[o-2][2];
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
		}

		// interpolate and increment target value
		FReal targetValue = iter.data().getPotential();
		for (unsigned int n=0; n<nnodes; ++n) {
			const unsigned int j[3] = {node_ids[n][0], node_ids[n][1], node_ids[n][2]};
			S[0] = T_of_x[1][0] * T_of_roots[1][j[0]];
			S[1] = T_of_x[1][1] * T_of_roots[1][j[1]];
			S[2] = T_of_x[1][2] * T_of_roots[1][j[2]];
			for (unsigned int o=2; o<ORDER; ++o) {
				S[0] += T_of_x[o][0] * T_of_roots[o][j[0]];
				S[1] += T_of_x[o][1] * T_of_roots[o][j[1]];
				S[2] += T_of_x[o][2] * T_of_roots[o][j[2]];
			}
			// gather contributions
332 333 334 335 336 337 338 339
			// S[0] *= FReal(2.); S[0] += FReal(1.);
			// S[1] *= FReal(2.); S[1] += FReal(1.);
			// S[2] *= FReal(2.); S[2] += FReal(1.);
			// targetValue	+= S[0] * S[1] * S[2] * localExpansion[n];
			S[0] += FReal(0.5);
			S[1] += FReal(0.5);
			S[2] += FReal(0.5);
			//
340
			targetValue	+= S[0] * S[1] * S[2] * localExpansion[n];
341
		}
342
		// scale
343 344
		//		targetValue /= nnodes;
		targetValue *= c1;
345

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
		// set potential
		iter.data().setPotential(targetValue);
		// increment target iterator
		iter.gotoNext();
	}
}






/**
 * Local to particle operation: application of \f$\nabla_x S_\ell(x,\bar x_m)\f$ (interpolation)
 */
template <int ORDER>
template <class ContainerClass>
COULAUD Olivier's avatar
COULAUD Olivier committed
363
inline void FChebInterpolator<ORDER>::applyL2PGradient(const FPoint& center,
364 365 366 367 368 369
																											 const FReal width,
																											 const FReal *const localExpansion,
																											 ContainerClass *const localParticles) const
{
	// setup local to global mapping
	const map_glob_loc map(center, width);
COULAUD Olivier's avatar
COULAUD Olivier committed
370
	FPoint Jacobian;
371 372
	map.computeJacobian(Jacobian);
	const FReal jacobian[3] = {Jacobian.getX(), Jacobian.getY(), Jacobian.getZ()}; 
COULAUD Olivier's avatar
COULAUD Olivier committed
373
	FPoint localPosition;
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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	FReal T_of_x[ORDER][3];
	FReal U_of_x[ORDER][3];
	FReal P[3];

	typename ContainerClass::BasicIterator iter(*localParticles);
	while(iter.hasNotFinished()){
			
		// map global position to [-1,1]
		map(iter.data().getPosition(), localPosition);
			
		// evaluate chebyshev polynomials of source particle
		// T_0(x_i) and T_1(x_i)
		T_of_x[0][0] = FReal(1.);	T_of_x[1][0] = localPosition.getX();
		T_of_x[0][1] = FReal(1.);	T_of_x[1][1] = localPosition.getY();
		T_of_x[0][2] = FReal(1.);	T_of_x[1][2] = localPosition.getZ();
		// U_0(x_i) and U_1(x_i)
		U_of_x[0][0] = FReal(1.);	U_of_x[1][0] = localPosition.getX() * FReal(2.);
		U_of_x[0][1] = FReal(1.);	U_of_x[1][1] = localPosition.getY() * FReal(2.);
		U_of_x[0][2] = FReal(1.);	U_of_x[1][2] = localPosition.getZ() * FReal(2.);
		for (unsigned int o=2; o<ORDER; ++o) {
			// T_o(x_i)
			T_of_x[o][0] = FReal(2.)*localPosition.getX()*T_of_x[o-1][0] - T_of_x[o-2][0];
			T_of_x[o][1] = FReal(2.)*localPosition.getY()*T_of_x[o-1][1] - T_of_x[o-2][1];
			T_of_x[o][2] = FReal(2.)*localPosition.getZ()*T_of_x[o-1][2] - T_of_x[o-2][2];
			// U_o(x_i)
			U_of_x[o][0] = FReal(2.)*localPosition.getX()*U_of_x[o-1][0] - U_of_x[o-2][0];
			U_of_x[o][1] = FReal(2.)*localPosition.getY()*U_of_x[o-1][1] - U_of_x[o-2][1];
			U_of_x[o][2] = FReal(2.)*localPosition.getZ()*U_of_x[o-1][2] - U_of_x[o-2][2];
		}

		// scale, because dT_o/dx = oU_{o-1}
		for (unsigned int o=2; o<ORDER; ++o) {
			U_of_x[o-1][0] *= FReal(o);
			U_of_x[o-1][1] *= FReal(o);
			U_of_x[o-1][2] *= FReal(o);
		}

		// apply P and increment forces
		FReal forces[3] = {FReal(0.), FReal(0.), FReal(0.)};
		for (unsigned int n=0; n<nnodes; ++n) {
			
			// tensor indices of chebyshev nodes
			const unsigned int j[3] = {node_ids[n][0], node_ids[n][1], node_ids[n][2]};

			// f0 component //////////////////////////////////////
			P[0] = U_of_x[0][0] * T_of_roots[1][j[0]];
			P[1] = T_of_x[1][1] * T_of_roots[1][j[1]];
			P[2] = T_of_x[1][2] * T_of_roots[1][j[2]];
			for (unsigned int o=2; o<ORDER; ++o) {
				P[0] += U_of_x[o-1][0] * T_of_roots[o][j[0]];
				P[1] += T_of_x[o  ][1] * T_of_roots[o][j[1]];
				P[2] += T_of_x[o  ][2] * T_of_roots[o][j[2]];
			}
			P[0] *= FReal(2.);
			P[1] *= FReal(2.); P[1] += FReal(1.);
			P[2] *= FReal(2.); P[2] += FReal(1.);
430
			forces[0]	+= P[0] * P[1] * P[2] * localExpansion[n];
431 432 433 434 435 436 437 438 439 440 441 442 443

			// f1 component //////////////////////////////////////
			P[0] = T_of_x[1][0] * T_of_roots[1][j[0]];
			P[1] = U_of_x[0][1] * T_of_roots[1][j[1]];
			P[2] = T_of_x[1][2] * T_of_roots[1][j[2]];
			for (unsigned int o=2; o<ORDER; ++o) {
				P[0] += T_of_x[o  ][0] * T_of_roots[o][j[0]];
				P[1] += U_of_x[o-1][1] * T_of_roots[o][j[1]];
				P[2] += T_of_x[o  ][2] * T_of_roots[o][j[2]];
			}
			P[0] *= FReal(2.); P[0] += FReal(1.);
			P[1] *= FReal(2.); 
			P[2] *= FReal(2.); P[2] += FReal(1.);
444
			forces[1]	+= P[0] * P[1] * P[2] * localExpansion[n];
445 446 447 448 449 450 451 452 453 454 455 456 457

			// f2 component //////////////////////////////////////
			P[0] = T_of_x[1][0] * T_of_roots[1][j[0]];
			P[1] = T_of_x[1][1] * T_of_roots[1][j[1]];
			P[2] = U_of_x[0][2] * T_of_roots[1][j[2]];
			for (unsigned int o=2; o<ORDER; ++o) {
				P[0] += T_of_x[o  ][0] * T_of_roots[o][j[0]];
				P[1] += T_of_x[o  ][1] * T_of_roots[o][j[1]];
				P[2] += U_of_x[o-1][2] * T_of_roots[o][j[2]];
			}
			P[0] *= FReal(2.); P[0] += FReal(1.);
			P[1] *= FReal(2.); P[1] += FReal(1.);
			P[2] *= FReal(2.);
458
			forces[2]	+= P[0] * P[1] * P[2] * localExpansion[n];
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
		}

		// scale forces
		forces[0] *= jacobian[0] / nnodes;
		forces[1] *= jacobian[1] / nnodes;
		forces[2] *= jacobian[2] / nnodes;

		// set computed forces
		iter.data().incForces(forces[0] * iter.data().getPhysicalValue(),
													forces[1] * iter.data().getPhysicalValue(),
													forces[2] * iter.data().getPhysicalValue());

		// increment iterator
		iter.gotoNext();
	}
}


477 478 479 480 481 482 483

/**
 * Local to particle operation: application of \f$S_\ell(x,\bar x_m)\f$ and
 * \f$\nabla_x S_\ell(x,\bar x_m)\f$ (interpolation)
 */
template <int ORDER>
template <class ContainerClass>
COULAUD Olivier's avatar
COULAUD Olivier committed
484
inline void FChebInterpolator<ORDER>::applyL2PTotal(const FPoint& center,
485 486 487 488 489 490
																										const FReal width,
																										const FReal *const localExpansion,
																										ContainerClass *const localParticles) const
{
	// setup local to global mapping
	const map_glob_loc map(center, width);
COULAUD Olivier's avatar
COULAUD Olivier committed
491
	FPoint Jacobian;
492 493
	map.computeJacobian(Jacobian);
	const FReal jacobian[3] = {Jacobian.getX(), Jacobian.getY(), Jacobian.getZ()}; 
COULAUD Olivier's avatar
COULAUD Olivier committed
494
	FPoint localPosition;
495 496
	FReal T_of_x[ORDER][3];
	FReal U_of_x[ORDER][3];
497
	FReal P[6];
498 499 500 501
	//
	FReal xpx,ypy,zpz ;
	FReal c1 = FReal(8.0) / nnodes ; 
	//
502 503 504 505 506 507 508 509
	typename ContainerClass::BasicIterator iter(*localParticles);
	while(iter.hasNotFinished()){
			
		// map global position to [-1,1]
		map(iter.data().getPosition(), localPosition);
			
		// evaluate chebyshev polynomials of source particle
		// T_0(x_i) and T_1(x_i)
510 511 512 513
		xpx = FReal(2.) * localPosition.getX() ;
		ypy = FReal(2.) * localPosition.getY() ;
		zpz = FReal(2.) * localPosition.getZ() ;
		//
514 515 516 517
		T_of_x[0][0] = FReal(1.);	T_of_x[1][0] = localPosition.getX();
		T_of_x[0][1] = FReal(1.);	T_of_x[1][1] = localPosition.getY();
		T_of_x[0][2] = FReal(1.);	T_of_x[1][2] = localPosition.getZ();
		// U_0(x_i) and U_1(x_i)
518 519 520 521 522 523
		// U_of_x[0][0] = FReal(1.);	U_of_x[1][0] = localPosition.getX() * FReal(2.);
		// U_of_x[0][1] = FReal(1.);	U_of_x[1][1] = localPosition.getY() * FReal(2.);
		// U_of_x[0][2] = FReal(1.);	U_of_x[1][2] = localPosition.getZ() * FReal(2.);
		U_of_x[0][0] = FReal(1.);	U_of_x[1][0] = xpx;
		U_of_x[0][1] = FReal(1.);	U_of_x[1][1] = ypy;
		U_of_x[0][2] = FReal(1.);	U_of_x[1][2] = zpz;
524 525
		for (unsigned int o=2; o<ORDER; ++o) {
			// T_o(x_i)
526 527 528 529 530 531 532 533 534 535
			// T_of_x[o][0] = FReal(2.)*localPosition.getX()*T_of_x[o-1][0] - T_of_x[o-2][0];
			// T_of_x[o][1] = FReal(2.)*localPosition.getY()*T_of_x[o-1][1] - T_of_x[o-2][1];
			// T_of_x[o][2] = FReal(2.)*localPosition.getZ()*T_of_x[o-1][2] - T_of_x[o-2][2];
			// // U_o(x_i)
			// U_of_x[o][0] = FReal(2.)*localPosition.getX()*U_of_x[o-1][0] - U_of_x[o-2][0];
			// U_of_x[o][1] = FReal(2.)*localPosition.getY()*U_of_x[o-1][1] - U_of_x[o-2][1];
			// U_of_x[o][2] = FReal(2.)*localPosition.getZ()*U_of_x[o-1][2] - U_of_x[o-2][2];
			T_of_x[o][0] = xpx*T_of_x[o-1][0] - T_of_x[o-2][0];
			T_of_x[o][1] = ypy*T_of_x[o-1][1] - T_of_x[o-2][1];
			T_of_x[o][2] = zpz*T_of_x[o-1][2] - T_of_x[o-2][2];
536
			// U_o(x_i)
537 538 539
			U_of_x[o][0] = xpx*U_of_x[o-1][0] - U_of_x[o-2][0];
			U_of_x[o][1] = ypy*U_of_x[o-1][1] - U_of_x[o-2][1];
			U_of_x[o][2] = zpz*U_of_x[o-1][2] - U_of_x[o-2][2];
540 541 542 543 544 545 546 547 548 549 550 551
		}

		// scale, because dT_o/dx = oU_{o-1}
		for (unsigned int o=2; o<ORDER; ++o) {
			U_of_x[o-1][0] *= FReal(o);
			U_of_x[o-1][1] *= FReal(o);
			U_of_x[o-1][2] *= FReal(o);
		}

		// apply P and increment forces
		FReal potential = FReal(0.);
		FReal forces[3] = {FReal(0.), FReal(0.), FReal(0.)};
552 553 554
		//
		// Optimization:
		//   Here we compute 1/2 S and 1/2 P  rather S and F like in the paper
555
		for (unsigned int n=0; n<nnodes; ++n) {
556 557 558
		  
		  // tensor indices of chebyshev nodes
		  const unsigned int j[3] = {node_ids[n][0], node_ids[n][1], node_ids[n][2]};
COULAUD Olivier's avatar
COULAUD Olivier committed
559 560 561 562
		  //
		  P[0] =  FReal(0.5) + T_of_x[1][0] * T_of_roots[1][j[0]];
		  P[1] =  FReal(0.5) + T_of_x[1][1] * T_of_roots[1][j[1]];
		  P[2] =  FReal(0.5) + T_of_x[1][2] * T_of_roots[1][j[2]];
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
		  P[3] = U_of_x[0][0] * T_of_roots[1][j[0]];
		  P[4] = U_of_x[0][1] * T_of_roots[1][j[1]];
		  P[5] = U_of_x[0][2] * T_of_roots[1][j[2]];
		  for (unsigned int o=2; o<ORDER; ++o) {
		    P[0] += T_of_x[o  ][0] * T_of_roots[o][j[0]];
		    P[1] += T_of_x[o  ][1] * T_of_roots[o][j[1]];
		    P[2] += T_of_x[o  ][2] * T_of_roots[o][j[2]];
		    P[3] += U_of_x[o-1][0] * T_of_roots[o][j[0]];
		    P[4] += U_of_x[o-1][1] * T_of_roots[o][j[1]];
		    P[5] += U_of_x[o-1][2] * T_of_roots[o][j[2]];
		  }
		  //
		  potential	+= P[0] * P[1] * P[2] * localExpansion[n];
		  forces[0]	+= P[3] * P[1] * P[2] * localExpansion[n];
		  forces[1]	+= P[0] * P[4] * P[2] * localExpansion[n];
		  forces[2]	+= P[0] * P[1] * P[5] * localExpansion[n];
579
		}
580 581
		//
		potential *= c1 ;
582 583 584
		forces[0] *= jacobian[0] *c1;
		forces[1] *= jacobian[1] *c1;
		forces[2] *= jacobian[2] *c1;
585 586 587 588 589
		// set computed potential
		iter.data().incPotential(potential);

		// set computed forces
		iter.data().incForces(forces[0] * iter.data().getPhysicalValue(),
590 591
				      forces[1] * iter.data().getPhysicalValue(),
				      forces[2] * iter.data().getPhysicalValue());
592 593 594 595 596 597 598

		// increment iterator
		iter.gotoNext();
	}
}


599
#endif