diff --git a/Attractor.cpp b/Attractor.cpp index 21a2abb..1dc49a5 100644 --- a/Attractor.cpp +++ b/Attractor.cpp @@ -1,98 +1,91 @@ -#include -#include -#include -#include -#include -#include -#include -using namespace std; - #include "Attractor.hpp" - +using namespace std; /* Constructors & initialisers */ -Attractor::Attractor(): - dim(3), par(4), formula(LORENZ), param(NULL), point(NULL), new_point(NULL) { - // Default attractor: 3D Lorenz attrractor +Attractor::Attractor() { - param[0] = 0.001; //dt - param[1] = 10; //sigma - param[2] = 28; //rho - param[3] = 8.0/3.0; //beta + myAttractor = new Lorenz3D(); } -Attractor::Attractor(const char* const filename) { - ifstream file(filename); +Attractor::Attractor(const char* const fileName) { + ifstream file(fileName); - cout << "Reading file " << filename << "..." << endl; + cout << "Reading file " << fileName << "..." << endl; if ( !file ) { - cout << " Error reading file '" << filename << "' dying now..." << endl; + cerr << " Error reading file '" << fileName << "' dying now..." << endl; exit(2); } // TODO : Use stfu - /* - file.attr: - lorenz - 3 - 0 - - 3.24454 - 1.25 - .... - */ + stfu::node system; + system.read(file); + stfu::node attractor = system.getChild("attractor"); - string fileFormula; - file >> fileFormula; + string attractorType = attractor.getValue("type"); + const string attractorDimension = attractor.getValue("dimensions"); - for ( unsigned int i = 0; fileFormula[i] != '\0'; i++ ) { - fileFormula[i] = tolower(fileFormula[i]); + for ( unsigned int i = 0; attractorType[i] != '\0'; i++ ) { + attractorType[i] = tolower(attractorType[i]); } + const unsigned int dimension = atoi(attractorDimension.c_str()); + + cout << " Formula: " << attractorType << endl; + cout << " Dimensions: " << dimension << endl; - unsigned int fileDim; - file >> fileDim; - - unsigned int fileOrde; - file >> fileOrde; - - cout << " Formula: " << fileFormula << endl; - cout << " Dimensions: " << fileDim << endl; - cout << " Orde: " << fileOrde << endl; - - if ( fileFormula == "lorenz" ) - init(fileDim, LORENZ, fileOrde); - else if ( fileFormula == "poly_n" ) - init(fileDim, POLY_N, fileOrde); - else if ( fileFormula == "poly_a" ) - init(fileDim, POLY_A, fileOrde); - else if ( fileFormula == "logistic" ) - init(fileDim, LOGISTIC, fileOrde); - else if ( fileFormula == "unravel" ) - init(fileDim, UNRAVEL, fileOrde); - else { - cout << " Formula not (yet) supported" << endl; + if ( attractorType == "lorenz" ){ + if ( dimension == 3 ) { + myAttractor = new Lorenz3D(); + } else { + cerr << "something wrong"; + exit(37); + } + } else if ( attractorType == "polynomial" ) { + const string attractorOrde = attractor.getValue("orde"); + const unsigned int orde = atoi(attractorOrde.c_str()); + cout << " Orde: " << orde << endl; + myAttractor = new Polynomial(dimension, orde); + + } else if ( attractorType == "polynomial a" ) { + if ( dimension == 3 ) { + myAttractor = new PolynomialA3D(); + } else { + cerr << "something wrong"; + exit(37); + } + } else if ( attractorType == "logistic" ) { + myAttractor = new Logistic(dimension); + + } else if ( attractorType == "unravel" ) { + if ( dimension == 3 ) { + myAttractor = new Unravel3D(); + } else { + cerr << "somtheing wrong"; + exit(37); + } + } else { + cout << "'" << attractorType << "' not recognized" << endl; exit(3); } - for ( unsigned int i = 0; i < par; i++ ) { - file >> param[i]; - cout << " Parameter " << i << " set to " << param[i] << ", "; + unsigned int numberOfParameters = myAttractor->getNumberOfParameters(); + double * & parameters = myAttractor->parameters(); + + for ( unsigned int i = 0; i < numberOfParameters; i++ ) { + stfu::node attractorParameters = attractor.getChild("parameters"); + parameters[i] = atof(attractorParameters.getValue(i).c_str()); + cout << " Parameter " << i << " set to " << parameters[i] << ", "; } cout << endl << " Reading file complete" << endl; } -void Attractor::init(unsigned int dim_in, FormulaChoice formula_in, unsigned int orde_in) { - -} - void Attractor::init_range() { - +/* // stabilize attractor for ( unsigned int i = 0; i < 100000; i++ ) { iterate(); @@ -125,6 +118,7 @@ void Attractor::init_range() { for ( vector::iterator it = projectors.begin(); it != projectors.end(); it++ ) { (*it)->finish_range(); } +*/ } bool Attractor::is_chaos() { @@ -134,10 +128,10 @@ bool Attractor::is_chaos() { Single point attractor Lyapunov exponent */ - +/* double sum = 0; for ( unsigned int i = 0; i < dim; i++ ) { - const double dist = new_point[i] - point[i]; + const double dist = 0; //new_point[i] - point[i]; sum += dist*dist; } if ( sum >= 1.0e7 ) { @@ -149,17 +143,17 @@ bool Attractor::is_chaos() { return false; } return true; +*/ + return true; } -/* - Iteration & Math -*/ void Attractor::iterate() { - // attractorKernel->iterate() + myAttractor->iterate(); } void Attractor::plot() { for ( vector::iterator it = projectors.begin(); it != projectors.end(); it++ ) { + const double * point = myAttractor->vector(); (*it)->plot(point); } } @@ -169,8 +163,13 @@ void Attractor::plot() { IO & control */ void Attractor::output() { - for ( unsigned int i = 0; i < dim; i++ ) { + const unsigned int* dim = (unsigned int*)myAttractor->getProperty("dimension"); + const double * point = myAttractor->vector(); + + for ( unsigned int i = 0; i < *dim; i++ ) { cout << point[i] << " "; } cout << endl; + + delete dim; } diff --git a/Attractor.hpp b/Attractor.hpp index d6da8c4..76d0bc9 100644 --- a/Attractor.hpp +++ b/Attractor.hpp @@ -1,16 +1,21 @@ #ifndef ATTRACTOR_HPP #define ATTRACTOR_HPP +#include +#include +#include +#include #include -//#include "Vector.hpp" -//#include "Parameters.hpp" +#include "stfu/stf.hpp" #include "Projector.hpp" #include "AttractorKernel.hpp" -enum FormulaChoice { - POLY_N, LORENZ, POLY_A, POLY_2, LOGISTIC, UNRAVEL -}; +#include "kernels/Logistic.hpp" +#include "kernels/Lorenz3D.hpp" +#include "kernels/Polynomial.hpp" +#include "kernels/PolynomialA3D.hpp" +#include "kernels/Unravel3D.hpp" class Projector; @@ -18,23 +23,16 @@ class Projector; class Attractor { public: - unsigned int dim; - unsigned int par; - unsigned int orde; - FormulaChoice formula; - - double * param; - double * point; - double * new_point; + AttractorKernel * myAttractor; - vector projectors; + std::vector projectors; public: Attractor(); Attractor(const char* const filename); - void init(unsigned int dimensions, FormulaChoice formula, unsigned int orde); + //void init(unsigned int dimensions, FormulaChoice formula, unsigned int orde); void init_range(); @@ -42,8 +40,6 @@ public: // TODO : lyapunov exponent uit rekenen bool is_chaos(); - - // TODO : optimaliseren voor lage graads veeltermen void iterate(); void plot(); diff --git a/AttractorKernel.hpp b/AttractorKernel.hpp index 850c09f..6e14e69 100644 --- a/AttractorKernel.hpp +++ b/AttractorKernel.hpp @@ -14,6 +14,8 @@ class AttractorKernel { virtual double& parameter(const unsigned int index) = 0; virtual double*& parameters() = 0; + virtual unsigned int getNumberOfParameters() = 0; + // get properties of the attractor // such as the dimension // you should delete the void pointer if you used it diff --git a/AwesomeAttractor.cbp b/AwesomeAttractor.cbp index 9333f37..48aa764 100644 --- a/AwesomeAttractor.cbp +++ b/AwesomeAttractor.cbp @@ -43,9 +43,8 @@ - - - + + @@ -58,6 +57,8 @@ + + diff --git a/Projector.cpp b/Projector.cpp index 06a1222..f66ea2c 100644 --- a/Projector.cpp +++ b/Projector.cpp @@ -78,14 +78,14 @@ void Projector::finish_range() { } -void Projector::project(double * point) { +void Projector::project(const double * point) { assert(extern_dim >= 2); project_point[0] = point[0]; project_point[1] = point[1]; } -void Projector::plot(double * point) { +void Projector::plot(const double * point) { project(point); const double x = project_point[0]*factor + offset[0]; diff --git a/Projector.hpp b/Projector.hpp index b6b86c8..84a8813 100644 --- a/Projector.hpp +++ b/Projector.hpp @@ -35,8 +35,8 @@ class Projector{ -genormalizeerde coordinaten als kleurintensiteit (gebruikt fp canvas) -kleurbanden, dus met een periodieke functie (gebruikt int canvas) */ - void project(double * point); - void plot(double * point); + void project(const double * point); + void plot(const double * point); void output(); }; diff --git a/kernels/Logistic.cpp b/kernels/Logistic.cpp index f3ac4e9..379561d 100644 --- a/kernels/Logistic.cpp +++ b/kernels/Logistic.cpp @@ -83,6 +83,10 @@ double & Logistic::parameter(const unsigned int index) { return myParameters[index]; } +unsigned int Logistic::getNumberOfParameters() { + return dimension; +} + double * & Logistic::vector() { return vectorNew; } diff --git a/kernels/Logistic.hpp b/kernels/Logistic.hpp index 40ccbd1..3f6de68 100644 --- a/kernels/Logistic.hpp +++ b/kernels/Logistic.hpp @@ -5,7 +5,7 @@ #include "../AttractorKernel.hpp" -class Logistic : AttractorKernel { +class Logistic : public AttractorKernel { double * myParameters; double * vectorNew; @@ -25,6 +25,8 @@ class Logistic : AttractorKernel { virtual double& parameter(const unsigned int index); virtual double*& parameters(); + virtual unsigned int getNumberOfParameters(); + // get properties of the attractor // such as the dimension // you should delete the void pointer if you used it diff --git a/kernels/Lorenz3D.cpp b/kernels/Lorenz3D.cpp index 23ee8ea..1b02f2e 100644 --- a/kernels/Lorenz3D.cpp +++ b/kernels/Lorenz3D.cpp @@ -1,29 +1,32 @@ #include "Lorenz3D.hpp" -Lorenz::Lorenz() { +const static unsigned int dimension = 3; +const static unsigned int numberOfParameters = 4; + +Lorenz3D::Lorenz3D() { init(); } -void Lorenz::init() { +void Lorenz3D::init() { // allocation - myParameters = new double[4]; - vectorNew = new double[3]; - vectorOld = new double[3]; + myParameters = new double[numberOfParameters]; + vectorNew = new double[dimension]; + vectorOld = new double[dimension]; // initialisation assert(myParameters != NULL); assert(vectorNew != NULL); assert(vectorOld != NULL); - for ( unsigned int i = 0; i < 4; i++ ) { + for ( unsigned int i = 0; i < numberOfParameters; i++ ) { myParameters[i] = 0.0; } - for ( unsigned int i = 0; i < 3; i++ ) { + for ( unsigned int i = 0; i < dimension; i++ ) { vectorNew[i] = vectorOld[i] = 1.0; } } // the main function -void Lorenz::iterate() { +void Lorenz3D::iterate() { swap(vectorNew, vectorOld); vectorNew[0] = vectorOld[0] + myParameters[0] * myParameters[1] * (vectorOld[1] - vectorOld[0]); @@ -41,32 +44,36 @@ void Lorenz::iterate() { */ // setters, getters, all i/o to other classes/objects -void * Lorenz::getProperty(const string identifier) { +void * Lorenz3D::getProperty(const string identifier) { if ( identifier == "dimension" ) { unsigned int * _return = new unsigned int; - *_return = 3; + *_return = dimension; return _return; } return NULL; } -void Lorenz::setProperty(const string identifier, const void * value) { +void Lorenz3D::setProperty(const string identifier, const void * value) { return; } -double * & Lorenz::parameters() { +double * & Lorenz3D::parameters() { return myParameters; } -double & Lorenz::parameter(const unsigned int index) { +double & Lorenz3D::parameter(const unsigned int index) { return myParameters[index]; } -double * & Lorenz::vector() { +unsigned int Lorenz3D::getNumberOfParameters() { + return numberOfParameters; +} + +double * & Lorenz3D::vector() { return vectorNew; } -double * & Lorenz::previousVector() { +double * & Lorenz3D::previousVector() { return vectorOld; } diff --git a/kernels/Lorenz3D.hpp b/kernels/Lorenz3D.hpp index dce9fab..1bf2e7e 100644 --- a/kernels/Lorenz3D.hpp +++ b/kernels/Lorenz3D.hpp @@ -9,7 +9,7 @@ using namespace std; #include "../AttractorKernel.hpp" -class Lorenz : public AttractorKernel { +class Lorenz3D : public AttractorKernel { double * myParameters; @@ -20,14 +20,16 @@ class Lorenz : public AttractorKernel { public: - Lorenz(); - Lorenz(const unsigned int dimensions); + Lorenz3D(); + Lorenz3D(const unsigned int dimensions); // parameters are stored in a array of doubles // if you want to use other types, use the properties virtual double& parameter(const unsigned int index); virtual double*& parameters(); + virtual unsigned int getNumberOfParameters(); + // get properties of the attractor // such as the dimension // you should delete the void pointer if you used it diff --git a/kernels/Polynomial.cpp b/kernels/Polynomial.cpp index 355d222..35ca8d6 100644 --- a/kernels/Polynomial.cpp +++ b/kernels/Polynomial.cpp @@ -128,6 +128,10 @@ double & Polynomial::parameter(const unsigned int index) { return myParameters[index]; } +unsigned int Polynomial::getNumberOfParameters() { + return numberOfParameters; +} + double * & Polynomial::vector() { return vectorNew; } diff --git a/kernels/Polynomial.hpp b/kernels/Polynomial.hpp index 3506871..7ec2196 100644 --- a/kernels/Polynomial.hpp +++ b/kernels/Polynomial.hpp @@ -30,6 +30,8 @@ class Polynomial : public AttractorKernel { virtual double& parameter(const unsigned int index); virtual double*& parameters(); + virtual unsigned int getNumberOfParameters(); + // get properties of the attractor // such as the dimension // you should delete the void pointer if you used it diff --git a/kernels/PolynomialA3D.cpp b/kernels/PolynomialA3D.cpp index 8a2b906..7776159 100644 --- a/kernels/PolynomialA3D.cpp +++ b/kernels/PolynomialA3D.cpp @@ -68,6 +68,10 @@ double & PolynomialA3D::parameter(const unsigned int index) { return myParameters[index]; } +unsigned int PolynomialA3D::getNumberOfParameters() { + return numberOfParameters; +} + double * & PolynomialA3D::vector() { return vectorNew; } diff --git a/kernels/PolynomialA3D.hpp b/kernels/PolynomialA3D.hpp index 5acae16..1434fc6 100644 --- a/kernels/PolynomialA3D.hpp +++ b/kernels/PolynomialA3D.hpp @@ -24,6 +24,8 @@ class PolynomialA3D : public AttractorKernel { virtual double& parameter(const unsigned int index); virtual double*& parameters(); + virtual unsigned int getNumberOfParameters(); + // get properties of the attractor // such as the dimension // you should delete the void pointer if you used it diff --git a/kernels/Unravel3D.cpp b/kernels/Unravel3D.cpp index 540c8d6..328629e 100644 --- a/kernels/Unravel3D.cpp +++ b/kernels/Unravel3D.cpp @@ -1,28 +1,31 @@ #include "Unravel3D.hpp" -Unravel::Unravel(){ +const static unsigned int dimension = 3; +const static unsigned int numberOfParameters = 7; + +Unravel3D::Unravel3D(){ init(); } -void Unravel::init() { +void Unravel3D::init() { // allocation - myParameters = new double[4]; - vectorNew = new double[3]; - vectorOld = new double[3]; + myParameters = new double[numberOfParameters]; + vectorNew = new double[dimension]; + vectorOld = new double[dimension]; // initialisation assert(myParameters != NULL); assert(vectorNew != NULL); assert(vectorOld != NULL); - for ( unsigned int i = 0; i < 7; i++ ) { + for ( unsigned int i = 0; i < numberOfParameters; i++ ) { myParameters[i] = 0.0; } - for ( unsigned int i = 0; i < 3; i++ ) { + for ( unsigned int i = 0; i < dimension; i++ ) { vectorNew[i] = vectorOld[i] = 0.0; } } -void Unravel::iterate() { +void Unravel3D::iterate() { swap(vectorNew, vectorOld); vectorNew[0] = myParameters[0]*(vectorOld[2] + myParameters[1]); @@ -41,31 +44,35 @@ void Unravel::iterate() { } // setters, getters, all i/o to other classes/objects -void * Unravel::getProperty(const string identifier) { +void * Unravel3D::getProperty(const string identifier) { if ( identifier == "dimension" ) { unsigned int * _return = new unsigned int; - *_return = 3; + *_return = dimension; return _return; } return NULL; } -void Unravel::setProperty(const string identifier, const void * value) { +void Unravel3D::setProperty(const string identifier, const void * value) { return; } -double * & Unravel::parameters() { +double * & Unravel3D::parameters() { return myParameters; } -double & Unravel::parameter(const unsigned int index) { +double & Unravel3D::parameter(const unsigned int index) { return myParameters[index]; } -double * & Unravel::vector() { +unsigned int Unravel3D::getNumberOfParameters() { + return numberOfParameters; +} + +double * & Unravel3D::vector() { return vectorNew; } -double * & Unravel::previousVector() { +double * & Unravel3D::previousVector() { return vectorOld; } diff --git a/kernels/Unravel3D.hpp b/kernels/Unravel3D.hpp index 3464c87..94a693a 100644 --- a/kernels/Unravel3D.hpp +++ b/kernels/Unravel3D.hpp @@ -10,7 +10,7 @@ using namespace std; #include "../AttractorKernel.hpp" -class Unravel : public AttractorKernel { +class Unravel3D : public AttractorKernel { double * myParameters; @@ -21,14 +21,16 @@ class Unravel : public AttractorKernel { public: - Unravel(); - Unravel(const unsigned int dimensions); + Unravel3D(); + Unravel3D(const unsigned int dimensions); // parameters are stored in a array of doubles // if you want to use other types, use the properties virtual double& parameter(const unsigned int index); virtual double*& parameters(); + virtual unsigned int getNumberOfParameters(); + // get properties of the attractor // such as the dimension // you should delete the void pointer if you used it diff --git a/main.cpp b/main.cpp index 90919bf..5f16345 100644 --- a/main.cpp +++ b/main.cpp @@ -11,9 +11,9 @@ using namespace std; // TODO : Allemaal files inlezen, voor makkelijker gebruik int main(int argc, char *argv[]) { - +/* AttractorKernel * mijnAttractortje; - mijnAttractortje = new Lorenz(); + mijnAttractortje = new Lorenz3D(); AttractorKernel &attractor = *mijnAttractortje; attractor.parameter(0) = 1.0; @@ -35,7 +35,13 @@ int main(int argc, char *argv[]) { } cout << endl; } +*/ + Attractor myAttractor("attractors/testAttractor.stf"); + for ( unsigned int i = 0; i < 20; i++ ) { + myAttractor.iterate(); + myAttractor.output(); + } /*if ( argc <= 2 ) { cout << endl << "nothing to do..." << endl; diff --git a/stfu/stf.hpp b/stfu/stf.hpp index 8a25538..8d8a08d 100644 --- a/stfu/stf.hpp +++ b/stfu/stf.hpp @@ -280,9 +280,9 @@ namespace stfu { bool read(istream &in); /** - Reads the STF from a file + Reads the STF from a file - \return Returns whether it was succesful + \return Returns whether it was succesful */ bool read(const char *filename);