Browse Source

Initial multipole channel splitting

Konstantin Ladutenko 10 years ago
parent
commit
2289ddfe06
4 changed files with 59 additions and 38 deletions
  1. 3 3
      compare.cc
  2. 22 22
      go.sh
  3. 32 13
      nmie.cc
  4. 2 0
      nmie.h

+ 3 - 3
compare.cc

@@ -259,10 +259,10 @@ int main(int argc, char *argv[]) {
     // printf("-- C/C++ time ratio: %Lg\n", static_cast<long double>(ctime_nsec)/static_cast<long double>(cpptime_nsec));
     
     if (has_comment) {
-      printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n", comment.c_str(), Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
+      printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e  old\n", comment.c_str(), Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
       printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e  wrapper\n", comment.c_str(), Qextw, Qscaw, Qabsw, Qbkw, Qprw, gw, Albedow);
     } else {
-      printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n", Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
+      printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e  old\n", Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
       printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e  wrapper\n", Qextw, Qscaw, Qabsw, Qbkw, Qprw, gw, Albedow);
     }
     
@@ -270,7 +270,7 @@ int main(int argc, char *argv[]) {
       printf(" Theta,         S1.r,         S1.i,         S2.r,         S2.i\n");
       
       for (i = 0; i < nt; i++) {
-        printf("%6.2f, %+.5e, %+.5e, %+.5e, %+.5e\n", Theta[i]*180.0/PI, S1[i].real(), S1[i].imag(), S2[i].real(), S2[i].imag());
+        printf("%6.2f, %+.5e, %+.5e, %+.5e, %+.5e  old\n", Theta[i]*180.0/PI, S1[i].real(), S1[i].imag(), S2[i].real(), S2[i].imag());
         printf("%6.2f, %+.5e, %+.5e, %+.5e, %+.5e  wrapper\n", Theta[i]*180.0/PI, S1w[i].real(), S1w[i].imag(), S2w[i].real(), S2w[i].imag());
       }
     }

+ 22 - 22
go.sh

@@ -5,17 +5,17 @@ file=compare.cc
 echo Compile with gcc
 rm -f *.bin
 rm -f ../scattnlay
-#g++ -Ofast -std=c++11 compare.cc nmie.cc nmie-wrapper.cc -lm -lrt -o scattnlay.bin -static
-# g++ -Ofast -std=c++11 compare.cc nmie.cc nmie-wrapper.cc -lm -lrt -o scattnlay-pg.bin -static -pg
+#g++ -Ofast -std=c++11 compare.cc nmie.cc nmie-old.cc -lm -lrt -o scattnlay.bin -static
+# g++ -Ofast -std=c++11 compare.cc nmie.cc nmie-old.cc -lm -lrt -o scattnlay-pg.bin -static -pg
 
 #google profiler  ######## FAST!!!
 echo Uncomment next line to compile compare.cc
-# g++ -Ofast -std=c++11 $file nmie.cc nmie-old.cc -lm -lrt -o scattnlay.bin /usr/lib/libtcmalloc.so.4 -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -march=native -mtune=native -msse4.2
+#g++ -Ofast -std=c++11 $file nmie.cc nmie-old.cc -lm -lrt -o scattnlay.bin /usr/lib/libtcmalloc.so.4 -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -march=native -mtune=native -msse4.2
 
-#  g++ -Ofast -std=c++11 compare.cc nmie.cc nmie-wrapper.cc -lm -lrt -o scattnlay-g.bin -ltcmalloc -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -g
+#  g++ -Ofast -std=c++11 compare.cc nmie.cc nmie-old.cc -lm -lrt -o scattnlay-g.bin -ltcmalloc -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -g
 
 #DEBUG!
-#clang++ -g -O1 -fsanitize=address  -fno-optimize-sibling-calls -fno-omit-frame-pointer -std=c++11 compare.cc nmie.cc nmie-wrapper.cc -lm -lrt -o scattnlay.bin
+clang++ -g -O1 -fsanitize=address  -fno-optimize-sibling-calls -fno-omit-frame-pointer -std=c++11 compare.cc nmie.cc nmie-old.cc -lm -lrt -o scattnlay.bin
 
 cp scattnlay.bin ../scattnlay
 # cp scattnlay-g.bin ../scattnlay-g
@@ -29,20 +29,20 @@ cd tests/shell
 
 PROGRAM='../../../scattnlay'
 time  ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer-3.4  $PROGRAM -l 5 0.4642 1.8000 1.7000 0.7114 0.8000 0.7000 0.7393 1.2000 0.0900 0.9168 2.8000 0.2000 1.0000 1.5000 0.4000  -t 0.0 90.0 5 -c test01
-#result
-# test01, +1.41154e+00, +4.17695e-01, +9.93844e-01, +1.59427e-01, +1.25809e+00, +3.67376e-01, +2.95915e-01
-
-echo BUG -- All designs should give almost the same answer
-#echo
-echo $PROGRAM -l 1 4.71238898038469 2 0.0001
-$PROGRAM -l 1 4.71238898038469 2 0.0001
-#echo
-echo $PROGRAM -l 2 4.71238898038469 2 0.0001 9.42477796076937 1 0
-$PROGRAM -l 2 4.71238898038469 2 0.0001 9.42477796076937 1.5 0.0001
+# #result
+# # test01, +1.41154e+00, +4.17695e-01, +9.93844e-01, +1.59427e-01, +1.25809e+00, +3.67376e-01, +2.95915e-01
+
+# echo BUG -- All designs should give almost the same answer
+# #echo
+# echo $PROGRAM -l 1 4.71238898038469 2 0.0001
+# $PROGRAM -l 1 4.71238898038469 2 0.0001
+# #echo
+# echo $PROGRAM -l 2 4.71238898038469 2 0.0001 9.42477796076937 1 0
+# $PROGRAM -l 2 4.71238898038469 2 0.0001 9.42477796076937 1.5 0.0001
 #echo
 #echo $PROGRAM -l 2 4.71238898038469 2 0.0001 9.42477796076938 1 0
 #$PROGRAM -l 2 4.71238898038469 2 0.0001 9.42477796076938 1.5 0.0001
-echo
+#echo
 #  #apt-get install oprofile
 # echo oprofile
 # PROGRAM='../../../scattnlay-g'
@@ -84,11 +84,11 @@ echo
 # echo      tar -zcvf cov-int.tar.gz cov-int
 # echo to submit to coverity
 
-cd $path
-file=test-negative-epsilon.cc
-#g++ -Ofast -std=c++11 $file nmie.cc -lm -lrt -o $file.bin /usr/lib/libtcmalloc.so.4 -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -march=native -mtune=native -msse4.2
+# cd $path
+# file=test-negative-epsilon.cc
+# #g++ -Ofast -std=c++11 $file nmie.cc -lm -lrt -o $file.bin /usr/lib/libtcmalloc.so.4 -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -march=native -mtune=native -msse4.2
 
-#DEBUG!
-clang++ -g -O1 -fsanitize=address  -fno-optimize-sibling-calls -fno-omit-frame-pointer -std=c++11 $file nmie.cc -lm -lrt -o $file.bin
+# #DEBUG!
+# clang++ -g -O1 -fsanitize=address  -fno-optimize-sibling-calls -fno-omit-frame-pointer -std=c++11 $file nmie.cc -lm -lrt -o $file.bin
 
-./$file.bin
+# ./$file.bin

+ 32 - 13
nmie.cc

@@ -983,6 +983,16 @@ c    MM       + 1  and - 1, alternately
     Qpr_ = 0;
     asymmetry_factor_ = 0;
     albedo_ = 0;
+    Qsca_ch_.clear();
+    Qext_ch_.clear();
+    Qabs_ch_.clear();
+    Qbk_ch_.clear();
+    Qpr_ch_.clear();
+    Qsca_ch_.resize(nmax_-1);
+    Qext_ch_.resize(nmax_-1);
+    Qabs_ch_.resize(nmax_-1);
+    Qbk_ch_.resize(nmax_-1);
+    Qpr_ch_.resize(nmax_-1);
     // Initialize the scattering amplitudes
     std::vector<std::complex<double> >	tmp1(theta_.size(),std::complex<double>(0.0, 0.0));
     S1_.swap(tmp1);
@@ -1038,7 +1048,6 @@ c    MM       + 1  and - 1, alternately
     if (size_parameter_.size() == 0)
       throw std::invalid_argument("Initialize model first!");
     std::vector<std::complex<double> > an, bn;
-    std::complex<double> Qbktmp(0.0, 0.0);
     const std::vector<double>& x = size_parameter_;
     // Calculate scattering coefficients
     ScattCoeffs(an, bn);
@@ -1051,28 +1060,33 @@ c    MM       + 1  and - 1, alternately
       Pi[i].resize(theta_.size());
       Tau[i].resize(theta_.size());
     }
-    calcPiTau(Pi, Tau);
-    InitMieCalculations();
-
+    calcPiTau(Pi, Tau);    
+    InitMieCalculations(); //
+    std::complex<double> Qbktmp(0.0, 0.0);
+    std::vector< std::complex<double> > Qbktmp_ch(nmax_ - 1, Qbktmp);
     // By using downward recurrence we avoid loss of precision due to float rounding errors
     // See: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
     //      http://en.wikipedia.org/wiki/Loss_of_significance
     for (int i = nmax_ - 2; i >= 0; i--) {
-      int n = i + 1;
+      const int n = i + 1;
       // Equation (27)
-      Qext_ += (n + n + 1)*(an[i].real() + bn[i].real());
+      Qext_ch_[i] = (n + n + 1)*(an[i].real() + bn[i].real());
+      Qext_ += Qext_ch_[i];
       // Equation (28)
-      Qsca_ += (n + n + 1)*(an[i].real()*an[i].real() + an[i].imag()*an[i].imag()
+      Qsca_ch_[i] += (n + n + 1)*(an[i].real()*an[i].real() + an[i].imag()*an[i].imag()
 			    + bn[i].real()*bn[i].real() + bn[i].imag()*bn[i].imag());
+      Qsca_ += Qsca_ch_[i];
       // Equation (29) TODO We must check carefully this equation. If we
       // remove the typecast to double then the result changes. Which is
       // the correct one??? Ovidio (2014/12/10) With cast ratio will
       // give double, without cast (n + n + 1)/(n*(n + 1)) will be
       // rounded to integer. Tig (2015/02/24)
-      Qpr_ += ((n*(n + 2)/(n + 1))*((an[i]*std::conj(an[n]) + bn[i]*std::conj(bn[n])).real())
+      Qpr_ch_[i]=((n*(n + 2)/(n + 1))*((an[i]*std::conj(an[n]) + bn[i]*std::conj(bn[n])).real())
 	       + ((double)(n + n + 1)/(n*(n + 1)))*(an[i]*std::conj(bn[i])).real());
-      // Equation (33)
-      Qbktmp = Qbktmp + (double)(n + n + 1)*(1 - 2*(n % 2))*(an[i]- bn[i]);
+      Qpr_ += Qpr_ch_[i];
+      // Equation (33)      
+      Qbktmp_ch[i] = (double)(n + n + 1)*(1 - 2*(n % 2))*(an[i]- bn[i]);
+      Qbktmp += Qbktmp_ch[i];
       // Calculate the scattering amplitudes (S1 and S2)    //
       // Equations (25a) - (25b)                            //
       for (int t = 0; t < theta_.size(); t++) {
@@ -1081,11 +1095,16 @@ c    MM       + 1  and - 1, alternately
       }
     }
     double x2 = pow2(x.back());
-    Qext_ = 2*(Qext_)/x2;                                 // Equation (27)
-    Qsca_ = 2*(Qsca_)/x2;                                 // Equation (28)
-    Qpr_ = Qext_ - 4*(Qpr_)/x2;                           // Equation (29)
+    Qext_ = 2.0*(Qext_)/x2;                                 // Equation (27)
+    for (double& Q : Qext_ch_) Q = 2.0*Q/x2;
+    Qsca_ = 2.0*(Qsca_)/x2;                                 // Equation (28)
+    for (double& Q : Qsca_ch_) Q = 2.0*Q/x2;
+    Qpr_ = Qext_ - 4.0*(Qpr_)/x2;                           // Equation (29)
+    for (int i = 0; i < nmax_ - 1; ++i) Qpr_ch_[i] = Qext_ch_[i] - 4.0*Qpr_ch_[i]/x2;
 
     Qabs_ = Qext_ - Qsca_;                                // Equation (30)
+    for (int i = 0; i < nmax_ - 1; ++i) Qabs_ch_[i] = Qext_ch_[i] - Qsca_ch_[i];
+    
     albedo_ = Qsca_ / Qext_;                              // Equation (31)
     asymmetry_factor_ = (Qext_ - Qpr_) / Qsca_;                          // Equation (32)
 

+ 2 - 0
nmie.h

@@ -226,6 +226,8 @@ namespace nmie {
     int nmax_preset_ = -1;
     /// Store result
     double Qsca_ = 0.0, Qext_ = 0.0, Qabs_ = 0.0, Qbk_ = 0.0, Qpr_ = 0.0, asymmetry_factor_ = 0.0, albedo_ = 0.0;
+    // Mie efficinecy from each multipole channel.
+    std::vector<double> Qsca_ch_, Qext_ch_, Qabs_ch_, Qbk_ch_, Qpr_ch_;
     std::vector<std::complex<double> > S1_, S2_;
 
     //Used constants