ArbitraryDomain.cpp 25.8 KB
Newer Older
gsell's avatar
gsell committed
1
#ifdef HAVE_ML_SOLVER
2 3 4 5
#include <map>
#include <cmath>
#include <iostream>
#include <assert.h>
gsell's avatar
gsell committed
6 7

#include "ArbitraryDomain.h"
8

gsell's avatar
gsell committed
9 10 11 12 13

//IFF: simplified case where we have intersect = 2
//IFF: every node counts own number of gridpoints and then sum to global procs before MyPID (including ghost layers in z direction).
//IFF: triangles intersecting in z dir? (cathode?) -> boundary condition there? neumann? then we should take a normal and stuff!?

14 15 16 17 18 19
ArbitraryDomain::ArbitraryDomain(BoundaryGeometry * bgeom, Vector_t Geo_mincoords, Vector_t Geo_maxcoords, Vector_t nr, Vector_t hr, NDIndex<3> locidx, std::string interpl) {
    bgeom_m=bgeom;
    Geo_mincoords_m = bgeom->getmincoords();
    Geo_maxcoords_m = bgeom->getmaxcoords();
    setNr(nr);
    setHr(hr);
gsell's avatar
gsell committed
20 21 22
    localidx = locidx;
    startIdx = 0;

23 24 25 26 27
    std::cout << "Arbitrary domain TK localidx:" << localidx << std::endl; 
    std::cout << Geo_mincoords_m << std::endl;
    std::cout << Geo_maxcoords_m << std::endl;
    std::cout << "bgeom-maxcoords" << bgeom->getmaxcoords() << std::endl;	
    std::cout << "hr_m" << hr << std::endl;	
gsell's avatar
gsell committed
28

29 30 31 32 33 34
    if(interpl == "constant")
        interpolationMethod = CONSTANT;
    else if(interpl == "linear")
        interpolationMethod = LINEAR;
    else if(interpl == "quadratic")
        interpolationMethod = QUADRATIC;
gsell's avatar
gsell committed
35 36 37

}

38 39
ArbitraryDomain::~ArbitraryDomain() {
    //nothing so far
gsell's avatar
gsell committed
40 41 42 43 44 45
}

