Commit be1563cd authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Updating radixcommand to support variant arguments.

Added radixcommand use in radixams.
parent 0699fda1
Pipeline #7403 passed with stages
in 21 minutes and 28 seconds
......@@ -9,7 +9,7 @@ ELSE()
ENDIF()
TRIBITS_PACKAGE_DEFINE_DEPENDENCIES(
LIB_REQUIRED_PACKAGES radixio radixmath radixbug
LIB_REQUIRED_PACKAGES radixcommand radixmath radixbug
LIB_OPTIONAL_TPLS ${QT_PACKAGES}
)
#include "radixmath/util.hh"
#include "radixcommand/commandline.hh"
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace radix;
int main(int argc, char **argv)
{
CommandLine command(argc, argv);
command.declareArgument("altitude"
, "Altitude at which to calcuate dose rate (0 for sea level)");
command.declareArgument("activity"
, "Activity of the area in Ci/m^2");
command.declareArgument("energy"
, "Gamma ray energy in MeV");
// expecting three arguments
if(argc != 4)
if(!command.validate(std::cerr))
{
// print usage info
std::cout << "Usage: amscli <options>" << std::endl << std::endl
<< "Options:" << std::endl
<< " altitude - Altitude at which to calculate dose rate (0 for sea level)" << std::endl
<< " activity - Activity of the area in Ci/m^2" << std::endl
<< " energy - Gamma ray energy in MeV" << std::endl;
command.help(std::cerr);
return 0;
}
// altitude above sea level - default to 0
auto z = 0.0;
z = std::stod(argv[1]);
double z = 0.0;
z = command.arg<double>(0);
// activity in Ci/m^2 of the area of interest - default to 1.767e3
auto a = 1.767e3;
a = std::stod(argv[2]);
double a = 1.767e3;
a = command.arg<double>(1);
// gamma-ray energy in MeV - default to 7e-1
auto e = 7e-1;
e = std::stod(argv[3]);
double e = 7e-1;
e = command.arg<double>(2);
// radius of area of interest (assumed to be altitude), distance
auto r = z;
auto s = std::sqrt(z*z + r*r);
// double r = z;
// double s = std::sqrt(z*z + r*r);
// sigma scalar; constant at sea level
auto sigma = 1.225;
double sigma = 1.225;
// not at sea level, update sigma
if(z != 0)
if(std::fabs(z) > 1e-16)
{
sigma = 1.911515e-16 * (z * z * z * z)
- 9.857582e-13 * (z * z * z)
......@@ -46,14 +57,14 @@ int main(int argc, char **argv)
}
// absorption/attenuation of gamma ray as it passes through air
auto mua = radix::gammaRayAbsorptionInAir(e);
// auto mua = radix::gammaRayAbsorptionInAir(e);
auto mut = radix::gammaRayAttenuationInAir(e);
// mu scalars
auto mutg = -mut * 1.225;
auto muag = -mua * 1.225;
// auto muag = -mua * 1.225;
auto mutz = -mut * sigma;
auto muaz = -mua * sigma;
// auto muaz = -mua * sigma;
// exponential integral values and their ratio
auto EIg = radix::exponentialIntegral(mutg);
......@@ -65,14 +76,18 @@ int main(int argc, char **argv)
auto DRz = DRg * EIRatio;
// report dose rate at sea level
if(z == 0)
if(std::fabs(z) < 1e-16)
{
std::cout << std::scientific << DRg << " is the Dose Rate at Ground Sea Level in R/hr" << std::endl;
std::cout << std::scientific << DRg
<< " is the Dose Rate at Ground Sea Level in R/hr"
<< std::endl;
}
// report dose rate at altitude above sea level
else
{
std::cout << std::scientific << DRz << " is the Dose Rate at your Altitude above Ground Sea Level in R/hr" << std::endl;
std::cout << std::scientific << DRz
<< " is the Dose Rate at your Altitude above Ground Sea Level in R/hr"
<< std::endl;
}
return 0;
......
......@@ -253,7 +253,7 @@ bool CommandLine::validate(std::vector<std::string> &errors) const
ss << "Missing required argument";
if((mDeclArgs.size() - mArgs.size())==1)
{
ss << " '" << mDeclArgs[mArgs.size()].first << "'" << std::endl;
ss << " '" << mDeclArgs[mArgs.size()].first << "'";
} else {
ss << "s:" << std::endl;
for(size_t i = mArgs.size(); i < mDeclArgs.size(); ++i)
......@@ -275,6 +275,17 @@ bool CommandLine::validate(std::vector<std::string> &errors) const
return true;
}
bool CommandLine::validate(std::ostream &out) const
{
std::vector<std::string> errors;
bool valid = validate(errors);
for(size_t i = 0; i < errors.size(); ++i)
{
out << errors[i] << std::endl;
}
return valid;
}
template<>
std::string CommandLine::get(const std::string& name) const
{
......@@ -309,6 +320,11 @@ std::string CommandLine::get(const std::string& name
}
return result;
}
template<>
std::string CommandLine::arg(size_t i) const
{
return mArgs[i];
}
} // namespace radix
......@@ -61,6 +61,14 @@ public:
template<typename return_type>
return_type get(const std::string& name, return_type defaultValue) const;
/**
* @brief arg retrieve argument as type \code return_type
* @param i int index of argument [0,N)
* @return \code return_type
*/
template<typename return_type>
return_type arg(size_t i) const;
/**
* @brief validate Checks for existince of required options
......@@ -68,6 +76,12 @@ public:
* @return success/failure
*/
bool validate(std::vector<std::string>& errors) const;
/**
* @brief validate Checks for existince of required options
* @param std::ostream& stream for errors
* @return success/failure
*/
bool validate(std::ostream& out) const;
private:
int mArgc;
char ** mArgv;
......
......@@ -42,4 +42,13 @@ return_type CommandLine::get(const std::string& name, return_type defaultValue)
}
return result;
}
template<typename return_type>
return_type CommandLine::arg(size_t i) const
{
return_type result;
std::stringstream ss(mArgs[i]);
ss >> result;
return result;
}
} // namespace radix
......@@ -12,68 +12,92 @@
using namespace std;
using namespace radix;
TEST(radixcommand, CommandLine)
TEST(radixcommand, mixed)
{
int fake_argc = 8;
char * fake_argv[] = {(char*)("/Users/jap/build/release/dummy"),
(char*)"-T=temp",
(char*)"-N=16",
(char*)"inputA",
(char*)"-M=machine.txt",
(char*)"-load=3.4",
(char*)"-flag",
(char*)"inputB"};
CommandLine line(fake_argc, fake_argv);
line.addOption("o"
, "Output filepath, whitespace must be escaped."
, true);
line.addOption("flag"
, "Print warnings."
, false);
line.printParsedLine(std::cout);
line.help(std::cout);
EXPECT_TRUE(line.exists("flag"));
EXPECT_EQ(16, line.get<int>("N"));
EXPECT_EQ(32, line.get("Z", 32));
EXPECT_DOUBLE_EQ(32., line.get("A", 32.));
EXPECT_ANY_THROW(line.get<int>("F"));
EXPECT_EQ("out.radix", line.get<std::string>("o", "out.radix"));
std::vector<std::string> errors;
EXPECT_FALSE(line.validate(errors));
std::cout << "Errors:" << std::endl;
for(size_t i = 0; i < errors.size(); ++i)
{
int fake_argc = 7;
char * fake_argv[] = {(char*)("/Users/jap/build/release/dummy"),
(char*)"-T=temp",
(char*)"-N=16",
(char*)"inputA",
(char*)"-M=machine.txt",
(char*)"-load=3.4",
(char*)"-flag",
(char*)"inputB"};
CommandLine line(fake_argc, fake_argv);
line.addOption("o"
, "Output filepath, whitespace must be escaped."
, true);
line.addOption("flag"
, "Print warnings."
, false);
line.printParsedLine(std::cout);
line.help(std::cout);
EXPECT_TRUE(line.exists("flag"));
EXPECT_EQ(16, line.get<int>("N"));
EXPECT_EQ(32, line.get("Z", 32));
EXPECT_DOUBLE_EQ(32., line.get("A", 32.));
EXPECT_ANY_THROW(line.get<int>("F"));
EXPECT_EQ("out.radix", line.get<std::string>("o", "out.radix"));
std::vector<std::string> errors;
EXPECT_FALSE(line.validate(errors));
std::cout << "Errors:" << std::endl;
for(size_t i = 0; i < errors.size(); ++i)
{
std::cout << "\t" <<errors[i] << std::endl;
}
std::cout << "\t" <<errors[i] << std::endl;
}
}
TEST(radixcommand, declared)
{
int fake_argc = 2;
char * fake_argv[] = {(char*)("/Users/jap/build/release/dummy"),
(char*)"-o=path/to a/file"};
CommandLine line(fake_argc, fake_argv);
line.declareArgument("input", "Path to input file.");
line.addOption("o"
, "Output filepath, whitespace must be escaped."
, true);
line.printParsedLine(std::cout);
EXPECT_EQ("path/to a/file", line.get<std::string>("o", ""));
std::vector<std::string> errors;
EXPECT_FALSE(line.validate(errors));
std::cout << "Errors:" << std::endl;
for(size_t i = 0; i < errors.size(); ++i)
{
std::cout << "\t" <<errors[i] << std::endl;
}
//test multiple missing arguments
line.declareArgument("output", "Path to output file w/o extension.");
EXPECT_FALSE(line.validate(errors));
std::cout << "Errors:" << std::endl;
for(size_t i = 0; i < errors.size(); ++i)
{
int fake_argc = 2;
char * fake_argv[] = {(char*)("/Users/jap/build/release/dummy"),
(char*)"-o=path/to a/file"};
CommandLine line(fake_argc, fake_argv);
line.declareArgument("input", "Path to input file.");
line.addOption("o"
, "Output filepath, whitespace must be escaped."
, true);
line.printParsedLine(std::cout);
EXPECT_EQ("path/to a/file", line.get<std::string>("o", ""));
std::vector<std::string> errors;
EXPECT_FALSE(line.validate(errors));
std::cout << "Errors:" << std::endl;
for(size_t i = 0; i < errors.size(); ++i)
{
std::cout << "\t" <<errors[i] << std::endl;
}
//test multiple missing arguments
line.declareArgument("output", "Path to output file w/o extension.");
EXPECT_FALSE(line.validate(errors));
std::cout << "Errors:" << std::endl;
for(size_t i = 0; i < errors.size(); ++i)
{
std::cout << "\t" <<errors[i] << std::endl;
}
line.help(std::cout);
std::cout << "\t" <<errors[i] << std::endl;
}
line.help(std::cout);
}
TEST(radixcommand, args)
{
int fake_argc = 7;
char * fake_argv[] = {(char*)("/Users/jap/build/release/dummy"),
(char*)"temp",
(char*)"16",
(char*)"inputA",
(char*)"machine.txt",
(char*)"3.4",
(char*)"input 6"};
CommandLine line(fake_argc, fake_argv);
std::string a0 = line.arg<std::string>(0);
EXPECT_EQ("temp", a0);
int a1 = line.arg<int>(1);
EXPECT_EQ(16, a1);
std::string a2 = line.arg<std::string>(2);
EXPECT_EQ("inputA", a2);
std::string a3 = line.arg<std::string>(3);
EXPECT_EQ("machine.txt", a3);
double a4 = line.arg<double>(4);
EXPECT_DOUBLE_EQ(3.4, a4);
std::string a5 = line.arg<std::string>(5);
EXPECT_EQ("input 6", a5);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment