Commit f17d735f authored by Guillaume Anciaux's avatar Guillaume Anciaux
Browse files

Merge branch 'master' of ssh://c4science.ch/source/sp4e into master

parents 5b3d7d30 516a7120
cmake_minimum_required(VERSION 2.6)
set(CMAKE_CXX_STANDARD 14)
add_subdirectory(src)
# Instructions to compile the code
Compilation:
mkdir build
cd build
ccmake ../
make
# Instructions to run the program:
In the build/src directory:
In order to get the help:
./compute_series --help
The arguments that can be provided are the following :
--series_type : pi or arithmetic (Default pi)
--dumper_type : print or write (Default print)
--output : cout or filename (Default cout)
--freq : int, output frequency of the serie value. (Default 10)
--maxiter: int, maximal series iteration. (Default 1000)
--precision: number of digits to use (Default 5)
--delimiter: separator to use for produced files (Default " ")
In order to run the program, at least one argument has to be provided.
./compute_series --series_type arithmetic --dumper_type write
\ No newline at end of file
add_executable(compute_series
main.cc
series.cc
compute_pi.cc
compute_arithmetic.cc
print_series.cc
write_series.cc
)
#include "compute_arithmetic.hh"
/* -------------------------------------------------------------------------- */
ComputeArithmetic::ComputeArithmetic() {
this->func = [](UInt i) { return 1. * i; };
}
/* -------------------------------------------------------------------------- */
double ComputeArithmetic::getAnalyticPrediction() {
return 1. * this->current_term * (this->current_term + 1) / 2.;
}
#ifndef __COMPUTE_ALGEBRAIC_HH__
#define __COMPUTE_ALGEBRAIC_HH__
/* -------------------------------------------------------------------------- */
#include "series.hh"
/* -------------------------------------------------------------------------- */
class ComputeArithmetic : public Series {
public:
ComputeArithmetic();
double getAnalyticPrediction() override;
};
#endif /* __COMPUTE_ALGEBRAIC_HH__ */
#include "compute_pi.hh"
#include <cmath>
/* -------------------------------------------------------------------------- */
ComputePi::ComputePi() {
func = [](UInt i) { return 1. / (1. * i * i); };
}
/* -------------------------------------------------------------------------- */
double ComputePi::compute(UInt N) {
Series::compute(N);
return sqrt(6. * current_value);
}
/* -------------------------------------------------------------------------- */
double ComputePi::getAnalyticPrediction() { return M_PI; }
/* -------------------------------------------------------------------------- */
#ifndef __COMPUTE_PI_HH__
#define __COMPUTE_PI_HH__
/* -------------------------------------------------------------------------- */
#include "series.hh"
/* -------------------------------------------------------------------------- */
class ComputePi : public Series {
public:
ComputePi();
double compute(UInt N) override;
double getAnalyticPrediction() override;
};
#endif /* __COMPUTE_PI_HH__ */
#ifndef __DUMPER_SERIES_HH__
#define __DUMPER_SERIES_HH__
/* -------------------------------------------------------------------------- */
#include "series.hh"
#include <iomanip>
/* -------------------------------------------------------------------------- */
class DumperSeries {
public:
DumperSeries(Series &series) : series(series){};
virtual void dump(std::ostream &os) = 0;
void setPrecision(UInt precision) { this->precision = precision; }
void setMaxIter(UInt maxiter) { this->maxiter = maxiter; }
protected:
Series &series;
UInt precision = 4;
UInt maxiter = 100;
};
inline std::ostream &operator<<(std::ostream &os, DumperSeries &dumper) {
dumper.dump(os);
return os;
}
#endif /* __DUMPER_SERIES_HH__ */
/* -------------------------------------------------------------------------- */
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
/* -------------------------------------------------------------------------- */
#include "compute_arithmetic.hh"
#include "compute_pi.hh"
#include "main.hh"
#include "print_series.hh"
#include "write_series.hh"
/* -------------------------------------------------------------------------- */
//********************************
// Definition of the options
//********************************
using ArgMap = std::map<std::string, std::string>;
ArgMap defaults{{"series_type", "pi"}, {"dumper_type", "print"},
{"output", "cout"}, {"freq", "10"},
{"maxiter", "1000"}, {"precision", "5"},
{"delimiter", " "}};
ArgMap options{{"series_type", "pi/arithmetic"},
{"dumper_type", "print/write"},
{"output", "cout/filename"},
{"freq", "Frequency to output series value"},
{"maxiter", "Maximal series iteration"},
{"precision", "number of digits to use"},
{"delimiter", "separator to use for produced files"}};
void printUsage(std::string prog_name);
ArgMap parseArgv(int argc, char **argv);
/* ---------------------------------------------------------------------- */
int main(int argc, char **argv) {
//********************************
// Parsing
//********************************
auto params = parseArgv(argc, argv);
auto series_type = params["series_type"];
auto dumper_type = params["dumper_type"];
auto max_iter = atoi(params["maxiter"].c_str());
auto precision = atoi(params["precision"].c_str());
auto delimiter = params["delimiter"];
auto freq = atoi(params["freq"].c_str());
auto output = params["output"];
//********************************
// Series object creation
//********************************
std::unique_ptr<Series> series = nullptr;
if (series_type == "pi")
series = std::make_unique<ComputePi>();
else if (series_type == "arithmetic")
series = std::make_unique<ComputeArithmetic>();
else
printUsage(argv[0]);
//********************************
// Dumper object creation
//********************************
std::unique_ptr<DumperSeries> dumper = nullptr;
if (dumper_type == "print")
dumper = std::make_unique<PrintSeries>(*series, freq);
else if (dumper_type == "write")
dumper = std::make_unique<WriteSeries>(*series, delimiter);
else
printUsage(argv[0]);
//********************************
// File object creation
//********************************
std::ostream *file = nullptr;
if (output != "cout")
file = new std::ofstream(output);
else
file = &std::cout;
//********************************
// Do the polymorphic job
//********************************
dumper->setPrecision(precision);
dumper->setMaxIter(max_iter);
*file << *dumper << std::endl;
if (output != "cout")
delete file;
return EXIT_SUCCESS;
}
/* ---------------------------------------------------------------------- */
// argument parsing system
/* ---------------------------------------------------------------------- */
void printUsage(std::string prog_name) {
std::cerr << "Usage: " << prog_name << " --option value\n" << std::endl;
for (auto &&key_val : options) {
std::cerr << "\t--" << key_val.first << ": " << key_val.second << std::endl;
}
std::cerr << std::endl;
throw std::runtime_error("Wrong Usage");
}
/* ---------------------------------------------------------------------- */
ArgMap parseArgv(int argc, char **argv) {
if (argc == 1)
printUsage(argv[0]);
auto params = defaults;
std::vector<std::string> vec_options;
for (int i = 1; i < argc; ++i) {
vec_options.push_back(argv[i]);
}
if (vec_options.size() % 2 != 0)
printUsage(argv[0]);
UInt noptions = argc / 2;
for (int i = 0; i < noptions; ++i) {
auto key = vec_options[2 * i];
auto value = vec_options[2 * i + 1];
key = key.substr(2);
if (key == "help")
printUsage(argv[0]);
if (options.count(key) == 0)
printUsage(argv[0]);
params[key] = value;
}
std::cout << "Selected options:" << std::endl;
std::for_each(params.begin(), params.end(), [](auto &v) {
std::cout << "\t" << v.first << ": " << v.second << "\n";
});
return params;
}
#ifndef __MAIN_HH__
#define __MAIN_HH__
typedef double Real;
typedef unsigned long UInt;
#endif /* __MAIN_HH__ */
#include <cmath>
#include <iostream>
/* -------------------------------------------------------------------------- */
#include "print_series.hh"
/* -------------------------------------------------------------------------- */
PrintSeries::PrintSeries(Series &series, UInt freq)
: DumperSeries(series), freq(freq) {}
/* -------------------------------------------------------------------------- */
void PrintSeries::dump(std::ostream &os) {
for (UInt i = 1; i * this->freq < this->maxiter; ++i) {
double res = series.compute(i * this->freq - 1);
double res2 = series.compute(i * this->freq);
os << std::setprecision(this->precision);
os << std::scientific;
os << i * this->freq << " " << res << " " << res2 - res;
if (!std::isnan(series.getAnalyticPrediction())) {
os << " " << std::abs(res2 - series.getAnalyticPrediction());
}
os << std::endl;
}
}
/* -------------------------------------------------------------------------- */
#include "dumper_series.hh"
/* -------------------------------------------------------------------------- */
class PrintSeries : public DumperSeries {
public:
PrintSeries(Series &series, UInt freq);
void dump(std::ostream &os) override;
private:
UInt freq = 10;
};
#include "series.hh"
#include <cmath>
/* -------------------------------------------------------------------------- */
Series::Series() {}
/* -------------------------------------------------------------------------- */
double Series::compute(UInt N) {
if (current_term <= N)
N -= this->current_term;
else {
this->current_value = 0.;
this->current_term = 0;
}
for (UInt k = 0; k < N; ++k) {
++current_term;
current_value += func(current_term);
}
return current_value;
}
/* -------------------------------------------------------------------------- */
double Series::getAnalyticPrediction() { return nan(""); }
#ifndef __SERIES_HH__
#define __SERIES_HH__
/* -------------------------------------------------------------------------- */
#include "main.hh"
#include <functional>
#include <ostream>
#include <cmath>
/* -------------------------------------------------------------------------- */
class Series {
public:
Series();
virtual double compute(UInt N);
virtual double getAnalyticPrediction();
protected:
std::function<Real(UInt)> func;
UInt current_term = 0;
double current_value = 0.;
};
#endif /* __SERIES_HH__ */
#include <cmath>
#include <iostream>
/* -------------------------------------------------------------------------- */
#include "write_series.hh"
#include <algorithm>
#include <fstream>
/* -------------------------------------------------------------------------- */
WriteSeries::WriteSeries(Series &series, const std::string &delimiter)
: DumperSeries(series), delimiter(delimiter) {}
/* -------------------------------------------------------------------------- */
void WriteSeries::dump(std::ostream &os) {
os << std::setprecision(this->precision);
os << std::scientific;
for (UInt i = 1; i < this->maxiter; ++i) {
double res = series.compute(i);
os << i << this->delimiter;
os << res << this->delimiter;
os << series.getAnalyticPrediction() << std::endl;
}
}
/* -------------------------------------------------------------------------- */
void WriteSeries::dump() {
std::string namefile = "iterations";
std::string extension;
if (extensions.count(delimiter) == 0)
extension = ".txt";
else
extension = extensions[delimiter];
std::ofstream outfile(namefile + extension);
this->dump(outfile);
}
/* -------------------------------------------------------------------------- */
#include "dumper_series.hh"
#include <map>
/* -------------------------------------------------------------------------- */
class WriteSeries : public DumperSeries {
public:
WriteSeries(Series &series, const std::string &delimiter);
void dump(std::ostream &os) override;
void dump();
private:
std::string delimiter = ",";
std::map<std::string, std::string> extensions{
{",", ".csv"}, {"|", ".psv"}, {" ", ".txt"}};
};
#X Y Z VX VY VZ FX FY FZ mass radius name
-0.428853781127 0.138052592453 0.0184539798107 -0.0142721834943 -0.0258465078583 -0.00108568032051 0.0 0.0 0.0 0.0552929494875 1.63083872022e-05 mercury
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 8705198.59549 0.00465475876589 sun
26.7415215422 -13.7285247684 -0.463678541844 0.0306436822316 0.0587377512845 -6.14341509352e-05 0.0 0.0 0.0 17.1511623039 0.000165537115496 neptune
-4.91046296885 -1.56747937293 0.0315135965208 0.209251590696 -0.675341112804 -0.00674176351131 0.0 0.0 0.0 317.906831497 0.000467326170305 jupiter
-17.9185300935 6.75209511505 0.109163409846 -0.0304399757204 -0.0709540818034 -0.000258781059936 0.0 0.0 0.0 14.5357063322 0.000170851362258 uranus
-1.43134394797 0.0275726508377 0.0100883149567 -0.000893454647321 -0.025165039027 -0.000362795024676 0.0 0.0 0.0 0.107473137995 2.27021947846e-05 mars
0.77377663205 -0.659505389698 -0.0 0.0561475784188 0.0658005662623 0.0 0.0 0.0 0.0 1.0 4.2587504556e-05 earth
-0.00194776149901 0.716955803146 0.0166991417597 -0.0940412147154 -3.427886131e-05 0.0017278303659 0.0 0.0 0.0 0.815044397449 4.04537843465e-05 venus
8.61254548136 4.25084801195 -0.106075309989 -0.114335334469 0.244139821153 0.0049799846855 0.0 0.0 0.0 95.1845135537 0.000402866696685 saturn
#X Y Z VX VY VZ FX FY FZ mass radius name
-0.447218916544 0.118500375015 0.0178223373124 -0.0125247559765 -0.0261130469367 -0.00114434561863 0.0 0.0 0.0 0.0552929494875 1.63083872022e-05 mercury
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 8705198.59549 0.00465475876589 sun
26.7429563744 -13.7257313361 -0.463680999437 0.0306375021396 0.058740948122 -6.13273607949e-05 0.0 0.0 0.0 17.1511623039 0.000165537115496 neptune
-4.90806014678 -1.57502179476 0.0314377433943 0.21023982853 -0.675028935 -0.00674812580308 0.0 0.0 0.0 317.906831497 0.000467326170305 jupiter
-17.9199443481 6.74836096934 0.109149050619 -0.0304252559426 -0.0709599991802 -0.000258872635868 0.0 0.0 0.0 14.5357063322 0.000170851362258 uranus
-1.43171542731 0.0123084695915 0.00986711115371 -0.000647518780936 -0.0251693074414 -0.000364521465803 0.0 0.0 0.0 0.107473137995 2.27021947846e-05 mars
0.784648706099 -0.646543640125 -0.0 0.0550256780679 0.0667407828919 0.0 0.0 0.0 0.0 1.0 4.2587504556e-05 earth
-0.022293076898 0.716683938736 0.0170667972226 -0.0939949149558 -0.0026856112046 0.00166535857203 0.0 0.0 0.0 0.815044397449 4.04537843465e-05 venus
8.61021270345 4.25560967168 -0.105976377893 -0.114476651946 0.244071462123 0.00498174000875 0.0 0.0 0.0 95.1845135537 0.000402866696685 saturn
#X Y Z VX VY VZ FX FY FZ mass radius name
-0.463953442595 0.0980891229229 0.0171018722109 -0.0108438616545 -0.026293438546 -0.00119653351044 0.0 0.0 0.0 0.0552929494875 1.63083872022e-05 mercury
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 8705198.59549 0.00465475876589 sun
26.7443909139 -13.7229377558 -0.463683452018 0.0306313217152 0.0587441443108 -6.12205701271e-05 0.0 0.0 0.0 17.1511623039 0.000165537115496 neptune
-4.90564585699 -1.58256044302 0.0313618175767 0.211227585627 -0.674715273695 -0.00675447310774 0.0 0.0 0.0 317.906831497 0.000467326170305 jupiter
-17.9213578127 6.74462656481 0.109134686799 -0.0304105350179 -0.0709659134714 -0.000258964199864 0.0 0.0 0.0 14.5357063322 0.000170851362258 uranus
-1.43193260129 -0.00295615307914 0.00964485696864 -0.000401614132061 -0.0251709981446 -0.000366209917198 0.0 0.0 0.0 0.107473137995 2.27021947846e-05 mars
0.79529983684 -0.633399834781 -0.0 0.0538882993764 0.0676621932101 0.0 0.0 0.0 0.0 1.0 4.2587504556e-05 earth
-0.0426212523373 0.715839970959 0.0174208413491 -0.093873347768 -0.00533395565307 0.00160157272458 0.0 0.0 0.0 0.815044397449 4.04537843465e-05 venus
8.60787730064 4.2603699824 -0.10587741417 -0.114617929851 0.244003023812 0.00498349367254 0.0 0.0 0.0 95.1845135537 0.000402866696685 saturn
#X Y Z VX VY VZ FX FY FZ mass radius name
-0.47905733812 0.0769460204044 0.0162996858465 -0.00922540137211 -0.0263971594243 -0.0012428758822 0.0 0.0 0.0 0.0552929494875 1.63083872022e-05 mercury
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 8705198.59549 0.00465475876589 sun
26.7458251605 -13.7201440275 -0.463685899586 0.0306251409583 0.0587473398511 -6.11137789329e-05 0.0 0.0 0.0 17.1511623039 0.000165537115496 neptune
-4.90322010585 -1.59009529951 0.0312858192557 0.212214859682 -0.674400129667 -0.00676080541084 0.0 0.0 0.0 317.906831497 0.000467326170305 jupiter
-17.9227704872 6.74089190163 0.109120318386 -0.030395812947 -0.0709718246768 -0.00025905575192 0.0 0.0 0.0 14.5357063322 0.000170851362258 uranus
-1.43199553192 -0.0182195254273 0.00942157761421 -0.000155770993968 -0.0251701128989 -0.000367860199755 0.0 0.0 0.0 0.107473137995 2.27021947846e-05 mars
0.805727026531 -0.620077676195 -0.0 0.0527357467946 0.0685645510222 0.0 0.0 0.0 0.0 1.0 4.2587504556e-05 earth
-0.0629161077769 0.714424718859 0.0177609957719 -0.0936766628319 -0.00797714541004 0.0015365259327 0.0 0.0 0.0 0.815044397449 4.04537843465e-05 venus
8.60553927369 4.26512894272 -0.10577841885 -0.114759168141 0.243934506243 0.00498524567636 0.0 0.0 0.0 95.1845135537 0.000402866696685 saturn
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