void ArbitraryDomain::Compute(Vector_t hr) {

    setHr(hr);

46 47
    hasGeometryChanged_m = true;

gsell's avatar
gsell committed
48 49 50 51 52 53
    //h5_float64_t edge1, edge2, t, q, p, P0;
    double edge1[3], edge2[3], t[3], q[3], p[3], P0[3];
    double det, invDet, u, v, tt;
    double dir[3], origin[3];
    int zGhostOffsetLeft = (localidx[2].first() == 0) ? 0 : 1;
    int zGhostOffsetRight = (localidx[2].last() == nr[2] - 1) ? 0 : 1;
54 55
    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;
gsell's avatar
gsell committed
56 57 58
    bool hit;


59 60 61
    //forall triangles
    int numbfaces = bgeom_m->getNumBFaces();
    for (int i = 0; i < numbfaces ; i++) {
gsell's avatar
gsell committed
62

63 64 65
        Vector_t x1 = bgeom_m->getVertexCoord(i, 1); //geo3Dcoords_m[allbfaces_m[4 * face_id + vertex_id]]
        Vector_t x2 = bgeom_m->getVertexCoord(i, 2); //geo3Dcoords_m[allbfaces_m[4 * face_id + vertex_id]]
        Vector_t x3 = bgeom_m->getVertexCoord(i, 3); //geo3Dcoords_m[allbfaces_m[4 * face_id + vertex_id]]
gsell's avatar
gsell committed
66

67 68 69
	P0[0] = x2[0];
        P0[1] = x2[1];
        P0[2] = x2[2];
gsell's avatar
gsell committed
70

71 72 73
        edge1[0] = x1[0] - x2[0];
        edge1[1] = x1[1] - x2[1];
        edge1[2] = x1[2] - x2[2];
gsell's avatar
gsell committed
74

75 76 77
        edge2[0] = x3[0] - x2[0];
        edge2[1] = x3[1] - x2[1];
        edge2[2] = x3[2] - x2[2];
gsell's avatar
gsell committed
78 79 80 81 82 83

        //IFF: use normed dir -> t = intersection (?)
        //x dir rays = forall points in origin y,z with dir (1,0,0) (Y,Z -> COORD)
        dir[0] = 1;
        dir[1] = 0;
        dir[2] = 0;
84 85 86
        origin[0] = 0; //Geo_mincoords_m(0); //0;
        origin[1] = 0; //Geo_mincoords_m(1); //0;
	origin[2] = getMinZ(); //0;
gsell's avatar
gsell committed
87
        for(int y = 0; y < nr[1]; y++) {
88
            origin[1] = (y - (nr[1]-1)/2.0)*hr[1];
gsell's avatar
gsell committed
89 90 91
            //ONE LAYER MORE = GHOST LAYER
            for(int z = localidx[2].first() - zGhostOffsetLeft; z <= localidx[2].last() + zGhostOffsetRight; z++) {

92
                origin[2] = getMinZ()+z * hr[2];
gsell's avatar
gsell committed
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

                crossProduct(dir, edge2, p);
                det = dotProduct(edge1, p);

                if(det > -1e-5 && det < 1e-5)
                    continue;

                invDet = 1.0 / det;

                t[0] = origin[0] - P0[0];
                t[1] = origin[1] - P0[1];
                t[2] = origin[2] - P0[2];
                u = dotProduct(t, p) * invDet;
                if(u < 0.0 || u > 1.0)
                    continue;

                crossProduct(t, edge1, q);
                v = dotProduct(dir, q) * invDet;
                if(v < 0.0 || u + v > 1.0)
                    continue;

                //ray really intersects triangle
                tt = dotProduct(edge2, q) * invDet;

                //put intersection point in structure
                std::pair<int, int> tmp(y, z);
                //IntersectZDir[tmp] = tt;
                hit = false;
                ret = IntersectXDir.equal_range(tmp);
                for(it = ret.first; it != ret.second; ++it)
                    hit = hit || (fabs(it->second - tt) < 1e-15);
                if(!hit)
                    IntersectXDir.insert(std::pair< std::pair<int, int>, double >(tmp, tt));

            }
        }

        //y dir rays = forall points in origin x,z with dir (0,1,0) (X,Z -> COORD)
        dir[0] = 0;
        dir[1] = 1;
        dir[2] = 0;
134 135 136
	origin[0] = 0;//Geo_mincoords_m(0); //0;
        origin[1] = 0;//Geo_mincoords_m(1); //0;
        origin[2] = getMinZ(); //0;
gsell's avatar
gsell committed
137
        for(int x = 0; x < nr[0]; x++) {
138
            origin[0] = (x - (nr[0]-1)/2.0)*hr[0];
gsell's avatar
gsell committed
139
            //ONE LAYER MORE = GHOST LAYER
140
            for(int z = localidx[2].first() - zGhostOffsetLeft; z <= localidx[2].last() + zGhostOffsetRight; z++) {
gsell's avatar
gsell committed
141

142
                origin[2] = getMinZ() + z * hr[2];
gsell's avatar
gsell committed
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

                crossProduct(dir, edge2, p);
                det = dotProduct(edge1, p);

                if(det > -1e-5 && det < 1e-5)
                    continue;

                invDet = 1.0 / det;

                t[0] = origin[0] - P0[0];
                t[1] = origin[1] - P0[1];
                t[2] = origin[2] - P0[2];
                u = dotProduct(t, p) * invDet;
                if(u < 0.0 || u > 1.0)
                    continue;

                crossProduct(t, edge1, q);
                v = dotProduct(dir, q) * invDet;
                if(v < 0.0 || u + v > 1.0)
                    continue;

                //ray really intersects triangle
                tt = dotProduct(edge2, q) * invDet;

                //put intersection point in structure
                std::pair<int, int> tmp(x, z);
                hit = false;
                ret = IntersectYDir.equal_range(tmp);
                for(it = ret.first; it != ret.second; ++it)
                    hit = hit || (fabs(it->second - tt) < 1e-15);
                if(!hit)
                    IntersectYDir.insert(std::pair< std::pair<int, int>, double >(tmp, tt));

            }
        }

        //z dir rays = forall points in origin x,y with dir (0,0,1) (X,Y -> COORD)
        dir[0] = 0;
        dir[1] = 0;
        dir[2] = 1;
183 184 185
        origin[0] = 0; //Geo_mincoords_m(0); //0;
        origin[1] = 0; //Geo_mincoords_m(1); //0;
        origin[2] = getMinZ(); //0;
gsell's avatar
gsell committed
186
        for(int x = 0; x < nr[0]; x++) {
187
            origin[0] = (x - (nr[0]-1)/2.0)*hr[0];
gsell's avatar
gsell committed
188
            for(int y = 0; y < nr[1]; y++) {
189
            	origin[1] = (y - (nr[1]-1)/2.0)*hr[1];
gsell's avatar
gsell committed
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 221 222 223 224 225

                crossProduct(dir, edge2, p);
                det = dotProduct(edge1, p);

                if(det > -1e-5 && det < 1e-5)
                    continue;

                invDet = 1.0 / det;

                t[0] = origin[0] - P0[0];
                t[1] = origin[1] - P0[1];
                t[2] = origin[2] - P0[2];
                u = dotProduct(t, p) * invDet;
                if(u < 0.0 || u > 1.0)
                    continue;

                crossProduct(t, edge1, q);
                v = dotProduct(dir, q) * invDet;
                if(v < 0.0 || u + v > 1.0)
                    continue;

                //ray really intersects triangle
                tt = dotProduct(edge2, q) * invDet;

                //put intersection point in structure
                std::pair<int, int> tmp(x, y);
                hit = false;
                ret = IntersectZDir.equal_range(tmp);
                for(it = ret.first; it != ret.second; ++it)
                    hit = hit || (fabs(it->second - tt) < 1e-15);
                if(!hit)
                    IntersectZDir.insert(std::pair< std::pair<int, int>, double >(tmp, tt));
            }
        }

    }
226 227 228
    std::cout << "number of intersections in x-dir: " << IntersectXDir.size() << std::endl;
    std::cout << "number of intersections in y-dir: " << IntersectYDir.size() << std::endl;
    std::cout << "number of intersections in z-dir: " << IntersectZDir.size() << std::endl;
gsell's avatar
gsell committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242

    //number of ghost nodes to the left
    int numGhostNodesLeft = 0;
    if(localidx[2].first() != 0) {
        for(int x = 0; x < nr[0]; x++) {
            for(int y = 0; y < nr[1]; y++) {

                if(isInside(x, y, localidx[2].first() - zGhostOffsetLeft))
                    numGhostNodesLeft++;

            }
        }
    }

243
    std::cout << "ghost nodes left: " << numGhostNodesLeft << std::endl;
gsell's avatar
gsell committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

    //xy points in z plane
    int numxy = 0;
    int numtotal = 0;
    numXY.clear();
    for(int idx = localidx[2].first(); idx <= localidx[2].last(); idx++) {
        numxy = 0;
        for(int x = 0; x < nr[0]; x++) {
            for(int y = 0; y < nr[1]; y++) {

                if(isInside(x, y, idx))
                    numxy++;

            }
        }

        numXY[idx-localidx[2].first()] = numxy;
        numtotal += numxy;

    }

265
    std::cout << "number of gridpoints: " << numtotal << std::endl;
gsell's avatar
gsell committed
266 267 268 269 270

    startIdx = 0;
    MPI_Scan(&numtotal, &startIdx, 1, MPI_INTEGER, MPI_SUM, Ippl::getComm());
    startIdx -= numtotal;

271
    std::cout << "start idx: " << startIdx << std::endl;
gsell's avatar
gsell committed
272 273 274 275 276 277

    //build up index and coord map
    IdxMap.clear();
    CoordMap.clear();
    register int idx = startIdx - numGhostNodesLeft;

278
	std::cout << idx << std::endl; 
gsell's avatar
gsell committed
279 280 281 282 283 284
    for(int x = 0; x < nr[0]; x++) {
        for(int y = 0; y < nr[1]; y++) {
            for(int z = localidx[2].first() - zGhostOffsetLeft; z <= localidx[2].last() + zGhostOffsetRight; z++) {

                if(isInside(x, y, z)) {
                    IdxMap[toCoordIdx(x, y, z)] = idx;
285
                    CoordMap[idx++] = toCoordIdx(x, y, z);
gsell's avatar
gsell committed
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
                }

            }
        }
    }

}

