ArbitraryDomain.cpp 27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// ------------------------------------------------------------------------
// $Version: 1.2.1 $
// ------------------------------------------------------------------------
// Copyright & License: See Copyright.readme in src directory
// ------------------------------------------------------------------------
// Class ArbitraryDomain
//   Interface to iterative solver and boundary geometry 
//   for space charge calculation
//
// ------------------------------------------------------------------------
// $Author: kaman $
// $Date: 2014 $
// ------------------------------------------------------------------------

15
#ifdef HAVE_SAAMG_SOLVER
16 17 18 19
#include <map>
#include <cmath>
#include <iostream>
#include <assert.h>
gsell's avatar
gsell committed
20 21

#include "ArbitraryDomain.h"
22

23 24 25 26 27 28 29 30 31
ArbitraryDomain::ArbitraryDomain(
	BoundaryGeometry * bgeom, 
	Vector_t nr, 
	Vector_t hr, 
	std::string interpl) {

    	bgeom_m  = bgeom;
    	Geo_mincoords_m = bgeom->getmincoords();
    	Geo_maxcoords_m = bgeom->getmaxcoords();
32

33 34
    	setNr(nr);
    	setHr(hr);
35

36 37 38 39 40 41 42 43
   	startId = 0;

	if(interpl == "CONSTANT")
        	interpolationMethod = CONSTANT;
	else if(interpl == "LINEAR")
        	interpolationMethod = LINEAR;
	else if(interpl == "QUADRATIC")
        	interpolationMethod = QUADRATIC;
gsell's avatar
gsell committed
44 45
}

46 47
ArbitraryDomain::~ArbitraryDomain() {
    //nothing so far
gsell's avatar
gsell committed
48 49
}

50 51 52 53 54 55
void ArbitraryDomain::Compute(Vector_t hr) {

    setHr(hr);
}


