Browse Source

added basic stf support, made attractor class as it should (not fully implemented yet)

master
Joshua 15 years ago
parent
commit
b056e48cff
  1. 151
      Attractor.cpp
  2. 30
      Attractor.hpp
  3. 2
      AttractorKernel.hpp
  4. 7
      AwesomeAttractor.cbp
  5. 4
      Projector.cpp
  6. 4
      Projector.hpp
  7. 4
      kernels/Logistic.cpp
  8. 4
      kernels/Logistic.hpp
  9. 37
      kernels/Lorenz3D.cpp
  10. 8
      kernels/Lorenz3D.hpp
  11. 4
      kernels/Polynomial.cpp
  12. 2
      kernels/Polynomial.hpp
  13. 4
      kernels/PolynomialA3D.cpp
  14. 2
      kernels/PolynomialA3D.hpp
  15. 37
      kernels/Unravel3D.cpp
  16. 8
      kernels/Unravel3D.hpp
  17. 10
      main.cpp

151
Attractor.cpp

@ -1,98 +1,91 @@
#include <cmath>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cassert>
#include <string>
using namespace std;
#include "Attractor.hpp" #include "Attractor.hpp"
using namespace std;
/* /*
Constructors & initialisers Constructors & initialisers
*/ */
Attractor::Attractor(): Attractor::Attractor() {
dim(3), par(4), formula(LORENZ), param(NULL), point(NULL), new_point(NULL) {
// Default attractor: 3D Lorenz attrractor
param[0] = 0.001; //dt myAttractor = new Lorenz3D();
param[1] = 10; //sigma
param[2] = 28; //rho
param[3] = 8.0/3.0; //beta
} }
Attractor::Attractor(const char* const filename) { Attractor::Attractor(const char* const fileName) {
ifstream file(filename); ifstream file(fileName);
cout << "Reading file " << filename << "..." << endl; cout << "Reading file " << fileName << "..." << endl;
if ( !file ) { if ( !file ) {
cout << " Error reading file '" << filename << "' dying now..." << endl; cerr << " Error reading file '" << fileName << "' dying now..." << endl;
exit(2); exit(2);
} }
// TODO : Use stfu // TODO : Use stfu
/* stfu::node system;
file.attr: system.read(file);
lorenz stfu::node attractor = system.getChild("attractor");
3
0 string attractorType = attractor.getValue("type");
const string attractorDimension = attractor.getValue("dimensions");
3.24454
1.25 for ( unsigned int i = 0; attractorType[i] != '\0'; i++ ) {
.... attractorType[i] = tolower(attractorType[i]);
*/ }
const unsigned int dimension = atoi(attractorDimension.c_str());
string fileFormula;
file >> fileFormula; cout << " Formula: " << attractorType << endl;
cout << " Dimensions: " << dimension << endl;
for ( unsigned int i = 0; fileFormula[i] != '\0'; i++ ) {
fileFormula[i] = tolower(fileFormula[i]); if ( attractorType == "lorenz" ){
} if ( dimension == 3 ) {
myAttractor = new Lorenz3D();
unsigned int fileDim; } else {
file >> fileDim; cerr << "something wrong";
exit(37);
unsigned int fileOrde; }
file >> fileOrde; } else if ( attractorType == "polynomial" ) {
const string attractorOrde = attractor.getValue("orde");
cout << " Formula: " << fileFormula << endl; const unsigned int orde = atoi(attractorOrde.c_str());
cout << " Dimensions: " << fileDim << endl; cout << " Orde: " << orde << endl;
cout << " Orde: " << fileOrde << endl; myAttractor = new Polynomial(dimension, orde);
if ( fileFormula == "lorenz" ) } else if ( attractorType == "polynomial a" ) {
init(fileDim, LORENZ, fileOrde); if ( dimension == 3 ) {
else if ( fileFormula == "poly_n" ) myAttractor = new PolynomialA3D();
init(fileDim, POLY_N, fileOrde); } else {
else if ( fileFormula == "poly_a" ) cerr << "something wrong";
init(fileDim, POLY_A, fileOrde); exit(37);
else if ( fileFormula == "logistic" ) }
init(fileDim, LOGISTIC, fileOrde); } else if ( attractorType == "logistic" ) {
else if ( fileFormula == "unravel" ) myAttractor = new Logistic(dimension);
init(fileDim, UNRAVEL, fileOrde);
else { } else if ( attractorType == "unravel" ) {
cout << " Formula not (yet) supported" << endl; if ( dimension == 3 ) {
myAttractor = new Unravel3D();
} else {
cerr << "somtheing wrong";
exit(37);
}
} else {
cout << "'" << attractorType << "' not recognized" << endl;
exit(3); exit(3);
} }
for ( unsigned int i = 0; i < par; i++ ) { unsigned int numberOfParameters = myAttractor->getNumberOfParameters();
file >> param[i]; double * & parameters = myAttractor->parameters();
cout << " Parameter " << i << " set to " << param[i] << ", ";
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; cout << endl << " Reading file complete" << endl;
} }
void Attractor::init(unsigned int dim_in, FormulaChoice formula_in, unsigned int orde_in) {
}
void Attractor::init_range() { void Attractor::init_range() {
/*
// stabilize attractor // stabilize attractor
for ( unsigned int i = 0; i < 100000; i++ ) { for ( unsigned int i = 0; i < 100000; i++ ) {
iterate(); iterate();
@ -125,6 +118,7 @@ void Attractor::init_range() {
for ( vector<Projector*>::iterator it = projectors.begin(); it != projectors.end(); it++ ) { for ( vector<Projector*>::iterator it = projectors.begin(); it != projectors.end(); it++ ) {
(*it)->finish_range(); (*it)->finish_range();
} }
*/
} }
bool Attractor::is_chaos() { bool Attractor::is_chaos() {
@ -134,10 +128,10 @@ bool Attractor::is_chaos() {
Single point attractor Single point attractor
Lyapunov exponent Lyapunov exponent
*/ */
/*
double sum = 0; double sum = 0;
for ( unsigned int i = 0; i < dim; i++ ) { 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; sum += dist*dist;
} }
if ( sum >= 1.0e7 ) { if ( sum >= 1.0e7 ) {
@ -149,17 +143,17 @@ bool Attractor::is_chaos() {
return false; return false;
} }
return true; return true;
*/
return true;
} }
/*
Iteration & Math
*/
void Attractor::iterate() { void Attractor::iterate() {
// attractorKernel->iterate() myAttractor->iterate();
} }
void Attractor::plot() { void Attractor::plot() {
for ( vector<Projector *>::iterator it = projectors.begin(); it != projectors.end(); it++ ) { for ( vector<Projector *>::iterator it = projectors.begin(); it != projectors.end(); it++ ) {
const double * point = myAttractor->vector();
(*it)->plot(point); (*it)->plot(point);
} }
} }
@ -169,8 +163,13 @@ void Attractor::plot() {
IO & control IO & control
*/ */
void Attractor::output() { 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 << point[i] << " ";
} }
cout << endl; cout << endl;
delete dim;
} }