/// conversion from (x,y,z) to index in xyz plane
inline int ArbitraryDomain::toCoordIdx(int x, int y, int z) {
    return (z * nr[1] + y) * nr[0] + x;
}

/// conversion from (x,y,z) to index on the 3D grid
/*inline*/
int ArbitraryDomain::getIdx(int x, int y, int z) {
    if(isInside(x, y, z) && x >= 0 && y >= 0 && z >= 0)
        return IdxMap[toCoordIdx(x, y, z)];
    else
        return -1;
}


/// conversion from a 3D index to (x,y,z)
inline void ArbitraryDomain::getCoord(int idx, int &x, int &y, int &z) {

    int idxx = CoordMap[idx];

    x = idxx % (int)nr[0];
    idxx /= nr[0];
    y = idxx % (int)nr[1];
    idxx /= nr[1];
    z = idxx;

}


//IFF: at the moment this implementation only allows 2 intersections with geometry!!
inline bool ArbitraryDomain::isInside(int x, int y, int z) {

    bool ret = false;
327 328 329 330 331
    //double cx = x * hr[0];
    //double cy = y * hr[1];
    double cx = (x - (nr[0]-1)/2)*hr[0];
    double cy = (y - (nr[1]-1)/2)*hr[1];
 //   double cz = z * hr[2];
gsell's avatar
gsell committed
332 333
    double val1 = 0;
    double val2 = 0;
334
    std::multimap < std::pair<int, int>, double >::iterator itr;
gsell's avatar
gsell committed
335 336 337 338 339 340 341 342 343 344

    //check if x is inside with y,z coords
    std::pair<int, int> coordyz(y, z);
    itr = IntersectXDir.find(coordyz);
    int count = IntersectXDir.count(coordyz);
    if(count == 1)
        ret = (cx == itr->second);
    else {
        val1 = itr->second;
        val2 = (++itr)->second;
345
        ret = (cx <= std::max(val1, val2)) && (cx >= std::min(val1, val2));
gsell's avatar
gsell committed
346 347 348 349 350 351 352 353 354 355 356
    }

    //check if y is inside with x,z coords
    std::pair<int, int> coordxz(x, z);
    itr = IntersectYDir.find(coordxz);
    count = IntersectYDir.count(coordxz);
    if(count == 1)
        ret = ret && (cy == itr->second);
    else {
        val1 = itr->second;
        val2 = (++itr)->second;
357
        ret = ret && (cy <= std::max(val1, val2)) && (cy >= std::min(val1, val2));
gsell's avatar
gsell committed
358 359 360
    }

    //check if z is inside with x,y coords
361
/*
gsell's avatar
gsell committed
362 363 364 365 366 367 368 369
    std::pair<int,int> coordxy(x,y);
    itr = IntersectZDir.find(coordxy);
    count = IntersectZDir.count(coordxy);
    if(count == 1)
      ret = ret && (cz == itr->second);
    else {
      val1 = itr->second;
      val2 = (++itr)->second;
370 371
      ret = ret && (cz <= std::max(val1, val2)) && (cz >= std::min(val1, val2));
    }
gsell's avatar
gsell committed
372

373 374
    ret = ret && (cz >= 0 && cz <= nr[2]);
*/
gsell's avatar
gsell committed
375 376 377 378 379 380 381 382 383 384 385 386
    return ret;

}