56
void ArbitraryDomain::Compute(Vector_t hr, NDIndex<3> localId) {
gsell's avatar
gsell committed
57 58 59

    setHr(hr);

60 61 62 63 64 65 66
    int zGhostOffsetLeft  = (localId[2].first()== 0) ? 0 : 1;
    int zGhostOffsetRight = (localId[2].last() == nr[2] - 1) ? 0 : 1;
    int yGhostOffsetLeft  = (localId[1].first()== 0) ? 0 : 1;
    int yGhostOffsetRight = (localId[1].last() == nr[1] - 1) ? 0 : 1;
    int xGhostOffsetLeft  = (localId[0].first()== 0) ? 0 : 1;
    int xGhostOffsetRight = (localId[0].last() == nr[0] - 1) ? 0 : 1;
    
67 68
    hasGeometryChanged_m = true;

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
    IntersectLoX.clear();
    IntersectHiX.clear();
    IntersectLoY.clear();
    IntersectHiY.clear();
    IntersectLoZ.clear();
    IntersectHiZ.clear();


    //calculate intersection 
    Vector_t P, dir, I;
    for (int idz = localId[2].first()-zGhostOffsetLeft; idz <= localId[2].last()+zGhostOffsetRight; idz++) {
	 P[2] = (idz - (nr[2]-1)/2.0)*hr[2];

	 for (int idy = localId[1].first()-yGhostOffsetLeft; idy <= localId[1].last()+yGhostOffsetRight; idy++) {
	     P[1] = (idy - (nr[1]-1)/2.0)*hr[1];

    	     for (int idx = localId[0].first()-xGhostOffsetLeft; idx <= localId[0].last()+xGhostOffsetRight; idx++) {
	       	  P[0] = (idx - (nr[0]-1)/2.0)*hr[0];

       			std::tuple<int, int, int> pos(idx, idy, idz);

	        	dir = Vector_t(0,0,1);
		        if (bgeom_m->intersectRayBoundary(P, dir, I))
       	      		 IntersectHiZ.insert(std::pair< std::tuple<int, int, int>, double >(pos, I[2]));

	        	dir = Vector_t(0,0,-1);
		        if (bgeom_m->intersectRayBoundary(P, dir, I))
       	      		 IntersectLoZ.insert(std::pair< std::tuple<int, int, int>, double >(pos, I[2]));

	        	dir = Vector_t(0,1,0);
		        if (bgeom_m->intersectRayBoundary(P, dir, I))
       	      		 IntersectHiY.insert(std::pair< std::tuple<int, int, int>, double >(pos, I[1]));

	        	dir = Vector_t(0,-1,0);
		        if (bgeom_m->intersectRayBoundary(P, dir, I))
       	      		 IntersectLoY.insert(std::pair< std::tuple<int, int, int>, double >(pos, I[1]));

	        	dir = Vector_t(1,0,0);
		        if (bgeom_m->intersectRayBoundary(P, dir, I))
       	      		 IntersectHiX.insert(std::pair< std::tuple<int, int, int>, double >(pos, I[0]));

	        	dir = Vector_t(-1,0,0);
		        if (bgeom_m->intersectRayBoundary(P, dir, I))
       	      		 IntersectLoX.insert(std::pair< std::tuple<int, int, int>, double >(pos, I[0]));
		}
	 }
     }

    //number of ghost nodes to the right 
    int znumGhostNodesRight = 0;
    if(zGhostOffsetRight != 0) {
        for(int idx = localId[0].first(); idx <= localId[0].last(); idx++) {
            for(int idy = localId[1].first(); idy <= localId[1].last(); idy++) {
                if(isInside(idx, idy, localId[2].last() + zGhostOffsetRight))
                    znumGhostNodesRight++;
gsell's avatar
gsell committed
124 125
            }
        }
126
    }
gsell's avatar
gsell committed
127

128 129 130 131 132 133 134
    //number of ghost nodes to the left 
    int znumGhostNodesLeft = 0;
    if(zGhostOffsetLeft != 0) {
        for(int idx = localId[0].first(); idx <= localId[0].last(); idx++) {
            for(int idy = localId[1].first(); idy <= localId[1].last(); idy++) {
                if(isInside(idx, idy, localId[2].first() - zGhostOffsetLeft))
                    znumGhostNodesLeft++;
gsell's avatar
gsell committed
135 136
            }
        }
137
    }
gsell's avatar
gsell committed
138

139 140 141 142 143 144 145
    //number of ghost nodes to the right 
    int ynumGhostNodesRight = 0;
    if(yGhostOffsetRight != 0) {
        for(int idx = localId[0].first(); idx <= localId[0].last(); idx++) {
            for(int idz = localId[2].first(); idz <= localId[2].last(); idz++) {
                if(isInside(idx, localId[1].last() + yGhostOffsetRight, idz))
                    ynumGhostNodesRight++;
gsell's avatar
gsell committed
146 147 148 149
            }
        }
    }

150 151 152 153 154 155 156 157 158 159
    //number of ghost nodes to the left 
    int ynumGhostNodesLeft = 0;
    if(yGhostOffsetLeft != 0) {
        for(int idx = localId[0].first(); idx <= localId[0].last(); idx++) {
            for(int idz = localId[2].first(); idz <= localId[2].last(); idz++) {
                if(isInside(idx, localId[1].first() - yGhostOffsetLeft, idz))
                    ynumGhostNodesLeft++;
            }
        }
    }
gsell's avatar
gsell committed
160 161


162 163 164 165 166 167 168
    //number of ghost nodes to the right 
    int xnumGhostNodesRight = 0;
    if(xGhostOffsetRight != 0) {
	for (int idy = localId[1].first(); idy <= localId[1].last(); idy++) {
            for (int idz = localId[2].first(); idz <= localId[2].last(); idz++) {
                if(isInside(localId[0].last() + xGhostOffsetRight, idy, idz))
                    xnumGhostNodesRight++;
gsell's avatar
gsell committed
169 170 171 172
            }
        }
    }

173 174 175 176 177 178 179 180 181 182
    //number of ghost nodes to the left 
    int xnumGhostNodesLeft = 0;
    if(xGhostOffsetLeft != 0) {
       	for (int idy = localId[1].first(); idy <= localId[1].last(); idy++) {
            for(int idz = localId[2].first(); idz <= localId[2].last(); idz++) {
                if(isInside(localId[0].first() - xGhostOffsetLeft, idy, idz))
                    xnumGhostNodesLeft++;
            }
        }
    }
gsell's avatar
gsell committed
183
    //xy points in z plane
184 185
    int numxy; 
    int numtotalxy = 0;
gsell's avatar
gsell committed
186

187
    numXY.clear();
gsell's avatar
gsell committed
188

189 190 191 192 193 194
    for (int idz = localId[2].first(); idz <= localId[2].last(); idz++) {
	numxy =0;
        for (int idy = localId[1].first(); idy <= localId[1].last(); idy++) {
            for (int idx =localId[0].first(); idx <= localId[0].last(); idx++) {
                if (isInside(idx, idy, idz))
                   numxy++;
gsell's avatar
gsell committed
195 196
            }
        }
197
        numtotalxy += numxy;
gsell's avatar
gsell committed
198 199
    }

200 201
    startId = 0;
    MPI_Scan(&numtotalxy, &startId, 1, MPI_INTEGER, MPI_SUM, Ippl::getComm());
gsell's avatar
gsell committed
202

203
    startId -= numtotalxy;
gsell's avatar
gsell committed
204 205 206 207

    //build up index and coord map
    IdxMap.clear();
    CoordMap.clear();
208 209 210 211 212 213 214 215 216 217 218 219 220
    
    register int id = startId - xnumGhostNodesLeft - ynumGhostNodesLeft - znumGhostNodesLeft;
     for (int idz = localId[2].first()-zGhostOffsetLeft; idz <= localId[2].last()+zGhostOffsetRight; idz++) {
    	 for (int idy = localId[1].first()-yGhostOffsetLeft; idy <= localId[1].last()+yGhostOffsetRight; idy++) {
    	     for (int idx = localId[0].first()-xGhostOffsetLeft; idx <= localId[0].last()+xGhostOffsetRight; idx++) {
	            if (isInside(idx, idy, idz)) {
                    IdxMap[toCoordIdx(idx, idy, idz)] = id;
                    CoordMap[id] = toCoordIdx(idx, idy, idz);
                    id++;
                 }
             }
         }
     }
gsell's avatar
gsell committed
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 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 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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
void ArbitraryDomain::Compute(Vector_t hr, NDIndex<3> localId, Vector_t globalMeanR, Vektor<double, 4> globalToLocalQuaternion){

    setHr(hr);

    globalMeanR_m = globalMeanR;
    globalToLocalQuaternion_m = globalToLocalQuaternion;	
    localToGlobalQuaternion_m[0] = globalToLocalQuaternion[0];
    for (int i=1; i<4; i++)
		localToGlobalQuaternion_m[i] = -globalToLocalQuaternion[i];	

    int zGhostOffsetLeft  = (localId[2].first()== 0) ? 0 : 1;
    int zGhostOffsetRight = (localId[2].last() == nr[2] - 1) ? 0 : 1;
    int yGhostOffsetLeft  = (localId[1].first()== 0) ? 0 : 1;
    int yGhostOffsetRight = (localId[1].last() == nr[1] - 1) ? 0 : 1;
    int xGhostOffsetLeft  = (localId[0].first()== 0) ? 0 : 1;
    int xGhostOffsetRight = (localId[0].last() == nr[0] - 1) ? 0 : 1;
    
    hasGeometryChanged_m = true;

    IntersectLoX.clear();
    IntersectHiX.clear();
    IntersectLoY.clear();
    IntersectHiY.clear();
    IntersectLoZ.clear();
    IntersectHiZ.clear();

    //calculate intersection 
    Vector_t P, saveP, dir, I;

    for (int idz = localId[2].first()-zGhostOffsetLeft; idz <= localId[2].last()+zGhostOffsetRight; idz++) {
	 saveP[2] = (idz - (nr[2]-1)/2.0)*hr[2];

	 for (int idy = localId[1].first()-yGhostOffsetLeft; idy <= localId[1].last()+yGhostOffsetRight; idy++) {
	     saveP[1] = (idy - (nr[1]-1)/2.0)*hr[1];

    	     for (int idx = localId[0].first()-xGhostOffsetLeft; idx <= localId[0].last()+xGhostOffsetRight; idx++) {
	       	  saveP[0] = (idx - (nr[0]-1)/2.0)*hr[0];

			P = saveP;
			rotateWithQuaternion(P, localToGlobalQuaternion_m); 
			P += globalMeanR;

       			std::tuple<int, int, int> pos(idx, idy, idz);

			rotateZAxisWithQuaternion(dir, localToGlobalQuaternion_m);
		        if (bgeom_m->intersectRayBoundary(P, dir, I))
			{
			   *gmsg << "zdir= +1G: dir:" << dir << " x,y,z= " << idx << "," << idy << "," << idz << " P=" << P << " I=" << I << endl;
			   I -= globalMeanR;
			   rotateWithQuaternion(I, globalToLocalQuaternion_m); 
			   *gmsg << "zdir= +1L: dir:" << dir << " x,y,z= " << idx << "," << idy << "," << idz << " I=" << I << endl;
       	      		   IntersectHiZ.insert(std::pair< std::tuple<int, int, int>, double >(pos, I[2]));
			}
			else
			{
			   *gmsg << "zdir= " << dir << " x,y,z= " << idx << "," << idy << "," << idz << " P=" << P <<" I=" << I << endl;
			}

		        if (bgeom_m->intersectRayBoundary(P, -dir, I))
			{
			   *gmsg << "zdir= -1G: dir:" << -dir << " x,y,z= " << idx << "," << idy << "," << idz << " P=" << P << " I=" << I << endl;
			   I -= globalMeanR;
			   rotateWithQuaternion(I, globalToLocalQuaternion_m); 
			   *gmsg << "zdir= -1L: dir:" << -dir << " x,y,z= " << idx << "," << idy << "," << idz << " I=" << I << endl;
       	      		   IntersectLoZ.insert(std::pair< std::tuple<int, int, int>, double >(pos, I[2]));
			}
			else
			{
			   *gmsg << "zdir= " << -dir << " x,y,z= " << idx << "," << idy << "," << idz << " P=" << P <<" I=" << I << endl;
			}

	        	rotateYAxisWithQuaternion(dir, localToGlobalQuaternion_m);
		        if (bgeom_m->intersectRayBoundary(P, dir, I))
			{
			   *gmsg << "ydir= +1G: dir:" << dir << " x,y,z= " << idx << "," << idy << "," << idz << " P=" << P << " I=" << I << endl;
			   I -= globalMeanR;
			   rotateWithQuaternion(I, globalToLocalQuaternion_m); 
       	      		   IntersectHiY.insert(std::pair< std::tuple<int, int, int>, double >(pos, I[1]));
			   *gmsg << "ydir= +1L: dir:" << dir << " x,y,z= " << idx << "," << idy << "," << idz << " I=" << I << endl;
			}
			else
			{
			   *gmsg << "ydir= " << dir << " x,y,z= " << idx << "," << idy << "," << idz << " P=" << P <<" I=" << I << endl;
			}


		        if (bgeom_m->intersectRayBoundary(P, -dir, I))
			{
			   *gmsg << "ydir= -1G: dir:" << -dir << " x,y,z= " << idx << "," << idy << "," << idz << " P=" << P << " I=" << I << endl;
			   I -= globalMeanR;
			   rotateWithQuaternion(I, globalToLocalQuaternion_m); 
       	      		   IntersectLoY.insert(std::pair< std::tuple<int, int, int>, double >(pos, I[1]));
			   *gmsg << "ydir= -1L: dir:" << -dir << " x,y,z= " << idx << "," << idy << "," << idz << " I=" << I << endl;
			}
			else{
			   *gmsg << "ydir= " << -dir << " x,y,z= " << idx << "," << idy << "," << idz << " P=" << P <<" I=" << I << endl;
			}


	        	rotateXAxisWithQuaternion(dir, localToGlobalQuaternion_m);
		        if (bgeom_m->intersectRayBoundary(P, dir, I))
			{
			   *gmsg << "xdir= +1G: dir:" << dir << " x,y,z= " << idx << "," << idy << "," << idz << " P=" << P << " I=" << I << endl;
			   I -= globalMeanR;
			   rotateWithQuaternion(I, globalToLocalQuaternion_m); 
			   *gmsg << "xdir= +1L: dir:" << dir << " x,y,z= " << idx << "," << idy << "," << idz << " I=" << I << endl;
       	      		   IntersectHiX.insert(std::pair< std::tuple<int, int, int>, double >(pos, I[0]));
			}
			else{
			   *gmsg << "xdir= " << dir << " x,y,z= " << idx << "," << idy << "," << idz << " P=" << P <<" I=" << I << endl;
			}


		        if (bgeom_m->intersectRayBoundary(P, -dir, I))
			{
			   *gmsg << "xdir= -1G: dir:" << -dir << " x,y,z= " << idx << "," << idy << "," << idz << " P=" << P << " I=" << I << endl;
			   I -= globalMeanR;
			   rotateWithQuaternion(I, globalToLocalQuaternion_m); 
			   *gmsg << "xdir= -1L: dir:" << -dir << " x,y,z= " << idx << "," << idy << "," << idz << " I=" << I << endl;
       	      		   IntersectLoX.insert(std::pair< std::tuple<int, int, int>, double >(pos, I[0]));
			}
			else{
			   *gmsg << "xdir= " << -dir << " x,y,z= " << idx << "," << idy << "," << idz << " P=" << P <<" I=" << I << endl;
			}

		}
	 }
     }

    //number of ghost nodes to the right 
    int znumGhostNodesRight = 0;
    if(zGhostOffsetRight != 0) {
        for(int idx = localId[0].first(); idx <= localId[0].last(); idx++) {
            for(int idy = localId[1].first(); idy <= localId[1].last(); idy++) {
                if(isInside(idx, idy, localId[2].last() + zGhostOffsetRight))
                    znumGhostNodesRight++;
            }
        }
    }

    //number of ghost nodes to the left 
    int znumGhostNodesLeft = 0;
    if(zGhostOffsetLeft != 0) {
        for(int idx = localId[0].first(); idx <= localId[0].last(); idx++) {
            for(int idy = localId[1].first(); idy <= localId[1].last(); idy++) {
                if(isInside(idx, idy, localId[2].first() - zGhostOffsetLeft))
                    znumGhostNodesLeft++;
            }
        }
    }

    //number of ghost nodes to the right 
    int ynumGhostNodesRight = 0;
    if(yGhostOffsetRight != 0) {
        for(int idx = localId[0].first(); idx <= localId[0].last(); idx++) {
            for(int idz = localId[2].first(); idz <= localId[2].last(); idz++) {
                if(isInside(idx, localId[1].last() + yGhostOffsetRight, idz))
                    ynumGhostNodesRight++;
            }
        }
    }

    //number of ghost nodes to the left 
    int ynumGhostNodesLeft = 0;
    if(yGhostOffsetLeft != 0) {
        for(int idx = localId[0].first(); idx <= localId[0].last(); idx++) {
            for(int idz = localId[2].first(); idz <= localId[2].last(); idz++) {
                if(isInside(idx, localId[1].first() - yGhostOffsetLeft, idz))
                    ynumGhostNodesLeft++;
            }
        }
    }


    //number of ghost nodes to the right 
    int xnumGhostNodesRight = 0;
    if(xGhostOffsetRight != 0) {
	for (int idy = localId[1].first(); idy <= localId[1].last(); idy++) {
            for (int idz = localId[2].first(); idz <= localId[2].last(); idz++) {
                if(isInside(localId[0].last() + xGhostOffsetRight, idy, idz))
                    xnumGhostNodesRight++;
            }
        }
    }

    //number of ghost nodes to the left 
    int xnumGhostNodesLeft = 0;
    if(xGhostOffsetLeft != 0) {
       	for (int idy = localId[1].first(); idy <= localId[1].last(); idy++) {
            for(int idz = localId[2].first(); idz <= localId[2].last(); idz++) {
                if(isInside(localId[0].first() - xGhostOffsetLeft, idy, idz))
                    xnumGhostNodesLeft++;
            }
        }
    }
    //xy points in z plane
    int numxy; 
    int numtotalxy = 0;

    numXY.clear();

    for (int idz = localId[2].first(); idz <= localId[2].last(); idz++) {
	numxy =0;
        for (int idy = localId[1].first(); idy <= localId[1].last(); idy++) {
            for (int idx =localId[0].first(); idx <= localId[0].last(); idx++) {
                if (isInside(idx, idy, idz))
                   numxy++;
            }
        }
        numtotalxy += numxy;
    }

    startId = 0;
    MPI_Scan(&numtotalxy, &startId, 1, MPI_INTEGER, MPI_SUM, Ippl::getComm());

    startId -= numtotalxy;

    //build up index and coord map
    IdxMap.clear();
    CoordMap.clear();
    
    register int id = startId - xnumGhostNodesLeft - ynumGhostNodesLeft - znumGhostNodesLeft;
     for (int idz = localId[2].first()-zGhostOffsetLeft; idz <= localId[2].last()+zGhostOffsetRight; idz++) {
    	 for (int idy = localId[1].first()-yGhostOffsetLeft; idy <= localId[1].last()+yGhostOffsetRight; idy++) {
    	     for (int idx = localId[0].first()-xGhostOffsetLeft; idx <= localId[0].last()+xGhostOffsetRight; idx++) {
	            if (isInside(idx, idy, idz)) {
                    IdxMap[toCoordIdx(idx, idy, idz)] = id;
                    CoordMap[id] = toCoordIdx(idx, idy, idz);
                    id++;
                 }
             }
         }
     }
}
457 458 459
// Conversion from (x,y,z) to index in xyz plane
inline int ArbitraryDomain::toCoordIdx(int idx, int idy, int idz) {
	return (idz * nr[1] + idy) * nr[0]  + idx;
gsell's avatar
gsell committed
460 461
}

462 463 464 465 466 467 468
// Conversion from (x,y,z) to index on the 3D grid
int ArbitraryDomain::getIdx(int idx, int idy, int idz) {
	if(isInside(idx, idy, idz) && idx>=0 && idy >=0 && idz >=0 )
       	       	 	return IdxMap[toCoordIdx(idx, idy, idz)];
    	else
        	return -1;
} 
gsell's avatar
gsell committed
469

470 471
// Conversion from a 3D index to (x,y,z)
inline void ArbitraryDomain::getCoord(int idxyz, int &idx, int &idy, int &idz) {
gsell's avatar
gsell committed
472

473
    int id = CoordMap[idxyz];
gsell's avatar
gsell committed
474

475 476 477 478 479
    idx = id % (int)nr[0];
    id /= nr[0];
    idy = id % (int)nr[1];
    id /= nr[1];
    idz = id;
gsell's avatar
gsell committed
480 481
}

482 483 484 485 486
inline bool ArbitraryDomain::isInside(int idx, int idy, int idz) {
 /* Expensive computation to check */
 /*
	Vector_t P0, P;
    	P0 = Vector_t(0,0,0);
gsell's avatar
gsell committed
487

488 489 490 491 492
	P[0] = (idx - (nr[0]-1)/2.0)*hr[0];
	P[1] = (idy - (nr[1]-1)/2.0)*hr[1];
	P[2] = (idz - (nr[2]-1)/2.0)*hr[z];
	return (bgeom_m->fastIsInside(P0, P) % 2 == 0); 
 */
gsell's avatar
gsell committed
493
    bool ret = false;
494 495 496
    double cx = (idx - (nr[0]-1)/2.0)*hr[0];
    double cy = (idy - (nr[1]-1)/2.0)*hr[1];
    double cz = (idz - (nr[2]-1)/2.0)*hr[2];
gsell's avatar
gsell committed
497

498 499
    int    countH, countL;
    std::multimap < std::tuple<int, int, int>, double >::iterator itrH, itrL;
gsell's avatar
gsell committed
500

501 502
    std::tuple<int, int, int> coordxyz(idx, idy, idz);
             
gsell's avatar
gsell committed
503
    //check if z is inside with x,y coords
504 505
    itrH = IntersectHiZ.find(coordxyz);
    itrL = IntersectLoZ.find(coordxyz);
506

507 508 509 510 511
    countH = IntersectHiZ.count(coordxyz);
    countL = IntersectLoZ.count(coordxyz);

    if(countH == 1 && countL == 1)
        ret = (cz <= itrH->second) && (cz >= itrL->second);
512 513

     //check if y is inside with x,z coords
514 515 516 517 518
    itrH = IntersectHiY.find(coordxyz);
    itrL = IntersectLoY.find(coordxyz);

    countH = IntersectHiY.count(coordxyz);
    countL = IntersectLoY.count(coordxyz);
gsell's avatar
gsell committed
519

520 521
    if(countH == 1 && countL == 1)
        ret = ret && (cy <= itrH->second) && (cy >= itrL->second);
gsell's avatar
gsell committed
522

523
    //check if x is inside with y,z coords
524 525 526 527 528 529 530 531
    itrH = IntersectHiX.find(coordxyz);
    itrL = IntersectLoX.find(coordxyz);

    countH = IntersectHiX.count(coordxyz);
    countL = IntersectLoX.count(coordxyz);

    if(countH == 1 && countL == 1)
        ret = ret && (cx <= itrH->second) && (cx >= itrL->second);
532 533
       
    return ret; 
gsell's avatar
gsell committed
534 535 536
}

int ArbitraryDomain::getNumXY(int z) {
537 538 539
    
	return numXY[z];
}
gsell's avatar
gsell committed
540 541


542
void ArbitraryDomain::getBoundaryStencil(int idxyz, double &W, double &E, double &S, double &N, double &F, double &B, double &C, double &scaleFactor) {
543
    int idx = 0, idy = 0, idz = 0;
544

545 546
    getCoord(idxyz, idx, idy, idz);
    getBoundaryStencil(idx, idy, idz, W, E, S, N, F, B, C, scaleFactor);
gsell's avatar
gsell committed
547 548
}

549
void ArbitraryDomain::getBoundaryStencil(int idx, int idy, int idz, double &W, double &E, double &S, double &N, double &F, double &B, double &C, double &scaleFactor) {
gsell's avatar
gsell committed
550

551
    scaleFactor = 1.0;
552
   // determine which interpolation method we use for points near the boundary
553 554
    switch(interpolationMethod){
    	case CONSTANT:
555
        	ConstantInterpolation(idx,idy,idz,W,E,S,N,F,B,C,scaleFactor);
556 557
        	break;
    	case LINEAR:
558
//        	LinearInterpolation(idx,idy,idz,W,E,S,N,F,B,C,scaleFactor);
559 560
        	break;
    	case QUADRATIC:
561
	    //  QuadraticInterpolation(idx,idy,idz,W,E,S,N,F,B,C,scaleFactor);
562
        	break;
563 564 565 566 567 568
    }

    // stencil center value has to be positive!
    assert(C > 0);
}

569
void ArbitraryDomain::ConstantInterpolation(int idx, int idy, int idz, double& W, double& E, double& S, double& N, double& F, double& B, double& C, double &scaleFactor) {
570 571 572 573 574 575 576 577 578

    W = -1/(hr[0]*hr[0]);
    E = -1/(hr[0]*hr[0]);
    N = -1/(hr[1]*hr[1]);
    S = -1/(hr[1]*hr[1]);
    F = -1/(hr[2]*hr[2]);
    B = -1/(hr[2]*hr[2]);
    C = 2/(hr[0]*hr[0]) + 2/(hr[1]*hr[1]) + 2/(hr[2]*hr[2]);

579
    if(!isInside(idx+1,idy,idz)) 
580 581
        E = 0.0;

582
    if(!isInside(idx-1,idy,idz))
583 584
        W = 0.0;

585
    if(!isInside(idx,idy+1,idz))
586 587
        N = 0.0;

588
    if(!isInside(idx,idy-1,idz)) 
589
        S = 0.0;
590
    
591
    if(!isInside(idx,idy,idz-1)) 
592
	F = 0.0;	
593

594
    if(!isInside(idx,idy,idz+1)) 
595
	B = 0.0;	
596 597 598

}
/*
599
void ArbitraryDomain::LinearInterpolation(int idx, int idy, int idz, double& W, double& E, double& S, double& N, double& F, double& B, double& C, double &scaleFactor) 
600 601 602 603
{

    scaleFactor = 1.0;

604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    double dx=-1, dy=-1, dz=-1;

    double cx = (idx - (nr[0]-1)/2.0)*hr[0]-globalMeanR_m[0];
    double cy = (idy - (nr[1]-1)/2.0)*hr[1]-globalMeanR_m[1];
    double cz = (idz - (nr[2]-1)/2.0)*hr[2]-globalMeanR_m[2];

    int    countH, countL;
    std::multimap < std::tuple<int, int, int>, double >::iterator itrH, itrL;

    std::tuple<int, int, int> coordxyz(idx, idy, idz);
             
    //check if z is inside with x,y coords
    itrH = IntersectHiZ.find(coordxyz);
    itrL = IntersectLoZ.find(coordxyz);

    countH = IntersectHiZ.count(coordxyz);
    countL = IntersectLoZ.count(coordxyz);

    if(countH == 1 && countL == 1)
        ret = (cz <= itrH->second) && (cz >= itrL->second);

625 626 627 628 629


    std::multimap< std::pair<int, int>, double >::iterator it;
    std::pair< std::multimap< std::pair<int, int>, double>::iterator, std::multimap< std::pair<int, int>, double>::iterator > ret;

630
    std::pair<int, int> coordyz(idy, idz);
631 632 633 634 635 636 637 638
    ret = IntersectXDir.equal_range(coordyz);
    for(it = ret.first; it != ret.second; ++it) {
        if(fabs(it->second - cx) < hr[0]) {
            dx = it->second;
            break;
        }
    }

639
    std::pair<int, int> coordxz(idx, idz);
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
    ret = IntersectYDir.equal_range(coordxz);
    for(it = ret.first; it != ret.second; ++it) {
        if(fabs(it->second - cy) < hr[1]) {
            dy = it->second;
            break;
        }
    }


    double dw=hr[0];
    double de=hr[0];
    double dn=hr[1];
    double ds=hr[1];
    C = 0.0;

    // we are a right boundary point
    if(dx >= 0 && dx > cx)
    {	
        C += 1/((dx-cx)*de);
        E = 0.0;
    } else {
        C += 1/(de*de);
        E = -1/(de*de);
    }

    // we are a left boundary point
    if(dx <= 0 && dx < cx)
    {
        C += 1/((cx-dx)*dw);
        W = 0.0;
    } else {
        C += 1/(dw*dw);
        W = -1/(dw*dw);
    }

    // we are a upper boundary point
    if(dy >= 0 && dy > cy)
    {
        C += 1/((dy-cy)*dn);
        N = 0.0;
    } else {
        C += 1/(dn*dn);
        N = -1/(dn*dn);
    }

    // we are a lower boundary point
    if(dy <= 0 && dy < cy)
    {
        C += 1/((cy-dy)*ds);
        S = 0.0;
    } else {
        C += 1/(ds*ds);
        S = -1/(ds*ds);
    }

    F = -1/(hr[2]*hr[2]); 
    B = -1/(hr[2]*hr[2]);

    //XXX: In stand-alone only Dirichlet for validation purposes
    if(z == 0 || z == nr[2]-1) {

        // Dirichlet
        C += 2/hr[2]*1/hr[2];

        //C += 1/hr[2]*1/hr[2];

        // case where we are on the Neumann BC in Z-direction
        // where we distinguish two cases  
        if(z == 0)
            F = 0.0;
        else 
            B = 0.0;

        //for test no neumann 
        //C += 2/((hr[2]*nr[2]/2.0) * hr[2]);
715 716
        //
        //   double d = hr[2]*(nr[2])/2;
717 718 719
        //   C += 2/(d * hr[2]);


720 721 722 723 724
        ////neumann stuff
        //W /= 2.0;
        //E /= 2.0;
        //N /= 2.0;
        //S /= 2.0;
725 726
        //C /= 2.0;

727
        scaleFactor *= 0.5;
728 729 730 731

    } else 
        C += 2*1/hr[2]*1/hr[2];
}
732
*/        
gsell's avatar
gsell committed
733

734
void ArbitraryDomain::getNeighbours(int id, int &W, int &E, int &S, int &N, int &F, int &B) {
gsell's avatar
gsell committed
735

736
    int idx = 0, idy = 0, idz = 0;
gsell's avatar
gsell committed
737

738 739
    getCoord(id, idx, idy, idz);
    getNeighbours(idx, idy, idz, W, E, S, N, F, B);
gsell's avatar
gsell committed
740 741
}

742
void ArbitraryDomain::getNeighbours(int idx, int idy, int idz, int &W, int &E, int &S, int &N, int &F, int &B) {
gsell's avatar
gsell committed
743

744 745
    if(idx > 0)
        W = getIdx(idx - 1, idy, idz);
gsell's avatar
gsell committed
746 747
    else
        W = -1;
748 749 750

    if(idx < nr[0] - 1)
        E = getIdx(idx + 1, idy, idz);
gsell's avatar
gsell committed
751 752 753
    else
        E = -1;

754 755
    if(idy < nr[1] - 1)
        N = getIdx(idx, idy + 1, idz);
gsell's avatar
gsell committed
756 757
    else
        N = -1;
758 759 760

    if(idy > 0)
        S = getIdx(idx, idy - 1, idz);
gsell's avatar
gsell committed
761 762 763
    else
        S = -1;

764 765
    if(idz > 0)
        F = getIdx(idx, idy, idz - 1);
gsell's avatar
gsell committed
766 767
    else
        F = -1;
768 769 770

    if(idz < nr[2] - 1)
        B = getIdx(idx, idy, idz + 1);
gsell's avatar
gsell committed
771 772 773 774 775 776 777 778 779 780 781 782
    else
        B = -1;

}


inline void ArbitraryDomain::crossProduct(double A[], double B[], double C[]) {
    C[0] = A[1] * B[2] - A[2] * B[1];
    C[1] = A[2] * B[0] - A[0] * B[2];
    C[2] = A[0] * B[1] - A[1] * B[0];
}

783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
inline void ArbitraryDomain::rotateWithQuaternion(Vector_t & v, Vektor<double, 4> const quaternion) {
    // rotates a Vector_t (3 elements) using a quaternion.
    // Flip direction of rotation by quaternionVectorcomponent *= -1

    Vector_t const quaternionVectorComponent = Vector_t(quaternion(1), quaternion(2), quaternion(3));
    double const quaternionScalarComponent = quaternion(0);
        
    v = 2.0 * dot(quaternionVectorComponent, v) * quaternionVectorComponent 
        + (quaternionScalarComponent * quaternionScalarComponent  
        -  dot(quaternionVectorComponent, quaternionVectorComponent)) * v 
        + 2.0 * quaternionScalarComponent * cross(quaternionVectorComponent, v);
}

inline void ArbitraryDomain::rotateXAxisWithQuaternion(Vector_t & v, Vektor<double, 4> const quaternion) {
    // rotates the positive xaxis using a quaternion.
   
    v(0) = quaternion(0) * quaternion(0) 
         + quaternion(1) * quaternion(1) 
         - quaternion(2) * quaternion(2) 
         - quaternion(3) * quaternion(3);
 
    v(1) = 2.0 * (quaternion(1) * quaternion(2) + quaternion(0) * quaternion(3));
    v(2) = 2.0 * (quaternion(1) * quaternion(3) - quaternion(0) * quaternion(2));
}

inline void ArbitraryDomain::rotateYAxisWithQuaternion(Vector_t & v, Vektor<double, 4> const quaternion) {
    // rotates the positive yaxis using a quaternion.
    
    v(0) = 2.0 * (quaternion(1) * quaternion(2) - quaternion(0) * quaternion(3));
      
    v(1) = quaternion(0) * quaternion(0) 
         - quaternion(1) * quaternion(1)
         + quaternion(2) * quaternion(2)
         - quaternion(3) * quaternion(3);

    v(2) = 2.0 * (quaternion(2) * quaternion(3) + quaternion(0) * quaternion(1));
}

inline void ArbitraryDomain::rotateZAxisWithQuaternion(Vector_t & v, Vektor<double, 4> const quaternion) {
    // rotates the positive zaxis using a quaternion.
    v(0) = 2.0 * (quaternion(1) * quaternion(3) + quaternion(0) * quaternion(2));
    v(1) = 2.0 * (quaternion(2) * quaternion(3) - quaternion(0) * quaternion(1));    

    v(2) = quaternion(0) * quaternion(0) 
         - quaternion(1) * quaternion(1)
         - quaternion(2) * quaternion(2)
         + quaternion(3) * quaternion(3);

}
832
#endif //#ifdef HAVE_SAAMG_SOLVER