Distribution.h 8.79 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#ifndef OPAL_Distribution_HH
#define OPAL_Distribution_HH

// ------------------------------------------------------------------------
// $RCSfile: Distribution.h,v $
// ------------------------------------------------------------------------
// $Revision: 1.1.1.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class: Distribution
//
// ------------------------------------------------------------------------
//
// $Date: 2000/03/27 09:33:44 $
// $Author: Andreas Adelmann $
//
// ------------------------------------------------------------------------
#include <iosfwd>
#include <fstream>
#include <string>
#include <map>

#include <gsl/gsl_rng.h>
#include <gsl/gsl_histogram.h>
#include <gsl/gsl_qrng.h>
28 29 30 31 32 33

#include "H5hut.h"

#include "AbstractObjects/Definition.h"
#include "Algorithms/PartData.h"
#include "ranlib.h"
34
#include "Algorithms/Vektor.h"
gsell's avatar
gsell committed
35 36

#define RANLIBX
37 38 39 40 41 42 43
#define sqr(x) x*x

class PartBunch;
class PartBins;
class EnvelopeBunch;
class BoundaryGeometry;
class LaserProfile;
gsell's avatar
gsell committed
44

adelmann's avatar
cleanup  
adelmann committed
45 46
enum DistrTypeT {NODIST,
                 GAUSS,
gsell's avatar
gsell committed
47 48
                 FROMFILE,
                 GUNGAUSSFLATTOPTH,
adelmann's avatar
cleanup  
adelmann committed
49
                 BINOMIAL,
gsell's avatar
gsell committed
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 78 79
                 SURFACEEMISSION,
                 SURFACERANDCREATE,
                 ASTRAFLATTOPTH
                };

// Class Distribution
// ------------------------------------------------------------------------
/// The Distribution definition.
//
//  A Distribution definition is used by most physics commands to define the
//  beam distribution. This includes particle charge, reference momentum, beam size,
//  beam momentum spread etc.

class Distribution: public Definition {

    //===============
    // Class methods.
    //===============
public:

    /// Exemplar constructor.
    Distribution();

    virtual ~Distribution();

    /// Test if replacement is allowed.
    //  Can replace only by another Distribution.
    virtual bool canReplaceBy(Object *object);

    /// Make clone.
80
    virtual Distribution *clone(const std::string &name);
gsell's avatar
gsell committed
81 82 83 84 85

    /// Check the Distribution data.
    virtual void execute();

    /// Find named Distribution.
86
    static Distribution *find(const std::string &name);
gsell's avatar
gsell committed
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 124 125 126 127 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

    /// Return the embedded CLASSIC PartData.
    const PartData &getReference() const;

    /// Update the Distribution data.
    virtual void update();

    /// configure rng generator such that calls to sample
    /// gives the random values
    void setup(PartBunch &beam, size_t Np, bool scan);

    /**
     * Add a list of distributions. This takes a list
     * of distributions and adds them together. Currently
     * this only works for the GUNGAUSSFLATTOPTH distribution.
     * Also, the transverse properties of the beam are defined only
     * by the first distribution in the list. For subsequent
     * distributions, only the longitudinal properties are used.
     */
    bool addDistributions(PartBunch &beam, std::vector<Distribution *> distributions, size_t numberOfParticles);

    /// gets back (x,p)
    std::pair<Vector_t, Vector_t> sample(double dt, int binNumber);

    /// gets back (x,p)
    std::pair<Vector_t, Vector_t> sampleNEW(double dt, int binNumber);

    void create(PartBunch &p, size_t Np);
    void create(PartBunch &p, size_t Np, bool scan);

    void create(PartBunch &p, BoundaryGeometry &bg);

    void createPriPart(PartBunch *beam, BoundaryGeometry &bg);

    void createSlicedBunch(int sl, double charge, double gamma, double mass, double current, double center, double Bz0, EnvelopeBunch *p);

    void doRestart(PartBunch &p, size_t Np, int restartStep);

    void doRestart_cycl(PartBunch &p, size_t Np, int restartStep, const int specifiedNumBunch);

    void doRestartEnvelope(EnvelopeBunch &p, size_t Np, int restartStep);

    Inform &printInfo(Inform &os) const;

    double getTBin() { return tBin_m; }
    double getTEmission();

    double getEkin() const;
    double getLaserEnergy() const;
    double getWorkFunctionRf() const;

    size_t getNumberOfDarkCurrentParticles();
    double getDarkCurrentParticlesInwardMargin();
    double getEInitThreshold();

    double getWorkFunction();
    double getFieldEnhancement();
    size_t getMaxFNemissionPartPerTri();
    double getFieldFNThreshold();
    double getFNParameterA();
    double getFNParameterB();
    double getFNParameterY();
    double getFNParameterVYZero();
    double getFNParameterVYSecond();
    int    getSecondaryEmissionFlag();
    bool   getEmissionMode() ;
153
    std::string getTypeofDistribution();
gsell's avatar
gsell committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

