From 3b012a8086d550433188cebbf95746d2455f0b6f Mon Sep 17 00:00:00 2001 From: David Mayerich Date: Thu, 15 May 2014 12:17:37 -0500 Subject: [PATCH] added code for command-line arguments --- math/complex.h | 84 +++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------- math/quad.h | 2 +- math/spherical_bessel.h | 22 +++++++++++----------- optics/material.h | 4 +++- tools/arguments.h | 416 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 480 insertions(+), 48 deletions(-) create mode 100644 tools/arguments.h diff --git a/math/complex.h b/math/complex.h index afc79af..4fca324 100644 --- a/math/complex.h +++ b/math/complex.h @@ -143,8 +143,8 @@ struct complex } CUDA_CALLABLE complex & operator=(const T &rhs) { - this->r = rhs; - this->i = 0; + this->r = rhs; + this->i = 0; return *this; } @@ -153,34 +153,34 @@ struct complex CUDA_CALLABLE complex operator+=(const complex &rhs) { *this = *this + rhs; - return *this; + return *this; } CUDA_CALLABLE complex operator+=(const T &rhs) { *this = *this + rhs; - return *this; + return *this; } CUDA_CALLABLE complex operator*=(const complex &rhs) { *this = *this * rhs; - return *this; + return *this; } CUDA_CALLABLE complex operator*=(const T &rhs) { *this = *this * rhs; - return *this; + return *this; } //divide and assign CUDA_CALLABLE complex operator/=(const complex &rhs) { *this = *this / rhs; - return *this; + return *this; } CUDA_CALLABLE complex operator/=(const T &rhs) { *this = *this / rhs; - return *this; + return *this; } //absolute value operator (returns the absolute value of the complex number) @@ -191,23 +191,23 @@ struct complex CUDA_CALLABLE complex log() { - complex result; - result.r = std::log(std::sqrt(r * r + i * i)); - result.i = std::atan2(i, r); + complex result; + result.r = (T)std::log(std::sqrt(r * r + i * i)); + result.i = (T)std::atan2(i, r); - return result; + return result; } CUDA_CALLABLE complex exp() { - complex result; + complex result; - T e_r = std::exp(r); - result.r = e_r * std::cos(i); - result.i = e_r * std::sin(i); + T e_r = std::exp(r); + result.r = e_r * (T)std::cos(i); + result.i = e_r * (T)std::sin(i); - return result; + return result; } /*CUDA_CALLABLE complex pow(int y) @@ -218,11 +218,11 @@ struct complex CUDA_CALLABLE complex pow(T y) { - complex result; + complex result; - result = log() * y; + result = (T)log() * y; - return result.exp(); + return result.exp(); } CUDA_CALLABLE complex sqrt() @@ -275,14 +275,14 @@ struct complex template CUDA_CALLABLE static rts::complex operator+(const double a, const rts::complex b) { - return rts::complex(a + b.r, b.i); + return rts::complex((T)a + b.r, b.i); } //subtraction with a real value template CUDA_CALLABLE static rts::complex operator-(const double a, const rts::complex b) { - return rts::complex(a - b.r, -b.i); + return rts::complex((T)a - b.r, -b.i); } //minus sign @@ -303,23 +303,16 @@ CUDA_CALLABLE static rts::complex operator*(const double a, const rts::comple template CUDA_CALLABLE static rts::complex operator/(const double a, const rts::complex b) { - //return complex(a * b.r, a * b.i); rts::complex result; T denom = b.r * b.r + b.i * b.i; - result.r = (a * b.r) / denom; - result.i = -(a * b.i) / denom; + result.r = ((T)a * b.r) / denom; + result.i = -((T)a * b.i) / denom; return result; } -//POW function -/*template -CUDA_CALLABLE static complex pow(complex x, int y) -{ - return x.pow(y); -}*/ template CUDA_CALLABLE static rts::complex pow(rts::complex x, T y) @@ -374,22 +367,43 @@ CUDA_CALLABLE static T imag(rts::complex a) } //trigonometric functions +//template +/*CUDA_CALLABLE static rts::complex sinf(const rts::complex x) +{ + rts::complex result; + result.r = sinf(x.r) * coshf(x.i); + result.i = cosf(x.r) * sinhf(x.i); + + return result; +}*/ + template CUDA_CALLABLE rts::complex sin(const rts::complex x) { rts::complex result; - result.r = std::sin(x.r) * std::cosh(x.i); - result.i = std::cos(x.r) * std::sinh(x.i); + result.r = (A)std::sin(x.r) * (A)std::cosh(x.i); + result.i = (A)std::cos(x.r) * (A)std::sinh(x.i); return result; } +//floating point template +//template +/*CUDA_CALLABLE static rts::complex cosf(const rts::complex x) +{ + rts::complex result; + result.r = cosf(x.r) * coshf(x.i); + result.i = -(sinf(x.r) * sinhf(x.i)); + + return result; +}*/ + template CUDA_CALLABLE rts::complex cos(const rts::complex x) { rts::complex result; - result.r = std::cos(x.r) * std::cosh(x.i); - result.i = -(std::sin(x.r) * std::sinh(x.i)); + result.r = (A)std::cos(x.r) * (A)std::cosh(x.i); + result.i = -((A)std::sin(x.r) * (A)std::sinh(x.i)); return result; } diff --git a/math/quad.h b/math/quad.h index 8564500..ec24058 100644 --- a/math/quad.h +++ b/math/quad.h @@ -192,7 +192,7 @@ struct quad T dc = (A+Y - p).len(); T dd = (A+X+Y - p).len(); - return fmax( da, fmax(db, fmax(dc, dd) ) ); + return std::max( da, std::max(db, std::max(dc, dd) ) ); } }; diff --git a/math/spherical_bessel.h b/math/spherical_bessel.h index b052d7d..2aece36 100644 --- a/math/spherical_bessel.h +++ b/math/spherical_bessel.h @@ -5,20 +5,20 @@ namespace rts{ -#define RTS_BESSEL_CONVERGENCE_MIN 0.0145 -#define RTS_BESSEL_CONVERGENCE_MAX 0.4 -#define RTS_BESSEL_MAXIMUM_FLOAT -1e33 +#define RTS_BESSEL_CONVERGENCE_MIN 0.0145f +#define RTS_BESSEL_CONVERGENCE_MAX 0.4f +#define RTS_BESSEL_MAXIMUM_FLOAT -1e33f template CUDA_CALLABLE void sbesselj(int n, complex x, complex* j) { //compute the first bessel function if(n >= 0) - j[0] = sin(x) / x; + j[0] = (T)sin(x) / x; //compute the second bessel function if(n >= 1) - j[1] = j[0] / x - cos(x) / x; + j[1] = j[0] / x - (T)cos(x) / x; //use the recurrence relation to compute the rest for(int i = 2; i <= n; i++) @@ -40,11 +40,11 @@ CUDA_CALLABLE void sbessely(int n, complex x, complex* y) { //compute the first bessel function if(n >= 0) - y[0] = -cos(x) / x; + y[0] = -(T)cos(x) / x; //compute the second bessel function if(n >= 1) - y[1] = y[0] / x - sin(x) / x; + y[1] = y[0] / x - (T)sin(x) / x; //use the recurrence relation to compute the rest for(int i = 2; i <= n; i++) @@ -85,18 +85,18 @@ template CUDA_CALLABLE void init_sbesselj(T x, T* j) { //compute the first 2 bessel functions - j[0] = sin(x) / x; + j[0] = (T)sin(x) / x; - j[1] = j[0] / x - cos(x) / x; + j[1] = j[0] / x - (T)cos(x) / x; } template CUDA_CALLABLE void init_sbessely(T x, T* y) { //compute the first 2 bessel functions - y[0] = -cos(x) / x; + y[0] = -(T)cos(x) / x; - y[1] = y[0] / x - sin(x) / x; + y[1] = y[0] / x - (T)sin(x) / x; } template diff --git a/optics/material.h b/optics/material.h index d6a4a99..9921702 100644 --- a/optics/material.h +++ b/optics/material.h @@ -11,7 +11,7 @@ #include "rts/math/complex.h" #include "rts/math/function.h" -#define PI 3.14159 +#define PI 3.14159f namespace rts{ @@ -144,6 +144,8 @@ namespace rts{ case field_A: newRI.n.imag(_A(val * scaleA, newRI.lambda)); break; + default: + break; } } diff --git a/tools/arguments.h b/tools/arguments.h new file mode 100644 index 0000000..eec324a --- /dev/null +++ b/tools/arguments.h @@ -0,0 +1,416 @@ +#ifndef RTS_ARGUMENTS +#define RTS_ARGUMENTS + +#include +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#endif + +namespace rts{ + + class argument + { + private: + bool ansi; + + //argument name + std::string name; + + //description of the argument + std::vector desc; + + //argument values + std::vector vals; + + //range or example + std::string range; + + //flag is true when the argument is user-specified + bool flag; + + void parse_val(const std::string &s){ + + vals.clear(); + + std::stringstream ss(s); + std::string item; + while (std::getline(ss, item, ' ')) { + vals.push_back(item); + } + } + + void parse_desc(const std::string &s){ + + desc.clear(); + + std::stringstream ss(s); + std::string item; + while (std::getline(ss, item, '\n')) { + desc.push_back(item); + } + } + + public: + void set_ansi(bool b){ ansi = b; } + //create an argument with a given name, description, and default value + argument(std::string _name, std::string _desc, std::string _default = "", std::string _range = "") + { + name = _name; + parse_desc(_desc); + parse_val(_default); + + //if a default value is provided, set the flag + if(_default != "") + flag = true; + else flag = false; + + range = _range; + + + } + + int nargs() + { + return vals.size(); + } + + //return the value of a text argument + std::string as_text(int n = 0) + { + if(!flag) + { + std::cout<<"ERROR - Argument requested without being set: "< n) + return vals[n]; + + else return ""; + } + + //return the value of a floating point argument + float as_float(int n = 0) + { + if(!flag) + { + std::cout<<"ERROR - Argument requested without being set: "< n) + { + float r; + if ( ! (istringstream(vals[n]) >> r) ) r = 0; + return r; + } + + else return 0; + } + + //return the value of an integer argument + int as_int(int n = 0) + { + if(!flag) + { + std::cout<<"ERROR - Argument requested without being set: "< n) + { + int r; + if ( ! (istringstream(vals[n]) >> r) ) r = 0; + return r; + } + + else return 0; + } + + //get the width of the left column + int col_width() + { + int n = 3; + //add the length of the argument name + n += name.size(); + + //if there are any default parameters + if(vals.size() > 0) + { + //padding (parenthesis, =, etc.) + n += 6; + + //for each default argument value + for(int v=0; v args; + + //column width of the longest argument + int col_width; + + //list of sections + std::vector sections; + + public: + + arglist(){ col_width = 0; } + + void set_ansi(bool b) + { + ansi = b; + for(int i=0; i(col_width, arg.col_width()); + } + + void section(std::string _name) + { + argsection s; + s.name = _name; + s.index = args.size(); + sections.push_back(s); + } + + //output the arguments (generally in response to --help) + std::string toStr() + { + std::stringstream ss; + + int si = -1; + + if(sections.size() > 0) + si = 0; + + //for each argument + for(int a=0; a= args.size()) + i = -1; + + return i; + } + + void set(std::string _name, std::string _value) + { + int i = index(_name); + + if(i != -1) + { + args[i].set(_value); + //adjust the column width if necessary + col_width = max(col_width, args[i].col_width()); + } + else + std::cout<<"ERROR - Argument not recognized: "<<_name<