30
Attractor.hpp

@ -1,16 +1,21 @@
#ifndef ATTRACTOR_HPP #ifndef ATTRACTOR_HPP
#define ATTRACTOR_HPP #define ATTRACTOR_HPP
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector> #include <vector>
//#include "Vector.hpp" #include "stfu/stf.hpp"
//#include "Parameters.hpp"
#include "Projector.hpp" #include "Projector.hpp"
#include "AttractorKernel.hpp" #include "AttractorKernel.hpp"
enum FormulaChoice { #include "kernels/Logistic.hpp"
POLY_N, LORENZ, POLY_A, POLY_2, LOGISTIC, UNRAVEL #include "kernels/Lorenz3D.hpp"
}; #include "kernels/Polynomial.hpp"
#include "kernels/PolynomialA3D.hpp"
#include "kernels/Unravel3D.hpp"
class Projector; class Projector;
@ -18,23 +23,16 @@ class Projector;
class Attractor { class Attractor {
public: public:
unsigned int dim;
unsigned int par;
unsigned int orde;
FormulaChoice formula;
double * param;
double * point;
double * new_point;
AttractorKernel * myAttractor;
vector<Projector *> projectors; std::vector<Projector *> projectors;
public: public:
Attractor(); Attractor();
Attractor(const char* const filename); 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(); void init_range();
@ -42,8 +40,6 @@ public:
// TODO : lyapunov exponent uit rekenen // TODO : lyapunov exponent uit rekenen
bool is_chaos(); bool is_chaos();
// TODO : optimaliseren voor lage graads veeltermen
void iterate(); void iterate();
void plot(); void plot();

2
AttractorKernel.hpp

@ -14,6 +14,8 @@ class AttractorKernel {
virtual double& parameter(const unsigned int index) = 0; virtual double& parameter(const unsigned int index) = 0;
virtual double*& parameters() = 0; virtual double*& parameters() = 0;
virtual unsigned int getNumberOfParameters() = 0;
// get properties of the attractor // get properties of the attractor
// such as the dimension // such as the dimension
// you should delete the void pointer if you used it // you should delete the void pointer if you used it

7
AwesomeAttractor.cbp

@ -43,9 +43,8 @@
<Unit filename="Projector.hpp"> <Unit filename="Projector.hpp">
<Option compilerVar="CC" /> <Option compilerVar="CC" />
</Unit> </Unit>
<Unit filename="attr/test.attr" /> <Unit filename="kernels/Logistic.cpp" />
<Unit filename="attr/test2.attr" /> <Unit filename="kernels/Logistic.hpp" />
<Unit filename="attr/waardes.txt" />
<Unit filename="kernels/Lorenz3D.cpp" /> <Unit filename="kernels/Lorenz3D.cpp" />
<Unit filename="kernels/Lorenz3D.hpp" /> <Unit filename="kernels/Lorenz3D.hpp" />
<Unit filename="kernels/Polynomial.cpp" /> <Unit filename="kernels/Polynomial.cpp" />
@ -58,6 +57,8 @@
<Unit filename="myMath.hpp" /> <Unit filename="myMath.hpp" />
<Unit filename="pngwriter/pngwriter.cc" /> <Unit filename="pngwriter/pngwriter.cc" />
<Unit filename="pngwriter/pngwriter.h" /> <Unit filename="pngwriter/pngwriter.h" />
<Unit filename="stfu/stf.cpp" />
<Unit filename="stfu/stf.hpp" />
<Extensions> <Extensions>
<code_completion /> <code_completion />
<envvars /> <envvars />

4
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); assert(extern_dim >= 2);
project_point[0] = point[0]; project_point[0] = point[0];
project_point[1] = point[1]; project_point[1] = point[1];
} }
void Projector::plot(double * point) { void Projector::plot(const double * point) {
project(point); project(point);
const double x = project_point[0]*factor + offset[0]; const double x = project_point[0]*factor + offset[0];

4
Projector.hpp

@ -35,8 +35,8 @@ class Projector{
-genormalizeerde coordinaten als kleurintensiteit (gebruikt fp canvas) -genormalizeerde coordinaten als kleurintensiteit (gebruikt fp canvas)
-kleurbanden, dus met een periodieke functie (gebruikt int canvas) -kleurbanden, dus met een periodieke functie (gebruikt int canvas)
*/ */
void project(double * point); void project(const double * point);
void plot(double * point); void plot(const double * point);
void output(); void output();
}; };

4
kernels/Logistic.cpp

@ -83,6 +83,10 @@ double & Logistic::parameter(const unsigned int index) {
return myParameters[index]; return myParameters[index];
} }
unsigned int Logistic::getNumberOfParameters() {
return dimension;
}
double * & Logistic::vector() { double * & Logistic::vector() {
return vectorNew; return vectorNew;
} }

4
kernels/Logistic.hpp

@ -5,7 +5,7 @@
#include "../AttractorKernel.hpp" #include "../AttractorKernel.hpp"
class Logistic : AttractorKernel { class Logistic : public AttractorKernel {
double * myParameters; double * myParameters;
double * vectorNew; double * vectorNew;
@ -25,6 +25,8 @@ class Logistic : AttractorKernel {
virtual double& parameter(const unsigned int index); virtual double& parameter(const unsigned int index);
virtual double*& parameters(); virtual double*& parameters();
virtual unsigned int getNumberOfParameters();
// get properties of the attractor // get properties of the attractor
// such as the dimension // such as the dimension
// you should delete the void pointer if you used it // you should delete the void pointer if you used it

37
kernels/Lorenz3D.cpp

@ -1,29 +1,32 @@
#include "Lorenz3D.hpp" #include "Lorenz3D.hpp"
Lorenz::Lorenz() { const static unsigned int dimension = 3;
const static unsigned int numberOfParameters = 4;
Lorenz3D::Lorenz3D() {
init(); init();
} }
void Lorenz::init() { void Lorenz3D::init() {
// allocation // allocation
myParameters = new double[4]; myParameters = new double[numberOfParameters];
vectorNew = new double[3]; vectorNew = new double[dimension];
vectorOld = new double[3]; vectorOld = new double[dimension];
// initialisation // initialisation
assert(myParameters != NULL); assert(myParameters != NULL);
assert(vectorNew != NULL); assert(vectorNew != NULL);
assert(vectorOld != NULL); assert(vectorOld != NULL);
for ( unsigned int i = 0; i < 4; i++ ) { for ( unsigned int i = 0; i < numberOfParameters; i++ ) {
myParameters[i] = 0.0; 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; vectorNew[i] = vectorOld[i] = 1.0;
} }
} }
// the main function // the main function
void Lorenz::iterate() { void Lorenz3D::iterate() {
swap(vectorNew, vectorOld); swap(vectorNew, vectorOld);
vectorNew[0] = vectorOld[0] + myParameters[0] * myParameters[1] * (vectorOld[1] - vectorOld[0]); 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 // setters, getters, all i/o to other classes/objects
void * Lorenz::getProperty(const string identifier) { void * Lorenz3D::getProperty(const string identifier) {
if ( identifier == "dimension" ) { if ( identifier == "dimension" ) {
unsigned int * _return = new unsigned int; unsigned int * _return = new unsigned int;
*_return = 3; *_return = dimension;
return _return; return _return;
} }
return NULL; return NULL;
} }
void Lorenz::setProperty(const string identifier, const void * value) { void Lorenz3D::setProperty(const string identifier, const void * value) {
return; return;
} }
double * & Lorenz::parameters() { double * & Lorenz3D::parameters() {
return myParameters; return myParameters;
} }
double & Lorenz::parameter(const unsigned int index) { double & Lorenz3D::parameter(const unsigned int index) {
return myParameters[index]; return myParameters[index];
} }
double * & Lorenz::vector() { unsigned int Lorenz3D::getNumberOfParameters() {
return numberOfParameters;
}
double * & Lorenz3D::vector() {
return vectorNew; return vectorNew;
} }
double * & Lorenz::previousVector() { double * & Lorenz3D::previousVector() {
return vectorOld; return vectorOld;
} }

8
kernels/Lorenz3D.hpp

@ -9,7 +9,7 @@ using namespace std;
#include "../AttractorKernel.hpp" #include "../AttractorKernel.hpp"
class Lorenz : public AttractorKernel { class Lorenz3D : public AttractorKernel {
double * myParameters; double * myParameters;
@ -20,14 +20,16 @@ class Lorenz : public AttractorKernel {
public: public:
Lorenz(); Lorenz3D();
Lorenz(const unsigned int dimensions); Lorenz3D(const unsigned int dimensions);
// parameters are stored in a array of doubles // parameters are stored in a array of doubles
// if you want to use other types, use the properties // if you want to use other types, use the properties
virtual double& parameter(const unsigned int index); virtual double& parameter(const unsigned int index);
virtual double*& parameters(); virtual double*& parameters();
virtual unsigned int getNumberOfParameters();
// get properties of the attractor // get properties of the attractor
// such as the dimension // such as the dimension
// you should delete the void pointer if you used it // you should delete the void pointer if you used it

4
kernels/Polynomial.cpp

@ -128,6 +128,10 @@ double & Polynomial::parameter(const unsigned int index) {
return myParameters[index]; return myParameters[index];
} }
unsigned int Polynomial::getNumberOfParameters() {
return numberOfParameters;
}
double * & Polynomial::vector() { double * & Polynomial::vector() {
return vectorNew; return vectorNew;
} }

2
kernels/Polynomial.hpp

@ -30,6 +30,8 @@ class Polynomial : public AttractorKernel {
virtual double& parameter(const unsigned int index); virtual double& parameter(const unsigned int index);
virtual double*& parameters(); virtual double*& parameters();
virtual unsigned int getNumberOfParameters();
// get properties of the attractor // get properties of the attractor
// such as the dimension // such as the dimension
// you should delete the void pointer if you used it // you should delete the void pointer if you used it

4
kernels/PolynomialA3D.cpp

@ -68,6 +68,10 @@ double & PolynomialA3D::parameter(const unsigned int index) {
return myParameters[index]; return myParameters[index];
} }
unsigned int PolynomialA3D::getNumberOfParameters() {
return numberOfParameters;
}
double * & PolynomialA3D::vector() { double * & PolynomialA3D::vector() {
return vectorNew; return vectorNew;
} }

2
kernels/PolynomialA3D.hpp

@ -24,6 +24,8 @@ class PolynomialA3D : public AttractorKernel {
virtual double& parameter(const unsigned int index); virtual double& parameter(const unsigned int index);
virtual double*& parameters(); virtual double*& parameters();
virtual unsigned int getNumberOfParameters();
// get properties of the attractor // get properties of the attractor
// such as the dimension // such as the dimension
// you should delete the void pointer if you used it // you should delete the void pointer if you used it

37
kernels/Unravel3D.cpp

@ -1,28 +1,31 @@
#include "Unravel3D.hpp" #include "Unravel3D.hpp"
Unravel::Unravel(){ const static unsigned int dimension = 3;
const static unsigned int numberOfParameters = 7;
Unravel3D::Unravel3D(){
init(); init();
} }
void Unravel::init() { void Unravel3D::init() {
// allocation // allocation
myParameters = new double[4]; myParameters = new double[numberOfParameters];
vectorNew = new double[3]; vectorNew = new double[dimension];
vectorOld = new double[3]; vectorOld = new double[dimension];
// initialisation // initialisation
assert(myParameters != NULL); assert(myParameters != NULL);
assert(vectorNew != NULL); assert(vectorNew != NULL);
assert(vectorOld != NULL); assert(vectorOld != NULL);
for ( unsigned int i = 0; i < 7; i++ ) { for ( unsigned int i = 0; i < numberOfParameters; i++ ) {
myParameters[i] = 0.0; 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; vectorNew[i] = vectorOld[i] = 0.0;
} }
} }
void Unravel::iterate() { void Unravel3D::iterate() {
swap(vectorNew, vectorOld); swap(vectorNew, vectorOld);
vectorNew[0] = myParameters[0]*(vectorOld[2] + myParameters[1]); vectorNew[0] = myParameters[0]*(vectorOld[2] + myParameters[1]);
@ -41,31 +44,35 @@ void Unravel::iterate() {
} }
// setters, getters, all i/o to other classes/objects // setters, getters, all i/o to other classes/objects
void * Unravel::getProperty(const string identifier) { void * Unravel3D::getProperty(const string identifier) {
if ( identifier == "dimension" ) { if ( identifier == "dimension" ) {
unsigned int * _return = new unsigned int; unsigned int * _return = new unsigned int;
*_return = 3; *_return = dimension;
return _return; return _return;
} }
return NULL; return NULL;
} }
void Unravel::setProperty(const string identifier, const void * value) { void Unravel3D::setProperty(const string identifier, const void * value) {
return; return;
} }
double * & Unravel::parameters() { double * & Unravel3D::parameters() {
return myParameters; return myParameters;
} }
double & Unravel::parameter(const unsigned int index) { double & Unravel3D::parameter(const unsigned int index) {
return myParameters[index]; return myParameters[index];
} }
double * & Unravel::vector() { unsigned int Unravel3D::getNumberOfParameters() {
return numberOfParameters;
}
double * & Unravel3D::vector() {
return vectorNew; return vectorNew;
} }
double * & Unravel::previousVector() { double * & Unravel3D::previousVector() {
return vectorOld; return vectorOld;
} }

8
kernels/Unravel3D.hpp

@ -10,7 +10,7 @@ using namespace std;
#include "../AttractorKernel.hpp" #include "../AttractorKernel.hpp"
class Unravel : public AttractorKernel { class Unravel3D : public AttractorKernel {
double * myParameters; double * myParameters;
@ -21,14 +21,16 @@ class Unravel : public AttractorKernel {
public: public:
Unravel(); Unravel3D();
Unravel(const unsigned int dimensions); Unravel3D(const unsigned int dimensions);
// parameters are stored in a array of doubles // parameters are stored in a array of doubles
// if you want to use other types, use the properties // if you want to use other types, use the properties
virtual double& parameter(const unsigned int index); virtual double& parameter(const unsigned int index);
virtual double*& parameters(); virtual double*& parameters();
virtual unsigned int getNumberOfParameters();
// get properties of the attractor // get properties of the attractor
// such as the dimension // such as the dimension
// you should delete the void pointer if you used it // you should delete the void pointer if you used it

10
main.cpp

@ -11,9 +11,9 @@ using namespace std;
// TODO : Allemaal files inlezen, voor makkelijker gebruik // TODO : Allemaal files inlezen, voor makkelijker gebruik
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
/*
AttractorKernel * mijnAttractortje; AttractorKernel * mijnAttractortje;
mijnAttractortje = new Lorenz(); mijnAttractortje = new Lorenz3D();
AttractorKernel &attractor = *mijnAttractortje; AttractorKernel &attractor = *mijnAttractortje;
attractor.parameter(0) = 1.0; attractor.parameter(0) = 1.0;
@ -35,7 +35,13 @@ int main(int argc, char *argv[]) {
} }
cout << endl; cout << endl;
} }
*/
Attractor myAttractor("attractors/testAttractor.stf");
for ( unsigned int i = 0; i < 20; i++ ) {
myAttractor.iterate();
myAttractor.output();
}
/*if ( argc <= 2 ) { /*if ( argc <= 2 ) {
cout << endl << "nothing to do..." << endl; cout << endl << "nothing to do..." << endl;