123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- #include <algorithm>
- #include <complex>
- #include <functional>
- #include <iostream>
- #include <stdexcept>
- #include <string>
- #include <vector>
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include <string.h>
- #include <google/heap-profiler.h>
- #include "nmie.h"
- #include "nmie-wrapper.h"
- timespec diff(timespec start, timespec end);
- const double PI=3.14159265358979323846;
- int main(int argc, char *argv[]) {
- try {
- std::vector<std::string> args;
- args.assign(argv, argv + argc);
- std::string error_msg(std::string("Insufficient parameters.\nUsage: ") + args[0]
- + " -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] "
- + "[-t ti tf nt] [-c comment]\n");
- enum mode_states {read_L, read_x, read_mr, read_mi, read_ti, read_tf, read_nt, read_comment};
-
- std::string comment;
- int has_comment = 0;
- int i, l, L = 0;
- std::vector<double> x, Theta;
- std::vector<std::complex<double> > m, S1, S2;
- double Qext, Qabs, Qsca, Qbk, Qpr, g, Albedo;
- std::vector<std::complex<double> > mw, S1w, S2w;
- double Qextw, Qabsw, Qscaw, Qbkw, Qprw, gw, Albedow;
- double ti = 0.0, tf = 90.0;
- int nt = 0;
- if (argc < 5) throw std::invalid_argument(error_msg);
-
-
-
- int mode = -1;
- double tmp_mr;
- for (auto arg : args) {
-
-
-
-
- if (arg == "-l") {
- mode = read_L;
- continue;
- }
- if (arg == "-t") {
- if ((mode != read_x) && (mode != read_comment))
- throw std::invalid_argument(std::string("Unfinished layer!\n")
- +error_msg);
- mode = read_ti;
- continue;
- }
- if (arg == "-c") {
- if ((mode != read_x) && (mode != read_nt))
- throw std::invalid_argument(std::string("Unfinished layer or theta!\n") + error_msg);
- mode = read_comment;
- continue;
- }
-
-
- if (mode == read_L) {
- L = std::stoi(arg);
- mode = read_x;
- continue;
- }
- if (mode == read_x) {
- x.push_back(std::stod(arg));
- mode = read_mr;
- continue;
- }
- if (mode == read_mr) {
- tmp_mr = std::stod(arg);
- mode = read_mi;
- continue;
- }
- if (mode == read_mi) {
- m.push_back(std::complex<double>( tmp_mr,std::stod(arg) ));
- mode = read_x;
- continue;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (mode == read_ti) {
- ti = std::stod(arg);
- mode = read_tf;
- continue;
- }
- if (mode == read_tf) {
- tf = std::stod(arg);
- mode = read_nt;
- continue;
- }
- if (mode == read_nt) {
- nt = std::stoi(arg);
- Theta.resize(nt);
- S1.resize(nt);
- S2.resize(nt);
- S1w.resize(nt);
- S2w.resize(nt);
- continue;
- }
-
-
-
-
-
-
-
-
-
-
- if (mode == read_comment) {
- comment = arg;
- has_comment = 1;
- continue;
- }
-
-
-
-
-
-
- }
- if ( (x.size() != m.size()) || (L != x.size()) )
- throw std::invalid_argument(std::string("Broken structure!\n")
- +error_msg);
- if ( (0 == m.size()) || ( 0 == x.size()) )
- throw std::invalid_argument(std::string("Empty structure!\n")
- +error_msg);
-
- if (nt < 0) {
- printf("Error reading Theta.\n");
- return -1;
- } else if (nt == 1) {
- Theta[0] = ti*PI/180.0;
- } else {
- for (i = 0; i < nt; i++) {
- Theta[i] = (ti + (double)i*(tf - ti)/(nt - 1))*PI/180.0;
- }
- }
- timespec time1, time2;
- long cpptime_nsec, best_cpp;
- long ctime_nsec, best_c;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- nMie(L, x, m, nt, Theta, &Qext, &Qsca, &Qabs, &Qbk, &Qpr, &g, &Albedo, S1, S2);
-
-
-
-
- nmie::nMie_wrapper(L, x, m, nt, Theta, &Qextw, &Qscaw, &Qabsw, &Qbkw, &Qprw, &gw, &Albedow, S1w, S2w);
- printf("\n");
-
-
-
-
-
- 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 wrapper\n", comment.c_str(), Qextw, Qscaw, Qabsw, Qbkw, Qprw, gw, Albedow);
- } else {
-
- printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e wrapper\n", Qextw, Qscaw, Qabsw, Qbkw, Qprw, gw, Albedow);
- }
-
- if (nt > 0) {
- 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 wrapper\n", Theta[i]*180.0/PI, S1w[i].real(), S1w[i].imag(), S2w[i].real(), S2w[i].imag());
- }
- }
- } catch( const std::invalid_argument& ia ) {
-
- std::cerr << "Invalid argument: " << ia.what() << std::endl;
- return -1;
- }
- return 0;
- }
- timespec diff(timespec start, timespec end)
- {
- timespec temp;
- if ((end.tv_nsec-start.tv_nsec)<0) {
- temp.tv_sec = end.tv_sec-start.tv_sec-1;
- temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
- } else {
- temp.tv_sec = end.tv_sec-start.tv_sec;
- temp.tv_nsec = end.tv_nsec-start.tv_nsec;
- }
- return temp;
- }
|