diff --git a/Attractor.cpp b/Attractor.cpp index b2cb872..6a2ce36 100644 --- a/Attractor.cpp +++ b/Attractor.cpp @@ -10,12 +10,12 @@ #include "Projector.hpp" -Attractor::Attractor(const char* const fileName) { +Attractor::Attractor(const std::string& filename) { // opening file - std::cout << "Reading file " << fileName << "..." << std::endl; + std::cout << "Reading file " << filename << "..." << std::endl; stfu::node system; - system.read(fileName); + system.read(filename.c_str()); stfu::node attractor = system.getChild("AttractorKernel"); myAttractor = AttractorKernel::createAttractorKernel(attractor); diff --git a/Attractor.hpp b/Attractor.hpp index 2d63062..c487563 100644 --- a/Attractor.hpp +++ b/Attractor.hpp @@ -1,6 +1,7 @@ #ifndef ATTRACTOR_HPP #define ATTRACTOR_HPP +#include #include class Projector; @@ -16,7 +17,7 @@ public: // should be private really std::vector projectors; - Attractor(const char* const filename); + Attractor(const std::string& filename); ~Attractor(); void init_range(); diff --git a/Parameters.cpp b/Parameters.cpp deleted file mode 100644 index 133c4a7..0000000 --- a/Parameters.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include -using namespace std; - -#include "Parameters.hpp" - - -Parameters::Parameters(unsigned int num_parameters, float default_val): - num_parameters(num_parameters) { - begin = new (nothrow) Vector(num_parameters, default_val); - eind = new (nothrow) Vector(num_parameters, default_val); - interpolated = new (nothrow) Vector(num_parameters, default_val); - // *interpolated = *begin - - check_pointers(); - -#ifdef HARDDEBUG - cout << "New Parameters with one default val:" << endl << *this << endl; -#endif -} - -/*Parameters::Parameters(unsigned int num_parameters, float default_val1, float default_val2): - num_parameters(num_parameters) { - begin = new (nothrow) Vector(num_parameters, default_val1); - eind = new (nothrow) Vector(num_parameters, default_val2); - interpolated = new (nothrow) Vector(num_parameters, default_val1); - // *interpolated = *begin - - check_pointers(); - -#ifdef HARDDEBUG - cout << "New Parameters with two default vals:" << endl << *this << endl; -#endif -}*/ - -Parameters::~Parameters() { - delete begin; - delete eind; - delete interpolated; - -#ifdef HARDDEBUG - cout << "Parameters deleted" << endl; -#endif -} - -void Parameters::check_pointers() { - assert(begin != NULL); - assert(eind != NULL); - assert(interpolated != NULL); -} - -void Parameters::set(unsigned int parameter, float val1, float val2) { - assert(parameter < num_parameters); - - begin->coordinates[parameter] = val1; - eind->coordinates[parameter] = val2; - interpolated->coordinates[parameter] = val1; - -#ifdef HARDDEBUG - cout << "Parameter " << parameter << " set to: " << val1 << " - " << val2 << endl; -#endif -} - -void Parameters::set(unsigned int parameter, float val) { - assert(parameter < num_parameters); - - begin->coordinates[parameter] = val; - eind->coordinates[parameter] = val; - interpolated->coordinates[parameter] = val; - -#ifdef HARDDEBUG - cout << "Parameter " << parameter << " set to: " << val << endl; -#endif -} - -float Parameters::get(unsigned int parameter) { - assert(parameter < num_parameters); - -#ifdef HARDDEBUG - cout << "Asked for parameter " << parameter << " with value:" << interpolated->coordinates[parameter] << endl; -#endif - - return interpolated->coordinates[parameter]; - -} - -void Parameters::interpolate(float time) { - /* - Dit is mogelijk met vector rekenen: - (*interpolated) = (*begin) * ( 1.0 - time ) + (*eind) * time; - Maar we doen het per element, zodat we simpelere code hebben, - geen vectoren hoeven te returnen en makkelijker kunnen optimaliseren - */ - const float invtime = 1.0 - time; - for ( unsigned int i = 0; i < num_parameters; i++ ) { - interpolated->coordinates[i] = invtime * begin->coordinates[i] + time * eind->coordinates[i]; - } - -#ifdef HARDDEBUG - cout << "interpolate() result" << endl << *interpolated << endl; -#endif -} - -ostream& operator<<(ostream& os, const Parameters& param) { - os << param.num_parameters << endl; - os << "Begin:" << endl << *param.begin << endl; - os << "Eind:" << endl << *param.eind << endl; - os << "Interpolated:" << endl << *param.interpolated << endl; - os < -using namespace std; - -#include "Vector.hpp" - -Vector::Vector(): - dimension(0) { - - coordinates = new (nothrow) float[0]; - - assert(coordinates != NULL); - -#ifdef HARDDEBUG - cout << "New vector (without elements)" << endl; -#endif -} - -Vector::Vector(unsigned int d): - dimension(d) { - - coordinates = new (nothrow) float[dimension]; - - assert(coordinates != NULL); - -#ifdef HARDDEBUG - cout << "New vector:" << endl << *this << endl; -#endif -} - -Vector::Vector(unsigned int d, float default_val): - dimension(d) { - - coordinates = new (nothrow) float[dimension]; - - assert(coordinates != NULL); - - for (unsigned int i = 0; i < dimension; i++) { - coordinates[i] = default_val; - } - -#ifdef HARDDEBUG - cout << "New vector with default values:" << endl << *this << endl; -#endif -} - - -Vector::~Vector() { - delete[] coordinates; - -#ifdef HARDDEBUG - cout << "coordinates deleted" << endl; -#endif -} - - -Vector& Vector::operator=(const Vector& a) { - if ( dimension != a.dimension ) { - dimension = a.dimension; - delete[] coordinates; - coordinates = new float[dimension]; - -#ifdef HARDDEBUG - cout << "Dimensions were not equal, made new vector" << endl; -#endif - } - - for ( unsigned int i = 0; i < dimension; i++ ) { - coordinates[i] = a.coordinates[i]; - } - -#ifdef HARDDEBUG - cout << "operator= result" << endl << *this << endl; -#endif - - return *this; -} - - -ostream& operator<<(ostream& os, const Vector& a) { - os << a.dimension << endl; - for ( unsigned int i = 0; i < a.dimension; i++ ) { - os << a.coordinates[i] << " "; - } - - os << endl; - return os; -} - - -float& Vector::operator[](const unsigned int index) { - assert(index < dimension); - return coordinates[index]; -} - -// matig werkende optelling en scalaire vermenigvuldiging van vectoren -/* -Vector Vector::operator+(const Vector a) const { - if ( dimension != a.dimension ) { - cout << "WARNING: dimensions not equal in vector addition" << endl; - exit(1); - } else { - static Vector ret(dimension); - for ( unsigned int i = 0; i < dimension; i++ ) { - ret.coordinates[i] = coordinates[i] + a.coordinates[i]; - } - -#ifdef HARDDEBUG - cout << "operator+ result" << endl << ret << endl; -#endif - return ret; - } -} - - -Vector Vector::operator*(const float a) const { - static Vector ret(dimension); - for ( unsigned int i = 0; i < dimension; i++ ) { - ret.coordinates[i] = coordinates[i] * a; - } - -#ifdef HARDDEBUG - cout << "operator* result" << endl << ret << endl; -#endif - return ret; -} -*/ diff --git a/Vector.hpp b/Vector.hpp deleted file mode 100644 index 7b60f12..0000000 --- a/Vector.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef VECTOR_HPP -#define VECTOR_HPP - -class Vector { - public: - - unsigned int dimension; - float * coordinates; - - // const, dest - Vector(); - Vector(unsigned int d); - Vector(unsigned int d, float default_val); - ~Vector(); - - // output operator - friend ostream& operator<<(ostream& os, const Vector& a); - - // easy access - float& Vector::operator[](const unsigned int index); - - // vector rekenen - Vector& operator=(const Vector& a); - // faaloperatoren - // Vector operator+(const Vector a) const; - // Vector operator*(const float a) const; -}; - - -#endif // VECTOR_HPP - diff --git a/attractors/testAttractor.stf b/attractors/testAttractor.stf index 47c20d9..34ed0bf 100644 --- a/attractors/testAttractor.stf +++ b/attractors/testAttractor.stf @@ -2,7 +2,7 @@ input: "attractor" output: "png" -attractor: { +AttractorKernel: { type: lorenz/unravel/polynomial/polynomial a/logistic => "polynomial" dimensions: most types only support 3D diff --git a/attractors/testPolynomial.stf b/attractors/testPolynomial.stf index b314c73..457f606 100644 --- a/attractors/testPolynomial.stf +++ b/attractors/testPolynomial.stf @@ -2,7 +2,7 @@ input: "attractor" output: "png" -attractor: { +AttractorKernel: { type: lorenz/unravel/polynomial/polynomial a/logistic => "polynomial" dimensions: most types only support 3D diff --git a/attractors/testUnravel.stf b/attractors/testUnravel.stf index c796727..954e5f1 100644 --- a/attractors/testUnravel.stf +++ b/attractors/testUnravel.stf @@ -2,7 +2,7 @@ input: "attractor" output: "png" -attractor: { +AttractorKernel: { type: lorenz/unravel/polynomial/polynomial a/logistic => "unravel" dimensions: most types only support 3D diff --git a/defines.hpp b/defines.hpp index 45a1138..a7da632 100644 --- a/defines.hpp +++ b/defines.hpp @@ -1,15 +1,13 @@ - -//TODO: do this with files -#define ATTRACTOR_FILE "attractors/testUnravel.stf" +#define DEFAULT_ATTRACTOR_FILE "attractors/testUnravel.stf" #ifdef UNI_BUILD #warning Building for the RU, are you sure? - #define WIDTH 8000 - #define HEIGHT 8000 - #define ITERATIONS 4200000000 + #define DEFAULT_WIDTH 8000 + #define DEFAULT_HEIGHT 8000 + #define DEFAULT_ITERATIONS 4200000000 #else - #define WIDTH 800 - #define HEIGHT 800 - #define ITERATIONS 1000000 + #define DEFAULT_WIDTH 800 + #define DEFAULT_HEIGHT 800 + #define DEFAULT_ITERATIONS 1000000 #endif diff --git a/main.cpp b/main.cpp index 7bb6da3..622eef0 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "Attractor.hpp" #include "Canvas.hpp" @@ -8,59 +9,68 @@ #include "defines.hpp" -/* - I compared the performance of C-style arrays with valarray. My conclusion is that accessing and setting elements makes no difference (sometimes C-syle array's were just 2% faster, but sometimes valarray was 2% faster (5000000 samples)). But the allocation and initialisation of the valrray was like 30% faster, so it might be a good idea to change my C-style array to valarray. It is also nicer in use, one can use functions like sum, etc. How valarray manages memory may also be more efficient, but all i know is that a valarray is bigger than a double* (in means of sizeof()), but that is not really an issue. So valarray!!! -*/ - int main(int argc, char *argv[]) { - - clock_t start, end; - double totalTime, totalIterations; - - bool verbose = false; - - // initialising stuff - Attractor myAttractor(ATTRACTOR_FILE); - - Projector projection; - Canvas canvas(WIDTH, HEIGHT, 3); - projection.canvas = &canvas; - - myAttractor.projectors.push_back(&projection); - myAttractor.init_range(); - - projection.output(); - - unsigned int iterations = ITERATIONS; - start = clock(); - for ( unsigned int j = 1; j <= 100; j++ ) { - for ( unsigned int i = 0; i <= iterations; i++ ) { - myAttractor.iterate(); - myAttractor.plot(); - } - if (verbose) { - myAttractor.output(); - std::cout << j << "% done" << std::endl; - } - } - end = clock(); - - totalIterations = 100.0*iterations; - totalTime = ((double)(end-start)/(double)(CLOCKS_PER_SEC)); - std::cout << std::endl << "total clock time: " << totalTime << std::endl; - std::cout << "average iterations per second: " << totalIterations/((double)(end-start)/(double)(CLOCKS_PER_SEC)) << std::endl << std::endl; - - // saving output - start = clock(); - canvas.output_file(); - end = clock(); - - totalTime = ((double)(end-start)/(double)(CLOCKS_PER_SEC)); - - std::cout << "total clock time for writing png: " << totalTime << std::endl; - std::cout << std::endl << "Awesome Attractor, version " << __DATE__ << std::endl; - - - return 0; + std::cout << "Awesome Attractor, version " << __DATE__ << std::endl; + + bool verbose = false; + std::string attractorFile = DEFAULT_ATTRACTOR_FILE; + unsigned int iterations = DEFAULT_ITERATIONS; + unsigned int width = DEFAULT_WIDTH; + unsigned int height = DEFAULT_HEIGHT; + + for ( int i = 1; i < argc; ++i ) { + if ( strcmp(argv[i], "-v") == 0 ) { + verbose = true; + } else if ( strcmp(argv[i], "-w") == 0 ) { + width = atoi(argv[++i]); + } else if ( strcmp(argv[i], "-h") == 0 ) { + height = atoi(argv[++i]); + } else if ( strcmp(argv[i], "-i") == 0 ) { + iterations = atoi(argv[++i]); + } else { + attractorFile = argv[i]; + } + } + + // initialising stuff + Attractor myAttractor(attractorFile); + + Projector projection; + Canvas canvas(width, height, 3); + projection.canvas = &canvas; + + myAttractor.projectors.push_back(&projection); + myAttractor.init_range(); + + projection.output(); + + clock_t start, end; + start = clock(); + for ( unsigned int j = 1; j <= 100; ++j ) { + for ( unsigned int i = 0; i <= iterations; i++ ) { + myAttractor.iterate(); + myAttractor.plot(); + } + std::cout << "\r" << j << "% done" << std::flush; + } + end = clock(); + + double totalIterations = 100.0*iterations; + double totalTime = ((double)(end-start)/(double)(CLOCKS_PER_SEC)); + std::cout << std::endl << "total clock time: " << totalTime << std::endl; + std::cout << "average iterations per second: " << totalIterations/totalTime << std::endl << std::endl; + + // saving output + start = clock(); + canvas.output_file(); + end = clock(); + + totalTime = ((double)(end-start)/(double)(CLOCKS_PER_SEC)); + + std::cout << "total clock time for writing png: " << totalTime << std::endl; + + + + return 0; }