int ArbitraryDomain::getNumXY(int z) {

    return numXY[z];

}

void ArbitraryDomain::getBoundaryStencil(int x, int y, int z, double &W, double &E, double &S, double &N, double &F, double &B, double &C, double &scaleFactor) {

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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
   // determine which interpolation method we use for points near the boundary
    switch(interpolationMethod) {

    case CONSTANT:
        ConstantInterpolation(x,y,z,W,E,S,N,F,B,C,scaleFactor);
        break;

    case LINEAR:
        LinearInterpolation(x,y,z,W,E,S,N,F,B,C,scaleFactor);
        break;

    case QUADRATIC:
	std::cout << "TK implement this later" <<std::endl;
    //    QuadraticInterpolation(x,y,z,W,E,S,N,F,B,C,scaleFactor);
        break;
    }

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

void ArbitraryDomain::ConstantInterpolation(int x, int y, int z, double& W, double& E, double& S, double& N, double& F, double& B, double& C, double &scaleFactor) {

    scaleFactor = 1.0;

    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]);

    // we are a right boundary point
    if(!isInside(x+1,y,z)) {
        E = 0.0;
    }

    // we are a left boundary point
    if(!isInside(x-1,y,z)) {
        W = 0.0;
    }

    // we are a upper boundary point
    if(!isInside(x,y+1,z)) {
        N = 0.0;
    }

    // we are a lower boundary point
    if(!isInside(x,y-1,z)) {
        S = 0.0;
    }

    if(z == 0 || z == nr[2] - 1) {
        // case where we are on the Robin BC in Z-direction
        // where we distinguish two cases
        // IFF: this values should not matter because they
        // never make it into the discretization matrix
        if(z == 0)
            F = 0.0;
        else
            B = 0.0;

        // add contribution of Robin discretization to center point
        // d the distance between the center of the bunch and the boundary
        //double cx = (x-(nr[0]-1)/2)*hr[0];
        //double cy = (y-(nr[1]-1)/2)*hr[1];
        //double cz = hr[2]*(nr[2]-1);
        //double d = sqrt(cx*cx+cy*cy+cz*cz);
        double d = hr[2] * (nr[2] - 1) / 2;
        C += 2 / (d * hr[2]);
        //C += 2/((hr[2]*(nr[2]-1)/2.0) * hr[2]);

        // scale all stencil-points in z-plane with 0.5 (Robin discretization)
        W /= 2.0;
        E /= 2.0;
        N /= 2.0;
        S /= 2.0;
        C /= 2.0;
        scaleFactor *= 0.5;
    }

}
/*
void ArbitraryDomain::LinearInterpolation(int x, int y, int z, double& W, double& E, double& S, double& N, double& F, double& B, double& C, double &scaleFactor) 
{

    scaleFactor = 1.0;

    double dx=-1, dy=-1;

    double cx = (x - (nr[0]-1)/2)*hr[0];
    double cy = (y - (nr[1]-1)/2)*hr[1];

    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;

    std::pair<int, int> coordyz(y, z);
    ret = IntersectXDir.equal_range(coordyz);
    for(it = ret.first; it != ret.second; ++it) {
        if(fabs(it->second - cx) < hr[0]) {
            dx = it->second;
            break;
        }
    }

    std::pair<int, int> coordxz(x, z);
    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]);
        
        //  double d = hr[2]*(nr[2])/2;
        //   C += 2/(d * hr[2]);


        //neumann stuff
        // W /= 2.0;
        // E /= 2.0;
        // N /= 2.0;
        // S /= 2.0;
        //C /= 2.0;

       // scaleFactor *= 0.5;
       // 

    } else 
        C += 2*1/hr[2]*1/hr[2];
}
*/
void ArbitraryDomain::LinearInterpolation(int x, int y, int z, double& W, double& E, double& S, double& N, double& F, double& B, double& C, double &scaleFactor) {

gsell's avatar
gsell committed
590 591
    double cx = x * hr[0];
    double cy = y * hr[1];
592
    //double cz = z * hr[2]; 
gsell's avatar
gsell committed
593

594 595
    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;
gsell's avatar
gsell committed
596 597 598

    //since every vector here is only an array[2] we
    //can catch all cases manually
599
    double dx = -1.0, dy = -1.0;//, dz = -1.0; 
gsell's avatar
gsell committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
    double dw = hr[0];
    double de = hr[0];
    double dn = hr[1];
    double ds = hr[1];
    double df = hr[2];
    double db = hr[2];
    C = 0.0;

    std::pair<int, int> coordyz(y, z);
    ret = IntersectXDir.equal_range(coordyz);
    for(it = ret.first; it != ret.second; ++it) {
        if(fabs(it->second - cx) < hr[0]) {
            dx = it->second;
            break;
        }
    }

    std::pair<int, int> coordxz(x, z);
    ret = IntersectYDir.equal_range(coordxz);
    for(it = ret.first; it != ret.second; ++it) {
        if(fabs(it->second - cy) < hr[1]) {
            dy = it->second;
            break;
        }
    }

626
/*
gsell's avatar
gsell committed
627 628 629
    std::pair<int, int> coordxy(x,y);
    ret = IntersectZDir.equal_range(coordxy);
    for(it=ret.first; it!=ret.second; ++it) {
630
      if(abs(it->second - cz) < hr[2]) {
gsell's avatar
gsell committed
631 632 633 634
        dz = it->second;
        break;
      }
    }
635
*/   
gsell's avatar
gsell committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657

    //we are a right boundary point
    //if(!isInside(x+1,y,z))
    if(dx >= 0 && dx > cx)
        de = dx - cx;

    //we are a left boundary point
    //if(!isInside(x-1,y,z))
    if(dx >= 0 && dx < cx)
        dw = cx - dx;

    //we are a upper boundary point
    //if(!isInside(x,y+1,z))
    if(dy >= 0 && dy > cy)
        dn = dy - cy;

    //we are a lower boundary point
    //if(!isInside(x,y-1,z))
    if(dy >= 0 && dy < cy)
        ds = cy - dy;

    //we are a lower boundary point
658 659 660 661
/*
    //if(!isInside(x,y,z+1))
    if(dz >= 0 && dz > cz)
        df = dz - cz;
gsell's avatar
gsell committed
662 663

    //we are a lower boundary point
664 665 666 667
    //if(!isInside(x,y,z-1))
    if(dy >= 0 && dy <= cy)
        db = cz - dz;
 */
gsell's avatar
gsell committed
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

    //for regular gridpoints no problem with symmetry, just boundary

    if(dw != 0)
        W = -(df + db) * (dn + ds) / dw;
    else
        W = 0;
    if(de != 0)
        E = -(df + db) * (dn + ds) / de;
    else
        E = 0;
    if(dn != 0)
        N = -(df + db) * (dw + de) / dn;
    else
        N = 0;
    if(ds != 0)
        S = -(df + db) * (dw + de) / ds;
    else
        S = 0;
    if(df != 0)
        F = -(dw + de) * (dn + ds) / df;
    else
        F = 0;
    if(db != 0)
        B = -(dw + de) * (dn + ds) / db;
    else
        B = 0;

    //RHS scaleFactor for current 3D index
    //0.5* comes from discretiztaion
    //scaleFactor = 0.5*(dw+de)*(dn+ds)*(df+db);
    scaleFactor = 0.5;
700
    if((dw + de) != 0)
gsell's avatar
gsell committed
701
        scaleFactor *= (dw + de);
702
    if((dn + ds) != 0)
gsell's avatar
gsell committed
703
        scaleFactor *= (dn + ds);
704
    if((df + db) != 0)
gsell's avatar
gsell committed
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
        scaleFactor *= (df + db);

    //catch the case where a point lies on the boundary
    //IFF: do this more elegant!
    double m1 = dw * de;
    double m2 = dn * ds;
    if(de == 0)
        m1 = dw;
    if(dw == 0)
        m1 = de;
    if(dn == 0)
        m2 = ds;
    if(ds == 0)
        m2 = dn;
    //IFF: dn+ds || dw+de can be 0
    //C = 2*(dn+ds)*(dw+de)/hr[2];
    C = 2 / hr[2];
    if(dw != 0 || de != 0)
        C *= (dw + de);
    if(dn != 0 || ds != 0)
        C *= (dn + ds);
    if(dw != 0 || de != 0)
        C += (df + db) * (dn + ds) * (dw + de) / m1;
    if(dn != 0 || ds != 0)
        C += (df + db) * (dw + de) * (dn + ds) / m2;

731 732
    if(C <= 0)
      std::cout << "!!!!!!!!!! C is <= 0: " << C <<  std::endl;
gsell's avatar
gsell committed
733

734
        // handle boundary condition in z direction
gsell's avatar
gsell committed
735
    if(z == 0 || z == nr[2] - 1) {
736 737 738
	F = -1 / (hr[2] * hr[2]);
	B = -1 / (hr[2] * hr[2]);
	C += 2 / (hr[2] * hr[2]);
gsell's avatar
gsell committed
739

740 741 742
        // case where we are on the NEUMAN BC in Z-direction
        // where we distinguish two cases
        if(z == 0)
gsell's avatar
gsell committed
743
            F = 0.0;
744
        else
gsell's avatar
gsell committed
745 746
            B = 0.0;

747 748 749 750
        W /= 2.0;
        E /= 2.0;
        N /= 2.0;
        S /= 2.0;
gsell's avatar
gsell committed
751
        C /= 2.0;
752
        scaleFactor *= 0.5;
gsell's avatar
gsell committed
753 754 755 756

        //if(C <= 0)
        //  cout << "!!!!!!!!!! C is <= 0" << endl;
    }
757
	
gsell's avatar
gsell committed
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 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 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
}

