BoxCornerDomain.h 7.24 KB
Newer Older
1 2 3 4
//
// Class BoxCornerDomain
//   :FIXME: add brief description
//
frey_m's avatar
frey_m committed
5 6 7
// Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich,
//               2013 - 2015, Tülin Kaman, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
8
//
frey_m's avatar
frey_m committed
9
// Implemented as part of the PhD thesis
frey_m's avatar
frey_m committed
10
// "Toward massively parallel multi-objective optimization with application to
frey_m's avatar
frey_m committed
11 12 13 14 15 16 17 18 19 20 21
// particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
22
//
gsell's avatar
gsell committed
23
#ifndef BOXCORNER_DOMAIN_H
24
#define BOXCORNER_DOMAIN_H
gsell's avatar
gsell committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38

#include <map>
#include <string>
#include <cmath>
#include <iostream>  // Neeeded for stream I/O
#include <fstream>   // Needed for file I/O
#include "IrregularDomain.h"


/*

    A_m and B_m are the half apperture of the box


kraus's avatar
kraus committed
39
                                     / (A_m,B_m)
gsell's avatar
gsell committed
40 41 42 43
                                    /
                                   /
                                  /
    L1_m                         /
kraus's avatar
kraus committed
44
------------      --------------+ (-A_m,B_m)
gsell's avatar
gsell committed
45
           | L2_m |             |
kraus's avatar
kraus committed
46
        C_m|      |             |
gsell's avatar
gsell committed
47
           |------|             |      /
kraus's avatar
kraus committed
48
         .....                  |     /
gsell's avatar
gsell committed
49 50 51
(0,0)---.......-----------------+    /
         .....                  |   /
   z                            |  /
kraus's avatar
kraus committed
52
   |                            | /
gsell's avatar
gsell committed
53 54
--------------------------------+/ (-A_m,-B_m)

55
            Length_m
gsell's avatar
gsell committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

Test in which of the 3 parts of the geometry we are in.

    if((z < L1_m) || (z > (L1_m + L2_m)))
        b = B_m;
    else
        b = B_m-C_m;

*/

class BoxCornerDomain : public IrregularDomain {

public:

    BoxCornerDomain(Vector_t nr, Vector_t hr);
71
    BoxCornerDomain(double A, double B, double C, double Length, double L1, double L2, Vector_t nr, Vector_t hr,
gsell's avatar
gsell committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
                    std::string interpl);
    ~BoxCornerDomain();

    /// returns discretization at (x,y,z)
    void getBoundaryStencil(int x, int y, int z, double &W, double &E, double &S, double &N, double &F, double &B,
                            double &C, double &scaleFactor);

    /// returns discretization at 3D index
    void getBoundaryStencil(int idx, double &W, double &E, double &S, double &N, double &F, double &B, double &C,
                            double &scaleFactor);

    /// returns index of neighbours at (x,y,z)
    void getNeighbours(int x, int y, int z, int &W, int &E, int &S, int &N, int &F, int &B);

    /// returns index of neighbours at 3D index
    void getNeighbours(int idx, int &W, int &E, int &S, int &N, int &F, int &B);

    /// returns type of boundary condition
    std::string getType() {return "BoxCorner";}


    /// we do not need this
94
    int getNumXY(int /*z*/) { return -1;}
gsell's avatar
gsell committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117


    /// as a function of z, determine the hight (B) of the geometry
    inline double getB(double z) {
      if((z < L1_m) || (z > (L1_m + L2_m)))
            return B_m;
        else
            return B_m - C_m;
    }

    /// queries if a given (x,y,z) coordinate lies inside the domain
    inline bool isInside(int x, int y, int z) {
        const double xx = (x - (nr[0] - 1) / 2.0) * hr[0];
        const double yy = (y - (nr[1] - 1) / 2.0) * hr[1];
        const double b = getB(z * hr[2]);
        return (xx < A_m && yy < b && z != 0 && z != nr[2] - 1);
    }

