TaperDomain.cpp 13 KB
Newer Older
1
#ifdef HAVE_SAAMG_SOLVER
gsell's avatar
gsell committed
2

kraus's avatar
kraus committed
3
#include "Solvers/TaperDomain.h"
gsell's avatar
gsell committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17

TaperDomain::TaperDomain(Vector_t nr, Vector_t hr) {
    setNr(nr);
    setHr(hr);
}

TaperDomain::TaperDomain(double rb, double rs, Vector_t nr, Vector_t hr, std::string interpl, double zmax_big) {

    radius_big = rb;
    radius_small = rs;
    z_max_big = zmax_big;
    setNr(nr);
    setHr(hr);

18
    if(interpl == "CONSTANT")
gsell's avatar
gsell committed
19
        interpolationMethod = CONSTANT;
20
    else if(interpl == "LINEAR")
gsell's avatar
gsell committed
21
        interpolationMethod = LINEAR;
22
    else if(interpl == "QUADRATIC")
gsell's avatar
gsell committed
23 24 25 26 27 28 29 30 31 32 33
        interpolationMethod = QUADRATIC;
}

TaperDomain::~TaperDomain() {
    //nothing so far
}


// for this domain we only have to calculate the intersection on one z
// cross-section
//if(hr[0] == getHr()[0] && hr[1] == getHr()[1])
34
void TaperDomain::compute(Vector_t hr) {
gsell's avatar
gsell committed
35 36 37 38

    setHr(hr);
    nxy_m = 0;

39 40
    int x, y, z;
    double pos, rb2, rs2;
gsell's avatar
gsell committed
41 42 43 44

    // clear previous coordinate maps
    IdxMap.clear();
    CoordMap.clear();
45
    int idx = 0;
gsell's avatar
gsell committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

    // FIXME: since we counting idx++ loop order matters!
    for(x = 0; x < nr[0]; x++) {
        for(y = 0; y < nr[1]; y++) {
            for(z = 0; z < nr[2]; z++) {

                if(isInside(x, y, z)) {
                    CoordMap[idx] = toCoordIdx(x, y, z);
                    IdxMap[toCoordIdx(x, y, z)] = idx++;
                    nxy_m++;
                }
            }
        }
    }

    switch(interpolationMethod) {

        case CONSTANT:
            break;
        case LINEAR:
        case QUADRATIC:

            rb2 = radius_big;
            rs2 = radius_small;

            // clear previous intersection points
            IntersectYDir.clear();
            IntersectXDir.clear();
            // IntersectYDir.count(2) == 2!

            for(x = 0; x < nr[0]; x++) {
                pos = (x - (nr[0] - 1) / 2) * hr[0];
snuverink_j's avatar
snuverink_j committed
78
                double yd = std::abs(sqrt(rb2 - pos * pos));
79 80
                IntersectYDir.insert(std::pair<int, double>(x, yd));
                IntersectYDir.insert(std::pair<int, double>(x, -yd));
gsell's avatar
gsell committed
81
                yd = std::abs(sqrt(rs2 - pos * pos));
82 83
                Intersectydir.insert(std::pair<int, double>(x, yd));
                Intersectydir.insert(std::pair<int, double>(x, -yd));
gsell's avatar
gsell committed
84 85 86 87
            }

            for(y = 0; y < nr[1]; y++) {
                pos = (y - (nr[1] - 1) / 2) * hr[1];
snuverink_j's avatar
snuverink_j committed
88
                double xd = std::abs(sqrt(rb2 - pos * pos));
89 90
                IntersectXDir.insert(std::pair<int, double>(y, xd));
                IntersectXDir.insert(std::pair<int, double>(y, -xd));
gsell's avatar
gsell committed
91
                xd = std::abs(sqrt(rs2 - pos * pos));
92 93
                Intersectxdir.insert(std::pair<int, double>(y, xd));
                Intersectxdir.insert(std::pair<int, double>(y, -xd));
gsell's avatar
gsell committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            }
    }
}

int TaperDomain::getNumXY(int z) {

    return nxy_m;

}

// ignore z since its the same for every z
std::vector<double> TaperDomain::getYDirIntersect(int x, int z) {
    std::vector<double> ret;
    for(std::multimap<int, double>::iterator it = IntersectYDir.find(x); it != IntersectYDir.end(); it++)
        ret.push_back(it->second);
    return ret;
}

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

    // determine which interpolation method we use for points near the boundary
    switch(interpolationMethod) {
        case CONSTANT:
117
            constantInterpolation(x, y, z, W, E, S, N, F, B, C, scaleFactor);
gsell's avatar
gsell committed
118 119
            break;
        case LINEAR:
120
            linearInterpolation(x, y, z, W, E, S, N, F, B, C, scaleFactor);
gsell's avatar
gsell committed
121 122
            break;
        case QUADRATIC:
123
            quadraticInterpolation(x, y, z, W, E, S, N, F, B, C, scaleFactor);
gsell's avatar
gsell committed
124 125 126 127 128 129 130 131 132 133 134 135
            break;
    }

    // simple check if center value of stencil is positive
#ifdef DEBUG
    if(C <= 0)
        cout << "Stencil C is <= 0! This should not case should never occure!" << endl;
#endif
}

// here we do not need to calculate intersection so we make use of the
// isInside function to determin if a given point is inside the domain
136
void TaperDomain::constantInterpolation(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
137 138 139 140 141 142 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

    //scaleFactor = hr[0]*hr[1]*hr[2];
    scaleFactor = 1.0;

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

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

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

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

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

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

    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;
    }

}

//TODO: remove isInside()
202
void TaperDomain::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
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

    scaleFactor = 1.0;

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

    // since every vector for elliptic domains has ALWAYS size 2 we
    // can catch all cases manually
    double dx = 0.0;
    std::multimap<int, double>::iterator it = IntersectXDir.find(y);
    if(cx < 0)
        it++;
    dx = it->second;

    double dy = 0.0;
    it = IntersectYDir.find(x);
    if(cy < 0)
        it++;
    dy = it->second;


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

    C = 0.0;

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

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

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

    //if(cy < dy && cy < 0 && dy < 0) {
    if(!isInside(x, y - 1, z)) {
        //we are a lower boundary point
        C += 1 / ((std::abs(dy) - std::abs(cy)) * 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]);
    C += 2 / (hr[2] * hr[2]);

    // handle boundary condition in z direction
    if(z == 0 || z == nr[2] - 1) {

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

        //C += 2/((hr[2]*(nr[2]-1)/2.0) * hr[2]);
        //hr[2]*(nr2[2]-1)/2 = radius
        double d = hr[2] * (nr[2] - 1) / 2;
        C += 2 / (d * hr[2]);

        W /= 2.0;
        E /= 2.0;
        N /= 2.0;
        S /= 2.0;
        C /= 2.0;
        scaleFactor /= 2.0;

    }

}

301
void TaperDomain::quadraticInterpolation(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
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 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

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

    // since every vector for elliptic domains has ALWAYS size 2 we
    // can catch all cases manually
    double dx = 0.0;
    std::multimap<int, double>::iterator it = IntersectXDir.find(y);
    if(cx < 0)
        it++;
    dx = it->second;

    double dy = 0.0;
    it = IntersectYDir.find(x);
    if(cy < 0)
        it++;
    dy = it->second;

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

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

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

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

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

    //for regular gridpoints no problem with symmetry, just boundary
    //z direction is right
    //implement isLastInside(dir)
    //we have LOCAL x,y coordinates!

    /*
       if(dw != 0 && !wIsB)
       W = -1/dw * (dn+ds) * 2*hr[2];
       else
       W = 0;
       if(de != 0 && !eIsB)
       E = -1/de * (dn+ds) * 2*hr[2];
       else
       E = 0;
       if(dn != 0 && !nIsB)
       N = -1/dn * (dw+de) * 2*hr[2];
       else
       N = 0;
       if(ds != 0 && !sIsB)
       S = -1/ds * (dw+de) * 2*hr[2];
       else
       S = 0;
       F = -(dw+de)*(dn+ds)/hr[2];
       B = -(dw+de)*(dn+ds)/hr[2];
       */

    if(dw != 0)
        W = -2 * hr[2] * (dn + ds) / dw;
    else
        W = 0;
    if(de != 0)
        E = -2 * hr[2] * (dn + ds) / de;
    else
        E = 0;
    if(dn != 0)
        N = -2 * hr[2] * (dw + de) / dn;
    else
        N = 0;
    if(ds != 0)
        S = -2 * hr[2] * (dw + de) / ds;
    else
        S = 0;
    F = -(dw + de) * (dn + ds) / hr[2];
    B = -(dw + de) * (dn + ds) / hr[2];

    // RHS scaleFactor for current 3D index
    // Factor 0.5 results from the SW/quadratic extrapolation
    scaleFactor = 0.5 * (dw + de) * (dn + ds) * (2 * hr[2]);

    // catch the case where a point lies on the boundary
    //FIXME: 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;
    //XXX: 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 += (2 * hr[2]) * (dn + ds) * (dw + de) / m1;
    if(dn != 0 || ds != 0)
        C += (2 * hr[2]) * (dw + de) * (dn + ds) / m2;

    //handle Neumann case
    if(z == 0 || z == nr[2] - 1) {

        if(z == 0)
            F = 0.0;
        else
            B = 0.0;

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

        scaleFactor /= 2.0;
    }
}

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

    //TODO: reverse map search?
    //double mem or O(n) search in map to get x,y,z from idx

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

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

}

void TaperDomain::getNeighbours(int idx, double &W, double &E, double &S, double &N, double &F, double &B) {

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

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

}

void TaperDomain::getNeighbours(int x, int y, int z, double &W, double &E, double &S, double &N, double &F, double &B) {

    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;

    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;
}

494
#endif //#ifdef HAVE_SAAMG_SOLVER