/*
 * OLD STENCIL O(h)
void EllipticDomain::getBoundaryStencil(int x, int y, int z, double& W, double& E, double& S, double& N, double& F, double& B, double& C) {


  double cx = (x-floor(nr[0]/2.0))*hr[0];
  double cy = (y-floor(nr[1]/2.0))*hr[1];

  //since every vector here is only an array[2] we
  //can catch all cases manually
  double dx = 0.0;
  //std::vector <double> tmp = IntersectXDir.find(y)->second;
  multimap<int, double>::iterator it = IntersectXDir.find(y);
  if(cx < 0)
    it++;
  dx = it->second;
  //else
  //  dx = (++it)->second;

  double dy = 0.0;
  //std::vector <double> tmp = IntersectYDir.find(x)->second;
  it = IntersectYDir.find(x);
  if(cy < 0)
    it++;
  dy = it->second;
  //else
  //  dy = (++it)->second;

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

  bool hasW = true;
  bool hasE = true;
  bool hasN = true;
  bool hasS = true;

  //TODO: cx > 0 --> xc <= dx
  //TODO: remove isInside and replace with direct computation if we are inside

  if(true) { // z != 0 && z != nr[2]-1) {

    //if((x-nr[0]/2.0+1)*hr[0] > dx && cx > 0) {
    if(!isInside(x+1,y,z)) {
      //we are a right boundary point
      C += (dx-cx);
      de = 0.0;
    } else {
      C += de;
    }

    //if((x-nr[0]/2.0-1)*hr[0] < dx && cx < 0) {
    if(!isInside(x-1,y,z)) {
      //we are a left boundary point
      C += (abs(dx)-abs(cx));
      dw = 0.0;
    } else {
      C += dw;
    }

    //if((y-nr[1]/2.0+1)*hr[1] > dy && cy > 0) {
    if(!isInside(x,y+1,z)) {
      //we are a upper boundary point
      C += (dy-cy);
      dn = 0.0;
    } else {
      C += dn;
    }

    //if((y-nr[1]/2.0-1)*hr[1] < dy && cy < 0) {
    if(!isInside(x,y-1,z)) {
      //we are a lower boundary point
      C += (abs(dy)-abs(cy));
      ds = 0.0;
    } else {
      C += ds;
    }

    W = -dw;
    E = -de;
    N = -dn;
    S = -ds;
    F = -hr[2];
    B = -hr[2];
    C += hr[2]+hr[2];

    if(C <= 0)
      cout << "!!!!!!!!!! C is <= 0" << endl;

    if(C <= 2*hr[2])
      cout << "!!!!!!!!!! C is smaller than surrounding nodes" << endl;

    //if(cx == dx && cy == dy) {
    //  C = 0.0;
    //}

  } else {

    //NEUMAN in Z!

    if(z == 0) {
      F = 0.0;
      B = -hr[2];
      C = 2*hr[2];
    } else {
      B = 0.0;
      F = -hr[2];
      C = 2*hr[2];
    }

    //if((x-nr[0]/2.0+1)*hr[0] > dx && cx > 0) {
    if(!isInside(x+1,y,z)) {
      //we are a right boundary point
      C += (dx-cx);
      de = 0.0;
    } else {
      C += de;
    }

    //if((x-nr[0]/2.0-1)*hr[0] < dx && cx < 0) {
    if(!isInside(x-1,y,z)) {
      //we are a left boundary point
      C += (abs(dx)-abs(cx));
      dw = 0.0;
    } else {
      C += dw;
    }

    //if((y-nr[1]/2.0+1)*hr[1] > dy && cy > 0) {
    if(!isInside(x,y+1,z)) {
      //we are a upper boundary point
      C += (dy-cy);
      dn = 0.0;
    } else {
      C += dn;
    }

    //if((y-nr[1]/2.0-1)*hr[1] < dy && cy < 0) {
    if(!isInside(x,y-1,z)) {
      //we are a lower boundary point
      C += (abs(dy)-abs(cy));
      ds = 0.0;
    } else {
      C += ds;
    }

    //neumann stuff
    W = -dw/2.0;
    E = -de/2.0;
    N = -dn/2.0;
    S = -ds/2.0;
    C /= 2.0;

    if(C <= 0)
      cout << "!!!!!!!!!! C is <= 0" << endl;

    //if(cx == dx && cy == dy) {
    //  C = 0.0;
    //}
  }
}
*/

void ArbitraryDomain::getBoundaryStencil(int idx, double &W, double &E, double &S, double &N, double &F, double &B, double &C, double &scaleFactor) {

927 928
    //IFF: reverse map seah5errh?
    //double mem or O(n) seah5errh in map to get x,y,z from idx
gsell's avatar
gsell committed
929 930 931 932 933 934 935 936

    int x = 0, y = 0, z = 0;

    getCoord(idx, x, y, z);
    getBoundaryStencil(x, y, z, W, E, S, N, F, B, C, scaleFactor);

}

937
void ArbitraryDomain::getNeighbours(int idx, int &W, int &E, int &S, int &N, int &F, int &B) {
gsell's avatar
gsell committed
938 939 940 941 942 943 944 945 946 947

    int x = 0, y = 0, z = 0;

    getCoord(idx, x, y, z);
    getNeighbours(x, y, z, W, E, S, N, F, B);

}

//IFF:should be ok
//getIdx returns -1 when not inside domain
948
void ArbitraryDomain::getNeighbours(int x, int y, int z, int &W, int &E, int &S, int &N, int &F, int &B) {
gsell's avatar
gsell committed
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987

    if(x > 0)
        W = getIdx(x - 1, y, z);
    else
        W = -1;
    if(x < nr[0] - 1)
        E = getIdx(x + 1, y, z);
    else
        E = -1;

    if(y < nr[1] - 1)
        N = getIdx(x, y + 1, z);
    else
        N = -1;
    if(y > 0)
        S = getIdx(x, y - 1, z);
    else
        S = -1;

    //we have ghost nodes that can handle nodes at the boundary for z-1 and z+1
    if(z > 0)
        F = getIdx(x, y, z - 1);
    else
        F = -1;
    if(z < nr[2] - 1)
        B = getIdx(x, y, z + 1);
    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];
}

#endif //#ifdef HAVE_ML_SOLVER