MCresearch / deep-light

use deep learning to compute light transmission
0 stars 2 forks source link

Some code seems to be useless or inefficient #13

Open ErjieWu opened 1 year ago

ErjieWu commented 1 year ago

[1] ./light/source/Zernike.cpp

  1. The following codes seems to realize the function: when input a integer nk, we want to get the total number of species of the Zernike polynomial Z_{n}^{l} with limitation 1≤n≤nk.

    int maxZernike(const int nk)
    {
    int maxZernike = 0;
    int l = 0;
    int ni = 0;
    int m0 = 0;
    int m1 = 0;
    int m = 0;
    for (ni = 1; ni <= nk; ni++)
    {
        m0 = ni - 2 * (int)(ni / 2);
        m1 = m0 + 2 * (int)(ni / 2);
        for (m = m0; m <= m1; m = m + 2)
        {
            l = l + 1;
            if (m != 0)
                l++;
        }
    }
    return l;
    }

    However, we can easily know that the number of species of the Zernike polynomial Z_{n}^{l} for a fixed n is n+1, so the result is simply the summation from 2 to nk+1, so the code can be simplified as:

    int maxZernike(const int nk)
    {
    int l = 0;
    l = (nk+3)*nk/2;
    return l;
    }
  2. The following code doesn't be used in any other codes, and the function it realizes is weird image (If we want to tick each Zernike polynomials with Noll's sequential indices, the yellow parts should be minus value.

    void nmlznk(const int maxZnkOrder, int &maxZnkDim, int *&nznk, int *&mznk)
    {
    int maxZernike = 0;
    int l = 0;
    int ni = 0;
    int m0 = 0;
    int m1 = 0;
    int m = 0;
    // int    lz = 0;
    int j = 0;
    // int    nm = 0;
    
    for (ni = 1; ni <= maxZnkOrder; ni++)
    {
        m0 = ni - 2 * (int)(ni / 2);
        m1 = m0 + 2 * (int)(ni / 2);
        for (m = m0; m <= m1; m = m + 2)
        {
            l = l + 1;
            if (l >= 1)
            {
                nznk[l] = ni;  // zernike系数从1开始储存
                mznk[l] = m;
                // lz = lznk_a(l, ni);
                // lznk[l] = lz;
                // cout << ni << m << lz << endl;
            }
            if (m != 0)
            {
                l = l + 1;
                if (l >= 1)
                {
                    nznk[l] = ni;  // zernike系数从1开始储存
                    mznk[l] = m;
                    // lz = lznk_a(l, ni);
                    // lznk[l] = lz;
                    // nm = (ni - m) / 2;
                    // cout << ni << m << lz << endl;
                }
            }
            if (l >= maxZnkDim)
            {
                maxZnkDim = l;
                return;
            }
        }
    }
    }

    The function mnznk actually realize the function we want, i.e. tick each Zernike polynomials with OSA/ANSI standard indices. By the way, the variable maxZnkDim actually realize the function in issue 1.

  3. The following code doesn't be used in any other codes (judge whether the parities of l and [ni/2] are the same). Maybe useless

    int lznk_a(const int l, const int ni)
    {
    int lz = 0;
    if ((ni / 2) / 2 != (ni / 2) * 0.5)
    {
        if (l / 2 == l * 0.5)
        {
            lz = 0;
        }
        else
        {
            lz = 1;
        }
    }
    else
    {
        if (l / 2 == l * 0.5)
        {
            lz = 1;
        }
        else
        {
            lz = 0;
        }
    }
    return lz;
    }
  4. A question: Why do we use a homemade random number generation method instead of C++ 's built-in random number algorithm in function rdm_gauss. Here we generate a new random number with uniform distribution in range [0,1) by multiplying the seed (or old random numbers) with the number rq = pi^{8+pi}+pi and taking the decimal part of the result. Does the random number generated by this method conform to the uniform distribution and pass the autocorrelation test?

    void rdm_gauss(double &a1, double &rdmg)
    {
    double PI2 = 0.0;
    double asd = 0.0;
    double r01 = 0.0;
    double r02 = 0.0;
    double rq = 0.0;
    PI2 = 2 * PI;
    rq = pow(PI, (8 + PI)) + PI;
    
    asd = a1;
    asd = asd * rq;
    asd = asd - (int)asd;
    r01 = sqrt(-2 * log(asd));
    asd = asd * rq;
    asd = asd - (int)asd;
    r02 = PI2 * asd;
    
    rdmg = r01 * cos(r02);
    a1 = asd;
    }  

[2] _./light/source/Opticalfield.cpp

  1. Line 49. I'm confused about the physical meaning of the variable INPUT.mgs. In case we consider Gaussian beam, shouldn't it take the value 1? The value 0 seems to be a uniform beam.
    // set inIntensity
    a02 = INPUT.a0 * INPUT.a0;
    for (int i = 0; i < INPUT.n_grid; i++) {
    x = (i + 1 - INPUT.n1) * INPUT.dxy0; // +不加1
    x2 = x * x;
    for (int j = 0; j < INPUT.n_grid; j++) {
      y = (j + 1 - INPUT.n1) * INPUT.dxy0;
      y2 = y * y;
      r2 = x2 + y2;
      if (r2 <= a02) {
    -->     opt.ur0[i][j] = exp(-1 * pow(r2 / a02, INPUT.mgs));   <--
        opt.ui0[i][j] = 0;
      }
    }
    }
Tinabeezzz commented 1 year ago

[1] ./light/source/Zernike.cpp

  1. Thanks for the simplification.
    1. These two functions are really not used, because the code I got is available so I did not delete them.
  2. You can change it to c++ 's built-in random number function. Refer to the commented code in ./light/source/ optical_fiele.cpp. [2] ./light/source/Optical_field.cpp
  3. Since the error of amplitude is not considered in the current study, the initial beam is simplified to a uniform beam.