    double getvSeyZero();//return sey_0 in Vaughan's model
    double getvEZero();//return the energy related to sey_0 in Vaughan's model
    double getvSeyMax();//return sey max in Vaughan's model
    double getvEmax();//return Emax in Vaughan's model
    double getvKenergy();//return fitting parameter denotes the roughness of surface for impact energy in Vaughan's model
    double getvKtheta();//return fitting parameter denotes the roughness of surface for impact angle in Vaughan's model
    double getvVThermal();//return thermal velocity of Maxwellian distribution of secondaries in Vaughan's model
    double getVw();//return velocity scalar for parallel plate benchmark;
    int getSurfMaterial();//material type for Furman-Pivi's model 0 for copper, 1 for stainless steel

private:
    void sampleGauss(PartBunch &beam, size_t Np);

    void createTimeBins(const int Np);

    void createBinom(Vector_t emit, Vector_t alpha, Vector_t beta, Vector_t gamma,
                     Vector_t bincoef, PartBunch &beam, size_t particles,
                     bool isBinned);

174
    void binnDistribution(PartBunch &beam, size_t Np, std::string distType);
gsell's avatar
gsell committed
175

176
    void binnDistributionFromFile(PartBunch &beam, const std::string fn);
gsell's avatar
gsell committed
177 178 179 180 181 182 183 184 185 186

    double eVtoBetaGamma(const double &valueIneV, const double &mass) {
        double tmp = 1. + valueIneV / mass;
        return sqrt(tmp * tmp - 1.);
    }

    double betaGammatoeV(const double &valueInbega, const double &mass) {
        return (sqrt(valueInbega * valueInbega + 1.) - 1.) * mass ;
    }

187 188 189 190
    void writeToFile();



gsell's avatar
gsell committed
191 192 193 194 195
    // Not implemented.
    Distribution(const Distribution &);
    void operator=(const Distribution &);

    // Clone constructor.
196
    Distribution(const std::string &name, Distribution *parent);
gsell's avatar
gsell committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

    //===============
    // Class members.
    //===============
private:

    // The particle reference data.
    PartData reference;

    // The structure for particle binning
    PartBins *pbin_m;

    // If we scan we already have a particles and to not have to create them anymore
    bool scan_m;


    //`setup() fills the following variables

215
    std::string distT_m;
gsell's avatar
gsell committed
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
    DistrTypeT distrTypeT_m;   // will replace distT_m

    double corr_m[7];

    Vector_t sigx_m;
    Vector_t sigp_m;

    Vector_t binc_m;

    Vector_t emit_m;
    Vector_t alpha_m;
    Vector_t beta_m;
    Vector_t gamma_m;

    double gauss_offset_m[2];

    double avrgpt_m;
    double avrgt_m;

    int nBins_m; // Number of energy bins the distribution is broken up into.
    int sBins_m; // Number of samples used to create the time histogram of each energy bin.
    gsl_rng *rn_m;
    gsl_qrng *R_m;

    /// this is for the transverse dimension
    gsl_qrng *qrng_m;

    RANLIB_class *rGen_m;

    /// time binned distribution with thermal energy
    double transvCutOff_m;
    double tEmission_m;
    double tPulseLengthFWHM_m;
    double tRise_m;
    double tFall_m;
    double sigmaRise_m;
    double sigmaFall_m;
    double cutoff_m;
    double tBin_m;

    gsl_histogram *h_m;
    double *distributionTable_m;

    bool   astraMode_m;
    double workf_m;          // eV
    double siglaser_m;      // m
    double elaser_m;        // eV
    double fe_m;            // Fermi energy eV
    double ag_m;            // Acceleration gradient eV/m
    double ekin_m;          // eV
    double phimax_m;        // rad
    double schottky_m;;      // eV
    double ptot_m;          // beta gamma
269

gsell's avatar
gsell committed
270 271 272 273 274
    std::ofstream os_m;

    /**
       Data for Laser Profile read in
    */
275 276
    std::string laserProfileFn_m;
    std::string laserImage_m;
gsell's avatar
gsell committed
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
    double intensityCut_m;

    LaserProfile *lp_m;


    /**
       Data for Dark Current calculations
    */
    size_t darkCurrentParts_m;
    /**
       Data for Dark Current initialized positions. A little bit inward along the triangle normal, positive: inside the geometry.
    */
    double darkInwardMargin_m;

    double eInitThreshold_m;

    double workFunction_m;

    double fieldEnhancement_m;

    double fieldThrFN_m;

    size_t maxFN_m;

    double paraFNA_m;

    double paraFNB_m;

    double paraFNY_m;

    double paraFNVYSe_m;

    double paraFNVYZe_m;

    int    secondaryFlag_m;

    double ppVw_m;

    double vVThermal_m;



};

inline Inform &operator<<(Inform &os, const Distribution &d) {
    return d.printInfo(os);
}

#endif // OPAL_Distribution_HH