    /// set semi-minor
    //void setSemiMinor(double sm) {SemiMinor = sm;}
    /// set semi-major
    //void setSemiMajor(double sm) {SemiMajor = sm;}

118 119
    void compute(Vector_t hr);
    void compute(Vector_t hr, NDIndex<3> localId);
gsell's avatar
gsell committed
120 121 122 123 124

    double getXRangeMin() { return -A_m; }
    double getXRangeMax() { return  A_m; }
    double getYRangeMin() { return  -B_m;} // actBMin_m; }
    double getYRangeMax() { return  B_m; } // actBMax_m; }
125
    double getZRangeMin() { return  L1_m;}
kraus's avatar
kraus committed
126
    double getZRangeMax() { return  L1_m+L2_m; }
127

gsell's avatar
gsell committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

    //TODO: ?
    int getStartIdx() {return 0;}

    bool hasGeometryChanged() { return hasGeometryChanged_m; }

private:

    //XXX: since the Y coorindate is dependent on the Z value we need (int,
    //int) -> intersection. To simplify things (for now) we use the same
    //structure for X...
    /// Map from a ([(x or y], z) to a list of intersection values with
    /// boundary.
    typedef std::multimap< std::pair<int, int>, double > BoxCornerPointList;

    /// all intersection points with grid lines in X direction
    BoxCornerPointList IntersectXDir;

    /// all intersection points with grid lines in Y direction
    BoxCornerPointList IntersectYDir;

    /// mapping (x,y,z) -> idx
    std::map<int, int> IdxMap;

    /// mapping idx -> (x,y,z)
    std::map<int, int> CoordMap;

    /// depth of the box
    double A_m;

    /// the maximal hight of the box
    double B_m;

kraus's avatar
kraus committed
161

gsell's avatar
gsell committed
162 163 164 165 166 167 168 169 170
    /// because the geometry can change in the y direction
    double actBMin_m;

    double actBMax_m;

    /// hight of the corner
    double C_m;

    /// lenght of the structure
171
    double Length_m;
gsell's avatar
gsell committed
172 173 174 175 176 177 178 179

    /// lenght of the first part of the structure
    double L1_m;

    /// lenght of the corner
    double L2_m;

    /// semi-major of the ellipse
180 181
    // :FIXME: unused
    //double SemiMajor;
gsell's avatar
gsell committed
182
    /// semi-minor of the ellipse
183 184
    // :FIXME: unused
    //double SemiMinor;
gsell's avatar
gsell committed
185 186 187 188 189 190 191 192 193 194 195 196 197

    /// interpolation type
    int interpolationMethod;
    /// flag indicating if geometry has changed for the current time-step
    bool hasGeometryChanged_m;

    /// for debug reasons
   std::ofstream os_m;





198
    inline double getXIntersection(double cx, int /*z*/) {
gsell's avatar
gsell committed
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        if(cx < 0)
            return -A_m;
        else
            return  A_m;
    }

    inline double getYIntersection(double cy, int z) {
        if(cy < 0)
            return -B_m;
        else
            return getB(z * hr[2]);
    }

    /// conversion from (x,y,z) to index in xyz plane
    inline int 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*/
    inline int 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 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;

    }

    /// different interpolation methods for boundary points
240 241 242
    void constantInterpolation(int x, int y, int z, double &W, double &E, double &S, double &N, double &F, double &B, double &C, double &scaleFactor);
    void linearInterpolation(int x, int y, int z, double &W, double &E, double &S, double &N, double &F, double &B, double &C, double &scaleFactor);
    void 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
243 244 245

};

246 247 248 249 250 251 252 253 254
#endif

// vi: set et ts=4 sw=4 sts=4:
// Local Variables:
// mode:c
// c-basic-offset: 4
// indent-tabs-mode: nil
// require-final-newline: nil
// End: